repositories / will
will
owned by admin
src/agent/usage.test.ts
Rawimport assert from 'node:assert/strict'
import { test } from 'node:test'
import { sumAssistantUsage } from './usage.ts'
const assistant = (model: string, usage: object, provider = 'zai') => ({
role: 'assistant',
provider,
model,
usage,
})
test('sums usage across assistant messages of one wake', () => {
const rows = sumAssistantUsage([
assistant('glm-5.3-flash', { input: 100, output: 50, cacheRead: 10, cacheWrite: 5 }),
{ role: 'user', content: 'hi' },
assistant('glm-5.3-flash', { input: 200, output: 20 }),
])
assert.deepEqual(rows, [
{
provider: 'zai',
model: 'glm-5.3-flash',
input: 300,
output: 70,
cacheRead: 10,
cacheWrite: 5,
responses: 2,
},
])
})
test('splits by provider/model and tolerates junk', () => {
const rows = sumAssistantUsage([
assistant('glm-5.3-flash', { input: 1 }),
assistant('glm-5.3-hig', { input: 2 }, 'cloudflare-ai-gateway'),
assistant('glm-5.3-flash', { input: 'junk', output: null }),
{ role: 'assistant', model: 'no-usage' },
'garbage',
42,
])
assert.equal(rows.length, 2)
const flash = rows.find((r) => r.model === 'glm-5.3-flash')
assert.equal(flash?.input, 1)
assert.equal(flash?.responses, 2)
})
test('non-array input yields no rows', () => {
assert.deepEqual(sumAssistantUsage(undefined), [])
assert.deepEqual(sumAssistantUsage({ messages: [] }), [])
})