#!/usr/bin/env node import assert from 'node:assert/strict'; import { mkdtempSync, mkdirSync, readFileSync, rmSync, writeFileSync, readdirSync } 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 REPO_SHIM = path.join(ROOT_DIR, 'scripts', 'long_task_watchdog.mjs'); const PACKAGE_ENTRY = path.join(ROOT_DIR, 'plugins', 'reporting-governance', 'scripts', 'long_task_watchdog.mjs'); function createFixtureRunner(scriptPath) { const fixtureRoot = mkdtempSync(path.join(tmpdir(), 'long-task-watchdog-test-')); const statePath = path.join(fixtureRoot, 'watchdog-state.json'); const evidenceDir = path.join(fixtureRoot, 'evidence'); const eventDir = path.join(fixtureRoot, 'events'); const notificationDir = path.join(fixtureRoot, 'notifications'); mkdirSync(evidenceDir, { recursive: true }); mkdirSync(eventDir, { recursive: true }); mkdirSync(notificationDir, { recursive: true }); function writeState(content) { const body = typeof content === 'string' ? content : JSON.stringify(content, null, 2); writeFileSync(statePath, body); return statePath; } function run(args = []) { const result = spawnSync( process.execPath, [ scriptPath, '--state', statePath, '--evidence-dir', evidenceDir, '--event-dir', eventDir, '--notification-dir', notificationDir, ...args, ], { cwd: ROOT_DIR, encoding: 'utf8', }, ); return { status: result.status, stdout: result.stdout ?? '', stderr: result.stderr ?? '', }; } function readState() { return JSON.parse(readFileSync(statePath, 'utf8')); } function listEvidence() { return readdirSync(evidenceDir).sort(); } function listEvents() { return readdirSync(eventDir).sort(); } function listNotifications() { return readdirSync(notificationDir).sort(); } function readJson(dirPath, fileName) { return JSON.parse(readFileSync(path.join(dirPath, fileName), 'utf8')); } function cleanup() { rmSync(fixtureRoot, { recursive: true, force: true }); } return { statePath, evidenceDir, eventDir, notificationDir, writeState, run, readState, listEvidence, listEvents, listNotifications, readJson, cleanup, }; } const tests = []; function test(name, fn) { tests.push({ name, fn }); } function printResult(prefix, name, detail = '') { process.stdout.write(`${prefix} ${name}${detail ? ` ${detail}` : ''}\n`); } function normalizePayload(payload) { return { ...payload, statePath: '', evidenceDir: '', eventDir: '', notificationDir: '', result: { ...payload.result, evidenceWrites: payload.result.evidenceWrites.map((item) => ({ ...item, path: '' })), eventWrites: payload.result.eventWrites.map((item) => ({ ...item, path: '', eventId: '' })), notificationWrites: payload.result.notificationWrites.map((item) => ({ ...item, path: '', notificationId: '' })), }, }; } test('repo shim matches package entrypoint for same overdue watchdog payload', () => { const shim = createFixtureRunner(REPO_SHIM); const pkg = createFixtureRunner(PACKAGE_ENTRY); const state = { version: 1, watchdogs: [ { id: 'reporting-governance-plugin-watchdog', task: 'reporting-governance plugin spec development', status: 'active', ownerSessionKey: 'agent:coder:main', reportChannel: 'telegram', reportTarget: '864811879', intervalMinutes: 10, lastMilestoneAt: '2026-05-07T08:00:00.000Z', lastAlertAt: null, }, ], }; try { shim.writeState(state); pkg.writeState(state); const shimResult = shim.run(['--compact', '--write-state', '--now', '2026-05-07T08:20:00.000Z']); const pkgResult = pkg.run(['--compact', '--write-state', '--now', '2026-05-07T08:20:00.000Z']); assert.equal(shimResult.status, 0, shimResult.stderr); assert.equal(pkgResult.status, 0, pkgResult.stderr); assert.deepEqual(normalizePayload(JSON.parse(shimResult.stdout)), normalizePayload(JSON.parse(pkgResult.stdout))); assert.deepEqual(shim.readState(), pkg.readState()); } finally { shim.cleanup(); pkg.cleanup(); } }); test('inactive watchdogs do not emit evidence, event, or notification queue items', () => { const runner = createFixtureRunner(PACKAGE_ENTRY); try { runner.writeState({ version: 1, watchdogs: [ { id: 'paused-watchdog', task: 'paused task', status: 'paused', intervalMinutes: 10, lastMilestoneAt: '2026-05-07T08:00:00.000Z', }, ], }); const result = runner.run(['--compact', '--now', '2026-05-07T08:20:00.000Z']); assert.equal(result.status, 0, result.stderr); const payload = JSON.parse(result.stdout); assert.equal(payload.result.emittedCount, 0); assert.equal(payload.result.eventCount, 0); assert.equal(payload.result.notificationCount, 0); assert.deepEqual(runner.listEvidence(), []); assert.deepEqual(runner.listEvents(), []); assert.deepEqual(runner.listNotifications(), []); } finally { runner.cleanup(); } }); test('overdue active watchdog emits evidence, canonical event, notification queue item, and updates lastAlertAt', () => { const runner = createFixtureRunner(PACKAGE_ENTRY); try { runner.writeState({ version: 1, watchdogs: [ { id: 'reporting-governance-plugin-watchdog', task: 'reporting-governance plugin spec development', status: 'active', ownerSessionKey: 'agent:coder:main', reportChannel: 'telegram', reportTarget: '864811879', intervalMinutes: 10, lastMilestoneAt: '2026-05-07T08:00:00.000Z', lastAlertAt: null, }, ], }); const result = runner.run(['--compact', '--write-state', '--now', '2026-05-07T08:20:00.000Z']); assert.equal(result.status, 0, result.stderr); const payload = JSON.parse(result.stdout); assert.equal(payload.result.emittedCount, 1); assert.equal(payload.result.eventCount, 1); assert.equal(payload.result.notificationCount, 1); const evidenceFiles = runner.listEvidence(); const eventFiles = runner.listEvents(); const notificationFiles = runner.listNotifications(); assert.equal(evidenceFiles.length, 1); assert.equal(eventFiles.length, 1); assert.equal(notificationFiles.length, 1); const eventPayload = runner.readJson(runner.eventDir, eventFiles[0]); assert.equal(eventPayload.event_type, 'watchdog_fired'); assert.equal(eventPayload.operator_context.channel, 'telegram'); assert.equal(eventPayload.operator_context.operator_id, '864811879'); const notificationPayload = runner.readJson(runner.notificationDir, notificationFiles[0]); assert.equal(notificationPayload.kind, 'notify_operator'); assert.equal(notificationPayload.status, 'pending'); assert.equal(notificationPayload.operator_notice.channel, 'telegram'); assert.equal(notificationPayload.operator_notice.target, '864811879'); assert.equal(notificationPayload.blocked_gap, null); assert.equal(notificationPayload.dispatch_hint.tool, 'message.send'); const nextState = runner.readState(); assert.equal(nextState.watchdogs[0].lastAlertAt, '2026-05-07T08:20:00.000Z'); assert.equal(nextState.watchdogs[0].lastNudgeAt, '2026-05-07T08:20:00.000Z'); } finally { runner.cleanup(); } }); test('same interval is not alerted twice once lastAlertAt covers the overdue window', () => { const runner = createFixtureRunner(PACKAGE_ENTRY); try { runner.writeState({ version: 1, watchdogs: [ { id: 'reporting-governance-plugin-watchdog', task: 'reporting-governance plugin spec development', status: 'active', intervalMinutes: 10, lastMilestoneAt: '2026-05-07T08:00:00.000Z', lastAlertAt: '2026-05-07T08:12:00.000Z', }, ], }); const result = runner.run(['--compact', '--write-state', '--now', '2026-05-07T08:15:00.000Z']); assert.equal(result.status, 0, result.stderr); const payload = JSON.parse(result.stdout); assert.equal(payload.result.emittedCount, 0); assert.equal(payload.result.eventCount, 0); assert.equal(payload.result.notificationCount, 0); assert.deepEqual(runner.listEvidence(), []); assert.deepEqual(runner.listEvents(), []); assert.deepEqual(runner.listNotifications(), []); } finally { runner.cleanup(); } }); test('notification queue item records blocked gap when report channel/target is missing', () => { const runner = createFixtureRunner(PACKAGE_ENTRY); try { runner.writeState({ version: 1, watchdogs: [ { id: 'missing-target-watchdog', task: 'targetless task', status: 'active', ownerSessionKey: 'agent:coder:main', intervalMinutes: 10, lastMilestoneAt: '2026-05-07T08:00:00.000Z', lastAlertAt: null, }, ], }); const result = runner.run(['--compact', '--now', '2026-05-07T08:20:00.000Z']); assert.equal(result.status, 0, result.stderr); const payload = JSON.parse(result.stdout); assert.equal(payload.result.notificationCount, 1); assert.equal(payload.result.notificationWrites[0].dispatchReady, false); const notificationFiles = runner.listNotifications(); const notificationPayload = runner.readJson(runner.notificationDir, notificationFiles[0]); assert.match(notificationPayload.blocked_gap, /reportChannel\/reportTarget/); } finally { runner.cleanup(); } }); let failures = 0; for (const { name, fn } of tests) { try { fn(); printResult('ok', name); } catch (error) { failures += 1; printResult('not ok', name, `- ${error instanceof Error ? error.message : String(error)}`); } } if (failures > 0) { process.exit(1); }