summaryrefslogtreecommitdiff
path: root/web/test/active-pane.test.mjs
blob: 37bb8c555d560e7615fe9cf8688c884c42ca0275 (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
// active-pane.test.mjs — Tests for Active pane partition logic.
//
// Run with: node --test web/test/active-pane.test.mjs

import { describe, it } from 'node:test';
import assert from 'node:assert/strict';
import { partitionActivePaneTasks } from '../app.js';

function makeTask(id, state, created_at) {
  return { id, name: `task-${id}`, state, created_at: created_at ?? `2024-01-01T00:0${id}:00Z` };
}

const ALL_STATES = [
  'PENDING', 'QUEUED', 'RUNNING', 'READY', 'BLOCKED',
  'COMPLETED', 'FAILED', 'TIMED_OUT', 'CANCELLED', 'BUDGET_EXCEEDED',
];

describe('partitionActivePaneTasks', () => {
  it('running contains only RUNNING tasks', () => {
    const tasks = ALL_STATES.map((s, i) => makeTask(String(i), s));
    const { running } = partitionActivePaneTasks(tasks);
    assert.equal(running.length, 1);
    assert.equal(running[0].state, 'RUNNING');
  });

  it('ready contains only READY tasks', () => {
    const tasks = ALL_STATES.map((s, i) => makeTask(String(i), s));
    const { ready } = partitionActivePaneTasks(tasks);
    assert.equal(ready.length, 1);
    assert.equal(ready[0].state, 'READY');
  });

  it('excludes QUEUED, BLOCKED, PENDING, COMPLETED, FAILED and all other states', () => {
    const tasks = ALL_STATES.map((s, i) => makeTask(String(i), s));
    const { running, ready } = partitionActivePaneTasks(tasks);
    const allReturned = [...running, ...ready];
    assert.equal(allReturned.length, 2);
    assert.ok(allReturned.every(t => t.state === 'RUNNING' || t.state === 'READY'));
  });

  it('returns empty arrays for empty input', () => {
    const { running, ready } = partitionActivePaneTasks([]);
    assert.deepEqual(running, []);
    assert.deepEqual(ready, []);
  });

  it('handles multiple RUNNING tasks sorted by created_at ascending', () => {
    const tasks = [
      makeTask('b', 'RUNNING', '2024-01-01T00:02:00Z'),
      makeTask('a', 'RUNNING', '2024-01-01T00:01:00Z'),
      makeTask('c', 'RUNNING', '2024-01-01T00:03:00Z'),
    ];
    const { running } = partitionActivePaneTasks(tasks);
    assert.equal(running.length, 3);
    assert.equal(running[0].id, 'a');
    assert.equal(running[1].id, 'b');
    assert.equal(running[2].id, 'c');
  });

  it('handles multiple READY tasks sorted by created_at ascending', () => {
    const tasks = [
      makeTask('y', 'READY', '2024-01-01T00:02:00Z'),
      makeTask('x', 'READY', '2024-01-01T00:01:00Z'),
    ];
    const { ready } = partitionActivePaneTasks(tasks);
    assert.equal(ready.length, 2);
    assert.equal(ready[0].id, 'x');
    assert.equal(ready[1].id, 'y');
  });

  it('returns both sections independently when both states present', () => {
    const tasks = [
      makeTask('r1', 'RUNNING', '2024-01-01T00:01:00Z'),
      makeTask('d1', 'READY',   '2024-01-01T00:02:00Z'),
      makeTask('r2', 'RUNNING', '2024-01-01T00:03:00Z'),
    ];
    const { running, ready } = partitionActivePaneTasks(tasks);
    assert.equal(running.length, 2);
    assert.equal(ready.length, 1);
  });
});