From c0cfd5727ee98a934f4b03e95404b42c2302df5f Mon Sep 17 00:00:00 2001 From: haelyra <49814733+haelyra@users.noreply.github.com> Date: Thu, 19 Nov 2026 16:25:03 -0500 Subject: [PATCH] test: separate missing hook targets from stdin pipe failures --- tests/hooks/plugin-hook-bootstrap.test.js | 47 ++++++++++++++++++--- tests/lib/helpers/plugin-bootstrap-epipe.js | 19 +++++++++ 2 files changed, 60 insertions(+), 6 deletions(-) create mode 100644 tests/lib/helpers/plugin-bootstrap-epipe.js diff --git a/tests/hooks/plugin-hook-bootstrap.test.js b/tests/hooks/plugin-hook-bootstrap.test.js index 55d1af505..e6f84e155 100644 --- a/tests/hooks/plugin-hook-bootstrap.test.js +++ b/tests/hooks/plugin-hook-bootstrap.test.js @@ -32,7 +32,7 @@ function writeFile(root, relativePath, content) { } function run(args = [], options = {}) { - return spawnSync(process.execPath, [SCRIPT, ...args], { + return spawnSync(process.execPath, [...(options.nodeArgs || []), SCRIPT, ...args], { input: options.input || '', encoding: 'utf8', env: { @@ -46,6 +46,18 @@ function run(args = [], options = {}) { }); } +function isolatedBootstrapEnv(root) { + return { + HOME: root, USERPROFILE: root, ECC_AGENT_DATA_HOME: path.join(root, 'agent-data'), + QODER_PLUGIN_ROOT: '', ECC_HOOK_DEADLINE_MS: '', NODE_OPTIONS: '', + }; +} + +function spawnDiagnostic(result) { + return JSON.stringify({ status: result.status, signal: result.signal, + error: result.error?.code || null, stderr: result.stderr }); +} + function test(name, fn) { try { fn(); @@ -342,13 +354,36 @@ process.exit(7); const root = createTempDir(); try { const result = run(['node', path.join('scripts', 'missing.js')], { - root, - input: 'raw-input', + root, cwd: root, + // No stdin is needed to observe MODULE_NOT_FOUND. Sending input can + // race the child's early exit and legitimately produce EPIPE instead. + input: '', + env: isolatedBootstrapEnv(root), }); + const diagnostic = spawnDiagnostic(result); + assert.strictEqual(result.error, undefined, diagnostic); + assert.strictEqual(result.status, 1, diagnostic); + assert.strictEqual(result.stdout, '', diagnostic); + assert.ok(result.stderr.includes('Cannot find module'), diagnostic); + } finally { + cleanup(root); + } + })) passed++; else failed++; - assert.strictEqual(result.status, 1); - assert.strictEqual(result.stdout, ''); - assert.ok(result.stderr.includes('Cannot find module')); + if (test('a child that exits 1 with an input EPIPE retains fail-open execution diagnostics', () => { + const root = createTempDir(); + try { + const result = run(['node', path.join('scripts', 'missing.js')], { + root, cwd: root, input: 'raw-input', env: isolatedBootstrapEnv(root), + nodeArgs: ['--require', path.join(__dirname, '../lib/helpers/plugin-bootstrap-epipe.js')], + }); + const diagnostic = spawnDiagnostic(result); + assert.strictEqual(result.error, undefined, diagnostic); + assert.strictEqual(result.status, 0, diagnostic); + assert.strictEqual(result.stdout, '', diagnostic); + assert.ok(result.stderr.includes('[TestSpawn] child exited 1 with EPIPE'), diagnostic); + assert.ok(result.stderr.includes('Cannot find module'), diagnostic); + assert.ok(result.stderr.includes('bootstrap execution failed: spawnSync node EPIPE'), diagnostic); } finally { cleanup(root); } diff --git a/tests/lib/helpers/plugin-bootstrap-epipe.js b/tests/lib/helpers/plugin-bootstrap-epipe.js new file mode 100644 index 000000000..0a2ed2085 --- /dev/null +++ b/tests/lib/helpers/plugin-bootstrap-epipe.js @@ -0,0 +1,19 @@ +'use strict'; + +// Isolate the child-result contract from an OS scheduling race: a missing +// target can exit 1 while closing stdin before the bootstrap finishes writing. +const assert = require('assert'); +const path = require('path'); +const childProcess = require('child_process'); +const target = path.resolve(process.env.CLAUDE_PLUGIN_ROOT, 'scripts', 'missing.js'); + +childProcess.spawnSync = (command, args) => { + assert.strictEqual(command, process.execPath); + assert.deepStrictEqual(args, [target]); + process.stderr.write('[TestSpawn] child exited 1 with EPIPE\n'); + return { + status: 1, signal: null, stdout: Buffer.alloc(0), + stderr: Buffer.from("Error: Cannot find module 'missing fixture'\n"), + error: Object.assign(new Error('spawnSync node EPIPE'), { code: 'EPIPE' }), + }; +};