grafeo-engine 0.5.41

Query engine and database management for Grafeo
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
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
//! Targeted tests for the MVCC versioning layer (undo log, property rollback,
//! label rollback, deletion rollback).
//!
//! These tests exercise edge cases and interleaved operations that the existing
//! test files do not cover, focusing on real-world transaction bugs.

use grafeo_common::types::Value;
use grafeo_engine::GrafeoDB;

// ============================================================================
// 1. Edge property rollback
// ============================================================================

/// SET an edge property in a transaction, then rollback.
/// Verify the property is removed (it did not exist before the tx).
#[test]
fn edge_property_set_then_rollback_removes_property() {
    let db = GrafeoDB::new_in_memory();
    let mut session = db.session();

    session
        .execute("INSERT (:Person {name: 'Alix'})-[:KNOWS]->(:Person {name: 'Gus'})")
        .unwrap();

    // Edge has no properties yet
    let before = session
        .execute("MATCH (:Person {name: 'Alix'})-[r:KNOWS]->() RETURN r.since")
        .unwrap();
    assert_eq!(before.rows()[0][0], Value::Null);

    session.begin_transaction().unwrap();
    session
        .execute(
            "MATCH (:Person {name: 'Alix'})-[r:KNOWS]->(:Person {name: 'Gus'}) SET r.since = 2020",
        )
        .unwrap();

    // Visible inside tx
    let during = session
        .execute("MATCH (:Person {name: 'Alix'})-[r:KNOWS]->() RETURN r.since")
        .unwrap();
    assert_eq!(during.rows()[0][0], Value::Int64(2020));

    session.rollback().unwrap();

    let after = session
        .execute("MATCH (:Person {name: 'Alix'})-[r:KNOWS]->() RETURN r.since")
        .unwrap();
    assert_eq!(
        after.rows()[0][0],
        Value::Null,
        "edge property should be gone after rollback"
    );
}

/// Overwrite an existing edge property in a transaction, then rollback.
/// The original value should be restored.
#[test]
fn edge_property_overwrite_then_rollback_restores_original() {
    let db = GrafeoDB::new_in_memory();
    let mut session = db.session();

    session
        .execute(
            "INSERT (:Person {name: 'Vincent'})-[:KNOWS {since: 2018}]->(:Person {name: 'Jules'})",
        )
        .unwrap();

    session.begin_transaction().unwrap();
    session
        .execute(
            "MATCH (:Person {name: 'Vincent'})-[r:KNOWS]->(:Person {name: 'Jules'}) SET r.since = 2025",
        )
        .unwrap();
    session.rollback().unwrap();

    let result = session
        .execute("MATCH (:Person {name: 'Vincent'})-[r:KNOWS]->() RETURN r.since")
        .unwrap();
    assert_eq!(
        result.rows()[0][0],
        Value::Int64(2018),
        "edge property should revert to 2018 after rollback"
    );
}

/// SET multiple edge properties in one tx, rollback, verify all reverted.
#[test]
fn edge_multiple_properties_rollback() {
    let db = GrafeoDB::new_in_memory();
    let mut session = db.session();

    session
        .execute(
            "INSERT (:City {name: 'Amsterdam'})-[:ROUTE {distance: 650, toll: 15}]->(:City {name: 'Berlin'})",
        )
        .unwrap();

    session.begin_transaction().unwrap();
    session
        .execute(
            "MATCH (:City {name: 'Amsterdam'})-[r:ROUTE]->(:City {name: 'Berlin'}) SET r.distance = 999, r.toll = 0",
        )
        .unwrap();
    session.rollback().unwrap();

    let result = session
        .execute("MATCH (:City {name: 'Amsterdam'})-[r:ROUTE]->() RETURN r.distance, r.toll")
        .unwrap();
    assert_eq!(
        result.rows()[0][0],
        Value::Int64(650),
        "distance should revert to 650"
    );
    assert_eq!(
        result.rows()[0][1],
        Value::Int64(15),
        "toll should revert to 15"
    );
}

// ============================================================================
// 2. REMOVE property rollback
// ============================================================================

/// REMOVE a node property in a tx, rollback, verify property is restored.
#[test]
fn remove_node_property_rollback_restores_value() {
    let db = GrafeoDB::new_in_memory();
    let mut session = db.session();

    session
        .execute("INSERT (:Person {name: 'Mia', age: 28})")
        .unwrap();

    session.begin_transaction().unwrap();
    session
        .execute("MATCH (p:Person {name: 'Mia'}) REMOVE p.age")
        .unwrap();

    // Property gone inside tx
    let during = session
        .execute("MATCH (p:Person {name: 'Mia'}) RETURN p.age")
        .unwrap();
    assert_eq!(during.rows()[0][0], Value::Null);

    session.rollback().unwrap();

    let after = session
        .execute("MATCH (p:Person {name: 'Mia'}) RETURN p.age")
        .unwrap();
    assert_eq!(
        after.rows()[0][0],
        Value::Int64(28),
        "age should be restored to 28 after rollback of REMOVE"
    );
}

/// SET then REMOVE same property in one tx, rollback.
/// Original value should be restored (not null, not the SET value).
#[test]
fn set_then_remove_same_property_rollback() {
    let db = GrafeoDB::new_in_memory();
    let mut session = db.session();

    session
        .execute("INSERT (:Config {key: 'timeout', value: 30})")
        .unwrap();

    session.begin_transaction().unwrap();
    session
        .execute("MATCH (c:Config {key: 'timeout'}) SET c.value = 60")
        .unwrap();
    session
        .execute("MATCH (c:Config {key: 'timeout'}) REMOVE c.value")
        .unwrap();
    session.rollback().unwrap();

    let result = session
        .execute("MATCH (c:Config {key: 'timeout'}) RETURN c.value")
        .unwrap();
    assert_eq!(
        result.rows()[0][0],
        Value::Int64(30),
        "value should revert to original 30, not null or 60"
    );
}

// ============================================================================
// 3. Multiple label operations rollback
// ============================================================================

/// Add multiple labels in a tx, rollback, verify all removed.
#[test]
fn add_multiple_labels_rollback_removes_all() {
    let db = GrafeoDB::new_in_memory();
    let mut session = db.session();

    session.execute("INSERT (:Person {name: 'Butch'})").unwrap();

    session.begin_transaction().unwrap();
    session
        .execute("MATCH (p:Person {name: 'Butch'}) SET p:Employee")
        .unwrap();
    session
        .execute("MATCH (p:Person {name: 'Butch'}) SET p:Manager")
        .unwrap();
    session.rollback().unwrap();

    let employee = session
        .execute("MATCH (p:Employee {name: 'Butch'}) RETURN p.name")
        .unwrap();
    assert_eq!(
        employee.row_count(),
        0,
        "Employee label should not exist after rollback"
    );

    let manager = session
        .execute("MATCH (p:Manager {name: 'Butch'}) RETURN p.name")
        .unwrap();
    assert_eq!(
        manager.row_count(),
        0,
        "Manager label should not exist after rollback"
    );

    // Original label should remain
    let person = session
        .execute("MATCH (p:Person {name: 'Butch'}) RETURN p.name")
        .unwrap();
    assert_eq!(
        person.row_count(),
        1,
        "Person label should survive rollback"
    );
}

/// Add a label, then remove it, in the same tx, then rollback.
/// The label should be back in its original state (present if it was present before).
#[test]
fn add_then_remove_label_same_tx_rollback() {
    let db = GrafeoDB::new_in_memory();
    let mut session = db.session();

    session
        .execute("INSERT (:Person {name: 'Django'})")
        .unwrap();

    session.begin_transaction().unwrap();
    session
        .execute("MATCH (p:Person {name: 'Django'}) SET p:Gunslinger")
        .unwrap();
    session
        .execute("MATCH (p:Person {name: 'Django'}) REMOVE p:Gunslinger")
        .unwrap();
    session.rollback().unwrap();

    // Gunslinger was added then removed within tx; rollback should undo both
    // Since node never had Gunslinger before the tx, it should not have it now
    let result = session
        .execute("MATCH (p:Gunslinger {name: 'Django'}) RETURN p.name")
        .unwrap();
    assert_eq!(
        result.row_count(),
        0,
        "Gunslinger label should not exist (was never there before tx)"
    );
}

/// Remove a pre-existing label and add a new one, rollback.
/// Pre-existing label restored, new label removed.
#[test]
fn swap_labels_rollback_restores_original() {
    let db = GrafeoDB::new_in_memory();
    let mut session = db.session();

    session
        .execute("INSERT (:Active {name: 'Shosanna'})")
        .unwrap();

    session.begin_transaction().unwrap();
    session
        .execute("MATCH (n:Active {name: 'Shosanna'}) REMOVE n:Active")
        .unwrap();
    session
        .execute("MATCH (n {name: 'Shosanna'}) SET n:Archived")
        .unwrap();
    session.rollback().unwrap();

    let active = session
        .execute("MATCH (n:Active {name: 'Shosanna'}) RETURN n.name")
        .unwrap();
    assert_eq!(active.row_count(), 1, "Active label should be restored");

    let archived = session
        .execute("MATCH (n:Archived {name: 'Shosanna'}) RETURN n.name")
        .unwrap();
    assert_eq!(
        archived.row_count(),
        0,
        "Archived label should not exist after rollback"
    );
}

// ============================================================================
// 4. Node deletion rollback: verify full restoration
// ============================================================================

/// Delete a node with multiple properties and labels, rollback.
/// Verify all properties and labels are restored.
#[test]
fn node_delete_rollback_restores_all_properties_and_labels() {
    let db = GrafeoDB::new_in_memory();
    let mut session = db.session();

    session
        .execute("INSERT (:Person:VIP {name: 'Hans', age: 55, city: 'Berlin'})")
        .unwrap();

    session.begin_transaction().unwrap();
    session
        .execute("MATCH (p:Person {name: 'Hans'}) DELETE p")
        .unwrap();
    session.rollback().unwrap();

    // All properties restored
    let result = session
        .execute("MATCH (p:Person {name: 'Hans'}) RETURN p.age, p.city")
        .unwrap();
    assert_eq!(result.row_count(), 1, "node should exist after rollback");
    assert_eq!(
        result.rows()[0][0],
        Value::Int64(55),
        "age should be restored"
    );
    assert_eq!(
        result.rows()[0][1],
        Value::String("Berlin".into()),
        "city should be restored"
    );

    // VIP label restored
    let vip = session
        .execute("MATCH (p:VIP {name: 'Hans'}) RETURN p.name")
        .unwrap();
    assert_eq!(
        vip.row_count(),
        1,
        "VIP label should be restored after rollback"
    );
}

// ============================================================================
// 5. Edge deletion rollback: verify properties restored
// ============================================================================

/// Delete an edge that has properties, rollback, verify edge and properties restored.
#[test]
fn edge_delete_rollback_restores_properties() {
    let db = GrafeoDB::new_in_memory();
    let mut session = db.session();

    session
        .execute(
            "INSERT (:Person {name: 'Beatrix'})-[:KNOWS {since: 2015, trust: 9}]->(:Person {name: 'Alix'})",
        )
        .unwrap();

    session.begin_transaction().unwrap();
    session
        .execute("MATCH (:Person {name: 'Beatrix'})-[r:KNOWS]->(:Person {name: 'Alix'}) DELETE r")
        .unwrap();

    // Edge gone inside tx
    let during = session
        .execute("MATCH (:Person {name: 'Beatrix'})-[r:KNOWS]->() RETURN r.since")
        .unwrap();
    assert_eq!(during.row_count(), 0);

    session.rollback().unwrap();

    let after = session
        .execute("MATCH (:Person {name: 'Beatrix'})-[r:KNOWS]->() RETURN r.since, r.trust")
        .unwrap();
    assert_eq!(after.row_count(), 1, "edge should be restored");
    assert_eq!(
        after.rows()[0][0],
        Value::Int64(2015),
        "edge 'since' property should be restored"
    );
    assert_eq!(
        after.rows()[0][1],
        Value::Int64(9),
        "edge 'trust' property should be restored"
    );
}

/// DETACH DELETE a node with multiple edges that have properties,
/// rollback, verify everything restored.
#[test]
fn detach_delete_rollback_restores_node_edges_and_properties() {
    let db = GrafeoDB::new_in_memory();
    let mut session = db.session();

    // Create a hub node with two outgoing edges
    session
        .execute("INSERT (:Person {name: 'Vincent', age: 40})")
        .unwrap();
    session.execute("INSERT (:Person {name: 'Jules'})").unwrap();
    session.execute("INSERT (:Person {name: 'Mia'})").unwrap();
    session
        .execute(
            "MATCH (v:Person {name: 'Vincent'}), (j:Person {name: 'Jules'}) \
             INSERT (v)-[:KNOWS {since: 2010}]->(j)",
        )
        .unwrap();
    session
        .execute(
            "MATCH (v:Person {name: 'Vincent'}), (m:Person {name: 'Mia'}) \
             INSERT (v)-[:LIKES {intensity: 8}]->(m)",
        )
        .unwrap();

    session.begin_transaction().unwrap();
    session
        .execute("MATCH (v:Person {name: 'Vincent'}) DETACH DELETE v")
        .unwrap();

    // Node and edges gone inside tx
    let during_nodes = session
        .execute("MATCH (v:Person {name: 'Vincent'}) RETURN v.name")
        .unwrap();
    assert_eq!(during_nodes.row_count(), 0);

    session.rollback().unwrap();

    // Node restored with property
    let node = session
        .execute("MATCH (v:Person {name: 'Vincent'}) RETURN v.age")
        .unwrap();
    assert_eq!(node.row_count(), 1, "node should be restored");
    assert_eq!(node.rows()[0][0], Value::Int64(40));

    // KNOWS edge restored with property
    let knows = session
        .execute("MATCH (:Person {name: 'Vincent'})-[r:KNOWS]->() RETURN r.since")
        .unwrap();
    assert_eq!(knows.row_count(), 1, "KNOWS edge should be restored");
    assert_eq!(knows.rows()[0][0], Value::Int64(2010));

    // LIKES edge restored with property
    let likes = session
        .execute("MATCH (:Person {name: 'Vincent'})-[r:LIKES]->() RETURN r.intensity")
        .unwrap();
    assert_eq!(likes.row_count(), 1, "LIKES edge should be restored");
    assert_eq!(likes.rows()[0][0], Value::Int64(8));
}

// ============================================================================
// 6. Interleaved operations: mix property sets, label changes, and deletions
// ============================================================================

/// In one transaction: set properties, add labels, delete a different node,
/// then rollback. Verify complete undo.
#[test]
fn interleaved_property_label_and_delete_rollback() {
    let db = GrafeoDB::new_in_memory();
    let mut session = db.session();

    session
        .execute("INSERT (:Person {name: 'Alix', age: 30})")
        .unwrap();
    session
        .execute("INSERT (:Temporary {name: 'ephemeral', data: 42})")
        .unwrap();

    session.begin_transaction().unwrap();

    // Modify Alix's property
    session
        .execute("MATCH (p:Person {name: 'Alix'}) SET p.age = 99")
        .unwrap();

    // Add label to Alix
    session
        .execute("MATCH (p:Person {name: 'Alix'}) SET p:VIP")
        .unwrap();

    // Delete the Temporary node
    session
        .execute("MATCH (t:Temporary {name: 'ephemeral'}) DELETE t")
        .unwrap();

    session.rollback().unwrap();

    // Alix's age reverted
    let alix = session
        .execute("MATCH (p:Person {name: 'Alix'}) RETURN p.age")
        .unwrap();
    assert_eq!(
        alix.rows()[0][0],
        Value::Int64(30),
        "age should revert to 30"
    );

    // Alix's VIP label removed
    let vip = session
        .execute("MATCH (p:VIP {name: 'Alix'}) RETURN p.name")
        .unwrap();
    assert_eq!(
        vip.row_count(),
        0,
        "VIP label should not exist after rollback"
    );

    // Temporary node restored
    let temp = session
        .execute("MATCH (t:Temporary {name: 'ephemeral'}) RETURN t.data")
        .unwrap();
    assert_eq!(temp.row_count(), 1, "Temporary node should be restored");
    assert_eq!(temp.rows()[0][0], Value::Int64(42));
}

/// Property set, label add, edge property set, node delete, all in one tx.
#[test]
fn kitchen_sink_operations_rollback() {
    let db = GrafeoDB::new_in_memory();
    let mut session = db.session();

    session
        .execute("INSERT (:Person {name: 'Gus', score: 10})-[:WORKS_WITH {years: 3}]->(:Person {name: 'Alix'})")
        .unwrap();

    session.begin_transaction().unwrap();

    // Modify node property
    session
        .execute("MATCH (p:Person {name: 'Gus'}) SET p.score = 100")
        .unwrap();

    // Add label
    session
        .execute("MATCH (p:Person {name: 'Gus'}) SET p:Senior")
        .unwrap();

    // Modify edge property
    session
        .execute(
            "MATCH (:Person {name: 'Gus'})-[r:WORKS_WITH]->(:Person {name: 'Alix'}) SET r.years = 10",
        )
        .unwrap();

    session.rollback().unwrap();

    // Node property reverted
    let score = session
        .execute("MATCH (p:Person {name: 'Gus'}) RETURN p.score")
        .unwrap();
    assert_eq!(
        score.rows()[0][0],
        Value::Int64(10),
        "score should revert to 10"
    );

    // Label removed
    let senior = session
        .execute("MATCH (p:Senior {name: 'Gus'}) RETURN p.name")
        .unwrap();
    assert_eq!(
        senior.row_count(),
        0,
        "Senior label should not exist after rollback"
    );

    // Edge property reverted
    let years = session
        .execute("MATCH (:Person {name: 'Gus'})-[r:WORKS_WITH]->() RETURN r.years")
        .unwrap();
    assert_eq!(
        years.rows()[0][0],
        Value::Int64(3),
        "edge years should revert to 3"
    );
}

// ============================================================================
// 7. Concurrent transactions: two sessions, one rolls back
// ============================================================================

/// Session 1 commits property changes, then session 2 modifies a different
/// node and rolls back. The committed changes from session 1 should not be
/// affected by session 2's rollback.
#[test]
fn sequential_commit_then_rollback_different_nodes() {
    let db = GrafeoDB::new_in_memory();

    let setup = db.session();
    setup
        .execute("INSERT (:Account {owner: 'Alix', balance: 100})")
        .unwrap();
    setup
        .execute("INSERT (:Account {owner: 'Gus', balance: 200})")
        .unwrap();

    // Session 1: modifies Alix, commits
    let mut session1 = db.session();
    session1.begin_transaction().unwrap();
    session1
        .execute("MATCH (a:Account {owner: 'Alix'}) SET a.balance = 500")
        .unwrap();
    session1.commit().unwrap();

    // Session 2: modifies Gus, rolls back
    let mut session2 = db.session();
    session2.begin_transaction().unwrap();
    session2
        .execute("MATCH (a:Account {owner: 'Gus'}) SET a.balance = 9999")
        .unwrap();
    session2.rollback().unwrap();

    // Verify with a fresh session
    let reader = db.session();

    let alix = reader
        .execute("MATCH (a:Account {owner: 'Alix'}) RETURN a.balance")
        .unwrap();
    assert_eq!(
        alix.rows()[0][0],
        Value::Int64(500),
        "Alix's committed balance should be 500"
    );

    let gus = reader
        .execute("MATCH (a:Account {owner: 'Gus'}) RETURN a.balance")
        .unwrap();
    assert_eq!(
        gus.rows()[0][0],
        Value::Int64(200),
        "Gus's balance should remain 200 after session2's rollback"
    );
}

/// Session 1 commits a label add, then session 2 adds a different label
/// to a different node and rolls back. Committed label survives.
#[test]
fn sequential_label_commit_then_rollback_different_nodes() {
    let db = GrafeoDB::new_in_memory();

    let setup = db.session();
    setup.execute("INSERT (:Person {name: 'Vincent'})").unwrap();
    setup.execute("INSERT (:Person {name: 'Jules'})").unwrap();

    // Session 1: adds Hitman label to Vincent, commits
    let mut session1 = db.session();
    session1.begin_transaction().unwrap();
    session1
        .execute("MATCH (p:Person {name: 'Vincent'}) SET p:Hitman")
        .unwrap();
    session1.commit().unwrap();

    // Session 2: adds Philosopher label to Jules, rolls back
    let mut session2 = db.session();
    session2.begin_transaction().unwrap();
    session2
        .execute("MATCH (p:Person {name: 'Jules'}) SET p:Philosopher")
        .unwrap();
    session2.rollback().unwrap();

    let reader = db.session();

    let hitman = reader
        .execute("MATCH (p:Hitman {name: 'Vincent'}) RETURN p.name")
        .unwrap();
    assert_eq!(
        hitman.row_count(),
        1,
        "Vincent should have Hitman label (committed)"
    );

    let philosopher = reader
        .execute("MATCH (p:Philosopher {name: 'Jules'}) RETURN p.name")
        .unwrap();
    assert_eq!(
        philosopher.row_count(),
        0,
        "Jules should NOT have Philosopher label (rolled back)"
    );
}

// ============================================================================
// 8. Property overwrite multiple times in one tx
// ============================================================================

/// Overwrite the same property three times in one tx, rollback.
/// Original value should be restored, not any intermediate value.
#[test]
fn triple_overwrite_same_property_rollback_restores_original() {
    let db = GrafeoDB::new_in_memory();
    let mut session = db.session();

    session
        .execute("INSERT (:Counter {name: 'visits', count: 0})")
        .unwrap();

    session.begin_transaction().unwrap();
    session
        .execute("MATCH (c:Counter {name: 'visits'}) SET c.count = 1")
        .unwrap();
    session
        .execute("MATCH (c:Counter {name: 'visits'}) SET c.count = 2")
        .unwrap();
    session
        .execute("MATCH (c:Counter {name: 'visits'}) SET c.count = 3")
        .unwrap();
    session.rollback().unwrap();

    let result = session
        .execute("MATCH (c:Counter {name: 'visits'}) RETURN c.count")
        .unwrap();
    assert_eq!(
        result.rows()[0][0],
        Value::Int64(0),
        "count should be 0 (original), not 1, 2, or 3"
    );
}

// ============================================================================
// 9. Commit-then-rollback chain: labels
// ============================================================================

/// Commit a label add in tx1, then rollback a label add in tx2.
/// The tx1 label should persist, the tx2 label should not.
#[test]
fn commit_label_then_rollback_different_label() {
    let db = GrafeoDB::new_in_memory();
    let mut session = db.session();

    session.execute("INSERT (:Person {name: 'Alix'})").unwrap();

    // tx1: add Employee, commit
    session.begin_transaction().unwrap();
    session
        .execute("MATCH (p:Person {name: 'Alix'}) SET p:Employee")
        .unwrap();
    session.commit().unwrap();

    // tx2: add Manager, rollback
    session.begin_transaction().unwrap();
    session
        .execute("MATCH (p:Person {name: 'Alix'}) SET p:Manager")
        .unwrap();
    session.rollback().unwrap();

    let employee = session
        .execute("MATCH (p:Employee {name: 'Alix'}) RETURN p.name")
        .unwrap();
    assert_eq!(
        employee.row_count(),
        1,
        "Employee label should persist from committed tx"
    );

    let manager = session
        .execute("MATCH (p:Manager {name: 'Alix'}) RETURN p.name")
        .unwrap();
    assert_eq!(
        manager.row_count(),
        0,
        "Manager label should not exist (rolled back)"
    );
}

// ============================================================================
// 10. Edge property on newly-created edge within tx
// ============================================================================

/// Create an edge and set a property on it within the same tx, then rollback.
/// Both the edge and property should be gone.
#[test]
fn create_edge_and_set_property_in_tx_rollback() {
    let db = GrafeoDB::new_in_memory();
    let mut session = db.session();

    session.execute("INSERT (:Person {name: 'Alix'})").unwrap();
    session.execute("INSERT (:Person {name: 'Gus'})").unwrap();

    session.begin_transaction().unwrap();
    session
        .execute(
            "MATCH (a:Person {name: 'Alix'}), (g:Person {name: 'Gus'}) \
             INSERT (a)-[:FRIENDS {level: 5}]->(g)",
        )
        .unwrap();

    // Edge visible inside tx
    let during = session
        .execute("MATCH ()-[r:FRIENDS]->() RETURN r.level")
        .unwrap();
    assert_eq!(during.row_count(), 1);

    session.rollback().unwrap();

    let after = session
        .execute("MATCH ()-[r:FRIENDS]->() RETURN r.level")
        .unwrap();
    assert_eq!(
        after.row_count(),
        0,
        "edge created in rolled-back tx should not exist"
    );
}

// ============================================================================
// 11. Property type change rollback
// ============================================================================

/// Change a property from one type to another (int to string), rollback.
/// Original type and value should be restored.
#[test]
fn property_type_change_rollback_restores_original_type() {
    let db = GrafeoDB::new_in_memory();
    let mut session = db.session();

    session
        .execute("INSERT (:Setting {key: 'mode', value: 1})")
        .unwrap();

    session.begin_transaction().unwrap();
    session
        .execute("MATCH (s:Setting {key: 'mode'}) SET s.value = 'debug'")
        .unwrap();

    // Verify type changed inside tx
    let during = session
        .execute("MATCH (s:Setting {key: 'mode'}) RETURN s.value")
        .unwrap();
    assert_eq!(during.rows()[0][0], Value::String("debug".into()));

    session.rollback().unwrap();

    let after = session
        .execute("MATCH (s:Setting {key: 'mode'}) RETURN s.value")
        .unwrap();
    assert_eq!(
        after.rows()[0][0],
        Value::Int64(1),
        "value should revert to int 1, not string 'debug'"
    );
}

// ============================================================================
// 12. Empty transaction rollback is harmless
// ============================================================================

/// Begin and immediately rollback without any mutations. Database should be unchanged.
#[test]
fn empty_transaction_rollback_leaves_data_intact() {
    let db = GrafeoDB::new_in_memory();
    let mut session = db.session();

    session
        .execute("INSERT (:Person {name: 'Alix', age: 30})")
        .unwrap();

    session.begin_transaction().unwrap();
    // No mutations
    session.rollback().unwrap();

    let result = session
        .execute("MATCH (p:Person {name: 'Alix'}) RETURN p.age")
        .unwrap();
    assert_eq!(result.row_count(), 1);
    assert_eq!(result.rows()[0][0], Value::Int64(30));
}

// ============================================================================
// 13. Rollback after read-only operations
// ============================================================================

/// A tx that only reads (MATCH/RETURN) and then rolls back should not corrupt state.
#[test]
fn read_only_transaction_rollback() {
    let db = GrafeoDB::new_in_memory();
    let mut session = db.session();

    session
        .execute("INSERT (:Person {name: 'Gus', score: 42})")
        .unwrap();

    session.begin_transaction().unwrap();
    let _read = session
        .execute("MATCH (p:Person {name: 'Gus'}) RETURN p.score")
        .unwrap();
    session.rollback().unwrap();

    // Data intact
    let after = session
        .execute("MATCH (p:Person {name: 'Gus'}) RETURN p.score")
        .unwrap();
    assert_eq!(after.rows()[0][0], Value::Int64(42));
}

// ============================================================================
// 14. Multiple nodes with same property name, rollback only affects modified
// ============================================================================

/// Two nodes share the same property key. Modify only one in a tx, rollback.
/// The other should be completely untouched.
#[test]
fn rollback_only_affects_modified_node_shared_property_key() {
    let db = GrafeoDB::new_in_memory();
    let mut session = db.session();

    session
        .execute("INSERT (:Person {name: 'Alix', score: 10})")
        .unwrap();
    session
        .execute("INSERT (:Person {name: 'Gus', score: 20})")
        .unwrap();

    session.begin_transaction().unwrap();
    session
        .execute("MATCH (p:Person {name: 'Alix'}) SET p.score = 999")
        .unwrap();
    session.rollback().unwrap();

    let alix = session
        .execute("MATCH (p:Person {name: 'Alix'}) RETURN p.score")
        .unwrap();
    assert_eq!(
        alix.rows()[0][0],
        Value::Int64(10),
        "Alix's score should revert to 10"
    );

    let gus = session
        .execute("MATCH (p:Person {name: 'Gus'}) RETURN p.score")
        .unwrap();
    assert_eq!(
        gus.rows()[0][0],
        Value::Int64(20),
        "Gus's score should be unaffected (20)"
    );
}

// ============================================================================
// 15. Rollback after node creation: adjacency consistency
// ============================================================================

/// Create a node and edges in a tx, rollback.
/// Verify that adjacency index is clean: no phantom edges.
#[test]
fn create_node_with_edges_rollback_cleans_adjacency() {
    let db = GrafeoDB::new_in_memory();
    let mut session = db.session();

    session.execute("INSERT (:Person {name: 'Alix'})").unwrap();

    session.begin_transaction().unwrap();
    session.execute("INSERT (:Person {name: 'Ghost'})").unwrap();
    session
        .execute(
            "MATCH (a:Person {name: 'Alix'}), (g:Person {name: 'Ghost'}) \
             INSERT (a)-[:HAUNTS]->(g)",
        )
        .unwrap();
    session.rollback().unwrap();

    // Ghost node should not exist
    let ghost = session
        .execute("MATCH (g:Person {name: 'Ghost'}) RETURN g.name")
        .unwrap();
    assert_eq!(
        ghost.row_count(),
        0,
        "Ghost node should not exist after rollback"
    );

    // No HAUNTS edges should exist
    let edges = session.execute("MATCH ()-[r:HAUNTS]->() RETURN r").unwrap();
    assert_eq!(
        edges.row_count(),
        0,
        "HAUNTS edge should not exist after rollback"
    );

    // Alix should have no outgoing edges
    let alix_edges = session
        .execute("MATCH (:Person {name: 'Alix'})-[r]->() RETURN r")
        .unwrap();
    assert_eq!(
        alix_edges.row_count(),
        0,
        "Alix should have no outgoing edges after rollback"
    );
}

// ============================================================================
// 16. Commit followed by rollback, same property
// ============================================================================

/// Commit a property change, then in a new tx change the same property and rollback.
/// The committed value should be the final state.
#[test]
fn commit_then_rollback_same_property_retains_committed_value() {
    let db = GrafeoDB::new_in_memory();
    let mut session = db.session();

    session
        .execute("INSERT (:Person {name: 'Mia', level: 1})")
        .unwrap();

    // tx1: set level to 5, commit
    session.begin_transaction().unwrap();
    session
        .execute("MATCH (p:Person {name: 'Mia'}) SET p.level = 5")
        .unwrap();
    session.commit().unwrap();

    // tx2: set level to 99, rollback
    session.begin_transaction().unwrap();
    session
        .execute("MATCH (p:Person {name: 'Mia'}) SET p.level = 99")
        .unwrap();
    session.rollback().unwrap();

    let result = session
        .execute("MATCH (p:Person {name: 'Mia'}) RETURN p.level")
        .unwrap();
    assert_eq!(
        result.rows()[0][0],
        Value::Int64(5),
        "level should be 5 (committed value), not 99 (rolled back)"
    );
}

// ============================================================================
// 17. Boolean and float property rollback (different value types)
// ============================================================================

/// Test rollback with boolean and float property types.
#[test]
fn rollback_preserves_boolean_and_float_property_types() {
    let db = GrafeoDB::new_in_memory();
    let mut session = db.session();

    session
        .execute("INSERT (:Sensor {name: 'thermostat', enabled: true, reading: 21.5})")
        .unwrap();

    session.begin_transaction().unwrap();
    session
        .execute("MATCH (s:Sensor {name: 'thermostat'}) SET s.enabled = false, s.reading = 99.9")
        .unwrap();
    session.rollback().unwrap();

    let result = session
        .execute("MATCH (s:Sensor {name: 'thermostat'}) RETURN s.enabled, s.reading")
        .unwrap();
    assert_eq!(
        result.rows()[0][0],
        Value::Bool(true),
        "enabled should revert to true"
    );
    assert_eq!(
        result.rows()[0][1],
        Value::Float64(21.5),
        "reading should revert to 21.5"
    );
}

// ============================================================================
// 18. Rapid begin-rollback cycles
// ============================================================================

/// Multiple begin/rollback cycles in sequence should not leak state or corrupt data.
#[test]
fn rapid_begin_rollback_cycles_no_state_leak() {
    let db = GrafeoDB::new_in_memory();
    let mut session = db.session();

    session
        .execute("INSERT (:Counter {name: 'stable', value: 42})")
        .unwrap();

    for i in 0..10 {
        session.begin_transaction().unwrap();
        session
            .execute(&format!(
                "MATCH (c:Counter {{name: 'stable'}}) SET c.value = {}",
                i * 100
            ))
            .unwrap();
        session.rollback().unwrap();
    }

    let result = session
        .execute("MATCH (c:Counter {name: 'stable'}) RETURN c.value")
        .unwrap();
    assert_eq!(
        result.rows()[0][0],
        Value::Int64(42),
        "value should remain 42 after 10 begin/rollback cycles"
    );
}