mod mock_cdp;
use std::sync::{Arc, Mutex};
use std::time::Duration;
use cdp_driver::{CdpClient, CdpError};
use serde_json::{Value, json};
use mock_cdp::{Command, Reply};
const PATIENT: Duration = Duration::from_secs(5);
const IMPATIENT: Duration = Duration::from_millis(250);
const A_SESSION: &str = "5E5510N0000000000000000000000001";
const ANOTHER_SESSION: &str = "5E5510N0000000000000000000000002";
async fn client_answering<F>(respond: F) -> (CdpClient, mock_cdp::MockCdp)
where
F: Fn(&Command) -> Reply + Send + Sync + 'static,
{
let server = mock_cdp::start(respond).await;
let client = CdpClient::connect(&server.ws_url)
.await
.expect("connect to the mock endpoint");
client.set_command_timeout(PATIENT);
(client, server)
}
async fn client_attaching(
session_id: &'static str,
) -> (CdpClient, mock_cdp::MockCdp, Arc<Mutex<Vec<Command>>>) {
let seen: Arc<Mutex<Vec<Command>>> = Arc::new(Mutex::new(Vec::new()));
let recorder = seen.clone();
let (client, server) = client_answering(move |command| {
recorder
.lock()
.expect("record the command")
.push(command.clone());
match command.method.as_str() {
"Target.attachToTarget" => Reply::Result(json!({ "sessionId": session_id })),
_ => Reply::Result(json!({})),
}
})
.await;
(client, server, seen)
}
async fn client_raising(frames: Vec<Value>) -> (CdpClient, mock_cdp::MockCdp) {
client_answering(move |command| match command.method.as_str() {
"Page.enable" => Reply::ResultThenEvents {
result: json!({}),
events: frames.clone(),
},
_ => Reply::Result(json!({})),
})
.await
}
fn an_event_from(session_id: &str) -> Value {
json!({
"method": "Page.loadEventFired",
"params": { "timestamp": 1234.5 },
"sessionId": session_id,
})
}
fn an_event_from_the_connected_target() -> Value {
json!({
"method": "Page.loadEventFired",
"params": { "timestamp": 6789.0 },
})
}
#[tokio::test]
async fn attaching_asks_for_a_flat_session_and_keeps_the_id() {
let (client, _server, seen) = client_attaching(A_SESSION).await;
let session = client
.attach_to_target("TARGET-1")
.await
.expect("attach should succeed");
assert_eq!(session.id(), A_SESSION);
let log = seen.lock().expect("read the recorded commands");
assert_eq!(log[0].method, "Target.attachToTarget");
assert_eq!(log[0].params["targetId"], "TARGET-1");
assert_eq!(
log[0].params["flatten"], true,
"without flatten the session would have to be tunnelled through the \
deprecated Target.sendMessageToTarget"
);
assert_eq!(
log[0].session_id, None,
"the attach itself is a browser-level command, not a session one"
);
}
#[tokio::test]
async fn a_command_in_a_session_carries_its_session_id_on_the_wire() {
let (client, _server, seen) = client_attaching(A_SESSION).await;
let session = client
.attach_to_target("TARGET-1")
.await
.expect("attach should succeed");
let _ = session
.call_raw("Page.navigate", json!({ "url": "about:blank" }))
.await
.expect("a session command should succeed");
let log = seen.lock().expect("read the recorded commands");
let navigate = log.last().expect("the navigate should have arrived");
assert_eq!(navigate.method, "Page.navigate");
assert_eq!(navigate.session_id.as_deref(), Some(A_SESSION));
assert_eq!(
navigate.params["url"], "about:blank",
"the session id belongs beside the params, never inside them"
);
assert!(
navigate.params.get("sessionId").is_none(),
"the session id must not be smuggled into the params"
);
}
#[tokio::test]
async fn a_command_outside_a_session_carries_no_session_id() {
let (client, _server, seen) = client_attaching(A_SESSION).await;
let _ = client
.call_raw("Page.navigate", json!({ "url": "about:blank" }))
.await
.expect("a plain command should succeed");
let log = seen.lock().expect("read the recorded commands");
assert_eq!(
log[0].session_id, None,
"a client with no session must send the envelope it always sent"
);
}
#[tokio::test]
async fn an_event_from_a_session_is_attributable_to_it() {
let (client, _server) = client_raising(vec![an_event_from(A_SESSION)]).await;
let mut events = client.subscribe_session_events();
let _ = client
.call_raw("Page.enable", json!({}))
.await
.expect("enable should succeed");
let event = tokio::time::timeout(PATIENT, events.recv())
.await
.expect("the event should arrive well inside the timeout")
.expect("the event channel should still be open");
assert_eq!(event.method, "Page.loadEventFired");
assert_eq!(event.session_id.as_deref(), Some(A_SESSION));
assert_eq!(event.params["timestamp"], 1234.5);
}
#[tokio::test]
async fn an_event_from_the_connected_target_is_marked_as_belonging_to_no_session() {
let (client, _server) = client_raising(vec![an_event_from_the_connected_target()]).await;
let mut events = client.subscribe_session_events();
let _ = client
.call_raw("Page.enable", json!({}))
.await
.expect("enable should succeed");
let event = tokio::time::timeout(PATIENT, events.recv())
.await
.expect("the event should arrive well inside the timeout")
.expect("the event channel should still be open");
assert_eq!(event.session_id, None);
}
#[tokio::test]
async fn an_untagged_event_still_reaches_the_existing_subscriber() {
let (client, _server) = client_raising(vec![an_event_from_the_connected_target()]).await;
let mut events = client.subscribe_events();
let _ = client
.call_raw("Page.enable", json!({}))
.await
.expect("enable should succeed");
let (method, params) = tokio::time::timeout(PATIENT, events.recv())
.await
.expect("the event should arrive well inside the timeout")
.expect("the event channel should still be open");
assert_eq!(method, "Page.loadEventFired");
assert_eq!(params["timestamp"], 6789.0);
}
#[tokio::test]
async fn a_session_event_stays_out_of_the_untagged_subscriber() {
let (client, _server) = client_raising(vec![an_event_from(A_SESSION)]).await;
let mut events = client.subscribe_events();
let _ = client
.call_raw("Page.enable", json!({}))
.await
.expect("enable should succeed");
let arrived = tokio::time::timeout(IMPATIENT, events.recv()).await;
assert!(
arrived.is_err(),
"a session's event must not be mistaken for the connected target's: {arrived:?}"
);
}
#[tokio::test]
async fn waiting_on_a_session_ignores_an_event_from_another_target() {
let (client, _server) = client_raising(vec![an_event_from(ANOTHER_SESSION)]).await;
let session = client.session(A_SESSION);
let (waited, _enabled) = tokio::join!(
session.wait_for_event("Page.loadEventFired", IMPATIENT.as_millis() as u64),
client.call_raw("Page.enable", json!({})),
);
assert!(
matches!(waited, Err(CdpError::Timeout)),
"an event from another target is not the one this session waited for: {waited:?}"
);
}
#[tokio::test]
async fn waiting_on_a_session_returns_its_own_event() {
let (client, _server) = client_raising(vec![
an_event_from(ANOTHER_SESSION),
an_event_from(A_SESSION),
])
.await;
let session = client.session(A_SESSION);
let (waited, _enabled) = tokio::join!(
session.wait_for_event("Page.loadEventFired", 5_000),
client.call_raw("Page.enable", json!({})),
);
let params = waited.expect("the session's own event should end the wait");
assert_eq!(params["timestamp"], 1234.5);
}
#[tokio::test]
async fn commands_on_two_sessions_each_get_their_own_reply() {
let (client, _server) = client_answering(|command| {
Reply::Result(json!({
"session": command.session_id,
"method": command.method,
}))
})
.await;
let first = client.session(A_SESSION);
let second = client.session(ANOTHER_SESSION);
let (first, second) = tokio::join!(
first.call_raw("Runtime.evaluate", json!({ "expression": "1" })),
second.call_raw("Page.getFrameTree", json!({})),
);
let first = first.expect("the first session's command should succeed");
let second = second.expect("the second session's command should succeed");
assert_eq!(first["session"], A_SESSION);
assert_eq!(first["method"], "Runtime.evaluate");
assert_eq!(second["session"], ANOTHER_SESSION);
assert_eq!(second["method"], "Page.getFrameTree");
}
#[tokio::test]
async fn detaching_ends_the_session_and_leaves_the_connection_working() {
let (client, _server, seen) = client_attaching(A_SESSION).await;
let session = client
.attach_to_target("TARGET-1")
.await
.expect("attach should succeed");
session.detach().await.expect("detach should succeed");
let after = client
.call_raw("Browser.getVersion", json!({}))
.await
.expect("detaching one session must not end the connection");
assert_eq!(after, json!({}));
let log = seen.lock().expect("read the recorded commands");
let detach = &log[1];
assert_eq!(detach.method, "Target.detachFromTarget");
assert_eq!(
detach.params["sessionId"], A_SESSION,
"detach names the session in its params, since it is addressed to the browser"
);
assert_eq!(
detach.session_id, None,
"a command sent into the session it is ending would have nowhere to land"
);
}