|
| 1 | +// GH #110: regression tests for the agent-device-wrapper test-seam fuse. |
| 2 | +// |
| 3 | +// The fuse is process-global by design (Codex review conf 90/90/95 — |
| 4 | +// adding a reset seam would defeat the guarantee, since any code that |
| 5 | +// could call reset is the same code that could leak the override). Each |
| 6 | +// scenario therefore runs in a freshly-spawned Node subprocess so the |
| 7 | +// fuse state is isolated. |
| 8 | +import { test } from 'node:test'; |
| 9 | +import assert from 'node:assert/strict'; |
| 10 | +import { spawnSync } from 'node:child_process'; |
| 11 | +import { fileURLToPath } from 'node:url'; |
| 12 | +import { dirname, resolve } from 'node:path'; |
| 13 | + |
| 14 | +const __dirname = dirname(fileURLToPath(import.meta.url)); |
| 15 | +const MOD_ABS_PATH = resolve(__dirname, '../../dist/agent-device-wrapper.js'); |
| 16 | + |
| 17 | +function runScenario(scenarioCode) { |
| 18 | + // Use the modern dynamic-import form to avoid CJS/ESM confusion. |
| 19 | + const wrapped = ` |
| 20 | + (async () => { |
| 21 | + try { |
| 22 | + const mod = await import(${JSON.stringify(MOD_ABS_PATH)}); |
| 23 | + ${scenarioCode} |
| 24 | + console.log('SCENARIO_OK'); |
| 25 | + } catch (e) { |
| 26 | + console.log('SCENARIO_THREW:' + (e && e.message ? e.message : String(e))); |
| 27 | + } |
| 28 | + })().catch(e => { console.log('SCENARIO_REJECTED:' + (e && e.message ? e.message : String(e))); process.exit(1); }); |
| 29 | + `; |
| 30 | + const result = spawnSync('node', ['--input-type=module', '-e', wrapped], { |
| 31 | + encoding: 'utf-8', |
| 32 | + timeout: 20_000, |
| 33 | + }); |
| 34 | + return { stdout: result.stdout ?? '', stderr: result.stderr ?? '', status: result.status }; |
| 35 | +} |
| 36 | + |
| 37 | +test('fuse: override fn is honored when set before any production dispatch', () => { |
| 38 | + const { stdout } = runScenario(` |
| 39 | + let captured = null; |
| 40 | + mod._setRunAgentDeviceForTest(async (args, opts) => { |
| 41 | + captured = { args, opts }; |
| 42 | + return { content: [{ type: 'text', text: '{"ok":true,"data":"stubbed"}' }] }; |
| 43 | + }); |
| 44 | + const r = await mod.runAgentDevice(['snapshot'], {}); |
| 45 | + if (!r.content[0].text.includes('stubbed')) throw new Error('override not invoked'); |
| 46 | + if (captured.args[0] !== 'snapshot') throw new Error('args not threaded through'); |
| 47 | + `); |
| 48 | + assert.match(stdout, /SCENARIO_OK/, `expected SCENARIO_OK, got: ${stdout}`); |
| 49 | +}); |
| 50 | + |
| 51 | +test('fuse: setting override to null re-enables production tier as long as fuse has not blown', () => { |
| 52 | + const { stdout } = runScenario(` |
| 53 | + let called = 0; |
| 54 | + mod._setRunAgentDeviceForTest(async () => { called++; return { content: [{ type: 'text', text: '{"ok":true}' }] }; }); |
| 55 | + await mod.runAgentDevice(['snapshot'], {}); |
| 56 | + if (called !== 1) throw new Error('override not invoked on first call'); |
| 57 | +
|
| 58 | + // Set back to null — fuse has NOT blown (no production dispatch occurred). |
| 59 | + mod._setRunAgentDeviceForTest(null); |
| 60 | + // Subsequent installs should still work |
| 61 | + let called2 = 0; |
| 62 | + mod._setRunAgentDeviceForTest(async () => { called2++; return { content: [{ type: 'text', text: '{"ok":true}' }] }; }); |
| 63 | + await mod.runAgentDevice(['tap'], {}); |
| 64 | + if (called2 !== 1) throw new Error('second override not invoked'); |
| 65 | + `); |
| 66 | + assert.match(stdout, /SCENARIO_OK/, `expected SCENARIO_OK, got: ${stdout}`); |
| 67 | +}); |
| 68 | + |
| 69 | +test('fuse: a production runAgentDevice call (no override) blows the fuse', () => { |
| 70 | + // The production tiers will fail (no booted device / fast-runner) but |
| 71 | + // the fuse must still be sealed regardless of the dispatch outcome. |
| 72 | + const { stdout } = runScenario(` |
| 73 | + // Direct call with no override installed → production dispatch |
| 74 | + // begins → fuse blows immediately (before any tier returns). |
| 75 | + let prodThrew = false; |
| 76 | + try { |
| 77 | + await mod.runAgentDevice(['list-devices'], { skipSession: true }); |
| 78 | + } catch { |
| 79 | + prodThrew = true; |
| 80 | + } |
| 81 | + // Whether the production tier returned cleanly or threw, the fuse |
| 82 | + // is locked. Attempting to install an override now must throw. |
| 83 | + let fuseThrew = false; |
| 84 | + try { |
| 85 | + mod._setRunAgentDeviceForTest(async () => ({ content: [{ type: 'text', text: '{"ok":true}' }] })); |
| 86 | + } catch (e) { |
| 87 | + fuseThrew = true; |
| 88 | + if (!String(e.message).includes('blown fuse')) throw new Error('wrong error: ' + e.message); |
| 89 | + if (!String(e.message).includes('list-devices')) throw new Error('error should mention the trigger cliArgs[0]'); |
| 90 | + } |
| 91 | + if (!fuseThrew) throw new Error('expected fuse to throw on re-arm'); |
| 92 | + `); |
| 93 | + assert.match(stdout, /SCENARIO_OK/, `expected SCENARIO_OK, got: ${stdout}`); |
| 94 | +}); |
| 95 | + |
| 96 | +test('fuse: error message includes GH #110 reference and remediation hint', () => { |
| 97 | + const { stdout, stderr, status } = runScenario(` |
| 98 | + // Production tier may throw (no booted device in test env) — that's |
| 99 | + // fine, the fuse must still seal. Use list-devices because it dodges |
| 100 | + // the slow fast-runner path that snapshot would take. |
| 101 | + try { await mod.runAgentDevice(['list-devices'], { skipSession: true }); } catch {} |
| 102 | + let threw = false; |
| 103 | + try { |
| 104 | + mod._setRunAgentDeviceForTest(null); |
| 105 | + } catch (e) { |
| 106 | + threw = true; |
| 107 | + if (!String(e.message).includes('GH #110')) throw new Error('error must reference GH #110'); |
| 108 | + if (!String(e.message).includes('--test-isolation=process')) throw new Error('error must include remediation hint'); |
| 109 | + console.log('GOOD_ERROR'); |
| 110 | + } |
| 111 | + if (!threw) throw new Error('expected fuse to throw on re-arm'); |
| 112 | + `); |
| 113 | + assert.match(stdout, /GOOD_ERROR[\s\S]*SCENARIO_OK/, `expected GOOD_ERROR then SCENARIO_OK\nstdout: ${stdout}\nstderr: ${stderr}\nstatus: ${status}`); |
| 114 | +}); |
| 115 | + |
| 116 | +test('fuse: setting null to clear override does not block when fuse has NOT blown', () => { |
| 117 | + // Edge: install an override, run it, then clear it back to null |
| 118 | + // BEFORE any production dispatch. This is the normal afterEach |
| 119 | + // cleanup case — must not throw. |
| 120 | + const { stdout } = runScenario(` |
| 121 | + mod._setRunAgentDeviceForTest(async () => ({ content: [{ type: 'text', text: '{"ok":true}' }] })); |
| 122 | + await mod.runAgentDevice(['snapshot'], {}); |
| 123 | + // Standard cleanup — must not throw because no production dispatch happened. |
| 124 | + mod._setRunAgentDeviceForTest(null); |
| 125 | + // Installing a new override after clean cleanup must still work. |
| 126 | + mod._setRunAgentDeviceForTest(async () => ({ content: [{ type: 'text', text: '{"ok":true,"data":"second"}' }] })); |
| 127 | + const r = await mod.runAgentDevice(['snapshot'], {}); |
| 128 | + if (!r.content[0].text.includes('second')) throw new Error('second override not active'); |
| 129 | + `); |
| 130 | + assert.match(stdout, /SCENARIO_OK/, `expected SCENARIO_OK, got: ${stdout}`); |
| 131 | +}); |
0 commit comments