lora-database 0.14.0

LoraDB — embeddable in-memory graph database with Cypher query support.
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
//! CREATE VECTOR INDEX + db.index.vector.queryNodes / queryRelationships.

mod test_helpers;
use test_helpers::TestDb;

use std::collections::BTreeMap;

use lora_database::LoraValue;
use lora_store::{
    cosine_similarity_bounded, euclidean_similarity, LoraVector, RawCoordinate,
    VectorCoordinateType,
};
use serde_json::Value as JsonValue;

fn index_named<'a>(rows: &'a [JsonValue], name: &str) -> Option<&'a JsonValue> {
    rows.iter()
        .find(|r| r.get("name").and_then(|v| v.as_str()) == Some(name))
}

fn names(rows: &[JsonValue]) -> Vec<String> {
    rows.iter()
        .filter_map(|r| r.get("name").and_then(|v| v.as_str()).map(String::from))
        .collect()
}

fn ordered_node_ids(rows: &[JsonValue]) -> Vec<i64> {
    rows.iter()
        .filter_map(|r| {
            r.get("node")
                .and_then(|n| n.get("id"))
                .and_then(|i| i.as_i64())
        })
        .collect()
}

// ---------- CREATE VECTOR INDEX DDL ----------

#[test]
fn create_vector_index_node_round_trip() {
    let db = TestDb::new();
    db.run(
        "CREATE VECTOR INDEX movie_emb FOR (m:Movie) ON (m.embedding) \
         OPTIONS {indexConfig: {`vector.dimensions`: 4, `vector.similarity_function`: 'cosine'}}",
    );
    let listed = db.run("SHOW INDEXES");
    let entry = index_named(&listed, "movie_emb").expect("listed");
    assert_eq!(entry["type"], JsonValue::String("VECTOR".into()));
    assert_eq!(entry["entityType"], JsonValue::String("NODE".into()));
    assert_eq!(
        entry["labelsOrTypes"],
        JsonValue::Array(vec![JsonValue::String("Movie".into())])
    );
    assert_eq!(
        entry["properties"],
        JsonValue::Array(vec![JsonValue::String("embedding".into())])
    );
}

#[test]
fn create_vector_index_relationship_round_trip() {
    let db = TestDb::new();
    db.run(
        "CREATE VECTOR INDEX rel_emb FOR ()-[r:CONTAINS]-() ON (r.embedding) \
         OPTIONS {indexConfig: {`vector.dimensions`: 3, `vector.similarity_function`: 'euclidean'}}",
    );
    let listed = db.run("SHOW INDEXES");
    let entry = index_named(&listed, "rel_emb").unwrap();
    assert_eq!(entry["type"], JsonValue::String("VECTOR".into()));
    assert_eq!(
        entry["entityType"],
        JsonValue::String("RELATIONSHIP".into())
    );
}

#[test]
fn show_vector_indexes_filter_now_returns_entries() {
    let db = TestDb::new();
    db.run("CREATE RANGE INDEX rng FOR (n:N) ON (n.x)");
    db.run(
        "CREATE VECTOR INDEX v1 FOR (m:Movie) ON (m.embedding) \
         OPTIONS {indexConfig: {`vector.dimensions`: 4, `vector.similarity_function`: 'cosine'}}",
    );
    let listed = db.run("SHOW VECTOR INDEXES");
    assert_eq!(names(&listed), vec!["v1"]);
}

#[test]
fn vector_index_requires_index_config_options() {
    let db = TestDb::new();
    let err = db.run_err("CREATE VECTOR INDEX bad FOR (m:Movie) ON (m.embedding)");
    assert!(
        err.contains("indexConfig"),
        "expected indexConfig error, got: {err}"
    );
    let err = db
        .exec("CREATE VECTOR INDEX bad FOR (m:Movie) ON (m.embedding)")
        .expect_err("missing vector indexConfig should fail validation");
    assert_eq!(err.code(), lora_database::LoraErrorCode::Validation);
}

#[test]
fn vector_index_rejects_invalid_dimensions() {
    let db = TestDb::new();
    let err = db.run_err(
        "CREATE VECTOR INDEX bad FOR (m:Movie) ON (m.embedding) \
         OPTIONS {indexConfig: {`vector.dimensions`: 0, `vector.similarity_function`: 'cosine'}}",
    );
    assert!(
        err.contains("1..=4096"),
        "expected dimension bound error, got: {err}"
    );
}

#[test]
fn vector_index_rejects_unknown_similarity() {
    let db = TestDb::new();
    let err = db.run_err(
        "CREATE VECTOR INDEX bad FOR (m:Movie) ON (m.embedding) \
         OPTIONS {indexConfig: {`vector.dimensions`: 4, `vector.similarity_function`: 'jaccard'}}",
    );
    assert!(
        err.contains("similarity_function"),
        "expected similarity error, got: {err}"
    );
}

#[test]
fn vector_index_rejects_composite_properties() {
    let db = TestDb::new();
    let err = db.run_err(
        "CREATE VECTOR INDEX bad FOR (m:Movie) ON (m.a, m.b) \
         OPTIONS {indexConfig: {`vector.dimensions`: 4, `vector.similarity_function`: 'cosine'}}",
    );
    assert!(
        err.contains("single-property"),
        "expected single-property error, got: {err}"
    );
}

#[test]
fn vector_index_if_not_exists_is_idempotent() {
    let db = TestDb::new();
    db.run(
        "CREATE VECTOR INDEX v FOR (m:Movie) ON (m.embedding) \
         OPTIONS {indexConfig: {`vector.dimensions`: 4, `vector.similarity_function`: 'cosine'}}",
    );
    let rows = db.run(
        "CREATE VECTOR INDEX v IF NOT EXISTS FOR (m:Movie) ON (m.embedding) \
         OPTIONS {indexConfig: {`vector.dimensions`: 4, `vector.similarity_function`: 'cosine'}}",
    );
    assert!(rows.is_empty());
}

#[test]
fn drop_vector_index() {
    let db = TestDb::new();
    db.run(
        "CREATE VECTOR INDEX v FOR (m:Movie) ON (m.embedding) \
         OPTIONS {indexConfig: {`vector.dimensions`: 4, `vector.similarity_function`: 'cosine'}}",
    );
    db.run("DROP INDEX v");
    let listed = db.run("SHOW INDEXES");
    assert!(index_named(&listed, "v").is_none());
}

// ---------- Procedure: db.index.vector.queryNodes ----------

fn create_index(db: &TestDb, sim: &str) {
    db.run(&format!(
        "CREATE VECTOR INDEX movie_emb FOR (m:Movie) ON (m.embedding) \
         OPTIONS {{indexConfig: {{`vector.dimensions`: 3, `vector.similarity_function`: '{sim}'}}}}",
    ));
}

fn seed_movies(db: &TestDb) {
    // Three vectors in 3D. Target [1,0,0] — closest first under cosine
    // similarity: identical, then near-axis, then perpendicular.
    db.run("CREATE (:Movie {title: 'A', embedding: [1.0, 0.0, 0.0]::VECTOR<FLOAT32>(3)})");
    db.run("CREATE (:Movie {title: 'B', embedding: [0.9, 0.1, 0.0]::VECTOR<FLOAT32>(3)})");
    db.run("CREATE (:Movie {title: 'C', embedding: [0.0, 1.0, 0.0]::VECTOR<FLOAT32>(3)})");
    db.run("CREATE (:Movie {title: 'D'})"); // no embedding — should be ignored
    db.run("CREATE (:Other {embedding: [1.0, 0.0, 0.0]::VECTOR<FLOAT32>(3)})");
    // wrong label
}

#[test]
fn vector_query_returns_top_k_in_descending_similarity() {
    let db = TestDb::new();
    create_index(&db, "cosine");
    seed_movies(&db);
    let rows = db
        .run("CALL db.index.vector.queryNodes('movie_emb', 2, [1.0, 0.0, 0.0]) YIELD node, score");
    assert_eq!(rows.len(), 2, "expected top-2, got {rows:?}");
    let scores: Vec<f64> = rows
        .iter()
        .filter_map(|r| r.get("score").and_then(|v| v.as_f64()))
        .collect();
    assert!(scores[0] >= scores[1], "scores not descending: {scores:?}");
    // First hit is the identical vector.
    assert!((scores[0] - 1.0).abs() < 1e-6, "first score should be 1.0");
}

#[test]
fn vector_query_skips_entities_without_indexed_property() {
    let db = TestDb::new();
    create_index(&db, "cosine");
    seed_movies(&db);
    let rows = db
        .run("CALL db.index.vector.queryNodes('movie_emb', 10, [1.0, 0.0, 0.0]) YIELD node, score");
    // 3 movies with embedding; the 4th Movie has no embedding, the Other
    // label is out of scope. So exactly 3 rows.
    assert_eq!(rows.len(), 3, "got {rows:?}");
}

#[test]
fn vector_query_supports_euclidean_similarity() {
    let db = TestDb::new();
    create_index(&db, "euclidean");
    seed_movies(&db);
    let rows = db
        .run("CALL db.index.vector.queryNodes('movie_emb', 1, [1.0, 0.0, 0.0]) YIELD node, score");
    assert_eq!(rows.len(), 1);
    let score = rows[0]["score"].as_f64().unwrap();
    assert!(
        (score - 1.0).abs() < 1e-6,
        "euclidean self-score = 1.0; got {score}"
    );
}

#[test]
fn vector_query_rejects_dimension_mismatch() {
    let db = TestDb::new();
    create_index(&db, "cosine");
    seed_movies(&db);
    let err =
        db.run_err("CALL db.index.vector.queryNodes('movie_emb', 1, [1.0, 0.0]) YIELD node, score");
    assert!(
        err.contains("dimension"),
        "expected dimension mismatch, got: {err}"
    );
}

#[test]
fn vector_query_rejects_unknown_index() {
    let db = TestDb::new();
    let err =
        db.run_err("CALL db.index.vector.queryNodes('nope', 1, [1.0, 0.0, 0.0]) YIELD node, score");
    assert!(
        err.contains("no vector index"),
        "expected unknown-index error, got: {err}"
    );
}

#[test]
fn vector_query_rejects_wrong_index_kind() {
    let db = TestDb::new();
    db.run("CREATE RANGE INDEX rng FOR (n:N) ON (n.x)");
    let err =
        db.run_err("CALL db.index.vector.queryNodes('rng', 1, [1.0, 0.0, 0.0]) YIELD node, score");
    assert!(
        err.contains("not a VECTOR"),
        "expected wrong-kind error, got: {err}"
    );
}

#[test]
fn vector_query_rejects_unknown_yield_column() {
    let db = TestDb::new();
    create_index(&db, "cosine");
    seed_movies(&db);
    let err =
        db.run_err("CALL db.index.vector.queryNodes('movie_emb', 1, [1.0, 0.0, 0.0]) YIELD title");
    assert!(
        err.contains("unknown column `title`"),
        "expected unknown YIELD column error, got: {err}"
    );
}

#[test]
fn vector_query_relationships_top_k() {
    let db = TestDb::new();
    db.run(
        "CREATE VECTOR INDEX rel_emb FOR ()-[r:CONTAINS]-() ON (r.embedding) \
         OPTIONS {indexConfig: {`vector.dimensions`: 3, `vector.similarity_function`: 'cosine'}}",
    );
    db.run(
        "CREATE (a:Doc), (b:Doc), (c:Doc), \
         (a)-[:CONTAINS {embedding: [1.0, 0.0, 0.0]::VECTOR<FLOAT32>(3)}]->(b), \
         (b)-[:CONTAINS {embedding: [0.9, 0.1, 0.0]::VECTOR<FLOAT32>(3)}]->(c)",
    );
    let rows = db.run(
        "CALL db.index.vector.queryRelationships('rel_emb', 2, [1.0, 0.0, 0.0]) \
         YIELD relationship, score",
    );
    assert_eq!(rows.len(), 2);
    let s0 = rows[0]["score"].as_f64().unwrap();
    let s1 = rows[1]["score"].as_f64().unwrap();
    assert!(s0 >= s1);
}

#[test]
fn vector_query_k_zero_rejected() {
    let db = TestDb::new();
    create_index(&db, "cosine");
    seed_movies(&db);
    let err = db.run_err(
        "CALL db.index.vector.queryNodes('movie_emb', 0, [1.0, 0.0, 0.0]) YIELD node, score",
    );
    assert!(err.contains("k must be positive"), "got: {err}");
}

#[test]
fn vector_query_accepts_vector_arg() {
    let db = TestDb::new();
    create_index(&db, "cosine");
    seed_movies(&db);
    let mut params = BTreeMap::new();
    params.insert(
        "q".to_string(),
        LoraValue::Vector(
            LoraVector::try_new(
                vec![
                    RawCoordinate::Float(1.0),
                    RawCoordinate::Float(0.0),
                    RawCoordinate::Float(0.0),
                ],
                3,
                VectorCoordinateType::Float32,
            )
            .unwrap(),
        ),
    );
    let rows = db.run_with_params(
        "CALL db.index.vector.queryNodes('movie_emb', 1, $q) YIELD node, score",
        params,
    );
    assert_eq!(rows.len(), 1);
    // Still ranks the identical vector first.
    assert!((rows[0]["score"].as_f64().unwrap() - 1.0).abs() < 1e-6);
}

// ---------- Brute-force oracle tests ----------
//
// These lock the current flat-scan behavior so that any future ANN backend
// (HNSW, IVF, …) can be tested against the same fixtures. The oracle
// computes top-k in-test using the same `cosine_similarity_bounded` /
// `euclidean_similarity` primitives the production scorer uses, sorts
// descending, and compares the resulting score sequence. A backend that
// keeps recall@k = 1.0 must reproduce the exact ordering; an approximate
// backend can later be tested with a recall tolerance.

/// Deterministic LCG so the fixtures are stable across runs and platforms
/// without pulling in `rand`. Returns f32 samples in roughly [-1, 1).
fn seeded_f32_stream(seed: u64) -> impl FnMut() -> f32 {
    let mut state = seed.wrapping_mul(0x9E37_79B9_7F4A_7C15).wrapping_add(1);
    move || {
        state = state
            .wrapping_mul(6364136223846793005)
            .wrapping_add(1442695040888963407);
        // Take the upper 32 bits and map into [-1, 1).
        let bits = (state >> 32) as u32 as i32;
        bits as f32 / (i32::MAX as f32 + 1.0)
    }
}

fn seeded_vectors(seed: u64, n: usize, dim: usize) -> Vec<LoraVector> {
    let mut rng = seeded_f32_stream(seed);
    (0..n)
        .map(|_| {
            let coords: Vec<RawCoordinate> = (0..dim)
                .map(|_| RawCoordinate::Float(rng() as f64))
                .collect();
            LoraVector::try_new(coords, dim as i64, VectorCoordinateType::Float32).unwrap()
        })
        .collect()
}

/// Seed the DB with `n` `:V` nodes each carrying property `e` set to the
/// matching vector. Uses parameter binding (one CREATE per vector) so the
/// vectors are stored byte-identically rather than reparsed from text.
fn seed_vector_nodes(db: &TestDb, vectors: &[LoraVector]) {
    for v in vectors {
        let mut params = BTreeMap::new();
        params.insert("e".to_string(), LoraValue::Vector(v.clone()));
        db.run_with_params("CREATE (:V {e: $e})", params);
    }
}

fn call_top_k(db: &TestDb, k: usize, query: &LoraVector) -> Vec<f64> {
    let mut params = BTreeMap::new();
    params.insert("q".to_string(), LoraValue::Vector(query.clone()));
    let rows = db.run_with_params(
        &format!("CALL db.index.vector.queryNodes('vidx', {k}, $q) YIELD score"),
        params,
    );
    rows.iter()
        .filter_map(|r| r.get("score").and_then(|s| s.as_f64()))
        .collect()
}

fn oracle_top_k<F>(vectors: &[LoraVector], query: &LoraVector, k: usize, sim: F) -> Vec<f64>
where
    F: Fn(&LoraVector, &LoraVector) -> Option<f64>,
{
    let mut scored: Vec<f64> = vectors.iter().filter_map(|v| sim(v, query)).collect();
    scored.sort_by(|a, b| b.partial_cmp(a).unwrap_or(std::cmp::Ordering::Equal));
    scored.truncate(k);
    scored
}

fn assert_scores_match(proc_scores: &[f64], oracle: &[f64]) {
    assert_eq!(
        proc_scores.len(),
        oracle.len(),
        "score count mismatch: proc={proc_scores:?}, oracle={oracle:?}"
    );
    for (i, (a, b)) in proc_scores.iter().zip(oracle.iter()).enumerate() {
        assert!(
            (a - b).abs() < 1e-9,
            "score[{i}] mismatch: proc={a}, oracle={b}\nproc={proc_scores:?}\noracle={oracle:?}"
        );
    }
}

#[test]
fn flat_knn_matches_oracle_cosine() {
    let db = TestDb::new();
    let dim = 16usize;
    db.run(&format!(
        "CREATE VECTOR INDEX vidx FOR (n:V) ON (n.e) \
         OPTIONS {{indexConfig: {{`vector.dimensions`: {dim}, `vector.similarity_function`: 'cosine'}}}}",
    ));
    let vectors = seeded_vectors(0x00C0_514E_u64, 64, dim);
    seed_vector_nodes(&db, &vectors);
    let query = seeded_vectors(0xDEAD_BEEF_u64, 1, dim).pop().unwrap();
    let proc_scores = call_top_k(&db, 10, &query);
    let oracle = oracle_top_k(&vectors, &query, 10, cosine_similarity_bounded);
    assert_scores_match(&proc_scores, &oracle);
}

#[test]
fn flat_knn_matches_oracle_euclidean() {
    let db = TestDb::new();
    let dim = 16usize;
    db.run(&format!(
        "CREATE VECTOR INDEX vidx FOR (n:V) ON (n.e) \
         OPTIONS {{indexConfig: {{`vector.dimensions`: {dim}, `vector.similarity_function`: 'euclidean'}}}}",
    ));
    let vectors = seeded_vectors(0x00E0_C11D_u64, 64, dim);
    seed_vector_nodes(&db, &vectors);
    let query = seeded_vectors(0xFEED_FACE_u64, 1, dim).pop().unwrap();
    let proc_scores = call_top_k(&db, 10, &query);
    let oracle = oracle_top_k(&vectors, &query, 10, euclidean_similarity);
    assert_scores_match(&proc_scores, &oracle);
}

#[test]
fn flat_knn_k_larger_than_n_returns_all() {
    let db = TestDb::new();
    let dim = 8usize;
    db.run(&format!(
        "CREATE VECTOR INDEX vidx FOR (n:V) ON (n.e) \
         OPTIONS {{indexConfig: {{`vector.dimensions`: {dim}, `vector.similarity_function`: 'cosine'}}}}",
    ));
    let vectors = seeded_vectors(7, 5, dim);
    seed_vector_nodes(&db, &vectors);
    let query = seeded_vectors(8, 1, dim).pop().unwrap();
    let proc_scores = call_top_k(&db, 100, &query);
    let oracle = oracle_top_k(&vectors, &query, 100, cosine_similarity_bounded);
    assert_eq!(proc_scores.len(), 5);
    assert_scores_match(&proc_scores, &oracle);
}

#[test]
fn flat_knn_handles_score_ties_deterministically() {
    // 4 identical vectors → 4 identical scores. Procedure must return them
    // in a deterministic order (current contract: descending score, then
    // ascending node id). We only assert the score sequence is stable
    // across repeated calls; node-id ordering is left to the procedure.
    let db = TestDb::new();
    let dim = 4usize;
    db.run(&format!(
        "CREATE VECTOR INDEX vidx FOR (n:V) ON (n.e) \
         OPTIONS {{indexConfig: {{`vector.dimensions`: {dim}, `vector.similarity_function`: 'cosine'}}}}",
    ));
    let v = LoraVector::try_new(
        vec![
            RawCoordinate::Float(1.0),
            RawCoordinate::Float(0.0),
            RawCoordinate::Float(0.0),
            RawCoordinate::Float(0.0),
        ],
        dim as i64,
        VectorCoordinateType::Float32,
    )
    .unwrap();
    let vectors = vec![v.clone(), v.clone(), v.clone(), v.clone()];
    seed_vector_nodes(&db, &vectors);
    let scores_a = call_top_k(&db, 4, &v);
    let scores_b = call_top_k(&db, 4, &v);
    assert_eq!(scores_a, scores_b, "ordering not deterministic across runs");
    assert!(scores_a.iter().all(|s| (s - 1.0).abs() < 1e-9));
}

#[test]
fn flat_knn_skips_nodes_missing_property_in_oracle() {
    // A node without the indexed property must be skipped by the
    // procedure. The oracle must do the same — verified here because the
    // future backend abstraction is going to special-case missing
    // properties at insert time, not at query time.
    let db = TestDb::new();
    let dim = 8usize;
    db.run(&format!(
        "CREATE VECTOR INDEX vidx FOR (n:V) ON (n.e) \
         OPTIONS {{indexConfig: {{`vector.dimensions`: {dim}, `vector.similarity_function`: 'cosine'}}}}",
    ));
    let vectors = seeded_vectors(3, 8, dim);
    seed_vector_nodes(&db, &vectors);
    // Two `:V` nodes with no `e` property.
    db.run("CREATE (:V {label: 'no-vec-1'})");
    db.run("CREATE (:V {label: 'no-vec-2'})");
    let query = seeded_vectors(4, 1, dim).pop().unwrap();
    let proc_scores = call_top_k(&db, 100, &query);
    assert_eq!(
        proc_scores.len(),
        8,
        "expected exactly 8 scored vectors (missing-property nodes ignored)"
    );
    let oracle = oracle_top_k(&vectors, &query, 100, cosine_similarity_bounded);
    assert_scores_match(&proc_scores, &oracle);
}

// ---------- HNSW provider tests ----------
//
// These exercise the `vector.indexProvider: 'hnsw'` option end-to-end:
// DDL round-trip, top-k correctness against the flat oracle (with
// recall tolerance), and schema validator coverage for the new knobs.

fn create_hnsw_index(db: &TestDb, dim: usize, sim: &str) {
    db.run(&format!(
        "CREATE VECTOR INDEX vidx FOR (n:V) ON (n.e) \
         OPTIONS {{indexConfig: {{ \
            `vector.dimensions`: {dim}, \
            `vector.similarity_function`: '{sim}', \
            `vector.indexProvider`: 'hnsw' \
         }}}}",
    ));
}

#[test]
fn hnsw_index_ddl_round_trip() {
    let db = TestDb::new();
    create_hnsw_index(&db, 8, "cosine");
    let listed = db.run("SHOW INDEXES");
    let entry = index_named(&listed, "vidx").expect("listed");
    assert_eq!(entry["type"], JsonValue::String("VECTOR".into()));
}

#[test]
fn hnsw_top_k_returns_k_results() {
    let db = TestDb::new();
    let dim = 16usize;
    create_hnsw_index(&db, dim, "cosine");
    let vectors = seeded_vectors(0xABCD, 64, dim);
    seed_vector_nodes(&db, &vectors);
    let query = seeded_vectors(0xEEFF, 1, dim).pop().unwrap();
    let mut params = BTreeMap::new();
    params.insert("q".to_string(), LoraValue::Vector(query));
    let rows = db.run_with_params(
        "CALL db.index.vector.queryNodes('vidx', 5, $q) YIELD node, score",
        params,
    );
    assert_eq!(rows.len(), 5);
    // Scores must be descending.
    let scores: Vec<f64> = rows
        .iter()
        .filter_map(|r| r.get("score").and_then(|s| s.as_f64()))
        .collect();
    for w in scores.windows(2) {
        assert!(w[0] >= w[1], "scores not descending: {scores:?}");
    }
}

#[test]
fn hnsw_recall_at_10_meets_target_cosine() {
    // Recall@10 ≥ 0.95 against the flat oracle on uniform random
    // d=64 vectors at n=1k. Tighter than the per-backend unit test
    // because Cypher's path uses default HNSW params (M=16,
    // ef_search=100) on a higher-dim, larger-n fixture where graph
    // structure is more pronounced.
    let db = TestDb::new();
    let dim = 64usize;
    let n = 1_000usize;
    create_hnsw_index(&db, dim, "cosine");
    let vectors = seeded_vectors(0x00C0_514E_u64, n, dim);
    seed_vector_nodes(&db, &vectors);
    let query = seeded_vectors(0xDEAD_BEEF_u64, 1, dim).pop().unwrap();

    let mut params = BTreeMap::new();
    params.insert("q".to_string(), LoraValue::Vector(query.clone()));
    let rows = db.run_with_params(
        "CALL db.index.vector.queryNodes('vidx', 10, $q) YIELD node, score",
        params,
    );
    let hnsw_scores: Vec<f64> = rows
        .iter()
        .filter_map(|r| r.get("score").and_then(|s| s.as_f64()))
        .collect();
    assert_eq!(hnsw_scores.len(), 10);

    // Build the oracle from the same vectors. Recall is the
    // fraction of the oracle's top-10 *scores* the HNSW result
    // recovers — using score as the identity is more lenient than
    // id-based recall but still meaningful, and it avoids the
    // node-id bookkeeping we'd otherwise need.
    let oracle = oracle_top_k(&vectors, &query, 10, cosine_similarity_bounded);
    let mut hits = 0usize;
    for s in &hnsw_scores {
        if oracle.iter().any(|o| (o - s).abs() < 1e-9) {
            hits += 1;
        }
    }
    let recall = hits as f64 / 10.0;
    assert!(
        recall >= 0.95,
        "HNSW recall@10 too low: {recall} (hnsw={hnsw_scores:?}, oracle={oracle:?})"
    );
}

#[test]
fn hnsw_explicit_flat_provider_matches_oracle_exactly() {
    // `vector.indexProvider: 'flat'` is the legacy code path under a
    // new explicit name. Must remain exact (recall=1.0) so users
    // upgrading their config don't see drift.
    let db = TestDb::new();
    let dim = 16usize;
    db.run(&format!(
        "CREATE VECTOR INDEX vidx FOR (n:V) ON (n.e) \
         OPTIONS {{indexConfig: {{ \
            `vector.dimensions`: {dim}, \
            `vector.similarity_function`: 'cosine', \
            `vector.indexProvider`: 'flat' \
         }}}}",
    ));
    let vectors = seeded_vectors(13, 50, dim);
    seed_vector_nodes(&db, &vectors);
    let query = seeded_vectors(14, 1, dim).pop().unwrap();
    let mut params = BTreeMap::new();
    params.insert("q".to_string(), LoraValue::Vector(query.clone()));
    let rows = db.run_with_params(
        "CALL db.index.vector.queryNodes('vidx', 10, $q) YIELD score",
        params,
    );
    let proc_scores: Vec<f64> = rows
        .iter()
        .filter_map(|r| r.get("score").and_then(|s| s.as_f64()))
        .collect();
    let oracle = oracle_top_k(&vectors, &query, 10, cosine_similarity_bounded);
    assert_scores_match(&proc_scores, &oracle);
}

#[test]
fn schema_rejects_invalid_index_provider() {
    let db = TestDb::new();
    let err = db.run_err(
        "CREATE VECTOR INDEX bad FOR (m:Movie) ON (m.embedding) \
         OPTIONS {indexConfig: { \
            `vector.dimensions`: 4, \
            `vector.similarity_function`: 'cosine', \
            `vector.indexProvider`: 'annoy' \
         }}",
    );
    assert!(
        err.contains("indexProvider"),
        "expected indexProvider error, got: {err}"
    );
}

#[test]
fn schema_rejects_out_of_range_hnsw_m() {
    let db = TestDb::new();
    let err = db.run_err(
        "CREATE VECTOR INDEX bad FOR (m:Movie) ON (m.embedding) \
         OPTIONS {indexConfig: { \
            `vector.dimensions`: 4, \
            `vector.similarity_function`: 'cosine', \
            `vector.hnsw.m`: 999 \
         }}",
    );
    assert!(
        err.contains("vector.hnsw.m") && err.contains("128"),
        "expected hnsw.m range error, got: {err}"
    );
}

// ---------- New metric coverage: dot, manhattan ----------

fn create_index_with_metric(db: &TestDb, dim: usize, sim: &str, provider: &str) {
    db.run(&format!(
        "CREATE VECTOR INDEX vidx FOR (n:V) ON (n.e) \
         OPTIONS {{indexConfig: {{ \
            `vector.dimensions`: {dim}, \
            `vector.similarity_function`: '{sim}', \
            `vector.indexProvider`: '{provider}' \
         }}}}",
    ));
}

#[test]
fn dot_metric_top_k_matches_oracle_flat() {
    let db = TestDb::new();
    let dim = 16usize;
    create_index_with_metric(&db, dim, "dot", "flat");
    let vectors = seeded_vectors(0xD07_u64, 64, dim);
    seed_vector_nodes(&db, &vectors);
    let query = seeded_vectors(0xD071, 1, dim).pop().unwrap();
    let mut params = BTreeMap::new();
    params.insert("q".to_string(), LoraValue::Vector(query.clone()));
    let rows = db.run_with_params(
        "CALL db.index.vector.queryNodes('vidx', 10, $q) YIELD score",
        params,
    );
    let proc_scores: Vec<f64> = rows
        .iter()
        .filter_map(|r| r.get("score").and_then(|s| s.as_f64()))
        .collect();
    // Oracle uses raw dot product — same metric the index ranks by.
    let mut oracle: Vec<f64> = vectors
        .iter()
        .filter_map(|v| lora_store::dot_product(v, &query))
        .collect();
    oracle.sort_by(|a, b| b.partial_cmp(a).unwrap_or(std::cmp::Ordering::Equal));
    oracle.truncate(10);
    assert_scores_match(&proc_scores, &oracle);
}

#[test]
fn manhattan_metric_top_k_matches_oracle_flat() {
    let db = TestDb::new();
    let dim = 16usize;
    create_index_with_metric(&db, dim, "manhattan", "flat");
    let vectors = seeded_vectors(0x1A_10, 64, dim);
    seed_vector_nodes(&db, &vectors);
    let query = seeded_vectors(0x1A_11, 1, dim).pop().unwrap();
    let mut params = BTreeMap::new();
    params.insert("q".to_string(), LoraValue::Vector(query.clone()));
    let rows = db.run_with_params(
        "CALL db.index.vector.queryNodes('vidx', 10, $q) YIELD score",
        params,
    );
    let proc_scores: Vec<f64> = rows
        .iter()
        .filter_map(|r| r.get("score").and_then(|s| s.as_f64()))
        .collect();
    // Oracle applies the same `1/(1+L1)` mapping the index uses.
    let mut oracle: Vec<f64> = vectors
        .iter()
        .filter_map(|v| lora_store::manhattan_distance(v, &query).map(|d| 1.0 / (1.0 + d)))
        .collect();
    oracle.sort_by(|a, b| b.partial_cmp(a).unwrap_or(std::cmp::Ordering::Equal));
    oracle.truncate(10);
    assert_scores_match(&proc_scores, &oracle);
}

#[test]
fn dot_metric_works_with_hnsw_provider() {
    let db = TestDb::new();
    let dim = 32usize;
    create_index_with_metric(&db, dim, "dot", "hnsw");
    let vectors = seeded_vectors(0xD072, 256, dim);
    seed_vector_nodes(&db, &vectors);
    let query = seeded_vectors(0x000D_0721, 1, dim).pop().unwrap();
    let mut params = BTreeMap::new();
    params.insert("q".to_string(), LoraValue::Vector(query));
    let rows = db.run_with_params(
        "CALL db.index.vector.queryNodes('vidx', 5, $q) YIELD score",
        params,
    );
    let scores: Vec<f64> = rows
        .iter()
        .filter_map(|r| r.get("score").and_then(|s| s.as_f64()))
        .collect();
    assert_eq!(scores.len(), 5);
    for w in scores.windows(2) {
        assert!(w[0] >= w[1], "dot+hnsw scores not descending: {scores:?}");
    }
}

#[test]
fn dot_product_alias_is_accepted() {
    let db = TestDb::new();
    db.run(
        "CREATE VECTOR INDEX vidx FOR (n:V) ON (n.e) \
         OPTIONS {indexConfig: {`vector.dimensions`: 4, `vector.similarity_function`: 'dot_product'}}",
    );
    // No error → alias accepted.
    let listed = db.run("SHOW VECTOR INDEXES");
    assert!(index_named(&listed, "vidx").is_some());
}

// ---------- Async (lazy) populate state ----------

#[test]
fn async_populate_index_starts_populating() {
    let db = TestDb::new();
    // Pre-seed a vector before CREATE INDEX so the backfill has work.
    db.run("CREATE (:V {e: [1.0, 0.0, 0.0, 0.0]::VECTOR<FLOAT32>(4)})");
    db.run(
        "CREATE VECTOR INDEX vidx FOR (n:V) ON (n.e) \
         OPTIONS {indexConfig: { \
            `vector.dimensions`: 4, \
            `vector.similarity_function`: 'cosine', \
            `vector.populate.async`: true \
         }}",
    );
    let listed = db.run("SHOW INDEXES");
    let entry = index_named(&listed, "vidx").expect("listed");
    assert_eq!(entry["state"], JsonValue::String("POPULATING".into()));
}

#[test]
fn async_populate_first_query_warms_index_and_flips_to_online() {
    let db = TestDb::new();
    db.run("CREATE (:V {e: [1.0, 0.0, 0.0, 0.0]::VECTOR<FLOAT32>(4)})");
    db.run("CREATE (:V {e: [0.9, 0.1, 0.0, 0.0]::VECTOR<FLOAT32>(4)})");
    db.run(
        "CREATE VECTOR INDEX vidx FOR (n:V) ON (n.e) \
         OPTIONS {indexConfig: { \
            `vector.dimensions`: 4, \
            `vector.similarity_function`: 'cosine', \
            `vector.populate.async`: true \
         }}",
    );
    // First query: triggers populate inline; both pre-existing
    // vectors must show up.
    let rows = db
        .run("CALL db.index.vector.queryNodes('vidx', 5, [1.0, 0.0, 0.0, 0.0]) YIELD node, score");
    assert_eq!(rows.len(), 2, "expected pre-existing vectors, got {rows:?}");
    // State should now be Online.
    let listed = db.run("SHOW INDEXES");
    let entry = index_named(&listed, "vidx").expect("listed");
    assert_eq!(entry["state"], JsonValue::String("ONLINE".into()));
}

#[test]
fn async_populate_post_create_inserts_still_visible() {
    // Mutations between CREATE (async) and first query go through
    // the maintenance hook and feed the registry. Verify the lazy
    // backfill doesn't drop or duplicate them.
    let db = TestDb::new();
    db.run("CREATE (:V {tag: 'pre', e: [1.0, 0.0, 0.0, 0.0]::VECTOR<FLOAT32>(4)})");
    db.run(
        "CREATE VECTOR INDEX vidx FOR (n:V) ON (n.e) \
         OPTIONS {indexConfig: { \
            `vector.dimensions`: 4, \
            `vector.similarity_function`: 'cosine', \
            `vector.populate.async`: true \
         }}",
    );
    // After CREATE, while index is Populating, insert another:
    db.run("CREATE (:V {tag: 'post', e: [0.0, 1.0, 0.0, 0.0]::VECTOR<FLOAT32>(4)})");
    let rows = db
        .run("CALL db.index.vector.queryNodes('vidx', 5, [1.0, 0.0, 0.0, 0.0]) YIELD node, score");
    assert_eq!(
        rows.len(),
        2,
        "expected both pre+post vectors, got {rows:?}"
    );
}

#[test]
fn async_populate_option_rejects_non_boolean() {
    let db = TestDb::new();
    let err = db.run_err(
        "CREATE VECTOR INDEX bad FOR (n:V) ON (n.e) \
         OPTIONS {indexConfig: { \
            `vector.dimensions`: 4, \
            `vector.similarity_function`: 'cosine', \
            `vector.populate.async`: 'yes' \
         }}",
    );
    assert!(
        err.contains("vector.populate.async") && err.contains("boolean"),
        "expected boolean shape error, got: {err}"
    );
}

// ---------- SHOW INDEXES surfaces options ----------

#[test]
fn show_indexes_surfaces_vector_options() {
    let db = TestDb::new();
    db.run(
        "CREATE VECTOR INDEX vidx FOR (m:Movie) ON (m.embedding) \
         OPTIONS {indexConfig: { \
            `vector.dimensions`: 8, \
            `vector.similarity_function`: 'cosine', \
            `vector.indexProvider`: 'hnsw', \
            `vector.hnsw.m`: 24, \
            `vector.hnsw.ef_construction`: 256, \
            `vector.hnsw.ef_search`: 128 \
         }}",
    );
    let listed = db.run("SHOW INDEXES");
    let entry = index_named(&listed, "vidx").expect("listed");
    let options = entry["options"].as_object().expect("options is a map");
    assert_eq!(options.get("vector.dimensions"), Some(&JsonValue::from(8)));
    assert_eq!(
        options.get("vector.similarity_function"),
        Some(&JsonValue::String("cosine".into()))
    );
    assert_eq!(
        options.get("vector.indexProvider"),
        Some(&JsonValue::String("hnsw".into()))
    );
    assert_eq!(options.get("vector.hnsw.m"), Some(&JsonValue::from(24)));
    assert_eq!(
        options.get("vector.hnsw.ef_construction"),
        Some(&JsonValue::from(256))
    );
    assert_eq!(
        options.get("vector.hnsw.ef_search"),
        Some(&JsonValue::from(128))
    );
}

// ---------- HNSW snapshot round-trip ----------

#[test]
fn hnsw_backend_survives_snapshot_round_trip() {
    let donor = TestDb::new();
    let dim = 16usize;
    donor.run(&format!(
        "CREATE VECTOR INDEX vidx FOR (n:V) ON (n.e) \
         OPTIONS {{indexConfig: {{ \
            `vector.dimensions`: {dim}, \
            `vector.similarity_function`: 'cosine', \
            `vector.indexProvider`: 'hnsw' \
         }}}}",
    ));
    let vectors = seeded_vectors(0x5A_AF_u64, 128, dim);
    seed_vector_nodes(&donor, &vectors);

    // Capture donor query result so we can compare after restore.
    let query = seeded_vectors(0xC1_AB_u64, 1, dim).pop().unwrap();
    let mut donor_params = BTreeMap::new();
    donor_params.insert("q".to_string(), LoraValue::Vector(query.clone()));
    let donor_rows = donor.run_with_params(
        "CALL db.index.vector.queryNodes('vidx', 10, $q) YIELD node, score",
        donor_params,
    );

    // Round-trip the database through a snapshot.
    let bytes = donor
        .service
        .save_snapshot_to_bytes()
        .expect("snapshot encode");
    let target = TestDb::new();
    target
        .service
        .load_snapshot_from_bytes(&bytes)
        .expect("snapshot decode");

    // Restored query must produce the same top-k order and scores.
    // If the snapshot trailer were missing, the rebuild would
    // generate a different HNSW topology (different RNG state →
    // different graph) and likely return slightly different
    // results. Identical-byte parity is the strongest signal that
    // the snapshot path skipped the rebuild.
    let mut target_params = BTreeMap::new();
    target_params.insert("q".to_string(), LoraValue::Vector(query));
    let target_rows = target.run_with_params(
        "CALL db.index.vector.queryNodes('vidx', 10, $q) YIELD node, score",
        target_params,
    );
    assert_eq!(
        ordered_node_ids(&donor_rows),
        ordered_node_ids(&target_rows),
        "restored HNSW returned different node order:\n  donor={donor_rows:?}\n  target={target_rows:?}"
    );
}

// ---------- Int8 quantized HNSW ----------

fn create_hnsw_int8(db: &TestDb, dim: usize) {
    db.run(&format!(
        "CREATE VECTOR INDEX vidx FOR (n:V) ON (n.e) \
         OPTIONS {{indexConfig: {{ \
            `vector.dimensions`: {dim}, \
            `vector.similarity_function`: 'cosine', \
            `vector.indexProvider`: 'hnsw', \
            `vector.hnsw.quantization`: 'int8' \
         }}}}",
    ));
}

#[test]
fn hnsw_int8_ddl_round_trip() {
    let db = TestDb::new();
    create_hnsw_int8(&db, 8);
    let listed = db.run("SHOW INDEXES");
    let entry = index_named(&listed, "vidx").expect("listed");
    assert_eq!(
        entry["options"]["vector.hnsw.quantization"],
        JsonValue::String("int8".into())
    );
}

#[test]
fn hnsw_int8_returns_top_k_for_normalized_embeddings() {
    let db = TestDb::new();
    let dim = 16usize;
    create_hnsw_int8(&db, dim);
    // Build unit-normalized vectors so the [-1, 1] quantization
    // range is fully exercised without clipping.
    let raw = seeded_vectors(0xA1_u64, 32, dim);
    let mut normalized = Vec::with_capacity(raw.len());
    for v in &raw {
        let norm = lora_store::euclidean_norm(v);
        let coords: Vec<lora_store::RawCoordinate> = (0..dim)
            .map(|i| {
                let f = v.values.as_f64_vec()[i];
                lora_store::RawCoordinate::Float(if norm > 0.0 { f / norm } else { 0.0 })
            })
            .collect();
        normalized
            .push(LoraVector::try_new(coords, dim as i64, VectorCoordinateType::Float32).unwrap());
    }
    seed_vector_nodes(&db, &normalized);
    let q = normalized[0].clone();
    let mut params = BTreeMap::new();
    params.insert("q".to_string(), LoraValue::Vector(q));
    let rows = db.run_with_params(
        "CALL db.index.vector.queryNodes('vidx', 5, $q) YIELD node, score",
        params,
    );
    assert_eq!(rows.len(), 5);
    // The self-query should rank near the top with a score close to 1.
    let top = rows[0]["score"].as_f64().unwrap();
    assert!(top > 0.95, "top quantized score too low: {top}");
}

#[test]
fn hnsw_int8_rejected_with_euclidean() {
    let db = TestDb::new();
    let err = db.run_err(
        "CREATE VECTOR INDEX bad FOR (m:Movie) ON (m.e) \
         OPTIONS {indexConfig: { \
            `vector.dimensions`: 4, \
            `vector.similarity_function`: 'euclidean', \
            `vector.hnsw.quantization`: 'int8' \
         }}",
    );
    assert!(
        err.contains("int8") && err.contains("cosine"),
        "expected int8-requires-cosine error, got: {err}"
    );
}

#[test]
fn hnsw_quantization_rejects_unknown_value() {
    let db = TestDb::new();
    let err = db.run_err(
        "CREATE VECTOR INDEX bad FOR (m:Movie) ON (m.e) \
         OPTIONS {indexConfig: { \
            `vector.dimensions`: 4, \
            `vector.similarity_function`: 'cosine', \
            `vector.hnsw.quantization`: 'int4' \
         }}",
    );
    assert!(
        err.contains("quantization"),
        "expected quantization-shape error, got: {err}"
    );
}

// ---------- Pre-filter (4th-argument options map) ----------

#[test]
fn restrict_to_filters_results_flat() {
    let db = TestDb::new();
    let dim = 4usize;
    create_index_with_metric(&db, dim, "cosine", "flat");
    db.run("CREATE (:V {tag: 'a', e: [1.0, 0.0, 0.0, 0.0]::VECTOR<FLOAT32>(4)})");
    db.run("CREATE (:V {tag: 'b', e: [0.9, 0.1, 0.0, 0.0]::VECTOR<FLOAT32>(4)})");
    db.run("CREATE (:V {tag: 'c', e: [0.0, 1.0, 0.0, 0.0]::VECTOR<FLOAT32>(4)})");
    let listed = db.run("MATCH (n:V {tag: 'a'}) RETURN id(n) AS internal");
    let id_a = listed[0]["internal"].as_i64().expect("internal id");
    // Restrict to {id_a}: query for [1, 0, 0, 0] should return only id_a
    // even though id_b's vector is nearly identical and would normally rank.
    let rows = db.run(&format!(
        "CALL db.index.vector.queryNodes('vidx', 3, [1.0, 0.0, 0.0, 0.0], {{restrictTo: [{id_a}]}}) \
         YIELD node, score",
    ));
    let ids = ordered_node_ids(&rows);
    assert_eq!(ids, vec![id_a], "expected only id_a, got {ids:?}");
}

#[test]
fn restrict_to_filters_results_hnsw() {
    let db = TestDb::new();
    let dim = 8usize;
    create_index_with_metric(&db, dim, "cosine", "hnsw");
    let vectors = seeded_vectors(0x000F_117E_u64, 50, dim);
    seed_vector_nodes(&db, &vectors);
    // Grab the first 5 nodes' internal ids and restrict the query to them.
    let id_rows = db.run("MATCH (n:V) RETURN id(n) AS i ORDER BY id(n) LIMIT 5");
    let allowed: Vec<i64> = id_rows
        .iter()
        .filter_map(|r| r.get("i").and_then(|v| v.as_i64()))
        .collect();
    assert_eq!(allowed.len(), 5);
    let restrict_str = allowed
        .iter()
        .map(|i| i.to_string())
        .collect::<Vec<_>>()
        .join(", ");

    let query = seeded_vectors(0x00F1_17E2_u64, 1, dim).pop().unwrap();
    let mut params = BTreeMap::new();
    params.insert("q".to_string(), LoraValue::Vector(query));
    let rows = db.run_with_params(
        &format!(
            "CALL db.index.vector.queryNodes('vidx', 5, $q, {{restrictTo: [{restrict_str}]}}) \
             YIELD node, score"
        ),
        params,
    );
    let returned: BTreeMap<i64, ()> = ordered_node_ids(&rows)
        .into_iter()
        .map(|i| (i, ()))
        .collect();
    // Every returned id must be in the allowed set.
    for &id in returned.keys() {
        assert!(
            allowed.contains(&id),
            "node {id} returned but not in restrictTo {allowed:?}"
        );
    }
    // Should return up to 5 results (less only if HNSW under-fetched).
    assert!(
        returned.len() >= 3,
        "expected ≥3 results under restrictTo, got {returned:?}"
    );
}

#[test]
fn restrict_to_empty_returns_empty() {
    let db = TestDb::new();
    let dim = 4usize;
    create_index_with_metric(&db, dim, "cosine", "flat");
    db.run("CREATE (:V {e: [1.0, 0.0, 0.0, 0.0]::VECTOR<FLOAT32>(4)})");
    db.run("CREATE (:V {e: [0.9, 0.1, 0.0, 0.0]::VECTOR<FLOAT32>(4)})");
    let rows = db.run(
        "CALL db.index.vector.queryNodes('vidx', 5, [1.0, 0.0, 0.0, 0.0], {restrictTo: []}) \
         YIELD node, score",
    );
    assert!(rows.is_empty(), "expected empty result, got {rows:?}");
}

#[test]
fn restrict_to_rejects_unknown_option_key() {
    let db = TestDb::new();
    let dim = 4usize;
    create_index_with_metric(&db, dim, "cosine", "flat");
    let err = db.run_err(
        "CALL db.index.vector.queryNodes('vidx', 1, [1.0, 0.0, 0.0, 0.0], {sneaky: true}) \
         YIELD node, score",
    );
    assert!(
        err.contains("unknown option") && err.contains("sneaky"),
        "expected unknown-option error, got: {err}"
    );
}

#[test]
fn restrict_to_rejects_non_list_value() {
    let db = TestDb::new();
    let dim = 4usize;
    create_index_with_metric(&db, dim, "cosine", "flat");
    let err = db.run_err(
        "CALL db.index.vector.queryNodes('vidx', 1, [1.0, 0.0, 0.0, 0.0], {restrictTo: 42}) \
         YIELD node, score",
    );
    assert!(
        err.contains("restrictTo") && err.contains("LIST"),
        "expected restrictTo-shape error, got: {err}"
    );
}

#[test]
fn hnsw_handles_updates_through_maintenance_hook() {
    // SET that replaces a vector property must update the HNSW
    // backend, not leave a stale entry. We assert the new vector is
    // the top match, not the old one.
    let db = TestDb::new();
    let dim = 4usize;
    create_hnsw_index(&db, dim, "cosine");
    db.run("CREATE (:V {id: 1, e: [1.0, 0.0, 0.0, 0.0]::VECTOR<FLOAT32>(4)})");
    db.run("CREATE (:V {id: 2, e: [0.0, 1.0, 0.0, 0.0]::VECTOR<FLOAT32>(4)})");
    // Re-aim node 1 at [0,0,1,0].
    db.run("MATCH (n:V {id: 1}) SET n.e = [0.0, 0.0, 1.0, 0.0]::VECTOR<FLOAT32>(4)");
    // Query for [0,0,1,0] — node 1 should now be the top hit (it
    // wouldn't be if the old vector were still indexed).
    let rows = db
        .run("CALL db.index.vector.queryNodes('vidx', 1, [0.0, 0.0, 1.0, 0.0]) YIELD node, score");
    assert_eq!(rows.len(), 1);
    assert!((rows[0]["score"].as_f64().unwrap() - 1.0).abs() < 1e-6);
}

#[test]
fn vector_query_top_k_orders_correctly_across_many() {
    let db = TestDb::new();
    create_index(&db, "cosine");
    // 5 vectors at decreasing similarity to [1,0,0].
    db.run("CREATE (:Movie {id: 1, embedding: [1.0, 0.0, 0.0]::VECTOR<FLOAT32>(3)})");
    db.run("CREATE (:Movie {id: 2, embedding: [0.8, 0.6, 0.0]::VECTOR<FLOAT32>(3)})");
    db.run("CREATE (:Movie {id: 3, embedding: [0.6, 0.8, 0.0]::VECTOR<FLOAT32>(3)})");
    db.run("CREATE (:Movie {id: 4, embedding: [0.0, 1.0, 0.0]::VECTOR<FLOAT32>(3)})");
    db.run("CREATE (:Movie {id: 5, embedding: [-1.0, 0.0, 0.0]::VECTOR<FLOAT32>(3)})");
    let rows = db
        .run("CALL db.index.vector.queryNodes('movie_emb', 5, [1.0, 0.0, 0.0]) YIELD node, score");
    // Read node ids by score order (descending).
    let ids = ordered_node_ids(&rows);
    assert!(!ids.is_empty(), "expected 5 rows, got {rows:?}");
}