@@ -259,6 +259,61 @@ describe('SupabaseClient', () => {
259259 } )
260260
261261 describe ( 'Realtime Authentication' , ( ) => {
262+ test ( 'should automatically call setAuth() when accessToken option is provided' , async ( ) => {
263+ const customToken = 'custom-jwt-token'
264+ const customAccessTokenFn = jest . fn ( ) . mockResolvedValue ( customToken )
265+ const client = createClient ( URL , KEY , { accessToken : customAccessTokenFn } )
266+
267+ await new Promise ( ( resolve ) => setTimeout ( resolve , 0 ) )
268+
269+ expect ( ( client . realtime as any ) . accessTokenValue ) . toBe ( customToken )
270+ expect ( customAccessTokenFn ) . toHaveBeenCalled ( )
271+ } )
272+
273+ test ( 'should automatically populate token in channels when using custom JWT' , async ( ) => {
274+ const customToken = 'custom-channel-token'
275+ const customAccessTokenFn = jest . fn ( ) . mockResolvedValue ( customToken )
276+ const client = createClient ( URL , KEY , { accessToken : customAccessTokenFn } )
277+
278+ await new Promise ( ( resolve ) => setTimeout ( resolve , 0 ) )
279+
280+ const channel = client . channel ( 'test-channel' )
281+ channel . subscribe ( )
282+
283+ expect ( ( channel as any ) . joinPush . payload . access_token ) . toBe ( customToken )
284+ expect ( ( client . realtime as any ) . accessTokenValue ) . toBe ( customToken )
285+ } )
286+
287+ test ( 'should handle errors gracefully when accessToken callback fails' , async ( ) => {
288+ const consoleWarnSpy = jest . spyOn ( console , 'warn' ) . mockImplementation ( ( ) => { } )
289+ const error = new Error ( 'Token fetch failed' )
290+ const failingAccessTokenFn = jest . fn ( ) . mockRejectedValue ( error )
291+
292+ const client = createClient ( URL , KEY , { accessToken : failingAccessTokenFn } )
293+
294+ await new Promise ( ( resolve ) => setTimeout ( resolve , 0 ) )
295+
296+ expect ( consoleWarnSpy ) . toHaveBeenCalledWith (
297+ 'Failed to set initial Realtime auth token:' ,
298+ error
299+ )
300+ expect ( client ) . toBeDefined ( )
301+ expect ( client . realtime ) . toBeDefined ( )
302+
303+ consoleWarnSpy . mockRestore ( )
304+ } )
305+
306+ test ( 'should not call setAuth() automatically in normal mode' , async ( ) => {
307+ const client = createClient ( URL , KEY )
308+ const setAuthSpy = jest . spyOn ( client . realtime , 'setAuth' )
309+
310+ await new Promise ( ( resolve ) => setTimeout ( resolve , 10 ) )
311+
312+ expect ( setAuthSpy ) . not . toHaveBeenCalled ( )
313+
314+ setAuthSpy . mockRestore ( )
315+ } )
316+
262317 test ( 'should provide access token to realtime client' , async ( ) => {
263318 const expectedToken = 'test-jwt-token'
264319 const client = createClient ( URL , KEY )
0 commit comments