@@ -40,7 +40,6 @@ const validIdentityToken: IdentityToken = {
4040 expiresAt : futureDate ,
4141 scopes : [ 'scope' , 'scope2' ] ,
4242 userId,
43- alias : userId ,
4443}
4544
4645const validTokens : OAuthSession = {
@@ -180,7 +179,7 @@ The CLI is currently unable to prompt for reauthentication.`,
180179 expect ( fetchSessions ) . toHaveBeenCalledOnce ( )
181180 } )
182181
183- test ( 'falls back to userId when email fetch fails' , async ( ) => {
182+ test ( 'leaves alias undefined when email fetch fails' , async ( ) => {
184183 // Given
185184 vi . mocked ( validateSession ) . mockResolvedValueOnce ( 'needs_full_auth' )
186185 vi . mocked ( fetchSessions ) . mockResolvedValue ( undefined )
@@ -194,9 +193,8 @@ The CLI is currently unable to prompt for reauthentication.`,
194193 expect ( businessPlatformRequest ) . toHaveBeenCalled ( )
195194 expect ( storeSessions ) . toHaveBeenCalledOnce ( )
196195
197- // Verify the session was stored with userId as alias (fallback)
198196 const storedSession = vi . mocked ( storeSessions ) . mock . calls [ 0 ] ! [ 0 ]
199- expect ( storedSession [ fqdn ] ! [ userId ] ! . identity . alias ) . toBe ( userId )
197+ expect ( storedSession [ fqdn ] ! [ userId ] ! . identity . alias ) . toBeUndefined ( )
200198
201199 expect ( got ) . toEqual ( validTokens )
202200 } )
@@ -207,7 +205,7 @@ The CLI is currently unable to prompt for reauthentication.`,
207205 vi . mocked ( fetchSessions ) . mockResolvedValue ( undefined )
208206
209207 // When
210- const got = await ensureAuthenticated ( defaultApplications )
208+ await ensureAuthenticated ( defaultApplications )
211209
212210 // Then
213211 expect ( businessPlatformRequest ) . toHaveBeenCalledWith ( expect . any ( String ) , 'access_token' )
@@ -666,7 +664,32 @@ describe('ensureAuthenticated email fetch functionality', () => {
666664 expect ( got ) . toEqual ( validTokens )
667665 } )
668666
669- test ( 'uses userId as alias when email is not available' , async ( ) => {
667+ test ( 'preserves existing alias during token refresh error fallback when email fetch fails' , async ( ) => {
668+ // Given
669+ const sessionsWithAlias : Sessions = {
670+ [ fqdn ] : {
671+ [ userId ] : {
672+ identity : { ...validIdentityToken , alias : 'my-custom-alias' } ,
673+ applications : { } ,
674+ } ,
675+ } ,
676+ }
677+ const tokenResponseError = new InvalidGrantError ( )
678+ vi . mocked ( validateSession ) . mockResolvedValueOnce ( 'needs_refresh' )
679+ vi . mocked ( fetchSessions ) . mockResolvedValue ( sessionsWithAlias )
680+ vi . mocked ( refreshAccessToken ) . mockRejectedValueOnce ( tokenResponseError )
681+ vi . mocked ( businessPlatformRequest ) . mockRejectedValueOnce ( new Error ( 'API Error' ) )
682+
683+ // When
684+ const got = await ensureAuthenticated ( defaultApplications )
685+
686+ // Then
687+ const storedSession = vi . mocked ( storeSessions ) . mock . calls [ 0 ] ! [ 0 ]
688+ expect ( storedSession [ fqdn ] ! [ userId ] ! . identity . alias ) . toBe ( 'my-custom-alias' )
689+ expect ( got ) . toEqual ( validTokens )
690+ } )
691+
692+ test ( 'leaves alias undefined when email is not available' , async ( ) => {
670693 // Given
671694 vi . mocked ( validateSession ) . mockResolvedValueOnce ( 'needs_full_auth' )
672695 vi . mocked ( fetchSessions ) . mockResolvedValue ( undefined )
@@ -681,7 +704,7 @@ describe('ensureAuthenticated email fetch functionality', () => {
681704
682705 // Then
683706 const storedSession = vi . mocked ( storeSessions ) . mock . calls [ 0 ] ! [ 0 ]
684- expect ( storedSession [ fqdn ] ! [ userId ] ! . identity . alias ) . toBe ( userId )
707+ expect ( storedSession [ fqdn ] ! [ userId ] ! . identity . alias ) . toBeUndefined ( )
685708 expect ( got ) . toEqual ( validTokens )
686709 } )
687710} )
0 commit comments