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

UsageProviderChart.test.ts

135 lines4.2 KB

1import { describe, expect, it } from "vite-plus/test";
2​
3import { buildDayColumns, 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("buildDayColumns", () => {
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(buildDayColumns(days, byDay, "cost").map((column) => column.total)).toEqual([30, 0, 5]);
73 });
74​
75 it("reads the requested metric", () => {
76 expect(buildDayColumns(days, byDay, "tokens").map((column) => column.total)).toEqual([
77 300, 0, 50,
78 ]);
79 });
80​
81 it("keeps band values absolute rather than cumulative", () => {
82 // Regression: the bands were once stack offsets, which drew Claude Code
83 // permanently above Codex regardless of which provider spent more.
84 const [first] = buildDayColumns(days, byDay, "cost");
85​
86 expect(first?.bands).toEqual([
87 { provider: "codex", value: 10 },
88 { provider: "claude", value: 20 },
89 { provider: "grok", value: 0 },
90 ]);
91 });
92​
93 it("reports the total as the sum of its bands", () => {
94 for (const column of buildDayColumns(days, byDay, "cost")) {
95 const sum = column.bands.reduce((running, band) => running + band.value, 0);
96 expect(column.total).toBeCloseTo(sum, 9);
97 }
98 });
99});
100​
101describe("providersWithUsage", () => {
102 it("omits providers with no cost or tokens", () => {
103 expect(
104 providersWithUsage([
105 { provider: "codex", costUsd: 0, totalTokens: 0 },
106 { provider: "claude", costUsd: 0, totalTokens: 200 },
107 ]),
108 ).toEqual(["claude"]);
109 });
110});
111​
112describe("hourly chart columns", () => {
113 it("zero-fills inactive hours and preserves hourly provider values", () => {
114 const byHour = new Map([
115 [
116 "2026-08-11T09:37:00.000Z",
117 {
118 day: "2026-08-11",
119 hourStart: "2026-08-11T09:37:00.000Z",
120 costUsd: 4,
121 totalTokens: 40,
122 byProvider: new Map([["codex" as const, { costUsd: 4, totalTokens: 40 }]]),
123 },
124 ],
125 ]);
126​
127 expect(
128 buildDayColumns(
129 ["2026-08-11T08:37:00.000Z", "2026-08-11T09:37:00.000Z", "2026-08-11T10:37:00.000Z"],
130 byHour,
131 "cost",
132 ).map((column) => column.total),
133 ).toEqual([0, 4, 0]);
134 });
135});