mirror of
https://github.com/discordeno/discordeno.git
synced 2026-06-03 01:10:07 +00:00
Merge pull request #1406 from Skillz4Killz/fp-attempt-9001
add role tests
This commit is contained in:
@@ -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)) {
|
||||
|
||||
20
tests/helpers/roles/createRole.ts
Normal file
20
tests/helpers/roles/createRole.ts
Normal file
@@ -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<Cache>,
|
||||
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));
|
||||
}
|
||||
29
tests/helpers/roles/deleteRole.ts
Normal file
29
tests/helpers/roles/deleteRole.ts
Normal file
@@ -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<Cache>,
|
||||
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.`);
|
||||
}
|
||||
}
|
||||
28
tests/helpers/roles/editRole.ts
Normal file
28
tests/helpers/roles/editRole.ts
Normal file
@@ -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<Cache>, 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);
|
||||
}
|
||||
9
tests/helpers/roles/getRoles.ts
Normal file
9
tests/helpers/roles/getRoles.ts
Normal file
@@ -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<Cache>, guildId: bigint, t: Deno.TestContext) {
|
||||
const roles = await bot.helpers.getRoles(guildId);
|
||||
|
||||
assertEquals(bot.cache.guilds.get(guildId)?.roles.size, roles.length);
|
||||
}
|
||||
66
tests/helpers/roles/roleChanges.ts
Normal file
66
tests/helpers/roles/roleChanges.ts
Normal file
@@ -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<bigint, bigint[]>();
|
||||
|
||||
export async function addRoleTest(bot: Bot<Cache>, 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<Cache>,
|
||||
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);
|
||||
}
|
||||
73
tests/mod.ts
73
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";
|
||||
import { createGuildTests } from "./helpers/guilds/createGuild.ts";
|
||||
import { deleteGuildTests } from "./helpers/guilds/deleteGuild.ts";
|
||||
import { editGuildTests } from "./helpers/guilds/editGuild.ts";
|
||||
@@ -74,7 +79,7 @@ Deno.test({
|
||||
},
|
||||
// debug: console.log,
|
||||
}),
|
||||
intents: ["Guilds", "GuildEmojis", "GuildMessages", "GuildMessageReactions", "GuildBans"],
|
||||
intents: ["Guilds", "GuildEmojis", "GuildMessages", "GuildMessageReactions", "GuildBans", "GuildMembers"],
|
||||
cache: {
|
||||
isAsync: false,
|
||||
},
|
||||
@@ -695,6 +700,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",
|
||||
|
||||
Reference in New Issue
Block a user