From df0e4052ad51186b176d9743ea4cb9d9ad6aa04e Mon Sep 17 00:00:00 2001 From: Skillz4Killz <23035000+Skillz4Killz@users.noreply.github.com> Date: Thu, 4 Nov 2021 00:29:20 +0000 Subject: [PATCH] add clone channel unit test --- src/bot.ts | 4 ++++ src/helpers/channels/clone_channel.ts | 2 +- src/helpers/mod.ts | 2 ++ tests/helpers/channels/cloneChannel.ts | 20 ++++++++++++++++++++ tests/mod.ts | 13 +++++++++++++ 5 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 tests/helpers/channels/cloneChannel.ts diff --git a/src/bot.ts b/src/bot.ts index fe6448c46..b70c8ab4d 100644 --- a/src/bot.ts +++ b/src/bot.ts @@ -17,6 +17,7 @@ import { calculateBits, isHigherPosition, requireOverwritePermissions, + calculatePermissions, } from "./util/permissions.ts"; import { checkRateLimits, @@ -322,6 +323,7 @@ export function createUtils(options: Partial) { formatImageURL, validateSlashCommands, requireOverwritePermissions, + calculatePermissions, }; } @@ -358,6 +360,7 @@ export interface HelperUtils { formatImageURL: typeof formatImageURL; validateSlashCommands: typeof validateSlashCommands; requireOverwritePermissions: typeof requireOverwritePermissions; + calculatePermissions: typeof calculatePermissions; } export function createGatewayManager( @@ -481,6 +484,7 @@ export interface Helpers { batchEditSlashCommandPermissions: typeof helpers.batchEditSlashCommandPermissions; categoryChildren: typeof helpers.categoryChildren; channelOverwriteHasPermission: typeof helpers.channelOverwriteHasPermission; + cloneChannel: typeof helpers.cloneChannel; connectToVoiceChannel: typeof helpers.connectToVoiceChannel; createChannel: typeof helpers.createChannel; createEmoji: typeof helpers.createEmoji; diff --git a/src/helpers/channels/clone_channel.ts b/src/helpers/channels/clone_channel.ts index 06c4ba793..6b3c7847b 100644 --- a/src/helpers/channels/clone_channel.ts +++ b/src/helpers/channels/clone_channel.ts @@ -17,7 +17,7 @@ export async function cloneChannel(bot: Bot, channelId: bigint, reason?: string) name: channelToClone.name!, topic: channelToClone.topic || undefined, permissionOverwrites: channelToClone.permissionOverwrites.map((overwrite) => ({ - id: overwrite.id.toString(), + id: overwrite.id, type: overwrite.type, allow: bot.utils.calculatePermissions(overwrite.allow), deny: bot.utils.calculatePermissions(overwrite.deny), diff --git a/src/helpers/mod.ts b/src/helpers/mod.ts index 37baae052..0b70e3f2d 100644 --- a/src/helpers/mod.ts +++ b/src/helpers/mod.ts @@ -153,6 +153,7 @@ import { startPrivateThread } from "./channels/threads/start_private_thread.ts"; import { startThread } from "./channels/threads/start_thread.ts"; import { unarchiveThread } from "./channels/threads/unarchive_thread.ts"; import { unlockThread } from "./channels/threads/unlock_thread.ts"; +import { cloneChannel } from "./channels/clone_channel.ts"; export { addDiscoverySubcategory, @@ -165,6 +166,7 @@ export { batchEditSlashCommandPermissions, categoryChildren, channelOverwriteHasPermission, + cloneChannel, connectToVoiceChannel, createChannel, createEmoji, diff --git a/tests/helpers/channels/cloneChannel.ts b/tests/helpers/channels/cloneChannel.ts new file mode 100644 index 000000000..e43f86444 --- /dev/null +++ b/tests/helpers/channels/cloneChannel.ts @@ -0,0 +1,20 @@ +import { Bot } from "../../../src/bot.ts"; +import { DiscordenoChannel } from "../../../src/transformers/channel.ts"; +import { assertExists, assertEquals } from "../../deps.ts"; +import { delayUntil } from "../../utils.ts"; + +export async function cloneChannelTests(bot: Bot, guildId: bigint, channel: DiscordenoChannel, options: {reason?: string}, t: Deno.TestContext) { + const cloned = await bot.helpers.cloneChannel(channel.id, options.reason); + + //Assertations + assertExists(cloned); + assertEquals(cloned.type, channel.type); + + // Delay the execution to allow CHANNEL_CREATE event to be processed + await delayUntil(10000, () => bot.cache.channels.has(cloned.id)); + + assertExists(bot.cache.channels.has(cloned.id)); + assertEquals(channel.topic, cloned.topic); + assertEquals(channel.bitrate, cloned.bitrate); + assertEquals(channel.permissionOverwrites.length, cloned.permissionOverwrites.length); +} diff --git a/tests/mod.ts b/tests/mod.ts index 0bb233f60..e88c0803e 100644 --- a/tests/mod.ts +++ b/tests/mod.ts @@ -59,6 +59,7 @@ import { getVanityURLTests } from "./helpers/guilds/getVanityUrl.ts"; import { getDiscoveryCategoriesTest, validDiscoveryTermTest } from "./helpers/misc/discoveries.ts"; import { categoryChildrenTest } from "./helpers/channels/categoryChannels.ts"; import { channelOverwriteHasPermissionTest } from "./helpers/channels/channelOverwriteHasPermission.ts"; +import { cloneChannelTests } from "./helpers/channels/cloneChannel.ts"; // CHANGE TO TRUE WHEN DEBUGGING SANITIZATION ERRORS const sanitizeMode = { @@ -555,6 +556,18 @@ Deno.test({ async fn() { await channelOverwriteHasPermissionTest(bot, guild.id, t); } + }), + t.step({ + name: "[channel] clone a channel w/o a reason", + async fn() { + await cloneChannelTests(bot, guild.id, channel, {}, t); + } + }), + t.step({ + name: "[channel] clone a channel w a reason", + async fn() { + await cloneChannelTests(bot, guild.id, channel, { reason: "Blame wolf"}, t); + } }) ]);