@@ -213,11 +213,11 @@ function makeContext(onClose?: (context: BrowserContext) => void, initialUrl = "
213213 viewport = size ;
214214 } ) ,
215215 goto : vi . fn ( async ( next : string , _options ?: { timeout ?: number ; waitUntil ?: string } ) => {
216- url = next ;
217- events . emit ( "load" ) ;
218216 const pending = initialNavigation ;
219217 initialNavigation = null ;
220218 await pending ?.( ) ;
219+ url = next ;
220+ events . emit ( "load" ) ;
221221 } ) ,
222222 goBack : vi . fn ( async ( ) => { } ) ,
223223 goForward : vi . fn ( async ( ) => { } ) ,
@@ -998,15 +998,117 @@ it.effect("a desktop page that loaded before the server attached reports its loa
998998 ) . pipe ( Effect . provide ( layer ) ) ,
999999) ;
10001000
1001+ it . effect . each ( [ "none" , "during" , "after-fragment" , "same-url" ] ) (
1002+ "Stop settles an initial navigation with its request event $0" ,
1003+ ( requestTiming ) =>
1004+ Effect . scoped (
1005+ Effect . gen ( function * ( ) {
1006+ const { browser, broker } = yield * ready ;
1007+ const manager = yield * Manager . PreviewManager ;
1008+ const events = yield * manager . subscribeEvents ;
1009+ const started = Promise . withResolvers < void > ( ) ;
1010+ const committed = Promise . withResolvers < void > ( ) ;
1011+ initialNavigation = ( ) => {
1012+ started . resolve ( ) ;
1013+ return committed . promise ;
1014+ } ;
1015+ yield * Effect . addFinalizer ( ( ) => Effect . sync ( ( ) => committed . resolve ( ) ) ) ;
1016+ const { tabId } = yield * manager . open ( {
1017+ threadId : scope . thread . threadId ,
1018+ runtime : "server" ,
1019+ url :
1020+ requestTiming === "after-fragment"
1021+ ? "https://example.com/pending#section"
1022+ : "https://example.com/pending" ,
1023+ } ) ;
1024+ const viewer = yield * browser . attachViewer ( viewerInput ( tabId , true ) ) ;
1025+ yield * Effect . promise ( ( ) => started . promise ) ;
1026+ const page = contexts . at ( - 1 ) ! . page ;
1027+ const request = {
1028+ url : ( ) => "https://example.com/pending" ,
1029+ method : ( ) => "GET" ,
1030+ isNavigationRequest : ( ) => true ,
1031+ frame : ( ) => page ,
1032+ failure : ( ) => ( { errorText : "net::ERR_ABORTED" } ) ,
1033+ } ;
1034+ const cdp = contexts . at ( - 1 ) ! . sessions . at ( - 1 ) ! ;
1035+ const send = cdp . send . getMockImplementation ( ) ! ;
1036+ cdp . send . mockImplementation ( async ( operation , input ) => {
1037+ if ( operation === "Page.stopLoading" ) {
1038+ committed . reject ( new Error ( "net::ERR_ABORTED" ) ) ;
1039+ if ( requestTiming === "during" ) page . emit ( "request" , request ) ;
1040+ }
1041+ return send ( operation , input ) ;
1042+ } ) ;
1043+ yield * viewer . input ( { type : "stopLoading" } ) ;
1044+ const stopped = yield * broker . invoke < PreviewAutomationStatus > ( {
1045+ scope,
1046+ tabId,
1047+ operation : "status" ,
1048+ input : { } ,
1049+ } ) ;
1050+ expect ( stopped . loading ) . toBe ( false ) ;
1051+ let settled = yield * PubSub . take ( events ) ;
1052+ while (
1053+ settled . type !== "navigated" ||
1054+ settled . tabId !== tabId ||
1055+ settled . snapshot . navStatus . _tag !== "Idle"
1056+ ) {
1057+ settled = yield * PubSub . take ( events ) ;
1058+ }
1059+ const corrected = requestTiming === "same-url" ;
1060+ using timeout = vi . spyOn ( globalThis , "setTimeout" ) ;
1061+ if ( corrected ) {
1062+ page . goto . mockImplementationOnce ( async ( ) => {
1063+ page . emit ( "request" , request ) ;
1064+ } ) ;
1065+ yield * viewer . input ( { type : "navigate" , url : request . url ( ) } ) ;
1066+ } else if ( requestTiming !== "none" ) {
1067+ if ( requestTiming === "after-fragment" ) page . emit ( "request" , request ) ;
1068+ page . emit ( "requestfailed" , request ) ;
1069+ }
1070+ const status = yield * broker . invoke < PreviewAutomationStatus > ( {
1071+ scope,
1072+ tabId,
1073+ operation : "status" ,
1074+ input : { } ,
1075+ } ) ;
1076+ expect ( status . loading ) . toBe ( corrected ) ;
1077+ expect ( timeout ) . not . toHaveBeenCalledWith ( expect . any ( Function ) , 5_000 ) ;
1078+ if ( ! corrected ) {
1079+ const { sessions } = yield * manager . list ( { threadId : scope . thread . threadId } ) ;
1080+ expect ( sessions . find ( ( session ) => session . tabId === tabId ) ?. navStatus ) . toEqual ( {
1081+ _tag : "Idle" ,
1082+ } ) ;
1083+ }
1084+ } ) ,
1085+ ) . pipe ( Effect . provide ( layer ) ) ,
1086+ ) ;
1087+
10011088it . effect . each ( [
1002- { method : "goto" as const , message : { type : "navigate" , url : "http://10.255.255.1/" } } ,
1003- { method : "goBack" as const , message : { type : "history" , delta : - 1 } } ,
1004- { method : "goForward" as const , message : { type : "history" , delta : 1 } } ,
1005- { method : "reload" as const , message : { type : "reload" } } ,
1006- ] ) ( "a pending viewer $method allows a corrected URL and text input" , ( { method, message } ) =>
1089+ {
1090+ method : "goto" as const ,
1091+ message : { type : "navigate" , url : "http://10.255.255.1/" } ,
1092+ abortEvent : "before" ,
1093+ } ,
1094+ { method : "goBack" as const , message : { type : "history" , delta : - 1 } , abortEvent : "before" } ,
1095+ { method : "goForward" as const , message : { type : "history" , delta : 1 } , abortEvent : "before" } ,
1096+ { method : "reload" as const , message : { type : "reload" } , abortEvent : "after" } ,
1097+ { method : "reload" as const , message : { type : "reload" } , abortEvent : "none" } ,
1098+ ...[ "before" , "after" , "none" ] . map ( ( abortEvent ) => ( {
1099+ method : "goto" as const ,
1100+ message : { type : "navigate" , url : "http://10.255.255.1/" } ,
1101+ abortEvent,
1102+ blank : true ,
1103+ } ) ) ,
1104+ ] ) ( "viewer $method Stop settles ($abortEvent, blank=$blank)" , ( testCase ) =>
10071105 Effect . scoped (
10081106 Effect . gen ( function * ( ) {
1009- const { browser, tabId } = yield * ready ;
1107+ const { method, message, abortEvent } = testCase ;
1108+ const blank = "blank" in testCase ;
1109+ const { browser, broker, tabId } = yield * ready ;
1110+ const manager = yield * Manager . PreviewManager ;
1111+ const events = yield * manager . subscribeEvents ;
10101112 const viewer = yield * browser . attachViewer ( viewerInput ( tabId , true ) ) ;
10111113 yield * viewer . input ( { type : "takeControl" } ) ;
10121114 const started = Promise . withResolvers < void > ( ) ;
@@ -1020,26 +1122,77 @@ it.effect.each([
10201122 yield * Effect . promise ( ( ) => started . promise ) ;
10211123 const cdp = contexts [ 0 ] ! . sessions . at ( - 1 ) ! ;
10221124 const send = cdp . send . getMockImplementation ( ) ! ;
1125+ const request = {
1126+ url : ( ) => "https://example.com/pending" ,
1127+ method : ( ) => "GET" ,
1128+ isNavigationRequest : ( ) => true ,
1129+ frame : ( ) => page ,
1130+ failure : ( ) => ( { errorText : "net::ERR_ABORTED" } ) ,
1131+ } ;
10231132 const typed = Promise . withResolvers < void > ( ) ;
1133+ let cancelled = false ;
10241134 cdp . send . mockImplementation ( async ( operation , input ) => {
10251135 if ( operation === "Input.insertText" ) typed . resolve ( ) ;
1136+ if ( operation === "Page.stopLoading" ) {
1137+ cancelled = true ;
1138+ committed . reject ( new Error ( "net::ERR_ABORTED" ) ) ;
1139+ if ( abortEvent === "before" ) page . emit ( "requestfailed" , request ) ;
1140+ }
10261141 return send ( operation , input ) ;
10271142 } ) ;
10281143 const correction = yield * Effect . gen ( function * ( ) {
1029- yield * viewer . input ( { type : "navigate" , url : "http://localhost:5173/fixed" } ) ;
1144+ if ( ! blank ) yield * viewer . input ( { type : "navigate" , url : "http://localhost:5173/fixed" } ) ;
10301145 yield * viewer . input ( { type : "text" , text : "hello" } ) ;
10311146 } ) . pipe ( Effect . forkScoped ) ;
10321147 yield * Effect . addFinalizer ( ( ) => Effect . sync ( ( ) => committed . resolve ( ) ) ) ;
10331148 // The text reaches the page while the first navigation is still uncommitted.
10341149 yield * Effect . promise ( ( ) => typed . promise ) ;
10351150 expect ( page . goto ) . toHaveBeenLastCalledWith (
1036- "http://localhost:5173/fixed" ,
1151+ blank ? "http://10.255.255.1/" : "http://localhost:5173/fixed" ,
10371152 expect . objectContaining ( { waitUntil : "commit" } ) ,
10381153 ) ;
10391154 expect ( contexts [ 0 ] ! . sessions . at ( - 1 ) ! . send ) . toHaveBeenCalledWith ( "Input.insertText" , {
10401155 text : "hello" ,
10411156 } ) ;
1042- committed . resolve ( ) ;
1157+ page . emit ( "request" , request ) ;
1158+ let loadingEvent = yield * PubSub . take ( events ) ;
1159+ while (
1160+ loadingEvent . type !== "navigated" ||
1161+ loadingEvent . snapshot . navStatus . _tag !== "Loading"
1162+ ) {
1163+ loadingEvent = yield * PubSub . take ( events ) ;
1164+ }
1165+ const reader = yield * browser . attachViewer ( viewerInput ( tabId , false ) ) ;
1166+ yield * reader . input ( { type : "stopLoading" } ) ;
1167+ expect ( cancelled ) . toBe ( false ) ;
1168+ if ( abortEvent === "none" && ! blank ) committed . resolve ( ) ;
1169+ yield * viewer . input ( { type : "stopLoading" } ) ;
1170+ expect ( cancelled ) . toBe ( true ) ;
1171+ using timeout = vi . spyOn ( globalThis , "setTimeout" ) ;
1172+ if ( abortEvent === "after" ) page . emit ( "requestfailed" , request ) ;
1173+ expect ( timeout ) . not . toHaveBeenCalledWith ( expect . any ( Function ) , 5_000 ) ;
1174+ const status = yield * broker . invoke < PreviewAutomationStatus > ( {
1175+ scope,
1176+ tabId,
1177+ operation : "status" ,
1178+ input : { } ,
1179+ } ) ;
1180+ expect ( status . loading ) . toBe ( false ) ;
1181+ const expectedStatus = blank ? "Idle" : "Success" ;
1182+ let settled = yield * PubSub . take ( events ) ;
1183+ while ( settled . type !== "navigated" || settled . snapshot . navStatus . _tag !== expectedStatus ) {
1184+ settled = yield * PubSub . take ( events ) ;
1185+ }
1186+ if ( blank ) {
1187+ const { sessions } = yield * manager . list ( { threadId : scope . thread . threadId } ) ;
1188+ expect ( sessions . find ( ( session ) => session . tabId === tabId ) ?. navStatus ) . toEqual ( {
1189+ _tag : "Idle" ,
1190+ } ) ;
1191+ }
1192+ if ( settled . snapshot . navStatus . _tag === "Success" )
1193+ expect ( settled . snapshot . navStatus . url ) . toBe ( "http://localhost:5173/fixed" ) ;
1194+ expect ( page [ method ] ) . toHaveBeenCalledTimes ( method === "goto" && ! blank ? 2 : 1 ) ;
1195+ yield * viewer . input ( { type : "releaseControl" } ) ;
10431196 yield * Fiber . join ( navigation ) ;
10441197 yield * Fiber . join ( correction ) ;
10451198 } ) ,
0 commit comments