sqlitegraph 2.1.1

Embedded graph database with full ACID transactions, HNSW vector search, dual backend support, and comprehensive graph algorithms library
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
//! V3 Backend Algorithm Integration Tests
//!
//! These tests verify that V3 Backend GraphBackend operations work correctly
//! for the primitive operations that graph algorithms depend on:
//! - entity_ids() - enumerate all nodes
//! - neighbors() - traverse edges with direction filtering
//! - insert_node() / insert_edge() - build test graphs

use crate::backend::native::v3::{
    PersistentHeaderV3, V3_FORMAT_VERSION, V3_HEADER_SIZE, V3Backend,
};

/// Verify that key V3 module exports are available
#[test]
fn test_v3_module_exports() {
    // Verify key exports are available
    let _header = PersistentHeaderV3::new_v3();
    assert_eq!(V3_HEADER_SIZE, 112);
    assert_eq!(V3_FORMAT_VERSION, 4);
}
use crate::backend::GraphBackend;
use crate::backend::sqlite::types::{BackendDirection, EdgeSpec, NeighborQuery, NodeSpec};
use tempfile::TempDir;

/// Test that V3 backend can create nodes and enumerate them
#[test]
fn test_v3_entity_ids_basic() {
    let temp_dir = TempDir::new().unwrap();
    let db_path = temp_dir.path().join("test.graph");

    let backend = V3Backend::create(&db_path).unwrap();

    // Initially empty
    let ids = backend.entity_ids().unwrap();
    assert!(ids.is_empty(), "New database should have no entities");

    // Insert nodes
    let node1 = backend
        .insert_node(NodeSpec {
            kind: "Test".to_string(),
            name: "A".to_string(),
            file_path: None,
            data: serde_json::json!({}),
        })
        .unwrap();

    let node2 = backend
        .insert_node(NodeSpec {
            kind: "Test".to_string(),
            name: "B".to_string(),
            file_path: None,
            data: serde_json::json!({}),
        })
        .unwrap();

    // Verify enumeration
    let ids = backend.entity_ids().unwrap();
    assert_eq!(ids.len(), 2, "Should have 2 entities");
    assert!(ids.contains(&node1), "Should contain node1");
    assert!(ids.contains(&node2), "Should contain node2");
}

/// Test that V3 backend can create edges and traverse them
#[test]
fn test_v3_outgoing_edges() {
    let temp_dir = TempDir::new().unwrap();
    let db_path = temp_dir.path().join("test.graph");

    let backend = V3Backend::create(&db_path).unwrap();

    // Create a chain: A -> B -> C
    let node_a = backend
        .insert_node(NodeSpec {
            kind: "Test".to_string(),
            name: "A".to_string(),
            file_path: None,
            data: serde_json::json!({}),
        })
        .unwrap();

    let node_b = backend
        .insert_node(NodeSpec {
            kind: "Test".to_string(),
            name: "B".to_string(),
            file_path: None,
            data: serde_json::json!({}),
        })
        .unwrap();

    let node_c = backend
        .insert_node(NodeSpec {
            kind: "Test".to_string(),
            name: "C".to_string(),
            file_path: None,
            data: serde_json::json!({}),
        })
        .unwrap();

    // Insert edges
    backend
        .insert_edge(EdgeSpec {
            from: node_a,
            to: node_b,
            edge_type: "links_to".to_string(),
            data: serde_json::json!({}),
        })
        .unwrap();

    backend
        .insert_edge(EdgeSpec {
            from: node_b,
            to: node_c,
            edge_type: "links_to".to_string(),
            data: serde_json::json!({}),
        })
        .unwrap();

    // Test outgoing edges
    let snapshot = crate::snapshot::SnapshotId::current();
    let outgoing_a = backend
        .neighbors(
            snapshot,
            node_a,
            NeighborQuery {
                direction: BackendDirection::Outgoing,
                edge_type: None,
            },
        )
        .unwrap();
    assert_eq!(outgoing_a.len(), 1, "A should have 1 outgoing edge");
    assert!(outgoing_a.contains(&node_b), "A should point to B");

    let outgoing_b = backend
        .neighbors(
            snapshot,
            node_b,
            NeighborQuery {
                direction: BackendDirection::Outgoing,
                edge_type: None,
            },
        )
        .unwrap();
    assert_eq!(outgoing_b.len(), 1, "B should have 1 outgoing edge");
    assert!(outgoing_b.contains(&node_c), "B should point to C");

    let outgoing_c = backend
        .neighbors(
            snapshot,
            node_c,
            NeighborQuery {
                direction: BackendDirection::Outgoing,
                edge_type: None,
            },
        )
        .unwrap();
    assert!(outgoing_c.is_empty(), "C should have no outgoing edges");
}

/// Test incoming edges (reverse traversal)
#[test]
fn test_v3_incoming_edges() {
    let temp_dir = TempDir::new().unwrap();
    let db_path = temp_dir.path().join("test.graph");

    let backend = V3Backend::create(&db_path).unwrap();

    // Create a star: A -> B, A -> C, A -> D
    let node_a = backend
        .insert_node(NodeSpec {
            kind: "Test".to_string(),
            name: "A".to_string(),
            file_path: None,
            data: serde_json::json!({}),
        })
        .unwrap();

    let node_b = backend
        .insert_node(NodeSpec {
            kind: "Test".to_string(),
            name: "B".to_string(),
            file_path: None,
            data: serde_json::json!({}),
        })
        .unwrap();

    let node_c = backend
        .insert_node(NodeSpec {
            kind: "Test".to_string(),
            name: "C".to_string(),
            file_path: None,
            data: serde_json::json!({}),
        })
        .unwrap();

    let node_d = backend
        .insert_node(NodeSpec {
            kind: "Test".to_string(),
            name: "D".to_string(),
            file_path: None,
            data: serde_json::json!({}),
        })
        .unwrap();

    // Insert edges from A to B, C, D
    for target in [node_b, node_c, node_d] {
        backend
            .insert_edge(EdgeSpec {
                from: node_a,
                to: target,
                edge_type: "links_to".to_string(),
                data: serde_json::json!({}),
            })
            .unwrap();
    }

    // Test incoming edges
    let snapshot = crate::snapshot::SnapshotId::current();

    let incoming_b = backend
        .neighbors(
            snapshot,
            node_b,
            NeighborQuery {
                direction: BackendDirection::Incoming,
                edge_type: None,
            },
        )
        .unwrap();
    assert_eq!(incoming_b.len(), 1, "B should have 1 incoming edge");
    assert!(incoming_b.contains(&node_a), "B should be pointed to by A");

    let incoming_c = backend
        .neighbors(
            snapshot,
            node_c,
            NeighborQuery {
                direction: BackendDirection::Incoming,
                edge_type: None,
            },
        )
        .unwrap();
    assert_eq!(incoming_c.len(), 1, "C should have 1 incoming edge");
    assert!(incoming_c.contains(&node_a), "C should be pointed to by A");

    let incoming_a = backend
        .neighbors(
            snapshot,
            node_a,
            NeighborQuery {
                direction: BackendDirection::Incoming,
                edge_type: None,
            },
        )
        .unwrap();
    assert!(incoming_a.is_empty(), "A should have no incoming edges");
}

/// Test k-hop traversal (used by many algorithms)
#[test]
fn test_v3_k_hop_traversal() {
    let temp_dir = TempDir::new().unwrap();
    let db_path = temp_dir.path().join("test.graph");

    let backend = V3Backend::create(&db_path).unwrap();

    // Create a binary tree: A -> B, A -> C, B -> D, B -> E
    let node_a = backend
        .insert_node(NodeSpec {
            kind: "Test".to_string(),
            name: "A".to_string(),
            file_path: None,
            data: serde_json::json!({}),
        })
        .unwrap();

    let node_b = backend
        .insert_node(NodeSpec {
            kind: "Test".to_string(),
            name: "B".to_string(),
            file_path: None,
            data: serde_json::json!({}),
        })
        .unwrap();

    let node_c = backend
        .insert_node(NodeSpec {
            kind: "Test".to_string(),
            name: "C".to_string(),
            file_path: None,
            data: serde_json::json!({}),
        })
        .unwrap();

    let node_d = backend
        .insert_node(NodeSpec {
            kind: "Test".to_string(),
            name: "D".to_string(),
            file_path: None,
            data: serde_json::json!({}),
        })
        .unwrap();

    let node_e = backend
        .insert_node(NodeSpec {
            kind: "Test".to_string(),
            name: "E".to_string(),
            file_path: None,
            data: serde_json::json!({}),
        })
        .unwrap();

    // Insert edges
    backend
        .insert_edge(EdgeSpec {
            from: node_a,
            to: node_b,
            edge_type: "links_to".to_string(),
            data: serde_json::json!({}),
        })
        .unwrap();

    backend
        .insert_edge(EdgeSpec {
            from: node_a,
            to: node_c,
            edge_type: "links_to".to_string(),
            data: serde_json::json!({}),
        })
        .unwrap();

    backend
        .insert_edge(EdgeSpec {
            from: node_b,
            to: node_d,
            edge_type: "links_to".to_string(),
            data: serde_json::json!({}),
        })
        .unwrap();

    backend
        .insert_edge(EdgeSpec {
            from: node_b,
            to: node_e,
            edge_type: "links_to".to_string(),
            data: serde_json::json!({}),
        })
        .unwrap();

    // Test k-hop from A
    let snapshot = crate::snapshot::SnapshotId::current();

    // k_hop returns all nodes within k hops (not exactly at k hops)
    // 1-hop: B, C (nodes within 1 hop)
    let hop1 = backend
        .k_hop(snapshot, node_a, 1, BackendDirection::Outgoing)
        .unwrap();
    assert_eq!(hop1.len(), 2, "A should have 2 nodes within 1 hop");
    assert!(hop1.contains(&node_b), "B should be within 1 hop from A");
    assert!(hop1.contains(&node_c), "C should be within 1 hop from A");

    // 2-hop: B, C, D, E (nodes within 2 hops, includes 1-hop neighbors)
    let hop2 = backend
        .k_hop(snapshot, node_a, 2, BackendDirection::Outgoing)
        .unwrap();
    assert_eq!(hop2.len(), 4, "A should have 4 nodes within 2 hops");
    assert!(hop2.contains(&node_b), "B should be within 2 hops from A");
    assert!(hop2.contains(&node_c), "C should be within 2 hops from A");
    assert!(hop2.contains(&node_d), "D should be within 2 hops from A");
    assert!(hop2.contains(&node_e), "E should be within 2 hops from A");
}

/// Test node degree (incoming + outgoing counts)
#[test]
fn test_v3_node_degree() {
    let temp_dir = TempDir::new().unwrap();
    let db_path = temp_dir.path().join("test.graph");

    let backend = V3Backend::create(&db_path).unwrap();

    // Create a bidirectional link: A <-> B
    let node_a = backend
        .insert_node(NodeSpec {
            kind: "Test".to_string(),
            name: "A".to_string(),
            file_path: None,
            data: serde_json::json!({}),
        })
        .unwrap();

    let node_b = backend
        .insert_node(NodeSpec {
            kind: "Test".to_string(),
            name: "B".to_string(),
            file_path: None,
            data: serde_json::json!({}),
        })
        .unwrap();

    // A -> B
    backend
        .insert_edge(EdgeSpec {
            from: node_a,
            to: node_b,
            edge_type: "links_to".to_string(),
            data: serde_json::json!({}),
        })
        .unwrap();

    // B -> A (making it bidirectional)
    backend
        .insert_edge(EdgeSpec {
            from: node_b,
            to: node_a,
            edge_type: "links_to".to_string(),
            data: serde_json::json!({}),
        })
        .unwrap();

    // Check degrees
    let snapshot = crate::snapshot::SnapshotId::current();

    let (out_a, in_a) = backend.node_degree(snapshot, node_a).unwrap();
    assert_eq!(out_a, 1, "A should have 1 outgoing edge");
    assert_eq!(in_a, 1, "A should have 1 incoming edge");

    let (out_b, in_b) = backend.node_degree(snapshot, node_b).unwrap();
    assert_eq!(out_b, 1, "B should have 1 outgoing edge");
    assert_eq!(in_b, 1, "B should have 1 incoming edge");
}

/// Test persistence - database file is created and can be reopened
///
/// NOTE: Full persistence (loading nodes after reopen) requires NodeStore
/// disk loading which is not yet implemented. This test verifies basic
/// file creation and header persistence.
#[test]
fn test_v3_persistence() {
    let temp_dir = TempDir::new().unwrap();
    let db_path = temp_dir.path().join("test.graph");

    // Create and populate
    {
        let backend = V3Backend::create(&db_path).unwrap();
        let id = backend
            .insert_node(NodeSpec {
                kind: "Test".to_string(),
                name: "Persistent".to_string(),
                file_path: None,
                data: serde_json::json!({"key": "value"}),
            })
            .unwrap();

        // Verify before close - node exists with correct ID
        let snapshot = crate::snapshot::SnapshotId::current();
        let node = backend.get_node(snapshot, id).unwrap();
        assert_eq!(node.id, id, "Node ID should match");
    }

    // Verify file was created
    assert!(db_path.exists(), "Database file should exist after create");

    // Reopen database - header should be valid
    let backend = V3Backend::open(&db_path).unwrap();
    let header = backend.header();
    assert_eq!(
        header.magic,
        crate::backend::native::v3::V3_MAGIC,
        "Header magic should be valid"
    );
    assert_eq!(
        header.version,
        crate::backend::native::v3::V3_FORMAT_VERSION,
        "Header version should be valid"
    );
    // NOTE: NodeStore disk loading not yet implemented - cannot verify node data after reopen
}

// ============================================================================
// Algorithm Integration Tests
// ============================================================================

/// Helper: Create a chain graph and return node IDs
fn create_chain_graph(backend: &V3Backend, n: usize) -> Vec<i64> {
    let mut ids = Vec::with_capacity(n);
    for i in 0..n {
        ids.push(
            backend
                .insert_node(NodeSpec {
                    kind: "Node".to_string(),
                    name: format!("chain_{}", i),
                    file_path: None,
                    data: serde_json::json!({"idx": i}),
                })
                .unwrap(),
        );
    }
    for i in 0..n - 1 {
        backend
            .insert_edge(EdgeSpec {
                from: ids[i],
                to: ids[i + 1],
                edge_type: "NEXT".to_string(),
                data: serde_json::json!({}),
            })
            .unwrap();
    }
    ids
}

/// Test: BFS (via k_hop) reaches all nodes in a chain
#[test]
fn test_v3_bfs_chain_reaches_all() {
    let temp_dir = TempDir::new().unwrap();
    let db_path = temp_dir.path().join("test.graph");
    let backend = V3Backend::create(&db_path).unwrap();

    let ids = create_chain_graph(&backend, 10);
    let snapshot = crate::snapshot::SnapshotId::current();

    // k_hop with depth 20 should reach all 9 edges (9 reachable nodes)
    let reachable = backend
        .k_hop(snapshot, ids[0], 20, BackendDirection::Outgoing)
        .unwrap();

    assert_eq!(
        reachable.len(),
        9,
        "BFS from first node should reach all 9 other nodes in chain"
    );
    // Verify they're in order
    for i in 1..10 {
        assert!(
            reachable.contains(&ids[i]),
            "Should reach node at index {}",
            i
        );
    }
}

/// Test: BFS stays within one component for disconnected chains
#[test]
fn test_v3_bfs_disconnected_stays_in_component() {
    let temp_dir = TempDir::new().unwrap();
    let db_path = temp_dir.path().join("test.graph");
    let backend = V3Backend::create(&db_path).unwrap();

    // Create two disconnected chains: A->B->C and D->E->F
    let chain1 = create_chain_graph(&backend, 3);
    let chain2 = create_chain_graph(&backend, 3);
    let snapshot = crate::snapshot::SnapshotId::current();

    // BFS from chain1 start should only reach chain1
    let reachable = backend
        .k_hop(snapshot, chain1[0], 10, BackendDirection::Outgoing)
        .unwrap();

    assert_eq!(
        reachable.len(),
        2,
        "BFS should only reach 2 nodes in same component"
    );
    assert!(reachable.contains(&chain1[1]), "Should reach chain1[1]");
    assert!(reachable.contains(&chain1[2]), "Should reach chain1[2]");
    assert!(
        !reachable.contains(&chain2[0]),
        "Should NOT reach chain2[0]"
    );
}

/// Test: Star topology - center reaches all leaves
#[test]
fn test_v3_star_outgoing() {
    let temp_dir = TempDir::new().unwrap();
    let db_path = temp_dir.path().join("test.graph");
    let backend = V3Backend::create(&db_path).unwrap();

    let center = backend
        .insert_node(NodeSpec {
            kind: "Node".to_string(),
            name: "center".to_string(),
            file_path: None,
            data: serde_json::json!({}),
        })
        .unwrap();

    let mut leaves = Vec::new();
    for i in 0..5 {
        let leaf = backend
            .insert_node(NodeSpec {
                kind: "Node".to_string(),
                name: format!("leaf_{}", i),
                file_path: None,
                data: serde_json::json!({}),
            })
            .unwrap();
        backend
            .insert_edge(EdgeSpec {
                from: center,
                to: leaf,
                edge_type: "CONNECTS".to_string(),
                data: serde_json::json!({}),
            })
            .unwrap();
        leaves.push(leaf);
    }

    let snapshot = crate::snapshot::SnapshotId::current();

    // Outgoing from center should reach all leaves
    let outgoing = backend
        .neighbors(
            snapshot,
            center,
            NeighborQuery {
                direction: BackendDirection::Outgoing,
                edge_type: None,
            },
        )
        .unwrap();
    assert_eq!(outgoing.len(), 5, "Center should have 5 outgoing edges");

    // Each leaf should have 1 incoming from center
    for leaf in &leaves {
        let incoming = backend
            .neighbors(
                snapshot,
                *leaf,
                NeighborQuery {
                    direction: BackendDirection::Incoming,
                    edge_type: None,
                },
            )
            .unwrap();
        assert_eq!(incoming.len(), 1, "Each leaf should have 1 incoming edge");
        assert!(incoming.contains(&center), "Incoming should be from center");
    }
}

/// Test: Binary tree k-hop at different depths
#[test]
fn test_v3_binary_tree_k_hop() {
    let temp_dir = TempDir::new().unwrap();
    let db_path = temp_dir.path().join("test.graph");
    let backend = V3Backend::create(&db_path).unwrap();

    // Create binary tree of depth 3: 7 nodes (1 + 2 + 4)
    let mut ids = Vec::new();
    for i in 0..7 {
        ids.push(
            backend
                .insert_node(NodeSpec {
                    kind: "Node".to_string(),
                    name: format!("bt_{}", i),
                    file_path: None,
                    data: serde_json::json!({"idx": i}),
                })
                .unwrap(),
        );
    }
    // Edges: 0->1, 0->2, 1->3, 1->4, 2->5, 2->6
    let edges = [(0, 1), (0, 2), (1, 3), (1, 4), (2, 5), (2, 6)];
    for (from, to) in edges {
        backend
            .insert_edge(EdgeSpec {
                from: ids[from],
                to: ids[to],
                edge_type: "CHILD".to_string(),
                data: serde_json::json!({}),
            })
            .unwrap();
    }

    let snapshot = crate::snapshot::SnapshotId::current();

    // k=1 from root: nodes 1, 2 (2 children)
    let hop1 = backend
        .k_hop(snapshot, ids[0], 1, BackendDirection::Outgoing)
        .unwrap();
    assert_eq!(hop1.len(), 2, "k=1 should reach 2 children");

    // k=2 from root: nodes 1, 2, 3, 4, 5, 6 (2 + 4 = 6)
    let hop2 = backend
        .k_hop(snapshot, ids[0], 2, BackendDirection::Outgoing)
        .unwrap();
    assert_eq!(hop2.len(), 6, "k=2 should reach all 6 descendants");

    // k=3 from root: same as k=2 (no more nodes)
    let hop3 = backend
        .k_hop(snapshot, ids[0], 3, BackendDirection::Outgoing)
        .unwrap();
    assert_eq!(hop3.len(), 6, "k=3 should still reach 6 (no deeper nodes)");
}

/// Test: Node degree correctness
#[test]
fn test_v3_node_degree_complex() {
    let temp_dir = TempDir::new().unwrap();
    let db_path = temp_dir.path().join("test.graph");
    let backend = V3Backend::create(&db_path).unwrap();

    // Create a diamond: A -> B, A -> C, B -> D, C -> D
    let a = backend
        .insert_node(NodeSpec {
            kind: "Node".to_string(),
            name: "A".to_string(),
            file_path: None,
            data: serde_json::json!({}),
        })
        .unwrap();
    let b = backend
        .insert_node(NodeSpec {
            kind: "Node".to_string(),
            name: "B".to_string(),
            file_path: None,
            data: serde_json::json!({}),
        })
        .unwrap();
    let c = backend
        .insert_node(NodeSpec {
            kind: "Node".to_string(),
            name: "C".to_string(),
            file_path: None,
            data: serde_json::json!({}),
        })
        .unwrap();
    let d = backend
        .insert_node(NodeSpec {
            kind: "Node".to_string(),
            name: "D".to_string(),
            file_path: None,
            data: serde_json::json!({}),
        })
        .unwrap();

    backend
        .insert_edge(EdgeSpec {
            from: a,
            to: b,
            edge_type: "E".to_string(),
            data: serde_json::json!({}),
        })
        .unwrap();
    backend
        .insert_edge(EdgeSpec {
            from: a,
            to: c,
            edge_type: "E".to_string(),
            data: serde_json::json!({}),
        })
        .unwrap();
    backend
        .insert_edge(EdgeSpec {
            from: b,
            to: d,
            edge_type: "E".to_string(),
            data: serde_json::json!({}),
        })
        .unwrap();
    backend
        .insert_edge(EdgeSpec {
            from: c,
            to: d,
            edge_type: "E".to_string(),
            data: serde_json::json!({}),
        })
        .unwrap();

    let snapshot = crate::snapshot::SnapshotId::current();

    // A: out=2, in=0
    let (out_a, in_a) = backend.node_degree(snapshot, a).unwrap();
    assert_eq!(out_a, 2, "A should have 2 outgoing");
    assert_eq!(in_a, 0, "A should have 0 incoming");

    // D: out=0, in=2
    let (out_d, in_d) = backend.node_degree(snapshot, d).unwrap();
    assert_eq!(out_d, 0, "D should have 0 outgoing");
    assert_eq!(in_d, 2, "D should have 2 incoming");

    // B: out=1, in=1
    let (out_b, in_b) = backend.node_degree(snapshot, b).unwrap();
    assert_eq!(out_b, 1, "B should have 1 outgoing");
    assert_eq!(in_b, 1, "B should have 1 incoming");
}

/// Test: Edge type filtering
#[test]
fn test_v3_edge_type_filtering() {
    let temp_dir = TempDir::new().unwrap();
    let db_path = temp_dir.path().join("test.graph");
    let backend = V3Backend::create(&db_path).unwrap();

    let node_a = backend
        .insert_node(NodeSpec {
            kind: "Node".to_string(),
            name: "A".to_string(),
            file_path: None,
            data: serde_json::json!({}),
        })
        .unwrap();
    let node_b = backend
        .insert_node(NodeSpec {
            kind: "Node".to_string(),
            name: "B".to_string(),
            file_path: None,
            data: serde_json::json!({}),
        })
        .unwrap();
    let node_c = backend
        .insert_node(NodeSpec {
            kind: "Node".to_string(),
            name: "C".to_string(),
            file_path: None,
            data: serde_json::json!({}),
        })
        .unwrap();

    backend
        .insert_edge(EdgeSpec {
            from: node_a,
            to: node_b,
            edge_type: "CALLS".to_string(),
            data: serde_json::json!({}),
        })
        .unwrap();
    backend
        .insert_edge(EdgeSpec {
            from: node_a,
            to: node_c,
            edge_type: "USES".to_string(),
            data: serde_json::json!({}),
        })
        .unwrap();

    let snapshot = crate::snapshot::SnapshotId::current();

    // All outgoing from A
    let all = backend
        .neighbors(
            snapshot,
            node_a,
            NeighborQuery {
                direction: BackendDirection::Outgoing,
                edge_type: None,
            },
        )
        .unwrap();
    assert_eq!(all.len(), 2, "A should have 2 outgoing edges total");

    // Filter by CALLS
    let calls = backend
        .neighbors(
            snapshot,
            node_a,
            NeighborQuery {
                direction: BackendDirection::Outgoing,
                edge_type: Some("CALLS".to_string()),
            },
        )
        .unwrap();
    assert_eq!(calls.len(), 1, "A should have 1 CALLS edge");
    assert!(calls.contains(&node_b), "CALLS edge should point to B");

    // Filter by USES
    let uses = backend
        .neighbors(
            snapshot,
            node_a,
            NeighborQuery {
                direction: BackendDirection::Outgoing,
                edge_type: Some("USES".to_string()),
            },
        )
        .unwrap();
    assert_eq!(uses.len(), 1, "A should have 1 USES edge");
    assert!(uses.contains(&node_c), "USES edge should point to C");
}

/// Test: Large graph operations (stress test)
#[test]
fn test_v3_large_graph_insert_and_query() {
    let temp_dir = TempDir::new().unwrap();
    let db_path = temp_dir.path().join("test.graph");
    let backend = V3Backend::create(&db_path).unwrap();

    let n = 200;
    let mut ids = Vec::with_capacity(n);

    // Insert 200 nodes
    for i in 0..n {
        ids.push(
            backend
                .insert_node(NodeSpec {
                    kind: "Node".to_string(),
                    name: format!("n_{}", i),
                    file_path: None,
                    data: serde_json::json!({"idx": i}),
                })
                .unwrap(),
        );
    }

    // Create chain edges
    for i in 0..n - 1 {
        backend
            .insert_edge(EdgeSpec {
                from: ids[i],
                to: ids[i + 1],
                edge_type: "NEXT".to_string(),
                data: serde_json::json!({}),
            })
            .unwrap();
    }

    let snapshot = crate::snapshot::SnapshotId::current();

    // Verify entity_ids returns all 200
    let all_ids = backend.entity_ids().unwrap();
    assert_eq!(all_ids.len(), n, "Should have {} entities", n);

    // Verify first node reaches far in the chain
    let reachable = backend
        .k_hop(snapshot, ids[0], 100, BackendDirection::Outgoing)
        .unwrap();
    assert!(
        reachable.len() >= 100,
        "Should reach at least 100 nodes from start"
    );

    // Verify last node has no outgoing
    let last_outgoing = backend
        .neighbors(
            snapshot,
            ids[n - 1],
            NeighborQuery {
                direction: BackendDirection::Outgoing,
                edge_type: None,
            },
        )
        .unwrap();
    assert!(
        last_outgoing.is_empty(),
        "Last node should have no outgoing edges"
    );

    // Verify first node has no incoming
    let first_incoming = backend
        .neighbors(
            snapshot,
            ids[0],
            NeighborQuery {
                direction: BackendDirection::Incoming,
                edge_type: None,
            },
        )
        .unwrap();
    assert!(
        first_incoming.is_empty(),
        "First node should have no incoming edges"
    );
}