From e163e2d1096289f660e65e1ffbb74e2edb805ed9 Mon Sep 17 00:00:00 2001 From: Skillz4Killz <23035000+Skillz4Killz@users.noreply.github.com> Date: Sat, 10 Apr 2021 17:03:32 -0400 Subject: [PATCH] fix: reaction remove tests (#811) --- .../messages/MESSAGE_REACTION_REMOVE.ts | 6 +- .../messages/MESSAGE_REACTION_REMOVE_EMOJI.ts | 7 +- tests/channels/category_children.ts | 12 ++-- tests/channels/create_channel.ts | 69 +++++++++++-------- tests/channels/delete_channel.ts | 16 ++--- tests/guilds/create_guild.ts | 2 +- tests/messages/add_reaction.ts | 10 +-- tests/messages/add_reactions.ts | 59 ++++++++-------- tests/messages/create_message.ts | 11 +-- tests/messages/delete_message.ts | 10 ++- tests/messages/delete_messages.ts | 18 ++--- tests/messages/edit_message.ts | 4 +- tests/messages/get_message.ts | 6 +- tests/messages/get_messages.ts | 10 ++- tests/messages/get_reactions.ts | 8 +-- tests/messages/pin_message.ts | 8 +-- tests/messages/remove_all_reactions.ts | 20 ++---- tests/messages/remove_reaction.ts | 20 ++---- tests/messages/remove_reaction_emoji.ts | 20 ++---- tests/messages/remove_user_reaction.ts | 22 ++---- tests/messages/unpin_message.ts | 10 ++- tests/mod.ts | 8 +-- tests/util/delay_until.ts | 7 ++ tests/ws/start_bot.ts | 5 +- 24 files changed, 174 insertions(+), 194 deletions(-) create mode 100644 tests/util/delay_until.ts diff --git a/src/handlers/messages/MESSAGE_REACTION_REMOVE.ts b/src/handlers/messages/MESSAGE_REACTION_REMOVE.ts index e2957810f..7d28cb76b 100644 --- a/src/handlers/messages/MESSAGE_REACTION_REMOVE.ts +++ b/src/handlers/messages/MESSAGE_REACTION_REMOVE.ts @@ -11,12 +11,16 @@ export async function handleMessageReactionRemove( if (message) { const reaction = message.reactions?.find((reaction) => - reaction.emoji.id === payload.emoji.id && + // MUST USE == because discord sends null and we use undefined + reaction.emoji.id == payload.emoji.id && reaction.emoji.name === payload.emoji.name ); if (reaction) { reaction.count--; + if (reaction.count === 0) message.reactions = message.reactions?.filter(r => r.count !== 0); + if (!message.reactions?.length) message.reactions = undefined; + await cacheHandlers.set("messages", payload.message_id, message); } } diff --git a/src/handlers/messages/MESSAGE_REACTION_REMOVE_EMOJI.ts b/src/handlers/messages/MESSAGE_REACTION_REMOVE_EMOJI.ts index 677c51b02..8828bf7f5 100644 --- a/src/handlers/messages/MESSAGE_REACTION_REMOVE_EMOJI.ts +++ b/src/handlers/messages/MESSAGE_REACTION_REMOVE_EMOJI.ts @@ -10,13 +10,16 @@ export async function handleMessageReactionRemoveEmoji( const message = await cacheHandlers.get("messages", payload.message_id); if (message?.reactions) { - message.reactions = message.reactions?.filter( + message.reactions = message.reactions.filter( (reaction) => !( - reaction.emoji.id === payload.emoji.id && + // MUST USE == because discord sends null and we use undefined + reaction.emoji.id == payload.emoji.id && reaction.emoji.name === payload.emoji.name ), ); + + if (!message.reactions.length) message.reactions = undefined await cacheHandlers.set("messages", payload.message_id, message); } diff --git a/tests/channels/category_children.ts b/tests/channels/category_children.ts index 55412d2fb..c286afc4f 100644 --- a/tests/channels/category_children.ts +++ b/tests/channels/category_children.ts @@ -16,11 +16,11 @@ Deno.test({ // Assertions assertExists(category); // Delay the execution by 5 seconds to allow CHANNEL_CREATE event to be processed - await delay(5000); + await delay(3000); if (!cache.channels.has(category.id)) { throw new Error( - "The channel seemed to be created but it was not cached.", + "The channel seemed to be created but it was not cached." ); } @@ -31,15 +31,15 @@ Deno.test({ name: `Discordeno-test-${num}`, parentId: category.id, }) - ), + ) ); // Delay the execution by 5 seconds to allow CHANNEL_CREATE event to be processed - await delay(5000); + await delay(3000); // If every channel is not present in the cache, error out if (!channels.every((c) => cache.channels.has(c.id))) { throw new Error( - "The channels seemed to be created but it was not cached.", + "The channels seemed to be created but it was not cached." ); } @@ -49,7 +49,7 @@ Deno.test({ !channels.every((c) => ids.has(c.id)) ) { throw new Error( - "The category channel ids did not match with the category channels.", + "The category channel ids did not match with the category channels." ); } }, diff --git a/tests/channels/create_channel.ts b/tests/channels/create_channel.ts index 1865050e4..a32849dc6 100644 --- a/tests/channels/create_channel.ts +++ b/tests/channels/create_channel.ts @@ -16,23 +16,21 @@ async function ifItFailsBlameWolf(options: CreateGuildChannel, save = false) { if (save) tempData.channelId = channel.id; // Delay the execution by 5 seconds to allow CHANNEL_CREATE event to be processed - await delay(5000); + await delay(3000); if (!cache.channels.has(channel.id)) { - throw new Error( - "The channel seemed to be created but it was not cached.", - ); + throw new Error("The channel seemed to be created but it was not cached."); } if (options.topic && channel.topic !== options.topic) { throw new Error( - "The channel was supposed to have a topic but it does not appear to be the same topic.", + "The channel was supposed to have a topic but it does not appear to be the same topic." ); } if (options.bitrate && channel.bitrate !== options.bitrate) { throw new Error( - "The channel was supposed to have a bitrate but it does not appear to be the same bitrate.", + "The channel was supposed to have a bitrate but it does not appear to be the same bitrate." ); } } @@ -48,10 +46,13 @@ Deno.test({ Deno.test({ name: "[channel] create a new category channel", async fn() { - await ifItFailsBlameWolf({ - name: "Discordeno-test", - type: DiscordChannelTypes.GUILD_CATEGORY, - }, true); + await ifItFailsBlameWolf( + { + name: "Discordeno-test", + type: DiscordChannelTypes.GUILD_CATEGORY, + }, + true + ); }, ...defaultTestOptions, }); @@ -75,10 +76,13 @@ Deno.test({ Deno.test({ name: "[channel] create a new voice channel", async fn() { - await ifItFailsBlameWolf({ - name: "Discordeno-test", - type: DiscordChannelTypes.GUILD_VOICE, - }, true); + await ifItFailsBlameWolf( + { + name: "Discordeno-test", + type: DiscordChannelTypes.GUILD_VOICE, + }, + true + ); }, ...defaultTestOptions, }); @@ -86,11 +90,14 @@ Deno.test({ Deno.test({ name: "[channel] create a new voice channel with a bitrate", async fn() { - await ifItFailsBlameWolf({ - name: "discordeno-test", - type: DiscordChannelTypes.GUILD_VOICE, - bitrate: 32000, - }, true); + await ifItFailsBlameWolf( + { + name: "discordeno-test", + type: DiscordChannelTypes.GUILD_VOICE, + bitrate: 32000, + }, + true + ); }, ...defaultTestOptions, }); @@ -98,11 +105,14 @@ Deno.test({ Deno.test({ name: "[channel] create a new voice channel with a user limit", async fn() { - await ifItFailsBlameWolf({ - name: "Discordeno-test", - type: DiscordChannelTypes.GUILD_VOICE, - userLimit: 32, - }, true); + await ifItFailsBlameWolf( + { + name: "Discordeno-test", + type: DiscordChannelTypes.GUILD_VOICE, + userLimit: 32, + }, + true + ); }, ...defaultTestOptions, }); @@ -110,10 +120,13 @@ Deno.test({ Deno.test({ name: "[channel] create a new text channel with a rate limit per user", async fn() { - await ifItFailsBlameWolf({ - name: "Discordeno-test", - rateLimitPerUser: 2423, - }, true); + await ifItFailsBlameWolf( + { + name: "Discordeno-test", + rateLimitPerUser: 2423, + }, + true + ); }, ...defaultTestOptions, }); diff --git a/tests/channels/delete_channel.ts b/tests/channels/delete_channel.ts index 2b779f996..b39445219 100644 --- a/tests/channels/delete_channel.ts +++ b/tests/channels/delete_channel.ts @@ -12,22 +12,22 @@ Deno.test({ name: "delete-channel", }); // wait 5 seconds to give it time for CHANNEL_CREATE event - await delay(5000); + await delay(3000); // Make sure the channel was created. if (!cache.channels.has(channel.id)) { throw new Error( - "The channel should have been created but it is not in the cache.", + "The channel should have been created but it is not in the cache." ); } // Delete the channel now without a reason await deleteChannel(tempData.guildId, channel.id); // wait 5 seconds to give it time for CHANNEL_DELETE event - await delay(5000); + await delay(3000); // Make sure it is gone from cache if (cache.channels.has(channel.id)) { throw new Error( - "The channel should have been deleted but it is still in cache.", + "The channel should have been deleted but it is still in cache." ); } }, @@ -42,22 +42,22 @@ Deno.test({ name: "delete-channel", }); // wait 5 seconds to give it time for CHANNEL_CREATE event - await delay(5000); + await delay(3000); // Make sure the channel was created. if (!cache.channels.has(channel.id)) { throw new Error( - "The channel should have been created but it is not in the cache.", + "The channel should have been created but it is not in the cache." ); } // Delete the channel now without a reason await deleteChannel(tempData.guildId, channel.id, "with a reason"); // wait 5 seconds to give it time for CHANNEL_DELETE event - await delay(5000); + await delay(3000); // Make sure it is gone from cache if (cache.channels.has(channel.id)) { throw new Error( - "The channel should have been deleted but it is still in cache.", + "The channel should have been deleted but it is still in cache." ); } }, diff --git a/tests/guilds/create_guild.ts b/tests/guilds/create_guild.ts index 6834e7632..d513c6d4b 100644 --- a/tests/guilds/create_guild.ts +++ b/tests/guilds/create_guild.ts @@ -17,7 +17,7 @@ Deno.test({ tempData.guildId = guild.id; // Delay the execution by 5 seconds to allow GUILD_CREATE event to be processed - await delay(5000); + await delay(3000); if (!cache.guilds.has(guild.id)) { throw new Error("The guild seemed to be created but it was not cached."); diff --git a/tests/messages/add_reaction.ts b/tests/messages/add_reaction.ts index 81870877e..de216d4a5 100644 --- a/tests/messages/add_reaction.ts +++ b/tests/messages/add_reaction.ts @@ -14,7 +14,7 @@ async function ifItFailsBlameWolf(type: "getter" | "raw", custom = false) { assertExists(message); // Delay the execution by 5 seconds to allow MESSAGE_CREATE event to be processed - await delay(5000); + await delay(3000); if (!cache.messages.has(message.id)) { throw new Error("The message seemed to be sent but it was not cached."); @@ -33,7 +33,7 @@ async function ifItFailsBlameWolf(type: "getter" | "raw", custom = false) { name: "blamewolf", image: "https://cdn.discordapp.com/emojis/814955268123000832.png", roles: [], - }, + } ) ).id }>`; @@ -45,16 +45,16 @@ async function ifItFailsBlameWolf(type: "getter" | "raw", custom = false) { await message.addReaction(emojiId); } - await delay(5000); + await delay(3000); assertEquals( await cache.messages .get(message.id) ?.reactions?.filter( (reaction: DiscordReaction) => - reaction.emoji?.name === (custom ? "blamewolf" : "❤"), + reaction.emoji?.name === (custom ? "blamewolf" : "❤") ).length, - 1, + 1 ); } diff --git a/tests/messages/add_reactions.ts b/tests/messages/add_reactions.ts index 6c8304b51..dc18b1494 100644 --- a/tests/messages/add_reactions.ts +++ b/tests/messages/add_reactions.ts @@ -11,7 +11,7 @@ import { assertEquals, assertExists } from "../deps.ts"; async function ifItFailsBlameWolf( type: "getter" | "raw", custom = false, - ordered = false, + ordered = false ) { const message = await sendMessage(tempData.channelId, "Hello World!"); @@ -19,12 +19,10 @@ async function ifItFailsBlameWolf( assertExists(message); // Delay the execution by 5 seconds to allow MESSAGE_CREATE event to be processed - await delay(5000); + await delay(3000); if (!cache.messages.has(message.id)) { - throw new Error( - "The message seemed to be sent but it was not cached.", - ); + throw new Error("The message seemed to be sent but it was not cached."); } let emojiIds = ["❤", "😃"]; @@ -32,28 +30,32 @@ async function ifItFailsBlameWolf( if (custom) { emojiIds = [ `<:blamewolf:${ - (await createEmoji( - tempData.guildId, - "blamewolf", - "https://cdn.discordapp.com/emojis/814955268123000832.png", - { - name: "blamewolf", - image: "https://cdn.discordapp.com/emojis/814955268123000832.png", - roles: [], - }, - )).id + ( + await createEmoji( + tempData.guildId, + "blamewolf", + "https://cdn.discordapp.com/emojis/814955268123000832.png", + { + name: "blamewolf", + image: "https://cdn.discordapp.com/emojis/814955268123000832.png", + roles: [], + } + ) + ).id }>`, `<:blamewolf2:${ - (await createEmoji( - tempData.guildId, - "blamewolf2", - "https://cdn.discordapp.com/emojis/814955268123000832.png", - { - name: "blamewolf2", - image: "https://cdn.discordapp.com/emojis/814955268123000832.png", - roles: [], - }, - )).id + ( + await createEmoji( + tempData.guildId, + "blamewolf2", + "https://cdn.discordapp.com/emojis/814955268123000832.png", + { + name: "blamewolf2", + image: "https://cdn.discordapp.com/emojis/814955268123000832.png", + roles: [], + } + ) + ).id }>`, ]; } @@ -64,12 +66,9 @@ async function ifItFailsBlameWolf( await message.addReactions(emojiIds, ordered); } - await delay(5000); + await delay(3000); - assertEquals( - await cache.messages.get(message.id)?.reactions?.length, - 2, - ); + assertEquals(await cache.messages.get(message.id)?.reactions?.length, 2); } Deno.test({ diff --git a/tests/messages/create_message.ts b/tests/messages/create_message.ts index 60ad0a39e..164c6587e 100644 --- a/tests/messages/create_message.ts +++ b/tests/messages/create_message.ts @@ -12,17 +12,18 @@ async function ifItFailsBlameWolf(type: "getter" | "raw") { assertExists(channel); // Wait few seconds for the channel create event to arrive and cache it - await delay(5000); + await delay(3000); - const message = type === "raw" - ? await sendMessage(channel.id, "Hello World!") - : await channel.send("Hello World!"); + const message = + type === "raw" + ? await sendMessage(channel.id, "Hello World!") + : await channel.send("Hello World!"); // Assertions assertExists(message); // Delay the execution by 5 seconds to allow MESSAGE_CREATE event to be processed - await delay(5000); + await delay(3000); if (!cache.messages.has(message.id)) { throw new Error("The message seemed to be sent but it was not cached."); diff --git a/tests/messages/delete_message.ts b/tests/messages/delete_message.ts index 371a79a9f..ac80dd00d 100644 --- a/tests/messages/delete_message.ts +++ b/tests/messages/delete_message.ts @@ -11,12 +11,10 @@ async function ifItFailsBlameWolf(type: "getter" | "raw", reason?: string) { // Assertions assertExists(message); // Delay the execution by 5 seconds to allow MESSAGE_CREATE event to be processed - await delay(5000); + await delay(3000); // Make sure the message was created. if (!cache.messages.has(message.id)) { - throw new Error( - "The message seemed to be sent but it was not cached.", - ); + throw new Error("The message seemed to be sent but it was not cached."); } // Delete the message now @@ -27,11 +25,11 @@ async function ifItFailsBlameWolf(type: "getter" | "raw", reason?: string) { } // Wait 5 seconds to give it time for MESSAGE_DELETE event - await delay(5000); + await delay(3000); // Make sure it is gone from cache if (cache.messages.has(message.id)) { throw new Error( - "The message should have been deleted but it is still in cache.", + "The message should have been deleted but it is still in cache." ); } } diff --git a/tests/messages/delete_messages.ts b/tests/messages/delete_messages.ts index d35482850..5072aaba3 100644 --- a/tests/messages/delete_messages.ts +++ b/tests/messages/delete_messages.ts @@ -14,12 +14,10 @@ async function ifItFailsBlameWolf(reason?: string) { // Assertions assertExists(message); // Delay the execution by 5 seconds to allow MESSAGE_CREATE event to be processed - await delay(5000); + await delay(3000); // Make sure the message was created. if (!cache.messages.has(message.id)) { - throw new Error( - "The message seemed to be sent but it was not cached.", - ); + throw new Error("The message seemed to be sent but it was not cached."); } const secondMessage = await sendMessage(tempData.channelId, "Hello World 2!"); @@ -27,27 +25,25 @@ async function ifItFailsBlameWolf(reason?: string) { // Assertions assertExists(secondMessage); // Delay the execution by 5 seconds to allow MESSAGE_CREATE event to be processed - await delay(5000); + await delay(3000); // Make sure the message was created. if (!cache.messages.has(secondMessage.id)) { - throw new Error( - "The message seemed to be sent but it was not cached.", - ); + throw new Error("The message seemed to be sent but it was not cached."); } // Delete the message now await deleteMessages( tempData.channelId, [message.id, secondMessage.id], - reason, + reason ); // Wait 5 seconds to give it time for MESSAGE_DELETE event - await delay(5000); + await delay(3000); // Make sure it is gone from cache if (cache.messages.has(message.id) || cache.messages.has(secondMessage.id)) { throw new Error( - "The message should have been deleted but it is still in cache.", + "The message should have been deleted but it is still in cache." ); } } diff --git a/tests/messages/edit_message.ts b/tests/messages/edit_message.ts index 830d3af89..56d86d5cc 100644 --- a/tests/messages/edit_message.ts +++ b/tests/messages/edit_message.ts @@ -11,7 +11,7 @@ async function ifItFailsBlameWolf(type: "getter" | "raw") { // Assertions assertExists(message); // Delay the execution by 5 seconds to allow MESSAGE_CREATE event to be processed - await delay(5000); + await delay(3000); // Make sure the message was created. if (!cache.messages.has(message.id)) { throw new Error("The message seemed to be sent but it was not cached."); @@ -24,7 +24,7 @@ async function ifItFailsBlameWolf(type: "getter" | "raw") { await message.edit("Goodbye World!"); } // Wait 5 seconds to give it time for MESSAGE_UPDATE event - await delay(5000); + await delay(3000); // Make sure it has been modified in cache assertEquals(cache.messages.get(message.id)?.content, "Goodbye World!"); diff --git a/tests/messages/get_message.ts b/tests/messages/get_message.ts index eae99f906..6d8c603af 100644 --- a/tests/messages/get_message.ts +++ b/tests/messages/get_message.ts @@ -10,12 +10,10 @@ Deno.test({ // Assertions assertExists(message); // Delay the execution by 5 seconds to allow MESSAGE_CREATE event to be processed - await delay(5000); + await delay(3000); // Make sure the message was created. if (!cache.messages.has(message.id)) { - throw new Error( - "The message seemed to be sent but it was not cached.", - ); + throw new Error("The message seemed to be sent but it was not cached."); } // Fetch the message diff --git a/tests/messages/get_messages.ts b/tests/messages/get_messages.ts index 58de29726..d556fcc9f 100644 --- a/tests/messages/get_messages.ts +++ b/tests/messages/get_messages.ts @@ -8,11 +8,11 @@ Deno.test({ const message = await sendMessage(tempData.channelId, "Hello World!"); const secondMessage = await sendMessage( tempData.channelId, - "Hello World 2!", + "Hello World 2!" ); const thirdMessage = await sendMessage( tempData.channelId, - "Hello World 3!", + "Hello World 3!" ); // Assertions @@ -20,16 +20,14 @@ Deno.test({ assertExists(secondMessage); assertExists(thirdMessage); // Delay the execution by 5 seconds to allow MESSAGE_CREATE event to be processed - await delay(5000); + await delay(3000); // Make sure the message was created. if ( !cache.messages.has(message.id) || !cache.messages.has(secondMessage.id) || !cache.messages.has(thirdMessage.id) ) { - throw new Error( - "The message seemed to be sent but it was not cached.", - ); + throw new Error("The message seemed to be sent but it was not cached."); } // Fetch the messages diff --git a/tests/messages/get_reactions.ts b/tests/messages/get_reactions.ts index 591c5f44f..5a6219f7e 100644 --- a/tests/messages/get_reactions.ts +++ b/tests/messages/get_reactions.ts @@ -16,12 +16,10 @@ Deno.test({ // Assertions assertExists(message); // Delay the execution by 5 seconds to allow MESSAGE_CREATE event to be processed - await delay(5000); + await delay(3000); // Make sure the message was created. if (!cache.messages.has(message.id)) { - throw new Error( - "The message seemed to be sent but it was not cached.", - ); + throw new Error("The message seemed to be sent but it was not cached."); } await addReaction(message.channelId, message.id, "❤"); @@ -30,7 +28,7 @@ Deno.test({ const fetchedReactions = await getReactions( tempData.channelId, message.id, - "❤", + "❤" ); // Check if getMessage has worked assertEquals(fetchedReactions.size, 1); diff --git a/tests/messages/pin_message.ts b/tests/messages/pin_message.ts index 5c9522790..d5ab28133 100644 --- a/tests/messages/pin_message.ts +++ b/tests/messages/pin_message.ts @@ -10,12 +10,10 @@ async function ifItFailsBlameWolf(type: "getter" | "raw") { assertExists(message); // Delay the execution by 5 seconds to allow MESSAGE_CREATE event to be processed - await delay(5000); + await delay(3000); if (!cache.messages.has(message.id)) { - throw new Error( - "The message seemed to be sent but it was not cached.", - ); + throw new Error("The message seemed to be sent but it was not cached."); } if (type === "raw") { @@ -27,7 +25,7 @@ async function ifItFailsBlameWolf(type: "getter" | "raw") { const pins = await getPins(tempData.channelId); assertEquals( pins.filter((msg: DiscordenoMessage) => msg.id === message.id).length, - 1, + 1 ); } diff --git a/tests/messages/remove_all_reactions.ts b/tests/messages/remove_all_reactions.ts index 8413ca185..aedf3a8eb 100644 --- a/tests/messages/remove_all_reactions.ts +++ b/tests/messages/remove_all_reactions.ts @@ -14,24 +14,19 @@ async function ifItFailsBlameWolf(type: "getter" | "raw") { // Assertions assertExists(message); // Delay the execution by 5 seconds to allow MESSAGE_CREATE event to be processed - await delay(5000); + await delay(3000); // Make sure the message was created. if (!cache.messages.has(message.id)) { - throw new Error( - "The message seemed to be sent but it was not cached.", - ); + throw new Error("The message seemed to be sent but it was not cached."); } // Add reactions to the message await addReactions(message.channelId, message.id, ["❤", "😃", "🤫"]); // Delay the execution by 5 seconds to allow MESSAGE_REACTION_ALL event to be processed - await delay(5000); + await delay(3000); // Be sure that the message has the reactions - assertEquals( - await cache.messages.get(message.id)?.reactions?.length, - 3, - ); + assertEquals(await cache.messages.get(message.id)?.reactions?.length, 3); if (type === "raw") { await removeAllReactions(message.channelId, message.id); @@ -40,13 +35,10 @@ async function ifItFailsBlameWolf(type: "getter" | "raw") { } // Delay the execution by 5 seconds to allow MESSAGE_REACTION_REMOVE_ALL event to be processed - await delay(5000); + await delay(3000); // Check if the reactions has been deleted - assertEquals( - await cache.messages.get(message.id)?.reactions, - undefined, - ); + assertEquals(await cache.messages.get(message.id)?.reactions, undefined); } Deno.test({ diff --git a/tests/messages/remove_reaction.ts b/tests/messages/remove_reaction.ts index db03da756..44bbc4f15 100644 --- a/tests/messages/remove_reaction.ts +++ b/tests/messages/remove_reaction.ts @@ -14,24 +14,19 @@ async function ifItFailsBlameWolf(type: "getter" | "raw") { // Assertions assertExists(message); // Delay the execution by 5 seconds to allow MESSAGE_CREATE event to be processed - await delay(5000); + await delay(3000); // Make sure the message was created. if (!cache.messages.has(message.id)) { - throw new Error( - "The message seemed to be sent but it was not cached.", - ); + throw new Error("The message seemed to be sent but it was not cached."); } // Add reactions to the message await addReaction(message.channelId, message.id, "❤"); // Delay the execution by 5 seconds to allow MESSAGE_REACTION_ALL event to be processed - await delay(5000); + await delay(3000); // Be sure that the message has the reactions - assertEquals( - await cache.messages.get(message.id)?.reactions?.length, - 1, - ); + assertEquals(await cache.messages.get(message.id)?.reactions?.length, 1); if (type === "raw") { await removeReaction(message.channelId, message.id, "❤"); @@ -40,13 +35,10 @@ async function ifItFailsBlameWolf(type: "getter" | "raw") { } // Delay the execution by 5 seconds to allow MESSAGE_REACTION_REMOVE_ALL event to be processed - await delay(5000); + await delay(3000); // Check if the reactions has been deleted - assertEquals( - await cache.messages.get(message.id)?.reactions, - undefined, - ); + assertEquals(await cache.messages.get(message.id)?.reactions, undefined); } Deno.test({ diff --git a/tests/messages/remove_reaction_emoji.ts b/tests/messages/remove_reaction_emoji.ts index ed9194f04..86451a9bb 100644 --- a/tests/messages/remove_reaction_emoji.ts +++ b/tests/messages/remove_reaction_emoji.ts @@ -15,24 +15,19 @@ async function ifItFailsBlameWolf(type: "getter" | "raw") { // Assertions assertExists(message); // Delay the execution by 5 seconds to allow MESSAGE_CREATE event to be processed - await delay(5000); + await delay(3000); // Make sure the message was created. if (!cache.messages.has(message.id)) { - throw new Error( - "The message seemed to be sent but it was not cached.", - ); + throw new Error("The message seemed to be sent but it was not cached."); } // Add reactions to the message await addReaction(message.channelId, message.id, "❤"); // Delay the execution by 5 seconds to allow MESSAGE_REACTION_ALL event to be processed - await delay(5000); + await delay(3000); // Be sure that the message has the reactions - assertEquals( - await cache.messages.get(message.id)?.reactions?.length, - 1, - ); + assertEquals(await cache.messages.get(message.id)?.reactions?.length, 1); if (type === "raw") { await removeReactionEmoji(message.channelId, message.id, "❤"); @@ -41,13 +36,10 @@ async function ifItFailsBlameWolf(type: "getter" | "raw") { } // Delay the execution by 5 seconds to allow MESSAGE_REACTION_REMOVE_ALL event to be processed - await delay(5000); + await delay(3000); // Check if the reactions has been deleted - assertEquals( - await cache.messages.get(message.id)?.reactions, - undefined, - ); + assertEquals(cache.messages.get(message.id)?.reactions, undefined); } Deno.test({ diff --git a/tests/messages/remove_user_reaction.ts b/tests/messages/remove_user_reaction.ts index cd22ea735..8aeeb4925 100644 --- a/tests/messages/remove_user_reaction.ts +++ b/tests/messages/remove_user_reaction.ts @@ -15,44 +15,36 @@ async function ifItFailsBlameWolf(type: "getter" | "raw") { // Assertions assertExists(message); // Delay the execution by 5 seconds to allow MESSAGE_CREATE event to be processed - await delay(5000); + await delay(3000); // Make sure the message was created. if (!cache.messages.has(message.id)) { - throw new Error( - "The message seemed to be sent but it was not cached.", - ); + throw new Error("The message seemed to be sent but it was not cached."); } // Add reactions to the message await addReaction(message.channelId, message.id, "❤"); // Delay the execution by 5 seconds to allow MESSAGE_REACTION_ALL event to be processed - await delay(5000); + await delay(3000); // Be sure that the message has the reactions - assertEquals( - await cache.messages.get(message.id)?.reactions?.length, - 1, - ); + assertEquals(await cache.messages.get(message.id)?.reactions?.length, 1); if (type === "raw") { await removeUserReaction( message.channelId, message.id, "❤", - message.author.id, + message.author.id ); } else { //await message.removeUserReaction("❤", message.author.id); } // Delay the execution by 5 seconds to allow MESSAGE_REACTION_REMOVE_ALL event to be processed - await delay(5000); + await delay(3000); // Check if the reactions has been deleted - assertEquals( - await cache.messages.get(message.id)?.reactions, - undefined, - ); + assertEquals(await cache.messages.get(message.id)?.reactions, undefined); } Deno.test({ diff --git a/tests/messages/unpin_message.ts b/tests/messages/unpin_message.ts index eea680590..1637e779d 100644 --- a/tests/messages/unpin_message.ts +++ b/tests/messages/unpin_message.ts @@ -10,12 +10,10 @@ async function ifItFailsBlameWolf(type: "getter" | "raw") { assertExists(message); // Delay the execution by 5 seconds to allow MESSAGE_CREATE event to be processed - await delay(5000); + await delay(3000); if (!cache.messages.has(message.id)) { - throw new Error( - "The message seemed to be sent but it was not cached.", - ); + throw new Error("The message seemed to be sent but it was not cached."); } await pin(message.channelId, message.id); @@ -24,7 +22,7 @@ async function ifItFailsBlameWolf(type: "getter" | "raw") { const pins = await getPins(tempData.channelId); assertEquals( pins.filter((msg: DiscordenoMessage) => msg.id === message.id).length, - 1, + 1 ); if (type === "raw") { @@ -38,7 +36,7 @@ async function ifItFailsBlameWolf(type: "getter" | "raw") { assertEquals( removedPins.filter((msg: DiscordenoMessage) => msg.id === message.id) .length, - 0, + 0 ); } diff --git a/tests/mod.ts b/tests/mod.ts index 993f7f514..e2c739dc9 100644 --- a/tests/mod.ts +++ b/tests/mod.ts @@ -19,6 +19,10 @@ import "./channels/delete_channel.ts"; // Messages tests import "./messages/add_reaction.ts"; import "./messages/add_reactions.ts"; +import "./messages/remove_all_reactions.ts"; +import "./messages/remove_reaction.ts"; +import "./messages/remove_reaction_emoji.ts"; +import "./messages/remove_user_reaction.ts"; import "./messages/create_message.ts"; import "./messages/delete_message.ts"; import "./messages/delete_messages.ts"; @@ -27,10 +31,6 @@ import "./messages/get_message.ts"; import "./messages/get_messages.ts"; import "./messages/get_reactions.ts"; import "./messages/pin_message.ts"; -import "./messages/remove_all_reactions.ts"; -import "./messages/remove_reaction.ts"; -import "./messages/remove_reaction_emoji.ts"; -import "./messages/remove_user_reaction.ts"; import "./messages/unpin_message.ts"; // Final cleanup diff --git a/tests/util/delay_until.ts b/tests/util/delay_until.ts new file mode 100644 index 000000000..81bd94839 --- /dev/null +++ b/tests/util/delay_until.ts @@ -0,0 +1,7 @@ +export function delayUntil(maxMs: number, isReady: () => boolean) { + const maxed = Date.now() + maxMs; + + while (Date.now() < maxed) { + if (isReady()) return; + } +} diff --git a/tests/ws/start_bot.ts b/tests/ws/start_bot.ts index 8111a6233..4db8378c0 100644 --- a/tests/ws/start_bot.ts +++ b/tests/ws/start_bot.ts @@ -40,11 +40,12 @@ Deno.test({ "GUILDS", "GUILD_EMOJIS", "GUILD_MESSAGE_REACTIONS", + "GUILD_EMOJIS", ], }); // Delay the execution by 5 seconds - await delay(5000); + await delay(3000); // DELETE GUILDS IF LESS THAN 10 SERVERS AS SAFETY MEASURE if (cache.guilds.size <= 10) { @@ -147,7 +148,7 @@ Deno.test({ // }) as Channel; // // Wait 5s for CHANNEL_UPDATE to fire -// await delay(5000); +// await delay(3000); // // Assertions // assertExists(channel);