@@ -46,7 +46,6 @@ const validIdentityToken: IdentityToken = {
4646 expiresAt : futureDate ,
4747 scopes : [ 'scope' , 'scope2' ] ,
4848 userId,
49- alias : userId ,
5049}
5150
5251const validTokens : OAuthSession = {
@@ -229,7 +228,7 @@ The CLI is currently unable to prompt for reauthentication.`,
229228 expect ( fetchSessions ) . toHaveBeenCalledOnce ( )
230229 } )
231230
232- test ( 'falls back to userId when email fetch fails' , async ( ) => {
231+ test ( 'falls back to userId as alias when email fetch fails' , async ( ) => {
233232 // Given
234233 vi . mocked ( validateSession ) . mockResolvedValueOnce ( 'needs_full_auth' )
235234 vi . mocked ( fetchSessions ) . mockResolvedValue ( undefined )
@@ -243,7 +242,6 @@ The CLI is currently unable to prompt for reauthentication.`,
243242 expect ( businessPlatformRequest ) . toHaveBeenCalled ( )
244243 expect ( storeSessions ) . toHaveBeenCalledOnce ( )
245244
246- // Verify the session was stored with userId as alias (fallback)
247245 const storedSession = vi . mocked ( storeSessions ) . mock . calls [ 0 ] ! [ 0 ]
248246 expect ( storedSession [ fqdn ] ! [ userId ] ! . identity . alias ) . toBe ( userId )
249247
@@ -262,7 +260,7 @@ The CLI is currently unable to prompt for reauthentication.`,
262260 vi . mocked ( exchangeAccessForApplicationTokens ) . mockResolvedValueOnce ( appTokensWithoutBusinessPlatform )
263261
264262 // When
265- const got = await ensureAuthenticated ( defaultApplications )
263+ await ensureAuthenticated ( defaultApplications )
266264
267265 // Then
268266 expect ( businessPlatformRequest ) . not . toHaveBeenCalled ( )
@@ -650,16 +648,24 @@ describe('ensureAuthenticated email fetch functionality', () => {
650648
651649 test ( 'preserves existing alias during refresh token flow' , async ( ) => {
652650 // Given
651+ const sessionsWithAlias : Sessions = {
652+ [ fqdn ] : {
653+ [ userId ] : {
654+ identity : { ...validIdentityToken , alias : 'user@example.com' } ,
655+ applications : appTokens ,
656+ } ,
657+ } ,
658+ }
653659 vi . mocked ( validateSession ) . mockResolvedValueOnce ( 'needs_refresh' )
654- vi . mocked ( fetchSessions ) . mockResolvedValue ( validSessions )
660+ vi . mocked ( fetchSessions ) . mockResolvedValue ( sessionsWithAlias )
655661
656662 // When
657663 const got = await ensureAuthenticated ( defaultApplications )
658664
659665 // Then
660- // The email fetch is not called during refresh - the session keeps its existing alias
661666 expect ( businessPlatformRequest ) . not . toHaveBeenCalled ( )
662- expect ( storeSessions ) . toBeCalledWith ( validSessions )
667+ const storedSession = vi . mocked ( storeSessions ) . mock . calls [ 0 ] ! [ 0 ]
668+ expect ( storedSession [ fqdn ] ! [ userId ] ! . identity . alias ) . toBe ( 'user@example.com' )
663669 expect ( got ) . toEqual ( validTokens )
664670 } )
665671
@@ -684,7 +690,32 @@ describe('ensureAuthenticated email fetch functionality', () => {
684690 expect ( got ) . toEqual ( validTokens )
685691 } )
686692
687- test ( 'uses userId as alias when email is not available' , async ( ) => {
693+ test ( 'preserves existing alias during token refresh error fallback when email fetch fails' , async ( ) => {
694+ // Given
695+ const sessionsWithAlias : Sessions = {
696+ [ fqdn ] : {
697+ [ userId ] : {
698+ identity : { ...validIdentityToken , alias : 'my-custom-alias' } ,
699+ applications : { } ,
700+ } ,
701+ } ,
702+ }
703+ const tokenResponseError = new InvalidGrantError ( )
704+ vi . mocked ( validateSession ) . mockResolvedValueOnce ( 'needs_refresh' )
705+ vi . mocked ( fetchSessions ) . mockResolvedValue ( sessionsWithAlias )
706+ vi . mocked ( refreshAccessToken ) . mockRejectedValueOnce ( tokenResponseError )
707+ vi . mocked ( businessPlatformRequest ) . mockRejectedValueOnce ( new Error ( 'API Error' ) )
708+
709+ // When
710+ const got = await ensureAuthenticated ( defaultApplications )
711+
712+ // Then
713+ const storedSession = vi . mocked ( storeSessions ) . mock . calls [ 0 ] ! [ 0 ]
714+ expect ( storedSession [ fqdn ] ! [ userId ] ! . identity . alias ) . toBe ( 'my-custom-alias' )
715+ expect ( got ) . toEqual ( validTokens )
716+ } )
717+
718+ test ( 'falls back to userId as alias when email is not available' , async ( ) => {
688719 // Given
689720 vi . mocked ( validateSession ) . mockResolvedValueOnce ( 'needs_full_auth' )
690721 vi . mocked ( fetchSessions ) . mockResolvedValue ( undefined )
0 commit comments