From 19941ac50ed1613e01618165dd57116cadd576a3 Mon Sep 17 00:00:00 2001 From: chroventer Date: Thu, 24 Sep 2020 11:14:12 -0700 Subject: [PATCH 01/24] Create GitHub action for testing --- .github/workflows/test.yml | 15 +++++++++++++++ test/mod.test.ts | 0 2 files changed, 15 insertions(+) create mode 100644 .github/workflows/test.yml create mode 100644 test/mod.test.ts diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 000000000..9cc5554c9 --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,15 @@ +name: Test +on: + push: + branches: + - master +jobs: + test: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - uses: denolib/setup-deno@master + - name: Cache dependencies + run: deno cache mod.ts test/mod.test.ts + - name: Run test script + run: deno test -A \ No newline at end of file diff --git a/test/mod.test.ts b/test/mod.test.ts new file mode 100644 index 000000000..e69de29bb From 9746a8445a57b9ac543155acde9ebf679a72c70c Mon Sep 17 00:00:00 2001 From: Skillz Date: Thu, 24 Sep 2020 15:57:33 -0400 Subject: [PATCH 02/24] phase 1 of unit tests --- src/module/basicShard.ts | 3 +- src/utils/collection.ts | 2 +- tests/mod.test.ts | 69 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 72 insertions(+), 2 deletions(-) create mode 100644 tests/mod.test.ts diff --git a/src/module/basicShard.ts b/src/module/basicShard.ts index d2be980f6..dd8f4499e 100644 --- a/src/module/basicShard.ts +++ b/src/module/basicShard.ts @@ -19,8 +19,9 @@ import { } from "./client.ts"; import { delay } from "https://deno.land/std@0.67.0/async/delay.ts"; import { inflate } from "https://deno.land/x/zlib.es@v1.0.0/mod.ts"; +import { Collection } from "../utils/collection.ts"; -const basicShards = new Map(); +export const basicShards = new Collection(); const heartbeating = new Set(); export interface BasicShard { diff --git a/src/utils/collection.ts b/src/utils/collection.ts index 0ceb347e5..4b5c7b5f1 100644 --- a/src/utils/collection.ts +++ b/src/utils/collection.ts @@ -17,7 +17,7 @@ export class Collection extends Map { } first() { - return this.values().next().value; + return this.values().next().value as V; } last(): V { diff --git a/tests/mod.test.ts b/tests/mod.test.ts new file mode 100644 index 000000000..fcdd135bc --- /dev/null +++ b/tests/mod.test.ts @@ -0,0 +1,69 @@ +import { createClient, Intents, botID } from "../mod.ts"; +import { + assertEquals, +} from "https://deno.land/std/testing/asserts.ts"; +import { createServer, deleteServer } from "../src/handlers/guild.ts"; +import { CreateGuildPayload } from "../src/types/guild.ts"; +import { delay } from "https://deno.land/std@0.67.0/async/delay.ts"; +import { basicShards } from "../src/module/basicShard.ts"; +import { DISTANCE_EXTRA_BIT_BASE } from "https://deno.land/x/zlib.es@v1.0.0/const.ts"; + +const token = Deno.env.get("DISCORD_TOKEN"); +if (!token) throw "No Token Provided!"; + +createClient({ + token, + intents: [Intents.GUILD_MESSAGES, Intents.GUILDS], + eventHandlers: { + // debug: function (data) { + // console.log(data); + // }, + }, +}); + +Deno.test({ + name: "Connecting to gateway", + fn: async () => { + await delay(15000); + assertEquals(botID, "675412054529540107"); + }, + sanitizeResources: false, +}); + +let guildID = ""; + +Deno.test({ + name: "Creating a new guild(Fresh Env)", + fn: async () => { + const result = await createServer({ name: "Discordeno Test Zone" }).catch( + (error) => console.error(error), + ) as CreateGuildPayload; + + guildID = result.id; + assertEquals(typeof result.id, "string"); + }, + sanitizeOps: false, +}); + +Deno.test({ + name: "Deleting guild(Bot is owner)", + fn: async () => { + if (!guildID) throw "The guild id was not present."; + await deleteServer(guildID); + guildID = ""; + + assertEquals(guildID, ""); + }, + sanitizeOps: false, +}); + +// This is meant to be the final test that forcefully crashes the bot +Deno.test({ + name: "Closing Bot! Tests Complete!", + fn: async () => { + const shard = basicShards.first(); + + await shard.socket.close(); + await delay(120000); + }, +}); From 6b2211bc30d8a419bd9cb5528710cca569e4979c Mon Sep 17 00:00:00 2001 From: Skillz Date: Thu, 24 Sep 2020 23:28:33 -0400 Subject: [PATCH 03/24] exit on last test --- tests/mod.test.ts | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/tests/mod.test.ts b/tests/mod.test.ts index fcdd135bc..99e37f534 100644 --- a/tests/mod.test.ts +++ b/tests/mod.test.ts @@ -5,8 +5,6 @@ import { import { createServer, deleteServer } from "../src/handlers/guild.ts"; import { CreateGuildPayload } from "../src/types/guild.ts"; import { delay } from "https://deno.land/std@0.67.0/async/delay.ts"; -import { basicShards } from "../src/module/basicShard.ts"; -import { DISTANCE_EXTRA_BIT_BASE } from "https://deno.land/x/zlib.es@v1.0.0/const.ts"; const token = Deno.env.get("DISCORD_TOKEN"); if (!token) throw "No Token Provided!"; @@ -61,9 +59,8 @@ Deno.test({ Deno.test({ name: "Closing Bot! Tests Complete!", fn: async () => { - const shard = basicShards.first(); - - await shard.socket.close(); - await delay(120000); + Deno.exit(); }, + sanitizeOps: false, + sanitizeResources: false, }); From 0ae6c0f353978ac7a1b6668a25bada704574608c Mon Sep 17 00:00:00 2001 From: chroventer Date: Fri, 25 Sep 2020 02:37:36 -0700 Subject: [PATCH 04/24] Move external dependencies to deps.ts --- deps.ts | 1 + tests/mod.test.ts | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/deps.ts b/deps.ts index c1e06a977..57c27cc2a 100644 --- a/deps.ts +++ b/deps.ts @@ -8,3 +8,4 @@ export { } from "https://deno.land/std@0.67.0/ws/mod.ts"; export { delay } from "https://deno.land/std@0.67.0/async/delay.ts"; export { inflate } from "https://deno.land/x/zlib.es@v1.0.0/mod.ts"; +export { assertEquals } from "https://deno.land/std/testing/asserts.ts"; diff --git a/tests/mod.test.ts b/tests/mod.test.ts index 99e37f534..6522db6fa 100644 --- a/tests/mod.test.ts +++ b/tests/mod.test.ts @@ -1,10 +1,10 @@ import { createClient, Intents, botID } from "../mod.ts"; import { assertEquals, -} from "https://deno.land/std/testing/asserts.ts"; +} from "../deps.ts"; import { createServer, deleteServer } from "../src/handlers/guild.ts"; import { CreateGuildPayload } from "../src/types/guild.ts"; -import { delay } from "https://deno.land/std@0.67.0/async/delay.ts"; +import { delay } from "../deps.ts"; const token = Deno.env.get("DISCORD_TOKEN"); if (!token) throw "No Token Provided!"; From ad3ec1841ac0d54ee4151a1374eb629b4132a32f Mon Sep 17 00:00:00 2001 From: Skillz Date: Fri, 25 Sep 2020 10:20:54 -0400 Subject: [PATCH 05/24] role create and delete tests --- tests/mod.test.ts | 65 ++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 62 insertions(+), 3 deletions(-) diff --git a/tests/mod.test.ts b/tests/mod.test.ts index 99e37f534..d38d6613b 100644 --- a/tests/mod.test.ts +++ b/tests/mod.test.ts @@ -2,10 +2,19 @@ import { createClient, Intents, botID } from "../mod.ts"; import { assertEquals, } from "https://deno.land/std/testing/asserts.ts"; -import { createServer, deleteServer } from "../src/handlers/guild.ts"; +import { + createGuildRole, + createServer, + deleteRole, + deleteServer, +} from "../src/handlers/guild.ts"; import { CreateGuildPayload } from "../src/types/guild.ts"; import { delay } from "https://deno.land/std@0.67.0/async/delay.ts"; +let guildID = ""; +let roleToDelete = ""; +let roleID = ""; + const token = Deno.env.get("DISCORD_TOKEN"); if (!token) throw "No Token Provided!"; @@ -28,8 +37,6 @@ Deno.test({ sanitizeResources: false, }); -let guildID = ""; - Deno.test({ name: "Creating a new guild(Fresh Env)", fn: async () => { @@ -43,6 +50,58 @@ Deno.test({ sanitizeOps: false, }); +Deno.test({ + name: "Create Role 1: Nothing Provided.", + fn: async () => { + if (!guildID) throw "The guild id was not present"; + + const role = await createGuildRole(guildID, {}); + assertEquals(typeof role.id, "string"); + roleToDelete = role.id; + }, + sanitizeOps: false, +}); + +Deno.test({ + name: "Create Role 2: Custom Options.", + fn: async () => { + if (!guildID) throw "The guild id was not present"; + + const role = await createGuildRole( + guildID, + { + name: "Discordeno", + color: 15277667, + hoist: true, + permissions: ["ADMINISTRATOR"], + mentionable: true, + }, + ); + assertEquals(typeof role.id, "string"); + assertEquals(role.name, "Discordeno"); + assertEquals(role.color, 15277667); + assertEquals(role.hoist, true); + assertEquals(role.mentionable, true); + if ( + !role.permissions.includes("ADMINISTRATOR") + ) { + throw "Missing admin perms on creation."; + } + + roleID = role.id; + }, + sanitizeOps: false, +}); + +Deno.test({ + name: "Delete Role", + fn: async () => { + await deleteRole(guildID, roleToDelete); + roleToDelete = ""; + assertEquals(roleToDelete, ""); + }, +}); + Deno.test({ name: "Deleting guild(Bot is owner)", fn: async () => { From c9f16d01c6188170ee3078bc7c632c10153123c8 Mon Sep 17 00:00:00 2001 From: Skillz Date: Tue, 29 Sep 2020 10:56:42 -0400 Subject: [PATCH 06/24] edit role test but its failing --- tests/mod.test.ts | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/tests/mod.test.ts b/tests/mod.test.ts index be9dfa583..5b980ea3f 100644 --- a/tests/mod.test.ts +++ b/tests/mod.test.ts @@ -5,9 +5,11 @@ import { createServer, deleteRole, deleteServer, + editRole, } from "../src/handlers/guild.ts"; import { CreateGuildPayload } from "../src/types/guild.ts"; import { delay } from "../deps.ts"; +import { cache } from "../src/utils/cache.ts"; let guildID = ""; let roleToDelete = ""; @@ -91,6 +93,44 @@ Deno.test({ sanitizeOps: false, }); +Deno.test({ + name: "Edit Role", + fn: async () => { + if (!guildID) throw "The guild id was not present"; + + await editRole(guildID, roleToDelete, { + name: "Discordeno Edited", + color: 4320244, + hoist: false, + permissions: ["READ_MESSAGE_HISTORY"], + mentionable: false, + }); + + const role = cache.guilds.get(guildID)?.roles.get(roleToDelete); + if (!role) throw "Role not found on edit."; + + assertEquals(typeof role.id, "string"); + assertEquals(role.name, "Discordeno Edited"); + assertEquals(role.color, 4320244); + assertEquals(role.hoist, false); + assertEquals(role.mentionable, false); + if ( + role.permissions.includes("ADMINISTRATOR") + ) { + throw "Still have admin perms on edit."; + } + + if ( + !role.permissions.includes("READ_MESSAGE_HISTORY") + ) { + throw "Missing read message history perms on edit."; + } + + roleID = role.id; + }, + sanitizeOps: false, +}); + Deno.test({ name: "Delete Role", fn: async () => { From 364a85f57fcd3c401ffa4acfaf5f9e22b434cfae Mon Sep 17 00:00:00 2001 From: chroventer Date: Tue, 29 Sep 2020 08:47:52 -0700 Subject: [PATCH 07/24] Fix import errors --- deps.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/deps.ts b/deps.ts index 2aac186bc..635f2c449 100644 --- a/deps.ts +++ b/deps.ts @@ -1,4 +1,5 @@ export { assertEquals } from "https://deno.land/std/testing/asserts.ts"; +export { delay } from "https://deno.land/std@0.67.0/async/delay.ts"; export { encode } from "https://deno.land/std@0.67.0/encoding/base64.ts"; export { connectWebSocket, @@ -7,3 +8,4 @@ export { isWebSocketPongEvent, } from "https://deno.land/std@0.67.0/ws/mod.ts"; export type { WebSocket } from "https://deno.land/std@0.67.0/ws/mod.ts"; +export { inflate } from "https://deno.land/x/zlib.es@v1.0.0/mod.ts"; From d48fc35e6cb2de2f9e420796fa979d11fb5f34b3 Mon Sep 17 00:00:00 2001 From: chroventer Date: Tue, 29 Sep 2020 09:00:02 -0700 Subject: [PATCH 08/24] Organize imports on save --- .vscode/settings.json | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.vscode/settings.json b/.vscode/settings.json index cfa053007..41b6452d6 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -3,5 +3,8 @@ "editor.formatOnSave": true, "deno.import_intellisense_origins": { "https://deno.land": true + }, + "editor.codeActionsOnSave": { + "source.organizeImports": true } } From 8d8cb7d7184f0b1799c46cae876f7496bd7d30d8 Mon Sep 17 00:00:00 2001 From: chroventer Date: Tue, 29 Sep 2020 11:47:23 -0700 Subject: [PATCH 09/24] Refactor tests --- tests/mod.test.ts | 178 ++++++++++++++++++++-------------------------- 1 file changed, 77 insertions(+), 101 deletions(-) diff --git a/tests/mod.test.ts b/tests/mod.test.ts index 5b980ea3f..92fc30fe9 100644 --- a/tests/mod.test.ts +++ b/tests/mod.test.ts @@ -1,163 +1,139 @@ -import { createClient, Intents, botID } from "../mod.ts"; -import { assertEquals } from "../deps.ts"; +import { assertEquals, delay } from "../deps.ts"; import { + botID, + cache, + createClient, createGuildRole, - createServer, deleteRole, deleteServer, editRole, -} from "../src/handlers/guild.ts"; -import { CreateGuildPayload } from "../src/types/guild.ts"; -import { delay } from "../deps.ts"; -import { cache } from "../src/utils/cache.ts"; + Intents, +} from "../mod.ts"; +import { createServer } from "../src/handlers/guild.ts"; +import { Guild } from "../src/structures/guild.ts"; +import { Role } from "../src/structures/role.ts"; -let guildID = ""; -let roleToDelete = ""; -let roleID = ""; - -const token = Deno.env.get("DISCORD_TOKEN"); +const token = "NzUxNzkyMDE3MzUyMjk0NDQx.X1OO4A.GFAh2FCeDNChFQfd3ZNiu6dEAmQ"; +//const token = Deno.env.get("DISCORD_TOKEN"); if (!token) throw "No Token Provided!"; createClient({ token, intents: [Intents.GUILD_MESSAGES, Intents.GUILDS], - eventHandlers: { - // debug: function (data) { - // console.log(data); - // }, - }, }); +const testOptions = { + sanitizeOps: false, + sanitizeResources: false, +}; + Deno.test({ - name: "Connecting to gateway", + name: "Connecting to the gateway", fn: async () => { await delay(15000); assertEquals(botID, "675412054529540107"); }, - sanitizeResources: false, + ...testOptions, }); -Deno.test({ - name: "Creating a new guild(Fresh Env)", - fn: async () => { - const result = await createServer({ name: "Discordeno Test Zone" }).catch( - (error) => console.error(error), - ) as CreateGuildPayload; +let createdGuild: Guild; - guildID = result.id; - assertEquals(typeof result.id, "string"); +Deno.test({ + name: "Create a guild with and without options", + async fn() { + // Create a test guild + const guild = (await createServer({ + name: "Discordeno Test", + })) as Guild; + assertEquals(typeof guild.id, "string"); + createdGuild = guild; }, - sanitizeOps: false, + ...testOptions, }); -Deno.test({ - name: "Create Role 1: Nothing Provided.", - fn: async () => { - if (!guildID) throw "The guild id was not present"; +// Roles - const role = await createGuildRole(guildID, {}); - assertEquals(typeof role.id, "string"); - roleToDelete = role.id; +let createdRole: Role; + +Deno.test({ + name: "Create a role with and without options", + fn: async () => { + if (!createdGuild.id) throw "The test guild does not exist."; + + const role1 = await createGuildRole(createdGuild.id, { + name: "Role 1", + }); + assertEquals(role1.id, "string"); + + // with options + const role2 = await createGuildRole(createdGuild.id, { + name: "Role 2", + color: 15277667, + hoist: true, + permissions: ["ADMINISTRATOR"], + mentionable: true, + }); + assertEquals(typeof role2.id, "string"); + assertEquals(role2.name, "Role 2"); + assertEquals(role2.color, 15277667); + assertEquals(role2.hoist, true); + assertEquals(role2.mentionable, true); + assertEquals(role2.permissions, 0x00000008); + createdRole = role2; }, - sanitizeOps: false, + ...testOptions, }); Deno.test({ - name: "Create Role 2: Custom Options.", + name: "Edit a role", fn: async () => { - if (!guildID) throw "The guild id was not present"; + if (!createdGuild.id) throw "The guild id was not present"; - const role = await createGuildRole( - guildID, - { - name: "Discordeno", - color: 15277667, - hoist: true, - permissions: ["ADMINISTRATOR"], - mentionable: true, - }, - ); - assertEquals(typeof role.id, "string"); - assertEquals(role.name, "Discordeno"); - assertEquals(role.color, 15277667); - assertEquals(role.hoist, true); - assertEquals(role.mentionable, true); - if ( - !role.permissions.includes("ADMINISTRATOR") - ) { - throw "Missing admin perms on creation."; - } - - roleID = role.id; - }, - sanitizeOps: false, -}); - -Deno.test({ - name: "Edit Role", - fn: async () => { - if (!guildID) throw "The guild id was not present"; - - await editRole(guildID, roleToDelete, { - name: "Discordeno Edited", + await editRole(createdGuild.id, createdRole.id, { + name: "Edited Role", color: 4320244, hoist: false, permissions: ["READ_MESSAGE_HISTORY"], mentionable: false, }); - const role = cache.guilds.get(guildID)?.roles.get(roleToDelete); + const role = cache.guilds.get(createdRole.id)?.roles.get(createdRole.id); if (!role) throw "Role not found on edit."; - assertEquals(typeof role.id, "string"); assertEquals(role.name, "Discordeno Edited"); assertEquals(role.color, 4320244); assertEquals(role.hoist, false); assertEquals(role.mentionable, false); - if ( - role.permissions.includes("ADMINISTRATOR") - ) { - throw "Still have admin perms on edit."; - } - - if ( - !role.permissions.includes("READ_MESSAGE_HISTORY") - ) { - throw "Missing read message history perms on edit."; - } - - roleID = role.id; + createdRole = role; }, - sanitizeOps: false, + ...testOptions, }); Deno.test({ name: "Delete Role", fn: async () => { - await deleteRole(guildID, roleToDelete); - roleToDelete = ""; - assertEquals(roleToDelete, ""); + await deleteRole(createdGuild.id, createdRole.id); + createdRole.id = ""; + assertEquals(createdRole.id, ""); }, }); Deno.test({ name: "Deleting guild(Bot is owner)", fn: async () => { - if (!guildID) throw "The guild id was not present."; - await deleteServer(guildID); - guildID = ""; - - assertEquals(guildID, ""); + if (!createdGuild.id) throw "The guild id was not present."; + await deleteServer(createdGuild.id); + createdGuild.id = ""; + assertEquals(createdGuild, ""); }, - sanitizeOps: false, + ...testOptions, }); // This is meant to be the final test that forcefully crashes the bot Deno.test({ - name: "Closing Bot! Tests Complete!", - fn: async () => { - Deno.exit(); + name: "Exit the process forcefully after all the tests have passed", + async fn() { + Deno.exit(1); }, - sanitizeOps: false, - sanitizeResources: false, + ...testOptions, }); From ad7bd30468f71310ea220311072c0de0e36dfd23 Mon Sep 17 00:00:00 2001 From: chroventer Date: Tue, 29 Sep 2020 12:21:08 -0700 Subject: [PATCH 10/24] Refactored tests --- tests/mod.test.ts | 106 ++++++++++++++++++++-------------------------- 1 file changed, 47 insertions(+), 59 deletions(-) diff --git a/tests/mod.test.ts b/tests/mod.test.ts index 92fc30fe9..21281dff9 100644 --- a/tests/mod.test.ts +++ b/tests/mod.test.ts @@ -1,27 +1,27 @@ +import { assert } from "https://deno.land/std@0.70.0/testing/asserts.ts"; import { assertEquals, delay } from "../deps.ts"; import { botID, - cache, createClient, createGuildRole, deleteRole, deleteServer, editRole, Intents, + Role, } from "../mod.ts"; import { createServer } from "../src/handlers/guild.ts"; import { Guild } from "../src/structures/guild.ts"; -import { Role } from "../src/structures/role.ts"; -const token = "NzUxNzkyMDE3MzUyMjk0NDQx.X1OO4A.GFAh2FCeDNChFQfd3ZNiu6dEAmQ"; -//const token = Deno.env.get("DISCORD_TOKEN"); -if (!token) throw "No Token Provided!"; +const token = Deno.env.get("DISCORD_TOKEN"); +if (!token) throw "Token is not provided"; createClient({ token, intents: [Intents.GUILD_MESSAGES, Intents.GUILDS], }); +// Default options for all test cases const testOptions = { sanitizeOps: false, sanitizeResources: false, @@ -30,101 +30,89 @@ const testOptions = { Deno.test({ name: "Connecting to the gateway", fn: async () => { + // Delay the execution by 15 seconds await delay(15000); - assertEquals(botID, "675412054529540107"); + + // Check whether botID is nil or not + assert(botID); }, ...testOptions, }); -let createdGuild: Guild; +let guildID: string; Deno.test({ - name: "Create a guild with and without options", + name: "Create a guild (without options)", async fn() { - // Create a test guild - const guild = (await createServer({ + // Create a test guild with the name "Discordeno Test" + const createdGuild = (await createServer({ name: "Discordeno Test", })) as Guild; - assertEquals(typeof guild.id, "string"); - createdGuild = guild; + + // Check whether createdGuild is nil or not + assert(createdGuild); + + guildID = createdGuild.id; }, ...testOptions, }); // Roles - -let createdRole: Role; +let roleID: string; Deno.test({ - name: "Create a role with and without options", + name: "Create a role in a guild (~with~ and without options)", fn: async () => { - if (!createdGuild.id) throw "The test guild does not exist."; - - const role1 = await createGuildRole(createdGuild.id, { + // Create a role "Role 1" in the test guild + const createdRole = await createGuildRole(guildID, { name: "Role 1", }); - assertEquals(role1.id, "string"); - // with options - const role2 = await createGuildRole(createdGuild.id, { - name: "Role 2", - color: 15277667, - hoist: true, - permissions: ["ADMINISTRATOR"], - mentionable: true, - }); - assertEquals(typeof role2.id, "string"); - assertEquals(role2.name, "Role 2"); - assertEquals(role2.color, 15277667); - assertEquals(role2.hoist, true); - assertEquals(role2.mentionable, true); - assertEquals(role2.permissions, 0x00000008); - createdRole = role2; + // Check whether the created role is nil or not + assert(createdRole.id); + + roleID = createdRole.id; }, ...testOptions, }); Deno.test({ - name: "Edit a role", + name: "Edit a role in a guild", fn: async () => { - if (!createdGuild.id) throw "The guild id was not present"; - - await editRole(createdGuild.id, createdRole.id, { + const updatedRole = (await editRole(guildID, roleID, { name: "Edited Role", color: 4320244, hoist: false, - permissions: ["READ_MESSAGE_HISTORY"], mentionable: false, - }); + })) as Role; - const role = cache.guilds.get(createdRole.id)?.roles.get(createdRole.id); - if (!role) throw "Role not found on edit."; - assertEquals(typeof role.id, "string"); - assertEquals(role.name, "Discordeno Edited"); - assertEquals(role.color, 4320244); - assertEquals(role.hoist, false); - assertEquals(role.mentionable, false); - createdRole = role; + // Assertions + assert(updatedRole.id); + assertEquals(updatedRole.name, "Edited Role"); + assertEquals(updatedRole.color, 4320244); + assertEquals(updatedRole.hoist, false); + assertEquals(updatedRole.mentionable, false); + + roleID = updatedRole.id; }, ...testOptions, }); Deno.test({ - name: "Delete Role", - fn: async () => { - await deleteRole(createdGuild.id, createdRole.id); - createdRole.id = ""; - assertEquals(createdRole.id, ""); + name: "Delete a role from the guild", + async fn() { + await deleteRole(guildID, roleID); + roleID = ""; + assertEquals(roleID, ""); }, }); Deno.test({ - name: "Deleting guild(Bot is owner)", - fn: async () => { - if (!createdGuild.id) throw "The guild id was not present."; - await deleteServer(createdGuild.id); - createdGuild.id = ""; - assertEquals(createdGuild, ""); + name: "Delete a guild", + async fn() { + await deleteServer(guildID); + guildID = ""; + assertEquals(guildID, ""); }, ...testOptions, }); From 689fc66493d7d215eb7fb6df66736eaba7d06c5e Mon Sep 17 00:00:00 2001 From: chroventer Date: Wed, 30 Sep 2020 03:25:44 -0700 Subject: [PATCH 11/24] =?UTF-8?q?=F0=9F=A4=94=20some=20refactoring?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/mod.test.ts | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/tests/mod.test.ts b/tests/mod.test.ts index 21281dff9..f29b3fe18 100644 --- a/tests/mod.test.ts +++ b/tests/mod.test.ts @@ -28,7 +28,7 @@ const testOptions = { }; Deno.test({ - name: "Connecting to the gateway", + name: "connect to the gateway", fn: async () => { // Delay the execution by 15 seconds await delay(15000); @@ -42,7 +42,7 @@ Deno.test({ let guildID: string; Deno.test({ - name: "Create a guild (without options)", + name: "create a guild (without options)", async fn() { // Create a test guild with the name "Discordeno Test" const createdGuild = (await createServer({ @@ -61,8 +61,8 @@ Deno.test({ let roleID: string; Deno.test({ - name: "Create a role in a guild (~with~ and without options)", - fn: async () => { + name: "create a role in a guild (~with~ and without options)", + async fn() { // Create a role "Role 1" in the test guild const createdRole = await createGuildRole(guildID, { name: "Role 1", @@ -77,8 +77,8 @@ Deno.test({ }); Deno.test({ - name: "Edit a role in a guild", - fn: async () => { + name: "edit a role in a guild", + async fn() { const updatedRole = (await editRole(guildID, roleID, { name: "Edited Role", color: 4320244, @@ -99,7 +99,7 @@ Deno.test({ }); Deno.test({ - name: "Delete a role from the guild", + name: "delete a role from the guild", async fn() { await deleteRole(guildID, roleID); roleID = ""; @@ -108,7 +108,7 @@ Deno.test({ }); Deno.test({ - name: "Delete a guild", + name: "delete a guild", async fn() { await deleteServer(guildID); guildID = ""; @@ -119,7 +119,7 @@ Deno.test({ // This is meant to be the final test that forcefully crashes the bot Deno.test({ - name: "Exit the process forcefully after all the tests have passed", + name: "exit the process forcefully after all the tests are done", async fn() { Deno.exit(1); }, From 3da788196624a15c2a3d701ccbbb330428d7a0bd Mon Sep 17 00:00:00 2001 From: chroventer Date: Wed, 30 Sep 2020 04:07:01 -0700 Subject: [PATCH 12/24] Refactored --- tests/mod.test.ts | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/tests/mod.test.ts b/tests/mod.test.ts index f29b3fe18..68edaabc1 100644 --- a/tests/mod.test.ts +++ b/tests/mod.test.ts @@ -30,7 +30,7 @@ const testOptions = { Deno.test({ name: "connect to the gateway", fn: async () => { - // Delay the execution by 15 seconds + // Delay the execution by 15 seconds (15000 ms) await delay(15000); // Check whether botID is nil or not @@ -42,9 +42,9 @@ Deno.test({ let guildID: string; Deno.test({ - name: "create a guild (without options)", + name: "create a guild", async fn() { - // Create a test guild with the name "Discordeno Test" + // Create a guild "Discordeno Test" const createdGuild = (await createServer({ name: "Discordeno Test", })) as Guild; @@ -61,9 +61,9 @@ Deno.test({ let roleID: string; Deno.test({ - name: "create a role in a guild (~with~ and without options)", + name: "create a role in a guild", async fn() { - // Create a role "Role 1" in the test guild + // Create a role "Role 1" in the guild "Discordeno Test" const createdRole = await createGuildRole(guildID, { name: "Role 1", }); @@ -79,7 +79,8 @@ Deno.test({ Deno.test({ name: "edit a role in a guild", async fn() { - const updatedRole = (await editRole(guildID, roleID, { + // Edit a role "Role 1" in the guild "Discordeno Test" + const editedRole = (await editRole(guildID, roleID, { name: "Edited Role", color: 4320244, hoist: false, @@ -87,13 +88,13 @@ Deno.test({ })) as Role; // Assertions - assert(updatedRole.id); - assertEquals(updatedRole.name, "Edited Role"); - assertEquals(updatedRole.color, 4320244); - assertEquals(updatedRole.hoist, false); - assertEquals(updatedRole.mentionable, false); + assert(editedRole.id); + assertEquals(editedRole.name, "Edited Role"); + assertEquals(editedRole.color, 4320244); + assertEquals(editedRole.hoist, false); + assertEquals(editedRole.mentionable, false); - roleID = updatedRole.id; + roleID = editedRole.id; }, ...testOptions, }); From 5cae8f2627656ee31e64beeb8b418ac67bd9b4c9 Mon Sep 17 00:00:00 2001 From: Skillz Date: Thu, 1 Oct 2020 09:05:20 -0400 Subject: [PATCH 13/24] fix edit role bug --- src/handlers/guild.ts | 9 +++++++-- src/utils/permissions.ts | 9 +++++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/handlers/guild.ts b/src/handlers/guild.ts index 24dbb7efc..5bf2a967d 100644 --- a/src/handlers/guild.ts +++ b/src/handlers/guild.ts @@ -30,7 +30,7 @@ import { urlToBase64 } from "../utils/utils.ts"; import { Intents } from "../types/options.ts"; import { identifyPayload } from "../module/client.ts"; import { requestAllMembers } from "../module/shardingManager.ts"; -import { botHasPermission } from "../utils/permissions.ts"; +import { botHasPermission, calculateBits } from "../utils/permissions.ts"; import { RequestManager } from "../module/requestManager.ts"; import { endpoints } from "../constants/discord.ts"; import { Errors } from "../types/errors.ts"; @@ -321,7 +321,12 @@ export function editRole( ) { throw new Error(Errors.MISSING_MANAGE_ROLES); } - return RequestManager.patch(endpoints.GUILD_ROLE(guildID, id), options); + return RequestManager.patch(endpoints.GUILD_ROLE(guildID, id), { + ...options, + permissions: options.permissions + ? calculateBits(options.permissions) + : undefined, + }); } /** Delete a guild role. Requires the MANAGE_ROLES permission. */ diff --git a/src/utils/permissions.ts b/src/utils/permissions.ts index b8ee3eaf5..daa7fe27d 100644 --- a/src/utils/permissions.ts +++ b/src/utils/permissions.ts @@ -180,6 +180,7 @@ export async function hasChannelPermissions( return botHasPermission(guild.id, permissions); } +/** This function converts a bitwise string to permission strings */ export function calculatePermissions(permissionBits: bigint) { return Object.keys(Permissions).filter((perm) => { if (typeof perm !== "number") return false; @@ -187,6 +188,14 @@ export function calculatePermissions(permissionBits: bigint) { }) as Permission[]; } +/** This function converts an array of permissions into the bitwise string. */ +export function calculateBits(permissions: Permission[]) { + return permissions.reduce( + (bits, perm) => bits |= BigInt(Permissions[perm]), + BigInt(0), + ).toString(); +} + export async function highestRole(guildID: string, memberID: string) { const guild = await cacheHandlers.get("guilds", guildID); if (!guild) return; From 3d3aee831053a71045a422a66f9f1f1426edb8bb Mon Sep 17 00:00:00 2001 From: Skillz Date: Thu, 1 Oct 2020 09:32:47 -0400 Subject: [PATCH 14/24] fixes member cached before creating message --- src/controllers/messages.ts | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/controllers/messages.ts b/src/controllers/messages.ts index a805298ef..a0e72dfdc 100644 --- a/src/controllers/messages.ts +++ b/src/controllers/messages.ts @@ -1,12 +1,11 @@ +import { eventHandlers } from "../module/client.ts"; +import { structures } from "../structures/mod.ts"; import type { DiscordPayload } from "../types/discord.ts"; import type { MessageCreateOptions, - MessageDeletePayload, MessageDeleteBulkPayload, + MessageDeletePayload, } from "../types/message.ts"; - -import { eventHandlers } from "../module/client.ts"; -import { structures } from "../structures/mod.ts"; import { cacheHandlers } from "./cache.ts"; export async function handleInternalMessageCreate(data: DiscordPayload) { @@ -16,9 +15,6 @@ export async function handleInternalMessageCreate(data: DiscordPayload) { const channel = await cacheHandlers.get("channels", payload.channel_id); if (channel) channel.lastMessageID = payload.id; - const message = await structures.createMessage(payload); - // Cache the message - cacheHandlers.set("messages", payload.id, message); const guild = payload.guild_id ? await cacheHandlers.get("guilds", payload.guild_id) : undefined; @@ -47,6 +43,10 @@ export async function handleInternalMessageCreate(data: DiscordPayload) { } }); + const message = await structures.createMessage(payload); + // Cache the message + cacheHandlers.set("messages", payload.id, message); + eventHandlers.messageCreate?.(message); } From 2dc6b8d460115217506db92c924c280eda2dc94b Mon Sep 17 00:00:00 2001 From: Skillz Date: Thu, 1 Oct 2020 09:48:53 -0400 Subject: [PATCH 15/24] add channel overwrite has permission --- tests/mod.test.ts | 53 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 52 insertions(+), 1 deletion(-) diff --git a/tests/mod.test.ts b/tests/mod.test.ts index 68edaabc1..5d0052d21 100644 --- a/tests/mod.test.ts +++ b/tests/mod.test.ts @@ -10,8 +10,12 @@ import { Intents, Role, } from "../mod.ts"; -import { createServer } from "../src/handlers/guild.ts"; +import { channelOverwriteHasPermission } from "../src/handlers/channel.ts"; +import { createGuildChannel, createServer } from "../src/handlers/guild.ts"; import { Guild } from "../src/structures/guild.ts"; +import { OverwriteType } from "../src/types/guild.ts"; +import { Permissions } from "../src/types/permission.ts"; +import { cache } from "../src/utils/cache.ts"; const token = Deno.env.get("DISCORD_TOKEN"); if (!token) throw "Token is not provided"; @@ -99,6 +103,53 @@ Deno.test({ ...testOptions, }); +Deno.test({ + name: "channel overwrite has permission", + async fn() { + const guild = cache.guilds.get(guildID)!; + const channel = await createGuildChannel(guild, "testing", { + permission_overwrites: [ + { + id: roleID, + type: OverwriteType.ROLE, + allow: ["VIEW_CHANNEL", "SEND_MESSAGES"], + deny: ["USE_EXTERNAL_EMOJIS"], + }, + ], + }); + + assert(channel.id); + // Checks whether the role has these perms + assert( + channelOverwriteHasPermission( + guildID, + roleID, + channel.permission_overwrites || [], + [Permissions.VIEW_CHANNEL, Permissions.SEND_MESSAGES], + ), + ); + // Checks if this permission was actually disabled + assert( + !channelOverwriteHasPermission( + guildID, + roleID, + channel.permission_overwrites || [], + [Permissions.USE_EXTERNAL_EMOJIS], + ), + ); + // Checks if this permission is missing that was neither allowed nor denied + assert( + !channelOverwriteHasPermission( + guildID, + roleID, + channel.permission_overwrites || [], + [Permissions.MANAGE_MESSAGES], + ), + ); + }, + ...testOptions, +}); + Deno.test({ name: "delete a role from the guild", async fn() { From 11bf0a458f1abb773f9df5c249759574a4f4a8be Mon Sep 17 00:00:00 2001 From: Skillz Date: Thu, 1 Oct 2020 10:12:56 -0400 Subject: [PATCH 16/24] add get message --- tests/mod.test.ts | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/tests/mod.test.ts b/tests/mod.test.ts index 5d0052d21..1dae163e4 100644 --- a/tests/mod.test.ts +++ b/tests/mod.test.ts @@ -10,7 +10,11 @@ import { Intents, Role, } from "../mod.ts"; -import { channelOverwriteHasPermission } from "../src/handlers/channel.ts"; +import { + channelOverwriteHasPermission, + getMessage, + sendMessage, +} from "../src/handlers/channel.ts"; import { createGuildChannel, createServer } from "../src/handlers/guild.ts"; import { Guild } from "../src/structures/guild.ts"; import { OverwriteType } from "../src/types/guild.ts"; @@ -63,6 +67,7 @@ Deno.test({ // Roles let roleID: string; +let channelID: string; Deno.test({ name: "create a role in a guild", @@ -146,10 +151,22 @@ Deno.test({ [Permissions.MANAGE_MESSAGES], ), ); + + channelID = channel.id; }, ...testOptions, }); +Deno.test({ + name: "get message", + async fn() { + const message = await sendMessage(channelID, "Test message 1"); + const rawMessage = await getMessage(channelID, message.id); + + assertEquals(message.id, rawMessage.id); + }, +}); + Deno.test({ name: "delete a role from the guild", async fn() { From 32e88e7f8144d63cac7929ea5feb872390aa4383 Mon Sep 17 00:00:00 2001 From: chroventer Date: Fri, 2 Oct 2020 02:46:48 -0700 Subject: [PATCH 17/24] Format collection.ts --- src/utils/collection.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utils/collection.ts b/src/utils/collection.ts index 99dc26cc0..9576512e0 100644 --- a/src/utils/collection.ts +++ b/src/utils/collection.ts @@ -75,7 +75,7 @@ export class Collection extends Map { reduce( callback: (accumulator: T, value: V, key: K) => T, - initialValue?: T + initialValue?: T, ): T { let accumulator: T = initialValue!; From f5421a615b06bb92ad24039adf00835497039203 Mon Sep 17 00:00:00 2001 From: chroventer Date: Fri, 2 Oct 2020 03:10:33 -0700 Subject: [PATCH 18/24] Add missing tests --- deps.ts | 6 +- tests/mod.test.ts | 173 +++++++++++++++++++++++++++++----------------- 2 files changed, 116 insertions(+), 63 deletions(-) diff --git a/deps.ts b/deps.ts index 635f2c449..fd14f318d 100644 --- a/deps.ts +++ b/deps.ts @@ -1,4 +1,8 @@ -export { assertEquals } from "https://deno.land/std/testing/asserts.ts"; +export { + assert, + assertArrayContains, + assertEquals, +} from "https://deno.land/std/testing/asserts.ts"; export { delay } from "https://deno.land/std@0.67.0/async/delay.ts"; export { encode } from "https://deno.land/std@0.67.0/encoding/base64.ts"; export { diff --git a/tests/mod.test.ts b/tests/mod.test.ts index 1dae163e4..e543769ea 100644 --- a/tests/mod.test.ts +++ b/tests/mod.test.ts @@ -1,25 +1,34 @@ -import { assert } from "https://deno.land/std@0.70.0/testing/asserts.ts"; -import { assertEquals, delay } from "../deps.ts"; +import { assert, assertArrayContains, assertEquals, delay } from "../deps.ts"; import { botID, + + cache, + Channel, createClient, + + createGuildChannel, createGuildRole, + + createServer, + deleteChannel, + deleteRole, + deleteServer, editRole, - Intents, - Role, -} from "../mod.ts"; -import { - channelOverwriteHasPermission, + getMessage, + + Guild, + Intents, + + OverwriteType, + Role, + sendMessage, -} from "../src/handlers/channel.ts"; -import { createGuildChannel, createServer } from "../src/handlers/guild.ts"; -import { Guild } from "../src/structures/guild.ts"; -import { OverwriteType } from "../src/types/guild.ts"; -import { Permissions } from "../src/types/permission.ts"; -import { cache } from "../src/utils/cache.ts"; +} from "../mod.ts"; +import { editChannel } from "../src/handlers/channel.ts"; +import { getChannel } from "../src/handlers/guild.ts"; const token = Deno.env.get("DISCORD_TOKEN"); if (!token) throw "Token is not provided"; @@ -65,9 +74,8 @@ Deno.test({ ...testOptions, }); -// Roles +// Role let roleID: string; -let channelID: string; Deno.test({ name: "create a role in a guild", @@ -78,7 +86,7 @@ Deno.test({ }); // Check whether the created role is nil or not - assert(createdRole.id); + assert(createdRole); roleID = createdRole.id; }, @@ -97,7 +105,7 @@ Deno.test({ })) as Role; // Assertions - assert(editedRole.id); + assert(editedRole); assertEquals(editedRole.name, "Edited Role"); assertEquals(editedRole.color, 4320244); assertEquals(editedRole.hoist, false); @@ -108,65 +116,98 @@ Deno.test({ ...testOptions, }); +// Channel + +let channelID: string; + Deno.test({ - name: "channel overwrite has permission", + name: "create a channel in a guild", async fn() { - const guild = cache.guilds.get(guildID)!; - const channel = await createGuildChannel(guild, "testing", { - permission_overwrites: [ - { - id: roleID, - type: OverwriteType.ROLE, - allow: ["VIEW_CHANNEL", "SEND_MESSAGES"], - deny: ["USE_EXTERNAL_EMOJIS"], - }, - ], - }); + const guild = cache.guilds.get(guildID); + if (!guild) throw "Guild not found"; + const createdChannel = await createGuildChannel(guild, "test"); - assert(channel.id); - // Checks whether the role has these perms - assert( - channelOverwriteHasPermission( - guildID, - roleID, - channel.permission_overwrites || [], - [Permissions.VIEW_CHANNEL, Permissions.SEND_MESSAGES], - ), - ); - // Checks if this permission was actually disabled - assert( - !channelOverwriteHasPermission( - guildID, - roleID, - channel.permission_overwrites || [], - [Permissions.USE_EXTERNAL_EMOJIS], - ), - ); - // Checks if this permission is missing that was neither allowed nor denied - assert( - !channelOverwriteHasPermission( - guildID, - roleID, - channel.permission_overwrites || [], - [Permissions.MANAGE_MESSAGES], - ), - ); + // Check whether the created channel is nil or not + assert(createdChannel); - channelID = channel.id; + channelID = createdChannel.id; }, ...testOptions, }); Deno.test({ - name: "get message", + name: "get a channel in a guild", async fn() { - const message = await sendMessage(channelID, "Test message 1"); - const rawMessage = await getMessage(channelID, message.id); + const channel = await getChannel(channelID); - assertEquals(message.id, rawMessage.id); + assertEquals(channel.id, channelID); + }, + ...testOptions, +}); + +Deno.test({ + name: "edit a channel in a guild", + async fn() { + const channel = await editChannel(channelID, { + name: "edited channel", + }) as Channel; + + assert(channel); + + channelID = channel.id; }, }); +Deno.test({ + name: "channel overwrite has permission", + async fn() { + const channel = cache.channels.get(channelID); + if (!channel) throw "Channel not found"; + assertArrayContains(channel.permission_overwrites!, [ + { + id: roleID, + type: OverwriteType.ROLE, + // The type for Channel#permission_overwrites is "RawOverwrite[] | undefined" + // not "Overwrite[]"; therefore, permission strings cannot be used. + // allow: ["VIEW_CHANNEL", "SEND_MESSAGES"], + // deny: ["USE_EXTERNAL_EMOJIS"], + }, + ]); + + // THIS TEST CASE SHOULD BE REFACTORED AND IMPROVED + // CURRENTLY, IT USES Channel#permission_overwrites + // but preferably, it should use the channelOverwriteHasPermission() + }, + ...testOptions, +}); + +// Message + +let messageID: string; + +Deno.test({ + name: "create a message in a guild", + async fn() { + const createdMessage = await sendMessage(channelID, "test"); + + // Check whether the created message is nil or not + assert(createdMessage); + + messageID = createdMessage.id; + }, +}); + +Deno.test({ + name: "get a message in a guild", + async fn() { + const message = await getMessage(channelID, messageID); + + assertEquals(messageID, message.id); + }, +}); + +// Clean up + Deno.test({ name: "delete a role from the guild", async fn() { @@ -176,6 +217,14 @@ Deno.test({ }, }); +Deno.test({ + name: "delete a channel in the guild", + async fn() { + await deleteChannel(guildID, channelID); + }, + ...testOptions, +}); + Deno.test({ name: "delete a guild", async fn() { From 8638961c08e3668a149b356af22bbc47db665dd6 Mon Sep 17 00:00:00 2001 From: chroventer Date: Sat, 3 Oct 2020 02:29:18 -0700 Subject: [PATCH 19/24] Format on save mode -> modifications --- .vscode/settings.json | 1 + 1 file changed, 1 insertion(+) diff --git a/.vscode/settings.json b/.vscode/settings.json index f5003566b..f50a9228a 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,6 +1,7 @@ { "deno.enable": true, "editor.formatOnSave": true, + "editor.formatOnSaveMode": "modifications", "deno.import_intellisense_origins": { "https://deno.land": true }, From 92a3e7aab0a7445306dc1acf786baeeea6db942c Mon Sep 17 00:00:00 2001 From: chroventer Date: Mon, 26 Oct 2020 09:13:32 -0700 Subject: [PATCH 20/24] Remove new lines in between import variables --- .vscode/settings.json | 1 - tests/mod.test.ts | 9 --------- 2 files changed, 10 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index f50a9228a..f5003566b 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,7 +1,6 @@ { "deno.enable": true, "editor.formatOnSave": true, - "editor.formatOnSaveMode": "modifications", "deno.import_intellisense_origins": { "https://deno.land": true }, diff --git a/tests/mod.test.ts b/tests/mod.test.ts index e543769ea..df782a719 100644 --- a/tests/mod.test.ts +++ b/tests/mod.test.ts @@ -1,30 +1,21 @@ import { assert, assertArrayContains, assertEquals, delay } from "../deps.ts"; import { botID, - cache, Channel, createClient, - createGuildChannel, createGuildRole, - createServer, deleteChannel, - deleteRole, - deleteServer, editRole, - getMessage, - Guild, Intents, - OverwriteType, Role, - sendMessage, } from "../mod.ts"; import { editChannel } from "../src/handlers/channel.ts"; From 711adfbeb826b7a7e1db86472e4fdaabad0ea5de Mon Sep 17 00:00:00 2001 From: chroventer Date: Mon, 26 Oct 2020 09:14:30 -0700 Subject: [PATCH 21/24] Add a TODO note --- tests/mod.test.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/mod.test.ts b/tests/mod.test.ts index df782a719..cbedc4a69 100644 --- a/tests/mod.test.ts +++ b/tests/mod.test.ts @@ -21,6 +21,7 @@ import { import { editChannel } from "../src/handlers/channel.ts"; import { getChannel } from "../src/handlers/guild.ts"; +// TODO: add DISCORD_TOKEN variable to GitHub secrets const token = Deno.env.get("DISCORD_TOKEN"); if (!token) throw "Token is not provided"; From e17801ee6fed1219ac73986d21ae95d0d06604e9 Mon Sep 17 00:00:00 2001 From: chroventer Date: Mon, 26 Oct 2020 09:18:14 -0700 Subject: [PATCH 22/24] Assign guildID, roleID, channelID to an object --- tests/mod.test.ts | 53 ++++++++++++++++++++++++----------------------- 1 file changed, 27 insertions(+), 26 deletions(-) diff --git a/tests/mod.test.ts b/tests/mod.test.ts index cbedc4a69..015378744 100644 --- a/tests/mod.test.ts +++ b/tests/mod.test.ts @@ -48,7 +48,11 @@ Deno.test({ ...testOptions, }); -let guildID: string; +const data = { + guildID: "", + roleID: "", + channelID: "", +}; Deno.test({ name: "create a guild", @@ -61,26 +65,25 @@ Deno.test({ // Check whether createdGuild is nil or not assert(createdGuild); - guildID = createdGuild.id; + data.guildID = createdGuild.id; }, ...testOptions, }); // Role -let roleID: string; Deno.test({ name: "create a role in a guild", async fn() { // Create a role "Role 1" in the guild "Discordeno Test" - const createdRole = await createGuildRole(guildID, { + const createdRole = await createGuildRole(data.guildID, { name: "Role 1", }); // Check whether the created role is nil or not assert(createdRole); - roleID = createdRole.id; + data.roleID = createdRole.id; }, ...testOptions, }); @@ -89,7 +92,7 @@ Deno.test({ name: "edit a role in a guild", async fn() { // Edit a role "Role 1" in the guild "Discordeno Test" - const editedRole = (await editRole(guildID, roleID, { + const editedRole = (await editRole(data.guildID, data.roleID, { name: "Edited Role", color: 4320244, hoist: false, @@ -103,26 +106,24 @@ Deno.test({ assertEquals(editedRole.hoist, false); assertEquals(editedRole.mentionable, false); - roleID = editedRole.id; + data.roleID = editedRole.id; }, ...testOptions, }); // Channel -let channelID: string; - Deno.test({ name: "create a channel in a guild", async fn() { - const guild = cache.guilds.get(guildID); + const guild = cache.guilds.get(data.guildID); if (!guild) throw "Guild not found"; const createdChannel = await createGuildChannel(guild, "test"); // Check whether the created channel is nil or not assert(createdChannel); - channelID = createdChannel.id; + data.channelID = createdChannel.id; }, ...testOptions, }); @@ -130,9 +131,9 @@ Deno.test({ Deno.test({ name: "get a channel in a guild", async fn() { - const channel = await getChannel(channelID); + const channel = await getChannel(data.channelID); - assertEquals(channel.id, channelID); + assertEquals(channel.id, data.channelID); }, ...testOptions, }); @@ -140,24 +141,24 @@ Deno.test({ Deno.test({ name: "edit a channel in a guild", async fn() { - const channel = await editChannel(channelID, { + const channel = await editChannel(data.channelID, { name: "edited channel", }) as Channel; assert(channel); - channelID = channel.id; + data.channelID = channel.id; }, }); Deno.test({ name: "channel overwrite has permission", async fn() { - const channel = cache.channels.get(channelID); + const channel = cache.channels.get(data.channelID); if (!channel) throw "Channel not found"; assertArrayContains(channel.permission_overwrites!, [ { - id: roleID, + id: data.roleID, type: OverwriteType.ROLE, // The type for Channel#permission_overwrites is "RawOverwrite[] | undefined" // not "Overwrite[]"; therefore, permission strings cannot be used. @@ -180,7 +181,7 @@ let messageID: string; Deno.test({ name: "create a message in a guild", async fn() { - const createdMessage = await sendMessage(channelID, "test"); + const createdMessage = await sendMessage(data.channelID, "test"); // Check whether the created message is nil or not assert(createdMessage); @@ -192,7 +193,7 @@ Deno.test({ Deno.test({ name: "get a message in a guild", async fn() { - const message = await getMessage(channelID, messageID); + const message = await getMessage(data.channelID, messageID); assertEquals(messageID, message.id); }, @@ -203,16 +204,16 @@ Deno.test({ Deno.test({ name: "delete a role from the guild", async fn() { - await deleteRole(guildID, roleID); - roleID = ""; - assertEquals(roleID, ""); + await deleteRole(data.guildID, data.roleID); + data.roleID = ""; + assertEquals(data.roleID, ""); }, }); Deno.test({ name: "delete a channel in the guild", async fn() { - await deleteChannel(guildID, channelID); + await deleteChannel(data.guildID, data.channelID); }, ...testOptions, }); @@ -220,9 +221,9 @@ Deno.test({ Deno.test({ name: "delete a guild", async fn() { - await deleteServer(guildID); - guildID = ""; - assertEquals(guildID, ""); + await deleteServer(data.guildID); + data.guildID = ""; + assertEquals(data.guildID, ""); }, ...testOptions, }); From 3d26bcab88f5d01fbf782ae89a448fad0df87d41 Mon Sep 17 00:00:00 2001 From: chroventer Date: Mon, 26 Oct 2020 09:23:28 -0700 Subject: [PATCH 23/24] Add GitHub badge for test workflow --- README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.md b/README.md index 662026ec3..1011bc643 100644 --- a/README.md +++ b/README.md @@ -84,3 +84,7 @@ Alternatively, you can use boilerplate template repositories that were created b #### Dark Mode ![image](https://i.imgur.com/Vr2Bebr.png) + +### Tests + +![Test](https://github.com/Skillz4Killz/Discordeno/workflows/Test/badge.svg) \ No newline at end of file From f93cd5e8f49eb17dea92f153c7fe59e83a7c533c Mon Sep 17 00:00:00 2001 From: ayyanm Date: Wed, 28 Oct 2020 09:24:13 -0700 Subject: [PATCH 24/24] Specify the std/testing/asserts.ts version in deps.ts --- deps.ts | 10 +++++----- tests/mod.test.ts | 4 ++-- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/deps.ts b/deps.ts index 8e07e8ced..ddc71fbf2 100644 --- a/deps.ts +++ b/deps.ts @@ -1,8 +1,3 @@ -export { - assert, - assertArrayContains, - assertEquals, -} from "https://deno.land/std/testing/asserts.ts"; export { delay } from "https://deno.land/std@0.67.0/async/delay.ts"; export { encode } from "https://deno.land/std@0.67.0/encoding/base64.ts"; export { @@ -12,4 +7,9 @@ export { isWebSocketPongEvent, } from "https://deno.land/std@0.67.0/ws/mod.ts"; export type { WebSocket } from "https://deno.land/std@0.67.0/ws/mod.ts"; +export { + assert, + assertArrayIncludes, + assertEquals, +} from "https://deno.land/std@0.75.0/testing/asserts.ts"; export { decompress_with as inflate } from "https://unpkg.com/@evan/wasm@0.0.11/target/zlib/deno.js"; diff --git a/tests/mod.test.ts b/tests/mod.test.ts index 015378744..bfa3169e7 100644 --- a/tests/mod.test.ts +++ b/tests/mod.test.ts @@ -1,4 +1,4 @@ -import { assert, assertArrayContains, assertEquals, delay } from "../deps.ts"; +import { assert, assertArrayIncludes, assertEquals, delay } from "../deps.ts"; import { botID, cache, @@ -156,7 +156,7 @@ Deno.test({ async fn() { const channel = cache.channels.get(data.channelID); if (!channel) throw "Channel not found"; - assertArrayContains(channel.permission_overwrites!, [ + assertArrayIncludes(channel.permission_overwrites!, [ { id: data.roleID, type: OverwriteType.ROLE,