From 300c760480183f372dd4b123ca191d5bb55fced2 Mon Sep 17 00:00:00 2001 From: Skillz4Killz <23035000+Skillz4Killz@users.noreply.github.com> Date: Tue, 2 Nov 2021 19:32:57 +0000 Subject: [PATCH] add role tests --- tests/helpers/channels/delete_channel.ts | 2 +- tests/helpers/roles/createRole.ts | 20 +++++++ tests/helpers/roles/deleteRole.ts | 29 ++++++++++ tests/helpers/roles/editRole.ts | 28 +++++++++ tests/helpers/roles/getRoles.ts | 9 +++ tests/helpers/roles/roleChanges.ts | 66 +++++++++++++++++++++ tests/mod.ts | 73 +++++++++++++++++++++++- 7 files changed, 225 insertions(+), 2 deletions(-) create mode 100644 tests/helpers/roles/createRole.ts create mode 100644 tests/helpers/roles/deleteRole.ts create mode 100644 tests/helpers/roles/editRole.ts create mode 100644 tests/helpers/roles/getRoles.ts create mode 100644 tests/helpers/roles/roleChanges.ts diff --git a/tests/helpers/channels/delete_channel.ts b/tests/helpers/channels/delete_channel.ts index 537754e4c..c0fbd4db8 100644 --- a/tests/helpers/channels/delete_channel.ts +++ b/tests/helpers/channels/delete_channel.ts @@ -15,7 +15,7 @@ export async function deleteChannelTests(bot: Bot, guildId: bigint, options: { r // Delete the channel now without a reason await bot.helpers.deleteChannel(channel.id, options.reason); - // wait 5 seconds to give it time for CHANNEL_DELETE event + // wait to give it time for event await delayUntil(10000, () => !bot.cache.channels.has(channel.id)); // Make sure it is gone from cache if (bot.cache.channels.has(channel.id)) { diff --git a/tests/helpers/roles/createRole.ts b/tests/helpers/roles/createRole.ts new file mode 100644 index 000000000..297da9370 --- /dev/null +++ b/tests/helpers/roles/createRole.ts @@ -0,0 +1,20 @@ +import { Bot } from "../../../src/bot.ts"; +import { Cache } from "../../../src/cache.ts"; +import { assertExists } from "../../deps.ts"; +import { delayUntil } from "../../utils.ts"; + +export async function createRoleTests( + bot: Bot, + guildId: bigint, + options: { reason?: string }, + t: Deno.TestContext +) { + const role = await bot.helpers.createRole(guildId, { name: "hoti" }, options.reason); + + assertExists(role); + + // Delay the execution to allow event to be processed + await delayUntil(10000, () => bot.cache.guilds.get(guildId)?.roles.has(role.id)); + + assertExists(bot.cache.guilds.get(guildId)?.roles.has(role.id)); +} diff --git a/tests/helpers/roles/deleteRole.ts b/tests/helpers/roles/deleteRole.ts new file mode 100644 index 000000000..fb47054cc --- /dev/null +++ b/tests/helpers/roles/deleteRole.ts @@ -0,0 +1,29 @@ +import { Bot } from "../../../src/bot.ts"; +import { Cache } from "../../../src/cache.ts"; +import { assertExists } from "../../deps.ts"; +import { delayUntil } from "../../utils.ts"; + +export async function deleteRoleTests( + bot: Bot, + guildId: bigint, + options: { reason?: string }, + t: Deno.TestContext +) { + const role = await bot.helpers.createRole(guildId, { name: "hoti" }, options.reason); + + assertExists(role); + + // Delay the execution to allow event to be processed + await delayUntil(10000, () => bot.cache.guilds.get(guildId)?.roles.has(role.id)); + + assertExists(bot.cache.guilds.get(guildId)?.roles.has(role.id)); + + await bot.helpers.deleteRole(guildId, role.id); + + // Delay the execution to allow event to be processed + await delayUntil(10000, () => !bot.cache.guilds.get(guildId)?.roles.has(role.id)); + + if (bot.cache.guilds.get(guildId)?.roles.has(role.id)) { + throw new Error(`The role should have been deleted but it is still in cache.`); + } +} diff --git a/tests/helpers/roles/editRole.ts b/tests/helpers/roles/editRole.ts new file mode 100644 index 000000000..d0f3c5d6c --- /dev/null +++ b/tests/helpers/roles/editRole.ts @@ -0,0 +1,28 @@ +import { Bot } from "../../../src/bot.ts"; +import { Cache } from "../../../src/cache.ts"; +import { assertEquals, assertExists } from "../../deps.ts"; +import { delayUntil } from "../../utils.ts"; + +export async function editRoleTests(bot: Bot, guildId: bigint, t: Deno.TestContext) { + const role = await bot.helpers.createRole(guildId, { + name: "hoti", + }); + + assertExists(role); + + // Delay the execution to allow event to be processed + await delayUntil(10000, () => bot.cache.guilds.get(guildId)?.roles.has(role.id)); + + if (!bot.cache.guilds.get(guildId)?.roles.has(role.id)) { + throw new Error(`The role seemed to be created but it was not cached.`); + } + + await bot.helpers.editRole(guildId, role.id, { + name: "#rememberAyntee", + }); + + // Delay the execution to allow event to be processed + await delayUntil(10000, () => bot.cache.guilds.get(guildId)?.roles.get(role.id)?.name === "#rememberAyntee"); + + assertEquals(bot.cache.guilds.get(guildId)?.roles.get(role.id)?.name === "#rememberAyntee", true); +} diff --git a/tests/helpers/roles/getRoles.ts b/tests/helpers/roles/getRoles.ts new file mode 100644 index 000000000..861196d79 --- /dev/null +++ b/tests/helpers/roles/getRoles.ts @@ -0,0 +1,9 @@ +import { Bot } from "../../../src/bot.ts"; +import { Cache } from "../../../src/cache.ts"; +import { assertEquals, assertExists } from "../../deps.ts"; + +export async function getRolesTest(bot: Bot, guildId: bigint, t: Deno.TestContext) { + const roles = await bot.helpers.getRoles(guildId); + + assertEquals(bot.cache.guilds.get(guildId)?.roles.size, roles.length); +} diff --git a/tests/helpers/roles/roleChanges.ts b/tests/helpers/roles/roleChanges.ts new file mode 100644 index 000000000..e2ce23df1 --- /dev/null +++ b/tests/helpers/roles/roleChanges.ts @@ -0,0 +1,66 @@ +import { Bot } from "../../../src/bot.ts"; +import { Cache } from "../../../src/cache.ts"; +import { assertEquals, assertExists } from "../../deps.ts"; +import { delayUntil } from "../../utils.ts"; + +const roleChanges = new Map(); + +export async function addRoleTest(bot: Bot, guildId: bigint, options: { reason?: string }, t: Deno.TestContext) { + const role = await bot.helpers.createRole(guildId, { + name: "hoti", + }); + + assertExists(role); + + // Delay the execution to allow event to be processed + await delayUntil(10000, () => bot.cache.guilds.get(guildId)?.roles.has(role.id)); + + assertExists(bot.cache.guilds.get(guildId)?.roles.has(role.id)); + + bot.events.guildMemberUpdate = function (bot, member, user) { + roleChanges.set(user.id, member.roles); + }; + + await bot.helpers.addRole(guildId, bot.id, role.id, options.reason); + + // Delay the execution to allow event to be processed + await delayUntil(10000, () => roleChanges.get(bot.id)?.includes(role.id)); + + assertEquals(roleChanges.get(bot.id)?.includes(role.id), true); +} + +export async function removeRoleTest( + bot: Bot, + guildId: bigint, + options: { reason?: string }, + t: Deno.TestContext +) { + const role = await bot.helpers.createRole(guildId, { + name: "hoti", + }); + + assertExists(role); + + // Delay the execution to allow event to be processed + await delayUntil(10000, () => bot.cache.guilds.get(guildId)?.roles.has(role.id)); + + assertExists(bot.cache.guilds.get(guildId)?.roles.has(role.id)); + + bot.events.guildMemberUpdate = function (bot, member, user) { + roleChanges.set(user.id, member.roles); + }; + + await bot.helpers.addRole(guildId, bot.id, role.id, options.reason); + + // Delay the execution to allow event to be processed + await delayUntil(10000, () => roleChanges.get(bot.id)?.includes(role.id)); + + assertEquals(roleChanges.get(bot.id)?.includes(role.id), true); + + await bot.helpers.removeRole(guildId, bot.id, role.id, options.reason); + + // Delay the execution to allow event to be processed + await delayUntil(10000, () => !roleChanges.get(bot.id)?.includes(role.id)); + + assertEquals(roleChanges.get(bot.id)?.includes(role.id), false); +} diff --git a/tests/mod.ts b/tests/mod.ts index d310ea1a6..270358a54 100644 --- a/tests/mod.ts +++ b/tests/mod.ts @@ -40,6 +40,11 @@ import { editEmojiTest } from "./helpers/emojis/edit_emoji.ts"; import { getEmojiTest } from "./helpers/emojis/get_emoji.ts"; import { getEmojisTest } from "./helpers/emojis/get_emojis.ts"; import { getBansTest, unbanTest, banTest } from "./helpers/members/ban.ts"; +import { createRoleTests } from "./helpers/roles/createRole.ts"; +import { deleteRoleTests } from "./helpers/roles/deleteRole.ts"; +import { getRolesTest } from "./helpers/roles/getRoles.ts"; +import { editRoleTests } from "./helpers/roles/editRole.ts"; +import { addRoleTest, removeRoleTest } from "./helpers/roles/roleChanges.ts"; // CHANGE TO TRUE WHEN DEBUGGING SANITIZATION ERRORS const sanitizeMode = { @@ -65,7 +70,7 @@ Deno.test({ }, // debug: console.log, }), - intents: ["Guilds", "GuildEmojis", "GuildMessages", "GuildMessageReactions", "GuildBans"], + intents: ["Guilds", "GuildEmojis", "GuildMessages", "GuildMessageReactions", "GuildBans", "GuildMembers"], cache: { isAsync: false, }, @@ -620,6 +625,72 @@ Deno.test({ ]); }); + // ROLE RELATED TESTS + await t.step({ + name: "[Role] Role related tests", + fn: async (t) => { + await t.step({ + name: "[Role] get all roles on a server", + fn: async (t) => { + await getRolesTest(bot, guild.id, t); + }, + ...sanitizeMode, + }); + + await Promise.all([ + t.step({ + name: "[Role] create a role without a reason", + fn: async (t) => { + await createRoleTests(bot, guild.id, {}, t); + }, + ...sanitizeMode, + }), + t.step({ + name: "[Role] create a role with a reason", + fn: async (t) => { + await createRoleTests(bot, guild.id, { reason: "Blame wolfy" }, t); + }, + ...sanitizeMode, + }), + t.step({ + name: "[Role] delete a role without a reason", + fn: async (t) => { + await deleteRoleTests(bot, guild.id, {}, t); + }, + ...sanitizeMode, + }), + t.step({ + name: "[Role] delete a role with a reason", + fn: async (t) => { + await deleteRoleTests(bot, guild.id, { reason: "Blame wolfy" }, t); + }, + ...sanitizeMode, + }), + t.step({ + name: "[Role] edit a role", + fn: async (t) => { + await editRoleTests(bot, guild.id, t); + }, + ...sanitizeMode, + }), + t.step({ + name: "[Role] add a role to a member", + fn: async (t) => { + await addRoleTest(bot, guild.id, { reason: "Blame wolf" }, t); + }, + ...sanitizeMode, + }), + t.step({ + name: "[Role] remove a role to a member", + fn: async (t) => { + await removeRoleTest(bot, guild.id, { reason: "Blame wolf" }, t); + }, + ...sanitizeMode, + }), + ]); + }, + }); + // CONDUCT MEMORY BENCHMARK TESTS await t.step({ name: "[Memory] Benchmark memory tests",