From ebc7d02f9a56313048da59df5dad9fda9ad9c4b7 Mon Sep 17 00:00:00 2001 From: Ethan Lane Date: Wed, 24 Jul 2024 18:18:00 +0100 Subject: [PATCH 1/8] Create unit test plan --- package.json | 2 +- tests/registry.test.ts | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) create mode 100644 tests/registry.test.ts diff --git a/package.json b/package.json index f37eae6..e0fdd55 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,7 @@ "clean": "rm -rf node_modules/ dist/", "build": "tsc", "start": "node ./dist/bot.js", - "test": "jest --passWithNoTests", + "test": "jest", "lint": "eslint .", "lint:fix": "eslint . --fix", "db:up": "typeorm migration:run -d dist/database/dataSources/appDataSource.js", diff --git a/tests/registry.test.ts b/tests/registry.test.ts new file mode 100644 index 0000000..3c1aa09 --- /dev/null +++ b/tests/registry.test.ts @@ -0,0 +1,7 @@ +describe("RegisterCommands", () => { + test.todo("EXPECT every command in the commands folder to be registered"); +}); + +describe("RegisterButtonEvents", () => { + test.todo("EXEPCT every button event in the button events folder to be registered"); +}); From 52be4481d41339f7db50309093d6dbcd1fa9e820 Mon Sep 17 00:00:00 2001 From: Ethan Lane Date: Thu, 25 Jul 2024 18:41:44 +0100 Subject: [PATCH 2/8] Add tests to check if all events are registered --- tests/registry.test.ts | 63 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 61 insertions(+), 2 deletions(-) diff --git a/tests/registry.test.ts b/tests/registry.test.ts index 3c1aa09..38d012c 100644 --- a/tests/registry.test.ts +++ b/tests/registry.test.ts @@ -1,7 +1,66 @@ +import {CoreClient} from "../src/client/client"; +import Registry from "../src/registry"; +import fs from "fs"; +import path from "path"; + describe("RegisterCommands", () => { - test.todo("EXPECT every command in the commands folder to be registered"); + test("EXPECT every command in the commands folder to be registered", () => { + const registeredCommands: string[] = []; + + CoreClient.RegisterCommand = jest.fn().mockImplementation((name: string) => { + registeredCommands.push(name); + }); + + Registry.RegisterCommands(); + + const commandFiles = getFilesInDirectory(path.join(process.cwd(), "src", "commands")) + .filter(x => x.endsWith(".ts")); + + for (let file of commandFiles) { + expect(registeredCommands).toContain(file.split("/").pop()!.split(".")[0]); + } + + expect(commandFiles.length).toBe(registeredCommands.length); + }); }); describe("RegisterButtonEvents", () => { - test.todo("EXEPCT every button event in the button events folder to be registered"); + test("EXEPCT every button event in the button events folder to be registered", () => { + const registeredButtonEvents: string[] = []; + + CoreClient.RegisterButtonEvent = jest.fn().mockImplementation((name: string) => { + registeredButtonEvents.push(name); + }); + + Registry.RegisterButtonEvents(); + + const eventFiles = getFilesInDirectory(path.join(process.cwd(), "src", "buttonEvents")) + .filter(x => x.endsWith(".ts")); + + for (let file of eventFiles) { + expect(registeredButtonEvents).toContain(file.split("/").pop()!.split(".")[0].toLowerCase()); + } + + expect(eventFiles.length).toBe(registeredButtonEvents.length); + }); }); + +function getFilesInDirectory(dir: string): string[] { + let results: string[] = []; + const list = fs.readdirSync(dir); + + list.forEach(file => { + file = path.join(dir, file); + const stat = fs.statSync(file); + + if (stat && stat.isDirectory()) { + /* recurse into a subdirectory */ + results = results.concat(getFilesInDirectory(file)); + } else { + /* is a file */ + results.push(file); + } + }); + + return results; +} From 49f4f4f80406507bbb16710020ad145e4973bf6a Mon Sep 17 00:00:00 2001 From: Ethan Lane Date: Thu, 25 Jul 2024 18:42:57 +0100 Subject: [PATCH 3/8] Fix lint --- tests/registry.test.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/registry.test.ts b/tests/registry.test.ts index 38d012c..71d80db 100644 --- a/tests/registry.test.ts +++ b/tests/registry.test.ts @@ -16,7 +16,7 @@ describe("RegisterCommands", () => { const commandFiles = getFilesInDirectory(path.join(process.cwd(), "src", "commands")) .filter(x => x.endsWith(".ts")); - for (let file of commandFiles) { + for (const file of commandFiles) { expect(registeredCommands).toContain(file.split("/").pop()!.split(".")[0]); } @@ -37,7 +37,7 @@ describe("RegisterButtonEvents", () => { const eventFiles = getFilesInDirectory(path.join(process.cwd(), "src", "buttonEvents")) .filter(x => x.endsWith(".ts")); - for (let file of eventFiles) { + for (const file of eventFiles) { expect(registeredButtonEvents).toContain(file.split("/").pop()!.split(".")[0].toLowerCase()); } From fef80709eefc87056ca1bc9f0704ed05c18bbb80 Mon Sep 17 00:00:00 2001 From: Ethan Lane Date: Fri, 26 Jul 2024 18:28:32 +0100 Subject: [PATCH 4/8] Resolve ws to ^8.17.1 (#319) - Upgrade ws to ^8.17.1 to fix vulnerability #269 Reviewed-on: https://git.vylpes.xyz/External/card-drop/pulls/319 Reviewed-by: VylpesTester Co-authored-by: Ethan Lane Co-committed-by: Ethan Lane --- package.json | 8 ++++---- yarn.lock | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/package.json b/package.json index f37eae6..102b607 100644 --- a/package.json +++ b/package.json @@ -15,11 +15,11 @@ "db:create": "typeorm migration:create ./src/database/migrations/app/new", "release": "np --no-publish" }, - "repository": "https://gitea.vylpes.xyz/External/card-drop.git", + "repository": "https://git.vylpes.xyz/External/card-drop.git", "author": "Ethan Lane ", "license": "MIT", "bugs": { - "url": "https//gitea.vylpes.xyz/External/card-drop/issues", + "url": "https//git.vylpes.xyz/External/card-drop/issues", "email": "helpdesk@vylpes.com" }, "homepage": "https://gitea.vylpes.xyz/External/card-drop", @@ -49,8 +49,8 @@ "winston-daily-rotate-file": "^5.0.0", "winston-discord-transport": "^1.3.0" }, - "overrides": { - "undici": "^5.28.3" + "resolutions": { + "**/ws": "^8.17.1" }, "devDependencies": { "@types/node": "^20.0.0", diff --git a/yarn.lock b/yarn.lock index 48fe278..b953c94 100644 --- a/yarn.lock +++ b/yarn.lock @@ -6838,10 +6838,10 @@ write-file-atomic@^4.0.2: imurmurhash "^0.1.4" signal-exit "^3.0.7" -ws@^8.16.0: - version "8.17.0" - resolved "https://registry.yarnpkg.com/ws/-/ws-8.17.0.tgz#d145d18eca2ed25aaf791a183903f7be5e295fea" - integrity sha512-uJq6108EgZMAl20KagGkzCKfMEjxmKvZHG7Tlq0Z6nOky7YF7aq4mOx6xK8TJ/i1LeK4Qus7INktacctDgY8Ow== +ws@^8.16.0, ws@^8.17.1: + version "8.18.0" + resolved "https://registry.yarnpkg.com/ws/-/ws-8.18.0.tgz#0d7505a6eafe2b0e712d232b42279f53bc289bbc" + integrity sha512-8VbfWfHLbbwu3+N6OKsOMpBdT4kXPDDB9cJk2bJ6mh9ucxdlnNvH1e+roYkKmN9Nxw2yjz7VzeO9oOz2zJ04Pw== xdg-basedir@^5.0.1, xdg-basedir@^5.1.0: version "5.1.0" From 097b7284e6b910352313ff0c6d581792c694e01c Mon Sep 17 00:00:00 2001 From: Ethan Lane Date: Fri, 26 Jul 2024 18:29:57 +0100 Subject: [PATCH 5/8] Fix list not being sorted (#320) - Fix list on `/allbalance` command not being sorted by currency #260 Reviewed-on: https://git.vylpes.xyz/External/card-drop/pulls/320 Reviewed-by: VylpesTester Co-authored-by: Ethan Lane Co-committed-by: Ethan Lane --- src/commands/allbalance.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/commands/allbalance.ts b/src/commands/allbalance.ts index 7db050a..0874e54 100644 --- a/src/commands/allbalance.ts +++ b/src/commands/allbalance.ts @@ -16,7 +16,8 @@ export default class AllBalance extends Command { public override async execute(interaction: CommandInteraction) { const users = await User.FetchAll(User); - const filteredUsers = users.filter(x => x.Currency > 0); + const filteredUsers = users.filter(x => x.Currency > 0) + .sort((a, b) => b.Currency - a.Currency); const embed = new EmbedBuilder() .setColor(EmbedColours.Ok) From a7b9a3a46373693352d6bc395826f409fe80e7c0 Mon Sep 17 00:00:00 2001 From: Ethan Lane Date: Wed, 24 Jul 2024 18:18:00 +0100 Subject: [PATCH 6/8] Create unit test plan --- package.json | 2 +- tests/registry.test.ts | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) create mode 100644 tests/registry.test.ts diff --git a/package.json b/package.json index 102b607..3488a56 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,7 @@ "clean": "rm -rf node_modules/ dist/", "build": "tsc", "start": "node ./dist/bot.js", - "test": "jest --passWithNoTests", + "test": "jest", "lint": "eslint .", "lint:fix": "eslint . --fix", "db:up": "typeorm migration:run -d dist/database/dataSources/appDataSource.js", diff --git a/tests/registry.test.ts b/tests/registry.test.ts new file mode 100644 index 0000000..3c1aa09 --- /dev/null +++ b/tests/registry.test.ts @@ -0,0 +1,7 @@ +describe("RegisterCommands", () => { + test.todo("EXPECT every command in the commands folder to be registered"); +}); + +describe("RegisterButtonEvents", () => { + test.todo("EXEPCT every button event in the button events folder to be registered"); +}); From a718011640601d4036d785c5382441d03cfcffff Mon Sep 17 00:00:00 2001 From: Ethan Lane Date: Thu, 25 Jul 2024 18:41:44 +0100 Subject: [PATCH 7/8] Add tests to check if all events are registered --- tests/registry.test.ts | 63 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 61 insertions(+), 2 deletions(-) diff --git a/tests/registry.test.ts b/tests/registry.test.ts index 3c1aa09..38d012c 100644 --- a/tests/registry.test.ts +++ b/tests/registry.test.ts @@ -1,7 +1,66 @@ +import {CoreClient} from "../src/client/client"; +import Registry from "../src/registry"; +import fs from "fs"; +import path from "path"; + describe("RegisterCommands", () => { - test.todo("EXPECT every command in the commands folder to be registered"); + test("EXPECT every command in the commands folder to be registered", () => { + const registeredCommands: string[] = []; + + CoreClient.RegisterCommand = jest.fn().mockImplementation((name: string) => { + registeredCommands.push(name); + }); + + Registry.RegisterCommands(); + + const commandFiles = getFilesInDirectory(path.join(process.cwd(), "src", "commands")) + .filter(x => x.endsWith(".ts")); + + for (let file of commandFiles) { + expect(registeredCommands).toContain(file.split("/").pop()!.split(".")[0]); + } + + expect(commandFiles.length).toBe(registeredCommands.length); + }); }); describe("RegisterButtonEvents", () => { - test.todo("EXEPCT every button event in the button events folder to be registered"); + test("EXEPCT every button event in the button events folder to be registered", () => { + const registeredButtonEvents: string[] = []; + + CoreClient.RegisterButtonEvent = jest.fn().mockImplementation((name: string) => { + registeredButtonEvents.push(name); + }); + + Registry.RegisterButtonEvents(); + + const eventFiles = getFilesInDirectory(path.join(process.cwd(), "src", "buttonEvents")) + .filter(x => x.endsWith(".ts")); + + for (let file of eventFiles) { + expect(registeredButtonEvents).toContain(file.split("/").pop()!.split(".")[0].toLowerCase()); + } + + expect(eventFiles.length).toBe(registeredButtonEvents.length); + }); }); + +function getFilesInDirectory(dir: string): string[] { + let results: string[] = []; + const list = fs.readdirSync(dir); + + list.forEach(file => { + file = path.join(dir, file); + const stat = fs.statSync(file); + + if (stat && stat.isDirectory()) { + /* recurse into a subdirectory */ + results = results.concat(getFilesInDirectory(file)); + } else { + /* is a file */ + results.push(file); + } + }); + + return results; +} From 425266eed9f2f28d59c22b6bd5d72fe09a5dc84a Mon Sep 17 00:00:00 2001 From: Ethan Lane Date: Thu, 25 Jul 2024 18:42:57 +0100 Subject: [PATCH 8/8] Fix lint --- tests/registry.test.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/registry.test.ts b/tests/registry.test.ts index 38d012c..71d80db 100644 --- a/tests/registry.test.ts +++ b/tests/registry.test.ts @@ -16,7 +16,7 @@ describe("RegisterCommands", () => { const commandFiles = getFilesInDirectory(path.join(process.cwd(), "src", "commands")) .filter(x => x.endsWith(".ts")); - for (let file of commandFiles) { + for (const file of commandFiles) { expect(registeredCommands).toContain(file.split("/").pop()!.split(".")[0]); } @@ -37,7 +37,7 @@ describe("RegisterButtonEvents", () => { const eventFiles = getFilesInDirectory(path.join(process.cwd(), "src", "buttonEvents")) .filter(x => x.endsWith(".ts")); - for (let file of eventFiles) { + for (const file of eventFiles) { expect(registeredButtonEvents).toContain(file.split("/").pop()!.split(".")[0].toLowerCase()); }