dictutils 0.1.2

Dictionary utilities for Mdict and other formats
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
//! Integration tests for dictionary operations
//!
//! This module provides comprehensive integration tests that test
//! dictionary operations with real dictionary files and scenarios.

#[cfg(test)]
mod tests {
    use super::*;
    use crate::traits::*;
    use crate::util::test_utils::*;
    use std::path::PathBuf;
    use std::fs;

    /// Test basic dictionary loading and operations
    mod basic_dictionary_tests {
        use super::*;

        #[test]
        fn test_dict_loader_basic() {
            let temp_dir = temp_dir().unwrap();
            let dict_path = temp_dir.path().join("test.mdict");
            
            // Create a mock dictionary
            let entries = generate_test_entries(10);
            create_test_dict(&dict_path, "mdict", &entries).unwrap();
            
            let loader = DictLoader::new();
            
            // Test format detection
            let format = loader.detect_format(&dict_path).unwrap();
            assert_eq!(format, "mdict");
            
            // Test if dictionary file is recognized
            assert!(loader.is_dictionary_file(&dict_path));
        }

        #[test]
        fn test_format_detection_edge_cases() {
            let temp_dir = temp_dir().unwrap();
            let loader = DictLoader::new();
            
            // Test various file extensions
            let test_cases = vec![
                ("test.mdict", "mdict"),
                ("test.dict", "stardict"), 
                ("test.zim", "zim"),
                ("test.unknown", ""),
            ];
            
            for (filename, expected_format) in test_cases {
                let file_path = temp_dir.path().join(filename);
                
                if !expected_format.is_empty() {
                    std::fs::write(&file_path, "test content").unwrap();
                }
                
                let result = loader.detect_format(&file_path);
                if expected_format.is_empty() {
                    assert!(result.is_err());
                } else {
                    assert_eq!(result.unwrap(), expected_format);
                }
            }
        }

        #[test]
        fn test_multiple_format_support() {
            let temp_dir = temp_dir().unwrap();
            let loader = DictLoader::new();
            
            // Test all supported formats
            let formats = loader.supported_formats();
            assert_eq!(formats.len(), 3);
            assert!(formats.contains(&"mdict".to_string()));
            assert!(formats.contains(&"stardict".to_string()));
            assert!(formats.contains(&"zim".to_string()));
        }

        #[test]
        fn test_directory_scanning() {
            let temp_dir = temp_dir().unwrap();
            
            // Create test files of different formats
            let mdict_file = temp_dir.path().join("test1.mdict");
            let dict_file = temp_dir.path().join("test2.dict");
            let zim_file = temp_dir.path().join("test3.zim");
            let other_file = temp_dir.path().join("other.txt");
            
            std::fs::write(&mdict_file, "mdict content").unwrap();
            std::fs::write(&dict_file, "dict content").unwrap();
            std::fs::write(&zim_file, "zim content").unwrap();
            std::fs::write(&other_file, "other content").unwrap();
            
            let loader = DictLoader::new();
            let dict_files = loader.scan_directory(&temp_dir).unwrap();
            
            assert_eq!(dict_files.len(), 3);
            
            let filenames: Vec<String> = dict_files.iter()
                .filter_map(|p| p.file_name().and_then(|n| n.to_str()).map(|s| s.to_string()))
                .collect();
            
            assert!(filenames.contains(&"test1.mdict".to_string()));
            assert!(filenames.contains(&"test2.dict".to_string()));
            assert!(filenames.contains(&"test3.zim".to_string()));
            assert!(!filenames.contains(&"other.txt".to_string()));
        }
    }

    /// Test dictionary metadata extraction
    mod metadata_tests {
        use super::*;

        #[test]
        fn test_metadata_extraction() {
            let temp_dir = temp_dir().unwrap();
            let dict_path = temp_dir.path().join("test.mdict");
            
            // Create a mock dictionary with specific metadata
            let entries = generate_test_entries(100);
            create_test_dict(&dict_path, "mdict", &entries).unwrap();
            
            let loader = DictLoader::new();
            
            // Test that basic metadata can be extracted
            // Note: This would test actual metadata extraction once implementations are fixed
            assert!(dict_path.exists());
        }

        #[test]
        fn test_file_size_reporting() {
            let temp_dir = temp_dir().unwrap();
            let dict_path = temp_dir.path().join("test.mdict");
            
            // Create test file with known size
            let test_content = "Test dictionary content with specific size";
            std::fs::write(&dict_path, test_content).unwrap();
            
            let loader = DictLoader::new();
            let size = crate::dict::utils::get_dict_size(&dict_path).unwrap();
            
            assert_eq!(size, test_content.len() as u64);
        }
    }

    /// Test search functionality
    mod search_tests {
        use super::*;

        #[test]
        fn test_prefix_search_scenarios() {
            let temp_dir = temp_dir().unwrap();
            let dict_path = temp_dir.path().join("test.mdict");
            
            // Create entries with common prefixes
            let mut entries = Vec::new();
            for i in 0..10 {
                entries.push((format!("prefix_{:03}", i), format!("Definition {}", i).into_bytes()));
            }
            for i in 0..10 {
                entries.push((format!("other_{:03}", i), format!("Other {}", i).into_bytes()));
            }
            
            create_test_dict(&dict_path, "mdict", &entries).unwrap();
            
            // Test that dictionary was created
            assert!(dict_path.exists());
        }

        #[test]
        fn test_fuzzy_search_scenarios() {
            let temp_dir = temp_dir().unwrap();
            let dict_path = temp_dir.path().join("test.mdict");
            
            // Create entries for fuzzy search testing
            let entries = vec![
                ("hello".to_string(), b"Hello world definition".to_vec()),
                ("hell".to_string(), b"Short form of hello".to_vec()),
                ("helo".to_string(), b"Common misspelling of hello".to_vec()),
                ("world".to_string(), b"World definition".to_vec()),
            ];
            
            create_test_dict(&dict_path, "mdict", &entries).unwrap();
            
            // Test dictionary creation
            assert!(dict_path.exists());
        }

        #[test]
        fn test_fulltext_search_scenarios() {
            let temp_dir = temp_dir().unwrap();
            let dict_path = temp_dir.path().join("test.mdict");
            
            // Create entries with rich content for full-text search
            let mut entries = Vec::new();
            let content_patterns = vec![
                "The quick brown fox jumps over the lazy dog",
                "Programming languages are fascinating",
                "Data structures and algorithms are important",
                "Search algorithms can be complex",
            ];
            
            for (i, pattern) in content_patterns.iter().enumerate() {
                entries.push((format!("term_{}", i), pattern.as_bytes().to_vec()));
            }
            
            create_test_dict(&dict_path, "mdict", &entries).unwrap();
            
            assert!(dict_path.exists());
        }
    }

    /// Test batch operations
    mod batch_operation_tests {
        use super::*;

        #[test]
        fn test_batch_loading() {
            let temp_dir = temp_dir().unwrap();
            
            // Create multiple dictionary files
            let dict1_path = temp_dir.path().join("dict1.mdict");
            let dict2_path = temp_dir.path().join("dict2.mdict");
            let dict3_path = temp_dir.path().join("dict3.mdict");
            
            let entries = generate_test_entries(50);
            
            create_test_dict(&dict1_path, "mdict", &entries).unwrap();
            create_test_dict(&dict2_path, "mdict", &entries).unwrap();
            create_test_dict(&dict3_path, "mdict", &entries).unwrap();
            
            // Test batch validation
            let paths = vec![dict1_path.clone(), dict2_path.clone(), dict3_path.clone()];
            let results = crate::dict::BatchOperations::validate_batch(&paths).unwrap();
            
            assert_eq!(results.len(), 3);
            for (_, is_valid) in results {
                assert!(is_valid);
            }
        }

        #[test]
        fn test_batch_search() {
            let temp_dir = temp_dir().unwrap();
            
            // Create dictionaries with overlapping content
            let dict1_path = temp_dir.path().join("dict1.mdict");
            let dict2_path = temp_dir.path().join("dict2.mdict");
            
            let mut entries1 = Vec::new();
            let mut entries2 = Vec::new();
            
            for i in 0..20 {
                entries1.push((format!("shared_{:03}", i), format!("Dict1 definition {}", i).into_bytes()));
                entries2.push((format!("shared_{:03}", i), format!("Dict2 definition {}", i).into_bytes()));
            }
            
            create_test_dict(&dict1_path, "mdict", &entries1).unwrap();
            create_test_dict(&dict2_path, "mdict", &entries2).unwrap();
            
            assert!(dict1_path.exists());
            assert!(dict2_path.exists());
        }

        #[test]
        fn test_parallel_processing_scenarios() {
            let temp_dir = temp_dir().unwrap();
            let dict_path = temp_dir.path().join("test.mdict");
            
            let entries = generate_test_entries(1000);
            create_test_dict(&dict_path, "mdict", &entries).unwrap();
            
            // Test parallel processing readiness
            assert!(dict_path.exists());
            
            // Check file size for parallel processing suitability
            let size = dict_path.metadata().unwrap().len();
            println!("Dictionary size for parallel processing: {} bytes", size);
        }
    }

    /// Test performance characteristics
    mod performance_integration_tests {
        use super::*;

        #[test]
        fn test_large_dictionary_loading() {
            let temp_dir = temp_dir().unwrap();
            let dict_path = temp_dir.path().join("large.mdict");
            
            // Create a large dictionary
            let entries = generate_large_dataset(10_000);
            create_test_dict(&dict_path, "mdict", &entries).unwrap();
            
            // Verify the dictionary was created
            assert!(dict_path.exists());
            
            let file_size = dict_path.metadata().unwrap().len();
            println!("Large dictionary size: {} bytes", file_size);
            
            // Performance expectation: large dictionaries should load within reasonable time
            assert!(file_size > 0);
        }

        #[test]
        fn test_memory_usage_patterns() {
            let temp_dir = temp_dir().unwrap();
            let dict_path = temp_dir.path().join("memory_test.mdict");
            
            // Create dictionary with large entries
            let mut entries = Vec::new();
            for i in 0..100 {
                let large_content = "x".repeat(10_000); // 10KB per entry
                entries.push((format!("large_{}", i), large_content.into_bytes()));
            }
            
            create_test_dict(&dict_path, "mdict", &entries).unwrap();
            assert!(dict_path.exists());
        }

        #[test]
        fn test_concurrent_access_scenarios() {
            let temp_dir = temp_dir().unwrap();
            let dict_path = temp_dir.path().join("concurrent.mdict");
            
            let entries = generate_test_entries(500);
            create_test_dict(&dict_path, "mdict", &entries).unwrap();
            
            // Simulate concurrent access patterns
            assert!(dict_path.exists());
            
            // Test that file can be read by multiple processes
            let metadata = dict_path.metadata().unwrap();
            assert!(metadata.len() > 0);
        }
    }

    /// Test error handling and recovery
    mod error_recovery_tests {
        use super::*;

        #[test]
        fn test_corrupted_file_handling() {
            let temp_dir = temp_dir().unwrap();
            let dict_path = temp_dir.path().join("corrupted.mdict");
            
            // Create a corrupted file
            let corrupted_content = "This is not a valid dictionary file";
            std::fs::write(&dict_path, corrupted_content).unwrap();
            
            let loader = DictLoader::new();
            
            // Format detection should fail for corrupted files
            let result = loader.detect_format(&dict_path);
            assert!(result.is_err());
        }

        #[test]
        fn test_missing_file_handling() {
            let temp_dir = temp_dir().unwrap();
            let non_existent_path = temp_dir.path().join("nonexistent.mdict");
            
            let loader = DictLoader::new();
            
            // Loading non-existent file should fail gracefully
            assert!(!non_existent_path.exists());
        }

        #[test]
        fn test_permission_denied_scenarios() {
            // This test would require actual permission scenarios
            // For now, test that the library handles missing files gracefully
            
            let temp_dir = temp_dir().unwrap();
            let protected_path = temp_dir.path().join("protected.mdict");
            
            // Create a file and test that it exists
            std::fs::write(&protected_path, "protected content").unwrap();
            assert!(protected_path.exists());
            
            // Test that readable utility works
            assert!(crate::dict::utils::is_readable(&protected_path));
        }

        #[test]
        fn test_partial_file_handling() {
            let temp_dir = temp_dir().unwrap();
            let partial_path = temp_dir.path().join("partial.mdict");
            
            // Create a partially written file
            let incomplete_content = "Incomplete";
            std::fs::write(&partial_path, incomplete_content).unwrap();
            
            // File should exist but be invalid
            assert!(partial_path.exists());
            
            let loader = DictLoader::new();
            let result = loader.detect_format(&partial_path);
            // Should handle gracefully (either detect or fail gracefully)
        }
    }

    /// Test edge cases and boundary conditions
    mod edge_case_integration_tests {
        use super::*;

        #[test]
        fn test_empty_dictionary() {
            let temp_dir = temp_dir().unwrap();
            let empty_dict_path = temp_dir.path().join("empty.mdict");
            
            // Create an empty dictionary
            let empty_entries = Vec::new();
            create_test_dict(&empty_dict_path, "mdict", &empty_entries).unwrap();
            
            assert!(empty_dict_path.exists());
        }

        #[test]
        fn test_single_entry_dictionary() {
            let temp_dir = temp_dir().unwrap();
            let single_dict_path = temp_dir.path().join("single.mdict");
            
            // Create a dictionary with just one entry
            let single_entry = vec![("single_key".to_string(), b"Single entry content".to_vec())];
            create_test_dict(&single_dict_path, "mdict", &single_entry).unwrap();
            
            assert!(single_dict_path.exists());
        }

        #[test]
        fn test_very_long_keys() {
            let temp_dir = temp_dir().unwrap();
            let long_key_dict_path = temp_dir.path().join("long_key.mdict");
            
            // Create entries with very long keys
            let mut entries = Vec::new();
            for i in 0..5 {
                let long_key = "a".repeat(1000) + &format!("_{}", i);
                entries.push((long_key, format!("Definition for long key {}", i).into_bytes()));
            }
            
            create_test_dict(&long_key_dict_path, "mdict", &entries).unwrap();
            assert!(long_key_dict_path.exists());
        }

        #[test]
        fn test_unicode_content() {
            let temp_dir = temp_dir().unwrap();
            let unicode_dict_path = temp_dir.path().join("unicode.mdict");
            
            // Create dictionary with various Unicode content
            let unicode_entries = generate_unicode_entries(20);
            create_test_dict(&unicode_dict_path, "mdict", &unicode_entries).unwrap();
            
            assert!(unicode_dict_path.exists());
        }

        #[test]
        fn test_mixed_content_sizes() {
            let temp_dir = temp_dir().unwrap();
            let mixed_dict_path = temp_dir.path().join("mixed.mdict");
            
            // Create dictionary with varying content sizes
            let mut entries = Vec::new();
            
            // Small entry
            entries.push(("small".to_string(), b"Small".to_vec()));
            
            // Medium entry
            entries.push(("medium".to_string(), b"This is a medium length entry with some content".to_vec()));
            
            // Large entry
            let large_content = "Large content. ".repeat(1000);
            entries.push(("large".to_string(), large_content.into_bytes()));
            
            create_test_dict(&mixed_dict_path, "mdict", &entries).unwrap();
            assert!(mixed_dict_path.exists());
        }
    }
}