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

UsageProviderChart.test.ts

122 lines3.8 KB

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