|
| 1 | +import { |
| 2 | + EnvironmentId, |
| 3 | + ProviderDriverKind, |
| 4 | + ProviderInstanceId, |
| 5 | + type ServerProvider, |
| 6 | +} from "@t3tools/contracts"; |
| 7 | +import type { LimitPresentations } from "@t3tools/shared/usageLimits"; |
| 8 | +import { act, type PropsWithChildren, type ReactElement } from "react"; |
| 9 | +import { create, type ReactTestRenderer } from "react-test-renderer"; |
| 10 | +import { afterEach, beforeEach, expect, it, vi } from "vite-plus/test"; |
| 11 | + |
| 12 | +const consume = vi.hoisted(() => vi.fn()); |
| 13 | + |
| 14 | +vi.mock("../../state/session", () => ({ |
| 15 | + useEnvironmentScope: () => true, |
| 16 | + readEnvironmentScope: () => true, |
| 17 | +})); |
| 18 | +vi.mock("../../state/server", () => ({ serverEnvironment: { consumeResetCredit: null } })); |
| 19 | +vi.mock("../../state/use-atom-command", () => ({ useAtomCommand: () => consume })); |
| 20 | +vi.mock("../../hooks/useSettings", () => ({ usePrimarySettings: () => "24h" })); |
| 21 | +vi.mock("../chat/ProviderInstanceIcon", () => ({ ProviderInstanceIcon: () => null })); |
| 22 | +vi.mock("../settings/RedactedSensitiveText", () => ({ RedactedSensitiveText: () => null })); |
| 23 | +vi.mock("../ui/button", () => ({ Button: "button" })); |
| 24 | +vi.mock("../ui/tooltip", () => ({ Tooltip: "div", TooltipPopup: "div", TooltipTrigger: "div" })); |
| 25 | +// Popovers and dialogs portal and animate; these keep only the open state the segment owns. |
| 26 | +vi.mock("../ui/popover", async () => { |
| 27 | + const React = await import("react"); |
| 28 | + const Open = React.createContext({ open: false, toggle: () => {} }); |
| 29 | + return { |
| 30 | + Popover: ({ |
| 31 | + open, |
| 32 | + onOpenChange, |
| 33 | + children, |
| 34 | + }: PropsWithChildren<{ open: boolean; onOpenChange: (open: boolean) => void }>) => { |
| 35 | + const value = React.useMemo( |
| 36 | + () => ({ open, toggle: () => onOpenChange(!open) }), |
| 37 | + [open, onOpenChange], |
| 38 | + ); |
| 39 | + return <Open.Provider value={value}>{children}</Open.Provider>; |
| 40 | + }, |
| 41 | + PopoverTrigger: ({ |
| 42 | + render, |
| 43 | + children, |
| 44 | + }: PropsWithChildren<{ render: ReactElement<{ onClick?: () => void }> }>) => |
| 45 | + React.cloneElement(render, { onClick: React.useContext(Open).toggle }, children), |
| 46 | + PopoverPopup: ({ children }: PropsWithChildren) => |
| 47 | + React.useContext(Open).open ? <div>{children}</div> : null, |
| 48 | + }; |
| 49 | +}); |
| 50 | +vi.mock("../ui/alert-dialog", () => ({ |
| 51 | + AlertDialog: ({ open, children }: PropsWithChildren<{ open: boolean }>) => |
| 52 | + open ? <div role="dialog">{children}</div> : null, |
| 53 | + AlertDialogPopup: "div", |
| 54 | + AlertDialogHeader: "div", |
| 55 | + AlertDialogTitle: "div", |
| 56 | + AlertDialogDescription: "div", |
| 57 | + AlertDialogFooter: "div", |
| 58 | + AlertDialogClose: "div", |
| 59 | +})); |
| 60 | + |
| 61 | +import { UsageLimitsPooled } from "./UsageLimitsPooled"; |
| 62 | + |
| 63 | +const presentations: LimitPresentations = new Map([ |
| 64 | + [ |
| 65 | + EnvironmentId.make("env-a"), |
| 66 | + { |
| 67 | + entry: { target: { label: "Test" } }, |
| 68 | + serverConfig: { |
| 69 | + usageLimitSources: [], |
| 70 | + providers: [ |
| 71 | + { |
| 72 | + instanceId: ProviderInstanceId.make("alpha"), |
| 73 | + driver: ProviderDriverKind.make("codex"), |
| 74 | + displayName: "Alpha", |
| 75 | + enabled: true, |
| 76 | + installed: true, |
| 77 | + version: null, |
| 78 | + status: "ready", |
| 79 | + auth: { status: "authenticated", email: "[email protected]" }, |
| 80 | + checkedAt: "2026-09-03T11:00:00Z", |
| 81 | + models: [], |
| 82 | + slashCommands: [], |
| 83 | + skills: [], |
| 84 | + usageLimits: { |
| 85 | + checkedAt: "2026-09-03T11:00:00Z", |
| 86 | + resetCredits: { availableCount: 1 }, |
| 87 | + windows: [ |
| 88 | + { |
| 89 | + id: "five_hour", |
| 90 | + kind: "session", |
| 91 | + label: "Session", |
| 92 | + usedPercent: 40, |
| 93 | + windowDurationMins: 300, |
| 94 | + resetsAt: "2026-09-03T14:00:00Z", |
| 95 | + }, |
| 96 | + ], |
| 97 | + }, |
| 98 | + } satisfies ServerProvider, |
| 99 | + ], |
| 100 | + }, |
| 101 | + }, |
| 102 | + ], |
| 103 | +]); |
| 104 | + |
| 105 | +let renderer: ReactTestRenderer; |
| 106 | + |
| 107 | +const button = (label: string) => |
| 108 | + renderer.root.find((node) => node.type === "button" && node.children.includes(label)); |
| 109 | +const segment = () => |
| 110 | + renderer.root.find( |
| 111 | + (node) => node.type === "button" && node.props["aria-label"]?.startsWith("Alpha:"), |
| 112 | + ); |
| 113 | +const text = (node: ReactTestRenderer["root"] | string): string => |
| 114 | + typeof node === "string" ? node : node.children.map(text).join(""); |
| 115 | +const status = () => |
| 116 | + renderer.root |
| 117 | + .findAll((node) => node.props.role === "status") |
| 118 | + .map(text) |
| 119 | + .join(" "); |
| 120 | + |
| 121 | +/** Confirms a reset whose answer the test releases, closing the popover the way a click does. */ |
| 122 | +async function confirmHeldReset() { |
| 123 | + let answer!: (result: unknown) => void; |
| 124 | + consume.mockReturnValue(new Promise((resolve) => (answer = resolve))); |
| 125 | + await act(() => { |
| 126 | + renderer = create(<UsageLimitsPooled presentations={presentations} now={0} />); |
| 127 | + }); |
| 128 | + await act(() => segment().props.onClick()); |
| 129 | + await act(() => button("Use reset").props.onClick()); |
| 130 | + await act(() => button("Use credit").props.onClick()); |
| 131 | + return answer; |
| 132 | +} |
| 133 | + |
| 134 | +beforeEach(() => { |
| 135 | + vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true); |
| 136 | + consume.mockReset(); |
| 137 | +}); |
| 138 | + |
| 139 | +afterEach(async () => { |
| 140 | + await act(() => renderer.unmount()); |
| 141 | + vi.unstubAllGlobals(); |
| 142 | +}); |
| 143 | + |
| 144 | +it("shows the pending reset after its popover closes, then the outcome", async () => { |
| 145 | + const answer = await confirmHeldReset(); |
| 146 | + expect(consume).toHaveBeenCalledOnce(); |
| 147 | + expect(status()).toBe("Alpha Using reset credit…"); |
| 148 | + |
| 149 | + await act(() => segment().props.onClick()); |
| 150 | + expect(button("Using…").props.disabled).toBe(true); |
| 151 | + |
| 152 | + await act(async () => answer({ _tag: "Success", value: { outcome: "reset" } })); |
| 153 | + expect(status()).toBe("Alpha Reset applied. Your windows have cleared."); |
| 154 | +}); |
| 155 | + |
| 156 | +it("replaces the pending reset with the failure", async () => { |
| 157 | + const answer = await confirmHeldReset(); |
| 158 | + await act(async () => |
| 159 | + answer({ _tag: "Failure", cause: { error: new Error("Codex rejected the credit.") } }), |
| 160 | + ); |
| 161 | + expect(status()).toBe("Alpha Codex rejected the credit."); |
| 162 | +}); |
0 commit comments