From d3e5f537d3f332aecc6c39a958b34beba1522ea1 Mon Sep 17 00:00:00 2001 From: TriForMine Date: Wed, 14 Apr 2021 09:26:39 +0200 Subject: [PATCH] tests: add invites tests (#855) * tests: add invites tests Co-authored-by: TriForMine --- src/helpers/invites/get_invite.ts | 2 +- tests/invites/create_invite.ts | 49 ++++++++++++++++++++++ tests/invites/delete_invite.ts | 53 ++++++++++++++++++++++++ tests/invites/get_channel_invites.ts | 61 ++++++++++++++++++++++++++++ tests/invites/get_invite.ts | 53 ++++++++++++++++++++++++ tests/invites/get_invites.ts | 33 +++++++++++++++ tests/mod.ts | 7 ++++ 7 files changed, 257 insertions(+), 1 deletion(-) create mode 100644 tests/invites/create_invite.ts create mode 100644 tests/invites/delete_invite.ts create mode 100644 tests/invites/get_channel_invites.ts create mode 100644 tests/invites/get_invite.ts create mode 100644 tests/invites/get_invites.ts diff --git a/src/helpers/invites/get_invite.ts b/src/helpers/invites/get_invite.ts index 68ec7158d..43f2cb35e 100644 --- a/src/helpers/invites/get_invite.ts +++ b/src/helpers/invites/get_invite.ts @@ -3,7 +3,7 @@ import { DiscordInvite, Invite } from "../../types/invites/invite.ts"; import { endpoints } from "../../util/constants.ts"; import { snakeKeysToCamelCase } from "../../util/utils.ts"; -/** Returns an invite for the given code. */ +/** Returns an invite for the given code or throws an error if the invite doesn't exists. */ export async function getInvite(inviteCode: string) { const result: DiscordInvite = await rest.runMethod( "get", diff --git a/tests/invites/create_invite.ts b/tests/invites/create_invite.ts new file mode 100644 index 000000000..077831bac --- /dev/null +++ b/tests/invites/create_invite.ts @@ -0,0 +1,49 @@ +import { cache } from "../../src/cache.ts"; +import { createChannel } from "../../src/helpers/channels/create_channel.ts"; +import { defaultTestOptions, tempData } from "../ws/start_bot.ts"; +import { delayUntil } from "../util/delay_until.ts"; +import { assertEquals, assertExists } from "../deps.ts"; +import { isChannelSynced } from "../../src/helpers/channels/is_channel_synced.ts"; +import { DiscordChannelTypes } from "../../src/types/channels/channel_types.ts"; +import { botId } from "../../src/bot.ts"; +import { DiscordOverwriteTypes } from "../../src/types/channels/overwrite_types.ts"; +import { startTyping } from "../../src/helpers/channels/start_typing.ts"; +import { createInvite } from "../../src/helpers/invites/create_invite.ts"; +import { getInvite } from "../../src/helpers/invites/get_invite.ts"; + +Deno.test({ + name: "[invite] create invite", + async fn() { + const channel = await createChannel(tempData.guildId, { + name: "invite-channel", + }); + + // Assertions + assertExists(channel); + + // Delay the execution by 5 seconds to allow CHANNEL_CREATE event to be processed + await delayUntil(10000, () => cache.channels.has(channel.id)); + + if (!cache.channels.has(channel.id)) { + throw new Error( + "The channel seemed to be created but it was not cached.", + ); + } + + const invite = await createInvite(channel.id, { + maxAge: 86400, + maxUses: 0, + temporary: false, // @ts-ignore + unique: false, + }); + + // Assertions + assertExists(invite); + + assertEquals( + await getInvite(invite.code) !== undefined, + true, + ); + }, + ...defaultTestOptions, +}); diff --git a/tests/invites/delete_invite.ts b/tests/invites/delete_invite.ts new file mode 100644 index 000000000..ca2129cc8 --- /dev/null +++ b/tests/invites/delete_invite.ts @@ -0,0 +1,53 @@ +import { cache } from "../../src/cache.ts"; +import { createChannel } from "../../src/helpers/channels/create_channel.ts"; +import { defaultTestOptions, tempData } from "../ws/start_bot.ts"; +import { delayUntil } from "../util/delay_until.ts"; +import { assertEquals, assertExists } from "../deps.ts"; +import { isChannelSynced } from "../../src/helpers/channels/is_channel_synced.ts"; +import { DiscordChannelTypes } from "../../src/types/channels/channel_types.ts"; +import { botId } from "../../src/bot.ts"; +import { DiscordOverwriteTypes } from "../../src/types/channels/overwrite_types.ts"; +import { startTyping } from "../../src/helpers/channels/start_typing.ts"; +import { createInvite } from "../../src/helpers/invites/create_invite.ts"; +import { getInvite } from "../../src/helpers/invites/get_invite.ts"; +import { deleteInvite } from "../../src/helpers/invites/delete_invite.ts"; +import { getChannelInvites } from "../../src/helpers/invites/get_channel_invites.ts"; + +Deno.test({ + name: "[invite] delete invite", + async fn() { + const channel = await createChannel(tempData.guildId, { + name: "invite-channel", + }); + + // Assertions + assertExists(channel); + + // Delay the execution by 5 seconds to allow CHANNEL_CREATE event to be processed + await delayUntil(10000, () => cache.channels.has(channel.id)); + + if (!cache.channels.has(channel.id)) { + throw new Error( + "The channel seemed to be created but it was not cached.", + ); + } + + const invite = await createInvite(channel.id, { + maxAge: 86400, + maxUses: 0, + temporary: false, // @ts-ignore + unique: false, + }); + + // Assertions + assertExists(invite); + + await deleteInvite(channel.id, invite.code); + + assertEquals( + (await getChannelInvites(channel.id))?.size, + 0, + ); + }, + ...defaultTestOptions, +}); diff --git a/tests/invites/get_channel_invites.ts b/tests/invites/get_channel_invites.ts new file mode 100644 index 000000000..01185ef8e --- /dev/null +++ b/tests/invites/get_channel_invites.ts @@ -0,0 +1,61 @@ +import { cache } from "../../src/cache.ts"; +import { createChannel } from "../../src/helpers/channels/create_channel.ts"; +import { defaultTestOptions, tempData } from "../ws/start_bot.ts"; +import { delayUntil } from "../util/delay_until.ts"; +import { assertEquals, assertExists } from "../deps.ts"; +import { isChannelSynced } from "../../src/helpers/channels/is_channel_synced.ts"; +import { DiscordChannelTypes } from "../../src/types/channels/channel_types.ts"; +import { botId } from "../../src/bot.ts"; +import { DiscordOverwriteTypes } from "../../src/types/channels/overwrite_types.ts"; +import { startTyping } from "../../src/helpers/channels/start_typing.ts"; +import { createInvite } from "../../src/helpers/invites/create_invite.ts"; +import { getInvite } from "../../src/helpers/invites/get_invite.ts"; +import { getChannelInvites } from "../../src/helpers/invites/get_channel_invites.ts"; +import { delay } from "../../src/util/utils.ts"; + +Deno.test({ + name: "[invite] get channel invites", + async fn() { + const channel = await createChannel(tempData.guildId, { + name: "invite-channel", + }); + + // Assertions + assertExists(channel); + + // Delay the execution by 5 seconds to allow CHANNEL_CREATE event to be processed + await delayUntil(10000, () => cache.channels.has(channel.id)); + + if (!cache.channels.has(channel.id)) { + throw new Error( + "The channel seemed to be created but it was not cached.", + ); + } + + const invite = await createInvite(channel.id, { + maxAge: 86400, + maxUses: 0, + temporary: false, // @ts-ignore + unique: false, + }); + + // Assertions + assertExists(invite); + + const secondInvite = await createInvite(channel.id, { + maxAge: 32400, + maxUses: 5, + temporary: true, // @ts-ignore + unique: true, + }); + + // Assertions + assertExists(secondInvite); + + assertEquals( + (await getChannelInvites(channel.id))?.size, + 2, + ); + }, + ...defaultTestOptions, +}); diff --git a/tests/invites/get_invite.ts b/tests/invites/get_invite.ts new file mode 100644 index 000000000..c64f26122 --- /dev/null +++ b/tests/invites/get_invite.ts @@ -0,0 +1,53 @@ +import { cache } from "../../src/cache.ts"; +import { createChannel } from "../../src/helpers/channels/create_channel.ts"; +import { defaultTestOptions, tempData } from "../ws/start_bot.ts"; +import { delayUntil } from "../util/delay_until.ts"; +import { assertEquals, assertExists } from "../deps.ts"; +import { isChannelSynced } from "../../src/helpers/channels/is_channel_synced.ts"; +import { DiscordChannelTypes } from "../../src/types/channels/channel_types.ts"; +import { botId } from "../../src/bot.ts"; +import { DiscordOverwriteTypes } from "../../src/types/channels/overwrite_types.ts"; +import { startTyping } from "../../src/helpers/channels/start_typing.ts"; +import { createInvite } from "../../src/helpers/invites/create_invite.ts"; +import { getInvite } from "../../src/helpers/invites/get_invite.ts"; + +Deno.test({ + name: "[invite] get invite", + async fn() { + const channel = await createChannel(tempData.guildId, { + name: "invite-channel", + }); + + // Assertions + assertExists(channel); + + // Delay the execution by 5 seconds to allow CHANNEL_CREATE event to be processed + await delayUntil(10000, () => cache.channels.has(channel.id)); + + if (!cache.channels.has(channel.id)) { + throw new Error( + "The channel seemed to be created but it was not cached.", + ); + } + + const invite = await createInvite(channel.id, { + maxAge: 86400, + maxUses: 0, + temporary: false, // @ts-ignore + unique: false, + }); + + // Assertions + assertExists(invite); + + const fetchedInvite = await getInvite(invite.code); + + assertExists(fetchedInvite); + + assertEquals( + fetchedInvite.code, + invite.code, + ); + }, + ...defaultTestOptions, +}); diff --git a/tests/invites/get_invites.ts b/tests/invites/get_invites.ts new file mode 100644 index 000000000..26acefbe8 --- /dev/null +++ b/tests/invites/get_invites.ts @@ -0,0 +1,33 @@ +import { defaultTestOptions, tempData } from "../ws/start_bot.ts"; +import { assertEquals } from "../deps.ts"; +import { getInvites } from "../../src/helpers/invites/get_invites.ts"; +import { cache } from "../../src/cache.ts"; + +async function ifItFailsBlameWolf(type: "getter" | "raw") { + let invites; + + if (type === "raw") { + invites = await getInvites(tempData.guildId); + } else { + const guild = cache.guilds.get(tempData.guildId); + invites = await guild?.invites(); + } + + assertEquals((invites?.size || 0) >= 0, true); +} + +Deno.test({ + name: "[invite] get invites", + async fn() { + await ifItFailsBlameWolf("raw"); + }, + ...defaultTestOptions, +}); + +Deno.test({ + name: "[invite] guild.invites()", + async fn() { + await ifItFailsBlameWolf("getter"); + }, + ...defaultTestOptions, +}); diff --git a/tests/mod.ts b/tests/mod.ts index daa91c90f..a8d890d61 100644 --- a/tests/mod.ts +++ b/tests/mod.ts @@ -34,6 +34,13 @@ import "./emojis/edit_emoji.ts"; import "./emojis/get_emoji.ts"; import "./emojis/get_emojis.ts"; +// Invites tests +import "./invites/create_invite.ts"; +import "./invites/delete_invite.ts"; +import "./invites/get_channel_invites.ts"; +import "./invites/get_invite.ts"; +import "./invites/get_invites.ts"; + // Messages tests import "./messages/add_reaction.ts"; import "./messages/add_reactions.ts";