From 9d3a6b8b8501f311b6d30355d62500287e9b3792 Mon Sep 17 00:00:00 2001 From: Tueem Date: Sun, 15 Dec 2024 23:15:37 +0100 Subject: [PATCH] add Autocomplete Test --- .../marinara/test/AutoCompleteTest.java | 23 +++ .../marinara/test/TestAutocomplete.java | 23 +++ .../mocks/AutocompleteInteractionMock.java | 179 ++++++++++++++++++ .../SlashCommandInteractionOptionMock.java | 20 +- 4 files changed, 235 insertions(+), 10 deletions(-) create mode 100644 wrapper/javacord/src/test/java/net/tomatentum/marinara/test/AutoCompleteTest.java create mode 100644 wrapper/javacord/src/test/java/net/tomatentum/marinara/test/TestAutocomplete.java create mode 100644 wrapper/javacord/src/test/java/net/tomatentum/marinara/test/mocks/AutocompleteInteractionMock.java diff --git a/wrapper/javacord/src/test/java/net/tomatentum/marinara/test/AutoCompleteTest.java b/wrapper/javacord/src/test/java/net/tomatentum/marinara/test/AutoCompleteTest.java new file mode 100644 index 0000000..443c2b4 --- /dev/null +++ b/wrapper/javacord/src/test/java/net/tomatentum/marinara/test/AutoCompleteTest.java @@ -0,0 +1,23 @@ +package net.tomatentum.marinara.test; + +import static org.junit.jupiter.api.Assertions.assertTrue; + +import org.junit.jupiter.api.Test; + +import net.tomatentum.marinara.Marinara; +import net.tomatentum.marinara.test.mocks.AutocompleteInteractionMock; +import net.tomatentum.marinara.test.mocks.DiscordApiMock; +import net.tomatentum.marinara.wrapper.LibraryWrapper; +import net.tomatentum.marinara.wrapper.javacord.JavacordWrapper; + +public class AutoCompleteTest { + + @Test + public void testAutocomplete() { + LibraryWrapper wrapper = new JavacordWrapper(new DiscordApiMock()); //null okay as we don't use the discord API in this test. + Marinara marinara = Marinara.load(wrapper); + marinara.getRegistry().addInteractions(new TestAutocomplete()); + wrapper.handleInteraction(new AutocompleteInteractionMock()); + assertTrue(AutocompleteInteractionMock.didAutocompleteRun); + } +} diff --git a/wrapper/javacord/src/test/java/net/tomatentum/marinara/test/TestAutocomplete.java b/wrapper/javacord/src/test/java/net/tomatentum/marinara/test/TestAutocomplete.java new file mode 100644 index 0000000..c38f4a2 --- /dev/null +++ b/wrapper/javacord/src/test/java/net/tomatentum/marinara/test/TestAutocomplete.java @@ -0,0 +1,23 @@ +package net.tomatentum.marinara.test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import java.util.Collections; + +import org.javacord.api.interaction.AutocompleteInteraction; + +import net.tomatentum.marinara.interaction.InteractionHandler; +import net.tomatentum.marinara.interaction.annotation.AutoComplete; +import net.tomatentum.marinara.interaction.commands.annotation.SlashCommand; + +public class TestAutocomplete implements InteractionHandler { + + @SlashCommand(name = "test") + @AutoComplete + public void autocomplete(AutocompleteInteraction context, String value) { + System.out.println("Success!"); + assertEquals(value, "test"); + context.respondWithChoices(Collections.emptyList()); + } + +} diff --git a/wrapper/javacord/src/test/java/net/tomatentum/marinara/test/mocks/AutocompleteInteractionMock.java b/wrapper/javacord/src/test/java/net/tomatentum/marinara/test/mocks/AutocompleteInteractionMock.java new file mode 100644 index 0000000..29d64b9 --- /dev/null +++ b/wrapper/javacord/src/test/java/net/tomatentum/marinara/test/mocks/AutocompleteInteractionMock.java @@ -0,0 +1,179 @@ +package net.tomatentum.marinara.test.mocks; + +import java.util.ArrayList; +import java.util.EnumSet; +import java.util.List; +import java.util.Optional; +import java.util.concurrent.CompletableFuture; + +import org.javacord.api.DiscordApi; +import org.javacord.api.entity.channel.TextChannel; +import org.javacord.api.entity.message.component.HighLevelComponent; +import org.javacord.api.entity.permission.PermissionType; +import org.javacord.api.entity.server.Server; +import org.javacord.api.entity.user.User; +import org.javacord.api.interaction.AutocompleteInteraction; +import org.javacord.api.interaction.DiscordLocale; +import org.javacord.api.interaction.InteractionType; +import org.javacord.api.interaction.SlashCommandInteractionOption; +import org.javacord.api.interaction.SlashCommandOptionChoice; +import org.javacord.api.interaction.callback.InteractionFollowupMessageBuilder; +import org.javacord.api.interaction.callback.InteractionImmediateResponseBuilder; +import org.javacord.api.interaction.callback.InteractionOriginalResponseUpdater; + +public class AutocompleteInteractionMock implements AutocompleteInteraction { + + public static boolean didAutocompleteRun = false; + + @Override + public String getFullCommandName() { + return "test"; + } + + @Override + public long getCommandId() { + // TODO Auto-generated method stub + throw new UnsupportedOperationException("Unimplemented method 'getCommandId'"); + } + + @Override + public String getCommandIdAsString() { + // TODO Auto-generated method stub + throw new UnsupportedOperationException("Unimplemented method 'getCommandIdAsString'"); + } + + @Override + public String getCommandName() { + return "test"; + } + + @Override + public Optional getRegisteredCommandServerId() { + // TODO Auto-generated method stub + throw new UnsupportedOperationException("Unimplemented method 'getRegisteredCommandServerId'"); + } + + @Override + public long getApplicationId() { + // TODO Auto-generated method stub + throw new UnsupportedOperationException("Unimplemented method 'getApplicationId'"); + } + + @Override + public InteractionType getType() { + return InteractionType.APPLICATION_COMMAND_AUTOCOMPLETE; + } + + @Override + public InteractionImmediateResponseBuilder createImmediateResponder() { + // TODO Auto-generated method stub + throw new UnsupportedOperationException("Unimplemented method 'createImmediateResponder'"); + } + + @Override + public CompletableFuture respondLater() { + // TODO Auto-generated method stub + throw new UnsupportedOperationException("Unimplemented method 'respondLater'"); + } + + @Override + public CompletableFuture respondLater(boolean ephemeral) { + // TODO Auto-generated method stub + throw new UnsupportedOperationException("Unimplemented method 'respondLater'"); + } + + @Override + public CompletableFuture respondWithModal(String customId, String title, + List components) { + // TODO Auto-generated method stub + throw new UnsupportedOperationException("Unimplemented method 'respondWithModal'"); + } + + @Override + public InteractionFollowupMessageBuilder createFollowupMessageBuilder() { + // TODO Auto-generated method stub + throw new UnsupportedOperationException("Unimplemented method 'createFollowupMessageBuilder'"); + } + + @Override + public Optional getServer() { + // TODO Auto-generated method stub + throw new UnsupportedOperationException("Unimplemented method 'getServer'"); + } + + @Override + public Optional getChannel() { + // TODO Auto-generated method stub + throw new UnsupportedOperationException("Unimplemented method 'getChannel'"); + } + + @Override + public User getUser() { + // TODO Auto-generated method stub + throw new UnsupportedOperationException("Unimplemented method 'getUser'"); + } + + @Override + public String getToken() { + // TODO Auto-generated method stub + throw new UnsupportedOperationException("Unimplemented method 'getToken'"); + } + + @Override + public int getVersion() { + // TODO Auto-generated method stub + throw new UnsupportedOperationException("Unimplemented method 'getVersion'"); + } + + @Override + public DiscordLocale getLocale() { + // TODO Auto-generated method stub + throw new UnsupportedOperationException("Unimplemented method 'getLocale'"); + } + + @Override + public Optional getServerLocale() { + // TODO Auto-generated method stub + throw new UnsupportedOperationException("Unimplemented method 'getServerLocale'"); + } + + @Override + public Optional> getBotPermissions() { + // TODO Auto-generated method stub + throw new UnsupportedOperationException("Unimplemented method 'getBotPermissions'"); + } + + @Override + public DiscordApi getApi() { + // TODO Auto-generated method stub + throw new UnsupportedOperationException("Unimplemented method 'getApi'"); + } + + @Override + public long getId() { + // TODO Auto-generated method stub + throw new UnsupportedOperationException("Unimplemented method 'getId'"); + } + + @Override + public List getOptions() { + return new ArrayList<>(); + } + + @Override + public List getArguments() { + return new ArrayList<>(); + } + + @Override + public CompletableFuture respondWithChoices(List choices) { + didAutocompleteRun = true; + return CompletableFuture.completedFuture(null); + } + + @Override + public SlashCommandInteractionOption getFocusedOption() { + return new SlashCommandInteractionOptionMock(); + } + +} diff --git a/wrapper/javacord/src/test/java/net/tomatentum/marinara/test/mocks/SlashCommandInteractionOptionMock.java b/wrapper/javacord/src/test/java/net/tomatentum/marinara/test/mocks/SlashCommandInteractionOptionMock.java index 4f077b1..b102a21 100644 --- a/wrapper/javacord/src/test/java/net/tomatentum/marinara/test/mocks/SlashCommandInteractionOptionMock.java +++ b/wrapper/javacord/src/test/java/net/tomatentum/marinara/test/mocks/SlashCommandInteractionOptionMock.java @@ -41,52 +41,52 @@ public class SlashCommandInteractionOptionMock implements SlashCommandInteractio @Override public Optional getLongValue() { - throw new UnsupportedOperationException("Unimplemented method 'getLongValue'"); + return Optional.empty(); } @Override public Optional getBooleanValue() { - throw new UnsupportedOperationException("Unimplemented method 'getBooleanValue'"); + return Optional.empty(); } @Override public Optional getUserValue() { - throw new UnsupportedOperationException("Unimplemented method 'getUserValue'"); + return Optional.empty(); } @Override public Optional> requestUserValue() { - throw new UnsupportedOperationException("Unimplemented method 'requestUserValue'"); + return Optional.empty(); } @Override public Optional getChannelValue() { - throw new UnsupportedOperationException("Unimplemented method 'getChannelValue'"); + return Optional.empty(); } @Override public Optional getAttachmentValue() { - throw new UnsupportedOperationException("Unimplemented method 'getAttachmentValue'"); + return Optional.empty(); } @Override public Optional getRoleValue() { - throw new UnsupportedOperationException("Unimplemented method 'getRoleValue'"); + return Optional.empty(); } @Override public Optional getMentionableValue() { - throw new UnsupportedOperationException("Unimplemented method 'getMentionableValue'"); + return Optional.empty(); } @Override public Optional getDecimalValue() { - throw new UnsupportedOperationException("Unimplemented method 'getDecimalValue'"); + return Optional.empty(); } @Override public Optional> requestMentionableValue() { - throw new UnsupportedOperationException("Unimplemented method 'requestMentionableValue'"); + return Optional.empty(); } @Override