codenexus 0.3.4

A queryable code knowledge graph tool built on LadybugDB and tree-sitter
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
// Copyright (c) 2026 Kirky.X. All rights reserved.
// SPDX-License-Identifier: MIT

//! Structured search by name / type / file (PRD §4.4.2), plus multi-mode
//! search (exact / regex / fuzzy / graph-enhanced / multi-signal).
//!
//! [`StructuredSearcher`] runs Cypher `MATCH` queries against specific node
//! tables, returning [`SearchResult`] lists. Because LadybugDB stores each
//! [`NodeLabel`] in a distinct table, "search all symbols by name" is
//! implemented as a fan-out across the relevant tables followed by a merge.
//!
//! [`SearchEngine`] provides the multi-mode search dispatcher (T019–T023).

use super::error::{QueryError, Result};
use super::SearchResult;
use crate::model::NodeLabel;
use crate::storage::capability::Storage;
use crate::storage::schema::{escape_cypher_string, escape_identifier};
use crate::storage::StorageConnection;
use serde::{Deserialize, Serialize};
use std::collections::{HashMap, HashSet};
use std::fmt;
use std::str::FromStr;

/// Maximum value accepted for [`SearchParams::limit`].
pub const MAX_LIMIT: usize = 500;
/// Default limit used by [`SearchParams::default`].
pub const DEFAULT_LIMIT: usize = 50;
/// Maximum Levenshtein distance accepted by fuzzy search.
pub const MAX_FUZZY_DISTANCE: usize = 3;

/// Selects which search algorithm [`SearchEngine::search`] dispatches to.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum SearchMode {
    /// Exact case-insensitive name match.
    Exact,
    /// Regular-expression match on name / qualifiedName.
    Regex,
    /// Levenshtein-distance fuzzy match.
    Fuzzy,
    /// Name match combined with degree / label graph filters.
    GraphEnhanced,
    /// Multi-signal scored search (name + degree + module + test coverage).
    MultiSignal,
}

impl fmt::Display for SearchMode {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            SearchMode::Exact => f.write_str("exact"),
            SearchMode::Regex => f.write_str("regex"),
            SearchMode::Fuzzy => f.write_str("fuzzy"),
            SearchMode::GraphEnhanced => f.write_str("graph_enhanced"),
            SearchMode::MultiSignal => f.write_str("multi_signal"),
        }
    }
}

impl FromStr for SearchMode {
    type Err = String;

    fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
        match s.trim().to_ascii_lowercase().as_str() {
            "" => Err("search mode is empty".to_string()),
            "exact" => Ok(SearchMode::Exact),
            "regex" => Ok(SearchMode::Regex),
            "fuzzy" => Ok(SearchMode::Fuzzy),
            "graph" | "graph_enhanced" | "graph-enhanced" => Ok(SearchMode::GraphEnhanced),
            "multi" | "multi_signal" | "multi-signal" => Ok(SearchMode::MultiSignal),
            other => Err(format!("unknown search mode: {other}")),
        }
    }
}

/// Parameters for [`SearchEngine::search`].
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct SearchParams {
    /// Search query (exact text, regex pattern, or fuzzy needle).
    pub query: String,
    /// Match strategy.
    pub mode: SearchMode,
    /// Restrict results to these node labels (e.g. `["Function"]`).
    pub label_filter: Option<Vec<String>>,
    /// `(min, max)` inclusive degree filter for graph-enhanced mode.
    pub degree_filter: Option<(u32, u32)>,
    /// Glob-style file path filter (currently informational).
    pub file_pattern: Option<String>,
    /// Maximum results to return (clamped to [`MAX_LIMIT`]).
    pub limit: usize,
}

impl Default for SearchParams {
    fn default() -> Self {
        Self {
            query: String::new(),
            mode: SearchMode::Exact,
            label_filter: None,
            degree_filter: None,
            file_pattern: None,
            limit: DEFAULT_LIMIT,
        }
    }
}

impl SearchParams {
    /// Clamps `limit` to `[0, MAX_LIMIT]`.
    pub fn clamped_limit(&self) -> usize {
        self.limit.min(MAX_LIMIT)
    }
}

/// Multi-mode search engine backed by a [`Storage`] capability.
///
/// Dispatches to mode-specific implementations via [`SearchEngine::search`]:
/// - [`SearchMode::Exact`] → name CONTAINS (case-insensitive)
/// - [`SearchMode::Regex`] → [`SearchEngine::search_regex`]
/// - [`SearchMode::Fuzzy`] → [`SearchEngine::search_fuzzy`]
/// - [`SearchMode::GraphEnhanced`] → [`SearchEngine::search_graph_enhanced`]
/// - [`SearchMode::MultiSignal`] → graph-enhanced + [`SearchEngine::score_multi_signal`]
pub struct SearchEngine<'a> {
    storage: &'a dyn Storage,
}

impl<'a> SearchEngine<'a> {
    /// Creates a new engine borrowing the given storage capability.
    #[must_use]
    pub fn new(storage: &'a dyn Storage) -> Self {
        Self { storage }
    }

    /// Dispatches to the appropriate search implementation based on `params.mode`.
    ///
    /// Results are sorted by descending `score`, then ascending `name`.
    ///
    /// # Errors
    ///
    /// Returns [`QueryError::InvalidQuery`] for empty queries, invalid regex,
    /// or `max_distance > MAX_FUZZY_DISTANCE`.
    pub fn search(&self, project: &str, params: &SearchParams) -> Result<Vec<SearchResult>> {
        let limit = params.clamped_limit();
        let mut results = match params.mode {
            SearchMode::Exact => self.search_exact(project, &params.query, limit)?,
            SearchMode::Regex => self.search_regex(project, &params.query)?,
            SearchMode::Fuzzy => self.search_fuzzy(project, &params.query, MAX_FUZZY_DISTANCE)?,
            SearchMode::GraphEnhanced => self.search_graph_enhanced(project, params)?,
            SearchMode::MultiSignal => {
                let mut hits = self.search_graph_enhanced(project, params)?;
                // A-04: batch-load TESTS targets + qn→node_id map once, then
                // score each candidate via in-memory HashSet/HashMap lookups.
                // Old path fired 17 storage queries per candidate (16 label
                // lookups + 1 TESTS edge count); new path fires 17 total.
                let tested_ids = self.load_tested_node_ids(project).unwrap_or_default();
                let qns: Vec<&str> = hits
                    .iter()
                    .filter_map(|h| h.qualified_name.as_deref())
                    .collect();
                let qn_to_id = self.load_qn_to_node_id_map(project, &qns);
                for hit in &mut hits {
                    hit.score = self.score_multi_signal(hit, params, &tested_ids, &qn_to_id);
                    hit.match_reason = "multi-signal".to_string();
                }
                hits
            }
        };
        results.sort_by(|a, b| {
            b.score
                .partial_cmp(&a.score)
                .unwrap_or(std::cmp::Ordering::Equal)
                .then_with(|| a.name.cmp(&b.name))
        });
        if limit < results.len() {
            results.truncate(limit);
        }
        Ok(results)
    }

    /// Exact case-insensitive substring search (delegates to storage-level CONTAINS).
    fn search_exact(&self, project: &str, query: &str, limit: usize) -> Result<Vec<SearchResult>> {
        if query.trim().is_empty() {
            return Err(QueryError::InvalidQuery(
                "query must not be empty".to_string(),
            ));
        }
        let escaped = escape_cypher_string(query);
        let project_esc = escape_cypher_string(project);
        let mut results = Vec::new();
        for &label in SYMBOL_LABELS {
            let table = escape_identifier(label.table_name());
            let cypher = format!(
                "MATCH (n:{table}) WHERE toLower(n.name) CONTAINS toLower('{escaped}') \
                 AND n.project = '{project_esc}' \
                 RETURN n.name AS name, n.qualifiedName AS qn, n.filePath AS filePath, \
                 n.startLine AS line;"
            );
            match self.storage.query(&cypher) {
                Ok(rows) => results.extend(rows_to_search_results(rows, label, query)),
                Err(_) => continue,
            }
        }
        if limit < results.len() {
            results.truncate(limit);
        }
        Ok(results)
    }

    /// Regex search over Function/Method/Class name and qualifiedName.
    ///
    /// Compiles `pattern` as a Rust `regex::Regex` and matches it against
    /// every `Function`, `Method`, and `Class` node in `project`. Both `name`
    /// and `qualifiedName` are tested (case-sensitive, `is_match` semantics).
    ///
    /// # Errors
    ///
    /// Returns [`QueryError::InvalidQuery`] if `pattern` is not a valid regex.
    fn search_regex(&self, project: &str, pattern: &str) -> Result<Vec<SearchResult>> {
        let re = regex::Regex::new(pattern)
            .map_err(|e| QueryError::InvalidQuery(format!("invalid regex: {e}")))?;
        let project_esc = escape_cypher_string(project);
        let labels = [NodeLabel::Function, NodeLabel::Method, NodeLabel::Class];
        let mut results = Vec::new();
        for &label in &labels {
            let table = escape_identifier(label.table_name());
            let cypher = format!(
                "MATCH (n:{table}) WHERE n.project = '{project_esc}' \
                 RETURN n.name AS name, n.qualifiedName AS qn, n.filePath AS filePath, \
                 n.startLine AS line;"
            );
            let rows = match self.storage.query(&cypher) {
                Ok(rows) => rows,
                Err(_) => continue,
            };
            for row in rows {
                let Some(name) = row.first().and_then(|v| v.as_str()) else {
                    continue;
                };
                let qn = row.get(1).and_then(|v| v.as_str()).map(String::from);
                let file_path = row.get(2).and_then(|v| v.as_str()).map(String::from);
                let start_line = row
                    .get(3)
                    .and_then(|v| v.as_i64())
                    .and_then(|i| u32::try_from(i).ok());
                let matched_on = if re.is_match(name) {
                    "name"
                } else if qn.as_deref().is_some_and(|q| re.is_match(q)) {
                    "qualifiedName"
                } else {
                    continue;
                };
                results.push(SearchResult {
                    name: name.to_string(),
                    label: label.to_string(),
                    file_path,
                    start_line,
                    qualified_name: qn,
                    score: 1.0,
                    match_reason: format!("regex match on {matched_on}"),
                    degree: 0,
                });
            }
        }
        Ok(results)
    }

    /// Fuzzy search using Levenshtein distance.
    ///
    /// Compares `query` (case-insensitive) against every symbol name in
    /// `project`. Results with `distance <= max_distance` are returned.
    /// `max_distance = 0` is equivalent to exact case-insensitive match.
    ///
    /// # Errors
    ///
    /// Returns [`QueryError::InvalidQuery`] if `query` is empty or
    /// `max_distance > MAX_FUZZY_DISTANCE`.
    fn search_fuzzy(
        &self,
        project: &str,
        query: &str,
        max_distance: usize,
    ) -> Result<Vec<SearchResult>> {
        if query.trim().is_empty() {
            return Err(QueryError::InvalidQuery(
                "fuzzy query must not be empty".to_string(),
            ));
        }
        if max_distance > MAX_FUZZY_DISTANCE {
            return Err(QueryError::InvalidQuery(format!(
                "max_distance {max_distance} exceeds limit {MAX_FUZZY_DISTANCE}"
            )));
        }
        let query_lower = query.to_ascii_lowercase();
        let project_esc = escape_cypher_string(project);
        let mut results = Vec::new();
        for &label in SYMBOL_LABELS {
            let table = escape_identifier(label.table_name());
            let cypher = format!(
                "MATCH (n:{table}) WHERE n.project = '{project_esc}' \
                 RETURN n.name AS name, n.qualifiedName AS qn, n.filePath AS filePath, \
                 n.startLine AS line;"
            );
            let rows = match self.storage.query(&cypher) {
                Ok(rows) => rows,
                Err(_) => continue,
            };
            for row in rows {
                let Some(name) = row.first().and_then(|v| v.as_str()) else {
                    continue;
                };
                let name_lower = name.to_ascii_lowercase();
                let dist = levenshtein(&query_lower, &name_lower);
                if dist > max_distance {
                    continue;
                }
                let qn = row.get(1).and_then(|v| v.as_str()).map(String::from);
                let file_path = row.get(2).and_then(|v| v.as_str()).map(String::from);
                let start_line = row
                    .get(3)
                    .and_then(|v| v.as_i64())
                    .and_then(|i| u32::try_from(i).ok());
                let max_len = query_lower.len().max(name_lower.len()).max(1);
                let score = 1.0 - (dist as f64 / max_len as f64);
                results.push(SearchResult {
                    name: name.to_string(),
                    label: label.to_string(),
                    file_path,
                    start_line,
                    qualified_name: qn,
                    score,
                    match_reason: format!("fuzzy d={dist}"),
                    degree: 0,
                });
            }
        }
        Ok(results)
    }

    /// Graph-enhanced search: name match + degree filter + label filter.
    ///
    /// Combines case-insensitive name CONTAINS with optional:
    /// - `label_filter`: restrict to specific node labels
    /// - `degree_filter`: `(min, max)` inclusive range on incoming CALLS count
    ///
    /// Score is the name relevance (exact=1.0, prefix=0.8, substring=0.5).
    fn search_graph_enhanced(
        &self,
        project: &str,
        params: &SearchParams,
    ) -> Result<Vec<SearchResult>> {
        if params.query.trim().is_empty() {
            return Err(QueryError::InvalidQuery(
                "query must not be empty".to_string(),
            ));
        }
        let project_esc = escape_cypher_string(project);
        let query = &params.query;

        // Determine which labels to search.
        let labels: Vec<NodeLabel> = match &params.label_filter {
            Some(names) => names.iter().filter_map(|n| parse_node_label(n)).collect(),
            None => SYMBOL_LABELS.to_vec(),
        };
        if labels.is_empty() {
            return Ok(Vec::new());
        }

        // Build CALLS indegree map: target_id → count.
        let degree_map = self.load_calls_indegree(project)?;

        let mut results = Vec::new();
        for &label in &labels {
            let table = escape_identifier(label.table_name());
            let cypher = format!(
                "MATCH (n:{table}) WHERE toLower(n.name) CONTAINS toLower('{escaped_q}') \
                 AND n.project = '{project_esc}' \
                 RETURN n.id AS id, n.name AS name, n.qualifiedName AS qn, \
                 n.filePath AS filePath, n.startLine AS line;",
                escaped_q = escape_cypher_string(query),
            );
            let rows = match self.storage.query(&cypher) {
                Ok(rows) => rows,
                Err(_) => continue,
            };
            for row in rows {
                let Some(name) = row.get(1).and_then(|v| v.as_str()) else {
                    continue;
                };
                let node_id = row
                    .first()
                    .and_then(|v| v.as_str())
                    .unwrap_or("")
                    .to_string();
                let degree = degree_map.get(&node_id).copied().unwrap_or(0);
                // Apply degree filter.
                if let Some((min, max)) = params.degree_filter {
                    if degree < min || degree > max {
                        continue;
                    }
                }
                let qn = row.get(2).and_then(|v| v.as_str()).map(String::from);
                let file_path = row.get(3).and_then(|v| v.as_str()).map(String::from);
                let start_line = row
                    .get(4)
                    .and_then(|v| v.as_i64())
                    .and_then(|i| u32::try_from(i).ok());
                let (score, reason) = relevance_score_with_reason(name, query);
                results.push(SearchResult {
                    name: name.to_string(),
                    label: label.to_string(),
                    file_path,
                    start_line,
                    qualified_name: qn,
                    score,
                    match_reason: format!("{reason} (degree={degree})"),
                    degree,
                });
            }
        }
        Ok(results)
    }

    /// Loads a map of `target_id → incoming CALLS count` for `project`.
    fn load_calls_indegree(&self, project: &str) -> Result<std::collections::HashMap<String, u32>> {
        let project_esc = escape_cypher_string(project);
        let cypher = format!(
            "MATCH (e:CodeRelation) WHERE e.type = 'CALLS' AND e.project = '{project_esc}' \
             RETURN e.target AS target;"
        );
        let mut map = std::collections::HashMap::new();
        match self.storage.query(&cypher) {
            Ok(rows) => {
                for row in rows {
                    if let Some(target) = row.first().and_then(|v| v.as_str()) {
                        *map.entry(target.to_string()).or_insert(0) += 1;
                    }
                }
            }
            Err(_) => return Ok(map),
        }
        Ok(map)
    }

    /// Multi-signal score in `[0.0, 1.0]`:
    /// - `name_relevance * 0.4` (exact=1.0, prefix/substring=0.8, no=0.0)
    /// - `degree_centrality * 0.3` (min(degree/100, 1.0))
    /// - `module_proximity * 0.2` (file_pattern match=1.0, else=0.5)
    /// - `test_coverage * 0.1` (has incoming TESTS edges=1.0, else=0.0)
    ///
    /// `tested_ids` and `qn_to_id` are batch-loaded once per MultiSignal
    /// search by [`SearchEngine::search`]; this method performs only
    /// in-memory lookups (A-04 batch refactor).
    fn score_multi_signal(
        &self,
        candidate: &SearchResult,
        params: &SearchParams,
        tested_ids: &HashSet<String>,
        qn_to_id: &HashMap<String, String>,
    ) -> f64 {
        let name_relevance = compute_name_relevance(&candidate.name, &params.query);
        let degree = candidate.degree;
        let degree_centrality = (degree as f64 / 100.0).min(1.0);
        let module_proximity = compute_module_proximity(&candidate.file_path, &params.file_pattern);
        let test_coverage = candidate
            .qualified_name
            .as_ref()
            .and_then(|qn| qn_to_id.get(qn))
            .map_or(0.0, |id| if tested_ids.contains(id) { 1.0 } else { 0.0 });

        name_relevance * 0.4
            + degree_centrality * 0.3
            + module_proximity * 0.2
            + test_coverage * 0.1
    }

    /// Batch-loads the set of node ids that are targets of `TESTS` edges in
    /// `project`. Single Cypher query regardless of candidate count (A-04).
    ///
    /// # Errors
    ///
    /// Returns the underlying [`QueryError`] if the storage query fails
    /// catastrophically; transient per-row parse failures are silently
    /// skipped (consistent with the rest of the search pipeline).
    fn load_tested_node_ids(&self, project: &str) -> Result<HashSet<String>> {
        let project_esc = escape_cypher_string(project);
        let cypher = format!(
            "MATCH (e:CodeRelation) WHERE e.type = 'TESTS' AND e.project = '{project_esc}' \
             RETURN e.target AS target;"
        );
        let mut set = HashSet::new();
        match self.storage.query(&cypher) {
            Ok(rows) => {
                for row in rows {
                    if let Some(target) = row.first().and_then(|v| v.as_str()) {
                        set.insert(target.to_string());
                    }
                }
            }
            Err(e) => return Err(e.into()),
        }
        Ok(set)
    }

    /// Batch-resolves `qualifiedName → node_id` for the supplied `qns` in
    /// `project`. Fans out across [`SYMBOL_LABELS`] once per label (16
    /// queries) regardless of candidate count, replacing the old per-
    /// candidate lookup (A-04).
    fn load_qn_to_node_id_map(&self, project: &str, qns: &[&str]) -> HashMap<String, String> {
        if qns.is_empty() {
            return HashMap::new();
        }
        let project_esc = escape_cypher_string(project);
        let qn_list = qns
            .iter()
            .map(|q| format!("'{}'", escape_cypher_string(q)))
            .collect::<Vec<_>>()
            .join(", ");
        let mut map = HashMap::new();
        for &label in SYMBOL_LABELS {
            let table = escape_identifier(label.table_name());
            let cypher = format!(
                "MATCH (n:{table}) WHERE n.qualifiedName IN [{qn_list}] \
                 AND n.project = '{project_esc}' \
                 RETURN n.qualifiedName AS qn, n.id AS id;"
            );
            if let Ok(rows) = self.storage.query(&cypher) {
                for row in rows {
                    let qn = row.first().and_then(|v| v.as_str());
                    let id = row.get(1).and_then(|v| v.as_str());
                    if let (Some(qn), Some(id)) = (qn, id) {
                        map.insert(qn.to_string(), id.to_string());
                    }
                }
            }
        }
        map
    }
}

/// Symbol-bearing node labels searched by [`StructuredSearcher::search`] and
/// [`StructuredSearcher::search_by_name`]. Project/Folder/File/Parameter are
/// excluded: Project has no `project` column, Folder/File are structural, and
/// Parameter is too granular for general symbol search.
const SYMBOL_LABELS: &[NodeLabel] = &[
    NodeLabel::Module,
    NodeLabel::Class,
    NodeLabel::Struct,
    NodeLabel::Enum,
    NodeLabel::Trait,
    NodeLabel::Impl,
    NodeLabel::Function,
    NodeLabel::Method,
    NodeLabel::Variable,
    NodeLabel::GlobalVar,
    NodeLabel::Const,
    NodeLabel::Static,
    NodeLabel::Macro,
    NodeLabel::TypeAlias,
    NodeLabel::Typedef,
    NodeLabel::Namespace,
];

/// Executes structured (name/type/file) searches against a [`StorageConnection`].
pub struct StructuredSearcher<'a> {
    conn: &'a StorageConnection,
}

impl<'a> StructuredSearcher<'a> {
    /// Creates a new [`StructuredSearcher`] borrowing `conn`.
    #[must_use]
    pub fn new(conn: &'a StorageConnection) -> Self {
        Self { conn }
    }

    /// Searches all symbol tables for nodes whose `name` contains `name`.
    ///
    /// Results are merged across tables and sorted by descending relevance
    /// score (exact > prefix > substring), then by name. The `project` filter
    /// is applied when supplied. Matching is case-insensitive.
    pub fn search_by_name(
        &self,
        name: &str,
        project: Option<&str>,
        limit: usize,
    ) -> Result<Vec<SearchResult>> {
        if name.trim().is_empty() {
            return Err(QueryError::InvalidQuery(
                "search name must not be empty".to_string(),
            ));
        }
        let escaped = escape_cypher_string(name);
        let mut results = Vec::new();
        for &label in SYMBOL_LABELS {
            let table = escape_identifier(label.table_name());
            // Use toLower() for case-insensitive CONTAINS matching.
            let cypher = match project {
                Some(p) => format!(
                    "MATCH (n:{table}) WHERE toLower(n.name) CONTAINS toLower('{escaped}') AND n.project = '{}' RETURN n.name AS name, n.qualifiedName AS qn, n.filePath AS filePath, n.startLine AS line;",
                    escape_cypher_string(p),
                ),
                None => format!(
                    "MATCH (n:{table}) WHERE toLower(n.name) CONTAINS toLower('{escaped}') RETURN n.name AS name, n.qualifiedName AS qn, n.filePath AS filePath, n.startLine AS line;",
                ),
            };
            // Some tables may lack a `qualifiedName` or `filePath` column, or
            // toLower() may be unsupported; skip those gracefully.
            match self.conn.query(&cypher) {
                Ok(rows) => results.extend(rows_to_search_results(rows, label, name)),
                Err(_) => continue,
            }
        }
        sort_and_truncate(&mut results, limit);
        Ok(results)
    }

    /// Returns all nodes of the given `label`, optionally filtered by project.
    pub fn search_by_type(
        &self,
        label: NodeLabel,
        project: Option<&str>,
        limit: usize,
    ) -> Result<Vec<SearchResult>> {
        let table = escape_identifier(label.table_name());
        let cypher = match project {
            Some(p) => format!(
                "MATCH (n:{table}) WHERE n.project = '{}' RETURN n.name AS name, n.qualifiedName AS qn, n.filePath AS filePath, n.startLine AS line;",
                escape_cypher_string(p),
            ),
            None => format!(
                "MATCH (n:{table}) RETURN n.name AS name, n.qualifiedName AS qn, n.filePath AS filePath, n.startLine AS line;",
            ),
        };
        let rows = self.conn.query(&cypher)?;
        let mut results = rows_to_search_results(rows, label, "");
        sort_and_truncate(&mut results, limit);
        Ok(results)
    }

    /// Returns all symbols located in the given `file_path`, optionally
    /// filtered by project.
    pub fn search_by_file(
        &self,
        file_path: &str,
        project: Option<&str>,
    ) -> Result<Vec<SearchResult>> {
        if file_path.trim().is_empty() {
            return Err(QueryError::InvalidQuery(
                "file path must not be empty".to_string(),
            ));
        }
        let escaped = escape_cypher_string(file_path);
        let mut results = Vec::new();
        for &label in SYMBOL_LABELS {
            let table = escape_identifier(label.table_name());
            let cypher = match project {
                Some(p) => format!(
                    "MATCH (n:{table}) WHERE n.filePath = '{escaped}' AND n.project = '{}' RETURN n.name AS name, n.qualifiedName AS qn, n.filePath AS filePath, n.startLine AS line;",
                    escape_cypher_string(p),
                ),
                None => format!(
                    "MATCH (n:{table}) WHERE n.filePath = '{escaped}' RETURN n.name AS name, n.qualifiedName AS qn, n.filePath AS filePath, n.startLine AS line;",
                ),
            };
            match self.conn.query(&cypher) {
                Ok(rows) => results.extend(rows_to_search_results(rows, label, "")),
                Err(_) => continue,
            }
        }
        // Sort by start line for deterministic file-order output.
        results.sort_by(|a, b| {
            a.start_line
                .unwrap_or(0)
                .cmp(&b.start_line.unwrap_or(0))
                .then_with(|| a.name.cmp(&b.name))
        });
        Ok(results)
    }

    /// General search: searches by name (CONTAINS) and returns results sorted
    /// by relevance. Equivalent to [`StructuredSearcher::search_by_name`] but
    /// provided as the default `search` entry point for the facade.
    pub fn search(
        &self,
        text: &str,
        project: Option<&str>,
        limit: usize,
    ) -> Result<Vec<SearchResult>> {
        self.search_by_name(text, project, limit)
    }
}

/// Converts query rows into [`SearchResult`]s, computing a relevance score
/// based on how closely `name` matches each result's name.
fn rows_to_search_results(
    rows: Vec<Vec<serde_json::Value>>,
    label: NodeLabel,
    query: &str,
) -> Vec<SearchResult> {
    let label_str = label.to_string();
    rows.into_iter()
        .filter_map(|row| {
            let name = row.first().and_then(|v| v.as_str())?.to_string();
            let qualified_name = row.get(1).and_then(|v| v.as_str()).map(String::from);
            let file_path = row.get(2).and_then(|v| v.as_str()).map(String::from);
            let start_line = row
                .get(3)
                .and_then(|v| v.as_i64())
                .and_then(|i| u32::try_from(i).ok());
            let (score, reason) = relevance_score_with_reason(&name, query);
            Some(SearchResult {
                name,
                label: label_str.clone(),
                file_path,
                start_line,
                qualified_name,
                score,
                match_reason: reason.to_string(),
                degree: 0,
            })
        })
        .collect()
}

/// Computes a relevance score in `[0.0, 1.0]` for `name` against `query`.
///
/// - Exact match → 1.0
/// - Prefix match → 0.8
/// - Substring match → 0.5
/// - No query (e.g. `search_by_type`) → 1.0 (neutral)
///
/// Computes both the score and a human-readable match reason.
fn relevance_score_with_reason(name: &str, query: &str) -> (f64, &'static str) {
    if query.is_empty() {
        return (1.0, "neutral");
    }
    let name_lower = name.to_ascii_lowercase();
    let query_lower = query.to_ascii_lowercase();
    if name_lower == query_lower {
        (1.0, "exact name match")
    } else if name_lower.starts_with(&query_lower) {
        (0.8, "prefix match")
    } else {
        (0.5, "substring match")
    }
}

/// Computes the standard Levenshtein edit distance between `a` and `b`.
///
/// Uses O(`min(a.len, b.len)`) space (single-row DP with the previous
/// diagonal value tracked separately).
pub fn levenshtein(a: &str, b: &str) -> usize {
    let a_bytes = a.as_bytes();
    let b_bytes = b.as_bytes();
    if a_bytes.is_empty() {
        return b_bytes.len();
    }
    if b_bytes.is_empty() {
        return a_bytes.len();
    }
    // Ensure `b` is the shorter string to minimise row width.
    let (a_bytes, b_bytes) = if a_bytes.len() < b_bytes.len() {
        (b_bytes, a_bytes)
    } else {
        (a_bytes, b_bytes)
    };
    let b_len = b_bytes.len();
    let mut prev_row: Vec<usize> = (0..=b_len).collect();
    let mut curr_row = vec![0usize; b_len + 1];
    for (i, &a_ch) in a_bytes.iter().enumerate() {
        curr_row[0] = i + 1;
        for (j, &b_ch) in b_bytes.iter().enumerate() {
            let cost = usize::from(a_ch != b_ch);
            curr_row[j + 1] = (prev_row[j + 1] + 1)
                .min(curr_row[j] + 1)
                .min(prev_row[j] + cost);
        }
        std::mem::swap(&mut prev_row, &mut curr_row);
    }
    prev_row[b_len]
}

/// Parses a node label name (e.g. `"Function"`, `"function"`) into a
/// [`NodeLabel`]. Returns `None` for unknown labels.
fn parse_node_label(name: &str) -> Option<NodeLabel> {
    name.parse::<NodeLabel>().ok()
}

/// Computes name relevance for multi-signal scoring (R-search-004).
///
/// - Exact case-insensitive match → 1.0
/// - Prefix or substring match → 0.8
/// - No match → 0.0
fn compute_name_relevance(name: &str, query: &str) -> f64 {
    if query.is_empty() {
        return 1.0;
    }
    let name_lower = name.to_ascii_lowercase();
    let query_lower = query.to_ascii_lowercase();
    if name_lower == query_lower {
        1.0
    } else if name_lower.contains(&query_lower) {
        0.8
    } else {
        0.0
    }
}

/// Computes module proximity for multi-signal scoring (R-search-004).
///
/// - `file_pattern` is provided and matches the candidate's `file_path` → 1.0
/// - Otherwise (no pattern or no match) → 0.5
fn compute_module_proximity(file_path: &Option<String>, file_pattern: &Option<String>) -> f64 {
    match (file_path, file_pattern) {
        (Some(path), Some(pattern)) if path.contains(pattern) => 1.0,
        _ => 0.5,
    }
}

/// Sorts results by descending score then ascending name, and truncates to `limit`.
fn sort_and_truncate(results: &mut Vec<SearchResult>, limit: usize) {
    results.sort_by(|a, b| {
        b.score
            .partial_cmp(&a.score)
            .unwrap_or(std::cmp::Ordering::Equal)
            .then_with(|| a.name.cmp(&b.name))
    });
    if limit < results.len() {
        results.truncate(limit);
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::model::{Edge, EdgeType, Language, Node, NodeLabel};
    use crate::storage::Repository;

    fn fresh_repo() -> Repository {
        Repository::in_memory().expect("in_memory repository")
    }

    fn sample_function(
        id: &str,
        project: &str,
        name: &str,
        qn: &str,
        file: &str,
        line: u32,
    ) -> Node {
        Node::builder(NodeLabel::Function, name, qn)
            .id(id)
            .project(project)
            .file_path(file)
            .start_line(line)
            .end_line(line + 10)
            .language(Language::Rust)
            .signature("fn x()")
            .build()
    }

    fn sample_class(id: &str, project: &str, name: &str, qn: &str, file: &str, line: u32) -> Node {
        Node::builder(NodeLabel::Class, name, qn)
            .id(id)
            .project(project)
            .file_path(file)
            .start_line(line)
            .end_line(line + 20)
            .language(Language::Rust)
            .build()
    }

    // --- search_by_name ---

    #[test]
    fn search_by_name_finds_substring_matches() {
        let repo = fresh_repo();
        repo.save_nodes(
            &[
                sample_function("f1", "demo", "parse_file", "demo.parse_file", "/a.rs", 1),
                sample_function("f2", "demo", "read_input", "demo.read_input", "/a.rs", 10),
                sample_function("f3", "demo", "parse_line", "demo.parse_line", "/b.rs", 1),
            ],
            NodeLabel::Function,
        )
        .expect("save_nodes");

        let searcher = StructuredSearcher::new(repo.connection());
        let results = searcher
            .search_by_name("parse", None, 100)
            .expect("search_by_name");
        let names: Vec<&str> = results.iter().map(|r| r.name.as_str()).collect();
        assert!(names.contains(&"parse_file"));
        assert!(names.contains(&"parse_line"));
        assert!(!names.contains(&"read_input"));
    }

    #[test]
    fn search_by_name_ac_search_001_returns_parse_symbols() {
        // AC-SEARCH-001: search "parse" returns symbols containing "parse".
        let repo = fresh_repo();
        repo.save_nodes(
            &[
                sample_function("f1", "demo", "parse", "demo.parse", "/a.rs", 1),
                sample_function("f2", "demo", "parse_token", "demo.parse_token", "/a.rs", 5),
                sample_function("f3", "demo", "tokenize", "demo.tokenize", "/b.rs", 1),
            ],
            NodeLabel::Function,
        )
        .expect("save_nodes");

        let searcher = StructuredSearcher::new(repo.connection());
        let results = searcher.search_by_name("parse", None, 100).expect("search");
        assert!(results.iter().all(|r| r.name.contains("parse")));
        assert!(results.iter().any(|r| r.name == "parse"));
        assert!(results.iter().any(|r| r.name == "parse_token"));
    }

    #[test]
    fn search_by_name_filters_by_project() {
        let repo = fresh_repo();
        repo.save_nodes(
            &[sample_function(
                "f1",
                "alpha",
                "parse",
                "alpha.parse",
                "/a.rs",
                1,
            )],
            NodeLabel::Function,
        )
        .expect("save_nodes alpha");
        repo.save_nodes(
            &[sample_function(
                "f2",
                "beta",
                "parse",
                "beta.parse",
                "/b.rs",
                1,
            )],
            NodeLabel::Function,
        )
        .expect("save_nodes beta");

        let searcher = StructuredSearcher::new(repo.connection());
        let results = searcher
            .search_by_name("parse", Some("alpha"), 100)
            .expect("search");
        assert_eq!(results.len(), 1);
        assert_eq!(results[0].qualified_name.as_deref(), Some("alpha.parse"));
    }

    #[test]
    fn search_by_name_respects_limit() {
        let repo = fresh_repo();
        let mut nodes = Vec::new();
        for i in 0..10 {
            nodes.push(sample_function(
                &format!("f{i}"),
                "demo",
                &format!("parse_{i}"),
                &format!("demo.parse_{i}"),
                "/a.rs",
                i + 1,
            ));
        }
        repo.save_nodes(&nodes, NodeLabel::Function)
            .expect("save_nodes");

        let searcher = StructuredSearcher::new(repo.connection());
        let results = searcher.search_by_name("parse", None, 3).expect("search");
        assert_eq!(results.len(), 3);
    }

    #[test]
    fn search_by_name_rejects_empty_query() {
        let repo = fresh_repo();
        let searcher = StructuredSearcher::new(repo.connection());
        let err = searcher
            .search_by_name("", None, 10)
            .expect_err("empty name should error");
        assert!(err.is_invalid_query());
    }

    #[test]
    fn search_by_name_returns_empty_when_no_match() {
        let repo = fresh_repo();
        repo.save_nodes(
            &[sample_function(
                "f1",
                "demo",
                "main",
                "demo.main",
                "/a.rs",
                1,
            )],
            NodeLabel::Function,
        )
        .expect("save_nodes");
        let searcher = StructuredSearcher::new(repo.connection());
        let results = searcher
            .search_by_name("nonexistent", None, 10)
            .expect("search");
        assert!(results.is_empty());
    }

    #[test]
    fn search_by_name_searches_across_multiple_labels() {
        let repo = fresh_repo();
        repo.save_nodes(
            &[sample_function(
                "f1",
                "demo",
                "parse",
                "demo.parse",
                "/a.rs",
                1,
            )],
            NodeLabel::Function,
        )
        .expect("save_nodes function");
        repo.save_nodes(
            &[sample_class(
                "c1",
                "demo",
                "Parser",
                "demo.Parser",
                "/a.rs",
                20,
            )],
            NodeLabel::Class,
        )
        .expect("save_nodes class");

        let searcher = StructuredSearcher::new(repo.connection());
        let results = searcher.search_by_name("parse", None, 100).expect("search");
        // Case-insensitive substring: "Parser" contains "parse" lowercased.
        let names: Vec<&str> = results.iter().map(|r| r.name.as_str()).collect();
        assert!(names.contains(&"parse"));
        assert!(names.contains(&"Parser"));
    }

    #[test]
    fn search_by_name_assigns_higher_score_to_exact_match() {
        let repo = fresh_repo();
        repo.save_nodes(
            &[
                sample_function("f1", "demo", "parse", "demo.parse", "/a.rs", 1),
                sample_function("f2", "demo", "parse_file", "demo.parse_file", "/a.rs", 5),
            ],
            NodeLabel::Function,
        )
        .expect("save_nodes");

        let searcher = StructuredSearcher::new(repo.connection());
        let results = searcher.search_by_name("parse", None, 100).expect("search");
        // Exact match should be first (highest score).
        assert_eq!(results[0].name, "parse");
        assert!(results[0].score > results[1].score);
    }

    // --- search_by_type ---

    #[test]
    fn search_by_type_returns_all_functions() {
        let repo = fresh_repo();
        repo.save_nodes(
            &[
                sample_function("f1", "demo", "alpha", "demo.alpha", "/a.rs", 1),
                sample_function("f2", "demo", "beta", "demo.beta", "/a.rs", 5),
            ],
            NodeLabel::Function,
        )
        .expect("save_nodes");
        repo.save_nodes(
            &[sample_class(
                "c1",
                "demo",
                "Gamma",
                "demo.Gamma",
                "/a.rs",
                10,
            )],
            NodeLabel::Class,
        )
        .expect("save_nodes class");

        let searcher = StructuredSearcher::new(repo.connection());
        let results = searcher
            .search_by_type(NodeLabel::Function, None, 100)
            .expect("search_by_type");
        assert_eq!(results.len(), 2);
        assert!(results.iter().all(|r| r.label == "Function"));
    }

    #[test]
    fn search_by_type_returns_all_classes() {
        let repo = fresh_repo();
        repo.save_nodes(
            &[
                sample_class("c1", "demo", "Alpha", "demo.Alpha", "/a.rs", 1),
                sample_class("c2", "demo", "Beta", "demo.Beta", "/a.rs", 10),
            ],
            NodeLabel::Class,
        )
        .expect("save_nodes");

        let searcher = StructuredSearcher::new(repo.connection());
        let results = searcher
            .search_by_type(NodeLabel::Class, None, 100)
            .expect("search_by_type");
        assert_eq!(results.len(), 2);
        assert!(results.iter().all(|r| r.label == "Class"));
    }

    #[test]
    fn search_by_type_filters_by_project() {
        let repo = fresh_repo();
        repo.save_nodes(
            &[sample_function(
                "f1",
                "alpha",
                "main",
                "alpha.main",
                "/a.rs",
                1,
            )],
            NodeLabel::Function,
        )
        .expect("save_nodes alpha");
        repo.save_nodes(
            &[
                sample_function("f2", "beta", "main", "beta.main", "/a.rs", 1),
                sample_function("f3", "beta", "util", "beta.util", "/a.rs", 5),
            ],
            NodeLabel::Function,
        )
        .expect("save_nodes beta");

        let searcher = StructuredSearcher::new(repo.connection());
        let results = searcher
            .search_by_type(NodeLabel::Function, Some("beta"), 100)
            .expect("search_by_type");
        assert_eq!(results.len(), 2);
        assert!(results
            .iter()
            .all(|r| r.qualified_name.as_ref().unwrap().starts_with("beta.")));
    }

    #[test]
    fn search_by_type_respects_limit() {
        let repo = fresh_repo();
        let mut nodes = Vec::new();
        for i in 0..10 {
            nodes.push(sample_function(
                &format!("f{i}"),
                "demo",
                &format!("func_{i}"),
                &format!("demo.func_{i}"),
                "/a.rs",
                i + 1,
            ));
        }
        repo.save_nodes(&nodes, NodeLabel::Function)
            .expect("save_nodes");

        let searcher = StructuredSearcher::new(repo.connection());
        let results = searcher
            .search_by_type(NodeLabel::Function, None, 3)
            .expect("search_by_type");
        assert_eq!(results.len(), 3);
    }

    #[test]
    fn search_by_type_returns_empty_when_none() {
        let repo = fresh_repo();
        let searcher = StructuredSearcher::new(repo.connection());
        let results = searcher
            .search_by_type(NodeLabel::Function, None, 100)
            .expect("search_by_type");
        assert!(results.is_empty());
    }

    // --- search_by_file ---

    #[test]
    fn search_by_file_returns_all_symbols_in_file() {
        let repo = fresh_repo();
        repo.save_nodes(
            &[
                sample_function("f1", "demo", "alpha", "demo.alpha", "/src/main.rs", 1),
                sample_function("f2", "demo", "beta", "demo.beta", "/src/main.rs", 10),
            ],
            NodeLabel::Function,
        )
        .expect("save_nodes");
        repo.save_nodes(
            &[sample_class(
                "c1",
                "demo",
                "Gamma",
                "demo.Gamma",
                "/src/main.rs",
                20,
            )],
            NodeLabel::Class,
        )
        .expect("save_nodes class");
        // A symbol in a different file should not appear.
        repo.save_nodes(
            &[sample_function(
                "f3",
                "demo",
                "delta",
                "demo.delta",
                "/src/lib.rs",
                1,
            )],
            NodeLabel::Function,
        )
        .expect("save_nodes other file");

        let searcher = StructuredSearcher::new(repo.connection());
        let results = searcher
            .search_by_file("/src/main.rs", None)
            .expect("search_by_file");
        assert_eq!(results.len(), 3);
        assert!(results
            .iter()
            .all(|r| r.file_path.as_deref() == Some("/src/main.rs")));
    }

    #[test]
    fn search_by_file_filters_by_project() {
        let repo = fresh_repo();
        repo.save_nodes(
            &[sample_function(
                "f1",
                "alpha",
                "main",
                "alpha.main",
                "/src/main.rs",
                1,
            )],
            NodeLabel::Function,
        )
        .expect("save_nodes alpha");
        repo.save_nodes(
            &[sample_function(
                "f2",
                "beta",
                "main",
                "beta.main",
                "/src/main.rs",
                1,
            )],
            NodeLabel::Function,
        )
        .expect("save_nodes beta");

        let searcher = StructuredSearcher::new(repo.connection());
        let results = searcher
            .search_by_file("/src/main.rs", Some("alpha"))
            .expect("search_by_file");
        assert_eq!(results.len(), 1);
        assert_eq!(results[0].qualified_name.as_deref(), Some("alpha.main"));
    }

    #[test]
    fn search_by_file_rejects_empty_path() {
        let repo = fresh_repo();
        let searcher = StructuredSearcher::new(repo.connection());
        let err = searcher
            .search_by_file("", None)
            .expect_err("empty path should error");
        assert!(err.is_invalid_query());
    }

    #[test]
    fn search_by_file_returns_empty_when_no_match() {
        let repo = fresh_repo();
        repo.save_nodes(
            &[sample_function(
                "f1",
                "demo",
                "main",
                "demo.main",
                "/a.rs",
                1,
            )],
            NodeLabel::Function,
        )
        .expect("save_nodes");
        let searcher = StructuredSearcher::new(repo.connection());
        let results = searcher
            .search_by_file("/nonexistent.rs", None)
            .expect("search_by_file");
        assert!(results.is_empty());
    }

    // --- search (general) ---

    #[test]
    fn search_delegates_to_search_by_name() {
        let repo = fresh_repo();
        repo.save_nodes(
            &[sample_function(
                "f1",
                "demo",
                "parse_file",
                "demo.parse_file",
                "/a.rs",
                1,
            )],
            NodeLabel::Function,
        )
        .expect("save_nodes");

        let searcher = StructuredSearcher::new(repo.connection());
        let results = searcher.search("parse", None, 100).expect("search");
        assert_eq!(results.len(), 1);
        assert_eq!(results[0].name, "parse_file");
    }

    // --- helpers ---

    #[test]
    fn relevance_score_exact_match_is_highest() {
        assert_eq!(relevance_score_with_reason("parse", "parse").0, 1.0);
        assert_eq!(relevance_score_with_reason("PARSE", "parse").0, 1.0);
    }

    #[test]
    fn relevance_score_prefix_match_is_high() {
        assert_eq!(relevance_score_with_reason("parse_file", "parse").0, 0.8);
    }

    #[test]
    fn relevance_score_substring_match_is_low() {
        assert_eq!(relevance_score_with_reason("my_parse_func", "parse").0, 0.5);
    }

    #[test]
    fn relevance_score_empty_query_is_neutral() {
        assert_eq!(relevance_score_with_reason("anything", "").0, 1.0);
    }

    // --- error continuation coverage ---

    #[test]
    fn search_by_name_continues_on_query_error() {
        // Cover the `Err(_) => continue` arm (line 86) of search_by_name:
        // when a per-table MATCH query fails (table dropped), the loop
        // skips it and continues to the next table.
        let repo = fresh_repo();
        repo.save_nodes(
            &[sample_function(
                "f1",
                "demo",
                "parse",
                "demo.parse",
                "/a.rs",
                1,
            )],
            NodeLabel::Function,
        )
        .expect("save_nodes");
        // Drop the Class table so its MATCH query errors.
        repo.connection()
            .execute("DROP TABLE Class;")
            .expect("drop table");
        // Verify the table is actually gone.
        let check = repo
            .connection()
            .query("MATCH (n:Class) RETURN n.name AS name;");
        assert!(check.is_err(), "Class table should be gone after DROP");
        let searcher = StructuredSearcher::new(repo.connection());
        // Should still return results from the Function table.
        let results = searcher.search_by_name("parse", None, 100).expect("search");
        assert!(results.iter().any(|r| r.name == "parse"));
    }

    #[test]
    fn search_by_file_continues_on_query_error() {
        // Cover the `Err(_) => continue` arm (line 143) of search_by_file.
        let repo = fresh_repo();
        repo.save_nodes(
            &[sample_function(
                "f1",
                "demo",
                "parse",
                "demo.parse",
                "/src/main.rs",
                1,
            )],
            NodeLabel::Function,
        )
        .expect("save_nodes");
        repo.connection()
            .execute("DROP TABLE Class;")
            .expect("drop table");
        let searcher = StructuredSearcher::new(repo.connection());
        let results = searcher
            .search_by_file("/src/main.rs", None)
            .expect("search_by_file");
        assert!(results.iter().any(|r| r.name == "parse"));
    }

    // --- T019: multi-mode search types ---

    use crate::kit::StorageModule;
    use crate::storage::StorageConfig;

    fn build_storage() -> std::sync::Arc<dyn Storage> {
        StorageModule::build_cap(&StorageConfig::in_memory()).expect("StorageModule::build_cap")
    }

    #[test]
    fn search_mode_serializes_to_descriptive_strings() {
        let json = serde_json::to_string(&SearchMode::Exact).expect("serialize Exact");
        assert_eq!(json, "\"Exact\"");
        let json = serde_json::to_string(&SearchMode::Regex).expect("serialize Regex");
        assert_eq!(json, "\"Regex\"");
        let json = serde_json::to_string(&SearchMode::Fuzzy).expect("serialize Fuzzy");
        assert_eq!(json, "\"Fuzzy\"");
        let json =
            serde_json::to_string(&SearchMode::GraphEnhanced).expect("serialize GraphEnhanced");
        assert_eq!(json, "\"GraphEnhanced\"");
        let json = serde_json::to_string(&SearchMode::MultiSignal).expect("serialize MultiSignal");
        assert_eq!(json, "\"MultiSignal\"");
    }

    #[test]
    fn search_mode_round_trips_through_json() {
        for mode in [
            SearchMode::Exact,
            SearchMode::Regex,
            SearchMode::Fuzzy,
            SearchMode::GraphEnhanced,
            SearchMode::MultiSignal,
        ] {
            let json = serde_json::to_string(&mode).expect("serialize");
            let back: SearchMode = serde_json::from_str(&json).expect("deserialize");
            assert_eq!(mode, back);
        }
    }

    #[test]
    fn search_params_default_has_limit_50_and_exact_mode() {
        let p = SearchParams::default();
        assert_eq!(p.limit, 50);
        assert_eq!(p.mode, SearchMode::Exact);
        assert!(p.label_filter.is_none());
        assert!(p.degree_filter.is_none());
        assert!(p.file_pattern.is_none());
    }

    #[test]
    fn search_params_clamps_limit_to_max_500() {
        let mut p = SearchParams {
            limit: 1000,
            ..Default::default()
        };
        assert_eq!(p.clamped_limit(), MAX_LIMIT);
        p.limit = 10;
        assert_eq!(p.clamped_limit(), 10);
    }

    #[test]
    fn search_params_round_trips_through_json() {
        let p = SearchParams {
            query: "handler".to_string(),
            mode: SearchMode::GraphEnhanced,
            label_filter: Some(vec!["Function".to_string()]),
            degree_filter: Some((5, 100)),
            file_pattern: Some("/src/**/*.rs".to_string()),
            limit: 25,
        };
        let json = serde_json::to_string(&p).expect("serialize");
        let back: SearchParams = serde_json::from_str(&json).expect("deserialize");
        assert_eq!(p, back);
    }

    #[test]
    fn search_engine_dispatches_to_exact_mode() {
        let storage = build_storage();
        let func = Node::builder(NodeLabel::Function, "parse_file", "demo.parse_file")
            .id("f1")
            .project("demo")
            .file_path("/a.rs")
            .start_line(1)
            .end_line(10)
            .language(Language::Rust)
            .build();
        storage
            .save_nodes(&[func], NodeLabel::Function)
            .expect("save_nodes");

        let engine = SearchEngine::new(storage.as_ref());
        let params = SearchParams {
            query: "parse".to_string(),
            mode: SearchMode::Exact,
            limit: 50,
            ..SearchParams::default()
        };
        let results = engine.search("demo", &params).expect("exact search");
        assert!(results.iter().any(|r| r.name == "parse_file"));
        assert!(results.iter().all(|r| r.match_reason.contains("match")));
    }

    #[test]
    fn search_engine_exact_rejects_empty_query() {
        let storage = build_storage();
        let engine = SearchEngine::new(storage.as_ref());
        let params = SearchParams {
            query: String::new(),
            mode: SearchMode::Exact,
            ..SearchParams::default()
        };
        let err = engine
            .search("demo", &params)
            .expect_err("empty query should error");
        assert!(err.is_invalid_query());
    }

    #[test]
    fn search_engine_regex_matches_pattern_on_name() {
        let storage = build_storage();
        let funcs = [
            Node::builder(NodeLabel::Function, "get_user_by_id", "demo.get_user_by_id")
                .id("f1")
                .project("demo")
                .file_path("/a.rs")
                .start_line(1)
                .end_line(10)
                .language(Language::Rust)
                .build(),
            Node::builder(NodeLabel::Function, "get_first_user", "demo.get_first_user")
                .id("f2")
                .project("demo")
                .file_path("/a.rs")
                .start_line(20)
                .end_line(30)
                .language(Language::Rust)
                .build(),
            Node::builder(NodeLabel::Function, "delete_user", "demo.delete_user")
                .id("f3")
                .project("demo")
                .file_path("/a.rs")
                .start_line(40)
                .end_line(50)
                .language(Language::Rust)
                .build(),
        ];
        storage
            .save_nodes(&funcs, NodeLabel::Function)
            .expect("save_nodes");

        let engine = SearchEngine::new(storage.as_ref());
        // Pattern `get_.*_user` matches "get_first_user" but not "get_user_by_id"
        // (no "_user" substring after "get_"). Use `get_.*` to match both.
        let params = SearchParams {
            query: r"get_.*_user".to_string(),
            mode: SearchMode::Regex,
            ..SearchParams::default()
        };
        let results = engine.search("demo", &params).expect("regex search");
        let names: Vec<&str> = results.iter().map(|r| r.name.as_str()).collect();
        assert!(
            names.contains(&"get_first_user"),
            "should match get_first_user, got {names:?}"
        );
        assert!(!names.contains(&"delete_user"));
        assert!(results
            .iter()
            .all(|r| r.match_reason.starts_with("regex match")));

        // Broader pattern matches both get_* functions.
        let params2 = SearchParams {
            query: r"get_.*".to_string(),
            mode: SearchMode::Regex,
            ..SearchParams::default()
        };
        let results2 = engine.search("demo", &params2).expect("regex search");
        let names2: Vec<&str> = results2.iter().map(|r| r.name.as_str()).collect();
        assert!(names2.contains(&"get_user_by_id"));
        assert!(names2.contains(&"get_first_user"));
        assert!(!names2.contains(&"delete_user"));
    }

    #[test]
    fn search_engine_regex_matches_suffix_pattern() {
        let storage = build_storage();
        let classes = [
            Node::builder(NodeLabel::Class, "RequestHandler", "demo.RequestHandler")
                .id("c1")
                .project("demo")
                .file_path("/a.rs")
                .start_line(1)
                .end_line(50)
                .language(Language::Rust)
                .build(),
            Node::builder(NodeLabel::Class, "BaseController", "demo.BaseController")
                .id("c2")
                .project("demo")
                .file_path("/a.rs")
                .start_line(60)
                .end_line(100)
                .language(Language::Rust)
                .build(),
        ];
        storage
            .save_nodes(&classes, NodeLabel::Class)
            .expect("save_nodes");

        let engine = SearchEngine::new(storage.as_ref());
        let params = SearchParams {
            query: r"Handler$".to_string(),
            mode: SearchMode::Regex,
            ..SearchParams::default()
        };
        let results = engine.search("demo", &params).expect("regex search");
        assert_eq!(results.len(), 1);
        assert_eq!(results[0].name, "RequestHandler");
    }

    #[test]
    fn search_engine_regex_matches_on_qualified_name() {
        let storage = build_storage();
        let func = Node::builder(NodeLabel::Function, "do_work", "demo.handler.process")
            .id("f1")
            .project("demo")
            .file_path("/a.rs")
            .start_line(1)
            .end_line(10)
            .language(Language::Rust)
            .build();
        storage
            .save_nodes(&[func], NodeLabel::Function)
            .expect("save_nodes");

        let engine = SearchEngine::new(storage.as_ref());
        let params = SearchParams {
            query: r"^demo\.handler".to_string(),
            mode: SearchMode::Regex,
            ..SearchParams::default()
        };
        let results = engine.search("demo", &params).expect("regex search");
        assert_eq!(results.len(), 1);
        assert_eq!(results[0].name, "do_work");
        assert!(results[0].match_reason.contains("qualifiedName"));
    }

    #[test]
    fn search_engine_regex_rejects_invalid_pattern() {
        let storage = build_storage();
        let engine = SearchEngine::new(storage.as_ref());
        let params = SearchParams {
            query: r"[invalid".to_string(),
            mode: SearchMode::Regex,
            ..SearchParams::default()
        };
        let err = engine
            .search("demo", &params)
            .expect_err("invalid regex should error");
        assert!(err.is_invalid_query());
    }

    #[test]
    fn search_engine_regex_returns_empty_when_no_match() {
        let storage = build_storage();
        let func = Node::builder(NodeLabel::Function, "main", "demo.main")
            .id("f1")
            .project("demo")
            .file_path("/a.rs")
            .start_line(1)
            .end_line(10)
            .language(Language::Rust)
            .build();
        storage
            .save_nodes(&[func], NodeLabel::Function)
            .expect("save_nodes");

        let engine = SearchEngine::new(storage.as_ref());
        let params = SearchParams {
            query: r"nonexistent_\d+".to_string(),
            mode: SearchMode::Regex,
            ..SearchParams::default()
        };
        let results = engine.search("demo", &params).expect("regex search");
        assert!(results.is_empty());
    }

    #[test]
    fn search_engine_fuzzy_matches_within_distance() {
        let storage = build_storage();
        let funcs = [
            Node::builder(NodeLabel::Function, "get_user", "demo.get_user")
                .id("f1")
                .project("demo")
                .file_path("/a.rs")
                .start_line(1)
                .end_line(10)
                .language(Language::Rust)
                .build(),
            Node::builder(NodeLabel::Function, "getUser", "demo.getUser")
                .id("f2")
                .project("demo")
                .file_path("/a.rs")
                .start_line(20)
                .end_line(30)
                .language(Language::Rust)
                .build(),
            Node::builder(
                NodeLabel::Function,
                "completely_unrelated",
                "demo.completely_unrelated",
            )
            .id("f3")
            .project("demo")
            .file_path("/a.rs")
            .start_line(40)
            .end_line(50)
            .language(Language::Rust)
            .build(),
        ];
        storage
            .save_nodes(&funcs, NodeLabel::Function)
            .expect("save_nodes");

        let engine = SearchEngine::new(storage.as_ref());
        // "getuser" vs "get_user" → distance 1 (insert "_")
        // "getuser" vs "getUser"  → distance 0 (case-insensitive equal)
        let results = engine
            .search_fuzzy("demo", "getuser", 2)
            .expect("fuzzy search");
        let names: Vec<&str> = results.iter().map(|r| r.name.as_str()).collect();
        assert!(
            names.contains(&"get_user"),
            "should match get_user, got {names:?}"
        );
        assert!(
            names.contains(&"getUser"),
            "should match getUser, got {names:?}"
        );
        assert!(!names.contains(&"completely_unrelated"));
        assert!(results.iter().all(|r| r.match_reason.starts_with("fuzzy")));
    }

    #[test]
    fn search_engine_fuzzy_max_distance_zero_is_exact() {
        let storage = build_storage();
        let funcs = [
            Node::builder(NodeLabel::Function, "fetch", "demo.fetch")
                .id("f1")
                .project("demo")
                .file_path("/a.rs")
                .start_line(1)
                .end_line(10)
                .language(Language::Rust)
                .build(),
            Node::builder(NodeLabel::Function, "fetcher", "demo.fetcher")
                .id("f2")
                .project("demo")
                .file_path("/a.rs")
                .start_line(20)
                .end_line(30)
                .language(Language::Rust)
                .build(),
        ];
        storage
            .save_nodes(&funcs, NodeLabel::Function)
            .expect("save_nodes");

        let engine = SearchEngine::new(storage.as_ref());
        // max_distance=0 → exact case-insensitive match only
        let results = engine
            .search_fuzzy("demo", "FETCH", 0)
            .expect("fuzzy search");
        assert_eq!(results.len(), 1);
        assert_eq!(results[0].name, "fetch");
        assert_eq!(results[0].match_reason, "fuzzy d=0");
    }

    #[test]
    fn search_engine_fuzzy_excludes_beyond_max_distance() {
        let storage = build_storage();
        // "fethc" vs "fetch" → standard Levenshtein distance = 2
        let func = Node::builder(NodeLabel::Function, "fetch", "demo.fetch")
            .id("f1")
            .project("demo")
            .file_path("/a.rs")
            .start_line(1)
            .end_line(10)
            .language(Language::Rust)
            .build();
        storage
            .save_nodes(&[func], NodeLabel::Function)
            .expect("save_nodes");

        let engine = SearchEngine::new(storage.as_ref());
        // distance = 2, so max_distance=1 should NOT match
        let results = engine
            .search_fuzzy("demo", "fethc", 1)
            .expect("fuzzy search");
        assert!(
            results.is_empty(),
            "distance 2 should not match max_distance 1"
        );

        // max_distance=2 should match
        let results = engine
            .search_fuzzy("demo", "fethc", 2)
            .expect("fuzzy search");
        assert_eq!(results.len(), 1);
        assert_eq!(results[0].name, "fetch");
        assert_eq!(results[0].match_reason, "fuzzy d=2");
    }

    #[test]
    fn search_engine_fuzzy_rejects_empty_query() {
        let storage = build_storage();
        let engine = SearchEngine::new(storage.as_ref());
        let err = engine
            .search_fuzzy("demo", "", 2)
            .expect_err("empty query should error");
        assert!(err.is_invalid_query());
    }

    #[test]
    fn search_engine_fuzzy_rejects_excessive_distance() {
        let storage = build_storage();
        let engine = SearchEngine::new(storage.as_ref());
        let err = engine
            .search_fuzzy("demo", "test", MAX_FUZZY_DISTANCE + 1)
            .expect_err("excessive distance should error");
        assert!(err.is_invalid_query());
    }

    #[test]
    fn levenshtein_computes_known_distances() {
        assert_eq!(levenshtein("", ""), 0);
        assert_eq!(levenshtein("abc", ""), 3);
        assert_eq!(levenshtein("", "abc"), 3);
        assert_eq!(levenshtein("abc", "abc"), 0);
        assert_eq!(levenshtein("kitten", "sitting"), 3);
        assert_eq!(levenshtein("getuser", "get_user"), 1);
        assert_eq!(levenshtein("getuser", "getuser"), 0);
        assert_eq!(levenshtein("fethc", "fetch"), 2);
    }

    #[test]
    fn search_engine_graph_enhanced_filters_by_degree() {
        let storage = build_storage();
        // Create two handler functions: one with 5 incoming CALLS, one with 2.
        let handlers = [
            Node::builder(
                NodeLabel::Function,
                "request_handler",
                "demo.request_handler",
            )
            .id("h1")
            .project("demo")
            .file_path("/a.rs")
            .start_line(1)
            .end_line(10)
            .language(Language::Rust)
            .build(),
            Node::builder(
                NodeLabel::Function,
                "response_handler",
                "demo.response_handler",
            )
            .id("h2")
            .project("demo")
            .file_path("/a.rs")
            .start_line(20)
            .end_line(30)
            .language(Language::Rust)
            .build(),
        ];
        storage
            .save_nodes(&handlers, NodeLabel::Function)
            .expect("save_nodes");

        // Create caller functions to generate CALLS edges.
        let callers: Vec<Node> = (0..7)
            .map(|i| {
                Node::builder(
                    NodeLabel::Function,
                    format!("caller_{i}"),
                    format!("demo.caller_{i}"),
                )
                .id(format!("c{i}"))
                .project("demo")
                .file_path("/b.rs")
                .start_line(i + 1)
                .end_line(i + 5)
                .language(Language::Rust)
                .build()
            })
            .collect();
        storage
            .save_nodes(&callers, NodeLabel::Function)
            .expect("save callers");

        // 5 callers → h1, 2 callers → h2.
        let edges_to_h1: Vec<Edge> = (0..5)
            .map(|i| Edge::new(format!("c{i}"), "h1", EdgeType::Calls, "demo"))
            .collect();
        let edges_to_h2: Vec<Edge> = (5..7)
            .map(|i| Edge::new(format!("c{i}"), "h2", EdgeType::Calls, "demo"))
            .collect();
        let mut all_edges = edges_to_h1;
        all_edges.extend(edges_to_h2);
        storage.save_edges(&all_edges).expect("save edges");

        let engine = SearchEngine::new(storage.as_ref());
        // degree_filter=(5,100) should only return h1 (degree=5).
        let params = SearchParams {
            query: "handler".to_string(),
            mode: SearchMode::GraphEnhanced,
            degree_filter: Some((5, 100)),
            ..SearchParams::default()
        };
        let results = engine.search("demo", &params).expect("graph search");
        assert_eq!(results.len(), 1, "only h1 should have degree >= 5");
        assert_eq!(results[0].name, "request_handler");
        assert!(results[0].match_reason.contains("degree=5"));

        // degree_filter=(0,3) should only return h2 (degree=2).
        let params2 = SearchParams {
            query: "handler".to_string(),
            mode: SearchMode::GraphEnhanced,
            degree_filter: Some((0, 3)),
            ..SearchParams::default()
        };
        let results2 = engine.search("demo", &params2).expect("graph search");
        assert_eq!(results2.len(), 1, "only h2 should have degree <= 3");
        assert_eq!(results2[0].name, "response_handler");
        assert!(results2[0].match_reason.contains("degree=2"));
    }

    #[test]
    fn search_engine_graph_enhanced_filters_by_label() {
        let storage = build_storage();
        let func = Node::builder(NodeLabel::Function, "data_handler", "demo.data_handler")
            .id("f1")
            .project("demo")
            .file_path("/a.rs")
            .start_line(1)
            .end_line(10)
            .language(Language::Rust)
            .build();
        let class = Node::builder(NodeLabel::Class, "EventHandler", "demo.EventHandler")
            .id("c1")
            .project("demo")
            .file_path("/a.rs")
            .start_line(20)
            .end_line(50)
            .language(Language::Rust)
            .build();
        storage
            .save_nodes(&[func], NodeLabel::Function)
            .expect("save func");
        storage
            .save_nodes(&[class], NodeLabel::Class)
            .expect("save class");

        let engine = SearchEngine::new(storage.as_ref());
        // label_filter=["Function"] → only Function nodes.
        let params = SearchParams {
            query: "handler".to_string(),
            mode: SearchMode::GraphEnhanced,
            label_filter: Some(vec!["Function".to_string()]),
            ..SearchParams::default()
        };
        let results = engine.search("demo", &params).expect("graph search");
        assert_eq!(results.len(), 1);
        assert_eq!(results[0].name, "data_handler");
        assert_eq!(results[0].label, "Function");

        // No label filter → both Function and Class match.
        let params2 = SearchParams {
            query: "handler".to_string(),
            mode: SearchMode::GraphEnhanced,
            ..SearchParams::default()
        };
        let results2 = engine.search("demo", &params2).expect("graph search");
        assert_eq!(results2.len(), 2);
        let labels: Vec<&str> = results2.iter().map(|r| r.label.as_str()).collect();
        assert!(labels.contains(&"Function"));
        assert!(labels.contains(&"Class"));
    }

    #[test]
    fn search_engine_graph_enhanced_includes_score() {
        let storage = build_storage();
        let func = Node::builder(NodeLabel::Function, "handler", "demo.handler")
            .id("f1")
            .project("demo")
            .file_path("/a.rs")
            .start_line(1)
            .end_line(10)
            .language(Language::Rust)
            .build();
        storage
            .save_nodes(&[func], NodeLabel::Function)
            .expect("save_nodes");

        let engine = SearchEngine::new(storage.as_ref());
        let params = SearchParams {
            query: "handler".to_string(),
            mode: SearchMode::GraphEnhanced,
            ..SearchParams::default()
        };
        let results = engine.search("demo", &params).expect("graph search");
        assert_eq!(results.len(), 1);
        // Exact name match → score 1.0.
        assert!((results[0].score - 1.0).abs() < 1e-9);
        assert!(results[0].match_reason.contains("degree=0"));
    }

    #[test]
    fn search_engine_graph_enhanced_rejects_empty_query() {
        let storage = build_storage();
        let engine = SearchEngine::new(storage.as_ref());
        let params = SearchParams {
            query: String::new(),
            mode: SearchMode::GraphEnhanced,
            ..SearchParams::default()
        };
        let err = engine
            .search("demo", &params)
            .expect_err("empty query should error");
        assert!(err.is_invalid_query());
    }

    #[test]
    fn search_engine_multi_signal_exact_high_degree_same_module_with_tests() {
        // R-search-004: exact match + high degree + same module + has tests
        // → score approaches 1.0.
        let storage = build_storage();
        let handler = Node::builder(NodeLabel::Function, "handler", "demo.handler")
            .id("h1")
            .project("demo")
            .file_path("/src/handlers.rs")
            .start_line(1)
            .end_line(10)
            .language(Language::Rust)
            .build();
        storage
            .save_nodes(&[handler], NodeLabel::Function)
            .expect("save_nodes");

        // 100 CALLS edges → degree_centrality = min(100/100, 1.0) = 1.0
        let calls_edges: Vec<Edge> = (0..100)
            .map(|i| Edge::new(format!("caller_{i}"), "h1", EdgeType::Calls, "demo"))
            .collect();
        storage.save_edges(&calls_edges).expect("save calls edges");

        // 1 TESTS edge → test_coverage = 1.0
        let tests_edge = Edge::new("test_fn", "h1", EdgeType::Tests, "demo");
        storage.save_edges(&[tests_edge]).expect("save tests edge");

        let engine = SearchEngine::new(storage.as_ref());
        let params = SearchParams {
            query: "handler".to_string(),
            mode: SearchMode::MultiSignal,
            file_pattern: Some("/src/handlers.rs".to_string()),
            ..SearchParams::default()
        };
        let results = engine.search("demo", &params).expect("multi-signal search");
        assert_eq!(results.len(), 1);
        let score = results[0].score;
        // name_relevance(1.0)*0.4 + degree_centrality(1.0)*0.3
        // + module_proximity(1.0)*0.2 + test_coverage(1.0)*0.1 = 1.0
        assert!(
            (score - 1.0).abs() < 1e-9,
            "exact+high_degree+same_module+tests should be 1.0, got {score}"
        );
        assert_eq!(results[0].match_reason, "multi-signal");
    }

    #[test]
    fn search_engine_multi_signal_substring_low_degree_different_module_no_tests() {
        // R-search-004: fuzzy match + low degree + different module + no tests
        // → score < 0.5.
        let storage = build_storage();
        let func = Node::builder(
            NodeLabel::Function,
            "request_handler",
            "demo.request_handler",
        )
        .id("h2")
        .project("demo")
        .file_path("/src/main.rs")
        .start_line(1)
        .end_line(10)
        .language(Language::Rust)
        .build();
        storage
            .save_nodes(&[func], NodeLabel::Function)
            .expect("save_nodes");

        let engine = SearchEngine::new(storage.as_ref());
        let params = SearchParams {
            query: "handler".to_string(),
            mode: SearchMode::MultiSignal,
            file_pattern: Some("/src/handlers.rs".to_string()),
            ..SearchParams::default()
        };
        let results = engine.search("demo", &params).expect("multi-signal search");
        assert_eq!(results.len(), 1);
        let score = results[0].score;
        // name_relevance(0.8)*0.4 + degree_centrality(0.0)*0.3
        // + module_proximity(0.5)*0.2 + test_coverage(0.0)*0.1
        // = 0.32 + 0.0 + 0.10 + 0.0 = 0.42
        assert!(
            score < 0.5,
            "substring+low_degree+different_module+no_tests should be < 0.5, got {score}"
        );
        assert!(score >= 0.0, "score must be >= 0.0, got {score}");
    }

    #[test]
    fn search_engine_multi_signal_score_always_in_unit_range() {
        let storage = build_storage();
        let func = Node::builder(NodeLabel::Function, "handler", "demo.handler")
            .id("h1")
            .project("demo")
            .file_path("/a.rs")
            .start_line(1)
            .end_line(10)
            .language(Language::Rust)
            .build();
        storage
            .save_nodes(&[func], NodeLabel::Function)
            .expect("save_nodes");

        let engine = SearchEngine::new(storage.as_ref());
        let params = SearchParams {
            query: "handler".to_string(),
            mode: SearchMode::MultiSignal,
            ..SearchParams::default()
        };
        let results = engine.search("demo", &params).expect("multi-signal search");
        for r in &results {
            assert!(
                (0.0..=1.0).contains(&r.score),
                "score {} out of [0.0, 1.0]",
                r.score
            );
        }
    }

    // ===== A-04 batch: load_tested_node_ids + load_qn_to_node_id_map =====

    #[test]
    fn load_tested_node_ids_returns_all_targets_for_project() {
        let storage = build_storage();
        let targets = ["h1", "h2", "h3"];
        let edges: Vec<Edge> = targets
            .iter()
            .map(|t| Edge::new("test_fn", *t, EdgeType::Tests, "demo"))
            .collect();
        storage.save_edges(&edges).expect("save_edges");

        let engine = SearchEngine::new(storage.as_ref());
        let set = engine
            .load_tested_node_ids("demo")
            .expect("load_tested_node_ids");
        assert_eq!(set.len(), 3, "expected 3 tested targets, got {set:?}");
        for t in &targets {
            assert!(set.contains(*t), "expected {t} in tested set");
        }
    }

    #[test]
    fn load_tested_node_ids_filters_by_project() {
        let storage = build_storage();
        let edges = [
            Edge::new("t1", "h1", EdgeType::Tests, "demo"),
            Edge::new("t2", "h2", EdgeType::Tests, "other"),
        ];
        storage.save_edges(&edges).expect("save_edges");

        let engine = SearchEngine::new(storage.as_ref());
        let set = engine
            .load_tested_node_ids("demo")
            .expect("load_tested_node_ids");
        assert_eq!(set.len(), 1);
        assert!(set.contains("h1"));
        assert!(!set.contains("h2"));
    }

    #[test]
    fn load_tested_node_ids_returns_empty_when_no_tests_edges() {
        let storage = build_storage();
        let edges = [
            Edge::new("c1", "h1", EdgeType::Calls, "demo"),
            Edge::new("u1", "h2", EdgeType::Usage, "demo"),
        ];
        storage.save_edges(&edges).expect("save_edges");

        let engine = SearchEngine::new(storage.as_ref());
        let set = engine
            .load_tested_node_ids("demo")
            .expect("load_tested_node_ids");
        assert!(
            set.is_empty(),
            "expected empty set when no TESTS edges, got {set:?}"
        );
    }

    #[test]
    fn load_qn_to_node_id_map_returns_mapping_for_known_qns() {
        let storage = build_storage();
        let f1 = sample_function("id_f1", "demo", "foo", "demo.foo", "/a.rs", 1);
        let f2 = sample_function("id_f2", "demo", "bar", "demo.bar", "/a.rs", 10);
        storage
            .save_nodes(&[f1, f2], NodeLabel::Function)
            .expect("save_nodes");

        let engine = SearchEngine::new(storage.as_ref());
        let map = engine.load_qn_to_node_id_map("demo", &["demo.foo", "demo.bar", "demo.missing"]);
        assert_eq!(map.get("demo.foo").map(String::as_str), Some("id_f1"));
        assert_eq!(map.get("demo.bar").map(String::as_str), Some("id_f2"));
        assert!(!map.contains_key("demo.missing"));
    }

    #[test]
    fn load_qn_to_node_id_map_returns_empty_for_empty_input() {
        let storage = build_storage();
        let engine = SearchEngine::new(storage.as_ref());
        let map = engine.load_qn_to_node_id_map("demo", &[]);
        assert!(map.is_empty());
    }

    #[test]
    fn load_qn_to_node_id_map_filters_by_project() {
        let storage = build_storage();
        let f1 = sample_function("id_f1", "demo", "foo", "demo.foo", "/a.rs", 1);
        let f2 = sample_function("id_f2", "other", "foo", "other.foo", "/a.rs", 1);
        storage
            .save_nodes(&[f1, f2], NodeLabel::Function)
            .expect("save_nodes");

        let engine = SearchEngine::new(storage.as_ref());
        let map = engine.load_qn_to_node_id_map("demo", &["demo.foo", "other.foo"]);
        assert!(map.contains_key("demo.foo"));
        assert!(
            !map.contains_key("other.foo"),
            "other project should be filtered"
        );
    }

    #[test]
    fn multi_signal_score_unaffected_by_storage_after_batch_load() {
        // Sanity: even when additional unrelated TESTS edges exist for other projects,
        // the score for this project's candidate is correctly computed.
        let storage = build_storage();
        let handler = Node::builder(NodeLabel::Function, "handler", "demo.handler")
            .id("h1")
            .project("demo")
            .file_path("/src/handlers.rs")
            .start_line(1)
            .end_line(10)
            .language(Language::Rust)
            .build();
        storage
            .save_nodes(&[handler], NodeLabel::Function)
            .expect("save_nodes");

        let calls_edges: Vec<Edge> = (0..100)
            .map(|i| Edge::new(format!("caller_{i}"), "h1", EdgeType::Calls, "demo"))
            .collect();
        storage.save_edges(&calls_edges).expect("save calls edges");

        // TESTS edge for h1 (demo) + noise TESTS edge for other project
        let tests_edges = [
            Edge::new("test_fn", "h1", EdgeType::Tests, "demo"),
            Edge::new("test_other", "h_other", EdgeType::Tests, "other"),
        ];
        storage.save_edges(&tests_edges).expect("save tests edges");

        let engine = SearchEngine::new(storage.as_ref());
        let params = SearchParams {
            query: "handler".to_string(),
            mode: SearchMode::MultiSignal,
            file_pattern: Some("/src/handlers.rs".to_string()),
            ..SearchParams::default()
        };
        let results = engine.search("demo", &params).expect("multi-signal search");
        assert_eq!(results.len(), 1);
        // name_relevance(1.0)*0.4 + degree_centrality(1.0)*0.3
        // + module_proximity(1.0)*0.2 + test_coverage(1.0)*0.1 = 1.0
        assert!(
            (results[0].score - 1.0).abs() < 1e-9,
            "expected score 1.0, got {}",
            results[0].score
        );
    }

    #[test]
    fn compute_name_relevance_returns_expected_values() {
        assert_eq!(compute_name_relevance("handler", "handler"), 1.0);
        assert_eq!(compute_name_relevance("HANDLER", "handler"), 1.0);
        assert_eq!(compute_name_relevance("handler_ext", "handler"), 0.8);
        assert_eq!(compute_name_relevance("my_handler", "handler"), 0.8);
        assert_eq!(compute_name_relevance("unrelated", "handler"), 0.0);
        assert_eq!(compute_name_relevance("anything", ""), 1.0);
    }

    #[test]
    fn compute_module_proximity_returns_expected_values() {
        assert_eq!(
            compute_module_proximity(
                &Some("/src/handlers.rs".to_string()),
                &Some("/src/handlers.rs".to_string())
            ),
            1.0
        );
        assert_eq!(
            compute_module_proximity(
                &Some("/src/main.rs".to_string()),
                &Some("/src/handlers.rs".to_string())
            ),
            0.5
        );
        assert_eq!(
            compute_module_proximity(&Some("/a.rs".to_string()), &None),
            0.5
        );
        assert_eq!(
            compute_module_proximity(&None, &Some("/a.rs".to_string())),
            0.5
        );
    }

    // --- Coverage: SearchEngine per-table error-path continues ---

    #[test]
    fn search_engine_exact_continues_on_query_error() {
        // Cover `Err(_) => continue` (line 174) in search_exact: when a
        // per-table MATCH query fails (table dropped), the loop skips it and
        // continues to the next table.
        let storage = build_storage();
        let func = Node::builder(NodeLabel::Function, "parse_file", "demo.parse_file")
            .id("f1")
            .project("demo")
            .file_path("/a.rs")
            .start_line(1)
            .end_line(10)
            .language(Language::Rust)
            .build();
        storage
            .save_nodes(&[func], NodeLabel::Function)
            .expect("save_nodes");
        storage.execute("DROP TABLE Class;").expect("drop table");
        let engine = SearchEngine::new(storage.as_ref());
        let params = SearchParams {
            query: "parse".to_string(),
            mode: SearchMode::Exact,
            ..SearchParams::default()
        };
        let results = engine.search("demo", &params).expect("exact search");
        assert!(results.iter().any(|r| r.name == "parse_file"));
    }

    #[test]
    fn search_engine_regex_continues_on_query_error() {
        // Cover `Err(_) => continue` (line 211) in search_regex: Class is one
        // of the three labels searched; dropping it triggers the error arm.
        let storage = build_storage();
        let func = Node::builder(NodeLabel::Function, "get_user", "demo.get_user")
            .id("f1")
            .project("demo")
            .file_path("/a.rs")
            .start_line(1)
            .end_line(10)
            .language(Language::Rust)
            .build();
        storage
            .save_nodes(&[func], NodeLabel::Function)
            .expect("save_nodes");
        storage.execute("DROP TABLE Class;").expect("drop table");
        let engine = SearchEngine::new(storage.as_ref());
        let params = SearchParams {
            query: r"get_.*".to_string(),
            mode: SearchMode::Regex,
            ..SearchParams::default()
        };
        let results = engine.search("demo", &params).expect("regex search");
        assert!(results.iter().any(|r| r.name == "get_user"));
    }

    #[test]
    fn search_engine_fuzzy_continues_on_query_error() {
        // Cover `Err(_) => continue` (line 281) in search_fuzzy.
        let storage = build_storage();
        let func = Node::builder(NodeLabel::Function, "parse", "demo.parse")
            .id("f1")
            .project("demo")
            .file_path("/a.rs")
            .start_line(1)
            .end_line(10)
            .language(Language::Rust)
            .build();
        storage
            .save_nodes(&[func], NodeLabel::Function)
            .expect("save_nodes");
        storage.execute("DROP TABLE Class;").expect("drop table");
        let engine = SearchEngine::new(storage.as_ref());
        let params = SearchParams {
            query: "parse".to_string(),
            mode: SearchMode::Fuzzy,
            ..SearchParams::default()
        };
        let results = engine.search("demo", &params).expect("fuzzy search");
        assert!(results.iter().any(|r| r.name == "parse"));
    }

    #[test]
    fn search_engine_graph_enhanced_continues_on_query_error() {
        // Cover `Err(_) => continue` (line 360) in search_graph_enhanced.
        let storage = build_storage();
        let func = Node::builder(NodeLabel::Function, "parse", "demo.parse")
            .id("f1")
            .project("demo")
            .file_path("/a.rs")
            .start_line(1)
            .end_line(10)
            .language(Language::Rust)
            .build();
        storage
            .save_nodes(&[func], NodeLabel::Function)
            .expect("save_nodes");
        storage.execute("DROP TABLE Class;").expect("drop table");
        let engine = SearchEngine::new(storage.as_ref());
        let params = SearchParams {
            query: "parse".to_string(),
            mode: SearchMode::GraphEnhanced,
            ..SearchParams::default()
        };
        let results = engine.search("demo", &params).expect("graph search");
        assert!(results.iter().any(|r| r.name == "parse"));
    }

    #[test]
    fn search_engine_graph_enhanced_returns_empty_when_label_filter_all_invalid() {
        // Cover `return Ok(Vec::new())` (line 342) when labels.is_empty():
        // all entries in label_filter fail to parse as NodeLabel.
        let storage = build_storage();
        let func = Node::builder(NodeLabel::Function, "parse", "demo.parse")
            .id("f1")
            .project("demo")
            .file_path("/a.rs")
            .start_line(1)
            .end_line(10)
            .language(Language::Rust)
            .build();
        storage
            .save_nodes(&[func], NodeLabel::Function)
            .expect("save_nodes");
        let engine = SearchEngine::new(storage.as_ref());
        let params = SearchParams {
            query: "parse".to_string(),
            mode: SearchMode::GraphEnhanced,
            label_filter: Some(vec!["NotARealLabel".to_string()]),
            ..SearchParams::default()
        };
        let results = engine.search("demo", &params).expect("graph search");
        assert!(results.is_empty(), "expected empty when no valid labels");
    }

    #[test]
    fn load_calls_indegree_returns_empty_map_on_query_error() {
        // Cover `Err(_) => return Ok(map)` (line 415) in load_calls_indegree:
        // when the CodeRelation table is dropped, the CALLS query errors and
        // an empty map is returned (not propagated as an error).
        let storage = build_storage();
        storage
            .execute("DROP TABLE CodeRelation;")
            .expect("drop table");
        let engine = SearchEngine::new(storage.as_ref());
        let map = engine
            .load_calls_indegree("demo")
            .expect("load_calls_indegree should return Ok(empty) on query error");
        assert!(map.is_empty(), "expected empty degree map, got {map:?}");
    }

    #[test]
    fn load_tested_node_ids_returns_err_on_query_error() {
        // Cover `Err(e) => return Err(e.into())` (line 477) in
        // load_tested_node_ids: when the CodeRelation table is dropped, the
        // TESTS query errors and the function returns Err (unlike
        // load_calls_indegree which swallows the error).
        let storage = build_storage();
        storage
            .execute("DROP TABLE CodeRelation;")
            .expect("drop table");
        let engine = SearchEngine::new(storage.as_ref());
        let result = engine.load_tested_node_ids("demo");
        assert!(
            result.is_err(),
            "expected error when CodeRelation table is dropped, got {result:?}"
        );
    }

    #[test]
    fn search_truncates_regex_results_exceeding_limit() {
        // Cover `if limit < results.len() { results.truncate(limit); }`
        // (lines 145-147) for SearchMode::Regex: search_regex does not apply
        // the limit internally, so the final truncate in `search()` is the
        // only path that caps the result count.
        let storage = build_storage();
        let funcs: Vec<Node> = (0..10)
            .map(|i| {
                Node::builder(
                    NodeLabel::Function,
                    format!("get_item_{i}"),
                    format!("demo.get_item_{i}"),
                )
                .id(format!("f{i}"))
                .project("demo")
                .file_path("/a.rs")
                .start_line(i * 10 + 1)
                .end_line(i * 10 + 9)
                .language(Language::Rust)
                .build()
            })
            .collect();
        storage
            .save_nodes(&funcs, NodeLabel::Function)
            .expect("save_nodes");
        let engine = SearchEngine::new(storage.as_ref());
        let params = SearchParams {
            query: r"get_item_.*".to_string(),
            mode: SearchMode::Regex,
            limit: 3,
            ..SearchParams::default()
        };
        let results = engine.search("demo", &params).expect("regex search");
        assert_eq!(
            results.len(),
            3,
            "regex results should be truncated to limit 3, got {}",
            results.len()
        );
    }

    #[test]
    fn search_truncates_fuzzy_results_exceeding_limit() {
        // Cover `if limit < results.len() { results.truncate(limit); }`
        // (lines 145-147) for SearchMode::Fuzzy: search_fuzzy does not apply
        // the limit internally, so the final truncate in `search()` is the
        // only path that caps the result count.
        let storage = build_storage();
        let funcs: Vec<Node> = (0..10)
            .map(|i| {
                Node::builder(
                    NodeLabel::Function,
                    format!("parse{i}"),
                    format!("demo.parse{i}"),
                )
                .id(format!("f{i}"))
                .project("demo")
                .file_path("/a.rs")
                .start_line(i * 10 + 1)
                .end_line(i * 10 + 9)
                .language(Language::Rust)
                .build()
            })
            .collect();
        storage
            .save_nodes(&funcs, NodeLabel::Function)
            .expect("save_nodes");
        let engine = SearchEngine::new(storage.as_ref());
        let params = SearchParams {
            query: "parse0".to_string(),
            mode: SearchMode::Fuzzy,
            limit: 3,
            ..SearchParams::default()
        };
        let results = engine.search("demo", &params).expect("fuzzy search");
        assert!(
            results.len() <= 3,
            "fuzzy results should be truncated to limit 3, got {}",
            results.len()
        );
        // parse0 has distance 0, parse1..parse9 have distance 1 — all within
        // MAX_FUZZY_DISTANCE (3), so without truncation there would be 10.
        assert!(
            !results.is_empty(),
            "fuzzy search should match at least parse0"
        );
    }

    #[test]
    fn compute_module_proximity_both_none_returns_half() {
        // Cover the `_ => 0.5` arm (line 796) for the (None, None) case,
        // which is not exercised by compute_module_proximity_returns_expected_values.
        assert_eq!(compute_module_proximity(&None, &None), 0.5);
    }

    #[test]
    fn load_qn_to_node_id_map_skips_table_query_errors() {
        // Cover `if let Ok(rows) = ...` Err branch (line 504): when a table
        // query fails (table dropped), the loop skips that table silently
        // and continues to the next label. The Function table still yields
        // results, so the returned map is non-empty.
        let storage = build_storage();
        let func = Node::builder(NodeLabel::Function, "foo", "demo.foo")
            .id("f1")
            .project("demo")
            .file_path("/a.rs")
            .start_line(1)
            .end_line(10)
            .language(Language::Rust)
            .build();
        storage
            .save_nodes(&[func], NodeLabel::Function)
            .expect("save_nodes");
        storage
            .execute("DROP TABLE Class;")
            .expect("drop Class table");
        let engine = SearchEngine::new(storage.as_ref());
        let map = engine.load_qn_to_node_id_map("demo", &["demo.foo"]);
        assert_eq!(
            map.get("demo.foo").map(String::as_str),
            Some("f1"),
            "Function table query should still succeed and populate the map"
        );
    }

    // --- Coverage gap tests: compute_module_proximity all branches ---

    #[test]
    fn compute_module_proximity_both_some_match_returns_one() {
        // Cover `(Some(path), Some(pattern)) if path.contains(pattern) => 1.0`
        assert_eq!(
            compute_module_proximity(&Some("/src/main.rs".to_string()), &Some("main".to_string())),
            1.0
        );
    }

    #[test]
    fn compute_module_proximity_both_some_no_match_returns_half() {
        // Cover `(Some(path), Some(pattern))` where pattern not in path → falls to `_ => 0.5`
        assert_eq!(
            compute_module_proximity(
                &Some("/src/main.rs".to_string()),
                &Some("other".to_string())
            ),
            0.5
        );
    }

    #[test]
    fn compute_module_proximity_some_none_returns_half() {
        // Cover `(Some(_), None) => 0.5`
        assert_eq!(
            compute_module_proximity(&Some("/a.rs".to_string()), &None),
            0.5
        );
    }

    #[test]
    fn compute_module_proximity_none_some_returns_half() {
        // Cover `(None, Some(_)) => 0.5`
        assert_eq!(
            compute_module_proximity(&None, &Some("pattern".to_string())),
            0.5
        );
    }

    // --- Coverage gap tests: compute_name_relevance ---

    #[test]
    fn compute_name_relevance_empty_query_returns_one() {
        // Cover `if query.is_empty() { return 1.0; }`
        assert_eq!(compute_name_relevance("foo", ""), 1.0);
    }

    #[test]
    fn compute_name_relevance_exact_match_returns_one() {
        // Cover `name_lower == query_lower => 1.0`
        assert_eq!(compute_name_relevance("Parse", "parse"), 1.0);
    }

    #[test]
    fn compute_name_relevance_contains_returns_zero_eight() {
        // Cover `name_lower.contains(&query_lower) => 0.8`
        assert_eq!(compute_name_relevance("parse_file", "parse"), 0.8);
    }

    #[test]
    fn compute_name_relevance_no_match_returns_zero() {
        // Cover `_ => 0.0`
        assert_eq!(compute_name_relevance("read_input", "parse"), 0.0);
    }

    // --- Coverage gap tests: relevance_score_with_reason neutral path ---

    #[test]
    fn relevance_score_with_reason_empty_query_returns_neutral() {
        // Cover `if query.is_empty() { return (1.0, "neutral"); }`
        let (score, reason) = relevance_score_with_reason("foo", "");
        assert_eq!(score, 1.0);
        assert_eq!(reason, "neutral");
    }

    // --- Coverage gap tests: parse_node_label ---

    #[test]
    fn parse_node_label_valid_labels_return_some() {
        assert_eq!(parse_node_label("Function"), Some(NodeLabel::Function));
        assert_eq!(parse_node_label("Class"), Some(NodeLabel::Class));
        assert_eq!(parse_node_label("function"), Some(NodeLabel::Function));
    }

    #[test]
    fn parse_node_label_unknown_label_returns_none() {
        // Cover `name.parse::<NodeLabel>().ok()` → None
        assert_eq!(parse_node_label("UnknownLabel"), None);
        assert_eq!(parse_node_label(""), None);
    }

    // --- Coverage gap tests: clamped_limit ---

    #[test]
    fn clamped_limit_caps_at_max_limit() {
        // Cover `self.limit.min(MAX_LIMIT)` when limit > MAX_LIMIT
        let params = SearchParams {
            limit: MAX_LIMIT + 100,
            ..SearchParams::default()
        };
        assert_eq!(params.clamped_limit(), MAX_LIMIT);
    }

    #[test]
    fn clamped_limit_preserves_small_values() {
        let params = SearchParams {
            limit: 10,
            ..SearchParams::default()
        };
        assert_eq!(params.clamped_limit(), 10);
    }

    #[test]
    fn clamped_limit_zero_returns_zero() {
        let params = SearchParams {
            limit: 0,
            ..SearchParams::default()
        };
        assert_eq!(params.clamped_limit(), 0);
    }

    // --- Coverage gap tests: levenshtein edge cases ---

    #[test]
    fn levenshtein_empty_a_returns_b_length() {
        // Cover `if a_bytes.is_empty() { return b_bytes.len(); }`
        assert_eq!(levenshtein("", "hello"), 5);
    }

    #[test]
    fn levenshtein_empty_b_returns_a_length() {
        // Cover `if b_bytes.is_empty() { return a_bytes.len(); }`
        assert_eq!(levenshtein("hello", ""), 5);
    }

    #[test]
    fn levenshtein_both_empty_returns_zero() {
        assert_eq!(levenshtein("", ""), 0);
    }

    #[test]
    fn levenshtein_swaps_shorter_to_b() {
        // Cover the swap branch: when a.len < b.len, they are swapped.
        // a="ab" (len 2), b="abc" (len 3) → after swap a="abc", b="ab"
        assert_eq!(levenshtein("ab", "abc"), 1);
    }

    // --- Coverage gap tests: search_graph_enhanced empty labels ---

    #[test]
    fn search_graph_enhanced_empty_label_filter_returns_empty() {
        // Cover `if labels.is_empty() { return Ok(Vec::new()); }`:
        // when label_filter contains only unknown labels, parse_node_label
        // returns None for all, leaving labels empty.
        let storage = build_storage();
        let func = Node::builder(NodeLabel::Function, "foo", "demo.foo")
            .id("f1")
            .project("demo")
            .file_path("/a.rs")
            .start_line(1)
            .end_line(10)
            .language(Language::Rust)
            .build();
        storage
            .save_nodes(&[func], NodeLabel::Function)
            .expect("save_nodes");
        let engine = SearchEngine::new(storage.as_ref());
        let params = SearchParams {
            query: "foo".to_string(),
            mode: SearchMode::GraphEnhanced,
            label_filter: Some(vec!["UnknownLabel".to_string()]),
            ..SearchParams::default()
        };
        let results = engine.search("demo", &params).expect("search");
        assert!(results.is_empty(), "unknown label filter → no results");
    }

    // --- Coverage gap tests: load_tested_node_ids error propagation ---

    #[test]
    fn load_tested_node_ids_returns_error_on_storage_failure() {
        // Cover `Err(e) => return Err(e.into())` (line 477): when the
        // CodeRelation table is dropped, the query errors and propagates.
        let storage = build_storage();
        storage
            .execute("DROP TABLE CodeRelation;")
            .expect("drop table");
        let engine = SearchEngine::new(storage.as_ref());
        let result = engine.load_tested_node_ids("demo");
        assert!(
            result.is_err(),
            "dropped CodeRelation should propagate error"
        );
    }

    // --- Coverage gap tests: search_graph_enhanced degree filter ---

    #[test]
    fn search_graph_enhanced_degree_filter_excludes_out_of_range() {
        // Cover `if degree < min || degree > max { continue; }` exclusion path:
        // a node with degree 0 is excluded when min=1.
        let storage = build_storage();
        let func = Node::builder(NodeLabel::Function, "foo", "demo.foo")
            .id("f1")
            .project("demo")
            .file_path("/a.rs")
            .start_line(1)
            .end_line(10)
            .language(Language::Rust)
            .build();
        storage
            .save_nodes(&[func], NodeLabel::Function)
            .expect("save_nodes");
        let engine = SearchEngine::new(storage.as_ref());
        let params = SearchParams {
            query: "foo".to_string(),
            mode: SearchMode::GraphEnhanced,
            degree_filter: Some((1, 100)),
            ..SearchParams::default()
        };
        let results = engine.search("demo", &params).expect("search");
        assert!(
            results.is_empty(),
            "node with degree 0 should be excluded by min=1 filter"
        );
    }

    #[test]
    fn search_graph_enhanced_degree_filter_includes_in_range() {
        // Cover the pass-through path: degree within filter range.
        let storage = build_storage();
        let caller = Node::builder(NodeLabel::Function, "caller", "demo.caller")
            .id("c1")
            .project("demo")
            .file_path("/a.rs")
            .start_line(1)
            .end_line(10)
            .language(Language::Rust)
            .build();
        let callee = Node::builder(NodeLabel::Function, "callee", "demo.callee")
            .id("c2")
            .project("demo")
            .file_path("/a.rs")
            .start_line(20)
            .end_line(30)
            .language(Language::Rust)
            .build();
        storage
            .save_nodes(&[caller, callee], NodeLabel::Function)
            .expect("save_nodes");
        let edge = Edge::builder("c1", "c2", EdgeType::Calls, "demo")
            .confidence(0.9)
            .build();
        storage.save_edges(&[edge]).expect("save_edges");
        let engine = SearchEngine::new(storage.as_ref());
        let params = SearchParams {
            query: "callee".to_string(),
            mode: SearchMode::GraphEnhanced,
            degree_filter: Some((1, 100)),
            ..SearchParams::default()
        };
        let results = engine.search("demo", &params).expect("search");
        assert!(
            results.iter().any(|r| r.name == "callee"),
            "callee with degree 1 should be included by min=1 filter"
        );
    }

    // --- Coverage gap tests: MultiSignal mode ---

    #[test]
    fn search_multi_signal_scores_and_sorts_results() {
        // Cover MultiSignal dispatch path: score_multi_signal is called
        // and results are sorted by score descending.
        let storage = build_storage();
        let func = Node::builder(NodeLabel::Function, "parse", "demo.parse")
            .id("f1")
            .project("demo")
            .file_path("/a.rs")
            .start_line(1)
            .end_line(10)
            .language(Language::Rust)
            .build();
        storage
            .save_nodes(&[func], NodeLabel::Function)
            .expect("save_nodes");
        let engine = SearchEngine::new(storage.as_ref());
        let params = SearchParams {
            query: "parse".to_string(),
            mode: SearchMode::MultiSignal,
            ..SearchParams::default()
        };
        let results = engine.search("demo", &params).expect("search");
        assert!(!results.is_empty(), "MultiSignal should find parse");
        assert_eq!(results[0].match_reason, "multi-signal");
    }

    // --- Coverage: MultiSignal unwrap_or_default when load_tested_node_ids fails ---

    #[test]
    fn search_multi_signal_with_dropped_coderelation_returns_results() {
        // Cover `let tested_ids = self.load_tested_node_ids(project).unwrap_or_default();`
        // (line 126): when CodeRelation table is dropped, load_tested_node_ids
        // returns Err, and unwrap_or_default() falls back to an empty HashSet.
        let storage = build_storage();
        let func = Node::builder(NodeLabel::Function, "handler", "demo.handler")
            .id("h1")
            .project("demo")
            .file_path("/a.rs")
            .start_line(1)
            .end_line(10)
            .language(Language::Rust)
            .build();
        storage
            .save_nodes(&[func], NodeLabel::Function)
            .expect("save_nodes");
        storage
            .execute("DROP TABLE CodeRelation;")
            .expect("drop table");
        let engine = SearchEngine::new(storage.as_ref());
        let params = SearchParams {
            query: "handler".to_string(),
            mode: SearchMode::MultiSignal,
            ..SearchParams::default()
        };
        // Should NOT error — unwrap_or_default swallows the load error.
        let results = engine.search("demo", &params).expect("multi-signal search");
        assert_eq!(results.len(), 1);
        assert_eq!(results[0].match_reason, "multi-signal");
        // test_coverage = 0.0 (no TESTS edges found since CodeRelation is gone)
        // name_relevance(1.0)*0.4 + degree_centrality(0.0)*0.3
        // + module_proximity(0.5)*0.2 + test_coverage(0.0)*0.1 = 0.5
        assert!(
            (results[0].score - 0.5).abs() < 1e-9,
            "expected score 0.5 without CodeRelation, got {}",
            results[0].score
        );
    }

    // --- Coverage: final truncate in search() for GraphEnhanced mode ---

    #[test]
    fn search_graph_enhanced_truncates_results_exceeding_limit() {
        // Cover `if limit < results.len() { results.truncate(limit); }` (lines 145-147)
        // for SearchMode::GraphEnhanced: search_graph_enhanced does NOT truncate
        // internally, so the final truncate in search() is the only cap.
        let storage = build_storage();
        let funcs: Vec<Node> = (0..10)
            .map(|i| {
                Node::builder(
                    NodeLabel::Function,
                    format!("handler_{i}"),
                    format!("demo.handler_{i}"),
                )
                .id(format!("f{i}"))
                .project("demo")
                .file_path("/a.rs")
                .start_line(i * 10 + 1)
                .end_line(i * 10 + 9)
                .language(Language::Rust)
                .build()
            })
            .collect();
        storage
            .save_nodes(&funcs, NodeLabel::Function)
            .expect("save_nodes");
        let engine = SearchEngine::new(storage.as_ref());
        let params = SearchParams {
            query: "handler".to_string(),
            mode: SearchMode::GraphEnhanced,
            limit: 3,
            ..SearchParams::default()
        };
        let results = engine.search("demo", &params).expect("graph search");
        assert_eq!(
            results.len(),
            3,
            "graph enhanced results should be truncated to limit 3, got {}",
            results.len()
        );
    }

    // --- Coverage: final truncate in search() for MultiSignal mode ---

    #[test]
    fn search_multi_signal_truncates_results_exceeding_limit() {
        // Cover `if limit < results.len() { results.truncate(limit); }` (lines 145-147)
        // for SearchMode::MultiSignal.
        let storage = build_storage();
        let funcs: Vec<Node> = (0..10)
            .map(|i| {
                Node::builder(
                    NodeLabel::Function,
                    format!("handler_{i}"),
                    format!("demo.handler_{i}"),
                )
                .id(format!("f{i}"))
                .project("demo")
                .file_path("/a.rs")
                .start_line(i * 10 + 1)
                .end_line(i * 10 + 9)
                .language(Language::Rust)
                .build()
            })
            .collect();
        storage
            .save_nodes(&funcs, NodeLabel::Function)
            .expect("save_nodes");
        let engine = SearchEngine::new(storage.as_ref());
        let params = SearchParams {
            query: "handler".to_string(),
            mode: SearchMode::MultiSignal,
            limit: 3,
            ..SearchParams::default()
        };
        let results = engine.search("demo", &params).expect("multi-signal search");
        assert_eq!(
            results.len(),
            3,
            "multi-signal results should be truncated to limit 3, got {}",
            results.len()
        );
    }

    // --- Coverage: final truncate in search() for Exact mode ---

    #[test]
    fn search_exact_truncates_results_exceeding_limit() {
        // Cover `if limit < results.len() { results.truncate(limit); }` (lines 145-147)
        // for SearchMode::Exact: search_exact truncates internally, but with
        // limit=0 the final truncate should still produce empty results.
        let storage = build_storage();
        let func = Node::builder(NodeLabel::Function, "parse", "demo.parse")
            .id("f1")
            .project("demo")
            .file_path("/a.rs")
            .start_line(1)
            .end_line(10)
            .language(Language::Rust)
            .build();
        storage
            .save_nodes(&[func], NodeLabel::Function)
            .expect("save_nodes");
        let engine = SearchEngine::new(storage.as_ref());
        let params = SearchParams {
            query: "parse".to_string(),
            mode: SearchMode::Exact,
            limit: 0,
            ..SearchParams::default()
        };
        let results = engine.search("demo", &params).expect("exact search");
        assert!(
            results.is_empty(),
            "limit=0 should return empty, got {}",
            results.len()
        );
    }

    // --- Coverage: GraphEnhanced degree filter boundary at max ---

    #[test]
    fn search_graph_enhanced_degree_filter_boundary_at_max_inclusive() {
        // Cover the pass-through path when degree == max (boundary inclusive):
        // `if degree < min || degree > max` should be false when degree == max.
        let storage = build_storage();
        let caller = Node::builder(NodeLabel::Function, "caller", "demo.caller")
            .id("c1")
            .project("demo")
            .file_path("/a.rs")
            .start_line(1)
            .end_line(10)
            .language(Language::Rust)
            .build();
        let callee = Node::builder(NodeLabel::Function, "target", "demo.target")
            .id("c2")
            .project("demo")
            .file_path("/a.rs")
            .start_line(20)
            .end_line(30)
            .language(Language::Rust)
            .build();
        storage
            .save_nodes(&[caller, callee], NodeLabel::Function)
            .expect("save_nodes");
        let edge = Edge::new("c1", "c2", EdgeType::Calls, "demo");
        storage.save_edges(&[edge]).expect("save_edges");
        let engine = SearchEngine::new(storage.as_ref());
        // degree=1, filter=(0,1) → degree == max → should be included
        let params = SearchParams {
            query: "target".to_string(),
            mode: SearchMode::GraphEnhanced,
            degree_filter: Some((0, 1)),
            ..SearchParams::default()
        };
        let results = engine.search("demo", &params).expect("graph search");
        assert!(
            results.iter().any(|r| r.name == "target"),
            "degree=1 with filter (0,1) should be included"
        );
    }

    // --- Coverage: MultiSignal with file_pattern match ---

    #[test]
    fn search_multi_signal_with_file_pattern_match_increases_score() {
        // Cover the module_proximity = 1.0 path in score_multi_signal when
        // file_pattern matches the candidate's file_path.
        let storage = build_storage();
        let func = Node::builder(NodeLabel::Function, "handler", "demo.handler")
            .id("h1")
            .project("demo")
            .file_path("/src/handlers.rs")
            .start_line(1)
            .end_line(10)
            .language(Language::Rust)
            .build();
        storage
            .save_nodes(&[func], NodeLabel::Function)
            .expect("save_nodes");
        let engine = SearchEngine::new(storage.as_ref());
        let params = SearchParams {
            query: "handler".to_string(),
            mode: SearchMode::MultiSignal,
            file_pattern: Some("/src/handlers.rs".to_string()),
            ..SearchParams::default()
        };
        let results = engine.search("demo", &params).expect("multi-signal search");
        assert_eq!(results.len(), 1);
        // name_relevance(1.0)*0.4 + degree_centrality(0.0)*0.3
        // + module_proximity(1.0)*0.2 + test_coverage(0.0)*0.1 = 0.6
        assert!(
            (results[0].score - 0.6).abs() < 1e-9,
            "exact match + file_pattern match → score 0.6, got {}",
            results[0].score
        );
    }

    // --- Coverage: MultiSignal with no file_pattern (module_proximity = 0.5) ---

    #[test]
    fn search_multi_signal_without_file_pattern_uses_default_proximity() {
        // Cover the `_ => 0.5` path in compute_module_proximity when
        // file_pattern is None, through the MultiSignal search path.
        let storage = build_storage();
        let func = Node::builder(NodeLabel::Function, "handler", "demo.handler")
            .id("h1")
            .project("demo")
            .file_path("/a.rs")
            .start_line(1)
            .end_line(10)
            .language(Language::Rust)
            .build();
        storage
            .save_nodes(&[func], NodeLabel::Function)
            .expect("save_nodes");
        let engine = SearchEngine::new(storage.as_ref());
        let params = SearchParams {
            query: "handler".to_string(),
            mode: SearchMode::MultiSignal,
            ..SearchParams::default() // file_pattern = None
        };
        let results = engine.search("demo", &params).expect("multi-signal search");
        assert_eq!(results.len(), 1);
        // name_relevance(1.0)*0.4 + degree_centrality(0.0)*0.3
        // + module_proximity(0.5)*0.2 + test_coverage(0.0)*0.1 = 0.5
        assert!(
            (results[0].score - 0.5).abs() < 1e-9,
            "exact match + no file_pattern → score 0.5, got {}",
            results[0].score
        );
    }

    // --- Coverage: GraphEnhanced with partial label filter (mix valid + invalid) ---

    #[test]
    fn search_graph_enhanced_with_partial_label_filter_uses_valid_only() {
        // Cover the filter_map path in search_graph_enhanced when label_filter
        // contains a mix of valid and invalid label names: invalid labels are
        // silently filtered out, valid labels are searched.
        let storage = build_storage();
        let func = Node::builder(NodeLabel::Function, "handler", "demo.handler")
            .id("f1")
            .project("demo")
            .file_path("/a.rs")
            .start_line(1)
            .end_line(10)
            .language(Language::Rust)
            .build();
        storage
            .save_nodes(&[func], NodeLabel::Function)
            .expect("save_nodes");
        let engine = SearchEngine::new(storage.as_ref());
        let params = SearchParams {
            query: "handler".to_string(),
            mode: SearchMode::GraphEnhanced,
            label_filter: Some(vec!["Function".to_string(), "NotARealLabel".to_string()]),
            ..SearchParams::default()
        };
        let results = engine.search("demo", &params).expect("graph search");
        assert_eq!(results.len(), 1);
        assert_eq!(results[0].label, "Function");
    }

    // --- Coverage: search() sort stability with equal scores ---

    #[test]
    fn search_sorts_by_score_desc_then_name_asc() {
        // Cover the sort_by closure (lines 139-144): when two results have
        // the same score, they should be sorted by name ascending.
        let storage = build_storage();
        let funcs = [
            Node::builder(NodeLabel::Function, "zebra", "demo.zebra")
                .id("f1")
                .project("demo")
                .file_path("/a.rs")
                .start_line(1)
                .end_line(10)
                .language(Language::Rust)
                .build(),
            Node::builder(NodeLabel::Function, "alpha", "demo.alpha")
                .id("f2")
                .project("demo")
                .file_path("/a.rs")
                .start_line(20)
                .end_line(30)
                .language(Language::Rust)
                .build(),
        ];
        storage
            .save_nodes(&funcs, NodeLabel::Function)
            .expect("save_nodes");
        let engine = SearchEngine::new(storage.as_ref());
        let params = SearchParams {
            query: "a".to_string(), // matches both as substring
            mode: SearchMode::Exact,
            ..SearchParams::default()
        };
        let results = engine.search("demo", &params).expect("exact search");
        assert_eq!(results.len(), 2);
        // Both have substring match score 0.5, so sorted by name ascending
        assert_eq!(results[0].name, "alpha");
        assert_eq!(results[1].name, "zebra");
    }

    // ====================================================================
    // Per-table Err(_) => continue coverage for SearchEngine modes
    // ====================================================================

    #[test]
    fn search_exact_continues_on_per_table_query_error() {
        // Cover `Err(_) => continue` (line 174) in search_exact: when a
        // table query fails (table dropped), the loop skips it and
        // continues to the next table.
        let storage = build_storage();
        let func = Node::builder(NodeLabel::Function, "parse", "demo.parse")
            .id("f1")
            .project("demo")
            .file_path("/a.rs")
            .start_line(1)
            .end_line(10)
            .language(Language::Rust)
            .build();
        storage
            .save_nodes(&[func], NodeLabel::Function)
            .expect("save_nodes");
        storage.execute("DROP TABLE Class;").expect("drop table");
        let engine = SearchEngine::new(storage.as_ref());
        let params = SearchParams {
            query: "parse".to_string(),
            mode: SearchMode::Exact,
            ..SearchParams::default()
        };
        let results = engine.search("demo", &params).expect("exact search");
        assert!(results.iter().any(|r| r.name == "parse"));
    }

    #[test]
    fn search_regex_continues_on_per_table_query_error() {
        // Cover `Err(_) => continue` (line 211) in search_regex.
        let storage = build_storage();
        let func = Node::builder(NodeLabel::Function, "get_user", "demo.get_user")
            .id("f1")
            .project("demo")
            .file_path("/a.rs")
            .start_line(1)
            .end_line(10)
            .language(Language::Rust)
            .build();
        storage
            .save_nodes(&[func], NodeLabel::Function)
            .expect("save_nodes");
        storage.execute("DROP TABLE Class;").expect("drop table");
        let engine = SearchEngine::new(storage.as_ref());
        let params = SearchParams {
            query: r"get_.*".to_string(),
            mode: SearchMode::Regex,
            ..SearchParams::default()
        };
        let results = engine.search("demo", &params).expect("regex search");
        assert!(results.iter().any(|r| r.name == "get_user"));
    }

    #[test]
    fn search_fuzzy_continues_on_per_table_query_error() {
        // Cover `Err(_) => continue` (line 281) in search_fuzzy.
        let storage = build_storage();
        let func = Node::builder(NodeLabel::Function, "parse", "demo.parse")
            .id("f1")
            .project("demo")
            .file_path("/a.rs")
            .start_line(1)
            .end_line(10)
            .language(Language::Rust)
            .build();
        storage
            .save_nodes(&[func], NodeLabel::Function)
            .expect("save_nodes");
        storage.execute("DROP TABLE Class;").expect("drop table");
        let engine = SearchEngine::new(storage.as_ref());
        let params = SearchParams {
            query: "parse".to_string(),
            mode: SearchMode::Fuzzy,
            ..SearchParams::default()
        };
        let results = engine.search("demo", &params).expect("fuzzy search");
        assert!(results.iter().any(|r| r.name == "parse"));
    }

    #[test]
    fn search_graph_enhanced_continues_on_per_table_query_error() {
        // Cover `Err(_) => continue` (line 360) in search_graph_enhanced.
        let storage = build_storage();
        let func = Node::builder(NodeLabel::Function, "parse", "demo.parse")
            .id("f1")
            .project("demo")
            .file_path("/a.rs")
            .start_line(1)
            .end_line(10)
            .language(Language::Rust)
            .build();
        storage
            .save_nodes(&[func], NodeLabel::Function)
            .expect("save_nodes");
        storage.execute("DROP TABLE Class;").expect("drop table");
        let engine = SearchEngine::new(storage.as_ref());
        let params = SearchParams {
            query: "parse".to_string(),
            mode: SearchMode::GraphEnhanced,
            ..SearchParams::default()
        };
        let results = engine.search("demo", &params).expect("graph search");
        assert!(results.iter().any(|r| r.name == "parse"));
    }

    // ====================================================================
    // search_by_type error propagation via `?`
    // ====================================================================

    #[test]
    fn search_by_type_propagates_query_error() {
        // Cover `let rows = self.conn.query(&cypher)?;` (line 611) in
        // search_by_type: when the target table is dropped, the error
        // propagates (unlike search_by_name which uses `continue`).
        let repo = fresh_repo();
        repo.save_nodes(
            &[sample_function(
                "f1",
                "demo",
                "parse",
                "demo.parse",
                "/a.rs",
                1,
            )],
            NodeLabel::Function,
        )
        .expect("save_nodes");
        repo.connection()
            .execute("DROP TABLE Function;")
            .expect("drop Function table");
        let searcher = StructuredSearcher::new(repo.connection());
        let result = searcher.search_by_type(NodeLabel::Function, None, 100);
        assert!(
            result.is_err(),
            "dropped Function table should propagate error, got {result:?}"
        );
    }

    // ====================================================================
    // rows_to_search_results: defensive None handling
    // ====================================================================

    #[test]
    fn rows_to_search_results_skips_row_with_missing_name() {
        // Cover `row.first().and_then(|v| v.as_str())?` None path (line 680):
        // when the first column is not a string (or row is empty), the row
        // is filtered out.
        let rows: Vec<Vec<serde_json::Value>> = vec![
            // Row with null name → filtered out.
            vec![
                serde_json::Value::Null,
                serde_json::Value::Null,
                serde_json::Value::Null,
                serde_json::Value::Null,
            ],
            // Row with valid name → kept.
            vec![
                serde_json::Value::String("foo".to_string()),
                serde_json::Value::String("demo.foo".to_string()),
                serde_json::Value::String("/a.rs".to_string()),
                serde_json::Value::Number(serde_json::Number::from(1)),
            ],
        ];
        let results = rows_to_search_results(rows, NodeLabel::Function, "foo");
        assert_eq!(
            results.len(),
            1,
            "only the row with a valid name should be kept"
        );
        assert_eq!(results[0].name, "foo");
    }

    #[test]
    fn rows_to_search_results_handles_missing_optional_fields() {
        // Cover None paths for qualified_name (line 681), file_path (line 682),
        // and start_line (lines 683-686): when these columns are null or
        // missing, the SearchResult should have None/None for optional fields.
        let rows: Vec<Vec<serde_json::Value>> = vec![vec![
            serde_json::Value::String("foo".to_string()),
            serde_json::Value::Null, // qualified_name = None
            serde_json::Value::Null, // file_path = None
            serde_json::Value::Null, // start_line = None (not i64)
        ]];
        let results = rows_to_search_results(rows, NodeLabel::Function, "");
        assert_eq!(results.len(), 1);
        assert_eq!(results[0].name, "foo");
        assert!(results[0].qualified_name.is_none());
        assert!(results[0].file_path.is_none());
        assert!(results[0].start_line.is_none());
    }

    #[test]
    fn rows_to_search_results_handles_negative_start_line() {
        // Cover `u32::try_from(i).ok()` returning None for negative values
        // (line 686): a negative start_line should result in None.
        let rows: Vec<Vec<serde_json::Value>> = vec![vec![
            serde_json::Value::String("foo".to_string()),
            serde_json::Value::Null,
            serde_json::Value::Null,
            serde_json::Value::Number(serde_json::Number::from(-5i64)),
        ]];
        let results = rows_to_search_results(rows, NodeLabel::Function, "");
        assert_eq!(results.len(), 1);
        assert!(
            results[0].start_line.is_none(),
            "negative start_line should be None"
        );
    }

    // ====================================================================
    // sort_and_truncate: NaN score handling
    // ====================================================================

    #[test]
    fn sort_and_truncate_handles_nan_scores() {
        // Cover `unwrap_or(std::cmp::Ordering::Equal)` (line 805): when
        // a result has a NaN score, partial_cmp returns None, and the
        // fallback to Equal is used.
        let mut results = vec![
            SearchResult {
                name: "alpha".to_string(),
                label: "Function".to_string(),
                file_path: None,
                start_line: None,
                qualified_name: None,
                score: f64::NAN,
                match_reason: "nan".to_string(),
                degree: 0,
            },
            SearchResult {
                name: "beta".to_string(),
                label: "Function".to_string(),
                file_path: None,
                start_line: None,
                qualified_name: None,
                score: 1.0,
                match_reason: "exact".to_string(),
                degree: 0,
            },
        ];
        sort_and_truncate(&mut results, 100);
        // beta (score=1.0) should come before alpha (score=NaN) since
        // NaN comparison falls back to Equal, and then name ascending
        // breaks the tie: "alpha" < "beta".
        // Actually: 1.0 > NaN → partial_cmp returns Greater for (1.0, NaN)
        // when comparing b.score to a.score: b=1.0, a=NaN → 1.0 > NaN is false
        // → partial_cmp returns None → Equal → then name cmp: alpha < beta
        // So alpha comes first. Either way, both should be present.
        assert_eq!(results.len(), 2);
    }

    #[test]
    fn sort_and_truncate_truncates_when_limit_exceeds() {
        // Direct test for sort_and_truncate truncation (line 808-809).
        let mut results: Vec<SearchResult> = (0..10)
            .map(|i| SearchResult {
                name: format!("func_{i}"),
                label: "Function".to_string(),
                file_path: None,
                start_line: None,
                qualified_name: None,
                score: 1.0,
                match_reason: "exact".to_string(),
                degree: 0,
            })
            .collect();
        sort_and_truncate(&mut results, 3);
        assert_eq!(results.len(), 3, "should be truncated to 3");
    }

    #[test]
    fn sort_and_truncate_no_truncation_when_limit_large() {
        // Cover the `if limit < results.len()` false branch (line 808):
        // when limit >= results.len(), no truncation occurs.
        let mut results = vec![SearchResult {
            name: "foo".to_string(),
            label: "Function".to_string(),
            file_path: None,
            start_line: None,
            qualified_name: None,
            score: 1.0,
            match_reason: "exact".to_string(),
            degree: 0,
        }];
        sort_and_truncate(&mut results, 100);
        assert_eq!(results.len(), 1, "should not be truncated");
    }

    // ===== SearchMode FromStr / Display tests =====

    #[test]
    fn search_mode_display_canonical_strings() {
        assert_eq!(SearchMode::Exact.to_string(), "exact");
        assert_eq!(SearchMode::Regex.to_string(), "regex");
        assert_eq!(SearchMode::Fuzzy.to_string(), "fuzzy");
        assert_eq!(SearchMode::GraphEnhanced.to_string(), "graph_enhanced");
        assert_eq!(SearchMode::MultiSignal.to_string(), "multi_signal");
    }

    #[test]
    fn search_mode_from_str_parses_canonical_forms() {
        assert_eq!("exact".parse::<SearchMode>().unwrap(), SearchMode::Exact);
        assert_eq!("regex".parse::<SearchMode>().unwrap(), SearchMode::Regex);
        assert_eq!("fuzzy".parse::<SearchMode>().unwrap(), SearchMode::Fuzzy);
        assert_eq!(
            "graph_enhanced".parse::<SearchMode>().unwrap(),
            SearchMode::GraphEnhanced
        );
        assert_eq!(
            "multi_signal".parse::<SearchMode>().unwrap(),
            SearchMode::MultiSignal
        );
    }

    #[test]
    fn search_mode_from_str_parses_aliases() {
        assert_eq!(
            "graph".parse::<SearchMode>().unwrap(),
            SearchMode::GraphEnhanced
        );
        assert_eq!(
            "graph-enhanced".parse::<SearchMode>().unwrap(),
            SearchMode::GraphEnhanced
        );
        assert_eq!(
            "multi".parse::<SearchMode>().unwrap(),
            SearchMode::MultiSignal
        );
        assert_eq!(
            "multi-signal".parse::<SearchMode>().unwrap(),
            SearchMode::MultiSignal
        );
    }

    #[test]
    fn search_mode_from_str_is_case_insensitive() {
        assert_eq!("EXACT".parse::<SearchMode>().unwrap(), SearchMode::Exact);
        assert_eq!("Regex".parse::<SearchMode>().unwrap(), SearchMode::Regex);
        assert_eq!("FUZZY".parse::<SearchMode>().unwrap(), SearchMode::Fuzzy);
        assert_eq!(
            "GRAPH".parse::<SearchMode>().unwrap(),
            SearchMode::GraphEnhanced
        );
        assert_eq!(
            "MULTI".parse::<SearchMode>().unwrap(),
            SearchMode::MultiSignal
        );
    }

    #[test]
    fn search_mode_from_str_trims_whitespace() {
        assert_eq!(
            "  exact  ".parse::<SearchMode>().unwrap(),
            SearchMode::Exact
        );
        assert_eq!(" regex ".parse::<SearchMode>().unwrap(), SearchMode::Regex);
    }

    #[test]
    fn search_mode_from_str_rejects_empty() {
        assert!("".parse::<SearchMode>().is_err());
        assert!("   ".parse::<SearchMode>().is_err());
    }

    #[test]
    fn search_mode_from_str_rejects_unknown() {
        assert!("semantic".parse::<SearchMode>().is_err());
        assert!("invalid".parse::<SearchMode>().is_err());
    }

    #[test]
    fn search_mode_from_str_error_contains_input() {
        let err = "bogus".parse::<SearchMode>().unwrap_err();
        assert!(err.contains("bogus"));
    }

    #[test]
    fn search_mode_display_fromstr_roundtrip() {
        for mode in [
            SearchMode::Exact,
            SearchMode::Regex,
            SearchMode::Fuzzy,
            SearchMode::GraphEnhanced,
            SearchMode::MultiSignal,
        ] {
            let s = mode.to_string();
            let parsed: SearchMode = s.parse().unwrap();
            assert_eq!(mode, parsed);
        }
    }
}