tests: add channel tests (#807)

This commit is contained in:
Skillz4Killz
2021-04-10 09:38:58 -04:00
committed by GitHub
parent 55d73d1937
commit 66b0aa3685
11 changed files with 179 additions and 70 deletions
+6 -3
View File
@@ -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<PermissionStrings> = 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);
+99 -9
View File
@@ -1,18 +1,19 @@
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";
Deno.test({
name: "[channel] create a new channel",
async fn() {
const channel = await createChannel(tempData.guildId, {
name: "Discordeno-test",
});
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);
tempData.channelId = channel.id;
if (save) tempData.channelId = channel.id;
// Delay the execution by 5 seconds to allow CHANNEL_CREATE event to be processed
await delay(5000);
@@ -22,6 +23,95 @@ Deno.test({
"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 text channel",
async fn() {
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[];
+4 -1
View File
@@ -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({
+3 -1
View File
@@ -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",
+3 -1
View File
@@ -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({
+19 -18
View File
@@ -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,16 +17,15 @@ 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(
(
await createEmoji(
tempData.guildId,
"blamewolf",
"https://cdn.discordapp.com/emojis/814955268123000832.png",
@@ -36,8 +33,9 @@ async function ifItFailsBlameWolf(type: "getter" | "raw", custom = false) {
name: "blamewolf",
image: "https://cdn.discordapp.com/emojis/814955268123000832.png",
roles: [],
},
)).id
}
)
).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
);
}
+14 -11
View File
@@ -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);
// 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.");
}
}
+4 -1
View File
@@ -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!");
+5 -4
View File
@@ -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
+1 -1
View File
@@ -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 = {
+2 -1
View File
@@ -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.