// Local renderer contract: no browser, real accounts, provider or auth material.
import test from 'node:test';
import assert from 'node:assert/strict';
import { readFileSync } from 'node:fs';
import { runInNewContext } from 'node:vm';
const source=readFileSync(new URL('../src/public/bus.js',import.meta.url),'utf8');
const start=source.indexOf(' function tokenUsage(value) {'),end=source.indexOf('\n // Row 155',start);
const render=runInNewContext(`const txt=v=>String(v??'').replace(/[&<>"']/g,c=>({'&':'&','<':'<','>':'>','"':'"',"'":'''}[c]));${source.slice(start,end)};tokenUsage`);
const counts={input:2,output:3,cacheWrite:4,cacheRead:5,unknownTurns:0};
test('P5 Console displays stack count per safe key separately from optional proxy account numbers',()=>{
const native={total:counts,byKey:[{modelKeyId:'012345abcdef',...counts}]};
const off=render(native);assert.match(off,/Key: 012345abcdef/);assert.doesNotMatch(off,/Proxy-reported/);
const on=render({...native,proxy:{source:'proxy',state:'available',byAccount:[{accountId:'abcdef012345',input:99,output:7,cacheWrite:0,cacheRead:0,requests:2,failures:1}]}});
assert.match(on,/Proxy-reported/);assert.match(on,/abcdef012345/);assert.match(on,/telemetry.*quota/i);assert.match(on,/>99);
assert.match(on,/Key: 012345abcdef/);assert.doesNotMatch(on,/msk-|upstream-canary/);
});
test('P5 Console escapes hostile labels and labels unavailable telemetry without presenting a fresh zero',()=>{
const page=render({total:counts,byKey:[{modelKeyId:'',...counts}],proxy:{source:'proxy',state:'unavailable',byAccount:[]}});
assert.doesNotMatch(page,/
{
const page=render({total:counts,byKey:[],orphanedKeyQuotas:['012345abcdef']});
assert.match(page,/
1 batch of proxy usage events could not be saved since the bus started\. The events in a dropped batch are lost and are not in these totals\.<\/p>/); assert.ok(one.indexOf('token-usage-proxy-dropped')>one.indexOf('Proxy-reported usage')); assert.match(render({...native,proxy:proxy({droppedBatches:3})}),/>3 batches of proxy usage events could not be saved/); for(const over of [{droppedBatches:0},{},{droppedBatches:-1},{droppedBatches:'2'},{droppedBatches:1.5},{droppedBatches:null}]){ const page=render({...native,proxy:proxy(over)});assert.match(page,/Proxy-reported/);assert.doesNotMatch(page,/token-usage-proxy-dropped|dropped batch/); } assert.doesNotMatch(render({...native,proxy:null}),/token-usage-proxy-dropped/); });