tina4 3.8.68

Tina4 — Unified CLI for Python, PHP, Ruby, and Node.js frameworks
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
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
// Native, language-agnostic code-metrics engine (ADR-0002).
//
// Scans SOURCE directly — per file LOC, cyclomatic complexity (McCabe),
// maintainability index (Radon/Microsoft), efferent coupling, function count —
// for Python / PHP / Ruby / TypeScript+JS / Rust, with NO Tina4 project and NO
// running framework required. Replaces the four per-framework metrics modules
// and, for the first time, covers the frontend (tina4-js .ts) and arbitrary
// non-framework code.
//
// Rust was added last and closed a real blind spot: until then the engine could
// not measure its OWN implementation language, so `tina4 metrics` pointed at
// this repo found zero files and the CLI was the one Tina4 codebase nobody could
// audit.
//
// Pascal / Delphi is deliberately ABSENT. The only published grammar crate
// cannot parse Delphi 10.3+ inline loop variables, which puts 51.5% of the real
// tina4delphi corpus inside a parse-error region; see `pascal_is_not_claimed`.
//
// tina4: ADR-0002 — formulas + thresholds mirror the Python master reference
// tina4-python/tina4_python/dev_admin/metrics.py EXACTLY, so the existing
// `--fail-on` gate thresholds carry over unchanged. The parity is locked by
// `parity_matches_python_master` below, which invokes the real metrics.py.

use std::collections::{BTreeMap, HashMap, HashSet};
use std::fs;
use std::path::{Path, PathBuf};

use serde::Serialize;
use tree_sitter::{Node, Parser};

// ── Languages ──────────────────────────────────────────────────────────────

#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub(crate) enum Lang {
    Python,
    Php,
    Ruby,
    Ts, // TypeScript + TSX + JavaScript (parsed with the tsx grammar)
    Rust,
}

impl Lang {
    pub(crate) fn from_path(path: &Path) -> Option<Lang> {
        match path.extension().and_then(|e| e.to_str())?.to_ascii_lowercase().as_str() {
            "py" | "pyw" => Some(Lang::Python),
            "php" => Some(Lang::Php),
            "rb" => Some(Lang::Ruby),
            "ts" | "tsx" | "js" | "jsx" | "mjs" | "cjs" | "mts" | "cts" => Some(Lang::Ts),
            "rs" => Some(Lang::Rust),
            _ => None,
        }
    }

    fn tree_sitter_language(self) -> tree_sitter::Language {
        match self {
            Lang::Python => tree_sitter_python::LANGUAGE.into(),
            Lang::Php => tree_sitter_php::LANGUAGE_PHP.into(),
            Lang::Ruby => tree_sitter_ruby::LANGUAGE.into(),
            Lang::Ts => tree_sitter_typescript::LANGUAGE_TSX.into(),
            Lang::Rust => tree_sitter_rust::LANGUAGE.into(),
        }
    }
}

// ── Per-file result ──────────────────────────────────────────────────────────

#[derive(Clone, Debug, Serialize)]
pub(crate) struct FunctionInfo {
    pub name: String,
    pub file: String,
    pub line: usize,
    pub complexity: u32,
    /// Code lines spanned by the function body. The dev dashboard's
    /// "most complex functions" table has a LOC column, so omitting this
    /// rendered a literal `undefined` in every row.
    pub loc: usize,
}

#[derive(Clone, Debug, Serialize)]
pub(crate) struct FileMetrics {
    pub path: String,
    pub loc: usize,
    pub complexity: u32,      // file_complexity: sum of every function's CC
    pub avg_complexity: f64,  // rounded to 2 dp
    pub functions: usize,
    pub maintainability: f64, // rounded to 1 dp, clamped [0, 100]
    // The Halstead volume that maintainability was derived FROM. Already
    // computed for the MI; serialising it costs nothing and closes the gap
    // left when coupling_afferent/instability were added without it.
    pub halstead_volume: f64, // rounded to 2 dp
    pub has_tests: bool,

    // ── Coupling ─────────────────────────────────────────────────────────────
    // `dep_count` is EVERY import the file writes, stdlib and third-party
    // included. It is the number the dev dashboard badges on a bubble, so its
    // meaning is deliberately unchanged.
    //
    // The coupling TRIPLE below is INTERNAL-only: it counts edges between files
    // inside the scanned tree, because that is the only thing an architectural
    // coupling number can mean. Efferent = files I depend on. Afferent = files
    // that depend on me. Instability = ce / (ca + ce), the Martin metric: 0.0 is
    // maximally stable (everyone depends on me, I depend on nobody), 1.0 is
    // maximally unstable (I depend on others, nobody depends on me).
    //
    // Mixing the two is what made the previous implementation meaningless: it
    // divided a total-import efferent count (up to 119) by an afferent count
    // that was always 0, so instability was the constant 1.0 everywhere.
    pub dep_count: usize,
    pub coupling_efferent: usize,
    pub coupling_afferent: usize,
    pub instability: f64, // rounded to 3 dp

    /// Fraction of lines that parsed cleanly (1.0 = perfect). Serialised so a
    /// consumer can see WHY a file was refused, and so a file that is close to
    /// the floor is visible before it drops below it.
    pub parse_health: f64, // rounded to 3 dp
}

// ── Rounding + MI formula (mirror metrics.py exactly) ─────────────────────────

fn round_dp(value: f64, dp: i32) -> f64 {
    let m = 10f64.powi(dp);
    (value * m).round() / m
}

/// MI = max(0, min(100, (171 - 5.2*ln(V) - 0.23*CC - 16.2*ln(LOC)) * 100/171)).
/// `V` is Halstead volume, `avg_cc` the mean per-function complexity, `loc` the
/// code-line count. Identical to `_maintainability_index` in metrics.py.
fn maintainability_index(volume: f64, avg_cc: f64, loc: usize) -> f64 {
    if loc == 0 {
        return 100.0;
    }
    let v = volume.max(1.0);
    let mi = 171.0 - 5.2 * v.ln() - 0.23 * avg_cc - 16.2 * (loc as f64).ln();
    (mi * 100.0 / 171.0).clamp(0.0, 100.0)
}

// ── LOC (code lines) ──────────────────────────────────────────────────────────
// Python rule is byte-for-byte what metrics.py uses: a line counts unless it is
// blank or (trimmed) starts with `#`. The others use the analogous line-comment
// prefixes for their language.

fn is_code_line(line: &str, lang: Lang) -> bool {
    let t = line.trim();
    if t.is_empty() {
        return false;
    }
    match lang {
        Lang::Python | Lang::Ruby => !t.starts_with('#'),
        Lang::Php => !(t.starts_with("//") || t.starts_with('#') || t.starts_with("/*") || t.starts_with('*')),
        // Rust shares TypeScript's comment shapes exactly: `//` also covers the
        // doc forms `///` and `//!`, `/*` opens a block (which nests in Rust, but
        // that only matters to the parser, not to a line-prefix count), and a
        // leading `*` is the block-continuation convention.
        //
        // Known, deliberate limitation, identical to the one PHP and TS already
        // carry: a line that STARTS with a dereference (`*counter = 0;`) is read
        // as a comment continuation. Rust hits this more often than TS does, but
        // the point of ADR-0002 is one definition of LOC for every language, so
        // Rust does not get a private rule. It undercounts by a handful of lines
        // in the worst file, never enough to move an MI band.
        Lang::Ts | Lang::Rust => !(t.starts_with("//") || t.starts_with("/*") || t.starts_with('*')),
    }
}

fn count_loc(source: &str, lang: Lang) -> usize {
    source.lines().filter(|l| is_code_line(l, lang)).count()
}

// ── Halstead volume ───────────────────────────────────────────────────────────

#[derive(Default)]
struct Halstead {
    total: usize,
    unique: HashSet<String>,
}
impl Halstead {
    fn add(&mut self, key: impl Into<String>) {
        self.total += 1;
        self.unique.insert(key.into());
    }
}

fn operator_token<'a>(node: Node, src: &'a [u8]) -> Option<&'a str> {
    let mut c = node.walk();
    for child in node.children(&mut c) {
        if !child.is_named() {
            return child.utf8_text(src).ok();
        }
    }
    None
}

fn volume(n1: usize, n2: usize, big_n1: usize, big_n2: usize) -> f64 {
    let vocabulary = n1 + n2;
    let length = big_n1 + big_n2;
    if vocabulary > 0 {
        (length as f64) * (vocabulary as f64).log2()
    } else {
        0.0
    }
}

// ---- Python-precise Halstead (mirrors ast.Name / ast.Constant + operator set) --

fn py_binop_name(tok: &str) -> &'static str {
    match tok {
        "+" => "Add", "-" => "Sub", "*" => "Mult", "/" => "Div", "%" => "Mod",
        "**" => "Pow", "//" => "FloorDiv", "<<" => "LShift", ">>" => "RShift",
        "&" => "BitAnd", "|" => "BitOr", "^" => "BitXor", "@" => "MatMult",
        _ => "BinOp",
    }
}

fn py_unary_name(tok: &str) -> &'static str {
    match tok {
        "-" => "USub", "+" => "UAdd", "~" => "Invert", _ => "UnaryOp",
    }
}

fn py_aug_name(tok: &str) -> &'static str {
    match tok {
        "+=" => "AugAdd", "-=" => "AugSub", "*=" => "AugMult", "/=" => "AugDiv",
        "%=" => "AugMod", "**=" => "AugPow", "//=" => "AugFloorDiv",
        "<<=" => "AugLShift", ">>=" => "AugRShift", "&=" => "AugBitAnd",
        "|=" => "AugBitOr", "^=" => "AugBitXor", "@=" => "AugMatMult",
        _ => "AugAssign",
    }
}

/// Anonymous operator tokens of a `comparison_operator`, folded so that
/// `is not` / `not in` are one operator each — matching ast's `node.ops`.
fn py_compare_ops(node: Node, src: &[u8]) -> Vec<&'static str> {
    let mut toks: Vec<String> = Vec::new();
    let mut c = node.walk();
    for child in node.children(&mut c) {
        if !child.is_named() {
            toks.push(child.utf8_text(src).unwrap_or("").to_string());
        }
    }
    let mut out = Vec::new();
    let mut i = 0;
    while i < toks.len() {
        let t = toks[i].as_str();
        match t {
            "is not" => out.push("IsNot"),
            "not in" => out.push("NotIn"),
            "is" => {
                if toks.get(i + 1).map(String::as_str) == Some("not") {
                    out.push("IsNot");
                    i += 1;
                } else {
                    out.push("Is");
                }
            }
            "not" => {
                if toks.get(i + 1).map(String::as_str) == Some("in") {
                    out.push("NotIn");
                    i += 1;
                } else {
                    out.push("Not");
                }
            }
            "<" => out.push("Lt"),
            "<=" => out.push("LtE"),
            ">" => out.push("Gt"),
            ">=" => out.push("GtE"),
            "==" => out.push("Eq"),
            "!=" | "<>" => out.push("NotEq"),
            "in" => out.push("In"),
            _ => {}
        }
        i += 1;
    }
    out
}

/// Does this `identifier` correspond to an `ast.Name` (a real operand)?  Excludes
/// declaration names, attribute tails, keyword-arg names, params, imports and
/// global/nonlocal — exactly the identifiers ast does NOT surface as `ast.Name`.
fn py_identifier_is_operand(parent_kind: Option<&str>, field: Option<&str>) -> bool {
    match parent_kind {
        Some("function_definition") | Some("class_definition") => field != Some("name"),
        Some("attribute") => field != Some("attribute"),
        Some("keyword_argument") => field != Some("name"),
        Some("parameters") | Some("lambda_parameters") => false,
        Some("default_parameter") => field != Some("name"),
        Some("typed_parameter") => false,
        Some("typed_default_parameter") => field == Some("value"),
        Some("list_splat_pattern") | Some("dictionary_splat_pattern") => false,
        Some("import_statement") | Some("import_from_statement") | Some("dotted_name")
        | Some("aliased_import") => false,
        Some("global_statement") | Some("nonlocal_statement") => false,
        Some("as_pattern_target") => false,
        _ => true,
    }
}

fn py_halstead(
    node: Node,
    parent_kind: Option<&str>,
    field: Option<&str>,
    parent_bool_op: Option<&str>,
    src: &[u8],
    operators: &mut Halstead,
    operands: &mut Halstead,
) {
    let kind = node.kind();
    let mut this_bool_op: Option<&str> = None;
    match kind {
        "binary_operator" => {
            if let Some(t) = operator_token(node, src) {
                operators.add(py_binop_name(t));
            }
        }
        "unary_operator" => {
            if let Some(t) = operator_token(node, src) {
                operators.add(py_unary_name(t));
            }
        }
        "not_operator" => operators.add("Not"),
        "boolean_operator" => {
            let tok = operator_token(node, src).unwrap_or("");
            this_bool_op = Some(if tok == "or" { "or" } else { "and" });
            if parent_bool_op != this_bool_op {
                operators.add(if tok == "or" { "Or" } else { "And" });
            }
        }
        "comparison_operator" => {
            for name in py_compare_ops(node, src) {
                operators.add(name);
            }
        }
        "augmented_assignment" => {
            if let Some(t) = operator_token(node, src) {
                operators.add(py_aug_name(t));
            }
        }
        "identifier" => {
            if py_identifier_is_operand(parent_kind, field) {
                operands.add(node.utf8_text(src).unwrap_or("").to_string());
            }
        }
        "string_content" => {
            let t = node.utf8_text(src).unwrap_or("");
            operands.add(t.chars().take(50).collect::<String>());
        }
        "integer" | "float" => operands.add(node.utf8_text(src).unwrap_or("").to_string()),
        "true" => operands.add("True"),
        "false" => operands.add("False"),
        "none" => operands.add("None"),
        _ => {}
    }

    let mut c = node.walk();
    if c.goto_first_child() {
        loop {
            let child = c.node();
            let child_field = c.field_name();
            py_halstead(child, Some(kind), child_field, this_bool_op, src, operators, operands);
            if !c.goto_next_sibling() {
                break;
            }
        }
    }
}

// ---- Generic Halstead (php / ruby / ts — no cross-language parity target) ------

const GENERIC_OPERATOR_TOKENS: &[&str] = &[
    "+", "-", "*", "/", "%", "**", "//", "++", "--", "==", "===", "!=", "!==", "<>",
    "<", ">", "<=", ">=", "<=>", "&&", "||", "!", "and", "or", "not", "xor", "&", "|",
    "^", "~", "<<", ">>", "=", "+=", "-=", "*=", "/=", "%=", "**=", "//=", "&=", "|=",
    "^=", "<<=", ">>=", ".=", "??", "?", "=~", "->", "=>", "::",
];

/// Rust's leaf kinds carry names no other grammar in the set uses, EXCEPT
/// `primitive_type`, which tree-sitter-php also emits (for `int`/`string` type
/// hints). That collision is why this list is gated on the language instead of
/// being merged into the shared one: folding it in would silently have moved
/// every PHP file's Halstead volume, and therefore its MI, with nothing in the
/// suite to catch it (the parity lock covers Python only). Gating makes
/// "the other four are untouched" true by construction rather than by luck.
fn rust_is_operand_leaf(kind: &str) -> bool {
    matches!(
        kind,
        "integer_literal"
            | "float_literal"
            | "boolean_literal"
            | "char_literal"
            | "primitive_type"
            | "field_identifier"
            | "self"
            | "super"
            | "crate"
    )
}

fn generic_is_operand_leaf(kind: &str, lang: Lang) -> bool {
    if lang == Lang::Rust && rust_is_operand_leaf(kind) {
        return true;
    }
    matches!(
        kind,
        "identifier"
            | "name"
            | "property_identifier"
            | "shorthand_property_identifier"
            | "type_identifier"
            | "constant"
            | "instance_variable"
            | "class_variable"
            | "global_variable"
            | "simple_symbol"
            | "integer"
            | "float"
            | "number"
            | "string_content"
            | "string_fragment"
            | "true"
            | "false"
            | "null"
            | "nil"
            | "none"
    )
}

fn generic_halstead(
    node: Node,
    src: &[u8],
    lang: Lang,
    operators: &mut Halstead,
    operands: &mut Halstead,
) {
    let kind = node.kind();
    if node.is_named() {
        if node.named_child_count() == 0 && generic_is_operand_leaf(kind, lang) {
            operands.add(node.utf8_text(src).unwrap_or("").chars().take(50).collect::<String>());
        }
    } else if GENERIC_OPERATOR_TOKENS.contains(&kind) {
        operators.add(kind);
    }
    let mut c = node.walk();
    if c.goto_first_child() {
        loop {
            generic_halstead(c.node(), src, lang, operators, operands);
            if !c.goto_next_sibling() {
                break;
            }
        }
    }
}

fn file_volume(root: Node, src: &[u8], lang: Lang) -> f64 {
    let mut operators = Halstead::default();
    let mut operands = Halstead::default();
    match lang {
        // Python alone has a cross-language parity target (metrics.py), so it
        // alone gets the ast-exact implementation. Rust joins PHP / Ruby / TS on
        // the generic walk: there is no second Rust implementation to agree with,
        // and the generic operator/operand split is the same McCabe-adjacent
        // definition the other three already feed into MI.
        Lang::Python => py_halstead(root, None, None, None, src, &mut operators, &mut operands),
        _ => generic_halstead(root, src, lang, &mut operators, &mut operands),
    }
    volume(operators.unique.len(), operands.unique.len(), operators.total, operands.total)
}

// ── Parse health ──────────────────────────────────────────────────────────────
//
// tree-sitter ALWAYS returns a tree. When it cannot parse something it wraps the
// region in an ERROR node and carries on, which is the right behaviour for an
// editor and a trap for a metrics engine: every number downstream is still
// computed, still plausible, and quietly wrong, because the decision points and
// operators inside an ERROR region are invisible to the walks that count them.
//
// A file the engine cannot read must therefore be REFUSED and said so, never
// silently reported and never silently skipped. This was found via Delphi (a
// grammar gap put 51.5% of a real corpus inside ERROR regions) but it is not a
// Delphi problem - it applies to all five shipping languages equally, and a
// malformed .py or a .ts using syntax newer than the vendored grammar hits it
// the same way.

/// Fraction of source lines that parsed cleanly: 1.0 is a perfect parse, 0.0 is
/// a file entirely inside error regions.
///
/// Measured by LINE COVERAGE rather than by counting ERROR nodes, because the
/// two diverge badly. One ERROR node can swallow a thousand lines, and a
/// thousand tiny ones can sit on a single line; only the span says how much of
/// the file the engine actually understood.
///
/// Children of an ERROR node are not descended into - they are junk by
/// definition, and the parent's span already covers them.
fn parse_health(root: Node, total_lines: usize) -> f64 {
    if total_lines == 0 {
        return 1.0;
    }
    let mut bad: HashSet<usize> = HashSet::new();
    let mut stack = vec![root];
    while let Some(n) = stack.pop() {
        // MISSING nodes are zero-width insertions the parser invented to
        // recover. They mark a real defect but cover no source, so they are
        // counted at their own line rather than over a span.
        if n.is_error() || n.is_missing() {
            for row in n.start_position().row..=n.end_position().row {
                bad.insert(row);
            }
            if n.is_error() {
                continue;
            }
        }
        let mut c = n.walk();
        for child in n.children(&mut c) {
            stack.push(child);
        }
    }
    let clean = total_lines.saturating_sub(bad.len());
    (clean as f64 / total_lines as f64).clamp(0.0, 1.0)
}

/// Below this fraction of cleanly-parsed lines a file is REFUSED rather than
/// reported.
///
/// Calibrated against every corpus to hand rather than guessed. Measured over
/// 1,875 files in tina4-python, tina4-php, tina4-ruby, tina4-nodejs, tina4-js
/// and this CLI's own src:
///
/// * 1,873 parse at health EXACTLY 1.000.
/// * 2 do not, and both sit far above the floor: 0.993
///   (`tina4-php/tests/OrmQueryBuilderBugsPostgresTest.php`) and 0.994
///   (`tina4-nodejs/types/core/src/devAdmin.d.ts`, a `.d.ts` read with the TSX
///   grammar). Real grammar gaps, and small ones.
/// * NONE is below 0.95.
///
/// So the gap between healthy and broken is not a gradient to tune along, it is
/// a cliff, and 0.95 sits in the empty space under it with the nearest real file
/// four points clear. It tolerates a stray region from one unsupported construct
/// - where the surrounding metrics are still broadly meaningful - and refuses
/// anything structurally misread. It is deliberately NOT 1.0: that would refuse
/// those two real files over a single unrecognised construct, and make the
/// engine useless on any codebase slightly ahead of its vendored grammars.
const MIN_PARSE_HEALTH: f64 = 0.95;

/// Deepest AST nesting the engine's RECURSIVE walks will attempt.
///
/// Five walks recurse once per tree level - `collect_functions`,
/// `count_own_decisions`, `py_halstead`, `generic_halstead` and
/// `collect_fragments` - and none of them had a bound. Measured on macOS 26.5.2
/// arm64 against the 58f7f73 release binary: a 60,000-term left-associative
/// Python expression written one term per line parses fine (tree depth 60,003),
/// then aborts the whole process with `fatal runtime error: stack overflow`,
/// exit 134. Nothing upstream catches it - the file is 10 bytes per line, so
/// `looks_minified` (threshold 200) does not fire.
///
/// This is a PRE-EXISTING defect, not something duplication introduced: the
/// three walks that feed LOC / CC / MI have recursed unguarded since the engine
/// was written, so the crash predates both `cd4dae8` and `58f7f73`.
///
/// 800 is bracketed by two measurements rather than picked:
///
/// * DEEPEST REAL FILE = 79, `src/agent.rs`, over 1,875 files in tina4-python,
///   tina4-php, tina4-ruby, tina4-nodejs, tina4-js and this CLI's own src.
///   Per-repo maxima are 50 / 50 / 35 / 56 / 31 / 79. Ten times the deepest
///   thing anyone has actually written is not a limit real code can hit.
/// * FIRST ABORT = depth 1800, measured in the harshest environment the walks
///   run in - a DEBUG build on a 2 MiB cargo-test worker thread (depth 1700
///   still completes). The release binary on the 8 MiB main thread survives far
///   more. Half the worst-case ceiling leaves the guard itself with margin.
///
/// A file past it is REFUSED like any other the engine cannot read. Refusal
/// names the file; a crash names nothing and loses the entire scan with it.
const MAX_AST_DEPTH: usize = 800;

/// Does any node nest deeper than `limit`?
///
/// Deliberately ITERATIVE. A recursive depth check would overflow the very stack
/// it exists to protect, on exactly the input it exists to catch.
fn depth_exceeds(root: Node, limit: usize) -> bool {
    let mut stack = vec![(root, 0usize)];
    while let Some((node, depth)) = stack.pop() {
        if depth > limit {
            return true;
        }
        let mut c = node.walk();
        for child in node.children(&mut c) {
            stack.push((child, depth + 1));
        }
    }
    false
}

// ── Cyclomatic complexity ─────────────────────────────────────────────────────
// CC = 1 + decision points, counted over the function's ENTIRE subtree (nested
// functions included — matching ast.walk in metrics.py). Python's decision set is
// replicated exactly; the other languages use the same McCabe definition applied
// through their grammar (if/loop/case/catch/ternary/&&/||).

fn is_boolean_binary(node: Node, src: &[u8]) -> bool {
    matches!(operator_token(node, src), Some("&&") | Some("||") | Some("and") | Some("or"))
}

fn is_decision(node: Node, lang: Lang, src: &[u8]) -> u32 {
    // Anonymous nodes are the literal keyword tokens. In tree-sitter's Ruby
    // grammar "if" / "unless" / "while" / "when" / "rescue" name BOTH a construct
    // and its keyword, so matching on kind alone counted every Ruby decision
    // twice - `return 1 if y` scored 3 instead of 2, and every Ruby complexity
    // number came out roughly double.
    if !node.is_named() {
        return 0;
    }
    let k = node.kind();
    match lang {
        Lang::Python => matches!(
            k,
            "if_statement"
                | "elif_clause"
                | "for_statement"
                | "while_statement"
                | "except_clause"
                | "assert_statement"
                | "conditional_expression"
                | "boolean_operator"
                | "for_in_clause"
                | "if_clause"
        ) as u32,
        Lang::Php => {
            if matches!(
                k,
                "if_statement"
                    | "else_if_clause"
                    | "for_statement"
                    | "foreach_statement"
                    | "while_statement"
                    | "do_statement"
                    | "case_statement"
                    | "catch_clause"
                    | "conditional_expression"
            ) {
                1
            } else {
                (k == "binary_expression" && is_boolean_binary(node, src)) as u32
            }
        }
        Lang::Ruby => {
            if matches!(
                k,
                "if" | "elsif" | "unless" | "while" | "until" | "for" | "when" | "rescue"
                    | "conditional" | "if_modifier" | "unless_modifier" | "while_modifier"
                    | "until_modifier"
            ) {
                1
            } else {
                (k == "binary" && is_boolean_binary(node, src)) as u32
            }
        }
        Lang::Ts => {
            if matches!(
                k,
                "if_statement"
                    | "for_statement"
                    | "for_in_statement"
                    | "while_statement"
                    | "do_statement"
                    | "switch_case"
                    | "catch_clause"
                    | "ternary_expression"
            ) {
                1
            } else {
                (k == "binary_expression" && is_boolean_binary(node, src)) as u32
            }
        }
        // Node kinds verified against tree-sitter-rust 0.24.2 by dumping a real
        // parse, not from memory. Three of them are easy to get wrong:
        //
        //  * `if let` / `while let` do NOT have their own node kinds. The grammar
        //    reuses `if_expression` / `while_expression` with a `let_condition`
        //    child, so matching the plain kinds already covers both. Adding an
        //    `if_let_expression` arm would have matched nothing and looked fine.
        //  * `else_clause` is NOT counted. `else` is the fall-through edge, not a
        //    decision; `else if` nests a second `if_expression` inside the clause
        //    and is counted there, so `if/else if/else` scores 2, which is right.
        //  * `try_expression` is the `?` operator. It is a real early-return
        //    branch and is invisible if you only look for keywords - this is the
        //    one the brief flagged and it is worth 1 per `?`.
        //
        // `loop_expression` has no condition but still closes a cycle in the
        // control-flow graph, so it earns its point like any other loop.
        Lang::Rust => {
            if matches!(
                k,
                "if_expression"
                    | "while_expression"
                    | "loop_expression"
                    | "for_expression"
                    | "try_expression"
            ) {
                1
            } else if k == "match_arm" {
                // Every arm is a branch EXCEPT the wildcard `_`, which is the
                // fall-through. This follows the precedent already set for
                // TypeScript, where tree-sitter names `default:` `switch_default`
                // (not `switch_case`) and the engine therefore never counts it.
                // Verified: `match { 1 => .., 2 | 3 => .., _ => .. }` scores 2.
                (!is_rust_wildcard_arm(node, src)) as u32
            } else {
                (k == "binary_expression" && is_boolean_binary(node, src)) as u32
            }
        }
    }
}

/// Is this `match_arm` the catch-all `_ => ...`?
///
/// Compared on the pattern's TEXT rather than its node kind: the kind name for a
/// wildcard has moved between grammar versions, and `_` is unambiguous.
fn is_rust_wildcard_arm(node: Node, src: &[u8]) -> bool {
    node.child_by_field_name("pattern")
        .and_then(|p| p.utf8_text(src).ok())
        .map(|t| t.trim() == "_")
        .unwrap_or(false)
}

/// True for a node that opens a new measurement scope: a nested function (it is
/// reported as a function in its own right) or a class body (its methods are).
/// Mirrors metrics.py, which skips FunctionDef / AsyncFunctionDef / ClassDef.
fn is_scope_boundary(kind: &str, lang: Lang) -> bool {
    if is_function_node(kind, lang) {
        return true;
    }
    match lang {
        Lang::Python => kind == "class_definition",
        Lang::Php => matches!(kind, "class_declaration" | "interface_declaration"
            | "trait_declaration" | "enum_declaration"),
        Lang::Ruby => matches!(kind, "class" | "module"),
        Lang::Ts => matches!(kind, "class_declaration" | "class"),
        Lang::Rust => is_class_node(kind, Lang::Rust),
    }
}

/// Decision points in this function's OWN body.
///
/// Descent stops at a nested function or class, because those are measured
/// separately. Counting them here as well charged a single branch to two
/// different functions, and the over-count compounded with nesting depth: an
/// IIFE wrapper or a registrar defining twenty inner handlers absorbed the whole
/// file's complexity and topped the offenders list, hiding the real hot spots.
///
/// A Python lambda is NOT a scope boundary - lambdas are never listed as
/// functions, so their decisions would vanish. TypeScript arrow functions ARE
/// listed, so they are boundaries.
fn count_own_decisions(node: Node, lang: Lang, src: &[u8]) -> u32 {
    let mut total = is_decision(node, lang, src);
    let mut c = node.walk();
    if c.goto_first_child() {
        loop {
            let child = c.node();
            if !is_scope_boundary(child.kind(), lang) {
                total += count_own_decisions(child, lang, src);
            }
            if !c.goto_next_sibling() {
                break;
            }
        }
    }
    total
}

// ── Functions ─────────────────────────────────────────────────────────────────

fn is_function_node(kind: &str, lang: Lang) -> bool {
    match lang {
        Lang::Python => kind == "function_definition",
        Lang::Php => matches!(kind, "function_definition" | "method_declaration"),
        Lang::Ruby => matches!(kind, "method" | "singleton_method"),
        // tina4-js is arrow-function heavy; count them so their complexity is
        // attributed (Python excludes only trivial one-line lambdas — arrows are
        // full function bodies, so they earn a slot).
        Lang::Ts => matches!(
            kind,
            "function_declaration"
                | "generator_function_declaration"
                | "method_definition"
                | "function_expression"
                | "arrow_function"
        ),
        // `closure_expression` is deliberately NOT here. A Rust closure is the
        // analogue of a Python lambda, not of a TypeScript arrow: the language is
        // saturated with one-line iterator adapters (`.map(|x| x + 1)`), and
        // listing each as a function would bury the real hot spots under hundreds
        // of CC-1 entries and drag every avg_complexity down. Like a lambda, a
        // closure's decisions stay charged to the function that writes it, which
        // is why `is_scope_boundary` does not stop at one either.
        //
        // `function_signature_item` (a bodyless trait method) IS counted, on the
        // precedent PHP already sets: tree-sitter-php reports an interface's
        // bodyless `public function sig();` as a `method_declaration`, and the
        // engine has always counted it.
        Lang::Rust => matches!(kind, "function_item" | "function_signature_item"),
    }
}

fn is_class_node(kind: &str, lang: Lang) -> bool {
    match lang {
        Lang::Python => kind == "class_definition",
        Lang::Php => matches!(kind, "class_declaration" | "trait_declaration" | "interface_declaration"),
        Lang::Ruby => matches!(kind, "class" | "module"),
        Lang::Ts => matches!(kind, "class_declaration" | "class"),
        // `impl_item` is what gives a Rust method its qualifier: `impl Point`
        // and `impl Draw for Point` both carry the implementing type in a field
        // named `type` (NOT `name` - see `node_name`), so `fn draw` inside either
        // is reported as `Point.draw`.
        Lang::Rust => matches!(
            kind,
            "impl_item" | "trait_item" | "struct_item" | "enum_item" | "union_item"
        ),
    }
}

/// Does this node DECLARE a named type a test could reference by name?
///
/// Wider than `is_class_node` on purpose, and used only by test detection.
/// `is_class_node` also drives function naming (`Calculator.add`), so widening it
/// would start naming interface method signatures after their interface.
///
/// TypeScript needed this: an interface-only module has no class at all, so a
/// test that references it by name was reported UNTESTED and raised a false
/// offender. PHP already counted `interface_declaration` here.
fn is_type_decl_node(kind: &str, lang: Lang) -> bool {
    if is_class_node(kind, lang) {
        return true;
    }
    match lang {
        Lang::Ts => matches!(kind, "interface_declaration" | "type_alias_declaration" | "enum_declaration"),
        // A Rust module's public surface is its types, and a test names them
        // directly (`metrics::FileMetrics`). `type_item` covers `type Alias = ..`.
        // Without this every .rs file raised a false "untested" offender, exactly
        // as an interface-only TypeScript module used to.
        Lang::Rust => kind == "type_item",
        _ => false,
    }
}

/// The declared name of a type or function node.
///
/// Falls back to the `type` field because Rust's `impl_item` has no `name`: both
/// `impl Point` and `impl Draw for Point` store the implementing type under
/// `type` (and the trait, when present, under `trait`). Without the fallback
/// every method in the CLI would have been reported bare as `run` or `new`
/// instead of `Report.run`. No other grammar in the set puts a `type` field on a
/// node this is called with, so the fallback cannot affect them.
fn node_name(node: Node, src: &[u8]) -> Option<String> {
    node.child_by_field_name("name")
        .or_else(|| node.child_by_field_name("type"))
        .and_then(|n| n.utf8_text(src).ok())
        .map(|s| s.to_string())
}

/// Outermost enclosing class name (matches metrics.py `_get_parent_class`, which
/// returns the BFS-first — i.e. outermost — containing class).
fn outer_class_name(node: Node, lang: Lang, src: &[u8]) -> Option<String> {
    let mut found: Option<String> = None;
    let mut cur = node.parent();
    while let Some(p) = cur {
        if is_class_node(p.kind(), lang) {
            if let Some(n) = node_name(p, src) {
                found = Some(n);
            }
        }
        cur = p.parent();
    }
    found
}

fn function_display_name(node: Node, lang: Lang, src: &[u8]) -> String {
    let base = node_name(node, src).unwrap_or_else(|| {
        // Arrow / anonymous: derive from `const name = () => …` or `name: () => …`.
        if let Some(parent) = node.parent() {
            if matches!(parent.kind(), "variable_declarator" | "pair" | "assignment_expression") {
                if let Some(n) = parent
                    .child_by_field_name("name")
                    .or_else(|| parent.child_by_field_name("key"))
                    .or_else(|| parent.child_by_field_name("left"))
                {
                    if let Ok(t) = n.utf8_text(src) {
                        return t.to_string();
                    }
                }
            }
        }
        "(anonymous)".to_string()
    });
    match outer_class_name(node, lang, src) {
        Some(cls) => format!("{cls}.{base}"),
        None => base,
    }
}

fn collect_functions<'a>(node: Node<'a>, lang: Lang, src: &[u8], out: &mut Vec<(Node<'a>, u32)>) {
    if is_function_node(node.kind(), lang) {
        out.push((node, count_own_decisions(node, lang, src) + 1));
    }
    let mut c = node.walk();
    if c.goto_first_child() {
        loop {
            collect_functions(c.node(), lang, src, out);
            if !c.goto_next_sibling() {
                break;
            }
        }
    }
}

// ── Coupling (efferent) ───────────────────────────────────────────────────────

fn is_import_node(kind: &str, lang: Lang) -> bool {
    match lang {
        Lang::Python => matches!(kind, "import_statement" | "import_from_statement"),
        Lang::Php => matches!(kind, "namespace_use_declaration" | "require_once_expression"
            | "require_expression" | "include_expression" | "include_once_expression"),
        Lang::Ruby => false, // require is a plain method call — counted below
        Lang::Ts => matches!(kind, "import_statement"),
        Lang::Rust => matches!(kind, "use_declaration" | "mod_item"),
    }
}

/// Strip one layer of quoting from a string-literal node's text.
fn unquote(raw: &str) -> String {
    let t = raw.trim();
    let t = t.strip_prefix("b").unwrap_or(t); // php b"" / rb byte-ish prefixes
    for q in ['"', '\'', '`'] {
        if let Some(inner) = t.strip_prefix(q).and_then(|s| s.strip_suffix(q)) {
            return inner.to_string();
        }
    }
    t.to_string()
}

/// The first string literal anywhere under `node` (import/require argument).
fn first_string_literal(node: Node, src: &[u8]) -> Option<String> {
    let mut stack = vec![node];
    while let Some(n) = stack.pop() {
        if n.kind().contains("string") && n.child_count() == 0 || n.kind() == "string" {
            if let Ok(t) = n.utf8_text(src) {
                let u = unquote(t);
                if !u.is_empty() {
                    return Some(u);
                }
            }
        }
        let mut c = n.walk();
        for child in n.children(&mut c) {
            stack.push(child);
        }
    }
    None
}

/// Every import SPECIFIER the file writes, as written in the source: a Python
/// dotted module, a TS module specifier, a Ruby require argument, a PHP `use`
/// path or include target. Resolution to a file happens later, in
/// `analyze_targets`, because it needs to know every file in the scan.
fn extract_import_specs(root: Node, lang: Lang, src: &[u8]) -> Vec<String> {
    let mut specs: Vec<String> = Vec::new();
    let mut stack = vec![root];
    while let Some(node) = stack.pop() {
        let kind = node.kind();
        match lang {
            Lang::Python if kind == "import_from_statement" => {
                // `from X import y` -> X (may be relative: `.`, `..pkg`)
                if let Some(m) = node.child_by_field_name("module_name") {
                    if let Ok(t) = m.utf8_text(src) {
                        specs.push(t.trim().to_string());
                    }
                } else if let Ok(t) = node.utf8_text(src) {
                    // bare `from . import x` — module_name absent in some grammars
                    if let Some(rest) = t.trim().strip_prefix("from ") {
                        let dots: String =
                            rest.chars().take_while(|c| *c == '.').collect();
                        if !dots.is_empty() {
                            specs.push(dots);
                        }
                    }
                }
            }
            Lang::Python if kind == "import_statement" => {
                // `import a.b, c` -> each dotted_name
                let mut c = node.walk();
                for child in node.children(&mut c) {
                    match child.kind() {
                        "dotted_name" => {
                            if let Ok(t) = child.utf8_text(src) {
                                specs.push(t.trim().to_string());
                            }
                        }
                        "aliased_import" => {
                            if let Some(n) = child.child_by_field_name("name") {
                                if let Ok(t) = n.utf8_text(src) {
                                    specs.push(t.trim().to_string());
                                }
                            }
                        }
                        _ => {}
                    }
                }
            }
            Lang::Ts if kind == "import_statement" => {
                if let Some(s) = node.child_by_field_name("source") {
                    if let Ok(t) = s.utf8_text(src) {
                        specs.push(unquote(t));
                    }
                } else if let Some(s) = first_string_literal(node, src) {
                    specs.push(s);
                }
            }
            // `use Tina4\Frond;` and `use Tina4\{A, B};` nest the path one level
            // down, inside a namespace_use_clause / -group-clause - matching only
            // the outer declaration's DIRECT children found nothing at all, which
            // is why PHP produced zero edges across 138 files.
            Lang::Php
                if matches!(kind, "namespace_use_clause" | "namespace_use_group_clause") =>
            {
                let mut c = node.walk();
                for child in node.children(&mut c) {
                    if child.kind().contains("qualified_name") || child.kind() == "name" {
                        if let Ok(t) = child.utf8_text(src) {
                            let t = t.trim();
                            if !t.is_empty() {
                                specs.push(t.to_string());
                            }
                            break; // the first name is the path; the rest is `as Alias`
                        }
                    }
                }
            }
            Lang::Php
                if matches!(
                    kind,
                    "require_once_expression"
                        | "require_expression"
                        | "include_expression"
                        | "include_once_expression"
                ) =>
            {
                if let Some(s) = first_string_literal(node, src) {
                    specs.push(s);
                }
            }
            // PHP's DOMINANT idiom is not `use` at all - it is an inline
            // fully-qualified reference, `\Tina4\Database::create(...)`, resolved
            // by the autoloader. In the real framework that is 243 references
            // against 72 `use` statements, so counting only `use` understated
            // PHP coupling by roughly 3-4x. A namespaced reference IS a
            // dependency; require a backslash so bare function calls are ignored.
            Lang::Php if kind.contains("qualified_name") => {
                if let Ok(t) = node.utf8_text(src) {
                    let t = t.trim();
                    if t.contains('\\') && t.len() > 1 {
                        specs.push(t.to_string());
                    }
                }
            }
            // `use crate::console::icon_ok;` -> "crate::console::icon_ok". The
            // whole path is kept as written and narrowed to a file in
            // `resolve_import`, which is the only place that knows the file set.
            Lang::Rust if kind == "use_declaration" => {
                if let Some(arg) = node.child_by_field_name("argument") {
                    if let Ok(t) = arg.utf8_text(src) {
                        // `use std::{fs, io}` -> keep the `std::` stem only; a
                        // brace group is always external here (an intra-crate
                        // group would still resolve through its stem).
                        let head = t.split('{').next().unwrap_or(t).trim().trim_end_matches("::");
                        if !head.is_empty() {
                            specs.push(head.to_string());
                        }
                    }
                }
            }
            // `mod agent;` with NO body is a declaration that agent.rs (or
            // agent/mod.rs) is part of this crate - the strongest internal edge
            // Rust has, and the one that wires main.rs to every other file in
            // this CLI. `mod x { .. }` with a body is an inline module and no
            // edge at all, so the bodyless form is what qualifies.
            Lang::Rust if kind == "mod_item" && node.child_by_field_name("body").is_none() => {
                if let Some(n) = node.child_by_field_name("name") {
                    if let Ok(t) = n.utf8_text(src) {
                        specs.push(format!("mod:{}", t.trim()));
                    }
                }
            }
            Lang::Ruby if kind == "call" => {
                if let Some(m) = node
                    .child_by_field_name("method")
                    .and_then(|n| n.utf8_text(src).ok())
                {
                    if matches!(m, "require" | "require_relative" | "load" | "autoload") {
                        if let Some(args) = node.child_by_field_name("arguments") {
                            if let Some(s) = first_string_literal(args, src) {
                                // `autoload :Sym, "path"` -> the path is the literal
                                specs.push(s);
                            }
                        }
                    }
                }
            }
            _ => {}
        }
        let mut c = node.walk();
        for child in node.children(&mut c) {
            stack.push(child);
        }
    }
    // Deduplicate: referencing the same module thirty times is ONE dependency,
    // not thirty. Without this, PHP's inline-FQN idiom would inflate dep_count
    // (the dashboard badge) enormously while the edge set stayed correct.
    specs.sort();
    specs.dedup();
    specs
}

/// Normalise a candidate path: drop `./`, collapse `a/../b`, unify separators.
fn normalise_path(raw: &str) -> String {
    let unified = raw.replace('\\', "/");
    let mut out: Vec<&str> = Vec::new();
    for seg in unified.split('/') {
        match seg {
            "" | "." => {}
            ".." => {
                out.pop();
            }
            s => out.push(s),
        }
    }
    out.join("/")
}

/// The directory part of a relative file path ("a/b/c.py" -> "a/b").
fn parent_dir(rel: &str) -> String {
    match rel.rfind('/') {
        Some(i) => rel[..i].to_string(),
        None => String::new(),
    }
}

/// Resolve one import specifier to a file INSIDE the scanned set, or None when
/// it points outside it (stdlib, a third-party package, a generated asset).
///
/// Only internal edges become coupling, so an unresolvable specifier is not an
/// error - it is simply an external dependency and is excluded by design.
fn resolve_import(
    spec: &str,
    from_rel: &str,
    lang: Lang,
    paths: &HashSet<String>,
    root_pkg: Option<&str>,
) -> Option<String> {
    let dir = parent_dir(from_rel);
    let mut candidates: Vec<String> = Vec::new();
    let mut push = |c: String| candidates.push(normalise_path(&c));

    match lang {
        Lang::Python => {
            // Leading dots = relative import; each dot climbs one package level.
            let dots = spec.chars().take_while(|c| *c == '.').count();
            let tail = spec.trim_start_matches('.').replace('.', "/");
            if dots > 0 {
                let mut base = dir.clone();
                for _ in 1..dots {
                    base = parent_dir(&base);
                }
                let joined = if tail.is_empty() {
                    base.clone()
                } else if base.is_empty() {
                    tail.clone()
                } else {
                    format!("{base}/{tail}")
                };
                push(format!("{joined}.py"));
                push(format!("{joined}/__init__.py"));
            } else {
                push(format!("{tail}.py"));
                push(format!("{tail}/__init__.py"));
                // Absolute intra-package import: `tina4_python.debug` while the
                // scan root IS tina4_python, so the package prefix is implicit.
                if let Some(pkg) = root_pkg {
                    if let Some(rest) = tail.strip_prefix(&format!("{pkg}/")) {
                        push(format!("{rest}.py"));
                        push(format!("{rest}/__init__.py"));
                    }
                }
            }
        }
        Lang::Ts => {
            // Only a relative specifier can be internal; bare ones are packages.
            if !(spec.starts_with('.') || spec.starts_with('/')) {
                return None;
            }
            let joined = if dir.is_empty() {
                spec.to_string()
            } else {
                format!("{dir}/{spec}")
            };
            let base = normalise_path(&joined);
            // TS source may import a ".js" path that is really the ".ts" file.
            let stem = base
                .strip_suffix(".js")
                .or_else(|| base.strip_suffix(".mjs"))
                .unwrap_or(&base)
                .to_string();
            for ext in ["ts", "tsx", "js", "mjs", "jsx"] {
                push(format!("{stem}.{ext}"));
                push(format!("{stem}/index.{ext}"));
            }
            push(base.clone());
        }
        Lang::Ruby => {
            let cleaned = spec.trim_end_matches(".rb");
            // require_relative is dir-relative; require is load-path-relative.
            let joined = if dir.is_empty() {
                cleaned.to_string()
            } else {
                format!("{dir}/{cleaned}")
            };
            push(format!("{joined}.rb"));
            push(format!("{cleaned}.rb"));
            push(format!("lib/{cleaned}.rb"));
            if let Some(pkg) = root_pkg {
                if let Some(rest) = cleaned.strip_prefix(&format!("{pkg}/")) {
                    push(format!("{rest}.rb"));
                }
            }
        }
        // Rust resolution, single-crate layout (what this CLI is).
        //
        // Two specifier shapes reach here. `mod:agent` is a bodyless `mod`
        // declaration and names a sibling file directly. Everything else is a
        // `use` path, where only the leading segment says whether it can be
        // internal at all: `crate` / `self` / `super` can, a bare first segment
        // (`std`, `clap`, `serde`) is an external crate and must resolve to None
        // or every third-party import would be miscounted as internal coupling.
        //
        // A `use` path mixes module segments with the ITEM it imports
        // (`crate::console::icon_ok` = module `console`, item `icon_ok`), and
        // nothing in the path says where the split is. So each prefix is offered
        // longest-first and the file set decides.
        Lang::Rust => {
            if let Some(m) = spec.strip_prefix("mod:") {
                let base = if dir.is_empty() { m.to_string() } else { format!("{dir}/{m}") };
                push(format!("{base}.rs"));
                push(format!("{base}/mod.rs"));
            } else {
                let segs: Vec<&str> = spec.split("::").filter(|s| !s.is_empty()).collect();
                let first = *segs.first()?;
                let (mut base, rest): (String, &[&str]) = match first {
                    // `crate::` is anchored at the crate root, which IS the scan
                    // root, so the base is empty rather than the current dir.
                    "crate" => (String::new(), &segs[1..]),
                    "self" => (dir.clone(), &segs[1..]),
                    "super" => {
                        let mut climbed = dir.clone();
                        let mut i = 0;
                        while segs.get(i) == Some(&"super") {
                            climbed = parent_dir(&climbed);
                            i += 1;
                        }
                        (climbed, &segs[i..])
                    }
                    _ => return None, // an external crate, excluded by design
                };
                if base == "." {
                    base = String::new();
                }
                // Longest prefix first: `console::icon_ok` tries console/icon_ok
                // before console, so a real nested module always wins over a
                // same-named item in its parent.
                for take in (1..=rest.len()).rev() {
                    let joined = rest[..take].join("/");
                    let full =
                        if base.is_empty() { joined.clone() } else { format!("{base}/{joined}") };
                    push(format!("{full}.rs"));
                    push(format!("{full}/mod.rs"));
                }
            }
        }
        Lang::Php => {
            // A `use` path is a namespace; PSR-4 maps it onto directories.
            let as_path = spec.trim_start_matches('\\').replace('\\', "/");
            push(format!("{as_path}.php"));
            if let Some(pkg) = root_pkg {
                if let Some(rest) = as_path.strip_prefix(&format!("{pkg}/")) {
                    push(format!("{rest}.php"));
                }
            }
            // Drop the vendor-style first segment (Tina4\Frond -> Frond.php).
            if let Some((_, rest)) = as_path.split_once('/') {
                push(format!("{rest}.php"));
            }
            // require/include of a literal path, possibly dir-relative.
            if !dir.is_empty() {
                push(format!("{dir}/{as_path}"));
            }
            push(as_path.clone());
        }
    }

    candidates
        .into_iter()
        .find(|c| !c.is_empty() && c != from_rel && paths.contains(c))
}

// ── Analyze one source string ─────────────────────────────────────────────────

/// What one file contributed to the report.
///
/// A single verdict, decided in ONE place, is the whole point. The engine used
/// to answer "did this file parse?" with `Option` (grammar missing) and then
/// silently report numbers for anything that came back `Some` - including a file
/// tree-sitter had wrapped almost entirely in ERROR nodes. Refusal has to be a
/// value the caller must handle, not an absence it can overlook.
pub(crate) enum FileAnalysis {
    Measured {
        metrics: FileMetrics,
        functions: Vec<FunctionInfo>,
        imports: Vec<String>,
        /// Clone fragments with `file` left at 0; only the caller knows the
        /// index. Computed here so the tree is parsed ONCE per file instead of
        /// once for metrics and again for duplication.
        fragments: Vec<CloneFragment>,
    },
    /// Read, and deliberately NOT reported on.
    Refused { reason: String, parse_health: f64 },
}

pub(crate) fn analyze_source(
    lang: Lang,
    source: &str,
    rel_path: &str,
    has_tests: bool,
) -> Option<FileAnalysis> {
    let mut parser = Parser::new();
    parser.set_language(&lang.tree_sitter_language()).ok()?;
    let tree = parser.parse(source, None)?;
    let root = tree.root_node();
    let src = source.as_bytes();

    // ── Refusal gate 1: nesting the recursive walks below cannot survive. ──
    // Checked BEFORE any of them run, because the failure mode is aborting the
    // process, not returning a wrong number.
    if depth_exceeds(root, MAX_AST_DEPTH) {
        return Some(FileAnalysis::Refused {
            reason: format!(
                "AST nests deeper than {MAX_AST_DEPTH} levels - measuring it would overflow the stack"
            ),
            // Nothing is known about its parse health; it was never walked.
            parse_health: 0.0,
        });
    }

    // ── Refusal gate 2: too much of the file sits inside parse-error regions. ──
    // Also before the walks, so a file the engine cannot read costs it nothing.
    let health = round_dp(parse_health(root, source.lines().count()), 3);
    if health < MIN_PARSE_HEALTH {
        return Some(FileAnalysis::Refused {
            reason: format!(
                "only {:.0}% of lines parsed - metrics would be wrong, not just imprecise",
                health * 100.0
            ),
            parse_health: health,
        });
    }

    let loc = count_loc(source, lang);

    let mut fn_nodes: Vec<(Node, u32)> = Vec::new();
    collect_functions(root, lang, src, &mut fn_nodes);

    let mut functions: Vec<FunctionInfo> = Vec::with_capacity(fn_nodes.len());
    let mut file_complexity: u32 = 0;
    for (node, cc) in &fn_nodes {
        file_complexity += *cc;
        let start = node.start_position().row;
        let end = node.end_position().row;
        // Count CODE lines in the function's span, using the same is_code_line
        // rule as the file-level LOC so the two numbers are comparable.
        let fn_loc = source
            .lines()
            .skip(start)
            .take(end.saturating_sub(start) + 1)
            .filter(|l| is_code_line(l, lang))
            .count();
        functions.push(FunctionInfo {
            name: function_display_name(*node, lang, src),
            file: rel_path.to_string(),
            line: start + 1,
            complexity: *cc,
            loc: fn_loc,
        });
    }

    let num_functions = fn_nodes.len();
    let avg_cc = if num_functions > 0 {
        file_complexity as f64 / num_functions as f64
    } else {
        0.0
    };

    let vol = file_volume(root, src, lang);
    let mi = round_dp(maintainability_index(vol, avg_cc, loc), 1);
    let specs = extract_import_specs(root, lang, src);

    let fm = FileMetrics {
        path: rel_path.to_string(),
        loc,
        complexity: file_complexity,
        avg_complexity: round_dp(avg_cc, 2),
        functions: num_functions,
        maintainability: mi,
        halstead_volume: round_dp(vol, 2),
        has_tests,
        // dep_count is every import as written; the coupling triple is filled in
        // by the second pass in analyze_targets, which alone knows every file.
        dep_count: specs.len(),
        coupling_efferent: 0,
        coupling_afferent: 0,
        instability: 0.0,
        parse_health: health,
    };

    let mut fragments: Vec<CloneFragment> = Vec::new();
    collect_fragments(root, 0, &mut fragments);

    Some(FileAnalysis::Measured { metrics: fm, functions, imports: specs, fragments })
}

// ── Duplication (DRY) ─────────────────────────────────────────────────────────
//
// Cross-file, AST-shape based, and therefore language-agnostic by construction:
// it works for all five languages through the same code path, and a sixth would
// need no work here. This is the Baxter et al. approach - hash sub-trees, group
// by hash - rather than the token-window scan PMD's CPD uses, because the engine
// already has a real parse and a shape hash is immune to reformatting.
//
// WHAT IT DETECTS, measured against tree-sitter 0.26.11 rather than asserted.
// Hashing the sequence of node KINDS with all identifier and literal TEXT
// excluded gives TYPE-1 PLUS CONSISTENT RENAMING: two blocks that differ only in
// their variable names, in same-kind literal values, or in whitespace and
// indentation collide, which is the point.
//
// It is NOT full Type-2 in the Roy/Cordy sense, and the doc used to say it was.
// Their Type-2 is verbatim "identical fragments except for variations in
// identifiers, literals, types, layout and COMMENTS", and comments are the one
// it fails: comment nodes are hashed like any other, so adding a comment changes
// the shape. Measured MISS in all five languages - Python `#` and docstring,
// Rust `//` and `///`, PHP `/** */`, TS jsdoc, Ruby `#`. Locked by
// `comments_are_hashed_so_this_is_not_full_type_2` so the claim cannot drift
// back. Fixing it is a real option (skip `is_extra()` nodes) but it changes
// which clones the engine reports, which is a `--fail-on` contract change and
// belongs in its own discussed change - not smuggled in as a doc correction.
//
// Type-3 (gapped) and Type-4 (semantic) clones are NOT detected at all; that
// needs sub-tree differencing, and a report that silently claimed to cover them
// would be worse than one with a stated ceiling.
//
// Anonymous token kinds ARE hashed, so `a + b` and `x - y` do not collide even
// though both are a binary expression over two identifiers. Excluding operators
// was the obvious simplification and it is exactly what makes a shape hash
// notorious for false positives.
//
// tina4: thresholds below are the one genuinely tunable decision here. They are
// deliberately expressed as two INDEPENDENT gates because either alone is known
// to misbehave: a node-count gate alone fires on a long flat list of struct
// fields, and a line gate alone fires on any six sparsely-formatted lines.

/// Minimum AST nodes in a sub-tree before it can be reported as duplicated.
///
/// Sits between jscpd's 50-token default and PMD CPD's 100-token Java default.
/// AST nodes are finer-grained than source tokens (an `if` statement is several
/// nodes), so a node count maps to roughly half as many tokens: 60 nodes is on
/// the order of 30-40 tokens of real code.
const MIN_CLONE_NODES: u32 = 60;

/// Minimum SOURCE LINES a duplicate must span. Matches SonarQube's 10-line
/// default intent while staying tighter, and it is what stops a one-line
/// accessor or a short guard clause from ever being reported however many times
/// it appears.
const MIN_CLONE_LINES: usize = 6;

/// One duplicated region: where it is and how big.
///
/// `file` is an INDEX into the scanned file list rather than a String. With one
/// fragment per qualifying sub-tree, a large project produces hundreds of
/// thousands of these, and a cloned path on each is the difference between a
/// few MB and a few hundred. The hash is a u64 for the same reason - the
/// normalised shape string is never retained, only folded in.
#[derive(Clone, Debug)]
pub(crate) struct CloneFragment {
    hash: u64,
    file: u32,
    start_line: usize,
    end_line: usize,
    nodes: u32,
}

impl CloneFragment {
    fn lines(&self) -> usize {
        self.end_line.saturating_sub(self.start_line) + 1
    }
    /// Does this fragment sit inside `other`, INCLUDING covering the exact same
    /// span?
    ///
    /// The equal-span case is the one that matters and it is easy to get wrong.
    /// A single duplicated block produces a fragment at every wrapper level that
    /// shares its line range - `expression_statement` around `call_expression`
    /// around the arguments - each with a DIFFERENT shape hash, so they form
    /// separate groups that all report the same lines. Excluding equal spans
    /// (the obvious reading of "strictly inside") let all of them through and the
    /// same clone was listed three or four times.
    ///
    /// Self-suppression is not a risk: a group is only ever tested against
    /// groups already KEPT, never against itself.
    fn inside(&self, other: &CloneFragment) -> bool {
        self.file == other.file
            && self.start_line >= other.start_line
            && self.end_line <= other.end_line
    }
}

/// Fold a node's kind and its children's hashes into one structural hash.
///
/// Bottom-up and single-pass: every node's hash is built from the hashes its
/// children already produced, so the whole file costs O(nodes). Serialising each
/// candidate sub-tree to a string and hashing that would be O(nodes * depth) and
/// would allocate a string per candidate.
fn shape_hash_of(kind: &str, child_hashes: &[u64]) -> u64 {
    use std::collections::hash_map::DefaultHasher;
    use std::hash::{Hash, Hasher};
    let mut h = DefaultHasher::new();
    kind.hash(&mut h);
    // Length is folded in so a node with two children cannot collide with a
    // differently-shaped node whose child hashes happen to concatenate the same.
    child_hashes.len().hash(&mut h);
    for c in child_hashes {
        c.hash(&mut h);
    }
    h.finish()
}

/// Walk one file's tree, emitting a fragment for every sub-tree big enough to be
/// worth reporting. Returns this node's (hash, node_count, parsed_cleanly).
///
/// `parsed_cleanly` is false for any sub-tree containing an ERROR or MISSING
/// node, and such a sub-tree NEVER becomes a fragment. A shape hashed out of a
/// misparse describes the parser's recovery, not the author's code, so two files
/// whose only similarity is being broken would otherwise be reported as
/// duplicates of each other. Measured on tree-sitter 0.26.11: seven lines of
/// pure Python garbage fold into a 69-node ERROR node spanning 7 lines, clearing
/// both gates below on its own.
///
/// This is belt AND braces with the parse-health refusal in `analyze_source`,
/// deliberately. The refusal drops whole files below a THRESHOLD; this makes the
/// guarantee unconditional, so it still holds for the error regions inside a
/// file healthy enough to be reported, and it does not quietly weaken if that
/// threshold is ever tuned.
fn collect_fragments(
    node: Node,
    file: u32,
    out: &mut Vec<CloneFragment>,
) -> (u64, u32, bool) {
    let mut child_hashes: Vec<u64> = Vec::new();
    let mut count: u32 = 1;
    let mut parsed_cleanly = !node.is_error() && !node.is_missing();
    let mut c = node.walk();
    if c.goto_first_child() {
        loop {
            let (h, n, clean) = collect_fragments(c.node(), file, out);
            child_hashes.push(h);
            count += n;
            parsed_cleanly &= clean;
            if !c.goto_next_sibling() {
                break;
            }
        }
    }
    let hash = shape_hash_of(node.kind(), &child_hashes);
    let start_line = node.start_position().row + 1;
    let end_line = node.end_position().row + 1;
    // Only NAMED nodes are candidates: an anonymous token is punctuation and can
    // never be a meaningful duplicate on its own, though its kind still shapes
    // the hash of the parent that contains it.
    if parsed_cleanly
        && node.is_named()
        && count >= MIN_CLONE_NODES
        && end_line.saturating_sub(start_line) + 1 >= MIN_CLONE_LINES
    {
        out.push(CloneFragment { hash, file, start_line, end_line, nodes: count });
    }
    (hash, count, parsed_cleanly)
}

/// A set of fragments that share a shape.
#[derive(Clone, Debug, Serialize)]
pub(crate) struct CloneGroup {
    pub files: Vec<String>,
    pub first_file: String,
    pub first_line: usize,
    pub copies: usize,
    pub lines: usize,
    pub cross_file: bool,
}

/// Group fragments by shape, then keep only the MAXIMAL ones.
///
/// The suppression pass is not an optimisation, it is what makes the output
/// readable. If a 200-node block is duplicated then so is every one of its
/// sub-blocks, so a raw grouping reports the same clone at a dozen nesting
/// depths and buries the finding. A group is dropped when every one of its
/// fragments sits inside a fragment of some larger surviving group.
fn group_clones(mut frags: Vec<CloneFragment>, paths: &[String]) -> Vec<CloneGroup> {
    // Largest first, so a parent is always considered before its children.
    frags.sort_by(|a, b| b.nodes.cmp(&a.nodes).then(a.file.cmp(&b.file)).then(a.start_line.cmp(&b.start_line)));

    let mut by_hash: BTreeMap<u64, Vec<CloneFragment>> = BTreeMap::new();
    for f in frags {
        by_hash.entry(f.hash).or_default().push(f);
    }

    // Only shapes that actually repeat.
    let mut candidates: Vec<Vec<CloneFragment>> =
        by_hash.into_values().filter(|v| v.len() >= 2).collect();
    candidates.sort_by(|a, b| b[0].nodes.cmp(&a[0].nodes));

    let mut kept: Vec<Vec<CloneFragment>> = Vec::new();
    for group in candidates {
        let covered = group.iter().all(|f| {
            kept.iter().any(|k| k.iter().any(|big| f.inside(big)))
        });
        if !covered {
            kept.push(group);
        }
    }

    let mut out: Vec<CloneGroup> = kept
        .into_iter()
        .map(|g| {
            let mut files: Vec<String> = g
                .iter()
                .map(|f| paths.get(f.file as usize).cloned().unwrap_or_default())
                .collect();
            files.sort();
            files.dedup();
            let first = &g[0];
            CloneGroup {
                cross_file: files.len() > 1,
                first_file: paths.get(first.file as usize).cloned().unwrap_or_default(),
                first_line: first.start_line,
                copies: g.len(),
                lines: first.lines(),
                files,
            }
        })
        .collect();
    // Biggest, most-repeated first - that is the order someone fixing them wants.
    out.sort_by(|a, b| {
        (b.lines * b.copies)
            .cmp(&(a.lines * a.copies))
            .then(b.copies.cmp(&a.copies))
            .then(a.first_file.cmp(&b.first_file))
    });
    out
}

// ── File discovery ─────────────────────────────────────────────────────────────

const IGNORED_DIRS: &[&str] = &[
    "node_modules", "vendor", ".git", "target", "dist", "build", "__pycache__",
    ".venv", "venv", "coverage", ".next", "out", ".tina4-docs", ".idea", ".pytest_cache",
    ".mypy_cache", ".ruff_cache", "site-packages",
];

/// Build output, not source: a bundled/minified asset that no human maintains.
///
/// This matters because the engine is language-agnostic and therefore sees `.js`
/// that the retired per-language modules never did. A single minified line scores
/// an absurd complexity (26,416 on one real bundle) and would otherwise take over
/// the offenders list and `--fail-on`, burying the code someone can actually fix.
fn is_generated_asset(path: &Path) -> bool {
    let name = path
        .file_name()
        .and_then(|n| n.to_str())
        .unwrap_or("")
        .to_ascii_lowercase();
    name.ends_with(".min.js")
        || name.ends_with(".min.ts")
        || name.ends_with(".min.css")
        || name.ends_with(".bundle.js")
        || name.ends_with("-min.js")
        || name.ends_with(".map")
}

/// Minified-by-content catch-all for a bundle that is not named like one.
/// A hand-written source file does not average hundreds of characters per line.
fn looks_minified(source: &str) -> bool {
    let lines = source.lines().count();
    if lines == 0 {
        return false;
    }
    // A single very long line is the classic minified shape.
    source.len() / lines > 200
}

/// Is this directory a documentation GENERATOR's output tree?
///
/// Detected by a marker file the generator always writes, not by directory name:
/// `docs/`, `html/` and `site/` are all perfectly good places for hand-written
/// source, so banning the name would exclude real code, while the marker only
/// ever appears in generated output.
///
/// This matters because the engine is language-agnostic and therefore sees any
/// `.js` it walks past. Doxygen ships jQuery plus its own `search.js` in
/// `docs/html/`, and scanning tina4delphi produced metrics for seven Doxygen
/// files and nothing else - a report entirely about code nobody wrote. It is
/// the same class of error as measuring a file that did not parse: measuring
/// something that is not the thing.
fn is_generated_docs_dir(dir: &Path) -> bool {
    // Doxygen always emits doxygen.css alongside its HTML/JS. Sphinx always
    // emits .buildinfo at its output root.
    ["doxygen.css", "doxygen.svg", ".buildinfo"]
        .iter()
        .any(|marker| dir.join(marker).is_file())
}

fn walk_dir(dir: &Path, files: &mut Vec<PathBuf>) {
    let Ok(entries) = fs::read_dir(dir) else { return };
    let mut items: Vec<PathBuf> = entries.flatten().map(|e| e.path()).collect();
    items.sort();
    for path in items {
        if path.is_dir() {
            let name = path.file_name().and_then(|n| n.to_str()).unwrap_or("");
            if name.starts_with('.') || IGNORED_DIRS.contains(&name) {
                continue;
            }
            if is_generated_docs_dir(&path) {
                continue;
            }
            walk_dir(&path, files);
        } else if Lang::from_path(&path).is_some() && !is_generated_asset(&path) {
            files.push(path);
        }
    }
}

/// Resolve the scan root(s). With `--path` honour it (file or dir). Otherwise
/// default to cwd auto-detecting `src/`, then `packages/*/src`, then `.`.
fn resolve_targets(path_flag: Option<&str>) -> Result<(Vec<PathBuf>, String), String> {
    let mut files = Vec::new();
    if let Some(p) = path_flag {
        let pb = PathBuf::from(p);
        if pb.is_file() {
            if Lang::from_path(&pb).is_none() {
                return Err(format!("unsupported file type: {p}"));
            }
            return Ok((vec![pb], p.to_string()));
        }
        if pb.is_dir() {
            walk_dir(&pb, &mut files);
            return Ok((files, p.to_string()));
        }
        return Err(format!("Directory not found: {p}"));
    }

    let src = PathBuf::from("src");
    if src.is_dir() {
        walk_dir(&src, &mut files);
        if !files.is_empty() {
            return Ok((files, "src".to_string()));
        }
    }
    let packages = PathBuf::from("packages");
    if packages.is_dir() {
        if let Ok(entries) = fs::read_dir(&packages) {
            let mut pkg_dirs: Vec<PathBuf> = entries.flatten().map(|e| e.path()).collect();
            pkg_dirs.sort();
            for pkg in pkg_dirs {
                let pkg_src = pkg.join("src");
                if pkg_src.is_dir() {
                    walk_dir(&pkg_src, &mut files);
                }
            }
        }
        if !files.is_empty() {
            return Ok((files, "packages/*/src".to_string()));
        }
    }
    walk_dir(Path::new("."), &mut files);
    Ok((files, ".".to_string()))
}

fn rel_display(file: &Path, root: &str) -> String {
    let root_path = Path::new(root);
    let rel = if root_path.is_file() {
        file.file_name().map(PathBuf::from).unwrap_or_else(|| file.to_path_buf())
    } else {
        file.strip_prefix(root_path).unwrap_or(file).to_path_buf()
    };
    rel.to_string_lossy().replace('\\', "/")
}

// ── Test detection (pragmatic; drives only the `info untested` offender) ────────
// A best-effort, language-agnostic reimplementation — NOT a byte-exact port of
// metrics.py's Python-dotted-path heuristic. It never affects `--fail-on`
// (untested is `info`). Signals: a dedicated test file named for the module, or a
// test file that mentions the module stem on an import/require/use line.

struct TestIndex {
    file_names: HashSet<String>,
    contents: Vec<String>,
}

fn build_test_index(root: &str) -> TestIndex {
    let mut file_names = HashSet::new();
    let mut contents = Vec::new();
    let base = {
        let p = Path::new(root);
        if p.is_file() { p.parent().map(PathBuf::from).unwrap_or_else(|| PathBuf::from(".")) } else { p.to_path_buf() }
    };
    // Search cwd and up to 5 ancestors of the scan root for test dirs.
    let mut roots: Vec<PathBuf> = vec![PathBuf::from(".")];
    let mut cur = base.clone();
    for _ in 0..6 {
        roots.push(cur.clone());
        match cur.parent() {
            Some(p) if p != cur => cur = p.to_path_buf(),
            _ => break,
        }
    }
    for r in roots {
        for td in ["tests", "test", "spec"] {
            let dir = r.join(td);
            if dir.is_dir() {
                let mut tf = Vec::new();
                walk_dir(&dir, &mut tf);
                for f in tf {
                    if let Some(name) = f.file_name().and_then(|n| n.to_str()) {
                        file_names.insert(name.to_ascii_lowercase());
                    }
                    if let Ok(c) = fs::read_to_string(&f) {
                        contents.push(c);
                    }
                }
            }
        }
    }
    TestIndex { file_names, contents }
}

/// Type names DECLARED in this module (class / trait / interface / module).
///
/// Used by the class-symbol stage of test detection: a test that imports a class
/// through the package root never mentions the module's file stem, so the stem
/// signal alone reports a well-tested module as untested.
fn declared_type_names(source: &str, lang: Lang) -> Vec<String> {
    let mut parser = Parser::new();
    if parser.set_language(&lang.tree_sitter_language()).is_err() {
        return Vec::new();
    }
    let Some(tree) = parser.parse(source, None) else { return Vec::new() };
    let bytes = source.as_bytes();
    let mut names = Vec::new();
    let mut stack = vec![tree.root_node()];
    while let Some(node) = stack.pop() {
        if is_type_decl_node(node.kind(), lang) {
            if let Some(name) = node_name(node, bytes) {
                names.push(name);
            }
        }
        let mut cursor = node.walk();
        for child in node.children(&mut cursor) {
            stack.push(child);
        }
    }
    names
}

/// True when `needle` appears in `haystack` as a whole identifier.
///
/// A substring match would let `Order` mark `OrderItem` as tested, and every
/// 3-char name would collide constantly. Identifier characters on either side
/// disqualify the hit.
fn mentions_symbol(haystack: &str, needle: &str) -> bool {
    if needle.is_empty() {
        return false;
    }
    let bytes = haystack.as_bytes();
    let n = needle.len();
    let is_ident = |b: u8| b.is_ascii_alphanumeric() || b == b'_';
    let mut from = 0usize;
    while let Some(rel) = haystack[from..].find(needle) {
        let start = from + rel;
        let end = start + n;
        let before_ok = start == 0 || !is_ident(bytes[start - 1]);
        let after_ok = end == bytes.len() || !is_ident(bytes[end]);
        if before_ok && after_ok {
            return true;
        }
        from = start + 1;
    }
    false
}

fn module_has_tests(file: &Path, idx: &TestIndex, declared_types: &[String]) -> bool {
    let stem = file.file_stem().and_then(|s| s.to_str()).unwrap_or("");
    let stem = if matches!(stem, "__init__" | "index" | "mod") {
        file.parent().and_then(|p| p.file_name()).and_then(|s| s.to_str()).unwrap_or(stem)
    } else {
        stem
    };
    if stem.is_empty() {
        return false;
    }
    let stem_l = stem.to_ascii_lowercase();
    // Stage 1: a dedicated test file named for this module.
    //
    // `{stem}test.` covers PHPUnit's PascalCase convention (Metrics.php ->
    // MetricsTest.php), which every other pattern here misses because it uses no
    // separator. Stage 3 cannot rescue it either: whole-identifier matching
    // correctly refuses to find `Widget` inside `WidgetTest`. Comparison is
    // lowercased and anchored with starts_with, so `Base` never matches
    // `DatabaseTest.php`.
    for pat in [
        format!("test_{stem_l}."),
        format!("test_{stem_l}s."),
        format!("{stem_l}_test."),
        format!("{stem_l}_spec."),
        format!("{stem_l}.test."),
        format!("{stem_l}.spec."),
        format!("{stem_l}test."),
    ] {
        if idx.file_names.iter().any(|n| n.starts_with(&pat)) {
            return true;
        }
    }
    // Stage 2: an import/require/use line that mentions the module stem.
    for content in &idx.contents {
        for line in content.lines() {
            let t = line.trim_start();
            let is_import = t.starts_with("import ")
                || t.starts_with("from ")
                || t.starts_with("require")
                || t.starts_with("use ")
                || t.contains("require(");
            if is_import && line.contains(stem) {
                return true;
            }
        }
    }
    // Stage 3: a TYPE DECLARED here is referenced by a test.
    //
    // A test that imports through the package root (`from src import ORM`) never
    // mentions the module's file stem, so stages 1 and 2 both miss it and a
    // well-tested module is reported untested. The class symbol is the only
    // signal in that case, and it is a real one.
    //
    // No minimum name length. A >3-char gate was the bug the Python master
    // fixed: it silently excluded exactly the short framework types that matter
    // most (ORM, Api, Log). Whole-identifier matching is what keeps a short name
    // honest, not a length floor.
    for ty in declared_types {
        for content in &idx.contents {
            if mentions_symbol(content, ty) {
                return true;
            }
        }
    }
    false
}

/// Rust keeps its unit tests INSIDE the file they test, behind `#[cfg(test)]`,
/// rather than in a sibling `tests/` tree. Every stage of `module_has_tests`
/// looks for an external test file, so without this the convention reads as "no
/// test anywhere" and every single .rs file raises a false `untested` offender —
/// which is exactly the noise that makes an offender list get ignored.
///
/// Matched on the source text, not the AST: `#[cfg(test)]` is an attribute whose
/// node shape varies across grammar versions, and the string is unambiguous.
fn rust_has_inline_tests(source: &str) -> bool {
    source.contains("#[cfg(test)]")
}

/// A file the engine REFUSED to measure because too little of it parsed.
///
/// Deliberately a first-class part of the report rather than an omission. A
/// refused file is excluded from `file_metrics` so it cannot skew an average
/// with numbers derived from rubble, and listed here so it is never merely
/// ABSENT - "the engine could not read this" and "this file is fine" must not
/// look the same to anyone reading the output.
#[derive(Serialize, Clone, Debug)]
pub(crate) struct RefusedFile {
    pub path: String,
    /// Fraction of lines that parsed cleanly, rounded to 3 dp.
    pub parse_health: f64,
    pub lines: usize,
    pub reason: String,
}

// ── Offenders ───────────────────────────────────────────────────────────────

#[derive(Serialize, Clone)]
pub(crate) struct Offender {
    pub file: String,
    pub line: usize,
    pub kind: String,
    pub severity: String,
    pub score: f64,
    pub detail: String,
}

fn severity_rank(sev: &str) -> u8 {
    match sev {
        "error" => 2,
        "warn" => 1,
        _ => 0,
    }
}

/// Build the ranked offender list, mirroring `offenders()` in metrics.py: one
/// rule each for function complexity, large_file, too_many_functions,
/// low_maintainability, untested.
fn build_offenders(files: &[FileMetrics], functions: &[FunctionInfo]) -> Vec<Offender> {
    let mut items: Vec<Offender> = Vec::new();

    // Function-level complexity — EVERY function over the threshold becomes an
    // offender, mirroring the Python master fix (fee4385): offenders() reads the
    // FULL, uncapped, complexity-sorted function list, not a display top-15 slice,
    // so a 16th+ over-threshold function is never silently dropped from the
    // offenders list OR from --fail-on. Display truncation is the CLI's `--top N`
    // (applied after the full set drives the exit code); it is not done here.
    let mut by_cc: Vec<&FunctionInfo> = functions.iter().collect();
    by_cc.sort_by(|a, b| b.complexity.cmp(&a.complexity));
    for fn_info in by_cc.iter() {
        let cc = fn_info.complexity;
        if cc > 10 {
            items.push(Offender {
                file: fn_info.file.clone(),
                line: fn_info.line,
                kind: "complexity".to_string(),
                severity: if cc > 20 { "error" } else { "warn" }.to_string(),
                score: cc as f64,
                // ASCII only. Every other `detail` here already is, and this one
                // em dash made the payload non-ASCII: a consumer whose runtime
                // tags subprocess output with a minimal locale (LANG=C) then
                // raised on the first string operation. Also the house style.
                detail: format!("{} - cyclomatic complexity {}", fn_info.name, cc),
            });
        }
    }

    // File-level rules, over files sorted by maintainability ascending.
    let mut by_mi: Vec<&FileMetrics> = files.iter().collect();
    by_mi.sort_by(|a, b| a.maintainability.partial_cmp(&b.maintainability).unwrap_or(std::cmp::Ordering::Equal));
    for fm in by_mi {
        if fm.loc > 500 {
            items.push(Offender {
                file: fm.path.clone(),
                line: 1,
                kind: "large_file".to_string(),
                severity: "warn".to_string(),
                score: fm.loc as f64 / 100.0,
                detail: format!("{} LOC (max 500)", fm.loc),
            });
        }
        if fm.functions > 20 {
            items.push(Offender {
                file: fm.path.clone(),
                line: 1,
                kind: "too_many_functions".to_string(),
                severity: "warn".to_string(),
                score: fm.functions as f64 / 4.0,
                detail: format!("{} functions (max 20)", fm.functions),
            });
        }
        // A low file-level MI is only a real maintainability signal when the
        // code is genuinely complex. The MI formula is size-dominated (the
        // 16.2*ln(SLOC) term), so a big-but-SIMPLE file scores near 0 purely
        // for being long: MEASURED, 400 branchless functions over 1200 lines
        // (avg CC 1.0) scored MI 8.4 and was raised as an ERROR. That says
        // nothing `large_file` (loc > 500) doesn't already say, and a genuinely
        // hot function is already caught by the per-function `complexity`
        // offender. So without a complexity gate this rule just re-flags size,
        // at error severity, on files whose functions are all trivial.
        //
        // Gate on avg per-function complexity: a file whose average function is
        // at least moderately branchy (avg CC >= 5) AND has a low MI is hard to
        // maintain in a way the size/hot-function rules miss. Below that the low
        // MI is a size artifact and `large_file` owns it.
        const LOW_MI_MIN_AVG_CC: f64 = 5.0;
        if fm.maintainability < 40.0 && fm.avg_complexity >= LOW_MI_MIN_AVG_CC {
            items.push(Offender {
                file: fm.path.clone(),
                line: 1,
                kind: "low_maintainability".to_string(),
                severity: if fm.maintainability < 20.0 { "error" } else { "warn" }.to_string(),
                score: 50.0 - fm.maintainability,
                detail: format!(
                    "maintainability index {:.1} (min 40), avg complexity {:.1}",
                    fm.maintainability, fm.avg_complexity
                ),
            });
        }
        if !fm.has_tests {
            items.push(Offender {
                file: fm.path.clone(),
                line: 1,
                kind: "untested".to_string(),
                severity: "info".to_string(),
                score: fm.loc as f64 / 100.0,
                detail: "no referencing test".to_string(),
            });
        }
    }

    sort_offenders(&mut items);
    items
}

/// Sort by (severity rank, score) descending, stable.
fn sort_offenders(items: &mut [Offender]) {
    items.sort_by(|a, b| {
        severity_rank(&b.severity)
            .cmp(&severity_rank(&a.severity))
            .then(b.score.partial_cmp(&a.score).unwrap_or(std::cmp::Ordering::Equal))
    });
}

/// Turn refused files into offenders so they appear in the ranked list, not
/// only in a separate section someone might not read.
///
/// Severity is `warn`, not `error`, and that is a deliberate compatibility
/// call: a file the engine cannot parse is a TOOLING gap (a grammar older than
/// the source it is fed), not a defect in the code being measured, and
/// promoting it to `error` would newly break every existing `--fail-on error`
/// CI run for reasons outside the author's control. `warn` still trips
/// `--fail-on warn`, and the summary line says it loudly either way.
fn refusal_offenders(refused: &[RefusedFile]) -> Vec<Offender> {
    refused
        .iter()
        .map(|r| Offender {
            file: r.path.clone(),
            line: 1,
            kind: "unparsed".to_string(),
            severity: "warn".to_string(),
            // Bigger unreadable files are a bigger hole in the report.
            score: r.lines as f64 / 100.0,
            detail: format!("NOT MEASURED - {}", r.reason),
        })
        .collect()
}

/// Turn clone groups into offenders.
///
/// Kept separate from `build_offenders` because duplication is the only rule
/// that is a property of the SCAN rather than of a single file - it cannot be
/// computed from one file's metrics, and folding it in would have forced every
/// existing caller to supply a whole-project argument it does not have.
///
/// Severity mirrors the existing rules rather than inventing a scale: a
/// duplicate spanning many lines or repeated many times is an `error`, anything
/// else over the reporting floor is a `warn`. `score` is lines * copies, which
/// is the number of duplicated lines the codebase would shed by unifying it -
/// the same "how much does fixing this buy" ranking the other rules use.
fn clone_offenders(groups: &[CloneGroup]) -> Vec<Offender> {
    groups
        .iter()
        .map(|g| {
            let wasted = g.lines * g.copies;
            let where_ = if g.cross_file {
                format!(" across {} files", g.files.len())
            } else {
                String::new()
            };
            Offender {
                file: g.first_file.clone(),
                line: g.first_line,
                kind: "duplication".to_string(),
                // Cross-file duplication is the expensive kind - it is the one
                // that drifts out of sync - so it escalates sooner.
                severity: if wasted >= 60 || (g.cross_file && wasted >= 40) {
                    "error"
                } else {
                    "warn"
                }
                .to_string(),
                score: wasted as f64,
                detail: format!(
                    "{} duplicated lines x {} copies{} - {} lines could be removed",
                    g.lines,
                    g.copies,
                    where_,
                    g.lines * (g.copies - 1)
                ),
            }
        })
        .collect()
}

// ── Summary + JSON payload ────────────────────────────────────────────────────

#[derive(Serialize)]
struct Summary {
    files_analyzed: usize,
    total_functions: usize,
    avg_complexity: f64,
    avg_maintainability: f64,
    scan_mode: String,
    scan_root: String,
    total_offenders: usize,
    /// Maximal duplicated blocks found across the whole scan, and the lines that
    /// would disappear if each group were unified to a single definition. Whole-
    /// scan facts, so they live on the summary rather than on any one file.
    duplicate_blocks: usize,
    duplicate_lines: usize,
    /// Files the parse-health guard REFUSED. Reported so a hole in the coverage
    /// is never invisible - the whole point of the guard.
    files_refused: usize,
}

#[derive(Serialize)]
struct JsonPayload {
    summary: Summary,
    offenders: Vec<Offender>,
    /// Per-file rows. The dev dashboard draws ONE BUBBLE PER FILE from these,
    /// sizing by `loc`, colouring by `avg_complexity` and badging `dep_count`,
    /// so this section is what keeps the metrics visualisation alive when the
    /// per-framework metrics modules are retired (ADR-0002).
    file_metrics: Vec<FileMetrics>,
    /// Top 15 by complexity, for the "most complex functions" table.
    most_complex_functions: Vec<FunctionInfo>,
    /// file -> imported files, both sides relative paths inside the scan.
    dependency_graph: BTreeMap<String, Vec<String>>,
    /// Maximal duplicated AST shapes, biggest payoff first.
    duplication: Vec<CloneGroup>,
    /// Files excluded from every number above because too little of them
    /// parsed. Present even when empty so a consumer can rely on the key.
    unparsed: Vec<RefusedFile>,
}

pub(crate) struct Report {
    files: Vec<FileMetrics>,
    functions: Vec<FunctionInfo>,
    offenders: Vec<Offender>,
    scan_root: String,
    /// file -> the files it imports, both sides RELATIVE PATHS in the scanned
    /// set. Keys and values live in the same namespace on purpose: the previous
    /// implementation keyed this on module names while looking it up by path, so
    /// nothing ever matched and the dependency view drew no edges at all.
    dependency_graph: BTreeMap<String, Vec<String>>,
    clones: Vec<CloneGroup>,
    refused: Vec<RefusedFile>,
}

pub(crate) fn analyze_targets(files: &[PathBuf], scan_root: &str) -> Report {
    let test_index = build_test_index(scan_root);
    let mut file_metrics: Vec<FileMetrics> = Vec::new();
    let mut all_functions: Vec<FunctionInfo> = Vec::new();
    // Pass 1 cannot resolve imports: a specifier only becomes an edge once every
    // file in the scan is known. Park the raw specifiers with their language.
    let mut pending: Vec<(String, Lang, Vec<String>)> = Vec::new();
    // Duplication is the one metric that cannot be computed a file at a time, so
    // fragments accumulate across the whole scan and are grouped in pass 2.
    // `clone_paths` is the index space `CloneFragment.file` points into.
    let mut fragments: Vec<CloneFragment> = Vec::new();
    let mut clone_paths: Vec<String> = Vec::new();
    let mut refused: Vec<RefusedFile> = Vec::new();

    for path in files {
        let Some(lang) = Lang::from_path(path) else { continue };
        let Ok(source) = fs::read_to_string(path) else { continue };
        // A bundle that is not NAMED like one still must not skew the report.
        if looks_minified(&source) {
            continue;
        }
        let rel = rel_display(path, scan_root);
        let declared = declared_type_names(&source, lang);
        let has_tests = (lang == Lang::Rust && rust_has_inline_tests(&source))
            || module_has_tests(path, &test_index, &declared);
        match analyze_source(lang, &source, &rel, has_tests) {
            // The engine could not read the file. It is kept OUT of every
            // aggregate - one file's rubble must not move another file's
            // numbers - and reported by name, because "could not read this" and
            // "this file is fine" must never look the same in the output.
            Some(FileAnalysis::Refused { reason, parse_health }) => {
                refused.push(RefusedFile {
                    path: rel,
                    parse_health,
                    lines: source.lines().count(),
                    reason,
                });
            }
            Some(FileAnalysis::Measured { metrics, functions, imports, fragments: frags }) => {
                // Fragments come back index-less; only this loop knows which
                // slot in `clone_paths` the file took.
                let idx = clone_paths.len() as u32;
                clone_paths.push(metrics.path.clone());
                fragments.extend(frags.into_iter().map(|f| CloneFragment { file: idx, ..f }));
                pending.push((metrics.path.clone(), lang, imports));
                file_metrics.push(metrics);
                all_functions.extend(functions);
            }
            // No grammar, or the parser returned nothing at all.
            None => {}
        }
    }

    // ── Pass 2: resolve specifiers to files, then derive the coupling triple ──
    let paths: HashSet<String> = file_metrics.iter().map(|f| f.path.clone()).collect();
    // The scan root's own basename is the implicit package prefix, so an absolute
    // intra-package import (`tina4_python.debug` while scanning tina4_python/)
    // still resolves.
    let root_pkg = Path::new(scan_root)
        .file_name()
        .and_then(|s| s.to_str())
        .map(|s| s.to_string());

    let mut dependency_graph: BTreeMap<String, Vec<String>> = BTreeMap::new();
    let mut afferent: HashMap<String, usize> = HashMap::new();

    for (from_rel, lang, specs) in &pending {
        let mut targets: Vec<String> = Vec::new();
        for spec in specs {
            if let Some(target) =
                resolve_import(spec, from_rel, *lang, &paths, root_pkg.as_deref())
            {
                if !targets.contains(&target) {
                    targets.push(target);
                }
            }
        }
        for t in &targets {
            *afferent.entry(t.clone()).or_insert(0) += 1;
        }
        targets.sort();
        dependency_graph.insert(from_rel.clone(), targets);
    }

    for fm in file_metrics.iter_mut() {
        let ce = dependency_graph.get(&fm.path).map_or(0, |v| v.len());
        let ca = *afferent.get(&fm.path).unwrap_or(&0);
        fm.coupling_efferent = ce;
        fm.coupling_afferent = ca;
        fm.instability = if ca + ce > 0 {
            round_dp(ce as f64 / (ca + ce) as f64, 3)
        } else {
            // No internal edges either way: the file is isolated, not unstable.
            0.0
        };
    }

    let clones = group_clones(fragments, &clone_paths);
    let mut offenders = build_offenders(&file_metrics, &all_functions);
    offenders.extend(clone_offenders(&clones));
    offenders.extend(refusal_offenders(&refused));
    sort_offenders(&mut offenders);

    Report {
        files: file_metrics,
        functions: all_functions,
        offenders,
        scan_root: scan_root.to_string(),
        dependency_graph,
        clones,
        refused,
    }
}

fn build_summary(report: &Report, total_offenders: usize) -> Summary {
    let total_cc: u32 = report.functions.iter().map(|f| f.complexity).sum();
    let avg_complexity = if report.functions.is_empty() {
        0.0
    } else {
        round_dp(total_cc as f64 / report.functions.len() as f64, 2)
    };
    let total_mi: f64 = report.files.iter().map(|f| f.maintainability).sum();
    let avg_maintainability = if report.files.is_empty() {
        0.0
    } else {
        round_dp(total_mi / report.files.len() as f64, 1)
    };
    Summary {
        files_analyzed: report.files.len(),
        total_functions: report.functions.len(),
        avg_complexity,
        avg_maintainability,
        scan_mode: "project".to_string(),
        scan_root: report.scan_root.clone(),
        total_offenders,
        duplicate_blocks: report.clones.len(),
        duplicate_lines: report.clones.iter().map(|c| c.lines * (c.copies - 1)).sum(),
        files_refused: report.refused.len(),
    }
}

// ── CLI entry point ─────────────────────────────────────────────────────────

/// `--fail-on` gate: exit 1 when an offender at/above the requested severity
/// exists. `warn` trips on warn OR error; `error` trips only on error.
fn compute_exit_code(fail_on: Option<&str>, has_warn: bool, has_error: bool) -> i32 {
    match fail_on {
        Some("warn") if has_warn || has_error => 1,
        Some("error") if has_error => 1,
        _ => 0,
    }
}

/// `tina4 metrics` — native, language-agnostic. Returns the process exit code.
pub fn run(path: Option<String>, top: Option<usize>, json: bool, fail_on: Option<String>) -> i32 {
    if let Some(f) = &fail_on {
        if f != "warn" && f != "error" {
            eprintln!("  invalid --fail-on '{f}' (use warn or error)");
            return 2;
        }
    }
    let top = top.unwrap_or(20);

    let (files, scan_root) = match resolve_targets(path.as_deref()) {
        Ok(v) => v,
        Err(e) => {
            if json {
                println!("{{\n  \"summary\": {{\n    \"error\": {}\n  }},\n  \"offenders\": []\n}}", serde_json::to_string(&e).unwrap_or_default());
            } else {
                println!("  metrics error: {e}");
            }
            return 2;
        }
    };

    let report = analyze_targets(&files, &scan_root);
    let total_offenders = report.offenders.len();
    let summary = build_summary(&report, total_offenders);

    // Exit code from the FULL offender set (before top truncation).
    let has_warn = report.offenders.iter().any(|o| o.severity == "warn");
    let has_error = report.offenders.iter().any(|o| o.severity == "error");
    let exit_code = compute_exit_code(fail_on.as_deref(), has_warn, has_error);

    let shown: Vec<Offender> = report.offenders.iter().take(top).cloned().collect();

    if json {
        // `--top N` truncates the OFFENDER display only. file_metrics stays
        // complete: the bubble chart must plot every file, and the exit code was
        // already computed from the full offender set above.
        let mut by_cc: Vec<FunctionInfo> = report.functions.clone();
        by_cc.sort_by(|a, b| b.complexity.cmp(&a.complexity));
        by_cc.truncate(15);
        let payload = JsonPayload {
            summary,
            offenders: shown,
            file_metrics: report.files.clone(),
            most_complex_functions: by_cc,
            dependency_graph: report.dependency_graph.clone(),
            duplication: report.clones.clone(),
            unparsed: report.refused.clone(),
        };
        println!("{}", serde_json::to_string_pretty(&payload).unwrap_or_else(|_| "{}".to_string()));
        return exit_code;
    }

    print_human(&summary, &shown);
    exit_code
}

fn print_human(summary: &Summary, shown: &[Offender]) {
    use std::io::IsTerminal;
    let use_color = std::io::stdout().is_terminal();
    let paint = |text: &str, code: &str| -> String {
        if use_color {
            format!("\u{1b}[{code}m{text}\u{1b}[0m")
        } else {
            text.to_string()
        }
    };

    println!();
    println!("  Tina4 Metrics \u{2014} {} scan ({})", summary.scan_mode, summary.scan_root);
    println!(
        "  files: {}   functions: {}   avg complexity: {}   avg maintainability: {}",
        summary.files_analyzed, summary.total_functions, summary.avg_complexity, summary.avg_maintainability
    );
    if summary.files_refused > 0 {
        println!(
            "  {}",
            paint(
                // Reason-agnostic on purpose: a file is refused for failing to
                // parse OR for nesting deeper than the walks survive, and the
                // per-file reason is on its `unparsed` offender.
                &format!(
                    "! {} file(s) NOT MEASURED - the engine could not read them (see `unparsed` offenders)",
                    summary.files_refused
                ),
                "33"
            )
        );
    }
    if summary.duplicate_blocks > 0 {
        println!(
            "  duplication: {} repeated blocks   {} lines removable by unifying them",
            summary.duplicate_blocks, summary.duplicate_lines
        );
    }
    let showing = if shown.is_empty() { String::new() } else { format!(" (showing top {})", shown.len()) };
    println!("  offenders: {} total{}", summary.total_offenders, showing);
    println!();

    if shown.is_empty() {
        // "clean" and "nothing was looked at" must not print the same thing.
        // Scanning tina4delphi found 39 Pascal files, claimed none of them, and
        // reported a green tick.
        if summary.files_analyzed == 0 {
            println!(
                "  {}",
                paint("no supported source files found - nothing was measured", "33")
            );
        } else {
            println!("  {}", paint("\u{2713} no offenders \u{2014} clean", "32"));
        }
        println!();
        return;
    }

    let loc_cells: Vec<String> = shown.iter().map(|o| format!("{}:{}", o.file, o.line)).collect();
    let loc_w = loc_cells.iter().map(|s| s.len()).chain(std::iter::once("FILE:LINE".len())).max().unwrap_or(9);
    let kind_w = shown.iter().map(|o| o.kind.len()).chain(std::iter::once("KIND".len())).max().unwrap_or(4);

    let header = format!(
        "  {:>3}  {:<8}  {:<kw$}  {:<lw$}  DETAIL",
        "#", "SEVERITY", "KIND", "FILE:LINE", kw = kind_w, lw = loc_w
    );
    println!("{}", paint(&header, "1"));
    println!("  {}", "-".repeat(header.len().saturating_sub(2)));
    for (i, o) in shown.iter().enumerate() {
        let code = match o.severity.as_str() {
            "error" => "31",
            "warn" => "33",
            _ => "2",
        };
        let sev_cell = paint(&format!("{:<8}", o.severity), code);
        println!(
            "  {:>3}  {}  {:<kw$}  {:<lw$}  {}",
            i + 1, sev_cell, o.kind, loc_cells[i], o.detail, kw = kind_w, lw = loc_w
        );
    }
    println!();
}

// ── Tests ─────────────────────────────────────────────────────────────────────

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

    fn manifest() -> PathBuf {
        PathBuf::from(env!("CARGO_MANIFEST_DIR"))
    }

    fn read_fixture(name: &str) -> String {
        std::fs::read_to_string(manifest().join("tests/fixtures").join(name)).unwrap()
    }

    /// Destructure a MEASURED analysis, or fail loudly.
    ///
    /// A test that quietly accepted a refusal would be asserting nothing at all,
    /// so a refusal panics with the reason attached.
    fn measured(
        lang: Lang,
        src: &str,
        rel: &str,
        has_tests: bool,
    ) -> (FileMetrics, Vec<FunctionInfo>, Vec<String>) {
        match analyze_source(lang, src, rel, has_tests).expect("grammar must load") {
            FileAnalysis::Measured { metrics, functions, imports, .. } => {
                (metrics, functions, imports)
            }
            FileAnalysis::Refused { reason, parse_health } => {
                panic!("{rel}: REFUSED at parse health {parse_health} - {reason}")
            }
        }
    }

    fn analyze_py(src: &str) -> (FileMetrics, Vec<FunctionInfo>) {
        let (fm, fns, _specs) = measured(Lang::Python, src, "t.py", false);
        (fm, fns)
    }

    fn analyze_ts(src: &str) -> (FileMetrics, Vec<FunctionInfo>) {
        let (fm, fns, _specs) = measured(Lang::Ts, src, "t.ts", false);
        (fm, fns)
    }

    fn analyze_php(src: &str) -> (FileMetrics, Vec<FunctionInfo>) {
        let (fm, fns, _specs) = measured(Lang::Php, src, "t.php", false);
        (fm, fns)
    }

    fn analyze_rb(src: &str) -> (FileMetrics, Vec<FunctionInfo>) {
        let (fm, fns, _specs) = measured(Lang::Ruby, src, "t.rb", false);
        (fm, fns)
    }

    fn analyze_rs(src: &str) -> (FileMetrics, Vec<FunctionInfo>) {
        let (fm, fns, _specs) = measured(Lang::Rust, src, "t.rs", false);
        (fm, fns)
    }

    // ---- Rust (ADR-0002 self-measurement) -------------------------------------
    //
    // Until Rust landed, the engine could not measure its OWN implementation
    // language: `tina4 metrics` pointed at this repo found zero files. Every
    // expected number below is hand-derived from the source in the test, so the
    // analyzer is calibrated against McCabe rather than merely self-consistent.

    #[test]
    fn rust_counts_every_decision_kind_once() {
        // Hand count, base 1 plus:
        //   if a > 0                -> if_expression      1
        //   if let Some(v) = b      -> if_expression      1  (NOT a separate kind)
        //   while a > 0             -> while_expression   1
        //   loop { break }          -> loop_expression    1
        //   for i in 0..10          -> for_expression     1
        //   c?                      -> try_expression     1  (the easy one to miss)
        //   a > 0 && a < 10         -> binary &&          1  (`>` and `<` are not)
        // = 8
        let src = "\
fn f(a: i32, b: Option<i32>, c: Result<i32, String>) -> Result<i32, String> {
    if a > 0 { return Ok(1); }
    if let Some(v) = b { let _ = v; }
    while a > 0 { break; }
    loop { break; }
    for i in 0..10 { let _ = i; }
    let _q = c?;
    let _z = a > 0 && a < 10;
    Ok(0)
}
";
        let (fm, fns) = analyze_rs(src);
        assert_eq!(fm.functions, 1);
        assert_eq!(fns[0].complexity, 8, "1 + if + if-let + while + loop + for + ? + &&");
    }

    #[test]
    fn rust_try_operator_is_a_real_branch() {
        // Isolated so a regression in `?` alone cannot hide inside the big count
        // above. Each `?` is an early return, so three of them are three branches.
        let one = "fn f(c: Result<i32, String>) -> Result<i32, String> { Ok(c?) }\n";
        assert_eq!(analyze_rs(one).1[0].complexity, 2, "1 + one ?");
        let three = "fn f(a: Result<i32, String>, b: Result<i32, String>, c: Result<i32, String>) -> Result<i32, String> { Ok(a? + b? + c?) }\n";
        assert_eq!(analyze_rs(three).1[0].complexity, 4, "1 + three ?");
        // The negative half: no `?` means no extra branch.
        let none = "fn f(c: i32) -> i32 { c }\n";
        assert_eq!(analyze_rs(none).1[0].complexity, 1);
    }

    #[test]
    fn rust_else_is_not_a_decision_but_else_if_is() {
        // `if/else` is ONE decision - `else` is the fall-through edge. `else if`
        // nests a second if_expression and so is a second decision. Counting
        // `else_clause` would score these 3 and 4 instead of 2 and 3.
        let if_else = "fn f(a: i32) -> i32 { if a > 0 { 1 } else { 2 } }\n";
        assert_eq!(analyze_rs(if_else).1[0].complexity, 2, "if/else = 1 decision");
        let chain = "fn f(a: i32) -> i32 { if a > 0 { 1 } else if a < 0 { 2 } else { 3 } }\n";
        assert_eq!(analyze_rs(chain).1[0].complexity, 3, "if / else-if / else = 2 decisions");
    }

    #[test]
    fn rust_wildcard_match_arm_is_not_a_decision() {
        // Mirrors the TypeScript precedent: tree-sitter names `default:`
        // `switch_default`, so the engine has never counted it. `_` is the same
        // fall-through, so three arms with a wildcard are two decisions.
        let src = "fn f(a: i32) -> i32 { match a { 1 => 1, 2 => 2, _ => 0 } }\n";
        assert_eq!(analyze_rs(src).1[0].complexity, 3, "1 + two real arms, wildcard excluded");
        // Without a wildcard every arm counts.
        let exhaustive = "fn f(a: bool) -> i32 { match a { true => 1, false => 0 } }\n";
        assert_eq!(analyze_rs(exhaustive).1[0].complexity, 3, "1 + two arms");
    }

    #[test]
    fn rust_closure_decisions_stay_with_the_enclosing_function() {
        // A closure is Rust's lambda, not TypeScript's arrow: `.map(|x| ..)` is
        // everywhere, and listing each as a function would bury the real hot
        // spots under CC-1 noise. So the closure is not reported separately and
        // its branch is charged to the function that writes it.
        let src = "fn f(xs: Vec<i32>) -> Vec<i32> { xs.into_iter().map(|x| if x > 0 { 1 } else { 0 }).collect() }\n";
        let (fm, fns) = analyze_rs(src);
        assert_eq!(fm.functions, 1, "the closure is not a function of its own");
        assert_eq!(fns[0].complexity, 2, "1 + the closure's if");
    }

    #[test]
    fn rust_impl_method_is_named_for_its_type() {
        // impl_item stores the type under a field called `type`, NOT `name`.
        // Without the node_name fallback every method reported bare.
        let inherent = "struct Point;\nimpl Point { fn new() -> Self { Point } }\n";
        let (_fm, fns) = analyze_rs(inherent);
        assert_eq!(fns[0].name, "Point.new");

        let trait_impl = "struct Point;\ntrait Draw { fn draw(&self); }\nimpl Draw for Point { fn draw(&self) {} }\n";
        let (_fm, fns) = analyze_rs(trait_impl);
        assert!(
            fns.iter().any(|f| f.name == "Point.draw"),
            "a trait impl is named for the implementing TYPE, not the trait: {:?}",
            fns.iter().map(|f| &f.name).collect::<Vec<_>>()
        );
    }

    #[test]
    fn rust_loc_excludes_line_doc_and_block_comments() {
        let src = "\
// a line comment
/// a doc comment
//! an inner doc comment
/* a block comment
 * continued
 */
fn f() -> i32 {
    1
}
";
        let (fm, _fns) = analyze_rs(src);
        // Only `fn f() -> i32 {`, `1` and `}` are code.
        assert_eq!(fm.loc, 3, "six comment lines must not count as code");
    }

    /// NEGATIVE CONTROL. A short, clean, low-complexity Rust file must score
    /// HIGH and raise no offender. Without this, a change that simply broke the
    /// Rust analyzer - making everything look complex and unmaintainable - would
    /// pass every assertion above, since those only check that numbers are large.
    #[test]
    fn rust_negative_control_a_clean_file_scores_clean() {
        let src = "\
pub fn add(a: i32, b: i32) -> i32 {
    a + b
}

pub fn sub(a: i32, b: i32) -> i32 {
    a - b
}
";
        // has_tests: true, because `untested` is a fact about the fixture, not
        // about the analyzer, and this control is asserting that the ANALYSIS is
        // clean.
        let (fm, fns, _s) = measured(Lang::Rust, src, "clean.rs", true);
        assert_eq!(fm.functions, 2);
        assert!(fns.iter().all(|f| f.complexity == 1), "straight-line code is CC 1");
        assert!(
            fm.maintainability > 60.0,
            "a trivial module must score HIGH (well clear of the 40 offender line), got {}",
            fm.maintainability
        );
        assert!(
            build_offenders(&[fm], &fns).is_empty(),
            "a clean file must raise no offender at all"
        );
    }

    /// Calibration, not self-consistency: the SAME two trivial functions written
    /// in all five languages must land in the same maintainability band.
    ///
    /// This is what catches a Rust analyzer that is internally coherent but
    /// systematically wrong - if the decision set or the operand-leaf list were
    /// badly off, Rust's MI would drift away from the other four and every
    /// number in the audit would be quietly mis-scaled. Measured at the time of
    /// writing: rust 70.4, ts 72.4, ruby 72.4, php 71.0.
    ///
    /// Python is excluded from the band on purpose: it alone uses the ast-exact
    /// `py_halstead` (volume 12 here against the generic walk's 31) because it
    /// alone has a cross-language parity target, so it legitimately scores
    /// higher and is not evidence of anything about Rust.
    #[test]
    fn rust_maintainability_is_in_band_with_the_other_languages() {
        let cases = [
            (
                "rust",
                Lang::Rust,
                "pub fn add(a: i32, b: i32) -> i32 {\n    a + b\n}\n\npub fn sub(a: i32, b: i32) -> i32 {\n    a - b\n}\n",
            ),
            (
                "ts",
                Lang::Ts,
                "export function add(a: number, b: number): number {\n    return a + b;\n}\n\nexport function sub(a: number, b: number): number {\n    return a - b;\n}\n",
            ),
            (
                "php",
                Lang::Php,
                "<?php\nfunction add($a, $b) {\n    return $a + $b;\n}\n\nfunction sub($a, $b) {\n    return $a - $b;\n}\n",
            ),
            (
                "ruby",
                Lang::Ruby,
                "def add(a, b)\n  a + b\nend\n\ndef sub(a, b)\n  a - b\nend\n",
            ),
        ];
        let mut scores = Vec::new();
        for (name, lang, src) in cases {
            let (fm, _f, _s) = measured(lang, src, "t", false);
            assert_eq!(fm.functions, 2, "{name}: both functions must be found");
            scores.push((name, fm.maintainability));
        }
        let rust = scores.iter().find(|(n, _)| *n == "rust").unwrap().1;
        for (name, mi) in &scores {
            assert!(
                (rust - mi).abs() < 8.0,
                "rust MI {rust} is out of band with {name} MI {mi} for identical logic \
                 - the Rust decision set or operand list has drifted"
            );
        }
    }

    #[test]
    fn rust_mod_and_crate_paths_resolve_to_real_files() {
        let src = "mod agent;\npub mod console;\nuse crate::console::icon_ok;\nuse std::fs;\nuse serde::Serialize;\n";
        let (_fm, _fns, specs) = measured(Lang::Rust, src, "main.rs", false);
        assert!(specs.contains(&"mod:agent".to_string()), "got {specs:?}");
        assert!(specs.contains(&"mod:console".to_string()), "got {specs:?}");
        assert!(specs.contains(&"crate::console::icon_ok".to_string()), "got {specs:?}");

        let paths = pathset(&["main.rs", "agent.rs", "console.rs"]);
        assert_eq!(
            resolve_import("mod:agent", "main.rs", Lang::Rust, &paths, None),
            Some("agent.rs".to_string())
        );
        assert_eq!(
            resolve_import("crate::console::icon_ok", "main.rs", Lang::Rust, &paths, None),
            Some("console.rs".to_string()),
            "the item tail is dropped once the module prefix matches a file"
        );
        // The negative half, and the one that matters most: an EXTERNAL crate
        // must not resolve, or every third-party import becomes fake internal
        // coupling and instability collapses to a constant.
        //
        // The names are chosen so the assertion can actually FAIL. `console` and
        // `session` are both real crates on crates.io AND real files in this
        // repo, so a resolver that ignores the leading segment would happily
        // bind `use console::Term` to the local console.rs and invent an edge
        // that does not exist. Asserting against `std::fs` alone proves nothing
        // here - there is no std.rs to collide with, so it returns None whether
        // the rule is right or wrong.
        assert_eq!(
            resolve_import("console::Term", "main.rs", Lang::Rust, &paths, None),
            None,
            "a BARE first segment is an external crate even when a local file shares its name"
        );
        assert_eq!(
            resolve_import("crate::console::Term", "main.rs", Lang::Rust, &paths, None),
            Some("console.rs".to_string()),
            "the same path via `crate::` IS the local file - this is the contrast that makes the rule meaningful"
        );
        assert_eq!(resolve_import("std::fs", "main.rs", Lang::Rust, &paths, None), None);
        assert_eq!(resolve_import("serde::Serialize", "main.rs", Lang::Rust, &paths, None), None);
    }

    /// Adding a language must not move the FOUR that already shipped.
    ///
    /// The live hazard was concrete: Rust needs `primitive_type` treated as a
    /// Halstead operand, and tree-sitter-php emits `primitive_type` too (for
    /// `int` / `string` type hints). Folding the Rust leaf kinds into the shared
    /// list would have shifted every PHP file's volume, and therefore its MI,
    /// with nothing in the suite to notice - the parity lock covers Python only.
    ///
    /// So the Rust kinds are gated on the language, and these are the exact
    /// volumes the generic walk produced BEFORE Rust existed. If a future change
    /// widens the shared list, this test goes red instead of the audit going
    /// quietly wrong.
    #[test]
    fn adding_rust_does_not_perturb_the_other_languages() {
        for (name, lang, src) in [
            (
                "ts",
                Lang::Ts,
                "export function add(a: number, b: number): number {\n    return a + b;\n}\n\nexport function sub(a: number, b: number): number {\n    return a - b;\n}\n",
            ),
            (
                "php",
                Lang::Php,
                "<?php\nfunction add($a, $b) {\n    return $a + $b;\n}\n\nfunction sub($a, $b) {\n    return $a - $b;\n}\n",
            ),
            (
                "ruby",
                Lang::Ruby,
                "def add(a, b)\n  a + b\nend\n\ndef sub(a, b)\n  a - b\nend\n",
            ),
        ] {
            let (fm, _f, _s) = measured(lang, src, "t", false);
            assert_eq!(
                fm.halstead_volume, 31.02,
                "{name}: Halstead volume moved - the Rust operand kinds have leaked \
                 into the shared list and every {name} MI in the audit is now wrong"
            );
        }
        // A PHP typed signature is the specific collision: `int` parses as
        // `primitive_type`, which Rust counts and PHP must NOT.
        let typed = "<?php\nfunction add(int $a, int $b): int {\n    return $a + $b;\n}\n";
        let (fm, _f, _s) = measured(Lang::Php, typed, "t.php", false);
        assert_eq!(
            fm.halstead_volume, 12.0,
            "PHP `primitive_type` must stay uncounted"
        );
    }

    /// Pascal / Delphi is deliberately NOT wired up, and that is a measured
    /// decision rather than an omission.
    ///
    /// The only published crate, `tree-sitter-pascal` 0.10.2 (Isopod), compiles
    /// against tree-sitter 0.26 and handles classic Object Pascal well, but it
    /// cannot parse Delphi 10.3+ inline loop variables (`for var X in Y do`,
    /// upstream issue #15, still open). Measured against the real tina4delphi
    /// corpus: 20,977 of 40,719 lines - 51.5% - fall inside an ERROR region,
    /// including 100% of Tina4Frond.pas and 92.8% of Tina4HTMLRender.pas.
    ///
    /// Claiming `.pas` would mean emitting an MI derived from a tree where half
    /// the decision points are invisible. A missing number is recoverable; a
    /// confident wrong one is not. So `.pas` stays unrecognised until a grammar
    /// that parses the corpus is available.
    #[test]
    fn pascal_is_not_claimed() {
        for ext in ["pas", "dpr", "dpk", "inc", "PAS"] {
            assert_eq!(
                Lang::from_path(Path::new(&format!("a.{ext}"))),
                None,
                "{ext} must not be claimed while no grammar can parse the corpus"
            );
        }
    }

    #[test]
    fn rust_inline_cfg_test_module_counts_as_tested() {
        // Rust puts unit tests inside the file. Every stage of module_has_tests
        // looks for an external test FILE, so without this every .rs file in the
        // repo raised a false `untested` offender.
        assert!(rust_has_inline_tests("fn f() {}\n#[cfg(test)]\nmod tests { #[test] fn t() {} }\n"));
        // The negative half: a file with no test module is genuinely untested.
        assert!(!rust_has_inline_tests("fn f() -> i32 { 1 }\n"));
        assert!(
            !rust_has_inline_tests("// mentions cfg and test but declares neither\n"),
            "the marker is the attribute, not the words"
        );
    }

    // ---- Language-agnostic building blocks -----------------------------------

    #[test]
    fn detects_language_from_extension() {
        for ext in ["py", "pyw"] {
            assert_eq!(Lang::from_path(Path::new(&format!("a.{ext}"))), Some(Lang::Python));
        }
        assert_eq!(Lang::from_path(Path::new("a.php")), Some(Lang::Php));
        assert_eq!(Lang::from_path(Path::new("a.rb")), Some(Lang::Ruby));
        for ext in ["ts", "tsx", "js", "jsx", "mjs"] {
            assert_eq!(Lang::from_path(Path::new(&format!("a.{ext}"))), Some(Lang::Ts));
        }
        assert_eq!(Lang::from_path(Path::new("a.rs")), Some(Lang::Rust));
        assert_eq!(Lang::from_path(Path::new("A.RS")), Some(Lang::Rust), "extension match is case-insensitive");
        assert_eq!(Lang::from_path(Path::new("a.md")), None);
        assert_eq!(Lang::from_path(Path::new("noext")), None);
        // Pascal is deliberately NOT wired up - see the `pascal_is_not_claimed`
        // test below for why claiming it would be worse than omitting it.
        assert_eq!(Lang::from_path(Path::new("a.pas")), None);
    }

    #[test]
    fn cyclomatic_complexity_counts_decision_points_python() {
        // 1 (base) + if + boolean_operator(and) + for = 4. A comparison is NOT a
        // decision point (matches metrics.py), so `x > 0` adds nothing.
        let src = "def f(x):\n    if x and x > 0:\n        for i in x:\n            pass\n    return x\n";
        let (fm, fns) = analyze_py(src);
        assert_eq!(fm.functions, 1);
        assert_eq!(fns[0].complexity, 4, "CC should be 1+if+and+for");
        assert_eq!(fm.complexity, 4);
    }

    #[test]
    fn comprehension_and_ternary_add_complexity_python() {
        // 1 + for_in_clause + if_clause + conditional_expression(ternary) = 4.
        let src = "def g(items):\n    xs = [n for n in items if n]\n    return 1 if items else 0\n";
        let (_fm, fns) = analyze_py(src);
        assert_eq!(fns[0].complexity, 4);
    }

    #[test]
    fn maintainability_index_is_bounded_and_named_with_class() {
        let src = "class Foo:\n    def bar(self):\n        return 1\n";
        let (fm, fns) = analyze_py(src);
        assert!(fm.maintainability >= 0.0 && fm.maintainability <= 100.0);
        assert_eq!(fns[0].name, "Foo.bar", "method name carries its class prefix");
    }

    #[test]
    fn empty_file_scores_full_maintainability() {
        let (fm, _f) = analyze_py("\n\n# just a comment\n");
        assert_eq!(fm.loc, 0);
        assert_eq!(fm.maintainability, 100.0);
        assert_eq!(fm.functions, 0);
    }

    // ---- Nested scopes are measured once, not twice --------------------------

    #[test]
    fn a_parent_is_not_charged_for_its_nested_functions_python() {
        // `outer` has NO branch of its own. Each inner has two. Before the fix
        // outer reported 5 - its own base plus all four inner branches.
        let src = "def outer(a):\n    def inner1(x):\n        if x: return 1\n        if x > 2: return 2\n        return 3\n    def inner2(y):\n        if y: return 1\n        if y > 2: return 2\n        return 3\n    return inner1(a) + inner2(a)\n";
        let (fm, fns) = analyze_py(src);
        let outer = fns.iter().find(|f| f.name == "outer").unwrap();
        assert_eq!(outer.complexity, 1, "outer branches on nothing itself");
        for name in ["inner1", "inner2"] {
            let f = fns.iter().find(|f| f.name == name).unwrap();
            assert_eq!(f.complexity, 3, "{name} keeps its own two branches");
        }
        // The file total is the sum of the per-function values, so it drops too.
        assert_eq!(fm.complexity, 7, "1 + 3 + 3, with nothing counted twice");
    }

    #[test]
    fn a_python_lambda_still_counts_toward_its_enclosing_function() {
        // Lambdas are never listed as functions of their own, so excluding them
        // would silently lose their decisions instead of relocating them.
        let src = "def f(xs):\n    return sorted(xs, key=lambda x: 1 if x else 0)\n";
        let (_fm, fns) = analyze_py(src);
        assert_eq!(fns.len(), 1, "the lambda is not reported separately");
        assert_eq!(fns[0].complexity, 2, "1 + the lambda's ternary");
    }

    #[test]
    fn a_method_in_a_nested_class_is_not_charged_to_the_outer_function() {
        let src = "def make():\n    class Inner:\n        def go(self, x):\n            if x: return 1\n            return 2\n    return Inner\n";
        let (_fm, fns) = analyze_py(src);
        let outer = fns.iter().find(|f| f.name == "make").unwrap();
        assert_eq!(outer.complexity, 1, "the nested class body is a separate scope");
        let go = fns.iter().find(|f| f.name.ends_with("go")).unwrap();
        assert_eq!(go.complexity, 2);
    }

    #[test]
    fn an_iife_wrapper_does_not_absorb_the_whole_module_typescript() {
        // The real symptom: public/js/frond.js reported cc=191 because the whole
        // file sat inside one anonymous wrapper.
        let src = "(function () {\n  function a(x) { if (x) { return 1; } return 2; }\n  function b(y) { if (y) { return 1; } return 2; }\n  return { a: a, b: b };\n})();\n";
        let (_fm, fns) = analyze_ts(src);
        let wrapper = fns.iter().min_by_key(|f| f.line).unwrap();
        assert_eq!(wrapper.complexity, 1, "the wrapper itself branches on nothing");
        assert!(fns.iter().any(|f| f.complexity == 2), "inner functions keep theirs");
    }

    #[test]
    fn ruby_keyword_tokens_are_not_counted_as_decisions() {
        // Regression: tree-sitter's Ruby grammar uses "if"/"unless"/"while"/"when"
        // for both the construct and its keyword token, so every decision was
        // counted twice and Ruby complexity came out roughly double. Verified
        // against metrics.rb, which reports 2 for each of these.
        for (src, expected, what) in [
            ("def m(y)\n  return 1 if y\n  2\nend\n", 2, "modifier if"),
            ("def m(z)\n  if z\n    1\n  else\n    2\n  end\nend\n", 2, "if/else"),
            ("def m(z)\n  while z\n    z -= 1\n  end\nend\n", 2, "while"),
            ("def m(z)\n  z.each { |i| puts i }\n  1\nend\n", 1, "a block is not a decision"),
        ] {
            let (_fm, fns) = analyze_rb(src);
            assert_eq!(fns[0].complexity, expected, "{what}");
        }
    }

    #[test]
    fn a_php_closure_counts_toward_its_enclosing_method() {
        // Anonymous functions are not listed as functions of their own (same rule
        // as a Python lambda), so their decisions stay where they are written
        // rather than disappearing.
        let php = "<?php\nclass A {\n  function outer($x) {\n    return array_map(function ($y) { if ($y) { return 1; } return 2; }, $x);\n  }\n}\n";
        let (_fm, fns) = analyze_php(php);
        let outer = fns.iter().find(|f| f.name.ends_with("outer")).unwrap();
        assert_eq!(outer.complexity, 2, "1 + the closure's if");
    }

    #[test]
    fn nested_methods_do_not_double_count_php_and_ruby() {
        // A named function declared inside a method IS listed separately, so its
        // branch must not also land on the method.
        let php = "<?php\nclass A {\n  function outer($x) {\n    function helper($y) { if ($y) { return 1; } return 2; }\n    return helper($x);\n  }\n}\n";
        let (_fm, fns) = analyze_php(php);
        let outer = fns.iter().find(|f| f.name.ends_with("outer")).unwrap();
        assert_eq!(outer.complexity, 1, "helper's branch belongs to helper");
        let helper = fns.iter().find(|f| f.name.ends_with("helper")).unwrap();
        assert_eq!(helper.complexity, 2);

        let rb = "class A\n  def outer(x)\n    inner(x)\n  end\n  def inner(y)\n    return 1 if y\n    2\n  end\nend\n";
        let (_fm, fns) = analyze_rb(rb);
        let outer = fns.iter().find(|f| f.name.ends_with("outer")).unwrap();
        assert_eq!(outer.complexity, 1);
        let inner = fns.iter().find(|f| f.name.ends_with("inner")).unwrap();
        assert_eq!(inner.complexity, 2);
    }

    // ---- Offender rules (mirror metrics.py thresholds) -----------------------

    fn file_with(mi: f64, loc: usize, funcs: usize, has_tests: bool) -> FileMetrics {
        // avg_complexity 8.0: a genuinely complex file, so the low-MI rule (now
        // complexity-gated) still fires for the tests that assert it does.
        file_with_cc(mi, loc, funcs, has_tests, 8.0)
    }

    fn file_with_cc(mi: f64, loc: usize, funcs: usize, has_tests: bool, avg_cc: f64) -> FileMetrics {
        FileMetrics {
            path: "x.py".into(), loc, complexity: (avg_cc * funcs as f64) as u32,
            avg_complexity: avg_cc,
            functions: funcs, maintainability: mi, halstead_volume: 0.0, has_tests,
            dep_count: 0, coupling_efferent: 0, coupling_afferent: 0, instability: 0.0,
            parse_health: 1.0,
        }
    }
    fn func_with(cc: u32) -> FunctionInfo {
        FunctionInfo { name: "f".into(), file: "x.py".into(), line: 1, complexity: cc, loc: 1 }
    }

    #[test]
    fn offender_low_maintainability_severity_split_at_20() {
        let warn = build_offenders(&[file_with(35.0, 10, 1, true)], &[]);
        assert_eq!(warn[0].kind, "low_maintainability");
        assert_eq!(warn[0].severity, "warn");
        let err = build_offenders(&[file_with(15.0, 10, 1, true)], &[]);
        assert_eq!(err[0].severity, "error");
        // MI >= 40 produces no low_maintainability offender.
        let clean = build_offenders(&[file_with(55.0, 10, 1, true)], &[]);
        assert!(clean.iter().all(|o| o.kind != "low_maintainability"));
    }

    #[test]
    fn low_maintainability_does_not_fire_on_a_big_but_simple_file() {
        // The false positive this rule shipped with. A large file of trivial,
        // branchless functions (avg CC 1.0) scores a low MI purely because the
        // formula is size-dominated - MEASURED at MI 8.4 for 400 branchless
        // functions over 1200 lines. That is size, not unmaintainability, and
        // `large_file` already reports it. The low-MI ERROR was noise on top.
        let big_simple = file_with_cc(8.0, 1200, 400, true, 1.0);
        let offs = build_offenders(&[big_simple], &[]);
        assert!(
            offs.iter().all(|o| o.kind != "low_maintainability"),
            "low_maintainability fired on a big-but-simple file (avg CC 1.0) - it is re-flagging size"
        );
        // ...but large_file still catches the real property (it IS large).
        assert!(offs.iter().any(|o| o.kind == "large_file"));
    }

    #[test]
    fn low_maintainability_still_fires_when_functions_are_complex() {
        // The signal that must survive the gate: low MI AND genuinely complex
        // functions (avg CC 8) is a real maintainability problem.
        let complex = file_with_cc(15.0, 1200, 400, true, 8.0);
        let offs = build_offenders(&[complex], &[]);
        let mi = offs.iter().find(|o| o.kind == "low_maintainability");
        assert!(mi.is_some(), "a low-MI file with complex functions must still flag");
        assert_eq!(mi.unwrap().severity, "error");
    }

    #[test]
    fn offender_complexity_severity_split_at_20() {
        let warn = build_offenders(&[], &[func_with(15)]);
        assert_eq!(warn[0].kind, "complexity");
        assert_eq!(warn[0].severity, "warn");
        let err = build_offenders(&[], &[func_with(25)]);
        assert_eq!(err[0].severity, "error");
        // CC <= 10 is not an offender.
        assert!(build_offenders(&[], &[func_with(10)]).is_empty());
    }

    #[test]
    fn offender_too_many_functions_and_untested() {
        let offs = build_offenders(&[file_with(90.0, 10, 21, false)], &[]);
        assert!(offs.iter().any(|o| o.kind == "too_many_functions" && o.severity == "warn"));
        assert!(offs.iter().any(|o| o.kind == "untested" && o.severity == "info"));
        // Exactly 20 functions and a matched test => neither offender.
        let clean = build_offenders(&[file_with(90.0, 10, 20, true)], &[]);
        assert!(clean.is_empty());
    }

    /// Named regression (parity with Python master fee4385). The old code capped
    /// the complexity offenders to the 15 most-complex functions (`by_cc.take(15)`),
    /// so a file with >15 over-threshold functions silently dropped the 16th+ from
    /// the offenders list AND from `--fail-on` — a too-complex function passed the
    /// gate. No mocks: builds REAL Python source with 18 functions each at
    /// cyclomatic complexity > 20 and drives it through the real tree-sitter
    /// analyzer, then asserts every one surfaces as an "error" complexity offender.
    #[test]
    fn offender_complexity_not_capped_all_over_threshold_surface() {
        let n: usize = 18; // > 15, so the old [:15] cap would have dropped fn15..fn17
        // 24 independent `if` statements => cyclomatic complexity = 1 + 24 = 25 (> 20).
        let body: String = (0..24).map(|j| format!("    if x == {j}:\n        x += 1\n")).collect();
        let src: String = (0..n)
            .map(|i| format!("def fn{i}(x):\n{body}    return x\n"))
            .collect::<Vec<_>>()
            .join("\n");

        let (_fm, fns) = analyze_py(&src);
        assert_eq!(fns.len(), n, "all {n} functions must parse");
        assert!(fns.iter().all(|f| f.complexity == 25), "each function is CC 25 (1 + 24 ifs)");

        let offs = build_offenders(&[], &fns);
        let complexity: Vec<&Offender> = offs.iter().filter(|o| o.kind == "complexity").collect();
        // Old capped behaviour surfaced exactly 15; the fix surfaces all 18.
        assert_eq!(
            complexity.len(),
            n,
            "expected {n} complexity offenders (was capped at 15), got {}",
            complexity.len()
        );
        assert!(
            complexity.iter().all(|o| o.severity == "error"),
            "CC 25 > 20 => every complexity offender is severity error"
        );
    }

    #[test]
    fn fail_on_gate_exit_codes() {
        assert_eq!(compute_exit_code(None, true, true), 0);
        assert_eq!(compute_exit_code(Some("warn"), false, false), 0);
        assert_eq!(compute_exit_code(Some("warn"), true, false), 1);
        assert_eq!(compute_exit_code(Some("warn"), false, true), 1);
        assert_eq!(compute_exit_code(Some("error"), true, false), 0);
        assert_eq!(compute_exit_code(Some("error"), false, true), 1);
    }

    // ---- Non-Python: proves no framework / no project needed -----------------

    #[test]
    fn analyzes_php_ruby_typescript_without_a_project() {
        let php = "<?php\nfunction f($a){ if ($a && $a > 0) { return 1; } return 0; }\n";
        let (fm, fns, _s) = measured(Lang::Php, php, "t.php", false);
        assert_eq!(fm.functions, 1);
        assert!(fns[0].complexity >= 3); // 1 + if + &&
        assert!(fm.maintainability > 0.0 && fm.maintainability <= 100.0);

        let rb = "def f(a)\n  return 1 if a && a > 0\n  0\nend\n";
        let (fm, _f, _s) = measured(Lang::Ruby, rb, "t.rb", false);
        assert_eq!(fm.functions, 1);

        let ts = "export const f = (a: number) => { if (a && a > 0) { return 1; } return 0; };\n";
        let (fm, fns, _s) = measured(Lang::Ts, ts, "t.ts", false);
        assert_eq!(fm.functions, 1, "top-level arrow function is counted");
        assert!(fns[0].complexity >= 3);
    }

    #[test]
    fn typescript_imports_are_counted_as_dep_count() {
        let ts = "import { a } from './a';\nimport b from './b';\nconst x = () => a + b;\n";
        let (fm, _f, specs) = measured(Lang::Ts, ts, "t.ts", false);
        // dep_count is every import as written - the number the dashboard badges.
        assert_eq!(fm.dep_count, 2);
        // Order is not part of the contract: the walk is a stack-based DFS and the
        // graph's targets are sorted downstream, so compare as a set.
        let mut got = specs.clone();
        got.sort();
        assert_eq!(got, vec!["./a".to_string(), "./b".to_string()]);
        // The coupling TRIPLE is internal-only and needs the whole file set, so
        // analyze_source leaves it at zero; analyze_targets fills it in.
        assert_eq!(fm.coupling_efferent, 0, "resolved in pass 2, not here");
    }

    #[test]
    fn php_inline_fully_qualified_references_are_dependencies() {
        // PHP resolves most dependencies through the autoloader from an INLINE
        // fully-qualified name, not a `use` statement. In the real framework that
        // is 243 inline references against 72 `use` lines, so counting only `use`
        // understated PHP coupling by 3-4x (40 edges instead of 138).
        let php = "<?php\nnamespace Tina4;\nfunction boot() {\n    \\Tina4\\DotEnv::load();\n    $d = \\Tina4\\Database::create('x');\n    return \\Tina4\\Database::create('y');\n}\n";
        let (fm, _f, specs) = measured(Lang::Php, php, "App.php", false);
        assert!(specs.iter().any(|s| s.contains("DotEnv")), "got {specs:?}");
        assert!(specs.iter().any(|s| s.contains("Database")), "got {specs:?}");

        // Referencing Database TWICE is ONE dependency, not two - otherwise the
        // dashboard's dep_count badge inflates wildly on PHP's idiom.
        assert_eq!(fm.dep_count, 2, "distinct dependencies, not reference count: {specs:?}");

        let paths: HashSet<String> =
            ["DotEnv.php", "Database.php"].iter().map(|s| s.to_string()).collect();
        assert_eq!(
            resolve_import("\\Tina4\\DotEnv", "App.php", Lang::Php, &paths, Some("Tina4")),
            Some("DotEnv.php".to_string())
        );
    }

    #[test]
    fn php_use_statements_and_requires_are_extracted() {
        // The path in `use A\B;` lives one level down, inside a
        // namespace_use_clause. Matching only the declaration's direct children
        // silently found NOTHING, so PHP produced 0 edges over 138 real files.
        let php = "<?php\nnamespace Tina4;\nuse Tina4\\ORM;\nuse Tina4\\Database\\Database;\nrequire_once \"helpers.php\";\nfunction f($a) { return $a; }\n";
        let (fm, _f, specs) = measured(Lang::Php, php, "Frond.php", false);
        let mut got = specs.clone();
        got.sort();
        assert_eq!(
            got,
            vec![
                "Tina4\\Database\\Database".to_string(),
                "Tina4\\ORM".to_string(),
                "helpers.php".to_string(),
            ]
        );
        assert_eq!(fm.dep_count, 3);

        // ...and they resolve onto real files via PSR-4.
        let paths: HashSet<String> = ["ORM.php", "Database/Database.php", "helpers.php"]
            .iter().map(|s| s.to_string()).collect();
        assert_eq!(
            resolve_import("Tina4\\ORM", "Frond.php", Lang::Php, &paths, Some("Tina4")),
            Some("ORM.php".to_string())
        );
        assert_eq!(
            resolve_import("Tina4\\Database\\Database", "Frond.php", Lang::Php, &paths, Some("Tina4")),
            Some("Database/Database.php".to_string())
        );
    }

    #[test]
    fn every_function_reports_its_own_loc() {
        // The dashboard's most-complex-functions table has a LOC column; without
        // this field every row rendered a literal "undefined".
        let py = "def small():\n    return 1\n\ndef bigger(a):\n    # comment line\n    if a:\n        return 2\n    return 3\n";
        let (_fm, fns) = analyze_py(py);
        let small = fns.iter().find(|f| f.name == "small").unwrap();
        let bigger = fns.iter().find(|f| f.name == "bigger").unwrap();
        assert_eq!(small.loc, 2, "def + return");
        // def + if + two returns = 4 code lines; the comment does NOT count,
        // matching the file-level is_code_line rule.
        assert_eq!(bigger.loc, 4, "comment excluded, same rule as file LOC");
        assert!(bigger.loc > small.loc);
        // and it is serialised, so the UI column can read it
        let json = serde_json::to_string(&bigger).unwrap();
        assert!(json.contains("\"loc\""), "loc must be in the JSON: {json}");
    }

    // ---- Generated-asset exclusion -------------------------------------------
    //
    // The engine is language-agnostic, so it sees `.js` the retired per-language
    // modules never did. One real minified bundle scored cyclomatic complexity
    // 26,416 on a single line and took the top FOUR offender slots, burying the
    // code a developer can actually fix. Build output is not source.

    #[test]
    fn minified_and_bundled_assets_are_excluded_by_name() {
        for n in [
            "tina4.min.js", "frond.min.js", "app.min.ts", "site.min.css",
            "vendor.bundle.js", "legacy-min.js", "app.js.map",
        ] {
            assert!(is_generated_asset(Path::new(n)), "{n} should be excluded");
        }
    }

    #[test]
    fn real_source_files_are_not_mistaken_for_assets() {
        for n in [
            "engine.py", "server.ts", "Frond.php", "cli.rb",
            "widget.js", "frond.js", "minify.js", "administer.ts",
        ] {
            assert!(!is_generated_asset(Path::new(n)), "{n} must still be analysed");
        }
    }

    #[test]
    fn a_bundle_not_named_like_one_is_caught_by_content() {
        // One enormous line is the minified shape, whatever the filename says.
        let minified = format!("var a=1;{}\n", "b(c,d);".repeat(400));
        assert!(looks_minified(&minified));

        // Ordinary source, even long, is not.
        let real = "def handler(request, response):\n    value = compute(request)\n    return response(value)\n".repeat(50);
        assert!(!looks_minified(&real), "normal source must not be skipped");

        // Degenerate inputs do not panic or false-positive.
        assert!(!looks_minified(""));
        assert!(!looks_minified("x = 1\n"));
    }

    // ---- Coupling resolution (the fix for the constant-1.0 instability) ------
    //
    // The retired per-framework implementation keyed its reverse graph on MODULE
    // NAMES while looking it up by FILE PATH, so afferent coupling was 0 for
    // every file, instability was the constant 1.0, and the dependency view drew
    // 0 edges from 902 recorded imports. These pin the corrected behaviour.

    fn pathset(items: &[&str]) -> HashSet<String> {
        items.iter().map(|s| s.to_string()).collect()
    }

    #[test]
    fn python_relative_and_absolute_imports_resolve_to_files() {
        let paths = pathset(&["frond/engine.py", "frond/parser.py", "debug/__init__.py", "env.py"]);
        // `from .parser import x` inside frond/engine.py
        assert_eq!(
            resolve_import(".parser", "frond/engine.py", Lang::Python, &paths, Some("tina4_python")),
            Some("frond/parser.py".to_string())
        );
        // a package __init__ target
        assert_eq!(
            resolve_import("debug", "env.py", Lang::Python, &paths, Some("tina4_python")),
            Some("debug/__init__.py".to_string())
        );
        // absolute intra-package import while the scan root IS the package
        assert_eq!(
            resolve_import("tina4_python.env", "frond/engine.py", Lang::Python, &paths, Some("tina4_python")),
            Some("env.py".to_string())
        );
    }

    #[test]
    fn external_imports_are_not_internal_edges() {
        let paths = pathset(&["env.py", "app/main.ts"]);
        // stdlib / third-party must NOT become coupling
        assert_eq!(resolve_import("os", "env.py", Lang::Python, &paths, None), None);
        assert_eq!(resolve_import("json", "env.py", Lang::Python, &paths, None), None);
        // a bare TS specifier is a package, never a file in the tree
        assert_eq!(resolve_import("react", "app/main.ts", Lang::Ts, &paths, None), None);
        assert_eq!(resolve_import("node:fs", "app/main.ts", Lang::Ts, &paths, None), None);
    }

    #[test]
    fn typescript_relative_specifier_resolves_including_js_to_ts() {
        let paths = pathset(&["core/src/server.ts", "core/src/router.ts", "core/src/index.ts"]);
        assert_eq!(
            resolve_import("./router", "core/src/server.ts", Lang::Ts, &paths, None),
            Some("core/src/router.ts".to_string())
        );
        // TS writes ".js" in ESM but the file on disk is ".ts"
        assert_eq!(
            resolve_import("./router.js", "core/src/server.ts", Lang::Ts, &paths, None),
            Some("core/src/router.ts".to_string())
        );
        // a directory specifier picks up index.ts
        assert_eq!(
            resolve_import("../src", "core/other/x.ts", Lang::Ts, &paths, None),
            Some("core/src/index.ts".to_string())
        );
    }

    #[test]
    fn ruby_and_php_specifiers_resolve() {
        let rb = pathset(&["lib/tina4/frond.rb", "lib/tina4/orm.rb"]);
        assert_eq!(
            resolve_import("orm", "lib/tina4/frond.rb", Lang::Ruby, &rb, None),
            Some("lib/tina4/orm.rb".to_string())
        );
        assert_eq!(
            resolve_import("tina4/orm", "app.rb", Lang::Ruby, &rb, None),
            Some("lib/tina4/orm.rb".to_string())
        );
        let php = pathset(&["Tina4/Frond.php", "Tina4/ORM.php"]);
        assert_eq!(
            resolve_import("Tina4\\ORM", "Tina4/Frond.php", Lang::Php, &php, None),
            Some("Tina4/ORM.php".to_string())
        );
    }

    #[test]
    fn a_file_never_couples_to_itself() {
        let paths = pathset(&["a.py"]);
        // `import a` from inside a.py must not create a self-edge
        assert_eq!(resolve_import("a", "a.py", Lang::Python, &paths, None), None);
    }

    #[test]
    fn instability_is_a_real_spread_not_a_constant() {
        // Build a tiny real tree on disk and run the full two-pass analysis.
        let dir = std::env::temp_dir().join(format!("tina4_coupling_{}", std::process::id()));
        let _ = fs::remove_dir_all(&dir);
        fs::create_dir_all(&dir).unwrap();
        // leaf.py is imported by two files and imports nothing -> ca=2, ce=0 -> 0.0
        fs::write(dir.join("leaf.py"), "def a():\n    return 1\n").unwrap();
        fs::write(dir.join("mid.py"), "import leaf\nimport os\ndef b():\n    return leaf.a()\n").unwrap();
        fs::write(dir.join("top.py"), "import leaf\nimport mid\ndef c():\n    return mid.b()\n").unwrap();

        let files = vec![dir.join("leaf.py"), dir.join("mid.py"), dir.join("top.py")];
        let report = analyze_targets(&files, dir.to_str().unwrap());
        let get = |n: &str| report.files.iter().find(|f| f.path == n).unwrap().clone();

        let leaf = get("leaf.py");
        assert_eq!(leaf.coupling_afferent, 2, "mid and top both import leaf");
        assert_eq!(leaf.coupling_efferent, 0);
        assert_eq!(leaf.instability, 0.0, "a pure dependency is maximally STABLE");

        let top = get("top.py");
        assert_eq!(top.coupling_afferent, 0, "nothing imports top");
        assert_eq!(top.coupling_efferent, 2, "top imports leaf and mid");
        assert_eq!(top.instability, 1.0, "a pure dependent is maximally UNSTABLE");

        let mid = get("mid.py");
        assert_eq!(mid.coupling_afferent, 1);
        assert_eq!(mid.coupling_efferent, 1, "os is external and excluded");
        assert_eq!(mid.instability, 0.5, "one in, one out");
        // dep_count keeps TOTAL-import semantics for the dashboard badge.
        assert_eq!(mid.dep_count, 2, "leaf + os");

        // Every recorded edge points at a file that actually exists.
        let known: HashSet<&String> = report.files.iter().map(|f| &f.path).collect();
        for (_from, targets) in report.dependency_graph.iter() {
            for t in targets {
                assert!(known.contains(t), "edge target {t} is not a scanned file");
            }
        }
        let total_edges: usize = report.dependency_graph.values().map(|v| v.len()).sum();
        assert_eq!(total_edges, 3, "leaf<-mid, leaf<-top, mid<-top");

        let _ = fs::remove_dir_all(&dir);
    }

    // ---- Duplication / DRY ---------------------------------------------------
    //
    // Real files on a real filesystem, driven through the real `analyze_targets`
    // two-pass scan. Every case plants a KNOWN answer, so a detector that finds
    // nothing and a detector that flags everything both fail.

    /// Write `files` into a fresh temp dir and run the full scan over them.
    fn scan_temp(tag: &str, files: &[(&str, &str)]) -> (Report, PathBuf) {
        let dir = std::env::temp_dir()
            .join(format!("tina4_dup_{tag}_{}", std::process::id()));
        let _ = fs::remove_dir_all(&dir);
        fs::create_dir_all(&dir).unwrap();
        let mut paths = Vec::new();
        for (name, body) in files {
            let p = dir.join(name);
            if let Some(parent) = p.parent() {
                fs::create_dir_all(parent).unwrap();
            }
            fs::write(&p, body).unwrap();
            paths.push(p);
        }
        let report = analyze_targets(&paths, dir.to_str().unwrap());
        (report, dir)
    }

    /// A block big enough to clear both gates, parameterised so callers can vary
    /// the identifiers (a Type-2 clone) or an operator (NOT a clone).
    fn dup_block(fn_name: &str, var: &str, op: &str) -> String {
        format!(
            "fn {fn_name}(input: i32) -> i32 {{
    let mut {var} = 0;
    for step in 0..input {{
        if step % 2 == 0 {{
            {var} = {var} {op} step;
        }} else if step % 3 == 0 {{
            {var} = {var} {op} (step * 2);
        }} else {{
            {var} = {var} {op} 1;
        }}
    }}
    if {var} > 100 {{
        {var} = 100;
    }}
    {var}
}}
"
        )
    }

    #[test]
    fn a_planted_duplicate_pair_is_found() {
        // THE positive gate. Two identical blocks, one file.
        let src = format!("{}\n{}", dup_block("alpha", "total", "+"), dup_block("beta", "total", "+"));
        let (report, dir) = scan_temp("pair", &[("a.rs", &src)]);
        assert!(
            !report.clones.is_empty(),
            "a planted identical pair must be reported as duplication"
        );
        let g = &report.clones[0];
        assert!(g.copies >= 2, "expected at least 2 copies, got {}", g.copies);
        assert!(
            report.offenders.iter().any(|o| o.kind == "duplication"),
            "the clone must surface as a `duplication` offender"
        );
        let _ = fs::remove_dir_all(dir);
    }

    #[test]
    fn changing_one_of_the_pair_stops_it_being_reported() {
        // The other half of the same gate, and the one that proves the detector
        // is reading STRUCTURE rather than just finding any two big blocks.
        // `+` becomes `-` in one copy: same shape, different operator.
        let src = format!("{}\n{}", dup_block("alpha", "total", "+"), dup_block("beta", "total", "-"));
        let (report, dir) = scan_temp("broken", &[("a.rs", &src)]);
        assert!(
            report.clones.is_empty(),
            "changing an OPERATOR must break the match - shape hashing that ignored \
             operator tokens would still call these duplicates: {:?}",
            report.clones
        );
        let _ = fs::remove_dir_all(dir);
    }

    #[test]
    fn renaming_identifiers_does_not_defeat_detection() {
        // Type-2 clone (Roy/Cordy): copy-paste-and-rename is the single most
        // common real duplication, and an engine that misses it is not measuring
        // DRY at all. Different function name, different variable name, identical
        // structure.
        let src = format!("{}\n{}", dup_block("alpha", "total", "+"), dup_block("gamma", "accumulator", "+"));
        let (report, dir) = scan_temp("renamed", &[("a.rs", &src)]);
        assert!(
            !report.clones.is_empty(),
            "a renamed copy is still a duplicate and must be found"
        );
        let _ = fs::remove_dir_all(dir);
    }

    #[test]
    fn duplication_is_detected_across_files() {
        // Cross-file is the whole point - a single-file duplication number is
        // nearly worthless, and it is also the case an accumulator scoped to one
        // file would silently miss.
        let a = dup_block("alpha", "total", "+");
        let b = dup_block("beta", "total", "+");
        let (report, dir) = scan_temp("xfile", &[("a.rs", &a), ("b.rs", &b)]);
        let cross: Vec<&CloneGroup> = report.clones.iter().filter(|c| c.cross_file).collect();
        assert!(
            !cross.is_empty(),
            "the same block in two different files must be reported as cross-file: {:?}",
            report.clones
        );
        assert_eq!(cross[0].files.len(), 2, "both files must be named");
        assert!(
            report
                .offenders
                .iter()
                .any(|o| o.kind == "duplication" && o.detail.contains("across 2 files")),
            "the offender detail must say it spans files"
        );
        let _ = fs::remove_dir_all(dir);
    }

    /// NEGATIVE CONTROL. A detector that flags everything would pass every test
    /// above. This file has NO duplication and must produce none.
    #[test]
    fn duplication_negative_control_distinct_code_is_not_flagged() {
        let src = "\
fn parse_port(raw: &str) -> Option<u16> {
    raw.trim().parse::<u16>().ok()
}

fn banner(name: &str, version: &str) -> String {
    format!(\"{name} v{version} ready\")
}

fn is_even(n: i64) -> bool {
    n % 2 == 0
}

fn clamp_ratio(value: f64) -> f64 {
    if value < 0.0 {
        return 0.0;
    }
    if value > 1.0 {
        return 1.0;
    }
    value
}
";
        let (report, dir) = scan_temp("clean", &[("clean.rs", src)]);
        assert!(
            report.clones.is_empty(),
            "structurally distinct functions must NOT be reported as duplicates: {:?}",
            report.clones
        );
        let _ = fs::remove_dir_all(dir);
    }

    /// The false-positive case that kills duplication tools in practice: a file
    /// full of near-identical trivial accessors. They ARE the same shape, and a
    /// size gate is the only thing standing between a useful report and one
    /// nobody reads.
    #[test]
    fn trivial_repeated_accessors_are_below_the_reporting_floor() {
        let src = "\
struct Config { host: String, port: String, user: String, pass: String, name: String }

impl Config {
    fn host(&self) -> &str { &self.host }
    fn port(&self) -> &str { &self.port }
    fn user(&self) -> &str { &self.user }
    fn pass(&self) -> &str { &self.pass }
    fn name(&self) -> &str { &self.name }
}
";
        let (report, dir) = scan_temp("getters", &[("cfg.rs", src)]);
        assert!(
            report.clones.is_empty(),
            "five identical one-line getters must NOT be reported - they are \
             identical by nature and unifying them would make the code worse: {:?}",
            report.clones
        );
        let _ = fs::remove_dir_all(dir);
    }

    #[test]
    fn duplication_works_for_every_language_not_just_rust() {
        // Language-agnostic by construction: the detector never names a node
        // kind, so a sixth language would need no work here. Proven, not
        // asserted - each language gets its own planted pair.
        let py_block = |n: &str| format!(
            "def {n}(items):\n    total = 0\n    for item in items:\n        if item > 0:\n            total += item\n        elif item < 0:\n            total -= item\n        else:\n            total += 1\n    if total > 100:\n        total = 100\n    return total\n");
        let php_block = |n: &str| format!(
            "function {n}($items) {{\n    $total = 0;\n    foreach ($items as $item) {{\n        if ($item > 0) {{\n            $total += $item;\n        }} elseif ($item < 0) {{\n            $total -= $item;\n        }} else {{\n            $total += 1;\n        }}\n    }}\n    if ($total > 100) {{\n        $total = 100;\n    }}\n    return $total;\n}}\n");
        let rb_block = |n: &str| format!(
            "def {n}(items)\n  total = 0\n  items.each do |item|\n    if item > 0\n      total += item\n    elsif item < 0\n      total -= item\n    else\n      total += 1\n    end\n  end\n  if total > 100\n    total = 100\n  end\n  total\nend\n");
        let ts_block = |n: &str| format!(
            "export function {n}(items: number[]): number {{\n    let total = 0;\n    for (const item of items) {{\n        if (item > 0) {{\n            total += item;\n        }} else if (item < 0) {{\n            total -= item;\n        }} else {{\n            total += 1;\n        }}\n    }}\n    if (total > 100) {{\n        total = 100;\n    }}\n    return total;\n}}\n");

        for (tag, name, body) in [
            ("py", "a.py", format!("{}\n{}", py_block("alpha"), py_block("beta"))),
            ("php", "a.php", format!("<?php\n{}\n{}", php_block("alpha"), php_block("beta"))),
            ("rb", "a.rb", format!("{}\n{}", rb_block("alpha"), rb_block("beta"))),
            ("ts", "a.ts", format!("{}\n{}", ts_block("alpha"), ts_block("beta"))),
        ] {
            let (report, dir) = scan_temp(tag, &[(name, &body)]);
            assert!(
                !report.clones.is_empty(),
                "{tag}: a planted duplicate pair must be found in EVERY language, \
                 not just Rust"
            );
            let _ = fs::remove_dir_all(dir);
        }
    }

    #[test]
    fn nested_copies_of_one_clone_are_reported_once() {
        // A duplicated block also duplicates every sub-block inside it, at every
        // wrapper level. Without maximal-only suppression the same clone lands in
        // the report three or four times and buries the real findings - which is
        // exactly what the first working version did (92 groups collapsed to 45).
        let src = format!("{}\n{}", dup_block("alpha", "total", "+"), dup_block("beta", "total", "+"));
        let (report, dir) = scan_temp("nested", &[("a.rs", &src)]);
        let at_same_place: Vec<&CloneGroup> = report
            .clones
            .iter()
            .filter(|c| c.first_file == "a.rs" && c.first_line == report.clones[0].first_line)
            .collect();
        assert_eq!(
            at_same_place.len(),
            1,
            "one duplicated region must yield ONE group, not one per nesting level: {:?}",
            report.clones
        );
        let _ = fs::remove_dir_all(dir);
    }

    // ---- Parse-health guard --------------------------------------------------
    //
    // tree-sitter always returns a tree, so a file it cannot parse still yields
    // LOC, CC and MI - all confidently wrong, because the decision points inside
    // an ERROR region are invisible to the walks that count them. Found via a
    // Delphi grammar gap that put 51.5% of a real corpus inside ERROR regions,
    // but it applies to every language the engine claims.

    /// A real, multi-branch function per language. Long enough to have
    /// decisions, functions and MI worth reporting, so "it still reports" means
    /// something.
    const HEALTHY: [(&str, &str, &str); 5] = [
        (
            "python", "ok.py",
            "def classify(value, limit):\n    if value is None:\n        return 'none'\n    if value > limit:\n        return 'high'\n    elif value < 0:\n        return 'negative'\n    return 'ok'\n\n\ndef total(rows):\n    out = 0\n    for row in rows:\n        if row:\n            out += row\n    return out\n",
        ),
        (
            "php", "ok.php",
            "<?php\nfunction classify($value, $limit) {\n    if ($value === null) { return 'none'; }\n    if ($value > $limit) { return 'high'; }\n    elseif ($value < 0) { return 'negative'; }\n    return 'ok';\n}\n\nfunction total($rows) {\n    $out = 0;\n    foreach ($rows as $row) {\n        if ($row) { $out += $row; }\n    }\n    return $out;\n}\n",
        ),
        (
            "ruby", "ok.rb",
            "def classify(value, limit)\n  return 'none' if value.nil?\n  return 'high' if value > limit\n  return 'negative' if value < 0\n  'ok'\nend\n\ndef total(rows)\n  out = 0\n  rows.each do |row|\n    out += row if row\n  end\n  out\nend\n",
        ),
        (
            "ts", "ok.ts",
            "export function classify(value: number | null, limit: number): string {\n  if (value === null) { return 'none'; }\n  if (value > limit) { return 'high'; }\n  else if (value < 0) { return 'negative'; }\n  return 'ok';\n}\n\nexport function total(rows: number[]): number {\n  let out = 0;\n  for (const row of rows) {\n    if (row) { out += row; }\n  }\n  return out;\n}\n",
        ),
        (
            "rust", "ok.rs",
            "pub fn classify(value: Option<i32>, limit: i32) -> &'static str {\n    let Some(v) = value else { return \"none\" };\n    if v > limit {\n        \"high\"\n    } else if v < 0 {\n        \"negative\"\n    } else {\n        \"ok\"\n    }\n}\n\npub fn total(rows: &[i32]) -> i32 {\n    let mut out = 0;\n    for row in rows {\n        if *row != 0 {\n            out += row;\n        }\n    }\n    out\n}\n",
        ),
    ];

    #[test]
    fn healthy_real_source_parses_at_full_health_in_every_language() {
        // The calibration behind MIN_PARSE_HEALTH, and the control for the
        // refusal tests below: if this drifts, the floor is wrong, not the guard.
        for (name, _file, src) in HEALTHY {
            let lang = match name {
                "python" => Lang::Python,
                "php" => Lang::Php,
                "ruby" => Lang::Ruby,
                "ts" => Lang::Ts,
                _ => Lang::Rust,
            };
            let (fm, _f, _s) = measured(lang, src, "t", false);
            assert_eq!(fm.parse_health, 1.0, "{name}: clean source must parse at 1.0");
            assert!(
                fm.parse_health >= MIN_PARSE_HEALTH,
                "{name}: healthy source must never be refused"
            );
        }
    }

    #[test]
    fn a_healthy_file_still_reports_full_metrics_in_every_language() {
        // THE NEGATIVE CONTROL, and the reason the refusal tests mean anything.
        // A guard that refused everything would pass every test above this one.
        // So: drive the same five languages through the WHOLE scan and demand
        // real numbers out the other side.
        for (name, file, src) in HEALTHY {
            let (report, dir) = scan_temp(&format!("healthy_{name}"), &[(file, src)]);
            assert_eq!(
                report.refused.len(),
                0,
                "{name}: healthy source must NOT be refused - {:?}",
                report.refused.iter().map(|r| &r.reason).collect::<Vec<_>>()
            );
            assert_eq!(report.files.len(), 1, "{name}: healthy source must be measured");
            let fm = &report.files[0];
            assert_eq!(fm.parse_health, 1.0, "{name}: health");
            assert_eq!(fm.functions, 2, "{name}: both functions must be found");
            assert!(fm.loc >= 10, "{name}: LOC must be real, got {}", fm.loc);
            assert!(fm.complexity >= 4, "{name}: CC must count the branches, got {}", fm.complexity);
            assert!(
                fm.maintainability > 0.0,
                "{name}: MI must be reported, got {}",
                fm.maintainability
            );
            assert!(
                !report.offenders.iter().any(|o| o.kind == "unparsed"),
                "{name}: healthy source must raise no `unparsed` offender"
            );
            let _ = fs::remove_dir_all(dir);
        }
    }

    #[test]
    fn a_badly_broken_file_is_refused_in_every_language() {
        // Real garbage, not a mock: text that the real grammar genuinely cannot
        // parse. Each is structurally broken from near the top so the ERROR
        // region covers most of the file, which is exactly the Delphi shape.
        let broken = [
            ("python", "py", "def f(:\n  ??? not python at all ~~~\n  <<<>>>\n  ]]]}}}\n  def ((\n"),
            ("php", "php", "<?php\nfunction ((( {{{ ]]] ???\n  &&& ||| >>>\n  class class class\n"),
            ("ruby", "rb", "def f(\n  ??? ]]] }}}\n  end end end end\n  class class class\n"),
            ("ts", "ts", "function ((( {{{ ]]]\n  ??? >>> <<<\n  class class class\n  ))) }}}\n"),
            ("rust", "rs", "fn f( { ]]] ???\n  >>> <<< |||\n  struct struct struct\n  ))) }}}\n"),
        ];
        for (name, ext, src) in broken {
            let (report, dir) = scan_temp(
                &format!("broken_{name}"),
                &[(&format!("bad.{ext}"), src)],
            );
            assert_eq!(
                report.files.len(),
                0,
                "{name}: an unparseable file must NOT appear in file_metrics - \
                 its numbers would poison every average"
            );
            assert_eq!(
                report.refused.len(),
                1,
                "{name}: an unparseable file must be REFUSED, not silently skipped"
            );
            assert!(
                report.refused[0].parse_health < MIN_PARSE_HEALTH,
                "{name}: refused at health {}",
                report.refused[0].parse_health
            );
            // Visible in the ranked list, not only in a section nobody reads.
            assert!(
                report.offenders.iter().any(|o| o.kind == "unparsed"
                    && o.detail.contains("NOT MEASURED")),
                "{name}: refusal must surface as an `unparsed` offender"
            );
            let _ = fs::remove_dir_all(dir);
        }
    }

    #[test]
    fn a_refused_file_does_not_drag_down_a_healthy_scan() {
        // The reason refusal beats "report it anyway": one unreadable file must
        // not move the numbers for the files that ARE readable.
        let good = "pub fn add(a: i32, b: i32) -> i32 {\n    a + b\n}\n";
        let (solo, d1) = scan_temp("solo_ok", &[("good.rs", good)]);
        let (mixed, d2) = scan_temp(
            "mixed_ok",
            &[("good.rs", good), ("bad.rs", "fn f( { ]]] ???\n  >>> <<< |||\n  struct struct struct\n  ))) }}}\n")],
        );
        assert_eq!(mixed.files.len(), 1, "only the healthy file is measured");
        assert_eq!(mixed.refused.len(), 1);
        assert_eq!(
            solo.files[0].maintainability, mixed.files[0].maintainability,
            "the healthy file's MI must be identical whether or not a broken \
             file sat next to it"
        );
        let _ = fs::remove_dir_all(d1);
        let _ = fs::remove_dir_all(d2);
    }

    #[test]
    fn a_refused_file_contributes_no_duplication() {
        // Shapes hashed out of a misparse are noise. Two identically-broken
        // files would otherwise "duplicate" each other and invent a finding.
        let junk = "fn f( { ]]] ???\n  >>> <<< |||\n  struct struct struct\n  ))) }}}\n  ??? ]]] {{{\n  <<< >>> |||\n  fn fn fn fn\n";
        let (report, dir) = scan_temp("junk_dup", &[("a.rs", junk), ("b.rs", junk)]);
        assert_eq!(report.refused.len(), 2, "both are rubble");
        assert!(
            report.clones.is_empty(),
            "two unparseable files must not be reported as duplicates of each \
             other: {:?}",
            report.clones
        );
        let _ = fs::remove_dir_all(dir);
    }

    #[test]
    fn generated_doxygen_output_is_not_scanned_as_source() {
        // Scanning tina4delphi used to report metrics for seven Doxygen files
        // and nothing else - a report entirely about code nobody wrote.
        // Detected by Doxygen's own marker file, not by directory name, because
        // `docs/` and `html/` are legitimate places for hand-written source.
        let dir = std::env::temp_dir().join(format!("tina4_doxy_{}", std::process::id()));
        let _ = fs::remove_dir_all(&dir);
        let gen = dir.join("docs/html");
        fs::create_dir_all(&gen).unwrap();
        fs::write(gen.join("doxygen.css"), "body{}\n").unwrap();
        fs::write(gen.join("search.js"), "function search(a){ if(a){return 1;} return 0; }\n").unwrap();
        // A hand-written source file in a plain docs dir must STILL be scanned.
        let real = dir.join("docs/examples");
        fs::create_dir_all(&real).unwrap();
        fs::write(real.join("demo.js"), "function demo(a){ if(a){return 1;} return 0; }\n").unwrap();

        let mut found = Vec::new();
        walk_dir(&dir, &mut found);
        let names: Vec<String> = found
            .iter()
            .map(|p| p.file_name().unwrap().to_string_lossy().to_string())
            .collect();
        assert!(
            !names.contains(&"search.js".to_string()),
            "generated Doxygen JS must not be scanned as source: {names:?}"
        );
        assert!(
            names.contains(&"demo.js".to_string()),
            "a real source file under docs/ must STILL be scanned - the marker \
             file is the signal, not the directory name: {names:?}"
        );
        let _ = fs::remove_dir_all(dir);
    }

    #[test]
    fn a_refusal_is_loud_enough_to_fail_a_ci_gate() {
        // The whole point of refusing rather than skipping. A refused file has
        // to reach `--fail-on warn`, or a scan that measured nothing can still
        // exit 0 and read as green.
        let (report, dir) = scan_temp(
            "refusal_gate",
            &[("bad.py", "def f(:\n  ??? not python at all ~~~\n  <<<>>>\n  ]]]}}}\n  def ((\n")],
        );
        let summary = build_summary(&report, report.offenders.len());
        assert_eq!(summary.files_refused, 1, "the summary must count the refusal");
        assert_eq!(summary.files_analyzed, 0);
        let has_warn = report.offenders.iter().any(|o| o.severity == "warn");
        let has_error = report.offenders.iter().any(|o| o.severity == "error");
        assert!(has_warn, "a refusal must raise at least a warn");
        assert_eq!(
            compute_exit_code(Some("warn"), has_warn, has_error),
            1,
            "`--fail-on warn` must go red on a file the engine could not read"
        );
        // The other half, deliberately: refusal is a TOOLING gap, not a defect
        // in the code being measured, so it must not newly break existing
        // `--fail-on error` runs.
        assert_eq!(
            compute_exit_code(Some("error"), has_warn, has_error),
            0,
            "`--fail-on error` must stay green - a grammar gap is not the \
             author's error"
        );
        let _ = fs::remove_dir_all(dir);
    }

    // ---- Recursion depth (a PRE-EXISTING crash, not a duplication one) --------
    //
    // Five walks recurse per AST level. Reproduced against the 58f7f73 release
    // binary on macOS 26.5.2 arm64: a 60,000-term left-associative Python
    // expression, one term per line, aborts the process with
    // `fatal runtime error: stack overflow`, exit 134, taking the entire scan
    // with it. `looks_minified` does not fire - the file is 10 bytes per line
    // against its 200 threshold.

    /// A left-associative expression `terms` deep, one term per line, so
    /// `looks_minified` cannot short-circuit the case under test.
    fn deep_expression(terms: usize) -> String {
        let mut src = String::from("x = (a0\n");
        for i in 1..terms {
            src.push_str(&format!("  + a{i}\n"));
        }
        src.push_str(")\n");
        src
    }

    #[test]
    fn a_pathologically_deep_file_is_refused_instead_of_aborting_the_scan() {
        let src = deep_expression(MAX_AST_DEPTH + 200);
        assert!(
            !looks_minified(&src),
            "the fixture must reach the guard, not be filtered as a bundle"
        );
        let good = "pub fn add(a: i32, b: i32) -> i32 {\n    a + b\n}\n";
        let (report, dir) = scan_temp("deep", &[("deep.py", &src), ("ok.rs", good)]);
        // Reaching this line at all is most of the assertion: the pre-change
        // binary never returns from here.
        assert_eq!(report.refused.len(), 1, "the deep file must be refused");
        assert!(
            report.refused[0].reason.contains("nests deeper"),
            "the reason must name the real cause: {}",
            report.refused[0].reason
        );
        assert_eq!(
            report.files.len(),
            1,
            "the REST of the scan must survive - one bad file must not cost the \
             whole report"
        );
        assert_eq!(report.files[0].path, "ok.rs");
        let _ = fs::remove_dir_all(dir);
    }

    #[test]
    fn a_deeply_nested_but_survivable_file_is_still_measured() {
        // The negative control for the depth guard. A limit that refused
        // anything nested at all would pass the test above and be useless.
        // 600 is 7x deeper than the deepest real file in the whole corpus
        // (79, src/agent.rs) and still comfortably measured.
        let src = deep_expression(600);
        let (report, dir) = scan_temp("deep_ok", &[("deepish.py", &src)]);
        assert_eq!(
            report.refused.len(),
            0,
            "600 levels is under the {MAX_AST_DEPTH} limit and must be measured: {:?}",
            report.refused.iter().map(|r| &r.reason).collect::<Vec<_>>()
        );
        assert_eq!(report.files.len(), 1);
        assert!(report.files[0].loc > 0);
        let _ = fs::remove_dir_all(dir);
    }

    #[test]
    fn depth_guard_boundary_is_exact() {
        // Pinned on a REAL parsed tree, both sides, so the guard cannot drift
        // off by one in either direction.
        let mut parser = Parser::new();
        parser.set_language(&Lang::Python.tree_sitter_language()).unwrap();
        let tree = parser.parse(deep_expression(50), None).unwrap();
        let root = tree.root_node();
        // Measure the tree's true depth iteratively, then bracket it.
        let mut depth = 0usize;
        let mut stack = vec![(root, 0usize)];
        while let Some((node, d)) = stack.pop() {
            if d > depth {
                depth = d;
            }
            let mut c = node.walk();
            for child in node.children(&mut c) {
                stack.push((child, d + 1));
            }
        }
        assert!(depth > 10, "the fixture must actually be deep, got {depth}");
        assert!(!depth_exceeds(root, depth), "depth {depth} does not EXCEED {depth}");
        assert!(
            depth_exceeds(root, depth - 1),
            "depth {depth} does exceed {}",
            depth - 1
        );
    }

    // ---- Duplication must never be built out of a misparse -------------------

    #[test]
    fn an_error_region_never_becomes_a_clone_fragment() {
        // Seven lines of garbage that tree-sitter folds into ONE ERROR node.
        // Measured on tree-sitter 0.26.11: that node is 69 nodes over 7 lines,
        // so it clears MIN_CLONE_NODES (60) and MIN_CLONE_LINES (6) on its own
        // and the pre-change `collect_fragments` emitted it as a candidate.
        let junk = "class ][ oops @@@\n  %%%% ????\n  ]]] [[[ }}}\n  &&& ||| ^^^\n  ~~~ !!! @@@\n  ((( ))) {{{\n  ,,, ... ;;;\n";
        let mut parser = Parser::new();
        parser.set_language(&Lang::Python.tree_sitter_language()).unwrap();
        let tree = parser.parse(junk, None).unwrap();

        // First prove the fixture is still the hard case it was written to be:
        // an ERROR node that WOULD qualify if nothing stopped it.
        let mut qualifying_error = false;
        let mut stack = vec![tree.root_node()];
        while let Some(node) = stack.pop() {
            let mut c = node.walk();
            let children: Vec<Node> = node.children(&mut c).collect();
            if node.is_error() {
                let mut count = 0u32;
                let mut inner = vec![node];
                while let Some(n) = inner.pop() {
                    count += 1;
                    let mut ic = n.walk();
                    for ch in n.children(&mut ic) {
                        inner.push(ch);
                    }
                }
                let lines = node.end_position().row - node.start_position().row + 1;
                if count >= MIN_CLONE_NODES && lines >= MIN_CLONE_LINES {
                    qualifying_error = true;
                }
            }
            stack.extend(children);
        }
        assert!(
            qualifying_error,
            "fixture no longer contains an ERROR node big enough to clear both \
             clone gates - it is not testing anything"
        );

        let mut fragments: Vec<CloneFragment> = Vec::new();
        let (_hash, _count, clean) = collect_fragments(tree.root_node(), 0, &mut fragments);
        assert!(!clean, "a tree with an ERROR node is not clean");
        assert!(
            fragments.is_empty(),
            "no fragment may be hashed out of a parse error: {fragments:?}"
        );
    }

    #[test]
    fn two_differently_broken_files_are_never_duplicates_of_each_other() {
        // The failure this guards against, stated plainly: being unparseable is
        // not a similarity. Two files whose only common property is that the
        // grammar gave up on them must produce no finding at all.
        let a = "def f(:\n  ??? not python ~~~\n  <<<>>>\n  ]]]}}}\n  def ((\n  class ][\n  @@@ %%%\n";
        let b = "class ][ oops @@@\n  %%%% ????\n  ]]] [[[ }}}\n  &&& ||| ^^^\n  ~~~ !!! @@@\n  ((( ))) {{{\n  ,,, ... ;;;\n";
        let (report, dir) = scan_temp("two_broken", &[("a.py", a), ("b.py", b)]);
        assert_eq!(report.refused.len(), 2, "both are rubble");
        assert!(
            report.clones.is_empty(),
            "two DIFFERENT unparseable files must not be reported as duplicates: {:?}",
            report.clones
        );
        assert!(
            !report.offenders.iter().any(|o| o.kind == "duplication"),
            "no duplication offender may come out of two misparses"
        );
        let _ = fs::remove_dir_all(dir);
    }

    #[test]
    fn comments_are_hashed_so_this_is_not_full_type_2() {
        // Locks the HONEST label on the detector. Roy/Cordy Type-II tolerates
        // comments; this implementation does not, in any of the five languages.
        // Measured, not assumed - and pinned here so the doc comment above
        // `collect_fragments` cannot drift back to claiming Type-2.
        //
        // The positive half is the same test: identifiers and same-kind literals
        // ARE normalised away, which is what makes the detector worth having.
        fn shape(lang: Lang, src: &str) -> u64 {
            let mut parser = Parser::new();
            parser.set_language(&lang.tree_sitter_language()).unwrap();
            let tree = parser.parse(src, None).unwrap();
            let mut out = Vec::new();
            collect_fragments(tree.root_node(), 0, &mut out).0
        }
        // Renaming is invisible - the property the whole metric rests on.
        assert_eq!(
            shape(Lang::Python, "def a(x):\n    return x + 1\n"),
            shape(Lang::Python, "def b(y):\n    return y + 1\n"),
            "an identifier rename must NOT change the shape"
        );
        // A same-kind literal change is invisible too.
        assert_eq!(
            shape(Lang::Python, "def a(x):\n    return x + 1\n"),
            shape(Lang::Python, "def a(x):\n    return x + 2\n"),
            "a same-kind literal change must NOT change the shape"
        );
        // Layout is invisible - whitespace never becomes a node.
        assert_eq!(
            shape(Lang::Python, "def a(x):\n    return x + 1\n"),
            shape(Lang::Python, "def a(x):\n        return  x  +  1\n"),
            "reformatting must NOT change the shape"
        );
        // An operator IS significant - the anonymous token is hashed.
        assert_ne!(
            shape(Lang::Python, "def a(x):\n    return x + 1\n"),
            shape(Lang::Python, "def a(x):\n    return x - 1\n"),
            "`+` and `-` must not collide"
        );
        // And the documented ceiling: comments are NOT normalised away.
        for (name, lang, plain, commented) in [
            (
                "python", Lang::Python,
                "def a(x):\n    return x + 1\n",
                "def a(x):\n    # explain\n    return x + 1\n",
            ),
            (
                "python-docstring", Lang::Python,
                "def a(x):\n    return x + 1\n",
                "def a(x):\n    \"doc\"\n    return x + 1\n",
            ),
            (
                "php", Lang::Php,
                "<?php\nfunction a($x) { return $x + 1; }\n",
                "<?php\n/** d */\nfunction a($x) { return $x + 1; }\n",
            ),
            (
                "ruby", Lang::Ruby,
                "def a(x)\n  x + 1\nend\n",
                "# c\ndef a(x)\n  x + 1\nend\n",
            ),
            (
                "ts", Lang::Ts,
                "function a(x: number) { return x + 1; }\n",
                "// c\nfunction a(x: number) { return x + 1; }\n",
            ),
            (
                "rust", Lang::Rust,
                "fn a(x: i32) -> i32 { x + 1 }\n",
                "/// doc\nfn a(x: i32) -> i32 { x + 1 }\n",
            ),
        ] {
            assert_ne!(
                shape(lang, plain),
                shape(lang, commented),
                "{name}: comments ARE hashed today. If this now matches, the \
                 engine has become comment-blind and the doc comment on the \
                 duplication section must be updated to say so - it is a \
                 change in which clones get reported, not a free win."
            );
        }
    }

    // ---- The parity lock against the REAL Python master reference ------------

    fn locate_tina4_python() -> Option<PathBuf> {
        if let Ok(d) = std::env::var("TINA4_PYTHON_DIR") {
            let p = PathBuf::from(d);
            if p.is_dir() {
                return Some(p);
            }
        }
        let candidate = manifest().join("../tina4-python");
        if candidate.join("tina4_python/dev_admin/metrics.py").is_file() {
            return Some(candidate);
        }
        None
    }

    fn python_bin(dir: &Path) -> Option<PathBuf> {
        let venv = dir.join(".venv/bin/python");
        if venv.is_file() {
            return Some(venv);
        }
        for name in ["python3", "python"] {
            if let Ok(p) = which::which(name) {
                return Some(p);
            }
        }
        None
    }

    #[derive(serde::Deserialize)]
    struct PyRef {
        loc: usize,
        complexity: u32,
        functions: usize,
        maintainability: f64,
        avg_complexity: f64,
        #[serde(default)]
        error: Option<String>,
    }

    /// No mocks: shells out to the REAL tina4-python metrics.py and asserts the
    /// Rust engine lands on the same numbers for the same real source files.
    #[test]
    fn parity_matches_python_master() {
        let Some(dir) = locate_tina4_python() else {
            eprintln!("SKIP parity: tina4-python not found (set TINA4_PYTHON_DIR)");
            return;
        };
        let Some(py) = python_bin(&dir) else {
            eprintln!("SKIP parity: no python interpreter available");
            return;
        };
        let driver = manifest().join("tests/parity_reference.py");

        for name in ["sample_container.py", "sample_metrics.py"] {
            let fixture = manifest().join("tests/fixtures").join(name);
            let out = Command::new(&py)
                .arg(&driver)
                .arg(&fixture)
                .env("TINA4_PYTHON_DIR", &dir)
                .output()
                .expect("failed to run parity_reference.py");
            let stdout = String::from_utf8_lossy(&out.stdout);
            let reference: PyRef = serde_json::from_str(stdout.trim())
                .unwrap_or_else(|_| panic!("bad driver output for {name}: {stdout}"));
            if let Some(err) = &reference.error {
                eprintln!("SKIP parity for {name}: {err}");
                return;
            }

            let src = read_fixture(name);
            let (fm, _fns) = analyze_py(&src);

            eprintln!(
                "PARITY {name}: py(loc={},cc={},fn={},mi={},avg={}) rust(loc={},cc={},fn={},mi={},avg={})",
                reference.loc, reference.complexity, reference.functions,
                reference.maintainability, reference.avg_complexity,
                fm.loc, fm.complexity, fm.functions, fm.maintainability, fm.avg_complexity
            );

            assert_eq!(fm.loc, reference.loc, "{name}: LOC must match exactly");
            assert_eq!(fm.complexity, reference.complexity, "{name}: total CC must match exactly");
            assert_eq!(fm.functions, reference.functions, "{name}: function count must match exactly");
            assert!(
                (fm.avg_complexity - reference.avg_complexity).abs() <= 0.01,
                "{name}: avg complexity {} vs {}", fm.avg_complexity, reference.avg_complexity
            );
            // MI matched EXACTLY to the reported 0.1 in development; the 0.15
            // tolerance is a guard against float-rounding drift across platforms.
            assert!(
                (fm.maintainability - reference.maintainability).abs() <= 0.15,
                "{name}: MI {} vs {}", fm.maintainability, reference.maintainability
            );
        }
    }

    // ── Test detection: the class-symbol stage ──────────────────────────
    //
    // A test that imports through the package root never mentions the module's
    // file stem, so stages 1 and 2 both miss it. Before this stage existed,
    // `tina4 metrics` reported a well-tested module as untested and raised an
    // "untested" offender for it.

    #[test]
    fn declared_type_names_finds_a_short_class() {
        let names = declared_type_names("class ORM:\n    def save(self):\n        return True\n", Lang::Python);
        assert!(names.contains(&"ORM".to_string()), "got {names:?}");
    }

    #[test]
    fn a_three_char_class_referenced_by_a_test_counts_as_tested() {
        // No length floor. A >3-char gate was the bug the Python master fixed:
        // it excluded exactly the short framework types that matter (ORM, Api, Log).
        let idx = TestIndex {
            file_names: ["test_models.py".to_string()].into_iter().collect(),
            contents: vec!["from src import ORM\n\ndef test_save():\n    assert ORM().save()\n".to_string()],
        };
        let declared = vec!["ORM".to_string()];
        assert!(
            module_has_tests(Path::new("src/orm.py"), &idx, &declared),
            "the ORM class symbol is the only signal here and it is a real one"
        );
    }

    #[test]
    fn an_unreferenced_class_is_still_untested() {
        // The negative half. Without it this stage could return true for
        // anything and the test above would still pass.
        let idx = TestIndex {
            file_names: ["test_other.py".to_string()].into_iter().collect(),
            contents: vec!["def test_nothing():\n    assert True\n".to_string()],
        };
        let declared = vec!["Widget".to_string()];
        assert!(
            !module_has_tests(Path::new("src/widget.py"), &idx, &declared),
            "a class no test mentions must not be reported as tested"
        );
    }

    #[test]
    fn a_symbol_match_is_whole_identifier_only() {
        // Substring matching would let Order mark OrderItem as tested, and every
        // short name would collide constantly. This is what makes dropping the
        // length floor safe.
        assert!(mentions_symbol("assert ORM().save()", "ORM"));
        assert!(mentions_symbol("from src import ORM", "ORM"));
        assert!(!mentions_symbol("class ORMBase: pass", "ORM"));
        assert!(!mentions_symbol("x = MyORM()", "ORM"));
        assert!(!mentions_symbol("FORMAT = 1", "ORM"));
        assert!(mentions_symbol("Order(1)", "Order"));
        assert!(!mentions_symbol("OrderItem(1)", "Order"));
    }

    // ── PHPUnit's PascalCase test filename (Metrics.php -> MetricsTest.php) ──
    // Every other stage-1 pattern uses a separator, so this convention matched
    // nothing and EVERY PHP source file raised a false "untested" offender.

    #[test]
    fn a_typescript_interface_is_a_declared_type() {
        // An interface-only module has no class, so without this the module was
        // reported UNTESTED however plainly a test referenced it.
        let names = declared_type_names(
            "export interface WidgetConnection {\n  id: string;\n}\n", Lang::Ts);
        assert!(names.contains(&"WidgetConnection".to_string()), "got {names:?}");
    }

    #[test]
    fn a_typescript_interface_referenced_by_a_test_counts_as_tested() {
        // The TS inline-type-import idiom: the reference sits mid-line on a
        // `const` declaration, so no import-line rule can see it. The type NAME
        // is the only signal, which is exactly what stage 3 is for.
        let idx = TestIndex {
            file_names: ["widget.test.ts".to_string()].into_iter().collect(),
            contents: vec!["const c: import(\"../src/widgetConnection.ts\").WidgetConnection = { id: \"1\" };".to_string()],
        };
        let declared = vec!["WidgetConnection".to_string()];
        assert!(module_has_tests(Path::new("src/widgetConnection.ts"), &idx, &declared));
    }

    #[test]
    fn an_unreferenced_interface_is_still_untested() {
        let idx = TestIndex {
            file_names: ["other.test.ts".to_string()].into_iter().collect(),
            contents: vec!["const x = 1;".to_string()],
        };
        let declared = vec!["SoloIface".to_string()];
        assert!(!module_has_tests(Path::new("src/ctrlIface.ts"), &idx, &declared));
    }

    #[test]
    fn a_phpunit_pascalcase_test_file_counts_as_tested() {
        let idx = TestIndex {
            file_names: ["metricstest.php".to_string()].into_iter().collect(),
            contents: vec!["<?php\nclass MetricsTest {}\n".to_string()],
        };
        assert!(
            module_has_tests(Path::new("Tina4/Metrics.php"), &idx, &[]),
            "MetricsTest.php is the dedicated test for Metrics.php"
        );
    }

    #[test]
    fn a_pascalcase_match_is_anchored_not_a_substring() {
        // The negative half: `Base` must NOT be marked tested by DatabaseTest.php.
        // starts_with (not contains) is what makes the separator-less pattern safe.
        let idx = TestIndex {
            file_names: ["databasetest.php".to_string()].into_iter().collect(),
            contents: vec!["<?php\nclass DatabaseTest {}\n".to_string()],
        };
        assert!(
            !module_has_tests(Path::new("Tina4/Base.php"), &idx, &[]),
            "DatabaseTest.php tests Database, not Base"
        );
    }


}