velesdb-core 1.14.4

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

use super::index::{HnswIndex, VacuumError};
use super::params::{HnswParams, SearchQuality};
use crate::distance::DistanceMetric;
use crate::index::VectorIndex;

// =========================================================================
// Parity test for issue #694
// HnswIndex::search_batch_parallel must use ef_search_for_scale on the
// HNSW-only fast path so that single-query and batch-query paths produce
// the same ef value for the same quality profile when dataset_size > 10K.
//
// Note: this test documents and locks in the parity *contract* — that single
// and batch paths return the same top-k for the same quality. With uniformly
// random data the contract is mostly upheld even pre-fix because the candidate
// pool is plentiful at any reasonable ef. Adversarial data (e.g. SIFT1M with
// many near-equidistant points) would surface the asymmetry more clearly.
// We accept the limitation here: the fix is a coherence guarantee, not a
// recall fix; the goal is that future refactors of either path cannot drift
// out of parity without this assertion failing.
// =========================================================================

#[test]
fn test_batch_search_matches_single_query_on_large_dataset_issue_694() {
    use rand::rngs::StdRng;
    use rand::{Rng, SeedableRng};

    // Arrange: 40K-vector index — strictly above the 10K threshold where
    // ef_search_for_scale starts boosting the ef value. Below 10K both
    // ef_search and ef_search_for_scale return the same number, so the
    // asymmetry only manifests above the threshold.
    //
    // Why 40K + Fast quality: at 40K the scale factor is sqrt(4)=2 (capped),
    // so Fast (base ef=64) becomes 128 with scaling — a 2x change. That gives
    // the candidate set enough wiggle room for the unscaled batch path
    // (pre-fix) to return a different top-k from the scaled single-query path.
    // Below 10K or at smaller scale factors the effect is too small to
    // produce visible top-k divergence on uniformly random data.
    let dim = 16_usize;
    let n = 40_000_usize;
    let mut rng = StdRng::seed_from_u64(0x00C0_FFEE);

    let index = HnswIndex::new(dim, DistanceMetric::Cosine).unwrap();
    let vectors: Vec<(u64, Vec<f32>)> = (0..n)
        .map(|id| {
            let v: Vec<f32> = (0..dim).map(|_| rng.random::<f32>() - 0.5).collect();
            (id as u64, v)
        })
        .collect();
    let refs: Vec<(u64, &[f32])> = vectors.iter().map(|(id, v)| (*id, v.as_slice())).collect();
    index.insert_batch_parallel(refs);
    assert_eq!(index.len(), n);

    // 8 random query vectors
    let queries: Vec<Vec<f32>> = (0..8)
        .map(|_| (0..dim).map(|_| rng.random::<f32>() - 0.5).collect())
        .collect();
    let query_refs: Vec<&[f32]> = queries.iter().map(Vec::as_slice).collect();

    let k = 10_usize;
    // Fast quality (ef=64) magnifies the asymmetry: pre-fix batch gets ef=64,
    // post-fix batch gets ef=128 (scaled). Higher base efs (Balanced=128,
    // Accurate=512) already saturate the candidate space and hide the bug.
    let quality = SearchQuality::Fast;

    // Act: run both paths
    let single: Vec<Vec<u64>> = queries
        .iter()
        .map(|q| {
            let r = index.search_with_quality(q.as_slice(), k, quality).unwrap();
            r.iter().map(|x| x.id).collect()
        })
        .collect();

    let batch_results = index
        .search_batch_parallel(&query_refs, k, quality)
        .expect("batch search should succeed");
    let batch: Vec<Vec<u64>> = batch_results
        .iter()
        .map(|r| r.iter().map(|x| x.id).collect())
        .collect();

    // Assert: result IDs must match per-query.
    //
    // Pre-fix (issue #694): batch used ef_search(k), single used
    // ef_search_for_scale(k, len). At n=12_000 the scale factor was sqrt(1.2)
    // ≈ 1.095, so single saw ef * 1.09 (rounded) and batch saw ef * 1, giving
    // mismatched candidate sets and divergent top-k.
    //
    // Post-fix: both paths call ef_search_for_scale(k, self.len()), so the
    // candidate sets are identical and the result lists are identical.
    for (i, (s, b)) in single.iter().zip(batch.iter()).enumerate() {
        assert_eq!(
            s, b,
            "query {i}: batch and single results must match after #694 fix.\nsingle={s:?}\nbatch={b:?}"
        );
    }
}

// =========================================================================
// TDD Tests - Written BEFORE implementation (RED phase)
// =========================================================================

// -------------------------------------------------------------------------
// Vacuum / Maintenance Tests
// -------------------------------------------------------------------------

#[test]
fn test_tombstone_count_empty_index() {
    // Arrange
    let index = HnswIndex::new(64, DistanceMetric::Cosine).unwrap();

    // Act & Assert
    assert_eq!(index.tombstone_count(), 0);
    assert!((index.tombstone_ratio() - 0.0).abs() < f64::EPSILON);
    assert!(!index.needs_vacuum());
}

#[test]
fn test_tombstone_count_after_deletions() {
    // Arrange
    let index = HnswIndex::new(64, DistanceMetric::Cosine).unwrap();

    // Insert 10 vectors
    for i in 0..10 {
        let v: Vec<f32> = (0..64).map(|j| (i + j) as f32 * 0.01).collect();
        index.insert(i as u64, &v);
    }

    // Delete 3 vectors (30%)
    index.remove(1);
    index.remove(3);
    index.remove(5);

    // Assert
    assert_eq!(index.len(), 7);
    assert_eq!(index.tombstone_count(), 3);
    assert!((index.tombstone_ratio() - 0.3).abs() < 0.01);
    assert!(index.needs_vacuum()); // > 20% threshold
}

#[test]
fn test_vacuum_rebuilds_index() {
    // Arrange
    let index = HnswIndex::new(64, DistanceMetric::Cosine).unwrap();

    // Insert 20 vectors
    for i in 0..20 {
        let v: Vec<f32> = (0..64).map(|j| (i + j) as f32 * 0.01).collect();
        index.insert(i as u64, &v);
    }

    // Delete 10 vectors (50% tombstones)
    for i in 0..10 {
        index.remove(i as u64);
    }

    assert_eq!(index.len(), 10);
    assert!(index.needs_vacuum());

    // Act
    let result = index.vacuum();

    // Assert
    assert!(result.is_ok());
    assert_eq!(result.unwrap(), 10);
    assert_eq!(index.len(), 10);
    assert_eq!(index.tombstone_count(), 0);
    assert!(!index.needs_vacuum());
}

#[test]
fn test_vacuum_preserves_search_results() {
    // Arrange
    let index = HnswIndex::new(64, DistanceMetric::Cosine).unwrap();

    // Insert vectors with known patterns
    for i in 0..50 {
        let v: Vec<f32> = (0..64).map(|j| (i * 100 + j) as f32 * 0.001).collect();
        index.insert(i as u64, &v);
    }

    // Delete some vectors
    for i in 0..25 {
        index.remove(i as u64);
    }

    // Query before vacuum
    let query: Vec<f32> = (0..64).map(|j| (30 * 100 + j) as f32 * 0.001).collect();
    let _results_before = index.search(&query, 5);

    // Act
    let _ = index.vacuum();

    // Assert - search still works and returns similar results
    let results_after = index.search(&query, 5);
    assert_eq!(results_after.len(), 5);
    // Results should include vectors 25-49 (the remaining ones)
    for sr in &results_after {
        assert!(sr.id >= 25 && sr.id < 50);
    }
}

#[test]
fn test_drop_after_vacuum_and_reload_is_safe() {
    use tempfile::tempdir;

    let dir = tempdir().expect("Failed to create temp dir");
    {
        let index = HnswIndex::new(32, DistanceMetric::Euclidean).unwrap();

        for i in 0u64..120 {
            let vector: Vec<f32> = (0..32).map(|j| (i + j as u64) as f32 * 0.01).collect();
            index.insert(i, &vector);
        }
        for i in 0u64..40 {
            index.remove(i);
        }

        let rebuilt = index.vacuum().expect("vacuum should succeed");
        assert_eq!(rebuilt, 80);
        index.save(dir.path()).expect("Failed to save");
    } // drop after ManuallyDrop replacement path

    let loaded = HnswIndex::load(dir.path(), 32, DistanceMetric::Euclidean)
        .expect("Failed to load after vacuum+drop");
    let query = vec![0.42; 32];
    let results = loaded.search(&query, 10);
    assert!(!results.is_empty(), "Loaded index should remain searchable");
}

#[test]
fn test_vacuum_fails_with_fast_insert_mode() {
    // Arrange
    let index = HnswIndex::new_fast_insert(64, DistanceMetric::Cosine).unwrap();

    for i in 0..10 {
        let v: Vec<f32> = (0..64).map(|j| (i + j) as f32 * 0.01).collect();
        index.insert(i as u64, &v);
    }

    // Act
    let result = index.vacuum();

    // Assert
    assert!(result.is_err());
    assert_eq!(result.unwrap_err(), VacuumError::VectorStorageDisabled);
}

#[test]
fn test_vacuum_empty_index() {
    // Arrange
    let index = HnswIndex::new(64, DistanceMetric::Cosine).unwrap();

    // Act
    let result = index.vacuum();

    // Assert
    assert!(result.is_ok());
    assert_eq!(result.unwrap(), 0);
}

// -------------------------------------------------------------------------
// Basic Index Tests
// -------------------------------------------------------------------------

#[test]
fn test_hnsw_new_creates_empty_index() {
    // Arrange & Act
    let index = HnswIndex::new(768, DistanceMetric::Cosine).unwrap();

    // Assert
    assert!(index.is_empty());
    assert_eq!(index.len(), 0);
    assert_eq!(index.dimension(), 768);
    assert_eq!(index.metric(), DistanceMetric::Cosine);
}

#[test]
fn test_hnsw_new_turbo_mode() {
    // TDD: Turbo mode uses aggressive params for max insert throughput
    // Target: 5k+ vec/s (vs ~2k/s with auto params)
    let index = HnswIndex::new_turbo(64, DistanceMetric::Cosine).unwrap();

    // Insert vectors - should be faster than standard mode
    for i in 0..100 {
        let v: Vec<f32> = (0..64).map(|j| (i + j) as f32 * 0.01).collect();
        index.insert(i as u64, &v);
    }

    // Assert - basic functionality works
    assert_eq!(index.len(), 100);

    // Search should still work (lower recall expected ~85%)
    let query: Vec<f32> = (0..64).map(|j| j as f32 * 0.01).collect();
    let results = index.search(&query, 10);
    assert!(!results.is_empty()); // At least some results
}

#[test]
fn test_hnsw_new_fast_insert_mode() {
    // Arrange & Act - fast insert mode disables vector storage
    let index = HnswIndex::new_fast_insert(64, DistanceMetric::Cosine).unwrap();

    // Insert vectors
    for i in 0..100 {
        let v: Vec<f32> = (0..64).map(|j| (i + j) as f32 * 0.01).collect();
        index.insert(i as u64, &v);
    }

    // Assert - basic functionality works
    assert_eq!(index.len(), 100);

    // Search should still work (uses HNSW approximate search)
    let query: Vec<f32> = (0..64).map(|j| j as f32 * 0.01).collect();
    let results = index.search(&query, 10);
    assert_eq!(results.len(), 10);
}

#[test]
fn test_hnsw_insert_single_vector() {
    // Arrange
    let index = HnswIndex::new(3, DistanceMetric::Cosine).unwrap();
    let vector = vec![1.0, 0.0, 0.0];

    // Act
    index.insert(1, &vector);

    // Assert
    assert_eq!(index.len(), 1);
    assert!(!index.is_empty());
}

#[test]
fn test_hnsw_insert_multiple_vectors() {
    // Arrange
    let index = HnswIndex::new(3, DistanceMetric::Cosine).unwrap();

    // Act
    index.insert(1, &[1.0, 0.0, 0.0]);
    index.insert(2, &[0.0, 1.0, 0.0]);
    index.insert(3, &[0.0, 0.0, 1.0]);

    // Assert
    assert_eq!(index.len(), 3);
}

#[test]
fn test_hnsw_search_returns_k_nearest() {
    // Arrange - use more vectors to make HNSW more stable
    let index = HnswIndex::new(3, DistanceMetric::Cosine).unwrap();
    index.insert(1, &[1.0, 0.0, 0.0]);
    index.insert(2, &[0.9, 0.1, 0.0]); // Similar to 1
    index.insert(3, &[0.0, 1.0, 0.0]); // Different
    index.insert(4, &[0.8, 0.2, 0.0]); // Similar to 1
    index.insert(5, &[0.0, 0.0, 1.0]); // Different

    // Act
    let results = index.search(&[1.0, 0.0, 0.0], 3);

    // Assert - HNSW may return fewer than k results with small datasets
    assert!(
        !results.is_empty() && results.len() <= 3,
        "Should return 1-3 results, got {}",
        results.len()
    );
    // First result should be exact match (id=1) - verify it's in top results
    let top_ids: Vec<u64> = results.iter().map(|sr| sr.id).collect();
    assert!(top_ids.contains(&1), "Exact match should be in top results");
}

#[test]
fn test_hnsw_search_empty_index() {
    // Arrange
    let index = HnswIndex::new(3, DistanceMetric::Cosine).unwrap();

    // Act
    let results = index.search(&[1.0, 0.0, 0.0], 10);

    // Assert
    assert!(results.is_empty());
}

#[test]
fn test_hnsw_remove_existing_vector() {
    // Arrange
    let index = HnswIndex::new(3, DistanceMetric::Cosine).unwrap();
    index.insert(1, &[1.0, 0.0, 0.0]);
    index.insert(2, &[0.0, 1.0, 0.0]);

    // Act
    let removed = index.remove(1);

    // Assert
    assert!(removed);
    assert_eq!(index.len(), 1);
}

#[test]
fn test_hnsw_remove_nonexistent_vector() {
    // Arrange
    let index = HnswIndex::new(3, DistanceMetric::Cosine).unwrap();
    index.insert(1, &[1.0, 0.0, 0.0]);

    // Act
    let removed = index.remove(999);

    // Assert
    assert!(!removed);
    assert_eq!(index.len(), 1);
}

#[test]
fn test_hnsw_euclidean_metric() {
    // Arrange - use more vectors to avoid HNSW flakiness with tiny datasets
    let index = HnswIndex::new(3, DistanceMetric::Euclidean).unwrap();
    index.insert(1, &[0.0, 0.0, 0.0]);
    index.insert(2, &[1.0, 0.0, 0.0]); // Distance 1
    index.insert(3, &[3.0, 4.0, 0.0]); // Distance 5
    index.insert(4, &[2.0, 0.0, 0.0]); // Distance 2
    index.insert(5, &[0.5, 0.5, 0.0]); // Distance ~0.7

    // Act
    let results = index.search(&[0.0, 0.0, 0.0], 3);

    // Assert - at least get some results, first should be closest
    assert!(!results.is_empty(), "Should return results");
    assert_eq!(results[0].id, 1, "Closest should be exact match");
}

#[test]
fn test_hnsw_dot_product_metric() {
    // Arrange - Use normalized positive vectors for dot product
    // DistDot in hnsw_rs requires non-negative dot products
    // Use more vectors to avoid HNSW flakiness with tiny datasets
    let index = HnswIndex::new(3, DistanceMetric::DotProduct).unwrap();

    // Insert vectors with distinct dot products when queried with [1,0,0]
    index.insert(1, &[1.0, 0.0, 0.0]); // dot=1.0 with query
    index.insert(2, &[0.5, 0.5, 0.5]); // dot=0.5 with query
    index.insert(3, &[0.1, 0.1, 0.1]); // dot=0.1 with query
    index.insert(4, &[0.8, 0.2, 0.0]); // dot=0.8 with query
    index.insert(5, &[0.3, 0.3, 0.3]); // dot=0.3 with query

    // Act - Query with unit vector x
    let query = [1.0, 0.0, 0.0];
    let results = index.search(&query, 3);

    // Assert - at least get some results, first should have highest dot product
    assert!(!results.is_empty(), "Should return results");
    assert_eq!(results[0].id, 1, "Highest dot product should be first");
}

#[test]
fn test_hnsw_insert_wrong_dimension_is_noop() {
    // Arrange
    let index = HnswIndex::new(3, DistanceMetric::Cosine).unwrap();

    // Act - dimension mismatch is silently rejected via VectorIndex trait
    index.insert(1, &[1.0, 0.0]); // Wrong dimension

    // Assert - no vector was inserted
    assert_eq!(index.len(), 0, "Wrong-dimension insert should be a no-op");
}

#[test]
fn test_hnsw_search_wrong_dimension_returns_error() {
    // Arrange
    let index = HnswIndex::new(3, DistanceMetric::Cosine).unwrap();
    index.insert(1, &[1.0, 0.0, 0.0]);

    // Act - dimension mismatch returns DimensionMismatch error
    let result = index.search_with_quality(&[1.0, 0.0], 10, SearchQuality::Balanced);
    assert!(result.is_err(), "Should return error on dimension mismatch");

    // The VectorIndex::search trait adapter returns empty Vec on error
    let results = index.search(&[1.0, 0.0], 10);
    assert!(
        results.is_empty(),
        "Trait search should return empty on mismatch"
    );
}

#[test]
fn test_hnsw_duplicate_insert_upserts_vector() {
    // Arrange
    let index = HnswIndex::new(3, DistanceMetric::Cosine).unwrap();
    index.insert(1, &[1.0, 0.0, 0.0]);

    // Act - Insert with same ID performs upsert (replaces the vector)
    index.insert(1, &[0.0, 1.0, 0.0]);

    // Assert
    assert_eq!(index.len(), 1); // Still only one entry

    // Verify the UPDATED vector is indexed (not the original)
    let results = index.search(&[0.0, 1.0, 0.0], 1);
    assert_eq!(results.len(), 1);
    assert_eq!(results[0].id, 1);
    // Score should be ~1.0 (only 1 vector in index, exact match expected)
    assert!(
        results[0].score > 0.9,
        "Updated vector should be indexed, got score {}",
        results[0].score,
    );
}

#[test]
fn test_hnsw_thread_safety() {
    use std::sync::Arc;
    use std::thread;

    // Arrange
    let index = Arc::new(HnswIndex::new(3, DistanceMetric::Cosine).unwrap());
    let mut handles = vec![];

    // Act - Insert from multiple threads (unique IDs)
    for i in 0..10 {
        let index_clone = Arc::clone(&index);
        handles.push(thread::spawn(move || {
            #[allow(clippy::cast_precision_loss)]
            index_clone.insert(i, &[i as f32, 0.0, 0.0]);
        }));
    }

    for handle in handles {
        handle.join().expect("Thread panicked");
    }

    // Set searching mode after parallel insertions (required by hnsw_rs)
    index.set_searching_mode();

    // Assert
    assert_eq!(index.len(), 10);
}

#[test]
fn test_hnsw_persistence() {
    use tempfile::tempdir;

    // Arrange
    let dir = tempdir().unwrap();
    let index = HnswIndex::new(3, DistanceMetric::Cosine).unwrap();
    index.insert(1, &[1.0, 0.0, 0.0]);
    index.insert(2, &[0.0, 1.0, 0.0]);

    // Act - Save
    index.save(dir.path()).unwrap();

    // Act - Load
    let loaded_index = HnswIndex::load(dir.path(), 3, DistanceMetric::Cosine).unwrap();

    // Assert
    assert_eq!(loaded_index.len(), 2);
    assert_eq!(loaded_index.dimension(), 3);
    assert_eq!(loaded_index.metric(), DistanceMetric::Cosine);

    // Verify search works on loaded index
    let results = loaded_index.search(&[1.0, 0.0, 0.0], 1);
    assert_eq!(results.len(), 1);
    assert_eq!(results[0].id, 1);
}

#[test]
fn test_hnsw_load_legacy_snapshot_without_vectors_disables_vacuum() {
    use std::fs;
    use tempfile::tempdir;

    // Arrange: create a valid snapshot then remove vector sidecar to simulate legacy format.
    let dir = tempdir().unwrap();
    let index = HnswIndex::new(3, DistanceMetric::Cosine).unwrap();
    index.insert(1, &[1.0, 0.0, 0.0]);
    index.insert(2, &[0.0, 1.0, 0.0]);
    index.save(dir.path()).unwrap();
    fs::remove_file(dir.path().join("native_vectors.bin")).unwrap();

    // Act: load snapshot missing vectors.
    let loaded_index = HnswIndex::load(dir.path(), 3, DistanceMetric::Cosine).unwrap();

    // Assert: keep queryability but block vacuum that would otherwise rebuild from missing vectors.
    assert_eq!(loaded_index.len(), 2);
    assert!(!loaded_index.has_vector_storage());
    assert_eq!(
        loaded_index.vacuum(),
        Err(VacuumError::VectorStorageDisabled)
    );
    assert_eq!(loaded_index.len(), 2);
}

#[test]
fn test_hnsw_fast_insert_save_does_not_persist_vectors_file() {
    use tempfile::tempdir;

    let dir = tempdir().unwrap();
    let index = HnswIndex::new_fast_insert(3, DistanceMetric::Cosine).unwrap();
    index.insert(1, &[1.0, 0.0, 0.0]);
    index.insert(2, &[0.0, 1.0, 0.0]);

    index.save(dir.path()).unwrap();
    assert!(!dir.path().join("native_vectors.bin").exists());

    let loaded = HnswIndex::load(dir.path(), 3, DistanceMetric::Cosine).unwrap();
    assert!(!loaded.has_vector_storage());
}

#[test]
fn test_hnsw_fast_insert_save_removes_stale_vectors_file() {
    use tempfile::tempdir;

    let dir = tempdir().unwrap();

    // Write a regular snapshot first (with vectors sidecar).
    let regular = HnswIndex::new(3, DistanceMetric::Cosine).unwrap();
    regular.insert(1, &[1.0, 0.0, 0.0]);
    regular.save(dir.path()).unwrap();
    assert!(dir.path().join("native_vectors.bin").exists());

    // Overwrite with fast-insert snapshot; stale vectors file must be removed.
    let fast = HnswIndex::new_fast_insert(3, DistanceMetric::Cosine).unwrap();
    fast.insert(2, &[0.0, 1.0, 0.0]);
    fast.save(dir.path()).unwrap();

    assert!(!dir.path().join("native_vectors.bin").exists());
}

#[test]
fn test_hnsw_insert_batch_parallel() {
    // Arrange
    let index = HnswIndex::new(3, DistanceMetric::Cosine).unwrap();
    let vectors: Vec<(u64, Vec<f32>)> = vec![
        (1, vec![1.0, 0.0, 0.0]),
        (2, vec![0.0, 1.0, 0.0]),
        (3, vec![0.0, 0.0, 1.0]),
        (4, vec![0.5, 0.5, 0.0]),
        (5, vec![0.5, 0.0, 0.5]),
    ];

    // Act
    let inserted = index.insert_batch_parallel(vectors.iter().map(|(id, v)| (*id, v.as_slice())));
    index.set_searching_mode();

    // Assert
    assert_eq!(inserted, 5);
    assert_eq!(index.len(), 5);

    // Verify search works
    let results = index.search(&[1.0, 0.0, 0.0], 3);
    assert_eq!(results.len(), 3);
    // ID 1 should be in the top results (exact match)
    // Note: Due to parallel insertion, graph structure may vary
    let result_ids: Vec<u64> = results.iter().map(|r| r.id).collect();
    assert!(result_ids.contains(&1), "ID 1 should be in top 3 results");
}

#[test]
fn test_hnsw_insert_batch_parallel_upserts_duplicates() {
    // Arrange
    let index = HnswIndex::new(3, DistanceMetric::Cosine).unwrap();

    // Insert one vector first
    index.insert(1, &[1.0, 0.0, 0.0]);

    // Act - Insert batch with existing ID (upsert) + new ID
    let vectors: Vec<(u64, Vec<f32>)> = vec![
        (1, vec![0.0, 1.0, 0.0]), // Upsert: updates existing id=1
        (2, vec![0.0, 0.0, 1.0]), // New
    ];
    let inserted = index.insert_batch_parallel(vectors.iter().map(|(id, v)| (*id, v.as_slice())));
    index.set_searching_mode();

    // Assert - Both vectors processed (1 upserted + 1 new)
    assert_eq!(inserted, 2);
    assert_eq!(index.len(), 2);
}

#[test]
fn test_hnsw_insert_batch_parallel_empty() {
    // Arrange
    let index = HnswIndex::new(3, DistanceMetric::Cosine).unwrap();
    let vectors: Vec<(u64, &[f32])> = vec![];

    // Act
    let inserted = index.insert_batch_parallel(vectors);

    // Assert
    assert_eq!(inserted, 0);
    assert!(index.is_empty());
}

#[test]
fn test_hnsw_insert_batch_parallel_wrong_dimension_returns_zero() {
    // Arrange
    let index = HnswIndex::new(3, DistanceMetric::Cosine).unwrap();
    let vectors: Vec<(u64, &[f32])> = vec![(1, &[1.0, 0.0])]; // Wrong dim

    // Act - dimension mismatch returns 0 inserted
    let inserted = index.insert_batch_parallel(vectors);
    assert_eq!(inserted, 0, "Wrong-dimension batch should insert nothing");
    assert_eq!(index.len(), 0, "Index should remain empty");
}

// =========================================================================
// HnswIndex with Params Tests
// Note: HnswParams unit tests are in params.rs
// =========================================================================

#[test]
fn test_hnsw_with_params() {
    let params = HnswParams::custom(48, 600, 500_000);
    let index = HnswIndex::with_params(1536, DistanceMetric::Cosine, params).unwrap();

    assert_eq!(index.dimension(), 1536);
    assert!(index.is_empty());
}

// =========================================================================
// SIMD Re-ranking Tests (TDD - RED phase)
// =========================================================================

#[test]
fn test_search_with_rerank_returns_k_results() {
    // Arrange
    let index = HnswIndex::new(3, DistanceMetric::Cosine).unwrap();
    index.insert(1, &[1.0, 0.0, 0.0]);
    index.insert(2, &[0.9, 0.1, 0.0]);
    index.insert(3, &[0.8, 0.2, 0.0]);
    index.insert(4, &[0.0, 1.0, 0.0]);
    index.insert(5, &[0.0, 0.0, 1.0]);

    // Act
    let results = index.search_with_rerank(&[1.0, 0.0, 0.0], 3, 5).unwrap();

    // Assert
    assert_eq!(results.len(), 3, "Should return exactly k results");
}

#[test]
#[allow(clippy::cast_precision_loss)]
fn test_search_with_rerank_improves_ranking() {
    // Arrange - vectors with subtle differences
    let index = HnswIndex::new(128, DistanceMetric::Cosine).unwrap();

    // Create vectors with known similarity ordering
    let base: Vec<f32> = (0..128).map(|i| (i as f32 * 0.01).sin()).collect();

    // Slightly modified versions
    let mut v1 = base.clone();
    v1[0] += 0.001; // Very similar

    let mut v2 = base.clone();
    v2[0] += 0.01; // Less similar

    let mut v3 = base.clone();
    v3[0] += 0.1; // Even less similar

    index.insert(1, &v1);
    index.insert(2, &v2);
    index.insert(3, &v3);

    // Act
    let results = index.search_with_rerank(&base, 3, 3).unwrap();

    // Assert - ID 1 should be closest (highest similarity)
    assert_eq!(results[0].id, 1, "Most similar vector should be first");
}

#[test]
fn test_search_with_rerank_handles_rerank_k_greater_than_index_size() {
    // Arrange - use more vectors to avoid HNSW flakiness
    let index = HnswIndex::new(3, DistanceMetric::Cosine).unwrap();
    index.insert(1, &[1.0, 0.0, 0.0]);
    index.insert(2, &[0.0, 1.0, 0.0]);
    index.insert(3, &[0.0, 0.0, 1.0]);
    index.insert(4, &[0.5, 0.5, 0.0]);
    index.insert(5, &[0.5, 0.0, 0.5]);

    // Act - rerank_k > index size
    let results = index.search_with_rerank(&[1.0, 0.0, 0.0], 3, 100).unwrap();

    // Assert - should return at least some results
    assert!(!results.is_empty(), "Should return results");
    assert!(results.len() <= 5, "Should not exceed index size");
}

#[test]
#[allow(clippy::cast_precision_loss, clippy::cast_sign_loss)]
fn test_search_with_rerank_uses_simd_distances() {
    // Arrange
    let index = HnswIndex::new(768, DistanceMetric::Cosine).unwrap();

    // Insert 100 vectors
    for i in 0..100_u64 {
        let v: Vec<f32> = (0..768)
            .map(|j| ((i + j as u64) as f32 * 0.01).sin())
            .collect();
        index.insert(i, &v);
    }

    let query: Vec<f32> = (0..768).map(|j| (j as f32 * 0.01).sin()).collect();

    // Act
    let results = index.search_with_rerank(&query, 10, 50).unwrap();

    // Assert - results should have valid distances (SIMD computed)
    // Note: HNSW may return fewer results if graph not fully connected
    assert!(!results.is_empty(), "Should return at least one result");
    for sr in &results {
        assert!(
            sr.score >= -1.0 && sr.score <= 1.0,
            "Cosine should be in [-1, 1]"
        );
    }

    // Results should be sorted by similarity (descending for cosine)
    for i in 1..results.len() {
        assert!(
            results[i - 1].score >= results[i].score,
            "Results should be sorted by similarity descending"
        );
    }
}

#[test]
fn test_search_with_rerank_euclidean_metric() {
    // Arrange
    let index = HnswIndex::new(3, DistanceMetric::Euclidean).unwrap();
    index.insert(1, &[0.0, 0.0, 0.0]);
    index.insert(2, &[1.0, 0.0, 0.0]);
    index.insert(3, &[2.0, 0.0, 0.0]);

    // Act
    let results = index.search_with_rerank(&[0.0, 0.0, 0.0], 3, 3).unwrap();

    // Assert - ID 1 should be closest (smallest distance)
    assert_eq!(results[0].id, 1, "Origin should be closest to itself");
    // For euclidean, smaller is better - results sorted ascending
    for i in 1..results.len() {
        assert!(
            results[i - 1].score <= results[i].score,
            "Euclidean results should be sorted ascending"
        );
    }
}

// =========================================================================
// WIS-8: Memory Leak Fix Tests
// Tests for multi-tenant scenarios and proper Drop behavior
// =========================================================================

#[test]
#[allow(
    clippy::cast_precision_loss,
    clippy::cast_sign_loss,
    clippy::uninlined_format_args
)]
fn test_hnsw_multi_tenant_load_unload() {
    // Arrange - Simulate multi-tenant scenario with multiple load/unload cycles
    // This test verifies that indices can be loaded and dropped without memory leak
    use tempfile::tempdir;

    let dir = tempdir().expect("Failed to create temp dir");

    // Create and save an index
    {
        let index = HnswIndex::new(128, DistanceMetric::Cosine).unwrap();
        for i in 0..100_u64 {
            let v: Vec<f32> = (0..128)
                .map(|j| ((i + j as u64) as f32 * 0.01).sin())
                .collect();
            index.insert(i, &v);
        }
        index.save(dir.path()).expect("Failed to save index");
    }

    // Act - Load and drop multiple times (simulates multi-tenant load/unload)
    for iteration in 0..5 {
        let loaded =
            HnswIndex::load(dir.path(), 128, DistanceMetric::Cosine).expect("Failed to load index");

        // Verify index works correctly
        assert_eq!(
            loaded.len(),
            100,
            "Iteration {}: Should have 100 vectors",
            iteration
        );

        let query: Vec<f32> = (0..128).map(|j| (j as f32 * 0.01).sin()).collect();
        let results = loaded.search(&query, 5);
        // HNSW may return fewer than k results depending on graph connectivity
        assert!(
            !results.is_empty() && results.len() <= 5,
            "Iteration {}: Should return 1-5 results, got {}",
            iteration,
            results.len()
        );

        // Index is dropped here, io_holder should be freed
    }

    // If we get here without crash/hang, memory is being managed correctly
}

#[test]
fn test_hnsw_drop_cleans_up_properly() {
    // Arrange - Create index, verify it can be dropped without issues
    use tempfile::tempdir;

    let dir = tempdir().expect("Failed to create temp dir");

    // Create, save, load, and drop
    {
        let index = HnswIndex::new(64, DistanceMetric::Euclidean).unwrap();
        index.insert(1, &vec![0.5; 64]);
        index.insert(2, &vec![0.3; 64]);
        index.save(dir.path()).expect("Failed to save");
    }

    // Load and immediately drop
    {
        let _loaded =
            HnswIndex::load(dir.path(), 64, DistanceMetric::Euclidean).expect("Failed to load");
        // Dropped here
    }

    // Load again to verify files are still valid after previous drop
    {
        let loaded = HnswIndex::load(dir.path(), 64, DistanceMetric::Euclidean)
            .expect("Failed to load after previous drop");
        assert_eq!(loaded.len(), 2);
    }
}

#[test]
#[allow(clippy::cast_precision_loss, clippy::uninlined_format_args)]
fn test_hnsw_save_load_preserves_all_metrics() {
    use tempfile::tempdir;

    // Test Cosine and Euclidean metrics
    // Note: DotProduct has numerical precision issues in hnsw_rs with certain vectors
    for metric in [DistanceMetric::Cosine, DistanceMetric::Euclidean] {
        let dir = tempdir().expect("Failed to create temp dir");
        let dim = 32;

        // Create varied vectors (not constant) to avoid numerical issues
        let v1: Vec<f32> = (0..dim).map(|i| (i as f32 * 0.1).sin()).collect();
        let v2: Vec<f32> = (0..dim).map(|i| (i as f32 * 0.2).cos()).collect();
        let query: Vec<f32> = (0..dim).map(|i| (i as f32 * 0.15).sin()).collect();

        // Create and save
        {
            let index = HnswIndex::new(dim, metric).unwrap();
            index.insert(1, &v1);
            index.insert(2, &v2);
            index.save(dir.path()).expect("Failed to save");
        }

        // Load and verify
        {
            let loaded = HnswIndex::load(dir.path(), dim, metric).expect("Failed to load");
            assert_eq!(
                loaded.len(),
                2,
                "Metric {:?}: Should have 2 vectors",
                metric
            );
            assert_eq!(loaded.metric(), metric, "Metric should be preserved");
            assert_eq!(loaded.dimension(), dim, "Dimension should be preserved");

            // Verify search works
            let results = loaded.search(&query, 2);
            assert!(
                !results.is_empty(),
                "Metric {:?}: Should return results",
                metric
            );
        }
    }
}

// =========================================================================
// SearchQuality Tests
// =========================================================================

#[test]
fn test_search_quality_fast() {
    let index = HnswIndex::new(3, DistanceMetric::Cosine).unwrap();
    // Insert more vectors for stable HNSW graph (small graphs are non-deterministic)
    index.insert(1, &[1.0, 0.0, 0.0]);
    index.insert(2, &[0.9, 0.1, 0.0]);
    index.insert(3, &[0.8, 0.2, 0.0]);
    index.insert(4, &[0.7, 0.3, 0.0]);
    index.insert(5, &[0.0, 1.0, 0.0]);

    let results = index
        .search_with_quality(&[1.0, 0.0, 0.0], 2, SearchQuality::Fast)
        .unwrap();
    // Fast mode may return fewer results with very small ef_search
    assert!(!results.is_empty(), "Should return at least one result");
    assert!(results.len() <= 2, "Should not exceed requested k");
}

#[test]
fn test_search_quality_balanced() {
    let index = HnswIndex::new(3, DistanceMetric::Cosine).unwrap();
    index.insert(1, &[1.0, 0.0, 0.0]);
    index.insert(2, &[0.9, 0.1, 0.0]);

    // Test Balanced quality mode
    let results = index
        .search_with_quality(&[1.0, 0.0, 0.0], 2, SearchQuality::Balanced)
        .unwrap();
    // HNSW may return fewer results for very small indices
    assert!(!results.is_empty(), "Should return at least one result");
    assert_eq!(
        results[0].id, 1,
        "Balanced search should find exact match first"
    );
}

#[test]
fn test_search_quality_custom_ef() {
    // Use more vectors to make HNSW more stable
    let index = HnswIndex::new(3, DistanceMetric::Cosine).unwrap();
    index.insert(1, &[1.0, 0.0, 0.0]);
    index.insert(2, &[0.9, 0.1, 0.0]);
    index.insert(3, &[0.8, 0.2, 0.0]);
    index.insert(4, &[0.0, 1.0, 0.0]);
    index.insert(5, &[0.0, 0.0, 1.0]);

    let results = index
        .search_with_quality(&[1.0, 0.0, 0.0], 3, SearchQuality::Custom(512))
        .unwrap();
    assert_eq!(results.len(), 3);
}

// Note: SearchQuality::ef_search unit tests are in params.rs

// =========================================================================
// Edge Cases and Error Handling
// =========================================================================

#[test]
fn test_hnsw_load_nonexistent_path() {
    let result = HnswIndex::load("nonexistent_path_12345", 128, DistanceMetric::Cosine);
    assert!(result.is_err(), "Loading from nonexistent path should fail");
}

#[test]
fn test_hnsw_search_with_rerank_empty_index() {
    let index = HnswIndex::new(3, DistanceMetric::Cosine).unwrap();
    let results = index.search_with_rerank(&[1.0, 0.0, 0.0], 10, 50).unwrap();
    assert!(
        results.is_empty(),
        "Empty index should return empty results"
    );
}

#[test]
fn test_hnsw_search_with_rerank_dot_product() {
    let index = HnswIndex::new(3, DistanceMetric::DotProduct).unwrap();
    index.insert(1, &[1.0, 0.0, 0.0]);
    index.insert(2, &[0.5, 0.5, 0.0]);
    index.insert(3, &[0.0, 1.0, 0.0]);

    let results = index.search_with_rerank(&[1.0, 0.0, 0.0], 3, 3).unwrap();

    // HNSW may return fewer results for very small indices
    assert!(!results.is_empty(), "Should return at least one result");
    // For dot product, ID 1 should have highest score
    assert_eq!(results[0].id, 1, "Highest dot product should be first");
}

#[test]
fn test_hnsw_io_holder_is_none_for_new_index() {
    // For newly created indices, io_holder should be None
    let index = HnswIndex::new(3, DistanceMetric::Cosine).unwrap();
    // We can't directly access io_holder, but we can verify the index works
    // and drops without issues (no io_holder to manage)
    index.insert(1, &[1.0, 0.0, 0.0]);
    assert_eq!(index.len(), 1);
    // Dropped here without io_holder cleanup needed
}

#[test]
#[allow(clippy::cast_precision_loss, clippy::cast_sign_loss)]
fn test_hnsw_large_batch_parallel_insert() {
    let index = HnswIndex::new(128, DistanceMetric::Cosine).unwrap();

    // Create 200 vectors (reduced from 1000 for faster test execution)
    let vectors: Vec<(u64, Vec<f32>)> = (0..200)
        .map(|i| {
            let v: Vec<f32> = (0..128).map(|j| ((i + j) as f32 * 0.001).sin()).collect();
            (i as u64, v)
        })
        .collect();

    let inserted = index.insert_batch_parallel(vectors.iter().map(|(id, v)| (*id, v.as_slice())));
    index.set_searching_mode();

    assert_eq!(inserted, 200, "Should insert 200 vectors");
    assert_eq!(index.len(), 200);

    // Verify search works
    let query: Vec<f32> = (0..128).map(|j| (j as f32 * 0.001).sin()).collect();
    let results = index.search(&query, 10);
    assert_eq!(results.len(), 10);
}

// =========================================================================
// TS-CORE-001: Adaptive Prefetch Tests
// =========================================================================

#[test]
#[allow(clippy::cast_precision_loss, clippy::cast_sign_loss)]
fn test_search_with_rerank_768d_prefetch() {
    // Test adaptive prefetch for 768D vectors (3KB each)
    // prefetch_distance should be 768*4/64 = 48, clamped to 16
    let index = HnswIndex::new(768, DistanceMetric::Cosine).unwrap();

    // Insert 100 vectors
    for i in 0u64..100 {
        let v: Vec<f32> = (0..768)
            .map(|j| ((i + j as u64) as f32 * 0.001).sin())
            .collect();
        index.insert(i, &v);
    }

    let query: Vec<f32> = (0..768).map(|j| (j as f32 * 0.001).sin()).collect();
    let results = index.search_with_rerank(&query, 10, 50).unwrap();

    assert!(!results.is_empty(), "Should return results");
    assert!(results.len() <= 10, "Should not exceed k");
}

#[test]
#[allow(clippy::cast_precision_loss, clippy::cast_sign_loss)]
fn test_search_with_rerank_small_dim_prefetch() {
    // Test adaptive prefetch for small vectors (32D = 128 bytes)
    // prefetch_distance should be 128/64 = 2, clamped to 4 (minimum)
    let index = HnswIndex::new(32, DistanceMetric::Cosine).unwrap();

    for i in 0u64..50 {
        let v: Vec<f32> = (0..32)
            .map(|j| ((i + j as u64) as f32 * 0.01).sin())
            .collect();
        index.insert(i, &v);
    }

    let query: Vec<f32> = (0..32).map(|j| (j as f32 * 0.01).sin()).collect();
    let results = index.search_with_rerank(&query, 5, 20).unwrap();

    assert!(!results.is_empty(), "Should return results");
}

// =========================================================================
// TS-CORE-002: Batch Search Optimization Tests
// =========================================================================

#[test]
#[allow(clippy::cast_precision_loss, clippy::cast_sign_loss)]
fn test_search_batch_parallel_consistency() {
    let index = HnswIndex::new(64, DistanceMetric::Cosine).unwrap();

    // Insert 100 vectors (reduced from 200 for faster test execution)
    for i in 0u64..100 {
        let v: Vec<f32> = (0..64)
            .map(|j| ((i + j as u64) as f32 * 0.01).sin())
            .collect();
        index.insert(i, &v);
    }

    // Create batch queries
    let queries: Vec<Vec<f32>> = (0..10)
        .map(|i| {
            (0..64)
                .map(|j| ((200 + i + j) as f32 * 0.01).sin())
                .collect()
        })
        .collect();
    let query_refs: Vec<&[f32]> = queries.iter().map(Vec::as_slice).collect();

    // Batch search
    let batch_results = index
        .search_batch_parallel(&query_refs, 5, SearchQuality::Balanced)
        .unwrap();

    // Individual searches for comparison
    let individual_results: Vec<Vec<crate::scored_result::ScoredResult>> = queries
        .iter()
        .map(|q| {
            index
                .search_with_quality(q, 5, SearchQuality::Balanced)
                .unwrap()
        })
        .collect();

    // Results should match (same IDs, though order might vary slightly)
    assert_eq!(batch_results.len(), individual_results.len());
    for (batch, individual) in batch_results.iter().zip(&individual_results) {
        assert_eq!(batch.len(), individual.len(), "Result counts should match");
    }
}

#[test]
fn test_search_batch_parallel_empty_queries() {
    let index = HnswIndex::new(3, DistanceMetric::Cosine).unwrap();
    index.insert(1, &[1.0, 0.0, 0.0]);

    let queries: Vec<&[f32]> = vec![];
    let results = index
        .search_batch_parallel(&queries, 5, SearchQuality::Fast)
        .unwrap();

    assert!(
        results.is_empty(),
        "Empty queries should return empty results"
    );
}

#[test]
#[allow(clippy::cast_precision_loss, clippy::cast_sign_loss)]
fn test_search_batch_parallel_large_batch() {
    let index = HnswIndex::new(128, DistanceMetric::Cosine).unwrap();

    // Insert 150 vectors (reduced from 500 for faster test execution)
    for i in 0u64..150 {
        let v: Vec<f32> = (0..128)
            .map(|j| ((i + j as u64) as f32 * 0.001).sin())
            .collect();
        index.insert(i, &v);
    }
    index.set_searching_mode();

    // 20 queries batch (reduced from 100 for faster test execution)
    let queries: Vec<Vec<f32>> = (0..20)
        .map(|i| {
            (0..128)
                .map(|j| ((150 + i + j) as f32 * 0.001).sin())
                .collect()
        })
        .collect();
    let query_refs: Vec<&[f32]> = queries.iter().map(Vec::as_slice).collect();

    // Use Balanced for faster test execution
    let results = index
        .search_batch_parallel(&query_refs, 10, SearchQuality::Balanced)
        .unwrap();

    assert_eq!(results.len(), 20, "Should return 20 result sets");
    for result in &results {
        assert_eq!(result.len(), 10, "Each result should have 10 neighbors");
    }
}

// =========================================================================
// Recall Quality Regression Tests
// =========================================================================

#[test]
#[allow(clippy::cast_precision_loss)]
fn test_recall_quality_minimum_threshold() {
    // Ensure recall@10 >= 90% for Accurate quality on small dataset
    let dim = 64;
    let n = 500;
    let k = 10;

    let index = HnswIndex::new(dim, DistanceMetric::Cosine).unwrap();

    // Generate deterministic dataset
    let dataset: Vec<Vec<f32>> = (0..n)
        .map(|i| {
            (0..dim)
                .map(|j| ((i * dim + j) as f32 * 0.001).sin())
                .collect()
        })
        .collect();

    for (idx, vec) in dataset.iter().enumerate() {
        #[allow(clippy::cast_possible_truncation)]
        index.insert(idx as u64, vec);
    }

    // Generate query
    let query: Vec<f32> = (0..dim).map(|j| (j as f32 * 0.001).sin()).collect();

    // Compute ground truth with brute force
    let mut distances: Vec<crate::scored_result::ScoredResult> = dataset
        .iter()
        .enumerate()
        .map(|(idx, vec)| {
            let sim = crate::simd_native::cosine_similarity_native(&query, vec);
            #[allow(clippy::cast_possible_truncation)]
            crate::scored_result::ScoredResult::new(idx as u64, sim)
        })
        .collect();
    distances.sort_by(|a, b| {
        b.score
            .partial_cmp(&a.score)
            .unwrap_or(std::cmp::Ordering::Equal)
    });
    let ground_truth: Vec<u64> = distances.iter().take(k).map(|sr| sr.id).collect();

    // HNSW search
    let results = index
        .search_with_quality(&query, k, SearchQuality::Accurate)
        .unwrap();
    let result_ids: std::collections::HashSet<u64> = results.iter().map(|sr| sr.id).collect();
    let gt_set: std::collections::HashSet<u64> = ground_truth.iter().copied().collect();

    let recall = result_ids.intersection(&gt_set).count() as f64 / k as f64;

    assert!(
        recall >= 0.8,
        "Recall@{k} should be >= 80% for Accurate, got {:.1}%",
        recall * 100.0
    );
}

#[test]
fn test_rerank_latency_target_configuration_roundtrip() {
    let index = HnswIndex::new(32, DistanceMetric::Cosine).unwrap();
    assert_eq!(index.rerank_latency_target_us(), 0);

    index.set_rerank_latency_target_us(250);
    assert_eq!(index.rerank_latency_target_us(), 250);
}

#[test]
fn test_rerank_latency_ema_updates_after_two_stage_search() {
    let index = HnswIndex::new(64, DistanceMetric::Cosine).unwrap();
    index.set_rerank_latency_target_us(1);

    for i in 0u64..1500 {
        let v: Vec<f32> = (0..64)
            .map(|j| ((i * 5 + j as u64) as f32 * 0.0013).sin())
            .collect();
        index.insert(i, &v);
    }

    let query: Vec<f32> = (0..64).map(|j| (j as f32 * 0.009).cos()).collect();
    let _ = index
        .search_with_quality(&query, 20, SearchQuality::Accurate)
        .unwrap();

    assert!(index.rerank_latency_ema_us() > 0);
}

#[test]
fn test_update_rerank_latency_ema_large_current_does_not_overflow() {
    let index = HnswIndex::new(64, DistanceMetric::Cosine).unwrap();

    for i in 0u64..400 {
        let v: Vec<f32> = (0..64)
            .map(|j| ((i * 3 + j as u64) as f32 * 0.0021).sin())
            .collect();
        index.insert(i, &v);
    }

    index
        .rerank_latency_ema_us
        .store(u64::MAX, std::sync::atomic::Ordering::Relaxed);

    let query: Vec<f32> = (0..64).map(|j| (j as f32 * 0.011).cos()).collect();
    let results = index
        .search_with_quality(&query, 20, SearchQuality::Accurate)
        .unwrap();

    assert!(!results.is_empty());

    let ema = index.rerank_latency_ema_us();
    let expected_min = (u64::MAX / 10) * 7;
    assert!(ema >= expected_min, "EMA underflow/wrap detected: {ema}");
}

#[test]
fn test_search_with_quality_custom_ef_uses_high_recall_path_without_regression() {
    let index = HnswIndex::new(64, DistanceMetric::Cosine).unwrap();

    for i in 0u64..2000 {
        let v: Vec<f32> = (0..64)
            .map(|j| ((i + j as u64) as f32 * 0.001).sin())
            .collect();
        index.insert(i, &v);
    }

    let query: Vec<f32> = (0..64).map(|j| (j as f32 * 0.013).cos()).collect();
    let results = index
        .search_with_quality(&query, 20, SearchQuality::Custom(512))
        .unwrap();

    assert!(!results.is_empty());
    assert!(results.len() <= 20);
}

#[test]
fn test_search_with_quality_accurate_stays_stable_on_medium_dataset() {
    let index = HnswIndex::new(128, DistanceMetric::Cosine).unwrap();

    for i in 0u64..5000 {
        let v: Vec<f32> = (0..128)
            .map(|j| ((i * 3 + j as u64) as f32 * 0.0007).sin())
            .collect();
        index.insert(i, &v);
    }

    let query: Vec<f32> = (0..128).map(|j| (j as f32 * 0.007).sin()).collect();
    let results = index
        .search_with_quality(&query, 15, SearchQuality::Accurate)
        .unwrap();

    assert!(!results.is_empty());
    assert!(results.len() <= 15);
}

// =========================================================================
// RF-3: Tests for search_brute_force_buffered (buffer reuse optimization)
// =========================================================================

#[test]
fn test_brute_force_buffered_same_results_as_original() {
    let index = HnswIndex::new(32, DistanceMetric::Cosine).unwrap();

    // Insert vectors
    for i in 0u64..50 {
        let v: Vec<f32> = (0..32)
            .map(|j| ((i + j as u64) as f32 * 0.01).sin())
            .collect();
        index.insert(i, &v);
    }

    let query: Vec<f32> = (0..32).map(|j| (j as f32 * 0.02).cos()).collect();

    // Compare results
    let original = index.search_brute_force(&query, 10).unwrap();
    let buffered = index.search_brute_force_buffered(&query, 10).unwrap();

    assert_eq!(original.len(), buffered.len());
    for (orig, buf) in original.iter().zip(buffered.iter()) {
        assert_eq!(orig.id, buf.id, "IDs should match");
        assert!(
            (orig.score - buf.score).abs() < 1e-6,
            "Distances should match"
        );
    }
}

#[test]
fn test_brute_force_buffered_empty_index() {
    let index = HnswIndex::new(16, DistanceMetric::Euclidean).unwrap();
    let query: Vec<f32> = vec![0.0; 16];

    let results = index.search_brute_force_buffered(&query, 5).unwrap();
    assert!(results.is_empty());
}

#[test]
fn test_brute_force_buffered_all_metrics() {
    for metric in [
        DistanceMetric::Cosine,
        DistanceMetric::Euclidean,
        DistanceMetric::DotProduct,
        DistanceMetric::Hamming,
        DistanceMetric::Jaccard,
    ] {
        let index = HnswIndex::new(8, metric).unwrap();
        index.insert(1, &[1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]);
        index.insert(2, &[0.5, 0.5, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]);
        index.insert(3, &[0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]);

        let results = index
            .search_brute_force_buffered(&[1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], 3)
            .unwrap();
        assert_eq!(results.len(), 3, "Should return 3 results for {metric:?}");
    }
}

#[test]
fn test_brute_force_buffered_repeated_calls_stable() {
    let index = HnswIndex::new(16, DistanceMetric::Cosine).unwrap();

    for i in 0u64..20 {
        let v: Vec<f32> = (0..16)
            .map(|j| ((i + j as u64) as f32 * 0.1).sin())
            .collect();
        index.insert(i, &v);
    }

    let query: Vec<f32> = vec![0.5; 16];

    // Multiple calls should return identical results
    let r1 = index.search_brute_force_buffered(&query, 5).unwrap();
    let r2 = index.search_brute_force_buffered(&query, 5).unwrap();
    let r3 = index.search_brute_force_buffered(&query, 5).unwrap();

    assert_eq!(r1, r2);
    assert_eq!(r2, r3);
}

// =========================================================================
// Stress Tests
// =========================================================================

#[test]
#[allow(clippy::cast_precision_loss, clippy::cast_sign_loss)]
fn test_concurrent_search_stress() {
    use std::sync::Arc;
    use std::thread;

    let index = Arc::new(HnswIndex::new(64, DistanceMetric::Cosine).unwrap());

    // Insert vectors
    for i in 0u64..100 {
        let v: Vec<f32> = (0..64)
            .map(|j| ((i + j as u64) as f32 * 0.01).sin())
            .collect();
        index.insert(i, &v);
    }

    // Spawn multiple search threads
    let handles: Vec<_> = (0..4)
        .map(|t| {
            let idx = Arc::clone(&index);
            thread::spawn(move || {
                for i in 0..50 {
                    let query: Vec<f32> = (0..64)
                        .map(|j| ((t * 100 + i + j) as f32 * 0.01).sin())
                        .collect();
                    let results = idx.search(&query, 5);
                    assert!(!results.is_empty());
                }
            })
        })
        .collect();

    for handle in handles {
        handle.join().expect("Thread panicked");
    }
}

#[test]
fn test_all_distance_metrics_search_with_rerank() {
    for metric in [
        DistanceMetric::Cosine,
        DistanceMetric::Euclidean,
        DistanceMetric::DotProduct,
        DistanceMetric::Hamming,
        DistanceMetric::Jaccard,
    ] {
        let index = HnswIndex::new(8, metric).unwrap();
        index.insert(1, &[1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]);
        index.insert(2, &[0.5, 0.5, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]);
        index.insert(3, &[0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]);

        let results = index
            .search_with_rerank(&[1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], 3, 3)
            .unwrap();

        assert!(
            !results.is_empty(),
            "search_with_rerank should work for {metric:?}"
        );
    }
}

// =========================================================================
// SAFETY: Drop Order Tests for io_holder unsafe invariant
// =========================================================================
//
// These tests verify that the unsafe lifetime extension in HnswIndex::load()
// doesn't cause use-after-free when the index is dropped.
//
// CRITICAL INVARIANT: `inner` (which borrows from io_holder) MUST be dropped
// BEFORE `io_holder`. Our Drop impl ensures this via ManuallyDrop.

#[test]
fn test_drop_safety_loaded_index_no_segfault() {
    // This test verifies that dropping a loaded HnswIndex doesn't segfault.
    // If the Drop order is wrong, this will cause use-after-free.
    use tempfile::tempdir;

    let dir = tempdir().expect("Failed to create temp dir");

    // 1. Create and save an index
    {
        let index = HnswIndex::new(4, DistanceMetric::Cosine).unwrap();
        index.insert(1, &[1.0, 0.0, 0.0, 0.0]);
        index.insert(2, &[0.0, 1.0, 0.0, 0.0]);
        index.insert(3, &[0.0, 0.0, 1.0, 0.0]);
        index.save(dir.path()).expect("Failed to save");
    }

    // 2. Load and drop multiple times to stress test Drop safety
    for _ in 0..5 {
        let loaded =
            HnswIndex::load(dir.path(), 4, DistanceMetric::Cosine).expect("Failed to load");

        // Perform operations that touch the borrowed data
        let results = loaded.search(&[1.0, 0.0, 0.0, 0.0], 2);
        assert!(!results.is_empty(), "Search should return results");

        // Index is dropped here - if Drop order is wrong, this segfaults
    }
}

#[test]
fn test_drop_safety_loaded_index_concurrent_drop() {
    // Stress test: multiple threads loading and dropping indices
    use std::sync::Arc;
    use std::thread;
    use tempfile::tempdir;

    let dir = tempdir().expect("Failed to create temp dir");

    // Create and save an index
    {
        let index = HnswIndex::new(4, DistanceMetric::Cosine).unwrap();
        for i in 0u64..10 {
            let v = vec![i as f32, 0.0, 0.0, 0.0];
            index.insert(i, &v);
        }
        index.save(dir.path()).expect("Failed to save");
    }

    let path = Arc::new(dir.path().to_path_buf());

    // Spawn threads that load, search, and drop
    let handles: Vec<_> = (0..4)
        .map(|_| {
            let p = Arc::clone(&path);
            thread::spawn(move || {
                for _ in 0..3 {
                    let loaded =
                        HnswIndex::load(&*p, 4, DistanceMetric::Cosine).expect("Failed to load");
                    let results = loaded.search(&[1.0, 0.0, 0.0, 0.0], 3);
                    assert!(!results.is_empty());
                    // Drop happens here
                }
            })
        })
        .collect();

    for h in handles {
        h.join().expect("Thread should not panic from Drop");
    }
}

#[test]
fn test_drop_safety_search_after_partial_operations() {
    // Test that search works correctly even with complex operation sequences
    // before drop, ensuring borrowed data is valid until Drop.
    use tempfile::tempdir;

    let dir = tempdir().expect("Failed to create temp dir");

    // Create index with various operations
    {
        let index = HnswIndex::new(8, DistanceMetric::Euclidean).unwrap();
        for i in 0u64..20 {
            let v: Vec<f32> = (0..8).map(|j| (i + j) as f32 * 0.1).collect();
            index.insert(i, &v);
        }
        index.save(dir.path()).expect("Failed to save");
    }

    // Load and perform many operations before drop
    let loaded = HnswIndex::load(dir.path(), 8, DistanceMetric::Euclidean).expect("Failed to load");

    // Multiple searches touching the mmap'd data
    for i in 0..10 {
        let query: Vec<f32> = (0..8).map(|j| (i + j) as f32 * 0.1).collect();
        let results = loaded.search(&query, 5);
        assert!(results.len() <= 5);
    }

    // Batch search
    let queries: Vec<Vec<f32>> = (0..5)
        .map(|i| (0..8).map(|j| (i + j) as f32 * 0.1).collect())
        .collect();
    let query_refs: Vec<&[f32]> = queries.iter().map(|v| v.as_slice()).collect();
    let batch_results = loaded
        .search_batch_parallel(&query_refs, 3, SearchQuality::Balanced)
        .unwrap();
    assert_eq!(batch_results.len(), 5);

    // Drop happens here - all borrowed data must still be valid
    drop(loaded);
}

// =========================================================================
// SEC-1: Stress Test - Drop under heavy concurrent load
// Validates ManuallyDrop + RwLock safety under extreme conditions
// =========================================================================

#[test]
fn test_drop_stress_concurrent_create_destroy_loop() {
    // Stress test: rapidly create/destroy indices while performing operations
    // This tests the ManuallyDrop pattern under pressure
    use std::sync::atomic::{AtomicUsize, Ordering};
    use std::sync::Arc;

    let success_count = Arc::new(AtomicUsize::new(0));
    let iterations = 50;

    for _ in 0..iterations {
        let success = Arc::clone(&success_count);

        // Create index, perform operations, drop
        let index = Arc::new(HnswIndex::new(16, DistanceMetric::Cosine).unwrap());

        // Spawn readers that will race with drop
        let handles: Vec<_> = (0..4)
            .map(|t| {
                let idx = Arc::clone(&index);
                std::thread::spawn(move || {
                    // Insert some vectors
                    for i in 0..10 {
                        let id = (t * 100 + i) as u64;
                        let v: Vec<f32> = (0..16).map(|j| (id + j) as f32 * 0.01).collect();
                        idx.insert(id, &v);
                    }
                    // Search
                    let q: Vec<f32> = (0..16).map(|i| i as f32 * 0.01).collect();
                    let _ = idx.search(&q, 5);
                })
            })
            .collect();

        // Wait for all threads
        for h in handles {
            h.join().expect("Thread panicked during stress test");
        }

        // Force drop while ensuring all operations completed
        drop(index);
        success.fetch_add(1, Ordering::SeqCst);
    }

    assert_eq!(
        success_count.load(Ordering::SeqCst),
        iterations,
        "All iterations should complete without panic"
    );
}

#[test]
fn test_drop_stress_load_search_destroy_cycle() {
    // Stress test: load from disk, search heavily, destroy - repeated
    use tempfile::tempdir;

    let dir = tempdir().expect("Failed to create temp dir");

    // Create and save initial index
    {
        let index = HnswIndex::new(32, DistanceMetric::Euclidean).unwrap();
        for i in 0u64..100 {
            let v: Vec<f32> = (0..32).map(|j| ((i + j) as f32).sin()).collect();
            index.insert(i, &v);
        }
        index.save(dir.path()).expect("Failed to save");
    }

    // Repeated load/search/destroy cycles
    for cycle in 0..20 {
        let loaded = HnswIndex::load(dir.path(), 32, DistanceMetric::Euclidean)
            .unwrap_or_else(|e| panic!("Cycle {cycle}: Failed to load: {e}"));

        // Heavy search load
        for i in 0..50 {
            let q: Vec<f32> = (0..32).map(|j| ((i + j) as f32).cos()).collect();
            let results = loaded.search(&q, 10);
            assert!(
                results.len() <= 10,
                "Cycle {cycle}: Search returned too many results"
            );
        }

        // Explicit drop to test ManuallyDrop pattern
        drop(loaded);
    }
}

#[test]
fn test_drop_stress_parallel_insert_then_drop() {
    // Stress test: parallel batch insert immediately followed by drop
    // Use Euclidean to avoid cosine normalization requirements
    // Reduced iterations and batch size for faster test execution
    for _ in 0..5 {
        let index = HnswIndex::new(64, DistanceMetric::Euclidean).unwrap();

        // Generate batch data with reasonable magnitude (reduced from 500)
        let batch: Vec<(u64, Vec<f32>)> = (0..100)
            .map(|i| {
                let v: Vec<f32> = (0..64).map(|j| (i + j) as f32 * 0.01).collect();
                (i as u64, v)
            })
            .collect();

        // Parallel insert
        let inserted = index.insert_batch_parallel(batch.iter().map(|(id, v)| (*id, v.as_slice())));
        assert!(inserted > 0, "Should insert at least some vectors");

        // Immediate drop without set_searching_mode
        // This tests that Drop handles partially-initialized state
        drop(index);
    }
}

// =========================================================================
// P1-GPU-1: GPU Batch Search Tests (TDD - Written BEFORE implementation)
// =========================================================================

#[test]
#[cfg(feature = "gpu")]
fn test_search_brute_force_gpu_returns_same_results_as_cpu() {
    // TDD: GPU brute force must return identical results to CPU
    let index = HnswIndex::new(128, DistanceMetric::Cosine).unwrap();

    // Insert test vectors
    for i in 0u64..100 {
        let v: Vec<f32> = (0..128)
            .map(|j| ((i + j as u64) as f32 * 0.01).sin())
            .collect();
        index.insert(i, &v);
    }

    let query: Vec<f32> = (0..128).map(|j| (j as f32 * 0.02).cos()).collect();

    // CPU brute force
    let cpu_results = index.search_brute_force(&query, 10).unwrap();

    // GPU brute force (if available)
    if let Some(gpu_results) = index.search_brute_force_gpu(&query, 10).unwrap() {
        assert_eq!(
            cpu_results.len(),
            gpu_results.len(),
            "Result count mismatch"
        );

        // Verify same IDs returned (order may differ slightly due to floating point)
        let cpu_ids: std::collections::HashSet<u64> = cpu_results.iter().map(|sr| sr.id).collect();
        let gpu_ids: std::collections::HashSet<u64> = gpu_results.iter().map(|sr| sr.id).collect();

        let overlap = cpu_ids.intersection(&gpu_ids).count();
        assert!(
            overlap >= 8,
            "GPU and CPU should return mostly same IDs (got {overlap}/10 overlap)"
        );
    }
}

#[test]
fn test_search_brute_force_gpu_fallback_to_none_without_gpu() {
    // TDD: Without GPU, should return None gracefully
    let index = HnswIndex::new(64, DistanceMetric::Cosine).unwrap();
    index.insert(1, &vec![0.5; 64]);

    let query = vec![0.5; 64];

    // Should not panic, returns Ok(None) if GPU unavailable
    let result = index.search_brute_force_gpu(&query, 5).unwrap();

    #[cfg(not(feature = "gpu"))]
    assert!(result.is_none(), "Should return None without GPU feature");
    #[cfg(feature = "gpu")]
    let _ = result;
}

#[test]
#[cfg(feature = "gpu")]
fn test_brute_force_gpu_euclidean() {
    // RED: GPU brute-force must dispatch Euclidean, not hardcode Cosine.
    let index = HnswIndex::new(4, DistanceMetric::Euclidean).unwrap();

    // Insert vectors at known positions so distances are deterministic.
    // vec0 = origin, vec1 = unit along x, vec2 = 2 units along x.
    index.insert(10, &[0.0, 0.0, 0.0, 0.0]);
    index.insert(20, &[1.0, 0.0, 0.0, 0.0]);
    index.insert(30, &[2.0, 0.0, 0.0, 0.0]);

    let query = [0.0, 0.0, 0.0, 0.0]; // closest to vec0

    if let Some(results) = index.search_brute_force_gpu(&query, 3).unwrap() {
        assert_eq!(results.len(), 3, "Should return 3 results");

        // Euclidean: ascending order (lower distance = better).
        // Expected order: id=10 (dist 0), id=20 (dist 1), id=30 (dist 2).
        assert_eq!(results[0].id, 10, "Closest should be id=10 (at origin)");
        assert_eq!(results[1].id, 20, "Second closest should be id=20");
        assert_eq!(results[2].id, 30, "Farthest should be id=30");

        // Verify actual distances
        assert!(
            results[0].score.abs() < 0.01,
            "Distance to origin should be ~0, got {}",
            results[0].score
        );
        assert!(
            (results[1].score - 1.0).abs() < 0.01,
            "Distance to (1,0,0,0) should be ~1, got {}",
            results[1].score
        );
    }
}

#[test]
#[cfg(feature = "gpu")]
fn test_brute_force_gpu_dot_product() {
    // RED: GPU brute-force must dispatch DotProduct, not hardcode Cosine.
    let index = HnswIndex::new(4, DistanceMetric::DotProduct).unwrap();

    // Insert vectors with different dot products to the query.
    index.insert(10, &[1.0, 0.0, 0.0, 0.0]); // dot with query = 1
    index.insert(20, &[2.0, 0.0, 0.0, 0.0]); // dot with query = 2
    index.insert(30, &[3.0, 0.0, 0.0, 0.0]); // dot with query = 3

    let query = [1.0, 0.0, 0.0, 0.0];

    if let Some(results) = index.search_brute_force_gpu(&query, 3).unwrap() {
        assert_eq!(results.len(), 3, "Should return 3 results");

        // DotProduct: descending order (higher = better).
        // Expected order: id=30 (dot 3), id=20 (dot 2), id=10 (dot 1).
        assert_eq!(
            results[0].id, 30,
            "Highest dot product should be id=30, got id={}",
            results[0].id
        );
        assert_eq!(
            results[1].id, 20,
            "Second highest dot product should be id=20, got id={}",
            results[1].id
        );
        assert_eq!(
            results[2].id, 10,
            "Lowest dot product should be id=10, got id={}",
            results[2].id
        );

        // Verify actual scores
        assert!(
            (results[0].score - 3.0).abs() < 0.01,
            "Dot product with (3,0,0,0) should be ~3, got {}",
            results[0].score
        );
    }
}

#[test]
fn test_compute_backend_selection() {
    // TDD: Verify compute backend selection works
    use crate::gpu::ComputeBackend;

    let backend = ComputeBackend::best_available();

    // Should always return a valid backend
    match backend {
        ComputeBackend::Simd => {
            // SIMD is always available
        }
        #[cfg(feature = "gpu")]
        ComputeBackend::Gpu => {
            // GPU selected when available
        }
    }
}

// =========================================================================
// FT-2: Property-Based Tests with proptest
// =========================================================================

mod proptest_tests {
    use super::*;
    use proptest::prelude::*;

    /// Strategy for generating valid vector dimensions (reasonable range)
    fn dimension_strategy() -> impl Strategy<Value = usize> {
        8usize..=256
    }

    /// Strategy for generating a random f32 vector of given dimension
    #[allow(dead_code)]
    fn vector_strategy(dim: usize) -> impl Strategy<Value = Vec<f32>> {
        proptest::collection::vec(-1.0f32..1.0, dim)
    }

    proptest! {
        #![proptest_config(ProptestConfig::with_cases(50))]

        /// Property: len() always equals number of successful insertions
        #[test]
        fn prop_len_equals_insertions(
            dim in dimension_strategy(),
            vectors in proptest::collection::vec(
                proptest::collection::vec(-1.0f32..1.0, 8usize..=64),
                1usize..=20
            )
        ) {
            let index = HnswIndex::new(dim, DistanceMetric::Euclidean).unwrap();
            let mut inserted = 0usize;

            for (i, v) in vectors.into_iter().enumerate() {
                if v.len() == dim {
                    index.insert(i as u64, &v);
                    inserted += 1;
                }
            }

            prop_assert_eq!(index.len(), inserted);
        }

        /// Property: search never returns more than k results
        #[test]
        fn prop_search_returns_at_most_k(
            dim in 16usize..=64,
            k in 1usize..=20,
            num_vectors in 5usize..=50
        ) {
            let index = HnswIndex::new(dim, DistanceMetric::Euclidean).unwrap();

            // Insert random vectors
            for i in 0..num_vectors {
                let v: Vec<f32> = (0..dim).map(|j| ((i + j) as f32 * 0.01).sin()).collect();
                index.insert(i as u64, &v);
            }

            let query: Vec<f32> = (0..dim).map(|j| (j as f32 * 0.02).cos()).collect();
            let results = index.search(&query, k);

            prop_assert!(results.len() <= k, "Search returned {} results, expected <= {}", results.len(), k);
        }

        /// Property: brute force search always returns exact results
        #[test]
        fn prop_brute_force_exact(
            dim in 8usize..=32,
            num_vectors in 3usize..=20
        ) {
            let index = HnswIndex::new(dim, DistanceMetric::Euclidean).unwrap();

            // Insert vectors with known distances from origin
            for i in 0..num_vectors {
                let mut v = vec![0.0f32; dim];
                v[0] = i as f32; // Distance from origin = i
                index.insert(i as u64, &v);
            }

            let query = vec![0.0f32; dim];
            let results = index.search_brute_force(&query, 3).unwrap();

            // First result should be id=0 (exact match at origin)
            if !results.is_empty() {
                prop_assert_eq!(results[0].id, 0, "Closest should be id=0 (at origin)");
            }
        }

        /// Property: remove always decreases len or returns false
        #[test]
        fn prop_remove_decreases_len(
            dim in 16usize..=32,
            id_to_remove in 0u64..10
        ) {
            let index = HnswIndex::new(dim, DistanceMetric::Cosine).unwrap();

            // Insert some vectors
            for i in 0u64..10 {
                let v: Vec<f32> = (0..dim).map(|j| ((i + j as u64) as f32 * 0.01).sin()).collect();
                index.insert(i, &v);
            }

            let len_before = index.len();
            let removed = index.remove(id_to_remove);

            if removed {
                prop_assert_eq!(index.len(), len_before - 1);
            } else {
                prop_assert_eq!(index.len(), len_before);
            }
        }

        /// Property: duplicate inserts are idempotent (no increase in len)
        #[test]
        fn prop_duplicate_insert_idempotent(
            dim in 16usize..=32
        ) {
            let index = HnswIndex::new(dim, DistanceMetric::Euclidean).unwrap();
            let v: Vec<f32> = (0..dim).map(|j| j as f32 * 0.1).collect();

            index.insert(42, &v);
            let len_after_first = index.len();

            index.insert(42, &v); // Duplicate
            let len_after_second = index.len();

            prop_assert_eq!(len_after_first, len_after_second, "Duplicate insert should be idempotent");
        }

        /// Property: batch insert count matches individual inserts
        #[test]
        fn prop_batch_insert_count(
            dim in 16usize..=32,
            batch_size in 5usize..=30
        ) {
            let index = HnswIndex::new(dim, DistanceMetric::Euclidean).unwrap();

            let batch: Vec<(u64, Vec<f32>)> = (0..batch_size)
                .map(|i| {
                    let v: Vec<f32> = (0..dim).map(|j| ((i + j) as f32 * 0.01).sin()).collect();
                    (i as u64, v)
                })
                .collect();

            // Use parallel insert (recommended API)
            let count = index.insert_batch_parallel(batch.iter().map(|(id, v)| (*id, v.as_slice())));

            prop_assert_eq!(count, batch_size, "Batch insert count mismatch");
            prop_assert_eq!(index.len(), batch_size, "Index len mismatch after batch");
        }
    }
}

// =========================================================================
// P1: Safety invariant tests for self-referential pattern
// =========================================================================
// NOTE: test_field_order_io_holder_after_inner is in index.rs (requires private field access)

/// Test that `ManuallyDrop` is used correctly for the inner field.
///
/// This verifies that:
/// 1. The inner field uses `ManuallyDrop` (checked by compilation)
/// 2. The custom Drop impl is present and correct
#[test]
fn test_manuallydrop_pattern_integrity() {
    // Create an index and verify it can be dropped without issues
    let index = HnswIndex::new(64, DistanceMetric::Cosine).unwrap();

    // Insert some data to ensure internal state is populated
    for i in 0..10 {
        let v: Vec<f32> = (0..64).map(|j| (i + j) as f32 * 0.01).collect();
        index.insert(i as u64, &v);
    }

    // Explicit drop - if ManuallyDrop is incorrectly handled, this could panic/UB
    drop(index);

    // If we reach here, the drop order is correct
}

/// Test that loading from disk and dropping works correctly.
///
/// This is the actual use case where the self-referential pattern matters:
/// when loading from disk, `inner` borrows from `io_holder`.
#[test]
fn test_load_and_drop_safety() {
    use tempfile::TempDir;

    let temp_dir = TempDir::new().expect("Failed to create temp dir");
    let path = temp_dir.path();

    // Create, populate, and save an index
    {
        let index = HnswIndex::new(64, DistanceMetric::Cosine).unwrap();
        for i in 0..50 {
            let v: Vec<f32> = (0..64).map(|j| (i + j) as f32 * 0.01).collect();
            index.insert(i as u64, &v);
        }
        index.save(path).expect("Save failed");
    }

    // Load and drop multiple times to stress-test the drop order
    for _ in 0..3 {
        let loaded = HnswIndex::load(path, 64, DistanceMetric::Cosine).expect("Load failed");

        // Verify it works
        let results = loaded.search(&vec![0.0f32; 64], 5);
        assert!(!results.is_empty(), "Search should return results");

        // Drop happens here - critical that inner drops before io_holder
        drop(loaded);
    }
}

// =========================================================================
// Regression: adaptive search spread for distance metrics
// =========================================================================

/// Regression test: `search_adaptive` spread calculation must work correctly
/// for distance-based metrics (Euclidean, Hamming) where results are sorted
/// ascending (best = lowest score).
///
/// Before the fix, the spread formula `(max - min) / min.abs()` produced a
/// negative value for ascending-sorted results, which always compared < 2.0,
/// preventing escalation from ever triggering on distance metrics. The fix
/// uses `(score_a - score_b).abs() / min(|a|, |b|)` which is sign-agnostic.
#[test]
#[allow(clippy::cast_precision_loss)]
fn test_adaptive_search_spread_positive_for_distance_metrics() {
    // Euclidean: distance metric (lower = better, sorted ascending).
    // We need > 100 vectors so brute-force small-collection path is skipped,
    // and we need enough spread so the adaptive threshold (2.0) is exceeded.
    let dim = 32;
    let index = HnswIndex::new(dim, DistanceMetric::Euclidean).unwrap();

    // Insert 150 vectors: a tight cluster near the origin and outliers far away.
    // This creates high spread in search results, which should trigger escalation.
    for i in 0u64..120 {
        // Tight cluster: small perturbation around origin
        let v: Vec<f32> = (0..dim)
            .map(|j| ((i + j as u64) as f32 * 0.001).sin() * 0.1)
            .collect();
        index.insert(i, &v);
    }
    for i in 120u64..150 {
        // Outliers: far from origin (magnitude ~10)
        let v: Vec<f32> = (0..dim)
            .map(|j| 10.0 + (i + j as u64) as f32 * 0.05)
            .collect();
        index.insert(i, &v);
    }

    // Query near the cluster — first results will have low distances (~0.x),
    // last results high distances (~50+), creating a large spread.
    let query: Vec<f32> = vec![0.0; dim];

    // Adaptive search with a narrow min_ef and generous max_ef.
    // If spread calculation is correct and positive, the adaptive path will
    // either escalate or return valid results — either way, the search must
    // complete successfully and return sensible results.
    let results = index
        .search_with_quality(
            &query,
            10,
            SearchQuality::Adaptive {
                min_ef: 32,
                max_ef: 256,
            },
        )
        .unwrap();

    assert!(
        !results.is_empty(),
        "Adaptive Euclidean search should return results"
    );
    assert!(results.len() <= 10, "Should not exceed requested k");

    // Results must be sorted ascending (distance metric: lower = better).
    for pair in results.windows(2) {
        assert!(
            pair[0].score <= pair[1].score + f32::EPSILON,
            "Euclidean results must be sorted ascending: {} > {}",
            pair[0].score,
            pair[1].score,
        );
    }

    // The closest vectors should come from the tight cluster (ids 0..120),
    // not the outliers (ids 120..150).
    let closest_id = results[0].id;
    assert!(
        closest_id < 120,
        "Closest result should be from the tight cluster, got id={closest_id}"
    );
}

/// Regression test: adaptive search works for similarity metrics (Cosine)
/// with spread results — verifying the fix did not regress the happy path.
#[test]
#[allow(clippy::cast_precision_loss)]
fn test_adaptive_search_spread_works_for_similarity_metrics() {
    let dim = 32;
    let index = HnswIndex::new(dim, DistanceMetric::Cosine).unwrap();

    // 150 vectors with varying similarity to the query
    for i in 0u64..150 {
        let v: Vec<f32> = (0..dim)
            .map(|j| ((i * 7 + j as u64) as f32 * 0.013).sin())
            .collect();
        index.insert(i, &v);
    }

    let query: Vec<f32> = (0..dim).map(|j| (j as f32 * 0.013).sin()).collect();

    let results = index
        .search_with_quality(
            &query,
            10,
            SearchQuality::Adaptive {
                min_ef: 32,
                max_ef: 256,
            },
        )
        .unwrap();

    assert!(
        !results.is_empty(),
        "Adaptive Cosine search should return results"
    );
    assert!(results.len() <= 10, "Should not exceed requested k");

    // Results must be sorted descending (similarity metric: higher = better).
    for pair in results.windows(2) {
        assert!(
            pair[0].score >= pair[1].score - f32::EPSILON,
            "Cosine results must be sorted descending: {} < {}",
            pair[0].score,
            pair[1].score,
        );
    }
}

// -------------------------------------------------------------------------
// Upsert Semantics Tests (Issue #371)
// -------------------------------------------------------------------------

#[test]
fn test_insert_same_id_updates_vector() {
    // Arrange: create index with vector storage enabled (default)
    let index = HnswIndex::new(4, DistanceMetric::Cosine).unwrap();

    // Insert id=1 with vector A (pointing along x-axis)
    let vector_a = [1.0, 0.0, 0.0, 0.0];
    index.insert(1, &vector_a);

    // Act: insert id=1 again with vector B (pointing along y-axis, orthogonal to A)
    let vector_b = [0.0, 1.0, 0.0, 0.0];
    index.insert(1, &vector_b);

    // Assert 1: index length must still be 1 (not 2)
    assert_eq!(index.len(), 1, "Upsert must not create duplicate entries");

    // Assert 2: search with query=B should return id=1 with high similarity
    let results = index.search(&vector_b, 1);
    assert_eq!(results.len(), 1, "Should find exactly one result");
    assert_eq!(results[0].id, 1, "Result must be id=1");
    assert!(
        results[0].score > 0.9,
        "Similarity to updated vector B should be > 0.9, got {}",
        results[0].score,
    );
}

// -------------------------------------------------------------------------
// Upsert + Tombstone / Vacuum TDD Cycle 3 Tests
// -------------------------------------------------------------------------

#[test]
fn test_upsert_tombstone_accumulation() {
    // Arrange: 10-dim index so each of 10 IDs gets a unique dominant dimension
    let dim = 10;
    let index = HnswIndex::new(dim, DistanceMetric::Cosine).unwrap();

    // Insert 10 vectors (generation 0)
    for i in 0..10_usize {
        index.insert(i as u64, &make_dominant_vector(dim, i, 0));
    }

    // Assert: no tombstones, 10 active entries
    assert_eq!(
        index.tombstone_count(),
        0,
        "Fresh index should have 0 tombstones"
    );
    assert_eq!(index.len(), 10, "Should have 10 active vectors");

    // Act: update all 10 vectors with new values (generation 1)
    for i in 0..10_usize {
        index.insert(i as u64, &make_dominant_vector(dim, i, 1));
    }

    // Assert: 10 tombstones (old slots are dead), still 10 active
    assert_eq!(
        index.tombstone_count(),
        10,
        "Updating 10 vectors should leave 10 tombstones",
    );
    assert_eq!(index.len(), 10, "Active count must remain 10 after upserts");

    // Tombstone ratio = 10 / 20 = 0.50 => needs_vacuum (threshold 0.20)
    assert!(
        index.needs_vacuum(),
        "Tombstone ratio {:.2} should exceed 0.20 threshold",
        index.tombstone_ratio(),
    );
}

#[test]
fn test_upsert_then_vacuum_cleans() {
    // Arrange: 10-dim index so each of 10 IDs has a unique dominant dimension
    let dim = 10;
    let index = HnswIndex::new(dim, DistanceMetric::Cosine).unwrap();

    // Insert 10 vectors (generation 0)
    for i in 0..10_usize {
        index.insert(i as u64, &make_dominant_vector(dim, i, 0));
    }

    // Update only the first 5 (id=0..4) with generation 1
    for i in 0..5_usize {
        index.insert(i as u64, &make_dominant_vector(dim, i, 1));
    }

    // Pre-vacuum assertions
    assert_eq!(
        index.tombstone_count(),
        5,
        "Updating 5 vectors should create 5 tombstones",
    );
    assert_eq!(index.len(), 10);

    // Act: vacuum
    let rebuilt = index.vacuum().expect("vacuum should succeed");
    assert_eq!(rebuilt, 10, "Vacuum should report 10 active vectors");

    // Post-vacuum: tombstones cleared
    assert_eq!(
        index.tombstone_count(),
        0,
        "Vacuum must eliminate all tombstones",
    );
    assert_eq!(index.len(), 10, "All 10 vectors must survive vacuum");

    // Verify search correctness: each id appears in its own top-1 result
    // Use the *current* generation vector as query
    for i in 0..10_usize {
        let gen = u8::from(i < 5);
        let query = make_dominant_vector(dim, i, gen);
        let results = index.search(&query, 1);
        assert!(
            !results.is_empty(),
            "Search for id={i} returned no results after vacuum",
        );
        assert_eq!(
            results[0].id, i as u64,
            "Top-1 for id={i} should be itself, got id={}",
            results[0].id,
        );
    }
}

#[test]
fn test_batch_upsert_updates_existing() {
    // Arrange: 10-dim index so each of 10 IDs gets a unique dominant dimension
    let dim = 10;
    let index = HnswIndex::new(dim, DistanceMetric::Cosine).unwrap();

    // Insert 10 vectors (generation 0) via batch
    let original_vectors: Vec<(u64, Vec<f32>)> = (0..10)
        .map(|id| (id, make_dominant_vector(dim, id as usize, 0)))
        .collect();

    let refs: Vec<(u64, &[f32])> = original_vectors
        .iter()
        .map(|(id, v)| (*id, v.as_slice()))
        .collect();

    let inserted = index.insert_batch_parallel(refs);
    assert_eq!(inserted, 10, "Should insert all 10 vectors");
    assert_eq!(index.len(), 10, "Index should contain 10 entries");

    // Act: update first 5 (id=0..4) with generation 1 via batch
    let updated_vectors: Vec<(u64, Vec<f32>)> = (0..5)
        .map(|id| (id, make_dominant_vector(dim, id as usize, 1)))
        .collect();

    let update_refs: Vec<(u64, &[f32])> = updated_vectors
        .iter()
        .map(|(id, v)| (*id, v.as_slice()))
        .collect();

    let upserted = index.insert_batch_parallel(update_refs);
    assert_eq!(upserted, 5, "Should upsert all 5 vectors");

    // Assert 1: index length must still be 10 (not 15)
    assert_eq!(
        index.len(),
        10,
        "Batch upsert must not create duplicate entries: expected 10, got {}",
        index.len(),
    );

    // Assert 2: for each updated id (0..4), search with generation 1 vector
    for id in 0..5_u64 {
        let query = make_dominant_vector(dim, id as usize, 1);
        let results = index.search(&query, 1);
        assert_eq!(
            results.len(),
            1,
            "Updated id={id}: should find exactly one result"
        );
        assert_eq!(
            results[0].id, id,
            "Updated id={id}: top-1 result should be the updated vector"
        );
        assert!(
            results[0].score > 0.9,
            "Updated id={id}: similarity to updated vector should be > 0.9, got {}",
            results[0].score,
        );
    }

    // Assert 3: for each non-updated id (5..9), search with generation 0 vector
    for id in 5..10_u64 {
        let query = make_dominant_vector(dim, id as usize, 0);
        let results = index.search(&query, 1);
        assert!(
            !results.is_empty(),
            "Non-updated id={id}: should find results"
        );
        assert_eq!(
            results[0].id, id,
            "Non-updated id={id}: top-1 result should be the original vector"
        );
    }
}

// -------------------------------------------------------------------------
// Upsert TDD Cycle 5: Recall Verification + Edge Cases
// -------------------------------------------------------------------------

/// Verifies that after updating a vector to a different cluster, search
/// finds it in the NEW location and no longer returns it from the OLD cluster.
#[allow(clippy::similar_names)] // origin/target are semantically distinct cluster labels
#[test]
fn test_upsert_search_recall() {
    let index = HnswIndex::new(8, DistanceMetric::Cosine).unwrap();

    // Origin cluster center: dominant component at dim 0
    let origin_center: Vec<f32> = vec![1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0];
    // Target cluster center: dominant component at dim 4
    let target_center: Vec<f32> = vec![0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0];

    // Build deterministic vectors with small perturbation per id.
    // Origin cluster: ids 0..49, base near origin_center
    let origin_vectors: Vec<(u64, Vec<f32>)> = (0..50)
        .map(|i| {
            let mut v = origin_center.clone();
            for (d, component) in v.iter_mut().enumerate() {
                *component += 0.01 * (i * 8 + d) as f32;
            }
            (i as u64, v)
        })
        .collect();

    // Target cluster: ids 50..99, base near target_center
    let target_vectors: Vec<(u64, Vec<f32>)> = (0..50)
        .map(|i| {
            let mut v = target_center.clone();
            for (d, component) in v.iter_mut().enumerate() {
                *component += 0.01 * (i * 8 + d) as f32;
            }
            (i as u64 + 50, v)
        })
        .collect();

    // Insert all 100 vectors via batch
    let all_vectors: Vec<(u64, Vec<f32>)> = origin_vectors
        .iter()
        .chain(target_vectors.iter())
        .map(|(id, v)| (*id, v.clone()))
        .collect();

    let refs: Vec<(u64, &[f32])> = all_vectors
        .iter()
        .map(|(id, v)| (*id, v.as_slice()))
        .collect();

    let inserted = index.insert_batch_parallel(refs);
    assert_eq!(inserted, 100, "Should insert all 100 vectors");

    // Update id=25 (origin cluster) to a target cluster vector
    let moved_vector: Vec<f32> = vec![0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0];
    index.insert(25, &moved_vector);

    assert_eq!(index.len(), 100, "Length must remain 100 after upsert");

    // Search near target cluster center: id=25 should appear (it moved there)
    let results_target = index.search(&target_center, 10);
    let found_in_target = results_target.iter().any(|r| r.id == 25);
    assert!(
        found_in_target,
        "id=25 should appear in target cluster results after upsert, got ids: {:?}",
        results_target.iter().map(|r| r.id).collect::<Vec<_>>(),
    );

    // Search near origin cluster center: id=25 should NOT appear (it left)
    let results_origin = index.search(&origin_center, 10);
    let found_in_origin = results_origin.iter().any(|r| r.id == 25);
    assert!(
        !found_in_origin,
        "id=25 should NOT appear in origin cluster results after upsert, got ids: {:?}",
        results_origin.iter().map(|r| r.id).collect::<Vec<_>>(),
    );
}

/// Verifies that updating the very first vector (likely the HNSW entry point)
/// does not break search for any vector in the index.
#[test]
fn test_upsert_entry_point_vector() {
    let index = HnswIndex::new(4, DistanceMetric::Cosine).unwrap();

    // Insert id=0 first — this becomes the HNSW entry point
    let ep_original = vec![1.0, 0.0, 0.0, 0.0];
    index.insert(0, &ep_original);

    // Update id=0 to a completely different direction
    let ep_updated = vec![0.0, 0.0, 0.0, 1.0];
    index.insert(0, &ep_updated);

    // Insert 20 more vectors with varied, well-separated values.
    // Each vector has a dominant component determined by (id % 4).
    let additional_vectors: Vec<(u64, Vec<f32>)> = (1..=20)
        .map(|i| {
            let dominant_dim = (i as usize) % 4;
            let mut v = vec![0.1_f32; 4];
            v[dominant_dim] += 1.0 + 0.05 * i as f32;
            (i as u64, v)
        })
        .collect();

    let refs: Vec<(u64, &[f32])> = additional_vectors
        .iter()
        .map(|(id, v)| (*id, v.as_slice()))
        .collect();

    let inserted = index.insert_batch_parallel(refs);
    assert_eq!(inserted, 20, "Should insert all 20 vectors");
    assert_eq!(index.len(), 21, "Index should contain 21 entries");

    // Verify: searching for the updated id=0 vector returns id=0 as top-1
    let results_ep = index.search(&ep_updated, 1);
    assert_eq!(results_ep.len(), 1, "Should find at least 1 result for EP");
    assert_eq!(
        results_ep[0].id, 0,
        "Top-1 for updated entry point should be id=0, got id={}",
        results_ep[0].id,
    );

    // Verify: each of the 20 additional vectors is findable as top-1
    for (id, vec) in &additional_vectors {
        let results = index.search(vec.as_slice(), 1);
        assert!(
            !results.is_empty(),
            "Search for id={id} should return results",
        );
        assert_eq!(
            results[0].id, *id,
            "Top-1 for id={id} should be itself, got id={}",
            results[0].id,
        );
    }
}

// -------------------------------------------------------------------------
// Upsert Rollback + Edge Case Tests (Issue #371)
// -------------------------------------------------------------------------

/// Verifies that batch insert with within-batch duplicate IDs correctly
/// deduplicates: only the last occurrence is reachable, and the count
/// reflects unique IDs (not raw entries). Within-batch duplicates are an
/// unusual pattern but must not corrupt mapping state.
#[test]
fn test_batch_upsert_within_batch_duplicates() {
    let dim = 4;
    let index = HnswIndex::new(dim, DistanceMetric::Cosine).unwrap();

    // Batch with id=1 appearing twice: first [1,0,0,0], then [0,1,0,0]
    let vec_a = vec![1.0_f32, 0.0, 0.0, 0.0];
    let vec_b = vec![0.0_f32, 1.0, 0.0, 0.0];
    let batch: Vec<(u64, &[f32])> = vec![(1, &vec_a), (1, &vec_b), (2, &vec_a)];

    let inserted = index.insert_batch_parallel(batch);
    // Count may include both entries for id=1, but len() must reflect unique IDs
    assert!(
        inserted >= 2,
        "At least 2 entries processed, got {inserted}"
    );
    assert_eq!(index.len(), 2, "Only 2 unique IDs should be mapped");

    // id=1 must be searchable and point to vec_b (last wins)
    let results = index.search(&vec_b, 1);
    assert_eq!(results.len(), 1);
    assert_eq!(
        results[0].id, 1,
        "id=1 must be findable after within-batch upsert"
    );
    assert!(
        results[0].score > 0.9,
        "id=1 should match vec_b, got score {}",
        results[0].score,
    );

    // id=2 must also be searchable
    let results2 = index.search(&vec_a, 1);
    assert_eq!(results2[0].id, 2, "id=2 must be findable");
}

/// Creates a deterministic vector where dimension `dominant_dim` has a
/// large component, making it easily distinguishable in cosine search.
///
/// Shared helper to avoid duplicating the same closure across tests.
fn make_dominant_vector(dim: usize, dominant_dim: usize, gen: u8) -> Vec<f32> {
    let mut v = vec![0.1_f32; dim];
    v[dominant_dim % dim] += 1.0 + f32::from(gen) * 0.5;
    v
}

/// Verifies that insert(id) after remove(id) correctly re-inserts
/// the vector and returns it in search results.
#[test]
fn test_upsert_after_delete() {
    let dim = 8;
    let index = HnswIndex::new(dim, DistanceMetric::Cosine).unwrap();

    // Insert id=1 with dominant dim 0
    let vec_a = make_dominant_vector(dim, 0, 0);
    index.insert(1, &vec_a);
    assert_eq!(index.len(), 1);

    // Remove id=1
    assert!(index.remove(1));
    assert_eq!(index.len(), 0);

    // Re-insert id=1 with dominant dim 4
    let vec_b = make_dominant_vector(dim, 4, 1);
    index.insert(1, &vec_b);
    assert_eq!(index.len(), 1, "Re-inserted id should count as 1 vector");

    // Search should find id=1 near vec_b
    let results = index.search(&vec_b, 1);
    assert_eq!(results.len(), 1);
    assert_eq!(
        results[0].id, 1,
        "After delete + re-insert, search must find re-inserted id=1"
    );
}

/// Verifies that upserting the same id N times leaves exactly N-1
/// tombstones and the index still contains exactly 1 live vector
/// that matches the latest version.
#[test]
fn test_repeated_upsert_accumulates_tombstones() {
    let dim = 8;
    let index = HnswIndex::new(dim, DistanceMetric::Cosine).unwrap();
    let num_upserts: usize = 20;

    for gen in 0..num_upserts {
        let v = make_dominant_vector(dim, gen % dim, gen as u8);
        index.insert(1, &v);
    }

    assert_eq!(
        index.len(),
        1,
        "Repeated upserts must not duplicate entries"
    );
    assert_eq!(
        index.tombstone_count(),
        num_upserts - 1,
        "Each upsert after the first should leave one tombstone"
    );

    // Search must return the latest vector (dominant dim = (num_upserts-1) % dim)
    let latest = make_dominant_vector(dim, (num_upserts - 1) % dim, (num_upserts - 1) as u8);
    let results = index.search(&latest, 1);
    assert_eq!(results.len(), 1);
    assert_eq!(
        results[0].id, 1,
        "Search must return the latest upserted vector"
    );
}

// -------------------------------------------------------------------------
// BUG-0002: insert_and_correct_mapping divergence correction
// -------------------------------------------------------------------------

/// Forces the HNSW graph node counter ahead of `ShardedMappings::next_idx`
/// to simulate the concurrent insert divergence that BUG-0002 describes.
///
/// When `self.inner.read()` allows concurrent inserts, two threads can
/// allocate mapping indices in one order but graph node IDs in another.
/// `insert_and_correct_mapping` detects this and calls `remove_reverse` +
/// `restore` to fix the bidirectional mapping.
#[test]
fn test_insert_and_correct_mapping_fixes_diverged_idx() {
    let dim = 4;
    let index = HnswIndex::new(dim, DistanceMetric::Euclidean).unwrap();

    // Phase 1: normal insert so both counters advance to 1
    index.insert(100, &[1.0, 0.0, 0.0, 0.0]);
    assert_eq!(index.len(), 1);
    assert_eq!(index.mappings.get_idx(100), Some(0));

    // Phase 2: advance graph counter WITHOUT advancing mapping counter.
    // This simulates a concurrent thread that got into the graph first.
    let ghost_vec = [0.0, 1.0, 0.0, 0.0];
    let ghost_node_id = index.inner.read().insert((&ghost_vec, 999)).unwrap();
    assert_eq!(ghost_node_id, 1, "Graph should assign node_id=1");

    // Phase 3: upsert_mapping for id=200 — mappings allocates idx=1
    let result = index.upsert_mapping(200);
    assert_eq!(result.idx, 1, "Mapping should allocate idx=1");
    assert_eq!(result.old_idx, None, "New ID has no old mapping");

    // Phase 4: insert_and_correct_mapping — graph assigns node_id=2 (not 1)
    let vector = [0.0, 0.0, 1.0, 0.0];
    let success = index.insert_and_correct_mapping(200, &vector, &result);
    assert!(success, "insert_and_correct_mapping should succeed");

    // Phase 5: verify mapping correction
    // The graph assigned node_id=2, so mapping must point to 2, not 1
    let corrected_idx = index.mappings.get_idx(200).unwrap();
    assert_eq!(
        corrected_idx, 2,
        "Mapping must be corrected to actual graph node_id"
    );
    assert_eq!(
        index.mappings.get_id(2),
        Some(200),
        "Reverse mapping must point to id=200"
    );
    // Stale reverse mapping for idx=1 must be gone
    assert_eq!(
        index.mappings.get_id(1),
        None,
        "Stale reverse mapping for original idx must be removed"
    );
}

/// Verifies that search still returns correct results after the
/// divergence correction path has been exercised.
#[test]
fn test_search_works_after_mapping_correction() {
    let dim = 4;
    let index = HnswIndex::new(dim, DistanceMetric::Euclidean).unwrap();

    // Insert a baseline vector normally
    index.insert(100, &[1.0, 0.0, 0.0, 0.0]);

    // Advance graph counter with a ghost insert
    let ghost_vec = [0.5, 0.5, 0.0, 0.0];
    index.inner.read().insert((&ghost_vec, 999)).unwrap();

    // Force divergence path for id=200
    let result = index.upsert_mapping(200);
    let target = [0.0, 0.0, 0.0, 1.0];
    index.insert_and_correct_mapping(200, &target, &result);

    // Search for id=200's vector — should find it despite correction
    let results = index.search(&[0.0, 0.0, 0.0, 1.0], 1);
    assert_eq!(results.len(), 1);
    assert_eq!(
        results[0].id, 200,
        "Search must find the vector after mapping correction"
    );
}

/// When `assigned_id == result.idx` (no divergence), the happy path
/// should leave mappings unchanged.
#[test]
fn test_insert_and_correct_mapping_no_divergence_happy_path() {
    let dim = 4;
    let index = HnswIndex::new(dim, DistanceMetric::Euclidean).unwrap();

    // Normal insert: no divergence expected
    let result = index.upsert_mapping(42);
    assert_eq!(result.idx, 0);

    let vector = [1.0, 0.0, 0.0, 0.0];
    let success = index.insert_and_correct_mapping(42, &vector, &result);
    assert!(success, "Happy path should succeed");

    // Mapping should be exactly as allocated — no correction needed
    assert_eq!(index.mappings.get_idx(42), Some(0));
    assert_eq!(index.mappings.get_id(0), Some(42));
    assert_eq!(index.len(), 1);
}

// =========================================================================
// Issue #396: parallel_insert ignores expected idx — mapping reconciliation
// =========================================================================

/// Regression test: batch insert after single inserts must reconcile mappings.
///
/// Single inserts consume graph node IDs, so when a subsequent batch
/// pre-registers mapping indices, the graph may assign different node IDs.
/// The reconciliation logic must correct the mappings to match.
#[test]
fn test_batch_after_single_insert_mapping_consistency() {
    let index = HnswIndex::new(4, DistanceMetric::Euclidean).unwrap();

    // Single-insert 1 vector: consumes graph node 0
    index.insert(100, &[1.0, 0.0, 0.0, 0.0]);
    assert_eq!(index.len(), 1);

    // Batch-insert 5 vectors. The mapping layer will pre-register indices
    // starting from next_idx (1..=5), but the graph may assign node IDs
    // starting from 1 as well — or they may diverge under races.
    let batch: Vec<(u64, &[f32])> = vec![
        (200, &[0.0, 1.0, 0.0, 0.0]),
        (201, &[0.0, 0.0, 1.0, 0.0]),
        (202, &[0.0, 0.0, 0.0, 1.0]),
        (203, &[0.5, 0.5, 0.0, 0.0]),
        (204, &[0.0, 0.5, 0.5, 0.0]),
    ];
    let inserted = index.insert_batch_parallel(batch);
    assert_eq!(inserted, 5);
    assert_eq!(index.len(), 6);

    // Every external ID must resolve to a valid internal idx, and that idx
    // must map back to the same external ID (bidirectional consistency).
    for &ext_id in &[100u64, 200, 201, 202, 203, 204] {
        let idx = index.mappings.get_idx(ext_id);
        assert!(idx.is_some(), "get_idx({ext_id}) must return Some");
        let reverse = index.mappings.get_id(idx.unwrap());
        assert_eq!(
            reverse,
            Some(ext_id),
            "Reverse mapping for idx {} must be {ext_id}, got {reverse:?}",
            idx.unwrap()
        );
    }

    // Verify search still returns correct results — the original vector
    // should be the nearest to itself.
    let results = index.search(&[1.0, 0.0, 0.0, 0.0], 1);
    assert_eq!(results.len(), 1);
    assert_eq!(results[0].id, 100, "Nearest to [1,0,0,0] must be id=100");
}

/// Regression test: sidecar vectors stored at graph-assigned IDs.
///
/// After reconciliation, vectors in `ShardedVectors` must be stored at
/// the graph-assigned node IDs (not the pre-registered mapping indices).
#[test]
fn test_batch_insert_vector_storage_uses_assigned_ids() {
    let index = HnswIndex::new(4, DistanceMetric::Euclidean).unwrap();

    // Pre-populate to create a gap between mapping indices and graph node IDs
    for i in 0..3u64 {
        let v = [i as f32, 0.0, 0.0, 0.0];
        index.insert(i, &v);
    }
    assert_eq!(index.len(), 3);

    // Batch-insert 4 vectors
    let batch: Vec<(u64, &[f32])> = vec![
        (10, &[10.0, 0.0, 0.0, 0.0]),
        (11, &[11.0, 0.0, 0.0, 0.0]),
        (12, &[12.0, 0.0, 0.0, 0.0]),
        (13, &[13.0, 0.0, 0.0, 0.0]),
    ];
    let inserted = index.insert_batch_parallel(batch);
    assert_eq!(inserted, 4);

    // For each batch-inserted ID, the sidecar vector at the mapped idx
    // must exist and match the original vector.
    for ext_id in 10..=13u64 {
        let idx = index.mappings.get_idx(ext_id).expect("mapping must exist");
        let stored = index.vectors.get(idx);
        assert!(
            stored.is_some(),
            "ShardedVectors must have a vector at idx {idx} for id {ext_id}"
        );
        let expected_first = ext_id as f32;
        let stored_vec = stored.unwrap();
        assert!(
            (stored_vec[0] - expected_first).abs() < f32::EPSILON,
            "Vector for id {ext_id} at idx {idx}: expected first component {expected_first}, got {}",
            stored_vec[0]
        );
    }
}

/// Regression test: batch upsert (insert + replace) maintains mapping consistency.
///
/// Insert 5 vectors, then batch-upsert 3 of them with new data. The replaced
/// vectors must have updated mappings and the new graph node IDs must be valid.
#[test]
fn test_batch_upsert_mapping_consistency() {
    let index = HnswIndex::new(4, DistanceMetric::Euclidean).unwrap();

    // Initial insert of 5 vectors
    for i in 0..5u64 {
        index.insert(i, &[i as f32, 0.0, 0.0, 0.0]);
    }
    assert_eq!(index.len(), 5);

    // Batch-upsert: update IDs 1, 2, 3 with new vectors
    let batch: Vec<(u64, &[f32])> = vec![
        (1, &[100.0, 0.0, 0.0, 0.0]),
        (2, &[200.0, 0.0, 0.0, 0.0]),
        (3, &[300.0, 0.0, 0.0, 0.0]),
    ];
    let inserted = index.insert_batch_parallel(batch);
    assert_eq!(inserted, 3);
    // Still 5 live IDs (3 replaced, not added)
    assert_eq!(index.len(), 5);

    // Verify all 5 IDs have consistent bidirectional mappings
    for ext_id in 0..5u64 {
        let idx = index.mappings.get_idx(ext_id);
        assert!(idx.is_some(), "get_idx({ext_id}) must return Some");
        let reverse = index.mappings.get_id(idx.unwrap());
        assert_eq!(
            reverse,
            Some(ext_id),
            "Reverse mapping for idx {} must be {ext_id}, got {reverse:?}",
            idx.unwrap()
        );
    }

    // The replaced vectors should be searchable with their new values.
    // Search for [100, 0, 0, 0] — nearest should be id=1.
    let results = index.search(&[100.0, 0.0, 0.0, 0.0], 1);
    assert_eq!(results.len(), 1);
    assert_eq!(results[0].id, 1, "Nearest to [100,0,0,0] must be id=1");
}

// =========================================================================
// Bitmap pre-filter tests for search_with_quality_and_bitmap
// =========================================================================

#[test]
fn test_search_with_quality_and_bitmap_exists() {
    // Arrange: create an index with several vectors
    let index = HnswIndex::new(3, DistanceMetric::Cosine).unwrap();
    for i in 0u64..20 {
        #[allow(clippy::cast_precision_loss)]
        let v = vec![i as f32 * 0.1, 1.0 - i as f32 * 0.05, 0.5];
        index.insert(i, &v);
    }

    // Build a bitmap containing IDs 0..10
    let mut bitmap = roaring::RoaringBitmap::new();
    for i in 0u32..10 {
        bitmap.insert(i);
    }

    // Act
    let results = index
        .search_with_quality_and_bitmap(&[0.5, 0.5, 0.5], 5, SearchQuality::Balanced, &bitmap)
        .unwrap();

    // Assert: returns results, all IDs in bitmap
    assert!(!results.is_empty(), "should return results");
    for sr in &results {
        assert!(
            sr.id < 10,
            "all result IDs should be in bitmap, got id={}",
            sr.id
        );
    }
}

#[test]
fn test_ids_exceeding_u32_max_pass_through() {
    // This test verifies the u32::MAX passthrough logic.
    // Since HnswIndex uses u64 IDs but RoaringBitmap only holds u32,
    // IDs > u32::MAX should pass through unconditionally.
    //
    // We test the filtering logic directly: create an index with a
    // normal ID and verify the bitmap filter works correctly.
    let index = HnswIndex::new(3, DistanceMetric::Cosine).unwrap();

    // Insert vectors with normal IDs
    index.insert(1, &[1.0, 0.0, 0.0]);
    index.insert(2, &[0.0, 1.0, 0.0]);
    index.insert(3, &[0.0, 0.0, 1.0]);

    // Bitmap contains only ID 1
    let mut bitmap = roaring::RoaringBitmap::new();
    bitmap.insert(1);

    let results = index
        .search_with_quality_and_bitmap(&[1.0, 0.0, 0.0], 3, SearchQuality::Balanced, &bitmap)
        .unwrap();

    // All results should have id=1 (the only one in bitmap)
    // IDs 2 and 3 are valid u32 but NOT in bitmap, so they're excluded
    for sr in &results {
        assert_eq!(
            sr.id, 1,
            "only id=1 should pass bitmap filter, got {}",
            sr.id
        );
    }
}

#[cfg(test)]
mod bitmap_property_tests {
    use super::*;
    use proptest::prelude::*;

    proptest! {
        #![proptest_config(ProptestConfig::with_cases(100))]

        /// **Validates: Requirements 1.1, 1.2, 2.2**
        ///
        /// Feature: bitmap-prefilter-v2, Property 1: Bitmap filtering invariant
        ///
        /// For any non-empty bitmap and any SearchQuality, all ScoredResult
        /// returned by search_with_quality_and_bitmap have an id present in
        /// the bitmap (via u32 conversion) or an id exceeding u32::MAX.
        #[test]
        fn prop_bitmap_filtering_invariant(
            bitmap_ids in proptest::collection::vec(0u32..200, 1..50),
            quality_idx in 0u8..3,
        ) {
            let dim = 8;
            let index = HnswIndex::new(dim, DistanceMetric::Cosine).unwrap();

            // Insert 200 vectors
            for i in 0u64..200 {
                #[allow(clippy::cast_precision_loss)]
                let v: Vec<f32> = (0..dim).map(|d| ((i + d as u64) as f32 * 0.01).sin()).collect();
                index.insert(i, &v);
            }

            let mut bitmap = roaring::RoaringBitmap::new();
            for &id in &bitmap_ids {
                bitmap.insert(id);
            }

            let quality = match quality_idx {
                0 => SearchQuality::Fast,
                1 => SearchQuality::Balanced,
                _ => SearchQuality::Accurate,
            };

            let query: Vec<f32> = (0..dim).map(|d| (d as f32 * 0.05).cos()).collect();
            let results = index
                .search_with_quality_and_bitmap(&query, 10, quality, &bitmap)
                .unwrap();

            for sr in &results {
                let in_bitmap = u32::try_from(sr.id)
                    .is_ok_and(|id32| bitmap.contains(id32));
                let exceeds_u32 = u32::try_from(sr.id).is_err();
                prop_assert!(
                    in_bitmap || exceeds_u32,
                    "result id={} must be in bitmap or exceed u32::MAX",
                    sr.id
                );
            }
        }
    }
}

// =========================================================================
// Full-scan with bitmap tests
// =========================================================================

#[test]
fn test_full_scan_empty_bitmap_returns_empty() {
    let index = HnswIndex::new(3, DistanceMetric::Cosine).unwrap();
    index.insert(1, &[1.0, 0.0, 0.0]);
    index.insert(2, &[0.0, 1.0, 0.0]);

    let bitmap = roaring::RoaringBitmap::new();
    let results = index
        .full_scan_with_bitmap(&[1.0, 0.0, 0.0], 5, &bitmap)
        .unwrap();
    assert!(
        results.is_empty(),
        "empty bitmap should return empty results"
    );
}

#[test]
#[allow(clippy::cast_precision_loss)]
fn test_full_scan_returns_exact_results() {
    let dim = 8;
    let index = HnswIndex::new(dim, DistanceMetric::Cosine).unwrap();

    // Insert 20 vectors with distinct patterns
    for i in 0u64..20 {
        let v: Vec<f32> = (0..dim)
            .map(|d| ((i + d as u64) as f32 * 0.1).sin())
            .collect();
        index.insert(i, &v);
    }

    // Bitmap: only IDs 0..5
    let mut bitmap = roaring::RoaringBitmap::new();
    for i in 0u32..5 {
        bitmap.insert(i);
    }

    let query: Vec<f32> = (0..dim).map(|d| (d as f32 * 0.1).sin()).collect();
    let results = index.full_scan_with_bitmap(&query, 3, &bitmap).unwrap();

    // Should return at most 3 results, all from bitmap
    assert!(!results.is_empty(), "should return results");
    assert!(results.len() <= 3, "should return at most k results");
    for sr in &results {
        assert!(
            sr.id < 5,
            "all results should be from bitmap, got id={}",
            sr.id
        );
    }

    // First result should be id=0 (exact match with query pattern)
    assert_eq!(results[0].id, 0, "closest vector should be id=0");
}

#[cfg(test)]
mod full_scan_property_tests {
    use super::*;
    use proptest::prelude::*;

    proptest! {
        #![proptest_config(ProptestConfig::with_cases(100))]

        /// **Validates: Requirements 3.5, 5.2, 3.2**
        ///
        /// Feature: bitmap-prefilter-v2, Property 4: Full-scan exactness
        ///
        /// For any set of vectors and any bitmap, the full-scan results are
        /// identical to a naive exhaustive distance computation on the same
        /// bitmap vectors, sorted by the metric.
        #[test]
        fn prop_full_scan_exactness(
            n_vectors in 5u64..50,
            bitmap_ids in proptest::collection::vec(0u32..50, 1..20),
            k in 1usize..10,
        ) {
            let dim = 4;
            let index = HnswIndex::new(dim, DistanceMetric::Cosine).unwrap();

            // Insert vectors
            for i in 0..n_vectors {
                #[allow(clippy::cast_precision_loss)]
                let v: Vec<f32> = (0..dim)
                    .map(|d| ((i + d as u64) as f32 * 0.07).sin())
                    .collect();
                index.insert(i, &v);
            }

            let mut bitmap = roaring::RoaringBitmap::new();
            for &id in &bitmap_ids {
                if u64::from(id) < n_vectors {
                    bitmap.insert(id);
                }
            }

            if bitmap.is_empty() {
                // Skip: empty bitmap always returns empty
                return Ok(());
            }

            #[allow(clippy::cast_precision_loss)]
            let query: Vec<f32> = (0..dim).map(|d| (d as f32 * 0.05).cos()).collect();

            let results = index
                .full_scan_with_bitmap(&query, k, &bitmap)
                .unwrap();

            // Naive exhaustive computation
            let mut naive: Vec<(u64, f32)> = Vec::new();
            for id32 in &bitmap {
                let id = u64::from(id32);
                if let Some(idx) = index.mappings.get_idx(id) {
                    let inner = index.inner.read();
                    let score = inner.with_contiguous_vectors(|vectors| {
                        vectors.get(idx).map(|v| index.compute_distance(&query, v))
                    });
                    if let Some(s) = score {
                        naive.push((id, s));
                    }
                }
            }

            // Sort naive by cosine (higher is better → descending)
            naive.sort_by(|a, b| b.1.total_cmp(&a.1));
            naive.truncate(k);

            // Compare IDs and scores
            prop_assert_eq!(
                results.len(),
                naive.len(),
                "result count mismatch"
            );
            for (sr, (expected_id, expected_score)) in results.iter().zip(naive.iter()) {
                prop_assert_eq!(sr.id, *expected_id, "ID mismatch");
                prop_assert!(
                    (sr.score - expected_score).abs() < 1e-6,
                    "score mismatch for id={}: got {}, expected {}",
                    sr.id, sr.score, expected_score
                );
            }
        }
    }
}