qrawl 0.6.0

Rust toolkit to crawl web data for AI agents
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
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
#![cfg(test)]
mod tests {
    use crate::tools::clean::utils::{
        canonicalize_domain, canonicalize_url, clean_email, clean_phone, decode_html_entities,
    };
    use crate::tools::clean::*;

    // Tests for clean_text()

    #[test]
    fn test_html_entities_named() {
        assert_eq!(clean_text("&"), "&");
        assert_eq!(clean_text("&lt;"), "<");
        assert_eq!(clean_text("&gt;"), ">");
        assert_eq!(clean_text("&quot;"), "\"");
        assert_eq!(clean_text("&apos;"), "'");
        // nbsp is decoded but then normalized by whitespace normalization
        assert_eq!(clean_text("&nbsp;"), "");
    }

    #[test]
    fn test_html_entities_numeric() {
        assert_eq!(clean_text("&#39;"), "'");
        assert_eq!(clean_text("&#x27;"), "'");
        assert_eq!(clean_text("&#34;"), "\"");
        assert_eq!(clean_text("&#x22;"), "\"");
    }

    #[test]
    fn test_html_entities_combined() {
        assert_eq!(clean_text("&lt;div&gt;"), "<div>");
        assert_eq!(clean_text("Tom &amp; Jerry"), "Tom & Jerry");
        assert_eq!(
            clean_text("It&#39;s &quot;great&quot;"),
            "It's \"great\""
        );
    }

    #[test]
    fn test_unicode_normalization() {
        // Precomposed vs decomposed characters
        let precomposed = "\u{00E9}"; // é (single character)
        let decomposed = "e\u{0301}"; // e + combining acute accent

        // After cleaning, both should be the same
        assert_eq!(clean_text(precomposed), clean_text(decomposed));
        assert_eq!(clean_text(precomposed), "é");
    }

    #[test]
    fn test_zero_width_characters() {
        assert_eq!(clean_text("hello\u{200B}world"), "helloworld");
        assert_eq!(clean_text("test\u{200C}ing"), "testing");
        assert_eq!(clean_text("word\u{200D}join"), "wordjoin");
        assert_eq!(clean_text("\u{FEFF}text"), "text");
    }

    #[test]
    fn test_control_characters() {
        // Should remove most control characters
        assert_eq!(clean_text("hello\x00world"), "helloworld");
        assert_eq!(clean_text("test\x01ing"), "testing");

        // But keep newlines and tabs (then normalized to space)
        assert_eq!(clean_text("line1\nline2"), "line1 line2"); // Normalized to space
        assert_eq!(clean_text("tab\there"), "tab here"); // Normalized to space
    }

    #[test]
    fn test_clean_text_handles_crlf_mixture() {
        let mixed = "First\r\nSecond\rThird\tFourth";
        assert_eq!(clean_text(mixed), "First Second Third Fourth");
    }

    #[test]
    fn test_whitespace_normalization() {
        assert_eq!(clean_text("hello   world"), "hello world");
        assert_eq!(clean_text("  trim  me  "), "trim me");
        assert_eq!(clean_text("multiple\n\n\nlines"), "multiple lines");
        assert_eq!(clean_text("lots\t\t\tof\t\ttabs"), "lots of tabs");
        assert_eq!(
            clean_text("  leading and trailing  "),
            "leading and trailing"
        );
    }

    #[test]
    fn test_combined_cleaning() {
        let dirty = "Hello &amp; &#39;world&#39;   with   \u{200B}spaces\x00";
        assert_eq!(clean_text(dirty), "Hello & 'world' with spaces");
    }

    #[test]
    fn test_real_world_examples() {
        // Recipe title with entities
        assert_eq!(
            clean_text("Ben &amp; Jerry&#39;s Ice Cream"),
            "Ben & Jerry's Ice Cream"
        );

        // Description with extra whitespace
        assert_eq!(
            clean_text("there   are   too   many   spaces!"),
            "there are too many spaces!"
        );

        // Mixed issues
        assert_eq!(
            clean_text("  &lt;b&gt;Bold&lt;/b&gt;   text  "),
            "<b>Bold</b> text"
        );
    }

    #[test]
    fn test_empty_and_whitespace() {
        assert_eq!(clean_text(""), "");
        assert_eq!(clean_text("   "), "");
        assert_eq!(clean_text("\n\n\n"), "");
        assert_eq!(clean_text("\t\t\t"), "");
    }

    #[test]
    fn test_no_changes_needed() {
        assert_eq!(clean_text("perfect text"), "perfect text");
        assert_eq!(clean_text("no entities here"), "no entities here");
    }

    #[test]
    fn test_preserves_intentional_characters() {
        // Should not remove these
        assert_eq!(clean_text("hello-world"), "hello-world");
        assert_eq!(clean_text("under_score"), "under_score");
        assert_eq!(clean_text("with.period"), "with.period");
        assert_eq!(clean_text("a/b/c"), "a/b/c");
    }

    #[test]
    fn test_unicode_characters() {
        // Emoji and other unicode should be preserved
        assert_eq!(clean_text("Hello 👋 World 🌍"), "Hello 👋 World 🌍");
        assert_eq!(clean_text("Café"), "Café");
        assert_eq!(clean_text("日本語"), "日本語");
    }

    // Tests for clean_html()

    #[tokio::test]
    async fn test_clean_html_removes_comments() {
        assert_eq!(
            clean_html("<div><!-- comment --><p>Text</p></div>").await,
            "<div><p>Text</p></div>"
        );
        assert_eq!(
            clean_html("<!-- start --><p>Content</p><!-- end -->").await,
            "<p>Content</p>"
        );
    }

    #[tokio::test]
    async fn test_clean_html_multiline_comments() {
        assert_eq!(
            clean_html("<div><!--\nmultiline\ncomment\n--><p>Text</p></div>").await,
            "<div><p>Text</p></div>"
        );
    }

    #[tokio::test]
    async fn test_clean_html_normalizes_whitespace() {
        assert_eq!(
            clean_html("<div>  <p>Text</p>  </div>").await,
            "<div> <p>Text</p> </div>"
        );
    }

    #[tokio::test]
    async fn test_clean_html_removes_empty_lines() {
        assert_eq!(
            clean_html("<div>\n\n<p>Text</p>\n\n</div>").await,
            "<div> <p>Text</p> </div>"
        );
    }

    #[tokio::test]
    async fn test_clean_html_combined() {
        let dirty = "<div>  <!-- comment -->  \n\n<p>Text</p>  \n\n  </div>";
        let expected = "<div> <p>Text</p> </div>";
        assert_eq!(clean_html(dirty).await, expected);
    }

    #[tokio::test]
    async fn test_clean_html_removes_scripts() {
        assert_eq!(
            clean_html("<div><script>alert('hi')</script><p>Text</p></div>").await,
            "<div><p>Text</p></div>"
        );
        assert_eq!(
            clean_html("<script src='app.js'></script><p>Content</p>").await,
            "<p>Content</p>"
        );
    }

    #[tokio::test]
    async fn test_clean_html_removes_styles() {
        assert_eq!(
            clean_html("<div><style>.red{color:red}</style><p>Text</p></div>").await,
            "<div><p>Text</p></div>"
        );
    }

    #[tokio::test]
    async fn test_clean_html_removes_noscript_iframe_svg() {
        assert_eq!(
            clean_html("<noscript>Enable JS</noscript><p>Text</p>").await,
            "<p>Text</p>"
        );
        assert_eq!(
            clean_html("<iframe src='ad.html'></iframe><p>Text</p>").await,
            "<p>Text</p>"
        );
        assert_eq!(
            clean_html("<svg><circle/></svg><p>Text</p>").await,
            "<p>Text</p>"
        );
    }

    #[tokio::test]
    async fn test_clean_html_removes_junk_attributes() {
        assert_eq!(
            clean_html("<div class='container' id='main' style='color:red'>Text</div>").await,
            "<div>Text</div>"
        );
        assert_eq!(
            clean_html("<button onclick='alert()' data-id='123'>Click</button>").await,
            "<button>Click</button>"
        );
        assert_eq!(
            clean_html("<div aria-label='info' role='button'>Text</div>").await,
            "<div>Text</div>"
        );
    }

    #[tokio::test]
    async fn test_clean_html_preserves_jsonld() {
        let html = r#"<script>alert('bad')</script><script type="application/ld+json">{"@context":"schema.org"}</script><p>Text</p>"#;
        let cleaned = clean_html(html).await;
        assert!(cleaned.contains(r#"<script type="application/ld+json">"#));
        assert!(cleaned.contains(r#"{"@context":"schema.org"}"#));
        assert!(!cleaned.contains("alert('bad')"));
    }

    #[tokio::test]
    async fn test_clean_html_handles_uppercase_tags() {
        let html = "<DIV><SCRIPT>alert('hi')</SCRIPT><STYLE>body{}</STYLE><P>Text</P></DIV>";
        assert_eq!(clean_html(html).await, "<DIV><P>Text</P></DIV>");
    }

    #[tokio::test]
    async fn test_clean_html_preserves_jsonld_case_variants() {
        let html = r#"<SCRIPT TYPE="APPLICATION/LD+JSON">{"@type":"Thing"}</SCRIPT><p>Text</p>"#;
        let cleaned = clean_html(html).await;
        assert!(cleaned
            .to_lowercase()
            .contains(r#"<script type="application/ld+json">"#));
        assert!(cleaned.contains(r#"{"@type":"Thing"}"#));
    }

    #[tokio::test]
    async fn test_clean_html_normalizes_escaped_newlines() {
        let html = "<div>Line\\nBreak</div>";
        assert_eq!(clean_html(html).await, "<div>Line Break</div>");
    }

    // Tests for clean_urls()

    #[test]
    fn test_clean_urls_exact_duplicates() {
        let urls = vec![
            "https://example.com".to_string(),
            "https://example.com".to_string(),
            "https://example.com".to_string(),
        ];
        let cleaned = clean_urls(&urls);
        assert_eq!(cleaned.len(), 1);
        assert_eq!(cleaned[0], "https://example.com");
    }

    #[test]
    fn test_clean_urls_protocol_normalization() {
        let urls = vec![
            "http://example.com".to_string(),
            "https://example.com".to_string(),
        ];
        let cleaned = clean_urls(&urls);
        assert_eq!(cleaned.len(), 1); // Both normalize to https
    }

    #[test]
    fn test_clean_urls_case_normalization() {
        let urls = vec![
            "https://Example.com".to_string(),
            "https://EXAMPLE.COM".to_string(),
            "https://example.com".to_string(),
        ];
        let cleaned = clean_urls(&urls);
        assert_eq!(cleaned.len(), 1);
    }

    #[test]
    fn test_clean_urls_www_stripping() {
        let urls = vec![
            "https://www.example.com".to_string(),
            "https://example.com".to_string(),
        ];
        let cleaned = clean_urls(&urls);
        assert_eq!(cleaned.len(), 1);
    }

    #[test]
    fn test_clean_urls_trailing_slash() {
        let urls = vec![
            "https://example.com/path".to_string(),
            "https://example.com/path/".to_string(),
        ];
        let cleaned = clean_urls(&urls);
        assert_eq!(cleaned.len(), 1);
    }

    #[test]
    fn test_clean_urls_query_param_order() {
        let urls = vec![
            "https://example.com?b=2&a=1".to_string(),
            "https://example.com?a=1&b=2".to_string(),
        ];
        let cleaned = clean_urls(&urls);
        assert_eq!(cleaned.len(), 1);
    }

    #[test]
    fn test_clean_urls_fragment_removal() {
        let urls = vec![
            "https://example.com/page#section1".to_string(),
            "https://example.com/page#section2".to_string(),
            "https://example.com/page".to_string(),
        ];
        let cleaned = clean_urls(&urls);
        assert_eq!(cleaned.len(), 1);
    }

    #[test]
    fn test_clean_urls_combined() {
        let urls = vec![
            "https://example.com/path".to_string(),
            "HTTP://www.example.com/path/".to_string(),
            "https://EXAMPLE.COM/path".to_string(),
            "http://example.com/path#frag".to_string(),
        ];
        let cleaned = clean_urls(&urls);
        assert_eq!(cleaned.len(), 1); // All canonicalize to same URL
    }

    #[test]
    fn test_clean_urls_preserves_order() {
        let urls = vec![
            "https://example.com/first".to_string(),
            "https://example.com/second".to_string(),
            "https://example.com/first".to_string(), // Duplicate
        ];
        let cleaned = clean_urls(&urls);
        assert_eq!(cleaned.len(), 2);
        assert_eq!(cleaned[0], "https://example.com/first");
        assert_eq!(cleaned[1], "https://example.com/second");
    }

    #[test]
    fn test_clean_urls_returns_canonical() {
        // Returns canonical URL
        let urls = vec!["HTTP://Example.com/Path".to_string()];
        let cleaned = clean_urls(&urls);
        assert_eq!(cleaned[0], "https://example.com/Path");
    }

    #[test]
    fn test_clean_urls_canonicalizes_idna_domains() {
        let urls = vec!["https://münich.com/path".to_string()];
        let cleaned = clean_urls(&urls);
        assert_eq!(cleaned[0], "https://xn--mnich-kva.com/path");
    }

    #[test]
    fn test_clean_urls_malformed() {
        let urls = vec![
            "not-a-url".to_string(),
            "https://example.com".to_string(),
            "also-not-url".to_string(),
        ];
        let cleaned = clean_urls(&urls);
        assert_eq!(cleaned.len(), 3); // Malformed URLs kept as-is, all different
    }

    #[test]
    fn test_clean_urls_empty_list() {
        let urls: Vec<String> = vec![];
        let cleaned = clean_urls(&urls);
        assert_eq!(cleaned.len(), 0);
    }

    // Tests for clean_emails()

    #[test]
    fn test_clean_emails_deduplication() {
        let emails = vec![
            " john@example.com ".to_string(),
            "John@Example.COM".to_string(),
            "\"John Doe\" <john@example.com>".to_string(),
        ];
        let cleaned = clean_emails(&emails);
        assert_eq!(cleaned.len(), 1);
        assert_eq!(cleaned[0], "john@example.com");
    }

    #[test]
    fn test_clean_emails_with_punctuation() {
        // Test CLI use case where emails have trailing punctuation
        let emails = vec![
            "john@example.com,".to_string(),
            "john@example.com".to_string(),
        ];
        let cleaned = clean_emails(&emails);
        assert_eq!(cleaned.len(), 1);
        assert_eq!(cleaned[0], "john@example.com");
    }

    #[test]
    fn test_clean_emails_filters_empty() {
        let emails = vec![
            "john@example.com".to_string(),
            "".to_string(),
            "jane@example.com".to_string(),
            "   ".to_string(),
        ];
        let cleaned = clean_emails(&emails);
        assert_eq!(cleaned.len(), 2);
    }

    #[test]
    fn test_clean_emails_preserves_order() {
        let emails = vec![
            "first@example.com".to_string(),
            "second@example.com".to_string(),
            "first@example.com".to_string(), // Duplicate
        ];
        let cleaned = clean_emails(&emails);
        assert_eq!(cleaned.len(), 2);
        assert_eq!(cleaned[0], "first@example.com");
        assert_eq!(cleaned[1], "second@example.com");
    }

    #[test]
    fn test_clean_emails_empty_list() {
        let emails: Vec<String> = vec![];
        let cleaned = clean_emails(&emails);
        assert_eq!(cleaned.len(), 0);
    }

    // Tests for clean_phones()

    #[test]
    fn test_clean_phones_deduplication() {
        let phones = vec![
            "(555) 123-4567".to_string(),
            "555-123-4567".to_string(),
            "555.123.4567".to_string(),
        ];
        let cleaned = clean_phones(&phones);
        assert_eq!(cleaned.len(), 1);
        assert_eq!(cleaned[0], "5551234567");
    }

    #[test]
    fn test_clean_phones_filters_empty() {
        let phones = vec![
            "555-123-4567".to_string(),
            "".to_string(),
            "555-987-6543".to_string(),
            "   ".to_string(),
        ];
        let cleaned = clean_phones(&phones);
        assert_eq!(cleaned.len(), 2);
    }

    #[test]
    fn test_clean_phones_preserves_order() {
        let phones = vec![
            "555-123-4567".to_string(),
            "555-987-6543".to_string(),
            "(555) 123-4567".to_string(), // Duplicate
        ];
        let cleaned = clean_phones(&phones);
        assert_eq!(cleaned.len(), 2);
        assert_eq!(cleaned[0], "5551234567");
        assert_eq!(cleaned[1], "5559876543");
    }

    #[test]
    fn test_clean_phones_with_extensions() {
        let phones = vec![
            "555-123-4567 ext. 123".to_string(),
            "555-123-4567 x456".to_string(),
        ];
        let cleaned = clean_phones(&phones);
        assert_eq!(cleaned.len(), 1); // Same after stripping extensions
        assert_eq!(cleaned[0], "5551234567");
    }

    #[test]
    fn test_clean_phones_international_vs_local() {
        // International and local versions should be treated as different
        let phones = vec!["+1-555-123-4567".to_string(), "555-123-4567".to_string()];
        let cleaned = clean_phones(&phones);
        assert_eq!(cleaned.len(), 2);
        assert_eq!(cleaned[0], "+15551234567");
        assert_eq!(cleaned[1], "5551234567");
    }

    #[test]
    fn test_clean_phones_empty_list() {
        let phones: Vec<String> = vec![];
        let cleaned = clean_phones(&phones);
        assert_eq!(cleaned.len(), 0);
    }

    // Tests for utility functions

    #[test]
    fn test_decode_html_entities() {
        assert_eq!(decode_html_entities("&amp;"), "&");
        assert_eq!(decode_html_entities("&lt;&gt;"), "<>");
        assert_eq!(decode_html_entities("&quot;"), "\"");
        assert_eq!(decode_html_entities("&#39;"), "'");
        assert_eq!(decode_html_entities("&#x27;"), "'");
        assert_eq!(decode_html_entities("&nbsp;"), "\u{00A0}");
    }

    #[test]
    fn test_canonicalize_domain() {
        // Basic lowercase
        assert_eq!(canonicalize_domain("Example.com"), "example.com");
        assert_eq!(canonicalize_domain("GITHUB.COM"), "github.com");

        // Strip www
        assert_eq!(canonicalize_domain("www.example.com"), "example.com");
        assert_eq!(canonicalize_domain("WWW.EXAMPLE.COM"), "example.com");

        // Combined
        assert_eq!(canonicalize_domain("WWW.GitHub.com"), "github.com");

        // Don't strip www if it's the whole domain
        assert_eq!(canonicalize_domain("www"), "www");

        // Subdomains preserved
        assert_eq!(canonicalize_domain("api.example.com"), "api.example.com");
        assert_eq!(
            canonicalize_domain("www.api.example.com"),
            "api.example.com"
        );
    }

    #[test]
    fn test_canonicalize_url() {
        // Protocol normalization
        assert_eq!(
            canonicalize_url("http://example.com"),
            "https://example.com"
        );

        assert_eq!(canonicalize_url("example.com"), "https://example.com");

        assert_eq!(canonicalize_url("www.example.com"), "https://example.com");

        // Domain canonicalization
        assert_eq!(
            canonicalize_url("https://WWW.Example.COM"),
            "https://example.com"
        );

        // Trailing slash normalization
        assert_eq!(
            canonicalize_url("https://example.com/path/"),
            "https://example.com/path"
        );
        assert_eq!(
            canonicalize_url("https://example.com/"),
            "https://example.com"
        ); // Root slash stripped

        // Query parameter sorting
        assert_eq!(
            canonicalize_url("https://example.com?b=2&a=1"),
            "https://example.com/?a=1&b=2"
        );

        // Fragment removal
        assert_eq!(
            canonicalize_url("https://example.com/page#section"),
            "https://example.com/page"
        );

        // Combined
        assert_eq!(
            canonicalize_url("HTTP://www.Example.COM/path/?b=2&a=1#frag"),
            "https://example.com/path?a=1&b=2"
        );

        // Malformed URLs kept as-is
        assert_eq!(canonicalize_url("not-a-url"), "not-a-url");
    }

    #[test]
    fn test_canonical_url_new_canonicalizes() {
        let url = CanonicalUrl::new("HTTP://www.Example.COM/path/?utm_source=x&id=7");
        assert_eq!(url.as_str(), "https://example.com/path?id=7");
    }

    #[test]
    fn test_canonical_url_idempotent() {
        let once = CanonicalUrl::new("https://Example.com/");
        let twice = CanonicalUrl::new(once.as_str());
        assert_eq!(once, twice);
    }

    #[test]
    fn test_canonical_url_serialize_transparent() {
        let url = CanonicalUrl::new("https://example.com");
        let json = serde_json::to_string(&url).unwrap();
        // Bare string, not a wrapper object.
        assert_eq!(json, "\"https://example.com\"");
    }

    #[test]
    fn test_canonical_url_deserialize_strict() {
        // Non-canonical input gets canonicalized on deserialize.
        let url: CanonicalUrl =
            serde_json::from_str("\"HTTP://www.Example.COM/path/?utm_source=x\"").unwrap();
        assert_eq!(url.as_str(), "https://example.com/path");
    }

    #[test]
    fn test_canonical_url_from_str_and_string() {
        let a: CanonicalUrl = "https://Example.com/".into();
        let b: CanonicalUrl = String::from("https://Example.com/").into();
        assert_eq!(a, b);
        assert_eq!(a.as_str(), "https://example.com");
    }

    #[test]
    fn test_canonicalize_url_strips_tracking_params() {
        // utm_* family stripped entirely
        assert_eq!(
            canonicalize_url("https://example.com?utm_source=x&utm_medium=y"),
            "https://example.com"
        );
        // tracking mixed with real params — real params preserved and sorted
        assert_eq!(
            canonicalize_url("https://example.com?utm_source=x&id=7"),
            "https://example.com/?id=7"
        );
        // each exact-match tracking param
        for param in &[
            "fbclid", "gclid", "mc_eid", "mc_cid", "_ga", "igshid", "ref_src", "ref_url",
        ] {
            assert_eq!(
                canonicalize_url(&format!("https://example.com?{}=abc&foo=1", param)),
                "https://example.com/?foo=1",
                "tracking param {} should be stripped",
                param
            );
        }
        // look-alikes NOT stripped
        assert_eq!(
            canonicalize_url("https://example.com?referral=abc"),
            "https://example.com/?referral=abc"
        );
        assert_eq!(
            canonicalize_url("https://example.com?utmx=abc"),
            "https://example.com/?utmx=abc"
        );
        // bare `ref` is intentionally preserved (legitimate query param on
        // many sites — e.g. GitHub `?ref=branch`)
        assert_eq!(
            canonicalize_url("https://example.com?ref=branch"),
            "https://example.com/?ref=branch"
        );
        // two "same" URLs differing only by trackers canonicalize identically
        assert_eq!(
            canonicalize_url("https://example.com/recipe?fbclid=abc"),
            canonicalize_url("https://example.com/recipe?utm_source=fb"),
        );
    }

    #[test]
    fn test_clean_email() {
        // Trim whitespace
        assert_eq!(clean_email(" john@example.com "), "john@example.com");

        // Lowercase
        assert_eq!(clean_email("John@Example.COM"), "john@example.com");

        // URL decode
        assert_eq!(clean_email("john%40example.com"), "john@example.com");

        // Extract from display name
        assert_eq!(
            clean_email("\"John Doe\" <john@example.com>"),
            "john@example.com"
        );
        assert_eq!(
            clean_email("John Doe <john@example.com>"),
            "john@example.com"
        );

        // Strip trailing punctuation
        assert_eq!(clean_email("john@example.com,"), "john@example.com");
        assert_eq!(clean_email("john@example.com;"), "john@example.com");
        assert_eq!(clean_email("john@example.com."), "john@example.com");

        // Combined
        assert_eq!(
            clean_email(" \"John\" <John@Example.COM> "),
            "john@example.com"
        );

        // Already clean
        assert_eq!(clean_email("john@example.com"), "john@example.com");
    }

    #[test]
    fn test_clean_phone() {
        // Strip separators
        assert_eq!(clean_phone("(555) 123-4567"), "5551234567");
        assert_eq!(clean_phone("555-123-4567"), "5551234567");
        assert_eq!(clean_phone("555.123.4567"), "5551234567");

        // Keep international prefix
        assert_eq!(clean_phone("+1-555-123-4567"), "+15551234567");
        assert_eq!(clean_phone("+1 (555) 123-4567"), "+15551234567");

        // Strip extensions
        assert_eq!(clean_phone("555-123-4567 ext. 123"), "5551234567");
        assert_eq!(clean_phone("555-123-4567 x123"), "5551234567");
        assert_eq!(clean_phone("555-123-4567 extension 123"), "5551234567");

        // Trim whitespace
        assert_eq!(clean_phone(" 555-123-4567 "), "5551234567");

        // Already clean
        assert_eq!(clean_phone("5551234567"), "5551234567");
    }
}