Files
Kilo-Org_kilocode/packages/opencode/test/cli/tui/usage.test.ts
T
Josh Holmer 14c279fcb4 tui: show token usage breakdown in sidebar for transparency
Adds input, output and cached token counts to session sidebar so users
can monitor their consumption while working.
2026-04-11 22:10:58 -04:00

62 lines
1.5 KiB
TypeScript

// kilocode_change - new file
import { describe, expect, test } from "bun:test"
import type { AssistantMessage, Message, UserMessage } from "@kilocode/sdk/v2"
import { formatCount, getUsage } from "../../../src/cli/cmd/tui/routes/session/usage"
function assistant(id: string, input: number, output: number, read: number): AssistantMessage {
return {
id,
sessionID: "ses_1",
role: "assistant",
time: { created: 1 },
parentID: "msg_parent",
modelID: "claude-sonnet",
providerID: "anthropic",
mode: "code",
agent: "code",
path: { cwd: "/tmp", root: "/tmp" },
cost: 0,
tokens: {
input,
output,
reasoning: 99,
cache: {
read,
write: 7,
},
},
}
}
function user(): UserMessage {
return {
id: "msg_user",
sessionID: "ses_1",
role: "user",
time: { created: 0 },
agent: "code",
model: {
providerID: "anthropic",
modelID: "claude-sonnet",
},
}
}
describe("session usage", () => {
test("sums input, output, and cache read across assistant messages only", () => {
const msg: Message[] = [user(), assistant("a", 1200, 45, 300), assistant("b", 800, 55, 700)]
expect(getUsage(msg)).toEqual({
input: 2000,
output: 100,
cached: 1000,
})
})
test("formats full counts with thousands separators", () => {
expect(formatCount(0)).toBe("0")
expect(formatCount(12345)).toBe("12,345")
expect(formatCount(9876543)).toBe("9,876,543")
})
})