@@ -3,6 +3,7 @@ import { afterAll, beforeAll, describe, expect, it, vi } from 'vitest';
33
44import { createSession , createSignIn , createSignUp , createUser } from '@/test/core-fixtures' ;
55
6+ import { SessionTokenCache } from '../../tokenCache' ;
67import { BaseResource , Client } from '../internal' ;
78
89const FIXED_DATE = new Date ( '2025-01-01T00:00:00Z' ) ;
@@ -148,6 +149,72 @@ describe('Client Singleton', () => {
148149 } ) ;
149150 } ) ;
150151
152+ describe ( '__internal_signOut' , ( ) => {
153+ it ( 'returns the response URL and clears sessions while retaining the client' , async ( ) => {
154+ const user = createUser ( { id : 'user_1' } ) ;
155+ const initial : ClientJSON = {
156+ object : 'client' ,
157+ id : 'client_before' ,
158+ sessions : [ createSession ( { id : 'session_1' } , user ) ] ,
159+ sign_in : createSignIn ( { id : 'sign_in_before' } , user ) ,
160+ sign_up : createSignUp ( { id : 'sign_up_before' } ) ,
161+ last_active_session_id : 'session_1' ,
162+ last_authentication_strategy : 'password' ,
163+ cookie_expires_at : Date . now ( ) + 30 * 24 * 60 * 60 * 1000 ,
164+ created_at : Date . now ( ) - 1000 ,
165+ updated_at : Date . now ( ) ,
166+ } ;
167+ const response = { ...initial , id : 'client_after' , sessions : [ ] , last_active_session_id : null } ;
168+ const request = vi . fn ( ) . mockResolvedValue ( { response, sign_out_url : 'https://primary.test/sign-out' } ) ;
169+ BaseResource . _fetch = request ;
170+ const clearCache = vi . spyOn ( SessionTokenCache , 'clear' ) ;
171+ const client = Client . getOrCreateInstance ( ) . fromJSON ( initial ) ;
172+ const result = await client . __internal_signOut ( {
173+ redirectUrl : 'https://satellite.test/after?tab=security#settings' ,
174+ } ) ;
175+
176+ expect ( request ) . toHaveBeenCalledWith ( {
177+ method : 'DELETE' ,
178+ path : '/client/sessions' ,
179+ search : { sign_out_redirect_url : 'https://satellite.test/after?tab=security#settings' } ,
180+ } ) ;
181+ expect ( result ) . toEqual ( { signOutUrl : 'https://primary.test/sign-out' } ) ;
182+ expect ( clearCache ) . toHaveBeenCalledOnce ( ) ;
183+ expect ( client . sessions ) . toEqual ( [ ] ) ;
184+ expect ( client . id ) . toBe ( 'client_after' ) ;
185+ expect ( client . signIn . id ) . toBe ( 'sign_in_before' ) ;
186+ expect ( client . signUp . id ) . toBe ( 'sign_up_before' ) ;
187+ expect ( client . lastActiveSessionId ) . toBeNull ( ) ;
188+ expect ( client . lastAuthenticationStrategy ) . toBe ( 'password' ) ;
189+ expect ( client . cookieExpiresAt ) . toEqual ( new Date ( initial . cookie_expires_at ! ) ) ;
190+ expect ( client . createdAt ) . toEqual ( new Date ( initial . created_at ! ) ) ;
191+ expect ( client . updatedAt ) . toEqual ( new Date ( initial . updated_at ! ) ) ;
192+ clearCache . mockRestore ( ) ;
193+ } ) ;
194+
195+ it . each ( [ null , { response : null } ] ) ( 'accepts a response without a sign-out URL' , async response => {
196+ BaseResource . _fetch = vi . fn ( ) . mockResolvedValue ( response ) ;
197+ const result = await Client . getOrCreateInstance ( ) . __internal_signOut ( {
198+ redirectUrl : 'https://satellite.test/' ,
199+ } ) ;
200+ expect ( result ) . toEqual ( { signOutUrl : undefined } ) ;
201+ } ) ;
202+
203+ it ( 'does not reuse the URL from an earlier request' , async ( ) => {
204+ BaseResource . _fetch = vi
205+ . fn ( )
206+ . mockResolvedValueOnce ( { response : null , sign_out_url : 'https://primary.test/sign-out' } )
207+ . mockResolvedValueOnce ( null ) ;
208+ const client = Client . getOrCreateInstance ( ) ;
209+ expect ( await client . __internal_signOut ( { redirectUrl : 'https://satellite.test/' } ) ) . toEqual ( {
210+ signOutUrl : 'https://primary.test/sign-out' ,
211+ } ) ;
212+ expect ( await client . __internal_signOut ( { redirectUrl : 'https://satellite.test/' } ) ) . toEqual ( {
213+ signOutUrl : undefined ,
214+ } ) ;
215+ } ) ;
216+ } ) ;
217+
151218 it ( 'preserves sign up and sign in identity when fromJSON receives matching ids' , ( ) => {
152219 const user = createUser ( { first_name : 'John' , last_name : 'Doe' , id : 'user_1' } ) ;
153220 const session = createSession ( { id : 'session_1' } , user ) ;
0 commit comments