diff --git a/tests/channels/channel_overwrite_has_permission.ts b/tests/channels/channel_overwrite_has_permission.ts new file mode 100644 index 000000000..3c31e13f6 --- /dev/null +++ b/tests/channels/channel_overwrite_has_permission.ts @@ -0,0 +1,63 @@ +import { defaultTestOptions, tempData } from "../ws/start_bot.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 { createChannel } from "../../src/helpers/channels/create_channel.ts"; +import { delayUntil } from "../util/delay_until.ts"; +import { DiscordOverwriteTypes } from "../../src/types/channels/overwrite_types.ts"; +import { botId } from "../../src/bot.ts"; +import { deleteChannelOverwrite } from "../../src/helpers/channels/delete_channel_overwrite.ts"; +import {editChannelOverwrite} from "../../src/helpers/channels/edit_channel_overwrite.ts"; +import {channelOverwriteHasPermission} from "../../src/helpers/channels/channel_overwrite_has_permission.ts"; +import {DiscordBitwisePermissionFlags} from "../../src/types/permissions/bitwise_permission_flags.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); + + // 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."); + } + + if ( + options.permissionOverwrites && + channel.permissionOverwrites?.length !== options.permissionOverwrites.length + ) { + throw new Error( + "The channel was supposed to have a permissionOverwrites but it does not appear to be the same permissionOverwrites.", + ); + } + + assertEquals( + channelOverwriteHasPermission(channel.guildId, botId, cache.channels.get(channel.id)?.permissionOverwrites, options.permissionOverwrites ? options.permissionOverwrites[0].allow : []), + true + ) +} + +Deno.test({ + name: "[channel] edit channel permission overwrites", + async fn() { + await ifItFailsBlameWolf( + { + name: "Discordeno-test", + permissionOverwrites: [ + { + id: botId, + type: DiscordOverwriteTypes.MEMBER, + allow: ["VIEW_CHANNEL"], + deny: [], + }, + ], + }, + true, + ); + }, + ...defaultTestOptions, +}); diff --git a/tests/channels/edit_channel.ts b/tests/channels/edit_channel.ts new file mode 100644 index 000000000..1b7ffd430 --- /dev/null +++ b/tests/channels/edit_channel.ts @@ -0,0 +1,67 @@ +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 {editChannel} from "../../src/helpers/channels/edit_channel.ts"; +import {assertEquals} from "../deps.ts"; + +async function ifItFailsBlameWolf(type: "getter" | "raw", reason?: string) { + // Create the necessary channels + const channel = await createChannel(tempData.guildId, { + name: "edit-channel", + }); + // wait 5 seconds to give it time for CHANNEL_CREATE event + await delayUntil(3000, () => cache.channels.has(channel.id)); + // Make sure the channel was created. + if (!cache.channels.has(channel.id)) { + throw new Error( + "The channel should have been created but it is not in the cache.", + ); + } + + // Edit the channel now + if (type === "raw") { + await editChannel(channel.id, { + name: "new-name" + }, reason); + } else { + await channel.edit({ + name: "new-name" + }); + } + // wait 5 seconds to give it time for CHANNEL_UPDATE event + await delayUntil(3000, () => cache.channels.get(channel.id)?.name === "new-name"); + assertEquals(cache.channels.get(channel.id)?.name, "new-name"); +} + +Deno.test({ + name: "[channel] edit a channel without a reason.", + async fn() { + await ifItFailsBlameWolf("raw"); + }, + ...defaultTestOptions, +}); + +Deno.test({ + name: "[channel] edit a channel with a reason.", + async fn() { + await ifItFailsBlameWolf("raw", "with a reason"); + }, + ...defaultTestOptions, +}); + +Deno.test({ + name: "[channel] channel.edit() without a reason.", + async fn() { + await ifItFailsBlameWolf("getter"); + }, + ...defaultTestOptions, +}); + +Deno.test({ + name: "[channel] channel.edit() with a reason.", + async fn() { + await ifItFailsBlameWolf("getter", "with a reason"); + }, + ...defaultTestOptions, +}); diff --git a/tests/channels/edit_channel_overwrite.ts b/tests/channels/edit_channel_overwrite.ts new file mode 100644 index 000000000..807350399 --- /dev/null +++ b/tests/channels/edit_channel_overwrite.ts @@ -0,0 +1,74 @@ +import { defaultTestOptions, tempData } from "../ws/start_bot.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 { createChannel } from "../../src/helpers/channels/create_channel.ts"; +import { delayUntil } from "../util/delay_until.ts"; +import { DiscordOverwriteTypes } from "../../src/types/channels/overwrite_types.ts"; +import { botId } from "../../src/bot.ts"; +import { deleteChannelOverwrite } from "../../src/helpers/channels/delete_channel_overwrite.ts"; +import {editChannelOverwrite} from "../../src/helpers/channels/edit_channel_overwrite.ts"; +import {channelOverwriteHasPermission} from "../../src/helpers/channels/channel_overwrite_has_permission.ts"; +import {DiscordBitwisePermissionFlags} from "../../src/types/permissions/bitwise_permission_flags.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); + + // 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."); + } + + if ( + options.permissionOverwrites && + channel.permissionOverwrites?.length !== options.permissionOverwrites.length + ) { + throw new Error( + "The channel was supposed to have a permissionOverwrites but it does not appear to be the same permissionOverwrites.", + ); + } + + await editChannelOverwrite(channel.guildId, channel.id, botId, { + type: DiscordOverwriteTypes.MEMBER, + allow: ["VIEW_CHANNEL", "ADD_REACTIONS"], + deny: [], + }); + + await delayUntil( + 10000, + () => channelOverwriteHasPermission(channel.guildId, botId, cache.channels.get(channel.id)?.permissionOverwrites, ["VIEW_CHANNEL", "ADD_REACTIONS"]), + ); + + assertEquals( + channelOverwriteHasPermission(channel.guildId, botId, cache.channels.get(channel.id)?.permissionOverwrites, ["VIEW_CHANNEL", "ADD_REACTIONS"]), + true + ) +} + +Deno.test({ + name: "[channel] edit channel permission overwrites", + async fn() { + await ifItFailsBlameWolf( + { + name: "Discordeno-test", + permissionOverwrites: [ + { + id: botId, + type: DiscordOverwriteTypes.MEMBER, + allow: ["VIEW_CHANNEL"], + deny: [], + }, + ], + }, + true, + ); + }, + ...defaultTestOptions, +}); diff --git a/tests/channels/get_channel.ts b/tests/channels/get_channel.ts new file mode 100644 index 000000000..2fed1f2e6 --- /dev/null +++ b/tests/channels/get_channel.ts @@ -0,0 +1,39 @@ +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 { defaultTestOptions, tempData } from "../ws/start_bot.ts"; +import { delayUntil } from "../util/delay_until.ts"; +import {editChannel} from "../../src/helpers/channels/edit_channel.ts"; +import {assertEquals} from "../deps.ts"; +import {getChannel} from "../../src/helpers/channels/get_channel.ts"; +import {botId} from "../../src/bot.ts"; + +Deno.test({ + name: "[channel] get a channel", + async fn() { + // Create the necessary channels + const channel = await createChannel(tempData.guildId, { + name: "get-channel", + }); + // wait 5 seconds to give it time for CHANNEL_CREATE event + await delayUntil(3000, () => cache.channels.has(channel.id)); + // Make sure the channel was created. + if (!cache.channels.has(channel.id)) { + throw new Error( + "The channel should have been created but it is not in the cache.", + ); + } + + cache.channels.delete(channel.id); + + // Delete the channel now without a reason + await getChannel(channel.id); + await delayUntil(3000, () => cache.channels.has(channel.id)); + + assertEquals( + cache.channels.has(channel.id), + true, + ); + }, + ...defaultTestOptions, +}); diff --git a/tests/channels/get_channels.ts b/tests/channels/get_channels.ts new file mode 100644 index 000000000..b1d14e100 --- /dev/null +++ b/tests/channels/get_channels.ts @@ -0,0 +1,27 @@ +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 { defaultTestOptions, tempData } from "../ws/start_bot.ts"; +import { delayUntil } from "../util/delay_until.ts"; +import {editChannel} from "../../src/helpers/channels/edit_channel.ts"; +import {assertEquals} from "../deps.ts"; +import {getChannel} from "../../src/helpers/channels/get_channel.ts"; +import {botId} from "../../src/bot.ts"; +import {getChannels} from "../../src/helpers/channels/get_channels.ts"; + +Deno.test({ + name: "[channel] get channels.", + async fn() { + cache.channels.clear(); + + // Delete the channel now without a reason + await getChannels(tempData.guildId); + await delayUntil(3000, () => cache.channels.size > 0); + + assertEquals( + cache.channels.size > 0, + true, + ); + }, + ...defaultTestOptions, +}); diff --git a/tests/channels/get_pins.ts b/tests/channels/get_pins.ts new file mode 100644 index 000000000..48df669a3 --- /dev/null +++ b/tests/channels/get_pins.ts @@ -0,0 +1,45 @@ +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 {getChannels} from "../../src/helpers/channels/get_channels.ts"; +import {sendMessage} from "../../src/helpers/messages/send_message.ts"; +import {getPins} from "../../src/helpers/channels/get_pins.ts"; +import {DiscordenoMessage} from "../../src/structures/message.ts"; + +Deno.test({ + name: "[channel] get pins.", + async fn() { + const channel = await createChannel(tempData.guildId, { + name: 'pins-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 message = await sendMessage(tempData.channelId, "Hello World!"); + const secondMessage = await sendMessage(tempData.channelId, "Goodbye World!"); + + // Delay the execution by 5 seconds to allow MESSAGE_CREATE event to be processed + await delayUntil(10000, () => cache.messages.has(message.id) && cache.messages.has(secondMessage.id)); + + await message.pin(); + await secondMessage.pin(); + + const pins = await getPins(tempData.channelId); + + assertEquals( + pins.length, + 2, + ); + }, + ...defaultTestOptions, +}); diff --git a/tests/channels/is_channel_synced.ts b/tests/channels/is_channel_synced.ts new file mode 100644 index 000000000..eb3506a73 --- /dev/null +++ b/tests/channels/is_channel_synced.ts @@ -0,0 +1,60 @@ +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"; + +Deno.test({ + name: "[channel] is channel synced.", + async fn() { + const category = await createChannel(tempData.guildId, { + name: "synced-category", + type: DiscordChannelTypes.GUILD_CATEGORY, + permissionOverwrites: [ + { + id: botId, + type: DiscordOverwriteTypes.MEMBER, + allow: ["VIEW_CHANNEL"], + deny: [], + }, + ], + }); + + // Assertions + assertExists(category); + + // Delay the execution by 5 seconds to allow CHANNEL_CREATE event to be processed + await delayUntil(10000, () => cache.channels.has(category.id)); + + if (!cache.channels.has(category.id)) { + throw new Error("The channel seemed to be created but it was not cached."); + } + + const channel = await createChannel(tempData.guildId, { + name: 'synced-channel', + parentId: category.id + }); + + // 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 isSynced = await isChannelSynced(channel.id); + + assertEquals( + isSynced, + true, + ); + }, + ...defaultTestOptions, +}); diff --git a/tests/channels/start_typing.ts b/tests/channels/start_typing.ts new file mode 100644 index 000000000..71026e88f --- /dev/null +++ b/tests/channels/start_typing.ts @@ -0,0 +1,32 @@ +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"; + +Deno.test({ + name: "[channel] is typing.", + async fn() { + const channel = await createChannel(tempData.guildId, { + name: 'typing-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."); + } + + await startTyping(channel.id); + }, + ...defaultTestOptions, +}); diff --git a/tests/channels/swap_channels.ts b/tests/channels/swap_channels.ts new file mode 100644 index 000000000..206c9a02c --- /dev/null +++ b/tests/channels/swap_channels.ts @@ -0,0 +1,62 @@ +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 {swapChannels} from "../../src/helpers/channels/swap_channels.ts"; + +Deno.test({ + name: "[channel] swap channels", + async fn() { + const channel = await createChannel(tempData.guildId, { + name: 'channel-1', + }); + + // Assertions + assertExists(channel); + + const secondChannel = await createChannel(tempData.guildId, { + name: 'channel-2', + }); + + // 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) && cache.channels.has(secondChannel.id)); + + if (!cache.channels.has(channel.id) || !cache.channels.has(secondChannel.id)) { + throw new Error("The channel seemed to be created but it was not cached."); + } + + await swapChannels(tempData.guildId, [ + { + id: channel.id, + position: secondChannel.position! + }, + { + id: secondChannel.id, + position: channel.position! + } + ]); + + // Delay the execution by 5 seconds to allow CHANNEL_UPDATE event to be processed + await delayUntil(10000, () => cache.channels.get(channel.id)?.position === secondChannel.position && cache.channels.get(secondChannel.id)?.position === channel.position); + + assertEquals( + cache.channels.get(channel.id)?.position, + secondChannel.position + ) + + assertEquals( + cache.channels.get(secondChannel.id)?.position, + channel.position + ) + }, + ...defaultTestOptions, +}); diff --git a/tests/mod.ts b/tests/mod.ts index 98439366e..401f083e9 100644 --- a/tests/mod.ts +++ b/tests/mod.ts @@ -12,10 +12,20 @@ import "./ws/start_bot.ts"; import "./guilds/create_guild.ts"; // Channel tests -import "./channels/create_channel.ts"; import "./channels/category_children.ts"; +import "./channels/channel_overwrite_has_permission.ts"; +import "./channels/create_channel.ts"; import "./channels/delete_channel.ts"; import "./channels/delete_channel_overwrite.ts"; +import "./channels/edit_channel.ts"; +import "./channels/edit_channel_overwrite.ts"; +import "./channels/get_channel.ts"; +import "./channels/get_channels.ts"; +import "./channels/get_pins.ts"; +import "./channels/is_channel_synced.ts"; +import "./channels/start_typing.ts"; +import "./channels/swap_channels.ts"; + // Emojis tests import "./emojis/create_emoji.ts";