summaryrefslogtreecommitdiff
path: root/web/test/sort-tasks.test.mjs
blob: fe4770243035113638f14d7bbc6142415f1c66c2 (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
// sort-tasks.test.mjs — TDD contract tests for sortTasksByDate
//
// sortTasksByDate is defined inline here to establish expected behaviour.
// Once sortTasksByDate is exported from web/app.js or a shared module,
// remove the inline definition and import it instead.
//
// Run with: node --test web/test/sort-tasks.test.mjs

import { describe, it } from 'node:test';
import assert from 'node:assert/strict';

// ── Implementation under contract ─────────────────────────────────────────────
// Remove this block once sortTasksByDate is available from app.js.

function sortTasksByDate(tasks) {
  return [...tasks].sort((a, b) => {
    if (!a.created_at && !b.created_at) return 0;
    if (!a.created_at) return 1;
    if (!b.created_at) return -1;
    return new Date(a.created_at) - new Date(b.created_at);
  });
}

// ── Helpers ────────────────────────────────────────────────────────────────────

function makeTask(id, created_at, state = 'PENDING') {
  return { id, name: `task-${id}`, state, created_at };
}

// ── Tests ──────────────────────────────────────────────────────────────────────

describe('sortTasksByDate', () => {
  it('sorts tasks oldest-first by created_at', () => {
    const tasks = [
      makeTask('c', '2026-03-06T12:00:00Z'),
      makeTask('a', '2026-03-04T08:00:00Z'),
      makeTask('b', '2026-03-05T10:00:00Z'),
    ];
    const result = sortTasksByDate(tasks);
    assert.equal(result[0].id, 'a', 'oldest should be first');
    assert.equal(result[1].id, 'b');
    assert.equal(result[2].id, 'c', 'newest should be last');
  });

  it('returns a new array (does not mutate input)', () => {
    const tasks = [
      makeTask('b', '2026-03-05T10:00:00Z'),
      makeTask('a', '2026-03-04T08:00:00Z'),
    ];
    const original = [...tasks];
    const result = sortTasksByDate(tasks);
    assert.notStrictEqual(result, tasks, 'should return a new array');
    assert.deepEqual(tasks, original, 'input should not be mutated');
  });

  it('returns an empty array when given an empty array', () => {
    assert.deepEqual(sortTasksByDate([]), []);
  });

  it('returns a single-element array unchanged', () => {
    const tasks = [makeTask('x', '2026-03-01T00:00:00Z')];
    const result = sortTasksByDate(tasks);
    assert.equal(result.length, 1);
    assert.equal(result[0].id, 'x');
  });

  it('places tasks with null created_at after tasks with a date', () => {
    const tasks = [
      makeTask('no-date', null),
      makeTask('has-date', '2026-03-01T00:00:00Z'),
    ];
    const result = sortTasksByDate(tasks);
    assert.equal(result[0].id, 'has-date', 'task with date should come first');
    assert.equal(result[1].id, 'no-date', 'task without date should come last');
  });

  it('works with mixed states (not just PENDING)', () => {
    const tasks = [
      makeTask('r', '2026-03-06T00:00:00Z', 'RUNNING'),
      makeTask('p', '2026-03-04T00:00:00Z', 'PENDING'),
      makeTask('q', '2026-03-05T00:00:00Z', 'QUEUED'),
    ];
    const result = sortTasksByDate(tasks);
    assert.equal(result[0].id, 'p');
    assert.equal(result[1].id, 'q');
    assert.equal(result[2].id, 'r');
  });
});