calnexus 0.1.4

A command-line math expression evaluator with 11 core and 4 optional (numerical/time/unit/fx) computation domains, symbolic calculus, REPL, and batch processing
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
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
// Copyright (c) 2026 Kirky.X. Licensed under the MIT License.

#![allow(clippy::approx_constant, non_snake_case)]

//! CLI 集成测试:使用 assert_cmd 执行真实二进制,验证 cli-interface spec。
//!
//! 覆盖 12 个 requirements / 23 个 scenarios。

use assert_cmd::Command;

// ===== Requirement 1: Single Expression Evaluation =====

#[test]
fn test_basic_addition() {
    // "2+3" → stdout contains "5", exit 0 (Req 1 Scen 1)
    let mut cmd = Command::cargo_bin("calnexus").unwrap();
    cmd.arg("2+3").assert().success().stdout("5\n");
}

#[test]
fn test_complex_arithmetic() {
    // "(2+9)*7-6" → stdout contains "71", exit 0 (Req 1 Scen 2)
    let mut cmd = Command::cargo_bin("calnexus").unwrap();
    cmd.arg("(2+9)*7-6").assert().success().stdout("71\n");
}

// ===== Requirement 2: stdin Pipeline =====

#[test]
fn test_stdin_simple_expression() {
    // echo "2+3" | calnexus → stdout contains "5", exit 0 (Req 2 Scen 1)
    let mut cmd = Command::cargo_bin("calnexus").unwrap();
    cmd.write_stdin("2+3").assert().success().stdout("5\n");
}

#[test]
fn test_stdin_scientific_expression() {
    // echo "sin(pi/2)" | calnexus → stdout contains "1", exit 0 (Req 2 Scen 2)
    let mut cmd = Command::cargo_bin("calnexus").unwrap();
    cmd.write_stdin("sin(pi/2)")
        .assert()
        .success()
        .stdout("1\n");
}

// ===== Requirement 3: JSON Output Format =====

#[test]
fn test_json_arithmetic() {
    // --json "2+3" → {"result":5,"domain":"arithmetic","cache":"miss"} (Req 3 Scen 1)
    let mut cmd = Command::cargo_bin("calnexus").unwrap();
    cmd.arg("--json").arg("2+3").assert().success().stdout(
        r#"{"result":5,"domain":"arithmetic","cache":"miss"}
"#,
    );
}

#[test]
fn test_json_scientific() {
    // --json "sin(pi/2)" → domain="scientific", result=1, cache="miss" (Req 3 Scen 2)
    let mut cmd = Command::cargo_bin("calnexus").unwrap();
    cmd.arg("--json")
        .arg("sin(pi/2)")
        .assert()
        .success()
        .stdout(
            r#"{"result":1,"domain":"scientific","cache":"miss"}
"#,
        );
}

// ===== Requirement 4: Single Variable Binding =====

#[test]
fn test_single_var_arithmetic() {
    // --var x=10 "x*2" → 20 (Req 4 Scen 2)
    let mut cmd = Command::cargo_bin("calnexus").unwrap();
    cmd.arg("--var")
        .arg("x=10")
        .arg("x*2")
        .assert()
        .success()
        .stdout("20\n");
}

#[test]
fn test_single_var_scientific() {
    // --var x=1 "sin(x)" → sin(1) ≈ 0.8414709848078965 (Req 4 Scen 1)
    // 注:spec 给出的期望值 0.9999996829318346 与 sin(3.14) 不符(sin(3.14)≈0.00159),
    // 此处用 x=1 验证变量代入功能,期望值为 sin(1) 的正确结果。
    let mut cmd = Command::cargo_bin("calnexus").unwrap();
    cmd.arg("--var")
        .arg("x=1")
        .arg("sin(x)")
        .assert()
        .success()
        .stdout("0.8414709848078965\n");
}

// ===== Requirement 5: Multiple Variable Binding =====

#[test]
fn test_two_variables() {
    // --var x=1 --var y=2 "x+y" → 3 (Req 5 Scen 1)
    let mut cmd = Command::cargo_bin("calnexus").unwrap();
    cmd.arg("--var")
        .arg("x=1")
        .arg("--var")
        .arg("y=2")
        .arg("x+y")
        .assert()
        .success()
        .stdout("3\n");
}

#[test]
fn test_three_variables() {
    // --var x=1 --var y=2 --var z=3 "x+y+z" → 6 (Req 5 Scen 2)
    let mut cmd = Command::cargo_bin("calnexus").unwrap();
    cmd.arg("--var")
        .arg("x=1")
        .arg("--var")
        .arg("y=2")
        .arg("--var")
        .arg("z=3")
        .arg("x+y+z")
        .assert()
        .success()
        .stdout("6\n");
}

// ===== Requirement 6: Computation Error Exit Code =====

#[test]
fn test_division_by_zero_exit_code() {
    // "5/0" → exit 1, stderr non-empty (Req 6 Scen 1)
    let mut cmd = Command::cargo_bin("calnexus").unwrap();
    cmd.arg("5/0").assert().failure().code(1);
}

#[test]
fn test_modulo_by_zero_exit_code() {
    // "10%0" → exit 1, stderr non-empty (Req 6 Scen 2)
    let mut cmd = Command::cargo_bin("calnexus").unwrap();
    cmd.arg("10%0").assert().failure().code(1);
}

// ===== Requirement 7: Invalid Expression Exit Code =====

#[test]
fn test_double_operator_exit_code() {
    // "2++3" → exit 1, stderr contains parse error (Req 7 Scen 1)
    let mut cmd = Command::cargo_bin("calnexus").unwrap();
    cmd.arg("2++3").assert().failure().code(1);
}

#[test]
fn test_unbalanced_parens_exit_code() {
    // "(2+3" → exit 1, stderr contains parse error (Req 7 Scen 2)
    let mut cmd = Command::cargo_bin("calnexus").unwrap();
    cmd.arg("(2+3").assert().failure().code(1);
}

// ===== Requirement 8: System Error Exit Code =====

#[test]
fn test_unknown_flag_exit_code() {
    // --unknown-flag → exit 2 (Req 8 Scen 2, clap default)
    let mut cmd = Command::cargo_bin("calnexus").unwrap();
    cmd.arg("--unknown-flag")
        .arg("2+3")
        .assert()
        .failure()
        .code(2);
}

// ===== Requirement 9: No Arguments Reads stdin =====

#[test]
fn test_no_args_piped_stdin() {
    // echo "2+3" | calnexus (no args) → stdout contains "5", exit 0 (Req 9 Scen 1)
    let mut cmd = Command::cargo_bin("calnexus").unwrap();
    cmd.write_stdin("2+3").assert().success().stdout("5\n");
}

#[test]
fn test_no_args_piped_stdin_scientific() {
    // echo "cos(0)" | calnexus → stdout contains "1", exit 0 (Req 9 Scen 2)
    let mut cmd = Command::cargo_bin("calnexus").unwrap();
    cmd.write_stdin("cos(0)").assert().success().stdout("1\n");
}

// ===== Requirement 11: Version Flag =====

#[test]
fn test_long_version_flag() {
    // --version → stdout contains "calnexus", exit 0 (Req 11 Scen 1)
    let mut cmd = Command::cargo_bin("calnexus").unwrap();
    let output = cmd
        .arg("--version")
        .assert()
        .success()
        .get_output()
        .stdout
        .clone();
    let stdout = String::from_utf8(output).unwrap();
    assert!(
        stdout.contains("calnexus"),
        "expected version string, got: {}",
        stdout
    );
}

#[test]
fn test_short_version_flag() {
    // -V → stdout contains "calnexus", exit 0 (Req 11 Scen 2)
    let mut cmd = Command::cargo_bin("calnexus").unwrap();
    let output = cmd.arg("-V").assert().success().get_output().stdout.clone();
    let stdout = String::from_utf8(output).unwrap();
    assert!(
        stdout.contains("calnexus"),
        "expected version string, got: {}",
        stdout
    );
}

// ===== Requirement 12: Help Flag =====

#[test]
fn test_long_help_flag() {
    // --help → stdout contains help text, exit 0 (Req 12 Scen 1)
    let mut cmd = Command::cargo_bin("calnexus").unwrap();
    let output = cmd
        .arg("--help")
        .assert()
        .success()
        .get_output()
        .stdout
        .clone();
    let stdout = String::from_utf8(output).unwrap();
    assert!(stdout.contains("--json"), "help should mention --json");
    assert!(stdout.contains("--var"), "help should mention --var");
    assert!(
        stdout.contains("--version"),
        "help should mention --version"
    );
}

#[test]
fn test_short_help_flag() {
    // -h → stdout contains help text, exit 0 (Req 12 Scen 2)
    let mut cmd = Command::cargo_bin("calnexus").unwrap();
    let output = cmd.arg("-h").assert().success().get_output().stdout.clone();
    let stdout = String::from_utf8(output).unwrap();
    assert!(!stdout.is_empty(), "help output should not be empty");
}

// ===== --precision flag 覆盖(precision 模式 + BigRational 输出) =====

#[test]
fn test_precision_flag_with_division() {
    // --precision 5 "1/3" → "0.33333"
    // 覆盖 cli.rs lines 168, 172-175(precision 模式)+ lines 85, 87, 98(BigRational 输出)
    let mut cmd = Command::cargo_bin("calnexus").unwrap();
    cmd.arg("--precision")
        .arg("5")
        .arg("1/3")
        .assert()
        .success()
        .stdout("0.33333\n");
}

#[test]
fn test_precision_flag_with_integer_result() {
    // --precision 3 "4/2" → "2"(整数结果仍走 precision 路径,但 rational_to_result 返回 BigInt)
    // 覆盖 precision 模式 + BigInt 输出
    let mut cmd = Command::cargo_bin("calnexus").unwrap();
    cmd.arg("--precision")
        .arg("3")
        .arg("4/2")
        .assert()
        .success()
        .stdout("2\n");
}

#[test]
fn test_precision_flag_zero_decimals() {
    // --precision 0 "1/2" → "0"(0 位小数)
    let mut cmd = Command::cargo_bin("calnexus").unwrap();
    cmd.arg("--precision")
        .arg("0")
        .arg("1/2")
        .assert()
        .success()
        .stdout("0\n");
}

// ===== precision(N, expr) 函数调用覆盖 =====

#[test]
fn test_precision_function_call() {
    // precision(5, 1/3) → "0.33333"
    // 覆盖 cli.rs lines 210-214(extract_format_precision)+ lines 85, 87, 98(BigRational 输出)
    let mut cmd = Command::cargo_bin("calnexus").unwrap();
    cmd.arg("precision(5, 1/3)")
        .assert()
        .success()
        .stdout("0.33333\n");
}

#[test]
fn test_precision_function_call_json() {
    // --json precision(5, 1/3) → JSON with BigRational
    let mut cmd = Command::cargo_bin("calnexus").unwrap();
    cmd.arg("--json")
        .arg("precision(5, 1/3)")
        .assert()
        .success()
        .stdout(
            r#"{"result":"0.33333","domain":"precision","cache":"miss"}
"#,
        );
}

// ===== BigInt 输出覆盖 =====

#[test]
fn test_bigint_addition_output() {
    // 大整数 + 1 → BigInt 输出
    // 覆盖 cli.rs lines 81, 97(BigInt 输出)
    let mut cmd = Command::cargo_bin("calnexus").unwrap();
    cmd.arg("123456789012345678901234567890 + 1")
        .assert()
        .success()
        .stdout("123456789012345678901234567891\n");
}

#[test]
fn test_bigint_literal_output() {
    // 单个大整数字面量 → BigInt 输出
    let mut cmd = Command::cargo_bin("calnexus").unwrap();
    cmd.arg("123456789012345678901234567890")
        .assert()
        .success()
        .stdout("123456789012345678901234567890\n");
}

#[test]
fn test_bigint_json_output() {
    // --json 大整数 → JSON with BigInt
    // 覆盖 cli.rs line 81(BigInt JSON 输出)
    let mut cmd = Command::cargo_bin("calnexus").unwrap();
    cmd.arg("--json")
        .arg("123456789012345678901234567890")
        .assert()
        .success()
        .stdout(
            r#"{"result":"123456789012345678901234567890","domain":"precision","cache":"miss"}
"#,
        );
}

// ===== Complex 输出覆盖 =====

#[test]
fn test_complex_output_standard() {
    // 3+4i → "3+4i"
    // 覆盖 cli.rs lines 69, 71, 95(Complex 输出 + format_complex 正虚部)
    let mut cmd = Command::cargo_bin("calnexus").unwrap();
    cmd.arg("3+4i").assert().success().stdout("3+4i\n");
}

#[test]
fn test_complex_output_negative_imaginary() {
    // 3-4i → "3-4i"
    // 覆盖 cli.rs line 225(format_complex 负虚部分支)
    let mut cmd = Command::cargo_bin("calnexus").unwrap();
    cmd.arg("3-4i").assert().success().stdout("3-4i\n");
}

#[test]
fn test_complex_json_output() {
    // --json 3+4i → JSON with Complex
    // 覆盖 cli.rs lines 69, 71(Complex JSON 输出)
    let mut cmd = Command::cargo_bin("calnexus").unwrap();
    cmd.arg("--json").arg("3+4i").assert().success().stdout(
        r#"{"result":"3+4i","domain":"complex","cache":"miss"}
"#,
    );
}

// ===== Matrix 输出覆盖 =====

#[test]
fn test_matrix_output_2x2() {
    // [[1,2],[3,4]] → "[[1,2],[3,4]]"
    // 覆盖 cli.rs lines 75, 77, 96(Matrix 输出 + format_matrix)
    let mut cmd = Command::cargo_bin("calnexus").unwrap();
    cmd.arg("[[1,2],[3,4]]")
        .assert()
        .success()
        .stdout("[[1,2],[3,4]]\n");
}

#[test]
fn test_matrix_json_output() {
    // --json [[1,2],[3,4]] → JSON with Matrix
    // 覆盖 cli.rs lines 75, 77(Matrix JSON 输出)
    let mut cmd = Command::cargo_bin("calnexus").unwrap();
    cmd.arg("--json")
        .arg("[[1,2],[3,4]]")
        .assert()
        .success()
        .stdout(
            r#"{"result":"[[1,2],[3,4]]","domain":"matrix","cache":"miss"}
"#,
        );
}

#[test]
fn test_matrix_output_1x3() {
    // [[1,2,3]] → "[[1,2,3]]"
    // 覆盖 format_matrix 不同维度
    let mut cmd = Command::cargo_bin("calnexus").unwrap();
    cmd.arg("[[1,2,3]]")
        .assert()
        .success()
        .stdout("[[1,2,3]]\n");
}

// ===== BigRational JSON 输出覆盖 =====

#[test]
fn test_bigrational_json_output_with_precision_flag() {
    // --json --precision 5 "1/3" → JSON with BigRational
    // 覆盖 cli.rs lines 85, 87(BigRational JSON 输出)
    let mut cmd = Command::cargo_bin("calnexus").unwrap();
    cmd.arg("--json")
        .arg("--precision")
        .arg("5")
        .arg("1/3")
        .assert()
        .success()
        .stdout(
            r#"{"result":"0.33333","domain":"precision","cache":"miss"}
"#,
        );
}

#[test]
fn test_bigrational_output_fraction_form() {
    // 无 --precision 时 precision(0, 1/3) 应当走 precision 域
    // 但 precision(N, expr) 中 N 必须为正整数,N=0 会报错;
    // 改用 precision(5, 2/3) 验证非整数 BigRational 输出
    let mut cmd = Command::cargo_bin("calnexus").unwrap();
    cmd.arg("precision(5, 2/3)")
        .assert()
        .success()
        .stdout("0.66666\n");
}

// ===== 错误路径覆盖 =====

#[test]
fn test_invalid_var_missing_equals_exit_code() {
    // --var invalid(无 `=`)→ exit 2
    // 覆盖 cli.rs lines 52-54, 141(parse_vars 错误 + run 返回 2)
    let mut cmd = Command::cargo_bin("calnexus").unwrap();
    cmd.arg("--var")
        .arg("invalid")
        .arg("2+3")
        .assert()
        .failure()
        .code(2);
}

#[test]
fn test_invalid_var_non_numeric_value_exit_code() {
    // --var x=abc(值非数字)→ exit 2
    let mut cmd = Command::cargo_bin("calnexus").unwrap();
    cmd.arg("--var")
        .arg("x=abc")
        .arg("x*2")
        .assert()
        .failure()
        .code(2);
}

#[test]
fn test_empty_stdin_exit_code() {
    // echo "" | calnexus(空 stdin)→ exit 2(T007: ErrorKind::Usage 用法错误)
    // 覆盖 cli.rs get_expression empty stdin 错误走 handle_error → exit_code 2
    let mut cmd = Command::cargo_bin("calnexus").unwrap();
    cmd.write_stdin("").assert().failure().code(2);
}

#[test]
fn test_whitespace_only_stdin_exit_code() {
    // echo "   " | calnexus(仅空白 stdin)→ exit 2(T007: ErrorKind::Usage 用法错误)
    // 覆盖 cli.rs get_expression empty stdin 错误走 handle_error → exit_code 2
    let mut cmd = Command::cargo_bin("calnexus").unwrap();
    cmd.write_stdin("   \n  ").assert().failure().code(2);
}

// ===== 额外 BigInt 运算覆盖 =====

#[test]
fn test_bigint_multiplication_output() {
    // 大整数乘法 → BigInt 输出
    let mut cmd = Command::cargo_bin("calnexus").unwrap();
    cmd.arg("123456789012345678901234567890 * 2")
        .assert()
        .success()
        .stdout("246913578024691357802469135780\n");
}

#[test]
fn test_bigint_subtraction_output() {
    // 大整数减法 → BigInt 输出
    let mut cmd = Command::cargo_bin("calnexus").unwrap();
    cmd.arg("123456789012345678901234567890 - 1")
        .assert()
        .success()
        .stdout("123456789012345678901234567889\n");
}

// ===== Statistics 域 CLI 端到端测试(任务 17.2) =====

#[test]
fn test_statistics_mean_cli() {
    // mean([1,2,3,4,5]) → 3
    let mut cmd = Command::cargo_bin("calnexus").unwrap();
    cmd.arg("mean([1,2,3,4,5])")
        .assert()
        .success()
        .stdout("3\n");
}

#[test]
fn test_statistics_sum_cli() {
    // sum([1,2,3,4,5]) → 15
    let mut cmd = Command::cargo_bin("calnexus").unwrap();
    cmd.arg("sum([1,2,3,4,5])")
        .assert()
        .success()
        .stdout("15\n");
}

#[test]
fn test_statistics_median_cli() {
    // median([1,2,3,4,5]) → 3
    let mut cmd = Command::cargo_bin("calnexus").unwrap();
    cmd.arg("median([1,2,3,4,5])")
        .assert()
        .success()
        .stdout("3\n");
}

#[test]
fn test_statistics_json_output() {
    // --json mean([1,2,3,4,5]) → domain="statistics"
    let mut cmd = Command::cargo_bin("calnexus").unwrap();
    cmd.arg("--json")
        .arg("mean([1,2,3,4,5])")
        .assert()
        .success()
        .stdout(
            r#"{"result":3,"domain":"statistics","cache":"miss"}
"#,
        );
}

#[test]
fn test_statistics_stdin_pipeline() {
    // echo "sum([10,20,30])" | calnexus → 60
    let mut cmd = Command::cargo_bin("calnexus").unwrap();
    cmd.write_stdin("sum([10,20,30])")
        .assert()
        .success()
        .stdout("60\n");
}

#[test]
fn test_statistics_empty_list_error_cli() {
    // mean([]) → exit 1(空列表 DomainError)
    let mut cmd = Command::cargo_bin("calnexus").unwrap();
    cmd.arg("mean([])").assert().failure().code(1);
}

// ===== v0.8 新增域 CLI 端到端测试(TG9)=====

// ----- 9.1 NumberTheory CLI 测试 -----

#[test]
fn test_cli_number_theory_gcd() {
    let mut cmd = Command::cargo_bin("calnexus").unwrap();
    cmd.arg("gcd(12,18)").assert().success().stdout("6\n");
}

#[test]
fn test_cli_number_theory_is_prime() {
    let mut cmd = Command::cargo_bin("calnexus").unwrap();
    cmd.arg("is_prime(7)").assert().success().stdout("1\n");
}

#[test]
fn test_cli_number_theory_prime_sieve() {
    let mut cmd = Command::cargo_bin("calnexus").unwrap();
    cmd.arg("prime_sieve(10)")
        .assert()
        .success()
        .stdout("[2,3,5,7]\n");
}

#[test]
fn test_cli_number_theory_json_domain() {
    let mut cmd = Command::cargo_bin("calnexus").unwrap();
    cmd.arg("--json")
        .arg("gcd(12,18)")
        .assert()
        .success()
        .stdout(
            r#"{"result":6,"domain":"number_theory","cache":"miss"}
"#,
        );
}

// ----- 9.2 Combinatorics CLI 测试 -----

#[test]
fn test_cli_combinatorics_C() {
    let mut cmd = Command::cargo_bin("calnexus").unwrap();
    cmd.arg("C(10,3)").assert().success().stdout("120\n");
}

#[test]
fn test_cli_combinatorics_catalan() {
    let mut cmd = Command::cargo_bin("calnexus").unwrap();
    cmd.arg("catalan(5)").assert().success().stdout("42\n");
}

#[test]
fn test_cli_combinatorics_P() {
    let mut cmd = Command::cargo_bin("calnexus").unwrap();
    cmd.arg("P(5,2)").assert().success().stdout("20\n");
}

#[test]
fn test_cli_combinatorics_stdin() {
    let mut cmd = Command::cargo_bin("calnexus").unwrap();
    cmd.write_stdin("catalan(5)")
        .assert()
        .success()
        .stdout("42\n");
}

// ----- 9.3 Vector CLI 测试 -----

#[test]
fn test_cli_vector_dot() {
    let mut cmd = Command::cargo_bin("calnexus").unwrap();
    cmd.arg("dot([1,2,3],[4,5,6])")
        .assert()
        .success()
        .stdout("32\n");
}

#[test]
fn test_cli_vector_norm() {
    let mut cmd = Command::cargo_bin("calnexus").unwrap();
    cmd.arg("norm([3,4])").assert().success().stdout("5\n");
}

#[test]
fn test_cli_vector_add() {
    let mut cmd = Command::cargo_bin("calnexus").unwrap();
    cmd.arg("[1,2]+[3,4]").assert().success().stdout("[4,6]\n");
}

#[test]
fn test_cli_vector_dimension_mismatch_error() {
    let mut cmd = Command::cargo_bin("calnexus").unwrap();
    cmd.arg("[1,2]+[3,4,5]").assert().failure().code(1);
}

// ----- 9.4 Polynomial CLI 测试 -----

#[test]
fn test_cli_polynomial_add() {
    // poly_add(x+1, x+2) = 2x+3(系数升幂 [3,2],输出降幂 → "2x+3")
    let mut cmd = Command::cargo_bin("calnexus").unwrap();
    cmd.arg("poly_add(x+1,x+2)")
        .assert()
        .success()
        .stdout("2x+3\n");
}

#[test]
fn test_cli_polynomial_roots_real() {
    let mut cmd = Command::cargo_bin("calnexus").unwrap();
    cmd.arg("roots(x^2-4)")
        .assert()
        .success()
        .stdout("[2,-2]\n");
}

#[test]
fn test_cli_polynomial_roots_complex() {
    let mut cmd = Command::cargo_bin("calnexus").unwrap();
    cmd.arg("roots(x^2+1)").assert().success();
    // 复根输出格式:[0+1i,0-1i]
}

#[test]
fn test_cli_polynomial_factor() {
    let mut cmd = Command::cargo_bin("calnexus").unwrap();
    cmd.arg("factor(x^2-4)")
        .assert()
        .success()
        .stdout("(x-2)*(x+2)\n");
}

// ===== TG7.5 REPL CLI 测试 =====

#[test]
fn test_cli_repl_start_and_quit() {
    let mut cmd = Command::cargo_bin("calnexus").unwrap();
    cmd.arg("--repl").write_stdin(":quit\n").assert().success();
}

#[test]
fn test_cli_repl_evaluate_arithmetic() {
    let mut cmd = Command::cargo_bin("calnexus").unwrap();
    cmd.arg("--repl")
        .write_stdin("2+3*4\n:quit\n")
        .assert()
        .success()
        .stdout(predicates::str::contains("14"));
}

#[test]
fn test_cli_repl_evaluate_sin() {
    let mut cmd = Command::cargo_bin("calnexus").unwrap();
    cmd.arg("--repl")
        .write_stdin("sin(0)\n:quit\n")
        .assert()
        .success()
        .stdout(predicates::str::contains("0"));
}

#[test]
fn test_cli_repl_let_and_use() {
    let mut cmd = Command::cargo_bin("calnexus").unwrap();
    cmd.arg("--repl")
        .write_stdin(":let x = 10\nx*2\n:quit\n")
        .assert()
        .success()
        .stdout(predicates::str::contains("20"));
}

#[test]
fn test_cli_repl_vars_command() {
    let mut cmd = Command::cargo_bin("calnexus").unwrap();
    cmd.arg("--repl")
        .write_stdin(":let x = 42\n:vars\n:quit\n")
        .assert()
        .success()
        .stdout(predicates::str::contains("42"));
}

// ===== TG7.6 批量 CLI 测试 =====

#[test]
fn test_cli_batch_basic() {
    let mut tmp = tempfile::NamedTempFile::new().unwrap();
    use std::io::Write;
    writeln!(tmp, "2+3").unwrap();
    writeln!(tmp, "4*5").unwrap();
    tmp.flush().unwrap();

    let mut cmd = Command::cargo_bin("calnexus").unwrap();
    cmd.arg("--batch")
        .arg(tmp.path())
        .assert()
        .success()
        .stdout(predicates::str::contains("5"))
        .stdout(predicates::str::contains("20"));
}

#[test]
fn test_cli_batch_comment_skipped() {
    let mut tmp = tempfile::NamedTempFile::new().unwrap();
    use std::io::Write;
    writeln!(tmp, "# comment").unwrap();
    writeln!(tmp, "1+1").unwrap();
    tmp.flush().unwrap();

    let mut cmd = Command::cargo_bin("calnexus").unwrap();
    cmd.arg("--batch")
        .arg(tmp.path())
        .assert()
        .success()
        .stdout(predicates::str::contains("2"));
}

#[test]
fn test_cli_batch_stdin() {
    let mut cmd = Command::cargo_bin("calnexus").unwrap();
    cmd.arg("--batch")
        .arg("-")
        .write_stdin("2+3\n4*5\n")
        .assert()
        .success()
        .stdout(predicates::str::contains("5"))
        .stdout(predicates::str::contains("20"));
}

#[test]
fn test_cli_batch_json_output() {
    let mut tmp = tempfile::NamedTempFile::new().unwrap();
    use std::io::Write;
    writeln!(tmp, "2+3").unwrap();
    tmp.flush().unwrap();

    let mut cmd = Command::cargo_bin("calnexus").unwrap();
    cmd.arg("--batch")
        .arg(tmp.path())
        .arg("--json")
        .assert()
        .success()
        .stdout(predicates::str::contains("\"result\""))
        .stdout(predicates::str::contains("\"5\""));
}

#[test]
fn test_cli_batch_nonexistent_file() {
    let mut cmd = Command::cargo_bin("calnexus").unwrap();
    cmd.arg("--batch")
        .arg("/nonexistent/path/file.txt")
        .assert()
        .failure()
        .code(2);
}

// ===== TG7.7 Symbolic CLI 测试 =====

#[test]
fn test_cli_symbolic_diff_power() {
    let mut cmd = Command::cargo_bin("calnexus").unwrap();
    cmd.arg("diff(x^2, x)")
        .assert()
        .success()
        .stdout(predicates::str::contains("2"));
}

#[test]
fn test_cli_symbolic_diff_sin() {
    let mut cmd = Command::cargo_bin("calnexus").unwrap();
    cmd.arg("diff(sin(x), x)")
        .assert()
        .success()
        .stdout(predicates::str::contains("cos"));
}

#[test]
fn test_cli_symbolic_simplify() {
    let mut cmd = Command::cargo_bin("calnexus").unwrap();
    cmd.arg("simplify(x+0)").assert().success().stdout("x\n");
}

#[test]
fn test_cli_symbolic_limit() {
    let mut cmd = Command::cargo_bin("calnexus").unwrap();
    cmd.arg("limit(sin(x)/x, x, 0)")
        .assert()
        .success()
        .stdout(predicates::str::contains("1"));
}

// ===== JSON 输出路径覆盖(lines 125,127,131,133,137,139,143)=====

#[test]
fn test_json_vector_output() {
    // --json [1,2]+[3,4] → JSON with Vector result
    // 覆盖 cli.rs lines 125, 127(Vector JSON 输出分支)
    let mut cmd = Command::cargo_bin("calnexus").unwrap();
    cmd.arg("--json")
        .arg("[1,2]+[3,4]")
        .assert()
        .success()
        .stdout(
            r#"{"result":"[4,6]","domain":"vector","cache":"miss"}
"#,
        );
}

#[test]
fn test_json_polynomial_output() {
    // --json poly_add(x+1,x+2) → JSON with Polynomial result
    // 覆盖 cli.rs lines 131, 133(Polynomial JSON 输出分支)
    let mut cmd = Command::cargo_bin("calnexus").unwrap();
    cmd.arg("--json")
        .arg("poly_add(x+1,x+2)")
        .assert()
        .success()
        .stdout(
            r#"{"result":"2x+3","domain":"polynomial","cache":"miss"}
"#,
        );
}

#[test]
fn test_json_complex_list_output() {
    // --json roots(x^2+1) → JSON with ComplexList result
    // 覆盖 cli.rs lines 137, 139(ComplexList JSON 输出分支)
    let mut cmd = Command::cargo_bin("calnexus").unwrap();
    cmd.arg("--json")
        .arg("roots(x^2+1)")
        .assert()
        .success()
        .stdout(
            r#"{"result":"[-0+1i,-0-1i]","domain":"polynomial","cache":"miss"}
"#,
        );
}

#[test]
fn test_json_symbolic_output() {
    // --json diff(x^2, x) → JSON with Symbolic result
    // 覆盖 cli.rs line 143(Symbolic JSON 输出分支)
    let mut cmd = Command::cargo_bin("calnexus").unwrap();
    cmd.arg("--json")
        .arg("diff(x^2, x)")
        .assert()
        .success()
        .stdout(
            r#"{"result":"2*x","domain":"symbolic","cache":"miss"}
"#,
        );
}

// ===== parse_vars 错误路径覆盖(lines 55-57, 68-70)=====

#[test]
fn test_repl_invalid_var_exit_code() {
    // --repl --var invalid(无 `=`)→ exit 2
    // 覆盖 cli.rs lines 55, 56, 57(REPL 模式 parse_vars 错误 + return 2)
    let mut cmd = Command::cargo_bin("calnexus").unwrap();
    cmd.arg("--repl")
        .arg("--var")
        .arg("invalid")
        .write_stdin(":quit\n")
        .assert()
        .failure()
        .code(2);
}

#[test]
fn test_batch_invalid_var_exit_code() {
    // --batch <file> --var invalid(无 `=`)→ exit 2
    // 覆盖 cli.rs lines 68, 69, 70(batch 模式 parse_vars 错误 + return 2)
    let mut tmp = tempfile::NamedTempFile::new().unwrap();
    use std::io::Write;
    writeln!(tmp, "2+3").unwrap();
    tmp.flush().unwrap();
    let mut cmd = Command::cargo_bin("calnexus").unwrap();
    cmd.arg("--batch")
        .arg(tmp.path())
        .arg("--var")
        .arg("invalid")
        .assert()
        .failure()
        .code(2);
}

// ===== REPL format_result 分支覆盖(lines 296-302)=====
// format_result 在 REPL evaluate_line 中调用,需通过 REPL 触发各 EvalResult 变体

#[test]
fn test_repl_complex_format_result() {
    // REPL 中求值 3+4i → format_result Complex 分支
    // 覆盖 cli.rs line 296(format_result::Complex)
    let mut cmd = Command::cargo_bin("calnexus").unwrap();
    cmd.arg("--repl")
        .write_stdin("3+4i\n:quit\n")
        .assert()
        .success()
        .stdout(predicates::str::contains("3+4i"));
}

#[test]
fn test_repl_matrix_format_result() {
    // REPL 中求值 [[1,2],[3,4]] → format_result Matrix 分支
    // 覆盖 cli.rs line 297(format_result::Matrix)
    let mut cmd = Command::cargo_bin("calnexus").unwrap();
    cmd.arg("--repl")
        .write_stdin("[[1,2],[3,4]]\n:quit\n")
        .assert()
        .success()
        .stdout(predicates::str::contains("[[1,2],[3,4]]"));
}

#[test]
fn test_repl_bigint_format_result() {
    // REPL 中求值大整数 → format_result BigInt 分支
    // 覆盖 cli.rs line 298(format_result::BigInt)
    let mut cmd = Command::cargo_bin("calnexus").unwrap();
    cmd.arg("--repl")
        .write_stdin("123456789012345678901234567890\n:quit\n")
        .assert()
        .success()
        .stdout(predicates::str::contains("123456789012345678901234567890"));
}

#[test]
fn test_repl_bigrational_format_result() {
    // REPL --precision 5 中求值 1/3 → format_result BigRational 分支
    // 覆盖 cli.rs line 299(format_result::BigRational)
    let mut cmd = Command::cargo_bin("calnexus").unwrap();
    cmd.arg("--repl")
        .arg("--precision")
        .arg("5")
        .write_stdin("1/3\n:quit\n")
        .assert()
        .success()
        .stdout(predicates::str::contains("0.33333"));
}

#[test]
fn test_repl_vector_format_result() {
    // REPL 中求值 [1,2]+[3,4] → format_result Vector 分支
    // 覆盖 cli.rs line 300(format_result::Vector)
    let mut cmd = Command::cargo_bin("calnexus").unwrap();
    cmd.arg("--repl")
        .write_stdin("[1,2]+[3,4]\n:quit\n")
        .assert()
        .success()
        .stdout(predicates::str::contains("[4,6]"));
}

#[test]
fn test_repl_polynomial_format_result() {
    // REPL 中求值 poly_add(x+1,x+2) → format_result Polynomial 分支
    // 覆盖 cli.rs line 301(format_result::Polynomial)
    let mut cmd = Command::cargo_bin("calnexus").unwrap();
    cmd.arg("--repl")
        .write_stdin("poly_add(x+1,x+2)\n:quit\n")
        .assert()
        .success()
        .stdout(predicates::str::contains("2x+3"));
}

#[test]
fn test_repl_complex_list_format_result() {
    // REPL 中求值 roots(x^2+1) → format_result ComplexList 分支
    // 覆盖 cli.rs line 302(format_result::ComplexList)
    let mut cmd = Command::cargo_bin("calnexus").unwrap();
    cmd.arg("--repl")
        .write_stdin("roots(x^2+1)\n:quit\n")
        .assert()
        .success()
        .stdout(predicates::str::contains("1i"));
}

// ===== format_polynomial 分支覆盖(lines 343,349,351,357-360,362,369,375)=====

#[test]
fn test_polynomial_sub_x2_minus_x() {
    // poly_sub(x^2, x) → "x^2-x"
    // 覆盖 cli.rs lines 343(coef=0 跳过), 351(i=1,coef=-1 → "-x"),
    //   357-358(i=2,coef=1 → "x^2"), 375(负项拼接)
    let mut cmd = Command::cargo_bin("calnexus").unwrap();
    cmd.arg("poly_sub(x^2, x)")
        .assert()
        .success()
        .stdout("x^2-x\n");
}

#[test]
fn test_polynomial_neg_x2() {
    // poly_sub(0, x^2) → "-x^2"
    // 覆盖 cli.rs lines 359-360(i>=2,coef=-1 → "-x^N")
    let mut cmd = Command::cargo_bin("calnexus").unwrap();
    cmd.arg("poly_sub(0, x^2)")
        .assert()
        .success()
        .stdout("-x^2\n");
}

#[test]
fn test_polynomial_x_leading_one() {
    // poly_add(x, 0) → "x"
    // 覆盖 cli.rs line 349(i=1,coef=1 → "x")
    let mut cmd = Command::cargo_bin("calnexus").unwrap();
    cmd.arg("poly_add(x, 0)").assert().success().stdout("x\n");
}

#[test]
fn test_polynomial_all_zero_coeffs() {
    // poly_sub(x, x) → "0"
    // 覆盖 cli.rs line 369(所有系数为零 → "0")
    let mut cmd = Command::cargo_bin("calnexus").unwrap();
    cmd.arg("poly_sub(x, x)").assert().success().stdout("0\n");
}

#[test]
fn test_polynomial_general_coef_high_degree() {
    // poly_mul(2, x^2) → "2x^2"
    // 覆盖 cli.rs line 362(i>=2,一般系数 → "cx^N")
    let mut cmd = Command::cargo_bin("calnexus").unwrap();
    cmd.arg("poly_mul(2, x^2)")
        .assert()
        .success()
        .stdout("2x^2\n");
}

// ===== IT-CLI-003/009/010: v1.1 新增 CLI 标志集成测试 =====

/// IT-CLI-003: `--latex "diff(x^2,x)"` 输出 LaTeX 格式
#[test]
fn it_cli_003_latex_output() {
    let mut cmd = Command::cargo_bin("calnexus").unwrap();
    cmd.args(["--latex", "diff(x^2,x)"])
        .assert()
        .success()
        .stdout(predicates::str::contains("\\frac{d}{dx}"));
}

/// IT-CLI-009: `--canonical "3+2"` → `(+ 2 3)`(PRD §3.2.4)
#[test]
fn it_cli_009_canonical_output() {
    let mut cmd = Command::cargo_bin("calnexus").unwrap();
    cmd.args(["--canonical", "3+2"])
        .assert()
        .success()
        .stdout("(+ 2 3)\n");
}

/// IT-CLI-010: `--steps "(2+9)*7-6"` → 步骤输出
#[test]
fn it_cli_010_steps_output() {
    let mut cmd = Command::cargo_bin("calnexus").unwrap();
    cmd.args(["--steps", "(2+9)*7-6"])
        .assert()
        .success()
        .stdout(predicates::str::contains("2+9=11"));
}

/// IT-CLI: `--latex --json` 冲突应退出码 2
#[test]
fn it_cli_latex_json_conflict_exit_2() {
    let mut cmd = Command::cargo_bin("calnexus").unwrap();
    cmd.args(["--latex", "--json", "2+3"])
        .assert()
        .failure()
        .code(2);
}

/// IT-CLI: `--canonical --repl` 冲突应退出码 2
#[test]
fn it_cli_canonical_repl_conflict_exit_2() {
    let mut cmd = Command::cargo_bin("calnexus").unwrap();
    cmd.args(["--canonical", "--repl"])
        .assert()
        .failure()
        .code(2);
}

/// IT-CLI: `--canonical --batch` 冲突应退出码 2
#[test]
fn it_cli_canonical_batch_conflict_exit_2() {
    let mut cmd = Command::cargo_bin("calnexus").unwrap();
    cmd.args(["--canonical", "--batch", "-"])
        .assert()
        .failure()
        .code(2);
}

// ===== 不可覆盖行说明 =====
// 以下 cli.rs 行因技术限制无法通过集成测试覆盖:
// - lines 178, 179:TTY stdin 路径(io::stdin().is_terminal() 为 true 时显示 help)。
//   集成测试中 stdin 始终为管道(非 TTY),此分支不可达。
//   line 179 的 `return Err(0)` 标注为 unreachable,clap --help 会先退出。
// - lines 184, 185:stdin read_to_string 错误路径。管道 stdin 不会产生 I/O 错误,
//   无法在集成测试中模拟。
// - line 338:format_polynomial 空系数向量(p.is_empty())。
//   多项式域对所有输入至少返回 [0.0],无法产生空向量。

// ===== 覆盖 cli.rs --canonical / --latex / --steps 错误路径 =====

/// `--canonical "2++3"` → 解析错误,退出码 1(lines 119-121)
#[test]
fn it_cli_canonical_parse_error_exit_1() {
    let mut cmd = Command::cargo_bin("calnexus").unwrap();
    cmd.args(["--canonical", "2++3"]).assert().failure().code(1);
}

// 注:lines 129-131(--canonical canonicalize 错误)不可覆盖:
// canonicalize_no_fold 不做常量折叠,对任何合法解析 AST 均返回 Ok。

/// `--latex "2++3"` → 解析错误,退出码 1(lines 138-140)
#[test]
fn it_cli_latex_parse_error_exit_1() {
    let mut cmd = Command::cargo_bin("calnexus").unwrap();
    cmd.args(["--latex", "2++3"]).assert().failure().code(1);
}

/// `--latex "1/0"` → 规范化错误,退出码 1(lines 145-147)
#[test]
fn it_cli_latex_canonicalize_error_exit_1() {
    let mut cmd = Command::cargo_bin("calnexus").unwrap();
    cmd.args(["--latex", "1/0"]).assert().failure().code(1);
}

/// `--steps "1/0"` → 步骤生成错误(除零),退出码 1(lines 159-161)
#[test]
fn it_cli_steps_div_zero_exit_1() {
    let mut cmd = Command::cargo_bin("calnexus").unwrap();
    cmd.args(["--steps", "1/0"]).assert().failure().code(1);
}

/// `--latex "sqrt(-1)"` → 求值错误(域错误),退出码 1(lines 174-176)
#[test]
fn it_cli_latex_eval_error_exit_1() {
    let mut cmd = Command::cargo_bin("calnexus").unwrap();
    cmd.args(["--latex", "sqrt(-1)"]).assert().failure().code(1);
}

// ===== T0.4.7: --explain / --lang / --json error / 退出码契约 =====

/// `--explain "2++3"` → stderr 包含详细解释,退出码 1
#[test]
fn test_explain_parse_error_exit_1() {
    let mut cmd = Command::cargo_bin("calnexus").unwrap();
    cmd.args(["--explain", "2++3"])
        .assert()
        .failure()
        .code(1)
        .stderr(predicates::str::contains("Parse error"));
}

/// `--explain "1/0"` → stderr 包除零 hint,退出码 1
#[test]
fn test_explain_div_zero_hint() {
    let mut cmd = Command::cargo_bin("calnexus").unwrap();
    cmd.args(["--explain", "1/0"])
        .assert()
        .failure()
        .code(1)
        .stderr(predicates::str::contains("check divisor"));
}

/// `--lang zh "2++3"` → stderr 包含中文错误消息,退出码 1
#[test]
fn test_lang_zh_parse_error() {
    let mut cmd = Command::cargo_bin("calnexus").unwrap();
    cmd.args(["--lang", "zh", "2++3"])
        .assert()
        .failure()
        .code(1)
        .stderr(predicates::str::contains("解析错误"));
}

/// `--lang en "2++3"` → stderr 包含英文错误消息,退出码 1
#[test]
fn test_lang_en_parse_error() {
    let mut cmd = Command::cargo_bin("calnexus").unwrap();
    cmd.args(["--lang", "en", "2++3"])
        .assert()
        .failure()
        .code(1)
        .stderr(predicates::str::contains("Parse error"));
}

/// `--json "2++3"` → stdout 输出 JSON error 对象(非双层嵌套),退出码 1
/// 断言用 starts_with 捕获 `{"error":{"error":...}}` 双层嵌套 bug
#[test]
fn test_json_error_output_exit_1() {
    let mut cmd = Command::cargo_bin("calnexus").unwrap();
    cmd.args(["--json", "2++3"])
        .assert()
        .failure()
        .code(1)
        .stdout(predicates::str::starts_with(r#"{"error":{"kind":"#));
}

/// `--explain --json` 互斥 → clap 报错,退出码 2
#[test]
fn test_explain_conflicts_with_json() {
    let mut cmd = Command::cargo_bin("calnexus").unwrap();
    cmd.args(["--explain", "--json", "2+3"])
        .assert()
        .failure()
        .code(2);
}

/// 成功求值退出码 0
#[test]
fn test_success_exit_0() {
    let mut cmd = Command::cargo_bin("calnexus").unwrap();
    cmd.arg("2+3").assert().success().code(0);
}

/// `--explain "asin(2)"` → stderr 包含 domain hint,退出码 1
#[test]
fn test_explain_domain_hint() {
    let mut cmd = Command::cargo_bin("calnexus").unwrap();
    cmd.args(["--explain", "asin(2)"])
        .assert()
        .failure()
        .code(1)
        .stderr(predicates::str::contains("asin domain is [-1, 1]"));
}

/// `--explain --lang en "2++3"` → stderr 不含任何中文字符(diting HIGH-1)
///
/// 验证 friendly()/to_explain() 中的 5 个硬编码中文标签(位置/提示/错误类别/退出码/建议)
/// 在 --lang en 时不出现。T001 Red 阶段:此测试应失败(当前代码含中文标签)。
#[test]
fn test_explain_lang_en_no_chinese_labels() {
    let mut cmd = Command::cargo_bin("calnexus").unwrap();
    let assert = cmd
        .args(["--explain", "--lang", "en", "2++3"])
        .assert()
        .failure()
        .code(1);
    let stderr = String::from_utf8_lossy(&assert.get_output().stderr);
    // 不含任何 CJK 字符(Unicode 范围 \u4e00-\u9fff)
    let has_cjk = stderr
        .chars()
        .any(|c| ('\u{4e00}'..='\u{9fff}').contains(&c));
    assert!(
        !has_cjk,
        "stderr with --lang en should not contain CJK characters, got: {}",
        stderr
    );
}

// ===== T007: --json 模式下 parse_vars / get_expression 错误应走 JSON 输出 =====
// 当前 Bug:parse_vars() 返回 Result<_, String>,get_expression() 返回 Result<_, i32>,
// 错误时直接 eprintln 文本 + 返回退出码,绕过 handle_error(),
// 导致 --json/--explain 模式下错误输出格式不一致(总是 eprintln 文本而非 JSON)。

/// `--json --var invalid 2+3` → stdout 应含 `"error"` JSON 字段,stderr 不应含 `error: invalid --var`。
///
/// T007 Red 阶段:此测试应失败(当前 parse_vars 错误走 eprintln 文本路径,不走 JSON)。
#[test]
fn test_json_mode_var_error_output_format() {
    let mut cmd = Command::cargo_bin("calnexus").unwrap();
    let assert = cmd
        .args(["--json", "--var", "invalid", "2+3"])
        .assert()
        .failure();
    let output = assert.get_output();
    let stdout = String::from_utf8_lossy(&output.stdout);
    let stderr = String::from_utf8_lossy(&output.stderr);
    // --json 模式错误应走 JSON 输出(stdout 含 "error" 字段)
    assert!(
        stdout.contains("\"error\""),
        "stdout should contain JSON error field, got stdout: {:?}, stderr: {:?}",
        stdout,
        stderr
    );
    // 不应走 eprintln 文本路径(stderr 不应含 "error: invalid --var")
    assert!(
        !stderr.contains("error: invalid --var"),
        "stderr should not contain eprintln text path, got stderr: {:?}",
        stderr
    );
}

/// `echo "" | calnexus --json`(空 stdin)→ stdout 应含 `"error"` JSON 字段,stderr 不应含 `error: empty expression`。
///
/// T007 Red 阶段:此测试应失败(当前 get_expression 错误走 eprintln 文本路径,不走 JSON)。
#[test]
fn test_json_mode_empty_stdin_error_output_format() {
    let mut cmd = Command::cargo_bin("calnexus").unwrap();
    let assert = cmd.arg("--json").write_stdin("").assert().failure();
    let output = assert.get_output();
    let stdout = String::from_utf8_lossy(&output.stdout);
    let stderr = String::from_utf8_lossy(&output.stderr);
    // --json 模式错误应走 JSON 输出(stdout 含 "error" 字段)
    assert!(
        stdout.contains("\"error\""),
        "stdout should contain JSON error field, got stdout: {:?}, stderr: {:?}",
        stdout,
        stderr
    );
    // 不应走 eprintln 文本路径(stderr 不应含 "error: empty expression")
    assert!(
        !stderr.contains("error: empty expression"),
        "stderr should not contain eprintln text path, got stderr: {:?}",
        stderr
    );
}

/// T008: --lang 未知值时 clap 退出码 2(fail-loud)
///
/// PossibleValuesParser 限制 --lang 只接受 "en"/"zh",
/// 传入 "fr" 时 clap 报错并退出码 2。
#[test]
fn test_lang_invalid_value_exit_2() {
    let mut cmd = Command::cargo_bin("calnexus").unwrap();
    let assert = cmd.args(["--lang", "fr", "2+3"]).assert().failure();
    let output = assert.get_output();
    assert_eq!(
        output.status.code(),
        Some(2),
        "invalid --lang value should exit with code 2, got: {:?}",
        output.status
    );
}

// ===== T021: --serve-http/--serve-mcp flag 冲突与 feature 门控 =====

/// T021: --serve-http 与 --repl 冲突时 clap 退出码 2(server feature 启用)。
#[cfg(feature = "server")]
#[test]
fn test_serve_http_flag_conflicts_repl() {
    let mut cmd = Command::cargo_bin("calnexus").unwrap();
    let assert = cmd.args(["--serve-http", "--repl"]).assert().failure();
    let output = assert.get_output();
    assert_eq!(
        output.status.code(),
        Some(2),
        "--serve-http --repl should exit with code 2 (conflict), got: {:?}",
        output.status
    );
}

/// T021: --serve-mcp 与 --batch 冲突时 clap 退出码 2(server feature 启用)。
#[cfg(feature = "server")]
#[test]
fn test_serve_mcp_flag_conflicts_batch() {
    let mut cmd = Command::cargo_bin("calnexus").unwrap();
    let assert = cmd.args(["--serve-mcp", "--batch", "-"]).assert().failure();
    let output = assert.get_output();
    assert_eq!(
        output.status.code(),
        Some(2),
        "--serve-mcp --batch should exit with code 2 (conflict), got: {:?}",
        output.status
    );
}

/// T021: --serve-http 与 --serve-mcp 互相冲突时 clap 退出码 2。
/// 防止静默吞 flag(规则 12:失败必须显性化)。
#[cfg(feature = "server")]
#[test]
fn test_serve_http_conflicts_serve_mcp() {
    let mut cmd = Command::cargo_bin("calnexus").unwrap();
    let assert = cmd.args(["--serve-http", "--serve-mcp"]).assert().failure();
    let output = assert.get_output();
    assert_eq!(
        output.status.code(),
        Some(2),
        "--serve-http --serve-mcp should exit with code 2 (mutual conflict), got: {:?}",
        output.status
    );
}

/// T021: --serve-http 与 --json 冲突时 clap 退出码 2(server feature 启用)。
#[cfg(feature = "server")]
#[test]
fn test_serve_http_conflicts_json() {
    let mut cmd = Command::cargo_bin("calnexus").unwrap();
    let assert = cmd.args(["--serve-http", "--json"]).assert().failure();
    let output = assert.get_output();
    assert_eq!(
        output.status.code(),
        Some(2),
        "--serve-http --json should exit with code 2 (conflict), got: {:?}",
        output.status
    );
}

/// T021: 无 server feature 时 --serve-http 是 unknown argument,clap 退出码 2。
/// 验证 feature 门控正确:无 server feature 时 flag 不存在。
#[cfg(not(feature = "server"))]
#[test]
fn test_serve_http_without_server_feature() {
    let mut cmd = Command::cargo_bin("calnexus").unwrap();
    let assert = cmd.args(["--serve-http"]).assert().failure();
    let output = assert.get_output();
    assert_eq!(
        output.status.code(),
        Some(2),
        "--serve-http without server feature should exit with code 2 (unknown arg), got: {:?}",
        output.status
    );
}

/// BUG-007 修复:`--batch --precision` 组合应显式冲突退出码 2。
///
/// 原行为:`batch.rs:88` 硬编码 `None` 忽略 precision,违反规则 12(失败必须显性化)。
/// 修复:添加 `conflicts_with_all` 互斥,clap 在参数解析阶段拒绝组合。
///
/// 使用临时文件(非 stdin)确保 batch 模式实际运行:未修复时退出 0(静默忽略 precision),
/// 修复后退出 2(冲突)。若用 stdin,空输入也会退出 2,无法区分冲突与空输入。
#[test]
fn test_batch_precision_conflict_exit_2() {
    let mut tmp = tempfile::NamedTempFile::new().unwrap();
    use std::io::Write;
    writeln!(tmp, "2+3").unwrap();
    tmp.flush().unwrap();

    let mut cmd = Command::cargo_bin("calnexus").unwrap();
    let assert = cmd
        .arg("--batch")
        .arg(tmp.path())
        .arg("--precision")
        .arg("5")
        .assert()
        .failure();
    let output = assert.get_output();
    assert_eq!(
        output.status.code(),
        Some(2),
        "--batch --precision should exit with code 2 (conflict), got: {:?}",
        output.status
    );
}

/// 测试矩阵 A802:`--repl --batch` 互斥。
///
/// 修复:此前 `repl` 与 `batch` 未声明互相 conflicts_with_all,运行时优先级链
/// (`run()` 中 `if cli.repl { ... }` 先于 `if let Some(path) = &cli.batch`) 会导致
/// batch 被 silent 吞掉(静默 fallback 到 REPL,隐性失败,违反规则 12 显性化)。
/// 添加双向 conflict 后 clap 在解析阶段拒绝,退出 2。
#[test]
fn test_repl_batch_conflict_exit_2() {
    let mut tmp = tempfile::NamedTempFile::new().unwrap();
    use std::io::Write;
    writeln!(tmp, "2+3").unwrap();
    tmp.flush().unwrap();

    let mut cmd = Command::cargo_bin("calnexus").unwrap();
    // 用临时文件(非 stdin)确保走 batch 路径而非空输入;冲突在 clap 解析阶段触发,
    // REPL 不会启动,但提供 quit 输入以防万一。
    let assert = cmd
        .arg("--repl")
        .arg("--batch")
        .arg(tmp.path())
        .write_stdin(":quit\n")
        .assert()
        .failure();
    let output = assert.get_output();
    assert_eq!(
        output.status.code(),
        Some(2),
        "--repl --batch should exit with code 2 (conflict), got: {:?}",
        output.status
    );
}