// 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); }); });