mod support;
use eoka_solvers::datadome::fixture::Fixture;
use serde_json::Value;
use std::{path::PathBuf, time::Duration};
use support::Session;
fn session(label: &str, port: Option<u16>) -> Session {
let session = Session::new(&format!("dd-cli-{label}"))
.flags(&["--json", "--no-geo-align"])
.env(
eoka_protocol::env::CHROME_ARGS,
"--host-resolver-rules=MAP * 127.0.0.1:--no-proxy-server:--site-per-process",
);
match port {
Some(port) => session.flags(&["--cdp", &port.to_string()]),
None => session,
}
}
impl Session {
fn async_command(&self, args: &[&str]) -> tokio::process::Command {
let mut command = self.tokio_command(args);
if matches!(args.first(), Some(&"eval" | &"exec")) {
command.arg("--raw");
}
command
}
async fn call(&self, args: &[&str]) -> Value {
let output =
tokio::time::timeout(Duration::from_secs(35), self.async_command(args).output())
.await
.expect("CLI deadline")
.unwrap();
let response = support::parse(args, &output);
assert_eq!(
output.status.success(),
response["ok"] == true,
"{response}"
);
response
}
async fn ok(&self, args: &[&str]) -> Value {
let response = self.call(args).await;
assert_eq!(response["ok"], true, "{response}");
let mut data = response["data"].clone();
if data["action"].is_string() && data["state"] == "running" {
let finished = self
.call(&["operations", "wait", data["id"].as_str().unwrap()])
.await;
assert_eq!(finished["ok"], true, "{finished}");
data = finished["data"].clone();
assert_eq!(data["outcome"], "verified_complete", "{data}");
}
data
}
async fn eval(&self, expression: &str) -> Value {
let data = self
.ok(&["eval", &format!("JSON.stringify({expression})")])
.await;
let encoded: String = serde_json::from_str(data.as_str().unwrap()).unwrap();
serde_json::from_str(&encoded).unwrap()
}
fn children(&self, pid: &str) -> Vec<u32> {
let mut children = Vec::new();
for task in std::fs::read_dir(format!("/proc/{}/task", pid.trim()))
.unwrap()
.flatten()
{
if let Ok(value) = std::fs::read_to_string(task.path().join("children")) {
children.extend(
value
.split_whitespace()
.map(|child| child.parse::<u32>().unwrap()),
);
}
}
children.sort_unstable();
children.dedup();
children
}
fn pid_path(&self) -> PathBuf {
eoka_sdk::session::pid_path(&self.name)
}
}
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
#[ignore = "local keyboard fixture; no live authentication"]
async fn key_types_native_text_into_top_level_and_focused_frame_inputs() {
let fixture = Fixture::scenario("simple").await;
let cli = session("key-text", None);
cli.ok(&["open", &fixture.url]).await;
let pid = std::fs::read_to_string(cli.pid_path()).unwrap();
cli.eval("(() => {const input=document.createElement('input');input.id='key-fixture';input.type='search';document.body.appendChild(input);window.keyEvents=[];for(const type of ['keydown','keypress','beforeinput','input','keyup'])input.addEventListener(type,event=>keyEvents.push({type,trusted:event.isTrusted}));input.focus();return true})()").await;
cli.ok(&["key", "o", "--expect", r##"{"kind":"value","target":{"query":{"kind":"css","selector":"#key-fixture"}},"value":"o"}"##]).await;
assert_eq!(
cli.eval("document.querySelector('#key-fixture').value")
.await,
"o"
);
let events = cli.eval("keyEvents").await;
let events = events.as_array().unwrap();
assert_eq!(
events
.iter()
.map(|event| event["type"].as_str().unwrap())
.collect::<Vec<_>>(),
["keydown", "keypress", "beforeinput", "input", "keyup"]
);
assert!(events.iter().all(|event| event["trusted"] == true));
cli.ok(&["key-down", "Shift"]).await;
cli.ok(&["key-down", "a"]).await;
cli.ok(&["key-up", "a"]).await;
cli.ok(&["key-up", "Shift"]).await;
cli.ok(&["key", "@", "--expect", r##"{"kind":"value","target":{"query":{"kind":"css","selector":"#key-fixture"}},"value":"oA@"}"##]).await;
assert_eq!(
cli.eval("document.querySelector('#key-fixture').value")
.await,
"oA@"
);
let select_all = if cfg!(target_os = "macos") {
"Cmd+A"
} else {
"Ctrl+A"
};
cli.ok(&["key-down", select_all]).await;
cli.ok(&["key-up", select_all]).await;
cli.ok(&["key", "Backspace", "--expect", r##"{"kind":"value","target":{"query":{"kind":"css","selector":"#key-fixture"}},"value":""}"##]).await;
assert_eq!(
cli.eval("document.querySelector('#key-fixture').value")
.await,
""
);
let frames = cli.ok(&["frames"]).await;
let frame = frames
.as_array()
.unwrap()
.iter()
.find(|frame| frame["url"].as_str().unwrap().contains("auth.test"))
.unwrap();
let frame_id = format!("id:{}", frame["id"].as_str().unwrap());
cli.ok(&["frame-eval", &frame_id, "(() => {const input=document.createElement('input');input.id='native-input';document.body.appendChild(input);input.addEventListener('input',event=>input.dataset.trusted=String(event.isTrusted));input.focus();return true})()"]).await;
let expect=serde_json::json!({"kind":"value","target":{"query":{"kind":"css","selector":"#native-input"},"frame_id":frame["id"]},"value":"o"}).to_string();
cli.ok(&["key", "o", "--expect", &expect]).await;
assert_eq!(
cli.ok(&[
"frame-eval",
&frame_id,
"document.querySelector('#native-input').value"
])
.await,
"\"o\""
);
assert_eq!(
cli.ok(&[
"frame-eval",
&frame_id,
"document.querySelector('#native-input').dataset.trusted"
])
.await,
"\"true\""
);
assert_eq!(std::fs::read_to_string(cli.pid_path()).unwrap(), pid);
assert_eq!(cli.ok(&["info"]).await["url"], fixture.url);
cli.ok(&["close"]).await;
}
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
#[ignore = "local Chrome OOPIF fixture; no live authentication"]
async fn frame_eval_preserves_the_embedded_context_and_selected_tab() {
let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
let port = listener.local_addr().unwrap().port();
drop(listener);
let browser = eoka::Browser::launch_with(|config| {
config.extra_args.extend([
format!("--remote-debugging-port={port}"),
"--host-resolver-rules=MAP * 127.0.0.1".into(),
"--no-proxy-server".into(),
"--disable-features=AutomationControlled,EnableAutomation".into(),
"--site-per-process".into(),
])
})
.await
.unwrap();
let fixture = Fixture::scenario("simple").await;
let page = browser.new_page(&fixture.url).await.unwrap();
let other = browser.new_page("about:blank").await.unwrap();
other.execute_sync("window.untouched='yes'").await.unwrap();
let cli = session("frame-eval", Some(port));
cli.ok(&["tab", "attach", page.target_id()]).await;
let pid = std::fs::read_to_string(cli.pid_path()).unwrap();
let count = browser.tabs().await.unwrap().len();
let targets: Value = page
.session()
.transport()
.send("Target.getTargets", &serde_json::json!({}))
.await
.unwrap();
assert!(
targets["targetInfos"]
.as_array()
.unwrap()
.iter()
.filter(|target| target["type"] == "iframe")
.count()
>= 2
);
let frames = cli.ok(&["frames"]).await;
let frames = frames.as_array().unwrap();
let auth = frames
.iter()
.find(|frame| frame["url"].as_str().unwrap().contains("auth.test"))
.unwrap();
let child = frames
.iter()
.find(|frame| frame["url"].as_str().unwrap().contains("/captcha/"))
.unwrap();
let auth_id = format!("id:{}", auth["id"].as_str().unwrap());
let child_id = format!("id:{}", child["id"].as_str().unwrap());
let result = cli.ok(&["frame-eval", "iframe", r#"(() => {const input=document.createElement('input');input.id='fixture-email';input.value='fixture@example.invalid';document.body.appendChild(input);return {host:location.hostname,email:input.value,embedded:window!==window.top}})()"#]).await;
let result: Value = serde_json::from_str(result.as_str().unwrap()).unwrap();
assert_eq!(
result,
serde_json::json!({"host":"auth.test","email":"fixture@example.invalid","embedded":true})
);
assert_eq!(
cli.ok(&[
"frame-eval",
&auth_id,
"document.querySelector('#fixture-email').value"
])
.await,
"\"fixture@example.invalid\""
);
assert_eq!(
cli.ok(&[
"frame-eval",
&child_id,
"document.querySelector('#fixture-email')"
])
.await,
"null"
);
assert_eq!(
cli.eval("document.querySelector('#fixture-email')===null")
.await,
true
);
let other_id = format!("id:{}", other.frames().await.unwrap()[0].id);
for invalid in [
"0",
"#missing",
"id:",
"id:missing",
&other_id,
&fixture.url,
] {
assert_eq!(
cli.call(&["frame-eval", invalid, "window.untouched='changed'"])
.await["ok"],
false
);
}
assert_eq!(
other
.evaluate_sync::<String>("window.untouched")
.await
.unwrap(),
"yes"
);
assert_eq!(browser.tabs().await.unwrap().len(), count);
assert_eq!(std::fs::read_to_string(cli.pid_path()).unwrap(), pid);
assert_eq!(cli.ok(&["info"]).await["url"], fixture.url);
page.execute_sync("document.querySelector('iframe').remove()")
.await
.unwrap();
assert_eq!(
cli.call(&["frame-eval", &child_id, "1+1"]).await["ok"],
false
);
assert_eq!(cli.ok(&["info"]).await["url"], fixture.url);
cli.ok(&["close"]).await;
other.execute_sync("window.stillAlive=true").await.unwrap();
browser.close().await.unwrap();
}
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
#[ignore = "local Chrome CLI/daemon fixture, not live DataDome acceptance"]
async fn datadome_cli_preserves_owned_session_and_reports_outcomes() {
let cli = session("owned", None);
let missing = cli.call(&["captcha", "datadome"]).await;
assert_eq!(missing["error_detail"]["code"], "session_unavailable");
assert!(!cli.pid_path().exists());
let mut pid = String::new();
let mut children = Vec::new();
let mut target = String::new();
for (mode, expected) in [
("simple", "challenge_cleared"),
("image", "challenge_cleared"),
("absent", "not_present"),
("blocked", "blocked"),
("banned", "ip_banned"),
("unsupported", "failed"),
("reblock", "blocked"),
("timeout", "datadome_timeout"),
] {
let fixture = if mode == "image" {
Fixture::start(false, false, false).await
} else {
Fixture::scenario(mode).await
};
cli.ok(&["open", &fixture.url]).await;
if pid.is_empty() {
pid = std::fs::read_to_string(cli.pid_path()).unwrap();
children = cli.children(&pid);
assert!(!children.is_empty());
cli.ok(&["network", "record", "start"]).await;
cli.ok(&["network", "intercept", "add", "*/accepted"]).await;
}
let before = cli.eval("({marker:window.fixtureMarker,local:localStorage.getItem('fixture'),session:sessionStorage.getItem('fixture')})").await;
let response = cli
.call(&[
"captcha",
"datadome",
"--timeout-ms",
if mode == "timeout" { "5000" } else { "20000" },
])
.await;
if mode == "timeout" {
assert_eq!(response["error_detail"]["code"], expected, "{response}");
let held = cli.eval("document.body.dataset.pressed==='true' && document.body.dataset.released!=='true'").await;
assert_eq!(held, false);
} else {
assert_eq!(response["data"]["outcome"], expected, "{mode}: {response}");
let id = response["data"]["tab_id"].as_str().unwrap();
if target.is_empty() {
target = id.into();
}
assert_eq!(id, target);
}
assert_eq!(std::fs::read_to_string(cli.pid_path()).unwrap(), pid);
assert_eq!(cli.children(&pid), children);
assert_eq!(cli.eval("({marker:window.fixtureMarker,local:localStorage.getItem('fixture'),session:sessionStorage.getItem('fixture')})").await, before);
assert_eq!(
cli.eval("document.cookie.includes('other=retained')").await,
true
);
assert_eq!(cli.ok(&["info"]).await["url"], fixture.url);
if expected == "challenge_cleared" {
assert_eq!(
cli.eval("document.body.dataset.restored==='true'").await,
true
);
}
cli.ok(&["snapshot"]).await;
cli.ok(&["click", "Continue", "--expect", r##"{"kind":"visible","target":{"query":{"kind":"css","selector":"body[data-continued=true]"}}}"##]).await;
assert_eq!(
cli.eval("document.body.dataset.continued==='true'").await,
true
);
cli.ok(&["network", "record", "status"]).await;
if mode == "simple" {
tokio::time::timeout(Duration::from_secs(2), fixture.started.notified())
.await
.unwrap();
cli.ok(&["exec", "setTimeout(()=>fetch('/accepted'),100)"])
.await;
tokio::time::timeout(Duration::from_secs(2), fixture.started.notified())
.await
.unwrap();
cli.ok(&["eval", "fetch('/accepted').then(r=>r.text())"])
.await;
}
}
let logs = cli.ok(&["network", "intercept", "log"]).await;
assert!(logs.to_string().contains("accepted"), "{logs}");
cli.ok(&["network", "record", "stop"]).await;
cli.ok(&["close"]).await;
}
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
#[ignore = "local Chrome interception fixture"]
async fn interception_preserves_binary_responses_and_bounds_worker_logs() {
let files = tempfile::tempdir().unwrap();
let response_path = files.path().join("response.bin");
let capture_path = files.path().join("request.json");
let bytes: Vec<u8> = (0..=255).collect();
std::fs::write(&response_path, &bytes).unwrap();
let fixture = Fixture::scenario("absent").await;
let cli = session("interception", None);
cli.ok(&["open", &fixture.url]).await;
cli.ok(&[
"network",
"intercept",
"add",
"*/binary",
"--respond",
response_path.to_str().unwrap(),
"--capture",
capture_path.to_str().unwrap(),
])
.await;
assert_eq!(
cli.ok(&["captcha", "datadome"]).await["outcome"],
"not_present"
);
let data = cli.ok(&["eval", "fetch('/binary',{method:'POST',body:'fixture-body'}).then(r=>r.arrayBuffer()).then(b=>Array.from(new Uint8Array(b)))"]).await;
assert_eq!(
serde_json::from_str::<Vec<u8>>(data.as_str().unwrap()).unwrap(),
bytes
);
let capture: Value = serde_json::from_slice(&std::fs::read(&capture_path).unwrap()).unwrap();
assert_eq!(capture["method"], "POST");
assert_eq!(capture["postData"], "fixture-body");
let logs = cli.ok(&["network", "intercept", "log", "--clear"]).await;
assert_eq!(logs.as_array().unwrap().len(), 1);
assert_eq!(logs[0]["action"], "responded");
cli.ok(&[
"network",
"intercept",
"add",
"*/bulk*",
"--respond",
response_path.to_str().unwrap(),
])
.await;
let data = cli.ok(&["eval", "(async()=>{for(let batch=0;batch<11;batch++){await Promise.all(Array.from({length:100},(_,i)=>fetch('/bulk?i='+(batch*100+i)).then(r=>r.arrayBuffer())))}return 1100})()"] ).await;
assert_eq!(data, "1100");
let logs = cli.ok(&["network", "intercept", "log", "--clear"]).await;
assert_eq!(logs.as_array().unwrap().len(), 1000);
assert!(logs
.as_array()
.unwrap()
.iter()
.all(|entry| entry["action"] == "responded"));
assert_eq!(
cli.ok(&["network", "intercept", "log"]).await,
serde_json::json!([])
);
cli.ok(&["exec", "window.streaming=true; window.streamingWork=(async()=>{while(window.streaming){await Promise.all(Array.from({length:8},()=>fetch('/bulk').then(r=>r.arrayBuffer())))}return 'done'})(); void 0"]).await;
assert_eq!(
cli.ok(&["captcha", "datadome"]).await["outcome"],
"not_present"
);
cli.ok(&["eval", "window.streaming=false; window.streamingWork"])
.await;
cli.ok(&["snapshot"]).await;
cli.ok(&["close"]).await;
}
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
#[ignore = "local Chrome client-disconnect fixture"]
async fn accepted_request_finishes_after_cli_disconnect_without_replay() {
let fixture = Fixture::scenario("simple").await;
let cli = session("disconnect", None);
cli.ok(&["open", &fixture.url]).await;
let pid = std::fs::read_to_string(cli.pid_path()).unwrap();
let mut child = cli
.async_command(&["captcha", "datadome", "--timeout-ms", "20000"])
.stdout(std::process::Stdio::piped())
.stderr(std::process::Stdio::piped())
.spawn()
.unwrap();
tokio::select! {
_ = fixture.started.notified() => {}
status = child.wait() => {
let mut output = String::new();
for stream in [
child.stdout.take().map(|s| Box::new(s) as Box<dyn tokio::io::AsyncRead + Unpin>),
child.stderr.take().map(|s| Box::new(s) as Box<dyn tokio::io::AsyncRead + Unpin>),
]
.into_iter()
.flatten()
{
let mut stream = stream;
tokio::io::AsyncReadExt::read_to_string(&mut stream, &mut output)
.await
.unwrap();
}
panic!("captcha exited before pressing the slider: {status:?}\n{output}");
}
_ = tokio::time::sleep(Duration::from_secs(20)) => {
panic!("captcha did not press the slider within its 20 s timeout");
}
}
child.kill().await.unwrap();
child.wait().await.unwrap();
let waited = std::time::Instant::now();
loop {
let response = cli.call(&["snapshot"]).await;
if response["ok"] == true {
break;
}
assert_eq!(
response["error_detail"]["code"], "daemon_busy",
"{response}"
);
assert!(
response["error"]
.as_str()
.unwrap()
.contains("`captcha datadome` has been running"),
"{response}"
);
assert!(
waited.elapsed() < Duration::from_secs(30),
"the accepted request never finished: {response}"
);
}
assert_eq!(
cli.eval("document.body.dataset.restored==='true'").await,
true
);
assert_eq!(std::fs::read_to_string(cli.pid_path()).unwrap(), pid);
assert_eq!(
cli.ok(&["captcha", "datadome"]).await["outcome"],
"not_present"
);
cli.ok(&["click", "Continue", "--expect", r##"{"kind":"visible","target":{"query":{"kind":"css","selector":"body[data-continued=true]"}}}"##]).await;
cli.ok(&["close"]).await;
}
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
#[ignore = "local Chrome borrowed-session fixture"]
async fn datadome_cli_preserves_borrowed_browser_other_tab_and_missing_target() {
let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
let port = listener.local_addr().unwrap().port();
drop(listener);
let browser = eoka::Browser::launch_with(|config| {
config.extra_args.extend([
format!("--remote-debugging-port={port}"),
"--host-resolver-rules=MAP * 127.0.0.1".into(),
"--no-proxy-server".into(),
"--site-per-process".into(),
])
})
.await
.unwrap();
let fixture = Fixture::scenario("simple").await;
let page = browser.new_page(&fixture.url).await.unwrap();
let other = browser.new_page("about:blank").await.unwrap();
other.execute_sync("window.untouched='yes'").await.unwrap();
let cli = session("borrowed", Some(port));
cli.ok(&["tab", "attach", page.target_id()]).await;
let data = cli
.ok(&["captcha", "datadome", "--timeout-ms", "20000"])
.await;
assert_eq!(data["outcome"], "challenge_cleared");
assert_eq!(data["tab_id"], page.target_id());
assert_eq!(
other
.evaluate_sync::<String>("window.untouched")
.await
.unwrap(),
"yes"
);
browser.close_tab(page.target_id()).await.unwrap();
let response = cli
.call(&["captcha", "datadome", "--timeout-ms", "5000"])
.await;
assert_eq!(response["ok"], false);
assert_eq!(
other
.evaluate_sync::<String>("window.untouched")
.await
.unwrap(),
"yes"
);
cli.ok(&["close"]).await;
other.execute_sync("window.stillAlive=true").await.unwrap();
browser.close().await.unwrap();
}
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
#[ignore = "local Chrome interception integrity regression"]
async fn failed_interception_files_suppress_clearance_and_idle_special_files_do_not_stall() {
let files = tempfile::tempdir().unwrap();
for option in ["--capture", "--respond"] {
let fixture = Fixture::scenario("simple").await;
let cli = session(
if option == "--capture" {
"bad-capture"
} else {
"bad-response"
},
None,
);
cli.ok(&["open", &fixture.url]).await;
let missing = files.path().join("missing-parent/file");
cli.ok(&[
"network",
"intercept",
"add",
"*/accepted",
option,
missing.to_str().unwrap(),
])
.await;
let response = cli.call(&["captcha", "datadome"]).await;
assert_eq!(
response["error_detail"]["code"], "datadome_cleanup_failed",
"{response}"
);
assert_eq!(cli.eval("document.body.dataset.restored").await, "true");
let logs = cli.ok(&["network", "intercept", "log"]).await;
assert!(logs
.as_array()
.unwrap()
.iter()
.any(|entry| entry["action"].as_str().unwrap().contains("failed")));
cli.ok(&["snapshot"]).await;
cli.ok(&["close"]).await;
}
let fifo = files.path().join("fifo");
assert!(std::process::Command::new("mkfifo")
.arg(&fifo)
.status()
.unwrap()
.success());
let fixture = Fixture::scenario("absent").await;
let cli = session("idle-special", None);
cli.ok(&["open", &fixture.url]).await;
for path in [fifo.to_str().unwrap(), "/dev/zero"] {
cli.ok(&["network", "intercept", "remove", "all"]).await;
cli.ok(&[
"network",
"intercept",
"add",
"*/accepted",
"--respond",
path,
])
.await;
cli.ok(&["exec", "setTimeout(()=>fetch('/accepted').then(()=>document.body.dataset.fetched='true'),100); void 0"]).await;
tokio::time::sleep(Duration::from_millis(250)).await;
tokio::time::timeout(Duration::from_secs(5), cli.ok(&["snapshot"]))
.await
.unwrap();
}
let socket = tokio::net::UnixStream::connect(eoka_sdk::session::socket_path(&cli.name))
.await
.unwrap();
let (reader, mut writer) = socket.into_split();
let mut reader = tokio::io::BufReader::new(reader);
let frame = eoka_protocol::wire::request_frame("shutdown", serde_json::json!({}));
let shutdown = tokio::time::timeout(
Duration::from_secs(5),
eoka_protocol::wire::call(&mut reader, &mut writer, Some(&frame)),
)
.await
.unwrap()
.unwrap();
assert!(shutdown.ok);
}