Open History to see the last commit
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 });
31}
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).toBeDefined();
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 // Content-block records repeat full parent usage; summing them overcounts.
53 const [text] = parseClaudeLine(claudeLine({ messageId: "msg_2", contentType: "text" }));
54 const [toolUse] = parseClaudeLine(claudeLine({ messageId: "msg_2", contentType: "tool_use" }));
55
56 expect(text?.dedupeKey).toBe(toolUse?.dedupeKey);
57 expect(text?.totals).toEqual(toolUse?.totals);
58 });
59
60 it("skips local placeholders in both reader paths but keeps real zero-token responses", () => {
61 const projected = {
62 type: "assistant",
63 timestamp: "2026-08-07T04:05:13.944Z",
64 message: {
65 model: "claude-fable-5",
66 usage: { input_tokens: 0, output_tokens: 0 },
67 },
68 };
69 const expected = [
70 expect.objectContaining({
71 model: "claude-fable-5",
72 totals: {
73 uncachedInputTokens: 0,
74 cachedInputTokens: 0,
75 cacheCreationTokens: 0,
76 outputTokens: 0,
77 reasoningTokens: 0,
78 },
79 }),
80 ];
81 expect(claudeUsageFormat.parseLine(JSON.stringify(projected), undefined)).toEqual(expected);
82 expect(claudeUsageFormat.parseProjected(projected, undefined)).toEqual(expected);
83
84 const synthetic = { ...projected, message: { ...projected.message, model: "<synthetic>" } };
85 expect(claudeUsageFormat.parseLine(JSON.stringify(synthetic), undefined)).toEqual([]);
86 expect(claudeUsageFormat.parseProjected(synthetic, undefined)).toEqual([]);
87 });
88});
89
90describe("Claude advisor iterations", () => {
91 const advisorTurn = {
92 type: "assistant",
93 timestamp: "2026-10-22T01:31:11.984Z",
94 sessionId: "5a128faa-8253-489e-b935-6c08e8e670c0",
95 requestId: "req_1",
96 message: {
97 id: "msg_1",
98 model: "claude-opus-5-5",
99 usage: {
100 input_tokens: 4,
101 cache_read_input_tokens: 118320,
102 cache_creation_input_tokens: 2676,
103 output_tokens: 4506,
104 speed: "standard",
105 iterations: [
106 {
107 type: "message",
108 input_tokens: 2,
109 cache_read_input_tokens: 58352,
110 cache_creation_input_tokens: 1616,
111 output_tokens: 141,
112 },
113 {
114 type: "advisor_message",
115 model: "claude-fable-5-1",
116 input_tokens: 54204,
117 cache_read_input_tokens: 0,
118 cache_creation_input_tokens: 0,
119 output_tokens: 4229,
120 },
121 {
122 type: "message",
123 input_tokens: 2,
124 cache_read_input_tokens: 59968,
125 cache_creation_input_tokens: 1060,
126 output_tokens: 4365,
127 },
128 ],
129 },
130 },
131 };
132 const withIterations = (iterations: readonly unknown[]) => ({
133 ...advisorTurn,
134 message: { ...advisorTurn.message, usage: { ...advisorTurn.message.usage, iterations } },
135 });
136 const parse = (turn: unknown) => claudeUsageFormat.parseLine(JSON.stringify(turn), undefined);
137
138 it("reports the advisor model's tokens as their own record beside the unchanged executor record", () => {
139 const executor = {
140 provider: "claude",
141 timestampMs: Date.parse("2026-10-22T01:31:11.984Z"),
142 model: "claude-opus-5-5",
143 sessionId: "5a128faa-8253-489e-b935-6c08e8e670c0",
144 totals: {
145 uncachedInputTokens: 4,
146 cachedInputTokens: 118320,
147 cacheCreationTokens: 2676,
148 outputTokens: 4506,
149 reasoningTokens: 0,
150 },
151 reportedCostUsd: null,
152 speed: "standard",
153 dedupeKey: "msg_1:req_1",
154 };
155 const expected = [
156 executor,
157 {
158 ...executor,
159 model: "claude-fable-5-1",
160 totals: {
161 uncachedInputTokens: 54204,
162 cachedInputTokens: 0,
163 cacheCreationTokens: 0,
164 outputTokens: 4229,
165 reasoningTokens: 0,
166 },
167 dedupeKey: expect.any(String),
168 },
169 ];
170
171 expect(parse(advisorTurn)).toEqual(expected);
172 expect(claudeUsageFormat.parseProjected(advisorTurn, undefined)).toEqual(expected);
173 });
174
175 it("keys the advisor record apart from the executor and the same across a message's content blocks", () => {
176 const block = (type: string) =>
177 parse({ ...advisorTurn, message: { ...advisorTurn.message, content: [{ type }] } });
178 const [executorKey, advisorKey] = block("text").map((record) => record.dedupeKey);
179
180 expect(advisorKey).toEqual(expect.any(String));
181 expect(advisorKey).not.toBe(executorKey);
182 expect(block("tool_use").map((record) => record.dedupeKey)).toEqual([executorKey, advisorKey]);
183
184 const unkeyed = parse({
185 ...advisorTurn,
186 requestId: undefined,
187 message: { ...advisorTurn.message, id: undefined },
188 });
189 expect(unkeyed.map((record) => record.dedupeKey)).toEqual([null, null]);
190 });
191
192 it("emits only the executor record without an advisor iteration", () => {
193 const executorIterations = advisorTurn.message.usage.iterations.filter(
194 (iteration) => iteration.type === "message",
195 );
196
197 expect(parse(withIterations(executorIterations))).toHaveLength(1);
198 expect(claudeUsageFormat.parseProjected(withIterations([]), undefined)).toHaveLength(1);
199 expect(
200 claudeUsageFormat.parseLine(
201 claudeLine({ messageId: "msg_3", contentType: "text" }),
202 undefined,
203 ),
204 ).toHaveLength(1);
205 });
206
207 it("ignores advisor iterations without a usable model and malformed entries", () => {
208 const unusable = withIterations([
209 null,
210 "advisor_message",
211 { type: "advisor_message", input_tokens: 5 },
212 { type: "advisor_message", model: "", input_tokens: 5 },
213 { type: "advisor_message", model: "<synthetic>", input_tokens: 5 },
214 ]);
215
216 expect(parse(unusable).map((record) => record.model)).toEqual(["claude-opus-5-5"]);
217 });
218});