// task-actions.test.mjs — button visibility logic for Cancel/Restart/Resume 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 ────────────────────────────────────────────────────────── // // Interrupted states (CANCELLED, FAILED, BUDGET_EXCEEDED) get BOTH a Resume // button and a Restart button. TIMED_OUT gets Resume only. const RESUME_STATES = new Set(['TIMED_OUT', 'CANCELLED', 'FAILED', 'BUDGET_EXCEEDED']); const RESTART_STATES = new Set(['CANCELLED', 'FAILED', 'BUDGET_EXCEEDED']); function getCardActions(state) { const actions = []; if (state === 'PENDING') return ['run']; if (state === 'RUNNING') return ['cancel']; if (state === 'READY') return ['approve']; if (RESUME_STATES.has(state)) actions.push('resume'); if (RESTART_STATES.has(state)) actions.push('restart'); return actions.length ? actions : [null]; } function getResumeEndpoint(state) { return RESUME_STATES.has(state) ? '/resume' : null; } function getRestartEndpoint(state) { return RESTART_STATES.has(state) ? '/run' : null; } // ── Tests ───────────────────────────────────────────────────────────────────── describe('task card action buttons', () => { it('shows Run button for PENDING', () => { assert.deepEqual(getCardActions('PENDING'), ['run']); }); it('shows Cancel button for RUNNING', () => { assert.deepEqual(getCardActions('RUNNING'), ['cancel']); }); it('shows Resume AND Restart buttons for FAILED', () => { assert.deepEqual(getCardActions('FAILED'), ['resume', 'restart']); }); it('shows Resume AND Restart buttons for CANCELLED', () => { assert.deepEqual(getCardActions('CANCELLED'), ['resume', 'restart']); }); it('shows Resume AND Restart buttons for BUDGET_EXCEEDED', () => { assert.deepEqual(getCardActions('BUDGET_EXCEEDED'), ['resume', 'restart']); }); it('shows Resume button only for TIMED_OUT (no restart)', () => { assert.deepEqual(getCardActions('TIMED_OUT'), ['resume']); }); it('shows approve buttons for READY', () => { assert.deepEqual(getCardActions('READY'), ['approve']); }); it('shows no button for COMPLETED', () => { assert.deepEqual(getCardActions('COMPLETED'), [null]); }); it('shows no button for QUEUED', () => { assert.deepEqual(getCardActions('QUEUED'), [null]); }); }); describe('task action API endpoints', () => { it('TIMED_OUT uses /resume endpoint', () => { assert.equal(getResumeEndpoint('TIMED_OUT'), '/resume'); }); it('CANCELLED uses /resume endpoint for resume', () => { assert.equal(getResumeEndpoint('CANCELLED'), '/resume'); }); it('FAILED uses /resume endpoint for resume', () => { assert.equal(getResumeEndpoint('FAILED'), '/resume'); }); it('BUDGET_EXCEEDED uses /resume endpoint for resume', () => { assert.equal(getResumeEndpoint('BUDGET_EXCEEDED'), '/resume'); }); it('CANCELLED uses /run endpoint for restart', () => { assert.equal(getRestartEndpoint('CANCELLED'), '/run'); }); it('FAILED uses /run endpoint for restart', () => { assert.equal(getRestartEndpoint('FAILED'), '/run'); }); it('BUDGET_EXCEEDED uses /run endpoint for restart', () => { assert.equal(getRestartEndpoint('BUDGET_EXCEEDED'), '/run'); }); it('TIMED_OUT has no /run restart endpoint', () => { assert.equal(getRestartEndpoint('TIMED_OUT'), null); }); });