summaryrefslogtreecommitdiff
path: root/web/test/budget.test.mjs
blob: 35d5256730b59bda471f85132b075811f540261c (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
// budget.test.mjs — Unit tests for budget headroom display.
//
// Run with: node --test web/test/budget.test.mjs

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

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

describe('formatBudgetHeadroom', () => {
  it('formats a limited provider with percent and dollars', () => {
    assert.equal(
      formatBudgetHeadroom({ provider: 'claude', limited: true, limit_usd: 10, remaining_usd: 6, fraction_remaining: 0.6 }),
      'Claude: 60% left ($6.00 of $10.00)',
    );
  });

  it('returns empty string for unlimited providers', () => {
    assert.equal(formatBudgetHeadroom({ provider: 'local', limited: false }), '');
    assert.equal(formatBudgetHeadroom(null), '');
  });
});

describe('renderBudgetHeadroom', () => {
  it('renders one chip per limited provider, skipping unlimited', () => {
    const wrap = renderBudgetHeadroom([
      { provider: 'claude', limited: true, limit_usd: 10, remaining_usd: 6, fraction_remaining: 0.6 },
      { provider: 'local', limited: false },
    ], makeDoc());
    assert.equal(wrap.children.length, 1);
    assert.equal(wrap.children[0].className, 'budget-chip');
    assert.match(wrap.children[0].textContent, /Claude: 60% left/);
  });

  it('flags providers under 20% remaining with --low', () => {
    const wrap = renderBudgetHeadroom([
      { provider: 'gemini', limited: true, limit_usd: 5, remaining_usd: 0.5, fraction_remaining: 0.1 },
    ], makeDoc());
    assert.equal(wrap.children[0].className, 'budget-chip budget-chip--low');
  });

  it('returns null when doc is null', () => {
    assert.equal(renderBudgetHeadroom([], null), null);
  });
});