matcher_rs 0.14.2

A high-performance matcher designed to solve LOGICAL and TEXT VARIATIONS problems in word matching, implemented in 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
use matcher_rs::{
    ProcessType, SimpleMatcherBuilder, reduce_text_process, reduce_text_process_emit, text_process,
};

// ===========================================================================
// Standalone transform API: text_process, reduce_text_process, etc.
// ===========================================================================

#[test]
fn test_delete_simd_skip_ascii_before_non_ascii() {
    // Regression: SIMD fast-skip in DeleteFindIter incorrectly advanced to the first
    // non-ASCII byte without checking for deletable ASCII bytes before it. Spaces
    // between non-deletable ASCII letters and Chinese characters were not deleted.
    let variants = reduce_text_process(ProcessType::VariantNormDeleteNormalize, "A B 測試 A 1");
    assert_eq!(variants[0], "A B 測試 A 1");
    assert_eq!(variants[1], "A B 测试 A 1");
    assert_eq!(variants[2], "AB测试A1");
    assert_eq!(variants[3], "ab测试a1");
}

#[test]
fn test_reduce_text_process() {
    let variants = reduce_text_process(ProcessType::VariantNormDeleteNormalize, "!A!測試!1!");

    // Step-by-step:
    // 0. Original: "!A!測試!1!"
    // 1. VariantNorm:  "!A!测试!1!"
    // 2. Delete:   "A测试1"
    // 3. Normalize:"a测试1"

    assert_eq!(variants.len(), 4);
    assert_eq!(variants[0], "!A!測試!1!");
    assert_eq!(variants[1], "!A!测试!1!");
    assert_eq!(variants[2], "A测试1");
    assert_eq!(variants[3], "a测试1");
}

#[test]
fn test_reduce_text_process_emit() {
    let variants =
        reduce_text_process_emit(ProcessType::VariantNormDeleteNormalize, "!A!測試!1!");

    // emit behavior: replace-type steps overwrite; Delete appends.
    // 1. Start:    ["!A!測試!1!"]
    // 2. VariantNorm:  ["!A!测试!1!"]  (overwritten)
    // 3. Delete:   ["!A!测试!1!", "A测试1"]  (pushed)
    // 4. Normalize:["!A!测试!1!", "a测试1"]  (overwritten last)

    assert_eq!(variants.len(), 2);
    assert_eq!(variants[0], "!A!测试!1!");
    assert_eq!(variants[1], "a测试1");
}

#[test]
fn test_reduce_text_process_all_combined() {
    let text = reduce_text_process(
        ProcessType::VariantNorm
            | ProcessType::Delete
            | ProcessType::Normalize
            | ProcessType::Romanize
            | ProcessType::RomanizeChar,
        "A!漢語西安1",
    );

    // Final result should be fully normalized romanize
    assert_eq!(text.last().unwrap(), "a han yu xi an1");
}

// ===========================================================================
// Exhaustive process-map validation
// ===========================================================================

const VARIANT_NORM_TEST_DATA: &str = include_str!("../process_map/VARIANT_NORM.txt");
const DELETE_TEST_DATA: &str = include_str!("../process_map/TEXT-DELETE.txt");
const NORM_TEST_DATA: &str = include_str!("../process_map/NORM.txt");
const NUM_NORM_TEST_DATA: &str = include_str!("../process_map/NUM-NORM.txt");
const ROMANIZE_TEST_DATA: &str = include_str!("../process_map/ROMANIZE.txt");

#[test]
fn test_process_map_ascii_invariants() {
    assert!(
        VARIANT_NORM_TEST_DATA
            .trim()
            .lines()
            .filter(|l| !l.starts_with('#'))
            .all(|line| !line
                .split('\t')
                .next()
                .expect("Missing VARIANT_NORM key")
                .is_ascii()),
        "VARIANT_NORM.txt should not contain ASCII keys"
    );

    let mut saw_ascii_delete = false;
    for token in DELETE_TEST_DATA
        .trim()
        .lines()
        .filter(|l| !l.starts_with('#'))
    {
        let cp = u32::from_str_radix(
            token
                .strip_prefix("U+")
                .expect("TEXT-DELETE entries must use U+XXXX format"),
            16,
        )
        .expect("TEXT-DELETE entry must contain a valid hexadecimal codepoint");
        saw_ascii_delete |= cp < 0x80;
    }
    assert!(
        saw_ascii_delete,
        "TEXT-DELETE.txt should contain ASCII delete entries"
    );

    for data in [NORM_TEST_DATA, NUM_NORM_TEST_DATA] {
        for line in data.trim().lines().filter(|l| !l.starts_with('#')) {
            let mut split = line.split('\t');
            let k = split.next().expect("Missing normalize key");
            let v = split.next().expect("Missing normalize value");
            assert!(
                !k.is_ascii() || v.is_ascii(),
                "ASCII normalize key must map to ASCII output: {k:?} -> {v:?}"
            );
        }
    }

    for line in ROMANIZE_TEST_DATA
        .trim()
        .lines()
        .filter(|l| !l.starts_with('#'))
    {
        let mut split = line.split('\t');
        let k = split.next().expect("Missing romanize key");
        let v = split.next().expect("Missing romanize value");
        assert!(!k.is_ascii(), "ROMANIZE.txt should not contain ASCII keys");
        assert!(v.is_ascii(), "ROMANIZE.txt values should stay ASCII");
    }
}

#[test]
fn test_process_map_variant_norm_exhaustive() {
    for line in VARIANT_NORM_TEST_DATA
        .trim()
        .lines()
        .filter(|l| !l.starts_with('#'))
    {
        let mut split = line.split('\t');
        let k = split.next().expect("Missing key in VARIANT_NORM.txt");
        let v = split.next().expect("Missing value in VARIANT_NORM.txt");

        assert_eq!(
            k.chars().count(),
            1,
            "VARIANT_NORM key must be one char: {k:?}"
        );
        assert_eq!(
            v.chars().count(),
            1,
            "VARIANT_NORM value must be one char: {v:?}"
        );
        assert_eq!(
            text_process(ProcessType::VariantNorm, k),
            v,
            "VariantNorm failed for {}",
            k
        );
    }
}

#[test]
fn test_process_map_delete_exhaustive() {
    for token in DELETE_TEST_DATA
        .trim()
        .lines()
        .filter(|l| !l.starts_with('#'))
    {
        let cp = u32::from_str_radix(
            token
                .strip_prefix("U+")
                .expect("TEXT-DELETE entries must use U+XXXX format"),
            16,
        )
        .expect("TEXT-DELETE entry must contain a valid hexadecimal codepoint");
        let ws = char::from_u32(cp).unwrap().to_string();
        assert_eq!(
            text_process(ProcessType::Delete, &ws),
            "",
            "Delete failed for codepoint U+{cp:04X}"
        );
    }
}

#[test]
fn test_process_map_normalize_exhaustive() {
    use std::collections::HashMap;
    let mut merged_map = HashMap::new();

    // Merging logic matches the step registry: NORM then NUM_NORM overwrites
    for data in [NORM_TEST_DATA, NUM_NORM_TEST_DATA] {
        for line in data.trim().lines().filter(|l| !l.starts_with('#')) {
            let mut split = line.split('\t');
            let k = split.next().expect("Missing key");
            let v = split.next().expect("Missing value");
            if k != v {
                merged_map.insert(k, v);
            }
        }
    }

    for (k, v) in merged_map {
        assert_eq!(
            text_process(ProcessType::Normalize, k),
            v,
            "Normalize failed for {}",
            k
        );
    }
}

#[test]
fn test_process_map_romanize_exhaustive() {
    for line in ROMANIZE_TEST_DATA
        .trim()
        .lines()
        .filter(|l| !l.starts_with('#'))
    {
        let mut split = line.split('\t');
        let k = split.next().expect("Missing key in ROMANIZE.txt");
        let v = split.next().expect("Missing value in ROMANIZE.txt");
        assert_eq!(k.chars().count(), 1, "ROMANIZE key must be one char: {k:?}");
        assert!(!v.is_empty(), "ROMANIZE value must not be empty for {k:?}");

        assert_eq!(
            text_process(ProcessType::Romanize, k),
            v,
            "Romanize failed for {}",
            k
        );
    }
}

#[test]
fn test_process_map_romanize_char_exhaustive() {
    for line in ROMANIZE_TEST_DATA
        .trim()
        .lines()
        .filter(|l| !l.starts_with('#'))
    {
        let mut split = line.split('\t');
        let k = split.next().expect("Missing key in ROMANIZE.txt");
        let v = split.next().expect("Missing value in ROMANIZE.txt");
        assert_eq!(k.chars().count(), 1, "ROMANIZE key must be one char: {k:?}");
        assert!(!v.is_empty(), "ROMANIZE value must not be empty for {k:?}");

        assert_eq!(
            text_process(ProcessType::RomanizeChar, k),
            v.trim(),
            "RomanizeChar failed for {}",
            k
        );
    }
}

// ===========================================================================
// Individual ProcessTypes through SimpleMatcher
// ===========================================================================

#[test]
fn test_variant_norm() {
    use matcher_rs::SimpleMatcher;
    use std::collections::HashMap;

    let simple_matcher = SimpleMatcher::new(&HashMap::from([(
        ProcessType::VariantNorm,
        HashMap::from([(1, "测试")]),
    )]))
    .unwrap();
    assert!(
        simple_matcher.is_match("測試"),
        "VariantNorm should match traditional variant of 测试"
    );

    let simple_matcher = SimpleMatcher::new(&HashMap::from([(
        ProcessType::VariantNorm,
        HashMap::from([(1, "測試")]),
    )]))
    .unwrap();
    assert!(
        simple_matcher.is_match("测试"),
        "VariantNorm should match simplified variant of 測試"
    );
}

#[test]
fn test_normalize() {
    use matcher_rs::SimpleMatcher;
    use std::collections::HashMap;

    let simple_matcher = SimpleMatcher::new(&HashMap::from([(
        ProcessType::Normalize,
        HashMap::from([(1, "ab41°f")]),
    )]))
    .unwrap();
    assert!(
        simple_matcher.is_match("ABⅣ①℉"),
        "Normalize should map compatibility chars via NFKC + casefold"
    );
}

#[test]
fn test_normalize_strips_diacritics() {
    let matcher = SimpleMatcherBuilder::new()
        .add_word(ProcessType::Normalize, 1, "cafe")
        .add_word(ProcessType::Normalize, 2, "c")
        .build()
        .unwrap();

    assert!(matcher.is_match("Café"), "should strip precomposed acute");
    assert!(matcher.is_match("café"), "should strip lowercase acute");
    assert!(matcher.is_match("CAFÉ"), "should strip uppercase acute");
    assert!(matcher.is_match("Ć"), "Ć should normalize to c");
}

#[test]
fn test_normalize_leaf_applies_ascii_mappings() {
    let matcher = SimpleMatcherBuilder::new()
        .add_word(ProcessType::Normalize, 1, "i")
        .build()
        .unwrap();

    assert!(
        matcher.is_match("I"),
        "Normalize leaf path should apply ASCII mappings like I -> i"
    );
}

#[test]
fn test_normalize_combining_characters() {
    // Normalize uses a per-codepoint lookup table, NOT full NFC composition.
    // Precomposed chars (é = U+00E9) have table entries that strip diacritics.
    // Decomposed combining marks (U+0301) are standalone codepoints without table
    // entries, so they pass through unchanged.
    let matcher = SimpleMatcherBuilder::new()
        .add_word(ProcessType::Normalize, 1, "cafe")
        .build()
        .unwrap();

    // Precomposed: "café" = U+0063 U+0061 U+0066 U+00E9 → "cafe"
    assert!(
        matcher.is_match("caf\u{00E9}"),
        "precomposed é should normalize to cafe via table lookup"
    );

    // Decomposed: "cafe\u{0301}" → "cafe" + combining mark passes through
    // The combining mark is a separate codepoint not in the normalize table,
    // so the output is "cafe\u{0301}" (the "cafe" prefix still matches though).
    assert!(
        matcher.is_match("cafe\u{0301}"),
        "cafe prefix matches even with trailing combining mark"
    );

    // Verify the precomposed form actually strips the diacritic
    let precomposed = text_process(ProcessType::Normalize, "caf\u{00E9}");
    assert_eq!(precomposed.as_ref(), "cafe", "precomposed é → e via table");

    // Verify the decomposed combining mark passes through (not composed then stripped)
    let decomposed = text_process(ProcessType::Normalize, "\u{0301}");
    assert_eq!(
        decomposed.as_ref(),
        "\u{0301}",
        "standalone combining mark has no table entry, passes through"
    );
}

#[test]
fn test_romanize() {
    use matcher_rs::SimpleMatcher;
    use std::collections::HashMap;

    let simple_matcher = SimpleMatcher::new(&HashMap::from([(
        ProcessType::Romanize,
        HashMap::from([(1, "西安")]),
    )]))
    .unwrap();
    assert!(
        simple_matcher.is_match("洗按"),
        "Romanize xi an should match 洗按 (xi an)"
    );
    assert!(
        !simple_matcher.is_match(""),
        "Romanize xi an should not match 现 (xian without space)"
    );
}

#[test]
fn test_romanize_skips_ascii_digits() {
    // Romanize/RomanizeChar should skip ASCII digits (no ASCII keys in generated table)
    for pt in [ProcessType::Romanize, ProcessType::RomanizeChar] {
        let matcher = SimpleMatcherBuilder::new()
            .add_word(pt, 1, "yi")
            .build()
            .unwrap();
        assert!(!matcher.is_match("1"), "{pt:?} should skip ASCII digits");
    }
}

#[test]
fn test_romanizechar() {
    use matcher_rs::SimpleMatcher;
    use std::collections::HashMap;

    let simple_matcher = SimpleMatcher::new(&HashMap::from([(
        ProcessType::RomanizeChar,
        HashMap::from([(1, "西安")]),
    )]))
    .unwrap();
    assert!(
        simple_matcher.is_match("洗按"),
        "RomanizeChar xi an should match 洗按"
    );
    assert!(
        simple_matcher.is_match(""),
        "RomanizeChar xi an should match 现 (xian without space)"
    );
    assert!(
        simple_matcher.is_match("xian"),
        "RomanizeChar should match literal xian"
    );
}

// ===========================================================================
// Composite ProcessTypes through SimpleMatcher
// ===========================================================================

#[test]
fn test_cross_variant_matching() {
    let matcher = SimpleMatcherBuilder::new()
        .add_word(ProcessType::None | ProcessType::Romanize, 1, "apple&西安")
        .build()
        .unwrap();

    assert!(
        matcher.is_match("apple 洗按"),
        "Cross-variant matching should work: 'apple' (None) and '西安' (Romanize)"
    );
}

#[test]
fn test_not_disqualification_across_variants() {
    let matcher = SimpleMatcherBuilder::new()
        .add_word(ProcessType::None | ProcessType::Delete, 1, "apple~pie")
        .build()
        .unwrap();

    assert!(
        !matcher.is_match("apple p.i.e"),
        "NOT disqualification should be global across variants"
    );
}

#[test]
fn test_complex_dag_transformations() {
    let matcher = SimpleMatcherBuilder::new()
        .add_word(
            ProcessType::VariantNorm | ProcessType::Delete | ProcessType::Normalize,
            1,
            "测试",
        )
        .build()
        .unwrap();

    assert!(
        matcher.is_match("測!試"),
        "Should match with VariantNorm and Delete combined"
    );
}

#[test]
fn test_complex_process_type_interactions() {
    let matcher = SimpleMatcherBuilder::new()
        .add_word(
            ProcessType::VariantNorm | ProcessType::Romanize,
            1,
            "apple&西安",
        )
        .build()
        .unwrap();

    assert!(!matcher.is_match("測 洗按"));
    assert!(matcher.is_match("apple 測 洗按"));
}

#[test]
fn test_process_type_none_with_delete() {
    // Composite None|Delete matches both raw text and delete-stripped text.
    let matcher = SimpleMatcherBuilder::new()
        .add_word(ProcessType::None | ProcessType::Delete, 1, "hello")
        .build()
        .unwrap();

    assert!(matcher.is_match("hello"), "raw match via None");
    assert!(matcher.is_match("h.e.l.l.o"), "stripped match via Delete");
    assert!(!matcher.is_match("hallo"));
}

#[test]
fn test_process_type_variant_norm_romanize() {
    let matcher = SimpleMatcherBuilder::new()
        .add_word(ProcessType::VariantNorm | ProcessType::Romanize, 1, "测试")
        .build()
        .unwrap();

    assert!(matcher.is_match("测试"), "simplified direct");
    assert!(
        matcher.is_match("測試"),
        "traditional variant via VariantNorm"
    );
    // Romanize path: different CJK chars with same romanize syllables
    assert!(
        matcher.is_match("策士"),
        "different chars with same romanize 'ce shi'"
    );
}

// ===========================================================================
// Unicode robustness
// ===========================================================================

#[test]
fn test_unicode_emoji_passthrough() {
    let matcher = SimpleMatcherBuilder::new()
        .add_word(ProcessType::VariantNormDeleteNormalize, 1, "test")
        .build()
        .unwrap();

    // Emoji and ZWJ sequences should not crash or corrupt matching
    let text = "test 👨\u{200D}👩\u{200D}👧\u{200D}👦 🎉";
    assert!(matcher.is_match(text));
    let results = matcher.process(text);
    assert_eq!(results.len(), 1);
    assert_eq!(results[0].word_id, 1);
}

#[test]
fn test_unicode_combining_marks() {
    let matcher = SimpleMatcherBuilder::new()
        .add_word(ProcessType::None, 1, "cafe")
        .build()
        .unwrap();

    // "cafe\u{0301}" = "café" with combining acute accent (5 codepoints, but "cafe" is a prefix)
    assert!(
        matcher.is_match("cafe\u{0301}"),
        "cafe should match as byte-prefix of cafe + combining accent"
    );
    let results = matcher.process("cafe\u{0301}");
    assert_eq!(results.len(), 1);
}

#[test]
fn test_process_into_with_transforms() {
    // process_into under Delete (currently only tested under ProcessType::None)
    let delete_matcher = SimpleMatcherBuilder::new()
        .add_word(ProcessType::Delete, 1, "hello&world")
        .build()
        .unwrap();

    let mut results = Vec::new();
    delete_matcher.process_into("h.e.l.l.o w.o.r.l.d", &mut results);
    assert_eq!(results.len(), 1);
    assert_eq!(results[0].word_id, 1);

    // process_into under VariantNorm
    let variant_norm_matcher = SimpleMatcherBuilder::new()
        .add_word(ProcessType::VariantNorm, 1, "测试")
        .build()
        .unwrap();

    results.clear();
    variant_norm_matcher.process_into("測試", &mut results);
    assert_eq!(results.len(), 1);
    assert_eq!(results[0].word_id, 1);
}

// ===========================================================================
// Romanize non-ASCII pattern matching
// ===========================================================================

#[test]
fn test_romanize_non_ascii_pattern_match() {
    // End-to-end: a non-ASCII pattern registered under Romanize must still be
    // found when the Romanize-transformed text contains unmapped non-ASCII chars.
    let matcher = SimpleMatcherBuilder::new()
        .add_word(ProcessType::Romanize, 1, "한글")
        .build()
        .unwrap();
    // Input: Chinese "你" (converted to romanize) + Korean "한글" (passes through).
    assert!(
        matcher.is_match("你한글"),
        "non-ASCII pattern under Romanize should match when unmapped chars pass through"
    );
    let results = matcher.process("你한글");
    assert_eq!(results.len(), 1);
    assert_eq!(results[0].word_id, 1);
}

// ===========================================================================
// ProcessType Display formatting
// ===========================================================================

#[test]
fn test_process_type_display() {
    assert_eq!(format!("{}", ProcessType::None), "none");
    assert_eq!(format!("{}", ProcessType::VariantNorm), "variant_norm");
    assert_eq!(format!("{}", ProcessType::Delete), "delete");
    assert_eq!(format!("{}", ProcessType::Normalize), "normalize");
    assert_eq!(format!("{}", ProcessType::Romanize), "romanize");
    assert_eq!(format!("{}", ProcessType::RomanizeChar), "romanize_char");
    assert_eq!(
        format!("{}", ProcessType::VariantNorm | ProcessType::Delete),
        "variant_norm_delete"
    );
    assert_eq!(
        format!("{}", ProcessType::VariantNormDeleteNormalize),
        "variant_norm_delete_normalize"
    );
    assert_eq!(format!("{}", ProcessType::empty()), "");
}

// ===========================================================================
// RomanizeChar passthrough for unmapped scripts
// ===========================================================================

#[test]
fn test_romanizechar_passthrough_unmapped() {
    // Thai has no Romanize mapping — passes through RomanizeChar unchanged.
    let matcher = SimpleMatcherBuilder::new()
        .add_word(ProcessType::RomanizeChar, 1, "สวัสดี")
        .build()
        .unwrap();
    assert!(matcher.is_match("สวัสดี"));
    assert!(!matcher.is_match("sawasdee"));
}

// ===========================================================================
// Streaming scan paths (search.rs coverage)
// ===========================================================================

#[test]
fn test_streaming_scan_normalize() {
    // Normalize + VariantNorm forces General mode; the Normalize leaf uses
    // scan_variant_streaming through the NormalizeMatcher byte iterator.
    let matcher = SimpleMatcherBuilder::new()
        .add_word(ProcessType::Normalize, 1, "ab")
        .add_word(ProcessType::VariantNorm, 2, "测试")
        .build()
        .unwrap();

    assert!(matcher.is_match("AB"), "fullwidth -> normalized to ab");
    assert!(
        matcher.is_match("測試"),
        "traditional -> simplified via VariantNorm"
    );
    let results = matcher.process("AB 測試");
    assert_eq!(results.len(), 2);
}

#[test]
fn test_streaming_scan_romanize() {
    // Romanize + VariantNorm forces General mode; the Romanize leaf uses
    // scan_variant_streaming through the RomanizeMatcher byte iterator.
    let matcher = SimpleMatcherBuilder::new()
        .add_word(ProcessType::Romanize, 1, "xi an")
        .add_word(ProcessType::VariantNorm, 2, "测试")
        .build()
        .unwrap();

    assert!(matcher.is_match("西安"), "romanize conversion");
    assert!(matcher.is_match("測試"), "variant_norm conversion");
}

#[test]
fn test_leaf_ascii_noop_optimization() {
    // VariantNorm is a no-op on pure ASCII. When parent text is ASCII and the leaf
    // step is VariantNorm, the scan reuses parent text (no materialization).
    let matcher = SimpleMatcherBuilder::new()
        .add_word(ProcessType::VariantNorm, 1, "hello")
        .add_word(ProcessType::None, 2, "world")
        .build()
        .unwrap();

    assert!(matcher.is_match("hello world"));
    let results = matcher.process("hello world");
    assert_eq!(results.len(), 2);
}

// ===========================================================================
// Unicode edge cases
// ===========================================================================

#[test]
fn test_unicode_private_use_area() {
    // U+E000..U+F8FF are Private Use Area chars — should pass through transforms unchanged
    let pua = "\u{E000}\u{E001}\u{F8FF}";
    let matcher = SimpleMatcherBuilder::new()
        .add_word(ProcessType::None, 1, pua)
        .build()
        .unwrap();
    assert!(matcher.is_match(pua));

    // VariantNorm should not alter PUA chars
    let result = text_process(ProcessType::VariantNorm, pua);
    assert_eq!(result.as_ref(), pua);
}

#[test]
fn test_unicode_4byte_emoji_pattern() {
    let emoji_pattern = "test\u{1F389}"; // test🎉
    let matcher = SimpleMatcherBuilder::new()
        .add_word(ProcessType::None, 1, emoji_pattern)
        .build()
        .unwrap();
    assert!(matcher.is_match("test\u{1F389}"));
    assert!(!matcher.is_match("test"));
    assert!(!matcher.is_match("\u{1F389}"));
}

// ===========================================================================
// CJK script expansion: table-driven text_process validation
// ===========================================================================

#[test]
fn test_text_process_cjk_scripts() {
    let cases: &[(ProcessType, &str, &str)] = &[
        // VariantNorm: halfwidth katakana → fullwidth
        (ProcessType::VariantNorm, "アイウ", "アイウ"),
        // VariantNorm: mixed halfwidth katakana with ASCII/CJK
        (
            ProcessType::VariantNorm,
            "hello カタカナ world",
            "hello カタカナ world",
        ),
        // VariantNorm: fullwidth katakana unchanged
        (ProcessType::VariantNorm, "アイウ", "アイウ"),
        // VariantNorm: Chinese T→S preserved
        (ProcessType::VariantNorm, "國語", "国语"),
        // Romanize: Japanese hiragana
        (ProcessType::Romanize, "あいう", " a i u"),
        // Romanize: Japanese katakana
        (ProcessType::Romanize, "カタカナ", " ka ta ka na"),
        // Romanize: Korean hangul
        (ProcessType::Romanize, "한글", " han geul"),
        // Romanize: Korean Seoul
        (ProcessType::Romanize, "서울", " seo ul"),
        // Romanize: Chinese pinyin preserved
        (ProcessType::Romanize, "中国", " zhong guo"),
        // Romanize: mixed Chinese + Japanese + Korean
        (ProcessType::Romanize, "中あ한", " zhong a han"),
        // RomanizeChar: strips inter-syllable spaces
        (ProcessType::RomanizeChar, "한글", "hangeul"),
    ];

    for &(pt, input, expected) in cases {
        let result = text_process(pt, input);
        assert_eq!(
            result.as_ref(),
            expected,
            "{pt:?} on {input:?}: expected {expected:?}, got {:?}",
            result.as_ref()
        );
    }
}

// ===========================================================================
// CJK script expansion: table-driven matcher integration
// ===========================================================================

#[test]
fn test_matcher_cjk_scripts() {
    let cases: &[(ProcessType, &str, &str)] = &[
        // Korean romanization matching
        (ProcessType::Romanize, "han", "한국"),
        // Japanese kana romanization matching
        (ProcessType::Romanize, "shi", "しんぶん"),
        // Half-width katakana matches full-width pattern
        (ProcessType::VariantNorm, "カタカナ", "カタカナ"),
    ];

    for &(pt, pattern, input) in cases {
        let matcher = SimpleMatcherBuilder::new()
            .add_word(pt, 1, pattern)
            .build()
            .unwrap();
        assert!(
            matcher.is_match(input),
            "{pt:?} pattern={pattern:?} should match input={input:?}"
        );
    }
}

// ===========================================================================
// Mixed CJK scripts through matcher
// ===========================================================================

#[test]
fn test_romanize_mixed_cjk_matcher() {
    // Verify matching a romanized pattern against mixed-CJK text through SimpleMatcher.
    let matcher = SimpleMatcherBuilder::new()
        .add_word(ProcessType::Romanize, 1, "zhong")
        .build()
        .unwrap();

    assert!(
        matcher.is_match("中あ한"),
        "romanized 'zhong' should match '中' in mixed CJK text"
    );
    let results = matcher.process("中あ한");
    assert_eq!(results.len(), 1);
    assert_eq!(results[0].word_id, 1);
}

// ===========================================================================
// EmojiNorm transform tests
// ===========================================================================

#[test]
fn test_emoji_norm_basic() {
    let result = text_process(ProcessType::EmojiNorm, "👍");
    assert_eq!(result, " thumbs_up");
}

#[test]
fn test_emoji_norm_fire() {
    let result = text_process(ProcessType::EmojiNorm, "🔥");
    assert_eq!(result, " fire");
}

#[test]
fn test_emoji_norm_mixed_text() {
    // "Hello " + " fire" + " World" → "Hello  fire World"
    let result = text_process(ProcessType::EmojiNorm, "Hello 🔥 World");
    assert_eq!(result, "Hello  fire World");
}

#[test]
fn test_emoji_norm_skin_tone_stripped() {
    // 👍🏽 = U+1F44D + U+1F3FD (skin tone modifier)
    // Skin tone stripped, base emoji normalized
    let result = text_process(ProcessType::EmojiNorm, "👍🏽");
    assert_eq!(result, " thumbs_up");
}

#[test]
fn test_emoji_norm_zwj_sequence() {
    // ZWJ (U+200D) stripped, each component normalized independently
    let result = text_process(ProcessType::EmojiNorm, "👨\u{200D}👩\u{200D}👧");
    assert_eq!(result, " man woman girl");
}

#[test]
fn test_emoji_norm_vs16_stripped() {
    // VS16 (U+FE0F) should be stripped
    let result = text_process(ProcessType::EmojiNorm, "\u{FE0F}");
    assert_eq!(result, " red_heart");
}

#[test]
fn test_emoji_norm_ascii_passthrough() {
    let result = text_process(ProcessType::EmojiNorm, "hello world");
    assert_eq!(result, "hello world");
}

#[test]
fn test_emoji_norm_multiple_emoji() {
    let result = text_process(ProcessType::EmojiNorm, "🔥❤🎉");
    assert_eq!(result, " fire red_heart party_popper");
}

#[test]
fn test_emoji_norm_matcher_integration() {
    let matcher = SimpleMatcherBuilder::new()
        .add_word(ProcessType::EmojiNorm, 1, "fire")
        .add_word(ProcessType::EmojiNorm, 2, "thumbs_up")
        .build()
        .unwrap();

    assert!(matcher.is_match("🔥"));
    assert!(matcher.is_match("👍🏽"));
    assert!(!matcher.is_match("hello"));

    let results = matcher.process("I love 🔥 and 👍");
    assert_eq!(results.len(), 2);
}

#[test]
fn test_emoji_norm_with_normalize() {
    // EmojiNorm | Normalize: emoji→words + casefold
    let pt = ProcessType::EmojiNorm | ProcessType::Normalize;
    let matcher = SimpleMatcherBuilder::new()
        .add_word(pt, 1, "fire")
        .build()
        .unwrap();

    assert!(matcher.is_match("🔥"));
}

// ===========================================================================
// ProcessType composition edge cases
// ===========================================================================

#[test]
fn test_delete_emoji_norm_composition_gotcha() {
    // Delete strips emoji codepoints BEFORE EmojiNorm can convert them.
    // This is a documented pitfall: Delete | EmojiNorm won't match emoji→word patterns.
    let pt = ProcessType::Delete | ProcessType::EmojiNorm;
    let matcher = SimpleMatcherBuilder::new()
        .add_word(pt, 1, "fire")
        .build()
        .unwrap();

    // "fire" as emoji is deleted before EmojiNorm runs, so no match
    assert!(
        !matcher.is_match("🔥"),
        "Delete|EmojiNorm should NOT match emoji (Delete strips it first)"
    );
    // But literal "fire" in text still matches (Delete doesn't remove letters)
    assert!(matcher.is_match("fire"));
}

#[test]
fn test_none_in_composite_preserves_raw_path() {
    // Including None in a composite type matches against both raw and transformed text.
    let pt = ProcessType::None | ProcessType::Delete;
    let matcher = SimpleMatcherBuilder::new()
        .add_word(pt, 1, "helloworld")
        .build()
        .unwrap();

    // "helloworld" matches raw text
    assert!(matcher.is_match("helloworld"));
    // "hello world" doesn't match raw (space), but Delete strips space → "helloworld"
    assert!(matcher.is_match("hello world"));
    // "hello-world" → Delete strips hyphen → "helloworld"
    assert!(matcher.is_match("hello-world"));
}

#[test]
fn test_romanize_vs_romanize_char() {
    // Romanize adds boundary spaces: 西安 → " xi  an " (two separate syllables)
    // RomanizeChar omits them:        西安 → "xian" (no boundaries)
    let matcher_r = SimpleMatcherBuilder::new()
        .add_word(ProcessType::Romanize, 1, "xian")
        .build()
        .unwrap();

    let matcher_rc = SimpleMatcherBuilder::new()
        .add_word(ProcessType::RomanizeChar, 1, "xian")
        .build()
        .unwrap();

    // 先 → Romanize: " xian " (single syllable, contains "xian")
    // 西安 → Romanize: " xi  an " (two syllables, does NOT contain "xian")
    assert!(
        matcher_r.is_match(""),
        "Romanize: 先 → ' xian ' should contain 'xian'"
    );
    assert!(
        !matcher_r.is_match("西安"),
        "Romanize: 西安 → ' xi  an ' should NOT contain 'xian' (boundary-separated)"
    );

    // RomanizeChar: both 先 and 西安 → "xian" (no spaces)
    assert!(matcher_rc.is_match(""));
    assert!(matcher_rc.is_match("西安"));
}

#[test]
fn test_variant_norm_delete_normalize_composition() {
    // VariantNormDeleteNormalize is the kitchen-sink transform.
    let pt = ProcessType::VariantNormDeleteNormalize;
    let matcher = SimpleMatcherBuilder::new()
        .add_word(pt, 1, "测试")
        .build()
        .unwrap();

    // Traditional + punctuation + width variants all normalize
    assert!(matcher.is_match("測試"));
    assert!(matcher.is_match("測,試"));
    assert!(matcher.is_match("測 試"));
}