velesdb-core 1.15.0

High-performance vector database engine written in Rust
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
//! Tests for `CsrSnapshot` and `SnapshotBuilder` (Task 1).
//!
//! Unit tests validate specific examples and edge cases.
//! Property-based tests validate structural invariants across random inputs.

#![allow(
    clippy::cast_precision_loss,
    clippy::cast_possible_truncation,
    clippy::cast_sign_loss,
    clippy::needless_pass_by_value,
    clippy::redundant_closure_for_method_calls,
    clippy::useless_vec,
    clippy::similar_names,
    clippy::module_name_repetitions
)]

use super::csr_snapshot::SnapshotBuilder;
use super::edge::{EdgeStore, GraphEdge};
use super::label_table::LabelTable;
use super::traversal::{bfs_traverse, TraversalConfig};
use super::traversal_csr::bfs_traverse_csr;
use std::collections::HashSet;

// =============================================================================
// Unit tests — Task 1.4
// =============================================================================

/// Empty EdgeStore → offsets == [0], all arrays empty.
#[test]
fn test_csr_empty_graph() {
    let store = EdgeStore::new();
    let label_table = LabelTable::new();
    let snapshot = SnapshotBuilder::build(&store, &label_table);

    assert_eq!(snapshot.offsets(), &[0]);
    assert_eq!(snapshot.node_count(), 0);
    assert_eq!(snapshot.edge_count(), 0);
}

/// SnapshotBuilder::empty() produces a valid empty snapshot.
#[test]
fn test_csr_snapshot_empty() {
    let snapshot = SnapshotBuilder::empty();

    assert_eq!(snapshot.offsets(), &[0]);
    assert_eq!(snapshot.node_count(), 0);
    assert_eq!(snapshot.edge_count(), 0);
    assert!(snapshot.neighbors(42).is_empty());
    assert!(snapshot.edge_ids(42).is_empty());
    assert!(snapshot.label_ids(42).is_empty());
    assert_eq!(snapshot.degree(42), 0);
    assert!(!snapshot.contains_node(42));
}

/// Node that is a source but has no outgoing edges after filtering
/// (e.g., all edge IDs in outgoing map point to removed edges).
/// In practice, EdgeStore keeps outgoing in sync, so a node with
/// an empty outgoing vec still appears in the CSR with degree 0.
#[test]
fn test_csr_single_node_no_edges() {
    // A node that only appears as a target (no outgoing edges)
    let mut store = EdgeStore::new();
    store
        .add_edge(GraphEdge::new(1, 100, 200, "KNOWS").expect("valid"))
        .expect("add");

    let label_table = LabelTable::new();
    let snapshot = SnapshotBuilder::build(&store, &label_table);

    // Node 100 has outgoing edges, node 200 does not
    assert!(snapshot.contains_node(100));
    assert!(!snapshot.contains_node(200)); // 200 is only a target
    assert_eq!(snapshot.degree(100), 1);
    assert_eq!(snapshot.degree(200), 0);
}

/// Known graph: verify O(1) slice access returns correct neighbors.
#[test]
fn test_csr_neighbors_returns_correct_slice() {
    let mut store = EdgeStore::new();
    store
        .add_edge(GraphEdge::new(10, 1, 2, "KNOWS").expect("valid"))
        .expect("add");
    store
        .add_edge(GraphEdge::new(11, 1, 3, "LIKES").expect("valid"))
        .expect("add");
    store
        .add_edge(GraphEdge::new(12, 2, 3, "KNOWS").expect("valid"))
        .expect("add");
    store
        .add_edge(GraphEdge::new(13, 3, 1, "FOLLOWS").expect("valid"))
        .expect("add");

    let label_table = LabelTable::new();
    let snapshot = SnapshotBuilder::build(&store, &label_table);

    // Node 1 → {2, 3}
    let n1: HashSet<u64> = snapshot.neighbors(1).iter().copied().collect();
    assert_eq!(n1, HashSet::from([2, 3]));
    assert_eq!(snapshot.degree(1), 2);

    // Node 2 → {3}
    assert_eq!(snapshot.neighbors(2), &[3]);
    assert_eq!(snapshot.degree(2), 1);

    // Node 3 → {1}
    assert_eq!(snapshot.neighbors(3), &[1]);
    assert_eq!(snapshot.degree(3), 1);

    // Edge IDs parallel to neighbors
    let eids_1: HashSet<u64> = snapshot.edge_ids(1).iter().copied().collect();
    assert_eq!(eids_1, HashSet::from([10, 11]));

    // Label IDs parallel to neighbors
    let lids = snapshot.label_ids(1);
    assert_eq!(lids.len(), 2);

    // Total counts
    assert_eq!(snapshot.node_count(), 3); // sources: 1, 2, 3
    assert_eq!(snapshot.edge_count(), 4);
}

/// Nonexistent node_id → empty slices.
#[test]
fn test_csr_unknown_node_returns_empty() {
    let mut store = EdgeStore::new();
    store
        .add_edge(GraphEdge::new(1, 100, 200, "A").expect("valid"))
        .expect("add");

    let label_table = LabelTable::new();
    let snapshot = SnapshotBuilder::build(&store, &label_table);

    assert!(snapshot.neighbors(999).is_empty());
    assert!(snapshot.edge_ids(999).is_empty());
    assert!(snapshot.label_ids(999).is_empty());
    assert_eq!(snapshot.degree(999), 0);
    assert!(!snapshot.contains_node(999));
}

/// label_at returns correct labels for each neighbor position.
#[test]
fn test_csr_label_at_correct() {
    let mut store = EdgeStore::new();
    store
        .add_edge(GraphEdge::new(10, 1, 2, "KNOWS").expect("valid"))
        .expect("add");
    store
        .add_edge(GraphEdge::new(11, 1, 3, "FOLLOWS").expect("valid"))
        .expect("add");

    let label_table = LabelTable::new();
    let snapshot = SnapshotBuilder::build(&store, &label_table);

    let targets = snapshot.neighbors(1);
    let eids = snapshot.edge_ids(1);

    // Each label_at should match the original edge's label
    for (i, &eid) in eids.iter().enumerate() {
        let label = snapshot.label_at(1, i).expect("label exists");
        let edge = store.get_edge(eid).expect("edge exists");
        assert_eq!(
            label,
            edge.label(),
            "label mismatch at position {i} for target {}",
            targets[i]
        );
    }

    // Out of range
    assert!(snapshot.label_at(1, 10).is_none());
    assert!(snapshot.label_at(999, 0).is_none());
}

/// has_label checks the interned label table.
#[test]
fn test_csr_has_label() {
    let mut store = EdgeStore::new();
    store
        .add_edge(GraphEdge::new(10, 1, 2, "KNOWS").expect("valid"))
        .expect("add");
    store
        .add_edge(GraphEdge::new(11, 2, 3, "FOLLOWS").expect("valid"))
        .expect("add");

    let label_table = LabelTable::new();
    let snapshot = SnapshotBuilder::build(&store, &label_table);

    assert!(snapshot.has_label("KNOWS"));
    assert!(snapshot.has_label("FOLLOWS"));
    assert!(!snapshot.has_label("LIKES"));
}

/// Deterministic layout: nodes are sorted by ID.
#[test]
fn test_csr_deterministic_node_order() {
    let mut store = EdgeStore::new();
    // Add edges in non-sorted order
    store
        .add_edge(GraphEdge::new(1, 300, 400, "C").expect("valid"))
        .expect("add");
    store
        .add_edge(GraphEdge::new(2, 100, 200, "A").expect("valid"))
        .expect("add");
    store
        .add_edge(GraphEdge::new(3, 200, 300, "B").expect("valid"))
        .expect("add");

    let label_table = LabelTable::new();
    let snapshot = SnapshotBuilder::build(&store, &label_table);

    // offsets should reflect sorted node order: 100, 200, 300
    assert_eq!(snapshot.node_count(), 3);
    assert!(snapshot.contains_node(100));
    assert!(snapshot.contains_node(200));
    assert!(snapshot.contains_node(300));

    // Offsets should be monotonically increasing
    let offsets = snapshot.offsets();
    for i in 0..offsets.len() - 1 {
        assert!(offsets[i] <= offsets[i + 1], "offsets not monotone at {i}");
    }
}

// =============================================================================
// Property-based tests — Tasks 1.2 and 1.3
// =============================================================================

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

    /// Generates a random `(EdgeStore, LabelTable)` with 1-50 nodes and 0-200 edges.
    fn arb_edge_store() -> impl Strategy<Value = (EdgeStore, LabelTable)> {
        // Generate node count, then edge list
        (1_u64..=50, 0_usize..=200).prop_flat_map(|(max_node, edge_count)| {
            let labels = vec!["KNOWS", "FOLLOWS", "LIKES", "WORKS_AT", "CREATED"];
            prop::collection::vec(
                (1..=max_node, 1..=max_node, 0..labels.len()),
                0..=edge_count,
            )
            .prop_map(move |edges| {
                let mut store = EdgeStore::new();
                let label_table = LabelTable::new();
                let labels = vec!["KNOWS", "FOLLOWS", "LIKES", "WORKS_AT", "CREATED"];
                for (i, (src, tgt, label_idx)) in edges.into_iter().enumerate() {
                    let label = labels[label_idx];
                    // Use index as edge ID to guarantee uniqueness
                    let eid = (i + 1) as u64;
                    if let Ok(edge) = GraphEdge::new(eid, src, tgt, label) {
                        let _ = store.add_edge(edge);
                    }
                }
                (store, label_table)
            })
        })
    }

    // Feature: graph-traversal-v2, Property 1: CSR structural invariants
    // **Validates: Requirements 1.1, 1.2**
    proptest! {
        #![proptest_config(ProptestConfig::with_cases(100))]
        #[test]
        fn prop_csr_structural_invariants((store, label_table) in arb_edge_store()) {
            let snapshot = SnapshotBuilder::build(&store, &label_table);

            let total_edges = snapshot.edge_count();
            let node_count = snapshot.node_count();
            let offsets = snapshot.offsets();

            // Parallel arrays: neighbors, edge_ids, label_ids all same length per node.
            let mut sum_degrees = 0usize;
            let source_nodes: HashSet<u64> = store.all_edges().iter().map(|e| e.source()).collect();
            for &nid in &source_nodes {
                if snapshot.contains_node(nid) {
                    let n = snapshot.neighbors(nid).len();
                    let e = snapshot.edge_ids(nid).len();
                    let l = snapshot.label_ids(nid).len();
                    prop_assert_eq!(n, e);
                    prop_assert_eq!(n, l);
                    sum_degrees += n;
                }
            }
            prop_assert_eq!(sum_degrees, total_edges);

            // offsets.len() == M + 1
            prop_assert_eq!(offsets.len(), node_count + 1);

            // offsets is monotonically non-decreasing
            for i in 0..offsets.len() - 1 {
                prop_assert!(offsets[i] <= offsets[i + 1],
                    "offsets not monotone at index {}: {} > {}", i, offsets[i], offsets[i + 1]);
            }

            // offsets[M] == N (last offset == total edges)
            prop_assert_eq!(*offsets.last().unwrap(), total_edges);

            // Per-node degree matches EdgeStore
            for &nid in &source_nodes {
                if snapshot.contains_node(nid) {
                    let csr_degree = snapshot.degree(nid);
                    let store_degree = store.get_outgoing(nid).len();
                    prop_assert_eq!(csr_degree, store_degree);
                }
            }
        }
    }

    // Feature: graph-traversal-v2, Property 2: Round-trip CsrSnapshot ↔ EdgeStore
    // **Validates: Requirements 1.6**
    proptest! {
        #![proptest_config(ProptestConfig::with_cases(100))]
        #[test]
        fn prop_csr_round_trip((store, label_table) in arb_edge_store()) {
            let snapshot = SnapshotBuilder::build(&store, &label_table);

            // For each source node in the EdgeStore, verify the CSR contains
            // the same set of (target, edge_id) pairs.
            let source_nodes: HashSet<u64> = store.all_edges().iter().map(|e| e.source()).collect();

            for &nid in &source_nodes {
                let csr_targets = snapshot.neighbors(nid);
                let csr_eids = snapshot.edge_ids(nid);

                // Build set from CSR
                let csr_set: HashSet<(u64, u64)> = csr_targets
                    .iter()
                    .zip(csr_eids.iter())
                    .map(|(&t, &e)| (t, e))
                    .collect();

                // Build set from EdgeStore
                let store_set: HashSet<(u64, u64)> = store
                    .get_outgoing(nid)
                    .iter()
                    .map(|e| (e.target(), e.id()))
                    .collect();

                prop_assert_eq!(csr_set, store_set);
            }
        }
    }

    /// Generates a valid `TraversalConfig` with `min_depth <= max_depth` and `limit > 0`.
    fn arb_traversal_config() -> impl Strategy<Value = TraversalConfig> {
        (1_u32..=5, 0_u32..=3, 1_usize..=50).prop_map(|(max_depth, min_offset, limit)| {
            let min_depth = if min_offset >= max_depth {
                1
            } else {
                max_depth - min_offset
            };
            TraversalConfig {
                min_depth,
                max_depth,
                limit,
                rel_types: Vec::new(),
            }
        })
    }

    // Feature: graph-traversal-v2, Property 3: BFS equivalence CSR vs EdgeStore
    // **Validates: Requirements 2.1, 2.4**
    proptest! {
        #![proptest_config(ProptestConfig::with_cases(100))]
        #[test]
        fn prop_bfs_equivalence(
            (store, label_table) in arb_edge_store(),
            config in arb_traversal_config(),
        ) {
            let snapshot = SnapshotBuilder::build(&store, &label_table);

            // Pick a source node from the store (first source node, or skip if empty).
            let source_nodes: Vec<u64> = store.all_edges().iter().map(|e| e.source()).collect();
            if let Some(&source_id) = source_nodes.first() {
                let csr_results = bfs_traverse_csr(&snapshot, source_id, &config);
                let store_results = bfs_traverse(&store, source_id, &config);

                // Compare as sets of (target_id, depth).
                let csr_set: HashSet<(u64, u32)> = csr_results
                    .iter()
                    .map(|r| (r.target_id, r.depth))
                    .collect();
                let store_set: HashSet<(u64, u32)> = store_results
                    .iter()
                    .map(|r| (r.target_id, r.depth))
                    .collect();

                prop_assert_eq!(csr_set, store_set,
                    "BFS equivalence failed for source={}, config=({},{}), limit={}",
                    source_id, config.min_depth, config.max_depth, config.limit);
            }
        }
    }

    // Feature: graph-traversal-v2, Property 4: BFS limit invariant
    // **Validates: Requirements 2.5**
    proptest! {
        #![proptest_config(ProptestConfig::with_cases(100))]
        #[test]
        fn prop_bfs_limit(
            (store, label_table) in arb_edge_store(),
            limit in 1_usize..100,
        ) {
            let snapshot = SnapshotBuilder::build(&store, &label_table);

            let source_nodes: Vec<u64> = store.all_edges().iter().map(|e| e.source()).collect();
            if let Some(&source_id) = source_nodes.first() {
                let config = TraversalConfig::with_range(1, 5).with_limit(limit);
                let results = bfs_traverse_csr(&snapshot, source_id, &config);

                prop_assert!(results.len() <= limit,
                    "BFS returned {} results but limit was {}",
                    results.len(), limit);
            }
        }
    }
}

// =============================================================================
// Unit tests — Task 3.4: bfs_traverse_csr
// =============================================================================

/// Source not in snapshot → empty Vec.
#[test]
fn test_bfs_csr_missing_source() {
    let mut store = EdgeStore::new();
    store
        .add_edge(GraphEdge::new(1, 10, 20, "KNOWS").expect("valid"))
        .expect("add");

    let label_table = LabelTable::new();
    let snapshot = SnapshotBuilder::build(&store, &label_table);

    let config = TraversalConfig::with_range(1, 3);
    let results = bfs_traverse_csr(&snapshot, 999, &config);
    assert!(results.is_empty(), "missing source should return empty Vec");
}

/// Verify min_depth/max_depth filtering on a known graph.
#[test]
fn test_bfs_csr_depth_range() {
    // Chain: 1 -> 2 -> 3 -> 4
    let mut store = EdgeStore::new();
    store
        .add_edge(GraphEdge::new(100, 1, 2, "KNOWS").expect("valid"))
        .expect("add");
    store
        .add_edge(GraphEdge::new(101, 2, 3, "KNOWS").expect("valid"))
        .expect("add");
    store
        .add_edge(GraphEdge::new(102, 3, 4, "KNOWS").expect("valid"))
        .expect("add");

    let label_table = LabelTable::new();
    let snapshot = SnapshotBuilder::build(&store, &label_table);

    // min_depth=2, max_depth=3 → should only return nodes at depth 2 and 3
    let config = TraversalConfig::with_range(2, 3);
    let results = bfs_traverse_csr(&snapshot, 1, &config);

    assert!(
        !results.iter().any(|r| r.depth < 2),
        "no results below min_depth"
    );
    assert!(
        results.iter().any(|r| r.target_id == 3 && r.depth == 2),
        "node 3 at depth 2"
    );
    assert!(
        results.iter().any(|r| r.target_id == 4 && r.depth == 3),
        "node 4 at depth 3"
    );
}

/// Verify results.len() <= limit.
#[test]
fn test_bfs_csr_limit_respected() {
    // Star graph: 1 -> {2, 3, 4, 5, 6}
    let mut store = EdgeStore::new();
    for i in 2..=6 {
        store
            .add_edge(GraphEdge::new(i, 1, i, "LINK").expect("valid"))
            .expect("add");
    }

    let label_table = LabelTable::new();
    let snapshot = SnapshotBuilder::build(&store, &label_table);

    let config = TraversalConfig::with_range(1, 1).with_limit(3);
    let results = bfs_traverse_csr(&snapshot, 1, &config);

    assert!(
        results.len() <= 3,
        "expected at most 3 results, got {}",
        results.len()
    );
}

// =============================================================================
// Unit tests — Task 5.4: ArcSwap integration in ConcurrentEdgeStore
// =============================================================================

/// After adding an edge, the CSR snapshot reflects the new edge.
#[test]
fn test_snapshot_rebuild_after_add() {
    use super::edge_concurrent::ConcurrentEdgeStore;

    let store = ConcurrentEdgeStore::with_shards(4).expect("test: valid shard count");
    store
        .add_edge(GraphEdge::new(1, 10, 20, "KNOWS").expect("valid"))
        .expect("add");

    let snapshot = store.get_csr_snapshot();
    assert!(
        snapshot.contains_node(10),
        "snapshot should contain source node"
    );
    let neighbors: HashSet<u64> = snapshot.neighbors(10).iter().copied().collect();
    assert!(
        neighbors.contains(&20),
        "snapshot should reflect added edge"
    );
}

/// After removing an edge, the CSR snapshot no longer contains it.
#[test]
fn test_snapshot_rebuild_after_remove() {
    use super::edge_concurrent::ConcurrentEdgeStore;

    let store = ConcurrentEdgeStore::with_shards(4).expect("test: valid shard count");
    store
        .add_edge(GraphEdge::new(1, 10, 20, "KNOWS").expect("valid"))
        .expect("add");
    store
        .add_edge(GraphEdge::new(2, 10, 30, "LIKES").expect("valid"))
        .expect("add");

    // Remove edge 10→20
    store.remove_edge(1);

    let snapshot = store.get_csr_snapshot();
    let neighbors: HashSet<u64> = snapshot.neighbors(10).iter().copied().collect();
    assert!(!neighbors.contains(&20), "removed edge should not appear");
    assert!(
        neighbors.contains(&30),
        "remaining edge should still appear"
    );
}

/// Existing `get_outgoing` API returns the same data as before (backward compat).
#[test]
fn test_get_outgoing_backward_compat() {
    use super::edge_concurrent::ConcurrentEdgeStore;

    let store = ConcurrentEdgeStore::with_shards(4).expect("test: valid shard count");
    store
        .add_edge(GraphEdge::new(1, 100, 200, "A").expect("valid"))
        .expect("add");
    store
        .add_edge(GraphEdge::new(2, 100, 300, "B").expect("valid"))
        .expect("add");

    let outgoing = store.get_outgoing(100);
    assert_eq!(outgoing.len(), 2);

    let targets: HashSet<u64> = outgoing.iter().map(|e| e.target()).collect();
    assert_eq!(targets, HashSet::from([200, 300]));
}

/// `traverse_bfs_csr` returns correct results via the lock-free snapshot.
#[test]
fn test_traverse_bfs_csr_on_concurrent_store() {
    use super::edge_concurrent::ConcurrentEdgeStore;

    let store = ConcurrentEdgeStore::with_shards(4).expect("test: valid shard count");
    // Chain: 1 -> 2 -> 3
    store
        .add_edge(GraphEdge::new(1, 1, 2, "NEXT").expect("valid"))
        .expect("add");
    store
        .add_edge(GraphEdge::new(2, 2, 3, "NEXT").expect("valid"))
        .expect("add");

    let config = TraversalConfig::with_range(1, 3);
    let results = store.traverse_bfs_csr(1, &config);

    let targets: HashSet<u64> = results.iter().map(|r| r.target_id).collect();
    assert!(targets.contains(&2), "should reach node 2");
    assert!(targets.contains(&3), "should reach node 3");
}

/// Snapshot reflects `remove_node_edges` cascade delete.
#[test]
fn test_snapshot_rebuild_after_remove_node_edges() {
    use super::edge_concurrent::ConcurrentEdgeStore;

    let store = ConcurrentEdgeStore::with_shards(4).expect("test: valid shard count");
    store
        .add_edge(GraphEdge::new(1, 10, 20, "A").expect("valid"))
        .expect("add");
    store
        .add_edge(GraphEdge::new(2, 10, 30, "B").expect("valid"))
        .expect("add");
    store
        .add_edge(GraphEdge::new(3, 40, 10, "C").expect("valid"))
        .expect("add");

    store.remove_node_edges(10);

    let snapshot = store.get_csr_snapshot();
    assert!(
        snapshot.neighbors(10).is_empty(),
        "node 10 should have no outgoing edges after cascade delete"
    );
    assert_eq!(snapshot.edge_count(), 0, "all edges should be removed");
}

// =============================================================================
// Unit tests — Task 7.4: Predicate pushdown
// =============================================================================

/// `NoFilter` returns all neighbors (no filtering).
#[test]
fn test_no_filter_returns_all() {
    use super::csr_snapshot::NoFilter;

    let mut store = EdgeStore::new();
    store
        .add_edge(GraphEdge::new(1, 10, 20, "KNOWS").expect("valid"))
        .expect("add");
    store
        .add_edge(GraphEdge::new(2, 10, 30, "LIKES").expect("valid"))
        .expect("add");
    store
        .add_edge(GraphEdge::new(3, 10, 40, "FOLLOWS").expect("valid"))
        .expect("add");

    let label_table = LabelTable::new();
    let snapshot = SnapshotBuilder::build(&store, &label_table);

    let no_filter = NoFilter;
    let filtered: Vec<(u64, u64, _)> = snapshot.neighbors_filtered(10, &no_filter).collect();

    // NoFilter should return all 3 neighbors
    assert_eq!(filtered.len(), 3);
    let targets: HashSet<u64> = filtered.iter().map(|&(t, _, _)| t).collect();
    assert_eq!(targets, HashSet::from([20, 30, 40]));
}

/// `LabelFilter` returns only edges with matching labels.
#[test]
fn test_label_filter_selective() {
    use super::csr_snapshot::LabelFilter;
    use rustc_hash::FxHashSet;

    let mut store = EdgeStore::new();
    store
        .add_edge(GraphEdge::new(1, 10, 20, "KNOWS").expect("valid"))
        .expect("add");
    store
        .add_edge(GraphEdge::new(2, 10, 30, "LIKES").expect("valid"))
        .expect("add");
    store
        .add_edge(GraphEdge::new(3, 10, 40, "KNOWS").expect("valid"))
        .expect("add");
    store
        .add_edge(GraphEdge::new(4, 10, 50, "FOLLOWS").expect("valid"))
        .expect("add");

    let label_table = LabelTable::new();
    let snapshot = SnapshotBuilder::build(&store, &label_table);

    // Find the LabelId for "KNOWS" from the snapshot's label_ids
    // We need to identify which LabelId corresponds to "KNOWS"
    let all_neighbors: Vec<(u64, u64, _)> = snapshot
        .neighbors_filtered(10, &super::csr_snapshot::NoFilter)
        .collect();

    // Find the label_id for KNOWS by checking which edges have target 20 or 40
    let knows_label_id = all_neighbors
        .iter()
        .find(|&&(t, _, _)| t == 20)
        .map(|&(_, _, lid)| lid)
        .expect("should find KNOWS edge");

    let mut allowed = FxHashSet::default();
    allowed.insert(knows_label_id);
    let label_filter = LabelFilter::new(allowed);

    let filtered: Vec<(u64, u64, _)> = snapshot.neighbors_filtered(10, &label_filter).collect();

    // Only KNOWS edges (targets 20 and 40)
    assert_eq!(filtered.len(), 2);
    let targets: HashSet<u64> = filtered.iter().map(|&(t, _, _)| t).collect();
    assert_eq!(targets, HashSet::from([20, 40]));
}

/// BFS with predicate pushdown produces same results as post-hoc filtering.
#[test]
fn test_bfs_filtered_vs_post_filter() {
    use super::csr_snapshot::{LabelFilter, NoFilter};
    use super::traversal_csr::bfs_traverse_csr_filtered;
    use rustc_hash::FxHashSet;

    // Build a graph: 1 -KNOWS-> 2 -LIKES-> 3 -KNOWS-> 4
    let mut store = EdgeStore::new();
    store
        .add_edge(GraphEdge::new(10, 1, 2, "KNOWS").expect("valid"))
        .expect("add");
    store
        .add_edge(GraphEdge::new(11, 2, 3, "LIKES").expect("valid"))
        .expect("add");
    store
        .add_edge(GraphEdge::new(12, 3, 4, "KNOWS").expect("valid"))
        .expect("add");
    store
        .add_edge(GraphEdge::new(13, 1, 5, "LIKES").expect("valid"))
        .expect("add");

    let label_table = LabelTable::new();
    let snapshot = SnapshotBuilder::build(&store, &label_table);

    // Find the LabelId for "KNOWS"
    let all_n: Vec<(u64, u64, _)> = snapshot.neighbors_filtered(1, &NoFilter).collect();
    let knows_lid = all_n
        .iter()
        .find(|&&(t, _, _)| t == 2)
        .map(|&(_, _, lid)| lid)
        .expect("KNOWS edge");

    let mut allowed = FxHashSet::default();
    allowed.insert(knows_lid);
    let predicate = LabelFilter::new(allowed);

    let config = TraversalConfig::with_range(1, 3);

    // Filtered BFS (pushdown)
    let filtered_results = bfs_traverse_csr_filtered(&snapshot, 1, &config, &predicate);

    // Unfiltered BFS then post-filter
    let all_results = bfs_traverse_csr(&snapshot, 1, &config);

    // Post-filter: only keep results reachable via KNOWS-only paths
    // With pushdown, BFS only follows KNOWS edges, so from node 1:
    //   depth 1: node 2 (via KNOWS) — included
    //   depth 2: nothing (node 2's edges are LIKES, filtered out)
    // Post-filter on all results would keep different nodes since it
    // doesn't restrict traversal paths. The pushdown is stricter.
    // Verify pushdown results are a subset of unfiltered results.
    let filtered_targets: HashSet<u64> = filtered_results.iter().map(|r| r.target_id).collect();
    let all_targets: HashSet<u64> = all_results.iter().map(|r| r.target_id).collect();

    // Pushdown results must be a subset of all results
    assert!(
        filtered_targets.is_subset(&all_targets),
        "filtered targets {:?} should be subset of all targets {:?}",
        filtered_targets,
        all_targets
    );

    // With KNOWS-only filter from node 1, only node 2 is reachable at depth 1
    assert!(filtered_targets.contains(&2), "node 2 reachable via KNOWS");
    // Node 5 is via LIKES, should NOT be in filtered results
    assert!(
        !filtered_targets.contains(&5),
        "node 5 via LIKES should be filtered out"
    );
}

// =============================================================================
// Property-based test — Task 7.3: Predicate pushdown correctness
// =============================================================================

mod predicate_property_tests {
    use super::*;
    use crate::collection::graph::csr_snapshot::{EdgePredicate, LabelFilter, NoFilter};
    use crate::collection::graph::label_table::LabelId;
    use proptest::prelude::*;
    use rustc_hash::FxHashSet;

    /// Generates a random `(EdgeStore, LabelTable)` with 1-50 nodes and 0-200 edges.
    /// (Duplicated from property_tests to keep module self-contained.)
    fn arb_edge_store() -> impl Strategy<Value = (EdgeStore, LabelTable)> {
        (1_u64..=50, 0_usize..=200).prop_flat_map(|(max_node, edge_count)| {
            let labels = vec!["KNOWS", "FOLLOWS", "LIKES", "WORKS_AT", "CREATED"];
            prop::collection::vec(
                (1..=max_node, 1..=max_node, 0..labels.len()),
                0..=edge_count,
            )
            .prop_map(move |edges| {
                let mut store = EdgeStore::new();
                let label_table = LabelTable::new();
                let labels = vec!["KNOWS", "FOLLOWS", "LIKES", "WORKS_AT", "CREATED"];
                for (i, (src, tgt, label_idx)) in edges.into_iter().enumerate() {
                    let label = labels[label_idx];
                    let eid = (i + 1) as u64;
                    if let Ok(edge) = GraphEdge::new(eid, src, tgt, label) {
                        let _ = store.add_edge(edge);
                    }
                }
                (store, label_table)
            })
        })
    }

    // Feature: graph-traversal-v2, Property 6: Predicate pushdown correctness
    // **Validates: Requirements 4.1**
    proptest! {
        #![proptest_config(ProptestConfig::with_cases(100))]
        #[test]
        fn prop_predicate_pushdown(
            (store, label_table) in arb_edge_store(),
            filter_bits in proptest::bits::u8::between(0, 5),
        ) {
            let snapshot = SnapshotBuilder::build(&store, &label_table);

            // Build a LabelFilter from the random bits
            let mut allowed = FxHashSet::default();
            for i in 0..5u32 {
                if filter_bits & (1 << i) != 0 {
                    allowed.insert(LabelId::from_u32(i));
                }
            }
            let predicate = LabelFilter::new(allowed.clone());

            // For each source node, verify filtered == subset of unfiltered where matches() is true
            let source_nodes: std::collections::HashSet<u64> =
                store.all_edges().iter().map(|e| e.source()).collect();

            for &nid in &source_nodes {
                if !snapshot.contains_node(nid) {
                    continue;
                }

                // Get all neighbors (unfiltered)
                let all_neighbors: Vec<(u64, u64, LabelId)> = snapshot
                    .neighbors_filtered(nid, &NoFilter)
                    .collect();

                // Get filtered neighbors
                let filtered: Vec<(u64, u64, LabelId)> = snapshot
                    .neighbors_filtered(nid, &predicate)
                    .collect();

                // Expected: subset of all_neighbors where predicate matches
                let expected: Vec<(u64, u64, LabelId)> = all_neighbors
                    .iter()
                    .filter(|&&(t, e, l)| predicate.matches(t, e, l))
                    .copied()
                    .collect();

                prop_assert_eq!(
                    filtered, expected,
                    "predicate pushdown mismatch for node {}",
                    nid
                );
            }
        }
    }
}

// =============================================================================
// Unit tests — Task 9: AdjacencySource trait
// =============================================================================

/// AdjacencySource on CsrSnapshot and EdgeStore return the same neighbor sets.
#[test]
fn test_adjacency_source_equivalence() {
    use super::csr_snapshot::AdjacencySource;

    let mut store = EdgeStore::new();
    store
        .add_edge(GraphEdge::new(1, 10, 20, "KNOWS").expect("valid"))
        .expect("add");
    store
        .add_edge(GraphEdge::new(2, 10, 30, "LIKES").expect("valid"))
        .expect("add");
    store
        .add_edge(GraphEdge::new(3, 20, 30, "FOLLOWS").expect("valid"))
        .expect("add");

    let label_table = LabelTable::new();
    let snapshot = SnapshotBuilder::build(&store, &label_table);

    // Compare AdjacencySource::neighbors for each source node
    for &nid in &[10u64, 20, 30] {
        let csr_neighbors: HashSet<u64> = AdjacencySource::neighbors(&snapshot, nid)
            .into_iter()
            .collect();
        let store_neighbors: HashSet<u64> = AdjacencySource::neighbors(&store, nid)
            .into_iter()
            .collect();
        assert_eq!(
            csr_neighbors, store_neighbors,
            "AdjacencySource mismatch for node {nid}"
        );
    }

    // Non-existent node returns empty
    assert!(AdjacencySource::neighbors(&snapshot, 999).is_empty());
    assert!(AdjacencySource::neighbors(&store, 999).is_empty());
}