Open History to see the last commit
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 { provider: "cursor", value: 0 },
93 { provider: "opencode", value: 0 },
94 { provider: "antigravity", value: 0 },
95 ]);
96 });
97
98 it("reports the total as the sum of its bands", () => {
99 for (const column of buildPeriodColumns(days, byDay, "cost")) {
100 const sum = column.bands.reduce((running, band) => running + band.value, 0);
101 expect(column.total).toBeCloseTo(sum, 9);
102 }
103 });
104});
105
106describe("providersWithUsage", () => {
107 it("omits providers with no cost or tokens", () => {
108 expect(
109 providersWithUsage([
110 { provider: "codex", costUsd: 0, totalTokens: 0 },
111 { provider: "claude", costUsd: 0, totalTokens: 200 },
112 ]),
113 ).toEqual(["claude"]);
114 });
115});
116
117describe("hourly chart columns", () => {
118 it("zero-fills inactive hours and preserves hourly provider values", () => {
119 const byHour = new Map([
120 [
121 "2026-08-11T09:37:00.000Z",
122 {
123 day: "2026-08-11",
124 hourStart: "2026-08-11T09:37:00.000Z",
125 costUsd: 4,
126 totalTokens: 40,
127 byProvider: new Map([["codex" as const, { costUsd: 4, totalTokens: 40 }]]),
128 },
129 ],
130 ]);
131
132 expect(
133 buildPeriodColumns(
134 ["2026-08-11T08:37:00.000Z", "2026-08-11T09:37:00.000Z", "2026-08-11T10:37:00.000Z"],
135 byHour,
136 "cost",
137 ).map((column) => column.total),
138 ).toEqual([0, 4, 0]);
139 });
140});