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

UsageLimitRecoveryCard.test.tsx

66 lines2.4 KB

1import type { EnvironmentThreadShell } from "@t3tools/client-runtime/state/shell";
2import type { EnvironmentId } from "@t3tools/contracts";
3import type { ReactNode } from "react";
4import { act } from "react";
5import { createRoot } from "react-dom/client";
6import { describe, expect, it, vi } from "vite-plus/test";
7​
8type ButtonProps = { label: string; disabled?: boolean; onPress: () => void };
9​
10vi.mock("react-native", () => ({
11 View: ({ children }: { children: ReactNode }) => <div>{children}</div>,
12}));
13vi.mock("../../components/ui/AppText", () => ({
14 AppText: ({ children }: { children: ReactNode }) => <span>{children}</span>,
15}));
16vi.mock("../../components/ui/Button", () => ({
17 Button: ({ label, disabled, onPress }: ButtonProps) => (
18 <button type="button" disabled={disabled} onClick={onPress}>
19 {label}
20 </button>
21 ),
22}));
23vi.mock("../../state/threads", () => ({ threadEnvironment: { updateMetadata: {} } }));
24vi.mock("../../state/use-atom-command", () => ({ useAtomCommand: () => vi.fn() }));
25​
26import { UsageLimitRecoveryCard } from "./UsageLimitRecoveryCard";
27​
28const DAY_MS = 24 * 60 * 60 * 1000;
29​
30function 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;
40​
41describe("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});