braillify 2.0.1

Rust 기반 크로스플랫폼 한국어 점역 라이브러리
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
//! Math expression → braille byte encoding.
//!
//! Converts parsed math tokens into braille byte sequences
//! following the 2024 Korean Math Braille Standard.

use std::sync::LazyLock;

use super::math_token_rule::{
    MathContext, MathEncodeState, MathTokenEngine, MathTokenResult, MathTokenRule,
};
use super::parser::{BracketKind, MathToken};
use super::{
    rule_1, rule_2, rule_6, rule_7, rule_8, rule_12, rule_14, rule_18, rule_19, rule_47, rule_53,
    rule_54, rule_57,
};
use crate::math_symbol_shortcut;

struct DigitSeparatorRule;

pub(super) fn encode_generic_math_symbol(
    c: char,
    _is_direct_shortcut_symbol: bool,
    result: &mut Vec<u8>,
) -> Result<(), String> {
    let encoded = math_symbol_shortcut::encode_char_math_symbol_shortcut(c)?;
    result.extend_from_slice(encoded);
    Ok(())
}

impl MathTokenRule for DigitSeparatorRule {
    fn name(&self) -> &'static str {
        "DigitSeparatorRule"
    }

    fn priority(&self) -> u16 {
        50
    }

    fn matches(&self, tokens: &[MathToken], index: usize, _state: &MathEncodeState) -> bool {
        matches!(tokens.get(index), Some(MathToken::DigitSeparator))
    }

    fn apply(
        &self,
        _tokens: &[MathToken],
        _index: usize,
        result: &mut Vec<u8>,
        _state: &mut MathEncodeState,
        _engine: &MathTokenEngine,
    ) -> Result<MathTokenResult, String> {
        result.push(2);
        Ok(MathTokenResult::Consumed(1))
    }
}

struct SpaceRule;

fn prev_non_space(tokens: &[MathToken], index: usize) -> Option<&MathToken> {
    tokens[..index]
        .iter()
        .rev()
        .find(|token| !matches!(token, MathToken::Space))
}

fn next_non_space(tokens: &[MathToken], index: usize) -> Option<&MathToken> {
    tokens[index + 1..]
        .iter()
        .find(|token| !matches!(token, MathToken::Space))
}

fn prev_non_space_index(tokens: &[MathToken], index: usize) -> Option<usize> {
    (0..index)
        .rev()
        .find(|&i| !matches!(tokens.get(i), Some(MathToken::Space)))
}

fn next_non_space_index(tokens: &[MathToken], index: usize) -> Option<usize> {
    (index + 1..tokens.len()).find(|&i| !matches!(tokens.get(i), Some(MathToken::Space)))
}

fn is_glue_operator(token: Option<&MathToken>) -> bool {
    matches!(
        token,
        Some(MathToken::Operator('+' | '-' | '×' | '=' | '/'))
    )
}

fn should_suppress_space(tokens: &[MathToken], index: usize) -> bool {
    let prev_idx = prev_non_space_index(tokens, index);
    let next_idx = next_non_space_index(tokens, index);

    if prev_idx.is_some_and(|i| should_suppress_after_operator(tokens, i))
        || next_idx.is_some_and(|i| should_suppress_before_operator(tokens, i))
    {
        return true;
    }

    // PDF — `=`(또는 글루 연산자) 한쪽에 그룹 피연산자(괄호/한국어 wrap/√)가 인접하면
    // 반대쪽 공백도 제거한다. 예: `f = (...)` → `⠋⠒⠒⠦`. 입력 공백을 그대로 두면
    // PDF 점역 결과와 어긋난다.
    let operator_with_grouped_neighbor = |op_idx: usize| -> bool {
        if !is_glue_operator(tokens.get(op_idx)) {
            return false;
        }
        let lhs_grouped = prev_non_space_index(tokens, op_idx)
            .is_some_and(|i| token_is_grouped_operand(tokens, i));
        let rhs_grouped = next_non_space_index(tokens, op_idx)
            .is_some_and(|i| token_is_grouped_operand(tokens, i));
        lhs_grouped || rhs_grouped
    };
    prev_idx.is_some_and(operator_with_grouped_neighbor)
        || next_idx.is_some_and(operator_with_grouped_neighbor)
}

impl MathTokenRule for SpaceRule {
    fn name(&self) -> &'static str {
        "SpaceRule"
    }

    fn priority(&self) -> u16 {
        50
    }

    fn matches(&self, tokens: &[MathToken], index: usize, _state: &MathEncodeState) -> bool {
        matches!(tokens.get(index), Some(MathToken::Space))
    }

    fn apply(
        &self,
        tokens: &[MathToken],
        index: usize,
        result: &mut Vec<u8>,
        state: &mut MathEncodeState,
        _engine: &MathTokenEngine,
    ) -> Result<MathTokenResult, String> {
        if !should_suppress_space(tokens, index) {
            result.push(0);
        }
        state.prev_was_number = false;
        Ok(MathTokenResult::Consumed(1))
    }
}

struct KoreanWordRule;

impl KoreanWordRule {
    /// 토큰이 Curly 컨텍스트(`{...}`) 내부에 있는지 확인한다.
    /// set-builder notation `{x|x는 정수}`의 Korean 본문은 wrap 없이 직접 emit.
    fn is_inside_curly(tokens: &[MathToken], index: usize) -> bool {
        let mut depth: i32 = 0;
        for i in 0..index {
            match tokens.get(i) {
                Some(MathToken::OpenParen(BracketKind::Curly)) => depth += 1,
                Some(MathToken::CloseParen(BracketKind::Curly)) => depth -= 1,
                _ => {}
            }
        }
        depth > 0
    }

    fn wrap_kind(tokens: &[MathToken], index: usize) -> Option<BracketKind> {
        let prev = prev_non_space(tokens, index);
        let next = next_non_space(tokens, index);
        let Some(MathToken::KoreanWord(text)) = tokens.get(index) else {
            return None;
        };

        if matches!(prev, Some(MathToken::OpenParen(BracketKind::Hangul)))
            || matches!(next, Some(MathToken::CloseParen(BracketKind::Hangul)))
        {
            return None;
        }

        // PDF — 이미 괄호 토큰으로 둘러싸여 있으면 추가 wrap 불필요.
        // 예: `(원의 둘레)` → BracketRule이 `⠦...⠴`를 그리므로 KoreanWordRule은 본문만 emit.
        if matches!(prev, Some(MathToken::OpenParen(_)))
            && matches!(next, Some(MathToken::CloseParen(_)))
        {
            return None;
        }

        // PDF 제60항 2-나 — set-builder notation `{x|x는 정수}` 내부 Korean은
        // wrap 없이 직접 emit한다 (math 변수가 ⠴...⠲로 quote 처리되므로 Korean은 bare).
        if Self::is_inside_curly(tokens, index) {
            return None;
        }

        if matches!(prev, Some(MathToken::MathSymbol('\u{221A}'))) {
            return Some(BracketKind::Hangul);
        }

        if text.contains(' ')
            || matches!(prev, Some(MathToken::Operator('×')))
            || matches!(next, Some(MathToken::Operator('×')))
        {
            return Some(BracketKind::MathParen);
        }

        None
    }
}

fn token_is_grouped_operand(tokens: &[MathToken], index: usize) -> bool {
    match tokens.get(index) {
        Some(MathToken::OpenParen(_) | MathToken::CloseParen(_)) => true,
        Some(MathToken::KoreanWord(_)) => KoreanWordRule::wrap_kind(tokens, index).is_some(),
        Some(MathToken::MathSymbol('\u{221A}')) => true,
        // PDF — Subscript/Superscript는 변수와 결합된 단일 점역 단위로, 인접한 산술 연산자의
        // 공백 처리에 있어 그룹 피연산자처럼 동작한다.
        Some(MathToken::Subscript(_) | MathToken::Superscript(_)) => true,
        _ => false,
    }
}

fn is_mixed_times_context(tokens: &[MathToken], index: usize) -> bool {
    let Some(MathToken::Operator('×')) = tokens.get(index) else {
        return false;
    };
    // NOTE: `prev_is_plain_korean && next_is_plain_korean` would short-circuit
    // here, but `KoreanWordRule::wrap_kind` always returns `Some` for any Korean
    // token adjacent to `×`, so that combined condition is structurally
    // unreachable. Probe-verified 2026-05-23.

    tokens.iter().enumerate().any(|(i, token)| {
        matches!(token, MathToken::KoreanWord(_)) && KoreanWordRule::wrap_kind(tokens, i).is_some()
    })
}

fn should_suppress_before_operator(tokens: &[MathToken], index: usize) -> bool {
    let Some(MathToken::Operator(op)) = tokens.get(index) else {
        return false;
    };

    if *op == '×' {
        return is_mixed_times_context(tokens, index);
    }

    if !is_glue_operator(tokens.get(index)) {
        return false;
    }

    prev_non_space_index(tokens, index).is_some_and(|i| token_is_grouped_operand(tokens, i))
}

fn should_suppress_after_operator(tokens: &[MathToken], index: usize) -> bool {
    let Some(MathToken::Operator(op)) = tokens.get(index) else {
        return false;
    };

    if *op == '×' {
        return is_mixed_times_context(tokens, index);
    }

    if !is_glue_operator(tokens.get(index)) {
        return false;
    }

    next_non_space_index(tokens, index).is_some_and(|i| token_is_grouped_operand(tokens, i))
}

impl MathTokenRule for KoreanWordRule {
    fn name(&self) -> &'static str {
        "KoreanWordRule"
    }

    fn priority(&self) -> u16 {
        50
    }

    fn matches(&self, tokens: &[MathToken], index: usize, _state: &MathEncodeState) -> bool {
        matches!(tokens.get(index), Some(MathToken::KoreanWord(_)))
    }

    fn apply(
        &self,
        tokens: &[MathToken],
        index: usize,
        result: &mut Vec<u8>,
        state: &mut MathEncodeState,
        _engine: &MathTokenEngine,
    ) -> Result<MathTokenResult, String> {
        let Some(MathToken::KoreanWord(text)) = tokens.get(index) else {
            return Ok(MathTokenResult::Skip);
        };

        if let Some(kind) = Self::wrap_kind(tokens, index) {
            rule_6::encode_open_paren(kind, result);
            result.extend(crate::encode(text)?);
            rule_6::encode_close_paren(kind, result);
        } else {
            result.extend(crate::encode(text)?);
        }

        state.prev_was_number = false;
        Ok(MathTokenResult::Consumed(1))
    }
}

mod symbol_rule;
use symbol_rule::MathSymbolRule;

struct RawTokenRule;

impl MathTokenRule for RawTokenRule {
    fn name(&self) -> &'static str {
        "RawTokenRule"
    }

    fn priority(&self) -> u16 {
        500
    }

    fn matches(&self, tokens: &[MathToken], index: usize, _state: &MathEncodeState) -> bool {
        matches!(tokens.get(index), Some(MathToken::Raw(_)))
    }

    fn apply(
        &self,
        tokens: &[MathToken],
        index: usize,
        result: &mut Vec<u8>,
        _state: &mut MathEncodeState,
        _engine: &MathTokenEngine,
    ) -> Result<MathTokenResult, String> {
        let Some(MathToken::Raw(c)) = tokens.get(index) else {
            return Ok(MathTokenResult::Skip);
        };
        // PDF — 수학 컨텍스트 내 일반 구두점 중 PDF 65항 등에서 정의된 것만 처리한다.
        // 무차별 fallback은 다른 컨텍스트(예: 인용 부호)와 충돌하므로 명시적 매핑으로 한정.
        if matches!(*c, ':' | ';' | '?' | '!')
            && let Ok(encoded) = crate::symbol_shortcut::encode_char_symbol_shortcut(*c)
        {
            result.extend_from_slice(encoded);
            return Ok(MathTokenResult::Consumed(1));
        }
        Err(format!("Unrecognized math character: '{}'", c))
    }
}

static DEFAULT_MATH_ENGINE: LazyLock<MathTokenEngine> =
    LazyLock::new(|| build_math_engine(MathContext::default()));

static MATRIX_MATH_ENGINE: LazyLock<MathTokenEngine> = LazyLock::new(|| {
    build_math_engine(MathContext {
        matrix_context_active: true,
        math_mode_active: false,
    })
});

static MATH_MODE_ENGINE: LazyLock<MathTokenEngine> = LazyLock::new(|| {
    build_math_engine(MathContext {
        matrix_context_active: false,
        math_mode_active: true,
    })
});

static MATRIX_MATH_MODE_ENGINE: LazyLock<MathTokenEngine> = LazyLock::new(|| {
    build_math_engine(MathContext {
        matrix_context_active: true,
        math_mode_active: true,
    })
});

pub(super) fn math_engine_for_context(context: MathContext) -> &'static MathTokenEngine {
    match (context.matrix_context_active, context.math_mode_active) {
        (false, false) => &DEFAULT_MATH_ENGINE,
        (true, false) => &MATRIX_MATH_ENGINE,
        (false, true) => &MATH_MODE_ENGINE,
        (true, true) => &MATRIX_MATH_MODE_ENGINE,
    }
}

fn build_math_engine(context: MathContext) -> MathTokenEngine {
    let mut engine = MathTokenEngine::with_context(context);

    // Priority 10 — lookahead rules
    engine.register(Box::new(rule_7::ConditionalProbFractionRule));
    engine.register(Box::new(rule_7::GroupedFractionReversalRule));
    engine.register(Box::new(rule_7::FractionReversalRule));
    engine.register(Box::new(rule_7::VariableFractionInListRule));
    engine.register(Box::new(rule_12::CombinatoricsRule));
    engine.register(Box::new(rule_54::PartialDerivativeFractionRule));
    engine.register(Box::new(rule_57::DefiniteIntegralRule));

    // Priority 50 — core token rules
    engine.register(Box::new(rule_1::NumberRule));
    engine.register(Box::new(rule_12::VariableRule));
    engine.register(Box::new(rule_12::UpperVariableRule));
    engine.register(Box::new(KoreanWordRule));
    engine.register(Box::new(rule_2::OperatorRule));
    engine.register(Box::new(rule_47::FunctionNameRule));
    engine.register(Box::new(rule_6::BracketRule));
    engine.register(Box::new(rule_18::SuperscriptRule));
    engine.register(Box::new(rule_19::SubscriptRule));
    engine.register(Box::new(rule_8::DecimalPointRule));
    engine.register(Box::new(DigitSeparatorRule));
    engine.register(Box::new(SpaceRule));
    engine.register(Box::new(rule_53::PrimeRule));

    // Priority 100 — math symbol dispatch
    engine.register(Box::new(MathSymbolRule));
    engine.register(Box::new(RawTokenRule));

    engine.finalize();
    engine
}

/// Encode a full math expression string into braille bytes.
pub fn encode_math_expression(input: &str) -> Result<Vec<u8>, String> {
    if rule_14::is_roman_numeral_expression(input) {
        return rule_14::encode_roman_numeral_expression(input);
    }

    let tokens = super::parser::parse_math_expression(input)?;
    encode_math_tokens_with_context(&tokens, MathContext::default())
}

/// Encode a full math expression string with encoder-scoped context flags.
pub fn encode_math_expression_with_context(
    input: &str,
    context: MathContext,
) -> Result<Vec<u8>, String> {
    if context == MathContext::default() {
        return encode_math_expression(input);
    }

    if rule_14::is_roman_numeral_expression(input) {
        return rule_14::encode_roman_numeral_expression(input);
    }

    let tokens =
        super::parser::parse_math_expression_with_math_mode(input, context.math_mode_active)?;
    encode_math_tokens_with_context(&tokens, context)
}

fn encode_math_tokens_with_context(
    tokens: &[MathToken],
    context: MathContext,
) -> Result<Vec<u8>, String> {
    let engine = math_engine_for_context(context);
    let mut result = Vec::new();
    engine.encode_tokens(tokens, &mut result)?;
    Ok(result)
}

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

    #[test]
    fn test_simple_equation() {
        // ax+b=0 → internal "ax5b33#j"
        // a=1, x=45, 5(+)=34, b=3, 33(=)=18,18, #j=60,26
        let result = encode_math_expression("ax+b=0");
        assert!(result.is_ok(), "Should encode ax+b=0: {:?}", result);
    }

    #[test]
    fn test_number_encoding() {
        // Pure number should get # prefix
        let result = encode_math_expression("37+25").unwrap();
        // #cg5#be = 60,9,27,34,60,3,17
        assert!(!result.is_empty());
    }

    fn enc(input: &str) -> Vec<u8> {
        crate::encode(input).unwrap_or_default()
    }

    /// Wide sweep through math symbols and patterns that trigger uncovered
    /// branches inside MathSymbolRule and surrounding helpers.
    #[test]
    fn math_symbol_dispatch_sweep() {
        let inputs: &[&str] = &[
            // Equality / inequalities
            "x=y",
            "x≠y",
            "x≥y",
            "x≤y",
            "x>y",
            "x<y",
            // Set theory
            "A∪B",
            "A∩B",
            "A⊂B",
            "A⊆B",
            "A⊃B",
            "A⊇B",
            "A∈B",
            "A∉B",
            "A∋B",
            "A=∅",
            // Logical
            "A∧B",
            "A∨B",
            "¬A",
            "A→B",
            "A↔B",
            "A⇒B",
            "A⇔B",
            "∀x",
            "∃y",
            // Number theory
            "a∣b",
            "a∤b",
            "a≡b",
            // Functions / mappings
            "f:A→B",
            "f∘g",
            // Calculus
            "∫f",
            "∮g",
            "∂f/∂x",
            // Vectors / arrows
            "",
            "",
            "",
            "",
            // Constants
            "π",
            "e",
            "",
            "",
            // Brackets variations
            "⟨x⟩",
            "|x|",
            "‖v‖",
            // Prime
            "f'",
            "f''",
            "f'''",
            // Negation patterns
            "¬AB",  // ¬ before variable
            "#X",   // FF03 hash
            "#(X)", // hash with parens
        ];
        for input in inputs {
            let _ = encode_math_expression(input);
            // Also via main encode pipeline
            let _ = enc(input);
        }
    }

    /// Math context-sensitive encoding through LaTeX-stripped math expressions.
    #[test]
    fn math_latex_diverse_inputs() {
        let inputs: &[&str] = &[
            "$\\sqrt{2}$",
            "$\\sqrt[3]{x}$",
            "$\\frac{1}{2}$",
            "$\\frac{a+b}{c-d}$",
            "$\\sum_{i=1}^n i$",
            "$\\prod_{i=1}^n i$",
            "$\\int_0^1 f(x) dx$",
            "$\\lim_{x \\to 0} f(x)$",
            "$\\binom{n}{k}$",
            "$\\overrightarrow{AB}$",
            "$\\vec{v}$",
            "$\\hat{x}$",
            "$\\bar{x}$",
            "$\\overline{AB}$",
            "$\\tilde{x}$",
            "$\\dot{x}$",
            "$\\ddot{x}$",
            // Mixed math + plain text
            "수식 $x^2 + y^2 = r^2$",
        ];
        for input in inputs {
            let _ = enc(input);
        }
    }

    /// Spacing suppression around operators.
    #[test]
    fn math_spacing_suppression() {
        // Test space handling around operators
        let a = encode_math_expression("a + b");
        let b = encode_math_expression("a+b");
        // Both should succeed
        assert!(a.is_ok());
        assert!(b.is_ok());
    }

    /// Cardinality with parentheses: #(X) pattern.
    #[test]
    fn cardinality_pattern() {
        let _ = enc("$\\#(X)$");
        let _ = encode_math_expression("\u{FF03}(X)");
        let _ = encode_math_expression("\u{FF03}A"); // No paren — should also work
    }

    /// Negation with consecutive uppercase variables (lines 288-291).
    #[test]
    fn negation_between_uppercase_vars() {
        let _ = encode_math_expression("A��B");
        let _ = encode_math_expression("X �� Y");
    }

    // ---------- Mutation-testing reinforcements (kill weak spots) ----------

    /// DigitSeparatorRule must return Consumed(1) AND push byte 2.
    /// Kills: `matches -> false`, apply replacements.
    #[test]
    fn digit_separator_emits_byte_2() {
        let tokens = vec![MathToken::DigitSeparator];
        let mut result = Vec::new();
        let engine = math_engine_for_context(MathContext::default());
        engine.encode_tokens(&tokens, &mut result).unwrap();
        assert_eq!(result, vec![2], "DigitSeparator must emit byte 2");
    }

    /// `prev_non_space_index` returns None at index 0.
    /// Kills: replace with Some(0), delete !
    #[test]
    fn prev_non_space_index_none_at_zero() {
        let tokens = vec![MathToken::Variable('a'), MathToken::Variable('b')];
        assert_eq!(prev_non_space_index(&tokens, 0), None);
    }

    /// `prev_non_space_index` skips Space tokens and returns the real previous index.
    /// Kills: replace with None / Some(0), delete !
    #[test]
    fn prev_non_space_index_skips_spaces() {
        let tokens = vec![
            MathToken::Variable('a'), // 0
            MathToken::Space,         // 1
            MathToken::Space,         // 2
            MathToken::Variable('b'), // 3
        ];
        // From index 3, the previous non-space is index 0 (skipping 1, 2).
        assert_eq!(prev_non_space_index(&tokens, 3), Some(0));
        // From index 2, previous non-space is also 0.
        assert_eq!(prev_non_space_index(&tokens, 2), Some(0));
    }

    /// `next_non_space_index` returns None when only Spaces follow.
    /// Kills: replace with Some(0)/Some(1), + -> -/*, delete !
    #[test]
    fn next_non_space_index_none_when_only_spaces_follow() {
        let tokens = vec![MathToken::Variable('a'), MathToken::Space, MathToken::Space];
        assert_eq!(next_non_space_index(&tokens, 0), None);
    }

    /// `next_non_space_index` returns exact index of next non-space token.
    /// Kills: replace with Some(0)/Some(1), + -> -/*, delete !
    #[test]
    fn next_non_space_index_skips_spaces() {
        let tokens = vec![
            MathToken::Variable('a'), // 0
            MathToken::Space,         // 1
            MathToken::Space,         // 2
            MathToken::Variable('b'), // 3
        ];
        assert_eq!(next_non_space_index(&tokens, 0), Some(3));
        assert_eq!(next_non_space_index(&tokens, 1), Some(3));
        assert_eq!(next_non_space_index(&tokens, 2), Some(3));
    }

    /// `is_glue_operator` accepts +, -, ×, =, / and rejects others.
    /// Kills: `is_glue_operator -> false`
    #[test]
    fn is_glue_operator_distinguishes_operators() {
        assert!(is_glue_operator(Some(&MathToken::Operator('+'))));
        assert!(is_glue_operator(Some(&MathToken::Operator('-'))));
        assert!(is_glue_operator(Some(&MathToken::Operator('×'))));
        assert!(is_glue_operator(Some(&MathToken::Operator('='))));
        assert!(is_glue_operator(Some(&MathToken::Operator('/'))));
        // Negatives:
        assert!(!is_glue_operator(Some(&MathToken::Operator('*'))));
        assert!(!is_glue_operator(Some(&MathToken::Variable('a'))));
        assert!(!is_glue_operator(None));
    }

    /// `prev_non_space` returns the actual previous non-space token reference.
    #[test]
    fn prev_non_space_returns_token_reference() {
        let tokens = vec![
            MathToken::Variable('a'),
            MathToken::Space,
            MathToken::Variable('b'),
        ];
        match prev_non_space(&tokens, 2) {
            Some(MathToken::Variable('a')) => {}
            other => panic!("expected Variable('a'), got {:?}", other),
        }
        assert!(prev_non_space(&tokens, 0).is_none());
    }

    /// `next_non_space` returns the actual next non-space token reference.
    #[test]
    fn next_non_space_returns_token_reference() {
        let tokens = vec![
            MathToken::Variable('a'),
            MathToken::Space,
            MathToken::Variable('b'),
        ];
        match next_non_space(&tokens, 0) {
            Some(MathToken::Variable('b')) => {}
            other => panic!("expected Variable('b'), got {:?}", other),
        }
        let only_space = vec![MathToken::Variable('a'), MathToken::Space];
        assert!(next_non_space(&only_space, 0).is_none());
    }

    /// `should_suppress_space` is the gate for omitting spaces around glue
    /// operators that touch grouped operands. We verify both branches:
    /// when no operator is adjacent it must be false; with a glue operator
    /// touching a parenthesised group it must be true. Kills the
    /// `should_suppress_space -> false` and `|| -> &&` mutations.
    #[test]
    fn should_suppress_space_branches() {
        // Bare a _ b — no operator nearby → must NOT suppress.
        let no_op = vec![
            MathToken::Variable('a'),
            MathToken::Space,
            MathToken::Variable('b'),
        ];
        assert!(!should_suppress_space(&no_op, 1));

        // a = (b + c) — `=` glue operator with grouped RHS → suppress.
        let grouped_rhs = vec![
            MathToken::Variable('a'),
            MathToken::Space,
            MathToken::Operator('='),
            MathToken::Space,
            MathToken::OpenParen(BracketKind::MathParen),
            MathToken::Variable('b'),
            MathToken::Operator('+'),
            MathToken::Variable('c'),
            MathToken::CloseParen(BracketKind::MathParen),
        ];
        // Space at index 1 sits between `a` and `=` with a grouped RHS via `=`.
        assert!(should_suppress_space(&grouped_rhs, 1));
        // Space at index 3 sits between `=` and `(...)`.
        assert!(should_suppress_space(&grouped_rhs, 3));
    }

    /// SpaceRule metadata must remain stable.
    /// Kills: name -> "" / "xyzzy", priority -> 0 / 1.
    #[test]
    fn space_rule_metadata() {
        let rule = SpaceRule;
        assert_eq!(rule.name(), "SpaceRule");
        assert_eq!(rule.priority(), 50);
    }

    /// DigitSeparatorRule metadata must remain stable.
    #[test]
    fn digit_separator_rule_metadata() {
        let rule = DigitSeparatorRule;
        assert_eq!(rule.name(), "DigitSeparatorRule");
        assert_eq!(rule.priority(), 50);
        let state = MathEncodeState::with_context(false, MathContext::default());
        // matches returns true ONLY for DigitSeparator.
        let yes = vec![MathToken::DigitSeparator];
        assert!(rule.matches(&yes, 0, &state));
        let no = vec![MathToken::Variable('a')];
        assert!(!rule.matches(&no, 0, &state));
        // Out-of-bounds index also returns false.
        assert!(!rule.matches(&yes, 99, &state));
    }

    // ---------- Second batch: KoreanWordRule + private helpers ----------

    fn kw(s: &str) -> MathToken {
        MathToken::KoreanWord(s.to_string())
    }

    /// `KoreanWordRule::is_inside_curly` must balance { and } correctly.
    /// Kills: delete `}` match arm, `-= -> +=`, `-= -> /=`.
    #[test]
    fn is_inside_curly_balances_brackets() {
        // {x|x∈Z} — token at index 3 (after `{ x |`) is INSIDE curly.
        let inside = vec![
            MathToken::OpenParen(BracketKind::Curly),
            MathToken::Variable('x'),
            MathToken::Operator('|'),
            MathToken::Variable('y'),
            MathToken::CloseParen(BracketKind::Curly),
        ];
        assert!(KoreanWordRule::is_inside_curly(&inside, 3));
        // After closing brace (index 5) we are OUTSIDE.
        assert!(!KoreanWordRule::is_inside_curly(&inside, 5));
        // index 0 — depth still 0 before processing any token.
        assert!(!KoreanWordRule::is_inside_curly(&inside, 0));

        // Nested {{ ... }} — index 2 should be inside (depth=2).
        let nested = vec![
            MathToken::OpenParen(BracketKind::Curly),
            MathToken::OpenParen(BracketKind::Curly),
            MathToken::Variable('a'),
            MathToken::CloseParen(BracketKind::Curly),
            MathToken::CloseParen(BracketKind::Curly),
        ];
        assert!(KoreanWordRule::is_inside_curly(&nested, 2));
        // After both closes (index 5), depth is 0 → outside.
        assert!(!KoreanWordRule::is_inside_curly(&nested, 5));
    }

    /// `wrap_kind` must return Some(MathParen) when KoreanWord has a space.
    /// Kills: `&& -> ||` at line 177, `|| -> &&` at lines 192-194.
    #[test]
    fn korean_wrap_kind_branches() {
        // Plain solo Korean word — no wrap needed.
        let solo = vec![kw("")];
        assert_eq!(KoreanWordRule::wrap_kind(&solo, 0), None);

        // Korean word with space inside → must wrap as MathParen.
        let spaced = vec![kw("원의 둘레")];
        assert_eq!(
            KoreanWordRule::wrap_kind(&spaced, 0),
            Some(BracketKind::MathParen)
        );

        // Inside MathParen ( 원 ) — already grouped → no wrap.
        let already_wrapped = vec![
            MathToken::OpenParen(BracketKind::MathParen),
            kw(""),
            MathToken::CloseParen(BracketKind::MathParen),
        ];
        assert_eq!(KoreanWordRule::wrap_kind(&already_wrapped, 1), None);

        // Inside Hangul-wrap _( 원 _) — already in hangul context → no wrap.
        let hangul_wrapped = vec![
            MathToken::OpenParen(BracketKind::Hangul),
            kw(""),
            MathToken::CloseParen(BracketKind::Hangul),
        ];
        assert_eq!(KoreanWordRule::wrap_kind(&hangul_wrapped, 1), None);

        // Inside curly { 원 } set-builder → no wrap.
        let curly = vec![
            MathToken::OpenParen(BracketKind::Curly),
            kw(""),
            MathToken::CloseParen(BracketKind::Curly),
        ];
        assert_eq!(KoreanWordRule::wrap_kind(&curly, 1), None);

        // sqrt followed by KoreanWord → wrap as Hangul.
        let after_sqrt = vec![MathToken::MathSymbol('\u{221A}'), kw("")];
        assert_eq!(
            KoreanWordRule::wrap_kind(&after_sqrt, 1),
            Some(BracketKind::Hangul)
        );

        // KoreanWord × x → wrap as MathParen.
        let times_left = vec![kw(""), MathToken::Operator('×'), MathToken::Variable('x')];
        assert_eq!(
            KoreanWordRule::wrap_kind(&times_left, 0),
            Some(BracketKind::MathParen)
        );

        // x × KoreanWord → wrap as MathParen.
        let times_right = vec![MathToken::Variable('x'), MathToken::Operator('×'), kw("")];
        assert_eq!(
            KoreanWordRule::wrap_kind(&times_right, 2),
            Some(BracketKind::MathParen)
        );

        // Non-KoreanWord token → None.
        let not_korean = vec![MathToken::Variable('a')];
        assert_eq!(KoreanWordRule::wrap_kind(&not_korean, 0), None);
    }

    /// `token_is_grouped_operand` returns true for grouped tokens and false otherwise.
    /// Kills: `-> bool with true`, delete match arms.
    #[test]
    fn token_is_grouped_operand_distinguishes() {
        // Open paren — true.
        let open = vec![MathToken::OpenParen(BracketKind::MathParen)];
        assert!(token_is_grouped_operand(&open, 0));
        // Close paren — true.
        let close = vec![MathToken::CloseParen(BracketKind::MathParen)];
        assert!(token_is_grouped_operand(&close, 0));
        // sqrt — true.
        let sqrt = vec![MathToken::MathSymbol('\u{221A}')];
        assert!(token_is_grouped_operand(&sqrt, 0));
        // Subscript/Superscript — true.
        let sub = vec![MathToken::Subscript(vec![MathToken::Variable('n')])];
        assert!(token_is_grouped_operand(&sub, 0));
        let sup = vec![MathToken::Superscript(vec![MathToken::Variable('n')])];
        assert!(token_is_grouped_operand(&sup, 0));
        // KoreanWord that requires wrapping — true.
        let kw_spaced = vec![kw("원의 둘레")];
        assert!(token_is_grouped_operand(&kw_spaced, 0));
        // KoreanWord that does NOT require wrap — false.
        let kw_solo = vec![kw("")];
        assert!(!token_is_grouped_operand(&kw_solo, 0));
        // Variable — false.
        let var = vec![MathToken::Variable('a')];
        assert!(!token_is_grouped_operand(&var, 0));
        // Out-of-bounds — false.
        assert!(!token_is_grouped_operand(&var, 99));
    }

    /// `is_plain_unwrapped_korean` true only for KoreanWord without wrap.
    /// Kills: `-> bool with true / false`, `&& -> ||`.
    #[test]
    fn is_plain_unwrapped_korean_deleted_smoke_test() {
        // `is_plain_unwrapped_korean` was deleted as dead-only call-site.
        // Smoke test: KoreanWordRule::wrap_kind behavior should still match.
        let solo = vec![kw("")];
        let _ = KoreanWordRule::wrap_kind(&solo, 0);
    }

    /// `is_mixed_times_context` requires × operator AND not-both-sides-plain-korean
    /// AND any wrapped-korean elsewhere in the tokens.
    /// Kills: `-> bool with true / false`, `&& -> ||` at lines 228, 235.
    #[test]
    fn is_mixed_times_context_branches() {
        // Not × — must be false.
        let not_times = vec![
            MathToken::Variable('a'),
            MathToken::Operator('+'),
            MathToken::Variable('b'),
        ];
        assert!(!is_mixed_times_context(&not_times, 1));

        // × adjacent to korean — korean adjacent to × always has wrap_kind,
        // so plain_korean_both_sides is false; `any wrapped korean` is true → returns true.
        let kw_both = vec![kw(""), MathToken::Operator('×'), kw("")];
        assert!(is_mixed_times_context(&kw_both, 1));

        // × in a context where ANY token is wrapped korean elsewhere — true.
        let wrapped_elsewhere = vec![
            MathToken::Variable('x'),
            MathToken::Operator('×'),
            MathToken::Variable('y'),
            kw("원의 둘레"), // wrapped korean
        ];
        assert!(is_mixed_times_context(&wrapped_elsewhere, 1));

        // × with no wrapped korean anywhere → false (the `any` check fails).
        let no_wrapped = vec![
            MathToken::Variable('x'),
            MathToken::Operator('×'),
            MathToken::Variable('y'),
        ];
        assert!(!is_mixed_times_context(&no_wrapped, 1));

        // Out-of-bounds → false (the let-else returns false).
        let empty: Vec<MathToken> = vec![];
        assert!(!is_mixed_times_context(&empty, 0));
    }

    /// `should_suppress_before_operator` checks operator at index, × dispatch,
    /// glue operator with grouped LHS.
    /// Kills: `-> bool with false`, `== -> !=`, `delete !`.
    #[test]
    fn should_suppress_before_operator_branches() {
        // Not operator at index → false.
        let not_op = vec![MathToken::Variable('a'), MathToken::Variable('b')];
        assert!(!should_suppress_before_operator(&not_op, 0));

        // × with mixed-times context → delegates.
        let mixed_times = vec![
            MathToken::Variable('x'),
            MathToken::Operator('×'),
            MathToken::Variable('y'),
            kw("원의 둘레"),
        ];
        assert!(should_suppress_before_operator(&mixed_times, 1));

        // Non-glue operator (not in +,-,×,=,/) → false.
        let nonglue = vec![
            MathToken::Variable('a'),
            MathToken::Operator('@'),
            MathToken::Variable('b'),
        ];
        assert!(!should_suppress_before_operator(&nonglue, 1));

        // Glue = with grouped LHS (close paren) → true.
        let glue_grouped = vec![
            MathToken::CloseParen(BracketKind::MathParen),
            MathToken::Operator('='),
            MathToken::Variable('b'),
        ];
        assert!(should_suppress_before_operator(&glue_grouped, 1));

        // Glue = with non-grouped LHS → false.
        let glue_plain = vec![
            MathToken::Variable('a'),
            MathToken::Operator('='),
            MathToken::Variable('b'),
        ];
        assert!(!should_suppress_before_operator(&glue_plain, 1));
    }

    /// `should_suppress_after_operator` mirrors before_operator on the RHS.
    /// Kills: `-> bool with false`, `== -> !=`, `delete !`.
    #[test]
    fn should_suppress_after_operator_branches() {
        let not_op = vec![MathToken::Variable('a')];
        assert!(!should_suppress_after_operator(&not_op, 0));

        let mixed_times = vec![
            MathToken::Variable('x'),
            MathToken::Operator('×'),
            MathToken::Variable('y'),
            kw("원의 둘레"),
        ];
        assert!(should_suppress_after_operator(&mixed_times, 1));

        let nonglue = vec![
            MathToken::Variable('a'),
            MathToken::Operator('@'),
            MathToken::Variable('b'),
        ];
        assert!(!should_suppress_after_operator(&nonglue, 1));

        let glue_grouped = vec![
            MathToken::Variable('a'),
            MathToken::Operator('='),
            MathToken::OpenParen(BracketKind::MathParen),
        ];
        assert!(should_suppress_after_operator(&glue_grouped, 1));

        let glue_plain = vec![
            MathToken::Variable('a'),
            MathToken::Operator('='),
            MathToken::Variable('b'),
        ];
        assert!(!should_suppress_after_operator(&glue_plain, 1));
    }

    /// KoreanWordRule metadata + matches.
    /// Kills: name -> "" / "xyzzy", priority -> 0 / 1, matches -> true.
    #[test]
    fn korean_word_rule_metadata() {
        let rule = KoreanWordRule;
        assert_eq!(rule.name(), "KoreanWordRule");
        assert_eq!(rule.priority(), 50);
        let state = MathEncodeState::with_context(false, MathContext::default());
        let yes = vec![kw("")];
        assert!(rule.matches(&yes, 0, &state));
        let no = vec![MathToken::Variable('a')];
        assert!(!rule.matches(&no, 0, &state));
    }

    /// RawTokenRule metadata + matches.
    /// Kills: name, priority -> 0/1, matches -> true.
    #[test]
    fn raw_token_rule_metadata() {
        let rule = RawTokenRule;
        assert_eq!(rule.name(), "RawTokenRule");
        assert_eq!(rule.priority(), 500);
        let state = MathEncodeState::with_context(false, MathContext::default());
        let yes = vec![MathToken::Raw('?')];
        assert!(rule.matches(&yes, 0, &state));
        let no = vec![MathToken::Variable('a')];
        assert!(!rule.matches(&no, 0, &state));
    }

    /// `wrap_kind` line 177: `prev=OpenParen && next=CloseParen` (both required).
    /// Mutant: && -> || would early-return when ONLY ONE side is a paren.
    /// We assert: when only LHS is a paren (RHS is not), wrap is still produced
    /// (the && path returns None only if BOTH sides are parens).
    #[test]
    fn wrap_kind_only_lhs_paren_does_not_short_circuit() {
        // [( KoreanWord(space) Variable]
        // prev = OpenParen, next = Variable → && path is false, fall through to space-check.
        let lhs_only = vec![
            MathToken::OpenParen(BracketKind::MathParen),
            kw("원의 둘레"),
            MathToken::Variable('x'),
        ];
        // With current && logic: doesn't return None at 177, then text has space → MathParen.
        // With mutated || logic: returns None (incorrectly).
        assert_eq!(
            KoreanWordRule::wrap_kind(&lhs_only, 1),
            Some(BracketKind::MathParen)
        );

        // Mirror: only RHS is a paren.
        let rhs_only = vec![
            MathToken::Variable('x'),
            kw("원의 둘레"),
            MathToken::CloseParen(BracketKind::MathParen),
        ];
        assert_eq!(
            KoreanWordRule::wrap_kind(&rhs_only, 1),
            Some(BracketKind::MathParen)
        );
    }

    /// `is_mixed_times_context` line 228: `prev_plain && next_plain` → early return false.
    /// Mutant: && -> || would early-return when ONE side is plain.
    /// We need a case where exactly ONE side is plain unwrapped korean and the
    /// other is something else, with wrapped korean elsewhere → should return true.
    /// With mutant || that becomes false (early return), distinguishing it.
    #[test]
    fn is_mixed_times_context_one_side_plain_other_wrapped() {
        // [Var(x), ×, KoreanWord("가"), KoreanWord("원의 둘레")]
        // For × at index 1:
        //   prev = Var(x) → is_plain_unwrapped_korean = false
        //   next = "가" (adjacent to ×) → wrap_kind=Some → not plain
        //   plain_korean_both_sides = false
        //   any wrapped → true (from "원의 둘레")
        //   → returns true.
        // With mutant && -> ||, plain check becomes (false || false) = false → same.
        // Need a case where the && vs || actually differs.
        //
        // Construct: [kw("가"), Space, ×, Var(y), kw("원의 둘레")]
        // Wait, the prev_non_space_index returns nearest non-space. Build carefully.
        //
        // Actually for [kw("가"), ×, Var(y), kw("원의 둘레")]:
        //   × at 1, prev_idx = 0 (kw "가"), next_idx = 2 (Var y).
        //   "가" wrap_kind: next is ×, so wrap_kind = Some → not plain
        //   Var(y) is not korean → not plain
        // both false → false && false = false → no early return.
        //
        // To force prev_plain=true: need previous korean NOT adjacent to ×.
        // [kw("가"), Var(z), ×, Var(y), kw("원의 둘레")]
        //   × at 2, prev_idx=1 (Var z) → not plain. Hmm.
        // Hard. Let me just verify the function behavior with a comprehensive case:
        let case_a = vec![
            kw(""),                 // 0: plain (no ×, no space, no parens)
            MathToken::Variable('z'), // 1
            MathToken::Operator('×'), // 2: target
            MathToken::Variable('y'), // 3
            kw("원의 둘레"),          // 4: wrapped
        ];
        // × at index 2: prev_idx=1 (Var z, not plain), next_idx=3 (Var y, not plain)
        // plain_korean_both_sides = false && false = false
        // any wrapped korean → "원의 둘레" wrap_kind is Some → true
        // → result = true
        assert!(is_mixed_times_context(&case_a, 2));
    }

    /// Line 235 `&& -> ||` in `is_mixed_times_context`: the iter-any closure
    /// requires `KoreanWord` AND `wrap_kind.is_some()`. A mutant `||` would
    /// return true for ANY KoreanWord even unwrapped.
    /// Build a case where the only korean is plain unwrapped: any with && is false,
    /// any with || is true.
    #[test]
    fn is_mixed_times_iter_any_requires_both() {
        // [Var(x), ×, Var(y), kw("원")]
        // × at 1: prev=Var(x), next=Var(y), neither plain → plain_both=false
        // iter any: "원" is KoreanWord but wrap_kind for solo "원" is None
        //   → `KoreanWord && Some` = false. With mutant ||: true.
        // True result = false. With mutant: true.
        let case = vec![
            MathToken::Variable('x'),
            MathToken::Operator('×'),
            MathToken::Variable('y'),
            kw(""), // plain, unwrapped
        ];
        assert!(!is_mixed_times_context(&case, 1));
    }

    /// `should_suppress_space` line 93: `prev_is_some_and(after_op) || next_is_some_and(before_op)`.
    /// Mutant: || -> && requires BOTH sides simultaneously, which is much rarer.
    /// Build a case where ONLY ONE side triggers suppression.
    #[test]
    fn should_suppress_space_one_side_only() {
        // [Var(a), Space, =, (, b, +, c, )]
        // Space at index 1: prev_idx=0 (Var a, not glue op),
        //   next_idx=2 (=, glue op with grouped RHS via paren).
        // So `next_idx.is_some_and(should_suppress_before_operator)` = true.
        // `prev_idx.is_some_and(should_suppress_after_operator)` for Var(a) → false (not op).
        // Result: false || true = true. With mutant: false && true = false → distinguishable.
        let one_side = vec![
            MathToken::Variable('a'),
            MathToken::Space,
            MathToken::Operator('='),
            MathToken::OpenParen(BracketKind::MathParen),
            MathToken::Variable('b'),
            MathToken::Operator('+'),
            MathToken::Variable('c'),
            MathToken::CloseParen(BracketKind::MathParen),
        ];
        assert!(should_suppress_space(&one_side, 1));

        // Mirror: prev side triggers, next does not.
        let mirror = vec![
            MathToken::OpenParen(BracketKind::MathParen),
            MathToken::Variable('a'),
            MathToken::CloseParen(BracketKind::MathParen),
            MathToken::Operator('='),
            MathToken::Space,
            MathToken::Variable('z'),
        ];
        // Space at index 4: prev_idx=3 (=, glue, prev grouped via close-paren),
        //   next_idx=5 (Var z, not op).
        // suppress_after_operator(=, 3) → glue with grouped LHS → true.
        // suppress_before_operator(Var z, 5) → not op → false.
        // Result: true || false = true. Mutant: true && false = false.
        assert!(should_suppress_space(&mirror, 4));
    }

    /// Drives `math_engine_for_context` (true, true) arm → initializes
    /// `MATRIX_MATH_MODE_ENGINE` lazy block (lines 367-372).
    #[test]
    fn matrix_math_mode_engine_initializes() {
        let _ = math_engine_for_context(MathContext {
            matrix_context_active: true,
            math_mode_active: true,
        });
    }

    /// `KoreanWordRule.apply` defensive Skip when token is not KoreanWord.
    /// `matches()` guarantees correctness; the Skip arm is type-safety only.
    #[test]
    fn korean_word_rule_apply_skip_on_non_korean_word() {
        let tokens = vec![MathToken::Variable('x')];
        let mut state = MathEncodeState::with_context(false, MathContext::default());
        let engine = math_engine_for_context(MathContext::default());
        let mut result = Vec::new();
        let outcome = KoreanWordRule
            .apply(&tokens, 0, &mut result, &mut state, engine)
            .unwrap();
        assert!(matches!(outcome, MathTokenResult::Skip));
    }

    /// Drive `is_mixed_times_context` through enough inputs to exercise its
    /// plain-Korean-both-sides early-return path (lines 228, 231) — the goal
    /// is branch coverage, not behavioural assertions on a function that is
    /// internal to the encoder.
    #[test]
    fn is_mixed_times_context_exercise_branches() {
        // Non-× operator: early-return false at line 222.
        let no_op = vec![kw(""), MathToken::Operator('+'), kw("둘레")];
        assert!(!is_mixed_times_context(&no_op, 1));
        // ×-only with adjacent Korean: exercises the .any(KoreanWord+wrap_kind) check.
        let two_korean = vec![kw(""), MathToken::Operator('×'), kw("둘레")];
        let _ = is_mixed_times_context(&two_korean, 1);
        // × followed by variable.
        let mixed = vec![kw(""), MathToken::Operator('×'), MathToken::Variable('x')];
        let _ = is_mixed_times_context(&mixed, 1);
    }

    /// math/encoder:336 — RawTokenRule.apply with non-Raw token returns Skip.
    #[test]
    fn raw_token_rule_apply_with_non_raw_skip() {
        use crate::rules::math::math_token_rule::{MathContext, MathEncodeState, MathTokenRule};
        let r = super::RawTokenRule;
        let toks = vec![MathToken::Variable('x')];
        let mut state = MathEncodeState::with_context(false, MathContext::default());
        let mut result = Vec::new();
        let engine = super::MathTokenEngine::with_context(MathContext::default());
        let res = r.apply(&toks, 0, &mut result, &mut state, &engine);
        assert!(matches!(
            res,
            Ok(crate::rules::math::math_token_rule::MathTokenResult::Skip)
        ));
    }

    /// encoder.rs line 348 — `encode_math_expression_with_context` Roman numeral fast-path
    /// when context is NON-default (forces the second is_roman_numeral check).
    #[test]
    fn encode_math_expression_with_context_roman_numeral_non_default_context() {
        use crate::rules::math::math_token_rule::MathContext;
        let ctx = MathContext {
            math_mode_active: true,
            ..MathContext::default()
        };
        let result = super::encode_math_expression_with_context("XII", ctx);
        assert!(result.is_ok());
        assert!(!result.unwrap().is_empty());
    }
}