exspec-lang-typescript 0.2.0

TypeScript language support for exspec
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
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
use std::collections::{HashMap, HashSet};
use std::path::{Path, PathBuf};
use std::sync::OnceLock;

use streaming_iterator::StreamingIterator;
use tree_sitter::{Node, Query, QueryCursor};

use super::{cached_query, TypeScriptExtractor};

const PRODUCTION_FUNCTION_QUERY: &str = include_str!("../queries/production_function.scm");
static PRODUCTION_FUNCTION_QUERY_CACHE: OnceLock<Query> = OnceLock::new();

const IMPORT_MAPPING_QUERY: &str = include_str!("../queries/import_mapping.scm");
static IMPORT_MAPPING_QUERY_CACHE: OnceLock<Query> = OnceLock::new();

const RE_EXPORT_QUERY: &str = include_str!("../queries/re_export.scm");
static RE_EXPORT_QUERY_CACHE: OnceLock<Query> = OnceLock::new();

const EXPORTED_SYMBOL_QUERY: &str = include_str!("../queries/exported_symbol.scm");
static EXPORTED_SYMBOL_QUERY_CACHE: OnceLock<Query> = OnceLock::new();

/// Maximum depth for barrel re-export resolution (NestJS measured max 2 hops).
const MAX_BARREL_DEPTH: usize = 3;

/// A production (non-test) function or method extracted from source code.
#[derive(Debug, Clone, PartialEq)]
pub struct ProductionFunction {
    pub name: String,
    pub file: String,
    pub line: usize,
    pub class_name: Option<String>,
    pub is_exported: bool,
}

/// A route extracted from a NestJS controller.
#[derive(Debug, Clone, PartialEq)]
pub struct Route {
    pub http_method: String,
    pub path: String,
    pub handler_name: String,
    pub class_name: String,
    pub file: String,
    pub line: usize,
}

/// A gap-relevant decorator extracted from source code.
#[derive(Debug, Clone, PartialEq)]
pub struct DecoratorInfo {
    pub name: String,
    pub arguments: Vec<String>,
    pub target_name: String,
    pub class_name: String,
    pub file: String,
    pub line: usize,
}

#[derive(Debug, Clone, PartialEq)]
pub struct FileMapping {
    pub production_file: String,
    pub test_files: Vec<String>,
    pub strategy: MappingStrategy,
}

#[derive(Debug, Clone, PartialEq)]
pub enum MappingStrategy {
    FileNameConvention,
    ImportTracing,
}

/// An import statement extracted from a TypeScript source file.
#[derive(Debug, Clone, PartialEq)]
pub struct ImportMapping {
    pub symbol_name: String,
    pub module_specifier: String,
    pub file: String,
    pub line: usize,
    /// All symbol names imported from this module specifier (same-statement grouping).
    /// For `import { Foo, Bar } from './module'`, both Foo and Bar appear here.
    pub symbols: Vec<String>,
}

/// A re-export statement extracted from a barrel (index.ts) file.
#[derive(Debug, Clone, PartialEq)]
pub struct BarrelReExport {
    /// Named symbols re-exported (empty for wildcard).
    pub symbols: Vec<String>,
    /// The module specifier of the re-export source.
    pub from_specifier: String,
    /// True if this is a wildcard re-export (`export * from '...'`).
    pub wildcard: bool,
}

/// HTTP method decorators recognized as route indicators.
const HTTP_METHODS: &[&str] = &["Get", "Post", "Put", "Patch", "Delete", "Head", "Options"];

/// Decorators relevant to gap analysis (guard/pipe/validation).
const GAP_RELEVANT_DECORATORS: &[&str] = &[
    "UseGuards",
    "UsePipes",
    "IsEmail",
    "IsNotEmpty",
    "MinLength",
    "MaxLength",
    "IsOptional",
    "IsString",
    "IsNumber",
    "IsInt",
    "IsBoolean",
    "IsDate",
    "IsEnum",
    "IsArray",
    "ValidateNested",
    "Min",
    "Max",
    "Matches",
    "IsUrl",
    "IsUUID",
];

impl TypeScriptExtractor {
    pub fn map_test_files(
        &self,
        production_files: &[String],
        test_files: &[String],
    ) -> Vec<FileMapping> {
        let mut tests_by_key: HashMap<(String, String), Vec<String>> = HashMap::new();

        for test_file in test_files {
            let Some(stem) = test_stem(test_file) else {
                continue;
            };
            let directory = Path::new(test_file)
                .parent()
                .map(|parent| parent.to_string_lossy().into_owned())
                .unwrap_or_default();

            tests_by_key
                .entry((directory, stem.to_string()))
                .or_default()
                .push(test_file.clone());
        }

        production_files
            .iter()
            .map(|production_file| {
                let test_matches = production_stem(production_file)
                    .and_then(|stem| {
                        let directory = Path::new(production_file)
                            .parent()
                            .map(|parent| parent.to_string_lossy().into_owned())
                            .unwrap_or_default();
                        tests_by_key.get(&(directory, stem.to_string())).cloned()
                    })
                    .unwrap_or_default();

                FileMapping {
                    production_file: production_file.clone(),
                    test_files: test_matches,
                    strategy: MappingStrategy::FileNameConvention,
                }
            })
            .collect()
    }

    /// Extract NestJS routes from a controller source file.
    pub fn extract_routes(&self, source: &str, file_path: &str) -> Vec<Route> {
        let mut parser = Self::parser();
        let tree = match parser.parse(source, None) {
            Some(t) => t,
            None => return Vec::new(),
        };
        let source_bytes = source.as_bytes();

        let mut routes = Vec::new();

        // Find all class declarations (including exported ones)
        for node in iter_children(tree.root_node()) {
            // Find class_declaration and its parent (for decorator search)
            let (container, class_node) = match node.kind() {
                "export_statement" => {
                    let cls = node
                        .named_children(&mut node.walk())
                        .find(|c| c.kind() == "class_declaration");
                    match cls {
                        Some(c) => (node, c),
                        None => continue,
                    }
                }
                "class_declaration" => (node, node),
                _ => continue,
            };

            // @Controller decorator may be on container (export_statement) or class_declaration
            let (base_path, class_name) =
                match extract_controller_info(container, class_node, source_bytes) {
                    Some(info) => info,
                    None => continue,
                };

            let class_body = match class_node.child_by_field_name("body") {
                Some(b) => b,
                None => continue,
            };

            let mut decorator_acc: Vec<Node> = Vec::new();
            for child in iter_children(class_body) {
                match child.kind() {
                    "decorator" => decorator_acc.push(child),
                    "method_definition" => {
                        let handler_name = child
                            .child_by_field_name("name")
                            .and_then(|n| n.utf8_text(source_bytes).ok())
                            .unwrap_or("")
                            .to_string();
                        let line = child.start_position().row + 1;

                        for dec in &decorator_acc {
                            if let Some((dec_name, dec_arg)) =
                                extract_decorator_call(*dec, source_bytes)
                            {
                                if HTTP_METHODS.contains(&dec_name.as_str()) {
                                    let sub_path = dec_arg.unwrap_or_default();
                                    routes.push(Route {
                                        http_method: dec_name.to_uppercase(),
                                        path: normalize_path(&base_path, &sub_path),
                                        handler_name: handler_name.clone(),
                                        class_name: class_name.clone(),
                                        file: file_path.to_string(),
                                        line,
                                    });
                                }
                            }
                        }
                        decorator_acc.clear();
                    }
                    _ => {}
                }
            }
        }

        routes
    }

    /// Extract gap-relevant decorators (guards, pipes, validators) from source.
    pub fn extract_decorators(&self, source: &str, file_path: &str) -> Vec<DecoratorInfo> {
        let mut parser = Self::parser();
        let tree = match parser.parse(source, None) {
            Some(t) => t,
            None => return Vec::new(),
        };
        let source_bytes = source.as_bytes();

        let mut decorators = Vec::new();

        for node in iter_children(tree.root_node()) {
            let (container, class_node) = match node.kind() {
                "export_statement" => {
                    let cls = node
                        .named_children(&mut node.walk())
                        .find(|c| c.kind() == "class_declaration");
                    match cls {
                        Some(c) => (node, c),
                        None => continue,
                    }
                }
                "class_declaration" => (node, node),
                _ => continue,
            };

            let class_name = class_node
                .child_by_field_name("name")
                .and_then(|n| n.utf8_text(source_bytes).ok())
                .unwrap_or("")
                .to_string();

            // BLOCK 1 fix: extract class-level gap-relevant decorators
            // Decorators on the class/container (e.g., @UseGuards at class level)
            let class_level_decorators: Vec<Node> = find_decorators_on_node(container, class_node);
            collect_gap_decorators(
                &class_level_decorators,
                &class_name, // target_name = class name for class-level
                &class_name,
                file_path,
                source_bytes,
                &mut decorators,
            );

            let class_body = match class_node.child_by_field_name("body") {
                Some(b) => b,
                None => continue,
            };

            let mut decorator_acc: Vec<Node> = Vec::new();
            for child in iter_children(class_body) {
                match child.kind() {
                    "decorator" => decorator_acc.push(child),
                    "method_definition" => {
                        let method_name = child
                            .child_by_field_name("name")
                            .and_then(|n| n.utf8_text(source_bytes).ok())
                            .unwrap_or("")
                            .to_string();

                        collect_gap_decorators(
                            &decorator_acc,
                            &method_name,
                            &class_name,
                            file_path,
                            source_bytes,
                            &mut decorators,
                        );
                        decorator_acc.clear();
                    }
                    // DTO field definitions: decorators are children of the field node
                    "public_field_definition" => {
                        let field_name = child
                            .child_by_field_name("name")
                            .and_then(|n| n.utf8_text(source_bytes).ok())
                            .unwrap_or("")
                            .to_string();

                        let field_decorators: Vec<Node> = iter_children(child)
                            .filter(|c| c.kind() == "decorator")
                            .collect();
                        collect_gap_decorators(
                            &field_decorators,
                            &field_name,
                            &class_name,
                            file_path,
                            source_bytes,
                            &mut decorators,
                        );
                        decorator_acc.clear();
                    }
                    _ => {}
                }
            }
        }

        decorators
    }

    /// Extract all production functions/methods from TypeScript source code.
    pub fn extract_production_functions(
        &self,
        source: &str,
        file_path: &str,
    ) -> Vec<ProductionFunction> {
        let mut parser = Self::parser();
        let tree = match parser.parse(source, None) {
            Some(t) => t,
            None => return Vec::new(),
        };

        let query = cached_query(&PRODUCTION_FUNCTION_QUERY_CACHE, PRODUCTION_FUNCTION_QUERY);
        let mut cursor = QueryCursor::new();
        let source_bytes = source.as_bytes();

        let idx_name = query
            .capture_index_for_name("name")
            .expect("@name capture not found in production_function.scm");
        let idx_exported_function = query
            .capture_index_for_name("exported_function")
            .expect("@exported_function capture not found");
        let idx_function = query
            .capture_index_for_name("function")
            .expect("@function capture not found");
        let idx_method = query
            .capture_index_for_name("method")
            .expect("@method capture not found");
        let idx_exported_arrow = query
            .capture_index_for_name("exported_arrow")
            .expect("@exported_arrow capture not found");
        let idx_arrow = query
            .capture_index_for_name("arrow")
            .expect("@arrow capture not found");

        // Use HashMap keyed by (line, name) to deduplicate overlapping patterns.
        // Exported patterns and non-exported patterns match the same node;
        // match order is implementation-dependent, so we upgrade is_exported
        // to true if any pattern marks it exported.
        let mut dedup: HashMap<(usize, String), ProductionFunction> = HashMap::new();

        let mut matches = cursor.matches(query, tree.root_node(), source_bytes);
        while let Some(m) = matches.next() {
            let name_node = match m.captures.iter().find(|c| c.index == idx_name) {
                Some(c) => c.node,
                None => continue,
            };
            let name = name_node.utf8_text(source_bytes).unwrap_or("").to_string();
            // Use the @name node's line for consistent deduplication across patterns
            let line = name_node.start_position().row + 1; // 1-indexed

            let (is_exported, class_name) = if m
                .captures
                .iter()
                .any(|c| c.index == idx_exported_function || c.index == idx_exported_arrow)
            {
                (true, None)
            } else if m
                .captures
                .iter()
                .any(|c| c.index == idx_function || c.index == idx_arrow)
            {
                (false, None)
            } else if let Some(c) = m.captures.iter().find(|c| c.index == idx_method) {
                let (cname, exported) = find_class_info(c.node, source_bytes);
                (exported, cname)
            } else {
                continue;
            };

            dedup
                .entry((line, name.clone()))
                .and_modify(|existing| {
                    if is_exported {
                        existing.is_exported = true;
                    }
                })
                .or_insert(ProductionFunction {
                    name,
                    file: file_path.to_string(),
                    line,
                    class_name,
                    is_exported,
                });
        }

        let mut results: Vec<ProductionFunction> = dedup.into_values().collect();
        results.sort_by_key(|f| f.line);
        results
    }
}

/// Iterate over all children of a node (named + anonymous).
fn iter_children(node: Node) -> impl Iterator<Item = Node> {
    (0..node.child_count()).filter_map(move |i| node.child(i))
}

/// Extract @Controller base path and class name.
/// `container` is the node that holds decorators (export_statement or class_declaration).
/// `class_node` is the class_declaration itself.
fn extract_controller_info(
    container: Node,
    class_node: Node,
    source: &[u8],
) -> Option<(String, String)> {
    let class_name = class_node
        .child_by_field_name("name")
        .and_then(|n| n.utf8_text(source).ok())?
        .to_string();

    // Look for @Controller decorator in both container and class_node
    for search_node in [container, class_node] {
        for i in 0..search_node.child_count() {
            let child = match search_node.child(i) {
                Some(c) => c,
                None => continue,
            };
            if child.kind() != "decorator" {
                continue;
            }
            if let Some((name, arg)) = extract_decorator_call(child, source) {
                if name == "Controller" {
                    let base_path = arg.unwrap_or_default();
                    return Some((base_path, class_name));
                }
            }
        }
    }
    None
}

/// Collect gap-relevant decorators from an accumulator into the output vec.
fn collect_gap_decorators(
    decorator_acc: &[Node],
    target_name: &str,
    class_name: &str,
    file_path: &str,
    source: &[u8],
    output: &mut Vec<DecoratorInfo>,
) {
    for dec in decorator_acc {
        if let Some((dec_name, _)) = extract_decorator_call(*dec, source) {
            if GAP_RELEVANT_DECORATORS.contains(&dec_name.as_str()) {
                let args = extract_decorator_args(*dec, source);
                output.push(DecoratorInfo {
                    name: dec_name,
                    arguments: args,
                    target_name: target_name.to_string(),
                    class_name: class_name.to_string(),
                    file: file_path.to_string(),
                    line: dec.start_position().row + 1,
                });
            }
        }
    }
}

/// Extract the name and first string argument from a decorator call.
/// Returns (name, Some(path)) for string literals, (name, Some("<dynamic>")) for
/// non-literal arguments (variables, objects), and (name, None) for no arguments.
fn extract_decorator_call(decorator_node: Node, source: &[u8]) -> Option<(String, Option<String>)> {
    for i in 0..decorator_node.child_count() {
        let child = match decorator_node.child(i) {
            Some(c) => c,
            None => continue,
        };

        match child.kind() {
            "call_expression" => {
                let func_node = child.child_by_field_name("function")?;
                let name = func_node.utf8_text(source).ok()?.to_string();
                let args_node = child.child_by_field_name("arguments")?;

                if args_node.named_child_count() == 0 {
                    // No arguments: @Get()
                    return Some((name, None));
                }
                // Try first string argument
                let first_string = find_first_string_arg(args_node, source);
                if first_string.is_some() {
                    return Some((name, first_string));
                }
                // Non-literal argument (variable, object, etc.): mark as dynamic
                return Some((name, Some("<dynamic>".to_string())));
            }
            "identifier" => {
                let name = child.utf8_text(source).ok()?.to_string();
                return Some((name, None));
            }
            _ => {}
        }
    }
    None
}

/// Extract all identifier arguments from a decorator call.
/// e.g., @UseGuards(AuthGuard, RoleGuard) -> ["AuthGuard", "RoleGuard"]
fn extract_decorator_args(decorator_node: Node, source: &[u8]) -> Vec<String> {
    let mut args = Vec::new();
    for i in 0..decorator_node.child_count() {
        let child = match decorator_node.child(i) {
            Some(c) => c,
            None => continue,
        };
        if child.kind() == "call_expression" {
            if let Some(args_node) = child.child_by_field_name("arguments") {
                for j in 0..args_node.named_child_count() {
                    if let Some(arg) = args_node.named_child(j) {
                        if let Ok(text) = arg.utf8_text(source) {
                            args.push(text.to_string());
                        }
                    }
                }
            }
        }
    }
    args
}

/// Find the first string literal argument in an arguments node.
fn find_first_string_arg(args_node: Node, source: &[u8]) -> Option<String> {
    for i in 0..args_node.named_child_count() {
        let arg = args_node.named_child(i)?;
        if arg.kind() == "string" {
            let text = arg.utf8_text(source).ok()?;
            // Strip quotes
            let stripped = text.trim_matches(|c| c == '\'' || c == '"');
            if !stripped.is_empty() {
                return Some(stripped.to_string());
            }
        }
    }
    None
}

/// Normalize and combine base path and sub path.
/// e.g., ("users", ":id") -> "/users/:id"
/// e.g., ("", "health") -> "/health"
/// e.g., ("api/v1/users", "") -> "/api/v1/users"
fn normalize_path(base: &str, sub: &str) -> String {
    let base = base.trim_matches('/');
    let sub = sub.trim_matches('/');
    match (base.is_empty(), sub.is_empty()) {
        (true, true) => "/".to_string(),
        (true, false) => format!("/{sub}"),
        (false, true) => format!("/{base}"),
        (false, false) => format!("/{base}/{sub}"),
    }
}

/// Collect decorator nodes from both container and class_node.
/// For `export class`, decorators are on the export_statement, not class_declaration.
fn find_decorators_on_node<'a>(container: Node<'a>, class_node: Node<'a>) -> Vec<Node<'a>> {
    let mut result = Vec::new();
    for search_node in [container, class_node] {
        for i in 0..search_node.child_count() {
            if let Some(child) = search_node.child(i) {
                if child.kind() == "decorator" {
                    result.push(child);
                }
            }
        }
    }
    result
}

/// Walk up from a method_definition node to find the containing class name and export status.
fn find_class_info(method_node: Node, source: &[u8]) -> (Option<String>, bool) {
    let mut current = method_node.parent();
    while let Some(node) = current {
        if node.kind() == "class_body" {
            if let Some(class_node) = node.parent() {
                let class_kind = class_node.kind();
                if class_kind == "class_declaration"
                    || class_kind == "class"
                    || class_kind == "abstract_class_declaration"
                {
                    let class_name = class_node
                        .child_by_field_name("name")
                        .and_then(|n| n.utf8_text(source).ok())
                        .map(|s| s.to_string());

                    // Check if class is inside an export_statement
                    let is_exported = class_node
                        .parent()
                        .is_some_and(|p| p.kind() == "export_statement");

                    return (class_name, is_exported);
                }
            }
        }
        current = node.parent();
    }
    (None, false)
}

/// Check if a symbol node belongs to a type-only import.
/// Handles both `import type { X }` (statement-level) and `import { type X }` (specifier-level).
fn is_type_only_import(symbol_node: Node) -> bool {
    // Case 1: `import { type X }` — import_specifier has a "type" child
    let parent = symbol_node.parent();
    if let Some(p) = parent {
        if p.kind() == "import_specifier" {
            for i in 0..p.child_count() {
                if let Some(child) = p.child(i) {
                    if child.kind() == "type" {
                        return true;
                    }
                }
            }
        }
    }

    // Case 2: `import type { X }` — import_statement has a "type" child (before import_clause)
    // Walk up to import_statement
    let mut current = Some(symbol_node);
    while let Some(node) = current {
        if node.kind() == "import_statement" {
            for i in 0..node.child_count() {
                if let Some(child) = node.child(i) {
                    if child.kind() == "type" {
                        return true;
                    }
                }
            }
            break;
        }
        current = node.parent();
    }
    false
}

/// Extract import statements from TypeScript source.
/// Returns only relative imports (starting with "." or ".."); npm packages are excluded.
impl TypeScriptExtractor {
    pub fn extract_imports(&self, source: &str, file_path: &str) -> Vec<ImportMapping> {
        let mut parser = Self::parser();
        let tree = match parser.parse(source, None) {
            Some(t) => t,
            None => return Vec::new(),
        };
        let source_bytes = source.as_bytes();
        let query = cached_query(&IMPORT_MAPPING_QUERY_CACHE, IMPORT_MAPPING_QUERY);
        let symbol_idx = query.capture_index_for_name("symbol_name").unwrap();
        let specifier_idx = query.capture_index_for_name("module_specifier").unwrap();

        let mut cursor = QueryCursor::new();
        let mut matches = cursor.matches(query, tree.root_node(), source_bytes);
        let mut result = Vec::new();

        while let Some(m) = matches.next() {
            let mut symbol_node = None;
            let mut symbol = None;
            let mut specifier = None;
            let mut symbol_line = 0usize;
            for cap in m.captures {
                if cap.index == symbol_idx {
                    symbol_node = Some(cap.node);
                    symbol = Some(cap.node.utf8_text(source_bytes).unwrap_or(""));
                    symbol_line = cap.node.start_position().row + 1;
                } else if cap.index == specifier_idx {
                    specifier = Some(cap.node.utf8_text(source_bytes).unwrap_or(""));
                }
            }
            if let (Some(sym), Some(spec)) = (symbol, specifier) {
                // Filter: only relative paths (./ or ../)
                if !spec.starts_with("./") && !spec.starts_with("../") {
                    continue;
                }

                // Filter: skip type-only imports
                // `import type { X }` → import_statement has "type" keyword child
                // `import { type X }` → import_specifier has "type" keyword child
                if let Some(snode) = symbol_node {
                    if is_type_only_import(snode) {
                        continue;
                    }
                }

                result.push(ImportMapping {
                    symbol_name: sym.to_string(),
                    module_specifier: spec.to_string(),
                    file: file_path.to_string(),
                    line: symbol_line,
                    symbols: Vec::new(),
                });
            }
        }
        // Populate `symbols`: for each entry, collect all symbol_names that share the same
        // module_specifier in this file.
        let specifier_to_symbols: HashMap<String, Vec<String>> =
            result.iter().fold(HashMap::new(), |mut acc, im| {
                acc.entry(im.module_specifier.clone())
                    .or_default()
                    .push(im.symbol_name.clone());
                acc
            });
        for im in &mut result {
            im.symbols = specifier_to_symbols
                .get(&im.module_specifier)
                .cloned()
                .unwrap_or_default();
        }
        result
    }

    /// Extract all import specifiers from TypeScript source (including non-relative).
    /// Used for tsconfig alias resolution. Does NOT filter by relative-only.
    /// Returns deduplicated (specifier, symbols) pairs.
    pub fn extract_all_import_specifiers(&self, source: &str) -> Vec<(String, Vec<String>)> {
        let mut parser = Self::parser();
        let tree = match parser.parse(source, None) {
            Some(t) => t,
            None => return Vec::new(),
        };
        let source_bytes = source.as_bytes();
        let query = cached_query(&IMPORT_MAPPING_QUERY_CACHE, IMPORT_MAPPING_QUERY);
        let symbol_idx = query.capture_index_for_name("symbol_name").unwrap();
        let specifier_idx = query.capture_index_for_name("module_specifier").unwrap();

        let mut cursor = QueryCursor::new();
        let mut matches = cursor.matches(query, tree.root_node(), source_bytes);
        // Map specifier -> symbols
        let mut specifier_symbols: std::collections::HashMap<String, Vec<String>> =
            std::collections::HashMap::new();

        while let Some(m) = matches.next() {
            let mut symbol_node = None;
            let mut symbol = None;
            let mut specifier = None;
            for cap in m.captures {
                if cap.index == symbol_idx {
                    symbol_node = Some(cap.node);
                    symbol = Some(cap.node.utf8_text(source_bytes).unwrap_or(""));
                } else if cap.index == specifier_idx {
                    specifier = Some(cap.node.utf8_text(source_bytes).unwrap_or(""));
                }
            }
            if let (Some(sym), Some(spec)) = (symbol, specifier) {
                // Skip relative imports (already handled by extract_imports)
                if spec.starts_with("./") || spec.starts_with("../") {
                    continue;
                }
                // Skip type-only imports
                if let Some(snode) = symbol_node {
                    if is_type_only_import(snode) {
                        continue;
                    }
                }
                specifier_symbols
                    .entry(spec.to_string())
                    .or_default()
                    .push(sym.to_string());
            }
        }

        specifier_symbols.into_iter().collect()
    }

    /// Extract barrel re-export statements (`export { X } from '...'` / `export * from '...'`).
    pub fn extract_barrel_re_exports(&self, source: &str, _file_path: &str) -> Vec<BarrelReExport> {
        let mut parser = Self::parser();
        let tree = match parser.parse(source, None) {
            Some(t) => t,
            None => return Vec::new(),
        };
        let source_bytes = source.as_bytes();
        let query = cached_query(&RE_EXPORT_QUERY_CACHE, RE_EXPORT_QUERY);

        let symbol_idx = query.capture_index_for_name("symbol_name");
        let wildcard_idx = query.capture_index_for_name("wildcard");
        let specifier_idx = query
            .capture_index_for_name("from_specifier")
            .expect("@from_specifier capture not found in re_export.scm");

        let mut cursor = QueryCursor::new();
        let mut matches = cursor.matches(query, tree.root_node(), source_bytes);

        // Group by match: each match corresponds to one export statement pattern.
        // Named re-export produces one match per symbol; wildcard produces one match.
        // We use a HashMap keyed by (from_specifier, is_wildcard) to group named symbols.
        struct ReExportEntry {
            symbols: Vec<String>,
            wildcard: bool,
        }
        let mut grouped: HashMap<String, ReExportEntry> = HashMap::new();

        while let Some(m) = matches.next() {
            let mut from_spec = None;
            let mut sym_name = None;
            let mut is_wildcard = false;

            for cap in m.captures {
                if wildcard_idx == Some(cap.index) {
                    is_wildcard = true;
                } else if cap.index == specifier_idx {
                    from_spec = Some(cap.node.utf8_text(source_bytes).unwrap_or("").to_string());
                } else if symbol_idx == Some(cap.index) {
                    sym_name = Some(cap.node.utf8_text(source_bytes).unwrap_or("").to_string());
                }
            }

            let Some(spec) = from_spec else { continue };

            let entry = grouped.entry(spec).or_insert(ReExportEntry {
                symbols: Vec::new(),
                wildcard: false,
            });
            if is_wildcard {
                entry.wildcard = true;
            }
            if let Some(sym) = sym_name {
                if !sym.is_empty() && !entry.symbols.contains(&sym) {
                    entry.symbols.push(sym);
                }
            }
        }

        grouped
            .into_iter()
            .map(|(from_spec, entry)| BarrelReExport {
                symbols: entry.symbols,
                from_specifier: from_spec,
                wildcard: entry.wildcard,
            })
            .collect()
    }

    pub fn map_test_files_with_imports(
        &self,
        production_files: &[String],
        test_sources: &HashMap<String, String>,
        scan_root: &Path,
    ) -> Vec<FileMapping> {
        let test_file_list: Vec<String> = test_sources.keys().cloned().collect();

        // Layer 1: filename convention
        let mut mappings = self.map_test_files(production_files, &test_file_list);

        // Build canonical path -> production index lookup
        let canonical_root = match scan_root.canonicalize() {
            Ok(r) => r,
            Err(_) => return mappings,
        };
        let mut canonical_to_idx: HashMap<String, usize> = HashMap::new();
        for (idx, prod) in production_files.iter().enumerate() {
            if let Ok(canonical) = Path::new(prod).canonicalize() {
                canonical_to_idx.insert(canonical.to_string_lossy().into_owned(), idx);
            }
        }

        // Collect Layer 1 matched test files
        let layer1_matched: std::collections::HashSet<String> = mappings
            .iter()
            .flat_map(|m| m.test_files.iter().cloned())
            .collect();

        // Discover and parse tsconfig.json for alias resolution (Layer 2b)
        let tsconfig_paths =
            crate::tsconfig::discover_tsconfig(&canonical_root).and_then(|tsconfig_path| {
                let content = std::fs::read_to_string(&tsconfig_path)
                    .map_err(|e| {
                        eprintln!("[exspec] warning: failed to read tsconfig: {e}");
                    })
                    .ok()?;
                let tsconfig_dir = tsconfig_path.parent().unwrap_or(&canonical_root);
                crate::tsconfig::TsconfigPaths::from_str(&content, tsconfig_dir)
                    .or_else(|| {
                        eprintln!("[exspec] warning: failed to parse tsconfig paths, alias resolution disabled");
                        None
                    })
            });

        // Layer 2: import tracing for all test files (Layer 1 matched tests may
        // also import other production files not matched by filename convention)
        for (test_file, source) in test_sources {
            let imports = self.extract_imports(source, test_file);
            let from_file = Path::new(test_file);
            let mut matched_indices = std::collections::HashSet::new();

            // Helper: given a resolved file path, follow barrel re-exports if needed and
            // collect matching production-file indices.
            let collect_matches = |resolved: &str,
                                   symbols: &[String],
                                   indices: &mut HashSet<usize>| {
                if is_barrel_file(resolved) {
                    let barrel_path = PathBuf::from(resolved);
                    let resolved_files =
                        resolve_barrel_exports(&barrel_path, symbols, &canonical_root);
                    for prod in resolved_files {
                        let prod_str = prod.to_string_lossy().into_owned();
                        if !is_non_sut_helper(&prod_str, canonical_to_idx.contains_key(&prod_str)) {
                            if let Some(&idx) = canonical_to_idx.get(&prod_str) {
                                indices.insert(idx);
                            }
                        }
                    }
                } else if !is_non_sut_helper(resolved, canonical_to_idx.contains_key(resolved)) {
                    if let Some(&idx) = canonical_to_idx.get(resolved) {
                        indices.insert(idx);
                    }
                }
            };

            for import in &imports {
                if let Some(resolved) =
                    resolve_import_path(&import.module_specifier, from_file, &canonical_root)
                {
                    collect_matches(&resolved, &import.symbols, &mut matched_indices);
                }
            }

            // Layer 2b: tsconfig alias resolution
            if let Some(ref tc_paths) = tsconfig_paths {
                let alias_imports = self.extract_all_import_specifiers(source);
                for (specifier, symbols) in &alias_imports {
                    let Some(alias_base) = tc_paths.resolve_alias(specifier) else {
                        continue;
                    };
                    if let Some(resolved) =
                        resolve_absolute_base_to_file(&alias_base, &canonical_root)
                    {
                        collect_matches(&resolved, symbols, &mut matched_indices);
                    }
                }
            }

            for idx in matched_indices {
                // Avoid duplicates: skip if already added by Layer 1
                if !mappings[idx].test_files.contains(test_file) {
                    mappings[idx].test_files.push(test_file.clone());
                }
            }
        }

        // Update strategy: if a production file had no Layer 1 matches but has Layer 2 matches,
        // set strategy to ImportTracing
        for mapping in &mut mappings {
            let has_layer1 = mapping
                .test_files
                .iter()
                .any(|t| layer1_matched.contains(t));
            if !has_layer1 && !mapping.test_files.is_empty() {
                mapping.strategy = MappingStrategy::ImportTracing;
            }
        }

        mappings
    }
}

/// Resolve a module specifier to an absolute file path.
/// Returns None if the file does not exist or is outside scan_root.
pub fn resolve_import_path(
    module_specifier: &str,
    from_file: &Path,
    scan_root: &Path,
) -> Option<String> {
    // Canonicalize base_dir: use the parent directory of from_file.
    // If the parent directory exists (even if from_file itself doesn't), canonicalize it.
    // Otherwise fall back to the non-canonical parent for path arithmetic.
    let base_dir_raw = from_file.parent()?;
    let base_dir = base_dir_raw
        .canonicalize()
        .unwrap_or_else(|_| base_dir_raw.to_path_buf());
    // We must JOIN (not resolve) so that dotted module names like "user.service" are preserved:
    // appending ".ts" yields "user.service.ts", not "user.ts".
    let raw_path = base_dir.join(module_specifier);
    let canonical_root = scan_root.canonicalize().ok()?;
    resolve_absolute_base_to_file(&raw_path, &canonical_root)
}

/// Resolve an already-computed absolute base path to an actual TypeScript/JavaScript file.
///
/// Probes in order:
/// 1. Direct hit (when `base` already has a known TS/JS extension).
/// 2. Append each known extension (preserves dotted names, e.g. `user.service` → `user.service.ts`).
/// 3. Directory index fallback (`<base>/index.ts`, `<base>/index.tsx`).
///
/// Returns `None` if no existing file is found inside `canonical_root`.
fn resolve_absolute_base_to_file(base: &Path, canonical_root: &Path) -> Option<String> {
    const TS_EXTENSIONS: &[&str] = &["ts", "tsx", "js", "jsx"];
    let has_known_ext = base
        .extension()
        .and_then(|e| e.to_str())
        .is_some_and(|e| TS_EXTENSIONS.contains(&e));

    let candidates: Vec<PathBuf> = if has_known_ext {
        vec![base.to_path_buf()]
    } else {
        let base_str = base.as_os_str().to_string_lossy();
        TS_EXTENSIONS
            .iter()
            .map(|ext| PathBuf::from(format!("{base_str}.{ext}")))
            .collect()
    };

    for candidate in &candidates {
        if let Ok(canonical) = candidate.canonicalize() {
            if canonical.starts_with(canonical_root) {
                return Some(canonical.to_string_lossy().into_owned());
            }
        }
    }

    // Fallback: directory index
    if !has_known_ext {
        let base_str = base.as_os_str().to_string_lossy();
        let index_candidates = [
            PathBuf::from(format!("{base_str}/index.ts")),
            PathBuf::from(format!("{base_str}/index.tsx")),
        ];
        for candidate in &index_candidates {
            if let Ok(canonical) = candidate.canonicalize() {
                if canonical.starts_with(canonical_root) {
                    return Some(canonical.to_string_lossy().into_owned());
                }
            }
        }
    }

    None
}

/// Type definition file: *.enum.*, *.interface.*, *.exception.*
/// Returns true if the file has a suffix pattern indicating a type definition.
fn is_type_definition_file(file_path: &str) -> bool {
    let Some(file_name) = Path::new(file_path).file_name().and_then(|f| f.to_str()) else {
        return false;
    };
    if let Some(stem) = Path::new(file_name).file_stem().and_then(|s| s.to_str()) {
        for suffix in &[".enum", ".interface", ".exception"] {
            if stem.ends_with(suffix) && stem != &suffix[1..] {
                return true;
            }
        }
    }
    false
}

/// Returns true if the resolved file path is a helper/non-SUT file that should be
/// excluded from Layer 2 import tracing.
///
/// Filtered patterns:
/// - Exact filenames: `constants.*`, `index.*`
/// - Suffix patterns: `*.enum.*`, `*.interface.*`, `*.exception.*` (skipped when `is_known_production`)
/// - Test utility paths: files under `/test/` or `/__tests__/`
fn is_non_sut_helper(file_path: &str, is_known_production: bool) -> bool {
    // Test-utility paths: files under /test/ or /__tests__/ directories.
    // Uses segment-based matching to avoid false positives (e.g., "contest/src/foo.ts").
    // Note: Windows path separators are intentionally not handled; this tool targets Unix-style paths.
    if file_path
        .split('/')
        .any(|seg| seg == "test" || seg == "__tests__")
    {
        return true;
    }

    let Some(file_name) = Path::new(file_path).file_name().and_then(|f| f.to_str()) else {
        return false;
    };

    // Exact-match barrel/constant files
    if matches!(
        file_name,
        "constants.ts"
            | "constants.js"
            | "constants.tsx"
            | "constants.jsx"
            | "index.ts"
            | "index.js"
            | "index.tsx"
            | "index.jsx"
    ) {
        return true;
    }

    // Suffix-match: *.enum.*, *.interface.*, *.exception.*
    // When is_known_production=true, type definition files are bypassed
    // (they are valid SUT targets when listed in production_files).
    if !is_known_production && is_type_definition_file(file_path) {
        return true;
    }

    false
}

/// Returns true if the file path ends with `index.ts` or `index.tsx`.
fn is_barrel_file(path: &str) -> bool {
    let file_name = Path::new(path)
        .file_name()
        .and_then(|f| f.to_str())
        .unwrap_or("");
    file_name == "index.ts" || file_name == "index.tsx"
}

/// Check if a TypeScript file exports any of the given symbol names.
/// Used to filter wildcard re-export targets by requested symbols.
fn file_exports_any_symbol(file_path: &Path, symbols: &[String]) -> bool {
    if symbols.is_empty() {
        return true;
    }
    let source = match std::fs::read_to_string(file_path) {
        Ok(s) => s,
        Err(_) => return false,
    };
    let mut parser = TypeScriptExtractor::parser();
    let tree = match parser.parse(&source, None) {
        Some(t) => t,
        None => return false,
    };
    let query = cached_query(&EXPORTED_SYMBOL_QUERY_CACHE, EXPORTED_SYMBOL_QUERY);
    let symbol_idx = query
        .capture_index_for_name("symbol_name")
        .expect("@symbol_name capture not found in exported_symbol.scm");

    let mut cursor = QueryCursor::new();
    let source_bytes = source.as_bytes();
    let mut matches = cursor.matches(query, tree.root_node(), source_bytes);
    while let Some(m) = matches.next() {
        for cap in m.captures {
            if cap.index == symbol_idx {
                let name = cap.node.utf8_text(source_bytes).unwrap_or("");
                if symbols.iter().any(|s| s == name) {
                    return true;
                }
            }
        }
    }
    false
}

/// Resolve barrel re-exports starting from `barrel_path` for the given `symbols`.
/// Follows up to 3 hops, prevents cycles via `visited` set.
/// Returns the list of resolved non-barrel production file paths.
pub fn resolve_barrel_exports(
    barrel_path: &Path,
    symbols: &[String],
    scan_root: &Path,
) -> Vec<PathBuf> {
    let canonical_root = match scan_root.canonicalize() {
        Ok(r) => r,
        Err(_) => return Vec::new(),
    };
    let extractor = crate::TypeScriptExtractor::new();
    let mut visited: HashSet<PathBuf> = HashSet::new();
    let mut results: Vec<PathBuf> = Vec::new();
    resolve_barrel_exports_inner(
        barrel_path,
        symbols,
        scan_root,
        &canonical_root,
        &extractor,
        &mut visited,
        0,
        &mut results,
    );
    results
}

#[allow(clippy::too_many_arguments)]
fn resolve_barrel_exports_inner(
    barrel_path: &Path,
    symbols: &[String],
    scan_root: &Path,
    canonical_root: &Path,
    extractor: &crate::TypeScriptExtractor,
    visited: &mut HashSet<PathBuf>,
    depth: usize,
    results: &mut Vec<PathBuf>,
) {
    if depth >= MAX_BARREL_DEPTH {
        return;
    }

    let canonical_barrel = match barrel_path.canonicalize() {
        Ok(p) => p,
        Err(_) => return,
    };
    if !visited.insert(canonical_barrel) {
        return;
    }

    let source = match std::fs::read_to_string(barrel_path) {
        Ok(s) => s,
        Err(_) => return,
    };

    let re_exports = extractor.extract_barrel_re_exports(&source, &barrel_path.to_string_lossy());

    for re_export in &re_exports {
        // For named re-exports, skip if none of the requested symbols match.
        // When symbols is empty (e.g. wildcard import or no symbol info available),
        // treat as "match all" to be conservative — may over-resolve but avoids FN.
        if !re_export.wildcard {
            let has_match =
                symbols.is_empty() || symbols.iter().any(|s| re_export.symbols.contains(s));
            if !has_match {
                continue;
            }
        }

        if let Some(resolved_str) =
            resolve_import_path(&re_export.from_specifier, barrel_path, scan_root)
        {
            if is_barrel_file(&resolved_str) {
                resolve_barrel_exports_inner(
                    &PathBuf::from(&resolved_str),
                    symbols,
                    scan_root,
                    canonical_root,
                    extractor,
                    visited,
                    depth + 1,
                    results,
                );
            } else if !is_non_sut_helper(&resolved_str, false) {
                // For wildcard re-exports with known symbols, verify the target file
                // actually exports at least one of the requested symbols before including it.
                if !symbols.is_empty()
                    && re_export.wildcard
                    && !file_exports_any_symbol(Path::new(&resolved_str), symbols)
                {
                    continue;
                }
                if let Ok(canonical) = PathBuf::from(&resolved_str).canonicalize() {
                    if canonical.starts_with(canonical_root) && !results.contains(&canonical) {
                        results.push(canonical);
                    }
                }
            }
        }
    }
}

fn production_stem(path: &str) -> Option<&str> {
    Path::new(path).file_stem()?.to_str()
}

fn test_stem(path: &str) -> Option<&str> {
    let stem = Path::new(path).file_stem()?.to_str()?;
    stem.strip_suffix(".spec")
        .or_else(|| stem.strip_suffix(".test"))
}

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

    fn fixture(name: &str) -> String {
        let path = format!(
            "{}/tests/fixtures/typescript/observe/{}",
            env!("CARGO_MANIFEST_DIR").replace("/crates/lang-typescript", ""),
            name
        );
        std::fs::read_to_string(&path).unwrap_or_else(|e| panic!("failed to read {path}: {e}"))
    }

    // TC1: exported function declarations are extracted with is_exported: true
    #[test]
    fn exported_functions_extracted() {
        // Given: exported_functions.ts with `export function findAll()` and `export function findById()`
        let source = fixture("exported_functions.ts");
        let extractor = TypeScriptExtractor::new();

        // When: extract production functions
        let funcs = extractor.extract_production_functions(&source, "exported_functions.ts");

        // Then: findAll and findById are extracted with is_exported: true
        let exported: Vec<&ProductionFunction> = funcs.iter().filter(|f| f.is_exported).collect();
        let names: Vec<&str> = exported.iter().map(|f| f.name.as_str()).collect();
        assert!(names.contains(&"findAll"), "expected findAll in {names:?}");
        assert!(
            names.contains(&"findById"),
            "expected findById in {names:?}"
        );
    }

    // TC2: non-exported function has is_exported: false
    #[test]
    fn non_exported_function_has_flag_false() {
        // Given: exported_functions.ts with `function internalHelper()`
        let source = fixture("exported_functions.ts");
        let extractor = TypeScriptExtractor::new();

        // When: extract production functions
        let funcs = extractor.extract_production_functions(&source, "exported_functions.ts");

        // Then: internalHelper has is_exported: false
        let helper = funcs.iter().find(|f| f.name == "internalHelper");
        assert!(helper.is_some(), "expected internalHelper to be extracted");
        assert!(!helper.unwrap().is_exported);
    }

    // TC3: class methods include class_name
    #[test]
    fn class_methods_with_class_name() {
        // Given: class_methods.ts with class UsersController { findAll(), create(), validate() }
        let source = fixture("class_methods.ts");
        let extractor = TypeScriptExtractor::new();

        // When: extract production functions
        let funcs = extractor.extract_production_functions(&source, "class_methods.ts");

        // Then: findAll, create, validate have class_name: Some("UsersController")
        let controller_methods: Vec<&ProductionFunction> = funcs
            .iter()
            .filter(|f| f.class_name.as_deref() == Some("UsersController"))
            .collect();
        let names: Vec<&str> = controller_methods.iter().map(|f| f.name.as_str()).collect();
        assert!(names.contains(&"findAll"), "expected findAll in {names:?}");
        assert!(names.contains(&"create"), "expected create in {names:?}");
        assert!(
            names.contains(&"validate"),
            "expected validate in {names:?}"
        );
    }

    // TC4: exported class methods are is_exported: true, non-exported class methods are false
    #[test]
    fn exported_class_is_exported() {
        // Given: class_methods.ts with exported UsersController and non-exported InternalService
        let source = fixture("class_methods.ts");
        let extractor = TypeScriptExtractor::new();

        // When: extract production functions
        let funcs = extractor.extract_production_functions(&source, "class_methods.ts");

        // Then: UsersController methods → is_exported: true
        let controller_methods: Vec<&ProductionFunction> = funcs
            .iter()
            .filter(|f| f.class_name.as_deref() == Some("UsersController"))
            .collect();
        assert!(
            controller_methods.iter().all(|f| f.is_exported),
            "all UsersController methods should be exported"
        );

        // Then: InternalService methods → is_exported: false
        let internal_methods: Vec<&ProductionFunction> = funcs
            .iter()
            .filter(|f| f.class_name.as_deref() == Some("InternalService"))
            .collect();
        assert!(
            !internal_methods.is_empty(),
            "expected InternalService methods"
        );
        assert!(
            internal_methods.iter().all(|f| !f.is_exported),
            "all InternalService methods should not be exported"
        );
    }

    // TC5: arrow function exports are extracted with is_exported: true
    #[test]
    fn arrow_exports_extracted() {
        // Given: arrow_exports.ts with `export const findAll = () => ...`
        let source = fixture("arrow_exports.ts");
        let extractor = TypeScriptExtractor::new();

        // When: extract production functions
        let funcs = extractor.extract_production_functions(&source, "arrow_exports.ts");

        // Then: findAll, findById are is_exported: true
        let exported: Vec<&ProductionFunction> = funcs.iter().filter(|f| f.is_exported).collect();
        let names: Vec<&str> = exported.iter().map(|f| f.name.as_str()).collect();
        assert!(names.contains(&"findAll"), "expected findAll in {names:?}");
        assert!(
            names.contains(&"findById"),
            "expected findById in {names:?}"
        );
    }

    // TC6: non-exported arrow function has is_exported: false
    #[test]
    fn non_exported_arrow_flag_false() {
        // Given: arrow_exports.ts with `const internalFn = () => ...`
        let source = fixture("arrow_exports.ts");
        let extractor = TypeScriptExtractor::new();

        // When: extract production functions
        let funcs = extractor.extract_production_functions(&source, "arrow_exports.ts");

        // Then: internalFn has is_exported: false
        let internal = funcs.iter().find(|f| f.name == "internalFn");
        assert!(internal.is_some(), "expected internalFn to be extracted");
        assert!(!internal.unwrap().is_exported);
    }

    // TC7: mixed file extracts all types with correct export status
    #[test]
    fn mixed_file_all_types() {
        // Given: mixed.ts with function declarations, arrow functions, and class methods
        let source = fixture("mixed.ts");
        let extractor = TypeScriptExtractor::new();

        // When: extract production functions
        let funcs = extractor.extract_production_functions(&source, "mixed.ts");

        // Then: all functions are extracted
        let names: Vec<&str> = funcs.iter().map(|f| f.name.as_str()).collect();
        // Exported: getUser, createUser, UserService.findAll, UserService.deleteById
        assert!(names.contains(&"getUser"), "expected getUser in {names:?}");
        assert!(
            names.contains(&"createUser"),
            "expected createUser in {names:?}"
        );
        // Non-exported: formatName, validateInput, PrivateHelper.transform
        assert!(
            names.contains(&"formatName"),
            "expected formatName in {names:?}"
        );
        assert!(
            names.contains(&"validateInput"),
            "expected validateInput in {names:?}"
        );

        // Verify export status
        let get_user = funcs.iter().find(|f| f.name == "getUser").unwrap();
        assert!(get_user.is_exported);
        let format_name = funcs.iter().find(|f| f.name == "formatName").unwrap();
        assert!(!format_name.is_exported);

        // Verify class methods have class_name
        let find_all = funcs
            .iter()
            .find(|f| f.name == "findAll" && f.class_name.is_some())
            .unwrap();
        assert_eq!(find_all.class_name.as_deref(), Some("UserService"));
        assert!(find_all.is_exported);

        let transform = funcs.iter().find(|f| f.name == "transform").unwrap();
        assert_eq!(transform.class_name.as_deref(), Some("PrivateHelper"));
        assert!(!transform.is_exported);
    }

    // TC8: decorated methods (NestJS) are correctly extracted
    #[test]
    fn decorated_methods_extracted() {
        // Given: nestjs_controller.ts with @Get(), @Post(), @Delete() decorated methods
        let source = fixture("nestjs_controller.ts");
        let extractor = TypeScriptExtractor::new();

        // When: extract production functions
        let funcs = extractor.extract_production_functions(&source, "nestjs_controller.ts");

        // Then: findAll, create, remove are extracted with class_name and is_exported
        let names: Vec<&str> = funcs.iter().map(|f| f.name.as_str()).collect();
        assert!(names.contains(&"findAll"), "expected findAll in {names:?}");
        assert!(names.contains(&"create"), "expected create in {names:?}");
        assert!(names.contains(&"remove"), "expected remove in {names:?}");

        for func in &funcs {
            assert_eq!(func.class_name.as_deref(), Some("UsersController"));
            assert!(func.is_exported);
        }
    }

    // TC9: line numbers match actual source positions
    #[test]
    fn line_numbers_correct() {
        // Given: exported_functions.ts
        let source = fixture("exported_functions.ts");
        let extractor = TypeScriptExtractor::new();

        // When: extract production functions
        let funcs = extractor.extract_production_functions(&source, "exported_functions.ts");

        // Then: line numbers correspond to actual positions (1-indexed)
        let find_all = funcs.iter().find(|f| f.name == "findAll").unwrap();
        assert_eq!(find_all.line, 1, "findAll should be on line 1");

        let find_by_id = funcs.iter().find(|f| f.name == "findById").unwrap();
        assert_eq!(find_by_id.line, 5, "findById should be on line 5");

        let helper = funcs.iter().find(|f| f.name == "internalHelper").unwrap();
        assert_eq!(helper.line, 9, "internalHelper should be on line 9");
    }

    // TC10: empty source returns empty Vec
    #[test]
    fn empty_source_returns_empty() {
        // Given: empty source code
        let extractor = TypeScriptExtractor::new();

        // When: extract production functions from empty string
        let funcs = extractor.extract_production_functions("", "empty.ts");

        // Then: returns empty Vec
        assert!(funcs.is_empty());
    }

    // === Route Extraction Tests ===

    // RT1: basic NestJS controller routes
    #[test]
    fn basic_controller_routes() {
        // Given: nestjs_controller.ts with @Controller('users') + @Get, @Post, @Delete
        let source = fixture("nestjs_controller.ts");
        let extractor = TypeScriptExtractor::new();

        // When: extract routes
        let routes = extractor.extract_routes(&source, "nestjs_controller.ts");

        // Then: GET /users, POST /users, DELETE /users/:id
        assert_eq!(routes.len(), 3, "expected 3 routes, got {routes:?}");
        let methods: Vec<&str> = routes.iter().map(|r| r.http_method.as_str()).collect();
        assert!(methods.contains(&"GET"), "expected GET in {methods:?}");
        assert!(methods.contains(&"POST"), "expected POST in {methods:?}");
        assert!(
            methods.contains(&"DELETE"),
            "expected DELETE in {methods:?}"
        );

        let get_route = routes.iter().find(|r| r.http_method == "GET").unwrap();
        assert_eq!(get_route.path, "/users");

        let delete_route = routes.iter().find(|r| r.http_method == "DELETE").unwrap();
        assert_eq!(delete_route.path, "/users/:id");
    }

    // RT2: route path combination
    #[test]
    fn route_path_combination() {
        // Given: nestjs_routes_advanced.ts with @Controller('api/v1/users') + @Get('active')
        let source = fixture("nestjs_routes_advanced.ts");
        let extractor = TypeScriptExtractor::new();

        // When: extract routes
        let routes = extractor.extract_routes(&source, "nestjs_routes_advanced.ts");

        // Then: GET /api/v1/users/active
        let active = routes
            .iter()
            .find(|r| r.handler_name == "findActive")
            .unwrap();
        assert_eq!(active.http_method, "GET");
        assert_eq!(active.path, "/api/v1/users/active");
    }

    // RT3: controller with no path argument
    #[test]
    fn controller_no_path() {
        // Given: nestjs_empty_controller.ts with @Controller() + @Get('health')
        let source = fixture("nestjs_empty_controller.ts");
        let extractor = TypeScriptExtractor::new();

        // When: extract routes
        let routes = extractor.extract_routes(&source, "nestjs_empty_controller.ts");

        // Then: GET /health
        assert_eq!(routes.len(), 1, "expected 1 route, got {routes:?}");
        assert_eq!(routes[0].http_method, "GET");
        assert_eq!(routes[0].path, "/health");
    }

    // RT4: method without route decorator is not extracted
    #[test]
    fn method_without_route_decorator() {
        // Given: nestjs_empty_controller.ts with helperMethod() (no decorator)
        let source = fixture("nestjs_empty_controller.ts");
        let extractor = TypeScriptExtractor::new();

        // When: extract routes
        let routes = extractor.extract_routes(&source, "nestjs_empty_controller.ts");

        // Then: helperMethod is not in routes
        let helper = routes.iter().find(|r| r.handler_name == "helperMethod");
        assert!(helper.is_none(), "helperMethod should not be a route");
    }

    // RT5: all HTTP methods
    #[test]
    fn all_http_methods() {
        // Given: nestjs_routes_advanced.ts with Get, Post, Put, Patch, Delete, Head, Options
        let source = fixture("nestjs_routes_advanced.ts");
        let extractor = TypeScriptExtractor::new();

        // When: extract routes
        let routes = extractor.extract_routes(&source, "nestjs_routes_advanced.ts");

        // Then: 9 routes (Get appears 3 times)
        assert_eq!(routes.len(), 9, "expected 9 routes, got {routes:?}");
        let methods: Vec<&str> = routes.iter().map(|r| r.http_method.as_str()).collect();
        assert!(methods.contains(&"GET"));
        assert!(methods.contains(&"POST"));
        assert!(methods.contains(&"PUT"));
        assert!(methods.contains(&"PATCH"));
        assert!(methods.contains(&"DELETE"));
        assert!(methods.contains(&"HEAD"));
        assert!(methods.contains(&"OPTIONS"));
    }

    // RT6: UseGuards decorator extraction
    #[test]
    fn use_guards_decorator() {
        // Given: nestjs_guards_pipes.ts with @UseGuards(AuthGuard)
        let source = fixture("nestjs_guards_pipes.ts");
        let extractor = TypeScriptExtractor::new();

        // When: extract decorators
        let decorators = extractor.extract_decorators(&source, "nestjs_guards_pipes.ts");

        // Then: UseGuards with AuthGuard
        let guards: Vec<&DecoratorInfo> = decorators
            .iter()
            .filter(|d| d.name == "UseGuards")
            .collect();
        assert!(!guards.is_empty(), "expected UseGuards decorators");
        let auth_guard = guards
            .iter()
            .find(|d| d.arguments.contains(&"AuthGuard".to_string()));
        assert!(auth_guard.is_some(), "expected AuthGuard argument");
    }

    // RT7: only gap-relevant decorators (UseGuards, not Delete)
    #[test]
    fn multiple_decorators_on_method() {
        // Given: nestjs_controller.ts with @Delete(':id') @UseGuards(AuthGuard) on remove()
        let source = fixture("nestjs_controller.ts");
        let extractor = TypeScriptExtractor::new();

        // When: extract decorators
        let decorators = extractor.extract_decorators(&source, "nestjs_controller.ts");

        // Then: UseGuards only (Delete is a route decorator, not gap-relevant)
        let names: Vec<&str> = decorators.iter().map(|d| d.name.as_str()).collect();
        assert!(
            names.contains(&"UseGuards"),
            "expected UseGuards in {names:?}"
        );
        assert!(
            !names.contains(&"Delete"),
            "Delete should not be in decorators"
        );
    }

    // RT8: class-validator decorators on DTO
    #[test]
    fn class_validator_on_dto() {
        // Given: nestjs_dto_validation.ts with @IsEmail, @IsNotEmpty on fields
        let source = fixture("nestjs_dto_validation.ts");
        let extractor = TypeScriptExtractor::new();

        // When: extract decorators
        let decorators = extractor.extract_decorators(&source, "nestjs_dto_validation.ts");

        // Then: IsEmail and IsNotEmpty extracted
        let names: Vec<&str> = decorators.iter().map(|d| d.name.as_str()).collect();
        assert!(names.contains(&"IsEmail"), "expected IsEmail in {names:?}");
        assert!(
            names.contains(&"IsNotEmpty"),
            "expected IsNotEmpty in {names:?}"
        );
    }

    // RT9: UsePipes decorator
    #[test]
    fn use_pipes_decorator() {
        // Given: nestjs_guards_pipes.ts with @UsePipes(ValidationPipe)
        let source = fixture("nestjs_guards_pipes.ts");
        let extractor = TypeScriptExtractor::new();

        // When: extract decorators
        let decorators = extractor.extract_decorators(&source, "nestjs_guards_pipes.ts");

        // Then: UsePipes with ValidationPipe
        let pipes: Vec<&DecoratorInfo> =
            decorators.iter().filter(|d| d.name == "UsePipes").collect();
        assert!(!pipes.is_empty(), "expected UsePipes decorators");
        assert!(pipes[0].arguments.contains(&"ValidationPipe".to_string()));
    }

    // RT10: empty source returns empty for routes and decorators
    #[test]
    fn empty_source_returns_empty_routes_and_decorators() {
        // Given: empty source
        let extractor = TypeScriptExtractor::new();

        // When: extract routes and decorators
        let routes = extractor.extract_routes("", "empty.ts");
        let decorators = extractor.extract_decorators("", "empty.ts");

        // Then: both empty
        assert!(routes.is_empty());
        assert!(decorators.is_empty());
    }

    // RT11: non-NestJS class returns no routes
    #[test]
    fn non_nestjs_class_ignored() {
        // Given: class_methods.ts (plain class, no @Controller)
        let source = fixture("class_methods.ts");
        let extractor = TypeScriptExtractor::new();

        // When: extract routes
        let routes = extractor.extract_routes(&source, "class_methods.ts");

        // Then: empty
        assert!(routes.is_empty(), "expected no routes from plain class");
    }

    // RT12: handler_name and class_name correct
    #[test]
    fn route_handler_and_class_name() {
        // Given: nestjs_controller.ts
        let source = fixture("nestjs_controller.ts");
        let extractor = TypeScriptExtractor::new();

        // When: extract routes
        let routes = extractor.extract_routes(&source, "nestjs_controller.ts");

        // Then: handler names and class name correct
        let handlers: Vec<&str> = routes.iter().map(|r| r.handler_name.as_str()).collect();
        assert!(handlers.contains(&"findAll"));
        assert!(handlers.contains(&"create"));
        assert!(handlers.contains(&"remove"));
        for route in &routes {
            assert_eq!(route.class_name, "UsersController");
        }
    }

    // RT13: class-level UseGuards decorator is extracted
    #[test]
    fn class_level_use_guards() {
        // Given: nestjs_guards_pipes.ts with @UseGuards(JwtAuthGuard) at class level
        let source = fixture("nestjs_guards_pipes.ts");
        let extractor = TypeScriptExtractor::new();

        // When: extract decorators
        let decorators = extractor.extract_decorators(&source, "nestjs_guards_pipes.ts");

        // Then: JwtAuthGuard class-level decorator is extracted
        let class_guards: Vec<&DecoratorInfo> = decorators
            .iter()
            .filter(|d| {
                d.name == "UseGuards"
                    && d.target_name == "ProtectedController"
                    && d.class_name == "ProtectedController"
            })
            .collect();
        assert!(
            !class_guards.is_empty(),
            "expected class-level UseGuards, got {decorators:?}"
        );
        assert!(class_guards[0]
            .arguments
            .contains(&"JwtAuthGuard".to_string()));
    }

    // RT14: non-literal controller path produces <dynamic>
    #[test]
    fn dynamic_controller_path() {
        // Given: nestjs_dynamic_routes.ts with @Controller(BASE_PATH)
        let source = fixture("nestjs_dynamic_routes.ts");
        let extractor = TypeScriptExtractor::new();

        // When: extract routes
        let routes = extractor.extract_routes(&source, "nestjs_dynamic_routes.ts");

        // Then: path contains <dynamic>
        assert_eq!(routes.len(), 1);
        assert!(
            routes[0].path.contains("<dynamic>"),
            "expected <dynamic> in path, got {:?}",
            routes[0].path
        );
    }

    // TC11: abstract class methods are extracted with class_name and export status
    #[test]
    fn abstract_class_methods_extracted() {
        // Given: abstract_class.ts with exported and non-exported abstract classes
        let source = fixture("abstract_class.ts");
        let extractor = TypeScriptExtractor::new();

        // When: extract production functions
        let funcs = extractor.extract_production_functions(&source, "abstract_class.ts");

        // Then: concrete methods are extracted (abstract methods have no body → method_signature, not method_definition)
        let validate = funcs.iter().find(|f| f.name == "validate");
        assert!(validate.is_some(), "expected validate to be extracted");
        let validate = validate.unwrap();
        assert_eq!(validate.class_name.as_deref(), Some("BaseService"));
        assert!(validate.is_exported);

        let process = funcs.iter().find(|f| f.name == "process");
        assert!(process.is_some(), "expected process to be extracted");
        let process = process.unwrap();
        assert_eq!(process.class_name.as_deref(), Some("InternalBase"));
        assert!(!process.is_exported);
    }

    #[test]
    fn basic_spec_mapping() {
        // Given: a production file and its matching .spec test file in the same directory
        let extractor = TypeScriptExtractor::new();
        let production_files = vec!["src/users.service.ts".to_string()];
        let test_files = vec!["src/users.service.spec.ts".to_string()];

        // When: map_test_files is called
        let mappings = extractor.map_test_files(&production_files, &test_files);

        // Then: the files are matched with FileNameConvention
        assert_eq!(
            mappings,
            vec![FileMapping {
                production_file: "src/users.service.ts".to_string(),
                test_files: vec!["src/users.service.spec.ts".to_string()],
                strategy: MappingStrategy::FileNameConvention,
            }]
        );
    }

    #[test]
    fn test_suffix_mapping() {
        // Given: a production file and its matching .test file
        let extractor = TypeScriptExtractor::new();
        let production_files = vec!["src/utils.ts".to_string()];
        let test_files = vec!["src/utils.test.ts".to_string()];

        // When: map_test_files is called
        let mappings = extractor.map_test_files(&production_files, &test_files);

        // Then: the files are matched
        assert_eq!(
            mappings[0].test_files,
            vec!["src/utils.test.ts".to_string()]
        );
    }

    #[test]
    fn multiple_test_files() {
        // Given: one production file and both .spec and .test files
        let extractor = TypeScriptExtractor::new();
        let production_files = vec!["src/app.ts".to_string()];
        let test_files = vec!["src/app.spec.ts".to_string(), "src/app.test.ts".to_string()];

        // When: map_test_files is called
        let mappings = extractor.map_test_files(&production_files, &test_files);

        // Then: both test files are matched
        assert_eq!(
            mappings[0].test_files,
            vec!["src/app.spec.ts".to_string(), "src/app.test.ts".to_string()]
        );
    }

    #[test]
    fn nestjs_controller() {
        // Given: a nested controller file and its matching spec file
        let extractor = TypeScriptExtractor::new();
        let production_files = vec!["src/users/users.controller.ts".to_string()];
        let test_files = vec!["src/users/users.controller.spec.ts".to_string()];

        // When: map_test_files is called
        let mappings = extractor.map_test_files(&production_files, &test_files);

        // Then: the nested files are matched
        assert_eq!(
            mappings[0].test_files,
            vec!["src/users/users.controller.spec.ts".to_string()]
        );
    }

    #[test]
    fn no_matching_test() {
        // Given: a production file and an unrelated test file
        let extractor = TypeScriptExtractor::new();
        let production_files = vec!["src/orphan.ts".to_string()];
        let test_files = vec!["src/other.spec.ts".to_string()];

        // When: map_test_files is called
        let mappings = extractor.map_test_files(&production_files, &test_files);

        // Then: the production file is still included with no tests
        assert_eq!(mappings[0].test_files, Vec::<String>::new());
    }

    #[test]
    fn different_directory_no_match() {
        // Given: matching stems in different directories
        let extractor = TypeScriptExtractor::new();
        let production_files = vec!["src/users.ts".to_string()];
        let test_files = vec!["test/users.spec.ts".to_string()];

        // When: map_test_files is called
        let mappings = extractor.map_test_files(&production_files, &test_files);

        // Then: no match is created because Layer 1 is same-directory only
        assert_eq!(mappings[0].test_files, Vec::<String>::new());
    }

    #[test]
    fn empty_input() {
        // Given: no production files and no test files
        let extractor = TypeScriptExtractor::new();

        // When: map_test_files is called
        let mappings = extractor.map_test_files(&[], &[]);

        // Then: an empty vector is returned
        assert!(mappings.is_empty());
    }

    #[test]
    fn tsx_files() {
        // Given: a TSX production file and its matching test file
        let extractor = TypeScriptExtractor::new();
        let production_files = vec!["src/App.tsx".to_string()];
        let test_files = vec!["src/App.test.tsx".to_string()];

        // When: map_test_files is called
        let mappings = extractor.map_test_files(&production_files, &test_files);

        // Then: the TSX files are matched
        assert_eq!(mappings[0].test_files, vec!["src/App.test.tsx".to_string()]);
    }

    #[test]
    fn unmatched_test_ignored() {
        // Given: one matching test file and one orphan test file
        let extractor = TypeScriptExtractor::new();
        let production_files = vec!["src/a.ts".to_string()];
        let test_files = vec!["src/a.spec.ts".to_string(), "src/b.spec.ts".to_string()];

        // When: map_test_files is called
        let mappings = extractor.map_test_files(&production_files, &test_files);

        // Then: only the matching test file is included
        assert_eq!(mappings.len(), 1);
        assert_eq!(mappings[0].test_files, vec!["src/a.spec.ts".to_string()]);
    }

    #[test]
    fn stem_extraction() {
        // Given: production and test file paths with ts and tsx extensions
        // When: production_stem and test_stem are called
        // Then: the normalized stems are extracted correctly
        assert_eq!(
            production_stem("src/users.service.ts"),
            Some("users.service")
        );
        assert_eq!(production_stem("src/App.tsx"), Some("App"));
        assert_eq!(
            test_stem("src/users.service.spec.ts"),
            Some("users.service")
        );
        assert_eq!(test_stem("src/utils.test.ts"), Some("utils"));
        assert_eq!(test_stem("src/App.test.tsx"), Some("App"));
        assert_eq!(test_stem("src/invalid.ts"), None);
    }

    // === extract_imports Tests (IM1-IM7) ===

    // IM1: named import の symbol と specifier が抽出される
    #[test]
    fn im1_named_import_symbol_and_specifier() {
        // Given: import_named.ts with `import { UsersController } from './users.controller'`
        let source = fixture("import_named.ts");
        let extractor = TypeScriptExtractor::new();

        // When: extract_imports
        let imports = extractor.extract_imports(&source, "import_named.ts");

        // Then: symbol: "UsersController", specifier: "./users.controller"
        let found = imports.iter().find(|i| i.symbol_name == "UsersController");
        assert!(
            found.is_some(),
            "expected UsersController in imports: {imports:?}"
        );
        assert_eq!(
            found.unwrap().module_specifier,
            "./users.controller",
            "wrong specifier"
        );
    }

    // IM2: 複数 named import (`{ A, B }`) が 2件返る (同specifier、異なるsymbol)
    #[test]
    fn im2_multiple_named_imports() {
        // Given: import_mixed.ts with `import { A, B } from './module'`
        let source = fixture("import_mixed.ts");
        let extractor = TypeScriptExtractor::new();

        // When: extract_imports
        let imports = extractor.extract_imports(&source, "import_mixed.ts");

        // Then: A と B が両方返る (同じ ./module specifier)
        let from_module: Vec<&ImportMapping> = imports
            .iter()
            .filter(|i| i.module_specifier == "./module")
            .collect();
        let symbols: Vec<&str> = from_module.iter().map(|i| i.symbol_name.as_str()).collect();
        assert!(symbols.contains(&"A"), "expected A in symbols: {symbols:?}");
        assert!(symbols.contains(&"B"), "expected B in symbols: {symbols:?}");
        // at least 2 from ./module (IM2: { A, B } + IM3: { A as B } both in import_mixed.ts)
        assert!(
            from_module.len() >= 2,
            "expected at least 2 imports from ./module, got {from_module:?}"
        );
    }

    // IM3: エイリアス import (`{ A as B }`) で元の名前 "A" が返る
    #[test]
    fn im3_alias_import_original_name() {
        // Given: import_mixed.ts with `import { A as B } from './module'`
        let source = fixture("import_mixed.ts");
        let extractor = TypeScriptExtractor::new();

        // When: extract_imports
        let imports = extractor.extract_imports(&source, "import_mixed.ts");

        // Then: symbol_name は "A" (エイリアス "B" ではなく元の名前)
        // import_mixed.ts has: { A, B } and { A as B } — both should yield A
        let a_count = imports.iter().filter(|i| i.symbol_name == "A").count();
        assert!(
            a_count >= 1,
            "expected at least one import with symbol_name 'A', got: {imports:?}"
        );
    }

    // IM4: default import の symbol と specifier が抽出される
    #[test]
    fn im4_default_import() {
        // Given: import_default.ts with `import UsersController from './users.controller'`
        let source = fixture("import_default.ts");
        let extractor = TypeScriptExtractor::new();

        // When: extract_imports
        let imports = extractor.extract_imports(&source, "import_default.ts");

        // Then: symbol: "UsersController", specifier: "./users.controller"
        assert_eq!(imports.len(), 1, "expected 1 import, got {imports:?}");
        assert_eq!(imports[0].symbol_name, "UsersController");
        assert_eq!(imports[0].module_specifier, "./users.controller");
    }

    // IM5: npm パッケージ import (`@nestjs/testing`) が除外される (空Vec)
    #[test]
    fn im5_npm_package_excluded() {
        // Given: source with only `import { Test } from '@nestjs/testing'`
        let source = "import { Test } from '@nestjs/testing';";
        let extractor = TypeScriptExtractor::new();

        // When: extract_imports
        let imports = extractor.extract_imports(source, "test.ts");

        // Then: 空Vec (npm パッケージは除外)
        assert!(imports.is_empty(), "expected empty vec, got {imports:?}");
    }

    // IM6: 相対 `../` パスが含まれる
    #[test]
    fn im6_relative_parent_path() {
        // Given: import_named.ts with `import { S } from '../services/s.service'`
        let source = fixture("import_named.ts");
        let extractor = TypeScriptExtractor::new();

        // When: extract_imports
        let imports = extractor.extract_imports(&source, "import_named.ts");

        // Then: specifier: "../services/s.service"
        let found = imports
            .iter()
            .find(|i| i.module_specifier == "../services/s.service");
        assert!(
            found.is_some(),
            "expected ../services/s.service in imports: {imports:?}"
        );
        assert_eq!(found.unwrap().symbol_name, "S");
    }

    // IM7: 空ソースで空Vec が返る
    #[test]
    fn im7_empty_source_returns_empty() {
        // Given: empty source
        let extractor = TypeScriptExtractor::new();

        // When: extract_imports
        let imports = extractor.extract_imports("", "empty.ts");

        // Then: 空Vec
        assert!(imports.is_empty());
    }

    // IM8: namespace import (`import * as X from './module'`) が抽出される
    #[test]
    fn im8_namespace_import() {
        // Given: import_namespace.ts with `import * as UsersController from './users.controller'`
        let source = fixture("import_namespace.ts");
        let extractor = TypeScriptExtractor::new();

        // When: extract_imports
        let imports = extractor.extract_imports(&source, "import_namespace.ts");

        // Then: UsersController が symbol_name として抽出される
        let found = imports.iter().find(|i| i.symbol_name == "UsersController");
        assert!(
            found.is_some(),
            "expected UsersController in imports: {imports:?}"
        );
        assert_eq!(found.unwrap().module_specifier, "./users.controller");

        // Then: helpers も相対パスなので抽出される
        let helpers = imports.iter().find(|i| i.symbol_name == "helpers");
        assert!(
            helpers.is_some(),
            "expected helpers in imports: {imports:?}"
        );
        assert_eq!(helpers.unwrap().module_specifier, "../utils/helpers");

        // Then: npm パッケージ (express) は除外される
        let express = imports.iter().find(|i| i.symbol_name == "express");
        assert!(
            express.is_none(),
            "npm package should be excluded: {imports:?}"
        );
    }

    // IM9: type-only import (`import type { X }`) が除外され、通常importは残る
    #[test]
    fn im9_type_only_import_excluded() {
        // Given: import_type_only.ts with type-only and normal imports
        let source = fixture("import_type_only.ts");
        let extractor = TypeScriptExtractor::new();

        // When: extract_imports
        let imports = extractor.extract_imports(&source, "import_type_only.ts");

        // Then: `import type { UserService }` は除外される
        let user_service = imports.iter().find(|i| i.symbol_name == "UserService");
        assert!(
            user_service.is_none(),
            "type-only import should be excluded: {imports:?}"
        );

        // Then: `import { type CreateUserDto }` (inline type modifier) も除外される
        let create_dto = imports.iter().find(|i| i.symbol_name == "CreateUserDto");
        assert!(
            create_dto.is_none(),
            "inline type modifier import should be excluded: {imports:?}"
        );

        // Then: `import { UsersController }` は残る
        let controller = imports.iter().find(|i| i.symbol_name == "UsersController");
        assert!(
            controller.is_some(),
            "normal import should remain: {imports:?}"
        );
        assert_eq!(controller.unwrap().module_specifier, "./users.controller");
    }

    // === resolve_import_path Tests (RP1-RP5) ===

    // RP1: 拡張子なし specifier + 実在 `.ts` ファイル → Some(canonical path)
    #[test]
    fn rp1_resolve_ts_without_extension() {
        use std::io::Write as IoWrite;
        use tempfile::TempDir;

        // Given: scan_root/src/users.controller.ts が実在する
        let dir = TempDir::new().unwrap();
        let src_dir = dir.path().join("src");
        std::fs::create_dir_all(&src_dir).unwrap();
        let target = src_dir.join("users.controller.ts");
        std::fs::File::create(&target).unwrap();

        let from_file = src_dir.join("users.controller.spec.ts");

        // When: resolve_import_path("./users.controller", ...)
        let result = resolve_import_path("./users.controller", &from_file, dir.path());

        // Then: Some(canonical path)
        assert!(
            result.is_some(),
            "expected Some for existing .ts file, got None"
        );
        let resolved = result.unwrap();
        assert!(
            resolved.ends_with("users.controller.ts"),
            "expected path ending with users.controller.ts, got {resolved}"
        );
    }

    // RP2: 拡張子付き specifier (`.ts`) + 実在ファイル → Some(canonical path)
    #[test]
    fn rp2_resolve_ts_with_extension() {
        use tempfile::TempDir;

        // Given: scan_root/src/users.controller.ts が実在する
        let dir = TempDir::new().unwrap();
        let src_dir = dir.path().join("src");
        std::fs::create_dir_all(&src_dir).unwrap();
        let target = src_dir.join("users.controller.ts");
        std::fs::File::create(&target).unwrap();

        let from_file = src_dir.join("users.controller.spec.ts");

        // When: resolve_import_path("./users.controller.ts", ...) (拡張子付き)
        let result = resolve_import_path("./users.controller.ts", &from_file, dir.path());

        // Then: Some(canonical path)
        assert!(
            result.is_some(),
            "expected Some for existing file with explicit .ts extension"
        );
    }

    // RP3: 存在しないファイル → None
    #[test]
    fn rp3_nonexistent_file_returns_none() {
        use tempfile::TempDir;

        // Given: scan_root が空
        let dir = TempDir::new().unwrap();
        let src_dir = dir.path().join("src");
        std::fs::create_dir_all(&src_dir).unwrap();
        let from_file = src_dir.join("some.spec.ts");

        // When: resolve_import_path("./nonexistent", ...)
        let result = resolve_import_path("./nonexistent", &from_file, dir.path());

        // Then: None
        assert!(result.is_none(), "expected None for nonexistent file");
    }

    // RP4: scan_root 外のパス (`../../outside`) → None
    #[test]
    fn rp4_outside_scan_root_returns_none() {
        use tempfile::TempDir;

        // Given: scan_root/src/ から ../../outside を参照 (scan_root 外)
        let dir = TempDir::new().unwrap();
        let src_dir = dir.path().join("src");
        std::fs::create_dir_all(&src_dir).unwrap();
        let from_file = src_dir.join("some.spec.ts");

        // When: resolve_import_path("../../outside", ...)
        let result = resolve_import_path("../../outside", &from_file, dir.path());

        // Then: None (path traversal ガード)
        assert!(result.is_none(), "expected None for path outside scan_root");
    }

    // RP5: 拡張子なし specifier + 実在 `.tsx` ファイル → Some(canonical path)
    #[test]
    fn rp5_resolve_tsx_without_extension() {
        use tempfile::TempDir;

        // Given: scan_root/src/App.tsx が実在する
        let dir = TempDir::new().unwrap();
        let src_dir = dir.path().join("src");
        std::fs::create_dir_all(&src_dir).unwrap();
        let target = src_dir.join("App.tsx");
        std::fs::File::create(&target).unwrap();

        let from_file = src_dir.join("App.test.tsx");

        // When: resolve_import_path("./App", ...)
        let result = resolve_import_path("./App", &from_file, dir.path());

        // Then: Some(canonical path ending in App.tsx)
        assert!(
            result.is_some(),
            "expected Some for existing .tsx file, got None"
        );
        let resolved = result.unwrap();
        assert!(
            resolved.ends_with("App.tsx"),
            "expected path ending with App.tsx, got {resolved}"
        );
    }

    // === map_test_files_with_imports Tests (MT1-MT4) ===

    // MT1: Layer 1 マッチ + Layer 2 マッチが共存 → 両方マッピングされる
    #[test]
    fn mt1_layer1_and_layer2_both_matched() {
        use tempfile::TempDir;

        // Given:
        //   production: src/users.controller.ts
        //   test (Layer 1 match): src/users.controller.spec.ts (same dir)
        //   test (Layer 2 match): test/users.controller.spec.ts (imports users.controller)
        let dir = TempDir::new().unwrap();
        let src_dir = dir.path().join("src");
        let test_dir = dir.path().join("test");
        std::fs::create_dir_all(&src_dir).unwrap();
        std::fs::create_dir_all(&test_dir).unwrap();

        let prod_path = src_dir.join("users.controller.ts");
        std::fs::File::create(&prod_path).unwrap();

        let layer1_test = src_dir.join("users.controller.spec.ts");
        let layer1_source = r#"// Layer 1 spec
describe('UsersController', () => {});
"#;

        let layer2_test = test_dir.join("users.controller.spec.ts");
        let layer2_source = format!(
            "import {{ UsersController }} from '../src/users.controller';\ndescribe('cross', () => {{}});\n"
        );

        let production_files = vec![prod_path.to_string_lossy().into_owned()];
        let mut test_sources = HashMap::new();
        test_sources.insert(
            layer1_test.to_string_lossy().into_owned(),
            layer1_source.to_string(),
        );
        test_sources.insert(
            layer2_test.to_string_lossy().into_owned(),
            layer2_source.to_string(),
        );

        let extractor = TypeScriptExtractor::new();

        // When: map_test_files_with_imports
        let mappings =
            extractor.map_test_files_with_imports(&production_files, &test_sources, dir.path());

        // Then: 両方のテストがマッピングされる
        assert_eq!(mappings.len(), 1, "expected 1 FileMapping");
        let mapping = &mappings[0];
        assert!(
            mapping
                .test_files
                .contains(&layer1_test.to_string_lossy().into_owned()),
            "expected Layer 1 test in mapping, got {:?}",
            mapping.test_files
        );
        assert!(
            mapping
                .test_files
                .contains(&layer2_test.to_string_lossy().into_owned()),
            "expected Layer 2 test in mapping, got {:?}",
            mapping.test_files
        );
    }

    // MT2: クロスディレクトリ import → ImportTracing でマッチ
    #[test]
    fn mt2_cross_directory_import_tracing() {
        use tempfile::TempDir;

        // Given:
        //   production: src/services/user.service.ts
        //   test: test/user.service.spec.ts (imports user.service from cross-directory)
        //   Layer 1 は別ディレクトリのためマッチしない
        let dir = TempDir::new().unwrap();
        let src_dir = dir.path().join("src").join("services");
        let test_dir = dir.path().join("test");
        std::fs::create_dir_all(&src_dir).unwrap();
        std::fs::create_dir_all(&test_dir).unwrap();

        let prod_path = src_dir.join("user.service.ts");
        std::fs::File::create(&prod_path).unwrap();

        let test_path = test_dir.join("user.service.spec.ts");
        let test_source = format!(
            "import {{ UserService }} from '../src/services/user.service';\ndescribe('cross', () => {{}});\n"
        );

        let production_files = vec![prod_path.to_string_lossy().into_owned()];
        let mut test_sources = HashMap::new();
        test_sources.insert(test_path.to_string_lossy().into_owned(), test_source);

        let extractor = TypeScriptExtractor::new();

        // When: map_test_files_with_imports
        let mappings =
            extractor.map_test_files_with_imports(&production_files, &test_sources, dir.path());

        // Then: ImportTracing でマッチ
        assert_eq!(mappings.len(), 1);
        let mapping = &mappings[0];
        assert!(
            mapping
                .test_files
                .contains(&test_path.to_string_lossy().into_owned()),
            "expected test in mapping via ImportTracing, got {:?}",
            mapping.test_files
        );
        assert_eq!(
            mapping.strategy,
            MappingStrategy::ImportTracing,
            "expected ImportTracing strategy"
        );
    }

    // MT3: npm import のみ → 未マッチ
    #[test]
    fn mt3_npm_only_import_not_matched() {
        use tempfile::TempDir;

        // Given:
        //   production: src/users.controller.ts
        //   test: test/something.spec.ts (imports only from npm)
        let dir = TempDir::new().unwrap();
        let src_dir = dir.path().join("src");
        let test_dir = dir.path().join("test");
        std::fs::create_dir_all(&src_dir).unwrap();
        std::fs::create_dir_all(&test_dir).unwrap();

        let prod_path = src_dir.join("users.controller.ts");
        std::fs::File::create(&prod_path).unwrap();

        let test_path = test_dir.join("something.spec.ts");
        let test_source =
            "import { Test } from '@nestjs/testing';\ndescribe('npm', () => {});\n".to_string();

        let production_files = vec![prod_path.to_string_lossy().into_owned()];
        let mut test_sources = HashMap::new();
        test_sources.insert(test_path.to_string_lossy().into_owned(), test_source);

        let extractor = TypeScriptExtractor::new();

        // When: map_test_files_with_imports
        let mappings =
            extractor.map_test_files_with_imports(&production_files, &test_sources, dir.path());

        // Then: 未マッチ (test_files は空)
        assert_eq!(mappings.len(), 1);
        assert!(
            mappings[0].test_files.is_empty(),
            "expected no test files for npm-only import, got {:?}",
            mappings[0].test_files
        );
    }

    // MT4: 1テストが複数 production を import → 両方にマッピング
    #[test]
    fn mt4_one_test_imports_multiple_productions() {
        use tempfile::TempDir;

        // Given:
        //   production A: src/a.service.ts
        //   production B: src/b.service.ts
        //   test: test/ab.spec.ts (imports both A and B)
        let dir = TempDir::new().unwrap();
        let src_dir = dir.path().join("src");
        let test_dir = dir.path().join("test");
        std::fs::create_dir_all(&src_dir).unwrap();
        std::fs::create_dir_all(&test_dir).unwrap();

        let prod_a = src_dir.join("a.service.ts");
        let prod_b = src_dir.join("b.service.ts");
        std::fs::File::create(&prod_a).unwrap();
        std::fs::File::create(&prod_b).unwrap();

        let test_path = test_dir.join("ab.spec.ts");
        let test_source = format!(
            "import {{ A }} from '../src/a.service';\nimport {{ B }} from '../src/b.service';\ndescribe('ab', () => {{}});\n"
        );

        let production_files = vec![
            prod_a.to_string_lossy().into_owned(),
            prod_b.to_string_lossy().into_owned(),
        ];
        let mut test_sources = HashMap::new();
        test_sources.insert(test_path.to_string_lossy().into_owned(), test_source);

        let extractor = TypeScriptExtractor::new();

        // When: map_test_files_with_imports
        let mappings =
            extractor.map_test_files_with_imports(&production_files, &test_sources, dir.path());

        // Then: A と B 両方に test がマッピングされる
        assert_eq!(mappings.len(), 2, "expected 2 FileMappings (A and B)");
        for mapping in &mappings {
            assert!(
                mapping
                    .test_files
                    .contains(&test_path.to_string_lossy().into_owned()),
                "expected ab.spec.ts mapped to {}, got {:?}",
                mapping.production_file,
                mapping.test_files
            );
        }
    }

    // HELPER-01: constants.ts is detected as non-SUT helper
    #[test]
    fn is_non_sut_helper_constants_ts() {
        assert!(is_non_sut_helper("src/constants.ts", false));
    }

    // HELPER-02: index.ts is detected as non-SUT helper
    #[test]
    fn is_non_sut_helper_index_ts() {
        assert!(is_non_sut_helper("src/index.ts", false));
    }

    // HELPER-03: extension variants (.js/.tsx/.jsx) are also detected
    #[test]
    fn is_non_sut_helper_extension_variants() {
        assert!(is_non_sut_helper("src/constants.js", false));
        assert!(is_non_sut_helper("src/constants.tsx", false));
        assert!(is_non_sut_helper("src/constants.jsx", false));
        assert!(is_non_sut_helper("src/index.js", false));
        assert!(is_non_sut_helper("src/index.tsx", false));
        assert!(is_non_sut_helper("src/index.jsx", false));
    }

    // HELPER-04: similar but distinct filenames are NOT helpers
    #[test]
    fn is_non_sut_helper_rejects_non_helpers() {
        assert!(!is_non_sut_helper("src/my-constants.ts", false));
        assert!(!is_non_sut_helper("src/service.ts", false));
        assert!(!is_non_sut_helper("src/app.constants.ts", false));
        assert!(!is_non_sut_helper("src/constants-v2.ts", false));
    }

    // HELPER-05: directory named constants/app.ts is NOT a helper
    #[test]
    fn is_non_sut_helper_rejects_directory_name() {
        assert!(!is_non_sut_helper("constants/app.ts", false));
        assert!(!is_non_sut_helper("index/service.ts", false));
    }

    // HELPER-06: *.enum.ts is detected as non-SUT helper
    #[test]
    fn is_non_sut_helper_enum_ts() {
        // Given: a file with .enum.ts suffix
        let path = "src/enums/request-method.enum.ts";
        // When: is_non_sut_helper() is called
        // Then: returns true
        assert!(is_non_sut_helper(path, false));
    }

    // HELPER-07: *.interface.ts is detected as non-SUT helper
    #[test]
    fn is_non_sut_helper_interface_ts() {
        // Given: a file with .interface.ts suffix
        let path = "src/interfaces/middleware-configuration.interface.ts";
        // When: is_non_sut_helper() is called
        // Then: returns true
        assert!(is_non_sut_helper(path, false));
    }

    // HELPER-08: *.exception.ts is detected as non-SUT helper
    #[test]
    fn is_non_sut_helper_exception_ts() {
        // Given: a file with .exception.ts suffix
        let path = "src/errors/unknown-module.exception.ts";
        // When: is_non_sut_helper() is called
        // Then: returns true
        assert!(is_non_sut_helper(path, false));
    }

    // HELPER-09: file inside a test path is detected as non-SUT helper
    #[test]
    fn is_non_sut_helper_test_path() {
        // Given: a file located under a /test/ directory
        let path = "packages/core/test/utils/string.cleaner.ts";
        // When: is_non_sut_helper() is called
        // Then: returns true
        assert!(is_non_sut_helper(path, false));
        // __tests__ variant
        assert!(is_non_sut_helper(
            "packages/core/__tests__/utils/helper.ts",
            false
        ));
        // segment-based: "contest" should NOT match
        assert!(!is_non_sut_helper(
            "/home/user/projects/contest/src/service.ts",
            false
        ));
        assert!(!is_non_sut_helper("src/latest/foo.ts", false));
    }

    // HELPER-10: suffix-like but plain filename (not a suffix) is rejected
    #[test]
    fn is_non_sut_helper_rejects_plain_filename() {
        // Given: files whose name is exactly enum.ts / interface.ts / exception.ts
        // (the type keyword is the entire filename, not a suffix)
        // When: is_non_sut_helper() is called
        // Then: returns false (these may be real SUT files)
        assert!(!is_non_sut_helper("src/enum.ts", false));
        assert!(!is_non_sut_helper("src/interface.ts", false));
        assert!(!is_non_sut_helper("src/exception.ts", false));
    }

    // HELPER-11: extension variants (.js/.tsx/.jsx) with enum/interface suffix are detected
    #[test]
    fn is_non_sut_helper_enum_interface_extension_variants() {
        // Given: files with .enum or .interface suffix and non-.ts extension
        // When: is_non_sut_helper() is called
        // Then: returns true
        assert!(is_non_sut_helper("src/foo.enum.js", false));
        assert!(is_non_sut_helper("src/bar.interface.tsx", false));
    }

    // === is_type_definition_file unit tests (TD-01 ~ TD-05) ===

    // TD-01: *.enum.ts is a type definition file
    #[test]
    fn is_type_definition_file_enum() {
        assert!(is_type_definition_file("src/foo.enum.ts"));
    }

    // TD-02: *.interface.ts is a type definition file
    #[test]
    fn is_type_definition_file_interface() {
        assert!(is_type_definition_file("src/bar.interface.ts"));
    }

    // TD-03: *.exception.ts is a type definition file
    #[test]
    fn is_type_definition_file_exception() {
        assert!(is_type_definition_file("src/baz.exception.ts"));
    }

    // TD-04: regular service file is NOT a type definition file
    #[test]
    fn is_type_definition_file_service() {
        assert!(!is_type_definition_file("src/service.ts"));
    }

    // TD-05: constants.ts is NOT a type definition file (suffix check only, not exact-match)
    #[test]
    fn is_type_definition_file_constants() {
        // constants.ts has no .enum/.interface/.exception suffix
        assert!(!is_type_definition_file("src/constants.ts"));
    }

    // === is_non_sut_helper (production-aware) unit tests (PA-01 ~ PA-03) ===

    // PA-01: enum file with known_production=true bypasses suffix filter
    #[test]
    fn is_non_sut_helper_production_enum_bypassed() {
        // Given: an enum file known to be in production_files
        // When: is_non_sut_helper with is_known_production=true
        // Then: returns false (not filtered)
        assert!(!is_non_sut_helper("src/foo.enum.ts", true));
    }

    // PA-02: enum file with known_production=false is still filtered
    #[test]
    fn is_non_sut_helper_unknown_enum_filtered() {
        // Given: an enum file NOT in production_files
        // When: is_non_sut_helper with is_known_production=false
        // Then: returns true (filtered as before)
        assert!(is_non_sut_helper("src/foo.enum.ts", false));
    }

    // PA-03: constants.ts is filtered regardless of known_production
    #[test]
    fn is_non_sut_helper_constants_always_filtered() {
        // Given: constants.ts (exact-match filter, not suffix)
        // When: is_non_sut_helper with is_known_production=true
        // Then: returns true (exact-match is independent of production status)
        assert!(is_non_sut_helper("src/constants.ts", true));
    }

    // === Barrel Import Resolution Tests (BARREL-01 ~ BARREL-09) ===

    // BARREL-01: resolve_import_path がディレクトリの index.ts にフォールバックする
    #[test]
    fn barrel_01_resolve_directory_to_index_ts() {
        use tempfile::TempDir;

        // Given: scan_root/decorators/index.ts が存在
        let dir = TempDir::new().unwrap();
        let decorators_dir = dir.path().join("decorators");
        std::fs::create_dir_all(&decorators_dir).unwrap();
        std::fs::File::create(decorators_dir.join("index.ts")).unwrap();

        // from_file は scan_root/src/some.spec.ts (../../decorators → decorators/)
        let src_dir = dir.path().join("src");
        std::fs::create_dir_all(&src_dir).unwrap();
        let from_file = src_dir.join("some.spec.ts");

        // When: resolve_import_path("../decorators", from_file, scan_root)
        let result = resolve_import_path("../decorators", &from_file, dir.path());

        // Then: decorators/index.ts のパスを返す
        assert!(
            result.is_some(),
            "expected Some for directory with index.ts, got None"
        );
        let resolved = result.unwrap();
        assert!(
            resolved.ends_with("decorators/index.ts"),
            "expected path ending with decorators/index.ts, got {resolved}"
        );
    }

    // BARREL-02: extract_barrel_re_exports が named re-export をキャプチャする
    #[test]
    fn barrel_02_re_export_named_capture() {
        // Given: `export { Foo } from './foo'`
        let source = "export { Foo } from './foo';";
        let extractor = TypeScriptExtractor::new();

        // When: extract_barrel_re_exports
        let re_exports = extractor.extract_barrel_re_exports(source, "index.ts");

        // Then: symbols=["Foo"], from="./foo", wildcard=false
        assert_eq!(
            re_exports.len(),
            1,
            "expected 1 re-export, got {re_exports:?}"
        );
        let re = &re_exports[0];
        assert_eq!(re.symbols, vec!["Foo".to_string()]);
        assert_eq!(re.from_specifier, "./foo");
        assert!(!re.wildcard);
    }

    // BARREL-03: extract_barrel_re_exports が wildcard re-export をキャプチャする
    #[test]
    fn barrel_03_re_export_wildcard_capture() {
        // Given: `export * from './foo'`
        let source = "export * from './foo';";
        let extractor = TypeScriptExtractor::new();

        // When: extract_barrel_re_exports
        let re_exports = extractor.extract_barrel_re_exports(source, "index.ts");

        // Then: wildcard=true, from="./foo"
        assert_eq!(
            re_exports.len(),
            1,
            "expected 1 re-export, got {re_exports:?}"
        );
        let re = &re_exports[0];
        assert!(re.wildcard, "expected wildcard=true");
        assert_eq!(re.from_specifier, "./foo");
    }

    // BARREL-04: resolve_barrel_exports が 1ホップのバレルを解決する
    #[test]
    fn barrel_04_resolve_barrel_exports_one_hop() {
        use tempfile::TempDir;

        // Given:
        //   index.ts: export { Foo } from './foo'
        //   foo.ts: (実在)
        let dir = TempDir::new().unwrap();
        let index_path = dir.path().join("index.ts");
        std::fs::write(&index_path, "export { Foo } from './foo';").unwrap();
        let foo_path = dir.path().join("foo.ts");
        std::fs::File::create(&foo_path).unwrap();

        // When: resolve_barrel_exports(index_path, ["Foo"], scan_root)
        let result = resolve_barrel_exports(&index_path, &["Foo".to_string()], dir.path());

        // Then: [foo.ts] を返す
        assert_eq!(result.len(), 1, "expected 1 resolved file, got {result:?}");
        assert!(
            result[0].ends_with("foo.ts"),
            "expected foo.ts, got {:?}",
            result[0]
        );
    }

    // BARREL-05: resolve_barrel_exports が 2ホップのバレルを解決する
    #[test]
    fn barrel_05_resolve_barrel_exports_two_hops() {
        use tempfile::TempDir;

        // Given:
        //   index.ts: export * from './core'
        //   core/index.ts: export { Foo } from './foo'
        //   core/foo.ts: (実在)
        let dir = TempDir::new().unwrap();
        let index_path = dir.path().join("index.ts");
        std::fs::write(&index_path, "export * from './core';").unwrap();

        let core_dir = dir.path().join("core");
        std::fs::create_dir_all(&core_dir).unwrap();
        std::fs::write(core_dir.join("index.ts"), "export { Foo } from './foo';").unwrap();
        let foo_path = core_dir.join("foo.ts");
        std::fs::File::create(&foo_path).unwrap();

        // When: resolve_barrel_exports(index_path, ["Foo"], scan_root)
        let result = resolve_barrel_exports(&index_path, &["Foo".to_string()], dir.path());

        // Then: core/foo.ts を返す
        assert_eq!(result.len(), 1, "expected 1 resolved file, got {result:?}");
        assert!(
            result[0].ends_with("foo.ts"),
            "expected foo.ts, got {:?}",
            result[0]
        );
    }

    // BARREL-06: 循環バレルで無限ループしない
    #[test]
    fn barrel_06_circular_barrel_no_infinite_loop() {
        use tempfile::TempDir;

        // Given:
        //   a/index.ts: export * from '../b'
        //   b/index.ts: export * from '../a'
        let dir = TempDir::new().unwrap();
        let a_dir = dir.path().join("a");
        let b_dir = dir.path().join("b");
        std::fs::create_dir_all(&a_dir).unwrap();
        std::fs::create_dir_all(&b_dir).unwrap();
        std::fs::write(a_dir.join("index.ts"), "export * from '../b';").unwrap();
        std::fs::write(b_dir.join("index.ts"), "export * from '../a';").unwrap();

        let a_index = a_dir.join("index.ts");

        // When: resolve_barrel_exports — must NOT panic or hang
        let result = resolve_barrel_exports(&a_index, &["Foo".to_string()], dir.path());

        // Then: 空結果を返し、パニックしない
        assert!(
            result.is_empty(),
            "expected empty result for circular barrel, got {result:?}"
        );
    }

    // BARREL-07: Layer 2 で barrel 経由の import が production file にマッチする
    #[test]
    fn barrel_07_layer2_barrel_import_matches_production() {
        use tempfile::TempDir;

        // Given:
        //   production: src/foo.service.ts
        //   barrel: src/decorators/index.ts — export { Foo } from './foo.service'
        //           ただし src/decorators/foo.service.ts として re-export 先を指す
        //   test: test/foo.spec.ts — import { Foo } from '../src/decorators'
        let dir = TempDir::new().unwrap();
        let src_dir = dir.path().join("src");
        let decorators_dir = src_dir.join("decorators");
        let test_dir = dir.path().join("test");
        std::fs::create_dir_all(&decorators_dir).unwrap();
        std::fs::create_dir_all(&test_dir).unwrap();

        // Production file
        let prod_path = src_dir.join("foo.service.ts");
        std::fs::File::create(&prod_path).unwrap();

        // Barrel: decorators/index.ts re-exports from ../foo.service
        std::fs::write(
            decorators_dir.join("index.ts"),
            "export { Foo } from '../foo.service';",
        )
        .unwrap();

        // Test imports from barrel directory
        let test_path = test_dir.join("foo.spec.ts");
        std::fs::write(
            &test_path,
            "import { Foo } from '../src/decorators';\ndescribe('foo', () => {});",
        )
        .unwrap();

        let production_files = vec![prod_path.to_string_lossy().into_owned()];
        let mut test_sources = HashMap::new();
        test_sources.insert(
            test_path.to_string_lossy().into_owned(),
            std::fs::read_to_string(&test_path).unwrap(),
        );

        let extractor = TypeScriptExtractor::new();

        // When: map_test_files_with_imports (barrel resolution enabled)
        let mappings =
            extractor.map_test_files_with_imports(&production_files, &test_sources, dir.path());

        // Then: foo.service.ts に foo.spec.ts がマッピングされる
        assert_eq!(mappings.len(), 1, "expected 1 FileMapping");
        assert!(
            mappings[0]
                .test_files
                .contains(&test_path.to_string_lossy().into_owned()),
            "expected foo.spec.ts mapped via barrel, got {:?}",
            mappings[0].test_files
        );
    }

    // BARREL-08: is_non_sut_helper フィルタが barrel 解決後のファイルに適用される
    #[test]
    fn barrel_08_non_sut_filter_applied_after_barrel_resolution() {
        use tempfile::TempDir;

        // Given:
        //   barrel: index.ts → export { SOME_CONST } from './constants'
        //   resolved: constants.ts (is_non_sut_helper → true)
        //   test imports from barrel
        let dir = TempDir::new().unwrap();
        let src_dir = dir.path().join("src");
        let test_dir = dir.path().join("test");
        std::fs::create_dir_all(&src_dir).unwrap();
        std::fs::create_dir_all(&test_dir).unwrap();

        // Production file (real SUT)
        let prod_path = src_dir.join("user.service.ts");
        std::fs::File::create(&prod_path).unwrap();

        // Barrel index: re-exports from constants
        std::fs::write(
            src_dir.join("index.ts"),
            "export { SOME_CONST } from './constants';",
        )
        .unwrap();
        // constants.ts (non-SUT helper)
        std::fs::File::create(src_dir.join("constants.ts")).unwrap();

        // Test imports from barrel (which resolves to constants.ts)
        let test_path = test_dir.join("barrel_const.spec.ts");
        std::fs::write(
            &test_path,
            "import { SOME_CONST } from '../src';\ndescribe('const', () => {});",
        )
        .unwrap();

        let production_files = vec![prod_path.to_string_lossy().into_owned()];
        let mut test_sources = HashMap::new();
        test_sources.insert(
            test_path.to_string_lossy().into_owned(),
            std::fs::read_to_string(&test_path).unwrap(),
        );

        let extractor = TypeScriptExtractor::new();

        // When: map_test_files_with_imports
        let mappings =
            extractor.map_test_files_with_imports(&production_files, &test_sources, dir.path());

        // Then: user.service.ts にはマッピングされない (constants.ts はフィルタ済み)
        assert_eq!(
            mappings.len(),
            1,
            "expected 1 FileMapping for user.service.ts"
        );
        assert!(
            mappings[0].test_files.is_empty(),
            "constants.ts should be filtered out, but got {:?}",
            mappings[0].test_files
        );
    }

    // BARREL-09: extract_imports が symbol 名を保持する (ImportMapping::symbols フィールド)
    #[test]
    fn barrel_09_extract_imports_retains_symbols() {
        // Given: `import { Foo, Bar } from './module'`
        let source = "import { Foo, Bar } from './module';";
        let extractor = TypeScriptExtractor::new();

        // When: extract_imports
        let imports = extractor.extract_imports(source, "test.ts");

        // Then: Foo と Bar の両方が symbols として存在する
        // ImportMapping は symbol_name を 1件ずつ返すが、
        // 同一 module_specifier からの import は symbols Vec に集約される
        let from_module: Vec<&ImportMapping> = imports
            .iter()
            .filter(|i| i.module_specifier == "./module")
            .collect();
        let names: Vec<&str> = from_module.iter().map(|i| i.symbol_name.as_str()).collect();
        assert!(names.contains(&"Foo"), "expected Foo in symbols: {names:?}");
        assert!(names.contains(&"Bar"), "expected Bar in symbols: {names:?}");

        // BARREL-09 の本質: ImportMapping に symbols フィールドが存在し、
        // 同じ specifier からの import が集約されること
        // (現在の ImportMapping は symbol_name: String のみ → symbols: Vec<String> への移行が必要)
        let grouped = imports
            .iter()
            .filter(|i| i.module_specifier == "./module")
            .fold(Vec::<String>::new(), |mut acc, i| {
                acc.push(i.symbol_name.clone());
                acc
            });
        // symbols フィールドが実装されたら、1つの ImportMapping に ["Foo", "Bar"] が入る想定
        // 現時点では 2件の ImportMapping として返されることを確認
        assert_eq!(
            grouped.len(),
            2,
            "expected 2 symbols from ./module, got {grouped:?}"
        );

        // Verify symbols field aggregation: each ImportMapping from ./module
        // should have both Foo and Bar in its symbols Vec
        let first_import = imports
            .iter()
            .find(|i| i.module_specifier == "./module")
            .expect("expected at least one import from ./module");
        let symbols = &first_import.symbols;
        assert!(
            symbols.contains(&"Foo".to_string()),
            "symbols should contain Foo, got {symbols:?}"
        );
        assert!(
            symbols.contains(&"Bar".to_string()),
            "symbols should contain Bar, got {symbols:?}"
        );
        assert_eq!(
            symbols.len(),
            2,
            "expected exactly 2 symbols, got {symbols:?}"
        );
    }

    // BARREL-10: wildcard-only barrel で symbol フィルタが効く
    // NestJS パターン: index.ts → export * from './core' → core/index.ts → export * from './foo'
    // テストが { Foo } のみ import → foo.ts のみマッチ、bar.ts はマッチしない
    #[test]
    fn barrel_10_wildcard_barrel_symbol_filter() {
        use tempfile::TempDir;

        // Given:
        //   index.ts: export * from './core'
        //   core/index.ts: export * from './foo' + export * from './bar'
        //   core/foo.ts: export function Foo() {}
        //   core/bar.ts: export function Bar() {}
        let dir = TempDir::new().unwrap();
        let core_dir = dir.path().join("core");
        std::fs::create_dir_all(&core_dir).unwrap();

        std::fs::write(dir.path().join("index.ts"), "export * from './core';").unwrap();
        std::fs::write(
            core_dir.join("index.ts"),
            "export * from './foo';\nexport * from './bar';",
        )
        .unwrap();
        std::fs::write(core_dir.join("foo.ts"), "export function Foo() {}").unwrap();
        std::fs::write(core_dir.join("bar.ts"), "export function Bar() {}").unwrap();

        // When: resolve with symbols=["Foo"]
        let result = resolve_barrel_exports(
            &dir.path().join("index.ts"),
            &["Foo".to_string()],
            dir.path(),
        );

        // Then: foo.ts のみ返す (bar.ts は Foo を export していないのでマッチしない)
        assert_eq!(result.len(), 1, "expected 1 resolved file, got {result:?}");
        assert!(
            result[0].ends_with("foo.ts"),
            "expected foo.ts, got {:?}",
            result[0]
        );
    }

    // BARREL-11: wildcard barrel + symbols empty → 全ファイルを返す (保守的)
    #[test]
    fn barrel_11_wildcard_barrel_empty_symbols_match_all() {
        use tempfile::TempDir;

        let dir = TempDir::new().unwrap();
        let core_dir = dir.path().join("core");
        std::fs::create_dir_all(&core_dir).unwrap();

        std::fs::write(dir.path().join("index.ts"), "export * from './core';").unwrap();
        std::fs::write(
            core_dir.join("index.ts"),
            "export * from './foo';\nexport * from './bar';",
        )
        .unwrap();
        std::fs::write(core_dir.join("foo.ts"), "export function Foo() {}").unwrap();
        std::fs::write(core_dir.join("bar.ts"), "export function Bar() {}").unwrap();

        // When: resolve with empty symbols (match all)
        let result = resolve_barrel_exports(&dir.path().join("index.ts"), &[], dir.path());

        // Then: both files returned
        assert_eq!(result.len(), 2, "expected 2 resolved files, got {result:?}");
    }

    // === Boundary Specification Tests (B1-B6) ===
    // These tests document CURRENT behavior at failure boundaries.
    // All assertions reflect known limitations, not desired future behavior.

    // TC-01: Boundary B1 — namespace re-export is NOT captured by extract_barrel_re_exports
    #[test]
    fn boundary_b1_ns_reexport_not_captured() {
        // Given: barrel index.ts with `export * as Ns from './validators'`
        let source = "export * as Validators from './validators';";
        let extractor = TypeScriptExtractor::new();

        // When: extract_barrel_re_exports
        let re_exports = extractor.extract_barrel_re_exports(source, "index.ts");

        // Then: namespace re-export is NOT captured (empty vec)
        // Note: re_export.scm only handles `export { X } from` and `export * from`,
        //       not `export * as Ns from` (namespace export is a different AST node)
        assert!(
            re_exports.is_empty(),
            "expected empty re_exports for namespace export, got {:?}",
            re_exports
        );
    }

    // TC-02: Boundary B1 — namespace re-export causes test-to-code mapping miss (FN)
    #[test]
    fn boundary_b1_ns_reexport_mapping_miss() {
        use tempfile::TempDir;

        // Given:
        //   validators/foo.service.ts (production)
        //   index.ts: `export * as Validators from './validators'`
        //   validators/index.ts: `export { FooService } from './foo.service'`
        //   test/foo.spec.ts: `import { Validators } from '../index'`
        let dir = TempDir::new().unwrap();
        let validators_dir = dir.path().join("validators");
        let test_dir = dir.path().join("test");
        std::fs::create_dir_all(&validators_dir).unwrap();
        std::fs::create_dir_all(&test_dir).unwrap();

        // Production file
        let prod_path = validators_dir.join("foo.service.ts");
        std::fs::File::create(&prod_path).unwrap();

        // Root barrel: namespace re-export (B1 boundary)
        std::fs::write(
            dir.path().join("index.ts"),
            "export * as Validators from './validators';",
        )
        .unwrap();

        // validators/index.ts: named re-export
        std::fs::write(
            validators_dir.join("index.ts"),
            "export { FooService } from './foo.service';",
        )
        .unwrap();

        // Test imports via namespace re-export
        let test_path = test_dir.join("foo.spec.ts");
        std::fs::write(
            &test_path,
            "import { Validators } from '../index';\ndescribe('FooService', () => {});",
        )
        .unwrap();

        let production_files = vec![prod_path.to_string_lossy().into_owned()];
        let mut test_sources = HashMap::new();
        test_sources.insert(
            test_path.to_string_lossy().into_owned(),
            std::fs::read_to_string(&test_path).unwrap(),
        );

        let extractor = TypeScriptExtractor::new();

        // When: map_test_files_with_imports
        let mappings =
            extractor.map_test_files_with_imports(&production_files, &test_sources, dir.path());

        // Then: foo.service.ts has NO test_files (FN — namespace re-export not resolved)
        // Layer 1 (filename convention) produces no match either, so the mapping has no test_files.
        let all_test_files: Vec<&String> =
            mappings.iter().flat_map(|m| m.test_files.iter()).collect();
        assert!(
            all_test_files.is_empty(),
            "expected no test_files mapped (FN: namespace re-export not resolved), got {:?}",
            all_test_files
        );
    }

    // TC-03: Boundary B2 — non-relative import is skipped by extract_imports
    #[test]
    fn boundary_b2_non_relative_import_skipped() {
        // Given: source with `import { Injectable } from '@nestjs/common'`
        let source = "import { Injectable } from '@nestjs/common';";
        let extractor = TypeScriptExtractor::new();

        // When: extract_imports
        let imports = extractor.extract_imports(source, "app.service.ts");

        // Then: imports is empty (non-relative paths are excluded)
        assert!(
            imports.is_empty(),
            "expected empty imports for non-relative path, got {:?}",
            imports
        );
    }

    // TC-04: Boundary B2 — cross-package barrel import is unresolvable (FN)
    #[test]
    fn boundary_b2_cross_pkg_barrel_unresolvable() {
        use tempfile::TempDir;

        // Given:
        //   packages/core/ (scan_root)
        //   packages/core/src/foo.service.ts (production)
        //   packages/common/src/foo.ts (production, in different package)
        //   packages/core/test/foo.spec.ts: `import { Foo } from '@org/common'` (non-relative)
        let dir = TempDir::new().unwrap();
        let core_src = dir.path().join("packages").join("core").join("src");
        let core_test = dir.path().join("packages").join("core").join("test");
        let common_src = dir.path().join("packages").join("common").join("src");
        std::fs::create_dir_all(&core_src).unwrap();
        std::fs::create_dir_all(&core_test).unwrap();
        std::fs::create_dir_all(&common_src).unwrap();

        let prod_path = core_src.join("foo.service.ts");
        std::fs::File::create(&prod_path).unwrap();

        let common_path = common_src.join("foo.ts");
        std::fs::File::create(&common_path).unwrap();

        let test_path = core_test.join("foo.spec.ts");
        std::fs::write(
            &test_path,
            "import { Foo } from '@org/common';\ndescribe('Foo', () => {});",
        )
        .unwrap();

        let scan_root = dir.path().join("packages").join("core");
        let production_files = vec![prod_path.to_string_lossy().into_owned()];
        let mut test_sources = HashMap::new();
        test_sources.insert(
            test_path.to_string_lossy().into_owned(),
            std::fs::read_to_string(&test_path).unwrap(),
        );

        let extractor = TypeScriptExtractor::new();

        // When: map_test_files_with_imports(scan_root=packages/core/)
        let mappings =
            extractor.map_test_files_with_imports(&production_files, &test_sources, &scan_root);

        // Then: packages/common/src/foo.ts has NO test_files (cross-package import not resolved)
        // Since `@org/common` is non-relative, extract_imports will skip it entirely.
        let all_test_files: Vec<&String> =
            mappings.iter().flat_map(|m| m.test_files.iter()).collect();
        assert!(
            all_test_files.is_empty(),
            "expected no test_files mapped (FN: cross-package import not resolved), got {:?}",
            all_test_files
        );
    }

    // TC-05: Boundary B3 — tsconfig path alias is treated same as non-relative import
    #[test]
    fn boundary_b3_tsconfig_alias_not_resolved() {
        // Given: source with `import { FooService } from '@app/services/foo.service'`
        let source = "import { FooService } from '@app/services/foo.service';";
        let extractor = TypeScriptExtractor::new();

        // When: extract_imports
        let imports = extractor.extract_imports(source, "app.module.ts");

        // Then: imports is empty (@app/ is non-relative, same code path as TC-03)
        // Note: tsconfig path aliases are treated identically to package imports.
        // Same root cause as B2 but different user expectation.
        assert!(
            imports.is_empty(),
            "expected empty imports for tsconfig alias, got {:?}",
            imports
        );
    }

    // TC-06: B4 — .enum.ts in production_files is NOT filtered (production-aware bypass)
    #[test]
    fn boundary_b4_enum_primary_target_filtered() {
        use tempfile::TempDir;

        // Given:
        //   src/route-paramtypes.enum.ts (production)
        //   test/route.spec.ts: `import { RouteParamtypes } from '../src/route-paramtypes.enum'`
        let dir = TempDir::new().unwrap();
        let src_dir = dir.path().join("src");
        let test_dir = dir.path().join("test");
        std::fs::create_dir_all(&src_dir).unwrap();
        std::fs::create_dir_all(&test_dir).unwrap();

        let prod_path = src_dir.join("route-paramtypes.enum.ts");
        std::fs::File::create(&prod_path).unwrap();

        let test_path = test_dir.join("route.spec.ts");
        std::fs::write(
            &test_path,
            "import { RouteParamtypes } from '../src/route-paramtypes.enum';\ndescribe('Route', () => {});",
        )
        .unwrap();

        let production_files = vec![prod_path.to_string_lossy().into_owned()];
        let mut test_sources = HashMap::new();
        test_sources.insert(
            test_path.to_string_lossy().into_owned(),
            std::fs::read_to_string(&test_path).unwrap(),
        );

        let extractor = TypeScriptExtractor::new();

        // When: map_test_files_with_imports
        let mappings =
            extractor.map_test_files_with_imports(&production_files, &test_sources, dir.path());

        // Then: route-paramtypes.enum.ts IS mapped to route.spec.ts (production-aware bypass)
        let enum_mapping = mappings
            .iter()
            .find(|m| m.production_file.ends_with("route-paramtypes.enum.ts"));
        assert!(
            enum_mapping.is_some(),
            "expected mapping for route-paramtypes.enum.ts"
        );
        let enum_mapping = enum_mapping.unwrap();
        assert!(
            !enum_mapping.test_files.is_empty(),
            "expected test_files for route-paramtypes.enum.ts (production file), got empty"
        );
    }

    // TC-07: B4 — .interface.ts in production_files is NOT filtered (production-aware bypass)
    #[test]
    fn boundary_b4_interface_primary_target_filtered() {
        use tempfile::TempDir;

        // Given:
        //   src/user.interface.ts (production)
        //   test/user.spec.ts: `import { User } from '../src/user.interface'`
        let dir = TempDir::new().unwrap();
        let src_dir = dir.path().join("src");
        let test_dir = dir.path().join("test");
        std::fs::create_dir_all(&src_dir).unwrap();
        std::fs::create_dir_all(&test_dir).unwrap();

        let prod_path = src_dir.join("user.interface.ts");
        std::fs::File::create(&prod_path).unwrap();

        let test_path = test_dir.join("user.spec.ts");
        std::fs::write(
            &test_path,
            "import { User } from '../src/user.interface';\ndescribe('User', () => {});",
        )
        .unwrap();

        let production_files = vec![prod_path.to_string_lossy().into_owned()];
        let mut test_sources = HashMap::new();
        test_sources.insert(
            test_path.to_string_lossy().into_owned(),
            std::fs::read_to_string(&test_path).unwrap(),
        );

        let extractor = TypeScriptExtractor::new();

        // When: map_test_files_with_imports
        let mappings =
            extractor.map_test_files_with_imports(&production_files, &test_sources, dir.path());

        // Then: user.interface.ts IS mapped to user.spec.ts (production-aware bypass)
        let iface_mapping = mappings
            .iter()
            .find(|m| m.production_file.ends_with("user.interface.ts"));
        assert!(
            iface_mapping.is_some(),
            "expected mapping for user.interface.ts"
        );
        let iface_mapping = iface_mapping.unwrap();
        assert!(
            !iface_mapping.test_files.is_empty(),
            "expected test_files for user.interface.ts (production file), got empty"
        );
    }

    // TC-08: Boundary B5 — dynamic import() is not captured by extract_imports
    #[test]
    fn boundary_b5_dynamic_import_not_extracted() {
        // Given: fixture("import_dynamic.ts") containing `const m = await import('./user.service')`
        let source = fixture("import_dynamic.ts");
        let extractor = TypeScriptExtractor::new();

        // When: extract_imports
        let imports = extractor.extract_imports(&source, "import_dynamic.ts");

        // Then: imports is empty (dynamic import() not captured by import_mapping.scm)
        assert!(
            imports.is_empty(),
            "expected empty imports for dynamic import(), got {:?}",
            imports
        );
    }

    // === tsconfig alias integration tests (OB-01 to OB-06) ===

    // OB-01: tsconfig alias basic — @app/foo.service -> src/foo.service.ts
    #[test]
    fn test_observe_tsconfig_alias_basic() {
        use tempfile::TempDir;

        // Given:
        //   tsconfig.json: @app/* -> src/*
        //   src/foo.service.ts (production)
        //   test/foo.service.spec.ts: `import { FooService } from '@app/foo.service'`
        let dir = TempDir::new().unwrap();
        let src_dir = dir.path().join("src");
        let test_dir = dir.path().join("test");
        std::fs::create_dir_all(&src_dir).unwrap();
        std::fs::create_dir_all(&test_dir).unwrap();

        let tsconfig = dir.path().join("tsconfig.json");
        std::fs::write(
            &tsconfig,
            r#"{"compilerOptions":{"baseUrl":".","paths":{"@app/*":["src/*"]}}}"#,
        )
        .unwrap();

        let prod_path = src_dir.join("foo.service.ts");
        std::fs::File::create(&prod_path).unwrap();

        let test_path = test_dir.join("foo.service.spec.ts");
        let test_source =
            "import { FooService } from '@app/foo.service';\ndescribe('FooService', () => {});\n";
        std::fs::write(&test_path, test_source).unwrap();

        let production_files = vec![prod_path.to_string_lossy().into_owned()];
        let mut test_sources = HashMap::new();
        test_sources.insert(
            test_path.to_string_lossy().into_owned(),
            test_source.to_string(),
        );

        let extractor = TypeScriptExtractor::new();

        // When: map_test_files_with_imports
        let mappings =
            extractor.map_test_files_with_imports(&production_files, &test_sources, dir.path());

        // Then: foo.service.ts is mapped to foo.service.spec.ts via alias resolution
        let mapping = mappings
            .iter()
            .find(|m| m.production_file.contains("foo.service.ts"))
            .expect("expected mapping for foo.service.ts");
        assert!(
            mapping
                .test_files
                .contains(&test_path.to_string_lossy().into_owned()),
            "expected foo.service.spec.ts in mapping via alias, got {:?}",
            mapping.test_files
        );
    }

    // OB-02: no tsconfig -> alias import produces no mapping
    #[test]
    fn test_observe_no_tsconfig_alias_ignored() {
        use tempfile::TempDir;

        // Given:
        //   NO tsconfig.json
        //   src/foo.service.ts (production)
        //   test/foo.service.spec.ts: `import { FooService } from '@app/foo.service'`
        let dir = TempDir::new().unwrap();
        let src_dir = dir.path().join("src");
        let test_dir = dir.path().join("test");
        std::fs::create_dir_all(&src_dir).unwrap();
        std::fs::create_dir_all(&test_dir).unwrap();

        let prod_path = src_dir.join("foo.service.ts");
        std::fs::File::create(&prod_path).unwrap();

        let test_path = test_dir.join("foo.service.spec.ts");
        let test_source =
            "import { FooService } from '@app/foo.service';\ndescribe('FooService', () => {});\n";

        let production_files = vec![prod_path.to_string_lossy().into_owned()];
        let mut test_sources = HashMap::new();
        test_sources.insert(
            test_path.to_string_lossy().into_owned(),
            test_source.to_string(),
        );

        let extractor = TypeScriptExtractor::new();

        // When: map_test_files_with_imports (no tsconfig.json present)
        let mappings =
            extractor.map_test_files_with_imports(&production_files, &test_sources, dir.path());

        // Then: no test_files mapped (alias import skipped without tsconfig)
        let all_test_files: Vec<&String> =
            mappings.iter().flat_map(|m| m.test_files.iter()).collect();
        assert!(
            all_test_files.is_empty(),
            "expected no test_files when tsconfig absent, got {:?}",
            all_test_files
        );
    }

    // OB-03: tsconfig alias + barrel -> resolves via barrel
    #[test]
    fn test_observe_tsconfig_alias_barrel() {
        use tempfile::TempDir;

        // Given:
        //   tsconfig: @app/* -> src/*
        //   src/bar.service.ts (production)
        //   src/services/index.ts (barrel): `export { BarService } from '../bar.service'`
        //   test/bar.service.spec.ts: `import { BarService } from '@app/services'`
        let dir = TempDir::new().unwrap();
        let src_dir = dir.path().join("src");
        let services_dir = src_dir.join("services");
        let test_dir = dir.path().join("test");
        std::fs::create_dir_all(&services_dir).unwrap();
        std::fs::create_dir_all(&test_dir).unwrap();

        std::fs::write(
            dir.path().join("tsconfig.json"),
            r#"{"compilerOptions":{"baseUrl":".","paths":{"@app/*":["src/*"]}}}"#,
        )
        .unwrap();

        let prod_path = src_dir.join("bar.service.ts");
        std::fs::File::create(&prod_path).unwrap();

        std::fs::write(
            services_dir.join("index.ts"),
            "export { BarService } from '../bar.service';\n",
        )
        .unwrap();

        let test_path = test_dir.join("bar.service.spec.ts");
        let test_source =
            "import { BarService } from '@app/services';\ndescribe('BarService', () => {});\n";
        std::fs::write(&test_path, test_source).unwrap();

        let production_files = vec![prod_path.to_string_lossy().into_owned()];
        let mut test_sources = HashMap::new();
        test_sources.insert(
            test_path.to_string_lossy().into_owned(),
            test_source.to_string(),
        );

        let extractor = TypeScriptExtractor::new();

        // When: map_test_files_with_imports
        let mappings =
            extractor.map_test_files_with_imports(&production_files, &test_sources, dir.path());

        // Then: bar.service.ts is mapped via alias + barrel resolution
        let mapping = mappings
            .iter()
            .find(|m| m.production_file.contains("bar.service.ts"))
            .expect("expected mapping for bar.service.ts");
        assert!(
            mapping
                .test_files
                .contains(&test_path.to_string_lossy().into_owned()),
            "expected bar.service.spec.ts mapped via alias+barrel, got {:?}",
            mapping.test_files
        );
    }

    // OB-04: mixed relative + alias imports -> both resolved
    #[test]
    fn test_observe_tsconfig_alias_mixed() {
        use tempfile::TempDir;

        // Given:
        //   tsconfig: @app/* -> src/*
        //   src/foo.service.ts, src/bar.service.ts (productions)
        //   test/mixed.spec.ts:
        //     `import { FooService } from '@app/foo.service'`   (alias)
        //     `import { BarService } from '../src/bar.service'` (relative)
        let dir = TempDir::new().unwrap();
        let src_dir = dir.path().join("src");
        let test_dir = dir.path().join("test");
        std::fs::create_dir_all(&src_dir).unwrap();
        std::fs::create_dir_all(&test_dir).unwrap();

        std::fs::write(
            dir.path().join("tsconfig.json"),
            r#"{"compilerOptions":{"baseUrl":".","paths":{"@app/*":["src/*"]}}}"#,
        )
        .unwrap();

        let foo_path = src_dir.join("foo.service.ts");
        let bar_path = src_dir.join("bar.service.ts");
        std::fs::File::create(&foo_path).unwrap();
        std::fs::File::create(&bar_path).unwrap();

        let test_path = test_dir.join("mixed.spec.ts");
        let test_source = "\
import { FooService } from '@app/foo.service';
import { BarService } from '../src/bar.service';
describe('Mixed', () => {});
";
        std::fs::write(&test_path, test_source).unwrap();

        let production_files = vec![
            foo_path.to_string_lossy().into_owned(),
            bar_path.to_string_lossy().into_owned(),
        ];
        let mut test_sources = HashMap::new();
        test_sources.insert(
            test_path.to_string_lossy().into_owned(),
            test_source.to_string(),
        );

        let extractor = TypeScriptExtractor::new();

        // When: map_test_files_with_imports
        let mappings =
            extractor.map_test_files_with_imports(&production_files, &test_sources, dir.path());

        // Then: both foo.service.ts and bar.service.ts are mapped
        let foo_mapping = mappings
            .iter()
            .find(|m| m.production_file.contains("foo.service.ts"))
            .expect("expected mapping for foo.service.ts");
        assert!(
            foo_mapping
                .test_files
                .contains(&test_path.to_string_lossy().into_owned()),
            "expected mixed.spec.ts in foo mapping, got {:?}",
            foo_mapping.test_files
        );
        let bar_mapping = mappings
            .iter()
            .find(|m| m.production_file.contains("bar.service.ts"))
            .expect("expected mapping for bar.service.ts");
        assert!(
            bar_mapping
                .test_files
                .contains(&test_path.to_string_lossy().into_owned()),
            "expected mixed.spec.ts in bar mapping, got {:?}",
            bar_mapping.test_files
        );
    }

    // OB-05: tsconfig alias + is_non_sut_helper filter -> constants.ts is excluded
    #[test]
    fn test_observe_tsconfig_alias_helper_filtered() {
        use tempfile::TempDir;

        // Given:
        //   tsconfig: @app/* -> src/*
        //   src/constants.ts (production, but filtered by is_non_sut_helper)
        //   test/constants.spec.ts: `import { APP_NAME } from '@app/constants'`
        let dir = TempDir::new().unwrap();
        let src_dir = dir.path().join("src");
        let test_dir = dir.path().join("test");
        std::fs::create_dir_all(&src_dir).unwrap();
        std::fs::create_dir_all(&test_dir).unwrap();

        std::fs::write(
            dir.path().join("tsconfig.json"),
            r#"{"compilerOptions":{"baseUrl":".","paths":{"@app/*":["src/*"]}}}"#,
        )
        .unwrap();

        let prod_path = src_dir.join("constants.ts");
        std::fs::File::create(&prod_path).unwrap();

        let test_path = test_dir.join("constants.spec.ts");
        let test_source =
            "import { APP_NAME } from '@app/constants';\ndescribe('Constants', () => {});\n";
        std::fs::write(&test_path, test_source).unwrap();

        let production_files = vec![prod_path.to_string_lossy().into_owned()];
        let mut test_sources = HashMap::new();
        test_sources.insert(
            test_path.to_string_lossy().into_owned(),
            test_source.to_string(),
        );

        let extractor = TypeScriptExtractor::new();

        // When: map_test_files_with_imports
        let mappings =
            extractor.map_test_files_with_imports(&production_files, &test_sources, dir.path());

        // Then: constants.ts is filtered by is_non_sut_helper → no test_files
        let all_test_files: Vec<&String> =
            mappings.iter().flat_map(|m| m.test_files.iter()).collect();
        assert!(
            all_test_files.is_empty(),
            "expected constants.ts filtered by is_non_sut_helper, got {:?}",
            all_test_files
        );
    }

    // OB-06: alias to nonexistent file -> no mapping, no error
    #[test]
    fn test_observe_tsconfig_alias_nonexistent() {
        use tempfile::TempDir;

        // Given:
        //   tsconfig: @app/* -> src/*
        //   src/foo.service.ts (production)
        //   test/nonexistent.spec.ts: `import { Missing } from '@app/nonexistent'`
        //   (src/nonexistent.ts does NOT exist)
        let dir = TempDir::new().unwrap();
        let src_dir = dir.path().join("src");
        let test_dir = dir.path().join("test");
        std::fs::create_dir_all(&src_dir).unwrap();
        std::fs::create_dir_all(&test_dir).unwrap();

        std::fs::write(
            dir.path().join("tsconfig.json"),
            r#"{"compilerOptions":{"baseUrl":".","paths":{"@app/*":["src/*"]}}}"#,
        )
        .unwrap();

        let prod_path = src_dir.join("foo.service.ts");
        std::fs::File::create(&prod_path).unwrap();

        let test_path = test_dir.join("nonexistent.spec.ts");
        let test_source =
            "import { Missing } from '@app/nonexistent';\ndescribe('Nonexistent', () => {});\n";
        std::fs::write(&test_path, test_source).unwrap();

        let production_files = vec![prod_path.to_string_lossy().into_owned()];
        let mut test_sources = HashMap::new();
        test_sources.insert(
            test_path.to_string_lossy().into_owned(),
            test_source.to_string(),
        );

        let extractor = TypeScriptExtractor::new();

        // When: map_test_files_with_imports (should not panic)
        let mappings =
            extractor.map_test_files_with_imports(&production_files, &test_sources, dir.path());

        // Then: no mapping (nonexistent.ts not in production_files), no panic
        let all_test_files: Vec<&String> =
            mappings.iter().flat_map(|m| m.test_files.iter()).collect();
        assert!(
            all_test_files.is_empty(),
            "expected no mapping for alias to nonexistent file, got {:?}",
            all_test_files
        );
    }

    // B3-update: boundary_b3_tsconfig_alias_resolved
    // With tsconfig.json present, @app/* alias SHOULD be resolved (FN → TP)
    #[test]
    fn boundary_b3_tsconfig_alias_resolved() {
        use tempfile::TempDir;

        // Given:
        //   tsconfig.json: @app/* -> src/*
        //   src/foo.service.ts (production)
        //   test/foo.service.spec.ts: `import { FooService } from '@app/services/foo.service'`
        let dir = TempDir::new().unwrap();
        let src_dir = dir.path().join("src");
        let services_dir = src_dir.join("services");
        let test_dir = dir.path().join("test");
        std::fs::create_dir_all(&services_dir).unwrap();
        std::fs::create_dir_all(&test_dir).unwrap();

        std::fs::write(
            dir.path().join("tsconfig.json"),
            r#"{"compilerOptions":{"baseUrl":".","paths":{"@app/*":["src/*"]}}}"#,
        )
        .unwrap();

        let prod_path = services_dir.join("foo.service.ts");
        std::fs::File::create(&prod_path).unwrap();

        let test_path = test_dir.join("foo.service.spec.ts");
        let test_source = "import { FooService } from '@app/services/foo.service';\ndescribe('FooService', () => {});\n";
        std::fs::write(&test_path, test_source).unwrap();

        let production_files = vec![prod_path.to_string_lossy().into_owned()];
        let mut test_sources = HashMap::new();
        test_sources.insert(
            test_path.to_string_lossy().into_owned(),
            test_source.to_string(),
        );

        let extractor = TypeScriptExtractor::new();

        // When: map_test_files_with_imports WITH tsconfig present
        let mappings =
            extractor.map_test_files_with_imports(&production_files, &test_sources, dir.path());

        // Then: foo.service.ts IS mapped (B3 resolved — FN → TP)
        let mapping = mappings
            .iter()
            .find(|m| m.production_file.contains("foo.service.ts"))
            .expect("expected FileMapping for foo.service.ts");
        assert!(
            mapping
                .test_files
                .contains(&test_path.to_string_lossy().into_owned()),
            "expected tsconfig alias to be resolved (B3 fix), got {:?}",
            mapping.test_files
        );
    }

    // TC-09: Boundary B6 — import target outside scan_root is not mapped
    #[test]
    fn boundary_b6_import_outside_scan_root() {
        use tempfile::TempDir;

        // Given:
        //   packages/core/ (scan_root)
        //   packages/core/src/foo.service.ts (production)
        //   packages/common/src/shared.ts (outside scan_root)
        //   packages/core/test/foo.spec.ts: `import { Shared } from '../../common/src/shared'`
        let dir = TempDir::new().unwrap();
        let core_src = dir.path().join("packages").join("core").join("src");
        let core_test = dir.path().join("packages").join("core").join("test");
        let common_src = dir.path().join("packages").join("common").join("src");
        std::fs::create_dir_all(&core_src).unwrap();
        std::fs::create_dir_all(&core_test).unwrap();
        std::fs::create_dir_all(&common_src).unwrap();

        let prod_path = core_src.join("foo.service.ts");
        std::fs::File::create(&prod_path).unwrap();

        // shared.ts is outside scan_root (packages/core/)
        let shared_path = common_src.join("shared.ts");
        std::fs::File::create(&shared_path).unwrap();

        let test_path = core_test.join("foo.spec.ts");
        std::fs::write(
            &test_path,
            "import { Shared } from '../../common/src/shared';\ndescribe('Foo', () => {});",
        )
        .unwrap();

        let scan_root = dir.path().join("packages").join("core");
        // Only production files within scan_root are registered
        let production_files = vec![prod_path.to_string_lossy().into_owned()];
        let mut test_sources = HashMap::new();
        test_sources.insert(
            test_path.to_string_lossy().into_owned(),
            std::fs::read_to_string(&test_path).unwrap(),
        );

        let extractor = TypeScriptExtractor::new();

        // When: map_test_files_with_imports(scan_root=packages/core/)
        let mappings =
            extractor.map_test_files_with_imports(&production_files, &test_sources, &scan_root);

        // Then: shared.ts outside scan_root is not in production_files, so no mapping occurs.
        // `../../common/src/shared` resolves outside scan_root; it won't be in production_files
        // and won't match foo.service.ts by filename either.
        let all_test_files: Vec<&String> =
            mappings.iter().flat_map(|m| m.test_files.iter()).collect();
        assert!(
            all_test_files.is_empty(),
            "expected no test_files (import target outside scan_root), got {:?}",
            all_test_files
        );
    }
}