Open History to see the last commit
1import { USAGE_CONTRACT_VERSION } from "@t3tools/contracts";
2import { mergeUsage } from "@t3tools/shared/usageMerge";
3import { renderToStaticMarkup } from "react-dom/server";
4import { beforeEach, describe, expect, it, vi } from "vite-plus/test";
5
6const testState = vi.hoisted(() => ({
7 useUsage: vi.fn(),
8 metric: "cost" as "cost" | "tokens",
9 breakdown: "time" as "model" | "time",
10}));
11
12vi.mock("react", async (importOriginal) => {
13 const actual = await importOriginal<typeof import("react")>();
14 return {
15 ...actual,
16 useState: vi.fn((initial: unknown) => [
17 typeof initial === "function"
18 ? {
19 days: 1,
20 window: {
21 sinceDay: "2026-08-10",
22 untilDay: "2026-08-11",
23 timeZone: "UTC",
24 resolution: "hour",
25 sinceTime: "2026-08-10T12:37:00.000Z",
26 untilTime: "2026-08-11T12:37:00.000Z",
27 },
28 }
29 : initial === "cost"
30 ? testState.metric
31 : initial === "model"
32 ? testState.breakdown
33 : initial,
34 vi.fn(),
35 ]),
36 };
37});
38
39vi.mock("../../env", () => ({ isElectron: false }));
40vi.mock("../../state/usage", () => ({ useUsage: testState.useUsage }));
41vi.mock("../ui/button", () => ({ Button: "button" }));
42vi.mock("../ui/scroll-area", () => ({ ScrollArea: "div" }));
43vi.mock("../ui/select", () => ({
44 Select: "div",
45 SelectItem: "div",
46 SelectPopup: "div",
47 SelectTrigger: "div",
48 SelectValue: "div",
49}));
50vi.mock("../ui/sidebar", () => ({ SidebarInset: "div" }));
51vi.mock("../ui/toggle-group", () => ({ Toggle: "button", ToggleGroup: "div" }));
52vi.mock("../WorkspaceBreadcrumb", () => ({
53 WorkspaceBreadcrumb: "div",
54 WorkspaceBreadcrumbItem: "div",
55 WorkspaceBreadcrumbSeparator: "span",
56}));
57vi.mock("../WorkspacePageContainer", () => ({ WorkspacePageContainer: "main" }));
58vi.mock("../WorkspacePageHeader", () => ({ WorkspacePageHeader: "header" }));
59vi.mock("./UsageProviderChart", () => ({ UsageProviderChart: "div" }));
60vi.mock("./UsagePriceOverrides", () => ({ UsagePriceOverrides: () => null }));
61vi.mock("./usageProviders", async (importOriginal) => {
62 const actual = await importOriginal<typeof import("./usageProviders")>();
63 return {
64 ...actual,
65 PROVIDER_PRESENTATION: {
66 codex: { color: "white", label: "Codex", mark: "span" },
67 claude: { color: "orange", label: "Claude Code", mark: "span" },
68 },
69 };
70});
71
72import { UsagePage } from "./UsagePage";
73
74const providerTotals = (codex: number, claude: number) =>
75 new Map([
76 ["codex", { costUsd: codex, totalTokens: codex * 1_000 }],
77 ["claude", { costUsd: claude, totalTokens: claude * 1_000 }],
78 ] as const);
79
80const modelTotals = Object.freeze([
81 {
82 model: "expensive-model",
83 provider: "claude" as const,
84 costUsd: 10,
85 totalTokens: 100,
86 records: 1,
87 costShare: 10 / 16,
88 },
89 {
90 model: "token-heavy-model",
91 provider: "codex" as const,
92 costUsd: 5,
93 totalTokens: 1_000,
94 records: 1,
95 costShare: 5 / 16,
96 },
97 {
98 model: "token-heavy-cheaper-model",
99 provider: "codex" as const,
100 costUsd: 1,
101 totalTokens: 1_000,
102 records: 1,
103 costShare: 1 / 16,
104 },
105]);
106
107beforeEach(() => {
108 testState.metric = "cost";
109 testState.breakdown = "time";
110 testState.useUsage.mockReturnValue({
111 merged: {
112 ...mergeUsage([], USAGE_CONTRACT_VERSION),
113 models: modelTotals,
114 hourly: [
115 {
116 day: "2026-08-10",
117 hourStart: "2026-08-10T13:37:00.000Z",
118 costUsd: 13,
119 totalTokens: 13_000,
120 byProvider: providerTotals(7, 6),
121 },
122 {
123 day: "2026-08-11",
124 hourStart: "2026-08-11T11:37:00.000Z",
125 costUsd: 11,
126 totalTokens: 11_000,
127 byProvider: providerTotals(6, 5),
128 },
129 ],
130 },
131 environments: [],
132 isPending: false,
133 isPartial: false,
134 refresh: vi.fn(),
135 });
136});
137
138describe("UsagePage hourly breakdown", () => {
139 it("keeps recent activity visible first without empty hourly rows", () => {
140 const markup = renderToStaticMarkup(<UsagePage />);
141 const body = markup.match(/<tbody>(.*?)<\/tbody>/)?.[1] ?? "";
142
143 expect(body.match(/<tr/g)).toHaveLength(2);
144 expect(body).toContain("$11.00");
145 expect(body).toContain("$13.00");
146 expect(body.indexOf("$11.00")).toBeLessThan(body.indexOf("$13.00"));
147 });
148
149 it("keeps chronological ordering when the token metric is selected", () => {
150 testState.metric = "tokens";
151
152 const markup = renderToStaticMarkup(<UsagePage />);
153 const body = markup.match(/<tbody>(.*?)<\/tbody>/)?.[1] ?? "";
154
155 expect(body).toMatch(/\$11\.00.*\$13\.00/);
156 });
157});
158
159describe("UsagePage model breakdown", () => {
160 it("sorts models by cost when the cost metric is selected", () => {
161 testState.breakdown = "model";
162
163 const markup = renderToStaticMarkup(<UsagePage />);
164 const body = markup.match(/<tbody>(.*?)<\/tbody>/)?.[1] ?? "";
165
166 expect(body).toMatch(/expensive-model.*token-heavy-model.*token-heavy-cheaper-model/);
167 });
168
169 it("sorts models by token usage when the token metric is selected", () => {
170 testState.metric = "tokens";
171 testState.breakdown = "model";
172
173 const markup = renderToStaticMarkup(<UsagePage />);
174 const body = markup.match(/<tbody>(.*?)<\/tbody>/)?.[1] ?? "";
175
176 expect(body).toMatch(/token-heavy-model.*token-heavy-cheaper-model.*expensive-model/);
177 expect(modelTotals.map((model) => model.model)).toEqual([
178 "expensive-model",
179 "token-heavy-model",
180 "token-heavy-cheaper-model",
181 ]);
182 });
183});