diff --git a/tests/messages/get_message.ts b/tests/messages/get_message.ts new file mode 100644 index 000000000..eae99f906 --- /dev/null +++ b/tests/messages/get_message.ts @@ -0,0 +1,28 @@ +import { cache, delay, getMessage, sendMessage } from "../../mod.ts"; +import { defaultTestOptions, tempData } from "../ws/start_bot.ts"; +import { assertEquals, assertExists } from "../deps.ts"; + +Deno.test({ + name: "[message] fetch a message", + async fn() { + const message = await sendMessage(tempData.channelId, "Hello World!"); + + // Assertions + assertExists(message); + // Delay the execution by 5 seconds to allow MESSAGE_CREATE event to be processed + await delay(5000); + // 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.", + ); + } + + // Fetch the message + const fetchedMessage = await getMessage(tempData.channelId, message.id); + // Check if getMessage has worked + assertEquals(fetchedMessage.id, message.id); + assertEquals(fetchedMessage.content, message.content); + }, + ...defaultTestOptions, +}); diff --git a/tests/messages/get_messages.ts b/tests/messages/get_messages.ts new file mode 100644 index 000000000..58de29726 --- /dev/null +++ b/tests/messages/get_messages.ts @@ -0,0 +1,44 @@ +import { cache, delay, getMessages, sendMessage } from "../../mod.ts"; +import { defaultTestOptions, tempData } from "../ws/start_bot.ts"; +import { assertEquals, assertExists } from "../deps.ts"; + +Deno.test({ + name: "[message] fetch messages", + async fn() { + const message = await sendMessage(tempData.channelId, "Hello World!"); + const secondMessage = await sendMessage( + tempData.channelId, + "Hello World 2!", + ); + const thirdMessage = await sendMessage( + tempData.channelId, + "Hello World 3!", + ); + + // Assertions + assertExists(message); + assertExists(secondMessage); + assertExists(thirdMessage); + // Delay the execution by 5 seconds to allow MESSAGE_CREATE event to be processed + await delay(5000); + // 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.", + ); + } + + // Fetch the messages + const fetchedMessages = await getMessages(tempData.channelId, { + after: message.id, + limit: 2, + }); + // Check if getMessages has worked + assertEquals(fetchedMessages.length, 2); + }, + ...defaultTestOptions, +}); diff --git a/tests/messages/get_reactions.ts b/tests/messages/get_reactions.ts new file mode 100644 index 000000000..591c5f44f --- /dev/null +++ b/tests/messages/get_reactions.ts @@ -0,0 +1,39 @@ +import { + addReaction, + cache, + delay, + getReactions, + sendMessage, +} from "../../mod.ts"; +import { defaultTestOptions, tempData } from "../ws/start_bot.ts"; +import { assertEquals, assertExists } from "../deps.ts"; + +Deno.test({ + name: "[message] fetch reactions", + async fn() { + const message = await sendMessage(tempData.channelId, "Hello World!"); + + // Assertions + assertExists(message); + // Delay the execution by 5 seconds to allow MESSAGE_CREATE event to be processed + await delay(5000); + // 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.", + ); + } + + await addReaction(message.channelId, message.id, "❤"); + + // Fetch the message + const fetchedReactions = await getReactions( + tempData.channelId, + message.id, + "❤", + ); + // Check if getMessage has worked + assertEquals(fetchedReactions.size, 1); + }, + ...defaultTestOptions, +}); diff --git a/tests/messages/pin_message.ts b/tests/messages/pin_message.ts new file mode 100644 index 000000000..5c9522790 --- /dev/null +++ b/tests/messages/pin_message.ts @@ -0,0 +1,48 @@ +import { cache, delay, getPins, pin, sendMessage } from "../../mod.ts"; +import { defaultTestOptions, tempData } from "../ws/start_bot.ts"; +import { assertEquals, assertExists } from "../deps.ts"; +import { DiscordenoMessage } from "../../src/structures/message.ts"; + +async function ifItFailsBlameWolf(type: "getter" | "raw") { + const message = await sendMessage(tempData.channelId, "Hello World!"); + + // Assertions + assertExists(message); + + // Delay the execution by 5 seconds to allow MESSAGE_CREATE event to be processed + await delay(5000); + + if (!cache.messages.has(message.id)) { + throw new Error( + "The message seemed to be sent but it was not cached.", + ); + } + + if (type === "raw") { + await pin(message.channelId, message.id); + } else { + await message.pin(); + } + + const pins = await getPins(tempData.channelId); + assertEquals( + pins.filter((msg: DiscordenoMessage) => msg.id === message.id).length, + 1, + ); +} + +Deno.test({ + name: "[message] pin a message", + async fn() { + await ifItFailsBlameWolf("raw"); + }, + ...defaultTestOptions, +}); + +Deno.test({ + name: "[message] message.pin()", + async fn() { + await ifItFailsBlameWolf("getter"); + }, + ...defaultTestOptions, +}); diff --git a/tests/messages/remove_all_reactions.ts b/tests/messages/remove_all_reactions.ts new file mode 100644 index 000000000..72454a5ba --- /dev/null +++ b/tests/messages/remove_all_reactions.ts @@ -0,0 +1,66 @@ +import { + addReactions, + cache, + delay, + removeAllReactions, + sendMessage, +} from "../../mod.ts"; +import { defaultTestOptions, tempData } from "../ws/start_bot.ts"; +import { assertEquals, assertExists } from "../deps.ts"; + +async function ifItFailsBlameWolf(type: "getter" | "raw") { + const message = await sendMessage(tempData.channelId, "Hello World!"); + + // Assertions + assertExists(message); + // Delay the execution by 5 seconds to allow MESSAGE_CREATE event to be processed + await delay(5000); + // 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.", + ); + } + + // 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); + + // Be sure that the message has the reactions + assertEquals( + await cache.messages.get(message.id)?.reactions?.length, + 3, + ); + + if (type === "raw") { + await removeAllReactions(message.channelId, message.id); + } else { + await message.removeAllReactions(); + } + + // Delay the execution by 5 seconds to allow MESSAGE_REACTION_REMOVE_ALL event to be processed + await delay(5000); + + // Check if the reactions has been deleted + assertEquals( + await cache.messages.get(message.id)?.reactions?.length, + 0, + ); +} + +Deno.test({ + name: "[message] remove all reactions", + async fn() { + await ifItFailsBlameWolf("raw"); + }, + ...defaultTestOptions, +}); + +Deno.test({ + name: "[message] message.removeAllReactions()", + async fn() { + await ifItFailsBlameWolf("getter"); + }, + ...defaultTestOptions, +}); diff --git a/tests/messages/remove_reaction.ts b/tests/messages/remove_reaction.ts new file mode 100644 index 000000000..50f1e549f --- /dev/null +++ b/tests/messages/remove_reaction.ts @@ -0,0 +1,66 @@ +import { + addReaction, + cache, + delay, + removeReaction, + sendMessage, +} from "../../mod.ts"; +import { defaultTestOptions, tempData } from "../ws/start_bot.ts"; +import { assertEquals, assertExists } from "../deps.ts"; + +async function ifItFailsBlameWolf(type: "getter" | "raw") { + const message = await sendMessage(tempData.channelId, "Hello World!"); + + // Assertions + assertExists(message); + // Delay the execution by 5 seconds to allow MESSAGE_CREATE event to be processed + await delay(5000); + // 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.", + ); + } + + // 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); + + // Be sure that the message has the reactions + assertEquals( + await cache.messages.get(message.id)?.reactions?.length, + 1, + ); + + if (type === "raw") { + await removeReaction(message.channelId, message.id, "❤"); + } else { + await message.removeReaction("❤"); + } + + // Delay the execution by 5 seconds to allow MESSAGE_REACTION_REMOVE_ALL event to be processed + await delay(5000); + + // Check if the reactions has been deleted + assertEquals( + await cache.messages.get(message.id)?.reactions?.length, + 0, + ); +} + +Deno.test({ + name: "[message] remove a reaction", + async fn() { + await ifItFailsBlameWolf("raw"); + }, + ...defaultTestOptions, +}); + +Deno.test({ + name: "[message] message.removeReaction()", + async fn() { + await ifItFailsBlameWolf("getter"); + }, + ...defaultTestOptions, +}); diff --git a/tests/messages/remove_reaction_emoji.ts b/tests/messages/remove_reaction_emoji.ts new file mode 100644 index 000000000..7d4814806 --- /dev/null +++ b/tests/messages/remove_reaction_emoji.ts @@ -0,0 +1,67 @@ +import { + addReaction, + cache, + delay, + removeReaction, + removeReactionEmoji, + sendMessage, +} from "../../mod.ts"; +import { defaultTestOptions, tempData } from "../ws/start_bot.ts"; +import { assertEquals, assertExists } from "../deps.ts"; + +async function ifItFailsBlameWolf(type: "getter" | "raw") { + const message = await sendMessage(tempData.channelId, "Hello World!"); + + // Assertions + assertExists(message); + // Delay the execution by 5 seconds to allow MESSAGE_CREATE event to be processed + await delay(5000); + // 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.", + ); + } + + // 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); + + // Be sure that the message has the reactions + assertEquals( + await cache.messages.get(message.id)?.reactions?.length, + 1, + ); + + if (type === "raw") { + await removeReactionEmoji(message.channelId, message.id, "❤"); + } else { + await message.removeReactionEmoji("❤"); + } + + // Delay the execution by 5 seconds to allow MESSAGE_REACTION_REMOVE_ALL event to be processed + await delay(5000); + + // Check if the reactions has been deleted + assertEquals( + await cache.messages.get(message.id)?.reactions?.length, + 0, + ); +} + +Deno.test({ + name: "[message] remove a reaction emoji", + async fn() { + await ifItFailsBlameWolf("raw"); + }, + ...defaultTestOptions, +}); + +Deno.test({ + name: "[message] message.removeReactionEmoji()", + async fn() { + await ifItFailsBlameWolf("getter"); + }, + ...defaultTestOptions, +}); diff --git a/tests/messages/remove_user_reaction.ts b/tests/messages/remove_user_reaction.ts new file mode 100644 index 000000000..5cdd48e1b --- /dev/null +++ b/tests/messages/remove_user_reaction.ts @@ -0,0 +1,72 @@ +import { + addReaction, + cache, + delay, + removeReaction, + removeUserReaction, + sendMessage, +} from "../../mod.ts"; +import { defaultTestOptions, tempData } from "../ws/start_bot.ts"; +import { assertEquals, assertExists } from "../deps.ts"; + +async function ifItFailsBlameWolf(type: "getter" | "raw") { + const message = await sendMessage(tempData.channelId, "Hello World!"); + + // Assertions + assertExists(message); + // Delay the execution by 5 seconds to allow MESSAGE_CREATE event to be processed + await delay(5000); + // 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.", + ); + } + + // 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); + + // Be sure that the message has the reactions + assertEquals( + await cache.messages.get(message.id)?.reactions?.length, + 1, + ); + + if (type === "raw") { + await removeUserReaction( + message.channelId, + message.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); + + // Check if the reactions has been deleted + assertEquals( + await cache.messages.get(message.id)?.reactions?.length, + 0, + ); +} + +Deno.test({ + name: "[message] remove a user reaction", + async fn() { + await ifItFailsBlameWolf("raw"); + }, + ...defaultTestOptions, +}); +/* +Deno.test({ + name: "[message] message.removeReaction()", + async fn() { + await ifItFailsBlameWolf("getter"); + }, + ...defaultTestOptions, +});*/ diff --git a/tests/messages/unpin_message.ts b/tests/messages/unpin_message.ts new file mode 100644 index 000000000..eea680590 --- /dev/null +++ b/tests/messages/unpin_message.ts @@ -0,0 +1,60 @@ +import { cache, delay, getPins, pin, sendMessage, unpin } from "../../mod.ts"; +import { defaultTestOptions, tempData } from "../ws/start_bot.ts"; +import { assertEquals, assertExists } from "../deps.ts"; +import { DiscordenoMessage } from "../../src/structures/message.ts"; + +async function ifItFailsBlameWolf(type: "getter" | "raw") { + const message = await sendMessage(tempData.channelId, "Hello World!"); + + // Assertions + assertExists(message); + + // Delay the execution by 5 seconds to allow MESSAGE_CREATE event to be processed + await delay(5000); + + if (!cache.messages.has(message.id)) { + throw new Error( + "The message seemed to be sent but it was not cached.", + ); + } + + await pin(message.channelId, message.id); + + // Be sure that the pin got added + const pins = await getPins(tempData.channelId); + assertEquals( + pins.filter((msg: DiscordenoMessage) => msg.id === message.id).length, + 1, + ); + + if (type === "raw") { + await unpin(message.channelId, message.id); + } else { + //await message.unpin(); + } + + // Be sure that the pin got removed + const removedPins = await getPins(tempData.channelId); + assertEquals( + removedPins.filter((msg: DiscordenoMessage) => msg.id === message.id) + .length, + 0, + ); +} + +Deno.test({ + name: "[message] unpin a message", + async fn() { + await ifItFailsBlameWolf("raw"); + }, + ...defaultTestOptions, +}); + +/* +Deno.test({ + name: "[message] message.unpin()", + async fn() { + await ifItFailsBlameWolf("getter"); + }, + ...defaultTestOptions, +});*/ diff --git a/tests/mod.ts b/tests/mod.ts index c799d6ff3..8714ca26f 100644 --- a/tests/mod.ts +++ b/tests/mod.ts @@ -22,6 +22,15 @@ import "./messages/create_message.ts"; import "./messages/delete_message.ts"; import "./messages/delete_messages.ts"; import "./messages/edit_message.ts"; +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 import "./guilds/delete_server.ts";