From 66b0aa36852b4f4c691a9548e1b51eeb07fdcea2 Mon Sep 17 00:00:00 2001 From: Skillz4Killz <23035000+Skillz4Killz@users.noreply.github.com> Date: Sat, 10 Apr 2021 09:38:58 -0400 Subject: [PATCH] tests: add channel tests (#807) --- src/helpers/channels/create_channel.ts | 9 +- tests/channels/create_channel.ts | 130 +++++++++++++++++++++---- tests/channels/delete_channel.ts | 5 +- tests/guilds/create_guild.ts | 4 +- tests/guilds/delete_server.ts | 4 +- tests/messages/add_reaction.ts | 51 +++++----- tests/messages/create_message.ts | 27 ++--- tests/messages/delete_message.ts | 5 +- tests/messages/edit_message.ts | 9 +- tests/util/utils.ts | 2 +- tests/ws/ws_close.ts | 3 +- 11 files changed, 179 insertions(+), 70 deletions(-) diff --git a/src/helpers/channels/create_channel.ts b/src/helpers/channels/create_channel.ts index 25c40cad6..817656efd 100644 --- a/src/helpers/channels/create_channel.ts +++ b/src/helpers/channels/create_channel.ts @@ -15,14 +15,14 @@ import { DiscordChannelTypes } from "../../types/channels/channel_types.ts"; /** Create a channel in your server. Bot needs MANAGE_CHANNEL permissions in the server. */ export async function createChannel( guildId: string, - options?: CreateGuildChannel, + options?: CreateGuildChannel ) { const requiredPerms: Set = new Set(["MANAGE_CHANNELS"]); options?.permissionOverwrites?.forEach((overwrite) => { eventHandlers.debug?.( "loop", - `Running forEach loop in create_channel file.`, + `Running forEach loop in create_channel file.` ); overwrite.allow.forEach(requiredPerms.add, requiredPerms); overwrite.deny.forEach(requiredPerms.add, requiredPerms); @@ -30,6 +30,9 @@ export async function createChannel( await requireBotGuildPermissions(guildId, [...requiredPerms]); + // BITRATES ARE IN THOUSANDS SO IF USER PROVIDES 32 WE CONVERT TO 32000 + if (options?.bitrate && options.bitrate < 1000) options.bitrate *= 1000; + const result = (await rest.runMethod( "post", endpoints.GUILD_CHANNELS(guildId), @@ -42,7 +45,7 @@ export async function createChannel( deny: calculateBits(perm.deny), })), type: options?.type || DiscordChannelTypes.GUILD_TEXT, - }, + } )) as DiscordChannel; const discordenoChannel = await structures.createDiscordenoChannel(result); diff --git a/tests/channels/create_channel.ts b/tests/channels/create_channel.ts index 7a0e8fdd0..78032eb3b 100644 --- a/tests/channels/create_channel.ts +++ b/tests/channels/create_channel.ts @@ -1,27 +1,117 @@ -import { cache, createChannel, delay } from "../../mod.ts"; import { defaultTestOptions, tempData } from "../ws/start_bot.ts"; -import { assertExists } from "../deps.ts"; +import { assertEquals, assertExists } from "../deps.ts"; +import { cache } from "../../src/cache.ts"; +import { DiscordChannelTypes } from "../../src/types/channels/channel_types.ts"; +import { CreateGuildChannel } from "../../src/types/guilds/create_guild_channel.ts"; +import { delay } from "../../src/util/utils.ts"; +import { createChannel } from "../../src/helpers/channels/create_channel.ts"; + +async function ifItFailsBlameWolf(options: CreateGuildChannel, save = false) { + const channel = await createChannel(tempData.guildId, options); + + // Assertions + assertExists(channel); + assertEquals(channel.type, options.type || DiscordChannelTypes.GUILD_TEXT); + + if (save) tempData.channelId = channel.id; + + // Delay the execution by 5 seconds to allow CHANNEL_CREATE event to be processed + await delay(5000); + + if (!cache.channels.has(channel.id)) { + throw new Error( + "The channel seemed to be created but it was not cached.", + ); + } + + if (options.topic && channel.topic !== options.topic) { + throw new Error( + "The channel was supposed to have a topic but it does not appear to be the same topic.", + ); + } + + if (options.bitrate && channel.bitrate !== options.bitrate) { + throw new Error( + "The channel was supposed to have a bitrate but it does not appear to be the same bitrate.", + ); + } +} Deno.test({ - name: "[channel] create a new channel", + name: "[channel] create a new text channel", async fn() { - const channel = await createChannel(tempData.guildId, { - name: "Discordeno-test", - }); - - // Assertions - assertExists(channel); - - tempData.channelId = channel.id; - - // Delay the execution by 5 seconds to allow CHANNEL_CREATE event to be processed - await delay(5000); - - if (!cache.channels.has(channel.id)) { - throw new Error( - "The channel seemed to be created but it was not cached.", - ); - } + await ifItFailsBlameWolf({ name: "Discordeno-test"}, true); }, ...defaultTestOptions, }); + +Deno.test({ + name: "[channel] create a new category channel", + async fn() { + await ifItFailsBlameWolf({ name: "Discordeno-test", type: DiscordChannelTypes.GUILD_CATEGORY}, true); + }, + ...defaultTestOptions, +}); + +// Deno.test({ +// name: "[channel] create a new news channel", +// async fn() { +// await ifItFailsBlameWolf({ name: "Discordeno-test", type: DiscordChannelTypes.GUILD_NEWS}, true); +// }, +// ...defaultTestOptions, +// }); + +// Deno.test({ +// name: "[channel] create a new store channel", +// async fn() { +// await ifItFailsBlameWolf({ name: "Discordeno-test", type: DiscordChannelTypes.GUILD_STORE}, true); +// }, +// ...defaultTestOptions, +// }); + +Deno.test({ + name: "[channel] create a new voice channel", + async fn() { + await ifItFailsBlameWolf({ name: "Discordeno-test", type: DiscordChannelTypes.GUILD_VOICE}, true); + }, + ...defaultTestOptions, +}); + +Deno.test({ + name: "[channel] create a new voice channel with a bitrate", + async fn() { + await ifItFailsBlameWolf({ name: "discordeno-test", type: DiscordChannelTypes.GUILD_VOICE, bitrate: 32000 }, true); + }, + ...defaultTestOptions, +}); + +Deno.test({ + name: "[channel] create a new voice channel with a user limit", + async fn() { + await ifItFailsBlameWolf({ name: "Discordeno-test", type: DiscordChannelTypes.GUILD_VOICE, userLimit: 32 }, true); + }, + ...defaultTestOptions, +}); + +Deno.test({ + name: "[channel] create a new text channel with a rate limit per user", + async fn() { + await ifItFailsBlameWolf({ name: "Discordeno-test", rateLimitPerUser: 2423 }, true); + }, + ...defaultTestOptions, +}); + +Deno.test({ + name: "[channel] create a new text channel with NSFW", + async fn() { + await ifItFailsBlameWolf({ name: "Discordeno-test", nsfw: true }, true); + }, + ...defaultTestOptions, +}); + + +// TODO: Need to validate tests for these options +// /** Sorting position of the channel */ +// position?: number; +// /** The channel's permission overwrites */ +// permissionOverwrites?: Overwrite[]; \ No newline at end of file diff --git a/tests/channels/delete_channel.ts b/tests/channels/delete_channel.ts index 401b91170..2b779f996 100644 --- a/tests/channels/delete_channel.ts +++ b/tests/channels/delete_channel.ts @@ -1,4 +1,7 @@ -import { cache, createChannel, delay, deleteChannel } from "../../mod.ts"; +import { cache } from "../../src/cache.ts"; +import { createChannel } from "../../src/helpers/channels/create_channel.ts"; +import { deleteChannel } from "../../src/helpers/channels/delete_channel.ts"; +import { delay } from "../../src/util/utils.ts"; import { defaultTestOptions, tempData } from "../ws/start_bot.ts"; Deno.test({ diff --git a/tests/guilds/create_guild.ts b/tests/guilds/create_guild.ts index 7da00835b..6834e7632 100644 --- a/tests/guilds/create_guild.ts +++ b/tests/guilds/create_guild.ts @@ -1,6 +1,8 @@ -import { cache, createGuild, delay } from "../../mod.ts"; import { defaultTestOptions, tempData } from "../ws/start_bot.ts"; import { assertExists } from "../deps.ts"; +import { cache } from "../../src/cache.ts"; +import { delay } from "../../src/util/utils.ts"; +import { createGuild } from "../../src/helpers/guilds/create_guild.ts"; Deno.test({ name: "[guild] create a new guild", diff --git a/tests/guilds/delete_server.ts b/tests/guilds/delete_server.ts index e92fd8da7..76c806782 100644 --- a/tests/guilds/delete_server.ts +++ b/tests/guilds/delete_server.ts @@ -1,4 +1,6 @@ -import { cache, delay, deleteServer } from "../../mod.ts"; +import { cache } from "../../src/cache.ts"; +import { deleteServer } from "../../src/helpers/guilds/delete_server.ts"; +import { delay } from "../../src/util/utils.ts"; import { defaultTestOptions, tempData } from "../ws/start_bot.ts"; Deno.test({ diff --git a/tests/messages/add_reaction.ts b/tests/messages/add_reaction.ts index 08cc5f7ac..30f4cd88a 100644 --- a/tests/messages/add_reaction.ts +++ b/tests/messages/add_reaction.ts @@ -1,13 +1,11 @@ -import { - addReaction, - cache, - createEmoji, - delay, - DiscordReaction, - sendMessage, -} from "../../mod.ts"; import { defaultTestOptions, tempData } from "../ws/start_bot.ts"; import { assertEquals, assertExists } from "../deps.ts"; +import { cache } from "../../src/cache.ts"; +import { DiscordReaction } from "../../src/types/messages/reaction.ts"; +import { delay } from "../../src/util/utils.ts"; +import { sendMessage } from "../../src/helpers/messages/send_message.ts"; +import { addReaction } from "../../src/helpers/messages/add_reaction.ts"; +import { createEmoji } from "../../src/helpers/emojis/create_emoji.ts" async function ifItFailsBlameWolf(type: "getter" | "raw", custom = false) { const message = await sendMessage(tempData.channelId, "Hello World!"); @@ -19,25 +17,25 @@ async function ifItFailsBlameWolf(type: "getter" | "raw", custom = false) { await delay(5000); if (!cache.messages.has(message.id)) { - throw new Error( - "The message seemed to be sent but it was not cached.", - ); + throw new Error("The message seemed to be sent but it was not cached."); } let emojiId = "❤"; if (custom) { emojiId = `<:blamewolf:${ - (await createEmoji( - tempData.guildId, - "blamewolf", - "https://cdn.discordapp.com/emojis/814955268123000832.png", - { - name: "blamewolf", - image: "https://cdn.discordapp.com/emojis/814955268123000832.png", - roles: [], - }, - )).id + ( + await createEmoji( + tempData.guildId, + "blamewolf", + "https://cdn.discordapp.com/emojis/814955268123000832.png", + { + name: "blamewolf", + image: "https://cdn.discordapp.com/emojis/814955268123000832.png", + roles: [], + } + ) + ).id }>`; } @@ -50,10 +48,13 @@ async function ifItFailsBlameWolf(type: "getter" | "raw", custom = false) { await delay(5000); assertEquals( - await cache.messages.get(message.id)?.reactions?.filter(( - reaction: DiscordReaction, - ) => reaction.emoji?.name === (custom ? "blamewolf" : "❤")).length, - 1, + await cache.messages + .get(message.id) + ?.reactions?.filter( + (reaction: DiscordReaction) => + reaction.emoji?.name === (custom ? "blamewolf" : "❤") + ).length, + 1 ); } diff --git a/tests/messages/create_message.ts b/tests/messages/create_message.ts index fbd03b51b..4db40aff1 100644 --- a/tests/messages/create_message.ts +++ b/tests/messages/create_message.ts @@ -1,18 +1,23 @@ -import { cache, delay, sendMessage } from "../../mod.ts"; import { defaultTestOptions, tempData } from "../ws/start_bot.ts"; import { assertExists } from "../deps.ts"; +import { cache } from "../../src/cache.ts"; +import { delay } from "../../src/util/utils.ts"; +import { sendMessage } from "../../src/helpers/messages/send_message.ts"; +import { createChannel } from "../../src/helpers/channels/create_channel.ts"; async function ifItFailsBlameWolf(type: "getter" | "raw") { - let message; - if (type === "raw") { - message = await sendMessage(tempData.channelId, "Hello World!"); - } else { - const channel = await cache.channels.get(tempData.channelId); + const channel = await createChannel(tempData.guildId, { + name: "Discordeno-test", + }); - assertExists(channel); + assertExists(channel); + // Wait few seconds for the channel create event to arrive and cache it + await delay(5000); - message = await channel.send("Hello World!"); - } + const message = + type === "raw" + ? await sendMessage(channel.id, "Hello World!") + : await channel.send("Hello World!"); // Assertions assertExists(message); @@ -21,9 +26,7 @@ async function ifItFailsBlameWolf(type: "getter" | "raw") { await delay(5000); if (!cache.messages.has(message.id)) { - throw new Error( - "The message seemed to be sent but it was not cached.", - ); + throw new Error("The message seemed to be sent but it was not cached."); } } diff --git a/tests/messages/delete_message.ts b/tests/messages/delete_message.ts index b141e072b..371a79a9f 100644 --- a/tests/messages/delete_message.ts +++ b/tests/messages/delete_message.ts @@ -1,6 +1,9 @@ -import { cache, delay, deleteMessage, sendMessage } from "../../mod.ts"; import { defaultTestOptions, tempData } from "../ws/start_bot.ts"; import { assertExists } from "../deps.ts"; +import { cache } from "../../src/cache.ts"; +import { delay } from "../../src/util/utils.ts"; +import { sendMessage } from "../../src/helpers/messages/send_message.ts"; +import { deleteMessage } from "../../src/helpers/messages/delete_message.ts"; async function ifItFailsBlameWolf(type: "getter" | "raw", reason?: string) { const message = await sendMessage(tempData.channelId, "Hello World!"); diff --git a/tests/messages/edit_message.ts b/tests/messages/edit_message.ts index 0cfaa390a..830d3af89 100644 --- a/tests/messages/edit_message.ts +++ b/tests/messages/edit_message.ts @@ -1,6 +1,9 @@ -import { cache, delay, editMessage, sendMessage } from "../../mod.ts"; import { defaultTestOptions, tempData } from "../ws/start_bot.ts"; import { assertEquals, assertExists } from "../deps.ts"; +import { cache } from "../../src/cache.ts"; +import { delay } from "../../src/util/utils.ts"; +import { sendMessage } from "../../src/helpers/messages/send_message.ts"; +import { editMessage } from "../../src/helpers/messages/edit_message.ts"; async function ifItFailsBlameWolf(type: "getter" | "raw") { const message = await sendMessage(tempData.channelId, "Hello World!"); @@ -11,9 +14,7 @@ async function ifItFailsBlameWolf(type: "getter" | "raw") { await delay(5000); // Make sure the message was created. if (!cache.messages.has(message.id)) { - throw new Error( - "The message seemed to be sent but it was not cached.", - ); + throw new Error("The message seemed to be sent but it was not cached."); } // Edit the message now diff --git a/tests/util/utils.ts b/tests/util/utils.ts index 805f0a467..77e3b8aef 100644 --- a/tests/util/utils.ts +++ b/tests/util/utils.ts @@ -1,4 +1,4 @@ -import { camelKeysToSnakeCase, snakeKeysToCamelCase } from "../../mod.ts"; +import { snakeKeysToCamelCase,camelKeysToSnakeCase } from "../../src/util/utils.ts"; import { assertEquals } from "../deps.ts"; const testSnakeObject = { diff --git a/tests/ws/ws_close.ts b/tests/ws/ws_close.ts index f6d274aae..3b313fc99 100644 --- a/tests/ws/ws_close.ts +++ b/tests/ws/ws_close.ts @@ -1,4 +1,5 @@ -import { delay, ws } from "../../mod.ts"; +import { delay } from "../../src/util/utils.ts"; +import { ws } from "../../src/ws/ws.ts"; import { defaultTestOptions } from "./start_bot.ts"; // Exit the Deno process once all tests are done.