160 lines
4.7 KiB
JavaScript
Executable File
160 lines
4.7 KiB
JavaScript
Executable File
#!/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 WATCHDOG_SCRIPT = path.join(ROOT_DIR, 'scripts', 'long_task_watchdog.mjs');
|
|
|
|
function createFixtureRunner() {
|
|
const fixtureRoot = mkdtempSync(path.join(tmpdir(), 'long-task-watchdog-test-'));
|
|
const statePath = path.join(fixtureRoot, 'watchdog-state.json');
|
|
const evidenceDir = path.join(fixtureRoot, 'evidence');
|
|
mkdirSync(evidenceDir, { 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, [WATCHDOG_SCRIPT, '--state', statePath, '--evidence-dir', evidenceDir, ...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 cleanup() {
|
|
rmSync(fixtureRoot, { recursive: true, force: true });
|
|
}
|
|
|
|
return { statePath, evidenceDir, writeState, run, readState, listEvidence, cleanup };
|
|
}
|
|
|
|
const tests = [];
|
|
function test(name, fn) { tests.push({ name, fn }); }
|
|
|
|
function printResult(prefix, name, detail = '') {
|
|
process.stdout.write(`${prefix} ${name}${detail ? ` ${detail}` : ''}\n`);
|
|
}
|
|
|
|
test('inactive watchdogs do not emit evidence', () => {
|
|
const runner = createFixtureRunner();
|
|
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.deepEqual(runner.listEvidence(), []);
|
|
} finally {
|
|
runner.cleanup();
|
|
}
|
|
});
|
|
|
|
test('overdue active watchdog emits external evidence and updates lastAlertAt when write-state is enabled', () => {
|
|
const runner = createFixtureRunner();
|
|
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);
|
|
const evidenceFiles = runner.listEvidence();
|
|
assert.equal(evidenceFiles.length, 1);
|
|
|
|
const nextState = runner.readState();
|
|
assert.equal(nextState.watchdogs[0].lastAlertAt, '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();
|
|
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.deepEqual(runner.listEvidence(), []);
|
|
} 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);
|
|
}
|