@@ -35,6 +35,9 @@ function makeSessions(extract: jest.Mock, act: jest.Mock) {
3535 ensureActivePage : jest . fn ( ) . mockResolvedValue ( page ) ,
3636 safeCloseStagehand : jest . fn ( ) . mockResolvedValue ( undefined ) ,
3737 closeSession : jest . fn ( ) . mockResolvedValue ( undefined ) ,
38+ getActivePageLiveViewUrl : jest
39+ . fn ( )
40+ . mockResolvedValue ( 'https://live-view/current' ) ,
3841 } ;
3942}
4043
@@ -172,6 +175,31 @@ describe('BrowserCredentialSigninService', () => {
172175 expect ( profiles . markNeedsReauth ) . toHaveBeenCalledTimes ( 1 ) ;
173176 } ) ;
174177
178+ it ( 're-points the live view after the sign-in attempt so a 2FA take-over sees the right tab' , async ( ) => {
179+ const extract = jest
180+ . fn ( )
181+ . mockResolvedValueOnce ( { state : 'unknown' } )
182+ . mockResolvedValue ( { state : 'needs_2fa' } ) ;
183+ const sessions = makeSessions ( extract , jest . fn ( ) . mockResolvedValue ( undefined ) ) ;
184+ const profiles = makeProfiles ( profile ) ;
185+ withCredentials ( { username : 'user@x.com' , password : 'secret' } ) ; // no code → manual 2FA
186+ const onLiveView = jest . fn ( ) ;
187+ const service = new BrowserCredentialSigninService (
188+ sessions as unknown as BrowserbaseSessionService ,
189+ profiles as unknown as BrowserAuthProfileService ,
190+ ) ;
191+
192+ const promise = service . signInWithStoredCredentials ( { ...input , onLiveView } ) ;
193+ await jest . runAllTimersAsync ( ) ;
194+ const result = await promise ;
195+
196+ expect ( result . failure ) . toBe ( 'needs_2fa' ) ;
197+ // Emitted after navigateToSignIn AND again after the sign-in attempt — so the
198+ // iframe follows to the 2FA page instead of staying on the pre-submit login
199+ // tab (the reported "your turn, but I can't see the code field" bug).
200+ expect ( onLiveView . mock . calls . length ) . toBeGreaterThanOrEqual ( 2 ) ;
201+ } ) ;
202+
175203 it ( 'throws when the profile does not exist' , async ( ) => {
176204 const sessions = makeSessions ( jest . fn ( ) , jest . fn ( ) ) ;
177205 const profiles = makeProfiles ( null ) ;
0 commit comments