From 1750fc58194b32cd628ad551e3fe9b0e9571a3cf Mon Sep 17 00:00:00 2001 From: Kagami Sascha Rosylight Date: Sat, 11 Feb 2023 20:46:33 +0100 Subject: [PATCH 01/17] Fix moduleNameMapper to not resolve `.wasm.js` to `.js` Fixes #9767 Undici [tries to import `./llhttp/llhttp.wasm.js`](https://github.com/nodejs/undici/blob/e155c6db5cec9bc577d548fa7c7378013631c79c/lib/client.js#L342) which is currently broken by the (hacky) module name mapper. --- packages/backend/jest-resolver.cjs | 14 -------------- packages/backend/jest.config.cjs | 11 +++++++++-- 2 files changed, 9 insertions(+), 16 deletions(-) delete mode 100644 packages/backend/jest-resolver.cjs diff --git a/packages/backend/jest-resolver.cjs b/packages/backend/jest-resolver.cjs deleted file mode 100644 index 4424b800dc80..000000000000 --- a/packages/backend/jest-resolver.cjs +++ /dev/null @@ -1,14 +0,0 @@ -// https://github.com/facebook/jest/issues/12270#issuecomment-1194746382 - -const nativeModule = require('node:module'); - -function resolver(module, options) { - const { basedir, defaultResolver } = options; - try { - return defaultResolver(module, options); - } catch (error) { - return nativeModule.createRequire(basedir).resolve(module); - } -} - -module.exports = resolver; diff --git a/packages/backend/jest.config.cjs b/packages/backend/jest.config.cjs index f0a3dc16c294..2f11f6a3e9a6 100644 --- a/packages/backend/jest.config.cjs +++ b/packages/backend/jest.config.cjs @@ -83,7 +83,14 @@ module.exports = { // A map from regular expressions to module names or to arrays of module names that allow to stub out resources with a single module moduleNameMapper: { - "^@/(.*?).js": "/src/$1.ts", + // Do not resolve .wasm.js to .wasm by the rule below + '^(.+)\\.wasm\\.js$': '$1.wasm.js', + // SWC converts @/foo/bar.js to `../../src/foo/bar.js`, and then this rule + // converts it again to `../../src/foo/bar` which then can be resolved to + // `.ts` files. + // See https://github.com/swc-project/jest/issues/64#issuecomment-1029753225 + // TODO: Use `--allowImportingTsExtensions` on TypeScript 5.0 so that we can + // directly import `.ts` files without this hack. '^(\\.{1,2}/.*)\\.js$': '$1', }, @@ -112,7 +119,7 @@ module.exports = { // resetModules: false, // A path to a custom resolver - resolver: './jest-resolver.cjs', + // resolver: './jest-resolver.cjs', // Automatically restore mock state between every test restoreMocks: true, From 857605c4e9c0e3b28b9d256421d4f953adbaae1f Mon Sep 17 00:00:00 2001 From: Kagami Sascha Rosylight Date: Sat, 11 Feb 2023 20:54:27 +0100 Subject: [PATCH 02/17] longer timeout value --- packages/backend/jest.config.cjs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/packages/backend/jest.config.cjs b/packages/backend/jest.config.cjs index 2f11f6a3e9a6..7d455b43a21a 100644 --- a/packages/backend/jest.config.cjs +++ b/packages/backend/jest.config.cjs @@ -206,4 +206,6 @@ module.exports = { // watchman: true, extensionsToTreatAsEsm: ['.ts'], + + testTimeout: 10000, }; From 7216a61b058834494b89e3a7380b8b52d34371aa Mon Sep 17 00:00:00 2001 From: Kagami Sascha Rosylight Date: Sat, 11 Feb 2023 20:57:27 +0100 Subject: [PATCH 03/17] 30s --- packages/backend/jest.config.cjs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/backend/jest.config.cjs b/packages/backend/jest.config.cjs index 7d455b43a21a..70c7e9078fa1 100644 --- a/packages/backend/jest.config.cjs +++ b/packages/backend/jest.config.cjs @@ -207,5 +207,5 @@ module.exports = { extensionsToTreatAsEsm: ['.ts'], - testTimeout: 10000, + testTimeout: 30000, }; From 1b77bf769b496719d174e28e0b803586be40f5a2 Mon Sep 17 00:00:00 2001 From: Kagami Sascha Rosylight Date: Sat, 11 Feb 2023 21:00:14 +0100 Subject: [PATCH 04/17] 50s --- packages/backend/jest.config.cjs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/backend/jest.config.cjs b/packages/backend/jest.config.cjs index 70c7e9078fa1..7c066ac34f62 100644 --- a/packages/backend/jest.config.cjs +++ b/packages/backend/jest.config.cjs @@ -207,5 +207,5 @@ module.exports = { extensionsToTreatAsEsm: ['.ts'], - testTimeout: 30000, + testTimeout: 50000, }; From f3e0f5796273c0cdcbd901e8c08ae3136b9768f8 Mon Sep 17 00:00:00 2001 From: Kagami Sascha Rosylight Date: Sat, 11 Feb 2023 21:06:49 +0100 Subject: [PATCH 05/17] 60s to be safe --- packages/backend/jest.config.cjs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/backend/jest.config.cjs b/packages/backend/jest.config.cjs index 7c066ac34f62..9a7b9936268a 100644 --- a/packages/backend/jest.config.cjs +++ b/packages/backend/jest.config.cjs @@ -207,5 +207,5 @@ module.exports = { extensionsToTreatAsEsm: ['.ts'], - testTimeout: 50000, + testTimeout: 60000, }; From d80d8550d73ba23f10c18ece993ad00a84c8994d Mon Sep 17 00:00:00 2001 From: Kagami Sascha Rosylight Date: Sat, 11 Feb 2023 21:13:03 +0100 Subject: [PATCH 06/17] Revert "60s to be safe" This reverts commit f3e0f5796273c0cdcbd901e8c08ae3136b9768f8. --- packages/backend/jest.config.cjs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/backend/jest.config.cjs b/packages/backend/jest.config.cjs index 9a7b9936268a..7c066ac34f62 100644 --- a/packages/backend/jest.config.cjs +++ b/packages/backend/jest.config.cjs @@ -207,5 +207,5 @@ module.exports = { extensionsToTreatAsEsm: ['.ts'], - testTimeout: 60000, + testTimeout: 50000, }; From 032dc79210dd2c0ab0a42734a264775040f72b1f Mon Sep 17 00:00:00 2001 From: Kagami Sascha Rosylight Date: Sat, 11 Feb 2023 21:21:18 +0100 Subject: [PATCH 07/17] 2cc98226cac2e0a08332e1ce281ba8917c5b58fc revert? --- packages/backend/test/unit/RoleService.ts | 94 +++++++++++------------ 1 file changed, 47 insertions(+), 47 deletions(-) diff --git a/packages/backend/test/unit/RoleService.ts b/packages/backend/test/unit/RoleService.ts index 221f743d3a1b..59141b8e88a7 100644 --- a/packages/backend/test/unit/RoleService.ts +++ b/packages/backend/test/unit/RoleService.ts @@ -102,33 +102,33 @@ describe('RoleService', () => { }); describe('getUserPolicies', () => { - test('instance default policies', async () => { + test('instance default policies', async () => { const user = await createUser(); metaService.fetch.mockResolvedValue({ policies: { canManageCustomEmojis: false, }, } as any); - + const result = await roleService.getUserPolicies(user.id); - + expect(result.canManageCustomEmojis).toBe(false); }); - - test('instance default policies 2', async () => { + + test('instance default policies 2', async () => { const user = await createUser(); metaService.fetch.mockResolvedValue({ policies: { canManageCustomEmojis: true, }, } as any); - + const result = await roleService.getUserPolicies(user.id); - + expect(result.canManageCustomEmojis).toBe(true); }); - - test('with role', async () => { + + test('with role', async () => { const user = await createUser(); const role = await createRole({ name: 'a', @@ -146,48 +146,48 @@ describe('RoleService', () => { canManageCustomEmojis: false, }, } as any); - + const result = await roleService.getUserPolicies(user.id); - + expect(result.canManageCustomEmojis).toBe(true); }); - test('priority', async () => { - const user = await createUser(); - const role1 = await createRole({ - name: 'role1', - policies: { - driveCapacityMb: { - useDefault: false, - priority: 0, - value: 200, - }, - }, - }); - const role2 = await createRole({ - name: 'role2', - policies: { - driveCapacityMb: { - useDefault: false, - priority: 1, - value: 100, - }, - }, - }); - await assign(role1.id, user.id); - await assign(role2.id, user.id); - metaService.fetch.mockResolvedValue({ - policies: { - driveCapacityMb: 50, - }, - } as any); - - const result = await roleService.getUserPolicies(user.id); - - expect(result.driveCapacityMb).toBe(100); - }); + // test('priority', async () => { + // const user = await createUser(); + // const role1 = await createRole({ + // name: 'role1', + // policies: { + // driveCapacityMb: { + // useDefault: false, + // priority: 0, + // value: 200, + // }, + // }, + // }); + // const role2 = await createRole({ + // name: 'role2', + // policies: { + // driveCapacityMb: { + // useDefault: false, + // priority: 1, + // value: 100, + // }, + // }, + // }); + // await assign(role1.id, user.id); + // await assign(role2.id, user.id); + // metaService.fetch.mockResolvedValue({ + // policies: { + // driveCapacityMb: 50, + // }, + // } as any); - test('conditional role', async () => { + // const result = await roleService.getUserPolicies(user.id); + + // expect(result.driveCapacityMb).toBe(100); + // }); + + test('conditional role', async () => { const user1 = await createUser({ createdAt: new Date(Date.now() - (1000 * 60 * 60 * 24 * 365)), }); @@ -222,7 +222,7 @@ describe('RoleService', () => { canManageCustomEmojis: false, }, } as any); - + const user1Policies = await roleService.getUserPolicies(user1.id); const user2Policies = await roleService.getUserPolicies(user2.id); expect(user1Policies.canManageCustomEmojis).toBe(false); From 6651fd378a6431e8b08e995384f3c030f6324b34 Mon Sep 17 00:00:00 2001 From: Kagami Sascha Rosylight Date: Sat, 11 Feb 2023 21:28:55 +0100 Subject: [PATCH 08/17] revert --- packages/backend/test/unit/RoleService.ts | 94 +++++++++++------------ 1 file changed, 47 insertions(+), 47 deletions(-) diff --git a/packages/backend/test/unit/RoleService.ts b/packages/backend/test/unit/RoleService.ts index 59141b8e88a7..221f743d3a1b 100644 --- a/packages/backend/test/unit/RoleService.ts +++ b/packages/backend/test/unit/RoleService.ts @@ -102,33 +102,33 @@ describe('RoleService', () => { }); describe('getUserPolicies', () => { - test('instance default policies', async () => { + test('instance default policies', async () => { const user = await createUser(); metaService.fetch.mockResolvedValue({ policies: { canManageCustomEmojis: false, }, } as any); - + const result = await roleService.getUserPolicies(user.id); - + expect(result.canManageCustomEmojis).toBe(false); }); - - test('instance default policies 2', async () => { + + test('instance default policies 2', async () => { const user = await createUser(); metaService.fetch.mockResolvedValue({ policies: { canManageCustomEmojis: true, }, } as any); - + const result = await roleService.getUserPolicies(user.id); - + expect(result.canManageCustomEmojis).toBe(true); }); - - test('with role', async () => { + + test('with role', async () => { const user = await createUser(); const role = await createRole({ name: 'a', @@ -146,48 +146,48 @@ describe('RoleService', () => { canManageCustomEmojis: false, }, } as any); - + const result = await roleService.getUserPolicies(user.id); - + expect(result.canManageCustomEmojis).toBe(true); }); - // test('priority', async () => { - // const user = await createUser(); - // const role1 = await createRole({ - // name: 'role1', - // policies: { - // driveCapacityMb: { - // useDefault: false, - // priority: 0, - // value: 200, - // }, - // }, - // }); - // const role2 = await createRole({ - // name: 'role2', - // policies: { - // driveCapacityMb: { - // useDefault: false, - // priority: 1, - // value: 100, - // }, - // }, - // }); - // await assign(role1.id, user.id); - // await assign(role2.id, user.id); - // metaService.fetch.mockResolvedValue({ - // policies: { - // driveCapacityMb: 50, - // }, - // } as any); - - // const result = await roleService.getUserPolicies(user.id); - - // expect(result.driveCapacityMb).toBe(100); - // }); + test('priority', async () => { + const user = await createUser(); + const role1 = await createRole({ + name: 'role1', + policies: { + driveCapacityMb: { + useDefault: false, + priority: 0, + value: 200, + }, + }, + }); + const role2 = await createRole({ + name: 'role2', + policies: { + driveCapacityMb: { + useDefault: false, + priority: 1, + value: 100, + }, + }, + }); + await assign(role1.id, user.id); + await assign(role2.id, user.id); + metaService.fetch.mockResolvedValue({ + policies: { + driveCapacityMb: 50, + }, + } as any); + + const result = await roleService.getUserPolicies(user.id); + + expect(result.driveCapacityMb).toBe(100); + }); - test('conditional role', async () => { + test('conditional role', async () => { const user1 = await createUser({ createdAt: new Date(Date.now() - (1000 * 60 * 60 * 24 * 365)), }); @@ -222,7 +222,7 @@ describe('RoleService', () => { canManageCustomEmojis: false, }, } as any); - + const user1Policies = await roleService.getUserPolicies(user1.id); const user2Policies = await roleService.getUserPolicies(user2.id); expect(user1Policies.canManageCustomEmojis).toBe(false); From 3fe59a7854c51fc046f73fe29bd304144a33fefd Mon Sep 17 00:00:00 2001 From: Kagami Sascha Rosylight Date: Sat, 11 Feb 2023 21:29:16 +0100 Subject: [PATCH 09/17] remove timeout --- packages/backend/jest.config.cjs | 2 -- 1 file changed, 2 deletions(-) diff --git a/packages/backend/jest.config.cjs b/packages/backend/jest.config.cjs index 7c066ac34f62..2f11f6a3e9a6 100644 --- a/packages/backend/jest.config.cjs +++ b/packages/backend/jest.config.cjs @@ -206,6 +206,4 @@ module.exports = { // watchman: true, extensionsToTreatAsEsm: ['.ts'], - - testTimeout: 50000, }; From 29214bdff80e15998f34171bf409d454a3d10129 Mon Sep 17 00:00:00 2001 From: Kagami Sascha Rosylight Date: Sat, 11 Feb 2023 21:42:05 +0100 Subject: [PATCH 10/17] detectOpenHandles --- packages/backend/package.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/backend/package.json b/packages/backend/package.json index 0351e02e0ffe..d0544088656a 100644 --- a/packages/backend/package.json +++ b/packages/backend/package.json @@ -12,8 +12,8 @@ "build": "tsc -p tsconfig.json || echo done. && tsc-alias -p tsconfig.json", "watch": "node watch.mjs", "lint": "tsc --noEmit && eslint --quiet \"src/**/*.ts\"", - "jest": "cross-env NODE_ENV=test node --experimental-vm-modules --experimental-import-meta-resolve node_modules/jest/bin/jest.js --forceExit --runInBand", - "jest-and-coverage": "cross-env NODE_ENV=test node --experimental-vm-modules --experimental-import-meta-resolve node_modules/jest/bin/jest.js --coverage --forceExit --runInBand", + "jest": "cross-env NODE_ENV=test node --experimental-vm-modules --experimental-import-meta-resolve node_modules/jest/bin/jest.js --forceExit --runInBand --detectOpenHandles", + "jest-and-coverage": "cross-env NODE_ENV=test node --experimental-vm-modules --experimental-import-meta-resolve node_modules/jest/bin/jest.js --coverage --forceExit --runInBand --detectOpenHandles", "jest-clear": "cross-env NODE_ENV=test node --experimental-vm-modules --experimental-import-meta-resolve node_modules/jest/bin/jest.js --clearCache", "test": "pnpm jest", "test-and-coverage": "pnpm jest-and-coverage" From b0919af6a80e52b7d9a979de18fb7f2a776e6213 Mon Sep 17 00:00:00 2001 From: Kagami Sascha Rosylight Date: Sat, 11 Feb 2023 21:49:22 +0100 Subject: [PATCH 11/17] Really solved? From ad0260037c2b74eb8e0380fa6911f411f0352443 Mon Sep 17 00:00:00 2001 From: Kagami Sascha Rosylight Date: Sat, 11 Feb 2023 21:53:15 +0100 Subject: [PATCH 12/17] Revert "detectOpenHandles" This reverts commit 29214bdff80e15998f34171bf409d454a3d10129. --- packages/backend/package.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/backend/package.json b/packages/backend/package.json index d0544088656a..0351e02e0ffe 100644 --- a/packages/backend/package.json +++ b/packages/backend/package.json @@ -12,8 +12,8 @@ "build": "tsc -p tsconfig.json || echo done. && tsc-alias -p tsconfig.json", "watch": "node watch.mjs", "lint": "tsc --noEmit && eslint --quiet \"src/**/*.ts\"", - "jest": "cross-env NODE_ENV=test node --experimental-vm-modules --experimental-import-meta-resolve node_modules/jest/bin/jest.js --forceExit --runInBand --detectOpenHandles", - "jest-and-coverage": "cross-env NODE_ENV=test node --experimental-vm-modules --experimental-import-meta-resolve node_modules/jest/bin/jest.js --coverage --forceExit --runInBand --detectOpenHandles", + "jest": "cross-env NODE_ENV=test node --experimental-vm-modules --experimental-import-meta-resolve node_modules/jest/bin/jest.js --forceExit --runInBand", + "jest-and-coverage": "cross-env NODE_ENV=test node --experimental-vm-modules --experimental-import-meta-resolve node_modules/jest/bin/jest.js --coverage --forceExit --runInBand", "jest-clear": "cross-env NODE_ENV=test node --experimental-vm-modules --experimental-import-meta-resolve node_modules/jest/bin/jest.js --clearCache", "test": "pnpm jest", "test-and-coverage": "pnpm jest-and-coverage" From fcf8c6806b339b7c21f53137f9939d00020904aa Mon Sep 17 00:00:00 2001 From: Kagami Sascha Rosylight Date: Sun, 12 Feb 2023 07:40:39 +0100 Subject: [PATCH 13/17] Add `coveragePathIgnorePatterns` --- packages/backend/jest.config.cjs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/backend/jest.config.cjs b/packages/backend/jest.config.cjs index 2f11f6a3e9a6..cd2023f2e5e7 100644 --- a/packages/backend/jest.config.cjs +++ b/packages/backend/jest.config.cjs @@ -26,9 +26,9 @@ module.exports = { coverageDirectory: "coverage", // An array of regexp pattern strings used to skip coverage collection - // coveragePathIgnorePatterns: [ - // "\\\\node_modules\\\\" - // ], + coveragePathIgnorePatterns: [ + "/node_modules/", + ], // Indicates which provider should be used to instrument code for coverage coverageProvider: "v8", From 2023e2ee04ee3cf3967418988a24e1af563510ef Mon Sep 17 00:00:00 2001 From: Kagami Sascha Rosylight Date: Sun, 12 Feb 2023 07:55:24 +0100 Subject: [PATCH 14/17] Revert "Add `coveragePathIgnorePatterns`" This reverts commit fcf8c6806b339b7c21f53137f9939d00020904aa. --- packages/backend/jest.config.cjs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/backend/jest.config.cjs b/packages/backend/jest.config.cjs index cd2023f2e5e7..2f11f6a3e9a6 100644 --- a/packages/backend/jest.config.cjs +++ b/packages/backend/jest.config.cjs @@ -26,9 +26,9 @@ module.exports = { coverageDirectory: "coverage", // An array of regexp pattern strings used to skip coverage collection - coveragePathIgnorePatterns: [ - "/node_modules/", - ], + // coveragePathIgnorePatterns: [ + // "\\\\node_modules\\\\" + // ], // Indicates which provider should be used to instrument code for coverage coverageProvider: "v8", From 2e576272f3d15388b126ceeae8a3988afc95bd77 Mon Sep 17 00:00:00 2001 From: Kagami Sascha Rosylight Date: Sun, 12 Feb 2023 08:58:48 +0100 Subject: [PATCH 15/17] Import jsonld dynamically --- packages/backend/src/core/activitypub/LdSignatureService.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/backend/src/core/activitypub/LdSignatureService.ts b/packages/backend/src/core/activitypub/LdSignatureService.ts index a29e1be5642e..2a5645a71324 100644 --- a/packages/backend/src/core/activitypub/LdSignatureService.ts +++ b/packages/backend/src/core/activitypub/LdSignatureService.ts @@ -85,7 +85,8 @@ class LdSignature { @bindThis public async normalize(data: any) { const customLoader = this.getLoader(); - return await jsonld.normalize(data, { + // XXX: Importing jsonld dynamically since Jest frequently fails to import it statically + return (await import("jsonld")).default.normalize(data, { documentLoader: customLoader, }); } From 37c2f2eee87f773154159d378db96c3f80380413 Mon Sep 17 00:00:00 2001 From: Kagami Sascha Rosylight Date: Sun, 12 Feb 2023 09:00:06 +0100 Subject: [PATCH 16/17] remove import --- packages/backend/src/core/activitypub/LdSignatureService.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/backend/src/core/activitypub/LdSignatureService.ts b/packages/backend/src/core/activitypub/LdSignatureService.ts index 2a5645a71324..a33bd7b8288c 100644 --- a/packages/backend/src/core/activitypub/LdSignatureService.ts +++ b/packages/backend/src/core/activitypub/LdSignatureService.ts @@ -1,6 +1,5 @@ import * as crypto from 'node:crypto'; import { Inject, Injectable } from '@nestjs/common'; -import jsonld from 'jsonld'; import { HttpRequestService } from '@/core/HttpRequestService.js'; import { bindThis } from '@/decorators.js'; import { CONTEXTS } from './misc/contexts.js'; From bff0bee5c209cfa07bdcb6796948b417a95f819c Mon Sep 17 00:00:00 2001 From: syuilo Date: Sun, 12 Feb 2023 17:18:11 +0900 Subject: [PATCH 17/17] add comment --- packages/backend/src/core/activitypub/LdSignatureService.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/backend/src/core/activitypub/LdSignatureService.ts b/packages/backend/src/core/activitypub/LdSignatureService.ts index a33bd7b8288c..618ae48b1b54 100644 --- a/packages/backend/src/core/activitypub/LdSignatureService.ts +++ b/packages/backend/src/core/activitypub/LdSignatureService.ts @@ -85,7 +85,8 @@ class LdSignature { public async normalize(data: any) { const customLoader = this.getLoader(); // XXX: Importing jsonld dynamically since Jest frequently fails to import it statically - return (await import("jsonld")).default.normalize(data, { + // https://github.com/misskey-dev/misskey/pull/9894#discussion_r1103753595 + return (await import('jsonld')).default.normalize(data, { documentLoader: customLoader, }); }