#!/usr/bin/env node import assert from 'node:assert/strict'; import { mkdtempSync, rmSync, writeFileSync } from 'node:fs'; import { tmpdir } from 'node:os'; import path from 'node:path'; import process from 'node:process'; import { spawnSync } from 'node:child_process'; const ROOT_DIR = path.resolve(import.meta.dirname, '..'); const WATCHDOG_SCRIPT = path.join(ROOT_DIR, 'scripts', 'subagent_delivery_watchdog.mjs'); function createFixtureRunner() { const fixtureRoot = mkdtempSync(path.join(tmpdir(), 'subagent-watchdog-test-')); function writeFixture(name, content) { const fixturePath = path.join(fixtureRoot, name); const body = typeof content === 'string' ? content : JSON.stringify(content, null, 2); writeFileSync(fixturePath, body); return fixturePath; } function runWatchdog(args = [], options = {}) { const result = spawnSync(process.execPath, [WATCHDOG_SCRIPT, ...args], { cwd: ROOT_DIR, encoding: 'utf8', ...options, }); return { status: result.status, signal: result.signal, stdout: result.stdout ?? '', stderr: result.stderr ?? '', error: result.error ?? null, }; } function cleanup() { rmSync(fixtureRoot, { recursive: true, force: true }); } return { fixtureRoot, writeFixture, runWatchdog, cleanup, }; } const tests = []; function test(name, fn) { tests.push({ name, fn }); } function printResult(prefix, name, detail = '') { const suffix = detail ? ` ${detail}` : ''; process.stdout.write(`${prefix} ${name}${suffix}\n`); } test('fixture runner can invoke watchdog skeleton with a generated input file', () => { const runner = createFixtureRunner(); try { const inputPath = runner.writeFixture('dispatch.json', { runId: 'fixture-run-001', childSessionKey: 'session:test', }); const result = runner.runWatchdog(['--compact', '--input', inputPath]); assert.equal(result.status, 0, `expected zero exit status, got ${result.status}\n${result.stderr}`); assert.equal(result.stderr, ''); const payload = JSON.parse(result.stdout); assert.equal(payload.ok, true); assert.equal(payload.tool, 'subagent_delivery_watchdog'); assert.equal(payload.result.status, 'not_implemented'); assert.equal(payload.input.path, inputPath); assert.equal(payload.input.exists, true); } finally { runner.cleanup(); } }); test('watchdog reports active before SLA when dispatch exists and no completion receipt has arrived yet', () => { const runner = createFixtureRunner(); try { const inputPath = runner.writeFixture('dispatch-before-sla.json', { runId: 'fixture-run-active-before-sla', childSessionKey: 'session:active-before-sla', dispatchAt: '2026-04-24T10:00:00.000Z', expectedBy: '2026-04-24T10:10:00.000Z', currentTime: '2026-04-24T10:05:00.000Z', }); const result = runner.runWatchdog(['--compact', '--input', inputPath]); assert.equal(result.status, 0, `expected zero exit status, got ${result.status} ${result.stderr}`); assert.equal(result.stderr, ''); const payload = JSON.parse(result.stdout); assert.equal(payload.ok, true); assert.equal(payload.input.path, inputPath); assert.equal(payload.input.exists, true); assert.equal(payload.result.status, 'active'); } finally { runner.cleanup(); } }); test('watchdog reports suspect delivery failure after SLA when dispatch exists and no completion receipt has arrived yet', () => { const runner = createFixtureRunner(); try { const inputPath = runner.writeFixture('dispatch-beyond-sla.json', { runId: 'fixture-run-suspect-delivery-failure', childSessionKey: 'session:suspect-delivery-failure', dispatchAt: '2026-04-24T10:00:00.000Z', expectedBy: '2026-04-24T10:10:00.000Z', currentTime: '2026-04-24T10:15:00.000Z', }); const result = runner.runWatchdog(['--compact', '--input', inputPath]); assert.equal(result.status, 0, `expected zero exit status, got ${result.status} ${result.stderr}`); assert.equal(result.stderr, ''); const payload = JSON.parse(result.stdout); assert.equal(payload.ok, true); assert.equal(payload.input.path, inputPath); assert.equal(payload.input.exists, true); assert.equal(payload.result.status, 'suspect_delivery_failure'); } finally { runner.cleanup(); } }); test('watchdog reports completed when dispatch exists and completion receipt has arrived', () => { const runner = createFixtureRunner(); try { const inputPath = runner.writeFixture('dispatch-completed.json', { runId: 'fixture-run-completed', childSessionKey: 'session:completed', dispatchAt: '2026-04-24T10:00:00.000Z', expectedBy: '2026-04-24T10:10:00.000Z', currentTime: '2026-04-24T10:05:00.000Z', completionReceiptAt: '2026-04-24T10:04:00.000Z', }); const result = runner.runWatchdog(['--compact', '--input', inputPath]); assert.equal(result.status, 0, `expected zero exit status, got ${result.status} ${result.stderr}`); assert.equal(result.stderr, ''); const payload = JSON.parse(result.stdout); assert.equal(payload.ok, true); assert.equal(payload.input.path, inputPath); assert.equal(payload.input.exists, true); assert.equal(payload.result.status, 'completed'); } finally { runner.cleanup(); } }); test('watchdog reports done but not forwarded when child run is marked done without a main-thread completion receipt', () => { const runner = createFixtureRunner(); try { const inputPath = runner.writeFixture('dispatch-done-not-forwarded.json', { runId: 'fixture-run-done-not-forwarded', childSessionKey: 'session:done-not-forwarded', dispatchAt: '2026-04-24T10:00:00.000Z', expectedBy: '2026-04-24T10:10:00.000Z', currentTime: '2026-04-24T10:05:00.000Z', childRunStatus: 'done', }); const result = runner.runWatchdog(['--compact', '--input', inputPath]); assert.equal(result.status, 0, `expected zero exit status, got ${result.status} ${result.stderr}`); assert.equal(result.stderr, ''); const payload = JSON.parse(result.stdout); assert.equal(payload.ok, true); assert.equal(payload.input.path, inputPath); assert.equal(payload.input.exists, true); assert.equal(payload.result.status, 'done_but_not_forwarded'); } finally { runner.cleanup(); } }); test('fixture runner exposes missing-input behavior for future fail-first cases', () => { const runner = createFixtureRunner(); try { const missingPath = path.join(runner.fixtureRoot, 'missing.json'); const result = runner.runWatchdog(['--compact', '--input', missingPath]); assert.equal(result.status, 0, `expected zero exit status, got ${result.status}\n${result.stderr}`); const payload = JSON.parse(result.stdout); assert.equal(payload.ok, true); assert.equal(payload.input.path, missingPath); assert.equal(payload.input.exists, false); assert.equal(payload.result.status, 'not_implemented'); } finally { runner.cleanup(); } }); function main() { let passed = 0; for (const { name, fn } of tests) { try { fn(); passed += 1; printResult('PASS', name); } catch (error) { printResult('FAIL', name, error instanceof Error ? `- ${error.message}` : `- ${String(error)}`); if (error instanceof Error && error.stack) { process.stderr.write(`${error.stack}\n`); } process.exitCode = 1; } } const failed = tests.length - passed; process.stdout.write(`\nSummary: ${passed} passed, ${failed} failed, ${tests.length} total\n`); } main();