browsing 0.1.4

Lightweight MCP/API for browser automation: navigate, get content (text), screenshot. Parallelism via RwLock.
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
//! Tests for error recovery and retry logic

use browsing::browser::cdp::CdpClient;
use browsing::error::BrowsingError;
use std::time::Duration;

const MAX_RETRY_ATTEMPTS: u32 = 3;

#[tokio::test]
async fn test_cdp_retry_logic_transient_error() {
    // This test would verify that transient errors are retried
    // Since we can't easily mock the CDP client, we document the expected behavior

    // Expected behavior:
    // 1. Transient errors (connection lost, no response) should trigger retries
    // 2. Retry should use exponential backoff
    // 3. After MAX_RETRY_ATTEMPTS, should return RetryLimitExceeded error

    let mut client = CdpClient::new("ws://localhost:9222".to_string());

    // Start the client (will fail since no browser is running)
    let result: Result<(), BrowsingError> = client.start().await;

    // Should get a CDP error (connection failed)
    assert!(result.is_err());

    if let Err(BrowsingError::Cdp(msg)) = result {
        assert!(msg.contains("Failed to connect") || msg.contains("CDP"));
    } else {
        panic!("Expected CDP error");
    }
}

#[tokio::test]
async fn test_cdp_retry_limit_exceeded() {
    // Test that retry limit is respected when connection is not established
    let client = CdpClient::new("ws://localhost:9999".to_string());

    // Try to send a command without starting the client
    // This will be treated as a connection error and will be retried
    let result: Result<serde_json::Value, BrowsingError> = client
        .send_command(
            "Page.navigate",
            serde_json::json!({"url": "https://example.com"}),
        )
        .await;

    // Should fail (no browser running)
    assert!(result.is_err());

    // The error should be a CDP error (not RetryLimitExceeded since connection failed immediately)
    match result {
        Err(BrowsingError::Cdp(_)) => {
            // Expected - connection failed without retries
        }
        Err(BrowsingError::RetryLimitExceeded(attempts, _)) => {
            // Also acceptable if retries were attempted
            assert!(attempts <= MAX_RETRY_ATTEMPTS + 1);
        }
        _ => {
            panic!("Expected CDP or RetryLimitExceeded error");
        }
    }
}

#[test]
fn test_backoff_delay_calculation() {
    // Test that backoff delay follows exponential pattern
    const INITIAL_RETRY_DELAY_MS: u64 = 100;
    const MAX_RETRY_DELAY_MS: u64 = 5000;

    let delays: Vec<u64> = (0..5)
        .map(|attempt| {
            let delay = INITIAL_RETRY_DELAY_MS * 2_u64.pow(attempt);
            delay.min(MAX_RETRY_DELAY_MS)
        })
        .collect();

    // Expected: 100, 200, 400, 800, 1600
    assert_eq!(delays[0], 100);
    assert_eq!(delays[1], 200);
    assert_eq!(delays[2], 400);
    assert_eq!(delays[3], 800);
    assert_eq!(delays[4], 1600);

    // Cap at MAX_RETRY_DELAY_MS (5000)
    let delay_10 = INITIAL_RETRY_DELAY_MS * 2_u64.pow(10);
    assert_eq!(delay_10.min(MAX_RETRY_DELAY_MS), MAX_RETRY_DELAY_MS);
}

#[test]
fn test_retryable_error_classification() {
    // Test that errors are correctly classified as retryable or not

    // Retryable errors
    let retryable_errors = vec![
        BrowsingError::Cdp("Failed to send command".to_string()),
        BrowsingError::Cdp("No response received".to_string()),
        BrowsingError::Cdp("connection lost".to_string()),
        BrowsingError::Cdp("WebSocket closed".to_string()),
        BrowsingError::Cdp("Target not found".to_string()),
        BrowsingError::Cdp("Session not found".to_string()),
    ];

    let client = CdpClient::new("ws://localhost:9222".to_string());

    for error in retryable_errors {
        assert!(
            client.is_retryable_error(&error),
            "Error should be retryable: {}",
            error
        );
    }

    // Non-retryable errors
    let non_retryable_errors = vec![
        BrowsingError::Config("Invalid config".to_string()),
        BrowsingError::Io(std::io::Error::new(std::io::ErrorKind::NotFound, "test")),
        BrowsingError::Browser("Browser crashed".to_string()),
        BrowsingError::Validation("Invalid input".to_string()),
    ];

    for error in non_retryable_errors {
        assert!(
            !client.is_retryable_error(&error),
            "Error should not be retryable: {}",
            error
        );
    }
}

#[test]
fn test_error_display() {
    // Test that all error types display correctly
    let errors = vec![
        BrowsingError::Config("config error".to_string()),
        BrowsingError::Cdp("CDP error".to_string()),
        BrowsingError::Browser("browser error".to_string()),
        BrowsingError::Llm("LLM error".to_string()),
        BrowsingError::Agent("agent error".to_string()),
        BrowsingError::Dom("DOM error".to_string()),
        BrowsingError::Tool("tool error".to_string()),
        BrowsingError::Validation("validation error".to_string()),
        BrowsingError::RetryLimitExceeded(3, "test error".to_string()),
        BrowsingError::ConnectionLost("connection lost".to_string()),
    ];

    for error in errors {
        let display = format!("{}", error);
        assert!(!display.is_empty());
        assert!(display.len() > 10);
    }
}

#[test]
fn test_metrics_edge_cases() {
    use browsing::metrics::Metrics;

    // Test with zero operations
    let mut metrics = Metrics::new();
    let summary = metrics.summary();

    assert!(summary.cdp_avg_time.is_none());
    assert!(summary.cdp_success_rate.is_none());
    assert!(summary.dom_avg_time.is_none());
    assert!(summary.dom_avg_elements.is_none());
    assert!(summary.nav_avg_time.is_none());
    assert!(summary.screenshot_avg_time.is_none());

    // Test with single operation
    metrics.record_cdp_command("test", Duration::from_millis(100), true);
    let summary = metrics.summary();

    assert_eq!(summary.cdp_avg_time, Some(Duration::from_millis(100)));
    assert_eq!(summary.cdp_success_rate, Some(100.0));

    // Test with all failures
    let mut fail_metrics = Metrics::new();
    fail_metrics.record_cdp_command("test", Duration::from_millis(100), false);
    fail_metrics.record_cdp_command("test", Duration::from_millis(100), false);
    fail_metrics.record_cdp_command("test", Duration::from_millis(100), false);

    let summary = fail_metrics.summary();
    assert_eq!(summary.cdp_success_rate, Some(0.0));
}

#[tokio::test]
async fn test_xpath_selector_generation() {
    use browsing::dom::serializer::DOMTreeSerializer;
    use browsing::dom::views::{EnhancedDOMTreeNode, NodeType};
    use std::collections::HashMap;

    // Test XPath generation with different element attributes
    let mut node = EnhancedDOMTreeNode {
        node_id: 1,
        backend_node_id: 1,
        node_type: NodeType::ElementNode,
        node_name: "button".to_string(),
        node_value: String::new(),
        attributes: HashMap::new(),
        is_scrollable: None,
        is_visible: None,
        absolute_position: None,
        target_id: String::new(),
        frame_id: None,
        session_id: None,
        content_document: None,
        shadow_root_type: None,
        shadow_roots: None,
        parent_node: None,
        children_nodes: None,
        ax_node: None,
        snapshot_node: None,
        uuid: "test-uuid".to_string(),
    };

    let serializer = DOMTreeSerializer::new(node.clone());

    // Test with id attribute
    node.attributes
        .insert("id".to_string(), "submit-btn".to_string());
    let xpath = serializer.generate_xpath_selector(&node);
    assert_eq!(xpath, "//*[@id='submit-btn']");

    // Test with name attribute
    node.attributes.clear();
    node.attributes
        .insert("name".to_string(), "username".to_string());
    node.node_name = "input".to_string();
    let xpath = serializer.generate_xpath_selector(&node);
    assert_eq!(xpath, "//input[@name='username']");

    // Test with data-testid
    node.attributes.clear();
    node.attributes
        .insert("data-testid".to_string(), "login-button".to_string());
    node.node_name = "button".to_string();
    let xpath = serializer.generate_xpath_selector(&node);
    assert_eq!(xpath, "//*[@data-testid='login-button']");

    // Test with special characters in id
    node.attributes.clear();
    node.attributes
        .insert("id".to_string(), "test'quote".to_string());
    let xpath = serializer.generate_xpath_selector(&node);
    assert!(xpath.contains("\"test'quote\""));
}

#[test]
fn test_proxy_config_parsing() {
    use browsing::config::parse_proxy_config_from_env;

    // Test with no proxy environment variables
    unsafe { std::env::remove_var("HTTP_PROXY") };
    unsafe { std::env::remove_var("HTTPS_PROXY") };
    unsafe { std::env::remove_var("NO_PROXY") };
    let proxy = parse_proxy_config_from_env();
    assert!(proxy.is_none());

    // Test with HTTP_PROXY
    unsafe { std::env::set_var("HTTP_PROXY", "http://proxy.example.com:8080") };
    let proxy = parse_proxy_config_from_env();
    assert!(proxy.is_some());
    assert_eq!(proxy.unwrap().server, "http://proxy.example.com:8080");

    // Test with HTTP_PROXY (first match is returned)
    unsafe { std::env::set_var("HTTP_PROXY", "http://proxy.example.com:8080") };
    unsafe { std::env::set_var("HTTPS_PROXY", "https://secure.proxy.com:3128") };
    let proxy = parse_proxy_config_from_env();
    assert!(proxy.is_some());
    assert_eq!(proxy.unwrap().server, "http://proxy.example.com:8080");

    // Test with NO_PROXY
    unsafe { std::env::set_var("HTTP_PROXY", "http://proxy.example.com:8080") };
    unsafe { std::env::set_var("NO_PROXY", "localhost,127.0.0.1,.example.com") };
    let proxy = parse_proxy_config_from_env();
    assert!(proxy.is_some());
    assert_eq!(
        proxy.unwrap().bypass,
        Some("localhost,127.0.0.1,.example.com".to_string())
    );

    // Test with authentication
    unsafe { std::env::set_var("HTTP_PROXY", "http://proxy.example.com:8080") };
    unsafe { std::env::set_var("PROXY_USERNAME", "testuser") };
    unsafe { std::env::set_var("PROXY_PASSWORD", "testpass") };
    let proxy = parse_proxy_config_from_env();
    assert!(proxy.is_some());
    let config = proxy.unwrap();
    assert_eq!(config.username, Some("testuser".to_string()));
    assert_eq!(config.password, Some("testpass".to_string()));

    // Clean up
    unsafe { std::env::remove_var("HTTP_PROXY") };
    unsafe { std::env::remove_var("HTTPS_PROXY") };
    unsafe { std::env::remove_var("NO_PROXY") };
    unsafe { std::env::remove_var("PROXY_USERNAME") };
    unsafe { std::env::remove_var("PROXY_PASSWORD") };
}

#[test]
fn test_config_validation() {
    use browsing::config::{AgentConfig, BrowserProfile, Config, LlmConfig};

    // Test valid configuration
    let config = Config {
        browser_profile: BrowserProfile {
            headless: Some(true),
            user_data_dir: None,
            allowed_domains: None,
            downloads_path: None,
            proxy: None,
        },
        llm: LlmConfig {
            api_key: Some("test-api-key-12345".to_string()),
            model: Some("gpt-4".to_string()),
            temperature: Some(0.7),
            max_tokens: Some(1000),
        },
        agent: AgentConfig {
            max_steps: Some(100),
            use_vision: Some(false),
            system_prompt: None,
        },
    };

    assert!(config.validate().is_ok());

    // Test empty API key
    let invalid_config = Config {
        llm: LlmConfig {
            api_key: Some("".to_string()),
            ..Default::default()
        },
        ..Default::default()
    };

    let errors = invalid_config.validate();
    assert!(errors.is_err());
    assert!(
        errors
            .unwrap_err()
            .iter()
            .any(|e| e.contains("API key is empty"))
    );

    // Test API key too short
    let invalid_config = Config {
        llm: LlmConfig {
            api_key: Some("short".to_string()),
            ..Default::default()
        },
        ..Default::default()
    };

    let errors = invalid_config.validate();
    assert!(errors.is_err());
    assert!(errors.unwrap_err().iter().any(|e| e.contains("too short")));

    // Test temperature out of range
    let invalid_config = Config {
        llm: LlmConfig {
            temperature: Some(3.0),
            ..Default::default()
        },
        ..Default::default()
    };

    let errors = invalid_config.validate();
    assert!(errors.is_err());
    assert!(
        errors
            .unwrap_err()
            .iter()
            .any(|e| e.contains("out of range"))
    );

    // Test max_steps zero
    let invalid_config = Config {
        agent: AgentConfig {
            max_steps: Some(0),
            ..Default::default()
        },
        ..Default::default()
    };

    let errors = invalid_config.validate();
    assert!(errors.is_err());
    assert!(
        errors
            .unwrap_err()
            .iter()
            .any(|e| e.contains("cannot be zero"))
    );

    // Test empty allowed domains
    let invalid_config = Config {
        browser_profile: BrowserProfile {
            allowed_domains: Some(vec![]),
            ..Default::default()
        },
        ..Default::default()
    };

    let errors = invalid_config.validate();
    assert!(errors.is_err());
    assert!(errors.unwrap_err().iter().any(|e| e.contains("empty")));

    // Test invalid proxy URL
    let invalid_config = Config {
        browser_profile: BrowserProfile {
            proxy: Some(browsing::browser::profile::ProxyConfig {
                server: "invalid-url".to_string(),
                bypass: None,
                username: None,
                password: None,
            }),
            ..Default::default()
        },
        ..Default::default()
    };

    let errors = invalid_config.validate();
    assert!(errors.is_err());
    assert!(
        errors
            .unwrap_err()
            .iter()
            .any(|e| e.contains("Invalid proxy URL"))
    );

    // Test validate_or_error
    let invalid_config = Config {
        llm: LlmConfig {
            api_key: Some("".to_string()),
            ..Default::default()
        },
        ..Default::default()
    };

    assert!(invalid_config.validate_or_error().is_err());
}