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

UsageProviderChart.test.ts

137 lines4.2 KB

1import { describe, expect, it } from "vite-plus/test";
2​
3import { buildPeriodColumns, niceScale } from "./UsageProviderChart";
4import { providersWithUsage } from "./usageProviders";
5​
6describe("niceScale", () => {
7 it("never puts the peak above the top of the scale", () => {
8 // Regression: an earlier version stopped at the last step below the peak,
9 // so the tallest day was drawn past the plot and clipped.
10 for (const peak of [1122.71, 999, 1, 0.04, 1_400_000_000, 37.5, 5000, 100.001]) {
11 const { max } = niceScale(peak, 4);
12 expect(max, `peak ${peak}`).toBeGreaterThanOrEqual(peak);
13 }
14 });
15​
16 it("starts at zero and ends at the maximum", () => {
17 const { max, ticks } = niceScale(1122.71, 4);
18​
19 expect(ticks[0]).toBe(0);
20 expect(ticks[ticks.length - 1]).toBeCloseTo(max, 6);
21 });
22​
23 it("uses evenly spaced 1/2/5 steps", () => {
24 const { ticks } = niceScale(1122.71, 4);
25 const steps = ticks.slice(1).map((tick, index) => tick - (ticks[index] ?? 0));
26​
27 for (const step of steps) expect(step).toBeCloseTo(steps[0] ?? 0, 6);
28 const [first = 0] = steps;
29 const normalized = first / 10 ** Math.floor(Math.log10(first));
30 expect([1, 2, 5, 10]).toContain(Math.round(normalized));
31 });
32​
33 it("keeps the tick count near the requested resolution", () => {
34 const { ticks } = niceScale(1122.71, 4);
35 expect(ticks.length).toBeGreaterThanOrEqual(3);
36 expect(ticks.length).toBeLessThanOrEqual(7);
37 });
38​
39 it("degrades to a single zero tick with no data", () => {
40 expect(niceScale(0, 4)).toEqual({ max: 0, ticks: [0] });
41 });
42});
43​
44describe("buildPeriodColumns", () => {
45 const days = ["2026-08-01", "2026-08-02", "2026-08-03"];
46 const byDay = new Map([
47 [
48 "2026-08-01",
49 {
50 day: "2026-08-01",
51 costUsd: 30,
52 totalTokens: 300,
53 byProvider: new Map([
54 ["codex" as const, { costUsd: 10, totalTokens: 100 }],
55 ["claude" as const, { costUsd: 20, totalTokens: 200 }],
56 ]),
57 },
58 ],
59 // 2026-08-02 is deliberately absent: a day with no activity.
60 [
61 "2026-08-03",
62 {
63 day: "2026-08-03",
64 costUsd: 5,
65 totalTokens: 50,
66 byProvider: new Map([["claude" as const, { costUsd: 5, totalTokens: 50 }]]),
67 },
68 ],
69 ]);
70​
71 it("plots each day on its own", () => {
72 expect(buildPeriodColumns(days, byDay, "cost").map((column) => column.total)).toEqual([
73 30, 0, 5,
74 ]);
75 });
76​
77 it("reads the requested metric", () => {
78 expect(buildPeriodColumns(days, byDay, "tokens").map((column) => column.total)).toEqual([
79 300, 0, 50,
80 ]);
81 });
82​
83 it("keeps band values absolute rather than cumulative", () => {
84 // Regression: the bands were once stack offsets, which drew Claude Code
85 // permanently above Codex regardless of which provider spent more.
86 const [first] = buildPeriodColumns(days, byDay, "cost");
87​
88 expect(first?.bands).toEqual([
89 { provider: "codex", value: 10 },
90 { provider: "claude", value: 20 },
91 { provider: "grok", value: 0 },
92 ]);
93 });
94​
95 it("reports the total as the sum of its bands", () => {
96 for (const column of buildPeriodColumns(days, byDay, "cost")) {
97 const sum = column.bands.reduce((running, band) => running + band.value, 0);
98 expect(column.total).toBeCloseTo(sum, 9);
99 }
100 });
101});
102​
103describe("providersWithUsage", () => {
104 it("omits providers with no cost or tokens", () => {
105 expect(
106 providersWithUsage([
107 { provider: "codex", costUsd: 0, totalTokens: 0 },
108 { provider: "claude", costUsd: 0, totalTokens: 200 },
109 ]),
110 ).toEqual(["claude"]);
111 });
112});
113​
114describe("hourly chart columns", () => {
115 it("zero-fills inactive hours and preserves hourly provider values", () => {
116 const byHour = new Map([
117 [
118 "2026-08-11T09:37:00.000Z",
119 {
120 day: "2026-08-11",
121 hourStart: "2026-08-11T09:37:00.000Z",
122 costUsd: 4,
123 totalTokens: 40,
124 byProvider: new Map([["codex" as const, { costUsd: 4, totalTokens: 40 }]]),
125 },
126 ],
127 ]);
128​
129 expect(
130 buildPeriodColumns(
131 ["2026-08-11T08:37:00.000Z", "2026-08-11T09:37:00.000Z", "2026-08-11T10:37:00.000Z"],
132 byHour,
133 "cost",
134 ).map((column) => column.total),
135 ).toEqual([0, 4, 0]);
136 });
137});