mirror of
https://github.com/harivansh-afk/sandbox-agent.git
synced 2026-04-21 12:00:23 +00:00
feat: [US-043] - Strengthen network monitoring integration test
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
parent
c96558e523
commit
321493ad0e
1 changed files with 68 additions and 25 deletions
|
|
@ -902,13 +902,37 @@ const TEST_HTML_NETWORK: &str = r#"<!DOCTYPE html>
|
||||||
<head><title>Network Test</title></head>
|
<head><title>Network Test</title></head>
|
||||||
<body>
|
<body>
|
||||||
<p>Network test page</p>
|
<p>Network test page</p>
|
||||||
|
<script>
|
||||||
|
// Trigger a real HTTP fetch so CDP captures a network request with a response
|
||||||
|
fetch('/hello.txt').catch(function() {});
|
||||||
|
</script>
|
||||||
</body>
|
</body>
|
||||||
</html>"#;
|
</html>"#;
|
||||||
|
|
||||||
|
const TEST_NETWORK_HELLO: &str = "hello from network test";
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
#[serial]
|
#[serial]
|
||||||
async fn v1_browser_network_monitoring() {
|
async fn v1_browser_network_monitoring() {
|
||||||
let test_app = TestApp::new(AuthConfig::disabled());
|
let test_app = TestApp::new(AuthConfig::disabled());
|
||||||
|
let http_port: u16 = 18770;
|
||||||
|
|
||||||
|
// Write test assets into the container
|
||||||
|
write_test_file(
|
||||||
|
&test_app.app,
|
||||||
|
"/tmp/network-test/index.html",
|
||||||
|
TEST_HTML_NETWORK,
|
||||||
|
)
|
||||||
|
.await;
|
||||||
|
write_test_file(
|
||||||
|
&test_app.app,
|
||||||
|
"/tmp/network-test/hello.txt",
|
||||||
|
TEST_NETWORK_HELLO,
|
||||||
|
)
|
||||||
|
.await;
|
||||||
|
|
||||||
|
// Start an HTTP server inside the container to serve the test pages
|
||||||
|
let server_pid = start_http_server(&test_app.app, "/tmp/network-test", http_port).await;
|
||||||
|
|
||||||
// Start browser
|
// Start browser
|
||||||
let (status, _, body) = send_request(
|
let (status, _, body) = send_request(
|
||||||
|
|
@ -926,51 +950,70 @@ async fn v1_browser_network_monitoring() {
|
||||||
String::from_utf8_lossy(&body)
|
String::from_utf8_lossy(&body)
|
||||||
);
|
);
|
||||||
|
|
||||||
// Write and navigate to a test page to generate network activity
|
// Navigate to the test page served over HTTP (triggers a fetch to /hello.txt)
|
||||||
write_test_file(&test_app.app, "/tmp/test-network.html", TEST_HTML_NETWORK).await;
|
|
||||||
let (status, _, _) = send_request(
|
let (status, _, _) = send_request(
|
||||||
&test_app.app,
|
&test_app.app,
|
||||||
Method::POST,
|
Method::POST,
|
||||||
"/v1/browser/navigate",
|
"/v1/browser/navigate",
|
||||||
Some(json!({ "url": "file:///tmp/test-network.html" })),
|
Some(json!({ "url": format!("http://localhost:{http_port}/index.html") })),
|
||||||
&[],
|
&[],
|
||||||
)
|
)
|
||||||
.await;
|
.await;
|
||||||
assert_eq!(status, StatusCode::OK);
|
assert_eq!(status, StatusCode::OK);
|
||||||
|
|
||||||
// Wait for CDP network events to be captured
|
// Poll for network requests until the expected entry appears (up to 5s)
|
||||||
tokio::time::sleep(Duration::from_secs(1)).await;
|
let mut captured_requests: Vec<serde_json::Value> = Vec::new();
|
||||||
|
for _ in 0..25 {
|
||||||
|
tokio::time::sleep(Duration::from_millis(200)).await;
|
||||||
|
let (status, _, body) =
|
||||||
|
send_request(&test_app.app, Method::GET, "/v1/browser/network", None, &[]).await;
|
||||||
|
assert_eq!(status, StatusCode::OK);
|
||||||
|
let parsed = parse_json(&body);
|
||||||
|
let requests = parsed["requests"].as_array().expect("requests array");
|
||||||
|
// Look for the hello.txt request with a populated status
|
||||||
|
if requests.iter().any(|r| {
|
||||||
|
r["url"]
|
||||||
|
.as_str()
|
||||||
|
.map_or(false, |u| u.contains("/hello.txt"))
|
||||||
|
&& r["status"].as_u64().is_some()
|
||||||
|
}) {
|
||||||
|
captured_requests = requests.clone();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Get network requests
|
|
||||||
let (status, _, body) =
|
|
||||||
send_request(&test_app.app, Method::GET, "/v1/browser/network", None, &[]).await;
|
|
||||||
assert_eq!(status, StatusCode::OK);
|
|
||||||
let parsed = parse_json(&body);
|
|
||||||
let requests = parsed["requests"].as_array().expect("requests array");
|
|
||||||
assert!(
|
assert!(
|
||||||
!requests.is_empty(),
|
!captured_requests.is_empty(),
|
||||||
"should have captured at least one network request from page navigation"
|
"should have captured network requests"
|
||||||
);
|
);
|
||||||
|
|
||||||
// Verify request entries have expected fields
|
// Find the hello.txt request and verify its values
|
||||||
let first = &requests[0];
|
let hello_req = captured_requests
|
||||||
assert!(
|
.iter()
|
||||||
first["url"].as_str().is_some() && !first["url"].as_str().unwrap().is_empty(),
|
.find(|r| {
|
||||||
"request should have a url"
|
r["url"]
|
||||||
|
.as_str()
|
||||||
|
.map_or(false, |u| u.contains("/hello.txt"))
|
||||||
|
})
|
||||||
|
.expect("should have captured the /hello.txt request");
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
hello_req["method"].as_str(),
|
||||||
|
Some("GET"),
|
||||||
|
"request method should be GET"
|
||||||
);
|
);
|
||||||
assert!(
|
assert_eq!(
|
||||||
first["method"].as_str().is_some(),
|
hello_req["status"].as_u64(),
|
||||||
"request should have a method"
|
Some(200),
|
||||||
);
|
"request status should be 200"
|
||||||
assert!(
|
|
||||||
first["timestamp"].as_str().is_some(),
|
|
||||||
"request should have a timestamp"
|
|
||||||
);
|
);
|
||||||
|
|
||||||
// Stop browser
|
// Stop browser
|
||||||
let (status, _, _) =
|
let (status, _, _) =
|
||||||
send_request(&test_app.app, Method::POST, "/v1/browser/stop", None, &[]).await;
|
send_request(&test_app.app, Method::POST, "/v1/browser/stop", None, &[]).await;
|
||||||
assert_eq!(status, StatusCode::OK);
|
assert_eq!(status, StatusCode::OK);
|
||||||
|
|
||||||
|
stop_http_server(&test_app.app, &server_pid).await;
|
||||||
}
|
}
|
||||||
|
|
||||||
const TEST_HTML_CRAWL_A: &str = r#"<!DOCTYPE html>
|
const TEST_HTML_CRAWL_A: &str = r#"<!DOCTYPE html>
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue