11import { managedRelaySessionAtom , setManagedRelaySession } from "@t3tools/client-runtime/relay" ;
22import * as Effect from "effect/Effect" ;
3+ import * as Exit from "effect/Exit" ;
4+ import { AsyncResult } from "effect/reactivity" ;
5+ import { act , createElement } from "react" ;
6+ import { create , type ReactTestRenderer } from "react-test-renderer" ;
37import { afterEach , describe , expect , it , vi } from "vite-plus/test" ;
48
59import { appAtomRegistry } from "../rpc/atomRegistry" ;
610import {
711 activateManagedRelayAuthentication ,
812 deactivateManagedRelayAuthentication ,
13+ ManagedRelayAuthProvider ,
914} from "./managedAuth" ;
1015
16+ const testState = vi . hoisted ( ( ) => ( {
17+ auth : { isLoaded : false , isSignedIn : false , userId : null as string | null } ,
18+ getToken : async ( ) => "token" ,
19+ removeRelayEnvironments : vi . fn ( ) ,
20+ } ) ) ;
21+
1122vi . mock ( "@clerk/react" , ( ) => ( {
12- useAuth : vi . fn ( ) ,
23+ useAuth : ( ) => ( { ...testState . auth , getToken : testState . getToken } ) ,
24+ } ) ) ;
25+
26+ vi . mock ( "../state/use-atom-command" , ( ) => ( {
27+ useAtomCommand : ( ) => testState . removeRelayEnvironments ,
1328} ) ) ;
1429
1530vi . mock ( "../lib/runtime" , ( ) => ( {
1631 runtime : {
17- runPromiseExit : vi . fn ( ) ,
32+ runPromiseExit : vi . fn ( async ( ) => Exit . void ) ,
1833 } ,
1934} ) ) ;
2035
@@ -24,10 +39,32 @@ vi.mock("../connection/catalog", () => ({
2439 } ,
2540} ) ) ;
2641
42+ let renderer : ReactTestRenderer | null = null ;
43+
2744afterEach ( ( ) => {
45+ act ( ( ) => renderer ?. unmount ( ) ) ;
46+ renderer = null ;
47+ testState . auth = { isLoaded : false , isSignedIn : false , userId : null } ;
48+ testState . removeRelayEnvironments . mockReset ( ) ;
2849 deactivateManagedRelayAuthentication ( ) ;
2950} ) ;
3051
52+ async function renderAuth ( auth : typeof testState . auth ) {
53+ testState . auth = auth ;
54+ testState . removeRelayEnvironments . mockResolvedValue ( AsyncResult . success ( undefined ) ) ;
55+ const element = createElement ( ManagedRelayAuthProvider , null , null ) ;
56+ await act ( async ( ) => {
57+ if ( renderer ) {
58+ renderer . update ( element ) ;
59+ } else {
60+ renderer = create ( element ) ;
61+ }
62+ } ) ;
63+ }
64+
65+ const signedOut = { isLoaded : true , isSignedIn : false , userId : null } ;
66+ const signedInAs = ( userId : string ) => ( { isLoaded : true , isSignedIn : true , userId } ) ;
67+
3168describe ( "managed relay authentication" , ( ) => {
3269 it ( "clears all token access synchronously before account cleanup can fail" , async ( ) => {
3370 activateManagedRelayAuthentication ( "account-1" , async ( ) => "account-1-token" ) ;
@@ -56,3 +93,27 @@ describe("managed relay authentication", () => {
5693 expect ( appAtomRegistry . get ( managedRelaySessionAtom ) ?. accountId ) . toBe ( "account-2" ) ;
5794 } ) ;
5895} ) ;
96+
97+ describe ( "ManagedRelayAuthProvider account cleanup" , ( ) => {
98+ it ( "keeps relay environments when the first loaded state is signed out" , async ( ) => {
99+ await renderAuth ( { isLoaded : false , isSignedIn : false , userId : null } ) ;
100+ await renderAuth ( signedOut ) ;
101+
102+ expect ( testState . removeRelayEnvironments ) . not . toHaveBeenCalled ( ) ;
103+ } ) ;
104+
105+ it ( "removes relay environments when a signed-in account signs out" , async ( ) => {
106+ await renderAuth ( signedInAs ( "account-1" ) ) ;
107+ await renderAuth ( signedOut ) ;
108+
109+ expect ( testState . removeRelayEnvironments ) . toHaveBeenCalledTimes ( 1 ) ;
110+ } ) ;
111+
112+ it ( "removes relay environments when the account changes" , async ( ) => {
113+ await renderAuth ( signedInAs ( "account-1" ) ) ;
114+ await renderAuth ( signedInAs ( "account-2" ) ) ;
115+
116+ expect ( testState . removeRelayEnvironments ) . toHaveBeenCalledTimes ( 1 ) ;
117+ expect ( appAtomRegistry . get ( managedRelaySessionAtom ) ?. accountId ) . toBe ( "account-2" ) ;
118+ } ) ;
119+ } ) ;
0 commit comments