The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import { describe, expect, it } from "@effect/vitest"; |
| 2 | |
| 3 | import { parseRateTable, priceUsage } from "../../usage/usagePricing.ts"; |
| 4 | import { claudeUsageFormat, parseClaudeLine } from "./claudeUsage.ts"; |
| 5 | |
| 6 | function claudeLine(overrides: { |
| 7 | messageId: string; |
| 8 | contentType: string; |
| 9 | model?: string; |
| 10 | outputTokens?: number; |
| 11 | speed?: string; |
| 12 | }): string { |
| 13 | return JSON.stringify({ |
| 14 | type: "assistant", |
| 15 | timestamp: "2026-08-07T04:05:13.944Z", |
| 16 | sessionId: "5a128faa-8253-489e-b935-6c08e8e670c0", |
| 17 | cwd: "/home/theo/project", |
| 18 | message: { |
| 19 | id: overrides.messageId, |
| 20 | role: "assistant", |
| 21 | model: overrides.model ?? "claude-fable-5", |
| 22 | content: [{ type: overrides.contentType }], |
| 23 | usage: { |
| 24 | input_tokens: 2, |
| 25 | cache_creation_input_tokens: 66818, |
| 26 | cache_read_input_tokens: 1000, |
| 27 | output_tokens: overrides.outputTokens ?? 286, |
| 28 | ...(overrides.speed === undefined ? {} : { speed: overrides.speed }), |
| 29 | }, |
| 30 | }, |
| 31 | }); |
| 32 | } |
| 33 | |
| 34 | describe("parseClaudeLine", () => { |
| 35 | it("extracts token totals and a dedupe key", () => { |
| 36 | const [record] = parseClaudeLine(claudeLine({ messageId: "msg_1", contentType: "text" })); |
| 37 | |
| 38 | expect(record).toBeDefined(); |
| 39 | expect(record?.provider).toBe("claude"); |
| 40 | expect(record?.model).toBe("claude-fable-5"); |
| 41 | expect(record?.totals).toEqual({ |
| 42 | uncachedInputTokens: 2, |
| 43 | cachedInputTokens: 1000, |
| 44 | cacheCreationTokens: 66818, |
| 45 | outputTokens: 286, |
| 46 | reasoningTokens: 0, |
| 47 | }); |
| 48 | expect(record?.dedupeKey).toBe("msg_1:"); |
| 49 | expect(record?.speed).toBe("standard"); |
| 50 | }); |
| 51 | |
| 52 | it("gives every content block of one message the same dedupe key", () => { |
| 53 | // Content-block records repeat full parent usage; summing them overcounts. |
| 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 | }); |
| 90 | |
| 91 | describe("Claude advisor iterations", () => { |
| 92 | const advisorTurn = { |
| 93 | type: "assistant", |
| 94 | timestamp: "2026-10-22T01:31:11.984Z", |
| 95 | sessionId: "5a128faa-8253-489e-b935-6c08e8e670c0", |
| 96 | requestId: "req_1", |
| 97 | message: { |
| 98 | id: "msg_1", |
| 99 | model: "claude-opus-5-5", |
| 100 | usage: { |
| 101 | input_tokens: 4, |
| 102 | cache_read_input_tokens: 118320, |
| 103 | cache_creation_input_tokens: 2676, |
| 104 | output_tokens: 4506, |
| 105 | speed: "standard", |
| 106 | iterations: [ |
| 107 | { |
| 108 | type: "message", |
| 109 | input_tokens: 2, |
| 110 | cache_read_input_tokens: 58352, |
| 111 | cache_creation_input_tokens: 1616, |
| 112 | output_tokens: 141, |
| 113 | }, |
| 114 | { |
| 115 | type: "advisor_message", |
| 116 | model: "claude-fable-5-1", |
| 117 | input_tokens: 54204, |
| 118 | cache_read_input_tokens: 0, |
| 119 | cache_creation_input_tokens: 500, |
| 120 | cache_creation: { ephemeral_5m_input_tokens: 200, ephemeral_1h_input_tokens: 300 }, |
| 121 | output_tokens: 4229, |
| 122 | }, |
| 123 | { |
| 124 | type: "message", |
| 125 | input_tokens: 2, |
| 126 | cache_read_input_tokens: 59968, |
| 127 | cache_creation_input_tokens: 1060, |
| 128 | output_tokens: 4365, |
| 129 | }, |
| 130 | ], |
| 131 | }, |
| 132 | }, |
| 133 | }; |
| 134 | const withIterations = (iterations: readonly unknown[]) => ({ |
| 135 | ...advisorTurn, |
| 136 | message: { ...advisorTurn.message, usage: { ...advisorTurn.message.usage, iterations } }, |
| 137 | }); |
| 138 | const parse = (turn: unknown) => claudeUsageFormat.parseLine(JSON.stringify(turn), undefined); |
| 139 | |
| 140 | it("reports the advisor model's tokens as their own record beside the unchanged executor record", () => { |
| 141 | const executor = { |
| 142 | provider: "claude", |
| 143 | timestampMs: Date.parse("2026-10-22T01:31:11.984Z"), |
| 144 | model: "claude-opus-5-5", |
| 145 | sessionId: "5a128faa-8253-489e-b935-6c08e8e670c0", |
| 146 | totals: { |
| 147 | uncachedInputTokens: 4, |
| 148 | cachedInputTokens: 118320, |
| 149 | cacheCreationTokens: 2676, |
| 150 | outputTokens: 4506, |
| 151 | reasoningTokens: 0, |
| 152 | }, |
| 153 | reportedCostUsd: null, |
| 154 | speed: "standard", |
| 155 | dedupeKey: "msg_1:req_1", |
| 156 | }; |
| 157 | const expected = [ |
| 158 | executor, |
| 159 | { |
| 160 | ...executor, |
| 161 | model: "claude-fable-5-1", |
| 162 | totals: { |
| 163 | uncachedInputTokens: 54204, |
| 164 | cachedInputTokens: 0, |
| 165 | cacheCreationTokens: 500, |
| 166 | outputTokens: 4229, |
| 167 | reasoningTokens: 0, |
| 168 | }, |
| 169 | cacheCreation1hTokens: 300, |
| 170 | dedupeKey: expect.any(String), |
| 171 | }, |
| 172 | ]; |
| 173 | |
| 174 | expect(parse(advisorTurn)).toEqual(expected); |
| 175 | expect(claudeUsageFormat.parseProjected(advisorTurn, undefined)).toEqual(expected); |
| 176 | }); |
| 177 | |
| 178 | it("keys the advisor record apart from the executor and the same across a message's content blocks", () => { |
| 179 | const block = (type: string) => |
| 180 | parse({ ...advisorTurn, message: { ...advisorTurn.message, content: [{ type }] } }); |
| 181 | const [executorKey, advisorKey] = block("text").map((record) => record.dedupeKey); |
| 182 | |
| 183 | expect(advisorKey).toEqual(expect.any(String)); |
| 184 | expect(advisorKey).not.toBe(executorKey); |
| 185 | expect(block("tool_use").map((record) => record.dedupeKey)).toEqual([executorKey, advisorKey]); |
| 186 | |
| 187 | const unkeyed = parse({ |
| 188 | ...advisorTurn, |
| 189 | requestId: undefined, |
| 190 | message: { ...advisorTurn.message, id: undefined }, |
| 191 | }); |
| 192 | expect(unkeyed.map((record) => record.dedupeKey)).toEqual([null, null]); |
| 193 | }); |
| 194 | |
| 195 | it("emits only the executor record without an advisor iteration", () => { |
| 196 | const executorIterations = advisorTurn.message.usage.iterations.filter( |
| 197 | (iteration) => iteration.type === "message", |
| 198 | ); |
| 199 | |
| 200 | expect(parse(withIterations(executorIterations))).toHaveLength(1); |
| 201 | expect(claudeUsageFormat.parseProjected(withIterations([]), undefined)).toHaveLength(1); |
| 202 | expect( |
| 203 | claudeUsageFormat.parseLine( |
| 204 | claudeLine({ messageId: "msg_3", contentType: "text" }), |
| 205 | undefined, |
| 206 | ), |
| 207 | ).toHaveLength(1); |
| 208 | }); |
| 209 | |
| 210 | it("ignores advisor iterations without a usable model and malformed entries", () => { |
| 211 | const unusable = withIterations([ |
| 212 | null, |
| 213 | "advisor_message", |
| 214 | { type: "advisor_message", input_tokens: 5 }, |
| 215 | { type: "advisor_message", model: "", input_tokens: 5 }, |
| 216 | { type: "advisor_message", model: "<synthetic>", input_tokens: 5 }, |
| 217 | ]); |
| 218 | |
| 219 | expect(parse(unusable).map((record) => record.model)).toEqual(["claude-opus-5-5"]); |
| 220 | }); |
| 221 | }); |
| 222 | |
| 223 | describe("Claude cache write pricing", () => { |
| 224 | it("prices one-hour cache writes at the one-hour rate and the rest at the five-minute rate", () => { |
| 225 | const model = "claude-opus-4-5-20251101"; |
| 226 | const rates = parseRateTable({ |
| 227 | [model]: { |
| 228 | litellm_provider: "anthropic", |
| 229 | input_cost_per_token: 5e-6, |
| 230 | output_cost_per_token: 25e-6, |
| 231 | cache_read_input_token_cost: 5e-7, |
| 232 | cache_creation_input_token_cost: 6.25e-6, |
| 233 | cache_creation_input_token_cost_above_1hr: 1e-5, |
| 234 | }, |
| 235 | }); |
| 236 | const [record] = parseClaudeLine( |
| 237 | JSON.stringify({ |
| 238 | type: "assistant", |
| 239 | timestamp: "2026-08-07T04:05:13.944Z", |
| 240 | sessionId: "s", |
| 241 | message: { |
| 242 | id: "msg_ttl", |
| 243 | model, |
| 244 | usage: { |
| 245 | input_tokens: 0, |
| 246 | cache_read_input_tokens: 0, |
| 247 | cache_creation_input_tokens: 1_000_000, |
| 248 | cache_creation: { |
| 249 | ephemeral_5m_input_tokens: 400_000, |
| 250 | ephemeral_1h_input_tokens: 600_000, |
| 251 | }, |
| 252 | output_tokens: 0, |
| 253 | }, |
| 254 | }, |
| 255 | }), |
| 256 | ); |
| 257 | |
| 258 | if (record === undefined) throw new Error("expected a usage record"); |
| 259 | // 0.4M x $6.25/M + 0.6M x $10/M |
| 260 | expect(priceUsage(rates, record).costUsd).toBeCloseTo(8.5, 9); |
| 261 | }); |
| 262 | }); |