Files
worldmonitor/tests/scripts-shared-mirror.test.mjs
Elie Habib da45e830c9 fix(regional-snapshots): mirror shared/geography.js into scripts/shared (#2954)
The derived-signals seed bundle failed on Railway with:

  [Regional-Snapshots] Error [ERR_MODULE_NOT_FOUND]: Cannot find module
    '/shared/geography.js' imported from /app/seed-regional-snapshots.mjs

Root cause: the Railway cron service for derived-signals deploys with
rootDirectory=scripts, so scripts/ becomes the container /app root. The
repo-root shared/ folder is NOT copied into the container.

seed-regional-snapshots.mjs imported '../shared/geography.js' which
resolved to /shared/geography.js at runtime and blew up. Same for four
scripts/regional-snapshot/* compute modules using '../../shared/...'.

Fix: mirror the runtime shared assets into scripts/shared/ and update
the five import paths.

- Add scripts/shared/geography.js (byte-for-byte copy of shared/geography.js)
- Add scripts/shared/package.json with {"type": "module"} so Node
  parses geography.js as ESM inside the rootDirectory=scripts deploy.
  scripts/package.json does NOT set type:module.
- scripts/shared/iso2-to-region.json and iso3-to-iso2.json were already
  mirrored from prior PRs, no change needed.
- scripts/seed-regional-snapshots.mjs:
    '../shared/geography.js' -> './shared/geography.js'
- scripts/regional-snapshot/actor-scoring.mjs:
  scripts/regional-snapshot/evidence-collector.mjs:
  scripts/regional-snapshot/scenario-builder.mjs:
    '../../shared/geography.js' -> '../shared/geography.js'
- scripts/regional-snapshot/balance-vector.mjs:
    '../../shared/geography.js' -> '../shared/geography.js'
    '../../shared/iso3-to-iso2.json' -> '../shared/iso3-to-iso2.json'

JSDoc type imports like {import('../../shared/regions.types.js').X}
are intentionally NOT changed. They live inside /** */ comments,
Node ignores them at runtime, and tsc still resolves them correctly
from the repo root during local typecheck.

Regression coverage: new tests/scripts-shared-mirror.test.mjs
1. Asserts scripts/shared/{geography.js, iso2-to-region.json,
   iso3-to-iso2.json, un-to-iso2.json} are byte-for-byte identical
   to their shared/ canonical counterparts.
2. Asserts scripts/shared/package.json has type:module.
3. For each regional-snapshot seed file, resolves every runtime
   "import ... from ...shared/..." to an absolute path and asserts
   it lands inside scripts/shared/. A regression that reintroduces
   "../shared/" from a scripts/ file or "../../shared/" from a
   scripts/regional-snapshot/ file will fail this check.

Verified with a temp-dir simulation of rootDirectory=scripts: all
five modules load clean. Full npm run test:data suite passes 4298/4298.

Secondary observation (not in this PR): the same log shows
Cross-Source-Signals missing 4 upstream keys (supply_chain:shipping:v2,
gdelt:intel:tone:{military,nuclear,maritime}), producing 0 composite
escalation zones. That is an upstream data-freshness issue in the
supply-chain and gdelt-intel seeders, not in this bundle.
2026-04-11 20:19:01 +04:00

98 lines
4.0 KiB
JavaScript

import { describe, it } from 'node:test';
import assert from 'node:assert/strict';
import { readFileSync } from 'node:fs';
import { dirname, join, resolve } from 'node:path';
import { fileURLToPath } from 'node:url';
const __dirname = dirname(fileURLToPath(import.meta.url));
const repoRoot = resolve(__dirname, '..');
// The Railway "derived-signals" seed bundle deploys with rootDirectory=scripts,
// which means the repo-root `shared/` folder is NOT present in the container.
// Scripts that need shared/* assets at runtime must import them from
// `scripts/shared/*` instead. `scripts/shared/*` is a byte-for-byte mirror
// of the subset of `shared/*` used by the Railway seeders.
//
// This test locks the mirror so a drift between `shared/X` and
// `scripts/shared/X` cannot slip through code review. When adding new
// mirrored files, append them to MIRRORED_FILES.
const MIRRORED_FILES = [
'geography.js',
'iso2-to-region.json',
'iso3-to-iso2.json',
'un-to-iso2.json',
];
describe('scripts/shared/ mirrors shared/', () => {
for (const relPath of MIRRORED_FILES) {
it(`${relPath} is identical between shared/ and scripts/shared/`, () => {
const canonical = readFileSync(join(repoRoot, 'shared', relPath), 'utf-8');
const mirror = readFileSync(join(repoRoot, 'scripts', 'shared', relPath), 'utf-8');
assert.equal(
mirror,
canonical,
`scripts/shared/${relPath} drifted from shared/${relPath}. ` +
`Run: cp shared/${relPath} scripts/shared/${relPath}`,
);
});
}
it('scripts/shared has a package.json marking it as ESM', () => {
// Required because scripts/package.json does NOT set "type": "module",
// so scripts/shared/geography.js (ESM syntax) would otherwise be parsed
// ambiguously when Railway loads it from rootDirectory=scripts.
const pkg = JSON.parse(readFileSync(join(repoRoot, 'scripts/shared/package.json'), 'utf-8'));
assert.equal(pkg.type, 'module');
});
});
describe('regional snapshot seed scripts use scripts/shared/ (not repo-root shared/)', () => {
// Guards the Railway rootDirectory=scripts runtime: an import whose
// resolved absolute path falls OUTSIDE scripts/shared/ (e.g. repo-root
// shared/) will ERR_MODULE_NOT_FOUND at runtime on Railway because the
// shared/ dir is not copied into the deploy root.
const FILES_THAT_MUST_USE_MIRROR = [
'scripts/seed-regional-snapshots.mjs',
'scripts/regional-snapshot/actor-scoring.mjs',
'scripts/regional-snapshot/balance-vector.mjs',
'scripts/regional-snapshot/evidence-collector.mjs',
'scripts/regional-snapshot/scenario-builder.mjs',
];
const scriptsSharedAbs = resolve(repoRoot, 'scripts/shared');
// Match any runtime `import ... from '<path>'` (ignores JSDoc `import()`
// type annotations which live inside /** */ comments). Only looks at
// lines that start with optional whitespace + `import`.
const RUNTIME_IMPORT_RE = /^\s*import\s[^\n]*?\bfrom\s+['"]([^'"]+)['"]/gm;
for (const rel of FILES_THAT_MUST_USE_MIRROR) {
it(`${rel} resolves all shared/ imports to scripts/shared/`, () => {
const src = readFileSync(join(repoRoot, rel), 'utf-8');
const fileAbs = resolve(repoRoot, rel);
const fileDir = dirname(fileAbs);
const offending = [];
for (const match of src.matchAll(RUNTIME_IMPORT_RE)) {
const specifier = match[1];
// Only inspect relative paths that land in a shared/ directory.
if (!/\/shared\//.test(specifier)) continue;
if (!specifier.startsWith('.')) continue;
const resolved = resolve(fileDir, specifier);
if (!resolved.startsWith(scriptsSharedAbs)) {
offending.push(` ${specifier}${resolved}`);
}
}
assert.equal(
offending.length,
0,
`${rel} has runtime import(s) that escape scripts/shared/:\n${offending.join('\n')}\n` +
`Railway service rootDirectory=scripts means these paths escape the deploy root. ` +
`Mirror the needed file into scripts/shared/ and update the import.`,
);
});
}
});