browsing 0.1.3

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
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
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
//! Comprehensive tests for utilities module
//!
//! These tests cover:
//! - URL extraction and validation
//! - Domain pattern matching
//! - Signal handling for graceful shutdown
//! - Various utility functions

use browsing::utils::signal::{is_shutdown_requested, set_shutdown_requested, SignalHandler};
use browsing::utils::{extract_urls, match_url_with_domain_pattern};

// ============================================================================
// URL Extraction Tests
// ============================================================================

#[test]
fn test_extract_urls_basic() {
    let text = "Visit https://example.com for more info";
    let urls = extract_urls(text);

    assert!(!urls.is_empty());
    assert!(urls.iter().any(|u| u.contains("example.com")));
}

#[test]
fn test_extract_urls_multiple() {
    let text = "Check out https://example.com and http://test.org/page";
    let urls = extract_urls(text);

    assert!(urls.len() >= 2);
    assert!(urls.iter().any(|u| u.contains("example.com")));
    assert!(urls.iter().any(|u| u.contains("test.org")));
}

#[test]
fn test_extract_urls_with_path() {
    let text = "Visit https://example.com/path/to/page?query=value";
    let urls = extract_urls(text);

    assert!(!urls.is_empty());
    assert!(urls.iter().any(|u| u.contains("example.com")));
    assert!(urls.iter().any(|u| u.contains("/path/to/page")));
}

#[test]
fn test_extract_urls_with_www() {
    let text = "Go to https://www.example.com";
    let urls = extract_urls(text);

    assert!(!urls.is_empty());
    assert!(urls.iter().any(|u| u.contains("www.example.com")));
}

#[test]
fn test_extract_urls_with_port() {
    let text = "Connect to http://localhost:8080/api";
    let urls = extract_urls(text);

    assert!(!urls.is_empty());
    assert!(urls.iter().any(|u| u.contains("localhost:8080")));
}

#[test]
fn test_extract_urls_with_fragment() {
    let text = "See https://example.com/page#section";
    let urls = extract_urls(text);

    assert!(!urls.is_empty());
    assert!(urls.iter().any(|u| u.contains("#section")));
}

#[test]
fn test_extract_urls_no_urls() {
    let text = "This is just plain text with no URLs";
    let urls = extract_urls(text);

    assert!(urls.is_empty());
}

#[test]
fn test_extract_urls_empty_string() {
    let text = "";
    let urls = extract_urls(text);

    assert!(urls.is_empty());
}

#[test]
fn test_extract_urls_with_punctuation() {
    let text = "Visit https://example.com, then go to http://test.org!";
    let urls = extract_urls(text);

    assert!(urls.len() >= 2);
    // URLs should be extracted without trailing punctuation
}

#[test]
fn test_extract_urls_mixed_case() {
    let text = "Go to HTTPS://EXAMPLE.COM and Http://Test.Org";
    let urls = extract_urls(text);

    assert!(urls.len() >= 2);
    // URLs should be normalized to lowercase
}

#[test]
fn test_extract_urls_with_credentials() {
    let text = "Use https://user:pass@example.com for authenticated access";
    let urls = extract_urls(text);

    assert!(!urls.is_empty());
    assert!(urls.iter().any(|u| u.contains("user:pass")));
}

#[test]
fn test_extract_urls_ipv4() {
    let text = "Connect to http://192.168.1.1:8080";
    let urls = extract_urls(text);

    assert!(!urls.is_empty());
    assert!(urls.iter().any(|u| u.contains("192.168.1.1")));
}

#[test]
fn test_extract_urls_ipv6() {
    let text = "Visit http://[2001:db8::1]:8080";
    let urls = extract_urls(text);

    // May or may not be extracted depending on regex
}

#[test]
fn test_extract_urls_with_parameters() {
    let text = "Go to https://example.com?param1=value1&param2=value2";
    let urls = extract_urls(text);

    assert!(!urls.is_empty());
}

#[test]
fn test_extract_urls_from_markdown() {
    let text = "[Link](https://example.com) and another [Link](http://test.org)";
    let urls = extract_urls(text);

    assert!(urls.len() >= 2);
}

#[test]
fn test_extract_urls_from_html() {
    let text = "Visit https://example.com for more info";
    let urls = extract_urls(text);

    assert!(!urls.is_empty());
    assert!(urls.iter().any(|u| u.contains("example.com")));
}

// ============================================================================
// Domain Pattern Matching Tests
// ============================================================================

#[test]
fn test_match_url_exact_domain() {
    assert!(match_url_with_domain_pattern(
        "https://example.com",
        "example.com"
    ));
}

#[test]
fn test_match_url_exact_domain_with_path() {
    assert!(match_url_with_domain_pattern(
        "https://example.com/path/to/page",
        "example.com"
    ));
}

#[test]
fn test_match_url_wildcard_subdomain() {
    assert!(match_url_with_domain_pattern(
        "https://www.example.com",
        "*.example.com"
    ));
    assert!(match_url_with_domain_pattern(
        "https://api.example.com",
        "*.example.com"
    ));
}

#[test]
fn test_match_url_wildcard_no_match_base() {
    assert!(!match_url_with_domain_pattern(
        "https://example.com",
        "*.example.com"
    ));
}

#[test]
fn test_match_url_wildcard_multiple_subdomains() {
    assert!(match_url_with_domain_pattern(
        "https://sub.api.example.com",
        "*.example.com"
    ));
}

#[test]
fn test_match_url_protocol_pattern() {
    assert!(match_url_with_domain_pattern(
        "https://example.com",
        "http*://example.com"
    ));
    assert!(match_url_with_domain_pattern(
        "http://example.com",
        "http*://example.com"
    ));
}

#[test]
fn test_match_url_protocol_pattern_no_match() {
    assert!(!match_url_with_domain_pattern(
        "ftp://example.com",
        "http*://example.com"
    ));
}

#[test]
fn test_match_url_exact_protocol() {
    assert!(match_url_with_domain_pattern(
        "https://example.com",
        "https://example.com"
    ));
}

#[test]
fn test_match_url_protocol_no_match() {
    assert!(!match_url_with_domain_pattern(
        "http://example.com",
        "https://example.com"
    ));
}

#[test]
fn test_match_url_empty_pattern() {
    assert!(!match_url_with_domain_pattern("https://example.com", ""));
}

#[test]
fn test_match_url_empty_url() {
    assert!(!match_url_with_domain_pattern("", "example.com"));
}

#[test]
fn test_match_url_both_empty() {
    assert!(!match_url_with_domain_pattern("", ""));
}

#[test]
fn test_match_url_invalid_url() {
    assert!(!match_url_with_domain_pattern("not-a-url", "example.com"));
}

#[test]
fn test_match_url_with_port() {
    assert!(match_url_with_domain_pattern(
        "https://example.com:8080",
        "example.com"
    ));
}

#[test]
fn test_match_url_subdomain_with_protocol() {
    assert!(match_url_with_domain_pattern(
        "https://www.example.com",
        "http*://*.example.com"
    ));
}

#[test]
fn test_match_url_complex_pattern() {
    assert!(match_url_with_domain_pattern(
        "https://api.v1.example.com",
        "*.example.com"
    ));
}

#[test]
fn test_match_url_case_sensitivity() {
    // Domain matching should be case-insensitive
    assert!(match_url_with_domain_pattern(
        "https://Example.COM",
        "example.com"
    ));
}

// ============================================================================
// Signal Handling Tests
// ============================================================================

#[test]
fn test_signal_handler_creation() {
    let handler = SignalHandler::new();
    assert!(!handler.is_shutdown_requested());
}

#[test]
fn test_signal_handler_default() {
    let handler = SignalHandler::default();
    assert!(!handler.is_shutdown_requested());
}

#[test]
fn test_signal_handler_set_shutdown() {
    let handler = SignalHandler::new();
    assert!(!handler.is_shutdown_requested());

    handler.set_shutdown();
    assert!(handler.is_shutdown_requested());
}

#[test]
fn test_global_shutdown_flag() {
    // Reset the global flag first
    set_shutdown_requested();
    assert!(is_shutdown_requested());
}

#[test]
fn test_shutdown_flag_persistence() {
    let handler1 = SignalHandler::new();
    let handler2 = SignalHandler::new();

    handler1.set_shutdown();

    // Both handlers should detect shutdown
    assert!(handler1.is_shutdown_requested());
    // handler2 won't see it because they have different flags
    // but the global flag should be set
    assert!(is_shutdown_requested());
}

// ============================================================================
// Additional Utility Tests
// ============================================================================

#[test]
fn test_url_scheme_validation() {
    let valid_schemes = vec!["http://", "https://", "ftp://", "file://"];

    for scheme in valid_schemes {
        assert!(scheme.ends_with("://"));
        assert!(scheme.len() >= 4);
    }
}

#[test]
fn test_url_components() {
    let url = "https://example.com:8080/path/to/page?query=value#fragment";

    assert!(url.contains("https://"));
    assert!(url.contains("example.com"));
    assert!(url.contains(":8080"));
    assert!(url.contains("/path/to/page"));
    assert!(url.contains("?query=value"));
    assert!(url.contains("#fragment"));
}

#[test]
fn test_domain_validation() {
    let valid_domains = vec![
        "example.com",
        "sub.example.com",
        "api.v1.example.com",
        "example.co.uk",
        "localhost",
    ];

    for domain in valid_domains {
        assert!(!domain.is_empty());
        assert!(!domain.contains("://"));
        assert!(!domain.contains("/"));
    }
}

#[test]
fn test_url_normalization() {
    // URLs should be normalized to have trailing slash or not
    let url1 = "https://example.com/";
    let url2 = "https://example.com";

    // These may or may not be equal depending on normalization
    assert!(url1.contains("https://example.com"));
}

#[test]
fn test_url_encoding() {
    let text = "Search for: café & restaurant";
    let encoded = urlencoding::encode(text);

    assert!(!encoded.contains(" "));
    assert!(!encoded.contains("&"));
}

#[test]
fn test_url_decoding() {
    let encoded = "caf%C3%A9%20%26%20restaurant";
    let decoded = urlencoding::decode(encoded);

    assert!(decoded.is_ok());
    assert_eq!(decoded.unwrap().as_ref(), "café & restaurant");
}

#[test]
fn test_regex_url_pattern() {
    use regex::Regex;

    let url_pattern = Regex::new(r"(?i)\bhttps?://[^\s]+").unwrap();

    let text = "Visit https://example.com and http://test.org";
    let matches: Vec<_> = url_pattern.find_iter(text).collect();

    assert_eq!(matches.len(), 2);
}

#[test]
fn test_text_sanitization() {
    let text = "Hello\nWorld\tTest";
    let sanitized = text.replace('\n', " ").replace('\t', " ");

    assert!(!sanitized.contains('\n'));
    assert!(!sanitized.contains('\t'));
}

#[test]
fn test_string_truncation() {
    let long_string = "a".repeat(1000);
    let truncated = &long_string[..100];

    assert_eq!(truncated.len(), 100);
    assert!(truncated.chars().all(|c| c == 'a'));
}

#[test]
fn test_whitespace_normalization() {
    let text = "Hello    World\t\tTest";
    let normalized = text.split_whitespace().collect::<Vec<_>>().join(" ");

    assert_eq!(normalized, "Hello World Test");
}

#[test]
fn test_empty_string_handling() {
    let empty = "";
    assert!(empty.is_empty());

    let whitespace_only = "   \t\n   ";
    assert!(whitespace_only.trim().is_empty());
}

// ============================================================================
// Integration Test Markers
// ============================================================================

#[test]
fn test_signal_handler_wait_for_shutdown() {
    // Test signal handler shutdown detection
    // Note: shutdown flag may have been set by other tests
    // We just verify that we can set and detect it

    // Test setting shutdown flag
    set_shutdown_requested();

    // Verify shutdown is detected
    assert!(is_shutdown_requested());

    // Note: We can't easily reset the flag in tests
    // In real scenarios, this would be handled by the system
}

#[test]
fn test_concurrent_url_extraction() {
    // Test concurrent URL extraction for thread safety
    let text1 = "Visit https://example1.com and https://example2.org";
    let text2 = "Check http://test.net/page and https://demo.io";
    let text3 = "Go to https://github.com and https://gitlab.com";

    // Collect results - need to extract each handle before joining
    let (urls1, urls2, urls3) = std::thread::scope(|s| {
        let handle1 = s.spawn(|| extract_urls(text1));
        let handle2 = s.spawn(|| extract_urls(text2));
        let handle3 = s.spawn(|| extract_urls(text3));

        let result1 = handle1.join().unwrap();
        let result2 = handle2.join().unwrap();
        let result3 = handle3.join().unwrap();

        (result1, result2, result3)
    });

    // Verify each extraction worked correctly
    assert!(urls1.len() >= 2);
    assert!(urls2.len() >= 2);
    assert!(urls3.len() >= 2);

    // Verify thread safety - all results should be consistent
    assert!(urls1.iter().any(|u| u.contains("example1.com")));
    assert!(urls2.iter().any(|u| u.contains("test.net")));
    assert!(urls3.iter().any(|u| u.contains("github.com")));
}

#[test]
fn test_url_extraction_performance() {
    // Test URL extraction performance with reasonably large dataset
    let mut large_text = String::new();

    // Create text with multiple URLs (not too large to keep test fast)
    for i in 0..100 {
        large_text.push_str(&format!("Check out https://example{}.com ", i));
        large_text.push_str(&format!("and https://test{}.org/page?q={} ", i, i));
    }

    let start = std::time::Instant::now();
    let urls = extract_urls(&large_text);
    let duration = start.elapsed();

    // Should find all URLs
    assert!(urls.len() >= 200);

    // Should complete in reasonable time (< 100ms)
    assert!(
        duration.as_millis() < 100,
        "URL extraction took too long: {:?}",
        duration
    );

    // Verify correctness of some results
    assert!(urls.iter().any(|u| u.contains("example0.com")));
    assert!(urls.iter().any(|u| u.contains("test99.org")));
}