use std::process::Command;
use std::time::{Duration, Instant};
mod common;
fn binary() -> String {
let mut path = std::env::current_exe().unwrap().parent().unwrap().parent().unwrap().to_path_buf();
path.push("chrome-agent");
path.to_string_lossy().into_owned()
}
fn run_cli(args: &[&str]) -> (String, i32) {
let output = Command::new(binary()).args(args).output().expect("Failed to run chrome-agent");
(
String::from_utf8_lossy(&output.stdout).to_string(),
output.status.code().unwrap_or(-1),
)
}
struct TestBrowser(String);
impl TestBrowser {
fn new(label: &str) -> Self {
Self(format!("{label}-{}", std::process::id()))
}
fn name(&self) -> &str {
&self.0
}
}
impl Drop for TestBrowser {
fn drop(&mut self) {
let _ = run_cli(&["--browser", &self.0, "close", "--purge"]);
}
}
#[test]
fn goto_returns_on_a_page_that_never_stops_mutating() {
if !common::browser_ready() {
return;
}
let b = TestBrowser::new("settle-ticker");
let url = common::fixture_url("goto_ticker.html");
let _ = run_cli(&["--browser", b.name(), "goto", &common::fixture_url("extract_cards.html")]);
let started = Instant::now();
let (_, code) = run_cli(&["--browser", b.name(), "goto", &url]);
let elapsed = started.elapsed();
assert_eq!(code, 0, "goto should succeed on a mutating page");
assert!(
elapsed < Duration::from_secs(15),
"goto took {elapsed:?} on a continuously mutating page; the settle probe has no ceiling"
);
}
#[test]
fn goto_does_not_wait_the_full_budget_on_a_static_page() {
if !common::browser_ready() {
return;
}
let b = TestBrowser::new("settle-static");
let url = common::fixture_url("extract_cards.html");
let _ = run_cli(&["--browser", b.name(), "goto", &url]);
let started = Instant::now();
let (_, code) = run_cli(&["--browser", b.name(), "goto", &url]);
let elapsed = started.elapsed();
assert_eq!(code, 0, "goto should succeed");
assert!(
elapsed < Duration::from_secs(2),
"goto took {elapsed:?} on a static page; the quiet window should start immediately \
rather than only after the first mutation"
);
}