|
| 1 | +import { createRef, useImperativeHandle } from "react"; |
| 2 | +import { EnvironmentId, ProviderInstanceId } from "@t3tools/contracts"; |
| 3 | +import { act } from "react"; |
| 4 | +import { createRoot } from "react-dom/client"; |
| 5 | +import { expect, it, vi } from "vite-plus/test"; |
| 6 | + |
| 7 | +const mocks = vi.hoisted(() => ({ consume: vi.fn(), canManage: vi.fn() })); |
| 8 | +vi.mock("../../components/ui/Dialog", () => ({ alert: vi.fn() })); |
| 9 | +vi.mock("../../state/server", () => ({ serverEnvironment: { consumeResetCredit: {} } })); |
| 10 | +vi.mock("../../state/session", () => ({ |
| 11 | + readEnvironmentScope: mocks.canManage, |
| 12 | + useEnvironmentScope: () => true, |
| 13 | +})); |
| 14 | +vi.mock("../../state/use-atom-command", () => ({ useAtomCommand: () => mocks.consume })); |
| 15 | + |
| 16 | +import * as Dialog from "../../components/ui/Dialog"; |
| 17 | +import { useResetCredit } from "./useResetCredit"; |
| 18 | + |
| 19 | +const environmentId = EnvironmentId.make("reset-environment"); |
| 20 | +const input = { instanceId: ProviderInstanceId.make("codex") }; |
| 21 | +const success = { _tag: "Success", value: { outcome: "reset" } } as const; |
| 22 | +type Result = typeof success | { readonly _tag: "Failure"; readonly cause: { error: Error } }; |
| 23 | + |
| 24 | +function setup() { |
| 25 | + mocks.consume.mockReset().mockResolvedValue(success); |
| 26 | + mocks.canManage.mockReset().mockReturnValue(true); |
| 27 | + vi.mocked(Dialog.alert).mockClear(); |
| 28 | + const root = createRoot(document.createElement("div")); |
| 29 | + const ref = createRef<ReturnType<typeof useResetCredit>>(); |
| 30 | + function Probe() { |
| 31 | + const value = useResetCredit(environmentId, input); |
| 32 | + useImperativeHandle(ref, () => value); |
| 33 | + return null; |
| 34 | + } |
| 35 | + act(() => root.render(<Probe />)); |
| 36 | + return { |
| 37 | + get value() { |
| 38 | + if (ref.current === null) throw new Error("Reset-credit hook was not mounted"); |
| 39 | + return ref.current; |
| 40 | + }, |
| 41 | + [Symbol.dispose]: () => act(() => root.unmount()), |
| 42 | + }; |
| 43 | +} |
| 44 | + |
| 45 | +function approval() { |
| 46 | + const action = vi |
| 47 | + .mocked(Dialog.alert) |
| 48 | + .mock.lastCall?.[2]?.find((button) => button.text === "Use credit")?.onPress; |
| 49 | + if (action === undefined) throw new Error("Missing reset-credit confirmation"); |
| 50 | + return action; |
| 51 | +} |
| 52 | + |
| 53 | +it.each(["success", "failure", "rejection"] as const)( |
| 54 | + "coalesces overlapping approvals and permits a new attempt after %s", |
| 55 | + async (outcome) => { |
| 56 | + using fixture = setup(); |
| 57 | + let resolve!: (value: Result) => void; |
| 58 | + let reject!: (reason: Error) => void; |
| 59 | + const promise = new Promise<Result>((complete, fail) => { |
| 60 | + resolve = complete; |
| 61 | + reject = fail; |
| 62 | + }); |
| 63 | + const pending = { promise, resolve, reject }; |
| 64 | + mocks.consume.mockReturnValueOnce(pending.promise); |
| 65 | + fixture.value.confirm(); |
| 66 | + const first = approval(); |
| 67 | + fixture.value.confirm(); |
| 68 | + const second = approval(); |
| 69 | + act(() => { |
| 70 | + first(); |
| 71 | + second(); |
| 72 | + }); |
| 73 | + expect(mocks.consume).toHaveBeenCalledTimes(1); |
| 74 | + expect(mocks.consume).toHaveBeenCalledWith({ environmentId, input }); |
| 75 | + expect(fixture.value.busy).toBe(true); |
| 76 | + const prompts = vi.mocked(Dialog.alert).mock.calls.length; |
| 77 | + fixture.value.confirm(); |
| 78 | + expect(Dialog.alert).toHaveBeenCalledTimes(prompts); |
| 79 | + |
| 80 | + await act(async () => { |
| 81 | + if (outcome === "rejection") pending.reject(new Error("Disconnected")); |
| 82 | + else if (outcome === "failure") { |
| 83 | + pending.resolve({ _tag: "Failure", cause: { error: new Error("Denied") } }); |
| 84 | + } else pending.resolve(success); |
| 85 | + await pending.promise.catch(() => undefined); |
| 86 | + }); |
| 87 | + expect(fixture.value.busy).toBe(false); |
| 88 | + expect(fixture.value.status).toBe( |
| 89 | + outcome === "success" |
| 90 | + ? "Reset applied. Your windows have cleared." |
| 91 | + : outcome === "failure" |
| 92 | + ? "Denied" |
| 93 | + : "Could not use the reset credit.", |
| 94 | + ); |
| 95 | + await act(async () => { |
| 96 | + fixture.value.confirm(); |
| 97 | + approval()(); |
| 98 | + }); |
| 99 | + expect(mocks.consume).toHaveBeenCalledTimes(2); |
| 100 | + expect(fixture.value.busy).toBe(false); |
| 101 | + }, |
| 102 | +); |
| 103 | + |
| 104 | +it("rechecks management permission after the confirmation opens", () => { |
| 105 | + using fixture = setup(); |
| 106 | + fixture.value.confirm(); |
| 107 | + const confirm = approval(); |
| 108 | + mocks.canManage.mockReturnValue(false); |
| 109 | + act(() => confirm()); |
| 110 | + expect(mocks.consume).not.toHaveBeenCalled(); |
| 111 | + expect(fixture.value.busy).toBe(false); |
| 112 | +}); |
0 commit comments