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("./usageProviders", async (importOriginal) => {
61 const actual = await importOriginal<typeof import("./usageProviders")>();
62 return {
63 ...actual,
64 PROVIDER_PRESENTATION: {
65 codex: { color: "white", label: "Codex", mark: "span" },
66 claude: { color: "orange", label: "Claude Code", mark: "span" },
67 },
68 };
69});
70
71import { UsagePage } from "./UsagePage";
72
73const providerTotals = (codex: number, claude: number) =>
74 new Map([
75 ["codex", { costUsd: codex, totalTokens: codex * 1_000 }],
76 ["claude", { costUsd: claude, totalTokens: claude * 1_000 }],
77 ] as const);
78
79const modelTotals = Object.freeze([
80 {
81 model: "expensive-model",
82 provider: "claude" as const,
83 costUsd: 10,
84 totalTokens: 100,
85 records: 1,
86 costShare: 10 / 16,
87 },
88 {
89 model: "token-heavy-model",
90 provider: "codex" as const,
91 costUsd: 5,
92 totalTokens: 1_000,
93 records: 1,
94 costShare: 5 / 16,
95 },
96 {
97 model: "token-heavy-cheaper-model",
98 provider: "codex" as const,
99 costUsd: 1,
100 totalTokens: 1_000,
101 records: 1,
102 costShare: 1 / 16,
103 },
104]);
105
106beforeEach(() => {
107 testState.metric = "cost";
108 testState.breakdown = "time";
109 testState.useUsage.mockReturnValue({
110 merged: {
111 ...mergeUsage([], USAGE_CONTRACT_VERSION),
112 models: modelTotals,
113 hourly: [
114 {
115 day: "2026-08-10",
116 hourStart: "2026-08-10T13:37:00.000Z",
117 costUsd: 13,
118 totalTokens: 13_000,
119 byProvider: providerTotals(7, 6),
120 },
121 {
122 day: "2026-08-11",
123 hourStart: "2026-08-11T11:37:00.000Z",
124 costUsd: 11,
125 totalTokens: 11_000,
126 byProvider: providerTotals(6, 5),
127 },
128 ],
129 },
130 environments: [],
131 isPending: false,
132 isPartial: false,
133 refresh: vi.fn(),
134 });
135});
136
137describe("UsagePage hourly breakdown", () => {
138 it("keeps recent activity visible first without empty hourly rows", () => {
139 const markup = renderToStaticMarkup(<UsagePage />);
140 const body = markup.match(/<tbody>(.*?)<\/tbody>/)?.[1] ?? "";
141
142 expect(body.match(/<tr/g)).toHaveLength(2);
143 expect(body).toContain("$11.00");
144 expect(body).toContain("$13.00");
145 expect(body.indexOf("$11.00")).toBeLessThan(body.indexOf("$13.00"));
146 });
147
148 it("keeps chronological ordering when the token metric is selected", () => {
149 testState.metric = "tokens";
150
151 const markup = renderToStaticMarkup(<UsagePage />);
152 const body = markup.match(/<tbody>(.*?)<\/tbody>/)?.[1] ?? "";
153
154 expect(body).toMatch(/\$11\.00.*\$13\.00/);
155 });
156});
157
158describe("UsagePage model breakdown", () => {
159 it("sorts models by cost when the cost metric is selected", () => {
160 testState.breakdown = "model";
161
162 const markup = renderToStaticMarkup(<UsagePage />);
163 const body = markup.match(/<tbody>(.*?)<\/tbody>/)?.[1] ?? "";
164
165 expect(body).toMatch(/expensive-model.*token-heavy-model.*token-heavy-cheaper-model/);
166 });
167
168 it("sorts models by token usage when the token metric is selected", () => {
169 testState.metric = "tokens";
170 testState.breakdown = "model";
171
172 const markup = renderToStaticMarkup(<UsagePage />);
173 const body = markup.match(/<tbody>(.*?)<\/tbody>/)?.[1] ?? "";
174
175 expect(body).toMatch(/token-heavy-model.*token-heavy-cheaper-model.*expensive-model/);
176 expect(modelTotals.map((model) => model.model)).toEqual([
177 "expensive-model",
178 "token-heavy-model",
179 "token-heavy-cheaper-model",
180 ]);
181 });
182});