kglite 0.16.4

Pure-Rust embedded Cypher knowledge graph engine with in-memory, mmap, and disk storage, and agent-facing schema introspection
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
//! Unit tests extracted from schema.rs to keep the production module focused.

use super::*;

#[cfg(test)]
mod type_id_index_tests {
    use super::*;
    use petgraph::graph::NodeIndex;

    fn integer_index() -> TypeIdIndex {
        let mut m = rustc_hash::FxHashMap::default();
        m.insert(1u32, NodeIndex::new(10));
        m.insert(42u32, NodeIndex::new(20));
        TypeIdIndex::Integer(m)
    }

    #[test]
    fn numeric_coercions_retained() {
        let idx = integer_index();
        // UniqueId / Int64 / Float all hit the integer key.
        assert_eq!(idx.get(&Value::UniqueId(42)), Some(NodeIndex::new(20)));
        assert_eq!(idx.get(&Value::Int64(42)), Some(NodeIndex::new(20)));
        assert_eq!(idx.get(&Value::Float64(42.0)), Some(NodeIndex::new(20)));
        // Non-integral float and out-of-range miss.
        assert_eq!(idx.get(&Value::Float64(42.5)), None);
        assert_eq!(idx.get(&Value::Int64(-1)), None);
    }

    #[test]
    fn string_no_longer_coerces_to_int() {
        // Regression lock (0.10.10): a `String` id must NOT be prefix-stripped
        // into the integer index. `{id:'a1'}` / `{id:'Q1'}` must NOT resolve to
        // `UniqueId(1)` — that was the wrong-node false-positive bug.
        let idx = integer_index();
        assert_eq!(idx.get(&Value::String("a1".into())), None);
        assert_eq!(idx.get(&Value::String("x1".into())), None);
        assert_eq!(idx.get(&Value::String("Q1".into())), None);
        assert_eq!(idx.get(&Value::String("1".into())), None);
    }
}

#[cfg(test)]
mod connection_type_compatibility_tests {
    use super::*;

    #[test]
    fn legacy_singular_endpoint_fields_remain_readable() {
        let info: ConnectionTypeInfo = serde_json::from_str(
            r#"{
                "source_type": "Person",
                "target_type": "Company",
                "property_types": {"since": "Int64"}
            }"#,
        )
        .unwrap();

        assert_eq!(info.source_types, HashSet::from(["Person".to_string()]));
        assert_eq!(info.target_types, HashSet::from(["Company".to_string()]));
        assert_eq!(
            info.property_types.get("since").map(String::as_str),
            Some("Int64")
        );
    }
}

#[cfg(test)]
mod maintenance_tests {
    use super::*;
    use crate::graph::storage::{GraphRead, GraphWrite};

    /// Helper: create a DirGraph with N Person nodes and edges between consecutive pairs.
    ///
    /// Built through **`add_nodes`**, the real ingest funnel, rather than by
    /// hand-assembling `NodeData` and pushing into `type_indices`. The hand-built
    /// form bypassed every construction path a user can reach, and since 0.16.0
    /// made construction columnar everywhere, that is the difference between a
    /// fixture whose nodes carry inline `id`/`title` values and one whose nodes
    /// carry the `Value::Null` sentinel with their identity in the type's
    /// `ColumnStore` — i.e. between a fixture that hides the sentinel class and
    /// one that exercises it. Tests here therefore read values through
    /// `node_view`, never off `node_weight` (codingest's 0.16.0 report, ask 4b).
    fn make_test_graph(num_nodes: usize, num_edges: bool) -> DirGraph {
        use crate::datatypes::DataFrame;
        use crate::graph::mutation::maintain::add_nodes;

        let mut g = DirGraph::new();
        if num_nodes > 0 {
            let rows: Vec<Vec<Value>> = (0..num_nodes)
                .map(|i| {
                    vec![
                        Value::UniqueId(i as u32),
                        Value::String(format!("Person_{i}")),
                        Value::Int64(20 + i as i64),
                    ]
                })
                .collect();
            let df = DataFrame::from_cypher_rows(
                vec!["id".to_string(), "title".to_string(), "age".to_string()],
                rows,
            )
            .expect("frame");
            add_nodes(
                &mut g,
                df,
                "Person".to_string(),
                "id".to_string(),
                Some("title".to_string()),
                None,
            )
            .expect("add_nodes");
        }
        if num_edges {
            for i in 0..(num_nodes.saturating_sub(1)) {
                let src = NodeIndex::new(i);
                let tgt = NodeIndex::new(i + 1);
                g.graph.add_edge(
                    src,
                    tgt,
                    EdgeData::new("KNOWS".to_string(), HashMap::new(), &mut g.interner),
                );
            }
        }
        g
    }

    #[test]
    fn test_graph_info_clean() {
        let g = make_test_graph(5, true);
        let info = g.graph_info();
        assert_eq!(info.node_count, 5);
        assert_eq!(info.node_capacity, 5);
        assert_eq!(info.node_tombstones, 0);
        assert_eq!(info.edge_count, 4);
        assert_eq!(info.fragmentation_ratio, 0.0);
        assert_eq!(info.type_count, 1);
    }

    #[test]
    fn test_graph_info_after_deletion() {
        let mut g = make_test_graph(5, false);
        // Delete node 2 — leaves a tombstone
        g.graph.remove_node(NodeIndex::new(2));
        let info = g.graph_info();
        assert_eq!(info.node_count, 4);
        assert_eq!(info.node_capacity, 5); // Still 5 slots
        assert_eq!(info.node_tombstones, 1);
        assert!(info.fragmentation_ratio > 0.19 && info.fragmentation_ratio < 0.21);
    }

    #[test]
    fn test_graph_info_empty() {
        let g = DirGraph::new();
        let info = g.graph_info();
        assert_eq!(info.node_count, 0);
        assert_eq!(info.node_capacity, 0);
        assert_eq!(info.fragmentation_ratio, 0.0);
    }

    #[test]
    fn test_reindex_rebuilds_type_indices() {
        let mut g = make_test_graph(5, false);

        // Manually corrupt type_indices (simulate drift)
        g.type_indices.clear();
        assert!(g.type_indices.is_empty());

        g.reindex();

        // type_indices should be rebuilt
        assert_eq!(g.type_indices.len(), 1);
        assert_eq!(g.type_indices.get("Person").unwrap().len(), 5);
    }

    #[test]
    fn test_reindex_rebuilds_property_indices() {
        let mut g = make_test_graph(5, false);

        // Create a property index
        g.create_index("Person", "age");
        assert!(g.has_index("Person", "age"));

        // Manually corrupt the property index
        g.property_indices
            .get_mut(&("Person".to_string(), "age".to_string()))
            .unwrap()
            .clear();

        g.reindex();

        // Property index should be rebuilt with correct data
        let stats = g.get_index_stats("Person", "age").unwrap();
        assert_eq!(stats.unique_values, 5); // ages 20..24
        assert_eq!(stats.total_entries, 5);
    }

    #[test]
    fn test_reindex_rebuilds_composite_indices() {
        let mut g = make_test_graph(5, false);
        g.create_composite_index("Person", &["age"]);
        assert!(g.has_composite_index("Person", &["age".to_string()]));

        // Corrupt composite index
        g.composite_indices.values_mut().for_each(|v| v.clear());

        g.reindex();

        let stats = g
            .get_composite_index_stats("Person", &["age".to_string()])
            .unwrap();
        assert_eq!(stats.unique_values, 5);
    }

    #[test]
    fn test_reindex_clears_id_indices() {
        let mut g = make_test_graph(3, false);
        g.build_id_index("Person");
        assert!(g.id_indices.contains_key("Person"));

        g.reindex();

        // id_indices should be cleared (lazy rebuild on next access)
        assert!(g.id_indices.is_empty());
    }

    #[test]
    fn test_reindex_after_deletion() {
        let mut g = make_test_graph(5, false);
        // Delete node 2
        g.graph.remove_node(NodeIndex::new(2));
        // type_indices still has the stale entry
        assert_eq!(g.type_indices.get("Person").unwrap().len(), 5);

        g.reindex();

        // Now type_indices should reflect only 4 live nodes
        assert_eq!(g.type_indices.get("Person").unwrap().len(), 4);
        // And none of them should be index 2
        assert!(!g
            .type_indices
            .get("Person")
            .unwrap()
            .contains(&NodeIndex::new(2)));
    }

    #[test]
    fn test_vacuum_noop_when_clean() {
        let mut g = make_test_graph(5, true);
        let mapping = g.vacuum();
        assert!(mapping.is_empty()); // No remapping needed
        assert_eq!(g.graph.node_count(), 5);
        assert_eq!(g.graph_info().node_tombstones, 0);
    }

    /// A vacuum must not change *which backend* the graph has.
    ///
    /// It used to assign `GraphBackend::Memory(..)` unconditionally, which
    /// silently downgraded a mapped graph to heap storage — the user asked for
    /// mmap-backed columns and quietly stopped getting them after the first
    /// auto-vacuum.
    #[test]
    fn test_vacuum_preserves_the_mapped_backend() {
        use crate::graph::storage::mode::{new_dir_graph_in_mode, StorageMode};
        let mut g = new_dir_graph_in_mode(StorageMode::Mapped, None).unwrap();
        for i in 0..5 {
            let data = NodeData::new(
                Value::Int64(i),
                Value::String(format!("n{i}")),
                "Person".to_string(),
                HashMap::new(),
                &mut g.interner,
            );
            g.graph.add_node(data);
        }
        g.graph.remove_node(NodeIndex::new(2));
        assert!(g.graph.is_mapped());

        let mapping = g.vacuum();

        assert_eq!(mapping.len(), 4, "the rebuild must actually have happened");
        assert!(g.graph.is_mapped(), "vacuum must not downgrade the backend");
        assert_eq!(g.graph.node_count(), 4);
        assert_eq!(g.graph_info().node_tombstones, 0);
    }

    /// The severe half of the same bug: a vacuum dropped the `Recording`
    /// wrapper, so a durable graph stopped write-ahead logging for the rest of
    /// the session — silently, with no error and no way for the caller to
    /// notice until a crash lost everything since the last checkpoint.
    #[test]
    fn test_vacuum_preserves_the_write_capture_wrapper() {
        use crate::graph::storage::recording::RecordingGraph;
        let mut g = make_test_graph(5, true);
        let inner = std::mem::replace(&mut g.graph, GraphBackend::new());
        g.graph = GraphBackend::Recording(Box::new(RecordingGraph::new(inner)));
        g.graph.remove_node(NodeIndex::new(2));

        let mapping = g.vacuum();
        assert_eq!(mapping.len(), 4, "the rebuild must actually have happened");
        assert!(
            matches!(g.graph, GraphBackend::Recording(_)),
            "vacuum must not drop the WAL capture wrapper"
        );

        // And the wrapper must still be capturing afterwards.
        let before = g.graph.recorded_ops_len();
        let data = NodeData::new(
            Value::Int64(99),
            Value::String("after".to_string()),
            "Person".to_string(),
            HashMap::new(),
            &mut g.interner,
        );
        g.graph.add_node(data);
        assert!(
            g.graph.recorded_ops_len() > before,
            "writes after a vacuum must still be captured"
        );
    }

    /// Disk keeps its data in frozen CSR mmap, not a `StableDiGraph`. A
    /// petgraph-style rebuild there would both lose the disk root and
    /// materialise the entire graph on the heap — the one thing the backend
    /// exists to avoid. Disk reclaims space by publishing a new generation.
    #[test]
    fn test_vacuum_is_a_noop_on_disk() {
        use crate::graph::storage::mode::{new_dir_graph_in_mode, StorageMode};
        let dir = tempfile::tempdir().unwrap();
        let mut g = new_dir_graph_in_mode(StorageMode::Disk, Some(dir.path())).unwrap();
        assert!(g.graph.is_disk());

        assert!(g.vacuum().is_empty());
        assert!(g.graph.is_disk(), "vacuum must not convert disk to heap");
    }

    #[test]
    fn test_vacuum_compacts_after_deletion() {
        let mut g = make_test_graph(5, true);
        // Delete middle node (creates tombstone)
        g.graph.remove_node(NodeIndex::new(2));
        assert_eq!(g.graph.node_count(), 4);
        assert_eq!(g.graph_info().node_tombstones, 1);

        let mapping = g.vacuum();

        // After vacuum: no tombstones, indices are contiguous
        assert_eq!(g.graph.node_count(), 4);
        assert_eq!(g.graph_info().node_tombstones, 0);
        assert_eq!(g.graph_info().node_capacity, 4);

        // Mapping should have 4 entries (one for each surviving node)
        assert_eq!(mapping.len(), 4);
    }

    #[test]
    fn test_vacuum_preserves_node_data() {
        let mut g = make_test_graph(3, false);
        g.graph.remove_node(NodeIndex::new(1)); // Delete Person_1

        let mapping = g.vacuum();

        // Verify all surviving nodes are present with correct data.
        //
        // Read through `node_view`, not `node_weight`: an ingested node's
        // inline `title` is the `Value::Null` sentinel and its real title lives
        // in the type's `ColumnStore`. Off `node_weight` this loop matches no
        // `Value::String` at all and collects an empty vector — the failure
        // mode is a test that stops observing anything rather than one that
        // observes something wrong.
        let mut titles: Vec<String> = Vec::new();
        for idx in g.graph.node_indices() {
            if let Some(node) = g.graph.node_view(idx) {
                if let Value::String(s) = &*node.title() {
                    titles.push(s.clone());
                }
            }
        }
        titles.sort();
        assert_eq!(titles, vec!["Person_0", "Person_2"]);
        assert_eq!(mapping.len(), 2);
    }

    #[test]
    fn test_vacuum_preserves_edges() {
        let mut g = make_test_graph(4, true);
        // Edges: 0→1, 1→2, 2→3
        // Delete node 0 (and its edge to 1)
        g.graph.remove_node(NodeIndex::new(0));
        // Remaining edges should be 1→2, 2→3

        let _mapping = g.vacuum();

        assert_eq!(g.graph.edge_count(), 2);
        assert_eq!(g.graph.node_count(), 3);
    }

    /// `test_vacuum_preserves_edges` counts edges, which a rebuild that
    /// remaps endpoints *wrongly* still satisfies: reversing every `(src,
    /// tgt)` pair leaves the count untouched and the whole Rust suite green.
    /// Direction is the part of an edge a vacuum can silently corrupt, so
    /// assert the endpoints themselves, by node title so the assertion does
    /// not encode the compaction's index arithmetic.
    #[test]
    fn test_vacuum_preserves_edge_direction() {
        let mut g = make_test_graph(4, true); // 0→1, 1→2, 2→3
        g.graph.remove_node(NodeIndex::new(0)); // leaves 1→2, 2→3

        g.vacuum();

        // `node_view`, not `node_weight` — see `test_vacuum_preserves_node_data`.
        let title = |idx: NodeIndex| match &*g.graph.node_view(idx).unwrap().title() {
            Value::String(s) => s.clone(),
            other => panic!("unexpected title {other:?}"),
        };
        let mut pairs: Vec<(String, String)> = g
            .graph
            .edge_indices()
            .map(|e| {
                let (src, tgt) = g.graph.edge_endpoints(e).unwrap();
                (title(src), title(tgt))
            })
            .collect();
        pairs.sort();
        assert_eq!(
            pairs,
            vec![
                ("Person_1".to_string(), "Person_2".to_string()),
                ("Person_2".to_string(), "Person_3".to_string()),
            ]
        );
    }

    #[test]
    fn test_vacuum_rebuilds_type_indices() {
        let mut g = make_test_graph(5, false);
        g.graph.remove_node(NodeIndex::new(2));

        g.vacuum();

        // type_indices should point to valid, contiguous indices
        assert_eq!(g.type_indices.get("Person").unwrap().len(), 4);
        for idx in g.type_indices.get("Person").unwrap().iter() {
            assert!(g.graph.node_weight(idx).is_some());
        }
    }

    #[test]
    fn test_vacuum_rebuilds_property_indices() {
        let mut g = make_test_graph(5, false);
        g.create_index("Person", "age");
        g.graph.remove_node(NodeIndex::new(2));

        g.vacuum();

        // Property index should still exist with correct entries
        assert!(g.has_index("Person", "age"));
        let stats = g.get_index_stats("Person", "age").unwrap();
        assert_eq!(stats.total_entries, 4); // 5 - 1 deleted
    }

    #[test]
    fn test_vacuum_heavy_fragmentation() {
        let mut g = make_test_graph(100, false);
        // Delete every other node — 50% fragmentation
        for i in (0..100).step_by(2) {
            g.graph.remove_node(NodeIndex::new(i));
        }
        assert_eq!(g.graph.node_count(), 50);
        let info = g.graph_info();
        assert!(info.fragmentation_ratio > 0.49);

        let mapping = g.vacuum();

        assert_eq!(mapping.len(), 50);
        assert_eq!(g.graph.node_count(), 50);
        assert_eq!(g.graph_info().node_tombstones, 0);
        assert_eq!(g.graph_info().fragmentation_ratio, 0.0);
    }

    // ========================================================================
    // Incremental Index Update Tests
    // ========================================================================

    #[test]
    fn test_update_property_indices_for_add() {
        let mut g = DirGraph::new();
        // Add a node and create an index
        let mut props = HashMap::new();
        props.insert("city".to_string(), Value::String("Oslo".to_string()));
        let n0 = g.graph.add_node(NodeData::new(
            Value::Int64(1),
            Value::String("Alice".to_string()),
            "Person".to_string(),
            props,
            &mut g.interner,
        ));
        g.type_indices
            .entry_or_default("Person".to_string())
            .push(n0);
        g.create_index("Person", "city");

        // Add a second node and call the helper
        let mut props2 = HashMap::new();
        props2.insert("city".to_string(), Value::String("Bergen".to_string()));
        let n1 = g.graph.add_node(NodeData::new(
            Value::Int64(2),
            Value::String("Bob".to_string()),
            "Person".to_string(),
            props2,
            &mut g.interner,
        ));
        g.type_indices
            .entry_or_default("Person".to_string())
            .push(n1);
        g.update_property_indices_for_add("Person", n1);

        // Verify index was updated
        let oslo = g.lookup_by_index("Person", "city", &Value::String("Oslo".to_string()));
        assert_eq!(oslo.unwrap().len(), 1);
        let bergen = g.lookup_by_index("Person", "city", &Value::String("Bergen".to_string()));
        let bergen = bergen.unwrap();
        assert_eq!(bergen.len(), 1);
        assert_eq!(bergen[0], n1);
    }

    #[test]
    fn test_update_property_indices_for_set() {
        let mut g = DirGraph::new();
        let mut props = HashMap::new();
        props.insert("city".to_string(), Value::String("Oslo".to_string()));
        let n0 = g.graph.add_node(NodeData::new(
            Value::Int64(1),
            Value::String("Alice".to_string()),
            "Person".to_string(),
            props,
            &mut g.interner,
        ));
        g.type_indices
            .entry_or_default("Person".to_string())
            .push(n0);
        g.create_index("Person", "city");

        // Simulate SET n.city = 'Bergen'
        let old_val = Value::String("Oslo".to_string());
        let new_val = Value::String("Bergen".to_string());
        // Actually change the property on the node
        let city_key = g.interner.get_or_intern("city");
        GraphWrite::set_node_property(&mut g.graph, n0, city_key, new_val.clone());
        g.update_property_indices_for_set("Person", n0, "city", Some(&old_val), &new_val);

        // Verify: Oslo bucket should be empty, Bergen should have the node
        let oslo = g.lookup_by_index("Person", "city", &Value::String("Oslo".to_string()));
        assert!(oslo.is_none() || oslo.unwrap().is_empty());
        let bergen = g.lookup_by_index("Person", "city", &Value::String("Bergen".to_string()));
        assert_eq!(bergen.unwrap(), vec![n0]);
    }

    #[test]
    fn test_update_property_indices_for_remove() {
        let mut g = DirGraph::new();
        let mut props = HashMap::new();
        props.insert("city".to_string(), Value::String("Oslo".to_string()));
        let n0 = g.graph.add_node(NodeData::new(
            Value::Int64(1),
            Value::String("Alice".to_string()),
            "Person".to_string(),
            props,
            &mut g.interner,
        ));
        g.type_indices
            .entry_or_default("Person".to_string())
            .push(n0);
        g.create_index("Person", "city");

        // Simulate REMOVE n.city
        let old_val = Value::String("Oslo".to_string());
        let city_key = g.interner.get_or_intern("city");
        GraphWrite::remove_node_property(&mut g.graph, n0, city_key);
        g.update_property_indices_for_remove("Person", n0, "city", &old_val);

        // Verify: Oslo bucket should be empty
        let oslo = g.lookup_by_index("Person", "city", &Value::String("Oslo".to_string()));
        assert!(oslo.is_none() || oslo.unwrap().is_empty());
    }

    #[test]
    fn test_update_composite_index_on_property_change() {
        let mut g = DirGraph::new();
        let mut props = HashMap::new();
        props.insert("city".to_string(), Value::String("Oslo".to_string()));
        props.insert("age".to_string(), Value::Int64(30));
        let n0 = g.graph.add_node(NodeData::new(
            Value::Int64(1),
            Value::String("Alice".to_string()),
            "Person".to_string(),
            props,
            &mut g.interner,
        ));
        g.type_indices
            .entry_or_default("Person".to_string())
            .push(n0);
        g.create_composite_index("Person", &["city", "age"]);

        // Verify initial state
        let key = (
            "Person".to_string(),
            vec!["city".to_string(), "age".to_string()],
        );
        assert!(g.composite_indices.get(&key).unwrap().len() == 1);

        // Change city to Bergen
        let old_val = Value::String("Oslo".to_string());
        let new_val = Value::String("Bergen".to_string());
        let city_key = g.interner.get_or_intern("city");
        GraphWrite::set_node_property(&mut g.graph, n0, city_key, new_val.clone());
        g.update_property_indices_for_set("Person", n0, "city", Some(&old_val), &new_val);

        // Verify: old composite value gone, new one present
        let comp_map = g.composite_indices.get(&key).unwrap();
        let old_comp = CompositeValue(vec![Value::String("Oslo".to_string()), Value::Int64(30)]);
        let new_comp = CompositeValue(vec![Value::String("Bergen".to_string()), Value::Int64(30)]);
        assert!(!comp_map.contains_key(&old_comp) || comp_map.get(&old_comp).unwrap().is_empty());
        assert_eq!(comp_map.get(&new_comp).unwrap(), &vec![n0]);
    }

    /// A type with no index costs **nothing** to maintain — not "no crash", no
    /// work at all.
    ///
    /// The three updaters used to run in full on such a type: a value read-back
    /// through `property_reader` (which resolves the alias and interns the
    /// resolved name), a `String` for the resolved field, a key-set `Vec`, and
    /// a hash probe per index family — all to edit maps that hold no key for
    /// the type. On a 100k-row `SET` that was ~23% of the statement, 1.8× the
    /// cell write it accompanied.
    ///
    /// The assertion is a call counter rather than a timing because the work is
    /// invisible to every other oracle: it clones no backend, journals nothing
    /// (`note_*` returns early with no index to name) and forks no schema map,
    /// so `BACKEND_CLONE_NODES`, `JOURNAL_NODE_PRE_IMAGES` and
    /// `SCHEMA_MAP_FORKS` all read identically whether it ran or not.
    #[test]
    fn test_no_update_when_no_index_exists() {
        use crate::graph::dir_graph::indexes::{
            index_maintenance_passes, reset_index_maintenance_passes,
        };

        let mut g = DirGraph::new();
        let mut props = HashMap::new();
        props.insert("city".to_string(), Value::String("Oslo".to_string()));
        let n0 = g.graph.add_node(NodeData::new(
            Value::Int64(1),
            Value::String("Alice".to_string()),
            "Person".to_string(),
            props,
            &mut g.interner,
        ));
        g.type_indices
            .entry_or_default("Person".to_string())
            .push(n0);
        // No index created — these must be no-ops, and must not even look.
        reset_index_maintenance_passes();
        g.update_property_indices_for_add("Person", n0);
        g.update_property_indices_for_set(
            "Person",
            n0,
            "city",
            Some(&Value::String("Oslo".to_string())),
            &Value::String("Bergen".to_string()),
        );
        g.update_property_indices_for_remove(
            "Person",
            n0,
            "city",
            &Value::String("Oslo".to_string()),
        );
        assert_eq!(
            index_maintenance_passes(),
            0,
            "a type with no index must skip incremental maintenance outright"
        );
        assert!(g.property_indices.is_empty());
    }

    /// The counter above is not vacuous, and the gate is keyed on the **type**,
    /// not on the graph: a type that *does* carry an index still pays full
    /// maintenance while an index-free type in the same graph pays none.
    ///
    /// Without this arm, "0 passes" would also be the reading for a gate that
    /// disabled index maintenance altogether — which is the way this
    /// optimisation breaks (silently, with a stale index that an indexed
    /// `MATCH` then reads as truth).
    #[test]
    fn index_maintenance_gate_is_per_type() {
        use crate::graph::dir_graph::indexes::{
            index_maintenance_passes, reset_index_maintenance_passes,
        };

        let mut g = DirGraph::new();
        let mut mk = |id: i64, city: &str, node_type: &str| {
            let mut props = HashMap::new();
            props.insert("city".to_string(), Value::String(city.to_string()));
            let idx = g.graph.add_node(NodeData::new(
                Value::Int64(id),
                Value::String(format!("n{id}")),
                node_type.to_string(),
                props,
                &mut g.interner,
            ));
            g.type_indices
                .entry_or_default(node_type.to_string())
                .push(idx);
            idx
        };
        let person = mk(1, "Oslo", "Person");
        let ghost = mk(2, "Oslo", "Ghost");
        g.create_index("Person", "city");

        reset_index_maintenance_passes();
        let old = Value::String("Oslo".to_string());
        let new = Value::String("Bergen".to_string());
        let city = g.interner.get_or_intern("city");
        GraphWrite::set_node_property(&mut g.graph, person, city, new.clone());
        g.update_property_indices_for_set("Person", person, "city", Some(&old), &new);
        assert_eq!(
            index_maintenance_passes(),
            1,
            "an indexed type must still run maintenance"
        );

        // …and the index really moved, which is what the pass was for.
        assert_eq!(
            g.lookup_by_index("Person", "city", &new),
            Some(vec![person])
        );

        // The same write on the index-free type in the same graph: no pass.
        reset_index_maintenance_passes();
        GraphWrite::set_node_property(&mut g.graph, ghost, city, new.clone());
        g.update_property_indices_for_set("Ghost", ghost, "city", Some(&old), &new);
        assert_eq!(
            index_maintenance_passes(),
            0,
            "an index-free type must skip maintenance even when the graph has \
             indexes on other types"
        );
    }

    // ─── Columnar storage tests ──────────────────────────────────────────

    #[test]
    fn test_enable_columnar_preserves_properties() {
        let mut g = make_test_graph(5, false);
        // Add metadata so columnar knows types
        let mut meta = HashMap::new();
        meta.insert("age".to_string(), "int64".to_string());
        g.node_type_metadata_mut()
            .insert("Person".to_string(), meta);
        g.rebuild_type_schemas();

        // Snapshot properties before
        let before: Vec<(Value, Value, i64)> = g
            .type_indices
            .get("Person")
            .unwrap()
            .iter()
            .map(|idx| {
                let n = g.graph.node_view(idx).unwrap();
                let age = n
                    .get_property("age")
                    .map(|c| match c.as_ref() {
                        Value::Int64(v) => *v,
                        _ => panic!("expected Int64"),
                    })
                    .unwrap();
                (n.id().into_owned(), n.title().into_owned(), age)
            })
            .collect();

        g.enable_columnar();
        assert!(g.column_store_count() > 0);

        // Verify properties match
        let after: Vec<(Value, Value, i64)> = g
            .type_indices
            .get("Person")
            .unwrap()
            .iter()
            .map(|idx| {
                let n = g.graph.node_view(idx).unwrap();
                let age = n
                    .get_property("age")
                    .map(|c| match c.as_ref() {
                        Value::Int64(v) => *v,
                        _ => panic!("expected Int64"),
                    })
                    .unwrap();
                (n.id().into_owned(), n.title().into_owned(), age)
            })
            .collect();

        assert_eq!(before, after);
    }

    #[test]
    fn test_columnar_set_property() {
        let mut g = make_test_graph(2, false);
        let mut meta = HashMap::new();
        meta.insert("age".to_string(), "int64".to_string());
        g.node_type_metadata_mut()
            .insert("Person".to_string(), meta);
        g.rebuild_type_schemas();
        g.enable_columnar();

        let idx = g.type_indices.get("Person").unwrap().get(0).unwrap();

        // Update existing property — through the backend, which owns the store.
        let age_key = g.interner.get_or_intern("age");
        GraphWrite::set_node_property(&mut g.graph, idx, age_key, Value::Int64(99));
        assert_eq!(
            g.graph
                .node_view(idx)
                .unwrap()
                .get_property("age")
                .map(|c| c.into_owned()),
            Some(Value::Int64(99))
        );
    }

    #[test]
    fn test_columnar_property_count_and_keys() {
        let mut g = make_test_graph(2, false);
        let mut meta = HashMap::new();
        meta.insert("age".to_string(), "int64".to_string());
        g.node_type_metadata_mut()
            .insert("Person".to_string(), meta);
        g.rebuild_type_schemas();
        g.enable_columnar();

        let idx = g.type_indices.get("Person").unwrap().get(0).unwrap();
        let node = g.graph.node_view(idx).unwrap();

        assert_eq!(node.property_count(), 1); // just "age"
        let keys: Vec<&str> = node.property_keys(&g.interner);
        assert_eq!(keys, vec!["age"]);
    }

    /// A columnar graph round-trips its properties through the **`.kgl` save
    /// path**, which is the only production serializer that meets a columnar
    /// node.
    ///
    /// Replaces `test_columnar_serialize_roundtrip`, which serialized the
    /// backend directly and expected properties in the topology stream. D1
    /// Phase 3 made that impossible: the store belongs to the backend, and
    /// `PropertyStorage` — which is what serde sees — carries only a row id.
    /// `write_kgl_to` therefore writes columns in their own sections and sets
    /// `StripPropertiesGuard` while serializing topology; the `Serialize` impl
    /// `debug_assert!`s that guard is set, so a new save path that forgets it
    /// fails loudly instead of writing empty property maps.
    #[test]
    fn columnar_properties_round_trip_through_the_kgl_save_path() {
        let mut g = make_test_graph(3, false);
        let mut meta = HashMap::new();
        meta.insert("age".to_string(), "int64".to_string());
        g.node_type_metadata_mut()
            .insert("Person".to_string(), meta);
        g.rebuild_type_schemas();
        g.enable_columnar();
        assert!(
            g.column_store_count() > 0,
            "fixture must be columnar, or this is vacuous"
        );

        let mut buf: Vec<u8> = Vec::new();
        crate::graph::io::file::write_kgl_to(&g, &mut buf).unwrap();
        let loaded = crate::graph::io::file::load_kgl_bytes(&buf).unwrap();

        let node0 = loaded.graph.node_view(NodeIndex::new(0)).unwrap();
        assert!(
            node0.get_property("age").is_some(),
            "columnar properties must survive the save path"
        );
        assert_eq!(
            node0.get_property("age").map(|c| c.into_owned()),
            g.graph
                .node_view(NodeIndex::new(0))
                .unwrap()
                .get_property("age")
                .map(|c| c.into_owned()),
            "and must come back with the same value"
        );
    }

    /// Vacuum a **columnar** graph — the combination the `test_vacuum_*` family
    /// above never reaches, because every one of them builds row storage.
    ///
    /// Three things this release changed meet here and nothing else pins their
    /// junction:
    ///
    /// * `GraphBackend::replace_heap_graph` has to *carry* the per-type column
    ///   stores across the swap. Dropping them leaves every node holding a
    ///   `PropertyStorage::Columnar(row_id)` with no store behind it, and
    ///   `NodeView` reads a storeless columnar node as an **empty property
    ///   set** — silently, with no error anywhere.
    /// * the compaction reassigns node indices, so `type_indices` and the row
    ///   ids must still agree afterwards.
    ///
    /// Asserted on the reserved `__id__`/`__title__` columns as well as an
    /// ordinary property, because a consolidated node stores `Null` inline and
    /// gets its identity back from the store — the failure mode is nodes that
    /// survive the vacuum with the right *count* and no identity.
    #[test]
    fn test_vacuum_preserves_columnar_properties_and_identity() {
        let mut g = make_test_graph(6, false);
        let mut meta = HashMap::new();
        meta.insert("age".to_string(), "int64".to_string());
        g.node_type_metadata_mut()
            .insert("Person".to_string(), meta);
        g.rebuild_type_schemas();
        g.enable_columnar();
        assert!(
            g.column_store_count() > 0,
            "fixture must be columnar, or this test is vacuous"
        );

        // Tombstone two nodes so the vacuum has something to compact.
        g.graph.remove_node(NodeIndex::new(1));
        g.graph.remove_node(NodeIndex::new(4));

        let expected: Vec<(Value, Value, Value)> = [0usize, 2, 3, 5]
            .iter()
            .map(|i| {
                (
                    Value::UniqueId(*i as u32),
                    Value::String(format!("Person_{i}")),
                    Value::Int64(20 + *i as i64),
                )
            })
            .collect();

        // Reads through `type_indices`, because that is the route every
        // per-type sweep takes. Stale entries are tolerated (a raw
        // `remove_node` leaves them until the vacuum cleans them) but a *live*
        // node that reads wrong is not.
        let observe = |g: &DirGraph| -> Vec<(Value, Value, Value)> {
            let mut seen: Vec<(Value, Value, Value)> = g
                .type_indices
                .get("Person")
                .expect("Person bucket")
                .iter()
                .filter_map(|idx| {
                    let node = g.graph.node_view(idx)?;
                    Some((
                        node.id().into_owned(),
                        node.title().into_owned(),
                        node.get_property("age")
                            .map(|c| c.into_owned())
                            .unwrap_or(Value::Null),
                    ))
                })
                .collect();
            seen.sort_by(|a, b| format!("{:?}", a.1).cmp(&format!("{:?}", b.1)));
            seen
        };

        assert_eq!(
            observe(&g),
            expected,
            "precondition: readable before vacuum"
        );

        g.vacuum();

        assert!(
            g.column_store_count() > 0,
            "the vacuum must carry the column stores across the heap swap; a \
             storeless columnar node reads as an empty property set, silently"
        );
        assert_eq!(g.graph.node_count(), 4);
        assert_eq!(
            observe(&g),
            expected,
            "every surviving node must keep its id, title and properties across \
             the compaction's index reassignment"
        );
    }
}

#[cfg(test)]
mod embedding_store_tests {
    use super::*;

    #[test]
    fn text_hash_is_deterministic_and_distinguishing() {
        assert_eq!(
            EmbeddingStore::text_hash("hello"),
            EmbeddingStore::text_hash("hello"),
            "same text must hash identically (cross-process stable)"
        );
        assert_ne!(
            EmbeddingStore::text_hash("hello"),
            EmbeddingStore::text_hash("world"),
        );
        assert_ne!(
            EmbeddingStore::text_hash("hello"),
            EmbeddingStore::text_hash("Hello"),
        );
    }

    #[test]
    fn is_stale_covers_missing_changed_and_unhashed() {
        let mut store = EmbeddingStore::new(2);
        let h = EmbeddingStore::text_hash("v1");

        // No embedding yet → stale.
        assert!(store.is_stale(7, h));

        // Embedding present + matching hash → not stale.
        store.set_embedding(7, &[1.0, 2.0]);
        store.set_text_hash(7, h);
        assert!(!store.is_stale(7, h));

        // Text changed (different hash) → stale.
        assert!(store.is_stale(7, EmbeddingStore::text_hash("v2")));

        // Embedding present but no recorded hash (e.g. add_embeddings) → stale,
        // so mode='changed' will (re)hash it on the next pass.
        store.set_embedding(9, &[3.0, 4.0]);
        assert!(store.is_stale(9, EmbeddingStore::text_hash("anything")));
    }

    #[test]
    fn new_store_has_empty_provenance() {
        let store = EmbeddingStore::new(4);
        assert_eq!(store.model_id, None);
        assert!(store.text_hashes.is_empty());
    }

    #[test]
    fn serialized_embedding_shape_requires_exact_data_cardinality() {
        let mut store = EmbeddingStore::new(2);
        store.set_embedding(7, &[1.0, 2.0]);
        assert_eq!(store.validate_shape(), Ok(()));
        store.data.pop();
        assert!(store.validate_shape().is_err());
    }

    #[test]
    fn serialized_embedding_shape_requires_node_slot_bijection() {
        let mut store = EmbeddingStore::new(2);
        store.set_embedding(7, &[1.0, 2.0]);
        store.node_to_slot.insert(7, 1);
        assert!(store.validate_shape().is_err());
    }

    #[test]
    fn malformed_embedding_store_cannot_build_an_index() {
        use crate::graph::algorithms::hnsw::HnswParams;
        use crate::graph::algorithms::vector::DistanceMetric;

        let mut store = EmbeddingStore::new(2);
        store.set_embedding(7, &[1.0, 2.0]);
        store.data.pop();
        let before = format!("{store:?}");
        assert!(store
            .build_index(DistanceMetric::Cosine, HnswParams::default(), 1)
            .is_err());
        assert_eq!(format!("{store:?}"), before);
    }

    #[test]
    fn zero_dimension_store_is_valid_but_cannot_build_an_index() {
        use crate::graph::algorithms::hnsw::HnswParams;
        use crate::graph::algorithms::vector::DistanceMetric;

        let mut store = EmbeddingStore::new(0);
        store.set_embedding(7, &[]);
        assert_eq!(store.validate_shape(), Ok(()));
        let before = format!("{store:?}");
        let error = store
            .build_index(DistanceMetric::Cosine, HnswParams::default(), 1)
            .unwrap_err();
        assert!(error.contains("non-zero embedding dimension"));
        assert_eq!(format!("{store:?}"), before);
    }

    #[test]
    fn invalid_hnsw_parameters_do_not_mutate_embedding_store() {
        use crate::graph::algorithms::hnsw::HnswParams;
        use crate::graph::algorithms::vector::DistanceMetric;

        let mut valid = EmbeddingStore::new(2);
        valid.set_embedding(7, &[1.0, 2.0]);
        let invalid = [
            HnswParams {
                m: 1,
                ..HnswParams::default()
            },
            HnswParams {
                ef_construction: 0,
                ..HnswParams::default()
            },
            HnswParams {
                ef_search: 0,
                ..HnswParams::default()
            },
            HnswParams {
                m: usize::MAX,
                ..HnswParams::default()
            },
        ];
        for params in invalid {
            let mut store = valid.clone();
            let before = format!("{store:?}");
            assert!(store
                .build_index(DistanceMetric::Cosine, params, 1)
                .is_err());
            assert_eq!(format!("{store:?}"), before);
        }
    }
}

/// Incremental index maintenance must file a node under the value an index
/// *rebuild* would file it under — including when the index is registered under
/// an id/title **alias** spelling, where the value the matcher's scan consults
/// is the node's id/title and not the verbatim property.
///
/// Before the fix, `update_property_indices_for_add` / `_for_set` read the node
/// by the user-facing key (`NodeData::get_property`) while `create_index` /
/// `refresh_indexes_for_type` read through the alias-resolving `read_indexed`.
/// A Cypher `CREATE` into such a type filed the node under a bucket value the
/// scan can never produce: indexed `MATCH` returned rows the same query without
/// the index did not, and the two spellings of the same predicate (inline map
/// vs `WHERE`) disagreed with each other.
#[cfg(test)]
mod alias_index_maintenance_tests {
    use super::*;
    use crate::datatypes::DataFrame;
    use crate::graph::mutation::maintain::add_nodes;
    use crate::graph::session::{execute_mut, execute_read, ExecuteOptions};
    use petgraph::graph::NodeIndex;

    /// `Term` carries both alias kinds: `term_id` is its id field and
    /// `term_name` its title field, so `add_nodes` hoists both columns off the
    /// property map. `city` is an ordinary property, for the non-alias arms.
    fn aliased_graph() -> DirGraph {
        let rows: Vec<Vec<Value>> = (1..=3)
            .map(|i| {
                vec![
                    Value::Int64(i),
                    Value::String(format!("term-{i}")),
                    Value::String("Oslo".to_string()),
                ]
            })
            .collect();
        let df = DataFrame::from_cypher_rows(
            vec![
                "term_id".to_string(),
                "term_name".to_string(),
                "city".to_string(),
            ],
            rows,
        )
        .expect("frame");
        let mut graph = DirGraph::new();
        add_nodes(
            &mut graph,
            df,
            "Term".to_string(),
            "term_id".to_string(),
            Some("term_name".to_string()),
            None,
        )
        .expect("add_nodes");
        graph
    }

    fn run(graph: &mut DirGraph, statement: &str) {
        let params = HashMap::new();
        execute_mut(graph, statement, &ExecuteOptions::eager(&params))
            .unwrap_or_else(|error| panic!("{statement}: {error}"));
    }

    fn count(graph: &DirGraph, query: &str) -> usize {
        let params = HashMap::new();
        let result = execute_read(graph, query, &ExecuteOptions::eager(&params))
            .unwrap_or_else(|error| panic!("{query}: {error}"));
        result.result.rows.len()
    }

    /// A graph carrying `index_on`, mutated by `mutations`.
    fn mutated(index_on: &str, mutations: &[&str]) -> DirGraph {
        let mut graph = aliased_graph();
        graph.create_index("Term", index_on);
        for statement in mutations {
            run(&mut graph, statement);
        }
        graph
    }

    /// The divergence oracle: ask `query` on the mutated graph with the index
    /// installed, then drop the index — same data, same query — and ask again.
    fn assert_index_agrees_with_scan(index_on: &str, mutations: &[&str], query: &str) {
        let mut graph = mutated(index_on, mutations);
        let with_index = count(&graph, query);
        graph.drop_index("Term", index_on).expect("drop");
        let scanned = count(&graph, query);
        assert_eq!(
            with_index, scanned,
            "indexed and unindexed answers diverge after {mutations:?} for: {query}"
        );
    }

    /// One property index's buckets in comparable form: (value, members) rows.
    type BucketRows = Vec<(Value, Vec<NodeIndex>)>;

    /// Every property-index bucket, in a comparable, order-normalised form.
    fn index_snapshot(graph: &DirGraph) -> Vec<(IndexKey, BucketRows)> {
        let mut snapshot: Vec<(IndexKey, BucketRows)> = graph
            .property_indices
            .iter()
            .map(|(key, index)| {
                let mut buckets: Vec<(Value, Vec<NodeIndex>)> = index
                    .iter()
                    .map(|(value, members)| {
                        let mut members = members.clone();
                        members.sort();
                        (value.clone(), members)
                    })
                    .collect();
                buckets.sort();
                (key.clone(), buckets)
            })
            .collect();
        snapshot.sort();
        snapshot
    }

    const CREATE_X: &str = "CREATE (:Term {term_id: 99, term_name: 'created-x'})";
    const CREATE_COLLIDING: &str = "CREATE (:Term {term_id: 98, term_name: 'term-1'})";

    #[test]
    fn cypher_create_on_a_title_aliased_index_agrees_with_the_scan() {
        // The phantom: the CREATE's verbatim `term_name` becomes a bucket the
        // scan can never produce, so the indexed MATCH returns a row the
        // unindexed one does not.
        assert_index_agrees_with_scan(
            "term_name",
            &[CREATE_X],
            "MATCH (t:Term {term_name: 'created-x'}) RETURN t",
        );
        assert_index_agrees_with_scan(
            "term_name",
            &[CREATE_X],
            "MATCH (t:Term) WHERE t.term_name = 'created-x' RETURN t",
        );
    }

    #[test]
    fn a_cypher_create_does_not_poison_an_existing_bucket() {
        // The poisoned bucket: the CREATE's verbatim value equals an existing
        // node's title, so the index reports two members for a value only one
        // node really carries.
        assert_index_agrees_with_scan(
            "term_name",
            &[CREATE_COLLIDING],
            "MATCH (t:Term {term_name: 'term-1'}) RETURN t",
        );
        assert_index_agrees_with_scan(
            "term_name",
            &[CREATE_COLLIDING],
            "MATCH (t:Term) WHERE t.term_name = 'term-1' RETURN t",
        );
    }

    #[test]
    fn the_two_spellings_of_one_predicate_agree_on_an_indexed_graph() {
        // Inline map vs WHERE: only the first consults the index, so a poisoned
        // index makes the same predicate answer two different ways.
        let graph = mutated("term_name", &[CREATE_X, CREATE_COLLIDING]);
        for value in ["created-x", "term-1", "term-2"] {
            let inline = count(
                &graph,
                &format!("MATCH (t:Term {{term_name: '{value}'}}) RETURN t"),
            );
            let filtered = count(
                &graph,
                &format!("MATCH (t:Term) WHERE t.term_name = '{value}' RETURN t"),
            );
            assert_eq!(
                inline, filtered,
                "inline-map and WHERE spellings disagree for term_name = '{value}'"
            );
        }
    }

    #[test]
    fn cypher_set_on_a_title_aliased_index_agrees_with_the_scan() {
        let set = "MATCH (t:Term {term_id: 1}) SET t.term_name = 'renamed'";
        for query in [
            "MATCH (t:Term {term_name: 'renamed'}) RETURN t",
            "MATCH (t:Term) WHERE t.term_name = 'renamed' RETURN t",
            "MATCH (t:Term {term_name: 'term-1'}) RETURN t",
            "MATCH (t:Term) WHERE t.term_name = 'term-1' RETURN t",
        ] {
            assert_index_agrees_with_scan("term_name", &[CREATE_X, set], query);
        }
    }

    #[test]
    fn cypher_set_on_the_title_keeps_an_alias_spelled_index_in_step() {
        // The index is registered under the alias spelling but holds titles, so
        // a write to `title` moves the node between its buckets.
        let set = "MATCH (t:Term {term_id: 1}) SET t.title = 'retitled'";
        for query in [
            "MATCH (t:Term {term_name: 'retitled'}) RETURN t",
            "MATCH (t:Term) WHERE t.term_name = 'retitled' RETURN t",
            "MATCH (t:Term {term_name: 'term-1'}) RETURN t",
        ] {
            assert_index_agrees_with_scan("term_name", &[set], query);
        }
    }

    #[test]
    fn cypher_set_on_the_name_synonym_moves_the_node_between_buckets() {
        // `name` is Cypher's own spelling of the title and the SET executor
        // writes the title through it, so — unlike an arbitrary alias spelling —
        // this write *does* move the field the index holds. A type that names
        // its title column `name` gets both readings of the same word.
        let rows: Vec<Vec<Value>> = (1..=3)
            .map(|i| vec![Value::Int64(i), Value::String(format!("person-{i}"))])
            .collect();
        let df =
            DataFrame::from_cypher_rows(vec!["person_id".to_string(), "name".to_string()], rows)
                .expect("frame");
        let mut graph = DirGraph::new();
        add_nodes(
            &mut graph,
            df,
            "Person".to_string(),
            "person_id".to_string(),
            Some("name".to_string()),
            None,
        )
        .expect("add_nodes");
        graph.create_index("Person", "name");
        run(
            &mut graph,
            "MATCH (p:Person {name: 'person-1'}) SET p.name = 'renamed'",
        );

        assert_eq!(
            count(&graph, "MATCH (p:Person {name: 'renamed'}) RETURN p"),
            1,
            "the index lost the node its SET renamed"
        );
        assert_eq!(
            count(&graph, "MATCH (p:Person {name: 'person-1'}) RETURN p"),
            0,
            "the index still answers with the pre-SET value"
        );
    }

    #[test]
    fn cypher_remove_on_a_title_aliased_index_agrees_with_the_scan() {
        let remove = "MATCH (t:Term {term_id: 1}) REMOVE t.term_name";
        for query in [
            "MATCH (t:Term {term_name: 'term-1'}) RETURN t",
            "MATCH (t:Term) WHERE t.term_name = 'term-1' RETURN t",
        ] {
            assert_index_agrees_with_scan("term_name", &[remove], query);
        }
    }

    #[test]
    fn incremental_maintenance_reproduces_a_rebuilt_index() {
        // The general contract, of which every case above is an instance:
        // whatever the writes were, the incrementally maintained index must
        // equal the one `refresh_indexes_for_type` builds from live state.
        let mutations = [
            CREATE_X,
            CREATE_COLLIDING,
            "MATCH (t:Term {term_id: 1}) SET t.term_name = 'renamed'",
            "MATCH (t:Term {term_id: 2}) SET t.title = 'retitled'",
            "MATCH (t:Term {term_id: 3}) SET t.city = 'Bergen'",
            "MATCH (t:Term {term_id: 99}) REMOVE t.city",
        ];
        let mut graph = aliased_graph();
        graph.create_index("Term", "term_name");
        graph.create_index("Term", "term_id");
        graph.create_index("Term", "city");
        for statement in mutations {
            run(&mut graph, statement);
        }

        let incremental = index_snapshot(&graph);
        graph.refresh_indexes_for_type("Term");
        assert_eq!(
            incremental,
            index_snapshot(&graph),
            "incremental index maintenance diverged from a rebuild"
        );
    }

    #[test]
    fn a_created_node_joins_a_doubly_indexed_property_once() {
        // A property carrying both a hash and a range index appears twice in
        // the key iteration; the node must still land in each bucket once, or
        // an indexed MATCH returns it twice.
        let mut graph = aliased_graph();
        graph.create_index("Term", "city");
        graph.create_range_index("Term", "city");
        run(&mut graph, "CREATE (:Term {term_id: 99, city: 'Bergen'})");

        let bucket = graph
            .lookup_by_index("Term", "city", &Value::String("Bergen".to_string()))
            .expect("bucket");
        assert_eq!(bucket.len(), 1, "hash-index bucket holds the node twice");
        let range = graph
            .lookup_range(
                "Term",
                "city",
                std::ops::Bound::Included(&Value::String("Bergen".to_string())),
                std::ops::Bound::Included(&Value::String("Bergen".to_string())),
            )
            .expect("range bucket");
        assert_eq!(range.len(), 1, "range-index bucket holds the node twice");
        assert_eq!(
            count(&graph, "MATCH (t:Term {city: 'Bergen'}) RETURN t"),
            1,
            "indexed MATCH returned the node more than once"
        );
    }

    /// Every node the index still points at, across all buckets.
    fn indexed_members(graph: &DirGraph, property: &str) -> Vec<NodeIndex> {
        graph
            .property_indices
            .get(&("Term".to_string(), property.to_string()))
            .expect("index")
            .iter()
            .flat_map(|(_, members)| members.iter().copied())
            .collect()
    }

    #[test]
    fn deleting_every_node_empties_the_buckets() {
        // The add/remove symmetry: whichever bucket a write filed a node under,
        // the delete has to reclaim it. Deletion evicts by *membership*, so it
        // is value-agnostic — this pins that it stays that way.
        let mut graph = mutated("term_name", &[CREATE_X]);
        run(&mut graph, "MATCH (t:Term) DELETE t");

        assert!(
            indexed_members(&graph, "term_name").is_empty(),
            "deleted nodes left entries behind: {:?}",
            indexed_members(&graph, "term_name")
        );
        assert_eq!(count(&graph, "MATCH (t:Term) RETURN t"), 0);
    }

    #[test]
    fn deleting_one_node_leaves_the_survivors_indexed() {
        let mut graph = mutated("term_name", &[CREATE_X]);
        run(&mut graph, "MATCH (t:Term {term_id: 1}) DELETE t");

        let members = indexed_members(&graph, "term_name");
        assert!(
            members.iter().all(|idx| graph.get_node(*idx).is_some()),
            "the index points at a deleted node: {members:?}"
        );
        assert_index_agrees_with_scan(
            "term_name",
            &[CREATE_X, "MATCH (t:Term {term_id: 1}) DELETE t"],
            "MATCH (t:Term {term_name: 'term-1'}) RETURN t",
        );
    }
}

#[cfg(test)]
mod soft_alias_names_tests {
    use super::*;

    /// [`SOFT_ALIAS_NAMES`] and [`soft_alias_fallback`] are two views of one
    /// fact, and a projection that completes from the *list* while resolution
    /// reads the *match* would silently stop recovering a name that fell out of
    /// sync. Both directions are checked: every listed name classifies, and
    /// nothing outside the list does (over the property names an engine
    /// actually sees — the identity fields, and the structural names' near
    /// misses).
    #[test]
    fn soft_alias_names_match_the_classifier() {
        for name in SOFT_ALIAS_NAMES {
            assert!(
                soft_alias_fallback(name).is_some(),
                "{name} is listed as a soft alias but does not classify as one"
            );
        }
        for name in [
            "id",
            "title",
            "names",
            "Name",
            "labels",
            "nodetype",
            "node_types",
            "",
        ] {
            assert!(
                soft_alias_fallback(name).is_none(),
                "{name} classifies as a soft alias but is not listed in SOFT_ALIAS_NAMES"
            );
        }
    }
}