From cf83444a9d341ae362e65a9f995100c69176887c Mon Sep 17 00:00:00 2001 From: Peter Stone Date: Thu, 5 Mar 2026 18:51:50 +0000 Subject: Rescue work from claudomator-work: question/answer, ratelimit, start-next-task Merges features developed in /site/doot.terst.org/claudomator-work (a stale clone) into the canonical repo: - executor: QuestionRegistry for human-in-the-loop answers, rate limit detection and exponential backoff retry (ratelimit.go, question.go) - executor/claude.go: process group isolation (SIGKILL orphans on cancel), os.Pipe for reliable stdout drain, backoff retry on rate limits - api/scripts.go: POST /api/scripts/start-next-task handler - api/server.go: startNextTaskScript field, answer-question route, BroadcastQuestion for WebSocket question events - web: Cancel/Restart buttons, question banner UI, log viewer, validate section, WebSocket auto-connect All tests pass. Co-Authored-By: Claude Sonnet 4.6 --- web/test/start-next-task.test.mjs | 84 +++++++++++++++++++++++++++++++++++++++ web/test/task-actions.test.mjs | 53 ++++++++++++++++++++++++ 2 files changed, 137 insertions(+) create mode 100644 web/test/start-next-task.test.mjs create mode 100644 web/test/task-actions.test.mjs (limited to 'web/test') diff --git a/web/test/start-next-task.test.mjs b/web/test/start-next-task.test.mjs new file mode 100644 index 0000000..6863f7e --- /dev/null +++ b/web/test/start-next-task.test.mjs @@ -0,0 +1,84 @@ +// start-next-task.test.mjs — contract tests for startNextTask fetch helper +// Run: node --test web/test/start-next-task.test.mjs + +import { describe, it } from 'node:test'; +import assert from 'node:assert/strict'; + +// ── Contract: startNextTask(basePath, fetchFn) ───────────────────────────────── +// POSTs to ${basePath}/api/scripts/start-next-task +// Returns {output, exit_code} on HTTP 2xx +// Throws on HTTP error + +async function startNextTask(basePath, fetchFn) { + const res = await fetchFn(`${basePath}/api/scripts/start-next-task`, { method: 'POST' }); + if (!res.ok) { + let msg = `HTTP ${res.status}`; + try { const body = await res.json(); msg = body.error || msg; } catch {} + throw new Error(msg); + } + return res.json(); +} + +describe('startNextTask', () => { + it('POSTs to /api/scripts/start-next-task', async () => { + let captured = null; + const mockFetch = (url, opts) => { + captured = { url, opts }; + return Promise.resolve({ + ok: true, + json: () => Promise.resolve({ output: 'claudomator start abc-123\n', exit_code: 0 }), + }); + }; + + await startNextTask('http://localhost:8484', mockFetch); + assert.equal(captured.url, 'http://localhost:8484/api/scripts/start-next-task'); + assert.equal(captured.opts.method, 'POST'); + }); + + it('returns output and exit_code on success', async () => { + const mockFetch = () => Promise.resolve({ + ok: true, + json: () => Promise.resolve({ output: 'claudomator start abc-123\n', exit_code: 0 }), + }); + + const result = await startNextTask('', mockFetch); + assert.equal(result.output, 'claudomator start abc-123\n'); + assert.equal(result.exit_code, 0); + }); + + it('returns output when no task available', async () => { + const mockFetch = () => Promise.resolve({ + ok: true, + json: () => Promise.resolve({ output: 'No task to start.\n', exit_code: 0 }), + }); + + const result = await startNextTask('', mockFetch); + assert.equal(result.output, 'No task to start.\n'); + }); + + it('throws with server error message on HTTP error', async () => { + const mockFetch = () => Promise.resolve({ + ok: false, + status: 500, + json: () => Promise.resolve({ error: 'script execution failed' }), + }); + + await assert.rejects( + () => startNextTask('', mockFetch), + /script execution failed/, + ); + }); + + it('throws with HTTP status on non-JSON error response', async () => { + const mockFetch = () => Promise.resolve({ + ok: false, + status: 503, + json: () => Promise.reject(new Error('not json')), + }); + + await assert.rejects( + () => startNextTask('', mockFetch), + /HTTP 503/, + ); + }); +}); diff --git a/web/test/task-actions.test.mjs b/web/test/task-actions.test.mjs new file mode 100644 index 0000000..f2c21c4 --- /dev/null +++ b/web/test/task-actions.test.mjs @@ -0,0 +1,53 @@ +// task-actions.test.mjs — button visibility logic for Cancel/Restart actions +// +// Run with: node --test web/test/task-actions.test.mjs + +import { describe, it } from 'node:test'; +import assert from 'node:assert/strict'; + +// ── Logic under test ────────────────────────────────────────────────────────── + +const RESTART_STATES = new Set(['FAILED', 'TIMED_OUT', 'CANCELLED']); + +function getCardAction(state) { + if (state === 'PENDING') return 'run'; + if (state === 'RUNNING') return 'cancel'; + if (RESTART_STATES.has(state)) return 'restart'; + return null; +} + +// ── Tests ───────────────────────────────────────────────────────────────────── + +describe('task card action buttons', () => { + it('shows Run button for PENDING', () => { + assert.equal(getCardAction('PENDING'), 'run'); + }); + + it('shows Cancel button for RUNNING', () => { + assert.equal(getCardAction('RUNNING'), 'cancel'); + }); + + it('shows Restart button for FAILED', () => { + assert.equal(getCardAction('FAILED'), 'restart'); + }); + + it('shows Restart button for TIMED_OUT', () => { + assert.equal(getCardAction('TIMED_OUT'), 'restart'); + }); + + it('shows Restart button for CANCELLED', () => { + assert.equal(getCardAction('CANCELLED'), 'restart'); + }); + + it('shows no button for COMPLETED', () => { + assert.equal(getCardAction('COMPLETED'), null); + }); + + it('shows no button for QUEUED', () => { + assert.equal(getCardAction('QUEUED'), null); + }); + + it('shows no button for BUDGET_EXCEEDED', () => { + assert.equal(getCardAction('BUDGET_EXCEEDED'), null); + }); +}); -- cgit v1.2.3