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

usageAggregation.test.ts

132 lines4.0 KB

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