From cf217784d10669c61bf172bc5ca2db9446519499 Mon Sep 17 00:00:00 2001 From: Lauri Ojansivu Date: Mon, 13 Jul 2026 08:18:59 +0300 Subject: [PATCH] Fix Mocha boot crash (test __dirname) and load the new avatar/import-export server files MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Two build/load fixes surfaced by running the full test suite: - server/imports.js: import the new server-only files so they actually load. In mainModule mode a file not imported here never loads, so Accounts.onLogin avatar localization (server/avatarLocalizationOnLogin.js) and the imported-user reconciliation Meteor methods (server/importedUserReconciliation.js) would silently never register. - Test __dirname collision: 'meteor test' crashed at boot with 'Identifier __dirname has already been declared'. The client test index (client/lib/tests/index.js) loads boardTriggersClass.tests.js, which referenced the bare __dirname global; under meteor test that makes Meteor inject a colliding with its CommonJS wrapper's, aborting the whole server bundle (the same failure already fixed once in dependencies.openapi.tests.js — this was the next file to trip it). Replace __dirname with a process.cwd()/PWD walk-up in boardTriggersClass.tests.js, and defensively in the node-run tests/rtl.test.js, tests/stickers.test.js and tests/stickers.packs.test.js too. Thanks to xet7. --- client/lib/tests/boardTriggersClass.tests.js | 20 ++++++++++++++++- server/imports.js | 9 ++++++++ tests/rtl.test.js | 20 ++++++++++++++++- tests/stickers.packs.test.js | 23 ++++++++++++++++---- tests/stickers.test.js | 23 ++++++++++++++++---- 5 files changed, 85 insertions(+), 10 deletions(-) diff --git a/client/lib/tests/boardTriggersClass.tests.js b/client/lib/tests/boardTriggersClass.tests.js index 401d861be5..86a4a2148f 100644 --- a/client/lib/tests/boardTriggersClass.tests.js +++ b/client/lib/tests/boardTriggersClass.tests.js @@ -17,7 +17,25 @@ const fs = require('fs'); const path = require('path'); const { expect } = require('chai'); -const repoRoot = path.resolve(__dirname, '..', '..', '..'); +// Find the repo root by walking up from process.cwd()/PWD, NOT from the bare `__dirname` +// global: under `meteor test` referencing `__dirname` makes Meteor inject a +// `const __dirname` that collides with the one its CommonJS module wrapper already +// provides, crashing the whole server bundle at boot with "Identifier '__dirname' has +// already been declared" (same fix as server/lib/tests/dependencies.openapi.tests.js). +function findRepoRoot(marker) { + const seeds = [process.cwd(), process.env.PWD, process.env.OLDPWD].filter(Boolean); + for (const seed of seeds) { + let dir = seed; + for (let i = 0; i < 12; i += 1) { + try { fs.accessSync(path.join(dir, marker)); return dir; } catch (e) { /* walk up */ } + const parent = path.dirname(dir); + if (parent === dir) break; + dir = parent; + } + } + return process.cwd(); +} +const repoRoot = findRepoRoot(path.join('client', 'components', 'rules', 'triggers', 'boardTriggers.jade')); const jadePath = path.join( repoRoot, 'client', diff --git a/server/imports.js b/server/imports.js index 86976451b9..ea05e27b9f 100644 --- a/server/imports.js +++ b/server/imports.js @@ -291,6 +291,15 @@ import '/server/permissions/unsavedEdits'; import '/server/permissions/userPositionHistory'; import '/server/permissions/users'; +// ---------------------------------------------------------------------------- +// External-avatar localization + imported-user reconciliation +// ---------------------------------------------------------------------------- +// Register the Accounts.onLogin avatar-localization trigger and the imported-user +// reconciliation Meteor methods. In mainModule mode a file that is not imported here is +// never loaded, so its hooks/methods would silently never register. +import '/server/avatarLocalizationOnLogin'; +import '/server/importedUserReconciliation'; + // ---------------------------------------------------------------------------- // 17. Sandstorm integration // ---------------------------------------------------------------------------- diff --git a/tests/rtl.test.js b/tests/rtl.test.js index 0955ab09b7..0861354cf6 100644 --- a/tests/rtl.test.js +++ b/tests/rtl.test.js @@ -25,7 +25,25 @@ const assert = require('assert'); const fs = require('fs'); const path = require('path'); -const repoRoot = path.resolve(__dirname, '..'); +// Find the repo root by walking up from process.cwd()/PWD, NOT from the bare `__dirname` +// global: this file is also loaded by `meteor test`, where referencing `__dirname` makes +// Meteor inject a `const __dirname` that collides with the one its CommonJS module wrapper +// already provides, crashing the whole server bundle at boot with "Identifier '__dirname' +// has already been declared" (same fix as server/lib/tests/dependencies.openapi.tests.js). +function findRepoRoot(marker) { + const seeds = [process.cwd(), process.env.PWD, process.env.OLDPWD].filter(Boolean); + for (const seed of seeds) { + let dir = seed; + for (let i = 0; i < 12; i += 1) { + try { fs.accessSync(path.join(dir, marker)); return dir; } catch (e) { /* walk up */ } + const parent = path.dirname(dir); + if (parent === dir) break; + dir = parent; + } + } + return process.cwd(); +} +const repoRoot = findRepoRoot('imports/i18n/languages.js'); const read = rel => fs.readFileSync(path.join(repoRoot, rel), 'utf8'); // --- Parse the rtl flags straight out of languages.js ----------------------- diff --git a/tests/stickers.packs.test.js b/tests/stickers.packs.test.js index 8149c93d46..d6331c54a2 100644 --- a/tests/stickers.packs.test.js +++ b/tests/stickers.packs.test.js @@ -11,10 +11,25 @@ const assert = require('assert'); const fs = require('fs'); const path = require('path'); -const src = fs.readFileSync( - path.join(__dirname, '..', 'models', 'metadata', 'stickers.js'), - 'utf8', -); +// Locate the source via a process.cwd()/PWD walk-up, NOT the bare `__dirname` global: +// under `meteor test` referencing `__dirname` makes Meteor inject a `const __dirname` +// that collides with the CommonJS module wrapper's, crashing the server bundle at boot +// with "Identifier '__dirname' has already been declared". +const STICKERS_REL = path.join('models', 'metadata', 'stickers.js'); +function findRepoRoot(marker) { + const seeds = [process.cwd(), process.env.PWD, process.env.OLDPWD].filter(Boolean); + for (const seed of seeds) { + let dir = seed; + for (let i = 0; i < 12; i += 1) { + try { fs.accessSync(path.join(dir, marker)); return dir; } catch (e) { /* walk up */ } + const parent = path.dirname(dir); + if (parent === dir) break; + dir = parent; + } + } + return process.cwd(); +} +const src = fs.readFileSync(path.join(findRepoRoot(STICKERS_REL), STICKERS_REL), 'utf8'); // --- parse the real tables from source ------------------------------------ function parseExactMap(name) { diff --git a/tests/stickers.test.js b/tests/stickers.test.js index 53129644c5..7cfcc477e7 100644 --- a/tests/stickers.test.js +++ b/tests/stickers.test.js @@ -11,10 +11,25 @@ const assert = require('assert'); const fs = require('fs'); const path = require('path'); -const src = fs.readFileSync( - path.join(__dirname, '..', 'models', 'metadata', 'stickers.js'), - 'utf8', -); +// Locate the source via a process.cwd()/PWD walk-up, NOT the bare `__dirname` global: +// under `meteor test` referencing `__dirname` makes Meteor inject a `const __dirname` +// that collides with the CommonJS module wrapper's, crashing the server bundle at boot +// with "Identifier '__dirname' has already been declared". +const STICKERS_REL = path.join('models', 'metadata', 'stickers.js'); +function findRepoRoot(marker) { + const seeds = [process.cwd(), process.env.PWD, process.env.OLDPWD].filter(Boolean); + for (const seed of seeds) { + let dir = seed; + for (let i = 0; i < 12; i += 1) { + try { fs.accessSync(path.join(dir, marker)); return dir; } catch (e) { /* walk up */ } + const parent = path.dirname(dir); + if (parent === dir) break; + dir = parent; + } + } + return process.cwd(); +} +const src = fs.readFileSync(path.join(findRepoRoot(STICKERS_REL), STICKERS_REL), 'utf8'); function parseArray(name) { const m = src.match(new RegExp(`${name} = \\[([\\s\\S]*?)\\]`));