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
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
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
// Copyright (c) 2025-2026 Kirky.X🌠
// SPDX-License-Identifier: Apache-2.0

use chardetng::{EncodingDetector, Iso2022JpDetection, Utf8Detection};
use encoding_rs::Encoding;
use log::{debug, warn};

use crate::i18n::{tr_log, tr_log_args};
use lru::LruCache;
use parking_lot::Mutex;
use std::sync::Arc;
use thiserror::Error;

/// 文本编码处理错误类型
#[derive(Error, Debug, Clone)]
pub enum TextEncodingError {
    #[error("Encoding detection failed: {0}")]
    DetectionFailed(String),

    #[error("Encoding conversion failed: {0}")]
    ConversionFailed(String),

    #[error("Unicode conversion failed: {0}")]
    UnicodeConversionFailed(String),

    #[error("Invalid encoding format: {0}")]
    InvalidEncoding(String),

    #[error("Text processing timeout")]
    ProcessingTimeout,

    #[error("Cache lock acquisition failed: {0}")]
    CacheLockError(String),
}

/// 编码检测结果
#[derive(Debug, Clone)]
pub struct EncodingDetection {
    pub encoding: String,
    pub confidence: f32,
    pub is_utf8: bool,
}

/// 文本编码处理器
#[derive(Clone)]
pub struct TextEncodingProcessor {
    encoding_cache: Arc<Mutex<LruCache<String, EncodingDetection>>>,
    short_text_threshold: usize,
}

impl Default for TextEncodingProcessor {
    fn default() -> Self {
        Self::new()
    }
}

impl TextEncodingProcessor {
    /// 创建新的文本编码处理器
    pub fn new() -> Self {
        Self {
            encoding_cache: Arc::new(Mutex::new(LruCache::new(
                std::num::NonZeroUsize::new(1000).expect("Cache size must be non-zero"),
            ))),
            short_text_threshold: 1000,
        }
    }

    /// 创建带有自定义配置的文本编码处理器
    pub fn with_config(cache_size: usize, short_text_threshold: usize) -> Self {
        Self {
            encoding_cache: Arc::new(Mutex::new(LruCache::new(
                std::num::NonZeroUsize::new(cache_size).expect("Cache size must be non-zero"),
            ))),
            short_text_threshold,
        }
    }

    /// 处理文本编码,确保输出为UTF-8格式
    pub fn process_text(&self, input: &[u8]) -> Result<String, TextEncodingError> {
        let result = if input.len() < self.short_text_threshold {
            self.process_short_text(input)?
        } else {
            self.process_long_text(input)?
        };
        Ok(self.trim_newlines(&result))
    }

    /// 处理短文本(优化性能)
    fn process_short_text(&self, input: &[u8]) -> Result<String, TextEncodingError> {
        debug!(
            "{}",
            tr_log_args("text-short-input", &[("length", input.len().to_string())],)
        );
        if let Ok(utf8_str) = std::str::from_utf8(input) {
            if self.contains_unicode_escapes(utf8_str) {
                return self.normalize_unicode(utf8_str);
            }
            return Ok(utf8_str.to_string());
        }
        let (result, _detection) = self.detect_and_convert_encoding(input);
        result
    }

    /// 处理长文本(完整处理流程)
    fn process_long_text(&self, input: &[u8]) -> Result<String, TextEncodingError> {
        debug!(
            "{}",
            tr_log_args("text-long-input", &[("length", input.len().to_string())],)
        );
        let cache_key = self.generate_cache_key(input);
        if let Some(cached_result) = self.get_cached_detection(&cache_key) {
            debug!(
                "{}",
                tr_log_args(
                    "text-cached-detection",
                    &[("detection", format!("{:?}", cached_result))],
                )
            );
            return self.apply_cached_conversion(input, &cached_result);
        }
        let (result, detection) = self.detect_and_convert_encoding(input);
        if result.is_ok() {
            self.cache_detection_result(&cache_key, &detection);
        }
        result
    }

    /// 检测文本是否包含Unicode转义序列
    fn contains_unicode_escapes(&self, text: &str) -> bool {
        text.contains("\\u") || text.contains("\\U") || text.contains("\\x")
    }

    /// 解析字符串中的Unicode转义序列
    fn parse_unicode_escapes(&self, text: &str) -> String {
        let mut result = String::new();
        let mut chars = text.chars().peekable();
        while let Some(c) = chars.next() {
            if c == '\\' {
                if chars.peek() == Some(&'u') {
                    chars.next();
                    if let Some(ch) = Self::decode_hex_escape(&mut chars, 4) {
                        result.push(ch);
                        continue;
                    }
                } else if chars.peek() == Some(&'U') {
                    chars.next();
                    if let Some(ch) = Self::decode_hex_escape(&mut chars, 8) {
                        result.push(ch);
                        continue;
                    }
                } else if chars.peek() == Some(&'x') {
                    chars.next();
                    if let Some(ch) = Self::decode_hex_escape(&mut chars, 2) {
                        result.push(ch);
                        continue;
                    }
                }
            }
            result.push(c);
        }
        result
    }

    /// 读取 `n` 个字符并按十六进制码点解码为 char
    ///
    /// 位数不足或码点非法时返回 `None`;已消费的字符不回退(与原实现一致,
    /// 解码失败时调用方仅输出转义前缀的反斜杠本身)。
    fn decode_hex_escape(
        chars: &mut std::iter::Peekable<std::str::Chars<'_>>,
        n: usize,
    ) -> Option<char> {
        // 栈上缓冲代替逐转义序列的 String 堆分配:n ∈ {2, 4, 8}(\x/\u/\U),
        // 每个 char 的 UTF-8 编码 ≤ 4 字节,8 × 4 = 32 字节必够容纳全部已读字符。
        // 语义与原实现一致:迭代器提前耗尽时按已读前缀解析(含空串、非 ASCII
        // 字符的 from_str_radix 非法 digit 错误路径均不变)。
        const MAX_HEX_CHARS: usize = 8;
        debug_assert!(n <= MAX_HEX_CHARS);
        let mut buf = [0u8; MAX_HEX_CHARS * 4];
        let mut len = 0usize;
        for _ in 0..n {
            if let Some(h) = chars.next() {
                len += h.encode_utf8(&mut buf[len..]).len();
            }
        }
        let hex_str = std::str::from_utf8(&buf[..len]).ok()?;
        let code_point = u32::from_str_radix(hex_str, 16).ok()?;
        char::from_u32(code_point)
    }

    /// Unicode字符串规范化转换
    fn normalize_unicode(&self, text: &str) -> Result<String, TextEncodingError> {
        debug!("{}", tr_log("text-unicode-escapes-detected"));
        let parsed = self.parse_unicode_escapes(text);
        debug!(
            "{}",
            tr_log_args(
                "text-unicode-escapes-parsed",
                &[("result", parsed.clone().to_string())],
            )
        );
        Ok(parsed)
    }

    /// 检测并转换编码
    fn detect_and_convert_encoding(
        &self,
        input: &[u8],
    ) -> (Result<String, TextEncodingError>, EncodingDetection) {
        debug!("{}", tr_log("text-detection-started"));
        let mut detector = EncodingDetector::new(Iso2022JpDetection::Deny);
        detector.feed(input, true);
        let encoding = detector.guess(None, Utf8Detection::Allow);
        let encoding_name = encoding.name();
        let is_utf8 = encoding_name == "utf-8";
        let confidence = if is_utf8 { 1.0 } else { 0.9 };
        let detection = EncodingDetection {
            encoding: encoding_name.to_string(),
            confidence,
            is_utf8,
        };
        debug!(
            "{}",
            tr_log_args(
                "text-encoding-detected",
                &[
                    ("encoding", encoding_name.to_string()),
                    ("confidence", format!("{:.2}", confidence)),
                ],
            )
        );
        if is_utf8 {
            let result = std::str::from_utf8(input)
                .map(|s| s.to_string())
                .map_err(|e| TextEncodingError::ConversionFailed(e.to_string()));
            return (result, detection);
        }
        let result = self.convert_encoding(input, encoding, confidence);
        (result, detection)
    }

    /// 去除字符串的首位换行符
    pub fn trim_newlines(&self, text: &str) -> String {
        let mut result = text.to_string();
        while result.starts_with('\n') {
            result = result[1..].to_string();
        }
        while result.ends_with('\n') && !result.is_empty() {
            result.pop();
        }
        result
    }

    /// 转换编码到UTF-8
    fn convert_encoding(
        &self,
        input: &[u8],
        encoding: &'static Encoding,
        confidence: f32,
    ) -> Result<String, TextEncodingError> {
        if confidence < 0.3 {
            warn!(
                "{}",
                tr_log_args(
                    "text-detection-low-confidence",
                    &[("confidence", format!("{:.2}", confidence))],
                )
            );
        }
        let (decoded, _, had_errors) = encoding.decode(input);
        if had_errors && confidence < 0.5 {
            return Err(TextEncodingError::ConversionFailed(format!(
                "Encoding conversion error, detected encoding: {}, confidence: {:.2}",
                encoding.name(),
                confidence
            )));
        }
        let result = decoded.into_owned();
        if self.contains_unicode_escapes(&result) {
            return self.normalize_unicode(&result);
        }
        Ok(result)
    }

    /// 生成缓存键
    fn generate_cache_key(&self, input: &[u8]) -> String {
        use std::collections::hash_map::DefaultHasher;
        use std::hash::{Hash, Hasher};
        let mut hasher = DefaultHasher::new();
        input.hash(&mut hasher);
        format!("{:x}", hasher.finish())
    }

    /// 获取缓存的检测结果
    fn get_cached_detection(&self, key: &str) -> Option<EncodingDetection> {
        let mut cache = self.encoding_cache.lock();
        cache.get(key).cloned()
    }

    /// 缓存检测结果
    fn cache_detection_result(&self, key: &str, detection: &EncodingDetection) {
        let mut cache = self.encoding_cache.lock();
        cache.put(key.to_string(), detection.clone());
    }

    /// 应用缓存的转换结果
    fn apply_cached_conversion(
        &self,
        input: &[u8],
        detection: &EncodingDetection,
    ) -> Result<String, TextEncodingError> {
        if detection.is_utf8 {
            std::str::from_utf8(input)
                .map(|s| s.to_string())
                .map_err(|e| TextEncodingError::ConversionFailed(e.to_string()))
        } else {
            let encoding = Encoding::for_label(detection.encoding.as_bytes())
                .ok_or_else(|| TextEncodingError::InvalidEncoding(detection.encoding.clone()))?;
            self.convert_encoding(input, encoding, detection.confidence)
        }
    }

    /// 批量处理文本编码
    pub fn process_batch(&self, inputs: Vec<&[u8]>) -> Vec<Result<String, TextEncodingError>> {
        inputs
            .into_iter()
            .map(|input| self.process_text(input))
            .collect()
    }

    /// 获取处理器统计信息
    pub fn get_stats(&self) -> TextProcessorStats {
        let cache_size = self.encoding_cache.lock().len();
        TextProcessorStats {
            cache_size,
            cache_capacity: 1000,
            short_text_threshold: self.short_text_threshold,
        }
    }
}

/// 处理器统计信息
#[derive(Debug, Clone)]
pub struct TextProcessorStats {
    pub cache_size: usize,
    pub cache_capacity: usize,
    pub short_text_threshold: usize,
}

/// 处理单个文本(使用提供的处理器实例)
pub fn process_with_processor(
    processor: &TextEncodingProcessor,
    input: &[u8],
) -> Result<String, TextEncodingError> {
    processor.process_text(input)
}

/// 批量处理文本(使用提供的处理器实例)
pub fn process_batch_with_processor(
    processor: &TextEncodingProcessor,
    inputs: Vec<&[u8]>,
) -> Vec<Result<String, TextEncodingError>> {
    processor.process_batch(inputs)
}

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

    #[test]
    fn test_utf8_text_processing() {
        let processor = TextEncodingProcessor::new();
        let input = "Hello, 世界! This is a test.";
        let result = processor.process_text(input.as_bytes()).unwrap();
        assert_eq!(result, input);
    }

    #[test]
    fn test_unicode_escape_processing() {
        let processor = TextEncodingProcessor::new();
        let input = "Hello 世界!";
        let result = processor.process_text(input.as_bytes()).unwrap();
        assert!(result.contains("世界"));
    }

    #[test]
    fn test_unicode_escape_sequence_conversion() {
        let processor = TextEncodingProcessor::new();
        let input = "\u{4e00}\u{7ad9}\u{5f0f}\u{7f51}\u{7ad9}\u{5efa}\u{8bbe}";
        let result = processor
            .process_text(input.as_bytes())
            .expect("Unicode conversion should succeed");
        assert!(!result.is_empty());
    }

    #[test]
    fn test_short_text_optimization() {
        let processor = TextEncodingProcessor::new();
        let short_text = "Short text";
        let result = processor.process_text(short_text.as_bytes()).unwrap();
        assert_eq!(result, short_text);
    }

    #[test]
    fn test_batch_processing() {
        let processor = TextEncodingProcessor::new();
        let inputs = vec![
            "Text 1".as_bytes(),
            "Text 2".as_bytes(),
            "Text 3".as_bytes(),
        ];
        let results = processor.process_batch(inputs);
        assert_eq!(results.len(), 3);
        assert!(results.iter().all(|r| r.is_ok()));
    }

    #[test]
    fn test_trim_newlines_basic() {
        let processor = TextEncodingProcessor::new();
        assert_eq!(processor.trim_newlines("hello"), "hello");
        assert_eq!(processor.trim_newlines("\nhello"), "hello");
        assert_eq!(processor.trim_newlines("hello\n"), "hello");
        assert_eq!(processor.trim_newlines("\nhello\n"), "hello");
        assert_eq!(processor.trim_newlines("\n\nhello\n\n"), "hello");
    }

    #[test]
    fn test_trim_newlines_middle_preserved() {
        let processor = TextEncodingProcessor::new();
        assert_eq!(processor.trim_newlines("hello\nworld"), "hello\nworld");
        assert_eq!(processor.trim_newlines("\nhello\nworld\n"), "hello\nworld");
    }

    #[test]
    fn test_trim_newlines_empty() {
        let processor = TextEncodingProcessor::new();
        assert_eq!(processor.trim_newlines(""), "");
        assert_eq!(processor.trim_newlines("\n"), "");
        assert_eq!(processor.trim_newlines("\n\n\n"), "");
    }

    #[test]
    fn test_with_config_custom_values() {
        let processor = TextEncodingProcessor::with_config(50, 200);
        let stats = processor.get_stats();
        assert_eq!(stats.short_text_threshold, 200);
    }

    #[test]
    fn test_default_impl() {
        let processor = TextEncodingProcessor::default();
        let stats = processor.get_stats();
        assert_eq!(stats.short_text_threshold, 1000);
    }

    #[test]
    fn test_long_text_processing_utf8() {
        let processor = TextEncodingProcessor::new();
        let long_text = "a".repeat(1500);
        let result = processor.process_text(long_text.as_bytes()).unwrap();
        assert_eq!(result, long_text);
    }

    #[test]
    fn test_long_text_caching_increases_cache_size() {
        let processor = TextEncodingProcessor::new();
        let long_text = "x".repeat(1500);

        assert_eq!(processor.get_stats().cache_size, 0);

        processor.process_text(long_text.as_bytes()).unwrap();
        assert_eq!(processor.get_stats().cache_size, 1);

        processor.process_text(long_text.as_bytes()).unwrap();
        assert_eq!(processor.get_stats().cache_size, 1);
    }

    #[test]
    fn test_get_stats_empty_cache() {
        let processor = TextEncodingProcessor::new();
        let stats = processor.get_stats();
        assert_eq!(stats.cache_size, 0);
        assert_eq!(stats.cache_capacity, 1000);
        assert_eq!(stats.short_text_threshold, 1000);
    }

    #[test]
    fn test_get_stats_after_processing() {
        let processor = TextEncodingProcessor::new();
        let long_text = "c".repeat(1500);
        processor.process_text(long_text.as_bytes()).unwrap();
        let stats = processor.get_stats();
        assert_eq!(stats.cache_size, 1);
    }

    #[test]
    fn test_unicode_lowercase_u_escape_sequence() {
        let processor = TextEncodingProcessor::new();
        let input = r"Hello \u0041 World";
        let result = processor.process_text(input.as_bytes()).unwrap();
        assert_eq!(result, "Hello A World");
    }

    #[test]
    fn test_unicode_uppercase_u_escape_sequence() {
        let processor = TextEncodingProcessor::new();
        let input = r"Hello \U00000041 World";
        let result = processor.process_text(input.as_bytes()).unwrap();
        assert_eq!(result, "Hello A World");
    }

    #[test]
    fn test_unicode_x_escape_sequence() {
        let processor = TextEncodingProcessor::new();
        let input = r"Hello \x41 World";
        let result = processor.process_text(input.as_bytes()).unwrap();
        assert_eq!(result, "Hello A World");
    }

    #[test]
    fn test_unicode_escape_chinese() {
        let processor = TextEncodingProcessor::new();
        let input = r"Hello \u4e16界";
        let result = processor.process_text(input.as_bytes()).unwrap();
        assert_eq!(result, "Hello 世界");
    }

    #[test]
    fn test_invalid_unicode_escape_fallback() {
        let processor = TextEncodingProcessor::new();
        let input = r"Hello \uGGGG World";
        let result = processor.process_text(input.as_bytes()).unwrap();
        assert_eq!(result, "Hello \\ World");
    }

    #[test]
    fn test_backslash_without_escape() {
        let processor = TextEncodingProcessor::new();
        let input = r"Hello \n World";
        let result = processor.process_text(input.as_bytes()).unwrap();
        assert_eq!(result, "Hello \\n World");
    }

    #[test]
    fn test_process_with_processor_free_function() {
        let processor = TextEncodingProcessor::new();
        let input = "test text";
        let result = process_with_processor(&processor, input.as_bytes()).unwrap();
        assert_eq!(result, input);
    }

    #[test]
    fn test_process_batch_with_processor_free_function() {
        let processor = TextEncodingProcessor::new();
        let inputs = vec!["a".as_bytes(), "b".as_bytes()];
        let results = process_batch_with_processor(&processor, inputs);
        assert_eq!(results.len(), 2);
        assert!(results.iter().all(|r| r.is_ok()));
    }

    #[test]
    fn test_text_encoding_error_display() {
        let err = TextEncodingError::DetectionFailed("test reason".to_string());
        assert_eq!(err.to_string(), "Encoding detection failed: test reason");

        let err = TextEncodingError::ConversionFailed("conv error".to_string());
        assert_eq!(err.to_string(), "Encoding conversion failed: conv error");

        let err = TextEncodingError::UnicodeConversionFailed("uni error".to_string());
        assert_eq!(err.to_string(), "Unicode conversion failed: uni error");

        let err = TextEncodingError::InvalidEncoding("bad enc".to_string());
        assert_eq!(err.to_string(), "Invalid encoding format: bad enc");

        let err = TextEncodingError::ProcessingTimeout;
        assert_eq!(err.to_string(), "Text processing timeout");

        let err = TextEncodingError::CacheLockError("lock fail".to_string());
        assert_eq!(err.to_string(), "Cache lock acquisition failed: lock fail");
    }

    #[test]
    fn test_encoding_detection_struct() {
        let detection = EncodingDetection {
            encoding: "utf-8".to_string(),
            confidence: 1.0,
            is_utf8: true,
        };
        assert_eq!(detection.encoding, "utf-8");
        assert_eq!(detection.confidence, 1.0);
        assert!(detection.is_utf8);
    }

    #[test]
    fn test_text_processor_stats_struct() {
        let stats = TextProcessorStats {
            cache_size: 10,
            cache_capacity: 100,
            short_text_threshold: 500,
        };
        assert_eq!(stats.cache_size, 10);
        assert_eq!(stats.cache_capacity, 100);
        assert_eq!(stats.short_text_threshold, 500);
    }

    #[test]
    fn test_batch_empty_input() {
        let processor = TextEncodingProcessor::new();
        let results = processor.process_batch(vec![]);
        assert_eq!(results.len(), 0);
    }

    // Supplementary tests: long text, cache, non-UTF8

    #[test]
    fn test_long_text_with_unicode_escapes_is_normalized() {
        // Long text (>= short_text_threshold) containing \u escapes must be
        // normalized through the convert_encoding/normalize_unicode path when
        // the content is non-UTF8. For UTF-8 content, detect_and_convert_encoding
        // returns directly, so unicode normalization happens via process_short_text
        // only. This test verifies long UTF-8 text still gets unicode normalization.
        let processor = TextEncodingProcessor::new();
        let prefix = "a".repeat(1000);
        let suffix = r"\u4e16\u754c";
        let long_text = format!("{}{}", prefix, suffix);
        let result = processor
            .process_text(long_text.as_bytes())
            .expect("long text with unicode escapes should process");
        assert!(
            result.contains("世界"),
            "unicode escapes should be normalized: got {}",
            result
        );
    }

    #[test]
    fn test_long_text_caching_returns_same_result_on_second_call() {
        // Verify cached conversion produces identical output to the first call.
        let processor = TextEncodingProcessor::new();
        let long_text = "b".repeat(1500);
        let first = processor.process_text(long_text.as_bytes()).unwrap();
        let second = processor.process_text(long_text.as_bytes()).unwrap();
        assert_eq!(first, second, "cached result must match first result");
        // Cache size should remain 1 (same key).
        assert_eq!(processor.get_stats().cache_size, 1);
    }

    #[test]
    fn test_with_config_custom_cache_size() {
        // Verify with_config sets a custom cache size and short_text_threshold.
        let processor = TextEncodingProcessor::with_config(50, 200);
        let stats = processor.get_stats();
        assert_eq!(stats.short_text_threshold, 200);
        // Note: cache_capacity is hardcoded to 1000 in get_stats; this is
        // the current implementation behavior (not configurable via with_config).
        assert_eq!(stats.cache_capacity, 1000);
    }

    #[test]
    fn test_with_config_small_threshold_uses_long_text_path() {
        // With a small threshold, even short content goes through the long-text
        // path (with caching). Verify the cache is populated.
        let processor = TextEncodingProcessor::with_config(50, 5);
        assert_eq!(processor.get_stats().cache_size, 0);
        // "hello world" is 11 bytes, > threshold of 5.
        let result = processor.process_text(b"hello world").unwrap();
        assert_eq!(result, "hello world");
        assert_eq!(
            processor.get_stats().cache_size,
            1,
            "content longer than threshold should be cached"
        );
    }

    #[test]
    fn test_long_text_unicode_escape_uppercase_u_in_long_content() {
        // Long UTF-8 text containing \U escape (8 hex digits).
        let processor = TextEncodingProcessor::new();
        let prefix = "x".repeat(1000);
        let suffix = r"\U00000041"; // 'A'
        let long_text = format!("{}{}", prefix, suffix);
        let result = processor.process_text(long_text.as_bytes()).unwrap();
        assert!(
            result.contains('A'),
            "\\U escape should be normalized to 'A': got {}",
            result
        );
    }

    #[test]
    fn test_long_text_unicode_escape_x_in_long_content() {
        // Long UTF-8 text containing \x escape (2 hex digits).
        let processor = TextEncodingProcessor::new();
        let prefix = "y".repeat(1000);
        let suffix = r"\x42"; // 'B'
        let long_text = format!("{}{}", prefix, suffix);
        let result = processor.process_text(long_text.as_bytes()).unwrap();
        assert!(
            result.contains('B'),
            "\\x escape should be normalized to 'B': got {}",
            result
        );
    }

    #[test]
    fn test_cache_key_is_deterministic_for_same_input() {
        // Same input should produce the same cache key, so second call hits cache.
        let processor = TextEncodingProcessor::with_config(100, 10);
        let content = "z".repeat(20);
        processor.process_text(content.as_bytes()).unwrap();
        assert_eq!(processor.get_stats().cache_size, 1);
        // Different content should create a new cache entry.
        let content2 = "w".repeat(20);
        processor.process_text(content2.as_bytes()).unwrap();
        assert_eq!(processor.get_stats().cache_size, 2);
    }

    #[test]
    fn test_trim_newlines_only_newlines_at_start_and_end() {
        // Middle newlines are preserved; only leading/trailing are trimmed.
        let processor = TextEncodingProcessor::new();
        assert_eq!(processor.trim_newlines("\n\n\n"), "");
        assert_eq!(processor.trim_newlines("content"), "content");
        assert_eq!(
            processor.trim_newlines("\n\ncontent\nmore\n"),
            "content\nmore"
        );
    }

    #[test]
    fn test_process_text_with_empty_input() {
        // Empty input is a valid edge case; should return empty string.
        let processor = TextEncodingProcessor::new();
        let result = processor
            .process_text(b"")
            .expect("empty input should succeed");
        assert_eq!(result, "");
    }

    #[test]
    fn test_process_batch_with_mixed_utf8_and_empty() {
        // Batch containing empty and non-empty UTF-8 inputs.
        let processor = TextEncodingProcessor::new();
        let inputs: Vec<&[u8]> = vec![b"", b"hello", b""];
        let results = processor.process_batch(inputs);
        assert_eq!(results.len(), 3);
        assert!(results.iter().all(|r| r.is_ok()));
        assert_eq!(results[0].as_ref().unwrap(), "");
        assert_eq!(results[1].as_ref().unwrap(), "hello");
        assert_eq!(results[2].as_ref().unwrap(), "");
    }

    #[test]
    fn test_non_utf8_content_is_decoded() {
        // Non-UTF8 content (GBK-encoded "中文") should be decoded successfully.
        // GBK encoding of "中文" is [0xD6, 0xD0, 0xCE, 0xC4].
        let processor = TextEncodingProcessor::new();
        let gbk_bytes: [u8; 4] = [0xD6, 0xD0, 0xCE, 0xC4];
        let result = processor.process_text(&gbk_bytes);
        // The decoder should succeed; the exact output depends on chardetng's
        // detection. We verify it does not error and produces non-empty output.
        match result {
            Ok(text) => {
                assert!(!text.is_empty(), "decoded text should be non-empty");
            }
            Err(_) => {
                // If detection fails, that's acceptable for very short non-UTF8 input;
                // the important thing is that the function does not panic.
            }
        }
    }

    #[test]
    fn test_non_utf8_long_content_caching() {
        // Long non-UTF8 content should go through the cache path.
        // Construct a long non-UTF8 byte sequence by repeating GBK bytes.
        let processor = TextEncodingProcessor::new();
        let mut non_utf8 = Vec::new();
        for _ in 0..300 {
            non_utf8.extend_from_slice(&[0xD6, 0xD0, 0xCE, 0xC4]);
        }
        let result = processor.process_text(&non_utf8);
        // Should either succeed (decoded) or fail gracefully; must not panic.
        if let Ok(text) = result {
            assert!(!text.is_empty());
        }
    }

    #[test]
    fn test_text_processor_clone_preserves_config() {
        // TextEncodingProcessor derives Clone; verify config is preserved.
        let processor = TextEncodingProcessor::with_config(50, 500);
        let cloned = processor.clone();
        assert_eq!(
            processor.get_stats().short_text_threshold,
            cloned.get_stats().short_text_threshold
        );
    }

    #[test]
    fn test_invalid_utf8_short_text_does_not_panic() {
        // Short invalid UTF-8 should be handled via detect_and_convert_encoding.
        let processor = TextEncodingProcessor::new();
        let invalid_utf8: [u8; 4] = [0xFF, 0xFE, 0xFD, 0xFC];
        let result = processor.process_text(&invalid_utf8);
        // Should not panic; may succeed or fail depending on detection.
        let _ = result;
    }

    #[test]
    fn test_get_stats_cache_capacity_is_constant() {
        // get_stats always reports cache_capacity=1000 regardless of with_config.
        // This documents the current implementation behavior.
        let processor = TextEncodingProcessor::with_config(50, 200);
        let stats = processor.get_stats();
        assert_eq!(stats.cache_capacity, 1000);
    }

    // Supplementary tests: unicode escape edge cases & cached non-UTF8 path

    #[test]
    fn test_unicode_escape_surrogate_code_point_falls_back_to_backslash() {
        // \uD800 is a surrogate code point; char::from_u32 returns None.
        // The parser should fall back to pushing the backslash and consuming
        // the 'u' + 4 hex chars (which are lost).
        let processor = TextEncodingProcessor::new();
        let input = r"\uD800";
        let result = processor.process_text(input.as_bytes()).unwrap();
        // Backslash is pushed; 'D800' chars are consumed by the hex loop.
        assert_eq!(result, "\\");
    }

    #[test]
    fn test_unicode_uppercase_u_escape_beyond_max_code_point() {
        // \U00110000 is beyond the maximum Unicode scalar value (0x10FFFF).
        // char::from_u32 returns None, so the backslash is pushed and the
        // 8 hex chars are consumed (lost).
        let processor = TextEncodingProcessor::new();
        let input = r"\U00110000";
        let result = processor.process_text(input.as_bytes()).unwrap();
        assert_eq!(result, "\\");
    }

    #[test]
    fn test_unicode_escape_incomplete_at_end_of_string() {
        // \u41 at end of string: only 2 hex chars available. The loop runs 4
        // times but chars.next returns None after 2, so hex_chars = "41".
        // 0x41 = 'A'. This documents the lenient parsing behavior.
        let processor = TextEncodingProcessor::new();
        let input = r"\u41";
        let result = processor.process_text(input.as_bytes()).unwrap();
        assert_eq!(result, "A");
    }

    #[test]
    fn test_unicode_x_escape_with_non_hex_chars() {
        // \xZZ: from_str_radix("ZZ", 16) fails, backslash is pushed, 'x' and
        // 'ZZ' are consumed (lost).
        let processor = TextEncodingProcessor::new();
        let input = r"\xZZ";
        let result = processor.process_text(input.as_bytes()).unwrap();
        assert_eq!(result, "\\");
    }

    #[test]
    fn test_unicode_x_escape_single_hex_digit() {
        // \xA at end: only 1 hex char available. hex_chars = "A" = 0x0A = '\n'.
        // process_text calls trim_newlines which removes the leading/trailing
        // \n, so the final result is empty.
        let processor = TextEncodingProcessor::new();
        let input = r"\xA";
        let result = processor.process_text(input.as_bytes()).unwrap();
        assert_eq!(result, "");
    }

    #[test]
    fn test_multiple_unicode_escapes_in_one_string() {
        // Mix of \u, \U, and \x escapes in a single string.
        let processor = TextEncodingProcessor::new();
        let input = r"\u0041\U00000042\x43";
        let result = processor.process_text(input.as_bytes()).unwrap();
        assert_eq!(result, "ABC");
    }

    #[test]
    fn test_backslash_u_at_end_of_string_with_no_hex() {
        // "\u" at end: 'u' is consumed, hex loop runs 4 times getting None,
        // hex_chars = "", from_str_radix("", 16) fails, backslash pushed.
        let processor = TextEncodingProcessor::new();
        let input = r"\u";
        let result = processor.process_text(input.as_bytes()).unwrap();
        assert_eq!(result, "\\");
    }

    #[test]
    fn test_backslash_followed_by_non_escape_char() {
        // \n: 'n' is not u/U/x, so all branches are skipped and backslash is
        // pushed, then 'n' is pushed in the next iteration.
        let processor = TextEncodingProcessor::new();
        let input = r"\n";
        let result = processor.process_text(input.as_bytes()).unwrap();
        assert_eq!(result, "\\n");
    }

    #[test]
    fn test_unicode_escape_max_valid_code_point() {
        // \U0010FFFF is the maximum valid Unicode scalar value.
        let processor = TextEncodingProcessor::new();
        let input = r"\U0010FFFF";
        let result = processor.process_text(input.as_bytes()).unwrap();
        // Should produce the character U+10FFFF.
        assert_eq!(result, "\u{10FFFF}");
    }

    #[test]
    fn test_unicode_escape_null_code_point() {
        // \u0000 is the null character; char::from_u32(0) returns Some('\0').
        let processor = TextEncodingProcessor::new();
        let input = r"\u0000";
        let result = processor.process_text(input.as_bytes()).unwrap();
        assert_eq!(result, "\0");
    }

    #[test]
    fn test_apply_cached_conversion_non_utf8_path() {
        // Process non-UTF8 content twice with a small threshold so the second
        // call uses the cached non-UTF8 detection (apply_cached_conversion
        // non-utf8 branch). We use GBK bytes and a threshold of 2.
        let processor = TextEncodingProcessor::with_config(100, 2);
        let gbk_bytes: [u8; 4] = [0xD6, 0xD0, 0xCE, 0xC4];
        // First call: detects and caches.
        let first = processor.process_text(&gbk_bytes);
        // Second call: should use cached detection (non-utf8 branch).
        let second = processor.process_text(&gbk_bytes);
        // Both calls should produce the same result (either both Ok or both Err).
        match (first, second) {
            (Ok(a), Ok(b)) => assert_eq!(a, b, "cached non-utf8 result should match first"),
            (Err(_), Err(_)) => {}
            (a, b) => panic!("both calls should agree: first={:?}, second={:?}", a, b),
        }
        // Cache should have exactly 1 entry.
        assert_eq!(processor.get_stats().cache_size, 1);
    }

    #[test]
    fn test_convert_encoding_with_unicode_escapes_in_non_utf8() {
        // When non-UTF8 content is decoded and the decoded text contains
        // unicode escape sequences, convert_encoding should normalize them.
        // This is hard to control precisely, so we verify the path does not
        // panic and produces some output.
        let processor = TextEncodingProcessor::with_config(100, 2);
        // Construct bytes that when decoded might contain \u sequences.
        // We use a long sequence to ensure long-text path with caching.
        let mut non_utf8 = Vec::new();
        for _ in 0..100 {
            non_utf8.extend_from_slice(&[0xD6, 0xD0, 0xCE, 0xC4]);
        }
        let result = processor.process_text(&non_utf8);
        // Should not panic; may succeed or fail.
        let _ = result;
    }

    #[test]
    fn test_long_text_processing_caches_detection_only_on_success() {
        // cache_detection_result is only called when result.is_ok. Verify
        // that a failed detection does not populate the cache.
        let processor = TextEncodingProcessor::with_config(100, 2);
        // Use bytes likely to fail detection in a way that produces an error.
        // Note: this is best-effort; if detection succeeds, cache will have 1.
        let weird_bytes: Vec<u8> = vec![0xFF; 10];
        let _ = processor.process_text(&weird_bytes);
        // If it succeeded, cache_size is 1; if it failed, cache_size is 0.
        // Either way, this documents the behavior.
        let stats = processor.get_stats();
        assert!(stats.cache_size <= 1);
    }

    #[test]
    fn test_process_text_preserves_content_with_special_chars() {
        // Verify that text with special characters (but no unicode escapes)
        // is preserved exactly.
        let processor = TextEncodingProcessor::new();
        let input = "Hello\tWorld\nNew Line\r\nCarriage";
        let result = processor.process_text(input.as_bytes()).unwrap();
        // trim_newlines only removes leading/trailing \n, not \t or \r.
        assert_eq!(result, input);
    }

    #[test]
    fn test_trim_newlines_preserves_carriage_returns() {
        // trim_newlines only removes \n, not \r. Leading \r\n becomes \r
        // (only \n at start would be removed; here start is \r so no change).
        // Trailing \r\n becomes \r (the trailing \n is removed).
        let processor = TextEncodingProcessor::new();
        assert_eq!(processor.trim_newlines("\rhello\r"), "\rhello\r");
        assert_eq!(processor.trim_newlines("\r\nhello\r\n"), "\r\nhello\r");
    }

    #[test]
    fn test_unicode_escape_mixed_with_regular_backslashes() {
        // Mix of valid escapes and plain backslashes.
        let processor = TextEncodingProcessor::new();
        let input = r"\\u0041\n\x42";
        // First \\ is a literal backslash (second \ is not u/U/x after first \).
        // Wait: parsing: c='\\', peek='\\' (not u/U/x), push '\\'.
        // Next: c='\\'(second), peek='u', consume 'u', hex="0041"='A', push 'A'.
        // Next: c='\\'(from \n), peek='n', not u/U/x, push '\\'.
        // Next: c='n', push 'n'.
        // Next: c='\\'(from \x), peek='x', consume 'x', hex="42"='B', push 'B'.
        let result = processor.process_text(input.as_bytes()).unwrap();
        assert_eq!(result, "\\A\\nB");
    }

    // detect_and_convert_encoding / apply_cached_conversion is_utf8=true 分支覆盖

    #[test]
    fn test_detect_and_convert_encoding_utf8_branch_with_chinese_long_text() {
        // 构造包含非 ASCII UTF-8 字符的长文本(>= short_text_threshold),
        // 确保 chardetng 检测为 utf-8(is_utf8=true),
        // 覆盖 detect_and_convert_encoding 行 203-206。
        let processor = TextEncodingProcessor::new();
        let prefix = "a".repeat(900);
        let chinese = "你好世界测试文本".repeat(20);
        let long_text = format!("{}{}", prefix, chinese);
        assert!(
            long_text.len() >= 1000,
            "long_text must be >= 1000 bytes for long-text path"
        );
        let result = processor
            .process_text(long_text.as_bytes())
            .expect("long UTF-8 text with Chinese should process");
        assert!(
            result.contains("你好世界测试文本"),
            "result should contain Chinese text"
        );
    }

    #[test]
    fn test_apply_cached_conversion_utf8_branch_on_second_call() {
        // 第二次调用相同的长 UTF-8 文本,走缓存路径,
        // apply_cached_conversion 的 is_utf8=true 分支(行 277-279)。
        let processor = TextEncodingProcessor::new();
        let prefix = "b".repeat(900);
        let chinese = "你好世界".repeat(30);
        let long_text = format!("{}{}", prefix, chinese);
        assert!(long_text.len() >= 1000);
        // 第一次调用:detect_and_convert_encoding + cache_detection_result
        let first = processor
            .process_text(long_text.as_bytes())
            .expect("first call should succeed");
        assert_eq!(processor.get_stats().cache_size, 1);
        // 第二次调用:apply_cached_conversion(is_utf8=true 分支)
        let second = processor
            .process_text(long_text.as_bytes())
            .expect("second call should use cache");
        assert_eq!(first, second, "cached result should match first result");
    }

    #[test]
    fn test_detect_and_convert_encoding_utf8_branch_with_mixed_long_text() {
        // 另一个长 UTF-8 文本,包含多种非 ASCII 字符,
        // 进一步确保 chardetng 检测为 utf-8。
        let processor = TextEncodingProcessor::new();
        let prefix = "x".repeat(800);
        let mixed = "你好世界こんにちは안녕하세요".repeat(20);
        let long_text = format!("{}{}", prefix, mixed);
        assert!(long_text.len() >= 1000);
        let result = processor
            .process_text(long_text.as_bytes())
            .expect("mixed long UTF-8 text should process");
        assert!(!result.is_empty());
    }
}