use std::collections::HashSet;
use std::fmt;
use std::future::{poll_fn, Future};
use std::pin::pin;
use std::task::Poll;
use std::time::{Duration, Instant};
use serde_json::{json, Value};
use crate::core::Error;
pub const WAIT_FOR_SETTLE_METHOD: &str = "Chromeleon.waitForSettle";
pub const PAGE_SETTLE_SWITCH: &str = "--page-settle";
pub const PAGE_SETTLE_QUIET_WINDOW_SWITCH: &str = "--page-settle-quiet-window-ms";
pub const PAGE_SETTLE_MIN_CHARS_SWITCH: &str = "--page-settle-min-chars";
pub const PAGE_SETTLE_TIMEOUT_SWITCH: &str = "--page-settle-timeout-ms";
pub const PAGE_SETTLE_SAMPLE_INTERVAL_SWITCH: &str = "--page-settle-sample-interval-ms";
pub const PAGE_SETTLE_PIERCE_SHADOW_SWITCH: &str = "--page-settle-pierce-shadow";
pub const PAGE_ENABLE_METHOD: &str = "Page.enable";
pub const GET_FRAME_TREE_METHOD: &str = "Page.getFrameTree";
pub const SET_LIFECYCLE_EVENTS_ENABLED_METHOD: &str = "Page.setLifecycleEventsEnabled";
pub const LIFECYCLE_EVENT: &str = "Page.lifecycleEvent";
pub const NETWORK_ALMOST_IDLE: &str = "networkAlmostIdle";
pub const NETWORK_IDLE: &str = "networkIdle";
pub const LOAD_LIFECYCLE: &str = "load";
pub const METHOD_NOT_FOUND: i64 = -32601;
pub const BLOCKED_STATUSES: &[u16] = &[401, 403, 407, 429, 503];
pub const REPLAY_WINDOW: Duration = Duration::from_millis(350);
pub const DEFAULT_QUIET_WINDOW_MS: u32 = 4000;
pub const DEFAULT_TIMEOUT_MS: u32 = 30_000;
const COMMAND_TIMEOUT_GRACE_MS: u32 = 250;
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
#[non_exhaustive]
pub struct SettleTuning {
pub quiet_window_ms: Option<u32>,
pub min_chars: Option<u32>,
pub timeout_ms: Option<u32>,
pub sample_interval_ms: Option<u32>,
pub pierce_shadow: bool,
}
impl SettleTuning {
pub fn new() -> Self {
SettleTuning::default()
}
pub fn quiet_window_ms(mut self, ms: u32) -> Self {
self.quiet_window_ms = Some(ms);
self
}
pub fn min_chars(mut self, chars: u32) -> Self {
self.min_chars = Some(chars);
self
}
pub fn timeout_ms(mut self, ms: u32) -> Self {
self.timeout_ms = Some(ms);
self
}
pub fn sample_interval_ms(mut self, ms: u32) -> Self {
self.sample_interval_ms = Some(ms);
self
}
pub fn pierce_shadow(mut self, on: bool) -> Self {
self.pierce_shadow = on;
self
}
}
pub fn settle_launch_args(tuning: SettleTuning) -> Vec<String> {
let mut args = vec![PAGE_SETTLE_SWITCH.to_string()];
if let Some(ms) = tuning.quiet_window_ms {
args.push(format!("{PAGE_SETTLE_QUIET_WINDOW_SWITCH}={ms}"));
}
if let Some(chars) = tuning.min_chars {
args.push(format!("{PAGE_SETTLE_MIN_CHARS_SWITCH}={chars}"));
}
if let Some(ms) = tuning.timeout_ms {
args.push(format!("{PAGE_SETTLE_TIMEOUT_SWITCH}={ms}"));
}
if let Some(ms) = tuning.sample_interval_ms {
args.push(format!("{PAGE_SETTLE_SAMPLE_INTERVAL_SWITCH}={ms}"));
}
if tuning.pierce_shadow {
args.push(PAGE_SETTLE_PIERCE_SHADOW_SWITCH.to_string());
}
args
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum SettleOutcome {
Settled,
Timeout,
Challenge,
Other(String),
}
impl SettleOutcome {
pub fn from_wire(outcome: &str) -> Self {
match outcome {
"settled" => SettleOutcome::Settled,
"timeout" => SettleOutcome::Timeout,
"challenge" => SettleOutcome::Challenge,
other => SettleOutcome::Other(other.to_string()),
}
}
pub fn as_str(&self) -> &str {
match self {
SettleOutcome::Settled => "settled",
SettleOutcome::Timeout => "timeout",
SettleOutcome::Challenge => "challenge",
SettleOutcome::Other(other) => other,
}
}
}
impl fmt::Display for SettleOutcome {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum SettleVia {
WaitForSettle,
NetworkAlmostIdle,
Load,
Timeout,
}
impl SettleVia {
pub fn as_str(&self) -> &'static str {
match self {
SettleVia::WaitForSettle => "waitForSettle",
SettleVia::NetworkAlmostIdle => "networkAlmostIdle",
SettleVia::Load => "load",
SettleVia::Timeout => "timeout",
}
}
}
impl fmt::Display for SettleVia {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub struct SettleState {
pub outcome: SettleOutcome,
pub elapsed_ms: f64,
pub text_length: Option<u64>,
pub navigations: u32,
pub http_status: Option<u16>,
pub reason: String,
pub via: SettleVia,
}
impl SettleState {
pub fn blocked(&self) -> bool {
self.outcome == SettleOutcome::Challenge
|| self
.http_status
.is_some_and(|status| BLOCKED_STATUSES.contains(&status))
}
pub fn is_settled(&self) -> bool {
self.outcome == SettleOutcome::Settled && !self.blocked()
}
pub fn from_reply(reply: &Value) -> Option<SettleState> {
let body = settle_body(reply);
let outcome = body.get("outcome")?.as_str()?;
Some(SettleState {
outcome: SettleOutcome::from_wire(outcome),
elapsed_ms: body.get("elapsedMs").and_then(Value::as_f64).unwrap_or(0.0),
text_length: body.get("textLength").and_then(Value::as_u64),
navigations: body
.get("navigations")
.and_then(Value::as_u64)
.and_then(|n| u32::try_from(n).ok())
.unwrap_or(0),
http_status: body
.get("httpStatus")
.and_then(Value::as_u64)
.and_then(|s| u16::try_from(s).ok()),
reason: body
.get("reason")
.and_then(Value::as_str)
.unwrap_or_default()
.to_string(),
via: SettleVia::WaitForSettle,
})
}
}
fn settle_body(reply: &Value) -> &Value {
if reply.get("outcome").is_some() {
return reply;
}
match reply.get("result") {
Some(result) if result.get("outcome").is_some() => result,
_ => reply,
}
}
pub fn is_method_not_found(reply: &Value) -> bool {
let Some(error) = reply.get("error") else {
return false;
};
if error.get("code").and_then(Value::as_i64) == Some(METHOD_NOT_FOUND) {
return true;
}
error
.get("message")
.and_then(Value::as_str)
.is_some_and(error_reads_as_unavailable)
}
pub fn error_reads_as_unavailable(text: &str) -> bool {
if text.contains(&METHOD_NOT_FOUND.to_string()) {
return true;
}
let lower = text.to_ascii_lowercase();
lower.contains("wasn't found")
|| lower.contains("was not found")
|| lower.contains("method not found")
|| lower.contains("not implemented")
|| lower.contains("unknown method")
}
fn error_detail(reply: &Value) -> Option<String> {
match reply.get("error") {
Some(error) if !error.is_null() => Some(error.to_string()),
_ => None,
}
}
pub fn main_frame_id(frame_tree_reply: &Value) -> Option<&str> {
let tree = frame_tree_reply
.get("frameTree")
.or_else(|| frame_tree_reply.get("result")?.get("frameTree"))?;
tree.get("frame")?.get("id")?.as_str()
}
#[derive(Debug, Clone)]
pub struct LifecycleTracker {
main_frame_id: Option<String>,
stale_loaders: HashSet<String>,
live_loaders: HashSet<String>,
arming: bool,
started_at: Option<f64>,
saw_almost_idle: bool,
almost_idle_at: Option<f64>,
saw_load: bool,
load_at: Option<f64>,
}
impl Default for LifecycleTracker {
fn default() -> Self {
LifecycleTracker::new()
}
}
impl LifecycleTracker {
pub fn new() -> Self {
LifecycleTracker {
main_frame_id: None,
stale_loaders: HashSet::new(),
live_loaders: HashSet::new(),
arming: true,
started_at: None,
saw_almost_idle: false,
almost_idle_at: None,
saw_load: false,
load_at: None,
}
}
pub fn set_main_frame_id(&mut self, frame_id: impl Into<String>) {
self.main_frame_id = Some(frame_id.into());
}
pub fn main_frame_id(&self) -> Option<&str> {
self.main_frame_id.as_deref()
}
pub fn arming(&self) -> bool {
self.arming
}
pub fn finish_arming(&mut self) {
self.arming = false;
}
pub fn observe(&mut self, method: &str, params: &Value) {
if method != LIFECYCLE_EVENT {
return;
}
let loader_id = params
.get("loaderId")
.and_then(Value::as_str)
.unwrap_or_default();
if self.arming {
self.stale_loaders.insert(loader_id.to_string());
return;
}
let frame_id = params
.get("frameId")
.and_then(Value::as_str)
.unwrap_or_default();
match self.main_frame_id.as_deref() {
Some(main) if main == frame_id => {}
_ => return,
}
if self.stale_loaders.contains(loader_id) {
return;
}
let timestamp = params.get("timestamp").and_then(Value::as_f64);
if self.live_loaders.insert(loader_id.to_string()) && self.started_at.is_none() {
self.started_at = timestamp;
}
match params
.get("name")
.and_then(Value::as_str)
.unwrap_or_default()
{
NETWORK_ALMOST_IDLE if !self.saw_almost_idle => {
self.saw_almost_idle = true;
self.almost_idle_at = timestamp;
}
LOAD_LIFECYCLE if !self.saw_load => {
self.saw_load = true;
self.load_at = timestamp;
}
_ => {}
}
}
pub fn stale_loader_count(&self) -> usize {
self.stale_loaders.len()
}
pub fn navigations(&self) -> u32 {
u32::try_from(self.live_loaders.len()).unwrap_or(u32::MAX)
}
pub fn network_almost_idle(&self) -> bool {
self.saw_almost_idle
}
pub fn loaded(&self) -> bool {
self.saw_load
}
pub fn signal(&self) -> Option<(SettleVia, Option<f64>)> {
if self.saw_almost_idle {
Some((SettleVia::NetworkAlmostIdle, self.almost_idle_at))
} else if self.saw_load {
Some((SettleVia::Load, self.load_at))
} else {
None
}
}
pub fn elapsed_ms(&self, timestamp: Option<f64>) -> Option<f64> {
Some((timestamp? - self.started_at?) * 1000.0)
}
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
#[non_exhaustive]
pub struct WaitOptions {
pub quiet_window_ms: Option<u32>,
pub min_chars: Option<u32>,
pub timeout_ms: Option<u32>,
pub prefer_lifecycle: bool,
}
impl WaitOptions {
pub fn new() -> Self {
WaitOptions::default()
}
pub fn quiet_window_ms(mut self, ms: u32) -> Self {
self.quiet_window_ms = Some(ms);
self
}
pub fn min_chars(mut self, chars: u32) -> Self {
self.min_chars = Some(chars);
self
}
pub fn timeout_ms(mut self, ms: u32) -> Self {
self.timeout_ms = Some(ms);
self
}
pub fn prefer_lifecycle(mut self, on: bool) -> Self {
self.prefer_lifecycle = on;
self
}
pub fn command_params(&self, timeout_ms: Option<u32>) -> Value {
let mut params = json!({});
if let Some(quiet) = self.quiet_window_ms {
params["quietWindowMs"] = json!(quiet);
}
if let Some(chars) = self.min_chars {
params["minChars"] = json!(chars);
}
if let Some(ms) = timeout_ms {
params["timeoutMs"] = json!(ms);
}
params
}
}
fn command_timeout_ms(timeout_ms: u32) -> u32 {
if timeout_ms > 2 * COMMAND_TIMEOUT_GRACE_MS {
timeout_ms - COMMAND_TIMEOUT_GRACE_MS
} else {
timeout_ms
}
}
fn remaining(deadline: Instant) -> Option<Duration> {
deadline
.checked_duration_since(Instant::now())
.filter(|d| !d.is_zero())
}
fn fallback_state(tracker: &LifecycleTracker, armed_at: Instant, reason: String) -> SettleState {
let (via, outcome, at) = match tracker.signal() {
Some((via, at)) => (via, SettleOutcome::Settled, at),
None => (SettleVia::Timeout, SettleOutcome::Timeout, None),
};
SettleState {
outcome,
elapsed_ms: tracker
.elapsed_ms(at)
.unwrap_or_else(|| armed_at.elapsed().as_secs_f64() * 1000.0),
text_length: None,
navigations: tracker.navigations(),
http_status: None,
reason,
via,
}
}
pub trait PageSession {
type Error: From<Error> + fmt::Display;
fn send(
&self,
method: &'static str,
params: Value,
) -> impl Future<Output = Result<Value, Self::Error>>;
fn next_event(&self, timeout: Duration) -> impl Future<Output = Option<(String, Value)>>;
fn method_unavailable(&self, err: &Self::Error) -> bool {
error_reads_as_unavailable(&err.to_string())
}
fn detach(&self) -> impl Future<Output = ()> {
async {}
}
}
impl<S: PageSession + ?Sized> PageSession for &S {
type Error = S::Error;
fn send(
&self,
method: &'static str,
params: Value,
) -> impl Future<Output = Result<Value, Self::Error>> {
(**self).send(method, params)
}
fn next_event(&self, timeout: Duration) -> impl Future<Output = Option<(String, Value)>> {
(**self).next_event(timeout)
}
fn method_unavailable(&self, err: &Self::Error) -> bool {
(**self).method_unavailable(err)
}
fn detach(&self) -> impl Future<Output = ()> {
(**self).detach()
}
}
#[derive(Debug)]
pub struct SettleWatch<S: PageSession> {
session: S,
tracker: LifecycleTracker,
armed_at: Instant,
result: Option<SettleState>,
closed: bool,
}
impl<S: PageSession> SettleWatch<S> {
pub async fn arm(session: S) -> Result<Self, S::Error> {
SettleWatch::arm_after(session, REPLAY_WINDOW).await
}
pub async fn arm_after(session: S, replay_window: Duration) -> Result<Self, S::Error> {
match SettleWatch::prepare(&session, replay_window).await {
Ok(tracker) => Ok(SettleWatch {
session,
tracker,
armed_at: Instant::now(),
result: None,
closed: false,
}),
Err(e) => {
session.detach().await;
Err(e)
}
}
}
async fn prepare(session: &S, replay_window: Duration) -> Result<LifecycleTracker, S::Error> {
session.send(PAGE_ENABLE_METHOD, json!({})).await?;
let frame_tree = session.send(GET_FRAME_TREE_METHOD, json!({})).await?;
let mut tracker = LifecycleTracker::new();
match main_frame_id(&frame_tree) {
Some(id) => tracker.set_main_frame_id(id),
None => {
return Err(S::Error::from(Error::MissingMainFrame {
detail: format!("{GET_FRAME_TREE_METHOD} named no main frame: {frame_tree}"),
}))
}
}
session
.send(
SET_LIFECYCLE_EVENTS_ENABLED_METHOD,
json!({"enabled": true}),
)
.await?;
let deadline = Instant::now() + replay_window;
while let Some(left) = remaining(deadline) {
match session.next_event(left).await {
Some((method, params)) => tracker.observe(&method, ¶ms),
None => break,
}
}
tracker.finish_arming();
Ok(tracker)
}
pub fn main_frame_id(&self) -> Option<&str> {
self.tracker.main_frame_id()
}
pub fn tracker(&self) -> &LifecycleTracker {
&self.tracker
}
pub fn armed_at(&self) -> Instant {
self.armed_at
}
pub fn session(&self) -> &S {
&self.session
}
pub fn into_session(self) -> S {
self.session
}
pub async fn wait(&mut self, options: WaitOptions) -> Result<SettleState, S::Error> {
if let Some(done) = &self.result {
return Ok(done.clone());
}
let armed_at = self.armed_at;
let timeout_ms = options.timeout_ms.unwrap_or(DEFAULT_TIMEOUT_MS);
let deadline = Instant::now() + Duration::from_millis(u64::from(timeout_ms));
let session = &self.session;
let tracker = &mut self.tracker;
let mut reason = String::new();
let mut answer = None;
if !options.prefer_lifecycle {
let command = async {
Answer::Reply(
session
.send(
WAIT_FOR_SETTLE_METHOD,
options.command_params(Some(command_timeout_ms(timeout_ms))),
)
.await,
)
};
let drain = async {
drain_until(session, tracker, deadline, false).await;
Answer::Deadline
};
match race(command, drain).await {
Answer::Reply(Ok(reply)) => match SettleState::from_reply(&reply) {
Some(state) => answer = Some(state),
None if is_method_not_found(&reply) => {
reason = unavailable_reason(&reply.to_string());
}
None => {
reason = match error_detail(&reply) {
Some(detail) => format!(
"{WAIT_FOR_SETTLE_METHOD} was refused ({detail}); \
fell back to the lifecycle feed"
),
None => format!(
"{WAIT_FOR_SETTLE_METHOD} answered without an outcome ({reply}); \
fell back to the lifecycle feed"
),
};
}
},
Answer::Reply(Err(e)) => {
if !session.method_unavailable(&e) {
return Err(e);
}
reason = unavailable_reason(&e.to_string());
}
Answer::Deadline => {
reason =
format!("{WAIT_FOR_SETTLE_METHOD} did not answer within {timeout_ms}ms");
}
}
} else {
reason = format!("{NETWORK_ALMOST_IDLE} was asked for instead of the command");
}
let state = match answer {
Some(state) => state,
None => {
drain_until(session, tracker, deadline, true).await;
fallback_state(tracker, armed_at, reason)
}
};
self.result = Some(state.clone());
Ok(state)
}
pub async fn close(&mut self) {
if self.closed {
return;
}
self.closed = true;
let _ = self
.session
.send(
SET_LIFECYCLE_EVENTS_ENABLED_METHOD,
json!({"enabled": false}),
)
.await;
self.session.detach().await;
}
}
fn unavailable_reason(detail: &str) -> String {
format!(
"this browser has no {WAIT_FOR_SETTLE_METHOD} — it was launched without \
{PAGE_SETTLE_SWITCH} ({detail}); fell back to the lifecycle feed"
)
}
enum Answer<E> {
Reply(Result<Value, E>),
Deadline,
}
async fn drain_until<S: PageSession>(
session: &S,
tracker: &mut LifecycleTracker,
deadline: Instant,
stop_on_idle: bool,
) {
while let Some(left) = remaining(deadline) {
if stop_on_idle && tracker.network_almost_idle() {
return;
}
match session.next_event(left).await {
Some((method, params)) => tracker.observe(&method, ¶ms),
None => return,
}
}
}
async fn race<T, A, B>(a: A, b: B) -> T
where
A: Future<Output = T>,
B: Future<Output = T>,
{
let mut a = pin!(a);
let mut b = pin!(b);
poll_fn(move |cx| {
if let Poll::Ready(value) = a.as_mut().poll(cx) {
return Poll::Ready(value);
}
b.as_mut().poll(cx)
})
.await
}
pub trait BlockingPageSession {
type Error: From<Error> + fmt::Display;
fn send(&mut self, method: &'static str, params: Value) -> Result<Value, Self::Error>;
fn next_event(&mut self, timeout: Duration) -> Option<(String, Value)>;
fn method_unavailable(&self, err: &Self::Error) -> bool {
error_reads_as_unavailable(&err.to_string())
}
fn detach(&mut self) {}
}
#[derive(Debug)]
pub struct BlockingSettleWatch<S: BlockingPageSession> {
session: S,
tracker: LifecycleTracker,
armed_at: Instant,
result: Option<SettleState>,
closed: bool,
}
impl<S: BlockingPageSession> BlockingSettleWatch<S> {
pub fn arm(session: S) -> Result<Self, S::Error> {
BlockingSettleWatch::arm_after(session, REPLAY_WINDOW)
}
pub fn arm_after(mut session: S, replay_window: Duration) -> Result<Self, S::Error> {
match BlockingSettleWatch::prepare(&mut session, replay_window) {
Ok(tracker) => Ok(BlockingSettleWatch {
session,
tracker,
armed_at: Instant::now(),
result: None,
closed: false,
}),
Err(e) => {
session.detach();
Err(e)
}
}
}
fn prepare(session: &mut S, replay_window: Duration) -> Result<LifecycleTracker, S::Error> {
session.send(PAGE_ENABLE_METHOD, json!({}))?;
let frame_tree = session.send(GET_FRAME_TREE_METHOD, json!({}))?;
let mut tracker = LifecycleTracker::new();
match main_frame_id(&frame_tree) {
Some(id) => tracker.set_main_frame_id(id),
None => {
return Err(S::Error::from(Error::MissingMainFrame {
detail: format!("{GET_FRAME_TREE_METHOD} named no main frame: {frame_tree}"),
}))
}
}
session.send(
SET_LIFECYCLE_EVENTS_ENABLED_METHOD,
json!({"enabled": true}),
)?;
let deadline = Instant::now() + replay_window;
while let Some(left) = remaining(deadline) {
match session.next_event(left) {
Some((method, params)) => tracker.observe(&method, ¶ms),
None => break,
}
}
tracker.finish_arming();
Ok(tracker)
}
pub fn main_frame_id(&self) -> Option<&str> {
self.tracker.main_frame_id()
}
pub fn tracker(&self) -> &LifecycleTracker {
&self.tracker
}
pub fn session(&self) -> &S {
&self.session
}
pub fn session_mut(&mut self) -> &mut S {
&mut self.session
}
pub fn armed_at(&self) -> Instant {
self.armed_at
}
pub fn wait(&mut self, options: WaitOptions) -> Result<SettleState, S::Error> {
if let Some(done) = &self.result {
return Ok(done.clone());
}
let timeout_ms = options.timeout_ms.unwrap_or(DEFAULT_TIMEOUT_MS);
let deadline = Instant::now() + Duration::from_millis(u64::from(timeout_ms));
let mut reason = String::new();
let mut answer = None;
if options.prefer_lifecycle {
reason = format!("{NETWORK_ALMOST_IDLE} was asked for instead of the command");
} else {
match self.session.send(
WAIT_FOR_SETTLE_METHOD,
options.command_params(Some(command_timeout_ms(timeout_ms))),
) {
Ok(reply) => match SettleState::from_reply(&reply) {
Some(state) => answer = Some(state),
None if is_method_not_found(&reply) => {
reason = unavailable_reason(&reply.to_string());
}
None => {
reason = match error_detail(&reply) {
Some(detail) => format!(
"{WAIT_FOR_SETTLE_METHOD} was refused ({detail}); \
fell back to the lifecycle feed"
),
None => format!(
"{WAIT_FOR_SETTLE_METHOD} answered without an outcome ({reply}); \
fell back to the lifecycle feed"
),
};
}
},
Err(e) => {
if !self.session.method_unavailable(&e) {
return Err(e);
}
reason = unavailable_reason(&e.to_string());
}
}
}
let state = match answer {
Some(state) => state,
None => {
while let Some(left) = remaining(deadline) {
if self.tracker.network_almost_idle() {
break;
}
match self.session.next_event(left) {
Some((method, params)) => self.tracker.observe(&method, ¶ms),
None => break,
}
}
fallback_state(&self.tracker, self.armed_at, reason)
}
};
self.result = Some(state.clone());
Ok(state)
}
pub fn close(&mut self) {
if self.closed {
return;
}
self.closed = true;
let _ = self.session.send(
SET_LIFECYCLE_EVENTS_ENABLED_METHOD,
json!({"enabled": false}),
);
self.session.detach();
}
}
impl<S: BlockingPageSession> Drop for BlockingSettleWatch<S> {
fn drop(&mut self) {
if !self.closed {
self.closed = true;
self.session.detach();
}
}
}
#[cfg(test)]
mod tests {
use super::*;
fn lifecycle(frame: &str, loader: &str, name: &str, timestamp: f64) -> Value {
json!({"frameId": frame, "loaderId": loader, "name": name, "timestamp": timestamp})
}
fn armed(main_frame: &str) -> LifecycleTracker {
let mut tracker = LifecycleTracker::new();
tracker.set_main_frame_id(main_frame);
tracker.finish_arming();
tracker
}
#[test]
fn launch_args_stay_minimal_without_tuning() {
assert_eq!(settle_launch_args(SettleTuning::new()), ["--page-settle"]);
assert_eq!(
settle_launch_args(
SettleTuning::new()
.quiet_window_ms(1500)
.min_chars(64)
.timeout_ms(20_000)
.sample_interval_ms(100)
.pierce_shadow(true)
),
[
"--page-settle",
"--page-settle-quiet-window-ms=1500",
"--page-settle-min-chars=64",
"--page-settle-timeout-ms=20000",
"--page-settle-sample-interval-ms=100",
"--page-settle-pierce-shadow",
]
);
}
#[test]
fn a_reply_is_read_back_field_for_field() {
let reply = json!({
"outcome": "settled",
"elapsedMs": 7306.0,
"textLength": 48213,
"navigations": 2,
"httpStatus": 200,
"reason": "quiet",
"somethingNewerBinariesSend": true,
});
let state = SettleState::from_reply(&reply).expect("a settled reply");
assert_eq!(state.outcome, SettleOutcome::Settled);
assert_eq!(state.elapsed_ms, 7306.0);
assert_eq!(state.text_length, Some(48213));
assert_eq!(state.navigations, 2);
assert_eq!(state.http_status, Some(200));
assert_eq!(state.reason, "quiet");
assert_eq!(state.via, SettleVia::WaitForSettle);
assert!(state.is_settled());
assert!(!state.blocked());
assert_eq!(
SettleState::from_reply(&json!({"id": 7, "result": reply})),
Some(state)
);
}
#[test]
fn an_outcome_the_client_predates_is_kept_not_dropped() {
let state = SettleState::from_reply(&json!({"outcome": "quiesced"})).expect("parsed");
assert_eq!(state.outcome, SettleOutcome::Other("quiesced".into()));
assert_eq!(state.outcome.as_str(), "quiesced");
assert_eq!(state.text_length, None);
assert_eq!(state.http_status, None);
assert_eq!(state.navigations, 0);
}
#[test]
fn a_reply_with_no_outcome_is_not_a_state() {
assert!(SettleState::from_reply(&json!({})).is_none());
assert!(SettleState::from_reply(&json!({"error": {"code": -32601}})).is_none());
}
#[test]
fn a_wall_is_blocked_whichever_way_it_arrives() {
let challenge =
SettleState::from_reply(&json!({"outcome": "challenge", "httpStatus": 200})).unwrap();
assert!(challenge.blocked());
assert!(!challenge.is_settled(), "a wall is never a successful load");
for status in BLOCKED_STATUSES {
let short = SettleState::from_reply(
&json!({"outcome": "timeout", "httpStatus": status, "elapsedMs": 30000.0}),
)
.unwrap();
assert!(short.blocked(), "status {status} read as clean");
}
let clean =
SettleState::from_reply(&json!({"outcome": "settled", "httpStatus": 200})).unwrap();
assert!(!clean.blocked());
let slow = SettleState::from_reply(&json!({"outcome": "timeout"})).unwrap();
assert!(!slow.blocked());
}
#[test]
fn method_not_found_is_recognised_by_code_and_by_message() {
assert!(is_method_not_found(
&json!({"error": {"code": -32601, "message": "'Chromeleon.waitForSettle' wasn't found"}})
));
assert!(is_method_not_found(
&json!({"error": {"message": "'Chromeleon.waitForSettle' wasn't found"}})
));
assert!(!is_method_not_found(&json!({"outcome": "settled"})));
assert!(!is_method_not_found(
&json!({"error": {"code": -32602, "message": "Invalid parameters"}})
));
assert!(error_reads_as_unavailable(
"CDP error -32601: not implemented"
));
assert!(error_reads_as_unavailable(
"'Chromeleon.waitForSettle' wasn't found"
));
assert!(error_reads_as_unavailable("Unknown method"));
assert!(!error_reads_as_unavailable("websocket closed"));
}
#[test]
fn the_main_frame_id_survives_either_reply_shape() {
let tree = json!({"frameTree": {"frame": {"id": "MAIN"}, "childFrames": [
{"frameTree": {"frame": {"id": "AD"}}}
]}});
assert_eq!(main_frame_id(&tree), Some("MAIN"));
assert_eq!(main_frame_id(&json!({"result": tree})), Some("MAIN"));
assert_eq!(main_frame_id(&json!({"result": {}})), None);
}
#[test]
fn the_about_blank_replay_is_written_off() {
let mut tracker = LifecycleTracker::new();
tracker.set_main_frame_id("MAIN");
for name in ["init", "load", "networkAlmostIdle", "networkIdle"] {
tracker.observe(LIFECYCLE_EVENT, &lifecycle("MAIN", "BLANK", name, 100.0));
}
tracker.finish_arming();
assert!(!tracker.network_almost_idle(), "the replay was counted");
assert_eq!(tracker.stale_loader_count(), 1);
tracker.observe(
LIFECYCLE_EVENT,
&lifecycle("MAIN", "BLANK", NETWORK_ALMOST_IDLE, 100.5),
);
assert_eq!(tracker.signal(), None);
assert_eq!(tracker.navigations(), 0);
tracker.observe(LIFECYCLE_EVENT, &lifecycle("MAIN", "NAV-1", "init", 101.0));
tracker.observe(
LIFECYCLE_EVENT,
&lifecycle("MAIN", "NAV-1", NETWORK_ALMOST_IDLE, 107.094),
);
assert_eq!(
tracker.signal(),
Some((SettleVia::NetworkAlmostIdle, Some(107.094)))
);
assert_eq!(tracker.navigations(), 1);
}
#[test]
fn subframes_do_not_answer_for_the_main_frame() {
let mut tracker = armed("MAIN");
tracker.observe(LIFECYCLE_EVENT, &lifecycle("MAIN", "NAV-1", "init", 0.0));
for frame in 1..23 {
tracker.observe(
LIFECYCLE_EVENT,
&lifecycle(&format!("SUB-{frame}"), "NAV-1", NETWORK_ALMOST_IDLE, 0.699),
);
tracker.observe(
LIFECYCLE_EVENT,
&lifecycle(&format!("SUB-{frame}"), "NAV-1", LOAD_LIFECYCLE, 0.699),
);
}
assert_eq!(tracker.signal(), None, "an iframe answered for the page");
tracker.observe(
LIFECYCLE_EVENT,
&lifecycle("MAIN", "NAV-1", NETWORK_ALMOST_IDLE, 6.094),
);
let (via, at) = tracker.signal().expect("the main frame's own signal");
assert_eq!(via, SettleVia::NetworkAlmostIdle);
let elapsed = tracker.elapsed_ms(at).expect("both timestamps known");
assert!((elapsed - 6094.0).abs() < 0.01, "{elapsed}");
}
#[test]
fn a_tracker_that_cannot_name_the_main_frame_counts_nothing() {
let mut tracker = LifecycleTracker::new();
tracker.finish_arming();
tracker.observe(
LIFECYCLE_EVENT,
&lifecycle("ANY", "NAV-1", NETWORK_ALMOST_IDLE, 1.0),
);
assert_eq!(tracker.signal(), None);
}
#[test]
fn network_almost_idle_beats_load_and_a_redirect_counts_twice() {
let mut tracker = armed("MAIN");
tracker.observe(LIFECYCLE_EVENT, &lifecycle("MAIN", "NAV-1", "init", 0.0));
tracker.observe(
LIFECYCLE_EVENT,
&lifecycle("MAIN", "NAV-1", LOAD_LIFECYCLE, 1.0),
);
assert_eq!(tracker.signal(), Some((SettleVia::Load, Some(1.0))));
assert!(tracker.loaded());
tracker.observe(LIFECYCLE_EVENT, &lifecycle("MAIN", "NAV-2", "init", 1.2));
tracker.observe(
LIFECYCLE_EVENT,
&lifecycle("MAIN", "NAV-2", NETWORK_ALMOST_IDLE, 3.989),
);
assert_eq!(
tracker.signal(),
Some((SettleVia::NetworkAlmostIdle, Some(3.989)))
);
assert_eq!(tracker.navigations(), 2, "a redirect chain is two loaders");
assert_eq!(tracker.elapsed_ms(Some(3.989)), Some(3989.0));
}
#[test]
fn anything_that_is_not_a_lifecycle_event_is_ignored() {
let mut tracker = armed("MAIN");
tracker.observe(
"Chromeleon.captchaSolved",
&json!({"frameId": "MAIN", "loaderId": "NAV-1"}),
);
tracker.observe(
"Page.frameNavigated",
&lifecycle("MAIN", "NAV-1", NETWORK_ALMOST_IDLE, 1.0),
);
assert_eq!(tracker.signal(), None);
}
#[test]
fn the_browser_is_asked_to_give_up_before_we_do() {
assert_eq!(command_timeout_ms(30_000), 29_750);
let params = WaitOptions::new()
.timeout_ms(30_000)
.command_params(Some(command_timeout_ms(30_000)));
assert_eq!(params["timeoutMs"], 29_750);
assert!(params.get("quietWindowMs").is_none());
assert!(params.get("minChars").is_none());
assert_eq!(command_timeout_ms(400), 400);
let tuned = WaitOptions::new()
.quiet_window_ms(1500)
.min_chars(64)
.command_params(Some(command_timeout_ms(10_000)));
assert_eq!(
tuned,
json!({"timeoutMs": 9_750, "quietWindowMs": 1500, "minChars": 64})
);
}
}