Files
msd-core/tests/npm-version-check-diagnosis.test.cjs
sim 8731a90be6 fix: revert execNpm import in check-env.cjs, keep the diagnosis improvement
The execNpm-routing redesign (previous commit) broke every real CI job:
check-env.cjs runs as its own standalone "Environment check" step BEFORE
`npm ci` / `npm run build:lib` -- a deliberate pre-flight, run before
there is even a node_modules to build with. Its require of
../gsd-core/bin/lib/shell-command-projection.cjs (a tsc-compiled artifact
that plain does not exist at that point in the pipeline) crashed with
MODULE_NOT_FOUND on every platform, immediately, confirmed via the real
CI log. My own local gsd-test run never caught this because it doesn't
replicate that exact pre-build step ordering.

Reverted the cross-module require entirely; check-env.cjs is back to a
self-contained spawnSync(npmCmd, ...) call, no requires reaching into
gsd-core/bin/lib. Kept the two things actually worth keeping from that
detour:
  - the 15_000ms timeout (matches execNpm's own default elsewhere in this
    repo -- not invented, an existing precedent -- vs. the original 10s
    that failed twice under real Windows CI contention);
  - computing `timedOut` via `error.code === 'ETIMEDOUT'` inline (the
    same canonical, cross-platform-correct predicate that seam uses),
    rather than the earlier signal === 'SIGTERM' check, which that seam's
    own docstring documents as platform-fragile with a Windows-specific
    false-negative risk.

Manually verified check-env.cjs runs correctly with gsd-core/bin/lib
temporarily removed entirely (simulating the real pre-npm-ci CI
ordering) before re-running gsd-test, since this repo blocks local
node --test.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-09-09 08:07:34 -04:00

72 lines
3.6 KiB
JavaScript

'use strict';
/**
* Regression coverage for #4460 (discovered blocking this PR's own Windows
* CI, unrelated to this PR's actual diff -- fixed inline per this repo's
* no-defer policy): scripts/check-env.cjs's npm-version check used to
* collapse every spawnSync failure mode into one message, "npm binary not
* found on PATH" -- including a TIMEOUT under CI load, which looks
* identical to a genuine ENOENT (exitCode stays non-zero, stdout stays
* empty either way). Confirmed live, twice, on real Windows CI: the check
* now correctly reports "timed out under CI load" for exactly that case.
*
* describeNpmVersionCheckFailure is the extracted, pure reason-selection
* logic. It takes a plain result object shaped like this repo's canonical
* OS-shell-projection seam's SpawnResultOutput (execNpm,
* src/shell-command-projection.cts) -- same `timedOut` (error.code ===
* 'ETIMEDOUT') semantics -- but check-env.cjs computes that shape inline
* rather than importing the seam itself: that seam's compiled output
* (gsd-core/bin/lib/*.cjs) does not exist yet when check-env.cjs runs as
* its own standalone pre-`npm ci` CI step (an earlier version of this fix
* imported it directly and crashed every real CI job with
* MODULE_NOT_FOUND). Kept in scripts/lib/ (not scripts/check-env.cjs
* itself, which runs its CLI unconditionally on require) so it can be
* required directly here without triggering a real environment check.
*/
const { describe, test } = require('node:test');
const assert = require('node:assert/strict');
const { describeNpmVersionCheckFailure } = require('../scripts/lib/npm-version-check-diagnosis.cjs');
describe('describeNpmVersionCheckFailure (#4460)', () => {
test('a genuine ENOENT (npm truly absent) reports the original message', () => {
const result = { exitCode: 127, stdout: '', stderr: 'npm: not found', signal: null, error: Object.assign(new Error('spawnSync npm ENOENT'), { code: 'ENOENT' }), timedOut: false };
assert.equal(
describeNpmVersionCheckFailure(result),
'npm binary not found on PATH',
);
});
test('a timed-out spawn is reported as a timeout, not a missing binary', () => {
const result = { exitCode: 1, stdout: '', stderr: '', signal: 'SIGTERM', error: Object.assign(new Error('ETIMEDOUT'), { code: 'ETIMEDOUT' }), timedOut: true };
const reason = describeNpmVersionCheckFailure(result);
assert.match(reason, /timed out under CI load/);
assert.doesNotMatch(reason, /^npm binary not found on PATH$/);
});
test('timedOut is authoritative even without an ENOENT-shaped error object', () => {
// Mirrors what execNpm/_spawnResult actually produces for a timeout:
// error is set (spawnSync always populates it when `timeout` fires),
// but its code is ETIMEDOUT, not ENOENT -- timedOut is what matters.
const result = { exitCode: 1, stdout: '', stderr: '', signal: 'SIGTERM', error: null, timedOut: true };
assert.match(describeNpmVersionCheckFailure(result), /timed out under CI load/);
});
test('a non-zero exit with no stdout and no timeout is reported with the actual exit code', () => {
const result = { exitCode: 1, stdout: '', stderr: '', signal: null, error: null, timedOut: false };
assert.equal(
describeNpmVersionCheckFailure(result),
'npm --version exited 1 with no usable output',
);
});
test('exitCode 0 with no stdout (defensive default) falls back to the original message', () => {
const result = { exitCode: 0, stdout: '', stderr: '', signal: null, error: null, timedOut: false };
assert.equal(
describeNpmVersionCheckFailure(result),
'npm binary not found on PATH',
);
});
});