The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { managedRelaySessionAtom } from "@t3tools/client-runtime/relay"; |
| 2 | import { afterEach, describe, expect, it, vi } from "vite-plus/test"; |
| 3 | |
| 4 | import { appAtomRegistry } from "../../state/atom-registry"; |
| 5 | import { |
| 6 | activateCloudRelayAccount, |
| 7 | cloudAccountService, |
| 8 | deactivateCloudRelayAccount, |
| 9 | } from "./cloudAccountService"; |
| 10 | import { setAgentAwarenessRelayTokenProvider } from "../agent-awareness/remoteRegistration"; |
| 11 | import { requestConnectOnboarding } from "./connectOnboarding"; |
| 12 | import { restoreCloudComposerDrafts } from "../../state/use-composer-drafts"; |
| 13 | |
| 14 | vi.mock("@clerk/expo", () => ({ |
| 15 | ClerkProvider: vi.fn(), |
| 16 | useAuth: vi.fn(), |
| 17 | })); |
| 18 | |
| 19 | vi.mock("@clerk/expo/token-cache", () => ({ |
| 20 | tokenCache: {}, |
| 21 | })); |
| 22 | |
| 23 | vi.mock("../../lib/runtime", () => ({ |
| 24 | runtime: { |
| 25 | runPromiseExit: vi.fn(), |
| 26 | }, |
| 27 | })); |
| 28 | |
| 29 | vi.mock("../../connection/catalog", () => ({ |
| 30 | environmentCatalog: { |
| 31 | removeRelayEnvironments: {}, |
| 32 | }, |
| 33 | })); |
| 34 | |
| 35 | vi.mock("./cloud-drafts", () => ({ removeCloudEnvironments: {} })); |
| 36 | vi.mock("./connectOnboarding", () => ({ |
| 37 | clearConnectOnboardingRequest: vi.fn(), |
| 38 | requestConnectOnboarding: vi.fn(), |
| 39 | })); |
| 40 | vi.mock("../../state/use-composer-drafts", () => ({ |
| 41 | getComposerCloudAccountId: vi.fn(async () => null), |
| 42 | restoreCloudComposerDrafts: vi.fn(async () => undefined), |
| 43 | })); |
| 44 | |
| 45 | vi.mock("./publicConfig", () => ({ |
| 46 | resolveCloudPublicConfig: vi.fn(() => ({ |
| 47 | clerk: { publishableKey: null }, |
| 48 | relay: { url: null }, |
| 49 | })), |
| 50 | resolveRelayClerkTokenOptions: vi.fn(), |
| 51 | })); |
| 52 | |
| 53 | vi.mock("../agent-awareness/remoteRegistration", () => ({ |
| 54 | releaseAgentAwarenessRelayTokenProvider: vi.fn(), |
| 55 | setAgentAwarenessRelayTokenProvider: vi.fn(), |
| 56 | unregisterAgentAwarenessDeviceForCurrentUser: vi.fn(), |
| 57 | })); |
| 58 | |
| 59 | type ClerkListener = (resources: { readonly user: { readonly id: string } | null }) => void; |
| 60 | |
| 61 | function fakeClerk() { |
| 62 | const listeners = new Set<ClerkListener>(); |
| 63 | const clerk = { |
| 64 | session: null, |
| 65 | addListener: (listener: ClerkListener) => { |
| 66 | listeners.add(listener); |
| 67 | return () => listeners.delete(listener); |
| 68 | }, |
| 69 | } as unknown as Parameters<typeof cloudAccountService.attach>[0]; |
| 70 | const emit = (userId: string | null) => { |
| 71 | for (const listener of listeners) listener({ user: userId === null ? null : { id: userId } }); |
| 72 | }; |
| 73 | return { clerk, emit }; |
| 74 | } |
| 75 | |
| 76 | afterEach(() => { |
| 77 | deactivateCloudRelayAccount(); |
| 78 | vi.clearAllMocks(); |
| 79 | }); |
| 80 | |
| 81 | describe("CloudAuthProvider relay account isolation", () => { |
| 82 | it("clears relay and agent-awareness credentials before cleanup can fail", async () => { |
| 83 | const tokenProvider = async () => "account-1-token"; |
| 84 | activateCloudRelayAccount("account-1", tokenProvider); |
| 85 | expect(appAtomRegistry.get(managedRelaySessionAtom)?.accountId).toBe("account-1"); |
| 86 | |
| 87 | deactivateCloudRelayAccount(); |
| 88 | const cleanup = Promise.reject(new Error("Persistence removal failed.")).catch(() => undefined); |
| 89 | |
| 90 | expect(appAtomRegistry.get(managedRelaySessionAtom)).toBeNull(); |
| 91 | expect(vi.mocked(setAgentAwarenessRelayTokenProvider)).toHaveBeenLastCalledWith(null); |
| 92 | await cleanup; |
| 93 | }); |
| 94 | }); |
| 95 | |
| 96 | describe("cloudAccountService", () => { |
| 97 | it("activates the signed-in account once its drafts are restored", async () => { |
| 98 | const { clerk, emit } = fakeClerk(); |
| 99 | using _detach = { [Symbol.dispose]: cloudAccountService.attach(clerk) }; |
| 100 | |
| 101 | emit("account-1"); |
| 102 | expect(appAtomRegistry.get(managedRelaySessionAtom)).toBeNull(); |
| 103 | |
| 104 | await vi.waitFor(() => |
| 105 | expect(appAtomRegistry.get(managedRelaySessionAtom)?.accountId).toBe("account-1"), |
| 106 | ); |
| 107 | expect(vi.mocked(restoreCloudComposerDrafts)).toHaveBeenCalledWith("account-1"); |
| 108 | // Clerk already had this account on its first emission, so this is not a fresh sign-in. |
| 109 | expect(vi.mocked(requestConnectOnboarding)).not.toHaveBeenCalled(); |
| 110 | }); |
| 111 | |
| 112 | it("retries an activation whose draft restore failed on the next emission", async () => { |
| 113 | const { clerk, emit } = fakeClerk(); |
| 114 | using _detach = { [Symbol.dispose]: cloudAccountService.attach(clerk) }; |
| 115 | vi.mocked(restoreCloudComposerDrafts).mockRejectedValueOnce(new Error("Storage busy.")); |
| 116 | |
| 117 | emit("account-1"); |
| 118 | await vi.waitFor(() => expect(cloudAccountService.getState()._tag).toBe("Failed")); |
| 119 | expect(appAtomRegistry.get(managedRelaySessionAtom)).toBeNull(); |
| 120 | |
| 121 | emit("account-1"); |
| 122 | await vi.waitFor(() => |
| 123 | expect(appAtomRegistry.get(managedRelaySessionAtom)?.accountId).toBe("account-1"), |
| 124 | ); |
| 125 | }); |
| 126 | }); |