stygian-browser 0.13.5

Anti-detection browser automation library for Rust with CDP stealth features
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
#![cfg(feature = "mcp")]

//! MCP integration tests for stygian-browser.
//!
//! These tests exercise the JSON-RPC tool surface directly via
//! `McpBrowserServer::dispatch`.
//!
//! Requirements:
//! - `mcp` feature enabled
//! - real Chrome/Chromium binary available
//! - external network access
//!
//! Run:
//! ```sh
//! cargo test -p stygian-browser --features mcp --test mcp_integration -- --ignored --test-threads=1
//! ```

use std::path::PathBuf;
use std::sync::atomic::{AtomicU64, Ordering};
use std::time::Duration;

use serde_json::{Value, json};
use stygian_browser::BrowserConfig;
use stygian_browser::BrowserPool;
use stygian_browser::config::PoolConfig;
use stygian_browser::mcp::McpBrowserServer;

type DynError = Box<dyn std::error::Error>;

fn unique_user_data_dir() -> PathBuf {
    static COUNTER: AtomicU64 = AtomicU64::new(0);
    let n = COUNTER.fetch_add(1, Ordering::Relaxed);
    let pid = std::process::id();
    std::env::temp_dir().join(format!("stygian-mcp-itest-{pid}-{n}"))
}

fn test_config() -> BrowserConfig {
    let mut cfg = BrowserConfig::builder()
        .headless(true)
        .pool(PoolConfig {
            min_size: 0,
            max_size: 1,
            acquire_timeout: Duration::from_secs(10),
            ..PoolConfig::default()
        })
        .build();

    cfg.launch_timeout = Duration::from_secs(30);
    cfg.cdp_timeout = Duration::from_secs(15);
    cfg.user_data_dir = Some(unique_user_data_dir());

    if let Ok(p) = std::env::var("STYGIAN_CHROME_PATH") {
        cfg.chrome_path = Some(PathBuf::from(p));
    }

    cfg
}

fn parse_tools_call_text(resp: &Value) -> Result<Value, DynError> {
    let is_error = resp
        .get("result")
        .and_then(|r| r.get("isError"))
        .and_then(Value::as_bool)
        .unwrap_or(true);

    if is_error {
        return Err(
            std::io::Error::other(format!("MCP tool returned isError=true: {resp}")).into(),
        );
    }

    let text = resp
        .get("result")
        .and_then(|r| r.get("content"))
        .and_then(Value::as_array)
        .and_then(|arr| arr.first())
        .and_then(|entry| entry.get("text"))
        .and_then(Value::as_str)
        .ok_or_else(|| std::io::Error::other(format!("missing tools/call text payload: {resp}")))?;

    Ok(serde_json::from_str(text)?)
}

async fn call_tool(
    server: &McpBrowserServer,
    id: u64,
    name: &str,
    arguments: Value,
) -> Result<Value, DynError> {
    let resp = server
        .dispatch(&json!({
            "jsonrpc": "2.0",
            "id": id,
            "method": "tools/call",
            "params": {
                "name": name,
                "arguments": arguments
            }
        }))
        .await;
    parse_tools_call_text(&resp)
}

fn session_id_from_payload(payload: &Value) -> Result<String, DynError> {
    payload
        .get("session_id")
        .and_then(Value::as_str)
        .map(ToString::to_string)
        .ok_or_else(|| std::io::Error::other("tool response returned no session_id").into())
}

async fn acquire_session(
    server: &McpBrowserServer,
    id: u64,
    target_profile: &str,
) -> Result<String, DynError> {
    let payload = call_tool(
        server,
        id,
        "browser_acquire",
        json!({ "target_profile": target_profile }),
    )
    .await?;
    session_id_from_payload(&payload)
}

async fn release_session(
    server: &McpBrowserServer,
    id: u64,
    session_id: &str,
) -> Result<Value, DynError> {
    call_tool(
        server,
        id,
        "browser_release",
        json!({ "session_id": session_id }),
    )
    .await
}

async fn assert_session_save_ttl(
    server: &McpBrowserServer,
    id: u64,
    session_id: &str,
    ttl_secs: u64,
) -> Result<(), DynError> {
    let save_payload = call_tool(
        server,
        id,
        "browser_session_save",
        json!({
            "session_id": session_id,
            "ttl_secs": ttl_secs
        }),
    )
    .await?;
    assert_eq!(
        save_payload.get("ttl_secs").and_then(Value::as_u64),
        Some(ttl_secs),
        "saved snapshot should persist requested ttl"
    );
    Ok(())
}

async fn assert_session_restore_saved(
    server: &McpBrowserServer,
    id: u64,
    session_id: &str,
) -> Result<(), DynError> {
    let restore_payload = call_tool(
        server,
        id,
        "browser_session_restore",
        json!({
            "session_id": session_id,
            "use_saved": true,
            "navigate_to_origin": false
        }),
    )
    .await?;
    assert_eq!(
        restore_payload.get("source").and_then(Value::as_str),
        Some("saved"),
        "restore should use in-memory saved snapshot by default"
    );
    Ok(())
}

async fn assert_humanize_applied(
    server: &McpBrowserServer,
    id: u64,
    session_id: &str,
) -> Result<(), DynError> {
    let humanize_payload = call_tool(
        server,
        id,
        "browser_humanize",
        json!({
            "session_id": session_id,
            "level": "none"
        }),
    )
    .await?;
    assert_eq!(
        humanize_payload.get("applied").and_then(Value::as_bool),
        Some(true),
        "humanize should report applied=true"
    );
    Ok(())
}

async fn assert_auth_session_mode(
    server: &McpBrowserServer,
    id: u64,
    session_id: &str,
    mode: &str,
    ttl_secs: Option<u64>,
) -> Result<(), DynError> {
    let mut args = json!({
        "session_id": session_id,
        "mode": mode,
        "navigate_to_origin": false,
        "interaction_level": "none"
    });
    if mode == "capture"
        && let Some(ttl) = ttl_secs
        && let Some(obj) = args.as_object_mut()
    {
        obj.insert("ttl_secs".to_string(), Value::from(ttl));
    }

    let payload = call_tool(server, id, "browser_auth_session", args).await?;
    assert_eq!(
        payload.get("mode").and_then(Value::as_str),
        Some(mode),
        "auth session wrapper should report {mode} mode"
    );
    Ok(())
}

#[cfg(feature = "mcp-attach")]
async fn assert_attach_contract(server: &McpBrowserServer, id: u64) -> Result<(), DynError> {
    let attach_contract_payload = call_tool(
        server,
        id,
        "browser_attach",
        json!({
            "mode": "extension_bridge",
            "profile_hint": "reddit-main"
        }),
    )
    .await?;
    assert_eq!(
        attach_contract_payload
            .get("supported")
            .and_then(Value::as_bool),
        Some(false),
        "extension bridge mode should clearly report unsupported until backend is implemented"
    );
    Ok(())
}

#[cfg(feature = "mcp-attach")]
async fn attach_cdp_ws_session(
    server: &McpBrowserServer,
    id: u64,
    endpoint: &str,
) -> Result<String, DynError> {
    let payload = call_tool(
        server,
        id,
        "browser_attach",
        json!({
            "mode": "cdp_ws",
            "endpoint": endpoint,
            "target_profile": "default"
        }),
    )
    .await?;

    assert_eq!(
        payload.get("supported").and_then(Value::as_bool),
        Some(true),
        "cdp_ws attach should return supported=true when connect succeeds"
    );

    session_id_from_payload(&payload)
}

#[tokio::test]
#[ignore = "requires Chrome and external network"]
async fn mcp_acquire_navigate_release_round_trip() -> Result<(), Box<dyn std::error::Error>> {
    let pool = BrowserPool::new(test_config()).await?;
    let server = McpBrowserServer::new(pool);

    let acquire_resp = server
        .dispatch(&json!({
            "jsonrpc": "2.0",
            "id": 1,
            "method": "tools/call",
            "params": {
                "name": "browser_acquire",
                "arguments": {
                    "stealth_level": "advanced",
                    "target_profile": "default"
                }
            }
        }))
        .await;
    let acquire_payload = parse_tools_call_text(&acquire_resp)?;
    let session_id = acquire_payload
        .get("session_id")
        .and_then(Value::as_str)
        .ok_or_else(|| std::io::Error::other("tool response returned no session_id"))?
        .to_string();

    let navigate_resp = server
        .dispatch(&json!({
            "jsonrpc": "2.0",
            "id": 2,
            "method": "tools/call",
            "params": {
                "name": "browser_navigate",
                "arguments": {
                    "session_id": session_id,
                    "url": "https://example.com",
                    "timeout_secs": 30
                }
            }
        }))
        .await;
    let navigate_payload = parse_tools_call_text(&navigate_resp)?;

    let title = navigate_payload
        .get("title")
        .and_then(Value::as_str)
        .unwrap_or_default()
        .to_lowercase();
    assert!(
        title.contains("example"),
        "expected navigation title to contain 'example', got payload: {navigate_payload}"
    );

    let release_resp = server
        .dispatch(&json!({
            "jsonrpc": "2.0",
            "id": 3,
            "method": "tools/call",
            "params": {
                "name": "browser_release",
                "arguments": {
                    "session_id": session_id
                }
            }
        }))
        .await;
    let release_payload = parse_tools_call_text(&release_resp)?;

    assert_eq!(
        release_payload.get("released").and_then(Value::as_bool),
        Some(true),
        "browser_release should return released=true; got payload: {release_payload}"
    );

    Ok(())
}

#[tokio::test]
#[ignore = "requires Chrome and external network"]
async fn mcp_browser_acquire_and_extract_returns_structured_payload() -> Result<(), DynError> {
    let pool = BrowserPool::new(test_config()).await?;
    let server = McpBrowserServer::new(pool);

    let payload = call_tool(
        &server,
        200,
        "browser_acquire_and_extract",
        json!({
            "url": "https://example.com",
            "mode": "resilient",
            "total_timeout_secs": 20
        }),
    )
    .await?;

    assert!(payload.get("success").is_some());
    assert!(payload.get("strategy_used").is_some());
    assert!(payload.get("final_url").is_some());
    assert!(payload.get("status_code").is_some());
    assert!(payload.get("diagnostics").is_some());

    let diagnostics = payload
        .get("diagnostics")
        .ok_or_else(|| std::io::Error::other("missing diagnostics bundle"))?;
    assert!(
        diagnostics
            .get("attempted")
            .and_then(Value::as_array)
            .is_some(),
        "diagnostics.attempted should be an array"
    );
    assert!(
        diagnostics
            .get("failures")
            .and_then(Value::as_array)
            .is_some(),
        "diagnostics.failures should be an array"
    );

    Ok(())
}

#[tokio::test]
#[ignore = "requires Chrome and external network"]
async fn mcp_session_save_restore_and_humanize_round_trip() -> Result<(), Box<dyn std::error::Error>>
{
    let pool = BrowserPool::new(test_config()).await?;
    let server = McpBrowserServer::new(pool);

    let session_id = acquire_session(&server, 11, "reddit").await?;

    let _ = call_tool(
        &server,
        12,
        "browser_navigate",
        json!({
            "session_id": session_id,
            "url": "https://example.com",
            "timeout_secs": 30
        }),
    )
    .await?;

    assert_session_save_ttl(&server, 13, &session_id, 3600).await?;
    assert_session_restore_saved(&server, 14, &session_id).await?;
    assert_humanize_applied(&server, 15, &session_id).await?;

    #[cfg(feature = "mcp-attach")]
    {
        assert_attach_contract(&server, 16).await?;
    }

    assert_auth_session_mode(&server, 17, &session_id, "capture", Some(1800)).await?;
    assert_auth_session_mode(&server, 18, &session_id, "resume", None).await?;

    let release_payload = release_session(&server, 19, &session_id).await?;
    assert_eq!(
        release_payload.get("released").and_then(Value::as_bool),
        Some(true),
        "browser_release should return released=true"
    );

    Ok(())
}

#[cfg(feature = "mcp-attach")]
#[tokio::test]
#[ignore = "requires STYGIAN_ATTACH_WS_ENDPOINT and reachable DevTools websocket"]
async fn mcp_attach_cdp_ws_navigate_release_round_trip() -> Result<(), Box<dyn std::error::Error>> {
    let endpoint = match std::env::var("STYGIAN_ATTACH_WS_ENDPOINT") {
        Ok(v) if !v.trim().is_empty() => v,
        _ => {
            eprintln!(
                "Skipping mcp_attach_cdp_ws_navigate_release_round_trip: STYGIAN_ATTACH_WS_ENDPOINT is not set"
            );
            return Ok(());
        }
    };

    let pool = BrowserPool::new(test_config()).await?;
    let server = McpBrowserServer::new(pool);

    let session_id = attach_cdp_ws_session(&server, 31, &endpoint).await?;

    let navigate_payload = call_tool(
        &server,
        32,
        "browser_navigate",
        json!({
            "session_id": session_id,
            "url": "https://example.com",
            "timeout_secs": 30
        }),
    )
    .await?;
    let title = navigate_payload
        .get("title")
        .and_then(Value::as_str)
        .unwrap_or_default()
        .to_lowercase();
    assert!(
        title.contains("example"),
        "expected attached session navigation title to contain 'example', got payload: {navigate_payload}"
    );

    let release_payload = release_session(&server, 33, &session_id).await?;
    assert_eq!(
        release_payload.get("released").and_then(Value::as_bool),
        Some(true),
        "browser_release should return released=true for attached session"
    );

    Ok(())
}