codeburn/tests/usage-aggregator-provider-scope.test.ts
iamtoruk 8cfaa358ac fix(dock): scope the glance Today row to the hovered provider
The Capacity Dock's glance popover rendered the machine-wide Today block in
every provider's card, so hovering Claude showed the total across every
provider as if it were Claude's.

CLI (add-only): ProviderCost and providerDetails now carry optional
inputTokens, outputTokens and sessions, summed per provider in all three
branches that build the provider list. They stay ABSENT when no day in the
period reported a breakdown, so a consumer can tell "no data" from zero.
STATUS_SNAPSHOT_RENDER_VERSION 4 -> 5, since a v4 snapshot's payload predates
the new fields; the daily cache already holds the tokens, so its version is
untouched.

App: the Today row reads the hovered provider's row out of providerDetails
(matched on the CLI provider id, which the dock's kimi entry bridges to
kimicode). Against a CLI that omits per-provider tokens the arrows drop out
rather than falling back to the global pair; a provider absent from a
breakdown that IS present reads as a real $0.00. The row keeps its fixed
frame, so the panel's computed height is unchanged either way.
2026-09-03 08:32:20 -07:00

117 lines
4.1 KiB
TypeScript

import { beforeAll, describe, expect, it, vi } from 'vitest'
import { buildMenubarPayloadForRange, overlayProviderDaySlices } from '../src/usage-aggregator.js'
import { getDateRange } from '../src/cli-date.js'
import { loadPricing } from '../src/models.js'
import type { DailyEntry, ProviderDaySlice } from '../src/daily-cache.js'
const parseAllSessions = vi.hoisted(() => vi.fn(async () => []))
vi.mock('../src/parser.js', async (importOriginal) => {
const mod = await importOriginal<typeof import('../src/parser.js')>()
return {
...mod,
parseAllSessions,
isSessionHydrationComplete: vi.fn(() => true),
sessionHydrationSnapshot: vi.fn(() => ({
complete: true,
deferredForFirstPaint: false,
indexedFiles: 0,
pendingFiles: 0,
})),
}
})
vi.mock('../src/daily-cache.js', async (importOriginal) => {
const mod = await importOriginal<typeof import('../src/daily-cache.js')>()
return {
...mod,
ensureCacheHydrated: vi.fn(async (parseSessions: (range: { start: Date; end: Date }) => Promise<unknown>) => {
await parseSessions({ start: new Date(0), end: new Date() })
return mod.emptyCache()
}),
loadDailyCache: vi.fn(async () => mod.emptyCache()),
}
})
describe('provider-scoped menubar aggregation', () => {
beforeAll(async () => {
await loadPricing()
})
it('never scans unrelated providers to render one selected provider', async () => {
parseAllSessions.mockClear()
await buildMenubarPayloadForRange(getDateRange('today'), {
provider: 'hermes',
optimize: false,
timeline: false,
})
expect(parseAllSessions.mock.calls.map(([, provider]) => provider))
.toEqual(['hermes'])
})
it('scopes the single-provider row to that provider period totals', async () => {
const payload = await buildMenubarPayloadForRange(getDateRange('today'), {
provider: 'hermes',
optimize: false,
timeline: false,
})
// The whole period IS this provider here, so its providerDetails row must
// mirror the period block rather than leave the dock glance without one.
expect(payload.current.providerDetails).toEqual([
expect.objectContaining({
id: 'hermes',
cost: payload.current.cost,
calls: payload.current.calls,
inputTokens: payload.current.inputTokens,
outputTokens: payload.current.outputTokens,
sessions: payload.current.sessions,
}),
])
})
it('overlays today without letting a shrunken parse rewrite settled history', () => {
const slice = (cost: number, calls: number): ProviderDaySlice => ({ cost, calls, savingsUSD: 0, sessions: calls })
const day = (date: string, providers: Record<string, ProviderDaySlice>): DailyEntry => ({
date,
cost: Object.values(providers).reduce((sum, provider) => sum + provider.cost, 0),
calls: Object.values(providers).reduce((sum, provider) => sum + provider.calls, 0),
sessions: Object.values(providers).reduce((sum, provider) => sum + (provider.sessions ?? 0), 0),
savingsUSD: 0,
inputTokens: 0,
outputTokens: 0,
cacheReadTokens: 0,
cacheWriteTokens: 0,
editTurns: 0,
oneShotTurns: 0,
models: {},
categories: {},
providers,
})
// 2026-08-20 is settled and its sources have partially aged off disk: the
// fresh parse sees 3 of the 100 calls the cache finalized, so the cache
// stays authoritative. 2026-08-21 is a settled day the cache never held
// (cold cache), where the fresh parse is the only source there is.
const result = overlayProviderDaySlices(
[
day('2026-08-20', { hermes: slice(10, 100), claude: slice(2, 3) }),
],
[
day('2026-08-20', { hermes: slice(0.3, 3) }),
day('2026-08-21', { hermes: slice(0.438, 6) }),
],
'hermes',
)
expect(result.map(entry => ({ date: entry.date, cost: entry.cost, calls: entry.calls }))).toEqual([
{ date: '2026-08-20', cost: 10, calls: 100 },
{ date: '2026-08-21', cost: 0.438, calls: 6 },
])
expect(result[0]!.providers).toEqual({ hermes: slice(10, 100) })
expect(result[1]!.providers).toEqual({ hermes: slice(0.438, 6) })
})
})