crawlrs 0.2.0

crawlrs - Enterprise-grade web scraping and crawling platform built with Rust
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
// Copyright (c) 2025-2026 Kirky.X🌠
// SPDX-License-Identifier: Apache-2.0

//! URL 归一化器
//!
//! 将等价 URL 归一为同一规范串,便于 Bloom/Interner/DB 三层去重命中。
//!
//! ## 归一规则
//!
//! - scheme 小写(`HTTPS` → `https`)
//! - host 小写(`Example.COM` → `example.com`)
//! - 去除 fragment(`#section`)
//! - 统一 trailing slash:根路径 `/` 保留;非根路径末尾的 `/` 去除
//! - query 按键排序(同键多值保持原相对顺序,稳定排序)
//! - `strip_query = true` 时直接去除 query 段
//!
//! ## Permutations
//!
//! 生成等价 URL 变体集合用于"全量去重检查"——任何变体命中 bloom/DB 都视为已爬。
//! 变体维度(笛卡尔积):
//! - www/non-www:自动加/去 `www.` 前缀
//! - http/https:切换协议
//! - index.html:路径末尾追加/去除 `index.html`、`index.htm`、`index.php`
//!
//! ## 错误显性化
//!
//! `normalize` 解析失败返回 [`UrlError::InvalidUrl`],不静默返回原串。

use crate::utils::url::UrlError;
use std::collections::BTreeMap;
use url::Url;

/// URL 归一化器
///
/// 状态仅 `strip_query` 一个布尔,无副作用,可 `Clone + Send + Sync`。
#[derive(Debug, Clone, Copy, Default)]
pub struct UrlNormalizer {
    /// 是否去除 query 段(默认保留)
    strip_query: bool,
}

impl UrlNormalizer {
    /// 创建归一化器
    ///
    /// # 参数
    ///
    /// - `strip_query`:`true` 时归一结果去除 query 段
    pub fn new(strip_query: bool) -> Self {
        Self { strip_query }
    }

    /// 默认配置(保留 query)
    pub fn with_default() -> Self {
        Self::new(false)
    }

    /// 归一化 URL
    ///
    /// # 参数
    ///
    /// - `url`:待归一化的 URL 字符串
    ///
    /// # 返回
    ///
    /// 成功返回归一化后的字符串;解析失败返回 [`UrlError::InvalidUrl`]。
    ///
    /// # 示例
    ///
    /// ```
    /// use crawlrs::utils::url_normalizer::UrlNormalizer;
    /// let n = UrlNormalizer::new(false);
    /// assert_eq!(
    ///     n.normalize("HTTPS://Example.COM/Path/?b=2&a=1#frag").unwrap(),
    ///     "https://example.com/Path?a=1&b=2"
    ///);
    /// ```
    pub fn normalize(&self, url: &str) -> Result<String, UrlError> {
        // 空串防御
        let trimmed = url.trim();
        if trimmed.is_empty() {
            return Err(UrlError::InvalidUrl("empty URL".to_string()));
        }

        // 解析(base = None,无法解析相对路径)
        // 仅接受 http/https
        let mut normalized = parse_and_validate_http_url(trimmed)?;

        // 重组(克隆以拥有可变 Url)
        normalized.set_fragment(None);

        // host 小写(scheme 已是小写,Url::parse 保证)
        lowercase_host(&mut normalized)?;

        // 路径 trailing slash 统一:非根路径去除末尾 /
        normalize_path(&mut normalized);

        // query 处理:strip、剥已知 tracking 参数、或排序
        self.normalize_query(&mut normalized);

        Ok(normalized.to_string())
    }

    /// query 处理:strip、剥已知 tracking 参数、或排序
    fn normalize_query(&self, normalized: &mut Url) {
        if self.strip_query {
            normalized.set_query(None);
        } else if let Some(q) = normalized.query() {
            let filtered = Self::strip_known_tracking(q);
            let sorted = Self::sort_query(&filtered);
            normalized.set_query(Some(&sorted));
        }
    }

    /// 已知 tracking 参数(剥除清单,保守集)
    ///
    /// `utm_` 为前缀匹配(utm_source/medium/campaign/term/content 等),
    /// 其余为各平台点击追踪的精确键名。
    const TRACKING_EXACT_KEYS: &[&str] = &[
        "fbclid", "gclid", "msclkid", "dclid", "twclid", "mc_eid", "_ga",
    ];

    /// 剥除已知 tracking 参数后重排(serde_urlencoded 解析失败时原样返回,
    /// 与 sort_query 的回退语义一致)
    fn strip_known_tracking(query: &str) -> String {
        let pairs: Vec<(String, String)> = match serde_urlencoded::from_str(query) {
            Ok(pairs) => pairs,
            Err(_) => return query.to_string(),
        };
        let kept: Vec<(String, String)> = pairs
            .into_iter()
            .filter(|(k, _)| {
                !k.starts_with("utm_") && !Self::TRACKING_EXACT_KEYS.contains(&k.as_str())
            })
            .collect();
        serde_urlencoded::to_string(&kept).unwrap_or_else(|_| query.to_string())
    }

    /// 排序 query 字符串
    ///
    /// 同键多值保持原相对顺序(稳定排序),仅按 key 排序。
    /// 例如 `?b=2&a=1&b=3` → `?a=1&b=2&b=3`。
    ///
    /// serde_urlencoded 解析失败时保留原始 query 串(不静默丢弃),
    /// 避免 query 不同的 URL 被误判为等价。
    fn sort_query(query: &str) -> String {
        // serde_urlencoded 解析为 Vec<(String, String)>,保持多值顺序
        // 解析失败时返回原始 query(不吞错为空 Vec)
        let pairs: Vec<(String, String)> = match serde_urlencoded::from_str(query) {
            Ok(p) => p,
            Err(_) => return query.to_string(),
        };

        // 用 BTreeMap 按 key 分组(保持插入顺序)
        let mut grouped: BTreeMap<String, Vec<String>> = BTreeMap::new();
        for (k, v) in pairs {
            grouped.entry(k).or_default().push(v);
        }

        // 重新组装(预分配容量,避免多次 realloc)
        let total_pairs: usize = grouped.values().map(|v| v.len()).sum();
        let mut parts: Vec<String> = Vec::with_capacity(total_pairs);
        for (k, vs) in grouped {
            for v in vs {
                parts.push(format!("{}={}", k, v));
            }
        }
        parts.join("&")
    }

    /// 生成等价 URL 变体集合
    ///
    /// 用于"全量去重检查"——任何变体命中 bloom/DB 都视为已爬。
    ///
    /// # 参数
    ///
    /// - `url`:原始 URL 字符串(不要求已归一化)
    ///
    /// # 返回
    ///
    /// 返回所有等价变体(含原 URL 归一化形式)。解析失败时返回仅含原串的 Vec
    /// (permutations 是"尽力而为"的扩展,原串始终保留)。
    ///
    /// # 变体维度
    ///
    /// - www/non-www:自动加/去 `www.` 前缀
    /// - http/https:切换协议
    /// - index.html:路径末尾追加 `index.html`/`index.htm`/`index.php`
    ///
    /// # 示例
    ///
    /// ```
    /// use crawlrs::utils::url_normalizer::UrlNormalizer;
    /// let n = UrlNormalizer::new(false);
    /// let perms = n.permutations("https://example.com/path");
    /// // 包含 https://example.com/path, https://www.example.com/path,
    /// // http://example.com/path, http://www.example.com/path,
    /// // https://example.com/path/index.html, ...
    /// assert!(perms.len() >= 4);
    /// ```
    pub fn permutations(&self, url: &str) -> Vec<String> {
        // 解析失败时回退原串(不静默失败,但 permutations 是尽力而为)
        let parsed = match Url::parse(url.trim()) {
            Ok(u) => u,
            Err(_) => return vec![url.to_string()],
        };

        // 仅 http/https 才做变体;其他协议原样返回
        if !matches!(parsed.scheme(), "http" | "https") {
            return vec![url.to_string()];
        }

        let mut variants = Self::expand_variants(&parsed);

        // 去重(无堆分配:sort + dedup 比 HashSet 更快)
        variants.sort_unstable();
        variants.dedup();

        variants
    }

    /// www/non-www × http/https × index 后缀的笛卡尔积变体。
    ///
    /// 笛卡尔积上界:2 schemes × 2 hosts × 4 path_variants = 16。
    fn expand_variants(parsed: &Url) -> Vec<String> {
        let mut variants: Vec<String> = Vec::with_capacity(16);
        let host = parsed.host_str().unwrap_or("");
        let path = parsed.path();
        let query = parsed.query();

        // 维度 1:www/non-www
        let host_variants: Vec<String> = if let Some(rest) = host.strip_prefix("www.") {
            vec![host.to_string(), rest.to_string()] // [www.example.com, example.com]
        } else {
            vec![host.to_string(), format!("www.{}", host)] // [example.com, www.example.com]
        };

        // 维度 2:http/https
        let scheme_variants = ["https", "http"];

        // 维度 3:index 变体
        let path_variants = Self::path_variants(path);

        // 笛卡尔积
        for scheme in scheme_variants {
            for host_v in &host_variants {
                for path_v in &path_variants {
                    let mut candidate = Url::parse(&format!("{}://{}{}", scheme, host_v, path_v))
                        .unwrap_or_else(|_| parsed.clone());
                    candidate.set_query(query);
                    candidate.set_fragment(None);
                    variants.push(candidate.to_string());
                }
            }
        }

        variants
    }

    /// 路径变体:原路径 + 各 index 后缀
    fn path_variants(path: &str) -> Vec<String> {
        // 上界:1 原路径 + 3 index 后缀 = 4
        let mut variants: Vec<String> = Vec::with_capacity(4);
        variants.push(path.to_string());

        // 已是 /xxx/index.html 时,去除 index.html 也作为变体
        for suffix in ["/index.html", "/index.htm", "/index.php"] {
            if let Some(stripped) = path.strip_suffix(suffix) {
                let stripped = if stripped.is_empty() { "/" } else { stripped };
                variants.push(stripped.to_string());
            } else {
                variants.push(format!("{}{}", path, suffix));
            }
        }

        // 去重(无堆分配)
        variants.sort_unstable();
        variants.dedup();
        variants
    }
}

/// 解析 URL 并校验 scheme(仅接受 http/https)。
fn parse_and_validate_http_url(trimmed: &str) -> Result<Url, UrlError> {
    // 解析(base = None,无法解析相对路径)
    let parsed =
        Url::parse(trimmed).map_err(|e| UrlError::InvalidUrl(format!("parse failed: {e}")))?;

    // 仅接受 http/https
    if !matches!(parsed.scheme(), "http" | "https") {
        return Err(UrlError::InvalidUrl(format!(
            "unsupported scheme: {} (only http/https)",
            parsed.scheme()
        )));
    }
    Ok(parsed)
}

/// host 小写。
///
/// 注意:set_host 会重新解析,但能正确处理 IDN 等边界情况;
/// set_host 失败时返回错误(显性化)。
fn lowercase_host(normalized: &mut Url) -> Result<(), UrlError> {
    if let Some(host) = normalized.host_str() {
        let lower = host.to_lowercase();
        if lower != host {
            normalized
                .set_host(Some(&lower))
                .map_err(|e| UrlError::InvalidUrl(format!("set_host failed: {e}")))?;
        }
    }
    Ok(())
}

/// 路径 trailing slash 统一:非根路径去除末尾 `/`。
fn normalize_path(normalized: &mut Url) {
    let path = normalized.path().to_string();
    if path.len() > 1 && path.ends_with('/') {
        // set_path 不接受空串,根路径用 "/"
        let new_path = path.trim_end_matches('/');
        normalized.set_path(if new_path.is_empty() { "/" } else { new_path });
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    // normalize: scheme/host lowercase

    #[test]
    fn test_normalize_lowercase_scheme_and_host() {
        let n = UrlNormalizer::new(false);
        assert_eq!(
            n.normalize("HTTPS://Example.COM/Path").unwrap(),
            "https://example.com/Path"
        );
    }

    #[test]
    fn test_normalize_preserves_path_case() {
        // 路径大小写保留(URL 路径区分大小写)
        let n = UrlNormalizer::new(false);
        assert_eq!(
            n.normalize("https://example.com/CamelCasePath").unwrap(),
            "https://example.com/CamelCasePath"
        );
    }

    // normalize: fragment

    #[test]
    fn test_normalize_strips_fragment() {
        let n = UrlNormalizer::new(false);
        assert_eq!(
            n.normalize("https://example.com/path#section").unwrap(),
            "https://example.com/path"
        );
    }

    #[test]
    fn test_normalize_strips_fragment_with_query() {
        let n = UrlNormalizer::new(false);
        assert_eq!(
            n.normalize("https://example.com/path?a=1#frag").unwrap(),
            "https://example.com/path?a=1"
        );
    }

    // normalize: trailing slash

    #[test]
    fn test_normalize_removes_trailing_slash() {
        let n = UrlNormalizer::new(false);
        assert_eq!(
            n.normalize("https://example.com/path/").unwrap(),
            "https://example.com/path"
        );
    }

    #[test]
    fn test_normalize_preserves_root_slash() {
        let n = UrlNormalizer::new(false);
        assert_eq!(
            n.normalize("https://example.com/").unwrap(),
            "https://example.com/"
        );
    }

    #[test]
    fn test_normalize_no_path_implicit_root() {
        let n = UrlNormalizer::new(false);
        assert_eq!(
            n.normalize("https://example.com").unwrap(),
            "https://example.com/"
        );
    }

    #[test]
    fn test_normalize_multiple_trailing_slashes() {
        let n = UrlNormalizer::new(false);
        // 多个 trailing slash 全部去除
        assert_eq!(
            n.normalize("https://example.com/path//").unwrap(),
            "https://example.com/path"
        );
    }

    // normalize: query sorting

    #[test]
    fn test_normalize_sorts_query() {
        let n = UrlNormalizer::new(false);
        assert_eq!(
            n.normalize("https://example.com/path?b=2&a=1").unwrap(),
            "https://example.com/path?a=1&b=2"
        );
    }

    #[test]
    fn test_normalize_sorts_query_stable_multi_values() {
        let n = UrlNormalizer::new(false);
        // b=2 在 b=3 之前保持原相对顺序
        assert_eq!(
            n.normalize("https://example.com/path?b=2&a=1&b=3").unwrap(),
            "https://example.com/path?a=1&b=2&b=3"
        );
    }

    #[test]
    fn test_normalize_equivalent_urls_to_same_string() {
        let n = UrlNormalizer::new(false);
        let a = n
            .normalize("HTTPS://Example.COM/Path/?b=2&a=1#frag")
            .unwrap();
        let b = n.normalize("https://example.com/Path?a=1&b=2").unwrap();
        assert_eq!(a, b);
    }

    // normalize: strip_query

    #[test]
    fn test_normalize_strip_query_true() {
        let n = UrlNormalizer::new(true);
        assert_eq!(
            n.normalize("https://example.com/path?a=1&b=2").unwrap(),
            "https://example.com/path"
        );
    }

    #[test]
    fn test_normalize_strip_query_false_keeps_query() {
        let n = UrlNormalizer::new(false);
        assert_eq!(
            n.normalize("https://example.com/path?a=1&b=2").unwrap(),
            "https://example.com/path?a=1&b=2"
        );
    }

    #[test]
    fn test_normalize_strip_query_equivalent_urls() {
        let n = UrlNormalizer::new(true);
        // strip_query 时,query 不同的 URL 视为等价
        let a = n.normalize("https://example.com/path?a=1").unwrap();
        let b = n.normalize("https://example.com/path?b=2").unwrap();
        assert_eq!(a, b);
        assert_eq!(a, "https://example.com/path");
    }

    // normalize: error cases

    #[test]
    fn test_normalize_empty_url_returns_err() {
        let n = UrlNormalizer::new(false);
        assert!(n.normalize("").is_err());
    }

    #[test]
    fn test_normalize_whitespace_only_returns_err() {
        let n = UrlNormalizer::new(false);
        assert!(n.normalize("   ").is_err());
    }

    #[test]
    fn test_normalize_invalid_url_returns_err() {
        let n = UrlNormalizer::new(false);
        // Url::parse 对 "not a url" 解析失败
        assert!(n.normalize("not a url").is_err());
    }

    #[test]
    fn test_normalize_unsupported_scheme_returns_err() {
        let n = UrlNormalizer::new(false);
        // ftp 不在 http/https 白名单
        let result = n.normalize("ftp://example.com/file");
        // Url 能解析 ftp,但我们拒绝
        assert!(result.is_err() || result.is_ok());
        // 如果解析成功且 scheme 是 ftp,应返回 Err
        if let Ok(s) = result {
            assert!(
                s.starts_with("http"),
                "non-http should be rejected, got: {s}"
            );
        }
    }

    // normalize: port handling

    #[test]
    fn test_normalize_preserves_port() {
        let n = UrlNormalizer::new(false);
        assert_eq!(
            n.normalize("https://example.com:8080/path").unwrap(),
            "https://example.com:8080/path"
        );
    }

    // normalize: idempotency

    #[test]
    fn test_normalize_idempotent() {
        let n = UrlNormalizer::new(false);
        let once = n
            .normalize("HTTPS://Example.COM/Path/?b=2&a=1#frag")
            .unwrap();
        let twice = n.normalize(&once).unwrap();
        assert_eq!(once, twice);
    }

    // permutations

    #[test]
    fn test_permutations_includes_original() {
        let n = UrlNormalizer::new(false);
        let perms = n.permutations("https://example.com/path");
        assert!(perms.iter().any(|u| u.contains("example.com/path")));
    }

    #[test]
    fn test_permutations_www_and_non_www() {
        let n = UrlNormalizer::new(false);
        let perms = n.permutations("https://example.com/path");
        // 应包含 example.com 和 www.example.com 两个 host
        assert!(
            perms.iter().any(|u| u.contains("://example.com")),
            "missing non-www"
        );
        assert!(
            perms.iter().any(|u| u.contains("://www.example.com")),
            "missing www"
        );
    }

    #[test]
    fn test_permutations_http_and_https() {
        let n = UrlNormalizer::new(false);
        let perms = n.permutations("https://example.com/path");
        let has_https = perms.iter().any(|u| u.starts_with("https://"));
        let has_http = perms.iter().any(|u| u.starts_with("http://"));
        assert!(has_https, "missing https variant");
        assert!(has_http, "missing http variant");
    }

    #[test]
    fn test_permutations_index_html() {
        let n = UrlNormalizer::new(false);
        let perms = n.permutations("https://example.com/path");
        // 应包含 path/index.html 变体
        assert!(
            perms.iter().any(|u| u.contains("/path/index.html")),
            "missing index.html variant"
        );
        assert!(
            perms.iter().any(|u| u.contains("/path/index.htm")),
            "missing index.htm variant"
        );
    }

    #[test]
    fn test_permutations_strips_index_html() {
        let n = UrlNormalizer::new(false);
        let perms = n.permutations("https://example.com/path/index.html");
        // 应包含去除 index.html 后的变体
        assert!(
            perms
                .iter()
                .any(|u| u == "https://example.com/path" || u == "https://example.com/path/"),
            "missing stripped index.html variant, got: {:?}",
            perms
        );
    }

    #[test]
    fn test_permutations_from_www_includes_non_www() {
        let n = UrlNormalizer::new(false);
        let perms = n.permutations("https://www.example.com/path");
        // 应包含去 www 后的变体
        assert!(
            perms
                .iter()
                .any(|u| u.contains("://example.com/path") && !u.contains("www.")),
            "missing non-www variant"
        );
    }

    #[test]
    fn test_permutations_deduplicates() {
        let n = UrlNormalizer::new(false);
        let perms = n.permutations("https://example.com/");
        // 不应有重复
        let mut sorted = perms.clone();
        sorted.sort();
        let original_len = perms.len();
        sorted.dedup();
        assert_eq!(sorted.len(), original_len, "duplicates found");
    }

    #[test]
    fn test_permutations_invalid_url_returns_original() {
        let n = UrlNormalizer::new(false);
        let perms = n.permutations("not a url");
        assert_eq!(perms, vec!["not a url".to_string()]);
    }

    #[test]
    fn test_permutations_non_http_scheme_returns_original() {
        let n = UrlNormalizer::new(false);
        let perms = n.permutations("ftp://example.com/file");
        assert_eq!(perms, vec!["ftp://example.com/file".to_string()]);
    }

    #[test]
    fn test_permutations_preserves_query() {
        let n = UrlNormalizer::new(false);
        let perms = n.permutations("https://example.com/path?a=1");
        // 所有变体都应保留 query
        for p in &perms {
            assert!(p.contains("?a=1"), "missing query in variant: {}", p);
        }
    }

    #[test]
    fn test_permutations_count_at_least_4() {
        // 至少 2 schemes × 2 hosts = 4 个基础变体
        let n = UrlNormalizer::new(false);
        let perms = n.permutations("https://example.com/path");
        assert!(
            perms.len() >= 4,
            "expected >= 4 variants, got: {}",
            perms.len()
        );
    }

    // UrlNormalizer construction

    #[test]
    fn test_new_default_keeps_query() {
        let n = UrlNormalizer::with_default();
        assert_eq!(
            n.normalize("https://example.com/path?a=1").unwrap(),
            "https://example.com/path?a=1"
        );
    }

    #[test]
    fn test_clone_copy() {
        let n1 = UrlNormalizer::new(true);
        let n2 = n1; // Copy
        assert_eq!(
            n1.normalize("https://example.com/path?a=1").unwrap(),
            n2.normalize("https://example.com/path?a=1").unwrap()
        );
    }

    // r2 F5:已知 tracking 参数剥离

    #[test]
    fn test_strip_tracking_utm_prefixed_keys() {
        let n = UrlNormalizer::with_default();
        let a = n
            .normalize("https://example.com/page?utm_source=x&id=1")
            .unwrap();
        let b = n.normalize("https://example.com/page?id=1").unwrap();
        assert_eq!(a, b, "utm_* variants must normalize to the same key");
        assert!(!a.contains("utm_"), "utm params must be stripped, got {a}");
        assert!(
            a.contains("id=1"),
            "non-tracking params must survive, got {a}"
        );
    }

    #[test]
    fn test_strip_tracking_exact_keys() {
        let n = UrlNormalizer::with_default();
        let a = n
            .normalize("https://example.com/p?fbclid=abc&gclid=def&id=2")
            .unwrap();
        assert!(!a.contains("fbclid") && !a.contains("gclid"), "got {a}");
        assert!(a.contains("id=2"), "got {a}");
    }

    #[test]
    fn test_malformed_query_falls_back_without_panic() {
        let n = UrlNormalizer::with_default();
        // serde_urlencoded 解析失败的畸形 query:保持原样参与归一,不 panic
        let result = n.normalize("https://example.com/p?%%%bad&id=3");
        assert!(result.is_ok(), "malformed query must not error");
    }
}