diff --git a/src/cache.ts b/src/cache.ts index 382717697..f6eba8f9d 100644 --- a/src/cache.ts +++ b/src/cache.ts @@ -188,6 +188,15 @@ export function createExecute(cache: Cache): CacheExecutor { return cached; }) .filter((m: DiscordenoMessage) => m); + case "GUILD_MEMBER_CHUNK": + options.users.forEach((user) => { + cache.users.set(user.id, user); + }); + // TODO: FIND A GOOD WAY FOR MEMBERS CACHE (GUILD ID) + // options.members.forEach((member) => { + // cache.members.set(member.id, member); + // }); + return; } }; } diff --git a/tests/helpers/channels/createChannel.ts b/tests/helpers/channels/create_channel.ts similarity index 100% rename from tests/helpers/channels/createChannel.ts rename to tests/helpers/channels/create_channel.ts diff --git a/tests/helpers/channels/deleteChannel.ts b/tests/helpers/channels/delete_channel.ts similarity index 100% rename from tests/helpers/channels/deleteChannel.ts rename to tests/helpers/channels/delete_channel.ts diff --git a/tests/helpers/emojis/create_emoji.ts b/tests/helpers/emojis/create_emoji.ts new file mode 100644 index 000000000..5ad7581a9 --- /dev/null +++ b/tests/helpers/emojis/create_emoji.ts @@ -0,0 +1,21 @@ +import { Bot } from "../../../src/bot.ts"; +import { assertExists } from "../../deps.ts"; +import { delayUntil } from "../../utils.ts"; + +export async function createEmojiTest(bot: Bot, guildId: bigint, t: Deno.TestContext) { + const emoji = await bot.helpers.createEmoji(guildId, { + name: "blamewolf", + image: "https://cdn.discordapp.com/emojis/814955268123000832.png", + roles: [], + }); + + // Assertions + assertExists(emoji); + + // Delay the execution to allow event to be processed + await delayUntil(10000, async () => (await bot.cache.guilds.get(guildId))?.emojis?.has(emoji.id)); + + if (!(await bot.cache.guilds.get(guildId))?.emojis?.has(emoji.id)) { + throw new Error("The emoji seemed to be created but it was not cached."); + } +} diff --git a/tests/helpers/emojis/delete_emoji.ts b/tests/helpers/emojis/delete_emoji.ts new file mode 100644 index 000000000..3cb3cdde5 --- /dev/null +++ b/tests/helpers/emojis/delete_emoji.ts @@ -0,0 +1,37 @@ +import { Bot } from "../../../src/bot.ts"; +import { assertExists } from "../../deps.ts"; +import { delayUntil } from "../../utils.ts"; + +export async function ifItFailsBlameWolf(bot: Bot, guildId: bigint, reason?: string) { + const emoji = await bot.helpers.createEmoji(guildId, { + name: "blamewolf", + image: "https://cdn.discordapp.com/emojis/814955268123000832.png", + roles: [], + }); + + // Assertions + assertExists(emoji); + + // Delay the execution to allow event to be processed + await delayUntil(10000, async () => (await bot.cache.guilds.get(guildId))?.emojis?.has(emoji.id)); + + if (!(await bot.cache.guilds.get(guildId))?.emojis?.has(emoji.id)) { + throw new Error("The emoji seemed to be created but it was not cached."); + } + + await bot.helpers.deleteEmoji(guildId, emoji.id, reason); + + await delayUntil(10000, async () => !(await bot.cache.guilds.get(guildId))?.emojis?.has(emoji.id)); + + if ((await bot.cache.guilds.get(guildId))?.emojis?.has(emoji.id)) { + throw new Error("The emoji seemed to be deleted but it's still cached."); + } +} + +export async function deleteEmojiWithReasonTest(bot: Bot, guildId: bigint, t: Deno.TestContext) { + await ifItFailsBlameWolf(bot, guildId); +} + +export async function deleteEmojiWithoutReasonTest(bot: Bot, guildId: bigint, t: Deno.TestContext) { + await ifItFailsBlameWolf(bot, guildId, "with a reason"); +} diff --git a/tests/helpers/emojis/edit_emoji.ts b/tests/helpers/emojis/edit_emoji.ts new file mode 100644 index 000000000..b0ecab94c --- /dev/null +++ b/tests/helpers/emojis/edit_emoji.ts @@ -0,0 +1,34 @@ +import { Bot } from "../../../src/bot.ts"; +import { assertExists } from "../../deps.ts"; +import { delayUntil } from "../../utils.ts"; + +export async function editEmojiTest(bot: Bot, guildId: bigint, t: Deno.TestContext) { + const emoji = await bot.helpers.createEmoji(guildId, { + name: "blamewolf", + image: "https://cdn.discordapp.com/emojis/814955268123000832.png", + roles: [], + }); + + // Assertions + assertExists(emoji); + + // Delay the execution to allow event to be processed + await delayUntil(10000, async () => (await bot.cache.guilds.get(guildId))?.emojis?.has(emoji.id)); + + if (!(await bot.cache.guilds.get(guildId))?.emojis?.has(emoji.id)) { + throw new Error("The emoji seemed to be created but it was not cached."); + } + + await bot.helpers.editEmoji(guildId, emoji.id, { + name: "blamewolf_infinite", + }); + + await delayUntil( + 10000, + async () => (await bot.cache.guilds.get(guildId))?.emojis?.get(emoji.id)?.name === "blamewolf_infinite" + ); + + if ((await bot.cache.guilds.get(guildId))?.emojis?.get(emoji.id)?.name !== "blamewolf_infinite") { + throw new Error("The emoji seemed to be edited but the cache was not updated."); + } +} diff --git a/tests/helpers/emojis/get_emoji.ts b/tests/helpers/emojis/get_emoji.ts new file mode 100644 index 000000000..ea94ad6a7 --- /dev/null +++ b/tests/helpers/emojis/get_emoji.ts @@ -0,0 +1,35 @@ +import { Bot } from "../../../src/bot.ts"; +import { assertExists } from "../../deps.ts"; +import { delayUntil } from "../../utils.ts"; + +export async function getEmojiTest(bot: Bot, guildId: bigint, t: Deno.TestContext) { + const emoji = await bot.helpers.createEmoji(guildId, { + name: "blamewolf", + image: "https://cdn.discordapp.com/emojis/814955268123000832.png", + roles: [], + }); + + // Assertions + assertExists(emoji); + + // Delay the execution to allow event to be processed + await delayUntil(10000, async () => (await bot.cache.guilds.get(guildId))?.emojis?.has(emoji.id)); + + if (!(await bot.cache.guilds.get(guildId))?.emojis?.has(emoji.id)) { + throw new Error("The emoji seemed to be created but it was not cached."); + } + + (await bot.cache.guilds.get(guildId))?.emojis?.delete(emoji.id); + + const getEmoji = await bot.helpers.getEmoji(guildId, emoji.id, true); + + // Assertions + assertExists(getEmoji); + + // Delay the execution to allow event to be processed + await delayUntil(10000, async () => (await bot.cache.guilds.get(guildId))?.emojis?.has(emoji.id)); + + if (!(await bot.cache.guilds.get(guildId))?.emojis?.has(emoji.id)) { + throw new Error("The emoji didn't got added to cache after using the getEmoji function."); + } +} diff --git a/tests/helpers/emojis/get_emojis.ts b/tests/helpers/emojis/get_emojis.ts new file mode 100644 index 000000000..b9e1a1bbc --- /dev/null +++ b/tests/helpers/emojis/get_emojis.ts @@ -0,0 +1,27 @@ +import { Bot } from "../../../src/bot.ts"; +import { assertExists } from "../../deps.ts"; +import { delayUntil } from "../../utils.ts"; + +export async function getEmojisTest(bot: Bot, guildId: bigint, t: Deno.TestContext) { + const emoji = await bot.helpers.createEmoji(guildId, { + name: "blamewolf", + image: "https://cdn.discordapp.com/emojis/814955268123000832.png", + roles: [], + }); + + // Assertions + assertExists(emoji); + + // Delay the execution to allow event to be processed + await delayUntil(10000, async () => (await bot.cache.guilds.get(guildId))?.emojis?.has(emoji.id)); + + if (!(await bot.cache.guilds.get(guildId))?.emojis?.has(emoji.id)) { + throw new Error("The emoji seemed to be created but it was not cached."); + } + + const emojis = await bot.helpers.getEmojis(guildId); + + if (emojis.size === 0) { + throw new Error("The getEmojis function returned 0 emojis."); + } +} diff --git a/tests/helpers/members/fetchMembers.ts b/tests/helpers/members/fetch_members.ts similarity index 100% rename from tests/helpers/members/fetchMembers.ts rename to tests/helpers/members/fetch_members.ts diff --git a/tests/helpers/messages/deleteMessage.ts b/tests/helpers/messages/delete_message.ts similarity index 100% rename from tests/helpers/messages/deleteMessage.ts rename to tests/helpers/messages/delete_message.ts diff --git a/tests/helpers/messages/deleteMessages.ts b/tests/helpers/messages/delete_messages.ts similarity index 100% rename from tests/helpers/messages/deleteMessages.ts rename to tests/helpers/messages/delete_messages.ts diff --git a/tests/helpers/messages/editMessage.ts b/tests/helpers/messages/edit_message.ts similarity index 100% rename from tests/helpers/messages/editMessage.ts rename to tests/helpers/messages/edit_message.ts diff --git a/tests/helpers/messages/getMessage.ts b/tests/helpers/messages/get_message.ts similarity index 100% rename from tests/helpers/messages/getMessage.ts rename to tests/helpers/messages/get_message.ts diff --git a/tests/helpers/messages/getMessages.ts b/tests/helpers/messages/get_messages.ts similarity index 100% rename from tests/helpers/messages/getMessages.ts rename to tests/helpers/messages/get_messages.ts diff --git a/tests/helpers/messages/sendMessage.ts b/tests/helpers/messages/send_message.ts similarity index 100% rename from tests/helpers/messages/sendMessage.ts rename to tests/helpers/messages/send_message.ts diff --git a/tests/local.ts b/tests/local.ts index bba4ce566..5b231a52d 100644 --- a/tests/local.ts +++ b/tests/local.ts @@ -2,3 +2,4 @@ import "./local/snowflake.ts"; import "./util/validate_length.ts"; import "./util/utils.ts"; import "./util/hash.ts"; +import "./util/format_urls.ts"; diff --git a/tests/mod.ts b/tests/mod.ts index 7714d53dc..afb100c7b 100644 --- a/tests/mod.ts +++ b/tests/mod.ts @@ -8,31 +8,36 @@ import { stopBot, } from "../mod.ts"; import { assertEquals, assertExists } from "./deps.ts"; -import { deleteMessageWithReasonTest, deleteMessageWithoutReasonTest } from "./helpers/messages/deleteMessage.ts"; -import { getMessagesTest } from "./helpers/messages/getMessages.ts"; -import { deleteMessagesWithoutReasonTest, deleteMessagesWithReasonTest } from "./helpers/messages/deleteMessages.ts"; +import { deleteMessageWithReasonTest, deleteMessageWithoutReasonTest } from "./helpers/messages/delete_message.ts"; +import { getMessagesTest } from "./helpers/messages/get_messages.ts"; +import { deleteMessagesWithoutReasonTest, deleteMessagesWithReasonTest } from "./helpers/messages/delete_messages.ts"; import { delayUntil } from "./utils.ts"; import { sendMessageWithComponents, sendMessageWithEmbedsTest, sendMessageWithTextTest, -} from "./helpers/messages/sendMessage.ts"; +} from "./helpers/messages/send_message.ts"; // CONDUCT LOCAL TESTS FIRST BEFORE RUNNING API TEST import "./local.ts"; -import { getMessageTest } from "./helpers/messages/getMessage.ts"; +import { getMessageTest } from "./helpers/messages/get_message.ts"; import { addReactionTest } from "./helpers/messages/reactions.ts"; -import { editMessageTest } from "./helpers/messages/editMessage.ts"; -import { fetchSingleMemberTest } from "./helpers/members/fetchMembers.ts"; +import { editMessageTest } from "./helpers/messages/edit_message.ts"; +import { fetchSingleMemberTest } from "./helpers/members/fetch_members.ts"; import { pinMessageTests } from "./helpers/messages/pin.ts"; import { removeAllReactionTests, removeReactionEmojiTest, removeReactionTest } from "./helpers/messages/reactions.ts"; -import { createChannelTests } from "./helpers/channels/createChannel.ts"; -import { deleteChannelTests } from "./helpers/channels/deleteChannel.ts"; import { createInviteTest } from "./helpers/invites/create_invite.ts"; import { deleteInviteTest } from "./helpers/invites/delete_invite.ts"; import { getChannelInvitesTest } from "./helpers/invites/get_channels_invites.ts"; import { getInviteTest } from "./helpers/invites/get_invite.ts"; import { getInvitesTest } from "./helpers/invites/get_invites.ts"; +import { createChannelTests } from "./helpers/channels/create_channel.ts"; +import { deleteChannelTests } from "./helpers/channels/delete_channel.ts"; +import { createEmojiTest } from "./helpers/emojis/create_emoji.ts"; +import { deleteEmojiWithoutReasonTest, deleteEmojiWithReasonTest } from "./helpers/emojis/delete_emoji.ts"; +import { editEmojiTest } from "./helpers/emojis/edit_emoji.ts"; +import { getEmojiTest } from "./helpers/emojis/get_emoji.ts"; +import { getEmojisTest } from "./helpers/emojis/get_emojis.ts"; Deno.test("[Bot] - Starting Tests", async (t) => { // CHANGE TO TRUE WHEN DEBUGGING SANITIZATION ERRORS @@ -56,7 +61,7 @@ Deno.test("[Bot] - Starting Tests", async (t) => { }, // debug: console.log, }), - intents: ["Guilds", "GuildMessages", "GuildMessageReactions"], + intents: ["Guilds", "GuildEmojis", "GuildMessages", "GuildMessageReactions"], cache: { isAsync: false, }, @@ -519,5 +524,53 @@ Deno.test("[Bot] - Starting Tests", async (t) => { ]); }); + // EMOJIS TESTS GROUPED + await t.step("Emojis related tests", async (t) => { + await Promise.all([ + t.step({ + name: "[emoji] create an emoji", + fn: async (t) => { + await createEmojiTest(bot, guild.id, t); + }, + ...sanitizeMode, + }), + t.step({ + name: "[emoji] delete an emoji without a reason", + fn: async (t) => { + await deleteEmojiWithoutReasonTest(bot, guild.id, t); + }, + ...sanitizeMode, + }), + t.step({ + name: "[emoji] delete an emoji with a reason", + fn: async (t) => { + await deleteEmojiWithReasonTest(bot, guild.id, t); + }, + ...sanitizeMode, + }), + t.step({ + name: "[emoji] edit an emoji", + fn: async (t) => { + await editEmojiTest(bot, guild.id, t); + }, + ...sanitizeMode, + }), + t.step({ + name: "[emoji] get an emoji", + fn: async (t) => { + await getEmojiTest(bot, guild.id, t); + }, + ...sanitizeMode, + }), + t.step({ + name: "[emoji] get multiple emojis", + fn: async (t) => { + await getEmojisTest(bot, guild.id, t); + }, + ...sanitizeMode, + }), + ]); + }); + await stopBot(bot); }); diff --git a/tests/local/snowflake.ts b/tests/util/bigint.ts similarity index 100% rename from tests/local/snowflake.ts rename to tests/util/bigint.ts diff --git a/tests/util/formatUrls.ts b/tests/util/format_urls.ts similarity index 100% rename from tests/util/formatUrls.ts rename to tests/util/format_urls.ts