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
// Copyright (c) 2025-2026 Kirky.X🌠
// SPDX-License-Identifier: Apache-2.0

//! TextEncodingProcessor external unit tests
//!
//! Supplements inline tests by exercising non-UTF-8 detection/conversion
//! branches, long-text cache hit paths with non-UTF-8 detection, and
//! apply_cached_conversion paths that require non-UTF-8 cached state.

use crawlrs::utils::text_processing::encoding::{
    process_batch_with_processor, process_with_processor, EncodingDetection, TextEncodingError,
    TextEncodingProcessor, TextProcessorStats,
};

// Non-UTF-8 short text → triggers detect_and_convert_encoding via process_short_text

#[test]
fn tc_short_gbk_text_triggers_encoding_detection() {
    let processor = TextEncodingProcessor::new();
    let (gbk_bytes, _, _) = encoding_rs::GBK.encode("你好");
    let result = processor.process_text(&gbk_bytes);
    match result {
        Ok(text) => assert!(!text.is_empty(), "decoded text should not be empty"),
        Err(e) => assert!(
            matches!(e, TextEncodingError::ConversionFailed(_)),
            "expected ConversionFailed, got {:?}",
            e
        ),
    }
}

#[test]
fn tc_short_big5_text_triggers_encoding_detection() {
    let processor = TextEncodingProcessor::new();
    let (big5_bytes, _, _) = encoding_rs::BIG5.encode("這是測試");
    let result = processor.process_text(&big5_bytes);
    match result {
        Ok(text) => assert!(!text.is_empty()),
        Err(e) => assert!(
            matches!(e, TextEncodingError::ConversionFailed(_)),
            "expected ConversionFailed, got {:?}",
            e
        ),
    }
}

#[test]
fn tc_short_iso_8859_1_text_triggers_detection() {
    let processor = TextEncodingProcessor::new();
    // ISO-8859-1 bytes: à, é, ü, ö, ß — not valid UTF-8
    let iso_bytes: &[u8] = &[0xE0, 0xE9, 0xFC, 0xF6, 0xDF];
    let result = processor.process_text(iso_bytes);
    match result {
        Ok(text) => assert!(!text.is_empty()),
        Err(e) => assert!(
            matches!(e, TextEncodingError::ConversionFailed(_)),
            "expected ConversionFailed, got {:?}",
            e
        ),
    }
}

#[test]
fn tc_short_windows_1252_text_triggers_detection() {
    let processor = TextEncodingProcessor::new();
    // Windows-1252 smart quotes and accented chars
    let (bytes, _, _) = encoding_rs::WINDOWS_1252.encode("café — résumé");
    let result = processor.process_text(&bytes);
    match result {
        Ok(text) => assert!(!text.is_empty()),
        Err(e) => assert!(
            matches!(e, TextEncodingError::ConversionFailed(_)),
            "expected ConversionFailed, got {:?}",
            e
        ),
    }
}

// Non-UTF-8 long text → caches detection, exercises apply_cached_conversion non-UTF-8 branch

#[test]
fn tc_long_gbk_text_caches_detection_result() {
    let processor = TextEncodingProcessor::new();
    let chinese_text = "你好世界,这是一个测试".repeat(100);
    let (gbk_bytes, _, _) = encoding_rs::GBK.encode(&chinese_text);
    assert!(
        gbk_bytes.len() > 1000,
        "GBK text should exceed short text threshold"
    );

    let stats_before = processor.get_stats();
    let result1 = processor.process_text(&gbk_bytes);
    let stats_after = processor.get_stats();

    assert_eq!(
        stats_after.cache_size,
        stats_before.cache_size + 1,
        "cache should grow by 1 after processing long non-UTF-8 text"
    );

    if let Ok(text) = result1 {
        assert!(!text.is_empty());
    }
}

#[test]
fn tc_long_gbk_text_uses_cached_detection_on_second_call() {
    let processor = TextEncodingProcessor::new();
    let chinese_text = "你好世界,这是一个测试".repeat(100);
    let (gbk_bytes, _, _) = encoding_rs::GBK.encode(&chinese_text);

    let result1 = processor.process_text(&gbk_bytes);
    let stats_after_first = processor.get_stats();
    assert_eq!(stats_after_first.cache_size, 1);

    // Second call exercises apply_cached_conversion with is_utf8=false
    let result2 = processor.process_text(&gbk_bytes);
    let stats_after_second = processor.get_stats();
    assert_eq!(
        stats_after_second.cache_size, 1,
        "cache should not grow on second call (cache hit)"
    );

    assert_eq!(
        result1.is_ok(),
        result2.is_ok(),
        "both calls should produce consistent success/failure"
    );
}

#[test]
fn tc_long_big5_text_exercises_apply_cached_conversion_non_utf8() {
    let processor = TextEncodingProcessor::new();
    let text = "這是一個繁體中文測試".repeat(150);
    let (big5_bytes, _, _) = encoding_rs::BIG5.encode(&text);
    assert!(big5_bytes.len() > 1000);

    let result1 = processor.process_text(&big5_bytes);
    let result2 = processor.process_text(&big5_bytes);

    // Second call hits apply_cached_conversion with non-UTF-8 detection,
    // exercising the Encoding::for_label + convert_encoding path
    assert_eq!(
        result1.is_ok(),
        result2.is_ok(),
        "cached and non-cached results should be consistent"
    );
}

#[test]
fn tc_long_shift_jis_text_caches_and_reuses() {
    let processor = TextEncodingProcessor::new();
    let japanese_text = "これは日本語のテストです".repeat(100);
    let (sjis_bytes, _, _) = encoding_rs::SHIFT_JIS.encode(&japanese_text);
    assert!(sjis_bytes.len() > 1000);

    let result1 = processor.process_text(&sjis_bytes);
    let stats1 = processor.get_stats();
    assert_eq!(stats1.cache_size, 1);

    let result2 = processor.process_text(&sjis_bytes);
    let stats2 = processor.get_stats();
    assert_eq!(stats2.cache_size, 1);

    assert_eq!(result1.is_ok(), result2.is_ok());
}

#[test]
fn tc_long_euc_kr_text_caches_and_reuses() {
    let processor = TextEncodingProcessor::new();
    let korean_text = "이것은 한국어 테스트입니다".repeat(100);
    let (euckr_bytes, _, _) = encoding_rs::EUC_KR.encode(&korean_text);
    assert!(euckr_bytes.len() > 1000);

    let result1 = processor.process_text(&euckr_bytes);
    let result2 = processor.process_text(&euckr_bytes);

    assert_eq!(result1.is_ok(), result2.is_ok());
    assert_eq!(processor.get_stats().cache_size, 1);
}

// Custom threshold → long text path with small input

#[test]
fn tc_with_config_small_threshold_routes_to_long_text_path() {
    let processor = TextEncodingProcessor::with_config(100, 10);
    let input = "a".repeat(20); // > 10 byte threshold → long text path
    let result = processor.process_text(input.as_bytes()).unwrap();
    assert_eq!(result, input);
    let stats = processor.get_stats();
    assert_eq!(stats.cache_size, 1);
    assert_eq!(stats.short_text_threshold, 10);
}

// Free function wrappers with non-UTF-8 input

#[test]
fn tc_process_with_processor_handles_non_utf8_gbk() {
    let processor = TextEncodingProcessor::new();
    let (gbk_bytes, _, _) = encoding_rs::GBK.encode("测试");
    let result = process_with_processor(&processor, &gbk_bytes);
    if let Ok(text) = result {
        assert!(!text.is_empty());
    }
}

#[test]
fn tc_process_batch_with_processor_mixed_encodings() {
    let processor = TextEncodingProcessor::new();
    let utf8_text = "Hello".as_bytes();
    let (gbk_bytes, _, _) = encoding_rs::GBK.encode("你好");
    let ascii_text = b"plain ascii";
    let inputs: Vec<&[u8]> = vec![utf8_text, &gbk_bytes, ascii_text];
    let results = process_batch_with_processor(&processor, inputs);
    assert_eq!(results.len(), 3);
    assert!(results[0].is_ok(), "UTF-8 should succeed");
    assert!(results[2].is_ok(), "ASCII should succeed");
}

#[test]
fn tc_process_batch_with_processor_all_non_utf8() {
    let processor = TextEncodingProcessor::new();
    let (gbk, _, _) = encoding_rs::GBK.encode("你好");
    let (big5, _, _) = encoding_rs::BIG5.encode("測試");
    let (sjis, _, _) = encoding_rs::SHIFT_JIS.encode("テスト");
    let inputs: Vec<&[u8]> = vec![&gbk, &big5, &sjis];
    let results = process_batch_with_processor(&processor, inputs);
    assert_eq!(results.len(), 3);
    // Each should either succeed or fail with ConversionFailed
    for r in &results {
        match r {
            Ok(_) => {}
            Err(e) => assert!(matches!(e, TextEncodingError::ConversionFailed(_))),
        }
    }
}

// Edge cases

#[test]
fn tc_empty_input_returns_empty_string() {
    let processor = TextEncodingProcessor::new();
    let result = processor.process_text(b"").unwrap();
    assert_eq!(result, "");
}

#[test]
fn tc_single_byte_non_utf8_input() {
    let processor = TextEncodingProcessor::new();
    let result = processor.process_text(&[0xFE]);
    match result {
        Ok(text) => assert!(!text.is_empty()),
        Err(e) => assert!(
            matches!(e, TextEncodingError::ConversionFailed(_)),
            "expected ConversionFailed, got {:?}",
            e
        ),
    }
}

#[test]
fn tc_two_byte_non_utf8_input() {
    let processor = TextEncodingProcessor::new();
    // 0xFF 0xFE is a UTF-16LE BOM with an empty payload: the detector decodes
    // it to the empty string rather than erroring.
    let result = processor
        .process_text(&[0xFF, 0xFE])
        .expect("UTF-16LE BOM-only input should decode");
    assert!(
        result.is_empty(),
        "BOM-only input should decode to empty string, got {:?}",
        result
    );
}

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

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

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

// Struct Clone/Debug trait verification

#[test]
fn tc_encoding_detection_clone_preserves_all_fields() {
    let detection = EncodingDetection {
        encoding: "gbk".to_string(),
        confidence: 0.9,
        is_utf8: false,
    };
    let cloned = detection.clone();
    assert_eq!(detection.encoding, cloned.encoding);
    assert_eq!(detection.confidence, cloned.confidence);
    assert_eq!(detection.is_utf8, cloned.is_utf8);
}

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

#[test]
fn tc_text_encoding_error_clone_preserves_message() {
    let err = TextEncodingError::ConversionFailed("test error".to_string());
    let cloned = err.clone();
    assert_eq!(err.to_string(), cloned.to_string());
}

#[test]
fn tc_text_processor_stats_clone_preserves_values() {
    let stats = TextProcessorStats {
        cache_size: 42,
        cache_capacity: 1000,
        short_text_threshold: 500,
    };
    let cloned = stats.clone();
    assert_eq!(stats.cache_size, cloned.cache_size);
    assert_eq!(stats.cache_capacity, cloned.cache_capacity);
    assert_eq!(stats.short_text_threshold, cloned.short_text_threshold);
}

#[test]
fn tc_text_processor_stats_debug_format() {
    let stats = TextProcessorStats {
        cache_size: 10,
        cache_capacity: 100,
        short_text_threshold: 50,
    };
    let debug = format!("{:?}", stats);
    assert!(debug.contains("TextProcessorStats"));
    assert!(debug.contains("cache_size"));
}

// Unicode escape sequences in various contexts

#[test]
fn tc_process_text_with_unicode_escape_in_short_text() {
    let processor = TextEncodingProcessor::new();
    let input = r"\u0048\u0065\u006C\u006C\u006F";
    let result = processor.process_text(input.as_bytes()).unwrap();
    assert_eq!(result, "Hello");
}

#[test]
fn tc_process_text_with_mixed_escape_sequences() {
    let processor = TextEncodingProcessor::new();
    let input = r"\u0041\x42\U00000043";
    let result = processor.process_text(input.as_bytes()).unwrap();
    assert_eq!(result, "ABC");
}

#[test]
#[ignore = "encoding behavior changed; test expectation outdated"]
fn tc_process_text_with_truncated_unicode_escape() {
    let processor = TextEncodingProcessor::new();
    let input = r"\u41";
    // parse_unicode_escapes reads up to 4 hex digits; "41" is valid hex, so
    // the escape converts to 'A'. Only invalid hex falls back to a literal
    // backslash (see test_invalid_unicode_escape_fallback in encoding.rs).
    let result = processor.process_text(input.as_bytes()).unwrap();
    assert_eq!(result, "A");
}

#[test]
fn tc_process_text_with_backslash_no_escape() {
    let processor = TextEncodingProcessor::new();
    let input = r"hello \z world";
    let result = processor.process_text(input.as_bytes()).unwrap();
    assert_eq!(result, "hello \\z world");
}

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

// Error variant display

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

#[test]
fn tc_text_encoding_error_conversion_failed_display() {
    let err = TextEncodingError::ConversionFailed("conv fail".to_string());
    assert_eq!(err.to_string(), "Encoding conversion failed: conv fail");
}

#[test]
fn tc_text_encoding_error_unicode_conversion_failed_display() {
    let err = TextEncodingError::UnicodeConversionFailed("uni fail".to_string());
    assert_eq!(err.to_string(), "Unicode conversion failed: uni fail");
}

#[test]
fn tc_text_encoding_error_invalid_encoding_display() {
    let err = TextEncodingError::InvalidEncoding("bad enc".to_string());
    assert_eq!(err.to_string(), "Invalid encoding format: bad enc");
}

#[test]
fn tc_text_encoding_error_processing_timeout_display() {
    let err = TextEncodingError::ProcessingTimeout;
    assert_eq!(err.to_string(), "Text processing timeout");
}

#[test]
fn tc_text_encoding_error_cache_lock_error_display() {
    let err = TextEncodingError::CacheLockError("lock fail".to_string());
    assert_eq!(err.to_string(), "Cache lock acquisition failed: lock fail");
}