terraphim_automata 1.16.34

Automata for searching and processing knowledge graphs
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
use terraphim_automata::autocomplete::{
    AutocompleteConfig, AutocompleteResult, autocomplete_search, build_autocomplete_index,
    deserialize_autocomplete_index, fuzzy_autocomplete_search,
    fuzzy_autocomplete_search_levenshtein, serialize_autocomplete_index,
};

#[cfg(feature = "remote-loading")]
use terraphim_automata::{AutomataPath, load_autocomplete_index};
use terraphim_types::{NormalizedTerm, NormalizedTermValue, Thesaurus};

/// Create a comprehensive test thesaurus with various term patterns
fn create_test_thesaurus() -> Thesaurus {
    let mut thesaurus = Thesaurus::new("Comprehensive Test".to_string());

    let test_data = vec![
        // Programming terms
        ("python", "Python Programming Language", 10),
        ("javascript", "JavaScript Programming Language", 9),
        ("rust", "Rust Programming Language", 15),
        ("programming", "Computer Programming", 8),
        ("algorithm", "Computer Algorithm", 12),
        ("data structure", "Data Structure", 11),
        // Machine Learning terms
        ("machine learning", "Machine Learning", 20),
        ("ml", "Machine Learning", 20),
        ("artificial intelligence", "Artificial Intelligence", 25),
        ("ai", "Artificial Intelligence", 25),
        ("neural network", "Neural Network", 18),
        ("deep learning", "Deep Learning", 22),
        ("supervised learning", "Supervised Learning", 16),
        ("unsupervised learning", "Unsupervised Learning", 14),
        // Data Science terms
        ("data science", "Data Science", 17),
        ("data analysis", "Data Analysis", 13),
        ("statistics", "Statistics", 10),
        ("visualization", "Data Visualization", 12),
        ("pandas", "Pandas Library", 8),
        ("numpy", "NumPy Library", 9),
        // Edge cases
        ("", "Empty Term", 1), // Empty string
        ("a", "Single Character", 2),
        (
            "very-long-term-with-many-hyphens-and-words",
            "Long Hyphenated Term",
            5,
        ),
        ("UPPERCASE", "Uppercase Term", 6),
        ("MixedCase", "Mixed Case Term", 7),
        ("special!@#chars", "Special Characters", 3),
        ("unicode🚀term", "Unicode Term", 4),
        ("   spaces   ", "Spaces Term", 3),
    ];

    for (key, normalized, id) in test_data {
        let normalized_term = NormalizedTerm {
            id,
            value: NormalizedTermValue::from(normalized),
            display_value: None,
            url: Some(format!(
                "https://example.com/{}",
                normalized.replace(' ', "-").to_lowercase()
            )),
        };
        thesaurus.insert(NormalizedTermValue::from(key), normalized_term);
    }

    thesaurus
}

#[test]
fn test_build_autocomplete_index_basic() {
    let thesaurus = create_test_thesaurus();
    let original_len = thesaurus.len();

    let index = build_autocomplete_index(thesaurus, None).unwrap();

    assert_eq!(index.name(), "Comprehensive Test");
    assert_eq!(index.len(), original_len);
    assert!(!index.is_empty());
}

#[test]
fn test_build_autocomplete_index_with_config() {
    let thesaurus = create_test_thesaurus();
    let config = AutocompleteConfig {
        max_results: 5,
        min_prefix_length: 2,
        case_sensitive: true,
    };

    let index = build_autocomplete_index(thesaurus, Some(config)).unwrap();

    assert!(!index.is_empty());
    // Config affects index building behavior
}

#[test]
fn test_autocomplete_search_prefix_matching() {
    let thesaurus = create_test_thesaurus();
    let index = build_autocomplete_index(thesaurus, None).unwrap();

    // Test basic prefix matching
    let results = autocomplete_search(&index, "ma", None).unwrap();
    assert!(!results.is_empty());

    // Should find "machine learning"
    let has_ml = results.iter().any(|r| r.term.contains("machine"));
    assert!(
        has_ml,
        "Should find terms containing 'machine' for prefix 'ma'"
    );

    // Test case insensitive matching
    let results = autocomplete_search(&index, "MA", None).unwrap();
    assert!(!results.is_empty(), "Should handle uppercase prefixes");
}

#[test]
fn test_autocomplete_search_exact_match() {
    let thesaurus = create_test_thesaurus();
    let index = build_autocomplete_index(thesaurus, None).unwrap();

    // Test exact match
    let results = autocomplete_search(&index, "python", None).unwrap();
    assert!(!results.is_empty());

    let python_result = results.iter().find(|r| r.term == "python");
    assert!(
        python_result.is_some(),
        "Should find exact match for 'python'"
    );

    let python_result = python_result.unwrap();
    assert_eq!(python_result.id, 10u64);
    assert_eq!(
        python_result.normalized_term,
        NormalizedTermValue::from("Python Programming Language")
    );
}

#[test]
fn test_autocomplete_search_ordering() {
    let thesaurus = create_test_thesaurus();
    let index = build_autocomplete_index(thesaurus, None).unwrap();

    // Search for terms that should be ordered by score
    let results = autocomplete_search(&index, "a", Some(10)).unwrap();

    // Check that results are sorted by score (descending)
    for i in 1..results.len() {
        assert!(
            results[i - 1].score >= results[i].score,
            "Results should be sorted by score (descending). Position {}: {} > {}",
            i,
            results[i - 1].score,
            results[i].score
        );
    }
}

#[test]
fn test_autocomplete_search_limits() {
    let thesaurus = create_test_thesaurus();
    let index = build_autocomplete_index(thesaurus, None).unwrap();

    // Test limit parameter
    let results = autocomplete_search(&index, "", Some(3)).unwrap();
    assert!(results.len() <= 3, "Should respect limit parameter");

    // Test with different limits
    for limit in [1, 5, 10, 20] {
        let results = autocomplete_search(&index, "a", Some(limit)).unwrap();
        assert!(results.len() <= limit, "Should respect limit of {}", limit);
    }
}

#[test]
fn test_autocomplete_search_empty_and_short_prefixes() {
    let thesaurus = create_test_thesaurus();
    let index = build_autocomplete_index(thesaurus, None).unwrap();

    // Empty prefix - should return results (FST can handle this)
    let _results = autocomplete_search(&index, "", Some(5)).unwrap();
    // FST implementation may return results for empty prefix

    // Single character prefix
    let results = autocomplete_search(&index, "a", Some(10)).unwrap();
    assert!(
        !results.is_empty(),
        "Should find results for single character prefix"
    );
}

#[test]
fn test_autocomplete_search_no_matches() {
    let thesaurus = create_test_thesaurus();
    let index = build_autocomplete_index(thesaurus, None).unwrap();

    // Search for prefix that shouldn't match anything
    let results = autocomplete_search(&index, "xyz123nonexistent", None).unwrap();
    assert!(
        results.is_empty(),
        "Should return empty results for non-matching prefix"
    );
}

#[test]
fn test_autocomplete_search_special_characters() {
    let thesaurus = create_test_thesaurus();
    let index = build_autocomplete_index(thesaurus, None).unwrap();

    // Test searching for terms with special characters
    let _results = autocomplete_search(&index, "special", None).unwrap();
    // May or may not find results depending on normalization

    // Test unicode characters
    let _results = autocomplete_search(&index, "unicode", None).unwrap();
    // Should handle unicode properly
}

#[test]
fn test_fuzzy_autocomplete_search_basic() {
    let thesaurus = create_test_thesaurus();
    let index = build_autocomplete_index(thesaurus, None).unwrap();

    // Test fuzzy search with typos (now using Jaro-Winkler by default)
    let _results = fuzzy_autocomplete_search(&index, "machne", 0.6, Some(5)).unwrap();
    // Should find "machine" related terms even with typo

    let _results = fuzzy_autocomplete_search(&index, "pythonx", 0.6, Some(5)).unwrap();
    // Should find "python" with extra character
}

#[test]
fn test_fuzzy_autocomplete_search_similarity_thresholds() {
    let thesaurus = create_test_thesaurus();
    let index = build_autocomplete_index(thesaurus, None).unwrap();

    // Test different similarity thresholds (now using Jaro-Winkler)
    for similarity in [0.3, 0.5, 0.7] {
        let _results = fuzzy_autocomplete_search(&index, "maching", similarity, Some(10)).unwrap();
        // Higher similarity threshold should potentially find fewer results
    }
}

#[test]
fn test_serialization_roundtrip() {
    let thesaurus = create_test_thesaurus();
    let original_index = build_autocomplete_index(thesaurus, None).unwrap();

    // Serialize
    let serialized = serialize_autocomplete_index(&original_index).unwrap();
    assert!(
        !serialized.is_empty(),
        "Serialized data should not be empty"
    );

    // Deserialize
    let deserialized_index = deserialize_autocomplete_index(&serialized).unwrap();

    // Verify metadata integrity
    assert_eq!(original_index.name(), deserialized_index.name());
    assert_eq!(original_index.len(), deserialized_index.len());

    // Verify search functionality is preserved
    let test_queries = ["ma", "python", "data", "ai"];

    for query in test_queries {
        let original_results = autocomplete_search(&original_index, query, Some(5)).unwrap();
        let deserialized_results =
            autocomplete_search(&deserialized_index, query, Some(5)).unwrap();

        assert_eq!(
            original_results.len(),
            deserialized_results.len(),
            "Result count should match for query: {}",
            query
        );

        for (orig, deser) in original_results.iter().zip(deserialized_results.iter()) {
            assert_eq!(orig.term, deser.term, "Terms should match");
            assert_eq!(orig.id, deser.id, "IDs should match");
            assert_eq!(orig.score, deser.score, "Scores should match");
            assert_eq!(
                orig.normalized_term, deser.normalized_term,
                "Normalized terms should match"
            );
            assert_eq!(orig.url, deser.url, "URLs should match");
        }
    }
}

#[test]
fn test_serialization_empty_index() {
    let empty_thesaurus = Thesaurus::new("Empty".to_string());
    let index = build_autocomplete_index(empty_thesaurus, None).unwrap();

    assert!(index.is_empty());

    // Should be able to serialize/deserialize empty index
    let serialized = serialize_autocomplete_index(&index).unwrap();
    let deserialized = deserialize_autocomplete_index(&serialized).unwrap();

    assert!(deserialized.is_empty());
    assert_eq!(index.name(), deserialized.name());
}

#[cfg(feature = "remote-loading")]
#[tokio::test]
async fn test_load_autocomplete_index_local() {
    // Test loading from local example file
    let result = load_autocomplete_index(&AutomataPath::local_example(), None).await;

    match result {
        Ok(index) => {
            assert!(!index.is_empty(), "Loaded index should not be empty");

            // Test search functionality on loaded index
            let results = autocomplete_search(&index, "foo", None).unwrap();
            // Should find some results from the test data

            // Test that all results have valid metadata
            for result in results {
                assert!(!result.term.is_empty(), "Term should not be empty");
                assert!(
                    !result.normalized_term.as_str().is_empty(),
                    "Normalized term should not be empty"
                );
            }
        }
        Err(e) => {
            // This is acceptable if the test data file doesn't exist
            eprintln!("Could not load test data for autocomplete index: {}", e);
        }
    }
}

#[test]
fn test_autocomplete_config_validation() {
    let thesaurus = create_test_thesaurus();

    // Test various configurations
    let configs = vec![
        AutocompleteConfig {
            max_results: 1,
            min_prefix_length: 1,
            case_sensitive: false,
        },
        AutocompleteConfig {
            max_results: 100,
            min_prefix_length: 3,
            case_sensitive: true,
        },
    ];

    for config in configs {
        let index = build_autocomplete_index(thesaurus.clone(), Some(config)).unwrap();
        assert!(
            !index.is_empty(),
            "Index should build successfully with any valid config"
        );
    }
}

#[test]
fn test_autocomplete_result_metadata() {
    let thesaurus = create_test_thesaurus();
    let index = build_autocomplete_index(thesaurus, None).unwrap();

    let results = autocomplete_search(&index, "python", None).unwrap();
    assert!(!results.is_empty());

    let python_result = results.iter().find(|r| r.term == "python").unwrap();

    // Verify all metadata fields are populated correctly
    assert_eq!(python_result.term, "python");
    assert_eq!(python_result.id, 10u64);
    assert_eq!(
        python_result.normalized_term,
        NormalizedTermValue::from("Python Programming Language")
    );
    assert!(python_result.url.is_some());
    assert!(python_result.score > 0.0);

    // Verify URL format
    let url = python_result.url.as_ref().unwrap();
    assert!(url.starts_with("https://example.com/"));
}

#[test]
fn test_autocomplete_concurrent_access() {
    use std::sync::Arc;
    use std::thread;

    let thesaurus = create_test_thesaurus();
    let index = Arc::new(build_autocomplete_index(thesaurus, None).unwrap());

    // Test concurrent access from multiple threads
    let mut handles = vec![];

    for i in 0..10 {
        let index = index.clone();
        let handle = thread::spawn(move || {
            let query = match i % 4 {
                0 => "ma",
                1 => "python",
                2 => "data",
                _ => "ai",
            };

            autocomplete_search(&index, query, Some(5)).unwrap()
        });
        handles.push(handle);
    }

    // Wait for all threads to complete
    for handle in handles {
        let results = handle.join().unwrap();
        // Each thread should complete successfully
        assert!(results.len() <= 5);
    }
}

#[test]
fn test_autocomplete_performance_characteristics() {
    // Test that search performance is reasonable
    let large_thesaurus = {
        let mut thesaurus = Thesaurus::new("Performance Test".to_string());

        // Create 1000 terms for performance testing
        for i in 0..1000 {
            let term = format!("performance_term_{:04}", i);
            let normalized_term = NormalizedTerm {
                id: i as u64 + 1,
                value: NormalizedTermValue::from(term.clone()),
                display_value: None,
                url: Some(format!("https://example.com/{}", term)),
            };
            thesaurus.insert(NormalizedTermValue::from(term), normalized_term);
        }

        thesaurus
    };

    let start = std::time::Instant::now();
    let index = build_autocomplete_index(large_thesaurus, None).unwrap();
    let build_time = start.elapsed();

    println!("Built index with 1000 terms in {:?}", build_time);
    assert!(
        build_time.as_millis() < 1000,
        "Index building should be fast"
    );

    // Test search performance
    let start = std::time::Instant::now();
    let results = autocomplete_search(&index, "performance", Some(10)).unwrap();
    let search_time = start.elapsed();

    println!(
        "Search completed in {:?} with {} results",
        search_time,
        results.len()
    );
    assert!(search_time.as_millis() < 100, "Search should be very fast");
}

#[test]
fn test_autocomplete_config_defaults() {
    let config = AutocompleteConfig::default();

    assert_eq!(config.max_results, 10);
    assert_eq!(config.min_prefix_length, 1);
    assert!(!config.case_sensitive);
}

#[test]
fn test_autocomplete_result_equality() {
    let result1 = AutocompleteResult {
        term: "test".to_string(),
        normalized_term: NormalizedTermValue::from("Test Term"),
        id: 1u64,
        url: Some("https://example.com/test".to_string()),
        score: 10.0,
    };

    let result2 = AutocompleteResult {
        term: "test".to_string(),
        normalized_term: NormalizedTermValue::from("Test Term"),
        id: 1u64,
        url: Some("https://example.com/test".to_string()),
        score: 10.0,
    };

    assert_eq!(result1, result2);
}

// Property-based testing
#[test]
fn test_autocomplete_property_all_results_start_with_prefix() {
    let thesaurus = create_test_thesaurus();
    let index = build_autocomplete_index(thesaurus, None).unwrap();

    let prefixes = ["a", "ma", "py", "data", "ai"];

    for prefix in prefixes {
        let results = autocomplete_search(&index, prefix, None).unwrap();

        for result in results {
            let term_lower = result.term.to_lowercase();
            let prefix_lower = prefix.to_lowercase();

            // Note: FST implementation may not guarantee prefix matching at character level
            // This test validates the expectation but may need adjustment based on FST behavior
            if !term_lower.starts_with(&prefix_lower) {
                println!(
                    "Warning: Term '{}' doesn't start with prefix '{}'",
                    result.term, prefix
                );
                // This might be expected behavior for FST-based implementation
            }
        }
    }
}

#[test]
fn test_autocomplete_property_result_limits_respected() {
    let thesaurus = create_test_thesaurus();
    let index = build_autocomplete_index(thesaurus, None).unwrap();

    for limit in [1, 5, 10, 50] {
        let results = autocomplete_search(&index, "a", Some(limit)).unwrap();
        assert!(
            results.len() <= limit,
            "Result count should not exceed limit {}",
            limit
        );
    }
}

#[test]
fn test_autocomplete_property_score_ordering() {
    let thesaurus = create_test_thesaurus();
    let index = build_autocomplete_index(thesaurus, None).unwrap();

    let results = autocomplete_search(&index, "a", Some(20)).unwrap();

    for i in 1..results.len() {
        assert!(
            results[i - 1].score >= results[i].score,
            "Scores should be in descending order: position {} has score {} > {}",
            i,
            results[i - 1].score,
            results[i].score
        );
    }
}

// ===== Jaro-Winkler vs Levenshtein Comparison Tests =====

#[test]
fn test_jaro_winkler_autocomplete_basic() {
    let thesaurus = create_test_thesaurus();
    let index = build_autocomplete_index(thesaurus, None).unwrap();

    // Test Jaro-Winkler fuzzy search with typos (now the default fuzzy_autocomplete_search)
    let results = fuzzy_autocomplete_search(&index, "machne", 0.6, Some(5)).unwrap();

    // Should find "machine learning" even with typo
    let has_machine = results.iter().any(|r| r.term.contains("machine"));
    assert!(
        has_machine,
        "Jaro-Winkler should find 'machine' for typo 'machne'"
    );

    println!("Jaro-Winkler results for 'machne':");
    for result in &results {
        println!("  {} (score: {:.3})", result.term, result.score);
    }
}

#[test]
fn test_comparison_prefix_emphasis() {
    let thesaurus = create_test_thesaurus();
    let index = build_autocomplete_index(thesaurus, None).unwrap();

    // Test with query that has good prefix match but character transposition
    let query = "machien"; // "machine" with transposed 'i' and 'e'

    let levenshtein_results =
        fuzzy_autocomplete_search_levenshtein(&index, query, 2, Some(5)).unwrap();
    let jaro_winkler_results = fuzzy_autocomplete_search(&index, query, 0.6, Some(5)).unwrap();

    println!("Comparison for query '{}' (transposed characters):", query);
    println!("Levenshtein (edit distance ≤ 2):");
    for result in &levenshtein_results {
        println!("  {} (score: {:.3})", result.term, result.score);
    }

    println!("Jaro-Winkler (similarity ≥ 0.6):");
    for result in &jaro_winkler_results {
        println!("  {} (score: {:.3})", result.term, result.score);
    }

    // Both should find machine learning related terms
    let lev_has_machine = levenshtein_results
        .iter()
        .any(|r| r.term.contains("machine"));
    let jw_has_machine = jaro_winkler_results
        .iter()
        .any(|r| r.term.contains("machine"));

    assert!(
        lev_has_machine || jw_has_machine,
        "At least one method should find 'machine' terms"
    );
}

#[test]
fn test_comparison_different_typo_patterns() {
    let thesaurus = create_test_thesaurus();
    let index = build_autocomplete_index(thesaurus, None).unwrap();

    let test_cases = vec![
        ("pythno", "python"),            // transposition
        ("pythn", "python"),             // missing character
        ("pythonx", "python"),           // extra character
        ("pyton", "python"),             // missing character
        ("machne", "machine"),           // missing character
        ("machien", "machine"),          // transposition
        ("datascience", "data science"), // missing space
        ("aritificial", "artificial"),   // transposition + missing
    ];

    for (typo, target) in test_cases {
        println!("\n=== Testing typo pattern: '{}' → '{}' ===", typo, target);

        let lev_results = fuzzy_autocomplete_search_levenshtein(&index, typo, 2, Some(3)).unwrap();
        let jw_results = fuzzy_autocomplete_search(&index, typo, 0.5, Some(3)).unwrap();

        println!("Levenshtein results:");
        for result in &lev_results {
            println!("  {} (score: {:.3})", result.term, result.score);
        }

        println!("Jaro-Winkler results:");
        for result in &jw_results {
            println!("  {} (score: {:.3})", result.term, result.score);
        }

        // Check if either method finds the target term
        let lev_finds_target = lev_results.iter().any(|r| {
            r.term.contains(target) || r.normalized_term.as_str().to_lowercase().contains(target)
        });
        let jw_finds_target = jw_results.iter().any(|r| {
            r.term.contains(target) || r.normalized_term.as_str().to_lowercase().contains(target)
        });

        if !lev_finds_target && !jw_finds_target {
            println!(
                "Neither method found target '{}' for typo '{}'",
                target, typo
            );
        } else {
            println!(
                "Success: {} found target",
                match (lev_finds_target, jw_finds_target) {
                    (true, true) => "Both methods",
                    (true, false) => "Levenshtein",
                    (false, true) => "Jaro-Winkler",
                    _ => unreachable!(),
                }
            );
        }
    }
}

#[test]
fn test_jaro_winkler_prefix_advantage() {
    let thesaurus = create_test_thesaurus();
    let index = build_autocomplete_index(thesaurus, None).unwrap();

    // Test cases where Jaro-Winkler should perform better due to prefix matching
    let prefix_cases = vec![
        "mach",  // Should strongly match "machine learning"
        "prog",  // Should match "programming"
        "artif", // Should match "artificial intelligence"
        "super", // Should match "supervised learning"
    ];

    for prefix in prefix_cases {
        println!("\n=== Testing prefix advantage: '{}' ===", prefix);

        // Use lower similarity threshold for Jaro-Winkler to see more results
        let jw_results = fuzzy_autocomplete_search(&index, prefix, 0.4, Some(5)).unwrap();

        println!("Jaro-Winkler results (min similarity 0.4):");
        for result in &jw_results {
            println!("  {} (score: {:.3})", result.term, result.score);
        }

        // Jaro-Winkler should find good matches for prefix-based queries
        assert!(
            !jw_results.is_empty(),
            "Jaro-Winkler should find matches for prefix '{}'",
            prefix
        );

        // Check that top results have decent scores (Jaro-Winkler emphasizes prefixes)
        if !jw_results.is_empty() {
            let top_score = jw_results[0].score;
            println!("Top Jaro-Winkler score for '{}': {:.3}", prefix, top_score);
        }
    }
}

#[test]
fn test_similarity_thresholds_comparison() {
    let thesaurus = create_test_thesaurus();
    let index = build_autocomplete_index(thesaurus, None).unwrap();

    let query = "machne"; // typo for "machine"

    // Test different similarity thresholds for Jaro-Winkler
    let thresholds = [0.3, 0.5, 0.7, 0.8, 0.9];

    println!("Jaro-Winkler threshold comparison for query '{}':", query);

    for &threshold in &thresholds {
        let results = fuzzy_autocomplete_search(&index, query, threshold, Some(10)).unwrap();
        println!("  Threshold {:.1}: {} results", threshold, results.len());

        if !results.is_empty() {
            println!(
                "    Top result: {} (score: {:.3})",
                results[0].term, results[0].score
            );
        }
    }

    // Test different edit distances for Levenshtein
    let edit_distances = [1, 2, 3];

    println!(
        "Levenshtein edit distance comparison for query '{}':",
        query
    );

    for &edit_distance in &edit_distances {
        let results =
            fuzzy_autocomplete_search_levenshtein(&index, query, edit_distance, Some(10)).unwrap();
        println!(
            "  Edit distance {}: {} results",
            edit_distance,
            results.len()
        );

        if !results.is_empty() {
            println!(
                "    Top result: {} (score: {:.3})",
                results[0].term, results[0].score
            );
        }
    }
}

#[test]
fn test_performance_comparison() {
    let thesaurus = create_test_thesaurus();
    let index = build_autocomplete_index(thesaurus, None).unwrap();

    let test_queries = ["machne", "pythno", "datascience", "aritificial"];

    for query in test_queries {
        println!("\n=== Performance comparison for '{}' ===", query);

        // Measure Levenshtein performance
        let start = std::time::Instant::now();
        let lev_results = fuzzy_autocomplete_search_levenshtein(&index, query, 2, Some(5)).unwrap();
        let lev_time = start.elapsed();

        // Measure Jaro-Winkler performance
        let start = std::time::Instant::now();
        let jw_results = fuzzy_autocomplete_search(&index, query, 0.5, Some(5)).unwrap();
        let jw_time = start.elapsed();

        println!(
            "Levenshtein: {:?} ({} results)",
            lev_time,
            lev_results.len()
        );
        println!("Jaro-Winkler: {:?} ({} results)", jw_time, jw_results.len());

        // Both should complete reasonably quickly
        assert!(lev_time.as_millis() < 1000, "Levenshtein should be fast");
        assert!(jw_time.as_millis() < 1000, "Jaro-Winkler should be fast");
    }
}

#[test]
fn test_word_level_matching_comparison() {
    let thesaurus = create_test_thesaurus();
    let index = build_autocomplete_index(thesaurus, None).unwrap();

    // Test word-level matching with multi-word terms
    let query = "learing"; // typo for "learning" in "machine learning"

    let lev_results = fuzzy_autocomplete_search_levenshtein(&index, query, 2, Some(5)).unwrap();
    let jw_results = fuzzy_autocomplete_search(&index, query, 0.6, Some(5)).unwrap();

    println!("Word-level matching comparison for '{}':", query);
    println!("Levenshtein results:");
    for result in &lev_results {
        println!("  {} (score: {:.3})", result.term, result.score);
    }

    println!("Jaro-Winkler results:");
    for result in &jw_results {
        println!("  {} (score: {:.3})", result.term, result.score);
    }

    // Both should find "machine learning" by matching the word "learning"
    let lev_has_learning = lev_results.iter().any(|r| r.term.contains("learning"));
    let jw_has_learning = jw_results.iter().any(|r| r.term.contains("learning"));

    if lev_has_learning || jw_has_learning {
        println!("Success: At least one method found 'learning' terms");
    }
}