1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
|
// tab-badges.test.mjs — TDD tests for computeTabBadgeCounts
//
// Tests the pure function that computes badge counts for the
// 'interrupted', 'ready', and 'running' tabs.
//
// Run with: node --test web/test/tab-badges.test.mjs
import { describe, it } from 'node:test';
import assert from 'node:assert/strict';
// ── Inline implementation (will be replaced by import once exported) ───────────
const INTERRUPTED_STATES = new Set(['CANCELLED', 'FAILED', 'BUDGET_EXCEEDED', 'BLOCKED']);
function computeTabBadgeCounts(tasks) {
let interrupted = 0;
let ready = 0;
let running = 0;
for (const t of tasks) {
if (INTERRUPTED_STATES.has(t.state)) interrupted++;
if (t.state === 'READY') ready++;
if (t.state === 'RUNNING') running++;
}
return { interrupted, ready, running };
}
// ── Helpers ────────────────────────────────────────────────────────────────────
function makeTask(state) {
return { id: state, name: `task-${state}`, state };
}
// ── Tests ──────────────────────────────────────────────────────────────────────
describe('computeTabBadgeCounts', () => {
it('returns all zeros for empty task list', () => {
assert.deepEqual(computeTabBadgeCounts([]), { interrupted: 0, ready: 0, running: 0 });
});
it('counts RUNNING tasks', () => {
const tasks = [makeTask('RUNNING'), makeTask('RUNNING'), makeTask('QUEUED')];
const counts = computeTabBadgeCounts(tasks);
assert.equal(counts.running, 2);
assert.equal(counts.ready, 0);
assert.equal(counts.interrupted, 0);
});
it('counts READY tasks', () => {
const tasks = [makeTask('READY'), makeTask('READY'), makeTask('QUEUED')];
const counts = computeTabBadgeCounts(tasks);
assert.equal(counts.ready, 2);
assert.equal(counts.running, 0);
assert.equal(counts.interrupted, 0);
});
it('counts CANCELLED as interrupted', () => {
const counts = computeTabBadgeCounts([makeTask('CANCELLED')]);
assert.equal(counts.interrupted, 1);
});
it('counts FAILED as interrupted', () => {
const counts = computeTabBadgeCounts([makeTask('FAILED')]);
assert.equal(counts.interrupted, 1);
});
it('counts BUDGET_EXCEEDED as interrupted', () => {
const counts = computeTabBadgeCounts([makeTask('BUDGET_EXCEEDED')]);
assert.equal(counts.interrupted, 1);
});
it('counts BLOCKED as interrupted', () => {
const counts = computeTabBadgeCounts([makeTask('BLOCKED')]);
assert.equal(counts.interrupted, 1);
});
it('does not count COMPLETED as interrupted', () => {
const counts = computeTabBadgeCounts([makeTask('COMPLETED')]);
assert.equal(counts.interrupted, 0);
});
it('does not count TIMED_OUT as interrupted', () => {
const counts = computeTabBadgeCounts([makeTask('TIMED_OUT')]);
assert.equal(counts.interrupted, 0);
});
it('counts across multiple states simultaneously', () => {
const tasks = [
makeTask('RUNNING'),
makeTask('RUNNING'),
makeTask('READY'),
makeTask('CANCELLED'),
makeTask('FAILED'),
makeTask('BLOCKED'),
makeTask('QUEUED'),
makeTask('COMPLETED'),
];
const counts = computeTabBadgeCounts(tasks);
assert.equal(counts.running, 2);
assert.equal(counts.ready, 1);
assert.equal(counts.interrupted, 3);
});
it('returns zero for a tab when no tasks match that state', () => {
const tasks = [makeTask('QUEUED'), makeTask('PENDING'), makeTask('COMPLETED')];
const counts = computeTabBadgeCounts(tasks);
assert.equal(counts.running, 0);
assert.equal(counts.ready, 0);
assert.equal(counts.interrupted, 0);
});
});
|