Files
stack/packages/webui/tests/usage-render.test.mjs
jason.woltje 38cfa72335 feat(row 333): count dropped proxy usage batches and show them in the Console (Vikunja #778)
The usage queue read drains the proxy. A batch that is then not written (the
client or the bus refuses its shape, an ambiguous key owner, a failed write) is
lost, and the Console showed proxy totals with no sign of it. The reader now
counts these per enabled business since the bus started, the proxy view returns
the count as droppedBatches, and the Console proxy section says so above 0.
A read that failed before the proxy answered or that it refused drained nothing
and is not counted. Telemetry only; no quota reads it. The log line stays
proxy-usage-read-failed.
2026-11-02 20:08:50 -06:00

43 lines
3.8 KiB
JavaScript

// 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=>({'&':'&amp;','<':'&lt;','>':'&gt;','"':'&quot;',"'":'&#39;'}[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:'<img src=x>',...counts}],proxy:{source:'proxy',state:'unavailable',byAccount:[]}});
assert.doesNotMatch(page,/<img/);assert.match(page,/unavailable/);
});
// Row 333 R3 (#1831): key quotas that match no hired agent are listed by id, escaped, and only when there are any.
test('R3 Console marks an orphaned key quota by id and says it limits nothing',()=>{
const page=render({total:counts,byKey:[],orphanedKeyQuotas:['012345abcdef']});
assert.match(page,/<ul id="token-usage-orphans" aria-label="Key quotas that match no agent">/);
assert.match(page,/<li>Key quota 012345abcdef matches no agent, so it limits nothing\. A fire and re-hire gives the agent a new key; set the quota on the new key id\.<\/li>/);
assert.doesNotMatch(render({total:counts,byKey:[{modelKeyId:'012345abcdef',...counts}]}),/token-usage-orphans|matches no agent/);
assert.doesNotMatch(render({total:counts,byKey:[],orphanedKeyQuotas:[]}),/token-usage-orphans/);
const hostile=render({total:counts,byKey:[],orphanedKeyQuotas:['<img src=x>']});
assert.doesNotMatch(hostile,/<img/);assert.match(hostile,/&lt;img src=x&gt;/);
});
// #778: batches the proxy gave up and the bus did not save. Shown in the proxy section only when above 0.
test('#778 Console says how many proxy batches were dropped, only when there are any, and that their events are not in the totals',()=>{
const native={total:counts,byKey:[]},proxy=(over={})=>({source:'proxy',state:'available',byAccount:[],...over});
const one=render({...native,proxy:proxy({droppedBatches:1})});
assert.match(one,/<p id="token-usage-proxy-dropped" role="status">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/);
});