* test(#3145): bound the installer/runtime cluster onto the process seam Migrates 156 unbounded sync spawn sites across 47 files. Allowlist 120 to 73. Timeouts are sized from evidence already in the tree rather than a house default, because this wave spawns installers rather than git plumbing and an undersized bound does not catch a hang -- it manufactures CI flake, which is worse, since a flake gets re-run instead of investigated. install.test.cjs records a real spawnSync ETIMEDOUT at a 60000ms cap on a loaded bench while another lane passed the same commit in 12.7s, so full installs are bound at 120000ms against that recorded incident. Also adds an auditable escape to the guard's timeout ceiling. The 600000ms cap was set in #3143 from partial evidence, but fragment-single-edit- propagation carries a documented, load-tested 900000ms bound on a run that chains a full build plus eight generators -- the guard would have rejected a correct timeout the moment that file left the allowlist. A value above the ceiling is now permitted only with an inline allow-spawn-timeout-ceiling marker carrying a non-empty reason. It raises the ceiling; it never waives the requirement for a bound, which is asserted directly. install-shared.cjs keeps its hand-rolled assert rather than routing through throwIfFailed: its message embeds both streams, and throwIfFailed carries only a trimmed stderr. The message now also names the outcome, so a bounded timeout reads as such across its 38 importers instead of as expected null to equal 0. * test(#3145): extract class-norm timeouts and correct the build-hooks sizing A pre-PR review found 52 copies of four class-norm timeout constants across this wave. These are not per-suite fixture bindings -- they are shared facts about how long a class of subprocess takes, derived from a recorded bench incident. That norm already moved once (60000 to 120000 after a real ETIMEDOUT), and 52 copies would have drifted the next time it moved. Extracts tests/helpers/timeouts.cjs, where each norm is justified once, and converts the copies. A site that genuinely differs -- a real tsc compile, or regen:derived -- keeps its own local constant with its own justification. Also corrects a misclassification: scripts/build-hooks.js was sized as a build at 120000 in twelve places and 60000 in another, but it compiles and bundles nothing. Its own header says no bundling needed; it copies pre-built files and syntax-checks them with vm. Three different values bounded one script; now there is one. * test(#3145): fix red CI — lint self-match and a Windows chunk overrun Two failures on PR 3176. lint-allow-test-rule-refs read a RuleTester fixture as a real exemption. The fixture exists to prove an unrelated marker does NOT suppress the rule, so it carries that marker's literal text as test data. Split via concatenation, the same idiom no-unbounded-spawn-allowlist.test.cjs already uses for its own self-match problem. The explanatory comment needed the same treatment. The Windows shard 3/3 chunk was killed at its 600000ms budget. Output stopped seven minutes before the kill, so this was an overrun rather than a slow chunk: regenDerivedPropagatesSingleFragmentEditWithNoSecondSourceSurface runs regen:derived bounded at 900000ms, which is larger than the whole chunk budget, so the chunk killer always fires first and it can never complete there. Both the test and that bound predate this change; modifying the file pulled it into the Windows targeted set and exposed it. Skipped on Windows with the reason recorded; the Linux lanes cover it. The 900000 bound and its ceiling marker are unchanged -- they are correct. * test(#3145): refresh the stale test-timings cost table The Windows shard was killed at its 600000ms per-chunk budget. run-tests.cjs packs chunks by measured duration from tests/test-timings.json, and an unknown file falls back to the table's median weight -- advisory by design, but it silently underweights exactly the files that matter. Four of the failing chunk's 22 files were absent from the table, including the two heaviest: fragment-single-edit-propagation.install.test.cjs at 230s (it runs regen:derived) and agent-fragments-emission.install.test.cjs at 79s. Both were weighted as average, so the chunk's total weight read 53.68 against a budget of 60 and the packer produced a single chunk. Regenerated from a passing full-suite run, per the remedy the script itself documents. 700 to 770 entries, 70 added, 0 dropped -- verified, since gen-test-timings.cjs replaces the table wholesale rather than merging. Proven against the real packer: the same 22 files now weigh 103.91 and split into two chunks. No logic, budget, or timeout was changed; raising a budget to make a red gate pass is not a fix. --------- Co-authored-by: sim <sim@local>
347 lines
12 KiB
JavaScript
347 lines
12 KiB
JavaScript
'use strict';
|
|
|
|
/**
|
|
* Tests for gsd-core/bin/ensure-runtime-build.cjs — the self-healing runtime
|
|
* build that compiles the gitignored ./lib/*.cjs artifacts on demand when the
|
|
* CLI is run from a channel (Claude Code plugin marketplace) that never ran
|
|
* `npm run build:lib`. See #2002.
|
|
*
|
|
* The unit tests inject `spawn` so no real tsc runs; the final integration test
|
|
* drives the REAL module against a real (tiny) tsc build in a temp package to
|
|
* prove the end-to-end heal. IO failures are injected by monkeypatching the fs
|
|
* method (root-independent, OS-independent), never via chmod.
|
|
*/
|
|
|
|
const { test } = require('node:test');
|
|
const assert = require('node:assert/strict');
|
|
const fs = require('node:fs');
|
|
const path = require('node:path');
|
|
const { runNode } = require('./helpers/process-seam.cjs');
|
|
const { throwIfFailed } = require('./helpers/git-fixture.cjs');
|
|
const { createTempDir, cleanup } = require('./helpers.cjs');
|
|
|
|
const {
|
|
ensureRuntimeBuild,
|
|
resolveTscScript,
|
|
isBuilt,
|
|
RuntimeBuildError,
|
|
SENTINEL,
|
|
} = require('../gsd-core/bin/ensure-runtime-build.cjs');
|
|
|
|
const REPO_ROOT = path.resolve(__dirname, '..');
|
|
// 180000: a build — this driver invokes ensureRuntimeBuild against a REAL
|
|
// tsc compile (not a query), at the tsc-compile end of the
|
|
// 120000-180000 build-class norm.
|
|
const BUILD_TIMEOUT_MS = 180000;
|
|
|
|
/** Fresh isolated package dir (auto-cleaned) with a libDir and optional tsconfig. */
|
|
function makeEnv(t, { withTsconfig = true, prebuilt = false } = {}) {
|
|
const dir = createTempDir('gsd-erb-');
|
|
t.after(() => cleanup(dir));
|
|
const libDir = path.join(dir, 'gsd-core', 'bin', 'lib');
|
|
fs.mkdirSync(libDir, { recursive: true });
|
|
if (withTsconfig) fs.writeFileSync(path.join(dir, 'tsconfig.build.json'), '{}');
|
|
if (prebuilt) fs.writeFileSync(path.join(libDir, SENTINEL), '// built\n');
|
|
return { dir, packageRoot: dir, libDir };
|
|
}
|
|
|
|
/** A spawn stub that "builds" by writing the sentinel, then reports success. */
|
|
function buildingSpawn(libDir, calls) {
|
|
return (cmd, args, options) => {
|
|
calls.push({ cmd, args, options });
|
|
fs.writeFileSync(path.join(libDir, SENTINEL), '// built by stub\n');
|
|
return { status: 0, stdout: '', stderr: '' };
|
|
};
|
|
}
|
|
|
|
const silent = () => {};
|
|
|
|
test('fast path: already-built lib is a no-op, never spawns tsc', (t) => {
|
|
const { libDir, packageRoot } = makeEnv(t, { prebuilt: true });
|
|
const calls = [];
|
|
const res = ensureRuntimeBuild({
|
|
libDir,
|
|
packageRoot,
|
|
spawn: (...a) => { calls.push(a); return { status: 0 }; },
|
|
log: silent,
|
|
});
|
|
assert.deepEqual(res, { built: true, healed: false });
|
|
assert.equal(calls.length, 0, 'must not spawn when already built');
|
|
});
|
|
|
|
test('heal path: missing lib is compiled once with the correct tsc invocation', (t) => {
|
|
const { libDir, packageRoot } = makeEnv(t);
|
|
const calls = [];
|
|
const res = ensureRuntimeBuild({
|
|
libDir,
|
|
packageRoot,
|
|
tscScript: '/fake/typescript/bin/tsc',
|
|
spawn: buildingSpawn(libDir, calls),
|
|
log: silent,
|
|
});
|
|
assert.equal(res.built, true);
|
|
assert.equal(res.healed, true);
|
|
assert.equal(calls.length, 1, 'compiles exactly once');
|
|
const { cmd, args, options } = calls[0];
|
|
assert.equal(cmd, process.execPath, 'runs `node <tsc>` (portable, no shell/.cmd)');
|
|
assert.deepEqual(args, [
|
|
'/fake/typescript/bin/tsc',
|
|
'-p',
|
|
path.join(packageRoot, 'tsconfig.build.json'),
|
|
]);
|
|
assert.equal(options.cwd, packageRoot);
|
|
assert.ok(isBuilt(libDir), 'sentinel exists after heal');
|
|
});
|
|
|
|
test('missing tsconfig.build.json throws an actionable RuntimeBuildError', (t) => {
|
|
const { libDir, packageRoot } = makeEnv(t, { withTsconfig: false });
|
|
assert.throws(
|
|
() => ensureRuntimeBuild({ libDir, packageRoot, tscScript: '/fake/tsc', log: silent }),
|
|
(err) => {
|
|
assert.ok(err instanceof RuntimeBuildError);
|
|
assert.match(err.message, /tsconfig\.build\.json not found/);
|
|
assert.match(err.message, /npm run build:lib/);
|
|
return true;
|
|
},
|
|
);
|
|
});
|
|
|
|
test('absent TypeScript throws with install guidance and does not spawn', (t) => {
|
|
const { libDir, packageRoot } = makeEnv(t);
|
|
const calls = [];
|
|
assert.throws(
|
|
() => ensureRuntimeBuild({
|
|
libDir,
|
|
packageRoot,
|
|
tscScript: null, // TypeScript not resolvable
|
|
spawn: (...a) => { calls.push(a); return { status: 0 }; },
|
|
log: silent,
|
|
}),
|
|
(err) => {
|
|
assert.ok(err instanceof RuntimeBuildError);
|
|
assert.match(err.message, /TypeScript is unavailable/);
|
|
assert.match(err.message, /npm install && npm run build:lib/);
|
|
return true;
|
|
},
|
|
);
|
|
assert.equal(calls.length, 0, 'never spawns when tsc is absent');
|
|
});
|
|
|
|
test('non-zero tsc exit surfaces the exit code and captured output', (t) => {
|
|
const { libDir, packageRoot } = makeEnv(t);
|
|
assert.throws(
|
|
() => ensureRuntimeBuild({
|
|
libDir,
|
|
packageRoot,
|
|
tscScript: '/fake/tsc',
|
|
spawn: () => ({ status: 2, stdout: '', stderr: 'src/x.cts(1,1): error TS1005' }),
|
|
log: silent,
|
|
}),
|
|
(err) => {
|
|
assert.ok(err instanceof RuntimeBuildError);
|
|
assert.match(err.message, /tsc exit 2/);
|
|
assert.match(err.message, /error TS1005/);
|
|
return true;
|
|
},
|
|
);
|
|
});
|
|
|
|
test('tsc reports success but sentinel is still missing → throws', (t) => {
|
|
const { libDir, packageRoot } = makeEnv(t);
|
|
assert.throws(
|
|
() => ensureRuntimeBuild({
|
|
libDir,
|
|
packageRoot,
|
|
tscScript: '/fake/tsc',
|
|
spawn: () => ({ status: 0 }), // "succeeds" but writes nothing
|
|
log: silent,
|
|
}),
|
|
(err) => {
|
|
assert.ok(err instanceof RuntimeBuildError);
|
|
assert.match(err.message, /still missing/);
|
|
return true;
|
|
},
|
|
);
|
|
});
|
|
|
|
test('the build lock releases after a successful heal', (t) => {
|
|
const { libDir, packageRoot } = makeEnv(t);
|
|
ensureRuntimeBuild({
|
|
libDir,
|
|
packageRoot,
|
|
tscScript: '/fake/tsc',
|
|
spawn: buildingSpawn(libDir, []),
|
|
log: silent,
|
|
});
|
|
assert.ok(!fs.existsSync(path.join(libDir, '.build.lock')), 'lock removed in finally');
|
|
});
|
|
|
|
test('lock contention: waits for a peer build instead of racing tsc', (t) => {
|
|
const { libDir, packageRoot } = makeEnv(t);
|
|
fs.mkdirSync(path.join(libDir, '.build.lock')); // a peer "holds" the lock
|
|
const calls = [];
|
|
const res = ensureRuntimeBuild({
|
|
libDir,
|
|
packageRoot,
|
|
tscScript: '/fake/tsc',
|
|
spawn: (...a) => { calls.push(a); return { status: 0 }; },
|
|
log: silent,
|
|
pollMs: 1,
|
|
waitTimeoutMs: 1000,
|
|
// Peer finishes the build between polls.
|
|
onPoll: () => fs.writeFileSync(path.join(libDir, SENTINEL), '// peer built\n'),
|
|
});
|
|
assert.equal(res.healed, true);
|
|
assert.equal(res.waited, true);
|
|
assert.equal(calls.length, 0, 'never launches a competing tsc');
|
|
});
|
|
|
|
test('lock takeover: a crashed peer (stale lock) is taken over and the build runs', (t) => {
|
|
const { libDir, packageRoot } = makeEnv(t);
|
|
const lockDir = path.join(libDir, '.build.lock');
|
|
fs.mkdirSync(lockDir);
|
|
const calls = [];
|
|
const res = ensureRuntimeBuild({
|
|
libDir,
|
|
packageRoot,
|
|
tscScript: '/fake/tsc',
|
|
spawn: buildingSpawn(libDir, calls),
|
|
log: silent,
|
|
pollMs: 1,
|
|
waitTimeoutMs: 0, // give up waiting immediately
|
|
// Peer vanished without finishing — free the stale lock so we can take over.
|
|
// eslint-disable-next-line local/no-raw-rmsync-in-tests -- simulating a peer releasing its lock, not temp-dir teardown
|
|
onPoll: () => fs.rmSync(lockDir, { recursive: true, force: true }),
|
|
});
|
|
assert.equal(res.healed, true);
|
|
assert.equal(calls.length, 1, 'takes over and builds once');
|
|
});
|
|
|
|
test('lock never frees before timeout → throws a lock-held RuntimeBuildError', (t) => {
|
|
const { libDir, packageRoot } = makeEnv(t);
|
|
fs.mkdirSync(path.join(libDir, '.build.lock')); // stuck peer, never released
|
|
assert.throws(
|
|
() => ensureRuntimeBuild({
|
|
libDir,
|
|
packageRoot,
|
|
tscScript: '/fake/tsc',
|
|
spawn: buildingSpawn(libDir, []),
|
|
log: silent,
|
|
pollMs: 1,
|
|
waitTimeoutMs: 0,
|
|
}),
|
|
(err) => {
|
|
assert.ok(err instanceof RuntimeBuildError);
|
|
assert.match(err.message, /did not complete within 0ms/);
|
|
return true;
|
|
},
|
|
);
|
|
});
|
|
|
|
test('IO fault on lock acquisition propagates (injected via fs monkeypatch, not chmod)', (t) => {
|
|
const { libDir, packageRoot } = makeEnv(t);
|
|
const lockDir = path.join(libDir, '.build.lock');
|
|
const orig = fs.mkdirSync;
|
|
fs.mkdirSync = (target, opts) => {
|
|
if (target === lockDir) {
|
|
const e = new Error('injected EPERM');
|
|
e.code = 'EPERM';
|
|
throw e;
|
|
}
|
|
return orig(target, opts);
|
|
};
|
|
try {
|
|
assert.throws(
|
|
() => ensureRuntimeBuild({
|
|
libDir,
|
|
packageRoot,
|
|
tscScript: '/fake/tsc',
|
|
spawn: buildingSpawn(libDir, []),
|
|
log: silent,
|
|
}),
|
|
(err) => {
|
|
// Non-EEXIST lock errors must NOT be swallowed as "peer holds lock".
|
|
assert.equal(err.code, 'EPERM');
|
|
return true;
|
|
},
|
|
);
|
|
} finally {
|
|
fs.mkdirSync = orig;
|
|
}
|
|
});
|
|
|
|
test('heal clears a stale tsc incremental cache before compiling (forces full emit)', (t) => {
|
|
const { libDir, packageRoot } = makeEnv(t);
|
|
const buildInfo = path.join(packageRoot, 'tsconfig.build.tsbuildinfo');
|
|
fs.writeFileSync(buildInfo, '{"stale":true}'); // leftover from a partial build
|
|
ensureRuntimeBuild({
|
|
libDir,
|
|
packageRoot,
|
|
tscScript: '/fake/tsc',
|
|
spawn: buildingSpawn(libDir, []),
|
|
log: silent,
|
|
});
|
|
assert.ok(!fs.existsSync(buildInfo), 'stale .tsbuildinfo removed so tsc re-emits');
|
|
});
|
|
|
|
test('resolveTscScript finds the real TypeScript under the repo root', () => {
|
|
const tsc = resolveTscScript(REPO_ROOT);
|
|
assert.ok(tsc && tsc.includes('typescript'), 'resolves the installed typescript/bin/tsc');
|
|
assert.ok(fs.existsSync(tsc), 'resolved tsc script exists on disk');
|
|
});
|
|
|
|
test('integration: real tsc heals a missing lib end-to-end (subprocess)', (t) => {
|
|
// Build a tiny package that mirrors the real layout: a src/*.cts that tsc
|
|
// compiles to gsd-core/bin/lib/cli-exit.cjs. Then invoke the REAL module from
|
|
// a subprocess with lib absent and assert it self-heals and the require works.
|
|
const dir = createTempDir('gsd-erb-int-');
|
|
t.after(() => cleanup(dir));
|
|
fs.mkdirSync(path.join(dir, 'src'), { recursive: true });
|
|
fs.mkdirSync(path.join(dir, 'gsd-core', 'bin', 'lib'), { recursive: true });
|
|
fs.writeFileSync(
|
|
path.join(dir, 'tsconfig.build.json'),
|
|
JSON.stringify({
|
|
compilerOptions: {
|
|
rootDir: 'src',
|
|
outDir: 'gsd-core/bin/lib',
|
|
module: 'nodenext',
|
|
moduleResolution: 'nodenext',
|
|
target: 'ES2022',
|
|
noEmitOnError: true,
|
|
},
|
|
include: ['src/**/*.cts'],
|
|
}),
|
|
);
|
|
fs.writeFileSync(
|
|
path.join(dir, 'src', 'cli-exit.cts'),
|
|
'export const runMain = (): string => "ok";\n',
|
|
);
|
|
|
|
// Resolve the REAL tsc and pass it explicitly, rather than symlinking
|
|
// node_modules into the temp package — directory-symlink creation is a
|
|
// privileged operation on Windows and would fail on CI runners. resolveTscScript
|
|
// itself is covered by its own unit test; here we just need a real tsc to run.
|
|
const realTsc = resolveTscScript(REPO_ROOT);
|
|
assert.ok(realTsc, 'real typescript resolvable for the integration build');
|
|
const driver = path.join(dir, 'driver.cjs');
|
|
fs.writeFileSync(
|
|
driver,
|
|
`const { ensureRuntimeBuild } = require(${JSON.stringify(
|
|
path.join(REPO_ROOT, 'gsd-core', 'bin', 'ensure-runtime-build.cjs'),
|
|
)});\n` +
|
|
`const libDir = ${JSON.stringify(path.join(dir, 'gsd-core', 'bin', 'lib'))};\n` +
|
|
`const packageRoot = ${JSON.stringify(dir)};\n` +
|
|
`const tscScript = ${JSON.stringify(realTsc)};\n` +
|
|
'const r = ensureRuntimeBuild({ libDir, packageRoot, tscScript, log: () => {} });\n' +
|
|
'const { runMain } = require(require("path").join(libDir, "cli-exit.cjs"));\n' +
|
|
'process.stdout.write(JSON.stringify({ healed: r.healed, run: runMain() }));\n',
|
|
);
|
|
|
|
assert.ok(!isBuilt(path.join(dir, 'gsd-core', 'bin', 'lib')), 'lib starts empty');
|
|
const driverResult = runNode([driver], { cwd: dir, timeoutMs: BUILD_TIMEOUT_MS });
|
|
throwIfFailed(driverResult, `node ${driver}`);
|
|
const parsed = JSON.parse(driverResult.stdout);
|
|
assert.equal(parsed.healed, true, 'self-healed the missing build');
|
|
assert.equal(parsed.run, 'ok', 'the freshly-compiled cli-exit.cjs loads and runs');
|
|
});
|