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

CloudAuthProvider.test.ts

60 lines1.8 KB

1import { managedRelaySessionAtom } from "@t3tools/client-runtime/relay";
2import { afterEach, describe, expect, it, vi } from "vite-plus/test";
3​
4import { appAtomRegistry } from "../../state/atom-registry";
5import { activateCloudRelayAccount, deactivateCloudRelayAccount } from "./CloudAuthProvider";
6import { setAgentAwarenessRelayTokenProvider } from "../agent-awareness/remoteRegistration";
7​
8vi.mock("@clerk/expo", () => ({
9 ClerkProvider: vi.fn(),
10 useAuth: vi.fn(),
11}));
12​
13vi.mock("@clerk/expo/token-cache", () => ({
14 tokenCache: {},
15}));
16​
17vi.mock("../../lib/runtime", () => ({
18 runtime: {
19 runPromiseExit: vi.fn(),
20 },
21}));
22​
23vi.mock("../../connection/catalog", () => ({
24 environmentCatalog: {
25 removeRelayEnvironments: {},
26 },
27}));
28​
29vi.mock("./publicConfig", () => ({
30 resolveCloudPublicConfig: vi.fn(() => ({
31 clerk: { publishableKey: null },
32 relay: { url: null },
33 })),
34 resolveRelayClerkTokenOptions: vi.fn(),
35}));
36​
37vi.mock("../agent-awareness/remoteRegistration", () => ({
38 setAgentAwarenessRelayTokenProvider: vi.fn(),
39 unregisterAgentAwarenessDeviceForCurrentUser: vi.fn(),
40}));
41​
42afterEach(() => {
43 deactivateCloudRelayAccount();
44 vi.clearAllMocks();
45});
46​
47describe("CloudAuthProvider relay account isolation", () => {
48 it("clears relay and agent-awareness credentials before cleanup can fail", async () => {
49 const tokenProvider = async () => "account-1-token";
50 activateCloudRelayAccount("account-1", tokenProvider);
51 expect(appAtomRegistry.get(managedRelaySessionAtom)?.accountId).toBe("account-1");
52​
53 deactivateCloudRelayAccount();
54 const cleanup = Promise.reject(new Error("Persistence removal failed.")).catch(() => undefined);
55​
56 expect(appAtomRegistry.get(managedRelaySessionAtom)).toBeNull();
57 expect(vi.mocked(setAgentAwarenessRelayTokenProvider)).toHaveBeenLastCalledWith(null);
58 await cleanup;
59 });
60});