// Row 125 (#1619, REQ-LIMIT-3, REQ-LIMIT-4): `mosaic limits` shows each token limit with its // count, or why it isn't counted, and `mosaic limits reset PATH...` restarts a limit's count. import test from 'node:test'; import assert from 'node:assert/strict'; import {main} from '../src/cli.mjs'; import {formatTokens,DAILY_NOTE} from '../src/limits.mjs'; import {io,tmp} from './helpers.mjs'; const SINCE='2026-10-18T07:00:00.000Z'; const TOKENS=[ {path:['tokens','instances','coder'],limit:100,since:SINCE,status:'counted',check:'token-limit',count:120,kinds:{input:10,output:20,cacheWrite:40,cacheRead:50},unknownTurns:2}, {path:['tokens','keys','k1'],limit:5,since:SINCE,status:'not-enforced',reason:'no records name a key'}, {path:['tokens','providers','zai'],limit:9,since:SINCE,status:'not-enforced',reason:null}, {path:['tokens','models','anthropic','claude-opus-5-5'],limit:7,since:SINCE,status:'check-failed',check:'token-limit',code:'check-threw'}, {path:['tokens','instances','pm'],limit:1,since:SINCE,status:'check-failed',check:null,code:'records-unreadable'}, ]; test('REQ-LIMIT-3 REQ-LIMIT-4 formatTokens shows each limit counted, not enforced, or check failed, in reset path words',()=>{ assert.equal(formatTokens(TOKENS),'token limits:\n'+[ ` tokens instances coder: limit 100 since ${SINCE}, count 120 (input 10, output 20, cacheWrite 40, cacheRead 50), unknown turns 2`, ` tokens keys k1: limit 5 since ${SINCE}, not enforced: no records name a key`, ` tokens providers zai: limit 9 since ${SINCE}, not enforced`, ` tokens models anthropic claude-opus-5-5: limit 7 since ${SINCE}, check failed: check-threw (token-limit)`, ` tokens instances pm: limit 1 since ${SINCE}, check failed: records-unreadable`, ].join('\n')+'\n'); for(const none of [[],undefined])assert.equal(formatTokens(none),'token limits: none\n'); }); test('REQ-LIMIT-2 row 126: any daily quota row adds one line after the list saying the count is this stack\'s own use',()=>{ const daily={path:['tokens','daily','jason-max'],limit:40,since:SINCE,status:'counted',check:'daily-quota',count:3,kinds:{input:1,output:1,cacheWrite:0,cacheRead:1},unknownTurns:0}; const out=formatTokens([TOKENS[0],daily,{...daily,path:['tokens','daily','other'],status:'not-enforced',reason:null}]); assert.ok(out.endsWith(` tokens daily other: limit 40 since ${SINCE}, not enforced\n${DAILY_NOTE}\n`),out); assert.equal(out.split(DAILY_NOTE).length,2,'said once'); assert.equal(DAILY_NOTE,"Daily quotas count only this stack's own use of the account, not its use anywhere else."); assert.ok(!formatTokens(TOKENS).includes(DAILY_NOTE)); }); test('REQ-LIMIT-3 the view prints the limits then the token lines; --json keeps tokens in the result',async t=>{ const root=tmp(t),deps={system:{dataRoot:root},transport:()=>()=>({revision:'b'.repeat(64),limits:{total:3},tokens:TOKENS})}; const x=io();await main(['limits','--business','acme'],x,deps); const [json,lines]=x.out.text.split('token limits:\n'); assert.deepEqual(JSON.parse(json),{revision:'b'.repeat(64),limits:{total:3}}); assert.match(lines,/tokens keys k1: limit 5 since \S+, not enforced: no records name a key/); const y=io();await main(['limits','--business','acme','--json'],y,deps); assert.deepEqual(JSON.parse(y.out.text).tokens,TOKENS); }); test('REQ-LIMIT-3 reset reads the revision, then sends limits.set with one path per limit, a word per level',async t=>{ const root=tmp(t),calls=[]; const deps={system:{dataRoot:root},transport:business=>(verb,args)=>{calls.push({business,verb,args});return {revision:'c'.repeat(64),limits:{},tokens:[]};}}; await main(['limits','reset','tokens','models','anthropic','claude-opus-5-5','--business','acme'],io(),deps); assert.deepEqual(calls,[ {business:'acme',verb:'limits',args:{}}, {business:'acme',verb:'limits.set',args:{revision:'c'.repeat(64),reset:[['tokens','models','anthropic','claude-opus-5-5']]}}, ]); }); test('REQ-LIMIT-3 reset with no path, or with --file, is refused before the bus',async t=>{ const root=tmp(t),calls=[]; const deps={system:{dataRoot:root},transport:()=>verb=>{calls.push(verb);return {};}}; await assert.rejects(main(['limits','reset','--business','acme'],io(),deps),/mosaic limits/); await assert.rejects(main(['limits','reset','tokens','--file','x.json','--business','acme'],io(),deps),/mosaic limits/); await assert.rejects(main(['limits','view','extra','--business','acme'],io(),deps),/mosaic limits/); assert.deepEqual(calls,[]); });