mushroomdb 0.6.9

Embedded graph database with Cypher queries, rule triggers, and Arrow export
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
/// Tests for materialized property views (Task 3).
///
/// Coverage:
/// - Degree view basic backfill and incremental update
/// - NeighborAgg Sum backfill and incremental update
/// - NeighborAgg Avg updates on neighbor prop change
/// - MIN retraction recompute (O(degree))
/// - Degree view over derived edges (fire AND retract)
/// - View prop in Cypher WHERE + grouped aggregation
/// - SET on a view prop → ViewPropReadOnly error
/// - Reopen rebuild identity (WAL replay)
/// - View over edges in both directions
/// - delete_view removes values cleanly
/// - Snapshot+WAL round-trip preserves views
/// - DST oracle: quiescent value == scratch recompute
use core_api::{
    AggFn, Direction, GraphDb, GraphError, Predicate, RuleDef, Value, ViewDef, ViewSource,
};

fn tmp(name: &str) -> std::path::PathBuf {
    let d = std::env::temp_dir().join(format!("graphdb-view-{}-{}", name, std::process::id()));
    let _ = std::fs::remove_dir_all(&d);
    d
}

fn degree_view(
    name: &str,
    label: &str,
    view_prop: &str,
    edge_type: &str,
    direction: Direction,
) -> ViewDef {
    ViewDef {
        name: name.into(),
        label: label.into(),
        view_prop: view_prop.into(),
        source: ViewSource::Degree {
            edge_type: edge_type.into(),
            direction,
        },
    }
}

fn neighbor_agg_view(
    name: &str,
    label: &str,
    view_prop: &str,
    edge_type: &str,
    direction: Direction,
    agg: AggFn,
    prop: &str,
) -> ViewDef {
    ViewDef {
        name: name.into(),
        label: label.into(),
        view_prop: view_prop.into(),
        source: ViewSource::NeighborAgg {
            edge_type: edge_type.into(),
            direction,
            agg,
            prop: prop.into(),
        },
    }
}

// ---------------------------------------------------------------------------
// Degree views
// ---------------------------------------------------------------------------

#[test]
fn degree_view_backfill_and_incremental() {
    let dir = tmp("deg");
    let mut db = GraphDb::open(&dir).unwrap();

    db.insert_node("City", "c1", vec![]).unwrap();
    db.insert_node("Person", "p1", vec![]).unwrap();
    db.insert_node("Person", "p2", vec![]).unwrap();

    // Create view BEFORE edges exist — backfill should yield 0.
    db.create_view(degree_view(
        "city_in_deg",
        "City",
        "pop",
        "LIVES_IN",
        Direction::In,
    ))
    .unwrap();
    let c1_id = "c1";
    assert_eq!(db.get_prop(c1_id, "pop"), Some(Value::Int(0)));

    // Add edges incrementally.
    db.insert_edge("LIVES_IN", "p1", "c1").unwrap();
    assert_eq!(db.get_prop(c1_id, "pop"), Some(Value::Int(1)));
    db.insert_edge("LIVES_IN", "p2", "c1").unwrap();
    assert_eq!(db.get_prop(c1_id, "pop"), Some(Value::Int(2)));

    // Delete an edge — decrement.
    db.delete_edge("LIVES_IN", "p1", "c1").unwrap();
    assert_eq!(db.get_prop(c1_id, "pop"), Some(Value::Int(1)));
}

#[test]
fn degree_view_out_direction() {
    let dir = tmp("deg_out");
    let mut db = GraphDb::open(&dir).unwrap();
    db.insert_node("Person", "p1", vec![]).unwrap();
    db.insert_node("City", "c1", vec![]).unwrap();
    db.insert_node("City", "c2", vec![]).unwrap();
    db.create_view(degree_view(
        "person_out_deg",
        "Person",
        "num_cities",
        "LIVES_IN",
        Direction::Out,
    ))
    .unwrap();
    assert_eq!(db.get_prop("p1", "num_cities"), Some(Value::Int(0)));
    db.insert_edge("LIVES_IN", "p1", "c1").unwrap();
    assert_eq!(db.get_prop("p1", "num_cities"), Some(Value::Int(1)));
    db.insert_edge("LIVES_IN", "p1", "c2").unwrap();
    assert_eq!(db.get_prop("p1", "num_cities"), Some(Value::Int(2)));
}

// ---------------------------------------------------------------------------
// NeighborAgg views
// ---------------------------------------------------------------------------

#[test]
fn neighbor_sum_backfill_and_incremental() {
    let dir = tmp("sum");
    let mut db = GraphDb::open(&dir).unwrap();

    db.insert_node("City", "c1", vec![]).unwrap();
    db.insert_node("Person", "p1", vec![("score".into(), Value::Float(3.0))])
        .unwrap();
    db.insert_node("Person", "p2", vec![("score".into(), Value::Float(7.0))])
        .unwrap();
    db.insert_edge("LIVES_IN", "p1", "c1").unwrap();
    db.insert_edge("LIVES_IN", "p2", "c1").unwrap();

    // Create view after edges — backfill should sum 3+7=10.
    db.create_view(neighbor_agg_view(
        "city_score",
        "City",
        "score_sum",
        "LIVES_IN",
        Direction::In,
        AggFn::Sum,
        "score",
    ))
    .unwrap();
    assert_eq!(db.get_prop("c1", "score_sum"), Some(Value::Float(10.0)));

    // Remove p1's edge — Sum decrements by 3.
    db.delete_edge("LIVES_IN", "p1", "c1").unwrap();
    assert_eq!(db.get_prop("c1", "score_sum"), Some(Value::Float(7.0)));

    // Add it back.
    db.insert_edge("LIVES_IN", "p1", "c1").unwrap();
    assert_eq!(db.get_prop("c1", "score_sum"), Some(Value::Float(10.0)));
}

#[test]
fn neighbor_count_skips_missing_prop() {
    // City c1 with two LIVES_IN people: one has `weight`, one does not
    // View NeighborAgg Count on `weight` → 1, not 2
    // Degree view on same etype → 2
    let dir = tmp("count_missing");
    let mut db = GraphDb::open(&dir).unwrap();

    db.insert_node("City", "c1", vec![]).unwrap();
    db.insert_node("Person", "p1", vec![("weight".into(), Value::Float(1.5))])
        .unwrap();
    db.insert_node("Person", "p2", vec![]).unwrap();
    db.insert_edge("LIVES_IN", "p1", "c1").unwrap();
    db.insert_edge("LIVES_IN", "p2", "c1").unwrap();

    db.create_view(neighbor_agg_view(
        "city_weight_count",
        "City",
        "weight_n",
        "LIVES_IN",
        Direction::In,
        AggFn::Count,
        "weight",
    ))
    .unwrap();
    db.create_view(degree_view(
        "city_in_deg",
        "City",
        "pop",
        "LIVES_IN",
        Direction::In,
    ))
    .unwrap();

    assert_eq!(db.get_prop("c1", "weight_n"), Some(Value::Int(1)));
    assert_eq!(db.get_prop("c1", "pop"), Some(Value::Int(2)));
    assert_eq!(
        db.get_prop("c1", "weight_n"),
        db.scratch_view_value("c1", "city_weight_count"),
        "incremental Count must match scratch recompute"
    );

    db.set_prop("p2", "weight", Value::Float(2.0)).unwrap();
    assert_eq!(db.get_prop("c1", "weight_n"), Some(Value::Int(2)));
    db.remove_prop("p1", "weight").unwrap();
    assert_eq!(db.get_prop("c1", "weight_n"), Some(Value::Int(1)));
    assert_eq!(db.get_prop("c1", "pop"), Some(Value::Int(2)));

    db.insert_node("Person", "p3", vec![("weight".into(), Value::Float(3.0))])
        .unwrap();
    db.insert_edge("LIVES_IN", "p3", "c1").unwrap();
    db.insert_node("Person", "p4", vec![]).unwrap();
    db.insert_edge("LIVES_IN", "p4", "c1").unwrap();
    assert_eq!(db.get_prop("c1", "weight_n"), Some(Value::Int(2)));
    assert_eq!(db.get_prop("c1", "pop"), Some(Value::Int(4)));
    db.delete_edge("LIVES_IN", "p3", "c1").unwrap();
    assert_eq!(db.get_prop("c1", "weight_n"), Some(Value::Int(1)));
    assert_eq!(
        db.get_prop("c1", "weight_n"),
        db.scratch_view_value("c1", "city_weight_count")
    );
}

#[test]
fn neighbor_avg_updates_on_prop_change() {
    let dir = tmp("avg");
    let mut db = GraphDb::open(&dir).unwrap();

    db.insert_node("City", "c1", vec![]).unwrap();
    db.insert_node("Person", "p1", vec![("score".into(), Value::Float(4.0))])
        .unwrap();
    db.insert_node("Person", "p2", vec![("score".into(), Value::Float(6.0))])
        .unwrap();
    db.insert_edge("LIVES_IN", "p1", "c1").unwrap();
    db.insert_edge("LIVES_IN", "p2", "c1").unwrap();

    db.create_view(neighbor_agg_view(
        "city_avg",
        "City",
        "score_avg",
        "LIVES_IN",
        Direction::In,
        AggFn::Avg,
        "score",
    ))
    .unwrap();
    // avg = (4+6)/2 = 5
    assert_eq!(db.get_prop("c1", "score_avg"), Some(Value::Float(5.0)));

    // Change p1's score — avg should update.
    db.set_prop("p1", "score", Value::Float(10.0)).unwrap();
    // avg = (10+6)/2 = 8
    assert_eq!(db.get_prop("c1", "score_avg"), Some(Value::Float(8.0)));

    // Remove p1's score entirely — only p2 contributes.
    db.remove_prop("p1", "score").unwrap();
    // avg = 6/1 = 6
    assert_eq!(db.get_prop("c1", "score_avg"), Some(Value::Float(6.0)));
}

#[test]
fn min_retraction_recomputes_correctly() {
    let dir = tmp("min");
    let mut db = GraphDb::open(&dir).unwrap();

    db.insert_node("City", "c1", vec![]).unwrap();
    db.insert_node("Person", "p1", vec![("age".into(), Value::Float(20.0))])
        .unwrap();
    db.insert_node("Person", "p2", vec![("age".into(), Value::Float(30.0))])
        .unwrap();
    db.insert_node("Person", "p3", vec![("age".into(), Value::Float(25.0))])
        .unwrap();
    db.insert_edge("LIVES_IN", "p1", "c1").unwrap();
    db.insert_edge("LIVES_IN", "p2", "c1").unwrap();
    db.insert_edge("LIVES_IN", "p3", "c1").unwrap();

    db.create_view(neighbor_agg_view(
        "city_min_age",
        "City",
        "min_age",
        "LIVES_IN",
        Direction::In,
        AggFn::Min,
        "age",
    ))
    .unwrap();
    // min = 20.0 (p1)
    assert_eq!(db.get_prop("c1", "min_age"), Some(Value::Float(20.0)));

    // Delete p1 (the min holder) → should recompute from remaining: min of 30, 25 = 25.
    db.delete_node("p1").unwrap();
    // O(degree) recompute triggered.
    assert_eq!(db.get_prop("c1", "min_age"), Some(Value::Float(25.0)));
}

// ---------------------------------------------------------------------------
// Derived edges (rules)
// ---------------------------------------------------------------------------

#[test]
fn degree_view_over_derived_edges_fire_and_retract() {
    let dir = tmp("derived");
    let mut db = GraphDb::open(&dir).unwrap();

    // Rule: Person with org_id → WORKS_AT → Org
    db.insert_node("Org", "o1", vec![]).unwrap();
    let rule = RuleDef {
        name: "works_at".into(),
        src_label: "Person".into(),
        dst_label: "Org".into(),
        predicate: Predicate::KeyMatch {
            field: "org_id".into(),
        },
        edge_type: "WORKS_AT".into(),
        weight_prop: None,
        max_edges: None,
        approximate: false,
        via_label: None,
        via_edge: None,
        via_dir: None,
        namespace: None,
    };
    db.create_rule(rule).unwrap();

    // Degree view on Org for how many people work there.
    db.create_view(degree_view(
        "org_headcount",
        "Org",
        "headcount",
        "WORKS_AT",
        Direction::In,
    ))
    .unwrap();
    assert_eq!(db.get_prop("o1", "headcount"), Some(Value::Int(0)));

    // Insert person → rule fires → derived WORKS_AT edge → headcount ++
    db.insert_node(
        "Person",
        "alice",
        vec![("org_id".into(), Value::Str("o1".into()))],
    )
    .unwrap();
    assert_eq!(db.get_prop("o1", "headcount"), Some(Value::Int(1)));

    // Delete person → rule retracts → headcount --
    db.delete_node("alice").unwrap();
    assert_eq!(db.get_prop("o1", "headcount"), Some(Value::Int(0)));
}

// ---------------------------------------------------------------------------
// Cypher integration
// ---------------------------------------------------------------------------

#[test]
fn view_prop_in_cypher_where_and_group() {
    use std::collections::BTreeMap;

    let dir = tmp("cypher");
    let mut db = GraphDb::open(&dir).unwrap();

    db.insert_node("City", "nyc", vec![]).unwrap();
    db.insert_node("City", "la", vec![]).unwrap();
    db.insert_node("Person", "p1", vec![]).unwrap();
    db.insert_node("Person", "p2", vec![]).unwrap();
    db.insert_node("Person", "p3", vec![]).unwrap();

    db.insert_edge("LIVES_IN", "p1", "nyc").unwrap();
    db.insert_edge("LIVES_IN", "p2", "nyc").unwrap();
    db.insert_edge("LIVES_IN", "p3", "la").unwrap();

    db.create_view(degree_view(
        "city_pop",
        "City",
        "pop",
        "LIVES_IN",
        Direction::In,
    ))
    .unwrap();

    // Filter: only cities with pop >= 2
    let results = db
        .query(
            "MATCH (c:City) WHERE c.pop >= 2 RETURN c.name",
            &BTreeMap::new(),
        )
        .unwrap();
    // "nyc" has pop=2; "la" has pop=1 — only nyc qualifies.
    // The query returns c.name but our nodes don't have a name prop set,
    // so we just check that exactly 1 row comes back.
    assert_eq!(results.len(), 1, "only nyc has pop >= 2");

    // Grouped aggregation: SUM(pop) GROUP BY label
    let results2 = db
        .query(
            "MATCH (c:City) RETURN SUM(c.pop) AS total",
            &BTreeMap::new(),
        )
        .unwrap();
    assert_eq!(results2.len(), 1);
    // total = 2 + 1 = 3
    // total = 2 + 1 = 3  (SUM of pop Int values; query engine may return Int or Float)
    let total = results2.get(0, "total").cloned();
    let total_f = match total {
        Some(Value::Int(n)) => n as f64,
        Some(Value::Float(f)) => f,
        other => panic!("unexpected total: {other:?}"),
    };
    assert!(
        (total_f - 3.0).abs() < 1e-10,
        "expected total=3, got {total_f}"
    );
}

// ---------------------------------------------------------------------------
// Write guard
// ---------------------------------------------------------------------------

#[test]
fn set_on_view_prop_returns_error() {
    let dir = tmp("guard");
    let mut db = GraphDb::open(&dir).unwrap();
    db.insert_node("City", "c1", vec![]).unwrap();
    db.create_view(degree_view(
        "city_pop",
        "City",
        "pop",
        "LIVES_IN",
        Direction::In,
    ))
    .unwrap();

    let err = db.set_prop("c1", "pop", Value::Int(999)).unwrap_err();
    match err {
        GraphError::ViewPropReadOnly { view_name } => {
            assert_eq!(view_name, "city_pop");
        }
        other => panic!("expected ViewPropReadOnly, got {other:?}"),
    }
}

#[test]
fn remove_on_view_prop_returns_error() {
    let dir = tmp("guard_remove");
    let mut db = GraphDb::open(&dir).unwrap();
    db.insert_node("City", "c1", vec![]).unwrap();
    db.create_view(degree_view(
        "city_pop",
        "City",
        "pop",
        "LIVES_IN",
        Direction::In,
    ))
    .unwrap();

    let err = db.remove_prop("c1", "pop").unwrap_err();
    assert!(matches!(err, GraphError::ViewPropReadOnly { .. }));
}

// ---------------------------------------------------------------------------
// Reopen rebuild identity
// ---------------------------------------------------------------------------

#[test]
fn reopen_rebuild_matches_live() {
    let dir = tmp("reopen");
    let vals_live: Vec<_> = {
        let mut db = GraphDb::open(&dir).unwrap();
        db.insert_node("City", "c1", vec![]).unwrap();
        db.insert_node("City", "c2", vec![]).unwrap();
        db.insert_node("Person", "p1", vec![("score".into(), Value::Float(5.0))])
            .unwrap();
        db.insert_node("Person", "p2", vec![("score".into(), Value::Float(3.0))])
            .unwrap();
        db.insert_edge("LIVES_IN", "p1", "c1").unwrap();
        db.insert_edge("LIVES_IN", "p2", "c1").unwrap();
        db.create_view(degree_view(
            "city_pop",
            "City",
            "pop",
            "LIVES_IN",
            Direction::In,
        ))
        .unwrap();
        db.create_view(neighbor_agg_view(
            "city_sum",
            "City",
            "score_sum",
            "LIVES_IN",
            Direction::In,
            AggFn::Sum,
            "score",
        ))
        .unwrap();
        vec![
            db.get_prop("c1", "pop"),
            db.get_prop("c2", "pop"),
            db.get_prop("c1", "score_sum"),
        ]
    };

    // Reopen and check values are identical.
    let db2 = GraphDb::open(&dir).unwrap();
    let vals_reopen = vec![
        db2.get_prop("c1", "pop"),
        db2.get_prop("c2", "pop"),
        db2.get_prop("c1", "score_sum"),
    ];

    assert_eq!(
        vals_live, vals_reopen,
        "reopen must rebuild identical values"
    );
}

// ---------------------------------------------------------------------------
// delete_view cleanup
// ---------------------------------------------------------------------------

#[test]
fn delete_view_removes_values() {
    let dir = tmp("del");
    let mut db = GraphDb::open(&dir).unwrap();
    db.insert_node("City", "c1", vec![]).unwrap();
    db.insert_node("Person", "p1", vec![]).unwrap();
    db.insert_edge("LIVES_IN", "p1", "c1").unwrap();
    db.create_view(degree_view(
        "city_pop",
        "City",
        "pop",
        "LIVES_IN",
        Direction::In,
    ))
    .unwrap();

    assert!(db.get_prop("c1", "pop").is_some());

    db.delete_view("city_pop").unwrap();
    assert!(
        db.get_prop("c1", "pop").is_none(),
        "view values removed after delete"
    );

    // View prop is now writable again.
    db.set_prop("c1", "pop", Value::Int(99)).unwrap();
    assert_eq!(db.get_prop("c1", "pop"), Some(Value::Int(99)));
}

// ---------------------------------------------------------------------------
// DST oracle: quiescent value == scratch recompute
// ---------------------------------------------------------------------------

#[test]
fn dst_oracle_degree_matches_scratch() {
    let dir = tmp("oracle");
    let mut db = GraphDb::open(&dir).unwrap();
    db.insert_node("City", "c1", vec![]).unwrap();
    db.insert_node("Person", "p1", vec![]).unwrap();
    db.insert_node("Person", "p2", vec![]).unwrap();
    db.insert_node("Person", "p3", vec![]).unwrap();
    db.insert_edge("LIVES_IN", "p1", "c1").unwrap();
    db.insert_edge("LIVES_IN", "p2", "c1").unwrap();
    db.insert_edge("LIVES_IN", "p3", "c1").unwrap();

    db.create_view(degree_view(
        "city_pop",
        "City",
        "pop",
        "LIVES_IN",
        Direction::In,
    ))
    .unwrap();

    let live = db.get_prop("c1", "pop");
    let scratch = db.scratch_view_value("c1", "city_pop");
    assert_eq!(live, scratch, "live value must equal scratch recompute");

    // Delete one edge and re-check.
    db.delete_edge("LIVES_IN", "p2", "c1").unwrap();
    let live2 = db.get_prop("c1", "pop");
    let scratch2 = db.scratch_view_value("c1", "city_pop");
    assert_eq!(
        live2, scratch2,
        "live value after edge delete must equal scratch"
    );
}

#[test]
fn dst_oracle_neighbor_sum_matches_scratch() {
    let dir = tmp("oracle_sum");
    let mut db = GraphDb::open(&dir).unwrap();
    db.insert_node("City", "c1", vec![]).unwrap();
    db.insert_node("Person", "p1", vec![("score".into(), Value::Float(10.0))])
        .unwrap();
    db.insert_node("Person", "p2", vec![("score".into(), Value::Float(20.0))])
        .unwrap();
    db.insert_edge("LIVES_IN", "p1", "c1").unwrap();
    db.insert_edge("LIVES_IN", "p2", "c1").unwrap();

    db.create_view(neighbor_agg_view(
        "city_sum",
        "City",
        "score_sum",
        "LIVES_IN",
        Direction::In,
        AggFn::Sum,
        "score",
    ))
    .unwrap();

    let live = db.get_prop("c1", "score_sum");
    let scratch = db.scratch_view_value("c1", "city_sum");
    assert_eq!(live, scratch);

    // Change a prop and check again.
    db.set_prop("p1", "score", Value::Float(50.0)).unwrap();
    let live2 = db.get_prop("c1", "score_sum");
    let scratch2 = db.scratch_view_value("c1", "city_sum");
    assert_eq!(live2, scratch2);
}

// ---------------------------------------------------------------------------
// Snapshot round-trip
// ---------------------------------------------------------------------------

#[test]
fn snapshot_preserves_views_and_values_rebuild() {
    let dir = tmp("snap");
    let pre_snap: Vec<_> = {
        let mut db = GraphDb::open(&dir).unwrap();
        db.insert_node("City", "c1", vec![]).unwrap();
        db.insert_node("Person", "p1", vec![("score".into(), Value::Float(7.0))])
            .unwrap();
        db.insert_edge("LIVES_IN", "p1", "c1").unwrap();
        db.create_view(degree_view(
            "city_pop",
            "City",
            "pop",
            "LIVES_IN",
            Direction::In,
        ))
        .unwrap();
        db.create_view(neighbor_agg_view(
            "city_sum",
            "City",
            "score_sum",
            "LIVES_IN",
            Direction::In,
            AggFn::Sum,
            "score",
        ))
        .unwrap();
        let vals = vec![db.get_prop("c1", "pop"), db.get_prop("c1", "score_sum")];
        db.snapshot().unwrap();
        vals
    };

    // Reopen after snapshot — views must still be present and values correct.
    let mut db2 = GraphDb::open(&dir).unwrap();
    let post_snap = vec![db2.get_prop("c1", "pop"), db2.get_prop("c1", "score_sum")];
    assert_eq!(pre_snap, post_snap, "values match after snapshot+reopen");

    // Views still operational after reopen.
    db2.insert_node("Person", "p2", vec![("score".into(), Value::Float(3.0))])
        .unwrap();
    db2.insert_edge("LIVES_IN", "p2", "c1").unwrap();
    assert_eq!(db2.get_prop("c1", "pop"), Some(Value::Int(2)));
    assert_eq!(db2.get_prop("c1", "score_sum"), Some(Value::Float(10.0)));
}

// ---------------------------------------------------------------------------
// Collision / error cases
// ---------------------------------------------------------------------------

#[test]
fn duplicate_view_name_rejected() {
    let dir = tmp("dup");
    let mut db = GraphDb::open(&dir).unwrap();
    db.create_view(degree_view("v", "City", "pop", "LIVES_IN", Direction::In))
        .unwrap();
    let err = db
        .create_view(degree_view("v", "City", "pop2", "LIVES_IN", Direction::In))
        .unwrap_err();
    assert!(matches!(err, GraphError::RuleInvalid { .. }));
}

#[test]
fn view_prop_collision_with_existing_view_rejected() {
    let dir = tmp("dup_prop");
    let mut db = GraphDb::open(&dir).unwrap();
    db.create_view(degree_view("v1", "City", "pop", "LIVES_IN", Direction::In))
        .unwrap();
    let err = db
        .create_view(degree_view("v2", "City", "pop", "LIVES_IN", Direction::Out))
        .unwrap_err();
    assert!(matches!(err, GraphError::RuleInvalid { .. }));
}

#[test]
fn delete_view_unknown_returns_not_found() {
    let dir = tmp("del_unk");
    let mut db = GraphDb::open(&dir).unwrap();
    let err = db.delete_view("no_such_view").unwrap_err();
    assert!(matches!(err, GraphError::RuleNotFound { .. }));
}

// ---------------------------------------------------------------------------
// pending_deltas discipline: view updates must not accumulate engine deltas
// ---------------------------------------------------------------------------

#[test]
fn pending_deltas_are_clean_through_view_heavy_workload() {
    // This test exercises create_view, insert_node (rule fire), delete_node
    // and verifies that no stale deltas are left after each commit.
    // The debug_assert in log_then_apply_with catches violations at runtime;
    // this test exists to document the invariant and exercise the path.
    let dir = tmp("deltas");
    let mut db = GraphDb::open(&dir).unwrap();

    // Rule: WORKS_AT
    let rule = RuleDef {
        name: "works_at".into(),
        src_label: "Person".into(),
        dst_label: "Org".into(),
        predicate: Predicate::KeyMatch {
            field: "org_id".into(),
        },
        edge_type: "WORKS_AT".into(),
        weight_prop: None,
        max_edges: None,
        approximate: false,
        via_label: None,
        via_edge: None,
        via_dir: None,
        namespace: None,
    };
    db.insert_node("Org", "o1", vec![]).unwrap();
    db.create_rule(rule).unwrap();
    db.create_view(degree_view(
        "org_headcount",
        "Org",
        "headcount",
        "WORKS_AT",
        Direction::In,
    ))
    .unwrap();

    // Multiple person inserts → rule fires → view updates.
    for i in 0..10u32 {
        db.insert_node(
            "Person",
            &format!("p{i}"),
            vec![("org_id".into(), Value::Str("o1".into()))],
        )
        .unwrap();
    }
    assert_eq!(db.get_prop("o1", "headcount"), Some(Value::Int(10)));

    // Delete all persons → rule retracts → view decrements.
    for i in 0..10u32 {
        db.delete_node(&format!("p{i}")).unwrap();
    }
    assert_eq!(db.get_prop("o1", "headcount"), Some(Value::Int(0)));
}