56 lines
1.7 KiB
TypeScript
56 lines
1.7 KiB
TypeScript
import { managedRelaySessionAtom, setManagedRelaySession } from "@t3tools/client-runtime/relay";
|
|
import { afterEach, describe, expect, it, vi } from "vite-plus/test";
|
|
|
|
import { appAtomRegistry } from "../rpc/atomRegistry";
|
|
import {
|
|
activateManagedRelayAuthentication,
|
|
deactivateManagedRelayAuthentication,
|
|
readManagedRelayClerkToken,
|
|
} from "./managedAuth";
|
|
|
|
vi.mock("@clerk/react", () => ({
|
|
useAuth: vi.fn(),
|
|
}));
|
|
|
|
vi.mock("../lib/runtime", () => ({
|
|
runtime: {
|
|
runPromiseExit: vi.fn(),
|
|
},
|
|
}));
|
|
|
|
vi.mock("../connection/catalog", () => ({
|
|
environmentCatalog: {
|
|
removeRelayEnvironments: {},
|
|
},
|
|
}));
|
|
|
|
afterEach(() => {
|
|
deactivateManagedRelayAuthentication();
|
|
});
|
|
|
|
describe("managed relay authentication", () => {
|
|
it("clears all token access synchronously before account cleanup can fail", async () => {
|
|
activateManagedRelayAuthentication("account-1", async () => "account-1-token");
|
|
expect(appAtomRegistry.get(managedRelaySessionAtom)?.accountId).toBe("account-1");
|
|
expect(await readManagedRelayClerkToken()).toBe("account-1-token");
|
|
|
|
deactivateManagedRelayAuthentication();
|
|
const cleanup = Promise.reject(new Error("Persistence removal failed.")).catch(() => undefined);
|
|
|
|
expect(appAtomRegistry.get(managedRelaySessionAtom)).toBeNull();
|
|
expect(await readManagedRelayClerkToken()).toBeNull();
|
|
await cleanup;
|
|
});
|
|
|
|
it("replaces an existing account session atomically", () => {
|
|
setManagedRelaySession(appAtomRegistry, {
|
|
accountId: "account-1",
|
|
readClerkToken: async () => "account-1-token",
|
|
});
|
|
|
|
activateManagedRelayAuthentication("account-2", async () => "account-2-token");
|
|
|
|
expect(appAtomRegistry.get(managedRelaySessionAtom)?.accountId).toBe("account-2");
|
|
});
|
|
});
|