diff --git a/src/main/java/dev/coly/jdat/JDAObjects.java b/src/main/java/dev/coly/jdat/JDAObjects.java index a7e64cc..f0b1e58 100644 --- a/src/main/java/dev/coly/jdat/JDAObjects.java +++ b/src/main/java/dev/coly/jdat/JDAObjects.java @@ -3,32 +3,64 @@ import dev.coly.util.Callback; import net.dv8tion.jda.api.JDA; import net.dv8tion.jda.api.Permission; +import net.dv8tion.jda.api.entities.Activity; +import net.dv8tion.jda.api.entities.Guild; import net.dv8tion.jda.api.entities.Member; import net.dv8tion.jda.api.entities.Message; -import net.dv8tion.jda.api.entities.SelfUser; -import net.dv8tion.jda.api.entities.channel.middleman.MessageChannel; import net.dv8tion.jda.api.entities.MessageEmbed; +import net.dv8tion.jda.api.entities.SelfUser; +import net.dv8tion.jda.api.entities.User; import net.dv8tion.jda.api.entities.channel.concrete.NewsChannel; import net.dv8tion.jda.api.entities.channel.concrete.TextChannel; -import net.dv8tion.jda.api.entities.User; +import net.dv8tion.jda.api.entities.channel.middleman.MessageChannel; import net.dv8tion.jda.api.entities.channel.unions.GuildChannelUnion; import net.dv8tion.jda.api.entities.channel.unions.MessageChannelUnion; +import net.dv8tion.jda.api.entities.messages.MessagePoll; +import net.dv8tion.jda.api.events.guild.GuildJoinEvent; +import net.dv8tion.jda.api.events.interaction.GenericInteractionCreateEvent; +import net.dv8tion.jda.api.events.interaction.ModalInteractionEvent; +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.interaction.component.ButtonInteractionEvent; +import net.dv8tion.jda.api.events.interaction.component.GenericComponentInteractionCreateEvent; import net.dv8tion.jda.api.events.message.MessageReceivedEvent; +import net.dv8tion.jda.api.interactions.Interaction; +import net.dv8tion.jda.api.interactions.callbacks.IMessageEditCallback; +import net.dv8tion.jda.api.interactions.callbacks.IReplyCallback; +import net.dv8tion.jda.api.interactions.commands.CommandInteraction; +import net.dv8tion.jda.api.interactions.commands.CommandInteractionPayload; import net.dv8tion.jda.api.interactions.commands.OptionMapping; +import net.dv8tion.jda.api.interactions.commands.OptionType; +import net.dv8tion.jda.api.interactions.commands.context.MessageContextInteraction; +import net.dv8tion.jda.api.interactions.components.ActionRow; +import net.dv8tion.jda.api.interactions.components.Component; +import net.dv8tion.jda.api.interactions.components.ComponentInteraction; +import net.dv8tion.jda.api.interactions.components.LayoutComponent; +import net.dv8tion.jda.api.interactions.components.buttons.Button; +import net.dv8tion.jda.api.interactions.components.buttons.ButtonInteraction; +import net.dv8tion.jda.api.interactions.components.selections.StringSelectInteraction; +import net.dv8tion.jda.api.interactions.modals.Modal; +import net.dv8tion.jda.api.interactions.modals.ModalInteraction; +import net.dv8tion.jda.api.interactions.modals.ModalMapping; import net.dv8tion.jda.api.requests.restaction.MessageCreateAction; import net.dv8tion.jda.api.requests.restaction.interactions.ReplyCallbackAction; +import net.dv8tion.jda.api.utils.FileUpload; import net.dv8tion.jda.api.utils.messages.MessageCreateData; +import net.dv8tion.jda.api.utils.messages.MessagePollData; import net.dv8tion.jda.internal.JDAImpl; +import net.dv8tion.jda.internal.entities.EntityBuilder; import org.jetbrains.annotations.NotNull; -import org.mockito.ArgumentMatchers; import org.mockito.Mockito; +import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; +import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Optional; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyBoolean; @@ -48,15 +80,15 @@ public class JDAObjects { /** * Get a mocked {@link SlashCommandInteractionEvent}. * - * @param channel the channel this event would be executed. - * @param name the name of the slash command. - * @param subcommandName the name of the subcommand of the slash command. - * @param subcommandGroup the subcommand group of the slash command. - * @param options a map with all options for the slash command a user would have inputted. - * @param messageCallback a callback to receive messages that would be sent back to the channel with. - * {@link MessageChannel#sendMessage(CharSequence)} or - * {@link MessageChannel#sendMessageEmbeds(Collection)} for example. - * @return a mocked {@link SlashCommandInteractionEvent}. + * @param channel the channel this event would be executed. + * @param name the name of the slash command. + * @param subcommandName the name of the subcommand of the slash command. + * @param subcommandGroup the subcommand group of the slash command. + * @param options a map with all options for the slash command a user would have inputted. + * @param messageCallback a callback to receive messages that would be sent back to the channel with. + * {@link MessageChannel#sendMessage(CharSequence)} or + * {@link MessageChannel#sendMessageEmbeds(Collection)} for example. + * @return a mocked {@link SlashCommandInteractionEvent}. */ public static SlashCommandInteractionEvent getSlashCommandInteractionEvent(MessageChannel channel, String name, String subcommandName, @@ -70,17 +102,17 @@ public static SlashCommandInteractionEvent getSlashCommandInteractionEvent(Messa /** * Get a mocked {@link SlashCommandInteractionEvent}. * - * @param channel the channel this event would be executed. - * @param name the name of the slash command. - * @param subcommandName the name of the subcommand of the slash command. - * @param subcommandGroup the subcommand group of the slash command. - * @param options a map with all options for the slash command a user would have inputted. - * @param messageCallback a callback to receive messages that would be sent back to the channel with. - * @param deferReply a callback that is called when a deferred reply is called. The boolean is true when - * the message is ephemeral and false if not. - * {@link MessageChannel#sendMessage(CharSequence)} or - * {@link MessageChannel#sendMessageEmbeds(Collection)} for example. - * @return a mocked {@link SlashCommandInteractionEvent}. + * @param channel the channel this event would be executed. + * @param name the name of the slash command. + * @param subcommandName the name of the subcommand of the slash command. + * @param subcommandGroup the subcommand group of the slash command. + * @param options a map with all options for the slash command a user would have inputted. + * @param messageCallback a callback to receive messages that would be sent back to the channel with. + * @param deferReply a callback that is called when a deferred reply is called. The boolean is true when + * the message is ephemeral and false if not. + * {@link MessageChannel#sendMessage(CharSequence)} or + * {@link MessageChannel#sendMessageEmbeds(Collection)} for example. + * @return a mocked {@link SlashCommandInteractionEvent}. */ public static SlashCommandInteractionEvent getSlashCommandInteractionEvent(MessageChannel channel, String name, String subcommandName, @@ -88,54 +120,12 @@ public static SlashCommandInteractionEvent getSlashCommandInteractionEvent(Messa Map options, Callback messageCallback, Callback deferReply) { - SlashCommandInteractionEvent event = mock(SlashCommandInteractionEvent.class); - when(event.getName()).thenAnswer(invocation -> name); - when(event.getSubcommandName()).thenAnswer(invocation -> subcommandName); - when(event.getSubcommandGroup()).thenAnswer(invocation -> subcommandGroup); - when(event.getChannel()).thenAnswer(invocation -> channel); - - when(event.getOption(anyString())).thenAnswer(invocation -> { - OptionMapping mapping = mock(OptionMapping.class); - // why - when(mapping.getAsAttachment()).thenAnswer(inv -> options.get((String) invocation.getArgument(0))); - when(mapping.getAsString()).thenAnswer(inv -> options.get((String) invocation.getArgument(0))); - when(mapping.getAsBoolean()).thenAnswer(inv -> options.get((String) invocation.getArgument(0))); - when(mapping.getAsLong()).thenAnswer(inv -> options.get((String) invocation.getArgument(0))); - when(mapping.getAsInt()).thenAnswer(inv -> options.get((String) invocation.getArgument(0))); - when(mapping.getAsDouble()).thenAnswer(inv -> options.get((String) invocation.getArgument(0))); - when(mapping.getAsMentionable()).thenAnswer(inv -> options.get((String) invocation.getArgument(0))); - when(mapping.getAsMember()).thenAnswer(inv -> options.get((String) invocation.getArgument(0))); - when(mapping.getAsUser()).thenAnswer(inv -> options.get((String) invocation.getArgument(0))); - when(mapping.getAsRole()).thenAnswer(inv -> options.get((String) invocation.getArgument(0))); - when(mapping.getAsChannel()).thenAnswer(inv -> options.get((String) invocation.getArgument(0))); - - return mapping; - }); + Member member = getMember("User", "0000"); + CommandInteraction interaction = getCommandInteraction(channel, member, name, subcommandName, subcommandGroup, + options, messageCallback, deferReply, CommandInteraction.class); + SlashCommandInteractionEvent event = getCommandInteractionEvent(interaction, SlashCommandInteractionEvent.class); - when(event.reply(anyString())).thenAnswer(invocation -> - getReplyCallbackAction(getMessage(invocation.getArgument(0), channel), messageCallback)); - when(event.reply(any(MessageCreateData.class))).thenAnswer(invocation -> - getReplyCallbackAction(getMessage(invocation.getArgument(0, MessageCreateData.class).getContent(), - channel), messageCallback)); - when(event.replyEmbeds(anyList())).thenAnswer(invocation -> - getReplyCallbackAction(getMessage(null, invocation.getArgument(0), channel), - messageCallback)); - when(event.replyEmbeds(any(MessageEmbed.class), any(MessageEmbed[].class))).thenAnswer(invocation -> { - List embeds = invocation.getArguments().length == 1 ? new ArrayList<>() : - Arrays.asList(invocation.getArgument(1)); - embeds.add(invocation.getArgument(0)); - return getReplyCallbackAction(getMessage(null, embeds, channel), messageCallback); - }); - - when(event.deferReply()).thenAnswer(invocation -> { - deferReply.callback(false); - return mock(ReplyCallbackAction.class); - }); - - when(event.deferReply(any(Boolean.class))).thenAnswer(invocation -> { - deferReply.callback(invocation.getArgument(0)); - return mock(ReplyCallbackAction.class); - }); + mockReplyWithInteraction(event, interaction); return event; } @@ -143,9 +133,9 @@ public static SlashCommandInteractionEvent getSlashCommandInteractionEvent(Messa /** * Get a mocked {@link ReplyCallbackAction}. * - * @param message the message that this reply should produce. - * @param messageCallback the callback for receiving the message. - * @return a mocked {@link ReplyCallbackAction}. + * @param message the message that this reply should produce. + * @param messageCallback the callback for receiving the message. + * @return a mocked {@link ReplyCallbackAction}. */ private static ReplyCallbackAction getReplyCallbackAction(Message message, Callback messageCallback) { ReplyCallbackAction action = mock(ReplyCallbackAction.class); @@ -156,8 +146,8 @@ private static ReplyCallbackAction getReplyCallbackAction(Message message, Callb }).when(action).queue(); when(action.setEphemeral(anyBoolean())).thenAnswer(invocation -> { - when(message.isEphemeral()).thenReturn(true); - return action; + when(message.isEphemeral()).thenReturn(true); + return action; }); return action; } @@ -165,10 +155,10 @@ private static ReplyCallbackAction getReplyCallbackAction(Message message, Callb /** * Get a mocked {@link MessageReceivedEvent}. * - * @param channel the channel the message would be sent in. - * @param message the message that would be sent. - * @param member the member that would send this message. - * @return a mocked {@link MessageReceivedEvent}. + * @param channel the channel the message would be sent in. + * @param message the message that would be sent. + * @param member the member that would send this message. + * @return a mocked {@link MessageReceivedEvent}. */ public static MessageReceivedEvent getMessageReceivedEvent(MessageChannel channel, Message message, Member member) { MessageReceivedEvent event = mock(MessageReceivedEvent.class); @@ -179,6 +169,165 @@ public static MessageReceivedEvent getMessageReceivedEvent(MessageChannel channe return event; } + /** + * Get a mocked {@link MessageContextInteractionEvent}. + * + * @param interaction the interaction triggered this event. + * @return a mocked {@link MessageContextInteractionEvent}. + */ + public static MessageContextInteractionEvent getMessageContextInteractionEvent( + MessageContextInteraction interaction) { + return getCommandInteractionEvent(interaction, MessageContextInteractionEvent.class); + } + + /** + * Get a mocked {@link ButtonInteractionEvent}. + * + * @param interaction the interaction that triggered this event. + * @return a mocked {@link ButtonInteractionEvent}. + */ + public static ButtonInteractionEvent getButtonInteractionEvent( + ButtonInteraction interaction, Callback deferEditCallback + ) { + return getGenericComponentInteractionCreateEvent(interaction, deferEditCallback, ButtonInteractionEvent.class); + } + + /** + * Get a mocked {@link ModalInteractionEvent}. + * + * @param interaction the interaction that triggered this event + * @return a mocked {@link ModalInteractionEvent}. + */ + public static ModalInteractionEvent getModalInteractionEvent(ModalInteraction interaction) { + return getGenericInteractionCreateEvent(interaction, ModalInteractionEvent.class); + } + + protected static T getCommandInteractionEvent( + CommandInteraction interaction, Class clazz) { + T event = getGenericInteractionCreateEvent(interaction, clazz); + + when(event.getFullCommandName()).thenAnswer(invocation -> interaction.getFullCommandName()); + when(event.getName()).thenAnswer(invocation -> interaction.getName()); + when(event.getSubcommandName()).thenAnswer(invocation -> interaction.getSubcommandName()); + when(event.getSubcommandGroup()).thenAnswer(invocation -> interaction.getSubcommandGroup()); + when(event.getOptions()).thenAnswer(invocation -> interaction.getOptions()); + when(event.getOption(anyString())).thenAnswer(invocation -> + interaction.getOption(invocation.getArgument(0))); + when(event.getOptionsByName(anyString())).thenAnswer(invocation -> + interaction.getOptionsByName(invocation.getArgument(0))); + when(event.getOptionsByType(any(OptionType.class))).thenAnswer(invocation -> + interaction.getOptionsByType(invocation.getArgument(0))); + + mockReplyWithInteraction(event, interaction); + + return event; + } + + protected static T getGenericComponentInteractionCreateEvent( + ComponentInteraction interaction, Callback deferEditCallback, Class clazz + ) { + T event = getGenericInteractionCreateEvent(interaction, clazz); + + when(event.getComponentId()).thenAnswer(invocation -> interaction.getComponentId()); + when(event.getComponent()).thenAnswer(invocation -> interaction.getComponent()); + when(event.getMessageId()).thenAnswer(invocation -> interaction.getMessageId()); + when(event.getMessageIdLong()).thenAnswer(invocation -> interaction.getMessageIdLong()); + when(event.getComponentType()).thenAnswer(invocation -> interaction.getComponentType()); + + when(event.deferEdit()).thenAnswer(invocation -> interaction.deferReply()); + when(event.replyModal(any(Modal.class))).thenAnswer(invocation -> + interaction.replyModal(invocation.getArgument(0))); + when(event.replyWithPremiumRequired()).thenAnswer(invocation -> + interaction.replyWithPremiumRequired()); + + mockReplyWithEditInteraction((IReplyMessageEditCallback) event, (IReplyMessageEditCallback) interaction); + + when(event.deferEdit()).thenAnswer(invocation -> { + deferEditCallback.callback(Void.TYPE.cast(null)); + return mock(ReplyCallbackAction.class); + }); + + return event; + } + + protected static T getGenericInteractionCreateEvent( + Interaction interaction, Class clazz + ) { + T event = mock(clazz); + JDA jda = getJDA(); + + when(event.getChannel()).thenAnswer(invocation -> interaction.getChannel()); + when(event.getJDA()).thenReturn(jda); + when(event.getInteraction()).thenAnswer(invocation -> interaction); + when(event.getChannelId()).thenAnswer(invocation -> interaction.getChannelId()); + when(event.getChannelIdLong()).thenAnswer(invocation -> interaction.getChannelIdLong()); + when(event.getChannelType()).thenAnswer(invocation -> interaction.getChannelType()); + when(event.getMessageChannel()).thenAnswer(invocation -> interaction.getMessageChannel()); + when(event.getGuildChannel()).thenAnswer(invocation -> interaction.getGuildChannel()); + when(event.getMember()).thenAnswer(invocation -> interaction.getMember()); + when(event.getType()).thenAnswer(invocation -> interaction.getType()); + + return event; + } + + /** + * Get a mocked {@link GuildJoinEvent}. + * + * @param guild the guild that was joined. + * @return a mocked {@link GuildJoinEvent}. + */ + public static GuildJoinEvent getGuildJoinEvent(Guild guild) { + GuildJoinEvent event = mock(GuildJoinEvent.class); + JDA jda = getJDA(); + + when(event.getGuild()).thenReturn(guild); + when(event.getJDA()).thenReturn(jda); + + return event; + } + + protected static void mockOptions(CommandInteractionPayload event, Map options) { + if (options == null) { + options = new HashMap<>(); + } + + List optionMappings = new ArrayList<>(); + for (Map.Entry entry : options.entrySet()) { + OptionMapping mapping = mock(OptionMapping.class); + + when(mapping.getName()).thenReturn(entry.getKey()); + // why + when(mapping.getAsAttachment()).thenAnswer(inv -> entry.getValue()); + when(mapping.getAsString()).thenAnswer(inv -> entry.getValue()); + when(mapping.getAsBoolean()).thenAnswer(inv -> entry.getValue()); + when(mapping.getAsLong()).thenAnswer(inv -> entry.getValue()); + when(mapping.getAsInt()).thenAnswer(inv -> entry.getValue()); + when(mapping.getAsDouble()).thenAnswer(inv -> entry.getValue()); + when(mapping.getAsMentionable()).thenAnswer(inv -> entry.getValue()); + when(mapping.getAsMember()).thenAnswer(inv -> entry.getValue()); + when(mapping.getAsUser()).thenAnswer(inv -> entry.getValue()); + when(mapping.getAsRole()).thenAnswer(inv -> entry.getValue()); + when(mapping.getAsChannel()).thenAnswer(inv -> entry.getValue()); + + optionMappings.add(mapping); + } + + when(event.getOptions()).thenReturn(optionMappings); + + when(event.getOption(anyString())).thenAnswer(invocation -> { + Optional mapping = optionMappings.stream() + .filter(optionMapping -> optionMapping.getName().equals(invocation.getArgument(0))) + .findFirst(); + if (mapping.isPresent()) { + return mapping.get(); + } + throw new IllegalArgumentException("Unknown option: " + invocation.getArgument(0)); + }); + + when(event.getOptionsByName(anyString())).thenAnswer(invocation -> optionMappings.stream() + .filter(optionMapping -> optionMapping.getName().equals(invocation.getArgument(0))).toList()); + } + /** * Get a mocked instance of JDA. * @@ -190,13 +339,12 @@ public static JDA getJDA() { } - /** * Get a mocked instance of JDA with the specified name and discriminator. * - * @param name the name of the self user. - * @param discriminator the discriminator of the self user. - * @return a mocked instance of {@link JDA}. + * @param name the name of the self user. + * @param discriminator the discriminator of the self user. + * @return a mocked instance of {@link JDA}. * @throws RuntimeException if interrupted while awaiting ready status. */ @NotNull @@ -220,11 +368,11 @@ public static JDA getJDA(String name, String discriminator) { /** * Get a mocked {@link MessageChannel}. * - * @param name the name of the channel. - * @param id the id of the channel. - * @param messageCallback the callback used for returning the {@link Message} when for example - * {@link MessageChannel#sendMessage(CharSequence)} or other methods are called. - * @return a mocked {@link MessageChannel}. + * @param name the name of the channel. + * @param id the id of the channel. + * @param messageCallback the callback used for returning the {@link Message} when for example + * {@link MessageChannel#sendMessage(CharSequence)} or other methods are called. + * @return a mocked {@link MessageChannel}. */ public static MessageChannel getMessageChannel(String name, long id, Callback messageCallback) { MessageChannel channel = mock(MessageChannel.class, withSettings() @@ -259,10 +407,10 @@ public static MessageChannel getMessageChannel(String name, long id, Callback messageCallback, Message message) { MessageCreateAction messageAction = mock(MessageCreateAction.class); @@ -276,9 +424,9 @@ public static MessageCreateAction getMessageCreateAction(Callback messa /** * Get a mocked {@link Message}. * - * @param content the content of the message. This is the raw, displayed and stripped content. - * @param channel the {@link MessageChannel} the message would be sent in. - * @return a mocked {@link Message}. + * @param content the content of the message. This is the raw, displayed and stripped content. + * @param channel the {@link MessageChannel} the message would be sent in. + * @return a mocked {@link Message}. */ public static Message getMessage(String content, MessageChannel channel) { return getMessage(content, new ArrayList<>(), channel); @@ -287,18 +435,50 @@ public static Message getMessage(String content, MessageChannel channel) { /** * Get a mocked {@link Message}. * - * @param content the content of the message. This is the raw, displayed and stripped content. - * @param embeds a list of {@link MessageEmbed}s that this message contains. - * @param channel the {@link MessageChannel} the message would be sent in. - * @return a mocked {@link Message}. + * @param content the content of the message. This is the raw, displayed and stripped content. + * @param embeds a list of {@link MessageEmbed}s that this message contains. + * @param channel the {@link MessageChannel} the message would be sent in. + * @return a mocked {@link Message}. */ public static Message getMessage(String content, List embeds, MessageChannel channel) { + return getMessage(content, embeds, null, List.of(), null, List.of(), channel); + } + + /** + * Get a mocked {@link Message}. + * + * @param content the content of the message. This is the raw, displayed, and stripped content. + * @param embeds a list of {@link MessageEmbed}s that this message contains. + * @param poll a poll attached to the message. + * @param attachments a list of attachments to this message. This will probably be files. + * @param activity an activity attached to the message. + * @param actionRows a list of actions rows with actions attached to the message. + * @param channel the {@link MessageChannel} the message would be sent in. + * @return a mocked {@link Message}. + */ + public static Message getMessage(String content, List embeds, MessagePoll poll, + List attachments, Activity activity, List actionRows, + MessageChannel channel) { Message message = mock(Message.class); when(message.getContentRaw()).thenAnswer(invocation -> content); when(message.getContentDisplay()).thenAnswer(invocation -> content); when(message.getContentStripped()).thenAnswer(invocation -> content); + when(message.getChannel()).thenAnswer(invocation -> channel); + when(message.getChannelId()).thenAnswer(invocation -> channel.getId()); + when(message.getChannelIdLong()).thenAnswer(invocation -> channel.getIdLong()); + when(message.getChannelType()).thenAnswer(invocation -> channel.getType()); + + when(message.getJDA()).thenAnswer(invocation -> getJDA()); + Member member = getMember("Author", "0000"); + when(message.getAuthor()).thenAnswer(invocation -> member.getUser()); + when(message.getMember()).thenAnswer(invocation -> member); + when(message.getEmbeds()).thenAnswer(invocation -> embeds); + when(message.getPoll()).thenAnswer(invocation -> poll); + when(message.getAttachments()).thenAnswer(invocation -> attachments); + when(message.getActivity()).thenAnswer(invocation -> activity); + when(message.getActionRows()).thenAnswer(invocation -> actionRows); return message; } @@ -307,7 +487,7 @@ public static Message getMessage(String content, List embeds, Mess * * @param name the name of the user. * @param discriminator the discriminator of the user. - * @return a mocked {@link Member}. + * @return a mocked {@link Member}. */ public static Member getMember(String name, String discriminator) { Member member = mock(Member.class); @@ -327,9 +507,9 @@ public static Member getMember(String name, String discriminator) { /** * Get a mocked {@link GuildChannelUnion}. * - * @param name the name of the channel - * @param id the id of the channel - * @return the mocked {@link GuildChannelUnion} + * @param name the name of the channel + * @param id the id of the channel + * @return the mocked {@link GuildChannelUnion} */ public static GuildChannelUnion getGuildChannelUnion(String name, long id, Callback callback) { GuildChannelUnion channelUnion = mock(GuildChannelUnion.class); @@ -352,7 +532,7 @@ public static GuildChannelUnion getGuildChannelUnion(String name, long id, Callb * * @param name the name of the user. * @param discriminator the discriminator of the user. - * @return a mocked {@link SelfUser}. + * @return a mocked {@link SelfUser}. */ @NotNull public static SelfUser getSelfUser(String name, String discriminator) { @@ -367,4 +547,281 @@ public static SelfUser getSelfUser(String name, String discriminator) { return selfUser; } + /** + * Get a mocked {@link Guild}. + * + * @param name the name of the guild. + * @return a mocked {@link Guild} + */ + public static Guild getGuild(String name) { + Guild guild = mock(Guild.class); + + when(guild.getName()).thenReturn(name); + when(guild.getId()).thenReturn("0"); + when(guild.getIdLong()).thenReturn(0L); + + return guild; + } + + /** + * Get a real but not functional {@link MessagePoll} from data. + * + * @param data the data for creating the poll. + * @return a real but not functional {@link MessagePoll}. + */ + public static MessagePoll getMessagePoll(MessagePollData data) { + return EntityBuilder.createMessagePoll( data.toData()); + } + + public static List getAttachments(List files) { + List attachments = new ArrayList<>(); + for (FileUpload file : files) { + int size = -1; + try { + size = file.getData().readAllBytes().length; + } catch (IOException ignored) {} + attachments.add(new Message.Attachment(0, "https://cdn.fake/" + file.getName(), + "https://proxy.fake/" + file.getName(), file.getName(), "unknwon", + file.getDescription(), size, -1, -1, false, null, -1, + (JDAImpl) getJDA())); + } + return attachments; + } + + /** + * Get a mocked {@link MessageContextInteraction}. + * + * @param channel the channel the interaction was triggered. + * @param member the member the interaction was triggered by. + * @param name the name of the application command the interaction triggered. + * @param options options for application command. + * @param messageCallback the callback used for message reply + * @param deferReply the callback used for defer reply + * @return a mocked {@link MessageContextInteraction}. + */ + public static MessageContextInteraction + getMessageContextInteraction(MessageChannel channel, Member member, String name, Map options, + Callback messageCallback, Callback deferReply) { + return getCommandInteraction(channel, member, name, null, null, options, + messageCallback, deferReply, MessageContextInteraction.class); + } + + public static ButtonInteraction + getButtonInteraction(MessageChannel channel, Button button, String componentId, Member member, + Callback messageCallback, Callback deferReply, Callback deferEditRepl) { + ButtonInteraction interaction = getComponentInteraction(channel, member, button, componentId, messageCallback, + deferReply, deferEditRepl, ButtonInteraction.class); + + when(interaction.getButton()).thenReturn(button); + when(interaction.getComponent()).thenReturn(button); + when(interaction.editButton(any(Button.class))).thenAnswer(invocation -> { + Button newButton = invocation.getArgument(0); + + Message message = interaction.getMessage(); + List components = new ArrayList<>(message.getActionRows()); + LayoutComponent.updateComponent(components, interaction.getComponentId(), newButton); + + if (interaction.isAcknowledged()) + return interaction.getHook().editMessageComponentsById(message.getId(), components) + .map(it -> null); + else + return interaction.editComponents(components).map(it -> null); + }); + + return interaction; + } + + public static ModalInteraction getModalInteraction(MessageChannel channel, Message message, String modalId, + List values, Member member) { + ModalInteraction interaction = getInteraction(channel, member, ModalInteraction.class); + + when(interaction.getModalId()).thenReturn(modalId); + when(interaction.getMessage()).thenReturn(message); + when(interaction.getChannel()).thenReturn(message.getChannel()); + when(interaction.getGuildChannel()).thenReturn(message.getGuildChannel()); + + when(interaction.getValues()).thenReturn(values); + when(interaction.getValue(anyString())).thenAnswer(invocation -> + values.stream() + .filter(mapping -> mapping.getId().equals(invocation.getArgument(0))) + .findFirst() + .orElse(null) + ); + return interaction; + } + + public static StringSelectInteraction + getStringSelectInteraction(MessageChannel channel, Button button, String componentId, Member member, + Callback messageCallback, Callback deferReply, Callback deferEditRepl) { + return getComponentInteraction(channel, member, button, componentId, messageCallback, deferReply, deferEditRepl, + StringSelectInteraction.class); + } + + protected static T + getCommandInteraction(MessageChannel channel, Member member, String name, String subcommand, String subgroup, + Map options, Callback messageCallback, + Callback deferReply, Class clazz) { + T interaction = getInteraction(channel, member, clazz); + + when(interaction.getName()).thenReturn(name); + when(interaction.getSubcommandName()).thenReturn(subcommand); + when(interaction.getSubcommandGroup()).thenReturn(subgroup); + + mockOptions(interaction, options); + mockReplyCallbacks(interaction, channel, messageCallback, deferReply); + + return interaction; + } + + protected static T + getComponentInteraction(MessageChannel channel, Member member, Component component, String componentId, + Callback messageCallback, Callback deferReply, + Callback deferEditReply, Class clazz) { + T interaction = getInteraction(channel, member, clazz); + + mockReplyCallbacks((IReplyMessageEditCallback) interaction, channel, messageCallback, deferReply, + deferEditReply); + + when(interaction.getComponent()).thenAnswer(invocation -> component); + when(interaction.getComponentId()).thenAnswer(invocation -> componentId); + when(interaction.getComponentType()).thenAnswer(invocation -> component.getType()); + + when(interaction.deferEdit()).thenAnswer(invocation -> interaction.deferReply()); + when(interaction.replyModal(any(Modal.class))).thenAnswer(invocation -> + interaction.replyModal(invocation.getArgument(0))); + when(interaction.replyWithPremiumRequired()).thenAnswer(invocation -> + interaction.replyWithPremiumRequired()); + + return interaction; + } + + protected static T getInteraction(MessageChannel channel, Member member, Class clazz) { + T interaction = mock(clazz); + + when(interaction.getMessageChannel()).thenReturn(channel); + when(interaction.getChannel()).thenAnswer(invocation -> channel); + when(interaction.getChannelId()).thenAnswer(invocation -> channel.getId()); + when(interaction.getChannelIdLong()).thenAnswer(invocation -> channel.getIdLong()); + when(interaction.getChannelType()).thenAnswer(invocation -> channel.getType()); + when(interaction.getMember()).thenReturn(member); + when(interaction.getJDA()).thenAnswer(invocation -> channel.getJDA()); + + return interaction; + } + + protected interface IReplyMessageEditCallback extends IReplyCallback, IMessageEditCallback {} + + protected static void mockReplyCallbacks(IReplyCallback callback, MessageChannel channel, + Callback messageCallback, Callback deferReply) { + when(callback.reply(anyString())).thenAnswer(invocation -> + getReplyCallbackAction(getMessage(invocation.getArgument(0), channel), messageCallback)); + when(callback.reply(any(MessageCreateData.class))).thenAnswer(invocation -> + getReplyCallbackAction(getMessage(invocation.getArgument(0, MessageCreateData.class).getContent(), + channel), messageCallback)); + + when(callback.replyEmbeds(anyList())).thenAnswer(invocation -> + getReplyCallbackAction(getMessage(null, invocation.getArgument(0), channel), + messageCallback)); + when(callback.replyEmbeds(any(MessageEmbed.class), any(MessageEmbed[].class))).thenAnswer(invocation -> { + List embeds = invocation.getArguments().length == 1 ? new ArrayList<>() : + Arrays.asList(invocation.getArgument(1)); + embeds.add(invocation.getArgument(0)); + return getReplyCallbackAction(getMessage(null, embeds, channel), messageCallback); + }); + + when(callback.replyComponents(anyList())).thenAnswer(invocation -> { + List actionRows = new ArrayList<>(invocation.getArgument(0)); + return getReplyCallbackAction(getMessage(null, null, null, List.of(), null, + actionRows, channel), messageCallback); + }); + when(callback.replyComponents(any(LayoutComponent.class), any(LayoutComponent[].class))).thenAnswer(invocation -> { + List actionRows = invocation.getArguments().length == 1 ? new ArrayList<>() : + Arrays.asList(invocation.getArgument(1)); + actionRows.add(invocation.getArgument(0)); + return getReplyCallbackAction(getMessage(null, null, null, List.of(), null, + actionRows, channel), messageCallback); + }); + + when(callback.replyPoll(any(MessagePollData.class))).thenAnswer(invocation -> { + MessagePoll poll = getMessagePoll(invocation.getArgument(0)); + return getReplyCallbackAction(getMessage(null, null, poll, List.of(), null, + List.of(), channel), messageCallback); + }); + + when(callback.replyFiles(anyList())).thenAnswer(invocation -> { + List files = invocation.getArgument(0); + return getReplyCallbackAction(getMessage(null, null, null, getAttachments(files), + null, List.of(), channel), messageCallback); + }); + when(callback.replyFiles(any(FileUpload[].class))).thenAnswer(invocation -> { + List files = invocation.getArguments().length == 1 ? new ArrayList<>() : + Arrays.asList(invocation.getArgument(1)); + files.add(invocation.getArgument(0)); + return getReplyCallbackAction(getMessage(null, null, null, getAttachments(files), + null, List.of(), channel), messageCallback); + }); + + when(callback.deferReply()).thenAnswer(invocation -> { + deferReply.callback(false); + return mock(ReplyCallbackAction.class); + }); + when(callback.deferReply(any(Boolean.class))).thenAnswer(invocation -> { + deferReply.callback(invocation.getArgument(0)); + return mock(ReplyCallbackAction.class); + }); + } + + protected static void mockReplyCallbacks(IReplyMessageEditCallback callback, MessageChannel channel, + Callback messageCallback, Callback deferReply, + Callback deferEditReply) { + mockReplyCallbacks(callback, channel, messageCallback, deferReply); + + when(callback.deferEdit()).thenAnswer(invocation -> { + deferEditReply.callback(Void.TYPE.cast(null)); + return mock(ReplyCallbackAction.class); + }); + } + + protected static void mockReplyWithInteraction(IReplyCallback callback, IReplyCallback interaction) { + when(callback.reply(anyString())).thenAnswer(invocation -> + interaction.reply((String) invocation.getArgument(0))); + when(callback.reply(any(MessageCreateData.class))).thenAnswer(invocation -> + interaction.reply((MessageCreateData) invocation.getArgument(0))); + + when(callback.replyEmbeds(anyList())).thenAnswer(invocation -> + interaction.replyEmbeds(invocation.getArgument(0))); + when(callback.replyEmbeds(any(MessageEmbed.class), any(MessageEmbed[].class))).thenAnswer(invocation -> { + if (invocation.getArguments().length == 1) { + return interaction.replyEmbeds(invocation.getArgument(0), new MessageEmbed[0]); + } else { + return interaction.replyEmbeds(invocation.getArgument(0), invocation.getArgument(1)); + } + }); + + when(callback.replyComponents(anyList())).thenAnswer(invocation -> + interaction.replyComponents(invocation.getArgument(0))); + when(callback.replyComponents(any(LayoutComponent.class), any(LayoutComponent[].class))) + .thenAnswer(invocation -> + + interaction.replyComponents(invocation.getArgument(0), invocation.getArgument(1))); + when(callback.replyPoll(any(MessagePollData.class))).thenAnswer(invocation -> + interaction.replyPoll(invocation.getArgument(0))); + + when(callback.replyFiles(anyList())).thenAnswer(invocation -> + interaction.replyFiles((Collection) invocation.getArgument(0))); + when(callback.replyFiles(any(FileUpload[].class))).thenAnswer(invocation -> + interaction.replyFiles((Collection) invocation.getArgument(0))); + + when(callback.deferReply()).thenAnswer(invocation -> + interaction.deferReply()); + when(callback.deferReply(any(Boolean.class))).thenAnswer(invocation -> + interaction.deferReply(invocation.getArgument(0))); + } + + protected static void mockReplyWithEditInteraction(IReplyMessageEditCallback callback, + IReplyMessageEditCallback interaction) { + mockReplyWithInteraction(callback, interaction); + + when(callback.deferEdit()).thenAnswer(invocation -> interaction.deferEdit()); + } } diff --git a/src/main/java/dev/coly/jdat/JDATesting.java b/src/main/java/dev/coly/jdat/JDATesting.java index 50645ab..b86c963 100644 --- a/src/main/java/dev/coly/jdat/JDATesting.java +++ b/src/main/java/dev/coly/jdat/JDATesting.java @@ -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; @@ -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 options) + String subcommandGroup, Map options) throws InterruptedException { - Callback 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); } /** @@ -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 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); } /** @@ -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 options, List 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 options) throws InterruptedException { + return testGenericCommandInteractionEvent(listener, name, null, null, options, + MessageContextInteractionEvent.class); + } + + private static Message + testGenericCommandInteractionEvent(EventListener listener, String name, String subcommand, String subgroup, + Map options, Class clazz) + throws InterruptedException { + Callback 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 void + assertGenericCommandInteractionEvent(EventListener listener, String command, String subcommand, String subgroup, + Map options, Class type, String expectedOutput) { + try { + Assertions.assertEquals( + testGenericCommandInteractionEvent(listener, command, subcommand, subgroup, options, type) + .getContentRaw(), + expectedOutput); + } catch (InterruptedException e){ + Assertions.fail(e); + } + } + + private static void + assertGenericCommandInteractionEvent(EventListener listener, String command, String subcommand, String subgroup, + Map options, Class type, + List 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 options, List 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 options, String expectedOutput) { + assertGenericCommandInteractionEvent(listener, command, null, null, options, + MessageContextInteractionEvent.class, expectedOutput); + } + private static void assertEmbeds(Message message, List expectedOutputs) { Assertions.assertEquals(expectedOutputs.size(), message.getEmbeds().size(), "Number of embeds"); diff --git a/src/test/java/dev/coly/jdat/TestCommandInteractionEvent.java b/src/test/java/dev/coly/jdat/TestCommandInteractionEvent.java new file mode 100644 index 0000000..62e3bf5 --- /dev/null +++ b/src/test/java/dev/coly/jdat/TestCommandInteractionEvent.java @@ -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 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 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 options = new HashMap<>(); + options.put("test", true); + Callback 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 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 options, + Callback messageCallback, + Callback 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); + } + +} diff --git a/src/test/java/dev/coly/jdat/TestEventListener.java b/src/test/java/dev/coly/jdat/TestEventListener.java index bcc75a4..86108ac 100644 --- a/src/test/java/dev/coly/jdat/TestEventListener.java +++ b/src/test/java/dev/coly/jdat/TestEventListener.java @@ -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; @@ -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(); diff --git a/src/test/java/dev/coly/jdat/TestJDAObjects.java b/src/test/java/dev/coly/jdat/TestJDAObjects.java index 75a95b0..4a4435c 100644 --- a/src/test/java/dev/coly/jdat/TestJDAObjects.java +++ b/src/test/java/dev/coly/jdat/TestJDAObjects.java @@ -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; @@ -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()); + } + } diff --git a/src/test/java/dev/coly/jdat/TestSlashCommands.java b/src/test/java/dev/coly/jdat/TestSlashCommands.java index b85b41d..c9f0ca4 100644 --- a/src/test/java/dev/coly/jdat/TestSlashCommands.java +++ b/src/test/java/dev/coly/jdat/TestSlashCommands.java @@ -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); + } + } + }