|
| 1 | +import type { EnvironmentThreadShell } from "@t3tools/client-runtime/state/shell"; |
| 2 | +import type { EnvironmentId } from "@t3tools/contracts"; |
| 3 | +import type { ReactNode } from "react"; |
| 4 | +import { act } from "react"; |
| 5 | +import { createRoot } from "react-dom/client"; |
| 6 | +import { describe, expect, it, vi } from "vite-plus/test"; |
| 7 | + |
| 8 | +type ButtonProps = { label: string; disabled?: boolean; onPress: () => void }; |
| 9 | + |
| 10 | +vi.mock("react-native", () => ({ |
| 11 | + View: ({ children }: { children: ReactNode }) => <div>{children}</div>, |
| 12 | +})); |
| 13 | +vi.mock("../../components/ui/AppText", () => ({ |
| 14 | + AppText: ({ children }: { children: ReactNode }) => <span>{children}</span>, |
| 15 | +})); |
| 16 | +vi.mock("../../components/ui/Button", () => ({ |
| 17 | + Button: ({ label, disabled, onPress }: ButtonProps) => ( |
| 18 | + <button type="button" disabled={disabled} onClick={onPress}> |
| 19 | + {label} |
| 20 | + </button> |
| 21 | + ), |
| 22 | +})); |
| 23 | +vi.mock("../../state/threads", () => ({ threadEnvironment: { updateMetadata: {} } })); |
| 24 | +vi.mock("../../state/use-atom-command", () => ({ useAtomCommand: () => vi.fn() })); |
| 25 | + |
| 26 | +import { UsageLimitRecoveryCard } from "./UsageLimitRecoveryCard"; |
| 27 | + |
| 28 | +const DAY_MS = 24 * 60 * 60 * 1000; |
| 29 | + |
| 30 | +function failedThread(resetAt: string): EnvironmentThreadShell { |
| 31 | + return { |
| 32 | + id: "thread-1", |
| 33 | + updatedAt: new Date(0).toISOString(), |
| 34 | + snoozedUntil: null, |
| 35 | + latestRun: { runId: "run-1", completedAt: new Date(0).toISOString() }, |
| 36 | + limitRecovery: null, |
| 37 | + runtime: { status: "failed", lastErrorClass: "usage_limit", usageLimitResetAt: resetAt }, |
| 38 | + } as unknown as EnvironmentThreadShell; |
| 39 | +} |
| 40 | + |
| 41 | +describe("UsageLimitRecoveryCard", () => { |
| 42 | + it("disables Snooze once a reset more than 24.8 days away passes", () => { |
| 43 | + using _timers = { [Symbol.dispose]: () => vi.useRealTimers() }; |
| 44 | + vi.useFakeTimers({ now: 0 }); |
| 45 | + const container = document.createElement("div"); |
| 46 | + const root = createRoot(container); |
| 47 | + using _root = { [Symbol.dispose]: () => act(() => root.unmount()) }; |
| 48 | + act(() => |
| 49 | + root.render( |
| 50 | + <UsageLimitRecoveryCard |
| 51 | + thread={failedThread(new Date(30 * DAY_MS).toISOString())} |
| 52 | + environmentId={"env-1" as EnvironmentId} |
| 53 | + />, |
| 54 | + ), |
| 55 | + ); |
| 56 | + const snooze = () => |
| 57 | + [...container.querySelectorAll("button")].find( |
| 58 | + (button) => button.textContent === "Snooze until reset", |
| 59 | + ); |
| 60 | + expect(snooze()?.disabled).toBe(false); |
| 61 | + |
| 62 | + act(() => vi.advanceTimersByTime(30 * DAY_MS)); |
| 63 | + |
| 64 | + expect(snooze()?.disabled).toBe(true); |
| 65 | + }); |
| 66 | +}); |
0 commit comments