code-kb-core 2.5.1

Core library for code-kb AST fact querying, slicing, and progressive disclosure
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
use code_kb_core::{
    Workspace, compute_blast_radius, file_skeleton_op, find_julie_extract_binary,
    find_references_for_symbol, find_references_scoped, format_blast_radius, format_references,
    fts_search_symbols_scoped, open_read_only, open_read_write, safe_tempdir, scan_workspace,
    search_symbols_scoped,
};
use std::fs;

fn scanned_repo(files: &[(&str, &str)]) -> (tempfile::TempDir, std::path::PathBuf) {
    find_julie_extract_binary().expect("julie-extract binary must be present for tests");
    let temp_dir = safe_tempdir();
    let root = temp_dir.path().to_path_buf();
    for (path, content) in files {
        let full = root.join(path);
        fs::create_dir_all(full.parent().unwrap()).unwrap();
        fs::write(full, content).unwrap();
    }
    let db_path = root.join(".code-kb").join("artifact.db");
    scan_workspace(&Workspace::new(root), &db_path, true).expect("scan failed");
    (temp_dir, db_path)
}

#[test]
fn member_access_rejects_a_different_receiver() {
    let (_repo, db) = scanned_repo(&[
        (
            "web/runners.py",
            "class Runner:\n    def invoke(self):\n        pass\n\nclass Shell:\n    def invoke(self):\n        pass\n",
        ),
        (
            "web/builders.py",
            "from web.runners import Runner, Shell\n\ndef make_runner() -> Runner:\n    return Runner()\n\ndef make_shell() -> Shell:\n    return Shell()\n",
        ),
        (
            "tests/conftest.py",
            "import pytest\nfrom web.runners import Runner\n\n@pytest.fixture\ndef runner():\n    return Runner()\n",
        ),
        (
            "other/tests/conftest.py",
            "import pytest\nfrom web.runners import Shell\n\n@pytest.fixture\ndef runner():\n    return Shell()\n",
        ),
        (
            "tests/test_cli.py",
            "from web.builders import make_runner, make_shell\n\ndef test_local():\n    runner = make_runner()\n    return runner.invoke\n\ndef test_fixture(runner):\n    return runner.invoke\n\ndef test_wrong():\n    runner = make_shell()\n    return runner.invoke\n",
        ),
        (
            "other/tests/test_cli.py",
            "def test_other_fixture(runner):\n    return runner.invoke\n",
        ),
    ]);
    let conn = open_read_only(&db).unwrap();
    let callers = |name| {
        let rows = find_references_scoped(&conn, name, "callers", 20, false, None).unwrap();
        assert!(
            rows.iter().all(|row| row.kind == "member_access"),
            "{rows:?}"
        );
        caller_names(&rows)
    };

    assert_eq!(callers("Runner.invoke"), ["test_fixture", "test_local"]);
    assert_eq!(
        callers("Shell.invoke"),
        ["test_other_fixture", "test_wrong"]
    );
}

#[test]
fn resolved_same_file_builder_calls_disambiguate_annotated_receivers() {
    let (_repo, db) = scanned_repo(&[
        (
            "pkg/server.py",
            "class Server:\n    def ctx(self):\n        pass\n",
        ),
        (
            "other/server.py",
            "class Server:\n    def ctx(self):\n        pass\n",
        ),
        (
            "pkg/cli.py",
            "from pkg.server import Server\n\ndef load() -> Server:\n    return Server()\n\ndef run():\n    app = load()\n    app.ctx()\n",
        ),
    ]);
    let conn = open_read_only(&db).unwrap();

    let package_server = find_references_scoped(
        &conn,
        "Server.ctx",
        "callers",
        20,
        false,
        Some("pkg/server.py"),
    )
    .unwrap();
    let rival_server = find_references_scoped(
        &conn,
        "Server.ctx",
        "callers",
        20,
        false,
        Some("other/server.py"),
    )
    .unwrap();

    assert_eq!(caller_names(&package_server), ["run"]);
    assert!(rival_server.is_empty(), "{rival_server:?}");
}

#[test]
fn a_nested_same_file_helper_call_keeps_the_constructor_receiver_supported() {
    let (_repo, db) = scanned_repo(&[
        (
            "pkg/cfg.py",
            "class Config:\n    def run(self):\n        pass\n",
        ),
        (
            "pkg/app.py",
            "from pkg.cfg import Config\n\ndef make_name():\n    return 'x'\n\ndef other(register):\n    c = Config(make_name())\n    register(c.run)\n",
        ),
    ]);
    let conn = open_read_only(&db).unwrap();

    let rows =
        find_references_scoped(&conn, "run", "callers", 20, false, Some("pkg/cfg.py")).unwrap();

    assert_eq!(caller_names(&rows), ["other"]);
    assert!(
        rows.iter().all(|row| row.kind == "member_access"),
        "{rows:?}"
    );
}

#[test]
fn name_only_type_usages_cross_the_javascript_family_languages() {
    let (_repo, db) = scanned_repo(&[
        ("src/model.ts", "export class Store {\n  size = 0;\n}\n"),
        (
            "src/view.tsx",
            "import { Store } from './model';\nexport function View(p: { store: Store }) {\n  const s: Store = p.store;\n  return s;\n}\n",
        ),
        (
            "src/other.ts",
            "import { Store } from './model';\nexport function other(): Store {\n  return new Store();\n}\n",
        ),
    ]);
    let conn = open_read_only(&db).unwrap();
    let store_id: String = conn
        .query_row(
            "SELECT symbol_id FROM symbols WHERE path = 'src/model.ts' AND name = 'Store' AND kind = 'class'",
            [],
            |row| row.get(0),
        )
        .unwrap();

    let rows = find_references_for_symbol(&conn, "Store", "callers", 20, &store_id).unwrap();

    assert!(
        rows.iter()
            .any(|row| row.kind == "type_usage" && row.path == "src/view.tsx"),
        "{rows:?}"
    );
}

#[test]
fn resolved_same_file_builder_calls_keep_lexical_identity() {
    let (_repo, db) = scanned_repo(&[(
        "app.py",
        "class A:\n    def ctx(self):\n        pass\n\nclass B:\n    def ctx(self):\n        pass\n\ndef load() -> A:\n    return A()\n\ndef run():\n    app = load()\n    app.ctx()\n    return app.ctx\n\ndef outer():\n    def load() -> B:\n        return B()\n    app = load()\n    app.ctx()\n    return app.ctx\n",
    )]);
    let conn = open_read_only(&db).unwrap();
    let callers = |target| {
        find_references_scoped(&conn, target, "callers", 20, false, Some("app.py")).unwrap()
    };

    let a_callers = callers("A.ctx");
    let b_callers = callers("B.ctx");
    assert_eq!(caller_names(&a_callers), ["run"]);
    assert_eq!(caller_names(&b_callers), ["outer"]);
    assert_eq!(
        caller_names(
            &a_callers
                .iter()
                .filter(|row| row.kind == "member_access")
                .cloned()
                .collect::<Vec<_>>()
        ),
        ["run"]
    );
    assert_eq!(
        caller_names(
            &b_callers
                .iter()
                .filter(|row| row.kind == "member_access")
                .cloned()
                .collect::<Vec<_>>()
        ),
        ["outer"]
    );
}

#[test]
fn inferred_same_file_builder_returns_disambiguate_flask_receivers() {
    let (_repo, db) = scanned_repo(&[
        (
            "pkg/flask.py",
            "class Flask:\n    def test_client(self):\n        pass\n",
        ),
        (
            "other/flask.py",
            "class Flask:\n    def test_client(self):\n        pass\n",
        ),
        (
            "pkg/app.py",
            "from pkg.flask import Flask\n\ndef create_app():\n    app = Flask()\n    return app\n\ndef run():\n    app = create_app()\n    app.test_client()\n",
        ),
    ]);
    // The committed extractor pin predates inferredReturnType; model that newer
    // metadata here so this consumer regression also runs against the pinned binary.
    let conn = open_read_write(&db).unwrap();
    let updated = conn
        .execute(
            "UPDATE symbols
             SET metadata_json = json_set(COALESCE(metadata_json, '{}'),
                                         '$.returnType', '',
                                         '$.inferredReturnType', 'Flask')
             WHERE name = 'create_app' AND path = 'pkg/app.py' AND kind = 'function'",
            [],
        )
        .unwrap();
    assert_eq!(updated, 1);
    drop(conn);
    let conn = open_read_only(&db).unwrap();

    let package_flask = find_references_scoped(
        &conn,
        "Flask.test_client",
        "callers",
        20,
        false,
        Some("pkg/flask.py"),
    )
    .unwrap();
    let rival_flask = find_references_scoped(
        &conn,
        "Flask.test_client",
        "callers",
        20,
        false,
        Some("other/flask.py"),
    )
    .unwrap();

    assert_eq!(caller_names(&package_flask), ["run"]);
    assert!(rival_flask.is_empty(), "{rival_flask:?}");
}

#[test]
fn a_relative_package_import_keeps_the_class_that_a_builder_returns() {
    let (_repo, db) = scanned_repo(&[
        (
            "pkg/app.py",
            "class Server:\n    def ctx(self):\n        pass\n",
        ),
        ("pkg/__init__.py", "from .app import Server\n"),
        (
            "pkg/factory.py",
            "from .app import Server\n\ndef load() -> Server:\n    from . import Server\n    return Server()\n",
        ),
        (
            "pkg/cli.py",
            "from .factory import load\n\ndef run():\n    app = load()\n    app.ctx()\n",
        ),
        (
            "other/app.py",
            "class Server:\n    def ctx(self):\n        pass\n",
        ),
    ]);
    let conn = open_read_only(&db).unwrap();
    let callers = |path| {
        let rows =
            find_references_scoped(&conn, "Server.ctx", "callers", 20, false, Some(path)).unwrap();
        caller_names(&rows)
    };

    assert_eq!(callers("pkg/app.py"), ["run"]);
    assert!(callers("other/app.py").is_empty());
}

#[test]
fn a_class_defined_after_a_same_named_import_is_the_one_a_builder_returns() {
    let (_repo, db) = scanned_repo(&[
        (
            "other/runner.py",
            "class Runner:\n    def invoke(self):\n        pass\n",
        ),
        (
            "client.py",
            "from other.runner import Runner\n\nclass Runner:\n    def invoke(self):\n        pass\n\ndef make() -> Runner:\n    return Runner()\n",
        ),
        (
            "use.py",
            "from client import make\n\ndef use():\n    runner = make()\n    runner.invoke()\n",
        ),
    ]);
    let conn = open_read_only(&db).unwrap();
    let callers = |path| {
        let rows = find_references_scoped(&conn, "Runner.invoke", "callers", 20, false, Some(path))
            .unwrap();
        caller_names(&rows)
    };

    assert_eq!(callers("client.py"), ["use"]);
    assert!(callers("other/runner.py").is_empty());
}

#[test]
fn a_base_instance_does_not_reference_a_subclass_override() {
    let (_repo, db) = scanned_repo(&[
        (
            "web/testing.py",
            "from click.testing import CliRunner\n\nclass FlaskCliRunner(CliRunner):\n    def invoke(self):\n        pass\n",
        ),
        (
            "tests/conftest.py",
            "import pytest\nfrom click.testing import CliRunner\nfrom web.testing import FlaskCliRunner\n\n@pytest.fixture\ndef runner():\n    return CliRunner()\n\n@pytest.fixture\ndef flask_runner():\n    return FlaskCliRunner()\n",
        ),
        (
            "tests/test_cli.py",
            "from click.testing import CliRunner\nfrom web.testing import FlaskCliRunner\n\ndef test_base_fixture(runner):\n    return runner.invoke\n\ndef test_subclass_fixture(flask_runner):\n    return flask_runner.invoke\n\ndef test_base_local():\n    runner = CliRunner()\n    return runner.invoke\n\ndef test_subclass_local():\n    runner = FlaskCliRunner()\n    return runner.invoke\n",
        ),
    ]);
    let conn = open_read_only(&db).unwrap();
    let rows =
        find_references_scoped(&conn, "FlaskCliRunner.invoke", "callers", 20, false, None).unwrap();

    assert_eq!(
        caller_names(&rows),
        ["test_subclass_fixture", "test_subclass_local"]
    );
}

#[test]
fn member_access_uses_the_nearest_inherited_definition() {
    let (_repo, db) = scanned_repo(&[
        (
            "web/runners.py",
            "class Base:\n    def invoke(self):\n        pass\n\nclass Middle(Base):\n    def invoke(self):\n        pass\n\n    def read_own(self):\n        return self.invoke\n\nclass Leaf(Middle):\n    def read_inherited(self):\n        return self.invoke\n\n    def read_super(self):\n        return super().invoke()\n",
        ),
        (
            "clients.py",
            "from web.runners import Base, Leaf\n\ndef read_base():\n    runner = Base()\n    return runner.invoke\n\ndef read_leaf():\n    runner = Leaf()\n    return runner.invoke\n",
        ),
    ]);
    let conn = open_read_only(&db).unwrap();
    let base = find_references_scoped(&conn, "Base.invoke", "callers", 20, false, None).unwrap();
    let middle =
        find_references_scoped(&conn, "Middle.invoke", "callers", 20, false, None).unwrap();

    assert_eq!(caller_names(&base), ["read_base"]);
    assert_eq!(
        caller_names(&middle),
        ["read_inherited", "read_leaf", "read_own", "read_super"]
    );
}

#[test]
fn unresolved_member_access_is_a_candidate() {
    let (_repo, db) = scanned_repo(&[
        (
            "web/runners.py",
            "class Runner:\n    def invoke(self):\n        pass\n\nclass Shell:\n    def invoke(self):\n        pass\n",
        ),
        (
            "clients.py",
            "from web.runners import Runner, Shell\nfrom external import make_runner\n\ndef unknown(value):\n    return value.invoke\n\ndef untyped():\n    value = make_runner()\n    return value.invoke\n\ndef rebound(flag):\n    if flag:\n        value = Runner()\n    else:\n        value = Shell()\n    return value.invoke\n\ndef mixed(flag):\n    if flag:\n        value = Runner()\n    else:\n        value = make_runner()\n    return value.invoke\n\ndef conditional(flag):\n    value = Runner() if flag else make_runner()\n    return value.invoke\n\ndef later(value):\n    saved = value.invoke\n    value = Shell()\n    return saved\n",
        ),
        (
            "tests/conftest.py",
            "import pytest\nfrom web.runners import Runner\nfrom external import make_runner\n\n@pytest.fixture\ndef mixed_runner(flag):\n    return Runner() if flag else make_runner()\n",
        ),
        (
            "tests/test_flow.py",
            "def test_mixed_fixture(mixed_runner):\n    return mixed_runner.invoke\n",
        ),
    ]);
    let conn = open_read_only(&db).unwrap();
    for target in ["Runner.invoke", "Shell.invoke"] {
        let rows = find_references_scoped(&conn, target, "callers", 20, false, None).unwrap();
        assert_eq!(
            caller_names(&rows),
            [
                "conditional",
                "later",
                "mixed",
                "rebound",
                "test_mixed_fixture",
                "unknown",
                "untyped"
            ]
        );
        assert!(
            rows.iter()
                .all(|row| row.kind == "member_access (candidate)"),
            "{rows:?}"
        );
    }
}

#[test]
fn supported_member_access_precedes_candidates_at_the_limit() {
    let (_repo, db) = scanned_repo(&[
        (
            "web/runners.py",
            "class Runner:\n    def invoke(self):\n        pass\n\nclass Shell:\n    def invoke(self):\n        pass\n",
        ),
        (
            "a_unknown.py",
            "def unknown(value):\n    return value.invoke\n",
        ),
        (
            "b_wrong.py",
            "from web.runners import Shell\n\ndef mismatch():\n    value = Shell()\n    return value.invoke\n",
        ),
        (
            "z_supported.py",
            "from web.runners import Runner\n\ndef known():\n    value = Runner()\n    return value.invoke, value.invoke\n",
        ),
    ]);
    let conn = open_read_only(&db).unwrap();
    let id = member_of(&conn, "Runner", "invoke");
    let rows = find_references_scoped(
        &conn,
        "Runner.invoke",
        "callers",
        1,
        false,
        Some("web/runners.py"),
    )
    .unwrap();
    assert_eq!(caller_names(&rows), ["known"]);
    assert_eq!(rows[0].kind, "member_access");
    let rows =
        code_kb_core::find_references_for_symbol(&conn, "invoke", "callers", 2, &id).unwrap();
    assert_eq!(
        rows.iter()
            .map(|row| row.from_symbol_name.as_str())
            .collect::<Vec<_>>(),
        ["known", "unknown"]
    );
    assert_eq!(rows[1].kind, "member_access (candidate)");
    assert!(
        find_references_scoped(&conn, "Runner.invoke", "callers", 0, false, None)
            .unwrap()
            .is_empty()
    );
}

#[test]
fn find_references_stops_receiver_checks_after_filling_the_limit() {
    let unresolved = (0..1_000)
        .map(|index| format!("def unresolved_{index}(value):\n    return value.invoke\n"))
        .collect::<String>();
    let (_repo, db) = scanned_repo(&[
        (
            "web/runners.py",
            "class Runner:\n    def invoke(self):\n        pass\n",
        ),
        (
            "a_supported.py",
            "from web.runners import Runner\n\ndef known():\n    value = Runner()\n    return value.invoke\n",
        ),
        ("z_unresolved.py", &unresolved),
    ]);
    let conn = open_read_only(&db).unwrap();
    let id = member_of(&conn, "Runner", "invoke");
    let operations = std::sync::Arc::new(std::sync::atomic::AtomicUsize::new(0));
    let observed = operations.clone();
    conn.progress_handler(
        100,
        Some(move || observed.fetch_add(100, std::sync::atomic::Ordering::Relaxed) >= 200_000),
    )
    .unwrap();
    let result = find_references_for_symbol(&conn, "invoke", "callers", 1, &id);
    conn.progress_handler(0, None::<fn() -> bool>).unwrap();
    let rows = result.expect("one supported caller should fit within the receiver-check budget");

    assert_eq!(caller_names(&rows), ["known"]);
    assert_eq!(rows[0].kind, "member_access");
}

#[test]
fn caller_search_skips_builder_scope_for_helpers_without_class_returns() {
    const CALLERS: usize = 2_048;
    let call_sites = (0..CALLERS)
        .map(|index| {
            format!("def caller_{index}():\n    value = make_value()\n    return value.get()\n\n")
        })
        .collect::<String>();
    let callers = format!("from pkg.builders import make_value\n\n{call_sites}");
    let (_repo, db) = scanned_repo(&[
        (
            "pkg/widget.py",
            "class Widget:\n    def get(self):\n        pass\n",
        ),
        (
            "pkg/builders.py",
            "def make_value():\n    return object()\n",
        ),
        ("src/callers.py", &callers),
    ]);
    let conn = open_read_write(&db).unwrap();
    let pending_calls: i64 = conn
        .query_row(
            "SELECT COUNT(*) FROM pending_relationships WHERE target_terminal_name = 'get'",
            [],
            |row| row.get(0),
        )
        .unwrap();
    assert_eq!(pending_calls, CALLERS as i64);
    conn.execute("DELETE FROM identifiers WHERE name = 'get'", [])
        .unwrap();
    drop(conn);

    let conn = open_read_only(&db).unwrap();
    let operations = std::sync::Arc::new(std::sync::atomic::AtomicUsize::new(0));
    let observed = operations.clone();
    conn.progress_handler(
        100,
        Some(move || observed.fetch_add(100, std::sync::atomic::Ordering::Relaxed) >= 1_250_000),
    )
    .unwrap();
    let result = find_references_scoped(&conn, "Widget.get", "callers", 20, false, None);
    conn.progress_handler(0, None::<fn() -> bool>).unwrap();
    let rows = result.expect("untyped builders should not trigger a full-scope check per caller");

    assert!(rows.is_empty(), "{rows:?}");
}

#[test]
fn a_super_read_without_metadata_is_not_a_supported_reference_to_itself() {
    let (_repo, db) = scanned_repo(&[(
        "types.py",
        "class Base:\n    def convert(self):\n        return 1\n\nclass Paths(Base):\n    def convert(self):\n        base = super().convert\n        return base()\n",
    )]);
    let conn = open_read_write(&db).unwrap();
    conn.execute(
        "UPDATE identifiers SET metadata_json = NULL WHERE name = 'convert'",
        [],
    )
    .unwrap();
    let rows = find_references_scoped(&conn, "Paths.convert", "callers", 20, false, None).unwrap();
    assert!(!rows.is_empty());
    assert!(
        rows.iter()
            .all(|row| row.kind == "member_access (candidate)"),
        "{rows:?}"
    );
}

#[test]
fn a_css_selector_is_not_a_candidate_reference_to_a_method() {
    let (_repo, db) = scanned_repo(&[
        (
            "app.py",
            "class Scaffold:\n    def post(self):\n        pass\n",
        ),
        ("static/style.css", ".post > header {\n  color: red;\n}\n"),
    ]);
    let conn = open_read_only(&db).unwrap();
    let rows = find_references_scoped(&conn, "Scaffold.post", "callers", 20, false, None).unwrap();
    assert!(rows.is_empty(), "{rows:?}");
}

#[test]
fn member_access_with_missing_metadata_is_a_candidate() {
    let (_repo, db) = scanned_repo(&[(
        "runners.py",
        "class Runner:\n    def invoke(self):\n        pass\n\nclass Child(Runner):\n    def read(self):\n        return super().invoke\n",
    )]);
    let conn = open_read_write(&db).unwrap();
    for metadata in [None, Some("{")] {
        conn.execute(
            "UPDATE identifiers SET metadata_json = ?1 WHERE name = 'invoke'",
            [metadata],
        )
        .unwrap();
        let rows =
            find_references_scoped(&conn, "Runner.invoke", "callers", 20, false, None).unwrap();
        assert_eq!(caller_names(&rows), ["read"]);
        assert_eq!(rows[0].kind, "member_access (candidate)");
    }
}

#[test]
fn member_access_with_missing_metadata_never_reaches_a_non_callable_target() {
    let (_repo, db) = scanned_repo(&[(
        "counter.rs",
        "struct Counter { value: i32 }\nfn read(counter: &Counter) -> i32 { counter.value }\n",
    )]);
    let conn = open_read_write(&db).unwrap();
    for metadata in [None, Some("{")] {
        conn.execute(
            "UPDATE identifiers SET metadata_json = ?1 WHERE name = 'value'",
            [metadata],
        )
        .unwrap();
        let rows = find_references_scoped(&conn, "value", "callers", 20, false, None).unwrap();
        assert!(rows.is_empty(), "{rows:?}");
    }
}

#[test]
fn a_call_is_not_duplicated_as_a_candidate_member_access() {
    let (_repo, db) = scanned_repo(&[
        (
            "runner.py",
            "class Runner:\n    def invoke(self):\n        pass\n",
        ),
        (
            "client.py",
            "from runner import Runner\n\ndef run():\n    value = Runner()\n    value.invoke()\n",
        ),
    ]);
    let conn = open_read_only(&db).unwrap();
    let rows = find_references_scoped(&conn, "Runner.invoke", "callers", 20, false, None).unwrap();
    assert_eq!(rows.len(), 1, "{rows:?}");
    assert_eq!(rows[0].kind, "calls");
    assert_eq!(rows[0].from_symbol_name, "run");
}

#[test]
fn non_callable_member_access_keeps_its_kind_without_type_metadata() {
    let (_repo, db) = scanned_repo(&[(
        "counter.rs",
        "struct Counter { value: i32 }\nfn read(counter: &Counter) -> i32 { counter.value }\n",
    )]);
    let conn = open_read_write(&db).unwrap();
    conn.execute_batch("ALTER TABLE symbols RENAME COLUMN metadata_json TO legacy_metadata_json;")
        .unwrap();
    let rows = find_references_scoped(&conn, "value", "callers", 20, false, None).unwrap();
    assert_eq!(rows.len(), 1, "{rows:?}");
    assert_eq!(rows[0].kind, "member_access");
}

#[test]
fn find_references_reports_type_usages_of_a_struct() {
    let (_repo, db_path) = scanned_repo(&[
        (
            "src/config.rs",
            "pub struct Config {\n    pub path: String,\n}\n",
        ),
        (
            "src/loader.rs",
            "use crate::config::Config;\n\npub fn load(existing: &Config) -> String {\n    existing.path.clone()\n}\n",
        ),
    ]);
    let conn = open_read_only(&db_path).unwrap();

    let refs = find_references_scoped(&conn, "Config", "callers", 20, false, None).unwrap();

    let type_usage = refs
        .iter()
        .find(|r| r.kind == "type_usage")
        .unwrap_or_else(|| panic!("expected a type_usage reference, got {refs:?}"));
    assert_eq!(type_usage.from_symbol_name, "load");
    assert_eq!(type_usage.path, "src/loader.rs");
    assert_eq!(type_usage.start_line, Some(3));
}

#[test]
fn name_only_type_usages_stay_with_the_declaring_nested_class_and_language() {
    let (_repo, db_path) = scanned_repo(&[
        (
            "java/CustomTypeAdaptersTest.java",
            "package demo;\npublic class CustomTypeAdaptersTest {\n    public static class Foo {}\n    Foo value;\n    Foo use() { return new Foo(); }\n}\n",
        ),
        (
            "java/RawSerializationTest.java",
            "package demo;\npublic class RawSerializationTest {\n    public static class Foo {}\n    Foo use() { return new Foo(); }\n}\n",
        ),
        (
            "python/other.py",
            "class Foo:\n    pass\n\ndef use(value: Foo) -> Foo:\n    return Foo()\n",
        ),
    ]);
    let conn = open_read_only(&db_path).unwrap();

    let refs = find_references_scoped(
        &conn,
        "Foo",
        "callers",
        20,
        false,
        Some("java/CustomTypeAdaptersTest.java"),
    )
    .unwrap();

    let mut sites: Vec<_> = refs
        .iter()
        .map(|row| (row.kind.as_str(), row.path.as_str()))
        .collect();
    sites.sort_unstable();
    assert_eq!(
        sites,
        [
            ("calls", "java/CustomTypeAdaptersTest.java"),
            ("type_usage", "java/CustomTypeAdaptersTest.java"),
        ],
        "{refs:?}"
    );
}

#[test]
fn member_access_identifiers_do_not_cross_language_boundaries() {
    let (_repo, db_path) = scanned_repo(&[
        (
            "src/HTTPHeaders.java",
            "package demo;\npublic class HTTPHeaders {\n    public String header() { return \"\"; }\n}\n",
        ),
        (
            "src/reader.js",
            "export function read(headers) { return headers.header; }\n",
        ),
    ]);
    let conn = open_read_only(&db_path).unwrap();

    let refs = find_references_scoped(
        &conn,
        "HTTPHeaders.header",
        "callers",
        20,
        false,
        Some("src/HTTPHeaders.java"),
    )
    .unwrap();

    assert!(refs.is_empty(), "{refs:?}");
}

#[test]
fn name_only_type_usages_resolve_to_the_nearest_nested_type() {
    let (_repo, db_path) = scanned_repo(&[(
        "src/Outer.java",
        "package demo;\nclass Outer {\n    static class Foo {}\n    Foo outerField;\n    static class Inner {\n        static class Foo {}\n        Foo field;\n        Foo use(Foo value) { return value; }\n    }\n}\n",
    )]);
    let conn = open_read_only(&db_path).unwrap();
    let outer_foo_id: String = conn
        .query_row(
            "SELECT outer_foo.symbol_id FROM symbols outer_foo
             JOIN symbols outer_type ON outer_type.symbol_id = outer_foo.parent_symbol_id
             WHERE outer_foo.path = 'src/Outer.java'
               AND outer_foo.name = 'Foo' AND outer_foo.kind = 'class'
               AND outer_type.name = 'Outer'",
            [],
            |row| row.get(0),
        )
        .unwrap();
    let inner_foo_id: String = conn
        .query_row(
            "SELECT inner_foo.symbol_id FROM symbols inner_foo
             JOIN symbols inner_type ON inner_type.symbol_id = inner_foo.parent_symbol_id
             WHERE inner_foo.path = 'src/Outer.java'
               AND inner_foo.name = 'Foo' AND inner_foo.kind = 'class'
               AND inner_type.name = 'Inner'",
            [],
            |row| row.get(0),
        )
        .unwrap();

    let refs = find_references_for_symbol(&conn, "Foo", "callers", 20, &inner_foo_id).unwrap();

    let type_usages: Vec<_> = refs.iter().filter(|row| row.kind == "type_usage").collect();
    assert!(
        !type_usages.is_empty(),
        "the nearer Inner.Foo must retain its own type usages: {refs:?}"
    );
    assert!(
        type_usages.iter().all(|row| row.path == "src/Outer.java"),
        "{type_usages:?}"
    );

    let outer_refs =
        find_references_for_symbol(&conn, "Foo", "callers", 20, &outer_foo_id).unwrap();
    let outer_type_usages: Vec<_> = outer_refs
        .iter()
        .filter(|row| row.kind == "type_usage")
        .collect();
    assert_eq!(
        outer_type_usages
            .iter()
            .map(|row| row.start_line)
            .collect::<Vec<_>>(),
        [Some(4)],
        "Outer.Foo must not claim the nearer Inner.Foo usages: {outer_refs:?}"
    );
}

#[test]
fn name_only_type_usages_resolve_to_a_same_file_top_level_type_first() {
    let (_repo, db_path) = scanned_repo(&[
        (
            "One.java",
            "package a;\nclass Foo {}\nclass One {\n    Foo field;\n    Foo use(Foo value) { return value; }\n}\n",
        ),
        (
            "Two.java",
            "package b;\nclass Foo {}\nclass Two {\n    Foo field;\n    Foo use(Foo value) { return value; }\n}\n",
        ),
    ]);
    let conn = open_read_only(&db_path).unwrap();
    let one_foo_id: String = conn
        .query_row(
            "SELECT symbol_id FROM symbols
             WHERE path = 'One.java' AND name = 'Foo' AND kind = 'class'",
            [],
            |row| row.get(0),
        )
        .unwrap();

    let refs = find_references_for_symbol(&conn, "Foo", "callers", 20, &one_foo_id).unwrap();
    let type_usages: Vec<_> = refs.iter().filter(|row| row.kind == "type_usage").collect();

    assert!(!type_usages.is_empty(), "{refs:?}");
    assert!(
        type_usages.iter().all(|row| row.path == "One.java"),
        "same-named type usages in another package must not reach One.Foo: {type_usages:?}"
    );
}

#[test]
fn a_csharp_type_usage_reaches_a_razor_component() {
    let (_repo, db) = scanned_repo(&[
        (
            "Shared/Tip.razor",
            "<div class=\"tip\">@Text</div>\n@code {\n    [Parameter] public string Text { get; set; }\n}\n",
        ),
        (
            "Shared/Chart.cs",
            "public class Chart\n{\n    void Render(RenderTreeBuilder builder)\n    {\n        builder.OpenComponent<Tip>(0);\n    }\n}\n",
        ),
    ]);
    let conn = open_read_only(&db).unwrap();
    let tip_id: String = conn
        .query_row(
            "SELECT symbol_id FROM symbols WHERE path = 'Shared/Tip.razor' AND name = 'Tip'",
            [],
            |row| row.get(0),
        )
        .unwrap();

    let rows = find_references_for_symbol(&conn, "Tip", "callers", 20, &tip_id).unwrap();

    assert!(
        rows.iter()
            .any(|row| row.kind == "type_usage" && row.path == "Shared/Chart.cs"),
        "{rows:?}"
    );
}

#[test]
fn grouped_type_receiver_rows_skip_other_languages_and_nearer_types() {
    let (_repo, db) = scanned_repo(&[
        (
            "src/One.java",
            "class One {\n    static class Foo {\n        static int size = 1;\n    }\n    int use() { return Foo.size; }\n}\n",
        ),
        (
            "src/Two.java",
            "class Two {\n    private enum Foo { BAR }\n    Object use() { return Foo.BAR; }\n}\n",
        ),
        ("tools/other.py", "def use(Foo):\n    return Foo.size\n"),
    ]);
    let conn = open_read_only(&db).unwrap();
    let foo_id: String = conn
        .query_row(
            "SELECT symbol_id FROM symbols WHERE path = 'src/One.java' AND name = 'Foo' AND kind = 'class'",
            [],
            |row| row.get(0),
        )
        .unwrap();

    let rows = find_references_for_symbol(&conn, "Foo", "callers", 20, &foo_id).unwrap();

    assert!(
        rows.iter().all(|row| row.path == "src/One.java"),
        "{rows:?}"
    );
}

#[test]
fn name_only_identifiers_allow_only_the_supported_qml_and_razor_language_bridges() {
    let (_repo, db_path) = scanned_repo(&[
        (
            "src/BridgeWidget.cpp",
            "class BridgeWidget { public: int value; };\n",
        ),
        (
            "src/BridgeModel.cs",
            "public class BridgeModel { public string Title { get; set; } }\n",
        ),
        (
            "ui/View.qml",
            "import QtQuick\nItem {\n    property BridgeWidget widget\n    property int currentValue: widget.value\n}\n",
        ),
        (
            "ui/View.razor",
            "@inject BridgeModel Model\n<p>@Model.Title</p>\n",
        ),
        ("src/other.py", "def use(value):\n    return value\n"),
    ]);
    let conn = open_read_write(&db_path).unwrap();
    conn.execute_batch(
        r#"INSERT INTO reference_sites
            (reference_site_id, file_id, path, language, is_exact, provenance)
         SELECT 'rs_qml_bridge', file_id, path, language, 0, 'spanless'
         FROM files WHERE path = 'ui/View.qml';
         INSERT INTO reference_sites
            (reference_site_id, file_id, path, language, is_exact, provenance)
         SELECT 'rs_razor_bridge', file_id, path, language, 0, 'spanless'
         FROM files WHERE path = 'ui/View.razor';
         INSERT INTO reference_sites
            (reference_site_id, file_id, path, language, is_exact, provenance)
         SELECT 'rs_unrelated', file_id, path, language, 0, 'spanless'
         FROM files WHERE path = 'src/other.py';
         INSERT INTO identifiers
            (identifier_id, reference_site_id, file_id, path, language, name, kind,
             start_line, start_column, end_line, end_column, start_byte, end_byte,
             confidence, metadata_json)
         SELECT 'i_qml_bridge', 'rs_qml_bridge', file_id, path, language,
                'BridgeWidget', 'type_usage', 3, 13, 3, 24, 0, 11, 1.0, '{}'
         FROM files WHERE path = 'ui/View.qml';
         INSERT INTO identifiers
            (identifier_id, reference_site_id, file_id, path, language, name, kind,
             start_line, start_column, end_line, end_column, start_byte, end_byte,
             confidence, metadata_json)
         SELECT 'i_qml_member', 'rs_qml_bridge', file_id, path, language,
                'value', 'member_access', 4, 39, 4, 44, 0, 5, 1.0,
                '{"receiver":"widget"}'
         FROM files WHERE path = 'ui/View.qml';
         INSERT INTO identifiers
            (identifier_id, reference_site_id, file_id, path, language, name, kind,
             start_line, start_column, end_line, end_column, start_byte, end_byte,
             confidence, metadata_json)
         SELECT 'i_razor_bridge', 'rs_razor_bridge', file_id, path, language,
                'BridgeModel', 'type_usage', 1, 8, 1, 19, 0, 11, 1.0, '{}'
         FROM files WHERE path = 'ui/View.razor';
         INSERT INTO identifiers
            (identifier_id, reference_site_id, file_id, path, language, name, kind,
             start_line, start_column, end_line, end_column, start_byte, end_byte,
             confidence, metadata_json)
         SELECT 'i_razor_member', 'rs_razor_bridge', file_id, path, language,
                'Title', 'member_access', 2, 10, 2, 15, 0, 5, 1.0,
                '{"receiver":"Model"}'
         FROM files WHERE path = 'ui/View.razor';
         INSERT INTO identifiers
            (identifier_id, reference_site_id, file_id, path, language, name, kind,
             start_line, start_column, end_line, end_column, start_byte, end_byte,
             confidence, metadata_json)
         SELECT 'i_unrelated', 'rs_unrelated', file_id, path, language,
                'BridgeWidget', 'type_usage', 1, 0, 1, 12, 0, 12, 1.0, '{}'
         FROM files WHERE path = 'src/other.py';
         INSERT INTO identifiers
            (identifier_id, reference_site_id, file_id, path, language, name, kind,
             start_line, start_column, end_line, end_column, start_byte, end_byte,
             confidence, metadata_json)
         SELECT 'i_unrelated_member', 'rs_unrelated', file_id, path, language,
                'value', 'member_access', 1, 0, 1, 5, 0, 5, 1.0,
                '{"receiver":"other"}'
         FROM files WHERE path = 'src/other.py';"#,
    )
    .unwrap();

    let language_for = |path: &str| -> String {
        conn.query_row(
            "SELECT language FROM files WHERE path = ?1",
            [path],
            |row| row.get(0),
        )
        .unwrap()
    };
    assert_eq!(language_for("ui/View.qml"), "qml");
    assert_eq!(language_for("ui/View.razor"), "razor");
    assert_eq!(language_for("src/BridgeWidget.cpp"), "cpp");
    assert_eq!(language_for("src/BridgeModel.cs"), "csharp");

    let target_id = |path: &str, name: &str| -> String {
        conn.query_row(
            "SELECT symbol_id FROM symbols WHERE path = ?1 AND name = ?2 ORDER BY start_line LIMIT 1",
            [path, name],
            |row| row.get(0),
        )
        .unwrap()
    };
    let cpp_refs = find_references_for_symbol(
        &conn,
        "BridgeWidget",
        "callers",
        20,
        &target_id("src/BridgeWidget.cpp", "BridgeWidget"),
    )
    .unwrap();
    let csharp_refs = find_references_for_symbol(
        &conn,
        "BridgeModel",
        "callers",
        20,
        &target_id("src/BridgeModel.cs", "BridgeModel"),
    )
    .unwrap();
    let cpp_member_refs = find_references_for_symbol(
        &conn,
        "value",
        "callers",
        20,
        &target_id("src/BridgeWidget.cpp", "value"),
    )
    .unwrap();
    let csharp_member_refs = find_references_for_symbol(
        &conn,
        "Title",
        "callers",
        20,
        &target_id("src/BridgeModel.cs", "Title"),
    )
    .unwrap();

    assert!(
        cpp_refs
            .iter()
            .any(|row| row.path == "ui/View.qml" && row.kind == "type_usage"),
        "QML-to-C++ bridge was filtered: {cpp_refs:?}"
    );
    assert!(
        !cpp_refs.iter().any(|row| row.path == "src/other.py"),
        "unrelated-language type usage leaked through: {cpp_refs:?}"
    );
    assert!(
        csharp_refs
            .iter()
            .any(|row| row.path == "ui/View.razor" && row.kind == "type_usage"),
        "Razor-to-C# bridge was filtered: {csharp_refs:?}"
    );
    assert!(
        cpp_member_refs
            .iter()
            .any(|row| row.path == "ui/View.qml" && row.kind == "member_access"),
        "QML-to-C++ member access bridge was filtered: {cpp_member_refs:?}"
    );
    assert!(
        !cpp_member_refs.iter().any(|row| row.path == "src/other.py"),
        "unrelated-language member access leaked through: {cpp_member_refs:?}"
    );
    assert!(
        csharp_member_refs
            .iter()
            .any(|row| row.path == "ui/View.razor" && row.kind == "member_access"),
        "Razor-to-C# member access bridge was filtered: {csharp_member_refs:?}"
    );
}

#[test]
fn find_references_reports_member_accesses_of_a_field() {
    let (_repo, db_path) = scanned_repo(&[
        (
            "src/config.rs",
            "pub struct Config {\n    pub path: String,\n}\n",
        ),
        (
            "src/loader.rs",
            "use crate::config::Config;\n\npub fn load(existing: &Config) -> String {\n    existing.path.clone()\n}\n",
        ),
    ]);
    let conn = open_read_only(&db_path).unwrap();

    let refs = find_references_scoped(&conn, "path", "callers", 20, false, None).unwrap();

    let access = refs
        .iter()
        .find(|r| r.kind == "member_access")
        .unwrap_or_else(|| panic!("expected a member_access reference, got {refs:?}"));
    assert_eq!(access.from_symbol_name, "load");
    assert_eq!(access.start_line, Some(4));
}

fn caller_names(refs: &[code_kb_core::ReferenceSite]) -> Vec<String> {
    let mut names: Vec<String> = refs.iter().map(|r| r.from_symbol_name.clone()).collect();
    names.sort();
    names.dedup();
    names
}

#[test]
fn callers_of_a_nested_csharp_static_method_follow_the_outer_class_across_files() {
    let (_repo, db_path) = scanned_repo(&[
        (
            "src/Defs.cs",
            "public class Outer\n{\n    public static class Inner\n    {\n        public static int Chain()\n        {\n            return 1;\n        }\n    }\n}\n",
        ),
        (
            "src/Caller.cs",
            "public class Caller\n{\n    public int Call()\n    {\n        return Outer.Inner.Chain();\n    }\n}\n",
        ),
    ]);
    let conn = open_read_only(&db_path).unwrap();

    let refs = find_references_scoped(&conn, "Chain", "callers", 20, false, None).unwrap();

    assert_eq!(caller_names(&refs), vec!["Call"], "got {refs:?}");
    assert_eq!(refs[0].kind, "calls");
    assert_eq!(refs[0].path, "src/Caller.cs");
    assert_eq!(refs[0].start_line, Some(5));
}

#[test]
fn callers_of_a_nested_scala_method_follow_the_outer_object_in_another_file() {
    let (_repo, db_path) = scanned_repo(&[
        (
            "src/Defs.scala",
            "object Outer {\n  object Inner {\n    def chain(): Int = 1\n  }\n}\n",
        ),
        (
            "src/Caller.scala",
            "object Caller {\n  def caller(): Int = Outer.Inner.chain()\n}\n",
        ),
    ]);
    let conn = open_read_only(&db_path).unwrap();

    let refs = find_references_scoped(&conn, "chain", "callers", 20, false, None).unwrap();

    assert_eq!(caller_names(&refs), vec!["caller"], "got {refs:?}");
    assert_eq!(refs[0].kind, "calls");
    assert_eq!(refs[0].path, "src/Caller.scala");
    assert_eq!(refs[0].start_line, Some(2));
}

const REPOSITORY_CS: &str = "using System;
using System.Collections.Generic;

namespace CIMProfile.Core.Repositories
{
    public class EmailSettingQueryRepository
    {
        public static class EmailSettingNames
        {
            public static readonly string AnnualAttestationNotification = \"AnnualAttestationNotification\";
        }

        public static class EmailSettingPlaceholderGenerators
        {
            public static Dictionary<string, string> AnnualAttestationNotification(string dashboardUrl, string unitHeadName, DateTime deadline, IEnumerable<string> cims)
            {
                return new Dictionary<string, string>();
            }
        }
    }
}
";

const BUILDER_CS: &str = "using System;
using System.Collections.Generic;
using CIMProfile.Core.Repositories;

namespace CIMProfile.Core.ApplicationServices
{
    public class AnnualReportingAttestationNotificationBuilder
    {
        public string Build(string recipientEmail, string unitHeadName, DateTime deadline, IEnumerable<string> cims)
        {
            var emailId = EmailSettingQueryRepository.EmailSettingNames.AnnualAttestationNotification;
            var body = EmailSettingService.GenerateTemplatedEmailBody(\"x\", EmailSettingQueryRepository.EmailSettingPlaceholderGenerators.AnnualAttestationNotification(\"url\", unitHeadName, deadline, cims));
            return emailId + body;
        }
    }
}
";

const CONTROLLER_CS: &str = "using System;
using CIMProfile.Core.Repositories;

namespace CIMProfile.Web.Controllers
{
    public class EmailSettingController
    {
        public object GetEmailSettingPreview()
        {
            var placeholders = EmailSettingQueryRepository.EmailSettingPlaceholderGenerators.AnnualAttestationNotification(\"https://iu.edu\", \"Jane Smith\", DateTime.Now, new[] { \"a\" });
            return placeholders;
        }
    }
}
";

const TEST_CS: &str = "using System;
using CIMProfile.Core.Repositories;

namespace CIMProfile.Tests
{
    public class ReportingAttestationServiceShould
    {
        public void Sends()
        {
            var today = DateTime.Now;
            var body1 = EmailSettingService.GenerateTemplatedEmailBody(\"x\", EmailSettingQueryRepository.EmailSettingPlaceholderGenerators.AnnualAttestationNotification(\"\", \"\", today, new[] { \"CIM 1\" }));
        }
    }
}
";

#[test]
fn same_named_constant_and_method_in_sibling_nested_classes_keep_separate_callers() {
    let (_repo, db_path) = scanned_repo(&[
        (
            "Core/Repositories/EmailSettingQueryRepository.cs",
            REPOSITORY_CS,
        ),
        (
            "Core/ApplicationServices/AnnualReportingAttestationNotificationBuilder.cs",
            BUILDER_CS,
        ),
        ("Web/Controllers/EmailSettingController.cs", CONTROLLER_CS),
        ("Tests/ReportingAttestationServiceShould.cs", TEST_CS),
    ]);
    let conn = open_read_only(&db_path).unwrap();

    let method_refs = find_references_scoped(
        &conn,
        "EmailSettingPlaceholderGenerators.AnnualAttestationNotification",
        "callers",
        20,
        false,
        None,
    )
    .unwrap();
    let mut method_sites: Vec<(String, String, Option<usize>, String)> = method_refs
        .iter()
        .map(|r| {
            (
                r.from_symbol_name.clone(),
                r.path.clone(),
                r.start_line,
                r.kind.clone(),
            )
        })
        .collect();
    method_sites.sort();
    assert_eq!(
        method_sites,
        vec![
            (
                "Build".to_string(),
                "Core/ApplicationServices/AnnualReportingAttestationNotificationBuilder.cs"
                    .to_string(),
                Some(12),
                "calls".to_string()
            ),
            (
                "GetEmailSettingPreview".to_string(),
                "Web/Controllers/EmailSettingController.cs".to_string(),
                Some(10),
                "calls".to_string()
            ),
            (
                "Sends".to_string(),
                "Tests/ReportingAttestationServiceShould.cs".to_string(),
                Some(11),
                "calls".to_string()
            ),
        ]
    );

    let constant_refs = find_references_scoped(
        &conn,
        "EmailSettingNames.AnnualAttestationNotification",
        "callers",
        20,
        false,
        None,
    )
    .unwrap();
    assert_eq!(constant_refs.len(), 1, "got {constant_refs:?}");
    assert_eq!(constant_refs[0].from_symbol_name, "Build");
    assert_eq!(constant_refs[0].kind, "member_access");
    assert_eq!(
        constant_refs[0].path,
        "Core/ApplicationServices/AnnualReportingAttestationNotificationBuilder.cs"
    );
    assert_eq!(constant_refs[0].start_line, Some(11));
}

#[test]
fn same_named_containers_with_same_named_members_keep_the_shared_caller() {
    let (_repo, db_path) = scanned_repo(&[
        (
            "src/A.cs",
            "namespace A\n{\n    public static class Settings\n    {\n        public static readonly int Value = 1;\n    }\n}\n",
        ),
        (
            "src/B.cs",
            "namespace B\n{\n    public static class Settings\n    {\n        public static readonly int Value = 2;\n    }\n}\n",
        ),
        (
            "src/Reader.cs",
            "public class Reader\n{\n    public int Read()\n    {\n        return Settings.Value;\n    }\n}\n",
        ),
    ]);
    let conn = open_read_only(&db_path).unwrap();

    for declaring_file in ["src/A.cs", "src/B.cs"] {
        let refs =
            find_references_scoped(&conn, "Value", "callers", 20, false, Some(declaring_file))
                .unwrap();
        assert_eq!(
            caller_names(&refs),
            vec!["Read"],
            "{declaring_file}: got {refs:?}"
        );
        assert_eq!(refs[0].kind, "member_access");
        assert_eq!(refs[0].start_line, Some(5));
    }
}

#[test]
fn references_and_blast_radius_report_the_same_missing_symbol() {
    let (_repo, db_path) = scanned_repo(&[(
        "src/config.rs",
        "pub struct Config {\n    pub path: String,\n}\n",
    )]);
    let conn = open_read_only(&db_path).unwrap();

    let refs_error = find_references_scoped(&conn, "Confgi", "callers", 20, false, None)
        .unwrap_err()
        .to_string();
    let blast_error = compute_blast_radius(&conn, &["Confgi"], &[], 1, 20)
        .unwrap_err()
        .to_string();

    assert_eq!(refs_error, blast_error);
    assert!(refs_error.contains("Did you mean one of:"), "{refs_error}");
    assert!(refs_error.contains("`Config`"), "{refs_error}");
}

const QML_SINGLETON_AND_CONSUMER: &[(&str, &str)] = &[
    (
        "Commons/Color.qml",
        "pragma Singleton\nimport QtQuick\n\nQtObject {\n    property color foreground: \"#ffffff\"\n    property color background: \"#000000\"\n}\n",
    ),
    (
        "Ui/Button.qml",
        "import QtQuick\nimport \"../Commons\"\n\nRectangle {\n    color: Color.background\n    border.color: Color.foreground\n}\n",
    ),
];

const QUALIFIED_RECEIVER_JS: &[(&str, &str)] = &[
    (
        "src/runtime.js",
        "export class runtime {\n    static start() {\n        return 1;\n    }\n}\n",
    ),
    (
        "src/host.js",
        "export function boot(chrome) {\n    return chrome.runtime.lastError;\n}\n\nexport function version() {\n    return runtime.tag;\n}\n",
    ),
];

#[test]
fn callers_of_a_type_include_the_member_accesses_that_name_it_as_receiver() {
    let (_repo, db_path) = scanned_repo(QML_SINGLETON_AND_CONSUMER);
    let conn = open_read_only(&db_path).unwrap();

    let refs = find_references_scoped(
        &conn,
        "Color",
        "callers",
        20,
        false,
        Some("Commons/Color.qml"),
    )
    .unwrap();

    type Site = (String, Option<usize>, String, String, Option<usize>);
    let mut sites: Vec<Site> = refs
        .iter()
        .map(|r| {
            (
                r.path.clone(),
                r.start_line,
                r.to_symbol_name.clone(),
                r.kind.clone(),
                r.occurrences,
            )
        })
        .collect();
    sites.sort();
    assert_eq!(
        sites,
        vec![(
            "Ui/Button.qml".to_string(),
            Some(5),
            "background".to_string(),
            "member_access".to_string(),
            Some(2)
        )],
        "{refs:?}"
    );
}

#[test]
fn a_call_site_caller_row_reports_no_occurrence_count() {
    let (_repo, db_path) = scanned_repo(&[
        (
            "Core/Repositories/EmailSettingQueryRepository.cs",
            REPOSITORY_CS,
        ),
        (
            "Core/ApplicationServices/AnnualReportingAttestationNotificationBuilder.cs",
            BUILDER_CS,
        ),
        ("Web/Controllers/EmailSettingController.cs", CONTROLLER_CS),
    ]);
    let conn = open_read_only(&db_path).unwrap();

    let refs = find_references_scoped(
        &conn,
        "EmailSettingPlaceholderGenerators.AnnualAttestationNotification",
        "callers",
        20,
        false,
        None,
    )
    .unwrap();

    assert!(refs.iter().any(|r| r.kind == "calls"), "{refs:?}");
    assert!(refs.iter().all(|r| r.occurrences.is_none()), "{refs:?}");
}

#[test]
fn a_member_access_under_a_foreign_qualifier_is_not_a_reference_to_the_type() {
    let (_repo, db_path) = scanned_repo(QUALIFIED_RECEIVER_JS);
    let conn = open_read_only(&db_path).unwrap();

    let refs = find_references_scoped(
        &conn,
        "runtime",
        "callers",
        20,
        false,
        Some("src/runtime.js"),
    )
    .unwrap();

    assert!(
        !refs.iter().any(|r| r.to_symbol_name == "lastError"),
        "{refs:?}"
    );
}

#[test]
fn receiver_matches_of_a_type_come_after_the_rows_that_name_it() {
    let (_repo, db_path) = scanned_repo(QUALIFIED_RECEIVER_JS);
    let conn = open_read_only(&db_path).unwrap();

    let refs = find_references_scoped(
        &conn,
        "runtime",
        "callers",
        20,
        false,
        Some("src/runtime.js"),
    )
    .unwrap();

    let names: Vec<&str> = refs.iter().map(|r| r.to_symbol_name.as_str()).collect();

    assert_eq!(names, vec!["runtime", "tag"], "{refs:?}");
}

const QML_SIGNAL_AND_HANDLERS: &[(&str, &str)] = &[
    (
        "Ui/Button.qml",
        "import QtQuick\n\nItem {\n    signal clicked()\n}\n",
    ),
    (
        "App/Main.qml",
        "import QtQuick\nimport \"../Ui\"\n\nItem {\n    Button {\n        id: button\n    }\n}\n",
    ),
];

#[test]
fn a_signal_handler_is_a_candidate_until_its_receiver_names_the_owner() {
    let (_repo, db_path) = scanned_repo(QML_SIGNAL_AND_HANDLERS);
    let conn = open_read_write(&db_path).unwrap();
    conn.execute_batch(
        r#"INSERT INTO reference_sites
            (reference_site_id, file_id, path, language, is_exact, provenance)
         SELECT 'rs_handlers', file_id, path, language, 0, 'spanless'
         FROM files WHERE path = 'App/Main.qml';
         INSERT INTO identifiers
            (identifier_id, reference_site_id, file_id, path, language, name, kind,
             start_line, start_column, end_line, end_column, start_byte, end_byte,
             confidence, metadata_json)
         SELECT 'i_' || v.id, 'rs_handlers', f.file_id, f.path, f.language,
                'clicked', 'member_access', v.line, 0, v.line, 9, 0, 9, 1.0, v.metadata
         FROM files f
         JOIN (SELECT 'owned' AS id, 6 AS line,
                      '{"role":"signal_handler","receiver":"Button"}' AS metadata
               UNION ALL SELECT 'unowned', 7, '{"role":"signal_handler"}'
               UNION ALL SELECT 'foreign', 8,
                      '{"role":"signal_handler","receiver":"Timer"}'
               UNION ALL SELECT 'plain', 9, '{"receiver":"Button"}') v
         WHERE f.path = 'App/Main.qml'"#,
    )
    .unwrap();

    let refs = find_references_scoped(
        &conn,
        "clicked",
        "callers",
        20,
        false,
        Some("Ui/Button.qml"),
    )
    .unwrap();

    let mut sites: Vec<(Option<usize>, &str)> = refs
        .iter()
        .map(|r| (r.start_line, r.kind.as_str()))
        .collect();
    sites.sort();
    assert_eq!(
        sites,
        vec![
            (Some(6), "handler"),
            (Some(7), "handler (candidate)"),
            (Some(8), "handler (candidate)"),
            (Some(9), "member_access"),
        ],
        "{refs:?}"
    );
}

const INHERITANCE_SITES_THAT_ALSO_EMIT_IDENTIFIERS: &[(&str, &str)] = &[
    (
        "src/tree.py",
        "class Parent:\n    def go(self):\n        return 1\n\n\nclass Child(Parent):\n    def go(self):\n        return 2\n",
    ),
    (
        "src/base.ts",
        "export class Base {\n    go() { return 1; }\n}\n",
    ),
    (
        "src/leaf.ts",
        "import { Base } from \"./base\";\n\nexport class Leaf extends Base {\n    go() { return 2; }\n}\n",
    ),
];

#[test]
fn a_site_with_both_a_relationship_and_an_identifier_yields_one_row() {
    let (_repo, db_path) = scanned_repo(INHERITANCE_SITES_THAT_ALSO_EMIT_IDENTIFIERS);
    let conn = open_read_only(&db_path).unwrap();

    let sites = |refs: &[code_kb_core::ReferenceSite]| -> Vec<(String, Option<usize>, String)> {
        refs.iter()
            .map(|r| (r.path.clone(), r.start_line, r.kind.clone()))
            .collect()
    };

    let concrete =
        find_references_scoped(&conn, "Parent", "callers", 20, false, Some("src/tree.py")).unwrap();
    let pending =
        find_references_scoped(&conn, "Base", "callers", 20, false, Some("src/base.ts")).unwrap();

    assert_eq!(
        sites(&concrete),
        vec![("src/tree.py".to_string(), Some(6), "extends".to_string())],
        "{concrete:?}"
    );
    assert_eq!(
        sites(&pending),
        vec![("src/leaf.ts".to_string(), Some(3), "extends".to_string())],
        "{pending:?}"
    );
}

#[test]
fn a_python_module_receiver_matches_its_package_and_a_local_class_shadows() {
    let (_repo, db_path) = scanned_repo(&[
        ("src/pkg/__init__.py", "from .app import App, make\n"),
        (
            "src/pkg/app.py",
            "class App:\n    pass\n\n\ndef make():\n    return 1\n",
        ),
        (
            "tests/test_app.py",
            "import pkg\nfrom other import helpers\n\n\ndef test_module_call():\n    return pkg.App()\n\n\ndef test_local_class():\n    class App(pkg.App):\n        pass\n\n    return App()\n\n\ndef test_other_module():\n    return helpers.make()\n",
        ),
    ]);
    let conn = open_read_only(&db_path).unwrap();
    let callers = |name: &str| -> Vec<String> {
        let refs =
            find_references_scoped(&conn, name, "callers", 20, false, Some("src/pkg/app.py"))
                .unwrap();
        let calls: Vec<_> = refs.into_iter().filter(|r| r.kind == "calls").collect();
        caller_names(&calls)
    };

    assert_eq!(callers("App"), vec!["test_module_call"]);
    assert!(callers("make").is_empty());
}

#[test]
fn a_second_definition_in_the_callers_own_file_does_not_hide_the_target() {
    let (_repo, db_path) = scanned_repo(&[
        (
            "src/twice.py",
            "def get():\n    return 1\n\n\ndef get():\n    return 2\n\n\ndef use():\n    return get()\n",
        ),
        (
            "src/issues.ts",
            "export type Base = { code: string };\n\nexport interface Issue extends Base {\n  expected: string;\n}\n",
        ),
    ]);
    let conn = open_read_only(&db_path).unwrap();
    let callees = |name: &str, path: &str| -> Vec<String> {
        find_references_scoped(&conn, name, "callees", 20, false, Some(path))
            .unwrap()
            .into_iter()
            .map(|r| r.to_symbol_name)
            .collect()
    };

    assert!(callees("use", "src/twice.py").contains(&"get".to_string()));
    assert_eq!(callees("Issue", "src/issues.ts"), vec!["Base"]);
}

#[test]
fn a_self_call_reaches_a_method_inherited_from_a_base_in_another_file() {
    let (_repo, db_path) = scanned_repo(&[
        (
            "src/base.py",
            "class App:\n    def trap(self, e):\n        return False\n",
        ),
        (
            "src/app.py",
            "from base import App\n\n\nclass Flask(App):\n    def handle(self, e):\n        return self.trap(e)\n",
        ),
        (
            "src/other.py",
            "class Other:\n    def trap(self, e):\n        return True\n",
        ),
    ]);
    let conn = open_read_only(&db_path).unwrap();

    let callees =
        find_references_scoped(&conn, "handle", "callees", 20, false, Some("src/app.py")).unwrap();
    let callers =
        find_references_scoped(&conn, "trap", "callers", 20, false, Some("src/base.py")).unwrap();
    let other_callers =
        find_references_scoped(&conn, "trap", "callers", 20, false, Some("src/other.py")).unwrap();

    assert_eq!(
        callees
            .iter()
            .map(|r| r.to_symbol_name.as_str())
            .collect::<Vec<_>>(),
        vec!["trap"]
    );
    assert_eq!(caller_names(&callers), vec!["handle"]);
    assert!(other_callers.is_empty(), "{other_callers:?}");
}

#[test]
fn blast_radius_follows_a_fixture_to_the_tests_that_take_it() {
    let (_repo, db_path) = scanned_repo(&[
        ("src/web/app.py", "def report(e):\n    return str(e)\n"),
        (
            "tests/test_runner.py",
            "import pytest\nfrom web.app import report\n\n\n@pytest.fixture\ndef invoke():\n    return report(None)\n\n\ndef test_invoke_path(invoke):\n    assert invoke\n\n\nclass TestRoutes:\n    @pytest.fixture\n    def method_invoke(self):\n        return report(None)\n\n    def setup_method(self):\n        report(None)\n\n    def test_simple(self, method_invoke):\n        assert method_invoke\n",
        ),
    ]);
    let conn = open_read_only(&db_path).unwrap();

    let result = compute_blast_radius(&conn, &["report"], &[], 2, 20).unwrap();

    let reasons: Vec<(&str, &str)> = result
        .likely_tests
        .iter()
        .map(|t| (t.name.as_str(), t.reason.as_str()))
        .collect();
    assert!(
        reasons.contains(&("TestRoutes::test_simple", "uses fixture `method_invoke`")),
        "{reasons:?}"
    );
    assert!(
        !reasons
            .iter()
            .any(|(_, reason)| reason.starts_with("fixture") || reason.starts_with("setup")),
        "{reasons:?}"
    );
    assert!(
        reasons.contains(&("test_invoke_path", "uses fixture `invoke`")),
        "{reasons:?}"
    );
}

#[test]
fn blast_radius_labels_a_test_class_setup_and_adds_the_tests_it_runs_before() {
    let (_repo, db_path) = scanned_repo(&[
        (
            "src/Store.cs",
            "namespace App;\n\npublic static class Store\n{\n    public static string PathFor(string root) => root;\n}\n",
        ),
        (
            "tests/PathTests.cs",
            "using Xunit;\n\nnamespace App.Tests;\n\npublic sealed class PathTests\n{\n    private readonly string path;\n\n    public PathTests()\n    {\n        path = Store.PathFor(\"root\");\n    }\n\n    [Fact]\n    public void Opens()\n    {\n        Assert.NotNull(path);\n    }\n}\n",
        ),
    ]);
    let conn = open_read_only(&db_path).unwrap();

    let result = compute_blast_radius(&conn, &["PathFor"], &[], 2, 20).unwrap();

    let reasons: Vec<(&str, &str)> = result
        .likely_tests
        .iter()
        .map(|t| (t.name.as_str(), t.reason.as_str()))
        .collect();
    assert!(
        reasons.contains(&("PathTests::Opens", "setup `PathTests` runs before it")),
        "{reasons:?}"
    );
    assert!(
        !reasons
            .iter()
            .any(|(name, _)| *name == "PathTests::PathTests"),
        "{reasons:?}"
    );
}

#[test]
fn blast_radius_reaches_tests_that_build_a_class_whose_call_method_reaches_the_target() {
    let (_repo, db_path) = scanned_repo(&[
        (
            "src/web/app.py",
            "class App:\n    def __call__(self, environ):\n        return self.dispatch(environ)\n\n    def dispatch(self, environ):\n        return self.handle_user_exception(environ)\n\n    def handle_user_exception(self, e):\n        return e\n",
        ),
        (
            "tests/conftest.py",
            "import pytest\nfrom web.app import App\n\n\n@pytest.fixture\ndef app():\n    return App()\n\n\n@pytest.fixture\ndef client(app):\n    return app.test_client()\n",
        ),
        (
            "tests/test_user_error_handler.py",
            "def test_user_error_is_handled(app, client):\n    assert client.get('/')\n\n\ndef test_user_exception_logger_is_set(app):\n    assert app.logger\n",
        ),
        (
            "tests/test_views.py",
            "from web.app import App\n\n\ndef test_view_renders():\n    assert App().test_client()\n\n\ndef test_exception_handling_renders():\n    assert App().test_client().get('/')\n",
        ),
        (
            "tests/test_config.py",
            "from web.app import App\n\n\ndef test_config_loads():\n    assert App()\n",
        ),
    ]);
    let conn = open_read_only(&db_path).unwrap();

    let result = compute_blast_radius(&conn, &["handle_user_exception"], &[], 3, 20).unwrap();

    let names: Vec<&str> = result
        .likely_tests
        .iter()
        .map(|t| t.name.as_str())
        .collect();
    let position = |name: &str| {
        names
            .iter()
            .position(|n| *n == name)
            .unwrap_or_else(|| panic!("{name} missing from {:?}", result.likely_tests))
    };
    assert!(position("test_user_error_is_handled") < position("test_exception_handling_renders"));
    assert!(!names.contains(&"test_view_renders"), "{names:?}");
    assert!(!names.contains(&"test_config_loads"), "{names:?}");
    assert!(
        !names.contains(&"test_user_exception_logger_is_set"),
        "{names:?}"
    );
    let reason = &result.likely_tests[position("test_exception_handling_renders")].reason;
    assert!(
        reason.starts_with(
            "possible: builds `App`, and a test client calls its `__call__`, which can reach the target; shares"
        ),
        "{reason}"
    );
    let reason = &result.likely_tests[position("test_user_error_is_handled")].reason;
    assert!(
        reason.starts_with(
            "possible: builds `App` through fixture `app`, and a test client calls its `__call__`, which can reach the target; shares"
        ),
        "{reason}"
    );
}

#[test]
fn blast_radius_rendering_keeps_handler_priority_across_file_groups_with_a_limit() {
    let (_repo, db_path) = scanned_repo(&[
        (
            "src/web/app.py",
            "class Flask:\n    def __call__(self, environ):\n        return self.handle_user_exception(environ)\n\n    def handle_user_exception(self, error):\n        return error\n\n    def errorhandler(self, error):\n        return error\n\n    def test_client(self):\n        return None\n",
        ),
        (
            "tests/conftest.py",
            "import pytest\nfrom web.app import Flask\n\n\n@pytest.fixture\ndef app():\n    return Flask()\n",
        ),
        (
            "tests/test_basic.py",
            "def test_error_handling(app):\n    app.errorhandler(Exception)\n    app.test_client().get('/')\n\n\ndef test_teardown_request_handler(app):\n    app.test_client().get('/')\n",
        ),
        (
            "tests/test_regression.py",
            "def test_aborting(app):\n    abort(403)\n    app.test_client().get('/')\n",
        ),
        (
            "tests/test_extra.py",
            "def test_exception_logging(app):\n    app.test_client().get('/')\n",
        ),
    ]);
    let conn = open_read_only(&db_path).unwrap();

    let result = compute_blast_radius(&conn, &["handle_user_exception"], &[], 3, 3).unwrap();
    assert_eq!(result.likely_tests.len(), 3, "{:?}", result.likely_tests);
    assert!(result.likely_tests_truncated, "{:?}", result.likely_tests);

    let rendered = format_blast_radius(&result);
    let handler = rendered.find("`test_error_handling`").unwrap();
    let aborting = rendered.find("`test_aborting`").unwrap();
    let name_only = rendered.find("`test_teardown_request_handler`").unwrap();
    assert!(handler < aborting && aborting < name_only, "{rendered}");
    assert!(!rendered.contains("`test_exception_logging`"), "{rendered}");
}

#[test]
fn blast_radius_lists_the_tests_behind_a_fixture_that_takes_a_fixture_not_the_fixture() {
    let (_repo, db_path) = scanned_repo(&[
        ("src/web/app.py", "def report(e):\n    return str(e)\n"),
        (
            "tests/conftest.py",
            "import pytest\nfrom web.app import report\n\n\n@pytest.fixture\ndef invoke():\n    return report(None)\n",
        ),
        (
            "tests/test_runner.py",
            "import pytest\n\n\nclass TestRunner:\n    @pytest.fixture\n    def runner(self, invoke):\n        return invoke\n\n    def test_runs(self, runner):\n        assert runner\n",
        ),
    ]);
    let conn = open_read_only(&db_path).unwrap();

    let result = compute_blast_radius(&conn, &["report"], &[], 2, 20).unwrap();

    let reasons: Vec<(&str, &str)> = result
        .likely_tests
        .iter()
        .map(|t| (t.name.as_str(), t.reason.as_str()))
        .collect();
    assert!(
        reasons.contains(&("TestRunner::test_runs", "uses fixture `invoke`")),
        "{reasons:?}"
    );
    assert!(
        !reasons.iter().any(|(name, _)| name.ends_with("runner")),
        "{reasons:?}"
    );
}

#[test]
fn two_accesses_on_one_line_are_one_reference() {
    let (_repo, db_path) = scanned_repo(&[
        (
            "src/app.py",
            "class App:\n    def wsgi_app(self, environ):\n        return environ\n",
        ),
        (
            "tests/test_app.py",
            "from app import App\n\n\ndef test_wrap(app: App):\n    app.wsgi_app = Wrap(app.wsgi_app)\n",
        ),
    ]);
    let conn = open_read_only(&db_path).unwrap();

    let refs = find_references_scoped(&conn, "wsgi_app", "callers", 20, false, None).unwrap();

    let wraps: Vec<_> = refs
        .iter()
        .filter(|r| r.from_symbol_name == "test_wrap")
        .collect();
    assert_eq!(wraps.len(), 1, "{refs:?}");
    assert_eq!(wraps[0].occurrences, None, "{refs:?}");

    let target = code_kb_core::get_symbol_by_name(&conn, "wsgi_app", Some("src/app.py"))
        .unwrap()
        .unwrap();
    let related = code_kb_core::find_related_tests(&conn, &target, 5).unwrap();
    assert!(related.iter().any(|t| t.name == "test_wrap"), "{related:?}");
}

#[test]
fn a_receiver_named_for_a_fixture_calls_methods_of_the_class_the_fixture_builds() {
    let (_repo, db_path) = scanned_repo(&[
        (
            "src/web/base.py",
            "class Scaffold:\n    def route(self, rule):\n        return rule\n",
        ),
        (
            "src/web/app.py",
            "from web.base import Scaffold\n\n\nclass Flask(Scaffold):\n    def run(self):\n        return 1\n",
        ),
        (
            "src/web/other.py",
            "class Server:\n    def run(self):\n        return 2\n",
        ),
        (
            "tests/conftest.py",
            "import pytest\nfrom web.app import Flask\n\n\n@pytest.fixture\ndef app():\n    return Flask()\n",
        ),
        (
            "tests/test_run.py",
            "def test_run(app):\n    app.run()\n    app.route('/')\n\n\ndef test_other(server):\n    server.run()\n",
        ),
    ]);
    let conn = open_read_only(&db_path).unwrap();
    let callers = |name: &str, path: &str| -> Vec<String> {
        let refs = find_references_scoped(&conn, name, "callers", 20, false, Some(path)).unwrap();
        caller_names(&refs)
    };

    assert_eq!(callers("run", "src/web/app.py"), vec!["test_run"]);
    assert_eq!(callers("route", "src/web/base.py"), vec!["test_run"]);
    assert!(callers("run", "src/web/other.py").is_empty());
}

#[test]
fn a_receiver_built_by_a_call_calls_methods_of_the_class_that_call_returns() {
    let (_repo, db_path) = scanned_repo(&[
        (
            "src/web/testing.py",
            "class CliRunner:\n    def invoke(self, args):\n        return args\n",
        ),
        (
            "src/web/other.py",
            "class Shell:\n    def invoke(self, args):\n        return args\n",
        ),
        (
            "src/web/app.py",
            "from web.testing import CliRunner\n\n\nclass Flask:\n    def test_cli_runner(self) -> CliRunner:\n        return CliRunner()\n",
        ),
        (
            "tests/conftest.py",
            "import pytest\n\n\n@pytest.fixture\ndef runner(app):\n    return app.test_cli_runner()\n",
        ),
        (
            "examples/tests/conftest.py",
            "import pytest\nfrom web.other import Shell\n\n\n@pytest.fixture\ndef runner():\n    return Shell()\n",
        ),
        (
            "tests/test_cli.py",
            "from web.testing import CliRunner\n\n\ndef test_local(app):\n    cli = app.test_cli_runner()\n    cli.invoke(['hello'])\n\n\ndef test_fixture(runner):\n    runner.invoke(['hello'])\n\n\ndef test_built(app):\n    made = CliRunner()\n    made.invoke(['hello'])\n",
        ),
    ]);
    let conn = open_read_only(&db_path).unwrap();
    let callers = |path: &str| -> Vec<String> {
        let refs =
            find_references_scoped(&conn, "invoke", "callers", 20, false, Some(path)).unwrap();
        caller_names(&refs)
    };

    assert_eq!(
        callers("src/web/testing.py"),
        vec!["test_built", "test_fixture", "test_local"]
    );
    assert!(callers("src/web/other.py").is_empty());
}

#[test]
fn callees_include_a_self_call_to_a_class_attribute() {
    let (_repo, db_path) = scanned_repo(&[(
        "src/web/app.py",
        "class App:\n    should_ignore: None = None\n\n    def run(self, error):\n        limit = 3\n        if self.should_ignore(error):\n            return limit\n        return self.step()\n\n    def step(self):\n        return 1\n",
    )]);
    let conn = open_read_only(&db_path).unwrap();

    let refs = find_references_scoped(&conn, "run", "callees", 20, false, None).unwrap();

    let mut callees: Vec<&str> = refs.iter().map(|r| r.to_symbol_name.as_str()).collect();
    callees.sort();
    assert_eq!(callees, vec!["should_ignore", "step"]);
}

#[test]
fn the_callers_of_a_constructor_are_the_calls_that_build_its_class() {
    let (_repo, db_path) = scanned_repo(&[
        (
            "src/web/cli.py",
            "class Group:\n    def __init__(self):\n        self.name = 'cli'\n",
        ),
        (
            "src/web/app.py",
            "from web.cli import Group\n\n\nclass Flask:\n    def __init__(self):\n        self.cli = Group()\n\n\ndef annotate(app: Flask) -> Flask:\n    return app\n",
        ),
        (
            "tests/test_app.py",
            "from web.app import Flask\n\n\ndef test_builds():\n    assert Flask()\n",
        ),
    ]);
    let conn = open_read_only(&db_path).unwrap();

    let refs = find_references_scoped(
        &conn,
        "Flask.__init__",
        "callers",
        20,
        false,
        Some("src/web/app.py"),
    )
    .unwrap();
    assert_eq!(caller_names(&refs), vec!["test_builds"], "{refs:?}");

    let result = compute_blast_radius(&conn, &["Group.__init__"], &[], 3, 20).unwrap();
    let tests: Vec<&str> = result
        .likely_tests
        .iter()
        .map(|t| t.name.as_str())
        .collect();
    assert!(tests.contains(&"test_builds"), "{tests:?}");
}

#[test]
fn whole_file_blast_radius_ranks_constructor_only_tests_after_direct_callers() {
    let (_repo, db_path) = scanned_repo(&[
        (
            "src/flask/cli.py",
            "class AppGroup:\n    def __init__(self):\n        pass\n\n\ndef invoke_cli():\n    return 1\n",
        ),
        (
            "src/flask/app.py",
            "from . import cli\n\n\nclass Flask:\n    def __init__(self):\n        self.cli = cli.AppGroup()\n",
        ),
        ("src/flask/__init__.py", "from .app import Flask\n"),
        (
            "src/flask/runner.py",
            "from flask.cli import invoke_cli\n\n\ndef run_cli():\n    return invoke_cli()\n",
        ),
        (
            "src/flask/entry.py",
            "from flask.runner import run_cli\n\n\ndef dispatch():\n    return run_cli()\n",
        ),
        (
            "tests/test_runner.py",
            "from flask.entry import dispatch\n\n\ndef test_runs_cli_command():\n    assert dispatch()\n",
        ),
        (
            "tests/test_application.py",
            "import flask\n\n\ndef test_constructs_app_only():\n    assert flask.Flask()\n\n\ndef test_constructs_another_app():\n    assert flask.Flask()\n",
        ),
    ]);
    let conn = open_read_only(&db_path).unwrap();

    let result = compute_blast_radius(&conn, &[], &["src/flask/cli.py"], 4, 20).unwrap();
    let tests: Vec<&str> = result
        .likely_tests
        .iter()
        .map(|test| test.name.as_str())
        .collect();
    let position = |name: &str| {
        tests
            .iter()
            .position(|test| *test == name)
            .unwrap_or_else(|| panic!("{name} missing from {tests:?}"))
    };

    let constructor_rows: Vec<_> = result
        .likely_tests
        .iter()
        .filter(|test| test.path == "tests/test_application.py")
        .collect();
    assert_eq!(constructor_rows.len(), 1, "{tests:?}");
    assert_eq!(constructor_rows[0].name, "tests/test_application.py");
    assert_eq!(
        constructor_rows[0].reason,
        "constructor-only callers (2 targets)"
    );
    assert!(
        position("test_runs_cli_command") < position("tests/test_application.py"),
        "{tests:?}"
    );
    let formatted = format_blast_radius(&result);
    assert!(
        formatted.contains("constructor-only callers (2 targets)"),
        "{formatted}"
    );
    assert!(
        !formatted.contains("`test_constructs_app_only`"),
        "{formatted}"
    );
}

#[test]
fn blast_radius_lists_tests_through_a_constructor_last_and_skips_app_factories() {
    let (_repo, db_path) = scanned_repo(&[
        (
            "src/web/cli.py",
            "class Group:\n    def __init__(self):\n        self.name = 'cli'\n\n\ndef invoke_cli():\n    return 1\n",
        ),
        (
            "src/web/app.py",
            "from web.cli import Group\n\n\nclass Flask:\n    def __init__(self):\n        self.cli = Group()\n",
        ),
        (
            "tests/test_app.py",
            "from web.app import Flask\n\n\ndef test_builds():\n    assert Flask()\n",
        ),
        (
            "tests/test_apps/factory.py",
            "from web.app import Flask\n\n\ndef create_app():\n    return Flask()\n",
        ),
        (
            "src/web/runner.py",
            "from web.cli import invoke_cli\n\n\ndef run():\n    return invoke_cli()\n",
        ),
        (
            "tests/test_zz_run.py",
            "from web.runner import run\n\n\ndef test_zz_invoke():\n    assert run()\n\n\nclass TestRun:\n    def helper(self):\n        return run()\n\n    def test_uses_helper(self):\n        assert self.helper()\n",
        ),
    ]);
    let conn = open_read_only(&db_path).unwrap();

    let result = compute_blast_radius(&conn, &[], &["src/web/cli.py"], 2, 20).unwrap();

    let tests: Vec<&str> = result
        .likely_tests
        .iter()
        .map(|t| t.name.as_str())
        .collect();
    assert!(!tests.contains(&"create_app"), "{tests:?}");
    assert!(tests.contains(&"TestRun::helper"), "{tests:?}");
    let position = |name: &str| {
        tests
            .iter()
            .position(|t| *t == name)
            .unwrap_or_else(|| panic!("{name} missing: {tests:?}"))
    };
    assert!(
        position("test_zz_invoke") < position("test_builds"),
        "{tests:?}"
    );
}

#[test]
fn import_sites_list_imports_whose_module_holds_the_definition() {
    let (_repo, db_path) = scanned_repo(&[
        ("src/pkg/app.py", "class App:\n    pass\n"),
        ("src/pkg/__init__.py", "from .app import App as App\n"),
        ("tests/test_app.py", "from pkg.app import App\n"),
        ("tests/test_other.py", "from other.widgets import App\n"),
        (
            "src/pkg/cli.py",
            "def load():\n    from . import App\n    return App\n",
        ),
    ]);
    let conn = open_read_only(&db_path).unwrap();

    let sites = code_kb_core::import_sites(&conn, "App", Some("src/pkg/app.py")).unwrap();

    let paths: Vec<&str> = sites.iter().map(|(path, _)| path.as_str()).collect();
    assert_eq!(
        paths,
        vec!["src/pkg/__init__.py", "src/pkg/cli.py", "tests/test_app.py"]
    );
}

#[test]
fn a_member_access_on_an_unknown_type_or_to_a_private_module_function_is_not_a_reference() {
    let (_repo, db_path) = scanned_repo(&[
        (
            "src/series.ts",
            "export class Series {\n  contains(x: number): boolean {\n    return x > 0;\n  }\n}\n\nconst error = () => 1;\n\nexport function run(): number {\n  return error();\n}\n",
        ),
        (
            "tests/series.test.ts",
            "import { Series } from '../src/series';\n\ntest('reads', () => {\n  const s = new Series();\n  const a = Assert.contains;\n  const b = s.contains;\n  const c = parsed.error;\n});\n",
        ),
    ]);
    let conn = open_read_only(&db_path).unwrap();
    let lines = |name: &str| -> Vec<(String, Option<usize>)> {
        find_references_scoped(&conn, name, "callers", 20, false, Some("src/series.ts"))
            .unwrap()
            .into_iter()
            .map(|r| (r.path, r.start_line))
            .collect()
    };

    let contains = lines("contains");
    assert!(
        contains.contains(&("tests/series.test.ts".to_string(), Some(6))),
        "{contains:?}"
    );
    assert!(
        !contains.contains(&("tests/series.test.ts".to_string(), Some(5))),
        "{contains:?}"
    );
    assert_eq!(
        lines("error"),
        vec![("src/series.ts".to_string(), Some(10))]
    );
}

#[test]
fn related_tests_leave_out_setup_methods_helper_classes_ambiguous_names_and_documents() {
    let (_repo, db_path) = scanned_repo(&[
        (
            "src/calc.py",
            "class Calc:\n    def total(self):\n        return 1\n\n    def add(self):\n        return 2\n",
        ),
        ("src/other.py", "def add():\n    return 3\n"),
        (
            "docs/page.html",
            "<html><body><form></form></body></html>\n",
        ),
        (
            "tests/test_calc.py",
            "import unittest\nfrom calc import Calc\n\n\nclass TotalHolder:\n    pass\n\n\nclass CalcTest(unittest.TestCase):\n    def setUp(self):\n        Calc().total()\n\n    def test_total(self):\n        pass\n\n\ndef test_add_twice():\n    pass\n\n\ndef test_form():\n    pass\n",
        ),
    ]);
    let conn = open_read_only(&db_path).unwrap();
    let related = |name: &str, path: &str| -> Vec<String> {
        let target = code_kb_core::get_symbol_by_name(&conn, name, Some(path))
            .unwrap()
            .unwrap();
        code_kb_core::find_related_tests(&conn, &target, 10)
            .unwrap()
            .into_iter()
            .map(|t| t.name)
            .collect()
    };

    assert_eq!(related("total", "src/calc.py"), vec!["test_total"]);
    assert!(related("add", "src/calc.py").is_empty());
    assert!(related("form", "docs/page.html").is_empty());
}

#[test]
fn only_a_function_inside_the_body_of_another_function_counts_as_nested() {
    let (_repo, db_path) = scanned_repo(&[(
        "src/view.js",
        "function View(name) {\n  this.name = name;\n}\n\nView.prototype.lookup = function lookup(name) {\n  return name;\n};\n\nfunction outer() {\n  function lookupLocal() {\n    return 1;\n  }\n  return lookupLocal();\n}\n",
    )]);
    let conn = open_read_only(&db_path).unwrap();

    let rows =
        code_kb_core::fts_search_symbols_explained(&conn, "lookup", None, None, false, 20, true)
            .unwrap();
    let nested = |name: &str| {
        rows.iter()
            .find(|r| r.symbol.name == name)
            .and_then(|r| r.explain.as_ref())
            .map(|e| e.nested)
            .unwrap_or_else(|| panic!("{name} missing: {rows:?}"))
    };

    assert_eq!(nested("lookup"), 0.0);
    assert!(nested("lookupLocal") < 0.0);
}

#[test]
fn qualify_members_names_the_class_of_a_member_but_not_of_a_document_heading() {
    let (_repo, db_path) = scanned_repo(&[
        (
            "src/cli.py",
            "class ScriptInfo:\n    def __init__(self):\n        pass\n\n\ndef main():\n    pass\n",
        ),
        ("docs/guide.md", "# Setup\n\n## Install\n\nText.\n"),
        (
            "lib/cart.dart",
            "class Cart {\n  final List<int> items;\n  const Cart.empty() : items = const [];\n}\n",
        ),
    ]);
    let conn = open_read_only(&db_path).unwrap();
    let mut rows: Vec<_> = ["__init__", "main", "Install", "Cart.empty"]
        .iter()
        .map(|name| {
            code_kb_core::get_symbol_by_name(&conn, name, None)
                .unwrap()
                .unwrap()
        })
        .collect();

    code_kb_core::qualify_members(&conn, rows.iter_mut()).unwrap();

    let names: Vec<&str> = rows.iter().map(|s| s.name.as_str()).collect();
    assert_eq!(
        names,
        vec!["ScriptInfo.__init__", "main", "Install", "Cart.empty"]
    );
}

#[test]
fn super_and_self_calls_reach_the_nearest_ancestor_that_defines_the_method() {
    let (_repo, db_path) = scanned_repo(&[
        (
            "src/base.py",
            "class Scaffold:\n    def __init__(self):\n        pass\n\n    def add_rule(self):\n        pass\n\n\nclass App(Scaffold):\n    def __init__(self):\n        super().__init__()\n\n    def add_rule(self):\n        pass\n",
        ),
        (
            "src/app.py",
            "from base import App\n\n\nclass Flask(App):\n    def __init__(self):\n        super().__init__()\n        self.add_rule()\n",
        ),
    ]);
    let conn = open_read_only(&db_path).unwrap();
    let lines = |class: &str, name: &str| -> Vec<(String, Option<usize>)> {
        code_kb_core::find_references_for_symbol(
            &conn,
            name,
            "callers",
            20,
            &member_of(&conn, class, name),
        )
        .unwrap()
        .into_iter()
        .map(|site| (site.path, site.start_line))
        .collect()
    };

    assert_eq!(
        lines("App", "add_rule"),
        vec![("src/app.py".to_string(), Some(7))]
    );
    assert!(lines("Scaffold", "add_rule").is_empty());
    assert_eq!(
        lines("Scaffold", "__init__"),
        vec![("src/base.py".to_string(), Some(11))]
    );

    let app_init = code_kb_core::find_references_for_symbol(
        &conn,
        "__init__",
        "callers",
        20,
        &member_of(&conn, "App", "__init__"),
    )
    .unwrap();
    assert_eq!(
        app_init
            .iter()
            .map(|s| (s.path.as_str(), s.start_line))
            .collect::<Vec<_>>(),
        vec![("src/app.py", Some(6))]
    );
}

fn member_of(conn: &rusqlite::Connection, class: &str, name: &str) -> String {
    conn.query_row(
        "SELECT m.symbol_id FROM symbols m JOIN symbols c ON c.symbol_id = m.parent_symbol_id
         WHERE c.name = ?1 AND m.name = ?2",
        [class, name],
        |row| row.get(0),
    )
    .unwrap()
}

#[test]
fn a_constructor_gets_the_tests_that_build_its_class_even_one_defined_in_the_test() {
    let (_repo, db_path) = scanned_repo(&[
        (
            "src/app.py",
            "class Flask:\n    def __init__(self, name):\n        self.name = name\n",
        ),
        (
            "tests/test_app.py",
            "from app import Flask\n\n\ndef test_builds():\n    Flask('x')\n\n\ndef test_local():\n    class Quiet(Flask):\n        pass\n\n    Quiet('y')\n",
        ),
    ]);
    let conn = open_read_only(&db_path).unwrap();
    let init = code_kb_core::get_symbol_by_id(&conn, &member_of(&conn, "Flask", "__init__"))
        .unwrap()
        .unwrap();

    let related: Vec<String> = code_kb_core::find_related_tests(&conn, &init, 5)
        .unwrap()
        .into_iter()
        .map(|t| t.name)
        .collect();
    assert_eq!(related, vec!["test_builds"]);

    let quiet = find_references_scoped(&conn, "Quiet", "callers", 20, false, None).unwrap();
    assert_eq!(caller_names(&quiet), vec!["test_local"]);
}

#[test]
fn a_callee_row_names_the_definition_it_reaches_or_the_candidates() {
    let (_repo, db_path) = scanned_repo(&[
        ("src/a.py", "def helper():\n    return 1\n"),
        ("src/b.py", "def helper():\n    return 2\n"),
        (
            "src/jobs.py",
            "def drain():\n    return helper()\n\n\nclass Runner:\n    def run(self):\n        self.step()\n\n    def step(self):\n        return 3\n",
        ),
    ]);
    let conn = open_read_only(&db_path).unwrap();

    let drain = find_references_scoped(&conn, "drain", "callees", 20, false, None).unwrap();
    let run = find_references_scoped(&conn, "run", "callees", 20, false, None).unwrap();

    let helper = drain.iter().find(|r| r.to_symbol_name == "helper");
    assert_eq!(
        helper.and_then(|r| r.target.as_deref()),
        Some("one of `helper` (src/a.py:1), `helper` (src/b.py:1)"),
        "{drain:?}"
    );
    let step = run.iter().find(|r| r.to_symbol_name == "step");
    assert_eq!(
        step.and_then(|r| r.target.as_deref()),
        Some("`Runner.step` (src/jobs.py:9)"),
        "{run:?}"
    );
}

#[test]
fn a_subclass_write_to_an_attribute_its_base_defines_is_not_a_definition() {
    let (repo, db_path) = scanned_repo(&[
        (
            "src/base.py",
            "class Base:\n    label: str\n\n    def __init__(self):\n        self.ready = False\n\n    @property\n    def mode(self):\n        return 1\n",
        ),
        (
            "src/app.py",
            "from .base import Base\n\n\nclass App(Base):\n    def run(self):\n        self.ready = True\n        self.mode = 2\n        self.label = \"app\"\n        self.own = 3\n",
        ),
    ]);
    let conn = open_read_only(&db_path).unwrap();
    let workspace = Workspace::new(repo.path().to_path_buf());

    let skeleton = file_skeleton_op(&workspace, &db_path, &conn, "src/app.py").unwrap();
    let ready = search_symbols_scoped(&conn, "ready", None, None, false, 20).unwrap();
    let mode = fts_search_symbols_scoped(&conn, "mode", None, None, false, 20).unwrap();

    assert!(!skeleton.contains("self.ready"), "{skeleton}");
    assert!(!skeleton.contains("self.mode"), "{skeleton}");
    assert!(skeleton.contains("self.label"), "{skeleton}");
    assert!(skeleton.contains("self.own"), "{skeleton}");
    let paths: Vec<_> = ready.iter().map(|s| s.path.as_str()).collect();
    assert_eq!(paths, ["src/base.py"]);
    assert!(
        mode.iter().all(|hit| hit.symbol.path == "src/base.py"),
        "{mode:?}"
    );
}

#[test]
fn a_whole_test_file_row_replaces_the_rows_of_the_tests_inside_it() {
    let (_repo, db_path) = scanned_repo(&[
        ("src/store.py", "def path_for(root):\n    return root\n"),
        (
            "tests/test_store.py",
            "from store import path_for\n\n\ndef test_path():\n    assert path_for('r')\n",
        ),
        (
            "tests/test_other.py",
            "from store import path_for\n\n\ndef test_other_path():\n    assert path_for('r')\n",
        ),
    ]);
    let conn = open_read_only(&db_path).unwrap();

    let result = compute_blast_radius(&conn, &["path_for"], &[], 2, 20).unwrap();

    let rows: Vec<(&str, &str)> = result
        .likely_tests
        .iter()
        .map(|t| (t.path.as_str(), t.name.as_str()))
        .collect();
    assert_eq!(
        rows,
        [
            ("tests/test_store.py", "tests/test_store.py"),
            ("tests/test_other.py", "test_other_path"),
        ]
    );
}

#[test]
fn a_relative_typescript_import_with_a_js_extension_picks_the_file_it_names() {
    let (_repo, db_path) = scanned_repo(&[
        (
            "src/v3/helpers/util.ts",
            "export namespace util {\n  export function joinValues(values: string[]): string {\n    return values.join(\"|\");\n  }\n}\n",
        ),
        (
            "src/v4/core/util.ts",
            "export function joinValues(values: string[]): string {\n  return values.join(\", \");\n}\n",
        ),
        (
            "src/v3/core/util.ts",
            "export function joinValues(values: string[]): string {\n  return values.join(\"/\");\n}\n",
        ),
        (
            "src/v3/locales/en.ts",
            "import { util } from \"../helpers/util.js\";\n\nexport function enMessage(values: string[]): string {\n  return util.joinValues(values);\n}\n",
        ),
        (
            "src/v4/locales/ru.ts",
            "import * as util from \"../core/util.js\";\n\nexport function ruMessage(values: string[]): string {\n  return util.joinValues(values);\n}\n",
        ),
    ]);
    let conn = open_read_only(&db_path).unwrap();
    let callers = |file: &str| {
        caller_names(
            &find_references_scoped(&conn, "joinValues", "callers", 20, false, Some(file)).unwrap(),
        )
    };

    assert_eq!(callers("src/v4/core/util.ts"), vec!["ruMessage"]);
    assert_eq!(callers("src/v3/helpers/util.ts"), vec!["enMessage"]);
    assert!(callers("src/v3/core/util.ts").is_empty());
}

#[test]
fn an_attribute_write_stays_when_only_an_unrelated_class_of_the_base_name_defines_it() {
    let (repo, db_path) = scanned_repo(&[
        ("src/models/base.py", "class Base:\n    pass\n"),
        (
            "src/other/base.py",
            "class Base:\n    def __init__(self):\n        self.ready = False\n",
        ),
        (
            "src/app/app.py",
            "from models.base import Base\n\n\nclass App(Base):\n    def run(self):\n        self.ready = True\n",
        ),
    ]);
    let conn = open_read_only(&db_path).unwrap();
    let workspace = Workspace::new(repo.path().to_path_buf());

    let skeleton = file_skeleton_op(&workspace, &db_path, &conn, "src/app/app.py").unwrap();

    assert!(skeleton.contains("self.ready"), "{skeleton}");
}

#[test]
fn lookup_fills_its_limit_after_leaving_out_inherited_writes() {
    let (_repo, db_path) = scanned_repo(&[
        (
            "src/base.py",
            "class Base:\n    def __init__(self):\n        self.ready = False\n",
        ),
        (
            "src/app.py",
            "from .base import Base\n\n\nclass App(Base):\n    def run(self):\n        self.ready = True\n",
        ),
        (
            "src/other.py",
            "class Other:\n    def __init__(self):\n        self.ready = 1\n",
        ),
    ]);
    let conn = open_read_only(&db_path).unwrap();

    let ready = search_symbols_scoped(&conn, "ready", None, None, false, 2).unwrap();

    let paths: Vec<_> = ready.iter().map(|s| s.path.as_str()).collect();
    assert_eq!(paths, ["src/base.py", "src/other.py"]);
}

#[test]
fn folded_imports_do_not_use_up_the_lookup_limit() {
    let (_repo, db_path) = scanned_repo(&[
        ("src/app.py", "class Flask:\n    pass\n"),
        ("src/a.py", "from app import Flask\n"),
        ("src/b.py", "from app import Flask\n"),
        ("src/c.py", "from app import Flask\n"),
    ]);
    let conn = open_read_only(&db_path).unwrap();

    let rows = search_symbols_scoped(&conn, "Flask", None, None, false, 1).unwrap();

    let kinds: Vec<_> = rows.iter().map(|s| s.kind.as_str()).collect();
    assert_eq!(kinds, ["class", "import", "import", "import"]);
}

#[test]
fn a_lookup_ending_in_a_dot_lists_the_members_of_that_class_in_source_order() {
    let (_repo, db_path) = scanned_repo(&[(
        "src/app.py",
        "class App:\n    def run(self):\n        pass\n\n    def config(self):\n        pass\n\n\nclass Other:\n    def run(self):\n        pass\n",
    )]);
    let conn = open_read_only(&db_path).unwrap();

    let members = search_symbols_scoped(&conn, "App.", None, None, false, 20).unwrap();

    let names: Vec<_> = members
        .iter()
        .map(|s| (s.name.as_str(), s.start_line))
        .collect();
    assert_eq!(names, [("run", 2), ("config", 5)]);
}

#[test]
fn a_member_lookup_skips_a_markdown_heading_with_the_class_name() {
    let (_repo, db_path) = scanned_repo(&[
        ("README.md", "# App\n\n## Install\n\nText.\n"),
        (
            "src/app.py",
            "class App:\n    def run(self):\n        pass\n",
        ),
    ]);
    let conn = open_read_only(&db_path).unwrap();

    let members = search_symbols_scoped(&conn, "App.", None, None, false, 20).unwrap();

    let names: Vec<_> = members.iter().map(|s| s.name.as_str()).collect();
    assert_eq!(names, ["run"]);
}

#[test]
fn the_kind_filter_accepts_the_attribute_kind_that_lookup_shows() {
    let (_repo, db_path) = scanned_repo(&[(
        "src/app.py",
        "class App:\n    def __init__(self):\n        self.cli = 1\n\n    def run(self):\n        pass\n",
    )]);
    let conn = open_read_only(&db_path).unwrap();

    let members = search_symbols_scoped(&conn, "App.", Some("attribute"), None, false, 20).unwrap();
    let exact =
        search_symbols_scoped(&conn, "App.cli", Some("attribute"), None, false, 20).unwrap();

    let names: Vec<_> = members.iter().map(|s| s.name.as_str()).collect();
    assert_eq!(names, ["cli"]);
    assert_eq!(exact.len(), 1);
}

fn bare_callers(files: &[(&str, &str)], target: &str) -> Vec<String> {
    let (_repo, db_path) = scanned_repo(files);
    let conn = open_read_only(&db_path).unwrap();
    let refs = find_references_scoped(&conn, target, "callers", 20, false, None).unwrap();
    caller_names(&refs)
}

#[test]
fn a_bare_java_call_reaches_a_method_the_caller_inherits_from_a_base_in_another_file() {
    let callers = bare_callers(
        &[
            ("src/Base.java", "class Base {\n    void helper() {}\n}\n"),
            (
                "src/Child.java",
                "class Child extends Base {\n    void run() {\n        helper();\n    }\n}\n",
            ),
        ],
        "helper",
    );

    assert_eq!(callers, vec!["run"]);
}

#[test]
fn a_bare_swift_call_reaches_a_method_the_test_class_inherits() {
    let callers = bare_callers(
        &[(
            "Tests/CombineTests.swift",
            "class CombineTestCase {\n    func store(_ body: () -> Void) {}\n}\n\nfinal class DataTests: CombineTestCase {\n    func testPublish() {\n        store {\n        }\n    }\n}\n",
        )],
        "store",
    );

    assert_eq!(callers, vec!["testPublish"]);
}

#[test]
fn a_bare_csharp_call_reaches_a_method_in_another_part_of_a_partial_class() {
    let callers = bare_callers(
        &[(
            "src/LinqBridge.cs",
            "static partial class Enumerable\n{\n    private static void CheckNotNull(object source, string name) {}\n}\n\nstatic partial class Enumerable\n{\n    public static int Sum(object source)\n    {\n        CheckNotNull(source, \"source\");\n        return 0;\n    }\n}\n",
        )],
        "CheckNotNull",
    );

    assert_eq!(callers, vec!["Sum"]);
}

#[test]
fn a_bare_cpp_call_in_a_struct_method_reaches_a_function_of_the_enclosing_namespace() {
    let callers = bare_callers(
        &[(
            "src/doctest.h",
            "namespace doctest {\nconst char* assertString(int at) { return \"\"; }\nstruct JUnitReporter {\n    void log_assert() {\n        assertString(1);\n    }\n};\n}\n",
        )],
        "assertString",
    );

    assert_eq!(callers, vec!["log_assert"]);
}

#[test]
fn a_csharp_constructor_call_reaches_a_sibling_nested_class() {
    let callers = bare_callers(
        &[(
            "src/Tests.cs",
            "public class Tests\n{\n    public class RootSomethingElse {}\n\n    public class Something\n    {\n        public Something()\n        {\n            var other = new RootSomethingElse();\n        }\n    }\n}\n",
        )],
        "RootSomethingElse",
    );

    assert_eq!(callers, vec!["Something"]);
}

#[test]
fn a_bare_python_call_never_reaches_a_method_of_a_base_class() {
    let callers = bare_callers(
        &[(
            "src/app.py",
            "class Base:\n    def helper(self):\n        pass\n\n\nclass Child(Base):\n    def run(self):\n        helper()\n",
        )],
        "helper",
    );

    assert!(callers.is_empty(), "got {callers:?}");
}

#[test]
fn a_csharp_constructor_call_reaches_a_class_of_the_enclosing_namespace() {
    let (_repo, db_path) = scanned_repo(&[(
        "src/ConverterTests.cs",
        "namespace Tests.Converters\n{\n    public class ConverterTests\n    {\n        public void ConverterDictionary()\n        {\n            var items = new ConverterDictionary<object>();\n        }\n    }\n\n    public class ConverterDictionary<T>\n    {\n    }\n}\n",
    )]);
    let conn = open_read_only(&db_path).unwrap();
    let class_id: String = conn
        .query_row(
            "SELECT symbol_id FROM symbols WHERE name = 'ConverterDictionary' AND kind = 'class'",
            [],
            |row| row.get(0),
        )
        .unwrap();

    let refs =
        find_references_for_symbol(&conn, "ConverterDictionary", "callers", 20, &class_id).unwrap();

    assert!(
        refs.iter().any(|r| r.kind == "instantiates"),
        "got {refs:?}"
    );
}

#[test]
fn a_bare_call_in_a_nested_csharp_class_prefers_the_nearest_enclosing_definition() {
    let callers = bare_callers(
        &[(
            "src/Outer.cs",
            "public class Outer\n{\n    static void Log() {}\n\n    public class Middle\n    {\n        static void Log() {}\n\n        public class Inner\n        {\n            void Run()\n            {\n                Log();\n            }\n        }\n    }\n}\n",
        )],
        "Outer.Log",
    );

    assert!(callers.is_empty(), "got {callers:?}");
}

#[test]
fn a_bare_python_call_in_a_nested_function_never_reaches_a_method_of_the_enclosing_class() {
    let callers = bare_callers(
        &[(
            "src/app.py",
            "class App:\n    def helper(self):\n        pass\n\n    def outer(self):\n        def inner():\n            helper()\n        inner()\n",
        )],
        "helper",
    );

    assert!(callers.is_empty(), "got {callers:?}");
}

#[test]
fn a_this_call_in_an_object_literal_method_reaches_a_sibling_method() {
    let callers = bare_callers(
        &[(
            "src/input.js",
            "var Input = {\n  setInputValue: function (value) {\n    return value;\n  },\n  resetInputValue: function () {\n    this.setInputValue(1);\n  }\n};\n",
        )],
        "setInputValue",
    );

    assert_eq!(callers, vec!["resetInputValue"]);
}

#[test]
fn a_super_call_never_reaches_a_method_of_the_callers_own_class() {
    let callers = bare_callers(
        &[(
            "src/child.js",
            "class Base {\n  run() {}\n}\n\nclass Child extends Base {\n  run() {\n    super.run();\n  }\n}\n",
        )],
        "Child.run",
    );

    assert!(callers.is_empty(), "got {callers:?}");
}

#[test]
fn a_bare_ruby_call_follows_only_the_closest_base_class_of_that_name() {
    let (_repo, db_path) = scanned_repo(&[
        (
            "lib/app/base.rb",
            "module App\n  class Base\n    def options\n    end\n  end\nend\n",
        ),
        (
            "lib/guard/base.rb",
            "module Guard\n  class Base\n    def options\n    end\n  end\nend\n",
        ),
        (
            "lib/guard/token.rb",
            "module Guard\n  class Token < Base\n    def accepts\n      options\n    end\n  end\nend\n",
        ),
    ]);
    let conn = open_read_only(&db_path).unwrap();
    let options_in = |path: &str| -> Vec<String> {
        let id: String = conn
            .query_row(
                "SELECT symbol_id FROM symbols WHERE name = 'options' AND path = ?1",
                [path],
                |row| row.get(0),
            )
            .unwrap();
        caller_names(&find_references_for_symbol(&conn, "options", "callers", 20, &id).unwrap())
    };

    assert_eq!(options_in("lib/guard/base.rb"), vec!["accepts"]);
    assert!(options_in("lib/app/base.rb").is_empty());
}

#[test]
fn an_import_in_a_closer_scope_hides_a_definition_of_an_enclosing_scope() {
    let callers = bare_callers(
        &[(
            "src/scopes.cpp",
            "namespace outer {\nvoid helper() {}\nnamespace middle {\nusing tools::helper;\nstruct Inner {\n    void run() {\n        helper();\n    }\n};\n}\n}\n",
        )],
        "helper",
    );

    assert!(callers.is_empty(), "got {callers:?}");
}

#[test]
fn a_csharp_constructor_call_reaches_a_class_of_the_enclosing_file_scoped_namespace() {
    let (_repo, db_path) = scanned_repo(&[(
        "src/ConverterTests.cs",
        "namespace Tests.Converters;\n\npublic class ConverterTests\n{\n    public void ConverterDictionary()\n    {\n        var items = new ConverterDictionary<object>();\n    }\n}\n\npublic class ConverterDictionary<T>\n{\n}\n",
    )]);
    let conn = open_read_only(&db_path).unwrap();
    let class_id: String = conn
        .query_row(
            "SELECT symbol_id FROM symbols WHERE name = 'ConverterDictionary' AND kind = 'class'",
            [],
            |row| row.get(0),
        )
        .unwrap();

    let refs =
        find_references_for_symbol(&conn, "ConverterDictionary", "callers", 20, &class_id).unwrap();

    assert!(
        refs.iter().any(|r| r.kind == "instantiates"),
        "got {refs:?}"
    );
}

#[test]
fn module_level_lambda_callers_allow_a_null_parent_and_keep_raw_names() {
    let (_repo, db) = scanned_repo(&[
        (
            "flask.py",
            "class Flask:\n    def __init__(self, name):\n        pass\n",
        ),
        (
            "app.py",
            "from flask import Flask\nmodule_app = lambda: Flask(\"module\")\n",
        ),
    ]);
    let conn = open_read_only(&db).unwrap();
    let flask_id: String = conn
        .query_row(
            "SELECT symbol_id FROM symbols WHERE name = 'Flask' AND kind = 'class'",
            [],
            |row| row.get(0),
        )
        .unwrap();

    let refs_result = find_references_for_symbol(&conn, "Flask", "callers", 20, &flask_id);
    assert!(
        refs_result.is_ok(),
        "module-level lambda failed: {refs_result:?}"
    );
    let refs = refs_result.unwrap();
    let lambda = refs
        .iter()
        .find(|site| site.from_symbol_name.starts_with("lambda_"))
        .expect("Flask should have a module-level lambda caller");
    let raw_name = lambda.from_symbol_name.as_str();
    assert!(lambda.enclosing_symbol_name.is_none());
    let text = format_references("Flask", &refs, "callers", 20);
    assert!(text.contains(&format!("`{raw_name}`")), "{text}");
    let json = serde_json::to_value(&refs).unwrap();
    assert!(json.as_array().unwrap().iter().any(|row| {
        row["from_symbol_name"].as_str() == Some(raw_name)
            && row.get("enclosing_symbol_name").is_none()
    }));

    let blast = compute_blast_radius(&conn, &["Flask"], &[], 2, 20).unwrap();
    let impacted = blast
        .impacted_symbols
        .iter()
        .find(|symbol| symbol.name == raw_name)
        .expect("blast radius should include the module-level lambda");
    assert!(impacted.enclosing_symbol_name.is_none());
    assert!(
        format_blast_radius(&blast).contains(&format!("`{raw_name}`")),
        "{}",
        format_blast_radius(&blast)
    );
}

#[test]
fn a_real_function_named_lambda_123_keeps_its_name_in_references_and_blast_radius() {
    let (_repo, db) = scanned_repo(&[
        (
            "flask.py",
            "class Flask:\n    def __init__(self, name):\n        pass\n",
        ),
        (
            "app.py",
            "from flask import Flask\ndef outer():\n    def lambda_123():\n        return Flask(\"real function\")\n    return lambda_123()\n",
        ),
    ]);
    let conn = open_read_only(&db).unwrap();
    let flask_id: String = conn
        .query_row(
            "SELECT symbol_id FROM symbols WHERE name = 'Flask' AND kind = 'class'",
            [],
            |row| row.get(0),
        )
        .unwrap();
    let refs = find_references_for_symbol(&conn, "Flask", "callers", 20, &flask_id).unwrap();
    let caller = refs
        .iter()
        .find(|site| site.from_symbol_name == "lambda_123")
        .expect("Flask should have a lambda_123 function caller");
    assert!(caller.enclosing_symbol_name.is_none());
    let references_text = format_references("Flask", &refs, "callers", 20);
    assert!(
        references_text.contains("`lambda_123`"),
        "{references_text}"
    );
    assert!(
        !references_text.contains("<lambda> in outer"),
        "{references_text}"
    );
    let references_json = serde_json::to_value(&refs).unwrap();
    assert!(references_json.as_array().unwrap().iter().any(|row| {
        row["from_symbol_name"] == "lambda_123" && row.get("enclosing_symbol_name").is_none()
    }));

    let blast = compute_blast_radius(&conn, &["Flask"], &[], 2, 20).unwrap();
    let impacted = blast
        .impacted_symbols
        .iter()
        .find(|symbol| symbol.name == "lambda_123")
        .expect("blast radius should include the lambda_123 function");
    assert!(impacted.enclosing_symbol_name.is_none());
    let blast_text = format_blast_radius(&blast);
    assert!(blast_text.contains("`lambda_123`"), "{blast_text}");
    assert!(!blast_text.contains("<lambda> in outer"), "{blast_text}");
    let blast_json = serde_json::to_value(&blast).unwrap();
    assert!(
        blast_json["impacted_symbols"]
            .as_array()
            .unwrap()
            .iter()
            .any(|row| {
                row["name"] == "lambda_123" && row.get("enclosing_symbol_name").is_none()
            })
    );
}

#[test]
fn lambda_callers_render_the_enclosing_symbol_without_changing_json_names() {
    let (_repo, db) = scanned_repo(&[
        (
            "flask.py",
            "class Flask:\n    def __init__(self, name):\n        pass\n",
        ),
        (
            "tests/test_cli.py",
            "class ScriptInfo:\n    def __init__(self, create_app):\n        self.create_app = create_app\n\ndef test_scriptinfo():\n    ScriptInfo(create_app=lambda: Flask(\"testapp\"))\n",
        ),
    ]);
    let conn = open_read_only(&db).unwrap();
    let flask_id: String = conn
        .query_row(
            "SELECT symbol_id FROM symbols WHERE name = 'Flask' AND kind = 'class'",
            [],
            |row| row.get(0),
        )
        .unwrap();
    let refs = find_references_for_symbol(&conn, "Flask", "callers", 20, &flask_id).unwrap();
    let lambda = refs
        .iter()
        .find(|site| site.from_symbol_name.starts_with("lambda_"))
        .expect("Flask should have a lambda caller");

    let references_text = format_references("Flask", &refs, "callers", 20);
    assert!(
        references_text.contains("`<lambda> in test_scriptinfo`"),
        "{references_text}"
    );
    let references_json = serde_json::to_value(&refs).unwrap();
    let raw_lambda_name = lambda.from_symbol_name.as_str();
    let lambda_json = references_json
        .as_array()
        .unwrap()
        .iter()
        .find(|row| {
            row.get("from_symbol_name")
                .and_then(serde_json::Value::as_str)
                == Some(raw_lambda_name)
        })
        .expect("JSON should retain the raw lambda name");
    assert!(lambda_json.get("enclosing_symbol_name").is_none());

    let blast = compute_blast_radius(&conn, &["Flask"], &[], 2, 20).unwrap();
    let blast_text = format_blast_radius(&blast);
    assert!(
        blast_text.contains("`<lambda> in test_scriptinfo`"),
        "{blast_text}"
    );
    assert!(
        blast
            .impacted_symbols
            .iter()
            .any(|symbol| symbol.name.starts_with("lambda_")),
        "{:?}",
        blast.impacted_symbols
    );
    let blast_json = serde_json::to_value(&blast).unwrap();
    let lambda_json = blast_json["impacted_symbols"]
        .as_array()
        .unwrap()
        .iter()
        .find(|symbol| {
            symbol.get("name").and_then(serde_json::Value::as_str) == Some(raw_lambda_name)
        })
        .expect("JSON should retain the raw lambda name");
    assert!(lambda_json.get("enclosing_symbol_name").is_none());
}