velesdb-core 1.15.0

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
//! Tests for `backend_adapter` module

use super::backend_adapter::*;
use super::distance::{CachedSimdDistance, DistanceEngine};
use super::graph::{NativeHnsw, NO_ENTRY_POINT};
use crate::distance::DistanceMetric;
use crate::metrics::recall_at_k;
use tempfile::tempdir;

// =========================================================================
// TDD Tests: NativeNeighbour
// =========================================================================

#[test]
fn test_native_neighbour_creation() {
    let n = NativeNeighbour::new(42, 0.5);
    assert_eq!(n.d_id, 42);
    assert!((n.distance - 0.5).abs() < f32::EPSILON);
}

#[test]
fn test_native_neighbour_equality() {
    let n1 = NativeNeighbour::new(1, 0.1);
    let n2 = NativeNeighbour::new(1, 0.1);
    let n3 = NativeNeighbour::new(2, 0.1);

    assert_eq!(n1, n2);
    assert_ne!(n1, n3);
}

// =========================================================================
// TDD Tests: parallel_insert
// =========================================================================

#[test]
fn test_parallel_insert_small_batch() {
    let engine = CachedSimdDistance::new(DistanceMetric::Euclidean, 32);
    let hnsw = NativeHnsw::new(engine, 16, 100, 100);

    let vectors: Vec<Vec<f32>> = (0..10).map(|i| vec![i as f32; 32]).collect();
    let data: Vec<(&[f32], usize)> = vectors
        .iter()
        .enumerate()
        .map(|(i, v)| (v.as_slice(), i))
        .collect();

    hnsw.parallel_insert(&data).expect("test");

    assert_eq!(hnsw.len(), 10);
}

#[test]
fn test_parallel_insert_large_batch() {
    let engine = CachedSimdDistance::new(DistanceMetric::Euclidean, 32);
    let hnsw = NativeHnsw::new(engine, 16, 100, 100);

    // Use 50 vectors to stay under Rayon parallelization threshold (100)
    // This avoids deadlocks when tests run in parallel
    let vectors: Vec<Vec<f32>> = (0..50).map(|i| vec![i as f32 * 0.01; 32]).collect();
    let data: Vec<(&[f32], usize)> = vectors
        .iter()
        .enumerate()
        .map(|(i, v)| (v.as_slice(), i))
        .collect();

    hnsw.parallel_insert(&data).expect("test");

    assert_eq!(hnsw.len(), 50);
}

// =========================================================================
// TDD Tests: search_neighbours
// =========================================================================

#[test]
fn test_search_neighbours_format() {
    let engine = CachedSimdDistance::new(DistanceMetric::Euclidean, 32);
    let hnsw = NativeHnsw::new(engine, 16, 100, 100);

    for i in 0..50 {
        hnsw.insert(&[i as f32 * 0.1; 32]).expect("test");
    }

    let query = vec![0.0; 32];
    let results = hnsw.search_neighbours(&query, 5, 50);

    assert!(results.len() <= 5);
    for result in &results {
        assert!(result.d_id < 50);
        assert!(result.distance >= 0.0);
    }
}

// =========================================================================
// TDD Tests: transform_score
// =========================================================================

#[test]
fn test_transform_score_euclidean() {
    let engine = CachedSimdDistance::new(DistanceMetric::Euclidean, 32);
    let hnsw = NativeHnsw::new(engine, 16, 100, 100);

    // Euclidean: transform_score applies sqrt (raw distances are squared L2)
    assert!(
        (hnsw.transform_score(0.25) - 0.5).abs() < f32::EPSILON,
        "sqrt(0.25) should be 0.5"
    );
    assert!(
        (hnsw.transform_score(25.0) - 5.0).abs() < 1e-5,
        "sqrt(25.0) should be 5.0"
    );
    assert!(
        hnsw.transform_score(0.0).abs() < f32::EPSILON,
        "sqrt(0.0) should be 0.0"
    );
}

#[test]
fn test_transform_score_cosine() {
    let engine = CachedSimdDistance::new(DistanceMetric::Cosine, 32);
    let hnsw = NativeHnsw::new(engine, 16, 100, 100);

    // Cosine: similarity = 1 - distance
    assert!((hnsw.transform_score(0.3) - 0.7).abs() < f32::EPSILON);
    assert!((hnsw.transform_score(1.5) - 0.0).abs() < f32::EPSILON); // clamped
}

#[test]
fn test_transform_score_dot_product() {
    let engine = CachedSimdDistance::new(DistanceMetric::DotProduct, 32);
    let hnsw = NativeHnsw::new(engine, 16, 100, 100);

    // DotProduct: score = -distance
    assert!((hnsw.transform_score(0.5) - (-0.5)).abs() < f32::EPSILON);
}

// =========================================================================
// TDD Tests: file_dump and file_load
// =========================================================================

#[test]
fn test_file_dump_creates_files() {
    let engine = CachedSimdDistance::new(DistanceMetric::Euclidean, 32);
    let hnsw = NativeHnsw::new(engine, 16, 100, 100);

    for i in 0..20 {
        hnsw.insert(&[i as f32; 32]).expect("test");
    }

    let dir = tempdir().unwrap();
    let result = hnsw.file_dump(dir.path(), "test_index");

    assert!(result.is_ok());
    assert!(dir.path().join("test_index.vectors").exists());
    assert!(dir.path().join("test_index.graph").exists());
}

#[test]
fn test_file_dump_and_load_roundtrip() {
    let engine = CachedSimdDistance::new(DistanceMetric::Euclidean, 32);
    let hnsw = NativeHnsw::new(engine, 16, 100, 100);

    // Insert some vectors
    let vectors: Vec<Vec<f32>> = (0..30)
        .map(|i| (0..32).map(|j| (i * 32 + j) as f32 * 0.01).collect())
        .collect();

    for v in &vectors {
        hnsw.insert(v).expect("test");
    }

    // Dump to files
    let dir = tempdir().unwrap();
    hnsw.file_dump(dir.path(), "roundtrip").unwrap();

    // Load from files
    let engine2 = CachedSimdDistance::new(DistanceMetric::Euclidean, 32);
    let loaded = NativeHnsw::file_load(dir.path(), "roundtrip", engine2).unwrap();

    // Verify loaded index
    assert_eq!(loaded.len(), 30);

    // Search should return same results
    let query = vectors[0].clone();
    let results_orig = hnsw.search(&query, 5, 50);
    let results_loaded = loaded.search(&query, 5, 50);

    assert_eq!(results_orig.len(), results_loaded.len());
    // First result should be the same (exact match)
    if !results_orig.is_empty() && !results_loaded.is_empty() {
        assert_eq!(results_orig[0].0, results_loaded[0].0);
    }
}

// =========================================================================
// TDD Tests: set_searching_mode (no-op but should not panic)
// =========================================================================

#[test]
fn test_set_searching_mode_no_panic() {
    let engine = CachedSimdDistance::new(DistanceMetric::Euclidean, 32);
    let mut hnsw = NativeHnsw::new(engine, 16, 100, 100);

    hnsw.set_searching_mode(true);
    hnsw.set_searching_mode(false);
    // Should not panic
}

// =========================================================================
// TDD Tests: NativeHnswBackend trait
// =========================================================================

#[test]
fn test_native_backend_trait_is_object_safe() {
    // Verify trait can be used as dyn object
    fn accepts_dyn_backend(_backend: &dyn NativeHnswBackend) {}

    let engine = CachedSimdDistance::new(DistanceMetric::Euclidean, 32);
    let hnsw = NativeHnsw::new(engine, 16, 100, 100);
    accepts_dyn_backend(&hnsw);
}

#[test]
fn test_native_backend_trait_search() {
    let engine = CachedSimdDistance::new(DistanceMetric::Euclidean, 32);
    let hnsw = NativeHnsw::new(engine, 16, 100, 100);

    // Insert via trait
    for i in 0..20 {
        let vec: Vec<f32> = (0..32).map(|j| (i * 32 + j) as f32 * 0.01).collect();
        <NativeHnsw<CachedSimdDistance> as NativeHnswBackend>::insert(&hnsw, (&vec, i))
            .expect("test");
    }

    // Search via trait
    let query: Vec<f32> = (0..32).map(|j| j as f32 * 0.01).collect();
    let results =
        <NativeHnsw<CachedSimdDistance> as NativeHnswBackend>::search(&hnsw, &query, 5, 50);

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

#[test]
fn test_native_backend_generic_function() {
    // Test that trait can be used in generic context
    fn search_with_backend<B: NativeHnswBackend>(
        backend: &B,
        query: &[f32],
        k: usize,
    ) -> Vec<NativeNeighbour> {
        backend.search(query, k, 100)
    }

    let engine = CachedSimdDistance::new(DistanceMetric::Euclidean, 32);
    let hnsw = NativeHnsw::new(engine, 16, 100, 100);

    for i in 0..10 {
        hnsw.insert(&[i as f32; 32]).expect("test");
    }

    let query = vec![0.0; 32];
    let results = search_with_backend(&hnsw, &query, 5);

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

#[test]
fn test_native_backend_len_and_is_empty() {
    let engine = CachedSimdDistance::new(DistanceMetric::Euclidean, 32);
    let hnsw = NativeHnsw::new(engine, 16, 100, 100);

    assert!(<NativeHnsw<CachedSimdDistance> as NativeHnswBackend>::is_empty(&hnsw));
    assert_eq!(
        <NativeHnsw<CachedSimdDistance> as NativeHnswBackend>::len(&hnsw),
        0
    );

    hnsw.insert(&[1.0; 32]).expect("test");

    assert!(!<NativeHnsw<CachedSimdDistance> as NativeHnswBackend>::is_empty(&hnsw));
    assert_eq!(
        <NativeHnsw<CachedSimdDistance> as NativeHnswBackend>::len(&hnsw),
        1
    );
}

// =========================================================================
// TDD Tests: chunked Phase B for large batch insert (#364 — RED)
// =========================================================================

#[test]
fn test_compute_chunk_size_boundaries() {
    // Formula: (batch_len / 50).max(1000).min(5000)
    assert_eq!(
        NativeHnsw::<CachedSimdDistance>::compute_chunk_size(100),
        1000
    );
    assert_eq!(
        NativeHnsw::<CachedSimdDistance>::compute_chunk_size(1_000),
        1000
    );
    assert_eq!(
        NativeHnsw::<CachedSimdDistance>::compute_chunk_size(10_000),
        1000
    );
    assert_eq!(
        NativeHnsw::<CachedSimdDistance>::compute_chunk_size(100_000),
        2000
    );
    assert_eq!(
        NativeHnsw::<CachedSimdDistance>::compute_chunk_size(500_000),
        5000
    );
}

#[test]
fn test_parallel_insert_chunked_count() {
    let engine = CachedSimdDistance::new(DistanceMetric::Euclidean, 32);
    let hnsw = NativeHnsw::new(engine, 16, 100, 100);

    // Generate 2000 deterministic 32-D vectors using index-based values
    let vectors: Vec<Vec<f32>> = (0..2000)
        .map(|i| (0..32).map(|j| ((i * 32 + j) as f32) * 0.001).collect())
        .collect();

    let data: Vec<(&[f32], usize)> = vectors
        .iter()
        .enumerate()
        .map(|(i, v)| (v.as_slice(), i))
        .collect();

    hnsw.parallel_insert(&data)
        .expect("parallel_insert of 2000 vectors should succeed");

    assert_eq!(hnsw.len(), 2000);
}

#[test]
fn test_parallel_insert_chunked_ep_update() {
    let engine = CachedSimdDistance::new(DistanceMetric::Euclidean, 32);
    let hnsw = NativeHnsw::new(engine, 16, 100, 100);

    // Generate 2000 deterministic 32-D vectors
    let vectors: Vec<Vec<f32>> = (0..2000)
        .map(|i| (0..32).map(|j| ((i * 32 + j) as f32) * 0.001).collect())
        .collect();

    let data: Vec<(&[f32], usize)> = vectors
        .iter()
        .enumerate()
        .map(|(i, v)| (v.as_slice(), i))
        .collect();

    hnsw.parallel_insert(&data)
        .expect("parallel_insert of 2000 vectors should succeed");

    // With 2000 nodes and deterministic PRNG (fixed seed 0x5DEE_CE66_D1A4_B5B5),
    // node 0 is never assigned the highest layer. The entry point must have been
    // promoted to a higher-layer node during chunked insertion.
    let ep_id = hnsw.entry_point.load(std::sync::atomic::Ordering::Acquire);
    assert_ne!(
        ep_id, NO_ENTRY_POINT,
        "entry_point should be set after inserting 2000 vectors"
    );
    assert_ne!(
        ep_id, 0,
        "entry point should have been promoted beyond node 0 with 2000 inserts"
    );
}

#[test]
fn test_parallel_insert_chunked_recall() {
    let engine = CachedSimdDistance::new(DistanceMetric::Euclidean, 32);
    let hnsw = NativeHnsw::new(engine, 16, 100, 100);

    // Generate 2000 deterministic 32-D vectors with enough spread for recall testing
    let vectors: Vec<Vec<f32>> = (0..2000)
        .map(|i| (0..32).map(|j| ((i * 32 + j) as f32) * 0.001).collect())
        .collect();

    let data: Vec<(&[f32], usize)> = vectors
        .iter()
        .enumerate()
        .map(|(i, v)| (v.as_slice(), i))
        .collect();

    hnsw.parallel_insert(&data)
        .expect("parallel_insert of 2000 vectors should succeed");

    // Brute-force distance engine (same metric as the index)
    let bf_engine = CachedSimdDistance::new(DistanceMetric::Euclidean, 32);
    let k = 10;
    let ef_search = 128;
    let num_queries = 50;

    let mut total_recall = 0.0;

    for q_idx in 0..num_queries {
        // Deterministic query vector derived from query index
        let query: Vec<f32> = (0..32)
            .map(|j| ((q_idx * 7 + j * 13) as f32) * 0.002)
            .collect();

        // HNSW search
        let hnsw_results = hnsw.search(&query, k, ef_search);
        let hnsw_ids: Vec<usize> = hnsw_results.iter().map(|&(id, _)| id).collect();

        // Brute-force ground truth: compute distance to every vector, sort, take top-k
        let mut distances: Vec<(usize, f32)> = vectors
            .iter()
            .enumerate()
            .map(|(id, v)| (id, bf_engine.distance(&query, v)))
            .collect();
        distances.sort_by(|a, b| a.1.partial_cmp(&b.1).unwrap_or(std::cmp::Ordering::Equal));
        let ground_truth: Vec<usize> = distances.iter().take(k).map(|&(id, _)| id).collect();

        total_recall += recall_at_k(&ground_truth, &hnsw_ids);
    }

    #[allow(clippy::cast_precision_loss)]
    // Reason: num_queries is a small constant (50); f64 is exact for integers up to 2^53.
    let avg_recall = total_recall / num_queries as f64;

    assert!(
        avg_recall >= 0.90,
        "average recall@{k} should be >= 0.90, got {avg_recall:.4}"
    );
}

// =========================================================================
// TDD Tests: adaptive_ef_for_batch (#486 — bulk insert optimization)
// =========================================================================

#[test]
fn test_adaptive_ef_small_batch_no_reduction() {
    let engine = CachedSimdDistance::new(DistanceMetric::Euclidean, 32);
    let hnsw = NativeHnsw::new(engine, 32, 400, 100);

    // Batches <= 1000 use full ef_construction with stagnation disabled
    let (ef, stag) = hnsw.adaptive_ef_for_batch(500);
    assert_eq!(ef, 400, "small batch should use full ef_construction");
    assert_eq!(stag, 0, "small batch should have stagnation disabled");

    let (ef, stag) = hnsw.adaptive_ef_for_batch(1000);
    assert_eq!(
        ef, 400,
        "batch of exactly 1000 should use full ef_construction"
    );
    assert_eq!(
        stag, 0,
        "batch of exactly 1000 should have stagnation disabled"
    );
}

#[test]
fn test_adaptive_ef_medium_batch_85_percent() {
    let engine = CachedSimdDistance::new(DistanceMetric::Euclidean, 32);
    let hnsw = NativeHnsw::new(engine, 32, 400, 100);

    // Batches > 1K and <= 10K use 85% of ef_construction
    let (ef, stag) = hnsw.adaptive_ef_for_batch(5_000);
    assert_eq!(
        ef, 340,
        "batch of 5K should use 85% of ef_construction (340)"
    );
    assert_eq!(stag, 170, "stagnation should be ef/2 = 170");
}

#[test]
fn test_adaptive_ef_large_batch_75_percent() {
    let engine = CachedSimdDistance::new(DistanceMetric::Euclidean, 32);
    let hnsw = NativeHnsw::new(engine, 32, 400, 100);

    // Batches > 10K and <= 50K use 75% of ef_construction
    let (ef, stag) = hnsw.adaptive_ef_for_batch(20_000);
    assert_eq!(
        ef, 300,
        "batch of 20K should use 75% of ef_construction (300)"
    );
    assert_eq!(stag, 150, "stagnation should be ef/2 = 150");
}

#[test]
fn test_adaptive_ef_very_large_batch_60_percent() {
    let engine = CachedSimdDistance::new(DistanceMetric::Euclidean, 32);
    let hnsw = NativeHnsw::new(engine, 32, 400, 100);

    // Batches > 50K use 60% of ef_construction
    let (ef, stag) = hnsw.adaptive_ef_for_batch(100_000);
    assert_eq!(
        ef, 240,
        "batch of 100K should use 60% of ef_construction (240)"
    );
    assert_eq!(stag, 120, "stagnation should be ef/2 = 120");
}

#[test]
fn test_adaptive_ef_floor_at_4x_max_connections() {
    let engine = CachedSimdDistance::new(DistanceMetric::Euclidean, 32);
    // ef_construction=40, M=32: 60% of 40 = 24, but floor is 4*M=128
    let hnsw = NativeHnsw::new(engine, 32, 40, 100);

    let (ef, stag) = hnsw.adaptive_ef_for_batch(100_000);
    assert_eq!(
        ef, 128,
        "ef should be floored at 4*max_connections when scaling goes below it"
    );
    assert_eq!(stag, 64, "stagnation should be ef/2 = 64");
}

#[test]
fn test_adaptive_ef_boundary_10001() {
    let engine = CachedSimdDistance::new(DistanceMetric::Euclidean, 32);
    let hnsw = NativeHnsw::new(engine, 16, 400, 100);

    // Exactly 10001 crosses into the 75% tier
    let (ef, _) = hnsw.adaptive_ef_for_batch(10_001);
    assert_eq!(ef, 300, "batch of 10001 should use 75% tier");
}

#[test]
fn test_adaptive_ef_boundary_50001() {
    let engine = CachedSimdDistance::new(DistanceMetric::Euclidean, 32);
    let hnsw = NativeHnsw::new(engine, 16, 400, 100);

    // Exactly 50001 crosses into the 60% tier
    let (ef, _) = hnsw.adaptive_ef_for_batch(50_001);
    assert_eq!(ef, 240, "batch of 50001 should use 60% tier");
}

#[test]
fn test_adaptive_ef_recall_preserved_with_2000_vectors() {
    // Regression test: adaptive ef for a 2000-vector batch (75% tier)
    // must maintain recall >= 0.90 (same threshold as non-adaptive test above).
    let engine = CachedSimdDistance::new(DistanceMetric::Euclidean, 32);
    let hnsw = NativeHnsw::new(engine, 16, 100, 100);

    let vectors: Vec<Vec<f32>> = (0..2000)
        .map(|i| (0..32).map(|j| ((i * 32 + j) as f32) * 0.001).collect())
        .collect();

    let data: Vec<(&[f32], usize)> = vectors
        .iter()
        .enumerate()
        .map(|(i, v)| (v.as_slice(), i))
        .collect();

    // This exercises connect_batch_chunked -> connect_node_with_ef with adaptive ef
    hnsw.parallel_insert(&data)
        .expect("parallel_insert should succeed");

    assert_eq!(hnsw.len(), 2000);

    let bf_engine = CachedSimdDistance::new(DistanceMetric::Euclidean, 32);
    let k = 10;
    let ef_search = 128;
    let num_queries = 50;
    let mut total_recall = 0.0;

    for q_idx in 0..num_queries {
        let query: Vec<f32> = (0..32)
            .map(|j| ((q_idx * 7 + j * 13) as f32) * 0.002)
            .collect();

        let hnsw_results = hnsw.search(&query, k, ef_search);
        let hnsw_ids: Vec<usize> = hnsw_results.iter().map(|&(id, _)| id).collect();

        let mut distances: Vec<(usize, f32)> = vectors
            .iter()
            .enumerate()
            .map(|(id, v)| (id, bf_engine.distance(&query, v)))
            .collect();
        distances.sort_by(|a, b| a.1.partial_cmp(&b.1).unwrap_or(std::cmp::Ordering::Equal));
        let ground_truth: Vec<usize> = distances.iter().take(k).map(|&(id, _)| id).collect();

        total_recall += recall_at_k(&ground_truth, &hnsw_ids);
    }

    #[allow(clippy::cast_precision_loss)]
    // Reason: num_queries is a small constant (50); f64 is exact for integers up to 2^53.
    let avg_recall = total_recall / num_queries as f64;

    assert!(
        avg_recall >= 0.90,
        "adaptive ef recall@{k} should be >= 0.90, got {avg_recall:.4}"
    );
}
// =========================================================================
// TDD Tests: BatchEfSchedule — graduated ef_construction (I1)
// =========================================================================

#[test]
fn test_batch_ef_schedule_small_batch_uniform() {
    // Batches < 1000 should use full ef for all phases
    let schedule = super::batch_schedule::compute_batch_ef_schedule(200, 500, 16);
    assert_eq!(schedule.scaffold_ef, 200);
    assert_eq!(schedule.bulk_ef, 200);
    assert_eq!(schedule.finalize_ef, 200);
    assert_eq!(schedule.scaffold_count, 500);
    assert_eq!(schedule.finalize_start, 500);
}

#[test]
fn test_batch_ef_schedule_large_batch_graduated() {
    // Batch of 10_000 with ef=200, M=16
    let schedule = super::batch_schedule::compute_batch_ef_schedule(200, 10_000, 16);
    assert_eq!(schedule.scaffold_ef, 200, "scaffold should use full ef");
    assert_eq!(schedule.bulk_ef, 100, "bulk should use 0.5x ef");
    assert_eq!(schedule.finalize_ef, 150, "finalize should use 0.75x ef");
    assert_eq!(schedule.scaffold_count, 1000, "scaffold = 10%");
    assert_eq!(schedule.finalize_start, 9000, "finalize starts at 90%");
}

#[test]
fn test_batch_ef_schedule_floor_enforcement() {
    // When 0.5x ef < 2*m, the floor should kick in
    // ef=40, m=16 → bulk = max(20, 32) = 32; finalize = max(30, 32) = 32
    let schedule = super::batch_schedule::compute_batch_ef_schedule(40, 5000, 16);
    assert_eq!(schedule.scaffold_ef, 40);
    assert_eq!(schedule.bulk_ef, 32, "bulk floored at 2*M");
    assert_eq!(schedule.finalize_ef, 32, "finalize floored at 2*M");
}

#[test]
fn test_batch_ef_schedule_ef_for_position() {
    let schedule = super::batch_schedule::compute_batch_ef_schedule(200, 10_000, 16);

    // Scaffold phase: positions 0..999
    assert_eq!(schedule.ef_for_position(0), 200);
    assert_eq!(schedule.ef_for_position(999), 200);

    // Bulk phase: positions 1000..8999
    assert_eq!(schedule.ef_for_position(1000), 100);
    assert_eq!(schedule.ef_for_position(5000), 100);
    assert_eq!(schedule.ef_for_position(8999), 100);

    // Finalize phase: positions 9000..9999
    assert_eq!(schedule.ef_for_position(9000), 150);
    assert_eq!(schedule.ef_for_position(9999), 150);
}

#[test]
fn test_batch_ef_schedule_boundary_batch_size() {
    // Exactly 1000: should apply graduated schedule
    let schedule = super::batch_schedule::compute_batch_ef_schedule(100, 1000, 16);
    assert_eq!(schedule.scaffold_count, 100);
    assert_eq!(schedule.finalize_start, 900);
    assert_eq!(schedule.bulk_ef, 50);
    assert_eq!(schedule.finalize_ef, 75);

    // 999: should use uniform full ef
    let schedule = super::batch_schedule::compute_batch_ef_schedule(100, 999, 16);
    assert_eq!(schedule.scaffold_ef, 100);
    assert_eq!(schedule.bulk_ef, 100);
    assert_eq!(schedule.finalize_ef, 100);
}

// =========================================================================
// TDD Tests: graduated ef_construction recall (I1)
// =========================================================================

/// Verifies that graduated ef_construction maintains recall >= 0.90
/// with 5000 vectors. The 3-phase schedule (scaffold/bulk/finalize)
/// reduces construction work while preserving graph quality.
#[test]
fn test_graduated_ef_construction_recall() {
    let engine = CachedSimdDistance::new(DistanceMetric::Euclidean, 64);
    let hnsw = NativeHnsw::new(engine, 16, 100, 100);

    // Generate 5000 deterministic 64-D vectors with enough spread for recall testing
    let vectors: Vec<Vec<f32>> = (0..5000)
        .map(|i| (0..64).map(|j| ((i * 64 + j) as f32) * 0.0001).collect())
        .collect();

    let data: Vec<(&[f32], usize)> = vectors
        .iter()
        .enumerate()
        .map(|(i, v)| (v.as_slice(), i))
        .collect();

    hnsw.parallel_insert(&data)
        .expect("test: parallel_insert of 5000 vectors should succeed");

    assert_eq!(hnsw.len(), 5000);

    let bf_engine = CachedSimdDistance::new(DistanceMetric::Euclidean, 64);
    let k = 10;
    let ef_search = 128;
    let num_queries = 100;

    let mut total_recall = 0.0;

    for q_idx in 0..num_queries {
        let query: Vec<f32> = (0..64)
            .map(|j| ((q_idx * 11 + j * 17) as f32) * 0.0003)
            .collect();

        let hnsw_results = hnsw.search(&query, k, ef_search);
        let hnsw_ids: Vec<usize> = hnsw_results.iter().map(|&(id, _)| id).collect();

        let mut distances: Vec<(usize, f32)> = vectors
            .iter()
            .enumerate()
            .map(|(id, v)| (id, bf_engine.distance(&query, v)))
            .collect();
        distances.sort_by(|a, b| a.1.partial_cmp(&b.1).unwrap_or(std::cmp::Ordering::Equal));
        let ground_truth: Vec<usize> = distances.iter().take(k).map(|&(id, _)| id).collect();

        total_recall += recall_at_k(&ground_truth, &hnsw_ids);
    }

    #[allow(clippy::cast_precision_loss)]
    // Reason: num_queries is a small constant (100); f64 is exact for integers up to 2^53.
    let avg_recall = total_recall / num_queries as f64;

    assert!(
        avg_recall >= 0.90,
        "graduated ef_construction: recall@{k} should be >= 0.90 at 5000 vectors, got {avg_recall:.4}"
    );
}

/// Verifies that the graduated schedule applies to cosine metric as well,
/// since cosine normalizes vectors before insertion.
#[test]
fn test_graduated_ef_construction_recall_cosine() {
    let engine = CachedSimdDistance::new(DistanceMetric::Cosine, 32);
    let hnsw = NativeHnsw::new(engine, 16, 100, 100);

    // Generate 3000 deterministic 32-D vectors
    let vectors: Vec<Vec<f32>> = (0..3000)
        .map(|i| {
            let raw: Vec<f32> = (0..32)
                .map(|j| ((i * 32 + j) as f32) * 0.001 + 0.1)
                .collect();
            // Pre-normalize for cosine metric ground-truth comparison
            let norm: f32 = raw.iter().map(|x| x * x).sum::<f32>().sqrt();
            raw.iter().map(|x| x / norm).collect()
        })
        .collect();

    let data: Vec<(&[f32], usize)> = vectors
        .iter()
        .enumerate()
        .map(|(i, v)| (v.as_slice(), i))
        .collect();

    hnsw.parallel_insert(&data)
        .expect("test: parallel_insert of 3000 cosine vectors should succeed");

    assert_eq!(hnsw.len(), 3000);

    let bf_engine = CachedSimdDistance::new(DistanceMetric::Cosine, 32);
    let k = 10;
    let ef_search = 128;
    let num_queries = 50;

    let mut total_recall = 0.0;

    for q_idx in 0..num_queries {
        let raw: Vec<f32> = (0..32)
            .map(|j| ((q_idx * 7 + j * 13) as f32) * 0.002 + 0.1)
            .collect();
        let norm: f32 = raw.iter().map(|x| x * x).sum::<f32>().sqrt();
        let query: Vec<f32> = raw.iter().map(|x| x / norm).collect();

        let hnsw_results = hnsw.search(&query, k, ef_search);
        let hnsw_ids: Vec<usize> = hnsw_results.iter().map(|&(id, _)| id).collect();

        let mut distances: Vec<(usize, f32)> = vectors
            .iter()
            .enumerate()
            .map(|(id, v)| (id, bf_engine.distance(&query, v)))
            .collect();
        distances.sort_by(|a, b| a.1.partial_cmp(&b.1).unwrap_or(std::cmp::Ordering::Equal));
        let ground_truth: Vec<usize> = distances.iter().take(k).map(|&(id, _)| id).collect();

        total_recall += recall_at_k(&ground_truth, &hnsw_ids);
    }

    #[allow(clippy::cast_precision_loss)]
    // Reason: num_queries is a small constant (50); f64 is exact for integers up to 2^53.
    let avg_recall = total_recall / num_queries as f64;

    assert!(
        avg_recall >= 0.89,
        "graduated ef cosine: recall@{k} should be >= 0.89 at 3000 vectors, got {avg_recall:.4}"
    );
}

// =========================================================================
// I2: Pre-Allocated Vector Storage — Regression tests
// =========================================================================

/// Verifies that batch insert with the split lock strategy (I2) produces
/// the same recall as sequential insert. This guards against the resize/push
/// split introducing any data corruption or ordering bugs.
#[test]
fn test_i2_preallocated_batch_insert_recall() {
    let dim = 64;
    let n = 1000;
    let k = 10;
    let ef_search = 128;
    let num_queries = 30;

    // Build index via parallel_insert (uses split reserve + push)
    let engine = CachedSimdDistance::new(DistanceMetric::Cosine, 64);
    let hnsw = NativeHnsw::new(engine, 16, 200, n);

    let vectors: Vec<Vec<f32>> = (0..n)
        .map(|i| {
            let mut v: Vec<f32> = (0..dim)
                .map(|j| ((i * dim + j) as f32) * 0.001 + 0.01)
                .collect();
            let norm: f32 = v.iter().map(|x| x * x).sum::<f32>().sqrt();
            for x in &mut v {
                *x /= norm;
            }
            v
        })
        .collect();

    let data: Vec<(&[f32], usize)> = vectors
        .iter()
        .enumerate()
        .map(|(i, v)| (v.as_slice(), i))
        .collect();

    hnsw.parallel_insert(&data)
        .expect("test: parallel_insert should succeed");

    assert_eq!(hnsw.len(), n, "all vectors should be inserted");

    // Recall check against brute-force
    let bf_engine = CachedSimdDistance::new(DistanceMetric::Cosine, dim);
    let mut total_recall = 0.0;

    for q_idx in 0..num_queries {
        let mut query: Vec<f32> = (0..dim)
            .map(|j| ((q_idx * 3 + j * 7) as f32) * 0.003)
            .collect();
        let norm: f32 = query.iter().map(|x| x * x).sum::<f32>().sqrt();
        for x in &mut query {
            *x /= norm;
        }

        let hnsw_results = hnsw.search(&query, k, ef_search);
        let hnsw_ids: Vec<usize> = hnsw_results.iter().map(|&(id, _)| id).collect();

        let mut distances: Vec<(usize, f32)> = vectors
            .iter()
            .enumerate()
            .map(|(id, v)| (id, bf_engine.distance(&query, v)))
            .collect();
        distances.sort_by(|a, b| a.1.partial_cmp(&b.1).unwrap_or(std::cmp::Ordering::Equal));
        let ground_truth: Vec<usize> = distances.iter().take(k).map(|&(id, _)| id).collect();

        total_recall += recall_at_k(&ground_truth, &hnsw_ids);
    }

    #[allow(clippy::cast_precision_loss)]
    // Reason: num_queries is a small constant (30); f64 is exact for integers up to 2^53.
    let avg_recall = total_recall / num_queries as f64;

    // Threshold 0.89 to account for float-precision edge cases where
    // recall = 27/30 = 0.9000... rounds to 0.8999... in f64 arithmetic.
    assert!(
        avg_recall >= 0.89,
        "I2 pre-allocated batch recall@{k} should be >= 0.89, got {avg_recall:.4}"
    );
}

/// Verifies that a batch insert much larger than the initial `max_elements`
/// correctly resizes in the reserve phase and pushes without corruption.
#[test]
fn test_i2_batch_exceeding_initial_capacity() {
    let engine = CachedSimdDistance::new(DistanceMetric::Euclidean, 32);
    // Initial max_elements = 16, but we insert 500 — forces multiple resizes
    let hnsw = NativeHnsw::new(engine, 16, 100, 16);

    let vectors: Vec<Vec<f32>> = (0..500).map(|i| vec![i as f32 * 0.01; 32]).collect();

    let data: Vec<(&[f32], usize)> = vectors
        .iter()
        .enumerate()
        .map(|(i, v)| (v.as_slice(), i))
        .collect();

    hnsw.parallel_insert(&data)
        .expect("test: batch exceeding initial capacity should succeed");

    assert_eq!(hnsw.len(), 500);

    // Verify vector data integrity by searching for exact matches
    let query = vectors[0].clone();
    let results = hnsw.search(&query, 1, 50);
    assert!(
        !results.is_empty(),
        "search should find at least one result"
    );
    assert_eq!(
        results[0].0, 0,
        "nearest neighbor of vector 0 should be itself"
    );
}