cranpose-core 0.1.0

Core runtime for a Jetpack Compose inspired UI framework in Rust
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
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
//! Tests for slot storage backends.

use crate::{SlotBackendKind, SlotStorage};

/// Smoke test to verify all backends can perform basic operations.
#[test]
fn test_all_backends_smoke() {
    // Test all four backends now that they've reached feature parity
    let backends = [
        SlotBackendKind::Baseline,
        SlotBackendKind::Chunked,
        SlotBackendKind::Hierarchical,
        SlotBackendKind::Split,
    ];

    for kind in backends {
        test_backend_smoke(kind);
    }
}

fn test_backend_smoke(kind: SlotBackendKind) {
    use crate::slot_backend::SlotBackend;

    let mut storage = SlotBackend::new(kind);

    // Begin a root group
    let result1 = storage.begin_group(100);
    assert!(
        !result1.restored_from_gap,
        "{:?}: First group should not be from gap",
        kind
    );

    // Allocate a value slot
    let slot = storage.alloc_value_slot(|| 42);

    // Read it back
    assert_eq!(
        *storage.read_value::<i32>(slot),
        42,
        "{:?}: Value should match",
        kind
    );

    // Write a new value
    storage.write_value(slot, 100);
    assert_eq!(
        *storage.read_value::<i32>(slot),
        100,
        "{:?}: Updated value should match",
        kind
    );

    // End the group
    storage.end_group();

    // Flush any pending operations
    storage.flush();
}

/// Test that backends handle recomposition correctly.
#[test]
fn test_backends_recomposition() {
    let backends = [
        SlotBackendKind::Baseline,
        SlotBackendKind::Chunked,
        SlotBackendKind::Hierarchical,
        SlotBackendKind::Split,
    ];

    for kind in backends {
        test_backend_recomposition(kind);
    }
}

fn test_backend_recomposition(kind: SlotBackendKind) {
    use crate::slot_backend::SlotBackend;

    let mut storage = SlotBackend::new(kind);

    // First composition
    storage.reset();
    let result = storage.begin_group(200);
    let group1 = result.group;

    // Associate a scope with this group
    storage.set_group_scope(group1, 1);

    let _slot1 = storage.alloc_value_slot(|| "hello");

    storage.end_group();
    storage.flush();

    // Recompose at that scope
    storage.reset();
    if let Some(_group) = storage.begin_recranpose_at_scope(1) {
        // Just verify we can navigate to this position
        // The actual value reuse depends on properly calling the same
        // init closure, which is handled by the composer
        storage.end_group();
        storage.end_recompose();
    } else {
        panic!("{:?}: Should find scope for recomposition", kind);
    }
}

/// Test gap behavior (conditional rendering).
#[test]
fn test_backends_gaps() {
    let backends = [
        SlotBackendKind::Baseline,
        SlotBackendKind::Chunked,
        SlotBackendKind::Hierarchical,
        SlotBackendKind::Split,
    ];

    for kind in backends {
        test_backend_gaps(kind);
    }
}

fn test_backend_gaps(kind: SlotBackendKind) {
    use crate::slot_backend::SlotBackend;

    let mut storage = SlotBackend::new(kind);

    // First composition: create a group with nested content
    storage.reset();
    let result = storage.begin_group(300);
    let group1 = result.group;
    storage.set_group_scope(group1, 2);

    // Create a nested group
    let _inner = storage.begin_group(301);
    let _slot = storage.alloc_value_slot(|| 123);
    storage.end_group();

    // Now end the outer group, but don't finalize
    // (In a real scenario, we'd have cursor < end)
    storage.end_group();

    storage.flush();

    // Just verify the backend can handle basic gap operations
    // Full gap restoration requires proper cursor management
    // that is complex to test in isolation
}

/// Test allocating fewer slots than before, creating gaps.
#[test]
fn test_backends_allocate_fewer_slots() {
    let backends = [
        SlotBackendKind::Baseline,
        SlotBackendKind::Chunked,
        SlotBackendKind::Hierarchical,
        SlotBackendKind::Split,
    ];

    for kind in backends {
        test_backend_allocate_fewer_slots(kind);
    }
}

fn test_backend_allocate_fewer_slots(kind: SlotBackendKind) {
    use crate::slot_backend::SlotBackend;

    let mut storage = SlotBackend::new(kind);

    // First composition: create three slots
    storage.reset();
    let _group = storage.begin_group(400);

    let slot1 = storage.alloc_value_slot(|| 10);
    let slot2 = storage.alloc_value_slot(|| 20);
    let slot3 = storage.alloc_value_slot(|| 30);

    assert_eq!(*storage.read_value::<i32>(slot1), 10);
    assert_eq!(*storage.read_value::<i32>(slot2), 20);
    assert_eq!(*storage.read_value::<i32>(slot3), 30);

    storage.end_group();
    storage.flush();

    // Second composition: only allocate two slots (one less)
    storage.reset();
    let _group = storage.begin_group(400);

    // Reuse first two slots
    let slot1_v2 = storage.alloc_value_slot(|| 10);
    let slot2_v2 = storage.alloc_value_slot(|| 20);

    assert_eq!(*storage.read_value::<i32>(slot1_v2), 10);
    assert_eq!(*storage.read_value::<i32>(slot2_v2), 20);

    // Don't allocate the third slot - it should become a gap
    storage.finalize_current_group();
    storage.end_group();
    storage.flush();

    // Third composition: allocate all three again
    storage.reset();
    let _group = storage.begin_group(400);

    let slot1_v3 = storage.alloc_value_slot(|| 10);
    let slot2_v3 = storage.alloc_value_slot(|| 20);
    let slot3_v3 = storage.alloc_value_slot(|| 30);

    assert_eq!(*storage.read_value::<i32>(slot1_v3), 10);
    assert_eq!(*storage.read_value::<i32>(slot2_v3), 20);
    assert_eq!(*storage.read_value::<i32>(slot3_v3), 30);

    storage.end_group();
    storage.flush();
}

/// Test gap restoration: finalize and re-enter a group with the same key.
#[test]
fn test_backends_gap_restore() {
    let backends = [
        SlotBackendKind::Baseline,
        SlotBackendKind::Chunked,
        SlotBackendKind::Hierarchical,
        SlotBackendKind::Split,
    ];

    for kind in backends {
        test_backend_gap_restore(kind);
    }
}

fn test_backend_gap_restore(kind: SlotBackendKind) {
    use crate::slot_backend::SlotBackend;

    let mut storage = SlotBackend::new(kind);

    // First composition: create a group with content
    storage.reset();
    let result1 = storage.begin_group(500);
    assert!(
        !result1.restored_from_gap,
        "{:?}: First group should not be from gap",
        kind
    );

    let _slot = storage.alloc_value_slot(|| 42);
    storage.end_group();

    // Mark the rest as gaps (simulating conditional rendering where we skip this group)
    storage.finalize_current_group();
    storage.flush();

    // Second composition: skip the group (don't render it)
    storage.reset();
    // Finalize without entering - marks it as gap
    storage.finalize_current_group();
    storage.flush();

    // Third composition: render it again - should restore from gap
    storage.reset();
    let result3 = storage.begin_group(500);

    // All backends now support gap restoration
    assert!(
        result3.restored_from_gap,
        "{:?}: Group should be restored from gap",
        kind
    );

    storage.end_group();
    storage.flush();
}

/// Regression test: insert in the middle after finalize.
#[test]
fn test_backends_insert_middle_after_finalize() {
    let backends = [
        SlotBackendKind::Baseline,
        SlotBackendKind::Chunked,
        SlotBackendKind::Hierarchical,
        SlotBackendKind::Split,
    ];

    for kind in backends {
        test_backend_insert_middle_after_finalize(kind);
    }
}

fn test_backend_insert_middle_after_finalize(kind: SlotBackendKind) {
    use crate::slot_backend::SlotBackend;

    let mut storage = SlotBackend::new(kind);

    // First composition: group 1 → 3 value slots → finalize → end → flush
    storage.reset();
    let _group = storage.begin_group(700);
    let slot1 = storage.alloc_value_slot(|| 10);
    let slot2 = storage.alloc_value_slot(|| 20);
    let slot3 = storage.alloc_value_slot(|| 30);
    storage.finalize_current_group();
    storage.end_group();
    storage.flush();

    // Verify initial values
    assert_eq!(*storage.read_value::<i32>(slot1), 10);
    assert_eq!(*storage.read_value::<i32>(slot2), 20);
    assert_eq!(*storage.read_value::<i32>(slot3), 30);

    // Second composition: group 1 → 2 value slots → insert a node → finalize
    storage.reset();
    let _group = storage.begin_group(700);
    let slot1_v2 = storage.alloc_value_slot(|| 10);
    let slot2_v2 = storage.alloc_value_slot(|| 20);

    // Insert a node in the middle (this triggers insertion logic)
    storage.record_node(42);

    storage.finalize_current_group();
    storage.end_group();
    storage.flush();

    // Verify that reading earlier slots still works
    assert_eq!(*storage.read_value::<i32>(slot1_v2), 10);
    assert_eq!(*storage.read_value::<i32>(slot2_v2), 20);
}

/// Regression test: gap restore must set frame end correctly.
#[test]
fn test_backends_gap_restore_frame_end() {
    let backends = [
        SlotBackendKind::Baseline,
        SlotBackendKind::Chunked,
        SlotBackendKind::Hierarchical,
        SlotBackendKind::Split,
    ];

    for kind in backends {
        test_backend_gap_restore_frame_end(kind);
    }
}

fn test_backend_gap_restore_frame_end(kind: SlotBackendKind) {
    use crate::slot_backend::SlotBackend;

    let mut storage = SlotBackend::new(kind);

    // First pass: group(key=10) → 2 values → end → flush
    storage.reset();
    let _group = storage.begin_group(800);
    let _slot1 = storage.alloc_value_slot(|| 100);
    let _slot2 = storage.alloc_value_slot(|| 200);
    storage.end_group();
    storage.finalize_current_group();
    storage.flush();

    // Second pass: group(key=10) (restored_from_gap) → immediately finalize
    // This tests that the frame end calculation (start + len + 1) is correct
    storage.reset();
    let result = storage.begin_group(800);

    // If this is a gap restore, frame.end should be set correctly to not walk past the group
    if result.restored_from_gap {
        // Immediately finalize without traversing children - should not panic
        storage.finalize_current_group();
    }

    storage.end_group();
    storage.flush();
}

/// Test scope-based recomposition across all backends.
#[test]
fn test_backends_scope_recomposition() {
    let backends = [
        SlotBackendKind::Baseline,
        SlotBackendKind::Chunked,
        SlotBackendKind::Hierarchical,
        SlotBackendKind::Split,
    ];

    for kind in backends {
        test_backend_scope_recomposition(kind);
    }
}

fn test_backend_scope_recomposition(kind: SlotBackendKind) {
    use crate::slot_backend::SlotBackend;

    let mut storage = SlotBackend::new(kind);

    // First composition: create nested groups with scopes
    storage.reset();

    let outer = storage.begin_group(600);
    storage.set_group_scope(outer.group, 10);

    let inner = storage.begin_group(601);
    storage.set_group_scope(inner.group, 11);

    let _slot = storage.alloc_value_slot(|| "test");

    storage.end_group(); // inner
    storage.end_group(); // outer
    storage.flush();

    // Recompose at inner scope
    storage.reset();
    let found = storage.begin_recranpose_at_scope(11);
    assert!(found.is_some(), "{:?}: Should find inner scope", kind);

    if found.is_some() {
        storage.end_group();
        storage.end_recompose();
    }

    storage.flush();
}

// ═══════════════════════════════════════════════════════════════════════════
// Helper functions for backend iteration
// ═══════════════════════════════════════════════════════════════════════════

fn all_backends() -> [SlotBackendKind; 4] {
    [
        SlotBackendKind::Baseline,
        SlotBackendKind::Chunked,
        SlotBackendKind::Hierarchical,
        SlotBackendKind::Split,
    ]
}

#[allow(dead_code)]
fn all_but_hierarchical() -> [SlotBackendKind; 3] {
    [
        SlotBackendKind::Baseline,
        SlotBackendKind::Chunked,
        SlotBackendKind::Split,
    ]
}

// ═══════════════════════════════════════════════════════════════════════════
// Hardened corner-case tests
// ═══════════════════════════════════════════════════════════════════════════

/// Test nested groups with partial finalize to ensure gap-restore math doesn't
/// cause the frame.end to walk past the parent's boundary.
#[test]
fn test_backends_nested_groups_partial_finalize() {
    for kind in all_backends() {
        use crate::slot_backend::SlotBackend;
        let mut storage = SlotBackend::new(kind);

        // pass 1: outer(100) -> inner(101) -> value
        storage.reset();
        let _outer = storage.begin_group(100);
        let _inner = storage.begin_group(101);
        let _slot = storage.alloc_value_slot(|| 1usize);
        storage.end_group(); // inner
                             // finalize outer while cursor is inside its range
        storage.finalize_current_group();
        storage.end_group(); // outer
        storage.flush();

        // pass 2: re-enter outer and inner; must not panic and must restore structure
        storage.reset();
        let _outer2 = storage.begin_group(100);
        // outer might be restored_from_gap
        let _inner2 = storage.begin_group(101);
        // When reusing a slot with matching type, old value persists
        let slot2 = storage.alloc_value_slot(|| 2usize);
        // Update to new value explicitly
        storage.write_value(slot2, 2usize);
        assert_eq!(*storage.read_value::<usize>(slot2), 2, "{:?}", kind);
        storage.end_group(); // inner
        storage.end_group(); // outer
        storage.flush();
    }
}

/// Test root-level finalize to ensure all backends support calling
/// finalize_current_group() when no groups are on the stack.
#[test]
fn test_backends_root_finalize_safe() {
    for kind in all_backends() {
        use crate::slot_backend::SlotBackend;
        let mut storage = SlotBackend::new(kind);

        // create some stuff
        storage.reset();
        let _g = storage.begin_group(2000);
        let _slot = storage.alloc_value_slot(|| 10i32);
        storage.end_group();
        storage.flush();

        // now reset and call finalize at root
        storage.reset();
        // root-level finalize should not panic
        let _ = storage.finalize_current_group();
        storage.flush();
    }
}

/// Test that all backends properly handle type mismatches when reusing value slots.
/// If a slot exists but has a different type, it should be overwritten with the new type.
#[test]
fn test_backends_value_slot_type_mismatch_overwrite() {
    for kind in all_backends() {
        use crate::slot_backend::SlotBackend;
        let mut storage = SlotBackend::new(kind);

        // pass 1: store an i32
        storage.reset();
        let _g = storage.begin_group(3000);
        let slot = storage.alloc_value_slot(|| 123i32);
        assert_eq!(*storage.read_value::<i32>(slot), 123);
        storage.end_group();
        storage.flush();

        // pass 2: same position, but now store &str
        storage.reset();
        let _g = storage.begin_group(3000);
        let slot2 = storage.alloc_value_slot(|| "hello");
        assert_eq!(storage.read_value::<&str>(slot2), &"hello");
        storage.end_group();
        storage.flush();
    }
}

/// Test interleaving nodes with values to stress the insertion and shifting logic.
/// Pattern: value -> node -> value
#[test]
fn test_backends_value_node_value_sequence() {
    for kind in all_backends() {
        use crate::slot_backend::SlotBackend;
        let mut storage = SlotBackend::new(kind);

        storage.reset();
        let _g = storage.begin_group(4000);

        let s1 = storage.alloc_value_slot(|| 1u8);
        storage.record_node(999); // interleave a node
        let s2 = storage.alloc_value_slot(|| 2u8);

        assert_eq!(*storage.read_value::<u8>(s1), 1, "{:?}", kind);
        assert_eq!(*storage.read_value::<u8>(s2), 2, "{:?}", kind);

        storage.end_group();
        storage.flush();
    }
}

/// Test that backends can handle long forward gap scans by creating many slots,
/// finalizing to create gaps, then inserting again.
#[test]
fn test_backends_gap_scan_forward() {
    for kind in all_backends() {
        use crate::slot_backend::SlotBackend;
        let mut storage = SlotBackend::new(kind);

        storage.reset();
        let _g = storage.begin_group(5000);

        // fill with several values so the next insert has to look ahead
        for i in 0..16 {
            let s = storage.alloc_value_slot(|| i);
            assert_eq!(*storage.read_value::<i32>(s), i, "{:?}", kind);
        }

        // finalize to create gaps beyond cursor
        storage.finalize_current_group();
        storage.end_group();
        storage.flush();

        // second pass: re-enter and insert again; must not panic
        storage.reset();
        let _g = storage.begin_group(5000);
        let _s = storage.alloc_value_slot(|| 999i32);
        storage.end_group();
        storage.flush();
    }
}

// ═══════════════════════════════════════════════════════════════════════════
// Deeper cross-backend tests (edge cases and regressions)
// ═══════════════════════════════════════════════════════════════════════════

/// Test that gap restore is rejected when the key doesn't match.
/// All backends must NOT restore from gap if the key changed.
#[test]
fn test_backends_gap_restore_rejects_key_mismatch() {
    for kind in all_backends() {
        use crate::slot_backend::SlotBackend;
        let mut storage = SlotBackend::new(kind);

        // pass 1: render key 900, with a value
        storage.reset();
        let _g = storage.begin_group(900);
        let _s = storage.alloc_value_slot(|| 1i32);
        storage.end_group();
        storage.finalize_current_group();
        storage.flush();

        // pass 2: now try to render key 901 at that spot
        storage.reset();
        let res = storage.begin_group(901);
        // must NOT be restored_from_gap because key differs
        assert!(
            !res.restored_from_gap,
            "{:?}: should not restore if key changed",
            kind
        );
        storage.end_group();
        storage.flush();
    }
}

/// Test allocating MORE slots after gap restore (opposite of fewer slots test).
/// Ensures that groups can grow after being restored from a gap.
#[test]
fn test_backends_allocate_more_slots_after_gap_restore() {
    for kind in all_backends() {
        use crate::slot_backend::SlotBackend;
        let mut storage = SlotBackend::new(kind);

        // pass 1: create group with 2 children
        storage.reset();
        let _g = storage.begin_group(910);
        let _a = storage.alloc_value_slot(|| 10i32);
        let _b = storage.alloc_value_slot(|| 20i32);
        storage.end_group();
        storage.flush();

        // pass 2: skip the group entirely to make it a gap
        storage.reset();
        storage.finalize_current_group(); // marks everything as gaps
        storage.flush();

        // pass 3: restore and allocate 3rd child
        storage.reset();
        let res = storage.begin_group(910);
        assert!(res.restored_from_gap, "{:?}: should restore from gap", kind);
        let _a2 = storage.alloc_value_slot(|| 10i32);
        let _b2 = storage.alloc_value_slot(|| 20i32);
        let c2 = storage.alloc_value_slot(|| 30i32);
        assert_eq!(*storage.read_value::<i32>(c2), 30);
        storage.end_group();
        storage.flush();
    }
}

/// Test multiple sibling groups where only the second is skipped.
/// Ensures root-level finalize doesn't over-mark siblings.
#[test]
fn test_backends_multiple_sibling_groups_gap_isolated() {
    for kind in all_backends() {
        use crate::slot_backend::SlotBackend;
        let mut storage = SlotBackend::new(kind);

        // pass 1: G1(1000), G2(1001)
        storage.reset();
        let _g1 = storage.begin_group(1000);
        let _s1 = storage.alloc_value_slot(|| 1i32);
        storage.end_group();

        let _g2 = storage.begin_group(1001);
        let _s2 = storage.alloc_value_slot(|| 2i32);
        storage.end_group();

        storage.flush();

        // pass 2: render only G1, skip G2 entirely
        storage.reset();
        let _g1b = storage.begin_group(1000);
        let _s1b = storage.alloc_value_slot(|| 11i32);
        storage.end_group();

        // Finalize to mark G2 (and anything after) as gaps
        storage.finalize_current_group();
        storage.flush();

        // pass 3: render both G1 and G2 – G2 should restore from gap
        storage.reset();
        let _g1c = storage.begin_group(1000);
        let _s1c = storage.alloc_value_slot(|| 111i32);
        storage.end_group();

        let g2c = storage.begin_group(1001);
        assert!(
            g2c.restored_from_gap,
            "{:?}: second sibling should restore from gap",
            kind
        );
        storage.end_group();
        storage.flush();
    }
}

/// Test insertion right before a following group.
/// Ensures that group frames of the following group are updated correctly after shifts.
#[test]
fn test_backends_insert_before_following_group() {
    for kind in all_backends() {
        use crate::slot_backend::SlotBackend;
        let mut storage = SlotBackend::new(kind);

        // pass 1: group 1, group 2
        storage.reset();
        let _g1 = storage.begin_group(1100);
        let _v1 = storage.alloc_value_slot(|| 1u32);
        storage.end_group();

        let _g2 = storage.begin_group(1101);
        let _v2 = storage.alloc_value_slot(|| 2u32);
        storage.end_group();
        storage.flush();

        // pass 2: re-enter, but in g1 insert an extra node so g2 shifts right
        storage.reset();
        let _g1b = storage.begin_group(1100);
        let _v1b = storage.alloc_value_slot(|| 1u32);
        // insertion that forces shifting
        storage.record_node(777);
        storage.end_group();

        let _g2b = storage.begin_group(1101);
        let v2b = storage.alloc_value_slot(|| 22u32);
        // Explicitly write value to ensure it's set (alloc may reuse old slot)
        storage.write_value(v2b, 22u32);
        assert_eq!(
            *storage.read_value::<u32>(v2b),
            22,
            "{:?}: shifted second group must still work",
            kind
        );
        storage.end_group();
        storage.flush();
    }
}

/// Test that scope recomposition returns None for non-existent scope and doesn't break next composition.
#[test]
fn test_backends_scope_recomposition_not_found_is_safe() {
    for kind in all_backends() {
        use crate::slot_backend::SlotBackend;
        let mut storage = SlotBackend::new(kind);

        // initial content
        storage.reset();
        let _g = storage.begin_group(1200);
        let _v = storage.alloc_value_slot(|| 1i32);
        storage.end_group();
        storage.flush();

        // try to recompose at non-existent scope
        storage.reset();
        let found = storage.begin_recranpose_at_scope(999_999);
        assert!(
            found.is_none(),
            "{:?}: nonexistent scope should return None",
            kind
        );
        // after a failed recompose we should still be able to do a normal pass
        let _g2 = storage.begin_group(1200);
        let _v2 = storage.alloc_value_slot(|| 2i32);
        storage.end_group();
        storage.flush();
    }
}

// ═══════════════════════════════════════════════════════════════════════════
// Backend-specific tests
// ═══════════════════════════════════════════════════════════════════════════

/// Split-specific test: verify that payload persists in the HashMap even when
/// layout slots are marked as gaps.
#[test]
fn test_split_payload_persists_across_gap() {
    use crate::slot_backend::{SlotBackend, SlotBackendKind};

    let mut storage = SlotBackend::new(SlotBackendKind::Split);

    // pass 1
    storage.reset();
    let _g = storage.begin_group(6000);
    let vs = storage.alloc_value_slot(|| String::from("keep me"));
    assert_eq!(storage.read_value::<String>(vs), "keep me");
    storage.end_group();
    storage.finalize_current_group();
    storage.flush();

    // pass 2: same group, same slot position — should restore from gap and still be able to read
    storage.reset();
    let _g = storage.begin_group(6000);
    let vs2 = storage.alloc_value_slot(|| String::from("should not overwrite if recovered"));
    // we accept either behavior (reused or overwritten), but it must not panic
    let _ = storage.read_value::<String>(vs2);
    storage.end_group();
    storage.flush();
}

/// Hierarchical-specific test: verify scope search works in the current storage.
#[test]
fn test_hierarchical_scope_search() {
    use crate::slot_backend::{SlotBackend, SlotBackendKind};

    let mut storage = SlotBackend::new(SlotBackendKind::Hierarchical);

    // Create a group with a scope
    storage.reset();
    let result = storage.begin_group(7000);
    storage.set_group_scope(result.group, 123);
    let _slot = storage.alloc_value_slot(|| 42i32);
    storage.end_group();
    storage.flush();

    // Try to recompose at that scope
    storage.reset();
    let found = storage.begin_recranpose_at_scope(123);
    assert!(found.is_some(), "Hierarchical should find scope 123");

    if found.is_some() {
        storage.end_group();
        storage.end_recompose();
    }

    storage.flush();
}

/// Split-specific test: gap with payload, then different type.
/// Ensures split backend properly overwrites payload when type changes.
#[test]
fn test_split_payload_gap_then_type_change() {
    use crate::slot_backend::{SlotBackend, SlotBackendKind};
    let mut storage = SlotBackend::new(SlotBackendKind::Split);

    // pass 1
    storage.reset();
    let _g = storage.begin_group(1300);
    let s = storage.alloc_value_slot(|| String::from("first"));
    assert_eq!(storage.read_value::<String>(s), "first");
    storage.end_group();
    storage.finalize_current_group();
    storage.flush();

    // pass 2: same place, but different type – must overwrite, not panic
    storage.reset();
    let _g2 = storage.begin_group(1300);
    let s2 = storage.alloc_value_slot(|| 1234i32);
    assert_eq!(*storage.read_value::<i32>(s2), 1234);
    storage.end_group();
    storage.flush();
}

/// Chunked-specific test: anchor rebuild after big shift.
/// Ensures chunked backend rebuilds anchors correctly after shifting operations.
#[test]
fn test_chunked_anchor_rebuild_after_shift() {
    use crate::slot_backend::{SlotBackend, SlotBackendKind};
    let mut storage = SlotBackend::new(SlotBackendKind::Chunked);

    // pass 1: make a bunch of values
    storage.reset();
    let _g = storage.begin_group(1400);
    let mut ids = Vec::new();
    for i in 0..8 {
        let s = storage.alloc_value_slot(|| i);
        ids.push(s);
    }
    storage.end_group();
    storage.flush();

    // pass 2: insert in the middle to trigger shift
    storage.reset();
    let _g2 = storage.begin_group(1400);
    // reuse first few
    let _ = storage.alloc_value_slot(|| 0i32);
    // insert a node to force shift
    storage.record_node(9999);
    // allocate another value after shift
    let s_after = storage.alloc_value_slot(|| 777i32);
    // Explicitly write value to ensure it's set (alloc may reuse old slot)
    storage.write_value(s_after, 777i32);
    assert_eq!(*storage.read_value::<i32>(s_after), 777);
    storage.end_group();
    storage.flush();
}

/// Test that chunked backend returns position-based slot IDs.
/// This ensures the chunked backend uses the same ValueSlotId semantics as other backends.
#[test]
fn test_chunked_value_slot_is_position_based() {
    use crate::slot_backend::{SlotBackend, SlotBackendKind};
    let mut storage = SlotBackend::new(SlotBackendKind::Chunked);

    storage.reset();
    let _g = storage.begin_group(9000);
    let slot = storage.alloc_value_slot(|| 123i32);
    // we expect the id to be 1 here because group is at 0, value at 1
    assert_eq!(
        slot.index(),
        1,
        "chunked backend must use position-based ValueSlotId like other backends"
    );
    storage.end_group();
    storage.flush();
}

/// Test that chunked backend can read values after root-level finalize.
/// This ensures the original bug (root finalize breaking reads) stays fixed.
#[test]
fn test_chunked_read_after_root_finalize() {
    use crate::slot_backend::{SlotBackend, SlotBackendKind};
    let mut storage = SlotBackend::new(SlotBackendKind::Chunked);

    // pass 1
    storage.reset();
    let _g = storage.begin_group(9100);
    let _slot = storage.alloc_value_slot(|| 10i32);
    storage.end_group();
    storage.flush();

    // pass 2: root finalize creates gaps
    storage.reset();
    storage.finalize_current_group(); // root-level finalize
    storage.flush();

    // pass 3: re-render and read again
    storage.reset();
    let _g = storage.begin_group(9100);
    let slot2 = storage.alloc_value_slot(|| 10i32);
    // should not panic, should be readable
    assert_eq!(*storage.read_value::<i32>(slot2), 10);
    storage.end_group();
    storage.flush();
}

// ═══════════════════════════════════════════════════════════════════════════
// Targeted corner-case hardening tests
// ═══════════════════════════════════════════════════════════════════════════

/// Test that gap restore with fewer children than stored respects parent frame bounds.
/// This catches "forgot +1" and "didn't shrink frame" mistakes where traversal
/// could walk past the parent's end boundary.
#[test]
fn test_backends_gap_restore_shorter_children_respects_parent_frame() {
    for kind in all_backends() {
        use crate::slot_backend::SlotBackend;
        let mut storage = SlotBackend::new(kind);

        // pass 1: parent(2000) → child(2001) → 3 values
        storage.reset();
        let _parent = storage.begin_group(2000);
        let _child = storage.begin_group(2001);
        let _v1 = storage.alloc_value_slot(|| 100u32);
        let _v2 = storage.alloc_value_slot(|| 200u32);
        let _v3 = storage.alloc_value_slot(|| 300u32);
        storage.end_group(); // child
                             // Finalize parent early to establish its bounds
        storage.finalize_current_group();
        storage.end_group(); // parent
        storage.flush();

        // pass 2: skip entire structure to create gaps
        storage.reset();
        storage.finalize_current_group();
        storage.flush();

        // pass 3: restore parent and child, but allocate only 1 child value
        storage.reset();
        let res_parent = storage.begin_group(2000);
        assert!(
            res_parent.restored_from_gap,
            "{:?}: parent should restore from gap",
            kind
        );

        let _res_child = storage.begin_group(2001);
        // Child might be restored from gap or freshly created depending on backend
        let v1_new = storage.alloc_value_slot(|| 111u32);
        storage.write_value(v1_new, 111u32);
        assert_eq!(
            *storage.read_value::<u32>(v1_new),
            111,
            "{:?}: should read new value",
            kind
        );

        // Finalize child to mark remaining slots as gaps
        storage.finalize_current_group();
        storage.end_group(); // child

        // This must not panic — the frame.end calculation must account for shorter children
        storage.end_group(); // parent
        storage.flush();
    }
}

/// Test that insertion inside the first group doesn't corrupt a following group's frame
/// when that second group has nested content.
#[test]
fn test_backends_insert_in_first_group_preserves_nested_second_group() {
    for kind in all_backends() {
        use crate::slot_backend::SlotBackend;
        let mut storage = SlotBackend::new(kind);

        // pass 1: group1(3000) → value, group2(3001) → inner(3002) → value
        storage.reset();
        let _g1 = storage.begin_group(3000);
        let _v1 = storage.alloc_value_slot(|| 10i32);
        storage.end_group();

        let _g2 = storage.begin_group(3001);
        let _inner = storage.begin_group(3002);
        let _v2 = storage.alloc_value_slot(|| 20i32);
        storage.end_group(); // inner
        storage.end_group(); // group2

        storage.flush();

        // pass 2: re-enter group1, insert a node to trigger shifting
        storage.reset();
        let _g1b = storage.begin_group(3000);
        let _v1b = storage.alloc_value_slot(|| 10i32);
        // Insert node — this shifts everything after, including group2
        storage.record_node(888);
        storage.end_group();

        // Now enter group2 and its nested inner group
        let _g2b = storage.begin_group(3001);
        let _innerb = storage.begin_group(3002);
        let v2b = storage.alloc_value_slot(|| 22i32);
        storage.write_value(v2b, 22i32);
        // This must not panic, and must read correctly
        assert_eq!(
            *storage.read_value::<i32>(v2b),
            22,
            "{:?}: nested group after shift must be readable",
            kind
        );
        storage.end_group(); // inner
        storage.end_group(); // group2

        storage.flush();
    }
}

/// Test that root-level finalize followed by gap restore keeps anchors consistent.
/// If someone "optimizes" anchor rebuild away, this breaks.
#[test]
fn test_backends_root_finalize_then_gap_restore_anchors_consistent() {
    for kind in all_backends() {
        use crate::slot_backend::SlotBackend;
        let mut storage = SlotBackend::new(kind);

        // pass 1: render group → value
        storage.reset();
        let _g = storage.begin_group(4000);
        let slot1 = storage.alloc_value_slot(|| 42i32);
        assert_eq!(*storage.read_value::<i32>(slot1), 42, "{:?}", kind);
        storage.end_group();
        storage.flush();

        // pass 2: root-level finalize creates gaps
        storage.reset();
        storage.finalize_current_group();
        storage.flush();

        // pass 3: restore group and read value again
        storage.reset();
        let res = storage.begin_group(4000);
        assert!(res.restored_from_gap, "{:?}: should restore from gap", kind);
        let slot2 = storage.alloc_value_slot(|| 42i32);
        // This must not panic and must have the correct value
        assert_eq!(
            *storage.read_value::<i32>(slot2),
            42,
            "{:?}: value must be readable after gap restore",
            kind
        );
        storage.end_group();
        storage.flush();
    }
}

/// Test that gap restore preserves scope, so scope-based recomposition still works.
/// We currently copy scope on restore — this test enforces that behavior.
#[test]
fn test_backends_gap_restore_preserves_scope() {
    for kind in all_backends() {
        use crate::slot_backend::SlotBackend;
        let mut storage = SlotBackend::new(kind);

        // pass 1: create group with scope 123
        storage.reset();
        let result = storage.begin_group(5000);
        storage.set_group_scope(result.group, 123);
        let _v = storage.alloc_value_slot(|| "test");
        storage.end_group();
        storage.flush();

        // pass 2: skip the group to make it a gap
        storage.reset();
        storage.finalize_current_group();
        storage.flush();

        // pass 3: restore the group from gap
        storage.reset();
        let res = storage.begin_group(5000);
        assert!(res.restored_from_gap, "{:?}: should restore from gap", kind);
        storage.end_group();
        storage.flush();

        // pass 4: verify scope is still findable
        storage.reset();
        let found = storage.begin_recranpose_at_scope(123);
        assert!(
            found.is_some(),
            "{:?}: scope 123 must be findable after gap restore",
            kind
        );
        if found.is_some() {
            storage.end_group();
            storage.end_recompose();
        }
        storage.flush();
    }
}

/// Test that type mismatch on reused layout slot in split backend properly overwrites payload.
/// We don't require gap-restore here; we just re-enter the same layout slot.
#[test]
fn test_split_type_mismatch_overwrites_payload_string_to_u64() {
    use crate::slot_backend::{SlotBackend, SlotBackendKind};
    let mut storage = SlotBackend::new(SlotBackendKind::Split);

    // pass 1: store String
    storage.reset();
    let _g = storage.begin_group(6000);
    let slot1 = storage.alloc_value_slot(|| String::from("original"));
    assert_eq!(storage.read_value::<String>(slot1), "original");
    storage.end_group();
    storage.flush();

    // pass 2: same group, same slot position, but with u64
    storage.reset();
    let _g2 = storage.begin_group(6000); // this reuses the existing layout, not a gap
    let slot2 = storage.alloc_value_slot(|| 9999u64);
    // must overwrite with new type
    assert_eq!(*storage.read_value::<u64>(slot2), 9999);
    storage.end_group();
    storage.flush();
}

#[test]
fn test_split_type_mismatch_overwrites_payload_string_to_u64_via_gap() {
    use crate::slot_backend::{SlotBackend, SlotBackendKind};
    let mut storage = SlotBackend::new(SlotBackendKind::Split);

    // pass 1: render String
    storage.reset();
    let _g = storage.begin_group(6000);
    let _s = storage.alloc_value_slot(|| String::from("original"));
    storage.end_group();
    storage.flush();

    // pass 2: skip everything -> make it a gap
    storage.reset();
    storage.finalize_current_group(); // cursor is 0 now, so group is turned into a gap
    storage.flush();

    // pass 3: re-enter, restore from gap, allocate u64
    storage.reset();
    let res = storage.begin_group(6000);
    assert!(res.restored_from_gap, "should restore from gap now");
    let s2 = storage.alloc_value_slot(|| 9999u64);
    assert_eq!(*storage.read_value::<u64>(s2), 9999);
    storage.end_group();
    storage.flush();
}

/// Test chunked gap scan upper bound: create >128 items (scan limit), make gaps past
/// the scan window, insert near the top. Must not panic; fallback overwrite works.
#[test]
fn test_chunked_gap_scan_upper_bound_fallback() {
    use crate::slot_backend::{SlotBackend, SlotBackendKind};
    let mut storage = SlotBackend::new(SlotBackendKind::Chunked);

    // pass 1: create group with 150 values (exceeds 128 scan limit)
    storage.reset();
    let _g = storage.begin_group(7000);
    for i in 0..150 {
        let s = storage.alloc_value_slot(|| i as u32);
        assert_eq!(*storage.read_value::<u32>(s), i as u32);
    }
    storage.end_group();
    storage.flush();

    // pass 2: create gaps past position 128 by finalizing after re-entering
    storage.reset();
    let _g = storage.begin_group(7000);
    // Traverse only the first few items
    let _s1 = storage.alloc_value_slot(|| 0u32);
    let _s2 = storage.alloc_value_slot(|| 1u32);
    // Finalize to mark positions 2..150 as gaps
    storage.finalize_current_group();
    storage.end_group();
    storage.flush();

    // pass 3: insert new item near the top (position 2)
    // This should trigger gap scan, but gaps are far away (past 128 window)
    // Must not panic; falls back to overwrite
    storage.reset();
    let _g = storage.begin_group(7000);
    let _s1 = storage.alloc_value_slot(|| 0u32);
    let _s2 = storage.alloc_value_slot(|| 1u32);
    // This allocation hits the gap scan limit and falls back
    let s3 = storage.alloc_value_slot(|| 999u32);
    storage.write_value(s3, 999u32);
    assert_eq!(
        *storage.read_value::<u32>(s3),
        999,
        "fallback overwrite must work"
    );
    storage.end_group();
    storage.flush();
}