sqlitegraph 2.2.2

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
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
//! MVCC Concurrent Tests - Multi-threaded stress testing
//!
//! **Purpose:** Validate MVCC-lite system under concurrent access
//! **Scope:** Multi-threaded tests of thread-safe components
//! **Focus:** SnapshotManager thread safety, stress testing, performance validation
//!
//! **NOTE:** SqliteGraph itself is NOT thread-safe (contains RefCell, non-sync types).
//! These tests focus on the thread-safe SnapshotManager component only.
//!
//! These tests verify that the snapshot system's thread-safe components provide
//! proper isolation under concurrent access patterns.

use sqlitegraph::algo;
use sqlitegraph::mvcc::SnapshotManager;
use sqlitegraph::{GraphEdgeCreate, GraphEntityCreate, SqliteGraph, SqliteGraphError};
use std::collections::HashMap;
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::{Arc, Barrier};
use std::thread;
use std::time::{Duration, Instant};

//
// TEST HELPERS
//

/// Helper: Get node count from graph
fn node_count(graph: &SqliteGraph) -> Result<i64, SqliteGraphError> {
    let ids = graph.list_entity_ids()?;
    Ok(ids.len() as i64)
}

/// Helper: Get edge count from graph
fn edge_count(graph: &SqliteGraph) -> Result<i64, SqliteGraphError> {
    let entity_ids = graph.list_entity_ids()?;
    let mut total_edges = 0;
    for &id in &entity_ids {
        let outgoing = graph.query().outgoing(id)?;
        total_edges += outgoing.len();
    }
    Ok(total_edges as i64)
}

/// Helper: Warm the cache by reading all adjacency data
fn warm_cache(graph: &SqliteGraph) -> Result<(), SqliteGraphError> {
    let entity_ids = graph.list_entity_ids()?;
    for &id in &entity_ids {
        let _ = graph.query().outgoing(id);
        let _ = graph.query().incoming(id);
    }
    Ok(())
}

/// Helper: Insert entity using proper API
fn insert_entity(graph: &SqliteGraph, create: GraphEntityCreate) -> Result<i64, SqliteGraphError> {
    let entity = sqlitegraph::GraphEntity {
        id: 0, // Will be assigned by database
        kind: create.kind,
        name: create.name,
        file_path: create.file_path,
        data: create.data,
    };
    graph.insert_entity(&entity)
}

/// Helper: Insert edge using proper API
fn insert_edge(graph: &SqliteGraph, create: GraphEdgeCreate) -> Result<i64, SqliteGraphError> {
    let edge = sqlitegraph::GraphEdge {
        id: 0, // Will be assigned by database
        from_id: create.from_id,
        to_id: create.to_id,
        edge_type: create.edge_type,
        data: create.data,
    };
    graph.insert_edge(&edge)
}

/// Helper: Create test graph with sample data
fn create_test_graph() -> Result<SqliteGraph, SqliteGraphError> {
    let graph = SqliteGraph::open_in_memory()?;

    // Create test entities
    let entity1 = GraphEntityCreate {
        kind: "function".to_string(),
        name: "main".to_string(),
        file_path: Some("src/main.rs".to_string()),
        data: serde_json::json!({"line": 10}),
    };

    let entity2 = GraphEntityCreate {
        kind: "function".to_string(),
        name: "helper".to_string(),
        file_path: Some("src/helper.rs".to_string()),
        data: serde_json::json!({"line": 5}),
    };

    let id1 = insert_entity(&graph, entity1)?;
    let id2 = insert_entity(&graph, entity2)?;

    // Create edge
    let edge = GraphEdgeCreate {
        from_id: id1,
        to_id: id2,
        edge_type: "calls".to_string(),
        data: serde_json::json!({}),
    };
    insert_edge(&graph, edge)?;

    Ok(graph)
}

/// Helper: Create larger test graph for stress tests
fn create_large_test_graph(size: usize) -> Result<SqliteGraph, SqliteGraphError> {
    let graph = SqliteGraph::open_in_memory()?;

    // Create entities
    for i in 0..size {
        let entity = GraphEntityCreate {
            kind: "node".to_string(),
            name: format!("node_{}", i),
            file_path: Some(format!("file_{}.rs", i)),
            data: serde_json::json!({"index": i}),
        };
        insert_entity(&graph, entity)?;
    }

    // Create edges (each node connects to next 2 nodes)
    let ids: Vec<i64> = graph.list_entity_ids()?;
    for (i, &id) in ids.iter().enumerate() {
        for j in 1..=2 {
            let target_idx = (i + j) % ids.len();
            let edge = GraphEdgeCreate {
                from_id: id,
                to_id: ids[target_idx],
                edge_type: "connects".to_string(),
                data: serde_json::json!({}),
            };
            insert_edge(&graph, edge)?;
        }
    }

    Ok(graph)
}

/// Helper: Create a SnapshotManager with test data
fn create_test_snapshot_manager(size: usize) -> SnapshotManager {
    let mut outgoing = HashMap::new();
    let mut incoming = HashMap::new();

    for i in 0..size {
        outgoing.insert(i as i64, vec![]);
        incoming.insert(i as i64, vec![]);
    }

    SnapshotManager::with_state(&outgoing, &incoming)
}

//
// GROUP 1: SNAPSHOT MANAGER CONCURRENCY TESTS
//

#[test]
fn test_concurrent_snapshot_acquisition() {
    // Scenario: 100 threads simultaneously acquire snapshots
    // Expected: All threads succeed, no deadlocks, all snapshots valid
    let manager = Arc::new(create_test_snapshot_manager(100));
    let barrier = Arc::new(Barrier::new(100));
    let success_count = Arc::new(AtomicU64::new(0));

    let handles: Vec<_> = (0..100)
        .map(|_| {
            let manager = manager.clone();
            let barrier = barrier.clone();
            let success_count = success_count.clone();

            thread::spawn(move || {
                barrier.wait();
                let snapshot = manager.acquire_snapshot();
                if snapshot.node_count() > 0 {
                    success_count.fetch_add(1, Ordering::Relaxed);
                }
            })
        })
        .collect();

    // Wait for all threads
    for h in handles {
        h.join().expect("Thread panicked");
    }

    // Verify all threads succeeded
    let success = success_count.load(Ordering::Relaxed);
    assert_eq!(success, 100, "Not all threads acquired valid snapshots");
}

#[test]
fn test_snapshot_during_state_update() {
    // Scenario: Concurrent snapshot acquisition during state updates
    // Expected: No torn reads, all snapshots consistent
    let manager = Arc::new(create_test_snapshot_manager(100));

    let mut outgoing = HashMap::new();
    let mut incoming = HashMap::new();

    // Initialize with some data
    for i in 0..100 {
        outgoing.insert(i, vec![]);
        incoming.insert(i, vec![]);
    }

    let manager1 = manager.clone();
    let manager2 = manager.clone();

    // Thread A: Update state continuously
    let handle1 = thread::spawn(move || {
        for i in 0..1000 {
            let mut new_outgoing = outgoing.clone();
            new_outgoing.insert(i % 100, vec![]);
            manager1.update_snapshot(&new_outgoing, &incoming);
        }
    });

    // Thread B: Acquire snapshots continuously
    let handle2 = thread::spawn(move || {
        for _ in 0..1000 {
            let snapshot = manager2.acquire_snapshot();
            // Verify snapshot is valid
            assert!(snapshot.node_count() <= 100);
        }
    });

    handle1.join().expect("Thread A panicked");
    handle2.join().expect("Thread B panicked");
}

#[test]
fn test_rapid_snapshot_creation() {
    // Scenario: Create 1000 snapshots in rapid succession
    // Expected: No memory leaks, all snapshots valid
    let manager = create_test_snapshot_manager(50);

    // Create many snapshots
    for _ in 0..1000 {
        let snapshot = manager.acquire_snapshot();
        assert!(snapshot.node_count() > 0, "Snapshot has no nodes");
    }

    // Verify final snapshot is still valid
    let final_snapshot = manager.acquire_snapshot();
    assert!(final_snapshot.node_count() > 0);
}

#[test]
fn test_100_simultaneous_snapshots() {
    // Scenario: 100 threads acquire snapshots simultaneously with barrier
    // Expected: All succeed, no contention issues
    let manager = Arc::new(create_test_snapshot_manager(100));
    let barrier = Arc::new(Barrier::new(100));
    let success_count = Arc::new(AtomicU64::new(0));

    let handles: Vec<_> = (0..100)
        .map(|_| {
            let manager = manager.clone();
            let barrier = barrier.clone();
            let success_count = success_count.clone();

            thread::spawn(move || {
                barrier.wait();
                let snapshot = manager.acquire_snapshot();
                let count = snapshot.node_count();

                if count > 0 && count <= 100 {
                    success_count.fetch_add(1, Ordering::Relaxed);
                }
            })
        })
        .collect();

    for h in handles {
        h.join().expect("Thread panicked");
    }

    let success = success_count.load(Ordering::Relaxed);
    assert_eq!(success, 100, "Not all threads succeeded");
}

#[test]
fn test_sustained_concurrent_access() {
    // Scenario: Sustained concurrent access for 2 seconds
    // Expected: No deadlocks, continuous progress
    let manager = Arc::new(create_test_snapshot_manager(50));
    let running = Arc::new(AtomicU64::new(1));
    let duration = Duration::from_secs(2);

    // Spawn 10 threads
    let handles: Vec<_> = (0..10)
        .map(|_| {
            let manager = manager.clone();
            let running = running.clone();
            let start = Instant::now();

            thread::spawn(move || {
                let mut count = 0;
                while running.load(Ordering::Relaxed) == 1 && start.elapsed() < duration {
                    let _ = manager.acquire_snapshot();
                    count += 1;
                }
                count
            })
        })
        .collect();

    // Let them run for 2 seconds
    thread::sleep(duration);
    running.store(0, Ordering::Relaxed);

    // Collect results
    let mut total = 0;
    for h in handles {
        let count = h.join().expect("Thread panicked");
        total += count;
    }

    // Verify reasonable throughput (at least 1000 snapshots total)
    assert!(
        total >= 1000,
        "Low throughput: {} snapshots in 2 seconds",
        total
    );
    println!(
        "Sustained concurrent access: {} snapshots in 2 seconds",
        total
    );
}

//
// GROUP 2: CORRECTNESS TESTS
//

#[test]
fn test_snapshot_state_immutability() {
    // Scenario: Verify SnapshotState is truly immutable
    // Expected: Snapshots never change after creation
    let manager = Arc::new(create_test_snapshot_manager(50));

    let snapshot1 = manager.acquire_snapshot();
    let original_count = snapshot1.node_count();

    // Update state
    let mut new_outgoing = HashMap::new();
    for i in 0..100 {
        new_outgoing.insert(i, vec![]);
    }
    manager.update_snapshot(&new_outgoing, &new_outgoing);

    // Original snapshot should be unchanged
    assert_eq!(snapshot1.node_count(), original_count);

    // New snapshot should reflect changes
    let snapshot2 = manager.acquire_snapshot();
    assert_eq!(snapshot2.node_count(), 100);
}

#[test]
fn test_arc_swap_atomic_guarantees() {
    // Scenario: Verify ArcSwap provides atomic pointer swaps
    // Expected: No torn reads, consistent state
    let manager = Arc::new(create_test_snapshot_manager(50));

    let mut state1 = HashMap::new();
    for i in 0..50 {
        state1.insert(i, vec![]);
    }

    let mut state2 = HashMap::new();
    for i in 0..100 {
        state2.insert(i, vec![]);
    }

    let manager1 = manager.clone();
    let manager2 = manager.clone();

    // Thread A: Rapid state updates
    let handle_a = thread::spawn(move || {
        for _ in 0..1000 {
            manager1.update_snapshot(&state1, &state1);
            manager1.update_snapshot(&state2, &state2);
        }
    });

    // Thread B: Rapid snapshot acquisition
    let handle_b = thread::spawn(move || {
        for _ in 0..1000 {
            let snapshot = manager2.acquire_snapshot();

            // Verify snapshot is consistent (either 50 or 100 nodes)
            let count = snapshot.node_count();
            assert!(
                count == 50 || count == 100,
                "Inconsistent snapshot state: {} nodes",
                count
            );
        }
    });

    handle_a.join().expect("Thread A panicked");
    handle_b.join().expect("Thread B panicked");
}

#[test]
fn test_concurrent_snapshot_ordering() {
    // Scenario: Multiple concurrent snapshots should see consistent ordering
    // Expected: Snapshots maintain happens-before relationship (or same timestamp if very fast)
    let manager = create_test_snapshot_manager(50);

    // Create snapshot 1
    let snapshot1 = manager.acquire_snapshot();
    let time1 = snapshot1.created_at;

    // Small delay to ensure different timestamp
    thread::sleep(Duration::from_millis(20));

    // Create snapshot 2
    let snapshot2 = manager.acquire_snapshot();
    let time2 = snapshot2.created_at;

    // Verify ordering (or equal if system is very fast)
    assert!(
        time2 >= time1,
        "Snapshot ordering violated: {:?} >= {:?}",
        time2,
        time1
    );
}

#[test]
fn test_snapshot_isolation_with_clones() {
    // Scenario: Multiple clones of same snapshot
    // Expected: All clones see same state
    let manager = create_test_snapshot_manager(50);

    let snapshot1 = manager.acquire_snapshot();
    let snapshot2 = Arc::clone(&snapshot1);

    assert_eq!(snapshot1.node_count(), snapshot2.node_count());
    assert_eq!(snapshot1.created_at, snapshot2.created_at);
}

//
// GROUP 3: MEMORY AND PERFORMANCE
//

#[test]
fn test_memory_no_leaks() {
    // Scenario: Create and drop many snapshots
    // Expected: No memory leaks
    let manager = create_test_snapshot_manager(50);

    // Create many snapshots in a loop
    for _ in 0..10_000 {
        let _snapshot = manager.acquire_snapshot();
        // Snapshot dropped here
    }

    // Final snapshot should still work
    let final_snapshot = manager.acquire_snapshot();
    assert!(final_snapshot.node_count() > 0);
}

#[test]
fn test_snapshot_clone_performance() {
    // Scenario: Clone Arc<SnapshotState> many times
    // Expected: Cloning is cheap (just atomic refcount increment)
    let manager = create_test_snapshot_manager(50);

    let snapshot = manager.acquire_snapshot();

    let start = Instant::now();

    // Clone 1000 times
    for _ in 0..1000 {
        let _ = Arc::clone(&snapshot);
    }

    let elapsed = start.elapsed();

    // Should be very fast (< 10ms)
    assert!(
        elapsed < Duration::from_millis(10),
        "Arc::clone too slow: {:?}",
        elapsed
    );

    println!("1000 Arc::clone operations in {:?}", elapsed);
}

#[test]
fn test_high_contention_snapshot_acquisition() {
    // Scenario: High contention with many threads
    // Expected: Still performs reasonably
    let manager = Arc::new(create_test_snapshot_manager(50));
    let barrier = Arc::new(Barrier::new(50));
    let success_count = Arc::new(AtomicU64::new(0));

    let handles: Vec<_> = (0..50)
        .map(|_| {
            let manager = manager.clone();
            let barrier = barrier.clone();
            let success_count = success_count.clone();

            thread::spawn(move || {
                barrier.wait();

                // Each thread acquires 100 snapshots
                for _ in 0..100 {
                    let snapshot = manager.acquire_snapshot();
                    if snapshot.node_count() > 0 {
                        success_count.fetch_add(1, Ordering::Relaxed);
                    }
                }
            })
        })
        .collect();

    for h in handles {
        h.join().expect("Thread panicked");
    }

    // Expected: 50 threads * 100 snapshots = 5000 successful acquisitions
    let success = success_count.load(Ordering::Relaxed);
    assert_eq!(
        success, 5000,
        "Expected all snapshot acquisitions to succeed"
    );
}

#[test]
fn test_snapshot_independence() {
    // Scenario: Multiple independent snapshots
    // Expected: Each snapshot is independent
    let manager = create_test_snapshot_manager(50);

    let snapshot1 = manager.acquire_snapshot();
    let snapshot2 = manager.acquire_snapshot();
    let snapshot3 = manager.acquire_snapshot();

    // All should have same state
    assert_eq!(snapshot1.node_count(), snapshot2.node_count());
    assert_eq!(snapshot2.node_count(), snapshot3.node_count());

    // Update state
    let mut new_outgoing = HashMap::new();
    for i in 0..100 {
        new_outgoing.insert(i, vec![]);
    }
    manager.update_snapshot(&new_outgoing, &new_outgoing);

    // Old snapshots unchanged
    assert_eq!(snapshot1.node_count(), 50);
    assert_eq!(snapshot2.node_count(), 50);
    assert_eq!(snapshot3.node_count(), 50);

    // New snapshot sees changes
    let snapshot4 = manager.acquire_snapshot();
    assert_eq!(snapshot4.node_count(), 100);
}

//
// GROUP 4: CONCURRENT ALGORITHM EXECUTION
//
// NOTE: SqliteGraph is NOT thread-safe (contains RefCell, non-Sync types).
// These tests focus on thread-safe SnapshotManager and verify algorithms
// work correctly with snapshots, not concurrent graph access.
//

#[test]
fn test_concurrent_snapshot_creation_with_algorithms() {
    // Scenario: Multiple threads create snapshots concurrently
    // Expected: All threads succeed, all snapshots valid
    let manager = Arc::new(create_test_snapshot_manager(100));
    let barrier = Arc::new(Barrier::new(10));
    let success_count = Arc::new(AtomicU64::new(0));

    let handles: Vec<_> = (0..10)
        .map(|_| {
            let manager = manager.clone();
            let barrier = barrier.clone();
            let success_count = success_count.clone();

            thread::spawn(move || {
                barrier.wait();

                // Each thread creates a snapshot
                let snapshot = manager.acquire_snapshot();

                if snapshot.node_count() > 0 {
                    success_count.fetch_add(1, Ordering::Relaxed);
                }
            })
        })
        .collect();

    for h in handles {
        h.join().expect("Thread panicked");
    }

    let success = success_count.load(Ordering::Relaxed);
    assert_eq!(success, 10, "Not all threads acquired valid snapshots");
}

#[test]
fn test_snapshot_state_with_algorithm_preparation() {
    // Scenario: Verify snapshot state is suitable for algorithm execution
    // Expected: Snapshot has all required data for algorithms
    let graph = create_test_graph().expect("Failed to create test graph");
    warm_cache(&graph).expect("Failed to warm cache");

    // Acquire snapshot
    let snapshot = graph
        .acquire_snapshot()
        .expect("Failed to acquire snapshot");

    // Verify snapshot has nodes
    assert!(snapshot.node_count() > 0, "Snapshot should have nodes");

    // Verify snapshot data structure is consistent
    let entity_ids = graph.list_entity_ids().expect("Failed to get entity IDs");
    for &id in &entity_ids {
        assert!(
            snapshot.contains_node(id),
            "Snapshot should contain node {}",
            id
        );
    }

    // Verify algorithms can run on the graph (not snapshot directly)
    let components = algo::connected_components(&graph);
    assert!(components.is_ok(), "Algorithm should run on graph");
}

//
// GROUP 5: ALGORITHM CONSISTENCY
//

#[test]
fn test_algorithm_determinism_multiple_runs() {
    // Scenario: Run same algorithm multiple times on same graph
    // Expected: Results are deterministic
    let graph = create_large_test_graph(30).expect("Failed to create graph");
    warm_cache(&graph).expect("Failed to warm cache");

    // Run PageRank twice
    let result1 = algo::pagerank(&graph, 0.85, 20);
    let result2 = algo::pagerank(&graph, 0.85, 20);

    assert!(result1.is_ok(), "First PageRank failed");
    assert!(result2.is_ok(), "Second PageRank failed");

    let scores1 = result1.unwrap();
    let scores2 = result2.unwrap();

    // Verify same number of scores
    assert_eq!(scores1.len(), scores2.len(), "Different number of scores");

    // Verify scores are approximately equal (floating point tolerance)
    for (s1, s2) in scores1.iter().zip(scores2.iter()) {
        assert_eq!(s1.0, s2.0, "Different node IDs");
        assert!(
            (s1.1 - s2.1).abs() < 1e-10,
            "Scores differ significantly: {} vs {}",
            s1.1,
            s2.1
        );
    }
}

#[test]
fn test_multiple_algorithms_same_graph() {
    // Scenario: Run multiple different algorithms on same graph
    // Expected: All algorithms succeed
    let graph = create_large_test_graph(40).expect("Failed to create graph");
    warm_cache(&graph).expect("Failed to warm cache");

    // Run multiple algorithms
    let components_result = algo::connected_components(&graph);
    let degrees_result = algo::nodes_by_degree(&graph, true);
    let pagerank_result = algo::pagerank(&graph, 0.85, 10);
    let cycles_result = algo::find_cycles_limited(&graph, 10);

    assert!(components_result.is_ok(), "Connected components failed");
    assert!(degrees_result.is_ok(), "Nodes by degree failed");
    assert!(pagerank_result.is_ok(), "PageRank failed");
    assert!(cycles_result.is_ok(), "Find cycles failed");

    // Verify results are non-empty for non-empty graph
    let components = components_result.unwrap();
    let degrees = degrees_result.unwrap();
    let pagerank = pagerank_result.unwrap();

    assert!(!components.is_empty() || graph.list_entity_ids().unwrap().is_empty());
    assert!(!degrees.is_empty() || graph.list_entity_ids().unwrap().is_empty());
    assert!(!pagerank.is_empty() || graph.list_entity_ids().unwrap().is_empty());
}

#[test]
fn test_algorithm_with_empty_graph() {
    // Scenario: Run algorithms on empty graph
    // Expected: All handle empty graph gracefully
    let graph = SqliteGraph::open_in_memory().expect("Failed to create graph");

    // Run algorithms on empty graph
    let components = algo::connected_components(&graph);
    let degrees = algo::nodes_by_degree(&graph, true);
    let pagerank = algo::pagerank(&graph, 0.85, 10);
    let cycles = algo::find_cycles_limited(&graph, 10);

    assert!(
        components.is_ok(),
        "Connected components should handle empty graph"
    );
    assert!(degrees.is_ok(), "Nodes by degree should handle empty graph");
    assert!(pagerank.is_ok(), "PageRank should handle empty graph");
    assert!(cycles.is_ok(), "Find cycles should handle empty graph");

    // Verify results are empty
    assert!(components.unwrap().is_empty());
    assert!(degrees.unwrap().is_empty());
    assert!(pagerank.unwrap().is_empty());
    assert!(cycles.unwrap().is_empty());
}

#[test]
fn test_algorithm_snapshot_consistency() {
    // Scenario: Acquire snapshot, run algorithm, verify consistency
    // Expected: Algorithm sees data consistent with snapshot
    let graph = create_test_graph().expect("Failed to create graph");
    warm_cache(&graph).expect("Failed to warm cache");

    // Acquire snapshot
    let snapshot = graph
        .acquire_snapshot()
        .expect("Failed to acquire snapshot");
    let snapshot_count = snapshot.node_count();

    // Run algorithm
    let components = algo::connected_components(&graph).expect("Components failed");

    // Verify consistency
    let graph_ids = graph.list_entity_ids().expect("Failed to get graph IDs");

    // Algorithm should see same number of nodes as snapshot
    assert_eq!(
        graph_ids.len() as usize,
        snapshot_count,
        "Algorithm and snapshot disagree on node count"
    );

    // All nodes in snapshot should be in graph
    assert!(!components.is_empty() || snapshot_count == 0);
}

//
// GROUP 6: STRESS TEST PATTERNS
//

#[test]
fn test_rapid_algorithm_execution() {
    // Scenario: Run algorithms rapidly in sequence
    // Expected: All operations succeed
    let graph = create_large_test_graph(30).expect("Failed to create graph");
    warm_cache(&graph).expect("Failed to warm cache");

    let start = Instant::now();

    // Run 100 algorithm executions
    for i in 0..100 {
        let result = if i % 4 == 0 {
            algo::connected_components(&graph).map(|_| ())
        } else if i % 4 == 1 {
            algo::nodes_by_degree(&graph, true).map(|_| ())
        } else if i % 4 == 2 {
            algo::pagerank(&graph, 0.85, 5).map(|_| ())
        } else {
            algo::find_cycles_limited(&graph, 5).map(|_| ())
        };

        assert!(result.is_ok(), "Algorithm {} failed", i);
    }

    let elapsed = start.elapsed();
    println!("100 algorithm executions in {:?}", elapsed);
}

#[test]
fn test_mixed_operations_sequence() {
    // Scenario: Alternate between reads, writes, and algorithms
    // Expected: All operations succeed
    let graph = create_large_test_graph(20).expect("Failed to create graph");

    for i in 0..50 {
        if i % 3 == 0 {
            // Read operation
            let _ = graph.list_entity_ids().expect("List IDs failed");
        } else if i % 3 == 1 {
            // Write operation
            let _ = insert_entity(
                &graph,
                GraphEntityCreate {
                    kind: "mixed".to_string(),
                    name: format!("mixed_node_{}", i),
                    file_path: Some(format!("mixed_{}.rs", i)),
                    data: serde_json::json!({}),
                },
            );
        } else {
            // Algorithm operation
            let _ = algo::nodes_by_degree(&graph, false);
        }
    }

    // Verify final state
    let final_count = node_count(&graph).expect("Failed to get final count");
    assert!(final_count > 20, "Graph should have more nodes");
}

#[test]
fn test_rapid_snapshot_creation_destruction_10k() {
    // Scenario: Rapid snapshot creation and destruction (10K iterations)
    // Expected: All operations succeed, no memory leaks
    let graph = create_large_test_graph(50).expect("Failed to create graph");
    warm_cache(&graph).expect("Failed to warm cache");

    let start = Instant::now();

    for i in 0..10_000 {
        let snapshot = graph
            .acquire_snapshot()
            .expect("Failed to acquire snapshot");

        // Verify snapshot is valid
        assert!(snapshot.node_count() > 0, "Snapshot {} invalid", i);

        // Snapshot dropped here
    }

    let elapsed = start.elapsed();
    println!("10K snapshot creations in {:?}", elapsed);

    // Final snapshot should still work
    let final_snapshot = graph
        .acquire_snapshot()
        .expect("Failed to acquire final snapshot");
    assert!(final_snapshot.node_count() > 0, "Final snapshot invalid");
}

#[test]
fn test_snapshot_during_algorithm_execution() {
    // Scenario: Acquire snapshots while algorithm is running
    // Expected: All snapshots are independent and consistent
    let graph = create_large_test_graph(30).expect("Failed to create graph");
    warm_cache(&graph).expect("Failed to warm cache");

    // Acquire initial snapshot
    let snapshot1 = graph
        .acquire_snapshot()
        .expect("Failed to acquire snapshot");
    let count1 = snapshot1.node_count();

    // Run algorithm
    let _ = algo::label_propagation(&graph, 5).expect("Label propagation failed");

    // Acquire second snapshot
    let snapshot2 = graph
        .acquire_snapshot()
        .expect("Failed to acquire snapshot");
    let count2 = snapshot2.node_count();

    // Verify snapshots are independent but same (no writes occurred)
    assert_eq!(snapshot1.node_count(), count1);
    assert_eq!(snapshot2.node_count(), count2);
    assert_eq!(count1, count2);
}

//
// GROUP 7: INTEGRATION WITH SQLITEGRAPH (SINGLE-THREADED)
//

#[test]
fn test_graph_snapshot_creation() {
    // Scenario: Create snapshot from SqliteGraph
    // Expected: Snapshot captures current state
    let graph = create_test_graph().expect("Failed to create test graph");
    warm_cache(&graph).expect("Failed to warm cache");

    let snapshot1 = graph
        .acquire_snapshot()
        .expect("Failed to acquire snapshot");
    let original_count = snapshot1.node_count();

    assert!(original_count > 0, "Snapshot should have nodes");

    // Modify graph
    let _ = insert_entity(
        &graph,
        GraphEntityCreate {
            kind: "new".to_string(),
            name: "new_node".to_string(),
            file_path: Some("new.rs".to_string()),
            data: serde_json::json!({}),
        },
    );

    // Original snapshot unchanged
    assert_eq!(snapshot1.node_count(), original_count);

    // New snapshot reflects changes
    warm_cache(&graph).expect("Failed to warm cache");
    let snapshot2 = graph
        .acquire_snapshot()
        .expect("Failed to acquire second snapshot");
    assert!(snapshot2.node_count() > original_count);
}

#[test]
fn test_graph_snapshot_isolation() {
    // Scenario: Verify snapshot isolation in graph context
    // Expected: Multiple snapshots are independent
    let graph = create_large_test_graph(100).expect("Failed to create large graph");
    warm_cache(&graph).expect("Failed to warm cache");

    let snapshot1 = graph
        .acquire_snapshot()
        .expect("Failed to acquire snapshot");
    let count1 = snapshot1.node_count();

    // Modify graph
    let _ = insert_entity(
        &graph,
        GraphEntityCreate {
            kind: "test".to_string(),
            name: "new_node".to_string(),
            file_path: Some("new.rs".to_string()),
            data: serde_json::json!({}),
        },
    );

    warm_cache(&graph).expect("Failed to warm cache");
    let snapshot2 = graph
        .acquire_snapshot()
        .expect("Failed to acquire snapshot");
    let count2 = snapshot2.node_count();

    // Verify isolation
    assert_eq!(snapshot1.node_count(), count1, "Snapshot 1 changed");
    assert_eq!(snapshot2.node_count(), count2, "Snapshot 2 has wrong count");
    assert!(count2 > count1, "Snapshot 2 should have more nodes");
}

#[test]
fn test_graph_snapshot_performance() {
    // Scenario: Measure snapshot acquisition performance
    // Expected: Fast snapshot creation
    let graph = create_test_graph().expect("Failed to create test graph");
    warm_cache(&graph).expect("Failed to warm cache");

    let start = Instant::now();

    for _ in 0..100 {
        let _ = graph
            .acquire_snapshot()
            .expect("Failed to acquire snapshot");
    }

    let elapsed = start.elapsed();

    println!("100 snapshot acquisitions in {:?}", elapsed);

    // Should be reasonably fast (< 100ms)
    assert!(
        elapsed < Duration::from_millis(100),
        "Snapshot acquisition too slow: {:?}",
        elapsed
    );
}