Add Command Execution test

This commit is contained in:
tueem 2024-11-01 17:38:02 +01:00
parent bbeb58e5e4
commit 11fd16fa77
No known key found for this signature in database
GPG Key ID: 819A0F7C36B9CF07
4 changed files with 284 additions and 9 deletions

@ -2,24 +2,52 @@ package net.tomatentum.marinara.test;
import org.javacord.api.DiscordApi;
import org.javacord.api.DiscordApiBuilder;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.api.TestInstance.Lifecycle;
import net.tomatentum.marinara.Marinara;
import net.tomatentum.marinara.test.mocks.SlashCommandInteractionMock;
import net.tomatentum.marinara.wrapper.LibraryWrapper;
import net.tomatentum.marinare.wrapper.javacord.JavacordWrapper;
@TestInstance(Lifecycle.PER_CLASS)
public class SlashCommandTest {
String DISCORD_TOKEN = System.getenv("DISCORD_TEST_TOKEN");
DiscordApi api;
@Test
public void testSlashCommand() {
DiscordApi api = new DiscordApiBuilder()
@BeforeAll
void setUp() {
api = new DiscordApiBuilder()
.setToken(DISCORD_TOKEN)
.login().join();
}
@AfterAll
void tearDown() {
api.disconnect();
api = null;
}
@Test
void testSlashCommand() {
Marinara marinara = Marinara.load(new JavacordWrapper(api));
marinara.getRegistry().addInteractions(new TestCommand());
marinara.getRegistry().registerCommands();
System.out.println("done");
System.out.println("Success!");
}
@Test
void testSlashCommandExecution() {
LibraryWrapper wrapper = new JavacordWrapper(api);
Marinara marinara = Marinara.load(wrapper);
marinara.getRegistry().addInteractions(new TestCommand());
wrapper.handleInteraction(new SlashCommandInteractionMock());
}
}

@ -1,5 +1,7 @@
package net.tomatentum.marinara.test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.javacord.api.interaction.SlashCommandInteraction;
import net.tomatentum.marinara.interaction.InteractionHandler;
@ -16,13 +18,14 @@ public class TestCommand implements InteractionHandler {
},
options = {
@SlashCommandOption(
name = "pommes",
description = "mit Fett",
type = SlashCommandOptionType.MENTIONABLE
name = "foo",
description = "foo bar is very fooby",
type = SlashCommandOptionType.STRING
)
}
)
public void exec(SlashCommandInteraction interaction) {
public void exec(SlashCommandInteraction interaction, String test) {
assertEquals(test, "test");
System.out.println("Success!");
}
}

@ -0,0 +1,147 @@
package net.tomatentum.marinara.test.mocks;
import java.util.Arrays;
import java.util.Collections;
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.DiscordLocale;
import org.javacord.api.interaction.InteractionType;
import org.javacord.api.interaction.SlashCommandInteraction;
import org.javacord.api.interaction.SlashCommandInteractionOption;
import org.javacord.api.interaction.callback.InteractionFollowupMessageBuilder;
import org.javacord.api.interaction.callback.InteractionImmediateResponseBuilder;
import org.javacord.api.interaction.callback.InteractionOriginalResponseUpdater;
public class SlashCommandInteractionMock implements SlashCommandInteraction{
@Override
public long getCommandId() {
throw new UnsupportedOperationException("Unimplemented method 'getCommandId'");
}
@Override
public String getCommandIdAsString() {
throw new UnsupportedOperationException("Unimplemented method 'getCommandIdAsString'");
}
@Override
public String getCommandName() {
return "test";
}
@Override
public Optional<Long> getRegisteredCommandServerId() {
throw new UnsupportedOperationException("Unimplemented method 'getRegisteredCommandServerId'");
}
@Override
public long getApplicationId() {
throw new UnsupportedOperationException("Unimplemented method 'getApplicationId'");
}
@Override
public InteractionType getType() {
return InteractionType.APPLICATION_COMMAND;
}
@Override
public InteractionImmediateResponseBuilder createImmediateResponder() {
throw new UnsupportedOperationException("Unimplemented method 'createImmediateResponder'");
}
@Override
public CompletableFuture<InteractionOriginalResponseUpdater> respondLater() {
throw new UnsupportedOperationException("Unimplemented method 'respondLater'");
}
@Override
public CompletableFuture<InteractionOriginalResponseUpdater> respondLater(boolean ephemeral) {
throw new UnsupportedOperationException("Unimplemented method 'respondLater'");
}
@Override
public CompletableFuture<Void> respondWithModal(String customId, String title,
List<HighLevelComponent> components) {
throw new UnsupportedOperationException("Unimplemented method 'respondWithModal'");
}
@Override
public InteractionFollowupMessageBuilder createFollowupMessageBuilder() {
throw new UnsupportedOperationException("Unimplemented method 'createFollowupMessageBuilder'");
}
@Override
public Optional<Server> getServer() {
throw new UnsupportedOperationException("Unimplemented method 'getServer'");
}
@Override
public Optional<TextChannel> getChannel() {
throw new UnsupportedOperationException("Unimplemented method 'getChannel'");
}
@Override
public User getUser() {
throw new UnsupportedOperationException("Unimplemented method 'getUser'");
}
@Override
public String getToken() {
throw new UnsupportedOperationException("Unimplemented method 'getToken'");
}
@Override
public int getVersion() {
throw new UnsupportedOperationException("Unimplemented method 'getVersion'");
}
@Override
public DiscordLocale getLocale() {
throw new UnsupportedOperationException("Unimplemented method 'getLocale'");
}
@Override
public Optional<DiscordLocale> getServerLocale() {
throw new UnsupportedOperationException("Unimplemented method 'getServerLocale'");
}
@Override
public Optional<EnumSet<PermissionType>> getBotPermissions() {
throw new UnsupportedOperationException("Unimplemented method 'getBotPermissions'");
}
@Override
public DiscordApi getApi() {
throw new UnsupportedOperationException("Unimplemented method 'getApi'");
}
@Override
public long getId() {
throw new UnsupportedOperationException("Unimplemented method 'getId'");
}
@Override
public List<SlashCommandInteractionOption> getOptions() {
return Arrays.asList(new SlashCommandInteractionOptionMock());
}
@Override
public List<SlashCommandInteractionOption> getArguments() {
return Arrays.asList(new SlashCommandInteractionOptionMock());
}
@Override
public String getFullCommandName() {
return "test";
}
}

@ -0,0 +1,97 @@
package net.tomatentum.marinara.test.mocks;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import org.javacord.api.entity.Attachment;
import org.javacord.api.entity.Mentionable;
import org.javacord.api.entity.channel.ServerChannel;
import org.javacord.api.entity.permission.Role;
import org.javacord.api.entity.user.User;
import org.javacord.api.interaction.SlashCommandInteractionOption;
public class SlashCommandInteractionOptionMock implements SlashCommandInteractionOption{
@Override
public List<SlashCommandInteractionOption> getArguments() {
return Collections.emptyList();
}
@Override
public String getName() {
return "foo";
}
@Override
public Optional<Boolean> isFocused() {
throw new UnsupportedOperationException("Unimplemented method 'isFocused'");
}
@Override
public Optional<String> getStringRepresentationValue() {
throw new UnsupportedOperationException("Unimplemented method 'getStringRepresentationValue'");
}
@Override
public Optional<String> getStringValue() {
return Optional.of("test");
}
@Override
public Optional<Long> getLongValue() {
throw new UnsupportedOperationException("Unimplemented method 'getLongValue'");
}
@Override
public Optional<Boolean> getBooleanValue() {
throw new UnsupportedOperationException("Unimplemented method 'getBooleanValue'");
}
@Override
public Optional<User> getUserValue() {
throw new UnsupportedOperationException("Unimplemented method 'getUserValue'");
}
@Override
public Optional<CompletableFuture<User>> requestUserValue() {
throw new UnsupportedOperationException("Unimplemented method 'requestUserValue'");
}
@Override
public Optional<ServerChannel> getChannelValue() {
throw new UnsupportedOperationException("Unimplemented method 'getChannelValue'");
}
@Override
public Optional<Attachment> getAttachmentValue() {
throw new UnsupportedOperationException("Unimplemented method 'getAttachmentValue'");
}
@Override
public Optional<Role> getRoleValue() {
throw new UnsupportedOperationException("Unimplemented method 'getRoleValue'");
}
@Override
public Optional<Mentionable> getMentionableValue() {
throw new UnsupportedOperationException("Unimplemented method 'getMentionableValue'");
}
@Override
public Optional<Double> getDecimalValue() {
throw new UnsupportedOperationException("Unimplemented method 'getDecimalValue'");
}
@Override
public Optional<CompletableFuture<Mentionable>> requestMentionableValue() {
throw new UnsupportedOperationException("Unimplemented method 'requestMentionableValue'");
}
@Override
public List<SlashCommandInteractionOption> getOptions() {
return Collections.emptyList();
}
}