Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 63 additions & 2 deletions apps/web/src/cloud/managedAuth.test.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,35 @@
import { managedRelaySessionAtom, setManagedRelaySession } from "@t3tools/client-runtime/relay";
import * as Effect from "effect/Effect";
import * as Exit from "effect/Exit";
import { AsyncResult } from "effect/reactivity";
import { act, createElement } from "react";
import { create, type ReactTestRenderer } from "react-test-renderer";
import { afterEach, describe, expect, it, vi } from "vite-plus/test";

import { appAtomRegistry } from "../rpc/atomRegistry";
import {
activateManagedRelayAuthentication,
deactivateManagedRelayAuthentication,
ManagedRelayAuthProvider,
} from "./managedAuth";

const testState = vi.hoisted(() => ({
auth: { isLoaded: false, isSignedIn: false, userId: null as string | null },
getToken: async () => "token",
removeRelayEnvironments: vi.fn(),
}));

vi.mock("@clerk/react", () => ({
useAuth: vi.fn(),
useAuth: () => ({ ...testState.auth, getToken: testState.getToken }),
}));

vi.mock("../state/use-atom-command", () => ({
useAtomCommand: () => testState.removeRelayEnvironments,
}));

vi.mock("../lib/runtime", () => ({
runtime: {
runPromiseExit: vi.fn(),
runPromiseExit: vi.fn(async () => Exit.void),
},
}));

Expand All @@ -24,10 +39,32 @@ vi.mock("../connection/catalog", () => ({
},
}));

let renderer: ReactTestRenderer | null = null;

afterEach(() => {
act(() => renderer?.unmount());
renderer = null;
testState.auth = { isLoaded: false, isSignedIn: false, userId: null };
testState.removeRelayEnvironments.mockReset();
deactivateManagedRelayAuthentication();
});

async function renderAuth(auth: typeof testState.auth) {
testState.auth = auth;
testState.removeRelayEnvironments.mockResolvedValue(AsyncResult.success(undefined));
const element = createElement(ManagedRelayAuthProvider, null, null);
await act(async () => {
if (renderer) {
renderer.update(element);
} else {
renderer = create(element);
}
});
}

const signedOut = { isLoaded: true, isSignedIn: false, userId: null };
const signedInAs = (userId: string) => ({ isLoaded: true, isSignedIn: true, userId });

describe("managed relay authentication", () => {
it("clears all token access synchronously before account cleanup can fail", async () => {
activateManagedRelayAuthentication("account-1", async () => "account-1-token");
Expand Down Expand Up @@ -56,3 +93,27 @@ describe("managed relay authentication", () => {
expect(appAtomRegistry.get(managedRelaySessionAtom)?.accountId).toBe("account-2");
});
});

describe("ManagedRelayAuthProvider account cleanup", () => {
it("keeps relay environments when the first loaded state is signed out", async () => {
await renderAuth({ isLoaded: false, isSignedIn: false, userId: null });
await renderAuth(signedOut);

expect(testState.removeRelayEnvironments).not.toHaveBeenCalled();
});

it("removes relay environments when a signed-in account signs out", async () => {
await renderAuth(signedInAs("account-1"));
await renderAuth(signedOut);

expect(testState.removeRelayEnvironments).toHaveBeenCalledTimes(1);
});

it("removes relay environments when the account changes", async () => {
await renderAuth(signedInAs("account-1"));
await renderAuth(signedInAs("account-2"));

expect(testState.removeRelayEnvironments).toHaveBeenCalledTimes(1);
expect(appAtomRegistry.get(managedRelaySessionAtom)?.accountId).toBe("account-2");
});
});
4 changes: 3 additions & 1 deletion apps/web/src/cloud/managedAuth.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,9 @@ export function ManagedRelayAuthProvider({ children }: { readonly children: Reac

if (!isSignedIn || !userId) {
deactivateManagedRelayAuthentication();
if (previousAccount !== null) {
// Clerk can load signed out while offline, so only a signed-in account
// observed in this session is cleaned up.
if (previousAccount !== undefined && previousAccount !== null) {
void queueAccountCleanup();
}
} else {
Expand Down
Loading