Skip to content

Commit

Permalink
feat: refactor help messages
Browse files Browse the repository at this point in the history
  • Loading branch information
Lauraducky committed Apr 17, 2023
1 parent e474c5c commit 4cd44ef
Show file tree
Hide file tree
Showing 22 changed files with 218 additions and 287 deletions.
92 changes: 39 additions & 53 deletions src/PockyBot.NET.Tests/Services/Triggers/HelpTests.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using GlobalX.ChatBots.Core.Messages;
Expand Down Expand Up @@ -25,6 +26,7 @@ public class HelpTests
private const string SenderId = "testSender";
private readonly List<UserRole> noRoles = new List<UserRole>();
private readonly List<UserRole> adminRole = new List<UserRole>{new UserRole{Role = Role.Admin}};
private readonly List<ITrigger> triggers = new List<ITrigger>();

private Message _message;
private Message _result;
Expand All @@ -38,13 +40,14 @@ public HelpTests()
});
_pockyUserRepository = Substitute.For<IPockyUserRepository>();
_configRepository = Substitute.For<IConfigRepository>();
_subject = new Help(_pockyUserRepository, pockyUserSettings, _configRepository);
_subject = new Help(_pockyUserRepository, pockyUserSettings, _configRepository, triggers);
}

[Fact]
public void ItShouldShowAListOfCommandsToANonAdminUser()
{
this.Given(x => GivenAHelpMessage(""))
.And(x => GivenAListOfTriggers())
.And(x => GivenTheUserHasRoles(noRoles))
.When(x => WhenRespondIsCalled())
.Then(x => ThenItShouldShowAListOfNonAdminCommands())
Expand All @@ -55,6 +58,7 @@ public void ItShouldShowAListOfCommandsToANonAdminUser()
public void ItShouldShowAListOfCommandsToANonAdminUserInADirectMessage()
{
this.Given(x => GivenADirectMessageHelpMessage(""))
.And(x => GivenAListOfTriggers())
.And(x => GivenTheUserHasRoles(noRoles))
.When(x => WhenRespondIsCalled())
.Then(x => ThenItShouldShowAListOfNonAdminCommands())
Expand All @@ -65,6 +69,7 @@ public void ItShouldShowAListOfCommandsToANonAdminUserInADirectMessage()
public void ItShouldShowAListOfAdminCommandsToAnAdminUser()
{
this.Given(x => GivenAHelpMessage(""))
.And(x => GivenAListOfTriggers())
.And(x => GivenTheUserHasRoles(adminRole))
.When(x => WhenRespondIsCalled())
.Then(x => ThenItShouldShowAListOfAdminCommands())
Expand All @@ -75,6 +80,7 @@ public void ItShouldShowAListOfAdminCommandsToAnAdminUser()
public void ItShouldShowTheDefaultHelpMessageWhenTheCommandDoesNotExist()
{
this.Given(x => GivenAHelpMessage("bogusCommand"))
.And(x => GivenAListOfTriggers())
.And(x => GivenTheUserHasRoles(noRoles))
.When(x => WhenRespondIsCalled())
.Then(x => ThenItShouldShowTheDefaultHelpMessage())
Expand All @@ -85,6 +91,7 @@ public void ItShouldShowTheDefaultHelpMessageWhenTheCommandDoesNotExist()
public void ItShouldShowTheDefaultHelpMessageWhenTheCommandDoesNotExistInADirectMessage()
{
this.Given(x => GivenADirectMessageHelpMessage("bogusCommand"))
.And(x => GivenAListOfTriggers())
.And(x => GivenTheUserHasRoles(noRoles))
.When(x => WhenRespondIsCalled())
.Then(x => ThenItShouldShowTheDefaultHelpMessage())
Expand All @@ -94,14 +101,10 @@ public void ItShouldShowTheDefaultHelpMessageWhenTheCommandDoesNotExistInADirect
[Theory]
[InlineData(Commands.Peg)]
[InlineData(Commands.Status)]
[InlineData(Commands.Keywords)]
[InlineData(Commands.Ping)]
[InlineData(Commands.Welcome)]
[InlineData(Commands.Rotation)]
[InlineData(Commands.LocationConfig)]
public void ItShouldShowTheHelpMessageForNonAdminCommands(string command)
{
this.Given(x => GivenAHelpMessage(command))
.And(x => GivenAListOfTriggers())
.And(x => GivenTheUserHasRoles(noRoles))
.When(x => WhenRespondIsCalled())
.Then(x => ThenItShouldShowTheCommandHelpMessage(command))
Expand All @@ -111,14 +114,10 @@ public void ItShouldShowTheHelpMessageForNonAdminCommands(string command)
[Theory]
[InlineData(Commands.Peg)]
[InlineData(Commands.Status)]
[InlineData(Commands.Keywords)]
[InlineData(Commands.Ping)]
[InlineData(Commands.Welcome)]
[InlineData(Commands.Rotation)]
[InlineData(Commands.LocationConfig)]
public void ItShouldShowTheHelpMessageForNonAdminCommandsInADirectMessage(string command)
{
this.Given(x => GivenADirectMessageHelpMessage(command))
.And(x => GivenAListOfTriggers())
.And(x => GivenTheUserHasRoles(noRoles))
.When(x => WhenRespondIsCalled())
.Then(x => ThenItShouldShowTheCommandHelpMessage(command))
Expand All @@ -130,21 +129,10 @@ public void ItShouldShowTheHelpMessageForNonAdminCommandsInADirectMessage(string
[InlineData(Commands.Finish, Role.Admin)]
[InlineData(Commands.Reset, Role.Reset)]
[InlineData(Commands.Reset, Role.Admin)]
[InlineData(Commands.StringConfig, Role.Admin)]
[InlineData(Commands.StringConfig, Role.Config)]
[InlineData(Commands.RoleConfig, Role.Admin)]
[InlineData(Commands.RoleConfig, Role.Config)]
[InlineData(Commands.LocationWeight, Role.Admin)]
[InlineData(Commands.LocationWeight, Role.Config)]
[InlineData(Commands.RemoveUser, Role.Admin)]
[InlineData(Commands.RemoveUser, Role.RemoveUser)]
[InlineData(Commands.Results, Role.Admin)]
[InlineData(Commands.Results, Role.Results)]
[InlineData(Commands.NumberConfig, Role.Admin)]
[InlineData(Commands.NumberConfig, Role.Config)]
internal void ItShouldShowTheHelpMessageForAdminCommandsToAdminUsers(string command, Role userRole)
{
this.Given(x => GivenAHelpMessage(command))
.And(x => GivenAListOfTriggers())
.And(x => GivenTheUserHasRoles(new List<UserRole>{new UserRole{Role = userRole}}))
.When(x => WhenRespondIsCalled())
.Then(x => ThenItShouldShowTheCommandHelpMessage(command))
Expand All @@ -154,15 +142,10 @@ internal void ItShouldShowTheHelpMessageForAdminCommandsToAdminUsers(string comm
[Theory]
[InlineData(Commands.Finish)]
[InlineData(Commands.Reset)]
[InlineData(Commands.StringConfig)]
[InlineData(Commands.RoleConfig)]
[InlineData(Commands.LocationWeight)]
[InlineData(Commands.RemoveUser)]
[InlineData(Commands.Results)]
[InlineData(Commands.NumberConfig)]
public void ItShouldShowTheDefaultHelpMessageForAdminCommandsToNonAdminUsers(string command)
{
this.Given(x => GivenAHelpMessage(command))
.And(x => GivenAListOfTriggers())
.And(x => GivenTheUserHasRoles(noRoles))
.When(x => WhenRespondIsCalled())
.Then(x => ThenItShouldShowTheDefaultHelpMessage())
Expand Down Expand Up @@ -213,6 +196,33 @@ private void GivenADirectMessageHelpMessage(string command)
};
}

private void GivenAListOfTriggers()
{
var peg = Substitute.For<ITrigger>();
peg.Command.Returns(Commands.Peg);
peg.Permissions.Returns(Array.Empty<Role>());
peg.GetHelpMessage(BotName, Arg.Any<PockyUser>()).Returns($"@{BotName} {Commands.Peg}");
triggers.Add(peg);

var status = Substitute.For<ITrigger>();
status.Command.Returns(Commands.Status);
status.Permissions.Returns(Array.Empty<Role>());
status.GetHelpMessage(BotName, Arg.Any<PockyUser>()).Returns($"@{BotName} {Commands.Status}");
triggers.Add(status);

var reset = Substitute.For<ITrigger>();
reset.Command.Returns(Commands.Reset);
reset.Permissions.Returns(new [] { Role.Reset, Role.Admin });
reset.GetHelpMessage(BotName, Arg.Any<PockyUser>()).Returns($"@{BotName} {Commands.Reset}");
triggers.Add(reset);

var finish = Substitute.For<ITrigger>();
finish.Command.Returns(Commands.Finish);
finish.Permissions.Returns(new [] { Role.Finish, Role.Admin });
finish.GetHelpMessage(BotName, Arg.Any<PockyUser>()).Returns($"@{BotName} {Commands.Finish}");
triggers.Add(finish);
}

private void GivenTheUserHasRoles(List<UserRole> roles)
{
_pockyUserRepository.GetUser(SenderId).Returns(new PockyUser
Expand All @@ -232,44 +242,20 @@ private void ThenItShouldShowAListOfNonAdminCommands()
_result.Text.ShouldStartWith("## What I can do (List of Commands)");
_result.Text.ShouldContain($"* {Commands.Peg}");
_result.Text.ShouldContain($"* {Commands.Status}");
_result.Text.ShouldContain($"* {Commands.Keywords}");
_result.Text.ShouldContain($"* {Commands.Ping}");
_result.Text.ShouldContain($"* {Commands.Welcome}");
_result.Text.ShouldContain($"* {Commands.Rotation}");
_result.Text.ShouldContain($"* {Commands.LocationConfig}");
_result.Text.ShouldContain($"* {Commands.UserLocation}");
_result.Text.ShouldContain($"For more information on a command type `@{BotName} help command-name` or direct message me with `help command-name`");
_result.Text.ShouldContain("I am still being worked on, so more features to come.");

_result.Text.ShouldNotContain($"* {Commands.Reset}");
_result.Text.ShouldNotContain($"* {Commands.Finish}");
_result.Text.ShouldNotContain($"* {Commands.StringConfig}");
_result.Text.ShouldNotContain($"* {Commands.RoleConfig}");
_result.Text.ShouldNotContain($"* {Commands.LocationWeight}");
_result.Text.ShouldNotContain($"* {Commands.RemoveUser}");
_result.Text.ShouldNotContain($"* {Commands.Results}");
_result.Text.ShouldNotContain($"* {Commands.NumberConfig}");
}

private void ThenItShouldShowAListOfAdminCommands()
{
_result.Text.ShouldStartWith("## What I can do (List of Commands)");
_result.Text.ShouldContain($"* {Commands.Peg}");
_result.Text.ShouldContain($"* {Commands.Status}");
_result.Text.ShouldContain($"* {Commands.Keywords}");
_result.Text.ShouldContain($"* {Commands.Ping}");
_result.Text.ShouldContain($"* {Commands.Welcome}");
_result.Text.ShouldContain($"* {Commands.Rotation}");
_result.Text.ShouldContain($"* {Commands.LocationConfig}");
_result.Text.ShouldContain($"* {Commands.UserLocation}");
_result.Text.ShouldContain($"* {Commands.Reset}");
_result.Text.ShouldContain($"* {Commands.Finish}");
_result.Text.ShouldContain($"* {Commands.StringConfig}");
_result.Text.ShouldContain($"* {Commands.RoleConfig}");
_result.Text.ShouldContain($"* {Commands.LocationWeight}");
_result.Text.ShouldContain($"* {Commands.RemoveUser}");
_result.Text.ShouldContain($"* {Commands.Results}");
_result.Text.ShouldContain($"* {Commands.NumberConfig}");
_result.Text.ShouldContain($"For more information on a command type `@{BotName} help command-name` or direct message me with `help command-name`");
_result.Text.ShouldContain("I am still being worked on, so more features to come.");
}
Expand Down
2 changes: 1 addition & 1 deletion src/PockyBot.NET/PockyBotFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public static IPockyBot CreatePockyBot(PockyBotSettings settings, IChatHelper ch
var triggers = new List<ITrigger>
{
new Ping(),
new Help(pockyUserRepository, wrappedSettings, configRepository),
new Help(pockyUserRepository, wrappedSettings, configRepository, null),
new Welcome(wrappedSettings, configRepository),
new Peg(pegRequestValidator, pockyUserRepository, pegHelper, configRepository, chatHelper, pegGiver, loggerFactory.CreateLogger<Peg>()),
new Status(pockyUserRepository, configRepository, pegHelper, loggerFactory.CreateLogger<Status>()),
Expand Down
5 changes: 5 additions & 0 deletions src/PockyBot.NET/Services/Triggers/Default.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,10 @@ 1. For a full list of commands type `@{_settings.BotName} help`.
I am still being worked on, so more features to come : )"
});
}

public string GetHelpMessage(string botName, PockyUser user)
{
return string.Empty;
}
}
}
7 changes: 7 additions & 0 deletions src/PockyBot.NET/Services/Triggers/Finish.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,12 @@ public async Task<Message> Respond(Message message)
Text = $"[Here are all pegs given this cycle]({uri})"
};
}

public string GetHelpMessage(string botName, PockyUser user)
{
return "### How to complete the cycle 🚲!\n" +
$"1. To display winners and results and clear the database, type `@{botName} {Commands.Finish}`.\n" +
"1. I will respond in the room you messaged me in.";
}
}
}
Loading

0 comments on commit 4cd44ef

Please sign in to comment.