summaryrefslogtreecommitdiff
path: root/web/test/changestats.test.mjs
blob: 53638125821f906a764bbefd62c6a74b40aefca5 (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
// changestats.test.mjs — Unit tests for changestats display functions.
//
// Run with: node --test web/test/changestats.test.mjs

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

// ── Mock DOM ───────────────────────────────────────────────────────────────────

function makeDoc() {
  return {
    createElement(tag) {
      const el = {
        tag,
        className: '',
        textContent: '',
        children: [],
        appendChild(child) { this.children.push(child); return child; },
      };
      return el;
    },
  };
}

// ── formatChangestats ──────────────────────────────────────────────────────────

describe('formatChangestats', () => {
  it('formats valid stats as "N files, +A -R"', () => {
    const result = formatChangestats({ files_changed: 5, lines_added: 127, lines_removed: 43 });
    assert.equal(result, '5 files, +127 -43');
  });

  it('returns empty string for null', () => {
    const result = formatChangestats(null);
    assert.equal(result, '');
  });

  it('returns empty string for undefined', () => {
    const result = formatChangestats(undefined);
    assert.equal(result, '');
  });

  it('formats zero values correctly', () => {
    const result = formatChangestats({ files_changed: 0, lines_added: 0, lines_removed: 0 });
    assert.equal(result, '0 files, +0 -0');
  });

  it('formats single file correctly', () => {
    const result = formatChangestats({ files_changed: 1, lines_added: 10, lines_removed: 2 });
    assert.equal(result, '1 files, +10 -2');
  });
});

// ── renderChangestatsBadge ─────────────────────────────────────────────────────

describe('renderChangestatsBadge', () => {
  it('returns element with class changestats-badge for valid stats', () => {
    const doc = makeDoc();
    const el = renderChangestatsBadge({ files_changed: 5, lines_added: 127, lines_removed: 43 }, doc);
    assert.ok(el, 'element should not be null');
    assert.equal(el.className, 'changestats-badge');
  });

  it('returns element with correct text content', () => {
    const doc = makeDoc();
    const el = renderChangestatsBadge({ files_changed: 5, lines_added: 127, lines_removed: 43 }, doc);
    assert.equal(el.textContent, '5 files, +127 -43');
  });

  it('returns null for null stats', () => {
    const doc = makeDoc();
    const el = renderChangestatsBadge(null, doc);
    assert.equal(el, null);
  });

  it('returns null for undefined stats', () => {
    const doc = makeDoc();
    const el = renderChangestatsBadge(undefined, doc);
    assert.equal(el, null);
  });
});

// ── State-based visibility ────────────────────────────────────────────────────
//
// Changestats badge should appear on COMPLETED (and READY) tasks that have
// changestats data, and must not appear on QUEUED tasks.

const CHANGESTATS_STATES = new Set(['COMPLETED', 'READY']);

function shouldShowChangestats(task) {
  return CHANGESTATS_STATES.has(task.state) && task.changestats != null;
}

describe('changestats badge visibility by task state', () => {
  it('COMPLETED task with changestats shows badge', () => {
    const task = { state: 'COMPLETED', changestats: { files_changed: 3, lines_added: 50, lines_removed: 10 } };
    assert.equal(shouldShowChangestats(task), true);
  });

  it('READY task with changestats shows badge', () => {
    const task = { state: 'READY', changestats: { files_changed: 1, lines_added: 5, lines_removed: 2 } };
    assert.equal(shouldShowChangestats(task), true);
  });

  it('QUEUED task hides changestats', () => {
    const task = { state: 'QUEUED', changestats: { files_changed: 3, lines_added: 50, lines_removed: 10 } };
    assert.equal(shouldShowChangestats(task), false);
  });

  it('COMPLETED task without changestats hides badge', () => {
    const task = { state: 'COMPLETED', changestats: null };
    assert.equal(shouldShowChangestats(task), false);
  });

  it('RUNNING task hides changestats', () => {
    const task = { state: 'RUNNING', changestats: null };
    assert.equal(shouldShowChangestats(task), false);
  });

  it('PENDING task hides changestats', () => {
    const task = { state: 'PENDING', changestats: null };
    assert.equal(shouldShowChangestats(task), false);
  });
});