tldr-cli 0.1.3

CLI binary for TLDR code analysis tool
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
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
//! Tests for output formatting (Phase 11)
//!
//! Tests for clone and similarity report text/DOT formatters.
//! Addresses premortem risks:
//! - S8-P3-T5: Clone type names are jargon
//! - S8-P3-T7: Empty results don't explain why
//! - S8-P3-T11: DOT node IDs with special characters

use std::path::PathBuf;

use crate::output::{
    clone_type_description, empty_results_hints, escape_dot_id, format_clones_dot,
    format_clones_text, format_similarity_text,
};
use tldr_core::analysis::{
    CloneConfig, CloneFragment, ClonePair, CloneStats, CloneType, ClonesOptions, ClonesReport,
    SimilarityConfig, SimilarityFragment, SimilarityMetric, SimilarityReport, SimilarityScores,
    TokenBreakdown,
};

// =============================================================================
// Clone Type Description Tests (S8-P3-T5)
// =============================================================================

/// Test: Clone type descriptions are human-readable, not jargon
/// Risk: S8-P3-T5 - Clone type names are jargon
#[test]
fn test_clone_type_description_type1() {
    let desc = clone_type_description(&CloneType::Type1);
    assert!(
        desc.contains("exact") || desc.contains("identical"),
        "Type-1 description should mention 'exact' or 'identical': {}",
        desc
    );
}

#[test]
fn test_clone_type_description_type2() {
    let desc = clone_type_description(&CloneType::Type2);
    assert!(
        desc.contains("identifier") || desc.contains("renamed") || desc.contains("literal"),
        "Type-2 description should mention 'identifier', 'renamed', or 'literal': {}",
        desc
    );
}

#[test]
fn test_clone_type_description_type3() {
    let desc = clone_type_description(&CloneType::Type3);
    assert!(
        desc.contains("similar") || desc.contains("addition") || desc.contains("deletion"),
        "Type-3 description should mention 'similar', 'addition', or 'deletion': {}",
        desc
    );
}

/// Test: Text output includes compact clone type column
#[test]
fn test_text_output_clone_type_explained() {
    // GIVEN: A report with a Type-2 clone
    let report = make_clone_report_with_pairs(vec![make_type2_clone_pair()]);

    // WHEN: Formatting as text
    let text = format_clones_text(&report);

    // THEN: Should include compact type indicator T2
    assert!(
        text.contains("T2"),
        "Type-2 should show compact 'T2' indicator, got: {}",
        text
    );
}

// =============================================================================
// Empty Results Hints Tests (S8-P3-T7)
// =============================================================================

/// Test: Empty results produce concise output
#[test]
fn test_empty_results_concise() {
    // GIVEN: A report with no clones
    let report = make_clone_report_with_pairs(vec![]);

    // WHEN: Formatting as text
    let text = format_clones_text(&report);

    // THEN: Should be concise with "No clones found."
    assert!(
        text.contains("No clones found."),
        "Empty results should say 'No clones found.', got: {}",
        text
    );
}

/// Test: Hints suggest lowering threshold
#[test]
fn test_empty_results_hint_threshold() {
    // GIVEN: Options and stats for empty results
    let options = ClonesOptions::new();
    let stats = CloneStats {
        files_analyzed: 10,
        total_tokens: 5000,
        ..Default::default()
    };

    // WHEN: Getting hints
    let hints = empty_results_hints(&options, &stats);

    // THEN: Should suggest threshold adjustment
    assert!(
        hints.iter().any(|h| h.contains("threshold")),
        "Should suggest threshold adjustment: {:?}",
        hints
    );
}

/// Test: Hints include analysis stats
#[test]
fn test_empty_results_hint_stats() {
    let options = ClonesOptions::new();
    let stats = CloneStats {
        files_analyzed: 42,
        total_tokens: 15234,
        ..Default::default()
    };

    let hints = empty_results_hints(&options, &stats);

    // Should include what was analyzed
    let hints_str = hints.join(" ");
    assert!(
        hints_str.contains("42") && hints_str.contains("15234"),
        "Should include files/tokens analyzed: {:?}",
        hints
    );
}

// =============================================================================
// DOT Output Special Characters Tests (S8-P3-T11)
// =============================================================================

/// Test: DOT node IDs escape backslashes (Windows paths)
/// Risk: S8-P3-T11 - DOT node IDs with special characters break graphviz
#[test]
fn test_dot_escape_backslash() {
    let id = escape_dot_id(r"C:\Users\test\file.py:10-20");
    assert!(
        !id.contains('\\') || id.contains(r"\\"),
        "Backslashes should be escaped or converted: {}",
        id
    );
}

/// Test: DOT node IDs escape quotes
#[test]
fn test_dot_escape_quotes() {
    let id = escape_dot_id(r#"file with "quotes".py:1-10"#);
    assert!(
        !id.contains('"') || id.contains(r#"\""#),
        "Quotes should be escaped: {}",
        id
    );
}

/// Test: DOT node IDs handle spaces
#[test]
fn test_dot_escape_spaces() {
    let id = escape_dot_id("path with spaces/file.py:1-10");
    // Spaces should either be in quoted strings or escaped
    assert!(
        id.starts_with('"') && id.ends_with('"'),
        "Node ID with spaces should be quoted: {}",
        id
    );
}

/// Test: DOT output is valid for graphviz
#[test]
fn test_dot_output_special_characters_escaped() {
    // GIVEN: A report with paths containing special characters
    let report = make_clone_report_with_special_paths();

    // WHEN: Formatting as DOT
    let dot = format_clones_dot(&report);

    // THEN: Should be valid DOT syntax (basic checks)
    assert!(dot.starts_with("digraph"), "Should start with digraph");
    assert!(dot.contains("->"), "Should contain edges");
    // No unquoted special characters
    assert!(
        !dot.contains(" -> ") || dot.contains("\" -> \""),
        "Edges should use quoted node IDs"
    );
}

/// Test: DOT output includes similarity percentages
#[test]
fn test_dot_output_includes_similarity() {
    let report = make_clone_report_with_pairs(vec![make_type2_clone_pair()]);

    let dot = format_clones_dot(&report);

    // Should include similarity as edge label
    assert!(
        dot.contains("label=") && (dot.contains("92") || dot.contains("0.92")),
        "DOT should include similarity label: {}",
        dot
    );
}

// =============================================================================
// Text Output Format Tests
// =============================================================================

/// Test: Text output includes compact header with stats
#[test]
fn test_text_output_header() {
    let report = make_clone_report_with_pairs(vec![]);

    let text = format_clones_text(&report);

    assert!(
        text.contains("Clone Detection:"),
        "Should have compact header"
    );
    assert!(
        text.contains("pairs") && text.contains("files") && text.contains("tokens"),
        "Header should include pairs, files, and tokens: {}",
        text
    );
}

/// Test: Text output includes statistics in compact header
#[test]
fn test_text_output_stats() {
    let mut report = make_clone_report_with_pairs(vec![]);
    report.stats = CloneStats {
        files_analyzed: 42,
        total_tokens: 15234,
        clones_found: 8,
        type1_count: 2,
        type2_count: 3,
        type3_count: 3,
        class_count: Some(3),
        detection_time_ms: 150,
    };

    let text = format_clones_text(&report);

    // Compact header: "Clone Detection: 8 pairs in 42 files (15234 tokens)"
    assert!(text.contains("42"), "Should show files analyzed");
    assert!(text.contains("15234"), "Should show tokens analyzed");
    assert!(text.contains("8"), "Should show clones found");
}

/// Test: Text output formats clone pairs as compact table rows
#[test]
fn test_text_output_clone_pair_format() {
    let report = make_clone_report_with_pairs(vec![make_type2_clone_pair()]);

    let text = format_clones_text(&report);

    // Should show file paths (with common prefix stripped)
    assert!(
        text.contains("login.py") && text.contains("signup.py"),
        "Should show file names: {}",
        text
    );
    // Should show line ranges
    assert!(
        text.contains("45-62") && text.contains("23-40"),
        "Should show line ranges: {}",
        text
    );
    // Should show similarity percentage
    assert!(text.contains("92%"), "Should show similarity: {}", text);
    // Should show compact type
    assert!(text.contains("T2"), "Should show compact type: {}", text);
    // Should have table header
    assert!(
        text.contains("File A") && text.contains("File B"),
        "Should have table header: {}",
        text
    );
}

// =============================================================================
// Similarity Text Output Tests
// =============================================================================

/// Test: Similarity text output includes all metrics
#[test]
fn test_similarity_text_output_metrics() {
    let report = make_similarity_report();

    let text = format_similarity_text(&report);

    assert!(
        text.contains("Dice") || text.contains("dice"),
        "Should include Dice"
    );
    assert!(
        text.contains("Jaccard") || text.contains("jaccard"),
        "Should include Jaccard"
    );
}

/// Test: Similarity text output includes interpretation
#[test]
fn test_similarity_text_output_interpretation() {
    let report = make_similarity_report();

    let text = format_similarity_text(&report);

    // Should have human-readable interpretation
    assert!(
        text.contains("similar") || text.contains("match") || text.contains("related"),
        "Should include interpretation: {}",
        text
    );
}

/// Test: Similarity text output includes token breakdown
#[test]
fn test_similarity_text_output_token_breakdown() {
    let report = make_similarity_report();

    let text = format_similarity_text(&report);

    // Should show shared/unique tokens
    assert!(
        text.contains("shared") || text.contains("Shared"),
        "Should include shared tokens"
    );
}

// =============================================================================
// Test Fixtures
// =============================================================================

fn make_clone_report_with_pairs(pairs: Vec<ClonePair>) -> ClonesReport {
    ClonesReport {
        root: PathBuf::from("src/"),
        language: "python".to_string(),
        clone_pairs: pairs,
        clone_classes: vec![],
        stats: CloneStats::default(),
        config: CloneConfig::default(),
    }
}

fn make_type2_clone_pair() -> ClonePair {
    ClonePair::new(
        1,
        CloneType::Type2,
        0.92,
        CloneFragment::new(PathBuf::from("src/auth/login.py"), 45, 62, 156),
        CloneFragment::new(PathBuf::from("src/auth/signup.py"), 23, 40, 152),
    )
}

fn make_clone_report_with_special_paths() -> ClonesReport {
    let pair = ClonePair::new(
        1,
        CloneType::Type1,
        1.0,
        CloneFragment::new(PathBuf::from("path with spaces/file.py"), 10, 20, 50),
        CloneFragment::new(PathBuf::from(r"C:\Users\test\other.py"), 5, 15, 50),
    );
    ClonesReport {
        root: PathBuf::from("."),
        language: "python".to_string(),
        clone_pairs: vec![pair],
        clone_classes: vec![],
        stats: CloneStats::default(),
        config: CloneConfig::default(),
    }
}

fn make_similarity_report() -> SimilarityReport {
    SimilarityReport::new(
        SimilarityFragment::new(PathBuf::from("src/a.py"), 100, 20),
        SimilarityFragment::new(PathBuf::from("src/b.py"), 95, 18),
        SimilarityScores::new(0.85, 0.74),
        TokenBreakdown::new(80, 20, 15),
        SimilarityConfig {
            metric: SimilarityMetric::Dice,
            ngram_size: 1,
            language: Some("python".to_string()),
        },
    )
}

// =============================================================================
// Imports Text Formatter Tests
// =============================================================================

use crate::output::format_imports_text;
use tldr_core::types::ImportInfo;

#[test]
fn test_imports_text_groups_by_module() {
    let imports = vec![
        ImportInfo {
            module: ".exceptions".into(),
            names: vec!["Abort".into(), "BadParameter".into()],
            is_from: true,
            alias: None,
        },
        ImportInfo {
            module: ".exceptions".into(),
            names: vec!["UsageError".into()],
            is_from: true,
            alias: None,
        },
        ImportInfo {
            module: ".core".into(),
            names: vec!["Command".into(), "Group".into()],
            is_from: true,
            alias: None,
        },
    ];
    let text = format_imports_text(&imports);
    // .core comes before .exceptions (BTreeMap sorted)
    assert!(text.contains(".core"));
    assert!(text.contains("Command, Group"));
    assert!(text.contains(".exceptions"));
    assert!(text.contains("Abort, BadParameter, UsageError"));
}

#[test]
fn test_imports_text_bare_imports() {
    let imports = vec![
        ImportInfo {
            module: "os".into(),
            names: vec![],
            is_from: false,
            alias: None,
        },
        ImportInfo {
            module: "sys".into(),
            names: vec![],
            is_from: false,
            alias: None,
        },
    ];
    let text = format_imports_text(&imports);
    assert!(text.contains("import"));
    assert!(text.contains("os"));
    assert!(text.contains("sys"));
}

#[test]
fn test_imports_text_aliased() {
    let imports = vec![ImportInfo {
        module: "typing".into(),
        names: vec![],
        is_from: false,
        alias: Some("t".into()),
    }];
    let text = format_imports_text(&imports);
    assert!(text.contains("typing as t"));
}

#[test]
fn test_imports_text_empty() {
    let imports: Vec<ImportInfo> = vec![];
    let text = format_imports_text(&imports);
    assert!(text.contains("No imports found"));
}

#[test]
fn test_imports_text_mixed() {
    let imports = vec![
        ImportInfo {
            module: ".utils".into(),
            names: vec!["echo".into(), "make_str".into()],
            is_from: true,
            alias: None,
        },
        ImportInfo {
            module: "os".into(),
            names: vec![],
            is_from: false,
            alias: None,
        },
        ImportInfo {
            module: "typing".into(),
            names: vec![],
            is_from: false,
            alias: Some("t".into()),
        },
    ];
    let text = format_imports_text(&imports);
    // from-imports come first, then bare imports
    assert!(text.contains(".utils"));
    assert!(text.contains("echo, make_str"));
    assert!(text.contains("os, typing as t"));
}

// =============================================================================
// Importers Text Formatter Tests
// =============================================================================

use crate::output::format_importers_text;
use tldr_core::types::{ImporterInfo, ImportersReport};

#[test]
fn test_importers_text_basic() {
    let report = ImportersReport {
        module: "os".into(),
        importers: vec![
            ImporterInfo {
                file: PathBuf::from("src/main.py"),
                line: 1,
                import_statement: "import os".into(),
            },
            ImporterInfo {
                file: PathBuf::from("src/utils.py"),
                line: 3,
                import_statement: "import os".into(),
            },
        ],
        total: 2,
    };
    let text = format_importers_text(&report);
    // Common prefix "src/" is stripped, showing relative paths
    assert!(
        text.contains("main.py:1"),
        "expected main.py:1, got: {}",
        text
    );
    assert!(
        text.contains("utils.py:3"),
        "expected utils.py:3, got: {}",
        text
    );
    assert!(text.contains("import os"));
}

#[test]
fn test_importers_text_empty() {
    let report = ImportersReport {
        module: "nonexistent".into(),
        importers: vec![],
        total: 0,
    };
    let text = format_importers_text(&report);
    assert!(text.contains("No files import this module"));
}

#[test]
fn test_importers_text_aligned() {
    let report = ImportersReport {
        module: "os".into(),
        importers: vec![
            ImporterInfo {
                file: PathBuf::from("a.py"),
                line: 1,
                import_statement: "import os".into(),
            },
            ImporterInfo {
                file: PathBuf::from("very/long/path/to/file.py"),
                line: 42,
                import_statement: "import os".into(),
            },
        ],
        total: 2,
    };
    let text = format_importers_text(&report);
    // Both lines should be present with aligned columns
    assert!(text.contains("a.py:1"));
    assert!(text.contains("very/long/path/to/file.py:42"));
}

// =============================================================================
// Diagnostics Text Formatter Tests (R1/R2/R3)
// =============================================================================

use crate::output::format_diagnostics_text;
use tldr_core::diagnostics::{
    Diagnostic, DiagnosticsReport, DiagnosticsSummary, Severity, ToolResult,
};

/// Helper: create a Diagnostic
fn make_diagnostic(
    location: (&str, u32, u32),
    severity: Severity,
    code: Option<&str>,
    message: &str,
    source: &str,
    url: Option<&str>,
) -> Diagnostic {
    let (file, line, col) = location;
    Diagnostic {
        file: PathBuf::from(file),
        line,
        column: col,
        end_line: None,
        end_column: None,
        severity,
        message: message.to_string(),
        code: code.map(|c| c.to_string()),
        source: source.to_string(),
        url: url.map(|u| u.to_string()),
    }
}

/// Helper: create a ToolResult
fn make_tool_result(name: &str, success: bool, count: usize) -> ToolResult {
    ToolResult {
        name: name.to_string(),
        version: Some("1.0.0".to_string()),
        success,
        duration_ms: 100,
        diagnostic_count: count,
        error: None,
    }
}

/// Helper: create a DiagnosticsReport with given diagnostics and tools
fn make_diagnostics_report(
    diagnostics: Vec<Diagnostic>,
    tools: Vec<ToolResult>,
    files_analyzed: usize,
) -> DiagnosticsReport {
    let summary = DiagnosticsSummary {
        errors: diagnostics
            .iter()
            .filter(|d| d.severity == Severity::Error)
            .count(),
        warnings: diagnostics
            .iter()
            .filter(|d| d.severity == Severity::Warning)
            .count(),
        info: diagnostics
            .iter()
            .filter(|d| d.severity == Severity::Information)
            .count(),
        hints: diagnostics
            .iter()
            .filter(|d| d.severity == Severity::Hint)
            .count(),
        total: diagnostics.len(),
    };
    DiagnosticsReport {
        diagnostics,
        summary,
        tools_run: tools,
        files_analyzed,
    }
}

// --- R1: Compact one-line summary header ---

/// Test: Header is a single compact line with tool names, file count, and error/warning counts
#[test]
fn test_diagnostics_text_compact_header() {
    let report = make_diagnostics_report(
        vec![
            make_diagnostic(
                ("/src/auth.py", 12, 5),
                Severity::Error,
                Some("E001"),
                "bad type",
                "pyright",
                None,
            ),
            make_diagnostic(
                ("/src/auth.py", 58, 1),
                Severity::Warning,
                Some("E501"),
                "line too long",
                "ruff",
                None,
            ),
        ],
        vec![
            make_tool_result("pyright", true, 1),
            make_tool_result("ruff", true, 1),
        ],
        42,
    );
    let text = format_diagnostics_text(&report, 0);

    // Should have compact summary line: "pyright + ruff | 42 files | 1 error, 1 warning"
    assert!(text.contains("pyright"), "Should list tool names: {}", text);
    assert!(text.contains("ruff"), "Should list tool names: {}", text);
    assert!(
        text.contains("42 files"),
        "Should show file count: {}",
        text
    );
    assert!(
        text.contains("1 error"),
        "Should show error count: {}",
        text
    );
    assert!(
        text.contains("1 warning"),
        "Should show warning count: {}",
        text
    );
}

/// Test: No decorative headers like "Diagnostics Report", "==================", "Summary", "-------"
#[test]
fn test_diagnostics_text_no_decorative_headers() {
    let report = make_diagnostics_report(
        vec![make_diagnostic(
            ("/src/a.py", 1, 1),
            Severity::Error,
            None,
            "err",
            "pyright",
            None,
        )],
        vec![make_tool_result("pyright", true, 1)],
        10,
    );
    let text = format_diagnostics_text(&report, 0);

    assert!(
        !text.contains("Diagnostics Report"),
        "Should not have 'Diagnostics Report' header: {}",
        text
    );
    assert!(
        !text.contains("=================="),
        "Should not have '==================' decoration: {}",
        text
    );
    assert!(
        !text.contains("Summary\n"),
        "Should not have separate 'Summary' header: {}",
        text
    );
    assert!(
        !text.contains("-------"),
        "Should not have '-------' decoration: {}",
        text
    );
}

/// Test: No ANSI escape codes in text output
#[test]
fn test_diagnostics_text_no_ansi_codes() {
    let report = make_diagnostics_report(
        vec![
            make_diagnostic(
                ("/src/a.py", 1, 1),
                Severity::Error,
                Some("E001"),
                "an error",
                "pyright",
                None,
            ),
            make_diagnostic(
                ("/src/a.py", 2, 1),
                Severity::Warning,
                Some("W001"),
                "a warning",
                "ruff",
                None,
            ),
        ],
        vec![
            make_tool_result("pyright", true, 1),
            make_tool_result("ruff", true, 1),
        ],
        5,
    );
    let text = format_diagnostics_text(&report, 0);

    // ANSI escape sequences start with \x1b[ or \033[
    assert!(
        !text.contains('\x1b'),
        "Should not contain ANSI escape codes: {:?}",
        text
    );
}

// --- R1: One-line-per-diagnostic format ---

/// Test: Each diagnostic is on a single line in format: file:line:col: severity[code] message (tool)
#[test]
fn test_diagnostics_text_one_line_per_diagnostic() {
    let report = make_diagnostics_report(
        vec![make_diagnostic(
            ("/project/src/auth.py", 12, 5),
            Severity::Error,
            Some("reportArgumentType"),
            "Type \"str\" is not assignable to type \"int\"",
            "pyright",
            Some("https://example.com/doc"),
        )],
        vec![make_tool_result("pyright", true, 1)],
        1,
    );
    let text = format_diagnostics_text(&report, 0);

    // Should contain one-line format: file:line:col: severity[code] message (tool)
    assert!(
        text.contains("auth.py:12:5: error[reportArgumentType]"),
        "Should have file:line:col: severity[code] format: {}",
        text
    );
    assert!(
        text.contains("(pyright)"),
        "Should have (tool) suffix: {}",
        text
    );
    assert!(
        text.contains("Type \"str\" is not assignable to type \"int\""),
        "Should contain the message: {}",
        text
    );
}

/// Test: No URLs in text output
#[test]
fn test_diagnostics_text_no_urls() {
    let report = make_diagnostics_report(
        vec![make_diagnostic(
            ("/src/a.py", 1, 1),
            Severity::Error,
            Some("E001"),
            "err",
            "pyright",
            Some("https://example.com/doc"),
        )],
        vec![make_tool_result("pyright", true, 1)],
        1,
    );
    let text = format_diagnostics_text(&report, 0);

    assert!(
        !text.contains("https://"),
        "Should not contain URLs: {}",
        text
    );
    assert!(
        !text.contains("http://"),
        "Should not contain URLs: {}",
        text
    );
}

/// Test: Diagnostics without a code omit the brackets
#[test]
fn test_diagnostics_text_no_code_no_brackets() {
    let report = make_diagnostics_report(
        vec![make_diagnostic(
            ("/src/a.py", 1, 1),
            Severity::Warning,
            None,
            "some warning",
            "ruff",
            None,
        )],
        vec![make_tool_result("ruff", true, 1)],
        1,
    );
    let text = format_diagnostics_text(&report, 0);

    // Should have "warning some warning" without empty brackets
    assert!(
        !text.contains("[]"),
        "Should not have empty brackets: {}",
        text
    );
    assert!(
        text.contains("warning some warning"),
        "Should have 'severity message' without code: {}",
        text
    );
}

// --- R2: Strip paths to relative ---

/// Test: Absolute paths are stripped to relative using common prefix
#[test]
fn test_diagnostics_text_paths_relative() {
    let report = make_diagnostics_report(
        vec![
            make_diagnostic(
                ("/home/user/project/src/auth.py", 12, 5),
                Severity::Error,
                Some("E001"),
                "err1",
                "pyright",
                None,
            ),
            make_diagnostic(
                ("/home/user/project/src/models.py", 24, 8),
                Severity::Warning,
                Some("W001"),
                "warn1",
                "ruff",
                None,
            ),
        ],
        vec![
            make_tool_result("pyright", true, 1),
            make_tool_result("ruff", true, 1),
        ],
        10,
    );
    let text = format_diagnostics_text(&report, 0);

    // Common prefix is /home/user/project/src/, so paths should be relative
    assert!(
        text.contains("auth.py:12:5:"),
        "Should show relative path auth.py: {}",
        text
    );
    assert!(
        text.contains("models.py:24:8:"),
        "Should show relative path models.py: {}",
        text
    );
    assert!(
        !text.contains("/home/user/project"),
        "Should not contain absolute prefix: {}",
        text
    );
}

/// Test: Single-file run shows just filename
#[test]
fn test_diagnostics_text_single_file_just_filename() {
    let report = make_diagnostics_report(
        vec![make_diagnostic(
            ("/home/user/project/src/auth.py", 12, 5),
            Severity::Error,
            Some("E001"),
            "err",
            "pyright",
            None,
        )],
        vec![make_tool_result("pyright", true, 1)],
        1,
    );
    let text = format_diagnostics_text(&report, 0);

    // Single file: common_path_prefix gives parent dir, so just "auth.py"
    assert!(
        text.contains("auth.py:12:5:"),
        "Should show just filename: {}",
        text
    );
    assert!(
        !text.contains("/home/user"),
        "Should not contain absolute path: {}",
        text
    );
}

// --- R3: Truncate pyright nested explanations ---

/// Test: Multi-line messages are truncated to first line only
#[test]
fn test_diagnostics_text_truncate_multiline_message() {
    let multiline_msg = "Type \"str\" is not assignable to type \"int\"\n  \"str\" is not assignable to \"int\"\n    Because reasons";
    let report = make_diagnostics_report(
        vec![make_diagnostic(
            ("/src/a.py", 12, 5),
            Severity::Error,
            Some("reportArgumentType"),
            multiline_msg,
            "pyright",
            None,
        )],
        vec![make_tool_result("pyright", true, 1)],
        1,
    );
    let text = format_diagnostics_text(&report, 0);

    assert!(
        text.contains("Type \"str\" is not assignable to type \"int\""),
        "Should contain first line of message: {}",
        text
    );
    assert!(
        !text.contains("Because reasons"),
        "Should NOT contain nested explanation lines: {}",
        text
    );
    assert!(
        !text.contains("is not assignable to \"int\""),
        "Should NOT contain second line of nested explanation: {}",
        text
    );
}

// --- Edge cases ---

/// Test: Empty diagnostics shows "No issues found."
#[test]
fn test_diagnostics_text_empty() {
    let report = make_diagnostics_report(
        vec![],
        vec![
            make_tool_result("pyright", true, 0),
            make_tool_result("ruff", true, 0),
        ],
        42,
    );
    let text = format_diagnostics_text(&report, 0);

    assert!(
        text.contains("No issues found"),
        "Should indicate no issues: {}",
        text
    );
    assert!(
        text.contains("pyright"),
        "Should still list tools: {}",
        text
    );
}

/// Test: Filtered count shown when non-zero
#[test]
fn test_diagnostics_text_filtered_count() {
    let report = make_diagnostics_report(
        vec![make_diagnostic(
            ("/src/a.py", 1, 1),
            Severity::Error,
            Some("E001"),
            "err",
            "pyright",
            None,
        )],
        vec![make_tool_result("pyright", true, 1)],
        5,
    );
    let text = format_diagnostics_text(&report, 3);

    assert!(text.contains("3"), "Should show filtered count: {}", text);
    assert!(
        text.contains("filtered"),
        "Should mention filtering: {}",
        text
    );
}

/// Test: Compact summary uses singular/plural correctly
#[test]
fn test_diagnostics_text_summary_pluralization() {
    // 1 error (singular)
    let report1 = make_diagnostics_report(
        vec![make_diagnostic(
            ("/src/a.py", 1, 1),
            Severity::Error,
            None,
            "err",
            "pyright",
            None,
        )],
        vec![make_tool_result("pyright", true, 1)],
        1,
    );
    let text1 = format_diagnostics_text(&report1, 0);
    assert!(
        text1.contains("1 error"),
        "Should use singular 'error': {}",
        text1
    );
    assert!(
        !text1.contains("1 errors"),
        "Should not use plural for 1: {}",
        text1
    );

    // 2 errors (plural)
    let report2 = make_diagnostics_report(
        vec![
            make_diagnostic(
                ("/src/a.py", 1, 1),
                Severity::Error,
                None,
                "err1",
                "pyright",
                None,
            ),
            make_diagnostic(
                ("/src/a.py", 2, 1),
                Severity::Error,
                None,
                "err2",
                "pyright",
                None,
            ),
        ],
        vec![make_tool_result("pyright", true, 2)],
        1,
    );
    let text2 = format_diagnostics_text(&report2, 0);
    assert!(
        text2.contains("2 errors"),
        "Should use plural 'errors': {}",
        text2
    );
}

// =============================================================================
// Smells Text Formatter Tests (token optimization rewrite)
// =============================================================================

use crate::output::format_smells_text;
use std::collections::HashMap;
use tldr_core::quality::smells::{SmellFinding, SmellType, SmellsReport, SmellsSummary};

/// Helper to create a SmellFinding for testing
fn make_smell(
    smell_type: SmellType,
    name: &str,
    file: &str,
    line: u32,
    severity: u8,
) -> SmellFinding {
    SmellFinding {
        smell_type,
        file: PathBuf::from(file),
        name: name.to_string(),
        line,
        reason: "test reason".to_string(),
        severity,
        suggestion: None,
    }
}

/// Helper to build a SmellsReport from a list of smells
fn make_smells_report(smells: Vec<SmellFinding>) -> SmellsReport {
    let mut by_file: HashMap<PathBuf, Vec<SmellFinding>> = HashMap::new();
    for s in &smells {
        by_file.entry(s.file.clone()).or_default().push(s.clone());
    }
    let mut by_type: HashMap<String, usize> = HashMap::new();
    for s in &smells {
        *by_type.entry(format!("{}", s.smell_type)).or_default() += 1;
    }
    let files_scanned = by_file.len();
    let total_smells = smells.len();
    let avg = if files_scanned > 0 {
        total_smells as f64 / files_scanned as f64
    } else {
        0.0
    };
    SmellsReport {
        smells,
        files_scanned,
        by_file,
        summary: SmellsSummary {
            total_smells,
            by_type,
            avg_smells_per_file: avg,
        },
    }
}

/// Test: Empty smells report shows "No code smells detected."
#[test]
fn test_smells_text_empty_report() {
    let report = make_smells_report(vec![]);
    let text = format_smells_text(&report);
    assert!(
        text.contains("No code smells detected."),
        "Empty report should say no smells: {}",
        text
    );
    assert!(
        text.contains("0 issues"),
        "Should show 0 issues in header: {}",
        text
    );
}

/// Test: Output does NOT contain comfy_table box-drawing characters
#[test]
fn test_smells_text_no_box_drawing() {
    let report = make_smells_report(vec![
        make_smell(SmellType::GodClass, "BigClass", "src/big.py", 10, 3),
        make_smell(SmellType::LongMethod, "long_func", "src/utils.py", 42, 2),
    ]);
    let text = format_smells_text(&report);

    // Box-drawing chars used by comfy_table UTF8_FULL preset
    let box_chars = [
        '\u{2500}', '\u{2502}', '\u{250C}', '\u{2510}', '\u{2514}', '\u{2518}', '\u{251C}',
        '\u{2524}', '\u{252C}', '\u{2534}', '\u{253C}', '\u{2506}', '\u{254C}', '\u{2503}',
    ];
    for ch in &box_chars {
        assert!(
            !text.contains(*ch),
            "Should not contain box-drawing char U+{:04X}: {}",
            *ch as u32,
            text
        );
    }
}

/// Test: Output contains header with column labels
#[test]
fn test_smells_text_has_header_row() {
    let report = make_smells_report(vec![make_smell(
        SmellType::DeadCode,
        "old_func",
        "src/code.py",
        5,
        1,
    )]);
    let text = format_smells_text(&report);
    assert!(
        text.contains("#"),
        "Header should contain # column: {}",
        text
    );
    assert!(
        text.contains("Sev"),
        "Header should contain Sev column: {}",
        text
    );
    assert!(
        text.contains("Type"),
        "Header should contain Type column: {}",
        text
    );
    assert!(
        text.contains("Name"),
        "Header should contain Name column: {}",
        text
    );
    assert!(
        text.contains("File"),
        "Header should contain File column: {}",
        text
    );
}

/// Test: Data rows show correct index, severity, type, name, file:line
#[test]
fn test_smells_text_data_rows() {
    let report = make_smells_report(vec![
        make_smell(SmellType::GodClass, "BigClass", "src/big.py", 10, 3),
        make_smell(SmellType::LongMethod, "long_func", "src/utils.py", 42, 2),
        make_smell(SmellType::DeadCode, "unused", "src/old.py", 99, 1),
    ]);
    let text = format_smells_text(&report);

    // Check each smell appears in the output (strip ANSI for content checking)
    let plain = strip_ansi_codes(&text);
    assert!(
        plain.contains("God Class"),
        "Should contain 'God Class': {}",
        plain
    );
    assert!(
        plain.contains("Long Method"),
        "Should contain 'Long Method': {}",
        plain
    );
    assert!(
        plain.contains("Dead Code"),
        "Should contain 'Dead Code': {}",
        plain
    );
    assert!(
        plain.contains("BigClass"),
        "Should contain name 'BigClass': {}",
        plain
    );
    assert!(
        plain.contains("long_func"),
        "Should contain name 'long_func': {}",
        plain
    );
    assert!(
        plain.contains("unused"),
        "Should contain name 'unused': {}",
        plain
    );
    // File:line format
    assert!(
        plain.contains("big.py:10"),
        "Should show file:line 'big.py:10': {}",
        plain
    );
    assert!(
        plain.contains("utils.py:42"),
        "Should show file:line 'utils.py:42': {}",
        plain
    );
    assert!(
        plain.contains("old.py:99"),
        "Should show file:line 'old.py:99': {}",
        plain
    );
}

/// Test: Long names are truncated to ~28 chars with "..."
#[test]
fn test_smells_text_name_truncation() {
    let long_name = "a_very_long_function_name_that_exceeds_the_limit";
    let report = make_smells_report(vec![make_smell(
        SmellType::LongMethod,
        long_name,
        "src/main.py",
        1,
        2,
    )]);
    let text = format_smells_text(&report);
    let plain = strip_ansi_codes(&text);
    assert!(
        !plain.contains(long_name),
        "Full long name should not appear (should be truncated): {}",
        plain
    );
    assert!(
        plain.contains("..."),
        "Truncated name should end with '...': {}",
        plain
    );
}

/// Test: Path stripping removes common prefix
#[test]
fn test_smells_text_path_stripping() {
    let report = make_smells_report(vec![
        make_smell(
            SmellType::GodClass,
            "A",
            "crates/tldr-core/src/quality/smells.rs",
            1,
            3,
        ),
        make_smell(
            SmellType::LongMethod,
            "B",
            "crates/tldr-core/src/quality/coverage.rs",
            2,
            2,
        ),
    ]);
    let text = format_smells_text(&report);
    let plain = strip_ansi_codes(&text);
    // After path stripping, the common prefix "crates/tldr-core/src/quality/" should be removed
    // Only basenames (or short relative paths) should appear
    assert!(
        !plain.contains("crates/tldr-core/src/quality/smells.rs"),
        "Full path should be stripped: {}",
        plain
    );
    assert!(
        plain.contains("smells.rs"),
        "Basename should still appear: {}",
        plain
    );
    assert!(
        plain.contains("coverage.rs"),
        "Basename should still appear: {}",
        plain
    );
}

/// Test: Summary section includes severity breakdown and per-type counts
#[test]
fn test_smells_text_summary_section() {
    let report = make_smells_report(vec![
        make_smell(SmellType::GodClass, "A", "src/a.py", 1, 3),
        make_smell(SmellType::LongMethod, "B", "src/b.py", 2, 2),
        make_smell(SmellType::DeadCode, "C", "src/c.py", 3, 1),
        make_smell(SmellType::DeadCode, "D", "src/d.py", 4, 1),
    ]);
    let text = format_smells_text(&report);
    let plain = strip_ansi_codes(&text);
    assert!(
        plain.contains("Summary"),
        "Should contain Summary section: {}",
        plain
    );
    assert!(plain.contains("sev-3"), "Should mention sev-3: {}", plain);
    assert!(plain.contains("sev-2"), "Should mention sev-2: {}", plain);
    assert!(plain.contains("sev-1"), "Should mention sev-1: {}", plain);
    assert!(
        plain.contains("4 files"),
        "Should mention file count: {}",
        plain
    );
    // Per-type breakdown
    assert!(
        plain.contains("Dead Code: 2"),
        "Should show per-type count: {}",
        plain
    );
}

/// Helper to strip ANSI escape codes for content assertions
fn strip_ansi_codes(s: &str) -> String {
    let re = regex::Regex::new(r"\x1b\[[0-9;]*m").unwrap();
    re.replace_all(s, "").to_string()
}

// =============================================================================
// Secrets Text Formatter Tests
// =============================================================================

use crate::output::format_secrets_text;
use tldr_core::security::secrets::SecretsSummary;
use tldr_core::security::secrets::Severity as SecretSeverity;
use tldr_core::{SecretFinding, SecretsReport};

fn make_secrets_report(findings: Vec<SecretFinding>, files_scanned: usize) -> SecretsReport {
    let mut by_severity = std::collections::HashMap::new();
    let mut by_pattern = std::collections::HashMap::new();
    for f in &findings {
        *by_severity.entry(format!("{}", f.severity)).or_insert(0) += 1;
        *by_pattern.entry(f.pattern.clone()).or_insert(0) += 1;
    }
    SecretsReport {
        findings,
        files_scanned,
        patterns_checked: 11,
        summary: SecretsSummary {
            total_findings: by_severity.values().sum(),
            by_severity,
            by_pattern,
        },
    }
}

fn make_finding(
    severity: SecretSeverity,
    pattern: &str,
    file: &str,
    line: u32,
    masked: &str,
) -> SecretFinding {
    SecretFinding {
        file: PathBuf::from(file),
        line,
        column: 0,
        pattern: pattern.to_string(),
        severity,
        masked_value: masked.to_string(),
        description: String::new(),
        line_content: None,
    }
}

/// Test: Empty findings shows "No secrets detected"
#[test]
fn test_secrets_text_empty() {
    let report = make_secrets_report(vec![], 50);
    let text = format_secrets_text(&report);
    let plain = strip_ansi_codes(&text);
    assert!(
        plain.contains("0 findings"),
        "Should show 0 findings: {}",
        plain
    );
    assert!(
        plain.contains("50 files scanned"),
        "Should show files scanned: {}",
        plain
    );
    assert!(
        plain.contains("No secrets detected"),
        "Should show no-detection message: {}",
        plain
    );
    assert!(
        !plain.contains("Severity"),
        "Should not show header for empty report: {}",
        plain
    );
}

/// Test: No box-drawing characters in output (no comfy_table)
#[test]
fn test_secrets_text_no_box_drawing() {
    let report = make_secrets_report(
        vec![make_finding(
            SecretSeverity::Critical,
            "AWS Access Key",
            "/src/config.py",
            42,
            "AKIA************MPLE",
        )],
        10,
    );
    let text = format_secrets_text(&report);
    for ch in [
        '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
    ] {
        assert!(
            !text.contains(ch),
            "Should not contain box-drawing char '{}': {}",
            ch,
            text
        );
    }
}

/// Test: Paths are stripped to relative
#[test]
fn test_secrets_text_path_stripping() {
    let report = make_secrets_report(
        vec![
            make_finding(
                SecretSeverity::High,
                "Password",
                "/long/common/prefix/src/config.py",
                10,
                "pass****ord",
            ),
            make_finding(
                SecretSeverity::Medium,
                "API Key",
                "/long/common/prefix/lib/api.py",
                20,
                "key****val",
            ),
        ],
        5,
    );
    let text = format_secrets_text(&report);
    let plain = strip_ansi_codes(&text);
    assert!(
        !plain.contains("/long/common/prefix/"),
        "Should strip common prefix: {}",
        plain
    );
    assert!(
        plain.contains("src/config.py"),
        "Should show relative path: {}",
        plain
    );
    assert!(
        plain.contains("lib/api.py"),
        "Should show relative path: {}",
        plain
    );
}

/// Test: Summary shows severity breakdown
#[test]
fn test_secrets_text_summary() {
    let report = make_secrets_report(
        vec![
            make_finding(
                SecretSeverity::Critical,
                "AWS Key",
                "/src/a.py",
                1,
                "AKIA****",
            ),
            make_finding(SecretSeverity::High, "Password", "/src/b.py", 2, "pass****"),
            make_finding(SecretSeverity::Medium, "JWT", "/src/c.py", 3, "eyJ****"),
        ],
        20,
    );
    let text = format_secrets_text(&report);
    let plain = strip_ansi_codes(&text);
    assert!(
        plain.contains("Summary:"),
        "Should contain Summary: {}",
        plain
    );
    assert!(
        plain.contains("1 critical"),
        "Should show critical count: {}",
        plain
    );
    assert!(
        plain.contains("1 high"),
        "Should show high count: {}",
        plain
    );
    assert!(
        plain.contains("1 medium"),
        "Should show medium count: {}",
        plain
    );
}

/// Test: Long file paths are truncated
#[test]
fn test_secrets_text_long_path_truncation() {
    // Two findings with different long paths so common prefix doesn't eat the whole path
    let report = make_secrets_report(
        vec![
            make_finding(
                SecretSeverity::Low,
                "Bearer",
                "/root/a/very/deeply/nested/directory/structure/with/many/levels/config.yaml",
                1,
                "bear****",
            ),
            make_finding(
                SecretSeverity::Low,
                "Bearer",
                "/root/b/other/path/short.py",
                2,
                "bear****",
            ),
        ],
        2,
    );
    let text = format_secrets_text(&report);
    let plain = strip_ansi_codes(&text);
    // First path after stripping /root/ is still >40 chars, should be truncated with ...
    assert!(
        plain.contains("..."),
        "Long path should be truncated: {}",
        plain
    );
}

// =============================================================================
// ModuleInfo Text Formatter Tests
// =============================================================================

use crate::output::format_module_info_text;
use tldr_core::types::{ClassInfo, FieldInfo, FunctionInfo, IntraFileCallGraph, ModuleInfo};
use tldr_core::Language;

/// Helper: build a minimal ModuleInfo for testing
fn make_module_info() -> ModuleInfo {
    ModuleInfo {
        file_path: PathBuf::from("/src/example.py"),
        language: Language::Python,
        docstring: Some("Example module for testing.".to_string()),
        imports: vec![
            ImportInfo {
                module: "os".to_string(),
                names: vec![],
                is_from: false,
                alias: None,
            },
            ImportInfo {
                module: "typing".to_string(),
                names: vec!["List".to_string(), "Optional".to_string()],
                is_from: true,
                alias: None,
            },
        ],
        functions: vec![
            FunctionInfo {
                name: "process_data".to_string(),
                params: vec!["data: list".to_string(), "config: dict".to_string()],
                return_type: Some("bool".to_string()),
                docstring: Some("Process input data.".to_string()),
                is_method: false,
                is_async: true,
                decorators: vec![],
                line_number: 10,
            },
            FunctionInfo {
                name: "helper".to_string(),
                params: vec![],
                return_type: None,
                docstring: None,
                is_method: false,
                is_async: false,
                decorators: vec![],
                line_number: 25,
            },
        ],
        classes: vec![ClassInfo {
            name: "DataHandler".to_string(),
            bases: vec!["BaseHandler".to_string(), "Serializable".to_string()],
            docstring: Some("Handles data processing.".to_string()),
            methods: vec![
                FunctionInfo {
                    name: "__init__".to_string(),
                    params: vec!["self".to_string(), "config: dict".to_string()],
                    return_type: None,
                    docstring: None,
                    is_method: true,
                    is_async: false,
                    decorators: vec![],
                    line_number: 32,
                },
                FunctionInfo {
                    name: "run".to_string(),
                    params: vec!["self".to_string()],
                    return_type: Some("Result".to_string()),
                    docstring: Some("Run the handler.".to_string()),
                    is_method: true,
                    is_async: true,
                    decorators: vec![],
                    line_number: 40,
                },
            ],
            fields: vec![FieldInfo {
                name: "config".to_string(),
                field_type: Some("dict".to_string()),
                default_value: None,
                is_static: false,
                is_constant: false,
                visibility: None,
                line_number: 33,
            }],
            decorators: vec![],
            line_number: 30,
        }],
        constants: vec![FieldInfo {
            name: "MAX_RETRIES".to_string(),
            field_type: Some("int".to_string()),
            default_value: Some("3".to_string()),
            is_static: false,
            is_constant: true,
            visibility: None,
            line_number: 5,
        }],
        call_graph: IntraFileCallGraph {
            calls: {
                let mut m = HashMap::new();
                m.insert("process_data".to_string(), vec!["helper".to_string()]);
                m.insert(
                    "DataHandler.run".to_string(),
                    vec!["process_data".to_string()],
                );
                m
            },
            called_by: {
                let mut m = HashMap::new();
                m.insert("helper".to_string(), vec!["process_data".to_string()]);
                m.insert(
                    "process_data".to_string(),
                    vec!["DataHandler.run".to_string()],
                );
                m
            },
        },
    }
}

/// Test: header shows file path and language
#[test]
fn test_module_info_text_header() {
    let info = make_module_info();
    let text = format_module_info_text(&info);
    let plain = strip_ansi_codes(&text);
    assert!(
        plain.contains("/src/example.py"),
        "Should contain file path: {}",
        plain
    );
    assert!(
        plain.contains("python"),
        "Should contain language: {}",
        plain
    );
}

/// Test: docstring is shown
#[test]
fn test_module_info_text_docstring() {
    let info = make_module_info();
    let text = format_module_info_text(&info);
    let plain = strip_ansi_codes(&text);
    assert!(
        plain.contains("Example module for testing."),
        "Should contain docstring: {}",
        plain
    );
}

/// Test: imports section present
#[test]
fn test_module_info_text_imports() {
    let info = make_module_info();
    let text = format_module_info_text(&info);
    let plain = strip_ansi_codes(&text);
    assert!(
        plain.contains("Imports"),
        "Should have Imports section: {}",
        plain
    );
    assert!(plain.contains("os"), "Should list os import: {}", plain);
    assert!(
        plain.contains("typing"),
        "Should list typing import: {}",
        plain
    );
}

/// Test: functions section with details
#[test]
fn test_module_info_text_functions() {
    let info = make_module_info();
    let text = format_module_info_text(&info);
    let plain = strip_ansi_codes(&text);
    assert!(
        plain.contains("Functions"),
        "Should have Functions section: {}",
        plain
    );
    assert!(
        plain.contains("process_data"),
        "Should list process_data: {}",
        plain
    );
    assert!(plain.contains("async"), "Should show async flag: {}", plain);
    assert!(plain.contains("bool"), "Should show return type: {}", plain);
    assert!(plain.contains("helper"), "Should list helper: {}", plain);
}

/// Test: classes section with bases and methods
#[test]
fn test_module_info_text_classes() {
    let info = make_module_info();
    let text = format_module_info_text(&info);
    let plain = strip_ansi_codes(&text);
    assert!(
        plain.contains("Classes"),
        "Should have Classes section: {}",
        plain
    );
    assert!(
        plain.contains("DataHandler"),
        "Should list DataHandler: {}",
        plain
    );
    assert!(
        plain.contains("BaseHandler"),
        "Should show base class: {}",
        plain
    );
    assert!(
        plain.contains("__init__"),
        "Should list __init__ method: {}",
        plain
    );
    assert!(plain.contains("run"), "Should list run method: {}", plain);
}

/// Test: constants section
#[test]
fn test_module_info_text_constants() {
    let info = make_module_info();
    let text = format_module_info_text(&info);
    let plain = strip_ansi_codes(&text);
    assert!(
        plain.contains("Constants"),
        "Should have Constants section: {}",
        plain
    );
    assert!(
        plain.contains("MAX_RETRIES"),
        "Should list MAX_RETRIES: {}",
        plain
    );
}

/// Test: call graph section
#[test]
fn test_module_info_text_call_graph() {
    let info = make_module_info();
    let text = format_module_info_text(&info);
    let plain = strip_ansi_codes(&text);
    assert!(
        plain.contains("Call Graph"),
        "Should have Call Graph section: {}",
        plain
    );
    assert!(
        plain.contains("process_data"),
        "Should show process_data in call graph: {}",
        plain
    );
    assert!(
        plain.contains("helper"),
        "Should show helper in call graph: {}",
        plain
    );
}

/// Test: empty module renders without panic
#[test]
fn test_module_info_text_empty() {
    let info = ModuleInfo {
        file_path: PathBuf::from("/src/empty.py"),
        language: Language::Python,
        docstring: None,
        imports: vec![],
        functions: vec![],
        classes: vec![],
        constants: vec![],
        call_graph: IntraFileCallGraph::default(),
    };
    let text = format_module_info_text(&info);
    let plain = strip_ansi_codes(&text);
    assert!(
        plain.contains("/src/empty.py"),
        "Should show file path: {}",
        plain
    );
    assert!(
        !plain.contains("Functions"),
        "Should not have Functions section for empty: {}",
        plain
    );
    assert!(
        !plain.contains("Classes"),
        "Should not have Classes section for empty: {}",
        plain
    );
}

/// Test: no box-drawing characters (no comfy_table)
#[test]
fn test_module_info_text_no_box_drawing() {
    let info = make_module_info();
    let text = format_module_info_text(&info);
    for ch in ['', '', '', '', '', '', '', '', '', '', '', ''] {
        assert!(
            !text.contains(ch),
            "Should not contain box-drawing char '{}': {}",
            ch,
            text
        );
    }
}

/// Test: long docstrings are truncated
#[test]
fn test_module_info_text_long_docstring_truncated() {
    let mut info = make_module_info();
    info.docstring = Some("A".repeat(200));
    let text = format_module_info_text(&info);
    let plain = strip_ansi_codes(&text);
    // Should not contain the full 200-char string
    assert!(
        !plain.contains(&"A".repeat(200)),
        "Should truncate long docstring"
    );
    assert!(
        plain.contains("..."),
        "Truncated docstring should end with ...: {}",
        plain
    );
}

/// Test: output is significantly shorter than JSON
#[test]
fn test_module_info_text_compression() {
    let info = make_module_info();
    let text = format_module_info_text(&info);
    let json = serde_json::to_string_pretty(&info).unwrap();
    // Text format should be at least 50% shorter than JSON
    assert!(
        text.len() < json.len() * 70 / 100,
        "Text ({} bytes) should be <70% of JSON ({} bytes)",
        text.len(),
        json.len()
    );
}