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

UsagePage.test.tsx

204 lines6.0 KB

1import { EnvironmentId, UsageDay, 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​
107const environments = [
108 {
109 environmentId: EnvironmentId.make("test-environment"),
110 label: "Test environment",
111 isPending: false,
112 error: null,
113 summary: {
114 contractVersion: USAGE_CONTRACT_VERSION,
115 readAt: "2026-08-11T12:37:00.000Z",
116 sinceDay: UsageDay.make("2026-08-10"),
117 untilDay: UsageDay.make("2026-08-11"),
118 timeZone: "UTC",
119 buckets: [],
120 sources: [],
121 pricing: { status: "fresh", source: "test", fetchedAt: null, knownModels: 1 },
122 scanDurationMs: 1,
123 },
124 },
125];
126​
127beforeEach(() => {
128 testState.metric = "cost";
129 testState.breakdown = "time";
130 testState.useUsage.mockReturnValue({
131 merged: {
132 ...mergeUsage([], USAGE_CONTRACT_VERSION),
133 models: modelTotals,
134 hourly: [
135 {
136 day: "2026-08-10",
137 hourStart: "2026-08-10T13:37:00.000Z",
138 costUsd: 13,
139 totalTokens: 13_000,
140 byProvider: providerTotals(7, 6),
141 },
142 {
143 day: "2026-08-11",
144 hourStart: "2026-08-11T11:37:00.000Z",
145 costUsd: 11,
146 totalTokens: 11_000,
147 byProvider: providerTotals(6, 5),
148 },
149 ],
150 },
151 environments,
152 selectedEnvironments: environments,
153 isPending: false,
154 isPartial: false,
155 refresh: vi.fn(),
156 });
157});
158​
159describe("UsagePage hourly breakdown", () => {
160 it("keeps recent activity visible first without empty hourly rows", () => {
161 const markup = renderToStaticMarkup(<UsagePage />);
162 const body = markup.match(/<tbody>(.*?)<\/tbody>/)?.[1] ?? "";
163​
164 expect(body.match(/<tr/g)).toHaveLength(2);
165 expect(body).toContain("$11.00");
166 expect(body).toContain("$13.00");
167 expect(body.indexOf("$11.00")).toBeLessThan(body.indexOf("$13.00"));
168 });
169​
170 it("keeps chronological ordering when the token metric is selected", () => {
171 testState.metric = "tokens";
172​
173 const markup = renderToStaticMarkup(<UsagePage />);
174 const body = markup.match(/<tbody>(.*?)<\/tbody>/)?.[1] ?? "";
175​
176 expect(body).toMatch(/\$11\.00.*\$13\.00/);
177 });
178});
179​
180describe("UsagePage model breakdown", () => {
181 it("sorts models by cost when the cost metric is selected", () => {
182 testState.breakdown = "model";
183​
184 const markup = renderToStaticMarkup(<UsagePage />);
185 const body = markup.match(/<tbody>(.*?)<\/tbody>/)?.[1] ?? "";
186​
187 expect(body).toMatch(/expensive-model.*token-heavy-model.*token-heavy-cheaper-model/);
188 });
189​
190 it("sorts models by token usage when the token metric is selected", () => {
191 testState.metric = "tokens";
192 testState.breakdown = "model";
193​
194 const markup = renderToStaticMarkup(<UsagePage />);
195 const body = markup.match(/<tbody>(.*?)<\/tbody>/)?.[1] ?? "";
196​
197 expect(body).toMatch(/token-heavy-model.*token-heavy-cheaper-model.*expensive-model/);
198 expect(modelTotals.map((model) => model.model)).toEqual([
199 "expensive-model",
200 "token-heavy-model",
201 "token-heavy-cheaper-model",
202 ]);
203 });
204});