Two review findings from round 2, both correct. 1. The regression suite killed its child only after the assertions. If `assert.rejects` or either assertion failed, execution never reached the kill, leaving a 60-second child attached to the runner — a suite guarding a process leak would leak one itself, precisely on the run where the regression had come back. Teardown is now registered from `onSpawn` via `t.after`, so it runs on every path out of every case. That is the helper's own ownership rule applied to the tests; killing an already-exited child is a no-op, so the early-exit cases pay nothing for it. 2. Document the new shared helper, per the maintenance rules. AGENTS.md's scripts/lib tree entry now carries announced-child.mjs and what its contract buys, and the smoke:web:app paragraph records why the spawn/readiness step moved out of the smoke (same reason teardownWebServer left prod-web-server's stop()). README's test:scripts row names the new suite alongside resolve-node-bin's. Not mirrored into .github/copilot-instructions.md: that file is a distillation of the rules a reviewer cites against a diff, and deliberately excludes the project-structure tree and tooling inventory. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_013hzwzS8UvBsr4yZm7o7sev Signed-off-by: cliffhall <cliff@futurescale.com>
139 lines
4.6 KiB
JavaScript
139 lines
4.6 KiB
JavaScript
/**
|
|
* Tests for the spawn/readiness ownership helper (#2000).
|
|
*
|
|
* The invariant under test is the one a smoke script can never check itself:
|
|
* a child that stays alive *through* the readiness timeout must still be
|
|
* reachable by the caller's teardown. `smoke:web:app`'s happy path always gets
|
|
* the announcement, so a regression here would be silent in `npm run ci` and
|
|
* would surface only as a stray process holding a port on a later run.
|
|
*
|
|
* Every case drives a real `node -e` child rather than a spy, so the assertion
|
|
* is about a real process being killable, not about a mock's call log.
|
|
*/
|
|
|
|
import test from "node:test";
|
|
import assert from "node:assert/strict";
|
|
import { startAnnouncedChild } from "./announced-child.mjs";
|
|
|
|
/** Wait for a real child to exit (or resolve immediately if it already has). */
|
|
function waitForExit(child) {
|
|
if (child.exitCode !== null || child.signalCode !== null)
|
|
return Promise.resolve();
|
|
return new Promise((resolve) => child.once("close", resolve));
|
|
}
|
|
|
|
/**
|
|
* Build an `onSpawn` that registers teardown the moment the child exists, so it
|
|
* is killed on every path out of the test — a failed assertion included.
|
|
*
|
|
* This is the helper's own ownership rule applied to the suite: killing only on
|
|
* the success path would leak a 60-second child into the runner precisely when
|
|
* the regression being guarded has come back, which is the one run where a leak
|
|
* matters most. Killing an already-exited child is a no-op, so the early-exit
|
|
* cases pay nothing for it.
|
|
*/
|
|
function killOnTeardown(t, capture = () => {}) {
|
|
return (child) => {
|
|
capture(child);
|
|
t.after(async () => {
|
|
child.kill("SIGKILL");
|
|
await waitForExit(child);
|
|
});
|
|
};
|
|
}
|
|
|
|
test("hands the child to onSpawn before waiting, and returns the match", async (t) => {
|
|
let seen = null;
|
|
const { child, match } = await startAnnouncedChild({
|
|
command: process.execPath,
|
|
args: ["-e", 'console.error("listening at http://127.0.0.1:9999")'],
|
|
pattern: /listening at (http:\/\/\S+)/i,
|
|
onSpawn: killOnTeardown(t, (c) => (seen = c)),
|
|
what: "probe",
|
|
timeoutMs: 10_000,
|
|
pollMs: 25,
|
|
});
|
|
assert.equal(seen, child, "onSpawn got the child that was returned");
|
|
assert.equal(match[1], "http://127.0.0.1:9999");
|
|
});
|
|
|
|
test("scans stdout as well as stderr", async (t) => {
|
|
const { match } = await startAnnouncedChild({
|
|
command: process.execPath,
|
|
args: ["-e", 'console.log("ready on 4242")'],
|
|
pattern: /ready on (\d+)/,
|
|
onSpawn: killOnTeardown(t),
|
|
what: "probe",
|
|
timeoutMs: 10_000,
|
|
pollMs: 25,
|
|
});
|
|
assert.equal(match[1], "4242");
|
|
});
|
|
|
|
test("a non-announcing child is still reachable and killable after the timeout", async (t) => {
|
|
// The regression: a child that is alive but never announces. The throw must
|
|
// not be the only thing that happens — the caller must already hold the
|
|
// handle, or `process.exit(1)` leaves this running.
|
|
let published = null;
|
|
await assert.rejects(
|
|
startAnnouncedChild({
|
|
// Sleeps well past the budget and prints nothing matching.
|
|
command: process.execPath,
|
|
args: ["-e", "setTimeout(() => {}, 60000)"],
|
|
pattern: /never going to match/,
|
|
onSpawn: killOnTeardown(t, (c) => (published = c)),
|
|
what: "probe",
|
|
timeoutMs: 300,
|
|
pollMs: 25,
|
|
}),
|
|
/probe did not start within/,
|
|
);
|
|
|
|
assert.ok(published, "the child was published before the readiness wait");
|
|
assert.equal(
|
|
published.exitCode,
|
|
null,
|
|
"and it is still alive, as in the bug",
|
|
);
|
|
|
|
published.kill("SIGKILL");
|
|
await waitForExit(published);
|
|
assert.ok(
|
|
published.exitCode !== null || published.signalCode !== null,
|
|
"teardown could actually stop it",
|
|
);
|
|
});
|
|
|
|
test("reports a child that exits before announcing, with its output", async (t) => {
|
|
let published = null;
|
|
await assert.rejects(
|
|
startAnnouncedChild({
|
|
command: process.execPath,
|
|
args: ["-e", 'console.error("boom: bad config"); process.exit(3)'],
|
|
pattern: /never going to match/,
|
|
onSpawn: killOnTeardown(t, (c) => (published = c)),
|
|
what: "probe",
|
|
timeoutMs: 10_000,
|
|
pollMs: 25,
|
|
}),
|
|
(err) =>
|
|
/probe exited early/.test(err.message) &&
|
|
/boom: bad config/.test(err.message),
|
|
);
|
|
assert.ok(published, "published even on the early-exit path");
|
|
});
|
|
|
|
test("reports a spawn failure rather than throwing it uncaught", async (t) => {
|
|
await assert.rejects(
|
|
startAnnouncedChild({
|
|
command: "definitely-not-an-executable-2000",
|
|
args: [],
|
|
pattern: /never/,
|
|
onSpawn: killOnTeardown(t),
|
|
what: "probe",
|
|
timeoutMs: 10_000,
|
|
pollMs: 25,
|
|
}),
|
|
/could not spawn the probe:/,
|
|
);
|
|
});
|