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}));
9
10vi.mock("react", async (importOriginal) => {
11 const actual = await importOriginal<typeof import("react")>();
12 return {
13 ...actual,
14 useState: vi.fn((initial: unknown) => [
15 typeof initial === "function"
16 ? {
17 days: 1,
18 window: {
19 sinceDay: "2026-08-10",
20 untilDay: "2026-08-11",
21 timeZone: "UTC",
22 resolution: "hour",
23 sinceTime: "2026-08-10T12:37:00.000Z",
24 untilTime: "2026-08-11T12:37:00.000Z",
25 },
26 }
27 : initial === "model"
28 ? "time"
29 : initial,
30 vi.fn(),
31 ]),
32 };
33});
34
35vi.mock("../../env", () => ({ isElectron: false }));
36vi.mock("../../state/usage", () => ({ useUsage: testState.useUsage }));
37vi.mock("../ui/button", () => ({ Button: "button" }));
38vi.mock("../ui/scroll-area", () => ({ ScrollArea: "div" }));
39vi.mock("../ui/select", () => ({
40 Select: "div",
41 SelectItem: "div",
42 SelectPopup: "div",
43 SelectTrigger: "div",
44 SelectValue: "div",
45}));
46vi.mock("../ui/sidebar", () => ({ SidebarInset: "div" }));
47vi.mock("../ui/toggle-group", () => ({ Toggle: "button", ToggleGroup: "div" }));
48vi.mock("../WorkspaceBreadcrumb", () => ({
49 WorkspaceBreadcrumb: "div",
50 WorkspaceBreadcrumbItem: "div",
51 WorkspaceBreadcrumbSeparator: "span",
52}));
53vi.mock("../WorkspacePageContainer", () => ({ WorkspacePageContainer: "main" }));
54vi.mock("../WorkspacePageHeader", () => ({ WorkspacePageHeader: "header" }));
55vi.mock("./UsageProviderChart", () => ({ UsageProviderChart: "div" }));
56vi.mock("./usageProviders", async (importOriginal) => {
57 const actual = await importOriginal<typeof import("./usageProviders")>();
58 return {
59 ...actual,
60 PROVIDER_PRESENTATION: {
61 codex: { color: "white", label: "Codex", mark: "span" },
62 claude: { color: "orange", label: "Claude Code", mark: "span" },
63 },
64 };
65});
66
67import { UsagePage } from "./UsagePage";
68
69const providerTotals = (codex: number, claude: number) =>
70 new Map([
71 ["codex", { costUsd: codex, totalTokens: codex * 1_000 }],
72 ["claude", { costUsd: claude, totalTokens: claude * 1_000 }],
73 ] as const);
74
75beforeEach(() => {
76 testState.useUsage.mockReturnValue({
77 merged: {
78 ...mergeUsage([], USAGE_CONTRACT_VERSION),
79 hourly: [
80 {
81 day: "2026-08-10",
82 hourStart: "2026-08-10T13:37:00.000Z",
83 costUsd: 13,
84 totalTokens: 13_000,
85 byProvider: providerTotals(7, 6),
86 },
87 {
88 day: "2026-08-11",
89 hourStart: "2026-08-11T11:37:00.000Z",
90 costUsd: 11,
91 totalTokens: 11_000,
92 byProvider: providerTotals(6, 5),
93 },
94 ],
95 },
96 environments: [],
97 isPending: false,
98 isPartial: false,
99 refresh: vi.fn(),
100 });
101});
102
103describe("UsagePage hourly breakdown", () => {
104 it("keeps recent activity visible first without empty hourly rows", () => {
105 const markup = renderToStaticMarkup(<UsagePage />);
106 const body = markup.match(/<tbody>(.*?)<\/tbody>/)?.[1] ?? "";
107
108 expect(body.match(/<tr/g)).toHaveLength(2);
109 expect(body).toContain("$11.00");
110 expect(body).toContain("$13.00");
111 expect(body.indexOf("$11.00")).toBeLessThan(body.indexOf("$13.00"));
112 });
113});