@@ -126,7 +126,10 @@ describe("waitForPageReady", () => {
126126
127127 await waitForPageReady ( { waitForNetworkIdle, evaluate } ) ;
128128
129+ // concurrency: 2 — the networkidle2 semantic navigation already uses, so
130+ // a single held-open request cannot block the wait until timeout.
129131 expect ( waitForNetworkIdle ) . toHaveBeenCalledTimes ( 1 ) ;
132+ expect ( waitForNetworkIdle ) . toHaveBeenCalledWith ( expect . objectContaining ( { concurrency : 2 } ) ) ;
130133 } ) ;
131134
132135 it ( "re-waits for idle once a still-parsing document finishes loading" , async ( ) => {
@@ -157,20 +160,96 @@ describe("waitForPageReady", () => {
157160 expect ( waitForNetworkIdle ) . toHaveBeenCalledTimes ( 2 ) ;
158161 } ) ;
159162
160- it ( "stays bounded when the document never reports a parsed state" , async ( ) => {
163+ it ( "rethrows degradable evaluate timeouts instead of polling through them" , async ( ) => {
164+ const waitForNetworkIdle = vi . fn ( async ( ) => undefined ) ;
165+ const evaluate = vi . fn ( async ( ) => {
166+ throw new TimeoutError (
167+ "Runtime.evaluate timed out. Increase the 'protocolTimeout' setting in launch/connect calls for a higher timeout if needed." ,
168+ ) ;
169+ } ) ;
170+
171+ await expect ( waitForPageReady ( { waitForNetworkIdle, evaluate } ) ) . rejects . toBeInstanceOf (
172+ TimeoutError ,
173+ ) ;
174+ // The stage degrades on the first timeout — it is not retried as
175+ // "still loading" for the full deadline.
176+ expect ( evaluate ) . toHaveBeenCalledTimes ( 1 ) ;
177+ } ) ;
178+
179+ it ( "stays bounded by wall clock when the document never reports a parsed state" , async ( ) => {
161180 vi . useFakeTimers ( ) ;
162181 try {
163182 const waitForNetworkIdle = vi . fn ( async ( ) => undefined ) ;
164183 const evaluate = vi . fn ( async ( ) => true ) ;
165184
166- const done = waitForPageReady ( { waitForNetworkIdle, evaluate } ) ;
185+ const start = Date . now ( ) ;
186+ const done = waitForPageReady ( { waitForNetworkIdle, evaluate } , { timeoutMs : 15_000 } ) ;
167187 await vi . runAllTimersAsync ( ) ;
168188 await done ;
169189
170- expect ( evaluate ) . toHaveBeenCalledTimes ( 30 ) ;
171- expect ( waitForNetworkIdle ) . toHaveBeenCalledTimes ( 2 ) ;
190+ expect ( Date . now ( ) - start ) . toBeLessThanOrEqual ( 15_000 ) ;
191+ // ~500ms polls over the deadline — not unbounded.
192+ expect ( evaluate . mock . calls . length ) . toBeLessThanOrEqual ( 31 ) ;
172193 } finally {
173194 vi . useRealTimers ( ) ;
174195 }
175196 } ) ;
197+
198+ it ( "bounds a hung evaluate to the shared deadline, not per-iteration latency" , async ( ) => {
199+ vi . useFakeTimers ( ) ;
200+ try {
201+ // Each probe hangs for a protocol-timeout-scale 30s; the settle must
202+ // still finish at the deadline rather than paying 30s per poll.
203+ const evaluate = vi . fn (
204+ ( ) =>
205+ new Promise < unknown > ( ( resolve ) => {
206+ setTimeout ( ( ) => resolve ( true ) , 30_000 ) ;
207+ } ) ,
208+ ) ;
209+ const waitForNetworkIdle = vi . fn ( async ( ) => undefined ) ;
210+
211+ const start = Date . now ( ) ;
212+ const done = waitForPageReady ( { waitForNetworkIdle, evaluate } , { timeoutMs : 15_000 } ) ;
213+ await vi . advanceTimersByTimeAsync ( 15_000 ) ;
214+ await done ;
215+
216+ expect ( Date . now ( ) - start ) . toBeLessThanOrEqual ( 15_000 ) ;
217+ expect ( evaluate ) . toHaveBeenCalledTimes ( 1 ) ;
218+ } finally {
219+ vi . useRealTimers ( ) ;
220+ }
221+ } ) ;
222+
223+ it ( "skips the leading idle wait in after-load mode on a parsed document" , async ( ) => {
224+ const waitForNetworkIdle = vi . fn ( async ( ) => undefined ) ;
225+ const evaluate = vi . fn ( async ( ) => false ) ;
226+
227+ await waitForPageReady ( { waitForNetworkIdle, evaluate } , { networkIdle : "after-load" } ) ;
228+
229+ expect ( waitForNetworkIdle ) . not . toHaveBeenCalled ( ) ;
230+ } ) ;
231+
232+ it ( "still idles in after-load mode once a loading document finishes" , async ( ) => {
233+ const waitForNetworkIdle = vi . fn ( async ( ) => undefined ) ;
234+ let loading = true ;
235+ const evaluate = vi . fn ( async ( ) => {
236+ const was = loading ;
237+ loading = false ;
238+ return was ;
239+ } ) ;
240+
241+ await waitForPageReady ( { waitForNetworkIdle, evaluate } , { networkIdle : "after-load" } ) ;
242+
243+ expect ( waitForNetworkIdle ) . toHaveBeenCalledTimes ( 1 ) ;
244+ } ) ;
245+
246+ it ( "is a no-op when the deadline has already passed" , async ( ) => {
247+ const waitForNetworkIdle = vi . fn ( async ( ) => undefined ) ;
248+ const evaluate = vi . fn ( async ( ) => false ) ;
249+
250+ await waitForPageReady ( { waitForNetworkIdle, evaluate } , { timeoutMs : 0 } ) ;
251+
252+ expect ( evaluate ) . not . toHaveBeenCalled ( ) ;
253+ expect ( waitForNetworkIdle ) . not . toHaveBeenCalled ( ) ;
254+ } ) ;
176255} ) ;
0 commit comments