Open History to see the last commit
1import { describe, expect, it } from "vite-plus/test";
2
3import { buildPeriodColumns, chartScale, niceScale } from "./UsageProviderChart";
4import { providersWithUsage } from "./usageProviders";
5
6describe("chartScale", () => {
7 const column = (codex: number) => ({
8 total: codex,
9 bands: [{ provider: "codex" as const, value: codex }],
10 });
11
12 it("holds unlabeled placeholder gridlines while loading providers have nothing to show", () => {
13 const scale = chartScale([column(0)], new Set(["codex" as const]));
14
15 expect(scale.labeled).toBe(false);
16 expect(scale.ticks.length).toBeGreaterThan(1);
17 });
18
19 it("scales to what is on screen, loading or not", () => {
20 expect(chartScale([column(40)], new Set(["codex" as const]))).toMatchObject({
21 max: 40,
22 labeled: true,
23 });
24 });
25});
26
27describe("niceScale", () => {
28 it("never puts the peak above the top of the scale", () => {
29 for (const peak of [1122.71, 999, 1, 0.04, 1_400_000_000, 37.5, 5000, 100.001]) {
30 const { max } = niceScale(peak, 4);
31 expect(max, `peak ${peak}`).toBeGreaterThanOrEqual(peak);
32 }
33 });
34
35 it("starts at zero and ends at the maximum", () => {
36 const { max, ticks } = niceScale(1122.71, 4);
37
38 expect(ticks[0]).toBe(0);
39 expect(ticks[ticks.length - 1]).toBeCloseTo(max, 6);
40 });
41
42 it("uses evenly spaced 1/2/5 steps", () => {
43 const { ticks } = niceScale(1122.71, 4);
44 const steps = ticks.slice(1).map((tick, index) => tick - (ticks[index] ?? 0));
45
46 for (const step of steps) expect(step).toBeCloseTo(steps[0] ?? 0, 6);
47 const [first = 0] = steps;
48 const normalized = first / 10 ** Math.floor(Math.log10(first));
49 expect([1, 2, 5, 10]).toContain(Math.round(normalized));
50 });
51
52 it("keeps the tick count near the requested resolution", () => {
53 const { ticks } = niceScale(1122.71, 4);
54 expect(ticks.length).toBeGreaterThanOrEqual(3);
55 expect(ticks.length).toBeLessThanOrEqual(7);
56 });
57
58 it("degrades to a single zero tick with no data", () => {
59 expect(niceScale(0, 4)).toEqual({ max: 0, ticks: [0] });
60 });
61});
62
63describe("buildPeriodColumns", () => {
64 const days = ["2026-08-01", "2026-08-02", "2026-08-03"];
65 const byDay = new Map([
66 [
67 "2026-08-01",
68 {
69 day: "2026-08-01",
70 costUsd: 35,
71 totalTokens: 350,
72 byProvider: new Map([
73 ["codex" as const, { costUsd: 10, totalTokens: 100 }],
74 ["claude" as const, { costUsd: 20, totalTokens: 200 }],
75 ["pi" as const, { costUsd: 5, totalTokens: 50 }],
76 ]),
77 },
78 ],
79 [
80 "2026-08-03",
81 {
82 day: "2026-08-03",
83 costUsd: 5,
84 totalTokens: 50,
85 byProvider: new Map([["claude" as const, { costUsd: 5, totalTokens: 50 }]]),
86 },
87 ],
88 ]);
89
90 it("plots each day on its own", () => {
91 expect(buildPeriodColumns(days, byDay, "cost").map((column) => column.total)).toEqual([
92 35, 0, 5,
93 ]);
94 });
95
96 it("reads the requested metric", () => {
97 expect(buildPeriodColumns(days, byDay, "tokens").map((column) => column.total)).toEqual([
98 350, 0, 50,
99 ]);
100 });
101
102 it("keeps band values absolute rather than cumulative", () => {
103 const [first] = buildPeriodColumns(days, byDay, "cost");
104
105 expect(first?.bands).toEqual([
106 { provider: "codex", value: 10 },
107 { provider: "claude", value: 20 },
108 { provider: "grok", value: 0 },
109 { provider: "cursor", value: 0 },
110 { provider: "opencode", value: 0 },
111 { provider: "antigravity", value: 0 },
112 { provider: "pi", value: 5 },
113 ]);
114 });
115
116 it("reports the total as the sum of its bands", () => {
117 for (const column of buildPeriodColumns(days, byDay, "cost")) {
118 const sum = column.bands.reduce((running, band) => running + band.value, 0);
119 expect(column.total).toBeCloseTo(sum, 9);
120 }
121 });
122});
123
124describe("providersWithUsage", () => {
125 it("omits providers with no cost or tokens", () => {
126 expect(
127 providersWithUsage([
128 { provider: "codex", costUsd: 0, totalTokens: 0 },
129 { provider: "claude", costUsd: 0, totalTokens: 200 },
130 ]),
131 ).toEqual(["claude"]);
132 });
133});
134
135describe("hourly chart columns", () => {
136 it("zero-fills inactive hours and preserves hourly provider values", () => {
137 const byHour = new Map([
138 [
139 "2026-08-11T09:37:00.000Z",
140 {
141 day: "2026-08-11",
142 hourStart: "2026-08-11T09:37:00.000Z",
143 costUsd: 4,
144 totalTokens: 40,
145 byProvider: new Map([["codex" as const, { costUsd: 4, totalTokens: 40 }]]),
146 },
147 ],
148 ]);
149
150 expect(
151 buildPeriodColumns(
152 ["2026-08-11T08:37:00.000Z", "2026-08-11T09:37:00.000Z", "2026-08-11T10:37:00.000Z"],
153 byHour,
154 "cost",
155 ).map((column) => column.total),
156 ).toEqual([0, 4, 0]);
157 });
158});