Skip to content

armgit / moli

Public Imported from GitHub
0

Best headless browser for AI agents. Lite, Fast, High-Compatibility. Built in Rust

refactor(fetch): unify readiness deadline

ldm0f23fca74ad31
diff --git a/moli-core/src/runtime/fetch_deadline.rs b/moli-core/src/runtime/fetch_deadline.rs
new file mode 100644
index 000000000..c48b50f82
--- /dev/null
+++ b/moli-core/src/runtime/fetch_deadline.rs
@@ -0,0 +1,117 @@
+//! One absolute timeout budget shared by every fetch-readiness phase.
+
+use anyhow::{Context, Result, anyhow, bail};
+use std::{future::Future, time::Duration};
+
+/// An absolute deadline for a complete fetch-readiness plan.
+///
+/// Cloning or passing this value to another phase never restarts the timeout.
+/// The initial navigation, lifecycle target, replacement navigation, response
+/// match, selector, and script waits can therefore consume one caller-owned
+/// budget instead of each receiving a fresh `Duration`.
+#[derive(Clone, Copy, Debug)]
+pub struct FetchDeadline {
+ timeout: Duration,
+ at: tokio::time::Instant,
+}
+
+impl FetchDeadline {
+ /// Starts a new deadline with `timeout` as its complete readiness budget.
+ pub fn new(timeout: Duration) -> Result<Self> {
+ let at = tokio::time::Instant::now()
+ .checked_add(timeout)
+ .with_context(|| {
+ anyhow!(
+ "fetch readiness timeout of {} ms exceeds the supported range",
+ timeout.as_millis()
+ )
+ })?;
+ Ok(Self { timeout, at })
+ }
+
+ /// Returns the original budget used to create this deadline.
+ pub fn timeout(self) -> Duration {
+ self.timeout
+ }
+
+ /// Returns the budget still available without changing the deadline.
+ pub fn remaining(self) -> Duration {
+ self.at
+ .saturating_duration_since(tokio::time::Instant::now())
+ }
+
+ pub(super) fn at(self) -> tokio::time::Instant {
+ self.at
+ }
+
+ pub(super) fn at_with_cushion(self, cushion: Duration) -> Result<tokio::time::Instant> {
+ self.at.checked_add(cushion).with_context(|| {
+ anyhow!(
+ "fetch readiness deadline with {} ms cleanup cushion exceeds the supported range",
+ cushion.as_millis()
+ )
+ })
+ }
+
+ pub(super) async fn wait<T, F>(self, phase: &'static str, future: F) -> Result<T>
+ where
+ F: Future<Output = Result<T>>,
+ {
+ match tokio::time::timeout_at(self.at, future).await {
+ Ok(result) => result,
+ Err(_) => bail!(
+ "fetch readiness timed out after {} ms while {phase}",
+ self.timeout.as_millis()
+ ),
+ }
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::FetchDeadline;
+ use anyhow::Result;
+ use std::time::Duration;
+
+ #[tokio::test]
+ async fn remaining_budget_is_not_restarted_between_phases() -> Result<()> {
+ let deadline = FetchDeadline::new(Duration::from_millis(250))?;
+ let initial = deadline.remaining();
+ tokio::time::sleep(Duration::from_millis(75)).await;
+ let after_first_phase = deadline.remaining();
+
+ assert!(initial <= Duration::from_millis(250));
+ assert!(
+ after_first_phase < initial,
+ "passing the deadline to another phase must retain elapsed time"
+ );
+ assert!(after_first_phase <= Duration::from_millis(200));
+ Ok(())
+ }
+
+ #[tokio::test]
+ async fn wait_reports_total_budget_and_active_phase() -> Result<()> {
+ let deadline = FetchDeadline::new(Duration::from_millis(25))?;
+ let error = deadline
+ .wait("waiting for a selector", async {
+ tokio::time::sleep(Duration::from_secs(1)).await;
+ Ok(())
+ })
+ .await
+ .unwrap_err();
+
+ assert_eq!(
+ error.to_string(),
+ "fetch readiness timed out after 25 ms while waiting for a selector"
+ );
+ Ok(())
+ }
+
+ #[test]
+ fn impossible_deadline_range_is_an_anyhow_error() {
+ let error = FetchDeadline::new(Duration::MAX).unwrap_err();
+ assert!(error.to_string().contains(
+ "fetch readiness timeout of 18446744073709551615999 ms exceeds the supported range"
+ ));
+ }
+}
diff --git a/moli-core/src/runtime/lifecycle_fetch.rs b/moli-core/src/runtime/lifecycle_fetch.rs
index 78fb309c5..bb9a4cf1d 100644
--- a/moli-core/src/runtime/lifecycle_fetch.rs
+++ b/moli-core/src/runtime/lifecycle_fetch.rs
@@ -5,7 +5,7 @@
//! lifecycle decision cannot reset or extend the caller's timeout budget.
use super::{
- Browser, FetchedDocument, RenderedDomWaitUntil, RendererLifecycleDecider,
+ Browser, FetchDeadline, FetchedDocument, RenderedDomWaitUntil, RendererLifecycleDecider,
RendererLifecycleDecision, RendererLifecycleSnapshot, RendererReplyBoundary,
};
use anyhow::{Context, Result, anyhow};
@@ -27,6 +27,25 @@ impl Browser {
timeout: Duration,
decider: F,
) -> Result<FetchedDocument>
+ where
+ F: FnOnce(RendererLifecycleSnapshot) -> Result<RendererLifecycleDecision> + Send + 'static,
+ {
+ let deadline = FetchDeadline::new(timeout)?;
+ self.fetch_document_with_lifecycle_decider_and_deadline(
+ request, wait_until, deadline, decider,
+ )
+ .await
+ }
+
+ /// Applies a lifecycle decision using a caller-owned absolute deadline.
+ /// The same deadline can then gate response, selector, and script waits.
+ pub async fn fetch_document_with_lifecycle_decider_and_deadline<F>(
+ &self,
+ request: Request,
+ wait_until: RenderedDomWaitUntil,
+ deadline: FetchDeadline,
+ decider: F,
+ ) -> Result<FetchedDocument>
where
F: FnOnce(RendererLifecycleSnapshot) -> Result<RendererLifecycleDecision> + Send + 'static,
{
@@ -43,7 +62,7 @@ impl Browser {
self.fetch_document_with_wait(
request,
wait_until,
- timeout,
+ deadline,
RendererReplyBoundary::Stage,
Some(decider),
)
diff --git a/moli-core/src/runtime/mod.rs b/moli-core/src/runtime/mod.rs
index df4d2c649..83356d2a3 100644
--- a/moli-core/src/runtime/mod.rs
+++ b/moli-core/src/runtime/mod.rs
@@ -1,3 +1,4 @@
+mod fetch_deadline;
mod lifecycle_fetch;
mod navigation_engine;
pub mod storage_partition;
@@ -34,6 +35,7 @@ use url::Url;
pub use crate::renderer::ExternalRawDocumentBodyStream;
pub use crate::renderer::PageVmInitStage;
pub use crate::renderer::RendererReplyBoundary;
+pub use fetch_deadline::FetchDeadline;
pub use moli_renderer_v8::{
DetachedParserScriptFetchContinuation, RendererBrowserContextRuntime,
RendererBrowserContextRuntimeOwner, RendererBrowserContextRuntimeOwnerAccess,
@@ -409,11 +411,26 @@ impl Browser {
request: Request,
wait_until: RenderedDomWaitUntil,
timeout: Duration,
+ ) -> Result<FetchedDocument> {
+ let deadline = FetchDeadline::new(timeout)?;
+ self.fetch_request_document_allow_http_error_with_wait_until_deadline(
+ request, wait_until, deadline,
+ )
+ .await
+ }
+
+ /// Fetches a document to `wait_until` without starting a new timeout
+ /// budget. Callers can reuse `deadline` for later readiness conditions.
+ pub async fn fetch_request_document_allow_http_error_with_wait_until_deadline(
+ &self,
+ request: Request,
+ wait_until: RenderedDomWaitUntil,
+ deadline: FetchDeadline,
) -> Result<FetchedDocument> {
self.fetch_document_with_wait(
request,
wait_until,
- timeout,
+ deadline,
RendererReplyBoundary::Stage,
None,
)
@@ -424,10 +441,11 @@ impl Browser {
&self,
request: Request,
wait_until: RenderedDomWaitUntil,
- timeout: Duration,
+ deadline: FetchDeadline,
reply_boundary: RendererReplyBoundary,
lifecycle_decider: Option<RendererLifecycleDecider>,
) -> Result<FetchedDocument> {
+ let timeout = deadline.timeout();
let stage = match wait_until {
RenderedDomWaitUntil::DomContentLoaded => PageVmInitStage::DomContentLoaded,
RenderedDomWaitUntil::Load => PageVmInitStage::Load,
@@ -444,15 +462,18 @@ impl Browser {
stage = ?stage,
"starting fetch_document_allow_http_error_with_wait_until deadline"
);
- let outer_timeout = wait_until_outer_timeout(wait_until, timeout);
- let deadline = tokio::time::Instant::now()
- .checked_add(outer_timeout)
- .with_context(|| {
- anyhow!(
- "fetch wait_until {wait_until:?} timeout of {} ms exceeds the supported range",
- timeout.as_millis()
- )
- })?;
+ // Network-idle and DOM-stable return a best-effort Page after their
+ // renderer-side readiness timer expires. Keep the historical one-second
+ // transport cushion so that Page can cross the owner boundary, while
+ // the logical FetchDeadline remains expired for every later plan step.
+ let outer_deadline = match wait_until {
+ RenderedDomWaitUntil::NetworkIdle | RenderedDomWaitUntil::DomStable => {
+ deadline.at_with_cushion(Duration::from_secs(1))?
+ }
+ RenderedDomWaitUntil::DomContentLoaded
+ | RenderedDomWaitUntil::Load
+ | RenderedDomWaitUntil::Done => deadline.at(),
+ };
let requested_url = request.url.clone();
if is_about_blank_url(&requested_url) {
return self
@@ -461,7 +482,7 @@ impl Browser {
wait_until,
timeout,
stage,
- deadline,
+ outer_deadline,
self.materialize_static_html_page(
&raw_url,
requested_url,
@@ -486,7 +507,7 @@ impl Browser {
wait_until,
timeout,
stage,
- deadline,
+ outer_deadline,
self.fetch_service_worker_main_resource_for_navigation(
&request,
&navigation_loader,
@@ -514,7 +535,7 @@ impl Browser {
wait_until,
timeout,
stage,
- deadline,
+ outer_deadline,
self.materialize_streaming_raw_response_page(
&raw_url,
requested_url,
@@ -536,7 +557,7 @@ impl Browser {
wait_until,
timeout,
stage,
- deadline,
+ outer_deadline,
navigation_loader.fetch_raw_stream(request),
)
.await?;
@@ -557,7 +578,7 @@ impl Browser {
wait_until,
timeout,
stage,
- deadline,
+ outer_deadline,
self.materialize_streaming_raw_response_page(
&raw_url,
requested_url,
@@ -629,6 +650,23 @@ impl Browser {
.await
}
+ /// Waits for a selector using the unspent portion of `deadline`.
+ pub async fn wait_for_selector_with_deadline(
+ &self,
+ page: &mut Page,
+ selector: &str,
+ deadline: FetchDeadline,
+ ) -> Result<crate::page::RendererDocumentQuerySelectorNode> {
+ let loader = self.resource_request_client();
+ let remaining = deadline.remaining();
+ deadline
+ .wait(
+ "waiting for a selector",
+ page.wait_for_selector(&loader, selector, remaining),
+ )
+ .await
+ }
+
pub async fn wait_for_script_truthy(
&self,
page: &mut Page,
@@ -639,6 +677,24 @@ impl Browser {
.await
}
+ /// Waits for a truthy script result using the unspent portion of
+ /// `deadline`.
+ pub async fn wait_for_script_truthy_with_deadline(
+ &self,
+ page: &mut Page,
+ expression: &str,
+ deadline: FetchDeadline,
+ ) -> Result<()> {
+ let loader = self.resource_request_client();
+ let remaining = deadline.remaining();
+ deadline
+ .wait(
+ "waiting for a script to become truthy",
+ page.wait_for_script_truthy(&loader, expression, remaining),
+ )
+ .await
+ }
+
pub async fn wait_for_subresource_response(
&self,
page: &mut Page,
@@ -649,6 +705,23 @@ impl Browser {
.await
}
+ /// Waits for a matching response using the unspent portion of `deadline`.
+ pub async fn wait_for_subresource_response_with_deadline(
+ &self,
+ page: &mut Page,
+ criteria: crate::page::SubresourceResponseWaitCriteria,
+ deadline: FetchDeadline,
+ ) -> Result<()> {
+ let loader = self.resource_request_client();
+ let remaining = deadline.remaining();
+ deadline
+ .wait(
+ "waiting for a subresource response",
+ page.wait_for_subresource_response(&loader, criteria, remaining),
+ )
+ .await
+ }
+
async fn wait_for_fetch_readiness(
&self,
page: &mut Page,
diff --git a/moli-core/src/runtime/tests.rs b/moli-core/src/runtime/tests.rs
index e3ea96e7d..cc3c7e76d 100644
--- a/moli-core/src/runtime/tests.rs
+++ b/moli-core/src/runtime/tests.rs
@@ -1,8 +1,8 @@
use moli_test_support as support;
use super::{
- Browser, NavigationEngine, NavigationPageStorageHandles, NavigationResourceStorageHandles,
- RenderedDomWaitUntil,
+ Browser, FetchDeadline, FetchedDocument, NavigationEngine, NavigationPageStorageHandles,
+ NavigationResourceStorageHandles, RenderedDomWaitUntil,
external_raw_document_body_from_streaming_response_with_body_eof_observer,
};
use crate::{
@@ -2077,6 +2077,55 @@ async fn wait_for_selector_cancellation_restores_entry_for_close() -> Result<()>
Ok(())
}
+#[tokio::test(flavor = "multi_thread")]
+async fn fetch_deadline_spans_lifecycle_selector_and_script_without_reset() -> Result<()> {
+ let server = FixtureServer::spawn().await?;
+ let browser = Browser::new(AppConfig::default())?;
+ let renderer_owner = browser.js_runtime.renderer_owner_handle();
+ let started = std::time::Instant::now();
+ let deadline = FetchDeadline::new(Duration::from_millis(600))?;
+ let fetched = browser
+ .fetch_request_document_allow_http_error_with_wait_until_deadline(
+ Request::get(&server.url("/static"))?,
+ RenderedDomWaitUntil::Load,
+ deadline,
+ )
+ .await?;
+ let FetchedDocument::Page(mut page) = fetched else {
+ panic!("static HTML fixture unexpectedly produced a raw document");
+ };
+ let page_id = page.renderer_page_id();
+
+ browser
+ .wait_for_selector_with_deadline(&mut page, "body", deadline)
+ .await?;
+ tokio::time::sleep(Duration::from_millis(300)).await;
+ let error = browser
+ .wait_for_script_truthy_with_deadline(&mut page, "false", deadline)
+ .await
+ .unwrap_err();
+
+ assert!(
+ error
+ .chain()
+ .any(|cause| cause.to_string().contains("timed out")),
+ "unexpected deadline error: {error:?}"
+ );
+ assert!(
+ started.elapsed() < Duration::from_millis(800),
+ "the script phase appears to have received a fresh timeout: elapsed={:?}",
+ started.elapsed()
+ );
+ tokio::time::timeout(Duration::from_secs(1), page.close_async()).await??;
+ assert!(
+ renderer_owner.record(page_id).is_none(),
+ "a deadline-cancelled wait must return the Page owner entry for close"
+ );
+
+ server.shutdown().await;
+ Ok(())
+}
+
#[tokio::test(flavor = "multi_thread")]
async fn live_page_selector_wait_allows_unrelated_evaluate_while_pending() -> Result<()> {
let server = FixtureServer::spawn().await?;
diff --git a/moli-test-support/src/routes_wait.rs b/moli-test-support/src/routes_wait.rs
index 04185d402..3f8f96d82 100644
--- a/moli-test-support/src/routes_wait.rs
+++ b/moli-test-support/src/routes_wait.rs
@@ -23,6 +23,38 @@ const WAIT_UNTIL_COMPLETE_DELAYED_DOM_MUTATION_HTML: &str = "<!doctype html><htm
const WAIT_UNTIL_COMPLETE_SLOW_FETCH_HTML: &str = "<!doctype html><html><body data-state=\"init\"><script>window.addEventListener('load', () => { fetch('/wait-until-very-slow-data').then(r => r.text()).then(text => { document.body.setAttribute('data-state', text); const main = document.createElement('main'); main.id = 'late-slow-fetch'; main.textContent = text; document.body.appendChild(main); }); });</script></body></html>";
const WAIT_UNTIL_COMPLETE_SLOW_XHR_HTML: &str = "<!doctype html><html><body data-state=\"init\"><script>window.addEventListener('load', () => { const xhr = new XMLHttpRequest(); xhr.open('GET', '/wait-until-very-slow-data'); xhr.onload = () => { document.body.setAttribute('data-state', xhr.responseText); const main = document.createElement('main'); main.id = 'late-slow-xhr'; main.textContent = xhr.responseText; document.body.appendChild(main); }; xhr.send(); });</script></body></html>";
const WAIT_UNTIL_DELAYED_JSON_FETCH_HTML: &str = "<!doctype html><html><body data-state=\"init\"><script>window.addEventListener('load', () => { setTimeout(() => { fetch('/wait-until-json-data').then(r => r.json()).then(data => { document.body.setAttribute('data-state', data.ret[0]); const main = document.createElement('main'); main.id = 'late-json'; main.textContent = data.data.url; document.body.appendChild(main); }); }, 75); });</script></body></html>";
+const WAIT_UNTIL_READINESS_PLAN_HTML: &str = r#"<!doctype html>
+<html><head><title>readiness plan</title></head><body data-readiness-order="">
+<script>
+window.readinessOrder = [];
+window.runtimeOwnedInOrderLoadOrder = [];
+const noteReadiness = value => {
+ window.readinessOrder.push(value);
+ document.body.setAttribute('data-readiness-order', window.readinessOrder.join(','));
+};
+fetch('/wait-until-json-data').then(response => response.json()).then(data => {
+ document.body.setAttribute('data-readiness-response', data.ret[0]);
+ noteReadiness('response');
+});
+</script>
+<script src="/assets/runtime_owned_in_order_load_slow.js"></script>
+<script>
+window.addEventListener('load', () => {
+ setTimeout(() => {
+ const ready = document.createElement('main');
+ ready.id = 'readiness-selector';
+ ready.textContent = 'selector-ready';
+ document.body.appendChild(ready);
+ noteReadiness('selector');
+ }, 100);
+ setTimeout(() => {
+ window.readinessScriptReady = true;
+ document.body.setAttribute('data-readiness-script', 'true');
+ noteReadiness('script');
+ }, 500);
+});
+</script>
+</body></html>"#;
const WAIT_UNTIL_COOKIE_FETCH_HTML: &str = "<!doctype html><html><body data-state=\"init\"><script>window.addEventListener('load', () => { setTimeout(() => { fetch('/wait-until-cookie-data').then(r => r.json()).then(data => { document.body.setAttribute('data-state', data.cookie); const main = document.createElement('main'); main.id = 'late-cookie'; main.textContent = data.cookie; document.body.appendChild(main); }); }, 75); });</script></body></html>";
// See WAIT_UNTIL_DELAYED_FETCH_HTML for the 75 -> 300 ms rationale.
const WAIT_UNTIL_DELAYED_XHR_HTML: &str = "<!doctype html><html><body data-state=\"init\"><script>window.addEventListener('load', () => { setTimeout(() => { const xhr = new XMLHttpRequest(); xhr.open('GET', '/wait-until-data'); xhr.onload = () => { document.body.setAttribute('data-state', xhr.responseText); const main = document.createElement('main'); main.id = 'late-xhr'; main.textContent = xhr.responseText; document.body.appendChild(main); }; xhr.send(); }, 300); });</script></body></html>";
@@ -39,6 +71,41 @@ const WAIT_UNTIL_INTERVAL_FETCH_HTML: &str = "<!doctype html><html><body data-st
const WAIT_UNTIL_INTERVAL_DOM_MUTATION_HTML: &str = "<!doctype html><html><body data-state=\"init\"><main id=\"mutation-count\">0</main><script>window.addEventListener('load', () => { setInterval(() => { const count = Number(document.body.getAttribute('data-mutation-count') || '0') + 1; document.body.setAttribute('data-mutation-count', String(count)); document.getElementById('mutation-count').textContent = String(count); }, 50); });</script></body></html>";
const WAIT_UNTIL_HTTP_ERROR_NAVIGATION_CHALLENGE_HTML: &str = "<!doctype html><html><head><title>403 challenge</title></head><body><main id=\"challenge\">http-error-navigation=challenge</main><script>window.addEventListener('load', () => { setTimeout(() => { document.cookie = 'moli-http-error-navigation=passed; Path=/; Max-Age=3600; SameSite=Lax'; location.reload(); }, 75); });</script></body></html>";
const WAIT_UNTIL_HTTP_ERROR_LATE_NAVIGATION_CHALLENGE_HTML: &str = "<!doctype html><html><head><title>late 403 challenge</title></head><body><main id=\"challenge\">http-error-navigation=late-challenge</main><script>window.addEventListener('load', () => { setTimeout(() => { document.cookie = 'moli-http-error-navigation=passed; Path=/; Max-Age=3600; SameSite=Lax'; location.reload(); }, 1200); });</script></body></html>";
+const WAIT_UNTIL_READINESS_HTTP_ERROR_CHALLENGE_HTML: &str = r#"<!doctype html>
+<html><head><title>readiness 403 challenge</title></head><body>
+<main id="readiness-challenge">readiness=challenge</main>
+<script>
+window.addEventListener('load', () => {
+ setTimeout(() => {
+ document.cookie = 'moli-readiness-navigation=passed; Path=/; Max-Age=3600; SameSite=Lax';
+ location.reload();
+ }, 250);
+});
+</script>
+</body></html>"#;
+const WAIT_UNTIL_READINESS_HTTP_ERROR_FINAL_HTML: &str = r#"<!doctype html>
+<html><head><title>readiness navigation passed</title></head><body>
+<main id="readiness-navigation-target">readiness=navigation-done</main>
+<script>
+window.addEventListener('load', () => {
+ setTimeout(() => {
+ fetch('/wait-until-json-data').then(response => response.json()).then(data => {
+ document.body.setAttribute('data-readiness-navigation-response', data.ret[0]);
+ });
+ }, 100);
+ setTimeout(() => {
+ const ready = document.createElement('main');
+ ready.id = 'readiness-navigation-selector';
+ ready.textContent = 'readiness-navigation=selector';
+ document.body.appendChild(ready);
+ }, 300);
+ setTimeout(() => {
+ window.readinessNavigationScriptReady = true;
+ document.body.setAttribute('data-readiness-navigation-script', 'true');
+ }, 700);
+});
+</script>
+</body></html>"#;
const WAIT_UNTIL_HTTP_ERROR_NAVIGATION_FINAL_HTML: &str = "<!doctype html><html><head><title>challenge passed</title></head><body><main id=\"http-error-navigation-target\">http-error-navigation=done</main><script>window.httpErrorNavigationDcl = false; window.httpErrorNavigationLoad = false; document.addEventListener('DOMContentLoaded', () => { window.httpErrorNavigationDcl = true; document.body.setAttribute('data-reached-dcl', 'true'); const script = document.createElement('script'); script.src = '/assets/runtime_owned_in_order_load_very_slow.js'; script.onload = () => { window.httpErrorNavigationSlowScript = true; const tail = document.createElement('main'); tail.id = 'http-error-navigation-load-tail'; tail.textContent = 'http-error-navigation=load-tail'; document.body.appendChild(tail); }; document.head.appendChild(script); }); window.addEventListener('load', () => { window.httpErrorNavigationLoad = true; document.body.setAttribute('data-reached-load', 'true'); });</script></body></html>";
const WAIT_UNTIL_HTTP_ERROR_IMMEDIATE_NAVIGATION_CHALLENGE_HTML: &str = r#"<!doctype html>
<html><head><title>immediate 403 challenge</title></head><body>
@@ -147,6 +214,10 @@ pub(super) fn add_wait_routes(router: Router) -> Router {
"/wait-until-delayed-json-fetch",
get(wait_until_delayed_json_fetch_page),
)
+ .route(
+ "/wait-until-readiness-plan",
+ get(wait_until_readiness_plan_page),
+ )
.route(
"/wait-until-cookie-fetch",
get(wait_until_cookie_fetch_page),
@@ -212,6 +283,10 @@ pub(super) fn add_wait_routes(router: Router) -> Router {
"/wait-until-http-error-late-navigation",
get(wait_until_http_error_late_navigation_page),
)
+ .route(
+ "/wait-until-readiness-http-error-navigation",
+ get(wait_until_readiness_http_error_navigation_page),
+ )
.route("/wait-until-data", get(wait_until_data_page))
.route("/wait-until-json-data", get(wait_until_json_data_page))
.route("/wait-until-cookie-data", get(wait_until_cookie_data_page))
@@ -329,6 +404,18 @@ async fn wait_until_http_error_late_navigation_page(headers: HeaderMap) -> Respo
.into_response()
}
+async fn wait_until_readiness_http_error_navigation_page(headers: HeaderMap) -> Response {
+ if has_cookie(&headers, "moli-readiness-navigation=passed") {
+ return Html(WAIT_UNTIL_READINESS_HTTP_ERROR_FINAL_HTML).into_response();
+ }
+
+ (
+ StatusCode::FORBIDDEN,
+ Html(WAIT_UNTIL_READINESS_HTTP_ERROR_CHALLENGE_HTML),
+ )
+ .into_response()
+}
+
async fn wait_until_domcontentloaded_fetch_page() -> Html<&'static str> {
Html(WAIT_UNTIL_DOMCONTENTLOADED_FETCH_HTML)
}
@@ -365,6 +452,10 @@ async fn wait_until_delayed_json_fetch_page() -> Html<&'static str> {
Html(WAIT_UNTIL_DELAYED_JSON_FETCH_HTML)
}
+async fn wait_until_readiness_plan_page() -> Html<&'static str> {
+ Html(WAIT_UNTIL_READINESS_PLAN_HTML)
+}
+
async fn wait_until_cookie_fetch_page() -> Response {
(
[(
diff --git a/moli/src/app.rs b/moli/src/app.rs
index 5c54604a4..78edd42a9 100644
--- a/moli/src/app.rs
+++ b/moli/src/app.rs
@@ -1,11 +1,12 @@
//! Callable command runner for the Moli CLI.
mod http_error_navigation;
+mod readiness;
-use std::{io::Write, sync::Arc, time::Duration};
+use std::{io::Write, sync::Arc};
use crate::{
- cli::{Cli, Commands, FetchWaitUntil, normalize_args_for_compat},
+ cli::{Cli, Commands, normalize_args_for_compat},
config::AppConfig,
cookie_cache, fetch_dump,
};
@@ -13,13 +14,12 @@ use anyhow::Result;
use anyhow::{Context, anyhow};
use clap::Parser;
use moli_core::runtime::{
- Browser, FetchedDocument, NavigationRuntimeConfig, PageVmInitStage, RenderedDomWaitUntil,
- storage_partition::StoragePartitionState,
+ Browser, FetchedDocument, NavigationRuntimeConfig, storage_partition::StoragePartitionState,
};
use moli_fetch::{Request, ensure_http_status_success};
use moli_protocol_server::ProtocolServer;
-use self::http_error_navigation::{fetch_with_http_error_navigation, is_http_error_status};
+use self::{http_error_navigation::is_http_error_status, readiness::ReadinessPlan};
pub async fn run_from_env() -> Result<()> {
let cli = Cli::parse_from(normalize_args_for_compat(std::env::args_os()));
@@ -44,59 +44,10 @@ pub async fn run_cli_with_config<W: Write>(
let browser = Browser::new(config.browser.clone())
.context("failed to initialize browser runtime")?;
load_cookie_state(&browser, &config)?;
- let timeout = std::time::Duration::from_millis(args.timeout);
- let navigation_grace = Duration::from_millis(args.redirect_wait_ms);
let request = build_fetch_request(&args.url, &config)?;
- let fetch_result = match args.wait_until {
- FetchWaitUntil::Done => {
- fetch_with_http_error_navigation(
- &browser,
- request,
- RenderedDomWaitUntil::Done,
- timeout,
- navigation_grace,
- )
- .await
- }
- FetchWaitUntil::DomContentLoaded => {
- fetch_with_http_error_navigation(
- &browser,
- request,
- RenderedDomWaitUntil::DomContentLoaded,
- timeout,
- navigation_grace,
- )
- .await
- }
- FetchWaitUntil::Load => {
- fetch_with_http_error_navigation(
- &browser,
- request,
- RenderedDomWaitUntil::Load,
- timeout,
- navigation_grace,
- )
- .await
- }
- FetchWaitUntil::NetworkIdle => {
- browser
- .fetch_request_document_allow_http_error_with_wait_until(
- request,
- RenderedDomWaitUntil::NetworkIdle,
- timeout,
- )
- .await
- }
- FetchWaitUntil::DomStable => {
- browser
- .fetch_request_document_allow_http_error_with_wait_until(
- request,
- RenderedDomWaitUntil::DomStable,
- timeout,
- )
- .await
- }
- };
+ let readiness =
+ ReadinessPlan::from_fetch_args(&args, config.fetch.response_wait.clone())?;
+ let fetch_result = readiness.fetch_document(&browser, request).await;
let fetched_document = match fetch_result {
Ok(document) => document,
Err(error) => {
@@ -108,7 +59,7 @@ pub async fn run_cli_with_config<W: Write>(
let mut page = match fetched_document {
FetchedDocument::Page(page) => page,
FetchedDocument::Raw(raw_document) => {
- if lifecycle_stage_for_fetch_wait(args.wait_until).is_some()
+ if readiness.lifecycle_stage().is_some()
&& is_http_error_status(raw_document.status())
{
let status_error = ensure_http_status_success(
@@ -123,12 +74,7 @@ pub async fn run_cli_with_config<W: Write>(
)
.with_context(|| anyhow!("failed to fetch `{}`", args.url));
}
- if config.fetch.response_wait.is_some()
- || args.wait_selector.is_some()
- || args.wait_script.is_some()
- || args.wait_script_file.is_some()
- || args.delay_ms > 0
- {
+ if readiness.has_page_waits() || args.delay_ms > 0 {
finalize_fetch_browser(browser);
return Err(anyhow!(
"raw non-HTML document fetch does not support page wait options"
@@ -146,9 +92,7 @@ pub async fn run_cli_with_config<W: Write>(
}
};
- if lifecycle_stage_for_fetch_wait(args.wait_until).is_some()
- && is_http_error_status(page.status())
- {
+ if readiness.lifecycle_stage().is_some() && is_http_error_status(page.status()) {
let error = ensure_http_status_success(
page.final_url().as_str(),
page.status(),
@@ -168,42 +112,15 @@ pub async fn run_cli_with_config<W: Write>(
return Err(error).with_context(|| anyhow!("failed to fetch `{}`", args.url));
}
- if let Some(response_wait) = config.fetch.response_wait.clone() {
- browser
- .wait_for_subresource_response(&mut page, response_wait, timeout)
- .await
- .context("failed while waiting for subresource response")?;
- }
-
- if let Some(selector) = args.wait_selector.as_deref() {
- browser
- .wait_for_selector(&mut page, selector, timeout)
- .await
- .with_context(|| anyhow!("failed while waiting for selector `{selector}`"))?;
- }
-
- let wait_script = match (
- args.wait_script.as_deref(),
- args.wait_script_file.as_deref(),
- ) {
- (Some(_), Some(_)) => {
- return Err(anyhow!(
- "`--wait-script` and `--wait-script-file` are mutually exclusive"
- ));
+ if let Err(error) = readiness.wait_for_page(&browser, &mut page).await {
+ if let Err(close_error) = page.close_async().await {
+ tracing::warn!(
+ error = %close_error,
+ "failed to close fetched page after readiness failure"
+ );
}
- (Some(script), None) => Some(script.to_owned()),
- (None, Some(path)) => Some(
- std::fs::read_to_string(path)
- .with_context(|| anyhow!("failed to read wait script file `{path}`"))?,
- ),
- (None, None) => None,
- };
-
- if let Some(script) = wait_script.as_deref() {
- browser
- .wait_for_script_truthy(&mut page, script, timeout)
- .await
- .context("failed while waiting for script to become truthy")?;
+ finalize_fetch_browser(browser);
+ return Err(error).with_context(|| anyhow!("failed to fetch `{}`", args.url));
}
if args.delay_ms > 0 {
@@ -248,18 +165,6 @@ fn build_fetch_request(url: &str, config: &AppConfig) -> Result<Request> {
Ok(request)
}
-fn lifecycle_stage_for_fetch_wait(wait_until: FetchWaitUntil) -> Option<PageVmInitStage> {
- // Only waits defined by a concrete Document lifecycle milestone opt into
- // HTTP-error navigation recovery. `done` uses the fetch path's historical
- // load boundary; network-idle and DOM-stable remain post-load page-state
- // waits and keep their existing HTTP-error dump behavior.
- match wait_until {
- FetchWaitUntil::DomContentLoaded => Some(PageVmInitStage::DomContentLoaded),
- FetchWaitUntil::Load | FetchWaitUntil::Done => Some(PageVmInitStage::Load),
- FetchWaitUntil::NetworkIdle | FetchWaitUntil::DomStable => None,
- }
-}
-
fn load_cookie_state(browser: &Browser, config: &AppConfig) -> Result<()> {
browser.import_cookies(load_cookie_state_cookies(config)?)?;
Ok(())
diff --git a/moli/src/app/http_error_navigation.rs b/moli/src/app/http_error_navigation.rs
index 41ce308d0..afcbff42a 100644
--- a/moli/src/app/http_error_navigation.rs
+++ b/moli/src/app/http_error_navigation.rs
@@ -5,7 +5,7 @@
use anyhow::Result;
use moli_core::runtime::{
- Browser, FetchedDocument, RenderedDomWaitUntil, RendererLifecycleDecision,
+ Browser, FetchDeadline, FetchedDocument, RenderedDomWaitUntil, RendererLifecycleDecision,
};
use moli_fetch::Request;
use std::time::Duration;
@@ -18,24 +18,29 @@ pub(super) async fn fetch_with_http_error_navigation(
browser: &Browser,
request: Request,
wait_until: RenderedDomWaitUntil,
- timeout: Duration,
+ deadline: FetchDeadline,
navigation_grace: Duration,
) -> Result<FetchedDocument> {
let navigation_grace_ms = navigation_grace.as_millis().min(u128::from(u64::MAX)) as u64;
// The first DCL/load is delivered to this synchronous decision normally.
// A 4xx/5xx may spend up to the configured grace waiting for a replacement
// navigation. That wait and the successor's matching lifecycle stage both
- // consume the original timeout budget; neither starts a fresh budget.
+ // consume the plan's absolute deadline; neither starts a fresh budget.
browser
- .fetch_document_with_lifecycle_decider(request, wait_until, timeout, move |target| {
- Ok(if is_http_error_status(target.status) {
- RendererLifecycleDecision::FollowNextDocument {
- navigation_grace_ms,
- }
- } else {
- RendererLifecycleDecision::Finish
- })
- })
+ .fetch_document_with_lifecycle_decider_and_deadline(
+ request,
+ wait_until,
+ deadline,
+ move |target| {
+ Ok(if is_http_error_status(target.status) {
+ RendererLifecycleDecision::FollowNextDocument {
+ navigation_grace_ms,
+ }
+ } else {
+ RendererLifecycleDecision::Finish
+ })
+ },
+ )
.await
}
diff --git a/moli/src/app/readiness.rs b/moli/src/app/readiness.rs
new file mode 100644
index 000000000..a1c11fe1b
--- /dev/null
+++ b/moli/src/app/readiness.rs
@@ -0,0 +1,205 @@
+//! CLI fetch-readiness policy and ordering.
+//!
+//! The renderer keeps ownership of the individual lifecycle, response,
+//! selector, and script wait state machines. This outer plan supplies all of
+//! them with one absolute deadline and preserves the CLI's established order:
+//! response first, then selector, then script. Completed response records are
+//! retained by the Page, so starting that wait after lifecycle completion does
+//! not lose an early matching response.
+
+use super::http_error_navigation::fetch_with_http_error_navigation;
+use crate::cli::{FetchArgs, FetchWaitUntil};
+use anyhow::{Context, Result, anyhow, bail};
+use moli_core::{
+ page::{Page, SubresourceResponseWaitCriteria},
+ runtime::{Browser, FetchDeadline, FetchedDocument, PageVmInitStage, RenderedDomWaitUntil},
+};
+use moli_fetch::Request;
+use std::time::Duration;
+
+#[derive(Debug)]
+pub(super) struct ReadinessPlan {
+ wait_until: RenderedDomWaitUntil,
+ deadline: FetchDeadline,
+ redirect_wait: Duration,
+ response: Option<SubresourceResponseWaitCriteria>,
+ selector: Option<String>,
+ script: Option<String>,
+}
+
+impl ReadinessPlan {
+ pub(super) fn from_fetch_args(
+ args: &FetchArgs,
+ response: Option<SubresourceResponseWaitCriteria>,
+ ) -> Result<Self> {
+ let script = resolve_wait_script(args)?;
+ Ok(Self {
+ wait_until: rendered_wait_until(args.wait_until),
+ deadline: FetchDeadline::new(Duration::from_millis(args.timeout))
+ .context("failed to create fetch readiness deadline")?,
+ redirect_wait: Duration::from_millis(args.redirect_wait_ms),
+ response,
+ selector: args.wait_selector.clone(),
+ script,
+ })
+ }
+
+ pub(super) fn lifecycle_stage(&self) -> Option<PageVmInitStage> {
+ // Only concrete Document lifecycle milestones opt into HTTP-error
+ // navigation recovery. `done` retains the historical load boundary;
+ // network-idle and DOM-stable retain best-effort HTTP-error dumps.
+ match self.wait_until {
+ RenderedDomWaitUntil::DomContentLoaded => Some(PageVmInitStage::DomContentLoaded),
+ RenderedDomWaitUntil::Load | RenderedDomWaitUntil::Done => Some(PageVmInitStage::Load),
+ RenderedDomWaitUntil::NetworkIdle | RenderedDomWaitUntil::DomStable => None,
+ }
+ }
+
+ pub(super) fn has_page_waits(&self) -> bool {
+ self.response.is_some() || self.selector.is_some() || self.script.is_some()
+ }
+
+ pub(super) async fn fetch_document(
+ &self,
+ browser: &Browser,
+ request: Request,
+ ) -> Result<FetchedDocument> {
+ match self.wait_until {
+ RenderedDomWaitUntil::DomContentLoaded
+ | RenderedDomWaitUntil::Load
+ | RenderedDomWaitUntil::Done => {
+ fetch_with_http_error_navigation(
+ browser,
+ request,
+ self.wait_until,
+ self.deadline,
+ self.redirect_wait,
+ )
+ .await
+ }
+ RenderedDomWaitUntil::NetworkIdle | RenderedDomWaitUntil::DomStable => {
+ browser
+ .fetch_request_document_allow_http_error_with_wait_until_deadline(
+ request,
+ self.wait_until,
+ self.deadline,
+ )
+ .await
+ }
+ }
+ }
+
+ pub(super) async fn wait_for_page(&self, browser: &Browser, page: &mut Page) -> Result<()> {
+ if let Some(response) = self.response.clone() {
+ browser
+ .wait_for_subresource_response_with_deadline(page, response, self.deadline)
+ .await
+ .context("failed while waiting for subresource response")?;
+ }
+
+ if let Some(selector) = self.selector.as_deref() {
+ browser
+ .wait_for_selector_with_deadline(page, selector, self.deadline)
+ .await
+ .with_context(|| anyhow!("failed while waiting for selector `{selector}`"))?;
+ }
+
+ if let Some(script) = self.script.as_deref() {
+ browser
+ .wait_for_script_truthy_with_deadline(page, script, self.deadline)
+ .await
+ .context("failed while waiting for script to become truthy")?;
+ }
+
+ Ok(())
+ }
+}
+
+fn rendered_wait_until(wait_until: FetchWaitUntil) -> RenderedDomWaitUntil {
+ match wait_until {
+ FetchWaitUntil::DomContentLoaded => RenderedDomWaitUntil::DomContentLoaded,
+ FetchWaitUntil::Load => RenderedDomWaitUntil::Load,
+ FetchWaitUntil::NetworkIdle => RenderedDomWaitUntil::NetworkIdle,
+ FetchWaitUntil::DomStable => RenderedDomWaitUntil::DomStable,
+ FetchWaitUntil::Done => RenderedDomWaitUntil::Done,
+ }
+}
+
+fn resolve_wait_script(args: &FetchArgs) -> Result<Option<String>> {
+ match (
+ args.wait_script.as_deref(),
+ args.wait_script_file.as_deref(),
+ ) {
+ (Some(_), Some(_)) => {
+ bail!("`--wait-script` and `--wait-script-file` are mutually exclusive")
+ }
+ (Some(script), None) => Ok(Some(script.to_owned())),
+ (None, Some(path)) => std::fs::read_to_string(path)
+ .map(Some)
+ .with_context(|| anyhow!("failed to read wait script file `{path}`")),
+ (None, None) => Ok(None),
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::{ReadinessPlan, resolve_wait_script};
+ use crate::cli::{Cli, Commands};
+ use anyhow::Result;
+ use clap::Parser;
+
+ fn fetch_args(extra: &[&str]) -> Box<crate::cli::FetchArgs> {
+ let mut raw = vec!["moli", "fetch"];
+ raw.extend_from_slice(extra);
+ raw.push("https://example.test/");
+ match Cli::try_parse_from(raw)
+ .expect("fetch arguments should parse")
+ .command
+ {
+ Commands::Fetch(args) => args,
+ command => panic!("expected fetch command, got {command:?}"),
+ }
+ }
+
+ #[test]
+ fn plan_collects_every_post_lifecycle_wait() -> Result<()> {
+ let args = fetch_args(&[
+ "--wait-selector",
+ "#ready",
+ "--wait-script",
+ "globalThis.ready",
+ ]);
+ let plan = ReadinessPlan::from_fetch_args(&args, Some(Default::default()))?;
+
+ assert!(plan.has_page_waits());
+ assert_eq!(plan.selector.as_deref(), Some("#ready"));
+ assert_eq!(plan.script.as_deref(), Some("globalThis.ready"));
+ assert!(plan.response.is_some());
+ Ok(())
+ }
+
+ #[test]
+ fn plan_without_response_selector_or_script_has_no_page_waits() -> Result<()> {
+ let args = fetch_args(&[]);
+ let plan = ReadinessPlan::from_fetch_args(&args, None)?;
+
+ assert!(!plan.has_page_waits());
+ Ok(())
+ }
+
+ #[test]
+ fn wait_script_sources_are_mutually_exclusive_before_fetch() {
+ let args = fetch_args(&[
+ "--wait-script",
+ "true",
+ "--wait-script-file",
+ "/does/not/matter.js",
+ ]);
+ let error = resolve_wait_script(&args).unwrap_err();
+
+ assert_eq!(
+ error.to_string(),
+ "`--wait-script` and `--wait-script-file` are mutually exclusive"
+ );
+ }
+}
diff --git a/moli/src/cli.rs b/moli/src/cli.rs
index 170d9e13c..5c228f1c5 100644
--- a/moli/src/cli.rs
+++ b/moli/src/cli.rs
@@ -135,9 +135,10 @@ pub struct FetchArgs {
#[arg(long, value_parser = parse_response_json_path_arg)]
pub wait_response_json: Option<ResponseJsonPathArg>,
- /// Maximum wait time in milliseconds. Initial and HTTP-error replacement
- /// navigations share one lifecycle deadline. Network-idle and DOM-stable
- /// fetches return the current page with a warning when it expires.
+ /// Maximum total readiness time in milliseconds. Initial and HTTP-error
+ /// replacement navigations, the selected lifecycle stage, response match,
+ /// selector, and script waits share one absolute deadline. Network-idle and
+ /// DOM-stable return the current page with a warning when it expires.
#[arg(long, alias = "wait-ms", default_value_t = 25_000)]
pub timeout: u64,
@@ -596,6 +597,8 @@ mod tests {
assert!(help.contains("--redirect-wait-ms <MILLISECONDS>"));
assert!(!help.contains("--redirect-time"));
+ assert!(help.contains("Maximum total readiness time in milliseconds"));
+ assert!(help.contains("response match, selector, and script waits share one absolute"));
}
#[test]
diff --git a/moli/tests/fetch_cli.rs b/moli/tests/fetch_cli.rs
index c56c5fc1e..517c67672 100644
--- a/moli/tests/fetch_cli.rs
+++ b/moli/tests/fetch_cli.rs
@@ -1700,6 +1700,135 @@ fn cli_wait_response_matches_url_body_and_json_without_networkidle() -> Result<(
Ok(())
}
+#[test]
+fn cli_readiness_plan_combines_early_response_selector_and_script() -> Result<()> {
+ let runtime = tokio::runtime::Runtime::new()?;
+ let server = runtime.block_on(FixtureServer::spawn())?;
+ let url = server.url("/wait-until-readiness-plan");
+ let output = run_fetch_cli_with_dump_and_args(
+ &url,
+ "html",
+ &[
+ "--timeout",
+ "1600",
+ "--wait-response-url",
+ "/wait-until-json-data",
+ "--wait-response-body",
+ "SUCCESS",
+ "--wait-response-json",
+ "data.url=/item/42",
+ "--wait-selector",
+ "#readiness-selector",
+ "--wait-script",
+ "globalThis.readinessScriptReady === true",
+ ],
+ )?;
+ runtime.block_on(server.shutdown());
+
+ assert!(
+ output.status.success(),
+ "combined readiness plan failed: stdout={}\nstderr={}",
+ String::from_utf8_lossy(&output.stdout),
+ String::from_utf8_lossy(&output.stderr)
+ );
+ let stdout = clean_output(&output.stdout);
+ assert!(
+ stdout.contains("data-readiness-response=\"SUCCESS\""),
+ "the response completed while the parser-blocking script delayed Load and must remain matchable: {stdout}"
+ );
+ assert!(
+ stdout.contains("id=\"readiness-selector\""),
+ "stdout={stdout}"
+ );
+ assert!(
+ stdout.contains("data-readiness-script=\"true\""),
+ "stdout={stdout}"
+ );
+ assert!(
+ stdout.contains("data-readiness-order=\"response,selector,script\""),
+ "stdout={stdout}"
+ );
+ Ok(())
+}
+
+#[test]
+fn cli_readiness_plan_does_not_restart_timeout_for_script() -> Result<()> {
+ let runtime = tokio::runtime::Runtime::new()?;
+ let server = runtime.block_on(FixtureServer::spawn())?;
+ let url = server.url("/wait-until-readiness-plan");
+ let output = run_fetch_cli_with_dump_and_args(
+ &url,
+ "html",
+ &[
+ "--timeout",
+ "700",
+ "--wait-response-url",
+ "/wait-until-json-data",
+ "--wait-selector",
+ "#readiness-selector",
+ "--wait-script",
+ "globalThis.readinessScriptReady === true",
+ ],
+ )?;
+ runtime.block_on(server.shutdown());
+
+ assert!(
+ !output.status.success(),
+ "a fresh 700 ms script timeout would incorrectly reach the 500 ms post-Load flag: stdout={} stderr={}",
+ String::from_utf8_lossy(&output.stdout),
+ String::from_utf8_lossy(&output.stderr)
+ );
+ let stdout = clean_output(&output.stdout);
+ let stderr = clean_output(&output.stderr);
+ assert!(stdout.is_empty(), "stdout={stdout}");
+ assert!(
+ stderr.contains("failed while waiting for script to become truthy"),
+ "the shared deadline should expire during the final plan phase: stderr={stderr}"
+ );
+ Ok(())
+}
+
+#[test]
+fn cli_readiness_timeout_identifies_response_selector_and_script_phases() -> Result<()> {
+ let runtime = tokio::runtime::Runtime::new()?;
+ let server = runtime.block_on(FixtureServer::spawn())?;
+ let url = server.url("/static");
+ let cases: [(&[&str], &str); 3] = [
+ (
+ &["--wait-response-url", "/response-that-never-arrives"],
+ "failed while waiting for subresource response",
+ ),
+ (
+ &["--wait-selector", "#selector-that-never-appears"],
+ "failed while waiting for selector `#selector-that-never-appears`",
+ ),
+ (
+ &["--wait-script", "globalThis.scriptThatNeverBecomesTruthy"],
+ "failed while waiting for script to become truthy",
+ ),
+ ];
+
+ for (args, expected_phase) in cases {
+ let mut args = args.to_vec();
+ args.extend(["--timeout", "350"]);
+ let output = run_fetch_cli_with_dump_and_args(&url, "html", &args)?;
+ let stdout = clean_output(&output.stdout);
+ let stderr = clean_output(&output.stderr);
+ assert!(
+ !output.status.success(),
+ "phase={expected_phase} stdout={stdout}"
+ );
+ assert!(stdout.is_empty(), "phase={expected_phase} stdout={stdout}");
+ assert!(
+ stderr.contains(expected_phase),
+ "expected phase `{expected_phase}` in stderr={stderr}"
+ );
+ }
+
+ runtime.block_on(server.shutdown());
+ Ok(())
+}
+
#[test]
fn cli_trace_network_can_include_matched_response_body_text() -> Result<()> {
let runtime = tokio::runtime::Runtime::new()?;
@@ -2258,6 +2387,93 @@ fn cli_redirect_wait_can_extend_http_error_navigation_grace() -> Result<()> {
Ok(())
}
+#[test]
+fn cli_readiness_plan_continues_after_http_error_replacement_navigation() -> Result<()> {
+ let runtime = tokio::runtime::Runtime::new()?;
+ let server = runtime.block_on(FixtureServer::spawn())?;
+ let url = server.url("/wait-until-readiness-http-error-navigation");
+ let output = run_fetch_cli_with_dump_and_args(
+ &url,
+ "html",
+ &[
+ "--timeout",
+ "1800",
+ "--wait-response-url",
+ "/wait-until-json-data",
+ "--wait-response-json",
+ "data.url=/item/42",
+ "--wait-selector",
+ "#readiness-navigation-selector",
+ "--wait-script",
+ "globalThis.readinessNavigationScriptReady === true",
+ ],
+ )?;
+ runtime.block_on(server.shutdown());
+
+ assert!(
+ output.status.success(),
+ "replacement readiness plan failed: stdout={}\nstderr={}",
+ String::from_utf8_lossy(&output.stdout),
+ String::from_utf8_lossy(&output.stderr)
+ );
+ let stdout = clean_output(&output.stdout);
+ assert!(
+ stdout.contains("id=\"readiness-navigation-target\""),
+ "stdout={stdout}"
+ );
+ assert!(
+ stdout.contains("data-readiness-navigation-response=\"SUCCESS\""),
+ "stdout={stdout}"
+ );
+ assert!(
+ stdout.contains("id=\"readiness-navigation-selector\""),
+ "stdout={stdout}"
+ );
+ assert!(
+ stdout.contains("data-readiness-navigation-script=\"true\""),
+ "stdout={stdout}"
+ );
+ assert!(!stdout.contains("id=\"readiness-challenge\""));
+ Ok(())
+}
+
+#[test]
+fn cli_http_error_replacement_and_post_waits_share_one_deadline() -> Result<()> {
+ let runtime = tokio::runtime::Runtime::new()?;
+ let server = runtime.block_on(FixtureServer::spawn())?;
+ let url = server.url("/wait-until-readiness-http-error-navigation");
+ let output = run_fetch_cli_with_dump_and_args(
+ &url,
+ "html",
+ &[
+ "--timeout",
+ "850",
+ "--wait-response-url",
+ "/wait-until-json-data",
+ "--wait-selector",
+ "#readiness-navigation-selector",
+ "--wait-script",
+ "globalThis.readinessNavigationScriptReady === true",
+ ],
+ )?;
+ runtime.block_on(server.shutdown());
+
+ assert!(
+ !output.status.success(),
+ "a fresh post-navigation script timeout would incorrectly succeed: stdout={} stderr={}",
+ String::from_utf8_lossy(&output.stdout),
+ String::from_utf8_lossy(&output.stderr)
+ );
+ let stdout = clean_output(&output.stdout);
+ let stderr = clean_output(&output.stderr);
+ assert!(stdout.is_empty(), "stdout={stdout}");
+ assert!(
+ stderr.contains("failed while waiting for script to become truthy"),
+ "the replacement and earlier post waits should leave the script phase with only the remaining budget: stderr={stderr}"
+ );
+ Ok(())
+}
+
#[test]
fn cli_zero_redirect_wait_accepts_navigation_already_pending_at_stage() -> Result<()> {
let runtime = tokio::runtime::Runtime::new()?;