Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

claudeUsage.test.ts

89 lines3.0 KB

1import { describe, expect, it } from "@effect/vitest";
2​
3import { claudeUsageFormat, parseClaudeLine } from "./claudeUsage.ts";
4​
5function claudeLine(overrides: {
6 messageId: string;
7 contentType: string;
8 model?: string;
9 outputTokens?: number;
10 speed?: string;
11}): string {
12 return JSON.stringify({
13 type: "assistant",
14 timestamp: "2026-08-07T04:05:13.944Z",
15 sessionId: "5a128faa-8253-489e-b935-6c08e8e670c0",
16 cwd: "/home/theo/project",
17 message: {
18 id: overrides.messageId,
19 role: "assistant",
20 model: overrides.model ?? "claude-fable-5",
21 content: [{ type: overrides.contentType }],
22 usage: {
23 input_tokens: 2,
24 cache_creation_input_tokens: 66818,
25 cache_read_input_tokens: 1000,
26 output_tokens: overrides.outputTokens ?? 286,
27 ...(overrides.speed === undefined ? {} : { speed: overrides.speed }),
28 },
29 },
30 });
32​
33describe("parseClaudeLine", () => {
34 it("extracts token totals and a dedupe key", () => {
35 const record = parseClaudeLine(claudeLine({ messageId: "msg_1", contentType: "text" }));
36​
37 expect(record).not.toBeNull();
38 expect(record?.provider).toBe("claude");
39 expect(record?.model).toBe("claude-fable-5");
40 expect(record?.totals).toEqual({
41 uncachedInputTokens: 2,
42 cachedInputTokens: 1000,
43 cacheCreationTokens: 66818,
44 outputTokens: 286,
45 reasoningTokens: 0,
46 });
47 expect(record?.dedupeKey).toBe("msg_1:");
48 expect(record?.speed).toBe("standard");
49 });
50​
51 it("gives every content block of one message the same dedupe key", () => {
52 // T3 Code writes one record per content block, each repeating the parent
53 // message's full usage. Summing them would overcount ~2.4x on real data.
54 const text = parseClaudeLine(claudeLine({ messageId: "msg_2", contentType: "text" }));
55 const toolUse = parseClaudeLine(claudeLine({ messageId: "msg_2", contentType: "tool_use" }));
56​
57 expect(text?.dedupeKey).toBe(toolUse?.dedupeKey);
58 expect(text?.totals).toEqual(toolUse?.totals);
59 });
60​
61 it("skips local placeholders in both reader paths but keeps real zero-token responses", () => {
62 const projected = {
63 type: "assistant",
64 timestamp: "2026-08-07T04:05:13.944Z",
65 message: {
66 model: "claude-fable-5",
67 usage: { input_tokens: 0, output_tokens: 0 },
68 },
69 };
70 const expected = [
71 expect.objectContaining({
72 model: "claude-fable-5",
73 totals: {
74 uncachedInputTokens: 0,
75 cachedInputTokens: 0,
76 cacheCreationTokens: 0,
77 outputTokens: 0,
78 reasoningTokens: 0,
79 },
80 }),
81 ];
82 expect(claudeUsageFormat.parseLine(JSON.stringify(projected), undefined)).toEqual(expected);
83 expect(claudeUsageFormat.parseProjected(projected, undefined)).toEqual(expected);
84​
85 const synthetic = { ...projected, message: { ...projected.message, model: "<synthetic>" } };
86 expect(claudeUsageFormat.parseLine(JSON.stringify(synthetic), undefined)).toEqual([]);
87 expect(claudeUsageFormat.parseProjected(synthetic, undefined)).toEqual([]);
88 });
89});