summaryrefslogtreecommitdiff
path: root/web/test/filter-tabs.test.mjs
blob: 68198635e541ee11c949a11e1aba1268146dcc24 (plain)
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// filter-tabs.test.mjs — TDD contract tests for filterTasksByTab
//
// 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, created_at = null) {
  return { id: state, name: `task-${state}`, state, created_at };
}

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', () => {
    const tasks = ALL_STATES.map(s => makeTask(s));
    const result = filterTasksByTab(tasks, 'active');
    for (const state of ['PENDING', 'QUEUED', 'RUNNING', 'READY']) {
      assert.ok(result.some(t => t.state === state), `${state} should be included`);
    }
  });

  it('excludes BLOCKED, COMPLETED, FAILED, TIMED_OUT, CANCELLED, BUDGET_EXCEEDED', () => {
    const tasks = ALL_STATES.map(s => makeTask(s));
    const result = filterTasksByTab(tasks, 'active');
    for (const state of ['BLOCKED', '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 — interrupted tab', () => {
  it('includes CANCELLED, FAILED, BUDGET_EXCEEDED, BLOCKED', () => {
    const tasks = ALL_STATES.map(s => makeTask(s));
    const result = filterTasksByTab(tasks, 'interrupted');
    for (const state of ['CANCELLED', 'FAILED', 'BUDGET_EXCEEDED', 'BLOCKED']) {
      assert.ok(result.some(t => t.state === state), `${state} should be included`);
    }
  });

  it('excludes all non-interrupted states', () => {
    const tasks = ALL_STATES.map(s => makeTask(s));
    const result = filterTasksByTab(tasks, 'interrupted');
    for (const state of ['PENDING', 'QUEUED', 'RUNNING', 'READY', 'COMPLETED', 'TIMED_OUT']) {
      assert.ok(!result.some(t => t.state === state), `${state} should be excluded`);
    }
  });

  it('returns empty array for empty input', () => {
    assert.deepEqual(filterTasksByTab([], 'interrupted'), []);
  });
});

describe('filterTasksByTab — done tab', () => {
  it('includes COMPLETED, TIMED_OUT (if recent)', () => {
    const now = new Date().toISOString();
    const tasks = [
        makeTask('COMPLETED', now),
        makeTask('TIMED_OUT', now),
    ];
    const result = filterTasksByTab(tasks, 'done');
    assert.equal(result.length, 2);
  });

  it('excludes COMPLETED, TIMED_OUT if older than 24h', () => {
    const longAgo = new Date(Date.now() - 25 * 60 * 60 * 1000).toISOString();
    const tasks = [
        makeTask('COMPLETED', longAgo),
        makeTask('TIMED_OUT', longAgo),
    ];
    const result = filterTasksByTab(tasks, 'done');
    assert.equal(result.length, 0, 'should hide tasks older than 24h');
  });

  it('includes tasks with null created_at by default (defensive)', () => {
      const tasks = [makeTask('COMPLETED', null)];
      const result = filterTasksByTab(tasks, 'done');
      assert.equal(result.length, 1);
  });

  it('excludes PENDING, QUEUED, RUNNING, READY, BLOCKED, CANCELLED, FAILED, BUDGET_EXCEEDED', () => {
    const now = new Date().toISOString();
    const tasks = ALL_STATES.map(s => makeTask(s, now));
    const result = filterTasksByTab(tasks, 'done');
    for (const state of ['PENDING', 'QUEUED', 'RUNNING', 'READY', 'BLOCKED', 'CANCELLED', 'FAILED', 'BUDGET_EXCEEDED']) {
      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(s => makeTask(s));
    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(s => makeTask(s));
    const result = filterTasksByTab(tasks, 'unknown-tab');
    assert.equal(result.length, ALL_STATES.length);
    assert.strictEqual(result, tasks, 'should return the same array reference');
  });
});