// filter-tabs.test.mjs — TDD contract tests for filterTasksByTab // // filterTasksByTab is defined inline here to establish expected behaviour. // Once filterTasksByTab is exported from web/app.js, remove the inline // definition and import it instead. // // Run with: node --test web/test/filter-tabs.test.mjs import { describe, it } from 'node:test'; import assert from 'node:assert/strict'; import { filterTasksByTab } from '../app.js'; // ── Helpers ──────────────────────────────────────────────────────────────────── function makeTask(state) { return { id: state, name: `task-${state}`, state }; } const ALL_STATES = [ 'PENDING', 'QUEUED', 'RUNNING', 'READY', 'BLOCKED', 'COMPLETED', 'FAILED', 'TIMED_OUT', 'CANCELLED', 'BUDGET_EXCEEDED', ]; // ── Tests ────────────────────────────────────────────────────────────────────── describe('filterTasksByTab — active tab', () => { it('includes PENDING, QUEUED, RUNNING, READY, BLOCKED', () => { const tasks = ALL_STATES.map(makeTask); const result = filterTasksByTab(tasks, 'active'); for (const state of ['PENDING', 'QUEUED', 'RUNNING', 'READY', 'BLOCKED']) { assert.ok(result.some(t => t.state === state), `${state} should be included`); } }); it('excludes COMPLETED, FAILED, TIMED_OUT, CANCELLED, BUDGET_EXCEEDED', () => { const tasks = ALL_STATES.map(makeTask); const result = filterTasksByTab(tasks, 'active'); for (const state of ['COMPLETED', 'FAILED', 'TIMED_OUT', 'CANCELLED', 'BUDGET_EXCEEDED']) { assert.ok(!result.some(t => t.state === state), `${state} should be excluded`); } }); it('returns empty array for empty input', () => { assert.deepEqual(filterTasksByTab([], 'active'), []); }); }); describe('filterTasksByTab — done tab', () => { it('includes COMPLETED, FAILED, TIMED_OUT, CANCELLED, BUDGET_EXCEEDED', () => { const tasks = ALL_STATES.map(makeTask); const result = filterTasksByTab(tasks, 'done'); for (const state of ['COMPLETED', 'FAILED', 'TIMED_OUT', 'CANCELLED', 'BUDGET_EXCEEDED']) { assert.ok(result.some(t => t.state === state), `${state} should be included`); } }); it('excludes PENDING, QUEUED, RUNNING, READY, BLOCKED', () => { const tasks = ALL_STATES.map(makeTask); const result = filterTasksByTab(tasks, 'done'); for (const state of ['PENDING', 'QUEUED', 'RUNNING', 'READY', 'BLOCKED']) { assert.ok(!result.some(t => t.state === state), `${state} should be excluded`); } }); it('returns empty array for empty input', () => { assert.deepEqual(filterTasksByTab([], 'done'), []); }); }); describe('filterTasksByTab — all tab', () => { it('returns all tasks unchanged', () => { const tasks = ALL_STATES.map(makeTask); const result = filterTasksByTab(tasks, 'all'); assert.equal(result.length, ALL_STATES.length); assert.strictEqual(result, tasks, 'should return the same array reference'); }); it('returns empty array for empty input', () => { assert.deepEqual(filterTasksByTab([], 'all'), []); }); }); describe('filterTasksByTab — unknown tab', () => { it('returns all tasks as defensive fallback', () => { const tasks = ALL_STATES.map(makeTask); const result = filterTasksByTab(tasks, 'unknown-tab'); assert.equal(result.length, ALL_STATES.length); assert.strictEqual(result, tasks, 'should return the same array reference'); }); });