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

CloudAuthProvider.test.ts

66 lines2.1 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("./cloud-drafts", () => ({ removeCloudEnvironments: {} }));
30vi.mock("../../state/use-composer-drafts", () => ({
31 getComposerCloudAccountId: vi.fn(async () => null),
32 restoreCloudComposerDrafts: vi.fn(async () => undefined),
33}));
34​
35vi.mock("./publicConfig", () => ({
36 resolveCloudPublicConfig: vi.fn(() => ({
37 clerk: { publishableKey: null },
38 relay: { url: null },
39 })),
40 resolveRelayClerkTokenOptions: vi.fn(),
41}));
42​
43vi.mock("../agent-awareness/remoteRegistration", () => ({
44 setAgentAwarenessRelayTokenProvider: vi.fn(),
45 unregisterAgentAwarenessDeviceForCurrentUser: vi.fn(),
46}));
47​
48afterEach(() => {
49 deactivateCloudRelayAccount();
50 vi.clearAllMocks();
51});
52​
53describe("CloudAuthProvider relay account isolation", () => {
54 it("clears relay and agent-awareness credentials before cleanup can fail", async () => {
55 const tokenProvider = async () => "account-1-token";
56 activateCloudRelayAccount("account-1", tokenProvider);
57 expect(appAtomRegistry.get(managedRelaySessionAtom)?.accountId).toBe("account-1");
58​
59 deactivateCloudRelayAccount();
60 const cleanup = Promise.reject(new Error("Persistence removal failed.")).catch(() => undefined);
61​
62 expect(appAtomRegistry.get(managedRelaySessionAtom)).toBeNull();
63 expect(vi.mocked(setAgentAwarenessRelayTokenProvider)).toHaveBeenLastCalledWith(null);
64 await cleanup;
65 });
66});