fix(tests): the suite no longer hands the session id to the lock CLI

npm test now preloads tests/helpers/no-session-id.mjs, which deletes CLAUDE_CODE_SESSION_ID before any test file loads, and the 18 files that spread process.env into a child use childEnv() instead (23 sites). A live session's stop guard is no longer pointed at a temp-repo lock (S133).

engines stays >=18: --import exists from Node 18.18/20.6, and the quoted test glob already needs Node 21 (criteria-runner NODE_GLOB_MAJOR), so the real floor does not move.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Kjell Tore Guttormsen 2026-10-12 14:09:14 +02:00
commit d8527976ef
Signed by: ktg
SSH key fingerprint: SHA256:JakMjO6FTBBzN0Bhfj9saOoEjaFxlSdYuZQQpM/lF9Q
22 changed files with 285 additions and 24 deletions

View file

@ -0,0 +1,20 @@
// tests/helpers/child-env.mjs
// The environment a test gives a child process. The lock CLI
// (lib/util/test-lock.mjs) writes a session pointer keyed by
// CLAUDE_CODE_SESSION_ID; a child that inherits the id of the session running
// the tests writes a pointer for that live session (S133). So the id never
// reaches a child unless the test sets one itself. Same shape as
// lib/util/command-env.mjs. tests/lib/session-id-isolation.test.mjs checks that
// no other file under tests/ hands the process environment to a child.
/**
* A copy of process.env without CLAUDE_CODE_SESSION_ID, with `overrides`
* applied last (a test that sets its own id keeps it).
*
* @param {Record<string, string|undefined>} [overrides] - vars to set or replace
* @returns {Record<string, string|undefined>} the child's environment
*/
export function childEnv(overrides = {}) {
const { CLAUDE_CODE_SESSION_ID, ...rest } = process.env;
return { ...rest, ...overrides };
}

View file

@ -6,6 +6,7 @@
// Provenance: borrowed within the same marketplace (same author, MIT).
import { execFile } from 'node:child_process';
import { childEnv } from './child-env.mjs';
// How long a hook may run before the helper kills it. The tests check a
// hook's answer, not its speed (SC #14 times itself, tests/helpers/timing.mjs),
@ -37,7 +38,7 @@ export function runHook(scriptPath, input) {
*/
export function runHookWithEnv(scriptPath, input, envOverrides, opts = {}) {
return new Promise((resolve) => {
const env = { ...process.env, ...envOverrides };
const env = childEnv({ ...envOverrides });
const child = execFile(
'node',
[scriptPath],

View file

@ -0,0 +1,6 @@
// tests/helpers/no-session-id.mjs
// Preloaded by `npm test` (node --import). The lock CLI (lib/util/test-lock.mjs)
// writes a session pointer keyed by CLAUDE_CODE_SESSION_ID, and the stop guard
// reads it; a suite run inside a Claude Code session must not write one for
// that live session (S133). The id is removed before any test file starts.
delete process.env.CLAUDE_CODE_SESSION_ID;