|
| 1 | +import { describe, expect, it } from "vitest" |
| 2 | + |
| 3 | +import { providerIdentifiers, type ClineMessage } from "@roo-code/types" |
| 4 | + |
| 5 | +import { summarizeUsageByModel } from "../usageByModel" |
| 6 | + |
| 7 | +const req = (data: Record<string, unknown>, ts = 1): ClineMessage => ({ |
| 8 | + ts, |
| 9 | + type: "say", |
| 10 | + say: "api_req_started", |
| 11 | + text: JSON.stringify(data), |
| 12 | +}) |
| 13 | + |
| 14 | +describe("summarizeUsageByModel", () => { |
| 15 | + it("groups requests by the provider+model recorded on each request", () => { |
| 16 | + const result = summarizeUsageByModel([ |
| 17 | + req({ |
| 18 | + apiProvider: providerIdentifiers.githubCopilot, |
| 19 | + modelId: "claude-opus-5.5", |
| 20 | + tokensIn: 1000, |
| 21 | + tokensOut: 100, |
| 22 | + cost: 0, |
| 23 | + }), |
| 24 | + req({ |
| 25 | + apiProvider: providerIdentifiers.githubCopilot, |
| 26 | + modelId: "claude-opus-5.5", |
| 27 | + tokensIn: 500, |
| 28 | + tokensOut: 50, |
| 29 | + cost: 0, |
| 30 | + }), |
| 31 | + req({ |
| 32 | + apiProvider: providerIdentifiers.gemini, |
| 33 | + modelId: "gemini-3.1-pro-preview", |
| 34 | + tokensIn: 200, |
| 35 | + tokensOut: 20, |
| 36 | + cost: 0.4, |
| 37 | + }), |
| 38 | + ]) |
| 39 | + |
| 40 | + expect(result).toEqual([ |
| 41 | + { |
| 42 | + apiProvider: providerIdentifiers.githubCopilot, |
| 43 | + modelId: "claude-opus-5.5", |
| 44 | + requests: 2, |
| 45 | + tokensIn: 1500, |
| 46 | + tokensOut: 150, |
| 47 | + cost: 0, |
| 48 | + }, |
| 49 | + { |
| 50 | + apiProvider: providerIdentifiers.gemini, |
| 51 | + modelId: "gemini-3.1-pro-preview", |
| 52 | + requests: 1, |
| 53 | + tokensIn: 200, |
| 54 | + tokensOut: 20, |
| 55 | + cost: 0.4, |
| 56 | + }, |
| 57 | + ]) |
| 58 | + }) |
| 59 | + |
| 60 | + it("keeps a task that switched providers mid-way split by what actually served each request", () => { |
| 61 | + const result = summarizeUsageByModel([ |
| 62 | + req({ |
| 63 | + apiProvider: providerIdentifiers.githubCopilot, |
| 64 | + modelId: "claude-opus-5.5", |
| 65 | + tokensIn: 100, |
| 66 | + tokensOut: 10, |
| 67 | + }), |
| 68 | + req({ apiProvider: providerIdentifiers.gemini, modelId: "gemini-pro", tokensIn: 300, tokensOut: 30 }), |
| 69 | + ]) |
| 70 | + expect(result.map((r) => r.apiProvider)).toEqual([ |
| 71 | + providerIdentifiers.githubCopilot, |
| 72 | + providerIdentifiers.gemini, |
| 73 | + ]) |
| 74 | + }) |
| 75 | + |
| 76 | + it("sums cache counters only when reported", () => { |
| 77 | + const [entry] = summarizeUsageByModel([ |
| 78 | + req({ |
| 79 | + apiProvider: providerIdentifiers.anthropic, |
| 80 | + modelId: "m", |
| 81 | + tokensIn: 1, |
| 82 | + tokensOut: 1, |
| 83 | + cacheReads: 5, |
| 84 | + cacheWrites: 2, |
| 85 | + }), |
| 86 | + req({ apiProvider: providerIdentifiers.anthropic, modelId: "m", tokensIn: 1, tokensOut: 1, cacheReads: 3 }), |
| 87 | + ]) |
| 88 | + expect(entry.cacheReads).toBe(8) |
| 89 | + expect(entry.cacheWrites).toBe(2) |
| 90 | + |
| 91 | + const [bare] = summarizeUsageByModel([req({ apiProvider: "a", modelId: "m", tokensIn: 1, tokensOut: 1 })]) |
| 92 | + expect(bare).not.toHaveProperty("cacheReads") |
| 93 | + }) |
| 94 | + |
| 95 | + it("puts requests recorded before this was tracked in an unrecorded entry rather than guessing", () => { |
| 96 | + const [entry] = summarizeUsageByModel([req({ apiProtocol: "openai", tokensIn: 10, tokensOut: 5, cost: 0 })]) |
| 97 | + expect(entry).toMatchObject({ apiProvider: undefined, modelId: undefined, requests: 1, tokensIn: 10 }) |
| 98 | + }) |
| 99 | + |
| 100 | + it("ignores in-flight placeholders and non-request messages", () => { |
| 101 | + const result = summarizeUsageByModel([ |
| 102 | + req({ apiProtocol: "openai", apiProvider: providerIdentifiers.gemini, modelId: "m" }), // no counts yet |
| 103 | + { ts: 2, type: "say", say: "text", text: "hello" }, |
| 104 | + { ts: 3, type: "say", say: "api_req_started", text: "not json" }, |
| 105 | + { ts: 4, type: "say", say: "api_req_started" }, |
| 106 | + ]) |
| 107 | + expect(result).toEqual([]) |
| 108 | + }) |
| 109 | + |
| 110 | + it("returns an empty list for no messages", () => { |
| 111 | + expect(summarizeUsageByModel([])).toEqual([]) |
| 112 | + }) |
| 113 | +}) |
0 commit comments