Skip to content
This repository was archived by the owner on Jan 2, 2026. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
667 changes: 562 additions & 105 deletions src/main/java/dev/coly/jdat/JDAObjects.java

Large diffs are not rendered by default.

126 changes: 109 additions & 17 deletions src/main/java/dev/coly/jdat/JDATesting.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@

import dev.coly.util.Annotations;
import dev.coly.util.Callback;
import net.dv8tion.jda.api.entities.Member;
import net.dv8tion.jda.api.entities.Message;
import net.dv8tion.jda.api.entities.channel.middleman.MessageChannel;
import net.dv8tion.jda.api.entities.MessageEmbed;
import net.dv8tion.jda.api.entities.channel.middleman.MessageChannel;
import net.dv8tion.jda.api.events.interaction.command.GenericCommandInteractionEvent;
import net.dv8tion.jda.api.events.interaction.command.MessageContextInteractionEvent;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
import net.dv8tion.jda.api.hooks.EventListener;
import net.dv8tion.jda.api.hooks.SubscribeEvent;
import net.dv8tion.jda.api.interactions.commands.CommandInteraction;
import org.jetbrains.annotations.NotNull;
import org.junit.jupiter.api.Assertions;

Expand Down Expand Up @@ -118,17 +122,23 @@ public static void assertGuildMessageReceivedEvent(EventListener listener, Strin
}
}

/**
* Test a {@link EventListener} with a {@link SlashCommandInteractionEvent}.
* One can test the return to see if one's code returns the correct {@link Message} output for a given input.
*
* @param listener The {@link EventListener} that will be tested.
* @param name The name of the command.
* @param options Options for this application command.
* @return The {@link Message} the {@link EventListener} would return given the input.
* Test this if it is correct.
* @throws InterruptedException This code uses a {@link CountDownLatch} that can the interrupted if the thread
* is interrupted.
*/
public static Message testSlashCommandEvent(EventListener listener, String name, String subcommand,
String subcommandGroup, Map<String, Object> options)
String subcommandGroup, Map<String, Object> options)
throws InterruptedException {
Callback<Message> messageCallback = Callback.single();

MessageChannel channel = JDAObjects.getMessageChannel("test-chanel", 0L, messageCallback);
SlashCommandInteractionEvent event = JDAObjects.getSlashCommandInteractionEvent(channel, name, subcommand,
subcommandGroup, options, messageCallback);

listener.onEvent(event);
return messageCallback.await();
return testGenericCommandInteractionEvent(listener, name, subcommand, subcommandGroup, options,
SlashCommandInteractionEvent.class);
}

/**
Expand Down Expand Up @@ -159,12 +169,8 @@ public static void assertSlashCommandEvent(EventListener listener, String comman
public static void assertSlashCommandEvent(EventListener listener, String command, String subcommand,
String subcommandGroup, Map<String, Object> options,
String expectedOutput) {
try {
Assertions.assertEquals(testSlashCommandEvent(listener, command, subcommand, subcommandGroup, options)
.getContentRaw(), expectedOutput);
} catch (InterruptedException e){
Assertions.fail(e);
}
assertGenericCommandInteractionEvent(listener, command, subcommand, subcommandGroup, options,
SlashCommandInteractionEvent.class, expectedOutput);
}

/**
Expand Down Expand Up @@ -195,14 +201,100 @@ public static void assertSlashCommandEvent(EventListener listener, String comman
public static void assertSlashCommandEvent(EventListener listener, String command, String subcommand,
String subcommandGroup, Map<String, Object> options,
List<MessageEmbed> expectedOutputs) {
assertGenericCommandInteractionEvent(listener, command, subcommand, subcommandGroup, options,
SlashCommandInteractionEvent.class, expectedOutputs);
}

/**
* Test a {@link EventListener} with a {@link MessageContextInteractionEvent}.
* One can test the return to see if one's code returns the correct {@link Message} output for a given input.
*
* @param listener The {@link EventListener} that will be tested.
* @param name The name of the command.
* @param options Options for this application command.
* @return The {@link Message} the {@link EventListener} would return given the input.
* Test this if it is correct.
* @throws InterruptedException This code uses a {@link CountDownLatch} that can the interrupted if the thread
* is interrupted.
*/
public static Message testMessageContextInteractionEvent(EventListener listener, String name,
Map<String, Object> options) throws InterruptedException {
return testGenericCommandInteractionEvent(listener, name, null, null, options,
MessageContextInteractionEvent.class);
}

private static <T extends GenericCommandInteractionEvent> Message
testGenericCommandInteractionEvent(EventListener listener, String name, String subcommand, String subgroup,
Map<String, Object> options, Class<T> clazz)
throws InterruptedException {
Callback<Message> messageCallback = Callback.single();

MessageChannel channel = JDAObjects.getMessageChannel("test-chanel", 0L, messageCallback);
Member member = JDAObjects.getMember("member", "0000");
CommandInteraction interaction = JDAObjects.getCommandInteraction(channel, member, name, subcommand, subgroup,
options, messageCallback, Callback.single(), CommandInteraction.class);
T event = JDAObjects.getCommandInteractionEvent(interaction, clazz);

listener.onEvent(event);
return messageCallback.await();
}

private static <T extends GenericCommandInteractionEvent> void
assertGenericCommandInteractionEvent(EventListener listener, String command, String subcommand, String subgroup,
Map<String, Object> options, Class<T> type, String expectedOutput) {
try {
Assertions.assertEquals(
testGenericCommandInteractionEvent(listener, command, subcommand, subgroup, options, type)
.getContentRaw(),
expectedOutput);
} catch (InterruptedException e){
Assertions.fail(e);
}
}

private static <T extends GenericCommandInteractionEvent> void
assertGenericCommandInteractionEvent(EventListener listener, String command, String subcommand, String subgroup,
Map<String, Object> options, Class<T> type,
List<MessageEmbed> expectedOutputs) {
try {
assertEmbeds(testSlashCommandEvent(listener, command, subcommand, subcommandGroup, options),
assertEmbeds(testGenericCommandInteractionEvent(listener, command, subcommand, subgroup, options, type),
expectedOutputs);
} catch (InterruptedException e) {
Assertions.fail(e);
}
}

/**
* Test a {@link EventListener} with a {@link MessageContextInteractionEvent}. The return will be tested against the
* expectedOutput specified.
*
* @param listener The {@link EventListener} that will be tested.
* @param command The command that will be tested.
* @param options Options that will be tested.
* @param expectedOutputs A list of {@link MessageEmbed} to test against. The order is important.
*/
public static void assertMessageContextInteractionEvent(EventListener listener, String command,
Map<String, Object> options, List<MessageEmbed> expectedOutputs) {
assertGenericCommandInteractionEvent(listener, command, null, null, options,
MessageContextInteractionEvent.class, expectedOutputs);
}


/**
* Test a {@link EventListener} with a {@link MessageContextInteractionEvent}. The raw content of the return message
* will be tested against the expectedOutput specified.
*
* @param listener The {@link EventListener} that will be tested.
* @param command The command that will be tested.
* @param options Options that will be tested.
* @param expectedOutput The excepted string that {@link Message#getContentRaw()}} will have.
*/
public static void assertMessageContextInteractionEvent(EventListener listener, String command,
Map<String, Object> options, String expectedOutput) {
assertGenericCommandInteractionEvent(listener, command, null, null, options,
MessageContextInteractionEvent.class, expectedOutput);
}

private static void assertEmbeds(Message message, List<MessageEmbed> expectedOutputs) {
Assertions.assertEquals(expectedOutputs.size(), message.getEmbeds().size(), "Number of embeds");

Expand Down
108 changes: 108 additions & 0 deletions src/test/java/dev/coly/jdat/TestCommandInteractionEvent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package dev.coly.jdat;

import dev.coly.util.Callback;
import net.dv8tion.jda.api.entities.Member;
import net.dv8tion.jda.api.entities.Message;
import net.dv8tion.jda.api.entities.channel.middleman.MessageChannel;
import net.dv8tion.jda.api.events.interaction.command.MessageContextInteractionEvent;
import net.dv8tion.jda.api.interactions.commands.context.MessageContextInteraction;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

public class TestCommandInteractionEvent {

@Test
public void testTestMessageContextInteractionEvent() {
try {
Message message = JDATesting.testMessageContextInteractionEvent(new TestEventListener(), "ping", null);
Assertions.assertNotNull(message);
Assertions.assertEquals("Pong!", message.getContentRaw());
} catch (InterruptedException e) {
Assertions.fail(e);
}
}

@Test
public void testAssertMessageContextInteractionEvent() {
JDATesting.assertMessageContextInteractionEvent(new TestEventListener(), "ping", null, "Pong!");
}

@Test
public void testAssertMessageContextInteractionEventWithOptions() {
Map<String, Object> map = new HashMap<>();
map.put("bool", true);
map.put("str", "text");
map.put("number", 42L);
map.put("user", JDAObjects.getMember("User", "0000").getUser());
JDATesting.assertMessageContextInteractionEvent(new TestEventListener(), "options", map,
"bool: true - str: text - number: 42 - user: User#0000");
}

@Test
public void testAssertMessageContextInteractionEventWithEmbeds() {
JDATesting.assertMessageContextInteractionEvent(new TestEventListener(), "embed", new HashMap<>(),
new ArrayList<>(Collections.singleton(TestEventListener.getTestEmbed())));
}

@Test
public void testGetMessageContextInteractionEvent() {
Map<String, Object> options = new HashMap<>();
options.put("test", true);
MessageContextInteraction interaction = getMessageContextInteraction("slash-command", options,
Callback.single(), Callback.single());
MessageContextInteractionEvent event = JDAObjects.getMessageContextInteractionEvent(interaction);
Assertions.assertNotNull(event.getChannel());
Assertions.assertEquals("test-channel", event.getChannel().getName());
Assertions.assertEquals("slash-command", event.getName());
Assertions.assertNotNull(event.getOption("test"));
Assertions.assertTrue(Objects.requireNonNull(event.getOption("test")).getAsBoolean());
}

@Test
public void testMessageContextInteractionWithDeferReply() {
Map<String, Object> options = new HashMap<>();
options.put("test", true);
Callback<Boolean> callback = Callback.single();
MessageContextInteraction interaction = getMessageContextInteraction("defer", options,
Callback.single(), callback);
MessageContextInteractionEvent event = JDAObjects.getMessageContextInteractionEvent(interaction);
new TestEventListener().onEvent(event);
try {
Assertions.assertFalse(callback.await(100L, TimeUnit.MILLISECONDS));
} catch (InterruptedException e) {
Assertions.fail(e);
}
}

@Test
public void testMessageContextInteractionWithEphemeral() {
Callback<Message> callback = Callback.single();
MessageContextInteraction interaction = getMessageContextInteraction("ephemeral", null,
callback, Callback.single());
MessageContextInteractionEvent event = JDAObjects.getMessageContextInteractionEvent(interaction);
new TestEventListener().onEvent(event);
try {
Message message = callback.await(100L, TimeUnit.MILLISECONDS);
Assertions.assertTrue(message.isEphemeral());
} catch (InterruptedException e) {
Assertions.fail(e);
}
}

private MessageContextInteraction getMessageContextInteraction(String command, Map<String, Object> options,
Callback<Message> messageCallback,
Callback<Boolean> deferCallback) {
MessageChannel channel = JDAObjects.getMessageChannel("test-channel", 1L, Callback.single());
Member member = JDAObjects.getMember("User", "0000");
return JDAObjects.getMessageContextInteraction(channel, member, command, options, messageCallback,
deferCallback);
}

}
4 changes: 2 additions & 2 deletions src/test/java/dev/coly/jdat/TestEventListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.entities.MessageEmbed;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
import net.dv8tion.jda.api.events.interaction.command.GenericCommandInteractionEvent;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;
import org.jetbrains.annotations.NotNull;
Expand All @@ -17,7 +17,7 @@ public void onMessageReceived(@NotNull MessageReceivedEvent e) {
}

@Override
public void onSlashCommandInteraction(@NotNull SlashCommandInteractionEvent e) {
public void onGenericCommandInteraction(@NotNull GenericCommandInteractionEvent e) {
switch (e.getName()) {
case "embed" -> e.replyEmbeds(getTestEmbed()).queue();
case "ping" -> e.reply("Pong!").queue();
Expand Down
16 changes: 16 additions & 0 deletions src/test/java/dev/coly/jdat/TestJDAObjects.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import dev.coly.util.Callback;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.Permission;
import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.entities.channel.unions.GuildChannelUnion;
import net.dv8tion.jda.api.events.guild.GuildJoinEvent;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -59,4 +61,18 @@ public void testGetGuildChannelUnion() {
Assertions.assertNotNull(guildChannelUnion.asNewsChannel());
}

@Test
public void testGetGuildJoinEvent() {
GuildJoinEvent event = JDAObjects.getGuildJoinEvent(JDAObjects.getGuild("Test Guild"));
Assertions.assertNotNull(event);
Assertions.assertEquals("Test Guild", event.getGuild().getName());
}

@Test
public void testGetGuild() {
Guild guild = JDAObjects.getGuild("Test Guild");
Assertions.assertNotNull(guild);
Assertions.assertEquals("Test Guild", guild.getName());
}

}
13 changes: 13 additions & 0 deletions src/test/java/dev/coly/jdat/TestSlashCommands.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,17 @@ public void testSlashCommandWithEphemeral() {
}
}

@Test
public void testTestSlashCommandEvent() {
try {
Message message = JDATesting.testSlashCommandEvent(new TestEventListener(), "ping",
null, null, null);

Assertions.assertNotNull(message);
Assertions.assertEquals("Pong!", message.getContentRaw());
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}

}