The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import type { EnvironmentThreadShell } from "@t3tools/client-runtime/state/shell"; |
| 2 | import type { EnvironmentId } from "@t3tools/contracts"; |
| 3 | import { |
| 4 | CHATGPT_USAGE_LIMIT_CODE, |
| 5 | CHATGPT_WORKSPACE_USAGE_LIMIT_CODE, |
| 6 | } from "@t3tools/shared/usageLimits"; |
| 7 | import * as Clipboard from "expo-clipboard"; |
| 8 | import type { ReactNode } from "react"; |
| 9 | import { act } from "react"; |
| 10 | import { createRoot } from "react-dom/client"; |
| 11 | import { describe, expect, it, vi } from "vite-plus/test"; |
| 12 | |
| 13 | type ButtonProps = { label: string; disabled?: boolean; onPress: () => void }; |
| 14 | |
| 15 | vi.mock("react-native", () => ({ |
| 16 | Linking: { openURL: vi.fn() }, |
| 17 | Pressable: ({ children, disabled, onPress }: ButtonProps & { children: ReactNode }) => ( |
| 18 | <button type="button" disabled={disabled} onClick={onPress}> |
| 19 | {children} |
| 20 | </button> |
| 21 | ), |
| 22 | View: ({ children }: { children: ReactNode }) => <div>{children}</div>, |
| 23 | })); |
| 24 | vi.mock("expo-clipboard", () => ({ setStringAsync: vi.fn() })); |
| 25 | vi.mock("../../components/ProviderIcon", () => ({ ProviderIcon: () => null })); |
| 26 | vi.mock("../../components/ui/AppText", () => ({ |
| 27 | AppText: ({ children }: { children: ReactNode }) => <span>{children}</span>, |
| 28 | })); |
| 29 | vi.mock("../../components/ui/Button", () => ({ |
| 30 | Button: ({ label, disabled, onPress }: ButtonProps) => ( |
| 31 | <button type="button" disabled={disabled} onClick={onPress}> |
| 32 | {label} |
| 33 | </button> |
| 34 | ), |
| 35 | })); |
| 36 | vi.mock("../../state/threads", () => ({ threadEnvironment: { updateMetadata: {} } })); |
| 37 | vi.mock("../../state/use-atom-command", () => ({ useAtomCommand: () => vi.fn() })); |
| 38 | |
| 39 | import { UsageLimitRecoveryCard } from "./UsageLimitRecoveryCard"; |
| 40 | |
| 41 | const DAY_MS = 24 * 60 * 60 * 1000; |
| 42 | |
| 43 | function failedThread( |
| 44 | resetAt: string | null, |
| 45 | lastErrorCode?: string, |
| 46 | lastErrorClass = "usage_limit", |
| 47 | ): EnvironmentThreadShell { |
| 48 | return { |
| 49 | id: "thread-1", |
| 50 | updatedAt: new Date(0).toISOString(), |
| 51 | snoozedUntil: null, |
| 52 | latestRun: { runId: "run-1", completedAt: new Date(0).toISOString() }, |
| 53 | limitRecovery: null, |
| 54 | runtime: { status: "failed", lastErrorClass, lastErrorCode, usageLimitResetAt: resetAt }, |
| 55 | } as unknown as EnvironmentThreadShell; |
| 56 | } |
| 57 | |
| 58 | describe("UsageLimitRecoveryCard", () => { |
| 59 | // Older servers report ChatGPT sharing limits as provider errors and may store a reset. |
| 60 | it.each([ |
| 61 | [CHATGPT_USAGE_LIMIT_CODE, "usage_limit", null, ["Manage usage"]], |
| 62 | [CHATGPT_WORKSPACE_USAGE_LIMIT_CODE, "usage_limit", null, ["Copy request for admin"]], |
| 63 | [CHATGPT_USAGE_LIMIT_CODE, "provider_error", null, ["Manage usage"]], |
| 64 | [ |
| 65 | CHATGPT_USAGE_LIMIT_CODE, |
| 66 | "usage_limit", |
| 67 | new Date(DAY_MS).toISOString(), |
| 68 | ["Manage usage", "Resume at reset", "Snooze until reset"], |
| 69 | ], |
| 70 | ])("shows one ChatGPT notice for %s (%s, reset %s)", (code, errorClass, resetAt, actions) => { |
| 71 | using _timers = { [Symbol.dispose]: () => vi.useRealTimers() }; |
| 72 | vi.useFakeTimers({ now: 0 }); |
| 73 | const container = document.createElement("div"); |
| 74 | const root = createRoot(container); |
| 75 | using _root = { [Symbol.dispose]: () => act(() => root.unmount()) }; |
| 76 | act(() => |
| 77 | root.render( |
| 78 | <UsageLimitRecoveryCard |
| 79 | thread={failedThread(resetAt, code, errorClass)} |
| 80 | environmentId={"env-1" as EnvironmentId} |
| 81 | />, |
| 82 | ), |
| 83 | ); |
| 84 | expect(container.textContent).toContain("ChatGPT usage limit reached"); |
| 85 | expect(container.textContent).not.toContain("did not report a reset time"); |
| 86 | expect([...container.querySelectorAll("button")].map((button) => button.textContent)).toEqual( |
| 87 | actions, |
| 88 | ); |
| 89 | }); |
| 90 | |
| 91 | it("drops a failed copy's error once a retry copies the request", async () => { |
| 92 | vi.mocked(Clipboard.setStringAsync) |
| 93 | .mockRejectedValueOnce(new Error("denied")) |
| 94 | .mockResolvedValueOnce(true); |
| 95 | const container = document.createElement("div"); |
| 96 | const root = createRoot(container); |
| 97 | using _root = { [Symbol.dispose]: () => act(() => root.unmount()) }; |
| 98 | act(() => |
| 99 | root.render( |
| 100 | <UsageLimitRecoveryCard |
| 101 | thread={failedThread(null, CHATGPT_WORKSPACE_USAGE_LIMIT_CODE)} |
| 102 | environmentId={"env-1" as EnvironmentId} |
| 103 | />, |
| 104 | ), |
| 105 | ); |
| 106 | const copy = () => container.querySelector("button")!.click(); |
| 107 | await act(async () => copy()); |
| 108 | expect(container.textContent).toContain("Could not copy the request"); |
| 109 | await act(async () => copy()); |
| 110 | expect(container.textContent).toContain("Request copied"); |
| 111 | expect(container.textContent).not.toContain("Could not copy the request"); |
| 112 | }); |
| 113 | |
| 114 | it("ignores a second copy while the first is still in flight", async () => { |
| 115 | const first = Promise.withResolvers<boolean>(); |
| 116 | vi.mocked(Clipboard.setStringAsync).mockReset().mockReturnValueOnce(first.promise); |
| 117 | const container = document.createElement("div"); |
| 118 | const root = createRoot(container); |
| 119 | using _root = { [Symbol.dispose]: () => act(() => root.unmount()) }; |
| 120 | act(() => |
| 121 | root.render( |
| 122 | <UsageLimitRecoveryCard |
| 123 | thread={failedThread(null, CHATGPT_WORKSPACE_USAGE_LIMIT_CODE)} |
| 124 | environmentId={"env-1" as EnvironmentId} |
| 125 | />, |
| 126 | ), |
| 127 | ); |
| 128 | const copy = () => container.querySelector("button")!.click(); |
| 129 | act(() => copy()); |
| 130 | act(() => copy()); |
| 131 | expect(Clipboard.setStringAsync).toHaveBeenCalledTimes(1); |
| 132 | await act(async () => first.resolve(true)); |
| 133 | expect(container.textContent).toContain("Request copied"); |
| 134 | }); |
| 135 | |
| 136 | it("disables Snooze once a reset more than 24.8 days away passes", () => { |
| 137 | using _timers = { [Symbol.dispose]: () => vi.useRealTimers() }; |
| 138 | vi.useFakeTimers({ now: 0 }); |
| 139 | const container = document.createElement("div"); |
| 140 | const root = createRoot(container); |
| 141 | using _root = { [Symbol.dispose]: () => act(() => root.unmount()) }; |
| 142 | act(() => |
| 143 | root.render( |
| 144 | <UsageLimitRecoveryCard |
| 145 | thread={failedThread(new Date(30 * DAY_MS).toISOString())} |
| 146 | environmentId={"env-1" as EnvironmentId} |
| 147 | />, |
| 148 | ), |
| 149 | ); |
| 150 | const snooze = () => |
| 151 | [...container.querySelectorAll("button")].find( |
| 152 | (button) => button.textContent === "Snooze until reset", |
| 153 | ); |
| 154 | expect(snooze()?.disabled).toBe(false); |
| 155 | expect(container.textContent).toContain("Usage limit reached. Resets "); |
| 156 | expect([...container.querySelectorAll("button")].map((button) => button.textContent)).toEqual([ |
| 157 | "Resume at reset", |
| 158 | "Snooze until reset", |
| 159 | ]); |
| 160 | |
| 161 | act(() => vi.advanceTimersByTime(30 * DAY_MS)); |
| 162 | |
| 163 | expect(snooze()?.disabled).toBe(true); |
| 164 | expect(container.textContent).toContain("Usage limit reached. Reset "); |
| 165 | expect([...container.querySelectorAll("button")].map((button) => button.textContent)).toEqual([ |
| 166 | "Resume now", |
| 167 | "Snooze until reset", |
| 168 | ]); |
| 169 | }); |
| 170 | }); |