@@ -158,6 +158,22 @@ describe('init/post construct', () => {
158158 expect ( console . error ) . toHaveBeenCalledWith ( 'Error monitoring tokens' , expect . any ( Error ) ) ;
159159 } ) ;
160160 } ) ;
161+
162+ it ( 'should handle error during initial monitorTokens in init()' , async ( ) => {
163+ expect . assertions ( 2 ) ;
164+
165+ // Spy and force monitorTokens to throw on first call
166+ const monitorTokensSpy = vi
167+ . spyOn ( authenticationProviderManager , 'monitorTokens' )
168+ . mockRejectedValueOnce ( new Error ( 'Initial monitor failure' ) ) ;
169+
170+ // Call postConstruct
171+ await authenticationProviderManager . postConstruct ( ) ;
172+
173+ // Should call monitorTokens and handle the error
174+ expect ( monitorTokensSpy ) . toHaveBeenCalledTimes ( 1 ) ;
175+ expect ( console . error ) . toHaveBeenCalledWith ( 'Error while the initial monitoring of tokens' , expect . any ( Error ) ) ;
176+ } ) ;
161177} ) ;
162178
163179test ( 'destroy' , async ( ) => {
@@ -360,4 +376,33 @@ describe('monitorTokens', () => {
360376 expect ( authenticationProviderManager . getOnDidChangeSessions ( ) . fire ) . not . toHaveBeenCalled ( ) ;
361377 expect ( PersistentSessionHelper . prototype . save ) . toHaveBeenCalledWith ( [ freshSession ] ) ;
362378 } ) ;
379+
380+ it ( 'should remove session and fire removed event on refresh error' , async ( ) => {
381+ expect . assertions ( 4 ) ;
382+
383+ const now = Math . round ( Date . now ( ) / 1000 ) ;
384+ const expiringSession : IAMSession = { session_id : 'fail-session' , expiration : now + 10 } as unknown as IAMSession ;
385+ const convertedSession = { id : 'fail-session' } as unknown as AuthenticationSession ;
386+
387+ authenticationProviderManager . getIamSessions ( ) . push ( expiringSession ) ;
388+
389+ vi . mocked ( IamSessionRefreshTokenHelper . prototype . refreshToken ) . mockRejectedValue ( new Error ( 'refresh fail' ) ) ;
390+ vi . mocked ( IamSessionConverterHelper . prototype . convertToAuthenticationSession ) . mockReturnValue ( convertedSession ) ;
391+
392+ await authenticationProviderManager . monitorTokens ( ) ;
393+
394+ // Should have removed session
395+ expect ( authenticationProviderManager . getIamSessions ( ) ) . toHaveLength ( 0 ) ;
396+
397+ // Should have fired removed event
398+ expect ( authenticationProviderManager . getOnDidChangeSessions ( ) . fire ) . toHaveBeenCalledWith ( {
399+ removed : [ convertedSession ] ,
400+ } ) ;
401+
402+ // Should have saved sessions (empty)
403+ expect ( PersistentSessionHelper . prototype . save ) . toHaveBeenCalledWith ( [ ] ) ;
404+
405+ // Should log error
406+ expect ( console . error ) . toHaveBeenCalledWith ( 'Error refreshing token' , expect . any ( Error ) ) ;
407+ } ) ;
363408} ) ;
0 commit comments