cranpose-core 0.0.58

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
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
use super::*;

/// Test that emit_node rejects reuse when the parent's previous children list
/// didn't contain the candidate node. This prevents nodes from "teleporting"
/// between parents.
#[test]
fn emit_node_rejects_reuse_when_parent_did_not_own_child() {
    let (handle, _runtime) = runtime_handle();
    let mut slots = SlotTable::default();
    let mut applier = test_applier();

    // Create a parent and child node in the applier
    let parent_a = applier.create(Box::new(RecordingNode::default()));
    let parent_b = applier.create(Box::new(RecordingNode::default()));

    // Setup the test: we'll push parent_b and try to reuse a child that was
    // previously under parent_a. The emit_node should reject the reuse.
    let (composer, slots_host, applier_host) =
        setup_composer(&mut slots, &mut applier, handle.clone(), Some(parent_a));

    // First, emit a child under parent_a's context
    let child_id = composer.emit_node(|| TestDummyNode);

    // Now push parent_b with last_node_reused=false (simulating new parent)
    composer.core.last_node_reused.set(Some(false));
    composer.push_parent(parent_b);

    // The parent_b's previous children should be empty since it wasn't reused
    {
        let stack = composer.parent_stack();
        let frame = stack.last().expect("parent frame should exist");
        assert!(
            frame.previous.is_empty(),
            "New parent should have empty previous children"
        );
    }

    composer.pop_parent();
    drop(composer);
    teardown_composer(&mut slots, &mut applier, slots_host, applier_host);

    // Verify child was created
    assert!(child_id > 0, "Child should have been created");
}

/// Test that push_parent uses empty previous children when the parent node
/// was not reused (last_node_reused = false). This ensures new parents start
/// fresh and don't inherit children from a different node.
#[test]
fn push_parent_uses_empty_previous_when_not_reused() {
    let (handle, _runtime) = runtime_handle();
    let mut slots = SlotTable::default();
    let mut applier = test_applier();

    let parent_id = applier.create(Box::new(RecordingNode::default()));

    let (composer, slots_host, applier_host) =
        setup_composer(&mut slots, &mut applier, handle.clone(), Some(parent_id));

    // Simulate: last node was NOT reused (new node created)
    composer.core.last_node_reused.set(Some(false));
    composer.push_parent(parent_id);

    // Verify that previous is empty when node was not reused
    {
        let stack = composer.parent_stack();
        let frame = stack.last().expect("parent frame should exist");
        assert!(
            frame.previous.is_empty(),
            "When parent was not reused, previous children should be empty"
        );
    }

    composer.pop_parent();
    drop(composer);
    teardown_composer(&mut slots, &mut applier, slots_host, applier_host);
}

#[test]
fn new_parent_attaches_children_immediately_without_sync_children() {
    let (handle, _runtime) = runtime_handle();
    let mut slots = SlotTable::default();
    let mut applier = test_applier();

    let parent_id = applier.create(Box::new(RecordingNode::default()));

    let (composer, slots_host, applier_host) =
        setup_composer(&mut slots, &mut applier, handle.clone(), None);

    composer.core.last_node_reused.set(Some(false));
    composer.push_parent(parent_id);
    let child_id = composer.emit_node(RecordingNode::default);
    composer.pop_parent();

    let commands = composer.take_commands();
    assert_eq!(commands.attach_children.len(), 1);
    assert_eq!(commands.sync_children.len(), 0);
    assert!(commands.sync_child_ids.is_empty());
    assert_eq!(commands.attach_children[0].parent_id, parent_id);
    assert_eq!(commands.attach_children[0].child_id, child_id);

    drop(composer);
    teardown_composer(&mut slots, &mut applier, slots_host, applier_host);
}

#[test]
fn reused_parent_with_existing_children_still_defers_to_sync_children() {
    let (handle, _runtime) = runtime_handle();
    let mut slots = SlotTable::default();
    let mut applier = test_applier();

    let parent_id = applier.create(Box::new(RecordingNode::default()));
    let child_id = applier.create(Box::new(RecordingNode::default()));
    applier
        .with_node(parent_id, |node: &mut RecordingNode| {
            node.insert_child(child_id);
        })
        .expect("parent exists");
    applier
        .with_node(child_id, |node: &mut RecordingNode| {
            node.on_attached_to_parent(parent_id);
        })
        .expect("child exists");

    let (composer, slots_host, applier_host) =
        setup_composer(&mut slots, &mut applier, handle.clone(), None);

    composer.core.last_node_reused.set(Some(true));
    composer.push_parent(parent_id);
    {
        let mut stack = composer.parent_stack();
        let frame = stack.last_mut().expect("parent frame should exist");
        frame.new_children.push(child_id);
    }
    composer.pop_parent();

    let commands = composer.take_commands();
    assert_eq!(commands.attach_children.len(), 0);
    assert_eq!(commands.sync_children.len(), 1);
    assert_eq!(commands.sync_children[0].parent_id, parent_id);
    assert_eq!(commands.sync_child_ids.as_slice(), [child_id]);

    drop(composer);
    teardown_composer(&mut slots, &mut applier, slots_host, applier_host);
}

#[test]
fn non_reused_parent_with_existing_children_still_defers_to_sync_children() {
    let (handle, _runtime) = runtime_handle();
    let mut slots = SlotTable::default();
    let mut applier = test_applier();

    let parent_id = applier.create(Box::new(RecordingNode::default()));
    let stale_child_id = applier.create(Box::new(RecordingNode::default()));
    let new_child_id = applier.create(Box::new(RecordingNode::default()));
    applier
        .with_node(parent_id, |node: &mut RecordingNode| {
            node.insert_child(stale_child_id);
        })
        .expect("parent exists");
    applier
        .with_node(stale_child_id, |node: &mut RecordingNode| {
            node.on_attached_to_parent(parent_id);
        })
        .expect("stale child exists");

    let (composer, slots_host, applier_host) =
        setup_composer(&mut slots, &mut applier, handle.clone(), None);

    composer.core.last_node_reused.set(Some(false));
    composer.push_parent(parent_id);
    {
        let stack = composer.parent_stack();
        let frame = stack.last().expect("parent frame should exist");
        assert_eq!(
            frame.previous.as_slice(),
            [stale_child_id],
            "non-reused parents must still diff existing live children",
        );
    }
    {
        let mut stack = composer.parent_stack();
        let frame = stack.last_mut().expect("parent frame should exist");
        frame.new_children.push(new_child_id);
    }
    composer.pop_parent();

    let commands = composer.take_commands();
    assert_eq!(commands.attach_children.len(), 0);
    assert_eq!(commands.sync_children.len(), 1);
    assert_eq!(commands.sync_children[0].parent_id, parent_id);
    assert_eq!(commands.sync_child_ids.as_slice(), [new_child_id]);

    drop(composer);
    teardown_composer(&mut slots, &mut applier, slots_host, applier_host);
}

#[test]
fn sync_children_reorders_small_child_lists_without_regressing_behavior() {
    let mut applier = test_applier();

    let parent_id = applier.create(Box::new(RecordingNode::default()));
    let child_a = applier.create(Box::new(RecordingNode::default()));
    let child_b = applier.create(Box::new(RecordingNode::default()));
    let child_c = applier.create(Box::new(RecordingNode::default()));
    let child_d = applier.create(Box::new(RecordingNode::default()));

    for child_id in [child_a, child_b, child_c] {
        applier
            .with_node(parent_id, |node: &mut RecordingNode| {
                node.insert_child(child_id);
            })
            .expect("parent exists");
        applier
            .with_node(child_id, |node: &mut RecordingNode| {
                node.on_attached_to_parent(parent_id);
            })
            .expect("child exists");
    }

    Command::SyncChildren {
        parent_id,
        expected_children: SmallVec::<[NodeId; 4]>::from_slice(&[child_c, child_a, child_d]),
    }
    .apply(&mut applier)
    .expect("sync children");

    let final_children = applier
        .with_node(parent_id, |node: &mut RecordingNode| node.children.clone())
        .expect("parent exists");
    assert_eq!(final_children, vec![child_c, child_a, child_d]);

    assert!(
        matches!(applier.get_mut(child_b), Err(NodeError::Missing { .. })),
        "removed child should no longer exist in the applier",
    );
    let parent_of_d = applier
        .with_node(child_d, |node: &mut RecordingNode| node.parent())
        .expect("inserted child exists");
    assert_eq!(parent_of_d, Some(parent_id));
}

#[test]
fn queued_sync_children_preserves_child_reparented_later_in_same_apply() {
    let mut applier = test_applier();
    let old_parent = applier.create(Box::new(RecordingNode::default()));
    let new_parent = applier.create(Box::new(RecordingNode::default()));
    let unmounts = Rc::new(Cell::new(0));
    let child = applier.create(Box::new(UnmountTrackingNode::new(Rc::clone(&unmounts))));

    insert_child_with_reparenting(&mut applier, old_parent, child);

    let mut commands = CommandQueue::default();
    commands.push(Command::SyncChildren {
        parent_id: old_parent,
        expected_children: SmallVec::new(),
    });
    commands.push(Command::AttachChild {
        parent_id: new_parent,
        child_id: child,
        bubble: DirtyBubble::LAYOUT_AND_MEASURE,
    });

    commands
        .apply(&mut applier)
        .expect("same-frame reparent should succeed");

    assert_eq!(
        unmounts.get(),
        0,
        "child must stay live until the later attach command reparents it",
    );
    let child_parent = applier
        .with_node(child, |node: &mut UnmountTrackingNode| node.parent())
        .expect("child should remain live");
    assert_eq!(child_parent, Some(new_parent));
    let old_children = applier
        .with_node(old_parent, |node: &mut RecordingNode| node.children.clone())
        .expect("old parent exists");
    assert!(
        old_children.is_empty(),
        "old parent should no longer list the reparented child",
    );
    let new_children = applier
        .with_node(new_parent, |node: &mut RecordingNode| node.children.clone())
        .expect("new parent exists");
    assert_eq!(new_children, vec![child]);
}

#[test]
fn skipped_group_root_nodes_only_considers_direct_parent_membership() {
    let (handle, _runtime) = runtime_handle();
    let mut slots = SlotTable::default();
    let mut applier = test_applier();

    let grandparent = applier.create(Box::new(RecordingNode::default()));
    let parent = applier.create(Box::new(RecordingNode::default()));
    let child = applier.create(Box::new(RecordingNode::default()));

    insert_child_with_reparenting(&mut applier, grandparent, parent);
    insert_child_with_reparenting(&mut applier, parent, child);

    let (composer, slots_host, applier_host) =
        setup_composer(&mut slots, &mut applier, handle, None);

    let roots = composer.skipped_group_root_nodes(&[grandparent, child]);
    assert_eq!(
        roots,
        vec![grandparent, child],
        "a node stays a skipped-group root when only a higher ancestor is in the skipped set",
    );

    drop(composer);
    teardown_composer(&mut slots, &mut applier, slots_host, applier_host);
}

#[test]
fn cold_recycled_nodes_are_not_reused_in_same_frame() {
    #[derive(Default)]
    struct RecyclableTestNode;

    impl Node for RecyclableTestNode {
        fn recycle_key(&self) -> Option<std::any::TypeId> {
            Some(std::any::TypeId::of::<Self>())
        }
    }

    let mut applier = test_applier();
    let stable_id = applier.create(Box::new(RecyclableTestNode));

    applier.remove(stable_id).expect("remove recyclable node");

    assert!(
        applier
            .take_recycled_node(std::any::TypeId::of::<RecyclableTestNode>())
            .is_none(),
        "cold shells removed in the current frame must not be reused immediately",
    );
    assert_eq!(applier.node_generation(stable_id), 1);
}

#[test]
fn fresh_recyclable_nodes_seed_same_frame_shell_reuse() {
    #[derive(Default)]
    struct SeedableNode;

    impl Node for SeedableNode {
        fn recycle_key(&self) -> Option<std::any::TypeId> {
            Some(std::any::TypeId::of::<Self>())
        }

        fn recycle_pool_limit(&self) -> Option<usize> {
            Some(2)
        }

        fn rehouse_for_recycle(&self) -> Option<Box<dyn Node>> {
            Some(Box::new(Self))
        }
    }

    let mut applier = test_applier();
    let key = std::any::TypeId::of::<SeedableNode>();
    let fresh = Box::new(SeedableNode);

    applier.record_fresh_recyclable_creation(key);
    if let Some(shell) = fresh.rehouse_for_recycle() {
        applier.seed_recycled_node_shell(key, fresh.recycle_pool_limit(), shell);
    }

    let recycled = applier
        .take_recycled_node(key)
        .expect("fresh miss should seed a reusable same-frame shell");
    assert_eq!(recycled.stable_id(), 0);
}

#[test]
fn recycled_nodes_reuse_stable_ids_without_growing_stable_id_arena() {
    #[derive(Default)]
    struct RecyclableTestNode;

    impl Node for RecyclableTestNode {
        fn recycle_key(&self) -> Option<std::any::TypeId> {
            Some(std::any::TypeId::of::<Self>())
        }
    }

    let mut applier = test_applier();
    let _keep_live = applier.create(Box::new(RecyclableTestNode));
    let stable_id = applier.create(Box::new(RecyclableTestNode));
    let next_stable_id_before_remove = applier.debug_stats().next_stable_id;

    applier.remove(stable_id).expect("remove recyclable node");
    applier.record_fresh_recyclable_creation(std::any::TypeId::of::<RecyclableTestNode>());
    applier.clear_recycled_nodes();

    let recycled = applier
        .take_recycled_node(std::any::TypeId::of::<RecyclableTestNode>())
        .expect("recycled node should be available after the frame boundary");
    assert_eq!(recycled.stable_id(), stable_id);
    assert_eq!(applier.node_generation(stable_id), 1);

    let (reused_id, node, warm_origin) = recycled.into_parts();
    applier
        .insert_with_id(reused_id, node)
        .expect("reinsert recycled stable id");
    applier.set_recycled_node_origin(reused_id, warm_origin);

    let stats = applier.debug_stats();
    assert_eq!(reused_id, stable_id);
    assert_eq!(stats.next_stable_id, next_stable_id_before_remove);
    assert_eq!(stats.stable_generations_len, next_stable_id_before_remove);
    assert_eq!(applier.node_generation(stable_id), 1);
}

#[test]
fn warm_recycled_nodes_can_be_reused_again_in_the_same_frame() {
    #[derive(Default)]
    struct RecyclableTestNode;

    impl Node for RecyclableTestNode {
        fn recycle_key(&self) -> Option<std::any::TypeId> {
            Some(std::any::TypeId::of::<Self>())
        }
    }

    let mut applier = test_applier();
    let _keep_live = applier.create(Box::new(RecyclableTestNode));
    let stable_id = applier.create(Box::new(RecyclableTestNode));

    applier.remove(stable_id).expect("remove recyclable node");
    applier.record_fresh_recyclable_creation(std::any::TypeId::of::<RecyclableTestNode>());
    applier.clear_recycled_nodes();

    let recycled = applier
        .take_recycled_node(std::any::TypeId::of::<RecyclableTestNode>())
        .expect("warm recyclable node");
    let (reused_id, node, warm_origin) = recycled.into_parts();
    applier
        .insert_with_id(reused_id, node)
        .expect("reinsert warm recyclable node");
    applier.set_recycled_node_origin(reused_id, warm_origin);

    applier
        .remove(reused_id)
        .expect("remove warm recyclable node again");

    let recycled_again = applier
        .take_recycled_node(std::any::TypeId::of::<RecyclableTestNode>())
        .expect("warm-origin shell should be reusable in the same frame");
    assert_eq!(recycled_again.stable_id(), stable_id);
}

#[test]
fn orphaned_cleanup_skips_recycled_nodes_with_new_generation() {
    #[derive(Default)]
    struct RecyclableTestNode;

    impl Node for RecyclableTestNode {
        fn recycle_key(&self) -> Option<std::any::TypeId> {
            Some(std::any::TypeId::of::<Self>())
        }
    }

    let mut composition = test_composition();
    let key = std::any::TypeId::of::<RecyclableTestNode>();
    let stable_id = composition
        .applier_mut()
        .create(Box::new(RecyclableTestNode));
    let old_generation = composition.applier_mut().node_generation(stable_id);

    {
        let mut applier = composition.applier_mut();
        applier
            .remove(stable_id)
            .expect("remove recyclable node before reuse");
        applier.record_fresh_recyclable_creation(key);
        applier.clear_recycled_nodes();

        let recycled = applier
            .take_recycled_node(key)
            .expect("warm recyclable node should be available");
        let (reused_id, node, warm_origin) = recycled.into_parts();
        assert_eq!(reused_id, stable_id);
        applier
            .insert_with_id(reused_id, node)
            .expect("reinsert warm recyclable node");
        applier.set_recycled_node_origin(reused_id, warm_origin);
    }

    let new_generation = composition.applier_mut().node_generation(stable_id);
    assert_ne!(
        old_generation, new_generation,
        "same-frame recycle must advance the stable generation",
    );

    composition
        .slots
        .borrow_mut()
        .push_orphaned_node_for_test(stable_id, old_generation);

    let removed_any = composition
        .finalize_compaction()
        .expect("orphaned cleanup should succeed");
    assert!(
        !removed_any,
        "stale orphaned generation should not remove the recycled live node",
    );
    assert!(
        composition.applier_mut().get_mut(stable_id).is_ok(),
        "recycled live node should survive stale orphan cleanup",
    );
}

#[test]
fn recycled_node_pool_limit_discards_oldest_shells() {
    #[derive(Default)]
    struct LimitedRecycleNode;

    impl Node for LimitedRecycleNode {
        fn recycle_key(&self) -> Option<std::any::TypeId> {
            Some(std::any::TypeId::of::<Self>())
        }

        fn recycle_pool_limit(&self) -> Option<usize> {
            Some(2)
        }
    }

    let mut applier = test_applier();
    let _keep_first = applier.create(Box::new(LimitedRecycleNode));
    let _keep_second = applier.create(Box::new(LimitedRecycleNode));
    let first = applier.create(Box::new(LimitedRecycleNode));
    let second = applier.create(Box::new(LimitedRecycleNode));
    let third = applier.create(Box::new(LimitedRecycleNode));

    applier.remove(first).expect("remove first recyclable node");
    applier
        .remove(second)
        .expect("remove second recyclable node");
    applier.remove(third).expect("remove third recyclable node");

    assert_eq!(
        applier.debug_recycled_node_count_for::<LimitedRecycleNode>(),
        2,
        "pending shells still respect the configured pool bound inside the frame",
    );

    applier.record_fresh_recyclable_creation(std::any::TypeId::of::<LimitedRecycleNode>());
    applier.record_fresh_recyclable_creation(std::any::TypeId::of::<LimitedRecycleNode>());
    applier.clear_recycled_nodes();

    assert_eq!(
        applier.debug_recycled_node_count_for::<LimitedRecycleNode>(),
        2,
        "warm recycle cohort should retain only the configured number of shells",
    );

    let most_recent = applier
        .take_recycled_node(std::any::TypeId::of::<LimitedRecycleNode>())
        .expect("most recent recycled node");
    let next_recent = applier
        .take_recycled_node(std::any::TypeId::of::<LimitedRecycleNode>())
        .expect("next recent recycled node");

    assert_eq!(most_recent.stable_id(), third);
    assert_eq!(next_recent.stable_id(), second);
}

#[test]
fn clear_recycled_nodes_trims_warm_pool_without_fresh_demand() {
    #[derive(Default)]
    struct LimitedRecycleNode;

    impl Node for LimitedRecycleNode {
        fn recycle_key(&self) -> Option<std::any::TypeId> {
            Some(std::any::TypeId::of::<Self>())
        }

        fn recycle_pool_limit(&self) -> Option<usize> {
            Some(4)
        }
    }

    let mut applier = test_applier();
    let _keep_live = applier.create(Box::new(LimitedRecycleNode));
    let removed: Vec<_> = (0..4)
        .map(|_| applier.create(Box::new(LimitedRecycleNode)))
        .collect();

    for id in removed {
        applier.remove(id).expect("remove recyclable node");
    }

    applier.clear_recycled_nodes();

    assert_eq!(
        applier.debug_recycled_node_count_for::<LimitedRecycleNode>(),
        0,
        "warm recycle cohort should be drained when the frame did not miss any recyclable shells",
    );
}

#[test]
fn warm_recycled_nodes_survive_idle_frame_after_recent_demand() {
    #[derive(Default)]
    struct LimitedRecycleNode;

    impl Node for LimitedRecycleNode {
        fn recycle_key(&self) -> Option<std::any::TypeId> {
            Some(std::any::TypeId::of::<Self>())
        }

        fn recycle_pool_limit(&self) -> Option<usize> {
            Some(4)
        }
    }

    let mut applier = test_applier();
    let _keep_live = applier.create(Box::new(LimitedRecycleNode));
    let recycled_id = applier.create(Box::new(LimitedRecycleNode));

    applier.remove(recycled_id).expect("remove recyclable node");
    applier.record_fresh_recyclable_creation(std::any::TypeId::of::<LimitedRecycleNode>());
    applier.clear_recycled_nodes();

    assert_eq!(
        applier.debug_recycled_node_count_for::<LimitedRecycleNode>(),
        1,
        "recent fresh demand should promote one warm shell",
    );

    applier.clear_recycled_nodes();

    assert_eq!(
        applier.debug_recycled_node_count_for::<LimitedRecycleNode>(),
        1,
        "recent demand floor should keep a compact warm shell across an idle frame",
    );
}

#[test]
fn clear_recycled_nodes_rebuilds_warm_pool_from_compact_prototype() {
    #[derive(Default)]
    struct PrototypeRecycleNode;

    impl Node for PrototypeRecycleNode {
        fn recycle_key(&self) -> Option<std::any::TypeId> {
            Some(std::any::TypeId::of::<Self>())
        }

        fn recycle_pool_limit(&self) -> Option<usize> {
            Some(8)
        }

        fn rehouse_for_recycle(&self) -> Option<Box<dyn Node>> {
            Some(Box::new(Self))
        }
    }

    let mut applier = test_applier();
    let key = std::any::TypeId::of::<PrototypeRecycleNode>();
    let seed = PrototypeRecycleNode;
    let shell = seed
        .rehouse_for_recycle()
        .expect("seed node should provide a compact shell");

    applier.seed_recycled_node_shell(key, seed.recycle_pool_limit(), shell);
    let recycled = applier
        .take_recycled_node(key)
        .expect("seeded shell should be immediately available");
    let (reused_id, node, warm_origin) = recycled.into_parts();
    applier
        .insert_with_id(reused_id, node)
        .expect("consumed warm shell should become the live cohort member");
    applier.set_recycled_node_origin(reused_id, warm_origin);

    for _ in 0..3 {
        applier.record_fresh_recyclable_creation(key);
    }
    applier.clear_recycled_nodes();

    assert_eq!(
        applier.debug_recycled_node_count_for::<PrototypeRecycleNode>(),
        3,
        "recent demand should rebuild the warm pool even after all spare shells were consumed",
    );
}

#[test]
fn large_recycle_pools_converge_to_standing_reserve_on_first_demand() {
    #[derive(Default)]
    struct LargeReserveNode;

    impl Node for LargeReserveNode {
        fn recycle_key(&self) -> Option<std::any::TypeId> {
            Some(std::any::TypeId::of::<Self>())
        }

        fn recycle_pool_limit(&self) -> Option<usize> {
            Some(128)
        }

        fn rehouse_for_recycle(&self) -> Option<Box<dyn Node>> {
            Some(Box::new(Self))
        }
    }

    let mut applier = test_applier();
    let key = std::any::TypeId::of::<LargeReserveNode>();
    let shell = LargeReserveNode
        .rehouse_for_recycle()
        .expect("large reserve node should provide a compact shell");
    applier.seed_recycled_node_shell(key, Some(128), shell);
    let recycled = applier
        .take_recycled_node(key)
        .expect("seeded shell should be immediately available");
    let (reused_id, node, warm_origin) = recycled.into_parts();
    applier
        .insert_with_id(reused_id, node)
        .expect("consumed shell should remain live");
    applier.set_recycled_node_origin(reused_id, warm_origin);

    applier.record_fresh_recyclable_creation(key);
    applier.clear_recycled_nodes();

    assert_eq!(
        applier.debug_recycled_node_count_for::<LargeReserveNode>(),
        32,
        "large recyclable types should pre-warm the standing reserve instead of growing it only after a spike",
    );
}

#[test]
fn clear_recycled_nodes_releases_excess_warm_id_capacity() {
    #[derive(Default)]
    struct LargeReserveNode;

    impl Node for LargeReserveNode {
        fn recycle_key(&self) -> Option<std::any::TypeId> {
            Some(std::any::TypeId::of::<Self>())
        }

        fn recycle_pool_limit(&self) -> Option<usize> {
            Some(128)
        }

        fn rehouse_for_recycle(&self) -> Option<Box<dyn Node>> {
            Some(Box::new(Self))
        }
    }

    let mut applier = test_applier();
    let key = std::any::TypeId::of::<LargeReserveNode>();
    let mut live_ids = Vec::with_capacity(4096);

    for _ in 0..4096 {
        if let Some(recycled) = applier.take_recycled_node(key) {
            let (id, node, warm_origin) = recycled.into_parts();
            applier
                .insert_with_id(id, node)
                .expect("reinsert recycled node");
            applier.set_recycled_node_origin(id, warm_origin);
            live_ids.push(id);
        } else {
            let node = Box::new(LargeReserveNode);
            applier.record_fresh_recyclable_creation(key);
            if let Some(shell) = node.rehouse_for_recycle() {
                applier.seed_recycled_node_shell(key, node.recycle_pool_limit(), shell);
            }
            let id = applier.create(node);
            live_ids.push(id);
        }
    }

    for id in live_ids {
        applier.remove(id).expect("remove recyclable node");
    }

    applier.clear_recycled_nodes();

    let stats = applier.debug_stats();
    assert!(
        stats.warm_recycled_node_id_count <= 32,
        "warm id bookkeeping should converge to the standing reserve instead of keeping spike-era ids live: {stats:?}",
    );
    assert!(
        stats.warm_recycled_node_id_capacity
            <= stats.warm_recycled_node_id_count.max(32).saturating_mul(4),
        "warm id bookkeeping retained excess capacity after the frame boundary: {stats:?}",
    );
}

#[test]
fn compact_prunes_stable_generation_entries_for_removed_nodes() {
    #[derive(Default)]
    struct PlainNode;

    impl Node for PlainNode {}

    let mut applier = test_applier();
    let keep = applier.create(Box::new(PlainNode));
    let removed: Vec<_> = (0..4096)
        .map(|_| applier.create(Box::new(PlainNode)))
        .collect();

    for id in removed {
        applier.remove(id).expect("remove node");
    }

    applier.compact();

    let stats = applier.debug_stats();
    assert_eq!(stats.stable_to_physical_len, 1);
    assert_eq!(stats.nodes_len, 1);
    assert_eq!(keep, 0);
    assert_eq!(
        stats.stable_generations_len, 1,
        "dead node generations should be pruned after compaction instead of retaining a dense arena",
    );
    assert!(
        stats.next_stable_id > keep,
        "stable ids must stay monotonic even after pruning generation entries",
    );
}

#[test]
fn remove_balanced_tree_uses_depth_bounded_traversal_stack() {
    #[derive(Default)]
    struct TreeNode {
        children: Vec<NodeId>,
        parent: Option<NodeId>,
    }

    impl Node for TreeNode {
        fn insert_child(&mut self, child: NodeId) {
            self.children.push(child);
        }

        fn remove_child(&mut self, child: NodeId) {
            self.children.retain(|&id| id != child);
        }

        fn children(&self) -> Vec<NodeId> {
            self.children.clone()
        }

        fn on_attached_to_parent(&mut self, parent: NodeId) {
            self.parent = Some(parent);
        }

        fn on_removed_from_parent(&mut self) {
            self.parent = None;
        }

        fn parent(&self) -> Option<NodeId> {
            self.parent
        }
    }

    fn build_balanced_binary_tree(applier: &mut MemoryApplier, remaining_depth: usize) -> NodeId {
        let node_id = applier.create(Box::new(TreeNode::default()));
        if remaining_depth == 0 {
            return node_id;
        }

        let left = build_balanced_binary_tree(applier, remaining_depth - 1);
        let right = build_balanced_binary_tree(applier, remaining_depth - 1);

        applier
            .with_node::<TreeNode, _>(node_id, |node| {
                node.insert_child(left);
                node.insert_child(right);
            })
            .expect("attach children to parent");

        for child_id in [left, right] {
            applier
                .with_node::<TreeNode, _>(child_id, |node| {
                    node.on_attached_to_parent(node_id);
                })
                .expect("attach parent to child");
        }

        node_id
    }

    let mut applier = test_applier();
    let tree_depth = 12usize;
    let root = build_balanced_binary_tree(&mut applier, tree_depth);

    let max_depth = applier
        .debug_remove_max_traversal_depth(root)
        .expect("remove balanced tree");

    assert!(
        max_depth <= tree_depth + 1,
        "removal traversal should scale with tree depth, not subtree size: tree_depth={tree_depth} max_depth={max_depth}",
    );
    assert!(applier.is_empty(), "all nodes should be removed");
}

/// Test that push_parent reuses previous children when the parent node
/// was reused (last_node_reused = true). This ensures stable parents
/// correctly track their children across recompositions.
#[test]
fn push_parent_inherits_previous_when_reused() {
    let (handle, _runtime) = runtime_handle();
    let mut slots = SlotTable::default();
    let mut applier = test_applier();

    let parent_id = applier.create(Box::new(RecordingNode::default()));
    let child_id = applier.create(Box::new(RecordingNode::default()));

    {
        let (composer, slots_host, applier_host) =
            setup_composer(&mut slots, &mut applier, handle.clone(), Some(parent_id));

        // First pass: establish children
        composer.core.last_node_reused.set(Some(true)); // Pretend parent was reused
        composer.push_parent(parent_id);

        // Add a real child node
        {
            let mut stack = composer.parent_stack();
            let frame = stack.last_mut().expect("parent frame should exist");
            frame.new_children.push(child_id);
        }

        composer.pop_parent();
        drop(composer);
        teardown_composer(&mut slots, &mut applier, slots_host, applier_host);
    }

    // Manually record the child on the parent node so push_parent reads it back
    applier
        .with_node(parent_id, |node: &mut RecordingNode| {
            if !node.children.contains(&child_id) {
                node.children.push(child_id);
            }
        })
        .expect("parent node exists");

    // Reset for second pass
    slots.reset();

    {
        let (composer, slots_host, applier_host) =
            setup_composer(&mut slots, &mut applier, handle.clone(), Some(parent_id));

        // Second pass: parent is reused, should inherit previous children
        composer.core.last_node_reused.set(Some(true));
        composer.push_parent(parent_id);

        // Verify that previous contains the child from first pass
        {
            let stack = composer.parent_stack();
            let frame = stack.last().expect("parent frame should exist");
            assert_eq!(
                frame.previous.as_slice(),
                [child_id],
                "When parent was reused, previous children should be inherited"
            );
        }

        composer.pop_parent();
        drop(composer);
        teardown_composer(&mut slots, &mut applier, slots_host, applier_host);
    }
}

/// Regression test: emit_node should successfully create/reuse nodes
/// even when push_parent starts with empty previous children (new parent case).
/// This was broken when we added a parent_contains check to emit_node that
/// prevented node creation when previous was empty.
///
/// Scenario (simulates tab switch):
/// 1. Render a parent with a child
/// 2. Remove the parent (tab switch away)  
/// 3. Restore the parent (tab switch back)
/// 4. Child should be created/reused successfully (functionality preserved)
#[test]
fn emit_node_creates_nodes_when_parent_restored_after_conditional_removal() {
    let mut composition = test_composition();
    let runtime = composition.runtime_handle();
    let toggle = MutableState::with_runtime(true, runtime.clone());

    let key = location_key(file!(), line!(), column!());

    // Track child node IDs across renders
    let child_ids: Rc<RefCell<Vec<NodeId>>> = Rc::new(RefCell::new(Vec::new()));

    // First render: parent visible with child
    println!("=== First render: parent visible ===");
    {
        let child_ids = Rc::clone(&child_ids);
        composition
            .render(key, move || {
                if toggle.value() {
                    with_current_composer(|composer| {
                        // Parent node
                        let _parent = composer.emit_node(|| TestDummyNode);
                        composer.core.last_node_reused.set(Some(true));
                        composer.push_parent(_parent);

                        // Child node - track its ID
                        let child = composer.emit_node(|| TestTextNode {
                            text: "Reusable Child".to_string(),
                        });
                        child_ids.borrow_mut().push(child);

                        composer.pop_parent();
                    });
                }
            })
            .expect("first render");
    }

    let first_child_id = child_ids.borrow()[0];
    println!("First child ID: {}", first_child_id);
    assert!(first_child_id > 0, "First child should be created");

    // Second render: parent removed (simulate tab switch away)
    println!("=== Second render: parent hidden ===");
    toggle.set_value(false);
    {
        composition
            .render(key, move || {
                if toggle.value() {
                    // Not rendered - parent is hidden
                }
            })
            .expect("second render");
    }

    // Third render: parent restored (simulate tab switch back)
    // This is where the regression occurred - emit_node was failing
    // when parent's previous children was empty
    println!("=== Third render: parent restored ===");
    toggle.set_value(true);
    {
        let child_ids = Rc::clone(&child_ids);
        composition
            .render(key, move || {
                if toggle.value() {
                    with_current_composer(|composer| {
                        // Parent node - may be recreated due to conditional
                        let _parent = composer.emit_node(|| TestDummyNode);
                        let reused = composer.core.last_node_reused.get();
                        println!("Parent reused: {:?}", reused);
                        composer.push_parent(_parent);

                        // CRITICAL: Child node should be successfully created/reused
                        // even though parent's previous children list may be empty.
                        // This is what broke when we added the parent_contains check.
                        let child = composer.emit_node(|| TestTextNode {
                            text: "Reusable Child".to_string(),
                        });
                        child_ids.borrow_mut().push(child);
                        println!("Third render child ID: {}", child);

                        composer.pop_parent();
                    });
                }
            })
            .expect("third render");
    }

    let third_child_id = child_ids.borrow().last().copied().unwrap();
    println!("Third child ID: {}", third_child_id);

    // The child should exist in the applier after parent restoration.
    assert!(
        composition.applier_mut().get_mut(third_child_id).is_ok(),
        "Child node should be successfully created after parent restoration."
    );

    // Verify we got two child IDs (one from first render, one from third render)
    assert_eq!(
        child_ids.borrow().len(),
        2,
        "Should have recorded child IDs from both visible renders"
    );
}

/// Test that nodes can be emitted under a new parent without requiring
/// the parent to have had them previously. This is the core invariant
/// that broke when emit_node checked parent_contains too strictly.
#[test]
fn emit_node_works_with_new_parent_having_empty_previous() {
    let (handle, _runtime) = runtime_handle();
    let mut slots = SlotTable::default();
    let mut applier = test_applier();

    let parent_id = applier.create(Box::new(RecordingNode::default()));

    let (composer, slots_host, applier_host) =
        setup_composer(&mut slots, &mut applier, handle.clone(), Some(parent_id));

    // Simulate a NEW parent (not reused) - this gives empty previous children
    composer.core.last_node_reused.set(Some(false));
    composer.push_parent(parent_id);

    // Verify previous is empty (this is the push_parent conditional behavior)
    {
        let stack = composer.parent_stack();
        let frame = stack.last().expect("parent frame should exist");
        assert!(
            frame.previous.is_empty(),
            "New parent should have empty previous"
        );
    }

    // Now emit a child - this should WORK even though previous is empty
    // The child should be created (or reused based on type, if one exists in slots)
    let child_id = composer.emit_node(|| TestDummyNode);

    // The child should have a valid ID
    assert!(child_id > 0, "Child should be emitted successfully");

    // last_node_reused should be set (either true for reuse or false for new)
    let was_reused = composer.core.last_node_reused.get();
    assert!(
        was_reused.is_some(),
        "emit_node should set last_node_reused"
    );

    composer.pop_parent();
    drop(composer);
    teardown_composer(&mut slots, &mut applier, slots_host, applier_host);
}

/// Test that state changes made in frame callbacks are visible globally.
///
/// This is a regression test for a bug where fling animation scroll state
/// would reset because frame callback state writes weren't being applied
/// to the global snapshot.
#[test]
fn frame_callback_state_changes_are_visible_globally() {
    let (handle, _runtime) = runtime_handle();
    let state = MutableState::with_runtime(0i32, handle.clone());

    // Register a frame callback that updates state
    let state_for_callback = state;
    let callback_ran = Rc::new(Cell::new(false));
    let callback_ran_for_closure = callback_ran.clone();

    let _registration = handle.frame_clock().with_frame_nanos(move |_| {
        // This state change should be visible after drain_frame_callbacks completes
        state_for_callback.set(42);
        callback_ran_for_closure.set(true);
    });

    // Before draining, state should be 0
    assert_eq!(state.get(), 0);

    // Drain frame callbacks - this should apply state changes to global snapshot
    handle.drain_frame_callbacks(1);

    // Verify callback ran
    assert!(callback_ran.get(), "Frame callback should have run");

    // CRITICAL: State change from callback should be visible
    // Before the fix, this would return 0 because the write was isolated
    assert_eq!(
        state.get(),
        42,
        "State change in frame callback should be visible globally"
    );
}

/// Test that multiple frame callbacks in sequence all have their state changes visible.
#[test]
fn multiple_frame_callbacks_state_visibility() {
    let (handle, _runtime) = runtime_handle();
    let state = MutableState::with_runtime(0i32, handle.clone());

    // First frame callback increments by 10
    let state1 = state;
    let _reg1 = handle.frame_clock().with_frame_nanos(move |_| {
        let current = state1.get();
        state1.set(current + 10);
    });

    // Second frame callback increments by 5
    let state2 = state;
    let _reg2 = handle.frame_clock().with_frame_nanos(move |_| {
        let current = state2.get();
        state2.set(current + 5);
    });

    // Drain all callbacks
    handle.drain_frame_callbacks(1);

    // Both callbacks should have run and their changes should be visible
    // The first sets to 10, the second reads 10 and sets to 15
    assert_eq!(
        state.get(),
        15,
        "Sequential frame callback state changes should accumulate correctly"
    );
}

/// Verifies that `MutableState::set` on a released state handle does not panic.
///
/// This reproduces a real crash: a SideEffect closure captures a `MutableState`
/// handle whose underlying `OwnedMutableState` is dropped (by group disposal)
/// before the side effect runs. The `set` call must silently skip the write.
#[test]
fn test_stale_state_handle_set_does_not_panic() {
    let _guard = reset_snapshot_runtime();
    let test_runtime = crate::runtime::TestRuntime::new();
    let handle = test_runtime.handle();

    // Allocate a state and get a handle, then release the underlying cell.
    let lease = handle.alloc_state(42u32);
    let state: MutableState<u32> = MutableState::from_lease(&lease);
    assert_eq!(state.get(), 42);

    // Drop the lease → releases the state arena slot
    drop(lease);

    // Setting a released state should NOT panic — it should be a no-op
    state.set(99);
}

/// Verifies that `MutableState::try_with` and `is_alive` work correctly
/// on released state handles.
///
/// This reproduces a real crash: a fling animation frame callback captures a
/// `MutableState` handle. A tab switch disposes the composition group (releasing
/// the state), but the frame callback still fires and tries to read the state.
#[test]
fn test_stale_state_handle_try_with_returns_none() {
    let _guard = reset_snapshot_runtime();
    let test_runtime = crate::runtime::TestRuntime::new();
    let handle = test_runtime.handle();

    let lease = handle.alloc_state(42u32);
    let state: MutableState<u32> = MutableState::from_lease(&lease);
    assert!(state.is_alive());
    assert_eq!(state.try_value(), Some(42));
    assert_eq!(state.try_with(|v| *v + 1), Some(43));

    // Drop the lease → releases the state arena slot
    drop(lease);

    assert!(!state.is_alive());
    assert_eq!(state.try_value(), None);
    assert_eq!(state.try_with(|v| *v + 1), None);
}

#[test]
fn param_state_update_reuses_existing_buffer_via_clone_from() {
    let mut state = crate::ParamState::<String> {
        value: Some(String::with_capacity(64)),
    };
    state
        .value
        .as_mut()
        .expect("seeded string")
        .push_str("seed value");

    let initial_ptr = state.value.as_ref().expect("seeded string").as_ptr();
    let updated = "short replacement";
    assert!(state.update(&updated.to_string()));

    let stored = state.value.as_ref().expect("updated string");
    assert_eq!(stored, updated);
    assert_eq!(
        stored.as_ptr(),
        initial_ptr,
        "ParamState::update should reuse the existing String allocation when capacity permits",
    );
}