Files
stack/packages/webui/tests/usage.test.mjs
jason.woltjeandClaude Opus 5.5 70504d23a4 bus, cli, webui: token accounting per run, schema 3h usage_records (row 124, #1618)
Rocko's round 2 candidate e09313aba4d155a1c5af7bfb227ec397e96c0d57444782cd0b48593102bcc684
(38 paths, base 23533625d), approved by Dewey in #1618 comment 28923 (queue rev 1757).
Packet: agents/rocko/work/queue-124/r2/.

- Schema 3g to 3h: packages/bus/src/migrations/3g-3h.sql adds an append-only
  usage_records table; nothing earlier changes. usage_records is in timedTables.
- The transcript usage-source module reads token counts from Pi and Claude
  Code session transcripts and follows no symlink.
- `mosaic usage` in the CLI and a read-only usage section in the Console's
  Settings (one GET route; 400 on a query string).

Decision 123 go: Mos, mos-0737. Activation (3g to 3h) is a migrating run
with Jason present, in Mos's combined re-pin; landing changes nothing live.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-18 11:08:17 -05:00

26 lines
1.5 KiB
JavaScript

import test from 'node:test';
import assert from 'node:assert/strict';
import { startServer } from '../src/serve.mjs';
import { close } from './fixture.mjs';
const count = { input: 2, output: 3, cacheWrite: 4, cacheRead: 5, unknownTurns: 1 };
export const usage = { total: count, byRun: [{ run: 'run1', ...count }], byInstance: [{ instance: 'coder', ...count }],
byModel: [{ provider: 'provider', model: 'model', ...count }], byProvider: [{ provider: 'provider', ...count }], byAccount: [{ account: null, ...count }] };
test('REQ-WEB-1 usage read keeps unknown counts and each token kind; no query or write is accepted', async t => {
let calls = 0;
const server = await startServer({ port: 0, bus: { usage: async () => { calls++; return usage; } } });
t.after(() => close(server));
const url = `http://127.0.0.1:${server.address().port}/api/bus/usage`;
const response = await fetch(url), body = await response.json();
assert.equal(response.status, 200); assert.deepEqual(body.rows, [usage]);
assert.equal((await fetch(url + '?business=other')).status, 400);
assert.equal((await fetch(url, { method: 'POST' })).status, 405);
assert.equal(calls, 1);
});
test('REQ-WEB-1 malformed usage is refused instead of shown as zero', async t => {
const server = await startServer({ port: 0, bus: { usage: async () => ({}) } });
t.after(() => close(server));
const response = await fetch(`http://127.0.0.1:${server.address().port}/api/bus/usage`);
assert.equal(response.status, 502);
});