diff --git a/tests/helpers/guilds/createGuild.ts b/tests/helpers/guilds/createGuild.ts new file mode 100644 index 000000000..601b298db --- /dev/null +++ b/tests/helpers/guilds/createGuild.ts @@ -0,0 +1,21 @@ +import { Bot } from "../../../src/bot.ts"; +import { CreateGuildChannel } from "../../../src/types/guilds/create_guild_channel.ts"; +import { DiscordChannelTypes } from "../../../src/types/mod.ts"; +import { assertExists, assertEquals } from "../../deps.ts"; +import { delayUntil } from "../../utils.ts"; + +export async function createGuildTests(bot: Bot, t: Deno.TestContext) { + const guild = await bot.helpers.createGuild({ + name: "Isekai Maid Fake Server", + }); + + // Assertions + assertExists(guild); + + // Delay the execution to allow event to be processed + await delayUntil(10000, () => bot.cache.guilds.has(guild.id)); + + if (!bot.cache.guilds.has(guild.id)) { + throw new Error(`The guild seemed to be created but it was not cached.`); + } +} diff --git a/tests/helpers/guilds/deleteGuild.ts b/tests/helpers/guilds/deleteGuild.ts new file mode 100644 index 000000000..20aedbd33 --- /dev/null +++ b/tests/helpers/guilds/deleteGuild.ts @@ -0,0 +1,30 @@ +import { Bot } from "../../../src/bot.ts"; +import { CreateGuildChannel } from "../../../src/types/guilds/create_guild_channel.ts"; +import { DiscordChannelTypes } from "../../../src/types/mod.ts"; +import { assertExists, assertEquals } from "../../deps.ts"; +import { delayUntil } from "../../utils.ts"; + +export async function deleteGuildTests(bot: Bot, t: Deno.TestContext) { + const guild = await bot.helpers.createGuild({ + name: "Isekai Maid Fake Server", + }); + + // Assertions + assertExists(guild); + + // Delay the execution to allow event to be processed + await delayUntil(10000, () => bot.cache.guilds.has(guild.id)); + + if (!bot.cache.guilds.has(guild.id)) { + throw new Error(`The guild seemed to be created but it was not cached.`); + } + + await bot.helpers.deleteGuild(guild.id); + + // Delay the execution to allow event to be processed + await delayUntil(10000, () => !bot.cache.guilds.has(guild.id)); + + if (bot.cache.guilds.has(guild.id)) { + throw new Error(`The guild seemed to be deleted but it's still cached.`); + } +} diff --git a/tests/helpers/guilds/editGuild.ts b/tests/helpers/guilds/editGuild.ts new file mode 100644 index 000000000..e8b94cd40 --- /dev/null +++ b/tests/helpers/guilds/editGuild.ts @@ -0,0 +1,21 @@ +import { Bot } from "../../../src/bot.ts"; +import { CreateGuildChannel } from "../../../src/types/guilds/create_guild_channel.ts"; +import { DiscordChannelTypes } from "../../../src/types/mod.ts"; +import { assertExists, assertEquals } from "../../deps.ts"; +import { delayUntil } from "../../utils.ts"; + +export async function editGuildTests(bot: Bot, guildId: bigint, t: Deno.TestContext) { + const guild = await bot.helpers.editGuild(guildId, { + name: "Discordeno Test 1.0", + }); + + // Assertions + assertExists(guild); + + // Delay the execution to allow event to be processed + await delayUntil(10000, async () => (await bot.cache.guilds.get(guild.id))?.name === "Discordeno Test 1.0"); + + if (!bot.cache.guilds.has(guild.id)) { + throw new Error(`The guild seemed to be edited but the cache didn't got updated.`); + } +} diff --git a/tests/helpers/guilds/getAuditLogs.ts b/tests/helpers/guilds/getAuditLogs.ts new file mode 100644 index 000000000..63f5e4344 --- /dev/null +++ b/tests/helpers/guilds/getAuditLogs.ts @@ -0,0 +1,12 @@ +import { Bot } from "../../../src/bot.ts"; +import { CreateGuildChannel } from "../../../src/types/guilds/create_guild_channel.ts"; +import { DiscordChannelTypes } from "../../../src/types/mod.ts"; +import { assertExists, assertEquals } from "../../deps.ts"; +import { delayUntil } from "../../utils.ts"; + +export async function getAuditLogsTests(bot: Bot, guildId: bigint, t: Deno.TestContext) { + const logs = await bot.helpers.getAuditLogs(guildId); + + // Assertions + assertExists(logs); +} diff --git a/tests/helpers/guilds/getAvailableVoiceRegions.ts b/tests/helpers/guilds/getAvailableVoiceRegions.ts new file mode 100644 index 000000000..702970ad0 --- /dev/null +++ b/tests/helpers/guilds/getAvailableVoiceRegions.ts @@ -0,0 +1,13 @@ +import { Bot } from "../../../src/bot.ts"; +import { CreateGuildChannel } from "../../../src/types/guilds/create_guild_channel.ts"; +import { DiscordChannelTypes } from "../../../src/types/mod.ts"; +import { assertExists, assertEquals } from "../../deps.ts"; +import { delayUntil } from "../../utils.ts"; +import { getAvailableVoiceRegions } from "../../../src/helpers/guilds/get_available_voice_regions.ts"; + +export async function getAvailableVoiceRegionsTests(bot: Bot, t: Deno.TestContext) { + const regions = await bot.helpers.getAvailableVoiceRegions(); + + // Assertions + assertExists(regions); +} diff --git a/tests/helpers/guilds/getBan.ts b/tests/helpers/guilds/getBan.ts new file mode 100644 index 000000000..3f1c142f4 --- /dev/null +++ b/tests/helpers/guilds/getBan.ts @@ -0,0 +1,16 @@ +import { Bot } from "../../../src/bot.ts"; +import { CreateGuildChannel } from "../../../src/types/guilds/create_guild_channel.ts"; +import { DiscordChannelTypes } from "../../../src/types/mod.ts"; +import { assertExists, assertEquals } from "../../deps.ts"; +import { delayUntil } from "../../utils.ts"; +import { getAvailableVoiceRegions } from "../../../src/helpers/guilds/get_available_voice_regions.ts"; + +export async function getBanTests(bot: Bot, guildId: bigint, t: Deno.TestContext) { + await bot.helpers.ban(guildId, 379643682984296448n); + + const fetchedBan = await bot.helpers.getBan(guildId, 379643682984296448n); + + // Assertions + assertExists(fetchedBan); + assertEquals(fetchedBan.user.id, 379643682984296448n); +} diff --git a/tests/helpers/guilds/getBans.ts b/tests/helpers/guilds/getBans.ts new file mode 100644 index 000000000..4ff2628d7 --- /dev/null +++ b/tests/helpers/guilds/getBans.ts @@ -0,0 +1,20 @@ +import { Bot } from "../../../src/bot.ts"; +import { CreateGuildChannel } from "../../../src/types/guilds/create_guild_channel.ts"; +import { DiscordChannelTypes } from "../../../src/types/mod.ts"; +import { assertExists, assertEquals } from "../../deps.ts"; +import { delayUntil } from "../../utils.ts"; +import { getAvailableVoiceRegions } from "../../../src/helpers/guilds/get_available_voice_regions.ts"; + +export async function getBansTests(bot: Bot, guildId: bigint, t: Deno.TestContext) { + await bot.helpers.ban(guildId, 416477607966670869n); + await bot.helpers.ban(guildId, 635383782576357407n); + + const fetchedBans = await bot.helpers.getBans(guildId); + + // Assertions + assertExists(fetchedBans); + + if (fetchedBans.size === 0) { + throw new Error("getBans didn't return any ban, but it should have returned at least 2 bans!"); + } +} diff --git a/tests/helpers/guilds/getGuild.ts b/tests/helpers/guilds/getGuild.ts new file mode 100644 index 000000000..1a79a3eae --- /dev/null +++ b/tests/helpers/guilds/getGuild.ts @@ -0,0 +1,14 @@ +import { Bot } from "../../../src/bot.ts"; +import { CreateGuildChannel } from "../../../src/types/guilds/create_guild_channel.ts"; +import { DiscordChannelTypes } from "../../../src/types/mod.ts"; +import { assertExists, assertEquals } from "../../deps.ts"; +import { delayUntil } from "../../utils.ts"; +import { getAvailableVoiceRegions } from "../../../src/helpers/guilds/get_available_voice_regions.ts"; + +export async function getGuildTests(bot: Bot, guildId: bigint, t: Deno.TestContext) { + const fetchedGuild = await bot.helpers.getGuild(guildId); + + // Assertions + assertExists(fetchedGuild); + assertEquals(fetchedGuild.id, guildId); +} diff --git a/tests/helpers/guilds/getVanityUrl.ts b/tests/helpers/guilds/getVanityUrl.ts new file mode 100644 index 000000000..50b6d148d --- /dev/null +++ b/tests/helpers/guilds/getVanityUrl.ts @@ -0,0 +1,14 @@ +import { Bot } from "../../../src/bot.ts"; +import { CreateGuildChannel } from "../../../src/types/guilds/create_guild_channel.ts"; +import { DiscordChannelTypes } from "../../../src/types/mod.ts"; +import { assertExists, assertEquals } from "../../deps.ts"; +import { delayUntil } from "../../utils.ts"; +import { getAvailableVoiceRegions } from "../../../src/helpers/guilds/get_available_voice_regions.ts"; + +export async function getVanityURLTests(bot: Bot, guildId: bigint, t: Deno.TestContext) { + const fetchedVanityURL = await bot.helpers.getVanityURL(guildId); + + // Assertions + assertExists(fetchedVanityURL); + assertEquals(fetchedVanityURL.code, null); +} diff --git a/tests/mod.ts b/tests/mod.ts index 26bbfa9b4..a04eca880 100644 --- a/tests/mod.ts +++ b/tests/mod.ts @@ -46,6 +46,15 @@ import { getRolesTest } from "./helpers/roles/getRoles.ts"; import { editRoleTests } from "./helpers/roles/editRole.ts"; import { addRoleTest, removeRoleTest } from "./helpers/roles/roleChanges.ts"; import { getUserTests } from "./helpers/misc/user.ts"; +import { createGuildTests } from "./helpers/guilds/createGuild.ts"; +import { deleteGuildTests } from "./helpers/guilds/deleteGuild.ts"; +import { editGuildTests } from "./helpers/guilds/editGuild.ts"; +import { getAuditLogsTests } from "./helpers/guilds/getAuditLogs.ts"; +import { getAvailableVoiceRegionsTests } from "./helpers/guilds/getAvailableVoiceRegions.ts"; +import { getBanTests } from "./helpers/guilds/getBan.ts"; +import { getBansTests } from "./helpers/guilds/getBans.ts"; +import { getGuildTests } from "./helpers/guilds/getGuild.ts"; +import { getVanityURLTests } from "./helpers/guilds/getVanityUrl.ts"; // CHANGE TO TRUE WHEN DEBUGGING SANITIZATION ERRORS const sanitizeMode = { @@ -108,47 +117,113 @@ Deno.test({ throw new Error(`The guild seemed to be created but it was not cached. ${guild.id.toString()}`); } - await Promise.all([ - t.step({ - name: "[guild] format a guild's icon url", - fn: async (t) => { - assertEquals(bot.helpers.guildIconURL(guild.id, { icon: guild.icon }), undefined); - assertEquals( - bot.helpers.guildIconURL(785384884197392384n, { - icon: 3837424427068676005442449262648382018748n, - }), - "https://cdn.discordapp.com/icons/785384884197392384/46f50fb412eab14ec455d5cf777154bc.jpg?size=128" - ); - }, - ...sanitizeMode, - }), - t.step({ - name: "[guild] format a guild's banner url", - fn: async (t) => { - assertEquals(bot.helpers.guildBannerURL(guild.id, { banner: guild.banner }), undefined); - assertEquals( - bot.helpers.guildBannerURL(613425648685547541n, { - banner: 3919584870146358272366452115178209474142n, - }), - "https://cdn.discordapp.com/banners/613425648685547541/84c4964c115c128fb9100952c3b4f65e.jpg?size=128" - ); - }, - ...sanitizeMode, - }), - t.step({ - name: "[guild] format a guild's splash url", - fn: async (t) => { - assertEquals(bot.helpers.guildSplashURL(guild.id, { splash: guild.splash }), undefined); - assertEquals( - bot.helpers.guildSplashURL(785384884197392384n, { - splash: 3837424427068676005442449262648382018748n, - }), - "https://cdn.discordapp.com/splashes/785384884197392384/46f50fb412eab14ec455d5cf777154bc.jpg?size=128" - ); - }, - ...sanitizeMode, - }), - ]); + // GUILD TESTS GROUPED + await t.step("Guild related tests", async (t) => { + await Promise.all([ + t.step({ + name: "[guild] format a guild's icon url", + fn: async (t) => { + assertEquals(bot.helpers.guildIconURL(guild.id, { icon: guild.icon }), undefined); + assertEquals( + bot.helpers.guildIconURL(785384884197392384n, { + icon: 3837424427068676005442449262648382018748n, + }), + "https://cdn.discordapp.com/icons/785384884197392384/46f50fb412eab14ec455d5cf777154bc.jpg?size=128" + ); + }, + ...sanitizeMode, + }), + t.step({ + name: "[guild] format a guild's banner url", + fn: async (t) => { + assertEquals(bot.helpers.guildBannerURL(guild.id, { banner: guild.banner }), undefined); + assertEquals( + bot.helpers.guildBannerURL(613425648685547541n, { + banner: 3919584870146358272366452115178209474142n, + }), + "https://cdn.discordapp.com/banners/613425648685547541/84c4964c115c128fb9100952c3b4f65e.jpg?size=128" + ); + }, + ...sanitizeMode, + }), + t.step({ + name: "[guild] format a guild's splash url", + fn: async (t) => { + assertEquals(bot.helpers.guildSplashURL(guild.id, { splash: guild.splash }), undefined); + assertEquals( + bot.helpers.guildSplashURL(785384884197392384n, { + splash: 3837424427068676005442449262648382018748n, + }), + "https://cdn.discordapp.com/splashes/785384884197392384/46f50fb412eab14ec455d5cf777154bc.jpg?size=128" + ); + }, + ...sanitizeMode, + }), + t.step({ + name: "[guild] create a guild", + fn: async (t) => { + await createGuildTests(bot, t); + }, + ...sanitizeMode, + }), + t.step({ + name: "[guild] delete a guild", + fn: async (t) => { + await deleteGuildTests(bot, t); + }, + ...sanitizeMode, + }), + t.step({ + name: "[guild] edit a guild", + fn: async (t) => { + await editGuildTests(bot, guild.id, t); + }, + ...sanitizeMode, + }), + t.step({ + name: "[guild] get audit logs", + fn: async (t) => { + await getAuditLogsTests(bot, guild.id, t); + }, + ...sanitizeMode, + }), + t.step({ + name: "[guild] get available voice regions", + fn: async (t) => { + await getAvailableVoiceRegionsTests(bot, t); + }, + ...sanitizeMode, + }), + t.step({ + name: "[guild] get a ban", + fn: async (t) => { + await getBanTests(bot, guild.id, t); + }, + ...sanitizeMode, + }), + t.step({ + name: "[guild] get bans", + fn: async (t) => { + await getBansTests(bot, guild.id, t); + }, + ...sanitizeMode, + }), + t.step({ + name: "[guild] get guilds", + fn: async (t) => { + await getGuildTests(bot, guild.id, t); + }, + ...sanitizeMode, + }), + t.step({ + name: "[guild] get vanity url", + fn: async (t) => { + await getVanityURLTests(bot, guild.id, t); + }, + ...sanitizeMode, + }), + ]); + }); // CHANNEL TESTS GROUPED await t.step("Channel related tests", async (t) => {