@@ -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 ( 'leaves alias undefined when email fetch fails' , async ( ) => {
233232 // Given
234233 vi . mocked ( validateSession ) . mockResolvedValueOnce ( 'needs_full_auth' )
235234 vi . mocked ( fetchSessions ) . mockResolvedValue ( undefined )
@@ -243,14 +242,13 @@ 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 ]
248- expect ( storedSession [ fqdn ] ! [ userId ] ! . identity . alias ) . toBe ( userId )
246+ expect ( storedSession [ fqdn ] ! [ userId ] ! . identity . alias ) . toBeUndefined ( )
249247
250248 expect ( got ) . toEqual ( validTokens )
251249 } )
252250
253- test ( 'falls back to userId when no business platform token available' , async ( ) => {
251+ test ( 'leaves alias undefined when no business platform token available' , async ( ) => {
254252 // Given
255253 vi . mocked ( validateSession ) . mockResolvedValueOnce ( 'needs_full_auth' )
256254 vi . mocked ( fetchSessions ) . mockResolvedValue ( undefined )
@@ -267,9 +265,8 @@ The CLI is currently unable to prompt for reauthentication.`,
267265 // Then
268266 expect ( businessPlatformRequest ) . not . toHaveBeenCalled ( )
269267
270- // Verify the session was stored with userId as alias (fallback)
271268 const storedSession = vi . mocked ( storeSessions ) . mock . calls [ 0 ] ! [ 0 ]
272- expect ( storedSession [ fqdn ] ! [ userId ] ! . identity . alias ) . toBe ( userId )
269+ expect ( storedSession [ fqdn ] ! [ userId ] ! . identity . alias ) . toBeUndefined ( )
273270 } )
274271
275272 test ( 'executes complete auth flow if requesting additional scopes' , async ( ) => {
@@ -684,7 +681,32 @@ describe('ensureAuthenticated email fetch functionality', () => {
684681 expect ( got ) . toEqual ( validTokens )
685682 } )
686683
687- test ( 'uses userId as alias when email is not available' , async ( ) => {
684+ test ( 'preserves existing alias during token refresh error fallback when email fetch fails' , async ( ) => {
685+ // Given
686+ const sessionsWithAlias : Sessions = {
687+ [ fqdn ] : {
688+ [ userId ] : {
689+ identity : { ...validIdentityToken , alias : 'my-custom-alias' } ,
690+ applications : appTokens ,
691+ } ,
692+ } ,
693+ }
694+ const tokenResponseError = new InvalidGrantError ( )
695+ vi . mocked ( validateSession ) . mockResolvedValueOnce ( 'needs_refresh' )
696+ vi . mocked ( fetchSessions ) . mockResolvedValue ( sessionsWithAlias )
697+ vi . mocked ( refreshAccessToken ) . mockRejectedValueOnce ( tokenResponseError )
698+ vi . mocked ( businessPlatformRequest ) . mockRejectedValueOnce ( new Error ( 'API Error' ) )
699+
700+ // When
701+ const got = await ensureAuthenticated ( defaultApplications )
702+
703+ // Then
704+ const storedSession = vi . mocked ( storeSessions ) . mock . calls [ 0 ] ! [ 0 ]
705+ expect ( storedSession [ fqdn ] ! [ userId ] ! . identity . alias ) . toBe ( 'my-custom-alias' )
706+ expect ( got ) . toEqual ( validTokens )
707+ } )
708+
709+ test ( 'leaves alias undefined when email is not available' , async ( ) => {
688710 // Given
689711 vi . mocked ( validateSession ) . mockResolvedValueOnce ( 'needs_full_auth' )
690712 vi . mocked ( fetchSessions ) . mockResolvedValue ( undefined )
@@ -699,7 +721,7 @@ describe('ensureAuthenticated email fetch functionality', () => {
699721
700722 // Then
701723 const storedSession = vi . mocked ( storeSessions ) . mock . calls [ 0 ] ! [ 0 ]
702- expect ( storedSession [ fqdn ] ! [ userId ] ! . identity . alias ) . toBe ( userId )
724+ expect ( storedSession [ fqdn ] ! [ userId ] ! . identity . alias ) . toBeUndefined ( )
703725 expect ( got ) . toEqual ( validTokens )
704726 } )
705727} )
0 commit comments