Open History to see the last commit
1import {
2 EnvironmentId,
3 ProviderDriverKind,
4 ProviderInstanceId,
5 type ServerProvider,
6} from "@t3tools/contracts";
7import type { LimitPresentations } from "@t3tools/shared/usageLimits";
8import { act } from "react";
9import { createRoot, type Root } from "react-dom/client";
10import { afterEach, beforeEach, expect, it, vi } from "vite-plus/test";
11import { userEvent } from "vite-plus/test/browser";
12
13// Real pointer clicks need the dialog's stacking styles to land above Base UI's modal overlay.
14import "~/index.css";
15
16const consume = vi.hoisted(() => vi.fn());
17vi.mock("../../state/session", () => ({
18 useEnvironmentScope: () => true,
19 readEnvironmentScope: () => true,
20}));
21vi.mock("../../state/server", () => ({ serverEnvironment: { consumeResetCredit: null } }));
22vi.mock("../../state/use-atom-command", () => ({ useAtomCommand: () => consume }));
23vi.mock("../../hooks/useSettings", () => ({ usePrimarySettings: () => "24h" }));
24vi.mock("../chat/ProviderInstanceIcon", () => ({ ProviderInstanceIcon: () => null }));
25vi.mock("../settings/RedactedSensitiveText", () => ({ RedactedSensitiveText: () => null }));
26
27import { UsageLimitsPooled } from "./UsageLimitsPooled";
28
29const presentations: LimitPresentations = new Map([
30 [
31 EnvironmentId.make("env-a"),
32 {
33 entry: { target: { label: "Test" } },
34 serverConfig: {
35 usageLimitSources: [],
36 providers: [
37 {
38 instanceId: ProviderInstanceId.make("alpha"),
39 driver: ProviderDriverKind.make("codex"),
40 displayName: "Alpha",
41 enabled: true,
42 installed: true,
43 version: null,
44 status: "ready",
45 auth: { status: "authenticated", email: "alpha@example.com" },
46 checkedAt: "2026-09-03T11:00:00Z",
47 models: [],
48 slashCommands: [],
49 skills: [],
50 usageLimits: {
51 checkedAt: "2026-09-03T11:00:00Z",
52 resetCredits: { availableCount: 1 },
53 windows: [
54 {
55 id: "five_hour",
56 kind: "session",
57 label: "Session",
58 usedPercent: 40,
59 windowDurationMins: 300,
60 resetsAt: "2026-09-03T14:00:00Z",
61 },
62 ],
63 },
64 } satisfies ServerProvider,
65 ],
66 },
67 },
68 ],
69]);
70
71let root: Root;
72let container: HTMLDivElement;
73
74beforeEach(async () => {
75 vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
76 consume.mockReset();
77 container = document.createElement("div");
78 document.body.append(container);
79 root = createRoot(container);
80 await act(() => root.render(<UsageLimitsPooled presentations={presentations} now={0} />));
81});
82
83afterEach(async () => {
84 await act(() => root.unmount());
85 container.remove();
86 vi.unstubAllGlobals();
87});
88
89async function click(label: string) {
90 const button = Array.from(document.querySelectorAll("button")).find(
91 (node) => node.textContent === label || node.getAttribute("aria-label")?.startsWith(label),
92 );
93 expect(button).toBeDefined();
94 await act(() => userEvent.click(button!));
95}
96
97it.each([
98 {
99 label: "success",
100 result: { _tag: "Success", value: { outcome: "reset" } },
101 text: "Reset applied. Your windows have cleared.",
102 },
103 {
104 label: "failure",
105 result: { _tag: "Failure", cause: { error: new Error("Codex rejected the credit.") } },
106 text: "Codex rejected the credit.",
107 },
108])("keeps pending feedback outside the closed popup until $label", async ({ result, text }) => {
109 let answer!: (result: unknown) => void;
110 consume.mockReturnValue(new Promise((resolve) => (answer = resolve)));
111 await click("Alpha:");
112 await click("Use reset");
113 expect(consume).not.toHaveBeenCalled();
114 await click("Cancel");
115 expect(consume).not.toHaveBeenCalled();
116 await click("Alpha:");
117 await click("Use reset");
118 await click("Use credit");
119 expect(consume).toHaveBeenCalledOnce();
120 // Both popups unmount once their exit animations finish.
121 await expect.poll(() => document.querySelector('[role="alertdialog"]')).toBeNull();
122 await expect.poll(() => document.querySelector('[data-slot="popover-popup"]')).toBeNull();
123 const status = document.querySelector('[role="status"]');
124 expect(status?.textContent).toBe("Alpha Using reset credit…");
125 await click("Alpha:");
126 const using = Array.from(document.querySelectorAll("button")).find(
127 (node) => node.textContent === "Using…",
128 );
129 expect(using?.disabled).toBe(true);
130 await act(async () => answer(result));
131 expect(status?.textContent).toBe(`Alpha ${text}`);
132});