|
| 1 | +package org.togetherjava.tjbot.commands.tags; |
| 2 | + |
| 3 | +import net.dv8tion.jda.api.entities.Member; |
| 4 | +import net.dv8tion.jda.api.entities.MessageEmbed; |
| 5 | +import net.dv8tion.jda.api.events.interaction.SlashCommandEvent; |
| 6 | +import org.jetbrains.annotations.NotNull; |
| 7 | +import org.jetbrains.annotations.Nullable; |
| 8 | +import org.junit.jupiter.api.BeforeEach; |
| 9 | +import org.junit.jupiter.api.DisplayName; |
| 10 | +import org.junit.jupiter.api.Test; |
| 11 | +import org.togetherjava.tjbot.commands.SlashCommand; |
| 12 | +import org.togetherjava.tjbot.db.Database; |
| 13 | +import org.togetherjava.tjbot.db.generated.tables.Tags; |
| 14 | +import org.togetherjava.tjbot.jda.JdaTester; |
| 15 | +import org.togetherjava.tjbot.jda.SlashCommandEventBuilder; |
| 16 | + |
| 17 | +import static org.mockito.Mockito.*; |
| 18 | + |
| 19 | +final class TagCommandTest { |
| 20 | + private TagSystem system; |
| 21 | + private JdaTester jdaTester; |
| 22 | + private SlashCommand command; |
| 23 | + |
| 24 | + @BeforeEach |
| 25 | + void setUp() { |
| 26 | + Database database = Database.createMemoryDatabase(Tags.TAGS); |
| 27 | + system = spy(new TagSystem(database)); |
| 28 | + jdaTester = new JdaTester(); |
| 29 | + command = new TagCommand(system); |
| 30 | + } |
| 31 | + |
| 32 | + private @NotNull SlashCommandEvent triggerSlashCommand(@NotNull String id, |
| 33 | + @Nullable Member userToReplyTo) { |
| 34 | + SlashCommandEventBuilder builder = |
| 35 | + jdaTester.createSlashCommandEvent(command).setOption(TagCommand.ID_OPTION, id); |
| 36 | + if (userToReplyTo != null) { |
| 37 | + builder.setOption(TagCommand.REPLY_TO_USER_OPTION, userToReplyTo); |
| 38 | + } |
| 39 | + |
| 40 | + SlashCommandEvent event = builder.build(); |
| 41 | + command.onSlashCommand(event); |
| 42 | + return event; |
| 43 | + } |
| 44 | + |
| 45 | + @Test |
| 46 | + @DisplayName("Respond that the tag could not be found if the system has no tags registered yet") |
| 47 | + void canNotFindTagInEmptySystem() { |
| 48 | + // GIVEN a system without any tags registered |
| 49 | + // WHEN triggering the slash command '/tag id:first' |
| 50 | + SlashCommandEvent event = triggerSlashCommand("first", null); |
| 51 | + |
| 52 | + // THEN responds that the tag could not be found |
| 53 | + verify(event).reply("Could not find any tag with id 'first'."); |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + @DisplayName("Respond that the tag could not be found but suggest a different tag instead, if the system has a different tag registered") |
| 58 | + void canNotFindTagSuggestDifferentTag() { |
| 59 | + // GIVEN a system with the tag "first" registered |
| 60 | + system.putTag("first", "foo"); |
| 61 | + |
| 62 | + // WHEN triggering the slash command '/tag id:second' |
| 63 | + SlashCommandEvent event = triggerSlashCommand("second", null); |
| 64 | + |
| 65 | + // THEN responds that the tag could not be found and instead suggests using the other tag |
| 66 | + verify(event) |
| 67 | + .reply("Could not find any tag with id 'second', did you perhaps mean 'first'?"); |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + @DisplayName("Respond with the tags content if the tag could be found") |
| 72 | + void canFindTheTagAndRespondWithContent() { |
| 73 | + // GIVEN a system with the tag "first" registered |
| 74 | + system.putTag("first", "foo"); |
| 75 | + |
| 76 | + // WHEN triggering the slash command '/tag id:first' |
| 77 | + SlashCommandEvent event = triggerSlashCommand("first", null); |
| 78 | + |
| 79 | + // THEN finds the tag and responds with its content |
| 80 | + verify(event).replyEmbeds(any(MessageEmbed.class)); |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + @DisplayName("Replies to given users and responds with the tags content if the tag could be found and a user is given") |
| 85 | + void canFindTagsAndRepliesToUser() { |
| 86 | + // GIVEN a system with the tag "first" registered and a user to reply to |
| 87 | + system.putTag("first", "foo"); |
| 88 | + Member userToReplyTo = jdaTester.createMemberSpy(1); |
| 89 | + |
| 90 | + // WHEN triggering the slash command '/tag id:first reply-to:...' with that user |
| 91 | + SlashCommandEvent event = triggerSlashCommand("first", userToReplyTo); |
| 92 | + |
| 93 | + // THEN responds with the tags content and replies to the user |
| 94 | + verify(event).replyEmbeds(any(MessageEmbed.class)); |
| 95 | + verify(jdaTester.getReplyActionMock()).setContent(userToReplyTo.getAsMention()); |
| 96 | + } |
| 97 | +} |
0 commit comments