veritas-kanban/scripts/dev-process-ownership.test.mjs
Brad Groux 31c04977cd
fix: verify checkout process ownership before dev cleanup
Verify every configured listener and exact checkout process identity before development cleanup sends SIGTERM. Local ownership tests and live disposable listener checks passed. Fixes #1611.
2026-10-22 20:42:32 -07:00

107 lines
3.7 KiB
JavaScript

import assert from 'node:assert/strict';
import test from 'node:test';
import {
applyDevCleanup,
isDevCommand,
parseProcessSnapshot,
planDevCleanup,
} from './dev-process-ownership.mjs';
const root = '/checkout/veritas-kanban';
const processEntry = (pid, command, parentPid = 1) => ({
pid,
parentPid,
command,
started: 'Thu Oct 22 20:00:00 2026',
});
const vite = processEntry(42, `node ${root}/web/node_modules/vite/bin/vite.js --port 3000`);
const tsx = processEntry(
43,
`node ${root}/server/node_modules/tsx/dist/cli.mjs watch src/index.ts`
);
function fixture(entries = [vite, tsx]) {
const snapshot = new Map(entries.map((entry) => [entry.pid, entry]));
const signals = [];
return {
snapshot: () => snapshot,
cwd: (pid) => `${root}/${pid === 43 ? 'server' : 'web'}`,
checkout: () => root,
listeners: (port) => [Number(port) === 3000 ? 42 : 43],
signal: (pid) => signals.push(pid),
signals,
};
}
test('recognizes exact source runners and rejects unknown commands', () => {
assert(isDevCommand(vite.command, `${root}/web`, root));
assert(isDevCommand(tsx.command, `${root}/server`, root));
assert(
isDevCommand(
`node --require ${root}/node_modules/tsx/dist/preflight.cjs --import file://${root}/node_modules/tsx/dist/loader.mjs src/index.ts`,
`${root}/server`,
root
)
);
assert(isDevCommand('node /global/pnpm.cjs --filter server dev', root, root));
assert(!isDevCommand('node /global/pnpm.cjs dev:clean', root, root));
assert(!isDevCommand('node unrelated.js', root, root));
assert(!isDevCommand(`node -e console.log('${root}/server/src/index.ts')`, root, root));
assert(!isDevCommand(`node /another-worktree/node_modules/vite/bin/vite.js`, root, root));
});
test('plans only verified runners and signals their exact identities', () => {
const io = fixture();
const plan = planDevCleanup(root, [3000, 3001], io);
assert.equal(applyDevCleanup(root, plan, io), 2);
assert.deepEqual(io.signals, [42, 43]);
});
test('foreign listener, another checkout and unknown command abort before every signal', () => {
for (const change of [
{ listeners: () => [999] },
{ checkout: () => '/another-worktree' },
{ snapshot: () => new Map([[42, processEntry(42, 'node unrelated.js')]]) },
]) {
const io = Object.assign(fixture(), change);
assert.throws(() => planDevCleanup(root, [3000, 3001], io), /unverified/);
assert.deepEqual(io.signals, []);
}
});
test('PID reuse, changed command and changed cwd fail closed at signaling', () => {
for (const change of [
{ snapshot: () => new Map([[42, { ...vite, started: 'Thu Oct 22 20:01:00 2026' }]]) },
{ snapshot: () => new Map([[42, { ...vite, command: 'node unrelated.js' }]]) },
{ cwd: () => '/another-worktree/web' },
]) {
const io = fixture([vite]);
const plan = planDevCleanup(root, [3000], io);
Object.assign(io, change);
assert.throws(() => applyDevCleanup(root, plan, io), /changed identity or checkout/);
assert.deepEqual(io.signals, []);
}
});
test('stops verified parents before their source children and ignores exited PIDs', () => {
const io = fixture([
{ ...tsx, pid: 43 },
{ ...vite, parentPid: 43 },
]);
const plan = planDevCleanup(root, [3000, 3001], io);
assert.deepEqual(
plan.map((entry) => entry.pid),
[43, 42]
);
io.snapshot = () => new Map();
assert.equal(applyDevCleanup(root, plan, io), 0);
assert.deepEqual(io.signals, []);
});
test('parses process start identity and parent without printing command contents', () => {
const result = parseProcessSnapshot(
` 42 1 Thu Oct 22 20:00:00 2026 ${vite.command}\nmalformed row`
);
assert.deepEqual(result.get(42), vite);
assert.equal(result.size, 1);
});