sqlitegraph 2.0.7

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
//! Tests for adjacency iteration functionality

use super::{AdjacencyHelpers, AdjacencyIterator};
use crate::backend::native::graph_file::GraphFile;
use crate::backend::native::node_store::NodeStore;
use crate::backend::native::types::*;

#[cfg(test)]
fn create_test_graph_file() -> (GraphFile, tempfile::NamedTempFile) {
    let temp_file = tempfile::NamedTempFile::new().unwrap();
    let path = temp_file.path();
    let graph_file = GraphFile::create(path).unwrap();
    (graph_file, temp_file)
}

#[test]
fn test_adjacency_iterator_empty() {
    let (mut graph_file, _temp_file) = create_test_graph_file();

    // Create a node with no edges
    let node = NodeRecord::new(
        1,
        "Test".to_string(),
        "node1".to_string(),
        serde_json::json!({}),
    );
    {
        let mut node_store = NodeStore::new(&mut graph_file);
        node_store.write_node(&node).unwrap();
    }

    // Test outgoing iterator
    let iterator = AdjacencyIterator::new_outgoing(&mut graph_file, 1).unwrap();
    assert_eq!(iterator.total_count(), 0);
    assert!(iterator.is_complete());

    // Test incoming iterator
    let iterator = AdjacencyIterator::new_incoming(&mut graph_file, 1).unwrap();
    assert_eq!(iterator.total_count(), 0);
    assert!(iterator.is_complete());
}

#[test]
fn test_adjacency_validation() {
    let (mut graph_file, _temp_file) = create_test_graph_file();
    let mut node_store = NodeStore::new(&mut graph_file);

    // Create a node
    let node = NodeRecord::new(
        1,
        "Test".to_string(),
        "node1".to_string(),
        serde_json::json!({}),
    );
    node_store.write_node(&node).unwrap();

    // Validate adjacency (should pass for node with no edges)
    let result = AdjacencyHelpers::validate_node_adjacency(&mut graph_file, 1);
    assert!(result.is_ok());
}

#[cfg(test)]
mod linear_detector_tests {
    use super::super::{LinearDetector, TraversalPattern};

    /// Chain graph confirms Linear after 3 steps.
    ///
    /// Graph structure:
    ///   1 -> 2 -> 3 -> 4 -> 5
    ///
    /// Each node has degree 1 (one outgoing edge).
    /// After 3 consecutive degree-1 observations, detector confirms Linear.
    #[test]
    fn test_linear_detector_chain() {
        let mut detector = LinearDetector::new();

        // Step 1: First degree-1 observation
        let pattern = detector.observe(1, 1);
        assert_eq!(
            pattern,
            TraversalPattern::Unknown,
            "Step 1 should be Unknown"
        );
        assert_eq!(
            detector.current_pattern(),
            TraversalPattern::Unknown,
            "Current pattern at step 1"
        );
        assert!(
            !detector.is_linear_confirmed(),
            "Should not be confirmed at step 1"
        );
        assert!((detector.confidence() - 1.0 / 3.0).abs() < f64::EPSILON);

        // Step 2: Second degree-1 observation
        let pattern = detector.observe(2, 1);
        assert_eq!(
            pattern,
            TraversalPattern::Unknown,
            "Step 2 should be Unknown"
        );
        assert_eq!(
            detector.current_pattern(),
            TraversalPattern::Unknown,
            "Current pattern at step 2"
        );
        assert!(
            !detector.is_linear_confirmed(),
            "Should not be confirmed at step 2"
        );
        assert!((detector.confidence() - 2.0 / 3.0).abs() < f64::EPSILON);

        // Step 3: Third degree-1 observation - threshold reached!
        let pattern = detector.observe(3, 1);
        assert_eq!(pattern, TraversalPattern::Linear, "Step 3 should be Linear");
        assert_eq!(
            detector.current_pattern(),
            TraversalPattern::Linear,
            "Current pattern at step 3"
        );
        assert!(
            detector.is_linear_confirmed(),
            "Should be confirmed at step 3"
        );
        assert_eq!(
            detector.confidence(),
            1.0,
            "Confidence should be 1.0 at step 3"
        );

        // Step 4+: Remain Linear
        let pattern = detector.observe(4, 1);
        assert_eq!(
            pattern,
            TraversalPattern::Linear,
            "Step 4 should stay Linear"
        );
        assert!(detector.is_linear_confirmed());

        let pattern = detector.observe(5, 1);
        assert_eq!(
            pattern,
            TraversalPattern::Linear,
            "Step 5 should stay Linear"
        );
        assert!(detector.is_linear_confirmed());
    }

    /// Star graph immediately triggers Branching.
    ///
    /// Graph structure:
    ///        2
    ///        |
    ///   4 - 1 - 3
    ///        |
    ///        5
    ///
    /// Node 1 (center) has degree 4, triggering immediate Branching.
    #[test]
    fn test_linear_detector_star() {
        let mut detector = LinearDetector::new();

        // Center node with degree 4 -> immediate Branching
        let pattern = detector.observe(1, 4);
        assert_eq!(
            pattern,
            TraversalPattern::Branching,
            "Degree 4 should trigger Branching immediately"
        );
        assert_eq!(
            detector.current_pattern(),
            TraversalPattern::Branching,
            "Current pattern should be Branching"
        );
        assert_eq!(
            detector.confidence(),
            0.0,
            "Confidence should be 0.0 for Branching"
        );
        assert!(
            !detector.is_linear_confirmed(),
            "Branching pattern should never be confirmed Linear"
        );

        // Branching is terminal - subsequent observations stay Branching
        let pattern = detector.observe(2, 1);
        assert_eq!(
            pattern,
            TraversalPattern::Branching,
            "Branching is terminal state"
        );

        let pattern = detector.observe(3, 1);
        assert_eq!(pattern, TraversalPattern::Branching, "Still Branching");

        assert_eq!(detector.confidence(), 0.0, "Confidence remains 0.0");
    }

    /// Diamond graph transitions from Unknown to Branching at degree-2 node.
    ///
    /// Graph structure:
    ///   1 -> 2 -> 4
    ///   1 -> 3 -> 4
    ///
    /// Starting from leaf (degree 1), then hitting diamond join (degree 2).
    /// Diamond join triggers immediate Branching before Linear confirmation.
    #[test]
    fn test_linear_detector_diamond() {
        let mut detector = LinearDetector::new();

        // Start from leaf node (degree 1)
        let pattern = detector.observe(1, 1);
        assert_eq!(
            pattern,
            TraversalPattern::Unknown,
            "First degree-1 should be Unknown"
        );
        assert!(!detector.is_linear_confirmed());

        // Second linear step
        let pattern = detector.observe(2, 1);
        assert_eq!(
            pattern,
            TraversalPattern::Unknown,
            "Second degree-1 should still be Unknown"
        );
        assert!(!detector.is_linear_confirmed());

        // Diamond join: degree 2 -> immediate Branching
        let pattern = detector.observe(3, 2);
        assert_eq!(
            pattern,
            TraversalPattern::Branching,
            "Degree 2 should trigger Branching"
        );
        assert_eq!(detector.current_pattern(), TraversalPattern::Branching);
        assert!(!detector.is_linear_confirmed());
        assert_eq!(detector.confidence(), 0.0);
    }

    /// Diamond variant: Start Accumulating then hit Branching.
    ///
    /// Tests the transition from Accumulating internal state to Branching.
    #[test]
    fn test_linear_detector_diamond_accumulating_then_branching() {
        let mut detector = LinearDetector::new();

        // Linear step 1: Unknown
        assert_eq!(detector.observe(1, 1), TraversalPattern::Unknown);

        // Linear step 2: Unknown (internally Accumulating)
        assert_eq!(detector.observe(2, 1), TraversalPattern::Unknown);

        // Degree 2 node: should transition to Branching from Accumulating
        assert_eq!(detector.observe(3, 2), TraversalPattern::Branching);
        assert!(!detector.is_linear_confirmed());
        assert_eq!(detector.confidence(), 0.0);
    }

    /// Tree graph shows Accumulating behavior without Linear confirmation.
    ///
    /// Graph structure:
    ///       1
    ///      /|\
    ///     2 3 4
    ///     |
    ///     5
    ///     |
    ///     6
    ///
    /// Root has degree 3 -> immediate Branching.
    /// Or: following a branch shows Accumulating until hitting another branch.
    #[test]
    fn test_linear_detector_tree() {
        let mut detector = LinearDetector::new();

        // Root node with degree 3 -> immediate Branching
        let pattern = detector.observe(1, 3);
        assert_eq!(
            pattern,
            TraversalPattern::Branching,
            "Root degree 3 triggers Branching"
        );
        assert!(!detector.is_linear_confirmed());
    }

    /// Tree depth-first traversal: Accumulating then Branching at child.
    ///
    /// Simulates DFS down a linear branch that then splits.
    #[test]
    fn test_linear_detector_tree_depth_first() {
        let mut detector = LinearDetector::new();

        // Linear steps on branch
        assert_eq!(detector.observe(1, 1), TraversalPattern::Unknown);
        assert!((detector.confidence() - 1.0 / 3.0).abs() < f64::EPSILON);

        assert_eq!(detector.observe(2, 1), TraversalPattern::Unknown);
        assert!((detector.confidence() - 2.0 / 3.0).abs() < f64::EPSILON);

        // Child node has degree 2 -> Branching before Linear confirmation
        assert_eq!(detector.observe(3, 2), TraversalPattern::Branching);
        assert!(!detector.is_linear_confirmed());
        assert_eq!(detector.confidence(), 0.0);
    }

    /// Confidence score progression through Linear detection.
    ///
    /// Verifies confidence increases: 0.0 -> 0.33 -> 0.67 -> 1.0
    #[test]
    fn test_linear_detector_confidence() {
        let mut detector = LinearDetector::new();

        // Initial: no observations
        assert_eq!(
            detector.confidence(),
            0.0,
            "Initial confidence should be 0.0"
        );
        assert_eq!(
            detector.current_pattern(),
            TraversalPattern::Unknown,
            "Initial pattern should be Unknown"
        );

        // Step 1: 1/3 ≈ 0.33
        let pattern = detector.observe(1, 1);
        assert_eq!(pattern, TraversalPattern::Unknown);
        assert!((detector.confidence() - 1.0 / 3.0).abs() < f64::EPSILON);
        assert_eq!(detector.current_pattern(), TraversalPattern::Unknown);

        // Step 2: 2/3 ≈ 0.67
        let pattern = detector.observe(2, 1);
        assert_eq!(pattern, TraversalPattern::Unknown);
        assert!((detector.confidence() - 2.0 / 3.0).abs() < f64::EPSILON);
        assert_eq!(detector.current_pattern(), TraversalPattern::Unknown);

        // Step 3: 3/3 = 1.0 (confirmed)
        let pattern = detector.observe(3, 1);
        assert_eq!(pattern, TraversalPattern::Linear);
        assert_eq!(detector.confidence(), 1.0);
        assert_eq!(detector.current_pattern(), TraversalPattern::Linear);

        // Step 4+: remain at 1.0
        let pattern = detector.observe(4, 1);
        assert_eq!(pattern, TraversalPattern::Linear);
        assert_eq!(detector.confidence(), 1.0);

        let pattern = detector.observe(5, 1);
        assert_eq!(pattern, TraversalPattern::Linear);
        assert_eq!(detector.confidence(), 1.0);
    }

    /// Reset clears detector state between traversals.
    ///
    /// Verifies reset() returns detector to initial Unknown state.
    #[test]
    fn test_linear_detector_reset() {
        let mut detector = LinearDetector::new();

        // Confirm Linear pattern first
        detector.observe(1, 1);
        detector.observe(2, 1);
        detector.observe(3, 1);

        assert!(
            detector.is_linear_confirmed(),
            "Should be Linear after 3 degree-1 steps"
        );
        assert_eq!(detector.confidence(), 1.0, "Confidence should be 1.0");
        assert_eq!(
            detector.current_pattern(),
            TraversalPattern::Linear,
            "Pattern should be Linear"
        );

        // Reset
        detector.reset();

        // Verify back to initial state
        assert!(
            !detector.is_linear_confirmed(),
            "Should not be confirmed after reset"
        );
        assert_eq!(
            detector.confidence(),
            0.0,
            "Confidence should be 0.0 after reset"
        );
        assert_eq!(
            detector.current_pattern(),
            TraversalPattern::Unknown,
            "Pattern should be Unknown after reset"
        );

        // Can detect again - should work identically
        let pattern = detector.observe(1, 1);
        assert_eq!(
            pattern,
            TraversalPattern::Unknown,
            "First observation after reset"
        );
        assert!((detector.confidence() - 1.0 / 3.0).abs() < f64::EPSILON);

        detector.observe(2, 1);
        detector.observe(3, 1);
        assert!(
            detector.is_linear_confirmed(),
            "Should confirm Linear again"
        );
    }

    /// Custom threshold behavior.
    ///
    /// Verifies threshold=5 requires 5 steps for Linear confirmation.
    #[test]
    fn test_linear_threshold_custom() {
        let detector = LinearDetector::with_threshold(5);
        assert_eq!(detector.confidence(), 0.0);

        let mut detector = LinearDetector::with_threshold(5);

        // Steps 1-4: Accumulating, not Linear yet
        let pattern = detector.observe(1, 1);
        assert_eq!(pattern, TraversalPattern::Unknown);
        assert!((detector.confidence() - 1.0 / 5.0).abs() < f64::EPSILON);
        assert!(!detector.is_linear_confirmed());

        let pattern = detector.observe(2, 1);
        assert_eq!(pattern, TraversalPattern::Unknown);
        assert!((detector.confidence() - 2.0 / 5.0).abs() < f64::EPSILON);
        assert!(!detector.is_linear_confirmed());

        let pattern = detector.observe(3, 1);
        assert_eq!(pattern, TraversalPattern::Unknown);
        assert!((detector.confidence() - 3.0 / 5.0).abs() < f64::EPSILON);
        assert!(!detector.is_linear_confirmed());

        let pattern = detector.observe(4, 1);
        assert_eq!(pattern, TraversalPattern::Unknown);
        assert!((detector.confidence() - 4.0 / 5.0).abs() < f64::EPSILON);
        assert!(!detector.is_linear_confirmed());

        // Step 5: Now confirmed
        let pattern = detector.observe(5, 1);
        assert_eq!(
            pattern,
            TraversalPattern::Linear,
            "Should confirm Linear at threshold=5"
        );
        assert_eq!(detector.confidence(), 1.0);
        assert!(detector.is_linear_confirmed());
    }

    /// Dead end handling: degree 0 keeps detector in Unknown.
    ///
    /// Dead ends (leaf nodes with no outgoing edges) should not
    /// contribute to Linear pattern detection.
    #[test]
    fn test_linear_detector_dead_end() {
        let mut detector = LinearDetector::new();

        // Degree 0: dead end, should stay Unknown
        let pattern = detector.observe(1, 0);
        assert_eq!(
            pattern,
            TraversalPattern::Unknown,
            "Degree 0 should be Unknown"
        );
        assert_eq!(
            detector.current_pattern(),
            TraversalPattern::Unknown,
            "Current pattern should be Unknown"
        );
        assert_eq!(detector.confidence(), 0.0, "Confidence should be 0.0");
        assert!(
            !detector.is_linear_confirmed(),
            "Should not be confirmed with degree 0"
        );

        // Another degree 0
        let pattern = detector.observe(2, 0);
        assert_eq!(pattern, TraversalPattern::Unknown);
        assert_eq!(detector.confidence(), 0.0);

        // Then degree 1 - should start counting from scratch
        let pattern = detector.observe(3, 1);
        assert_eq!(pattern, TraversalPattern::Unknown);
        assert!((detector.confidence() - 1.0 / 3.0).abs() < f64::EPSILON);
    }

    /// Dead end between linear steps does NOT reset progress (by design).
    ///
    /// Chain: 1 -> (dead end) -> 3 -> 4 -> 5
    /// Degree 0 doesn't increment consecutive_linear but also doesn't reset it.
    /// The detector maintains Accumulating state but doesn't progress toward Linear.
    #[test]
    fn test_linear_detector_dead_end_breaks_chain() {
        let mut detector = LinearDetector::new();

        // Step 1: degree 1 -> Accumulating
        assert_eq!(detector.observe(1, 1), TraversalPattern::Unknown);
        assert!((detector.confidence() - 1.0 / 3.0).abs() < f64::EPSILON);

        // Step 2: degree 0 -> stays in Accumulating (doesn't reset counter)
        // The state remains Accumulating, returns Unknown for pattern
        assert_eq!(detector.observe(2, 0), TraversalPattern::Unknown);
        // Confidence still reflects progress (doesn't reset)
        assert!((detector.confidence() - 1.0 / 3.0).abs() < f64::EPSILON);

        // Step 3: degree 1 -> increments counter to 2
        assert_eq!(detector.observe(3, 1), TraversalPattern::Unknown);
        assert!((detector.confidence() - 2.0 / 3.0).abs() < f64::EPSILON);

        // Step 4: degree 1 -> now threshold reached!
        assert_eq!(detector.observe(4, 1), TraversalPattern::Linear);
        assert!(detector.is_linear_confirmed());

        // Note: The dead end didn't reset progress, just didn't advance it.
        // This is intentional: dead ends are rare in chain traversals.
    }
}

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

    /// Helper to create a graph file with a specified number of nodes
    fn create_test_graph_file_with_nodes(node_count: u64) -> (GraphFile, tempfile::NamedTempFile) {
        let (mut graph_file, temp_file) = create_test_graph_file();
        let mut node_store = NodeStore::new(&mut graph_file);

        // Create nodes sequentially
        for i in 1..=node_count {
            let node = NodeRecord::new(
                i as i64,
                "TestNode".to_string(),
                format!("node_{}", i),
                serde_json::json!({"index": i}),
            );
            node_store.write_node(&node).unwrap();
        }

        (graph_file, temp_file)
    }

    #[test]
    fn test_read_slots_batch_basic() {
        let (mut graph_file, _temp_file) = create_test_graph_file_with_nodes(10);
        let mut node_store = NodeStore::new(&mut graph_file);

        // Read 3 slots starting at node 1
        let result = node_store.read_slots_batch(1, 3);
        assert!(result.is_ok());

        let nodes = result.unwrap();
        assert_eq!(nodes.len(), 3);
        assert_eq!(nodes[0].id, 1);
        assert_eq!(nodes[1].id, 2);
        assert_eq!(nodes[2].id, 3);
    }

    #[test]
    fn test_read_slots_batch_bounds_clamping() {
        let (mut graph_file, _temp_file) = create_test_graph_file_with_nodes(5);
        let mut node_store = NodeStore::new(&mut graph_file);

        // Request 10 slots but only 5 exist - should clamp to 5
        let result = node_store.read_slots_batch(1, 10);
        assert!(result.is_ok());

        let nodes = result.unwrap();
        assert_eq!(nodes.len(), 5); // Clamped to available nodes
    }

    #[test]
    fn test_read_slots_batch_invalid_start() {
        let (mut graph_file, _temp_file) = create_test_graph_file_with_nodes(10);
        let mut node_store = NodeStore::new(&mut graph_file);

        // Start node ID <= 0 should error
        let result = node_store.read_slots_batch(0, 3);
        assert!(result.is_err());

        // Start node ID > node_count should error
        let result = node_store.read_slots_batch(99, 3);
        assert!(result.is_err());
    }

    #[test]
    fn test_sequential_buffer_prefetch_integration() {
        let (mut graph_file, _temp_file) = create_test_graph_file_with_nodes(20);
        let mut buffer = SequentialReadBuffer::new();

        // Prefetch from node 5 (should cache nodes 5-12 with default window of 8)
        let result = buffer.prefetch_from(&mut graph_file, 5);
        assert!(result.is_ok());

        // Verify nodes are cached
        assert!(buffer.contains(5));
        assert!(buffer.contains(6));
        assert!(buffer.contains(12));

        // Nodes outside prefetch window should not be cached
        assert!(!buffer.contains(4));
        assert!(!buffer.contains(13));
    }

    #[test]
    fn test_sequential_buffer_get_after_prefetch() {
        let (mut graph_file, _temp_file) = create_test_graph_file_with_nodes(10);
        let mut buffer = SequentialReadBuffer::new();

        // Prefetch from node 1
        buffer.prefetch_from(&mut graph_file, 1).unwrap();

        // Get should return Some for cached nodes
        let node = buffer.get(3);
        assert!(node.is_some());
        assert_eq!(node.unwrap().id, 3);

        // Get should return None for uncached nodes
        let node = buffer.get(99);
        assert!(node.is_none());
    }

    #[test]
    fn test_sequential_buffer_custom_prefetch_window() {
        let (mut graph_file, _temp_file) = create_test_graph_file_with_nodes(20);
        let mut buffer = SequentialReadBuffer::with_prefetch_window(4);

        // Prefetch from node 5 with custom window of 4
        buffer.prefetch_from(&mut graph_file, 5).unwrap();

        // Verify only 4 nodes are cached (5-8)
        assert!(buffer.contains(5));
        assert!(buffer.contains(8));
        assert!(!buffer.contains(9));

        // Verify buffer reports correct length
        assert_eq!(buffer.len(), 4);
    }

    #[test]
    fn test_read_slots_batch_single_node() {
        let (mut graph_file, _temp_file) = create_test_graph_file_with_nodes(10);
        let mut node_store = NodeStore::new(&mut graph_file);

        // Read just 1 slot
        let result = node_store.read_slots_batch(5, 1);
        assert!(result.is_ok());

        let nodes = result.unwrap();
        assert_eq!(nodes.len(), 1);
        assert_eq!(nodes[0].id, 5);
        assert_eq!(nodes[0].kind, "TestNode");
        assert_eq!(nodes[0].name, "node_5");
    }

    #[test]
    fn test_sequential_buffer_empty_after_prefetch_error() {
        let (mut graph_file, _temp_file) = create_test_graph_file_with_nodes(5);
        let mut buffer = SequentialReadBuffer::new();

        // Try to prefetch from invalid node ID (beyond file)
        let result = buffer.prefetch_from(&mut graph_file, 99);
        assert!(result.is_err());

        // Buffer should remain empty after failed prefetch
        assert!(buffer.is_empty());
        assert!(!buffer.contains(1));
    }
}