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

usageAggregation.test.ts

129 lines4.0 KB

1import { describe, expect, it } from "@effect/vitest";
2​
3import { UsageAggregator } from "./usageAggregation.ts";
4import { parseRateTable } from "./usagePricing.ts";
5import type { UsageRecord } from "./usageTranscripts.ts";
6​
7const rates = parseRateTable({
8 "claude-fable-5": {
9 input_cost_per_token: 1e-5,
10 output_cost_per_token: 5e-5,
11 cache_read_input_token_cost: 1e-6,
12 cache_creation_input_token_cost: 1.25e-5,
13 },
14});
15​
16function record(overrides: Partial<UsageRecord> = {}): UsageRecord {
17 return {
18 provider: "claude",
19 // 2026-08-07T04:05Z is still Aug 6 in Los Angeles.
20 timestampMs: Date.parse("2026-08-07T04:05:13.944Z"),
21 model: "claude-fable-5",
22 sessionId: "session-a",
23 totals: {
24 uncachedInputTokens: 100,
25 cachedInputTokens: 1000,
26 cacheCreationTokens: 10,
27 outputTokens: 50,
28 reasoningTokens: 0,
29 },
30 reportedCostUsd: null,
31 dedupeKey: null,
32 ...overrides,
33 };
35​
36function aggregate(records: readonly UsageRecord[], timeZone = "UTC") {
37 const aggregator = new UsageAggregator({
38 timeZone,
39 sinceDay: "2026-08-01",
40 untilDay: "2026-08-31",
41 rates,
42 });
43 for (const item of records) aggregator.add(item);
44 return aggregator.finish();
46​
47describe("UsageAggregator", () => {
48 it("keeps only the first record for a repeated dedupe key", () => {
49 const result = aggregate([
50 record({ dedupeKey: "msg_1:" }),
51 record({ dedupeKey: "msg_1:" }),
52 record({ dedupeKey: "msg_1:" }),
53 ]);
54​
55 expect(result.duplicatesDropped).toBe(2);
56 expect(result.buckets).toHaveLength(1);
57 expect(result.buckets[0]?.records).toBe(1);
58 expect(result.buckets[0]?.totals.outputTokens).toBe(50);
59 });
60​
61 it("still sums records that carry no dedupe key", () => {
62 const result = aggregate([record(), record()]);
63​
64 expect(result.duplicatesDropped).toBe(0);
65 expect(result.buckets[0]?.totals.outputTokens).toBe(100);
66 });
67​
68 it("buckets by the day in the requested time zone", () => {
69 const utc = aggregate([record()], "UTC");
70 const losAngeles = aggregate([record()], "America/Los_Angeles");
71​
72 expect(utc.buckets[0]?.day).toBe("2026-08-07");
73 expect(losAngeles.buckets[0]?.day).toBe("2026-08-06");
74 });
75​
76 it("prices against the rate table", () => {
77 const result = aggregate([record()]);
78​
79 // 100*1e-5 + 1000*1e-6 + 10*1.25e-5 + 50*5e-5
80 expect(result.buckets[0]?.costUsd).toBeCloseTo(0.004625, 9);
81 expect(result.buckets[0]?.costSource).toBe("modelPriced");
82 });
83​
84 it("counts tokens but not cost for a model with no rate", () => {
85 const result = aggregate([record({ model: "kimi-k3" })]);
86​
87 expect(result.buckets[0]?.costUsd).toBe(0);
88 expect(result.buckets[0]?.costSource).toBe("unpriced");
89 expect(result.buckets[0]?.unpricedRecords).toBe(1);
90 expect(result.buckets[0]?.totals.outputTokens).toBe(50);
91 });
92​
93 it("prefers a reported cost over the rate table", () => {
94 const result = aggregate([record({ reportedCostUsd: 1.25 })]);
95​
96 expect(result.buckets[0]?.costUsd).toBe(1.25);
97 expect(result.buckets[0]?.costSource).toBe("providerReported");
98 });
99​
100 it("drops records outside the window", () => {
101 const result = aggregate([record({ timestampMs: Date.parse("2026-07-01T12:00:00Z") })]);
102​
103 expect(result.outOfWindow).toBe(1);
104 expect(result.buckets).toHaveLength(0);
105 });
106​
107 it("reports whether a record contributed", () => {
108 const aggregator = new UsageAggregator({
109 timeZone: "UTC",
110 sinceDay: "2026-08-01",
111 untilDay: "2026-08-31",
112 rates,
113 });
114​
115 expect(aggregator.add(record({ dedupeKey: "msg_1:" }))).toBe(true);
116 expect(aggregator.add(record({ dedupeKey: "msg_1:" }))).toBe(false);
117 expect(aggregator.add(record({ timestampMs: Date.parse("2026-07-01T12:00:00Z") }))).toBe(false);
118 });
119​
120 it("separates providers and models into their own buckets", () => {
121 const result = aggregate([
122 record(),
123 record({ provider: "codex", model: "gpt-5.6-sol" }),
124 record({ model: "claude-opus-5" }),
125 ]);
126​
127 expect(result.buckets).toHaveLength(3);
128 });
129});