Files
stack/packages/webui/tests/usage-render.test.mjs
T
jason.woltje 3692fca522 fix(row333 R3): per-key quota follows the agent's key lineage across rotation and re-issue (#1831)
Ruling 1: model records keep a bounded previous list (64 ids, oldest dropped)
of ids that left by rotation, grace expiry or re-issue; a fire removes the
record. A tokens.keys leaf matches and counts every id in the lineage that
holds it. Lineage ids are unique host-wide. An orphaned leaf limits nothing,
logs one value-free line when it becomes orphaned, and the Console usage
view lists it.
Ruling 2: an agent with no model key under a key quota refuses
key-quota-no-model-key, passed through the broker, launcher and stop reason.
Ruling 3: covered by the lineage; no mayLaunch change.
Ruling 4: the usage module receives a frozen { readUsageQueue } only.
Ruling 5: every telemetry fixture wait races a 5 s real-timer deadline.
2026-11-02 17:54:07 -06:00

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