cranpose-core 0.0.59

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
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
use super::*;

// ============================================================================
// Slot Table Unit Tests - Explicit Hidden Entries
// ============================================================================

#[test]
fn slot_table_marks_values_hidden() {
    let mut slots = test_slot_table();
    let mut state = test_slot_session();

    // Create initial composition with 3 value slots
    let _idx1 = use_test_value_slot(&mut slots, &mut state, || 1i32);
    let _idx2 = use_test_value_slot(&mut slots, &mut state, || 2i32);
    let _idx3 = use_test_value_slot(&mut slots, &mut state, || 3i32);

    // Hide the middle slot
    hide_test_range(&mut slots, 1, 2, None);

    // Verify we can still read first and last slots
    assert_eq!(slots.read_value::<i32>(0), &1);
    assert_eq!(slots.read_value::<i32>(2), &3);
}

#[test]
fn slot_table_restores_hidden_values() {
    let mut slots = test_slot_table();
    let mut state = test_slot_session();

    // Create initial value
    let idx1 = use_test_value_slot(&mut slots, &mut state, || 1i32);
    assert_eq!(slots.read_value::<i32>(idx1), &1);

    // Hide the slot.
    reset_slot_session(&mut state);
    hide_test_range(&mut slots, 0, 1, None);

    // Reset cursor to restore.
    reset_slot_session(&mut state);

    let initialized = Cell::new(false);
    let idx2 = use_test_value_slot(&mut slots, &mut state, || {
        initialized.set(true);
        42i32
    });

    assert_eq!(idx2, 0, "hidden value should restore in place");
    assert!(
        !initialized.get(),
        "restoring a hidden value must not reinitialize it"
    );
    assert_eq!(slots.read_value::<i32>(idx2), &1);
}

#[test]
fn slot_table_replaces_mismatched_value_types() {
    let mut slots = test_slot_table();
    let mut state = test_slot_session();

    // Create initial value of type i32
    let idx = use_test_value_slot(&mut slots, &mut state, || 1i32);
    assert_eq!(slots.read_value::<i32>(idx), &1);

    // Reset and try to use with different type
    reset_slot_session(&mut state);
    let idx2 = use_test_value_slot(&mut slots, &mut state, || "hello");

    // Should replace at same position
    assert_eq!(idx, idx2);
    assert_eq!(slots.read_value::<&str>(idx2), &"hello");
}

#[test]
fn slot_table_handles_nested_hidden_groups() {
    let mut slots = test_slot_table();
    let mut state = test_slot_session();

    // Create a parent group
    let parent_idx = begin_test_group(&mut slots, &mut state, 100).0;

    // Create child group
    let child_idx = begin_test_group(&mut slots, &mut state, 200).0;
    let _val_idx = use_test_value_slot(&mut slots, &mut state, || 42i32);
    end_test_group(&mut slots, &mut state); // End child

    end_test_group(&mut slots, &mut state); // End parent

    // Verify parent group was created
    let groups = slots.debug_dump_groups();
    assert!(groups.iter().any(|(idx, _, _, _)| *idx == parent_idx));

    // Hide the parent group range. The child subtree should become hidden too.
    hide_test_range(&mut slots, parent_idx, child_idx + 2, None);

    // Structure remains preserved for reuse through hidden entries.
}

#[test]
fn slot_table_preserves_sibling_groups_when_hiding_ranges() {
    let mut slots = test_slot_table();
    let mut state = test_slot_session();

    // Create first group with a value
    let g1 = begin_test_group(&mut slots, &mut state, 1).0;
    let _v1 = use_test_value_slot(&mut slots, &mut state, || "first");
    end_test_group(&mut slots, &mut state);

    // Create second group with a value
    let g2 = begin_test_group(&mut slots, &mut state, 2).0;
    let _v2 = use_test_value_slot(&mut slots, &mut state, || "second");
    end_test_group(&mut slots, &mut state);

    // Create third group with a value
    let _g3 = begin_test_group(&mut slots, &mut state, 3);
    let v3_idx = use_test_value_slot(&mut slots, &mut state, || "third");
    end_test_group(&mut slots, &mut state);

    // Capture initial group count
    let initial_groups = slots.debug_dump_groups();
    assert_eq!(initial_groups.len(), 3, "should have 3 groups initially");

    // Hide only the first group's range.
    hide_test_range(&mut slots, g1, g2, None);

    // Third group should still be accessible (the value should remain)
    assert_eq!(slots.read_value::<&str>(v3_idx), &"third");

    // Groups outside the hidden range remain live.
    let remaining_groups = slots.debug_dump_groups();
    assert!(
        remaining_groups.len() >= 2,
        "groups outside marked range should be preserved, found {} groups",
        remaining_groups.len()
    );
}

#[test]
fn slot_table_tab_switching_preserves_scopes() {
    let mut composition = test_composition();
    let runtime = composition.runtime_handle();
    let active_tab = MutableState::with_runtime(0i32, runtime.clone());
    let tab1_counter = MutableState::with_runtime(0i32, runtime.clone());
    let tab2_counter = MutableState::with_runtime(0i32, runtime.clone());

    thread_local! {
        static TAB1_RENDERS: Cell<usize> = const { Cell::new(0) };
        static TAB2_RENDERS: Cell<usize> = const { Cell::new(0) };
    }

    #[composable]
    fn tab_content_1(counter: MutableState<i32>) {
        TAB1_RENDERS.with(|c| c.set(c.get() + 1));
        let count = counter.value();
        let id = cranpose_test_node(TestTextNode::default);
        with_node_mut(id, |node: &mut TestTextNode| {
            node.text = format!("Tab 1: {}", count);
        })
        .expect("update tab1 node");
    }

    #[composable]
    fn tab_content_2(counter: MutableState<i32>) {
        TAB2_RENDERS.with(|c| c.set(c.get() + 1));
        let count = counter.value();
        let id = cranpose_test_node(TestTextNode::default);
        with_node_mut(id, |node: &mut TestTextNode| {
            node.text = format!("Tab 2: {}", count);
        })
        .expect("update tab2 node");
    }

    let mut render = {
        move || {
            let tab = active_tab.value();
            match tab {
                0 => tab_content_1(tab1_counter),
                1 => tab_content_2(tab2_counter),
                _ => {}
            }
        }
    };

    // Initial render - Tab 1
    TAB1_RENDERS.with(|c| c.set(0));
    TAB2_RENDERS.with(|c| c.set(0));

    let key = location_key(file!(), line!(), column!());
    composition
        .render(key, &mut render)
        .expect("initial render");

    assert_eq!(
        TAB1_RENDERS.with(|c| c.get()),
        1,
        "tab1 should render initially"
    );
    assert_eq!(
        TAB2_RENDERS.with(|c| c.get()),
        0,
        "tab2 should not render initially"
    );

    // Switch to Tab 2
    active_tab.set_value(1);
    composition
        .render(key, &mut render)
        .expect("switch to tab2");

    assert_eq!(
        TAB1_RENDERS.with(|c| c.get()),
        1,
        "tab1 render count unchanged"
    );
    assert_eq!(
        TAB2_RENDERS.with(|c| c.get()),
        1,
        "tab2 should render after switch"
    );

    // Update tab2 counter - should trigger recomposition
    TAB1_RENDERS.with(|c| c.set(0));
    TAB2_RENDERS.with(|c| c.set(0));
    tab2_counter.set_value(5);

    let _ = composition
        .process_invalid_scopes()
        .expect("recompose tab2");

    assert_eq!(
        TAB1_RENDERS.with(|c| c.get()),
        0,
        "tab1 should not recompose"
    );
    assert!(
        TAB2_RENDERS.with(|c| c.get()) > 0,
        "tab2 should recompose on counter change"
    );

    // Switch back to Tab 1
    TAB1_RENDERS.with(|c| c.set(0));
    TAB2_RENDERS.with(|c| c.set(0));
    active_tab.set_value(0);
    composition
        .render(key, &mut render)
        .expect("switch back to tab1");

    assert!(
        TAB1_RENDERS.with(|c| c.get()) > 0,
        "tab1 should render after switch back"
    );
    assert_eq!(TAB2_RENDERS.with(|c| c.get()), 0, "tab2 should not render");

    // Update tab1 counter - should trigger recomposition even after tab switch cycle
    TAB1_RENDERS.with(|c| c.set(0));
    TAB2_RENDERS.with(|c| c.set(0));
    tab1_counter.set_value(10);

    let _ = composition
        .process_invalid_scopes()
        .expect("recompose tab1 after cycle");

    assert!(
        TAB1_RENDERS.with(|c| c.get()) > 0,
        "tab1 scope should work after tab cycle"
    );
    assert_eq!(
        TAB2_RENDERS.with(|c| c.get()),
        0,
        "tab2 should not recompose"
    );
}

#[test]
fn slot_table_conditional_rendering_preserves_sibling_scopes() {
    let mut composition = test_composition();
    let runtime = composition.runtime_handle();
    let show_middle = MutableState::with_runtime(true, runtime.clone());
    let top_counter = MutableState::with_runtime(0i32, runtime.clone());
    let middle_counter = MutableState::with_runtime(0i32, runtime.clone());
    let bottom_counter = MutableState::with_runtime(0i32, runtime.clone());

    thread_local! {
        static TOP_RENDERS: Cell<usize> = const { Cell::new(0) };
        static MIDDLE_RENDERS: Cell<usize> = const { Cell::new(0) };
        static BOTTOM_RENDERS: Cell<usize> = const { Cell::new(0) };
    }

    #[composable]
    fn top_component(counter: MutableState<i32>) {
        TOP_RENDERS.with(|c| c.set(c.get() + 1));
        let count = counter.value();
        let id = cranpose_test_node(TestTextNode::default);
        with_node_mut(id, |node: &mut TestTextNode| {
            node.text = format!("Top: {}", count);
        })
        .expect("update top node");
    }

    #[composable]
    fn middle_component(counter: MutableState<i32>) {
        MIDDLE_RENDERS.with(|c| c.set(c.get() + 1));
        let count = counter.value();
        let id = cranpose_test_node(TestTextNode::default);
        with_node_mut(id, |node: &mut TestTextNode| {
            node.text = format!("Middle: {}", count);
        })
        .expect("update middle node");
    }

    #[composable]
    fn bottom_component(counter: MutableState<i32>) {
        BOTTOM_RENDERS.with(|c| c.set(c.get() + 1));
        let count = counter.value();
        let id = cranpose_test_node(TestTextNode::default);
        with_node_mut(id, |node: &mut TestTextNode| {
            node.text = format!("Bottom: {}", count);
        })
        .expect("update bottom node");
    }

    let mut render = {
        move || {
            top_component(top_counter);

            if show_middle.value() {
                middle_component(middle_counter);
            }

            bottom_component(bottom_counter);
        }
    };

    // Initial render with all components
    TOP_RENDERS.with(|c| c.set(0));
    MIDDLE_RENDERS.with(|c| c.set(0));
    BOTTOM_RENDERS.with(|c| c.set(0));

    let key = location_key(file!(), line!(), column!());
    composition
        .render(key, &mut render)
        .expect("initial render");

    assert_eq!(TOP_RENDERS.with(|c| c.get()), 1);
    assert_eq!(MIDDLE_RENDERS.with(|c| c.get()), 1);
    assert_eq!(BOTTOM_RENDERS.with(|c| c.get()), 1);

    // Hide middle component
    show_middle.set_value(false);
    composition.render(key, &mut render).expect("hide middle");

    // Update bottom counter - should still work
    TOP_RENDERS.with(|c| c.set(0));
    MIDDLE_RENDERS.with(|c| c.set(0));
    BOTTOM_RENDERS.with(|c| c.set(0));
    bottom_counter.set_value(5);

    let _ = composition
        .process_invalid_scopes()
        .expect("recompose bottom");

    assert_eq!(TOP_RENDERS.with(|c| c.get()), 0, "top should not recompose");
    assert_eq!(
        MIDDLE_RENDERS.with(|c| c.get()),
        0,
        "middle should not recompose"
    );
    assert!(
        BOTTOM_RENDERS.with(|c| c.get()) > 0,
        "bottom scope should work after middle removed"
    );

    // Update top counter - should still work
    TOP_RENDERS.with(|c| c.set(0));
    MIDDLE_RENDERS.with(|c| c.set(0));
    BOTTOM_RENDERS.with(|c| c.set(0));
    top_counter.set_value(3);

    let _ = composition.process_invalid_scopes().expect("recompose top");

    assert!(
        TOP_RENDERS.with(|c| c.get()) > 0,
        "top scope should work after middle removed"
    );
    assert_eq!(
        MIDDLE_RENDERS.with(|c| c.get()),
        0,
        "middle should not recompose"
    );
    assert_eq!(
        BOTTOM_RENDERS.with(|c| c.get()),
        0,
        "bottom should not recompose"
    );

    // Show middle again
    show_middle.set_value(true);
    composition
        .render(key, &mut render)
        .expect("show middle again");

    // Update middle counter - should work after restoration
    TOP_RENDERS.with(|c| c.set(0));
    MIDDLE_RENDERS.with(|c| c.set(0));
    BOTTOM_RENDERS.with(|c| c.set(0));
    middle_counter.set_value(7);

    let _ = composition
        .process_invalid_scopes()
        .expect("recompose middle after restore");

    assert_eq!(TOP_RENDERS.with(|c| c.get()), 0, "top should not recompose");
    assert!(
        MIDDLE_RENDERS.with(|c| c.get()) > 0,
        "middle scope should work after restoration"
    );
    assert_eq!(
        BOTTOM_RENDERS.with(|c| c.get()),
        0,
        "bottom should not recompose"
    );
}

#[test]
fn slot_table_gaps_work_with_nested_conditionals() {
    let mut composition = test_composition();
    let runtime = composition.runtime_handle();
    let outer_visible = MutableState::with_runtime(true, runtime.clone());
    let inner_visible = MutableState::with_runtime(true, runtime.clone());
    let outer_counter = MutableState::with_runtime(0i32, runtime.clone());
    let inner_counter = MutableState::with_runtime(0i32, runtime.clone());
    let after_counter = MutableState::with_runtime(0i32, runtime.clone());

    thread_local! {
        static OUTER_RENDERS: Cell<usize> = const { Cell::new(0) };
        static INNER_RENDERS: Cell<usize> = const { Cell::new(0) };
        static AFTER_RENDERS: Cell<usize> = const { Cell::new(0) };
    }

    #[composable]
    fn inner_content(counter: MutableState<i32>) {
        INNER_RENDERS.with(|c| c.set(c.get() + 1));
        let _count = counter.value();
    }

    #[composable]
    fn outer_content(
        inner_visible: MutableState<bool>,
        outer_counter: MutableState<i32>,
        inner_counter: MutableState<i32>,
    ) {
        OUTER_RENDERS.with(|c| c.set(c.get() + 1));
        let _count = outer_counter.value();

        if inner_visible.value() {
            inner_content(inner_counter);
        }
    }

    #[composable]
    fn after_content(counter: MutableState<i32>) {
        AFTER_RENDERS.with(|c| c.set(c.get() + 1));
        let _count = counter.value();
    }

    let mut render = {
        move || {
            if outer_visible.value() {
                outer_content(inner_visible, outer_counter, inner_counter);
            }
            after_content(after_counter);
        }
    };

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

    // Initial render - all visible
    composition
        .render(key, &mut render)
        .expect("initial render");

    // Hide inner
    inner_visible.set_value(false);
    composition.render(key, &mut render).expect("hide inner");

    // Verify after component scope still works
    AFTER_RENDERS.with(|c| c.set(0));
    after_counter.set_value(1);
    let _ = composition
        .process_invalid_scopes()
        .expect("recompose after");
    assert!(
        AFTER_RENDERS.with(|c| c.get()) > 0,
        "after scope should work with inner hidden"
    );

    // Hide outer (and inner is already hidden)
    outer_visible.set_value(false);
    composition.render(key, &mut render).expect("hide outer");

    // Verify after component scope still works
    AFTER_RENDERS.with(|c| c.set(0));
    after_counter.set_value(2);
    let _ = composition
        .process_invalid_scopes()
        .expect("recompose after with outer hidden");
    assert!(
        AFTER_RENDERS.with(|c| c.get()) > 0,
        "after scope should work with outer hidden"
    );

    // Show outer but keep inner hidden
    outer_visible.set_value(true);
    composition.render(key, &mut render).expect("show outer");

    // Verify outer scope works
    OUTER_RENDERS.with(|c| c.set(0));
    outer_counter.set_value(1);
    let _ = composition
        .process_invalid_scopes()
        .expect("recompose outer");
    assert!(
        OUTER_RENDERS.with(|c| c.get()) > 0,
        "outer scope should work after restoration"
    );

    // Show inner too
    inner_visible.set_value(true);
    composition.render(key, &mut render).expect("show inner");

    // Verify inner scope works after full restoration
    INNER_RENDERS.with(|c| c.set(0));
    inner_counter.set_value(1);
    let _ = composition
        .process_invalid_scopes()
        .expect("recompose inner");
    assert!(
        INNER_RENDERS.with(|c| c.get()) > 0,
        "inner scope should work after full restoration"
    );
}

#[test]
fn slot_table_multiple_rapid_tab_switches() {
    // Simulates rapid tab switching that could cause UI corruption
    let mut composition = test_composition();
    let runtime = composition.runtime_handle();
    let active_tab = MutableState::with_runtime(0i32, runtime.clone());

    thread_local! {
        static RENDER_LOG: RefCell<Vec<String>> = const { RefCell::new(Vec::new()) };
    }

    #[composable]
    fn tab_with_multiple_elements(tab_id: i32, counter: MutableState<i32>) {
        RENDER_LOG.with(|log| log.borrow_mut().push(format!("tab{}_start", tab_id)));

        // Multiple nodes to make the slot table more complex
        let count = counter.value();
        for i in 0..3 {
            let id = cranpose_test_node(TestTextNode::default);
            with_node_mut(id, |node: &mut TestTextNode| {
                node.text = format!("Tab {} Item {} ({})", tab_id, i, count);
            })
            .expect("update node");
        }

        RENDER_LOG.with(|log| log.borrow_mut().push(format!("tab{}_end", tab_id)));
    }

    let tab_counters: Vec<_> = (0..4)
        .map(|_| MutableState::with_runtime(0i32, runtime.clone()))
        .collect();

    let mut render = {
        let tab_counters = tab_counters.clone();
        move || {
            let tab = active_tab.value();
            if tab >= 0 && (tab as usize) < tab_counters.len() {
                tab_with_multiple_elements(tab, tab_counters[tab as usize]);
            }
        }
    };

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

    // Rapidly switch between tabs multiple times
    for cycle in 0..3 {
        for tab in 0..4 {
            RENDER_LOG.with(|log| log.borrow_mut().clear());
            active_tab.set_value(tab);
            composition
                .render(key, &mut render)
                .unwrap_or_else(|_| panic!("render cycle {} tab {}", cycle, tab));

            let log = RENDER_LOG.with(|log| log.borrow().clone());
            assert!(
                log.len() >= 2,
                "cycle {} tab {} should render start and end markers, got {:?}",
                cycle,
                tab,
                log
            );
            assert!(
                log[0].starts_with(&format!("tab{}_start", tab)),
                "cycle {} tab {} should start correctly, got {:?}",
                cycle,
                tab,
                log
            );
        }
    }

    // After all tab switches, counters should still work
    RENDER_LOG.with(|log| log.borrow_mut().clear());
    tab_counters[2].set_value(42);
    active_tab.set_value(2);
    composition.render(key, &mut render).expect("final render");

    let _ = composition
        .process_invalid_scopes()
        .expect("final recompose");

    // If scopes are preserved correctly, the counter update should have triggered recomposition
    let final_log = RENDER_LOG.with(|log| log.borrow().clone());
    assert!(
        final_log.len() >= 2,
        "final render should work after rapid tab switches, got {:?}",
        final_log
    );
}

#[test]
fn tab_switching_with_keyed_children() {
    // Test tab switching where children use keys for identity
    let mut composition = test_composition();
    let runtime = composition.runtime_handle();
    let active_tab = MutableState::with_runtime(0i32, runtime.clone());
    let counter = MutableState::with_runtime(0i32, runtime.clone());

    thread_local! {
        static TAB1_KEYED_RENDERS: Cell<usize> = const { Cell::new(0) };
        static TAB2_KEYED_RENDERS: Cell<usize> = const { Cell::new(0) };
    }

    #[composable]
    fn keyed_content(tab_id: i32, counter: MutableState<i32>) {
        if tab_id == 0 {
            TAB1_KEYED_RENDERS.with(|c| c.set(c.get() + 1));
        } else {
            TAB2_KEYED_RENDERS.with(|c| c.set(c.get() + 1));
        }

        let count = counter.value();
        cranpose_core::with_key(&format!("item_{}", tab_id), || {
            let id = cranpose_test_node(TestTextNode::default);
            with_node_mut(id, |node: &mut TestTextNode| {
                node.text = format!("Tab {} with key: {}", tab_id, count);
            })
            .expect("update keyed node");
        });
    }

    let mut render = {
        move || {
            let tab = active_tab.value();
            keyed_content(tab, counter);
        }
    };

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

    // Initial render - Tab 0
    TAB1_KEYED_RENDERS.with(|c| c.set(0));
    TAB2_KEYED_RENDERS.with(|c| c.set(0));
    composition
        .render(key, &mut render)
        .expect("initial render");
    assert_eq!(TAB1_KEYED_RENDERS.with(|c| c.get()), 1);

    // Switch to Tab 1
    active_tab.set_value(1);
    composition
        .render(key, &mut render)
        .expect("switch to tab 1");
    assert_eq!(TAB2_KEYED_RENDERS.with(|c| c.get()), 1);

    // Update counter and switch back to Tab 0
    counter.set_value(42);
    active_tab.set_value(0);
    TAB1_KEYED_RENDERS.with(|c| c.set(0));
    composition
        .render(key, &mut render)
        .expect("switch back to tab 0");

    // Tab 0 should rerender with updated counter
    assert!(
        TAB1_KEYED_RENDERS.with(|c| c.get()) > 0,
        "Tab 0 should rerender with updated counter value"
    );

    // Verify scope still works
    TAB1_KEYED_RENDERS.with(|c| c.set(0));
    counter.set_value(100);
    let _ = composition
        .process_invalid_scopes()
        .expect("recompose after counter update");
    assert!(
        TAB1_KEYED_RENDERS.with(|c| c.get()) > 0,
        "Tab 0 scope should still work after key-based tab switching"
    );
}

#[test]
fn stable_outer_parent_node_survives_keyed_inner_switch() {
    let mut composition = test_composition();
    let runtime = composition.runtime_handle();
    let active_tab = MutableState::with_runtime(0i32, runtime.clone());

    #[composable]
    fn keyed_switching_tree(active_tab: MutableState<i32>) {
        let active = active_tab.value();
        with_current_composer(|composer| {
            let root = composer.emit_node(RecordingNode::default);
            composer.push_parent(root);

            let _tab_bar = composer.emit_node(TrackingChild::default);
            let _spacer = composer.emit_node(TrackingChild::default);

            let outer_parent = composer.emit_node(RecordingNode::default);
            STABLE_OUTER_PARENT_ID.with(|slot| slot.set(Some(outer_parent)));
            composer.push_parent(outer_parent);

            cranpose_core::with_key(&active, || {
                if active == 0 {
                    let panel = composer.emit_node(RecordingNode::default);
                    composer.push_parent(panel);
                    composer.emit_node(|| TrackingChild {
                        label: "counter".to_string(),
                        ..Default::default()
                    });
                    composer.pop_parent();
                } else {
                    let panel = composer.emit_node(RecordingNode::default);
                    composer.push_parent(panel);
                    composer.emit_node(|| TrackingChild {
                        label: "animations-a".to_string(),
                        ..Default::default()
                    });
                    composer.emit_node(|| TrackingChild {
                        label: "animations-b".to_string(),
                        ..Default::default()
                    });
                    composer.pop_parent();
                }
            });

            composer.pop_parent();
            composer.pop_parent();
        });
    }

    let key = location_key(file!(), line!(), column!());
    composition
        .render(key, &mut || keyed_switching_tree(active_tab))
        .expect("initial render");

    let outer_parent_id = STABLE_OUTER_PARENT_ID
        .with(Cell::get)
        .expect("outer parent id captured");
    assert!(
        composition.applier_mut().get_mut(outer_parent_id).is_ok(),
        "outer parent should exist after initial render",
    );

    active_tab.set_value(1);
    let recomposed = composition
        .process_invalid_scopes()
        .expect("recompose after keyed switch");
    assert!(recomposed, "switching tabs should trigger recomposition");

    let root_id = composition.root().expect("root node exists");
    let root_children = composition
        .applier_mut()
        .with_node(root_id, |node: &mut RecordingNode| node.children.clone())
        .expect("root recording node exists");
    let outer_parent_exists = composition.applier_mut().get_mut(outer_parent_id).is_ok();
    let slot_owns_outer_parent = composition
        .debug_dump_all_slots()
        .iter()
        .any(|(_, desc)| desc == &format!("Node(id={outer_parent_id})"));

    assert!(
        outer_parent_exists,
        "stable outer parent node was removed during keyed inner switch: root_children={root_children:?}",
    );
    assert!(
        root_children.contains(&outer_parent_id),
        "root should still reference outer parent after keyed inner switch: root_children={root_children:?}",
    );
    assert!(
        slot_owns_outer_parent,
        "slot table must continue owning the stable outer parent node after keyed inner switch",
    );
}

#[test]
fn tab_switching_with_different_node_types() {
    // Test switching between tabs that create different node types
    let mut composition = test_composition();
    let runtime = composition.runtime_handle();
    let active_tab = MutableState::with_runtime(0i32, runtime.clone());

    thread_local! {
        static TEXT_NODE_COUNT: Cell<usize> = const { Cell::new(0) };
        static DUMMY_NODE_COUNT: Cell<usize> = const { Cell::new(0) };
    }

    #[composable]
    fn text_tab() {
        TEXT_NODE_COUNT.with(|c| c.set(c.get() + 1));
        let id = cranpose_test_node(TestTextNode::default);
        with_node_mut(id, |node: &mut TestTextNode| {
            node.text = "Text Node Tab".to_string();
        })
        .expect("update text node");
    }

    #[composable]
    fn dummy_tab() {
        DUMMY_NODE_COUNT.with(|c| c.set(c.get() + 1));
        cranpose_test_node(|| TestDummyNode);
    }

    let mut render = {
        move || match active_tab.value() {
            0 => text_tab(),
            _ => dummy_tab(),
        }
    };

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

    // Start with text node
    TEXT_NODE_COUNT.with(|c| c.set(0));
    DUMMY_NODE_COUNT.with(|c| c.set(0));
    composition
        .render(key, &mut render)
        .expect("initial render with text node");
    assert_eq!(TEXT_NODE_COUNT.with(|c| c.get()), 1);
    assert_eq!(DUMMY_NODE_COUNT.with(|c| c.get()), 0);

    // Switch to dummy node
    active_tab.set_value(1);
    composition
        .render(key, &mut render)
        .expect("switch to dummy node");
    assert_eq!(DUMMY_NODE_COUNT.with(|c| c.get()), 1);

    // Switch back to text node - should work without corruption
    active_tab.set_value(0);
    TEXT_NODE_COUNT.with(|c| c.set(0));
    composition
        .render(key, &mut render)
        .expect("switch back to text node");
    assert!(
        TEXT_NODE_COUNT.with(|c| c.get()) > 0,
        "Should successfully render text node after switching from different node type"
    );
}

#[test]
fn tab_switching_with_dynamic_lists() {
    // Test tab switching with tabs containing dynamic lists of varying sizes
    let mut composition = test_composition();
    let runtime = composition.runtime_handle();
    let active_tab = MutableState::with_runtime(0i32, runtime.clone());
    let list_size = MutableState::with_runtime(3usize, runtime.clone());

    thread_local! {
        static LIST_TAB_CALLED: Cell<bool> = const { Cell::new(false) };
        static LAST_ITEM_COUNT: Cell<usize> = const { Cell::new(0) };
    }

    #[composable]
    fn list_tab(count: usize) {
        LIST_TAB_CALLED.with(|c| c.set(true));
        LAST_ITEM_COUNT.with(|c| c.set(count));
        for i in 0..count {
            let id = cranpose_test_node(TestTextNode::default);
            with_node_mut(id, |node: &mut TestTextNode| {
                node.text = format!("Item {}", i);
            })
            .expect("update list item");
        }
    }

    let mut render = {
        move || {
            LIST_TAB_CALLED.with(|c| c.set(false));
            if active_tab.value() == 0 {
                list_tab(list_size.value());
            }
        }
    };

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

    // Initial render with 3 items
    composition
        .render(key, &mut render)
        .expect("initial render");
    assert!(
        LIST_TAB_CALLED.with(|c| c.get()),
        "list_tab should be called on initial render"
    );
    assert_eq!(LAST_ITEM_COUNT.with(|c| c.get()), 3);

    // Switch away
    active_tab.set_value(1);
    composition.render(key, &mut render).expect("switch away");
    assert!(
        !LIST_TAB_CALLED.with(|c| c.get()),
        "list_tab should NOT be called when tab is inactive"
    );

    // Change list size and switch back
    list_size.set_value(5);
    active_tab.set_value(0);
    composition
        .render(key, &mut render)
        .expect("switch back with larger list");
    assert!(
        LIST_TAB_CALLED.with(|c| c.get()),
        "list_tab should be called after switch back"
    );
    assert_eq!(
        LAST_ITEM_COUNT.with(|c| c.get()),
        5,
        "Should render 5 items after switching back"
    );

    // Switch away and come back with smaller list
    active_tab.set_value(1);
    composition
        .render(key, &mut render)
        .expect("switch away again");
    assert!(
        !LIST_TAB_CALLED.with(|c| c.get()),
        "list_tab should NOT be called when inactive"
    );

    list_size.set_value(2);
    active_tab.set_value(0);
    composition
        .render(key, &mut render)
        .expect("switch back with smaller list");
    assert!(
        LIST_TAB_CALLED.with(|c| c.get()),
        "list_tab should be called after second switch back"
    );
    assert_eq!(
        LAST_ITEM_COUNT.with(|c| c.get()),
        2,
        "Should render only 2 items after shrinking list"
    );
}

#[test]
fn tab_switching_with_nested_components() {
    // Test tab switching with nested component hierarchies
    let mut composition = test_composition();
    let runtime = composition.runtime_handle();
    let active_tab = MutableState::with_runtime(0i32, runtime.clone());
    let outer_counter = MutableState::with_runtime(0i32, runtime.clone());
    let inner_counter = MutableState::with_runtime(0i32, runtime.clone());

    thread_local! {
        static OUTER_RENDERS: Cell<usize> = const { Cell::new(0) };
        static INNER_RENDERS: Cell<usize> = const { Cell::new(0) };
    }

    #[composable]
    fn inner_component(counter: MutableState<i32>) {
        INNER_RENDERS.with(|c| c.set(c.get() + 1));
        let count = counter.value();
        let id = cranpose_test_node(TestTextNode::default);
        with_node_mut(id, |node: &mut TestTextNode| {
            node.text = format!("Inner: {}", count);
        })
        .expect("update inner node");
    }

    #[composable]
    fn outer_component(outer_counter: MutableState<i32>, inner_counter: MutableState<i32>) {
        println!("outer_component called");
        OUTER_RENDERS.with(|c| c.set(c.get() + 1));
        let count = outer_counter.value();
        let id = cranpose_test_node(TestTextNode::default);
        with_node_mut(id, |node: &mut TestTextNode| {
            node.text = format!("Outer: {}", count);
        })
        .expect("update outer node");

        inner_component(inner_counter);
    }

    #[composable]
    fn empty_tab() {
        // Empty tab content
    }

    let mut render = {
        move || {
            let tab = active_tab.value();
            println!("Render closure called, active_tab={}", tab);
            if tab == 0 {
                println!("About to call outer_component");
                outer_component(outer_counter, inner_counter);
            } else {
                empty_tab();
            }
        }
    };

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

    // Initial render
    OUTER_RENDERS.with(|c| c.set(0));
    INNER_RENDERS.with(|c| c.set(0));
    println!("=== INITIAL RENDER ===");
    composition
        .render(key, &mut render)
        .expect("initial render");
    assert_eq!(OUTER_RENDERS.with(|c| c.get()), 1);
    assert_eq!(INNER_RENDERS.with(|c| c.get()), 1);

    // Switch away
    active_tab.set_value(1);
    composition.render(key, &mut render).expect("switch away");

    // Switch back
    active_tab.set_value(0);
    OUTER_RENDERS.with(|c| c.set(0));
    INNER_RENDERS.with(|c| c.set(0));
    println!("Before switch back render");
    match composition.render(key, &mut render) {
        Ok(_) => println!("Render succeeded"),
        Err(e) => println!("Render failed: {:?}", e),
    }
    let outer_renders = OUTER_RENDERS.with(|c| c.get());
    let inner_renders = INNER_RENDERS.with(|c| c.get());
    println!(
        "After switch back: outer={}, inner={}",
        outer_renders, inner_renders
    );
    assert!(
        outer_renders > 0,
        "Outer should render, got {}",
        outer_renders
    );
    assert!(
        inner_renders > 0,
        "Inner should render, got {}",
        inner_renders
    );

    // Verify both scopes work after tab switch
    OUTER_RENDERS.with(|c| c.set(0));
    INNER_RENDERS.with(|c| c.set(0));
    outer_counter.set_value(5);
    let _ = composition
        .process_invalid_scopes()
        .expect("recompose outer");
    let outer_count = OUTER_RENDERS.with(|c| c.get());
    let inner_count = INNER_RENDERS.with(|c| c.get());
    assert!(
        outer_count > 0,
        "Outer scope should work after tab switch, got {}",
        outer_count
    );
    // NOTE: Inner should NOT rerender when only outer_counter changes because inner's inputs haven't changed
    // This is the expected Compose behavior - smart recomposition skips children when inputs are unchanged
    assert_eq!(
        inner_count, 0,
        "Inner should not rerender when only outer_counter changes (inner_counter is unchanged)"
    );

    // Verify inner scope independently
    OUTER_RENDERS.with(|c| c.set(0));
    INNER_RENDERS.with(|c| c.set(0));
    inner_counter.set_value(10);
    let _ = composition
        .process_invalid_scopes()
        .expect("recompose inner");
    assert_eq!(
        OUTER_RENDERS.with(|c| c.get()),
        0,
        "Outer should not rerender for inner-only change"
    );
    assert!(
        INNER_RENDERS.with(|c| c.get()) > 0,
        "Inner scope should work independently after tab switch"
    );
}

#[test]
fn restored_keyed_branch_forces_deep_regular_param_recompose() {
    let mut composition = test_composition();
    let runtime = composition.runtime_handle();
    let active_tab = MutableState::with_runtime(0i32, runtime.clone());
    let version_state = MutableState::with_runtime(0i32, runtime);
    let key = location_key(file!(), line!(), column!());

    thread_local! {
        static STORED_ACTION: RefCell<Option<Box<dyn FnMut()>>> = const { RefCell::new(None) };
        static OBSERVED_VALUE: Cell<i32> = const { Cell::new(-1) };
    }

    #[composable]
    fn callback_sink(on_action: impl FnMut() + 'static) {
        STORED_ACTION.with(|slot| {
            *slot.borrow_mut() = Some(Box::new(on_action));
        });
    }

    #[composable]
    fn action_binding(version_state: MutableState<i32>) {
        let version = version_state.value();
        crate::with_key(&version, || {
            let token = useState(|| version);
            callback_sink(move || {
                OBSERVED_VALUE.with(|cell| cell.set(token.get()));
            });
        });
    }

    #[composable]
    fn stage_three(version_state: MutableState<i32>) {
        action_binding(version_state);
    }

    #[composable]
    fn stage_two(version_state: MutableState<i32>) {
        stage_three(version_state);
    }

    #[composable]
    fn stage_one(version_state: MutableState<i32>) {
        stage_two(version_state);
    }

    #[composable]
    fn inactive_tab() {}

    let mut render = move || {
        let active = active_tab.value();
        crate::with_key(&active, || {
            if active == 0 {
                stage_one(version_state);
            } else {
                inactive_tab();
            }
        });
    };

    composition
        .render(key, &mut render)
        .expect("initial render");
    let initial_result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
        STORED_ACTION.with(|slot| {
            slot.borrow_mut()
                .as_mut()
                .expect("initial callback should be installed")();
        });
    }));
    assert!(
        initial_result.is_ok(),
        "initial deep callback should be callable"
    );
    assert_eq!(
        OBSERVED_VALUE.with(|cell| cell.get()),
        0,
        "initial callback should observe the initial keyed token",
    );

    active_tab.set_value(1);
    composition.render(key, &mut render).expect("switch away");

    version_state.set_value(1);

    active_tab.set_value(0);
    composition.render(key, &mut render).expect("switch back");

    let restored_result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
        STORED_ACTION.with(|slot| {
            slot.borrow_mut()
                .as_mut()
                .expect("restored callback should be installed")();
        });
    }));
    assert!(
        restored_result.is_ok(),
        "restored keyed branch should refresh deep callbacks instead of keeping stale ones",
    );
    assert_eq!(
        OBSERVED_VALUE.with(|cell| cell.get()),
        1,
        "deep callback should observe the restored keyed token after tab switch",
    );
}

#[test]
fn restored_branch_refreshes_deep_callback_after_recompose_via_content_lambda() {
    let mut composition = test_composition();
    let runtime = composition.runtime_handle();
    let active_tab = MutableState::with_runtime(0i32, runtime.clone());
    let hover_state = MutableState::with_runtime(0i32, runtime);
    let key = location_key(file!(), line!(), column!());

    thread_local! {
        static STORED_ACTION: RefCell<Option<Box<dyn FnMut()>>> = const { RefCell::new(None) };
        static OBSERVED_VALUE: Cell<i32> = const { Cell::new(-1) };
    }

    #[composable]
    fn callback_sink(on_action: impl FnMut() + 'static) {
        STORED_ACTION.with(|slot| {
            *slot.borrow_mut() = Some(Box::new(on_action));
        });
    }

    #[composable]
    fn content_host(content: impl FnMut() + 'static) {
        content();
    }

    #[composable]
    fn counter_tab(hover_state: MutableState<i32>) {
        let token = useState(|| 41i32);
        let _hover = hover_state.value();

        content_host(move || {
            callback_sink(move || {
                OBSERVED_VALUE.with(|cell| cell.set(token.get()));
            });
        });
    }

    #[composable]
    fn inactive_tab() {}

    let mut render = move || {
        let active = active_tab.value();
        crate::with_key(&active, || {
            if active == 0 {
                counter_tab(hover_state);
            } else {
                inactive_tab();
            }
        });
    };

    fn drain_invalid_scopes(
        composition: &mut Composition<MemoryApplier>,
        key: Key,
        render: &mut dyn FnMut(),
    ) {
        let _ = composition
            .process_invalid_scopes()
            .expect("recompose invalid scopes");
        if composition.take_root_render_request() {
            composition
                .render(key, render)
                .expect("render requested root");
        }
    }

    composition
        .render(key, &mut render)
        .expect("initial render");

    let initial_result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
        STORED_ACTION.with(|slot| {
            slot.borrow_mut()
                .as_mut()
                .expect("initial callback should be installed")();
        });
    }));
    assert!(
        initial_result.is_ok(),
        "initial callback should be callable"
    );
    assert_eq!(
        OBSERVED_VALUE.with(|cell| cell.get()),
        41,
        "initial callback should observe the initial tab-local state",
    );

    active_tab.set_value(1);
    drain_invalid_scopes(&mut composition, key, &mut render);

    active_tab.set_value(0);
    drain_invalid_scopes(&mut composition, key, &mut render);

    hover_state.set_value(1);
    drain_invalid_scopes(&mut composition, key, &mut render);

    let restored_result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
        STORED_ACTION.with(|slot| {
            slot.borrow_mut()
                .as_mut()
                .expect("restored callback should stay installed")();
        });
    }));
    assert!(
        restored_result.is_ok(),
        "restored callback should refresh through ancestor recomposition instead of reading a removed state cell",
    );
    assert_eq!(
        OBSERVED_VALUE.with(|cell| cell.get()),
        41,
        "restored callback should still observe the live tab-local state after ancestor recomposition",
    );
}

#[test]
fn restored_keyed_first_sibling_keeps_later_live_subtree_and_descendant_scope_identity() {
    let mut composition = test_composition();
    let runtime = composition.runtime_handle();
    let active_tab = MutableState::with_runtime(0i32, runtime);
    let key = location_key(file!(), line!(), column!());

    thread_local! {
        static ROOT_ID: Cell<Option<NodeId>> = const { Cell::new(None) };
        static CONTENT_ROOT_ID: Cell<Option<NodeId>> = const { Cell::new(None) };
        static COUNTER_LABEL_ID: Cell<Option<NodeId>> = const { Cell::new(None) };
        static PARITY_LABEL_ID: Cell<Option<NodeId>> = const { Cell::new(None) };
        static COUNTER_STATE: RefCell<Option<MutableState<i32>>> = const { RefCell::new(None) };
        static WAVE_STATE: RefCell<Option<MutableState<f32>>> = const { RefCell::new(None) };
        static ANIMATED_SCOPE_ID: Cell<Option<ScopeId>> = const { Cell::new(None) };
        static ANIMATED_SCOPE_RECOMPOSITIONS: Cell<usize> = const { Cell::new(0) };
    }

    fn set_text(id: NodeId, text: String) {
        with_node_mut(id, |node: &mut TestTextNode| {
            node.text = text;
        })
        .expect("update test text node");
    }

    fn drain_invalid_scopes(
        composition: &mut Composition<MemoryApplier>,
        key: Key,
        render: &mut dyn FnMut(),
    ) {
        loop {
            let recomposed = composition
                .process_invalid_scopes()
                .expect("process invalid scopes");
            let requested_root = composition.take_root_render_request();
            if requested_root {
                composition
                    .render(key, &mut *render)
                    .expect("render requested root");
            }
            if !recomposed && !requested_root {
                break;
            }
        }
    }

    #[composable]
    fn tab_button(label: &'static str) {
        let id = cranpose_test_node(TestTextNode::default);
        set_text(id, label.to_string());
    }

    #[composable]
    fn parity_branch(counter: MutableState<i32>) {
        crate::debug_label_current_scope("parity_branch");
        let is_even = counter.get() % 2 == 0;
        crate::with_key(&is_even, || {
            let id = cranpose_test_node(TestTextNode::default);
            PARITY_LABEL_ID.with(|slot| slot.set(Some(id)));
            set_text(
                id,
                if is_even {
                    "if counter % 2 == 0".to_string()
                } else {
                    "if counter % 2 != 0".to_string()
                },
            );
        });
    }

    #[composable]
    fn animated_descendant(wave: MutableState<f32>) {
        crate::debug_label_current_scope("animated_descendant");
        ANIMATED_SCOPE_RECOMPOSITIONS.with(|count| count.set(count.get() + 1));
        with_current_composer(|composer| {
            let scope = composer
                .current_recranpose_scope()
                .expect("animated scope available");
            ANIMATED_SCOPE_ID.with(|slot| slot.set(Some(scope.id())));
        });
        let id = cranpose_test_node(TestTextNode::default);
        set_text(id, format!("Wave: {:.2}", wave.get()));
    }

    #[composable]
    fn animated_subtree(wave: MutableState<f32>) {
        with_current_composer(|composer| {
            let wrapper = composer.emit_node(RecordingNode::default);
            composer.push_parent(wrapper);
            animated_descendant(wave);
            composer.pop_parent();
        });
    }

    #[composable]
    fn main_content(counter: MutableState<i32>, wave: MutableState<f32>) {
        crate::debug_label_current_scope("main_content");
        with_current_composer(|composer| {
            let content_root = composer.emit_node(RecordingNode::default);
            CONTENT_ROOT_ID.with(|slot| slot.set(Some(content_root)));
            composer.push_parent(content_root);

            let counter_label = composer.emit_node(TestTextNode::default);
            COUNTER_LABEL_ID.with(|slot| slot.set(Some(counter_label)));
            set_text(counter_label, format!("Counter: {}", counter.get()));

            animated_subtree(wave);

            composer.pop_parent();
        });
    }

    #[composable]
    fn counter_tab() {
        crate::debug_label_current_scope("counter_tab");
        let counter = useState(|| 0i32);
        let wave = useState(|| 0.0f32);
        COUNTER_STATE.with(|slot| {
            *slot.borrow_mut() = Some(counter);
        });
        WAVE_STATE.with(|slot| {
            *slot.borrow_mut() = Some(wave);
        });

        parity_branch(counter);
        main_content(counter, wave);
    }

    #[composable]
    fn inactive_tab() {
        COUNTER_STATE.with(|slot| {
            slot.borrow_mut().take();
        });
        WAVE_STATE.with(|slot| {
            slot.borrow_mut().take();
        });
        let id = cranpose_test_node(TestTextNode::default);
        set_text(id, "Inactive Tab".to_string());
    }

    let mut render = move || {
        with_current_composer(|composer| {
            let root = composer.emit_node(RecordingNode::default);
            ROOT_ID.with(|slot| slot.set(Some(root)));
            composer.push_parent(root);

            tab_button("Counter App");
            tab_button("Other Tab");

            let active = active_tab.value();
            crate::with_key(&active, || {
                if active == 0 {
                    counter_tab();
                } else {
                    inactive_tab();
                }
            });

            composer.pop_parent();
        });
    };

    composition
        .render(key, &mut render)
        .expect("initial render");

    let initial_root_id = ROOT_ID
        .with(|slot| slot.get())
        .expect("root id after initial render");
    let initial_content_root = CONTENT_ROOT_ID
        .with(|slot| slot.get())
        .expect("content root after initial render");
    let initial_counter_label = COUNTER_LABEL_ID
        .with(|slot| slot.get())
        .expect("counter label after initial render");
    let initial_animated_scope = ANIMATED_SCOPE_ID
        .with(|slot| slot.get())
        .expect("animated scope after initial render");

    let initial_root_children = composition
        .applier_mut()
        .with_node::<RecordingNode, _>(initial_root_id, |node| node.children.clone())
        .expect("root recording node after initial render");
    assert!(
        initial_root_children.contains(&initial_content_root),
        "initial root should own the live content subtree",
    );
    assert_eq!(
        composition
            .applier_mut()
            .with_node::<TestTextNode, _>(initial_counter_label, |node| node.text.clone())
            .expect("counter label text after initial render"),
        "Counter: 0",
    );

    active_tab.set_value(1);
    drain_invalid_scopes(&mut composition, key, &mut render);

    active_tab.set_value(0);
    drain_invalid_scopes(&mut composition, key, &mut render);

    let restored_counter = COUNTER_STATE
        .with(|slot| slot.borrow().as_ref().copied())
        .expect("counter state after restore");
    let restored_wave = WAVE_STATE
        .with(|slot| slot.borrow().as_ref().copied())
        .expect("wave state after restore");
    let restored_content_root = CONTENT_ROOT_ID
        .with(|slot| slot.get())
        .expect("content root after restore");
    let restored_counter_label = COUNTER_LABEL_ID
        .with(|slot| slot.get())
        .expect("counter label after restore");
    let restored_animated_scope = ANIMATED_SCOPE_ID
        .with(|slot| slot.get())
        .expect("animated scope after restore");

    assert_eq!(
        restored_counter.get(),
        0,
        "restored counter should restart at zero"
    );
    assert_ne!(
        restored_animated_scope, initial_animated_scope,
        "roundtripping the whole tab should create a fresh descendant scope",
    );
    assert_eq!(
        composition
            .applier_mut()
            .with_node::<TestTextNode, _>(restored_counter_label, |node| node.text.clone())
            .expect("counter label text after restore"),
        "Counter: 0",
    );

    restored_wave.set_value(0.5);
    restored_counter.set_value(1);
    drain_invalid_scopes(&mut composition, key, &mut render);

    let root_children = composition
        .applier_mut()
        .with_node::<RecordingNode, _>(initial_root_id, |node| node.children.clone())
        .expect("root recording node after counter update");
    let slot_groups_after_counter = composition.debug_dump_slot_table_groups();
    let slots_after_counter = composition.debug_dump_all_slots();
    let counter_label_text = composition
        .applier_mut()
        .with_node::<TestTextNode, _>(restored_counter_label, |node| node.text.clone())
        .unwrap_or_else(|err| {
            panic!(
                "counter label text after counter update: {err:?}; root_children={root_children:?}; groups={slot_groups_after_counter:?}; slots={slots_after_counter:?}"
            )
        });
    let parity_text = composition
        .applier_mut()
        .with_node::<TestTextNode, _>(
            PARITY_LABEL_ID
                .with(|slot| slot.get())
                .expect("parity label after counter update"),
            |node| node.text.clone(),
        )
        .expect("parity label text after counter update");
    let animated_scope_after_counter = ANIMATED_SCOPE_ID
        .with(|slot| slot.get())
        .expect("animated scope after counter update");
    let animated_recompositions = ANIMATED_SCOPE_RECOMPOSITIONS.with(Cell::get);

    assert!(
        root_children.contains(&restored_content_root),
        "counter flip after restore must keep the later live subtree attached to the root: root_children={root_children:?}; groups={slot_groups_after_counter:?}; slots={slots_after_counter:?}",
    );
    assert_eq!(
        counter_label_text, "Counter: 1",
        "counter flip after restore must update the later live subtree instead of dropping it",
    );
    assert_eq!(parity_text, "if counter % 2 != 0");
    assert_eq!(
        animated_scope_after_counter, restored_animated_scope,
        "descendant animated scope identity must stay stable across sibling recomposition after restore",
    );
    assert!(
        animated_recompositions >= 2,
        "wave invalidation should recompose the descendant animated scope at least once after restore",
    );
}

#[test]
fn debug_nested_component_slot_table_state() {
    // Debug test to understand slot table state during nested component recomposition
    let mut composition = test_composition();
    let runtime = composition.runtime_handle();
    let active_tab = MutableState::with_runtime(0i32, runtime.clone());
    let outer_counter = MutableState::with_runtime(0i32, runtime.clone());
    let inner_counter = MutableState::with_runtime(0i32, runtime.clone());

    thread_local! {
        static OUTER_RENDERS: Cell<usize> = const { Cell::new(0) };
        static INNER_RENDERS: Cell<usize> = const { Cell::new(0) };
    }

    #[composable]
    fn inner_component(counter: MutableState<i32>) {
        INNER_RENDERS.with(|c| c.set(c.get() + 1));
        let _count = counter.value();
    }

    #[composable]
    fn outer_component(outer_counter: MutableState<i32>, inner_counter: MutableState<i32>) {
        OUTER_RENDERS.with(|c| c.set(c.get() + 1));
        let _count = outer_counter.value();
        inner_component(inner_counter);
    }

    let mut render = {
        move || {
            if active_tab.value() == 0 {
                outer_component(outer_counter, inner_counter);
            }
        }
    };

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

    // Initial render
    composition
        .render(key, &mut render)
        .expect("initial render");
    println!("After initial render:");
    for (idx, kind) in composition.debug_dump_all_slots() {
        println!("  [{}] {}", idx, kind);
    }

    // Switch away
    active_tab.set_value(1);
    composition.render(key, &mut render).expect("switch away");
    println!("\nAfter switch away:");
    for (idx, kind) in composition.debug_dump_all_slots() {
        println!("  [{}] {}", idx, kind);
    }

    // Switch back
    active_tab.set_value(0);
    composition.render(key, &mut render).expect("switch back");
    println!("\nAfter switch back:");
    for (idx, kind) in composition.debug_dump_all_slots() {
        println!("  [{}] {}", idx, kind);
    }

    // Trigger recomposition
    OUTER_RENDERS.with(|c| c.set(0));
    INNER_RENDERS.with(|c| c.set(0));
    outer_counter.set_value(5);

    println!("\nBefore process_invalid_scopes:");
    let groups4 = composition.debug_dump_slot_table_groups();
    for (idx, key, scope, len) in &groups4 {
        println!(
            "  Group at {}: key={:?}, scope={:?}, len={}",
            idx, key, scope, len
        );
    }

    let _ = composition
        .process_invalid_scopes()
        .expect("recompose outer");

    println!("\nAfter process_invalid_scopes:");
    let groups5 = composition.debug_dump_slot_table_groups();
    for (idx, key, scope, len) in &groups5 {
        println!(
            "  Group at {}: key={:?}, scope={:?}, len={}",
            idx, key, scope, len
        );
    }

    println!(
        "\nOuter renders: {}, Inner renders: {}",
        OUTER_RENDERS.with(|c| c.get()),
        INNER_RENDERS.with(|c| c.get())
    );
}

#[test]
fn composable_macro_injects_scope_label() {
    let _guard = reset_snapshot_runtime();
    crate::set_debug_scope_tracking_override_for_tests(Some(true));
    crate::DEBUG_SCOPE_LABELS.with(|labels| labels.borrow_mut().clear());

    thread_local! {
        static AUTO_SCOPE_ID: Cell<Option<usize>> = const { Cell::new(None) };
    }

    #[composable]
    fn auto_labeled_leaf() {
        with_current_composer(|composer| {
            let scope = composer
                .current_recranpose_scope()
                .expect("auto-labeled scope should exist");
            AUTO_SCOPE_ID.with(|slot| slot.set(Some(scope.id())));
        });
    }

    let mut composition = test_composition();
    composition
        .render(location_key(file!(), line!(), column!()), &mut || {
            auto_labeled_leaf()
        })
        .expect("render auto-labeled composable");

    let scope_id = AUTO_SCOPE_ID
        .with(|slot| slot.get())
        .expect("scope id should be captured");
    assert_eq!(
        crate::debug_scope_label(scope_id),
        Some("auto_labeled_leaf")
    );

    crate::set_debug_scope_tracking_override_for_tests(None);
}

#[test]
fn tab_switching_memory_slot_reuse() {
    // Verify that slots are properly reused and not leaked during tab switches
    let mut composition = test_composition();
    let runtime = composition.runtime_handle();
    let active_tab = MutableState::with_runtime(0i32, runtime.clone());

    #[composable]
    fn tab_with_markers(tab_id: i32) {
        // Create multiple nodes to occupy slots
        for i in 0..5 {
            let id = cranpose_test_node(TestTextNode::default);
            with_node_mut(id, |node: &mut TestTextNode| {
                node.text = format!("Tab {} Item {}", tab_id, i);
            })
            .expect("update node");
        }
    }

    let mut render = {
        move || {
            tab_with_markers(active_tab.value());
        }
    };

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

    // Initial render
    composition
        .render(key, &mut render)
        .expect("initial render");

    // Perform many tab switches to check for slot leaks
    for cycle in 0..10 {
        for tab in 0..4 {
            active_tab.set_value(tab);
            composition
                .render(key, &mut render)
                .unwrap_or_else(|_| panic!("render cycle {} tab {}", cycle, tab));
        }
    }

    // If we made it here without panicking or running out of memory,
    // slots are being properly reused
    // Verify final render still works correctly
    active_tab.set_value(0);
    composition
        .render(key, &mut render)
        .expect("final render after many switches");
}

#[test]
fn tab_switching_with_state_during_switch() {
    // Test updating state while switching tabs - edge case for race conditions
    let mut composition = test_composition();
    let runtime = composition.runtime_handle();
    let active_tab = MutableState::with_runtime(0i32, runtime.clone());
    let shared_counter = MutableState::with_runtime(0i32, runtime.clone());

    thread_local! {
        static TAB0_RENDERS: Cell<usize> = const { Cell::new(0) };
        static TAB1_RENDERS: Cell<usize> = const { Cell::new(0) };
    }

    #[composable]
    fn tab_content(tab_id: i32, counter: MutableState<i32>) {
        if tab_id == 0 {
            TAB0_RENDERS.with(|c| c.set(c.get() + 1));
        } else {
            TAB1_RENDERS.with(|c| c.set(c.get() + 1));
        }
        let count = counter.value();
        let id = cranpose_test_node(TestTextNode::default);
        with_node_mut(id, |node: &mut TestTextNode| {
            node.text = format!("Tab {} Count {}", tab_id, count);
        })
        .expect("update node");
    }

    let mut render = {
        move || {
            tab_content(active_tab.value(), shared_counter);
        }
    };

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

    // Initial render
    TAB0_RENDERS.with(|c| c.set(0));
    composition
        .render(key, &mut render)
        .expect("initial render");
    assert_eq!(TAB0_RENDERS.with(|c| c.get()), 1);

    // Update state AND switch tabs simultaneously
    shared_counter.set_value(42);
    active_tab.set_value(1);
    TAB1_RENDERS.with(|c| c.set(0));
    composition
        .render(key, &mut render)
        .expect("switch with state update");

    // Tab 1 should render with updated counter
    assert!(
        TAB1_RENDERS.with(|c| c.get()) > 0,
        "Tab 1 should render with updated state"
    );

    // Switch back - scope should still work
    active_tab.set_value(0);
    TAB0_RENDERS.with(|c| c.set(0));
    composition.render(key, &mut render).expect("switch back");

    shared_counter.set_value(100);
    TAB0_RENDERS.with(|c| c.set(0));
    let _ = composition
        .process_invalid_scopes()
        .expect("recompose after state update");
    assert!(
        TAB0_RENDERS.with(|c| c.get()) > 0,
        "Tab 0 scope should still work after concurrent state/tab change"
    );
}

#[test]
fn tab_switching_with_empty_tab() {
    // Test switching to/from an empty tab (no nodes created)
    let mut composition = test_composition();
    let runtime = composition.runtime_handle();
    let active_tab = MutableState::with_runtime(0i32, runtime.clone());
    let counter = MutableState::with_runtime(0i32, runtime.clone());

    thread_local! {
        static CONTENT_RENDERS: Cell<usize> = const { Cell::new(0) };
    }

    #[composable]
    fn content_tab(counter: MutableState<i32>) {
        CONTENT_RENDERS.with(|c| c.set(c.get() + 1));
        let count = counter.value();
        let id = cranpose_test_node(TestTextNode::default);
        with_node_mut(id, |node: &mut TestTextNode| {
            node.text = format!("Content: {}", count);
        })
        .expect("update node");
    }

    #[composable]
    fn empty_tab() {
        // Intentionally empty - no nodes created
    }

    let mut render = {
        move || match active_tab.value() {
            0 => content_tab(counter),
            _ => empty_tab(),
        }
    };

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

    // Start with content
    CONTENT_RENDERS.with(|c| c.set(0));
    composition
        .render(key, &mut render)
        .expect("initial render");
    assert_eq!(CONTENT_RENDERS.with(|c| c.get()), 1);

    // Switch to empty tab
    active_tab.set_value(1);
    composition
        .render(key, &mut render)
        .expect("switch to empty");

    // Switch back to content
    active_tab.set_value(0);
    CONTENT_RENDERS.with(|c| c.set(0));
    composition
        .render(key, &mut render)
        .expect("switch back from empty");
    assert!(
        CONTENT_RENDERS.with(|c| c.get()) > 0,
        "Should render content after empty tab"
    );

    // Verify scope still works
    CONTENT_RENDERS.with(|c| c.set(0));
    counter.set_value(42);
    let _ = composition
        .process_invalid_scopes()
        .expect("recompose after empty");
    assert!(
        CONTENT_RENDERS.with(|c| c.get()) > 0,
        "Scope should work after switching from empty tab"
    );
}

#[test]
fn tab_switching_preserves_node_order() {
    // Verify that node order is preserved correctly across tab switches
    let mut composition = test_composition();
    let runtime = composition.runtime_handle();
    let active_tab = MutableState::with_runtime(0i32, runtime.clone());

    thread_local! {
        static RENDER_ORDER: RefCell<Vec<String>> = const { RefCell::new(Vec::new()) };
    }

    #[composable]
    fn ordered_tab(tab_id: i32) {
        RENDER_ORDER.with(|o| o.borrow_mut().clear());
        let prefix = if tab_id == 0 { "A" } else { "B" };
        for i in 0..3 {
            RENDER_ORDER.with(|o| o.borrow_mut().push(format!("{}_{}", prefix, i)));
            let id = cranpose_test_node(TestTextNode::default);
            with_node_mut(id, |node: &mut TestTextNode| {
                node.text = format!("{} Item {}", prefix, i);
            })
            .expect("update node");
        }
    }

    let mut render = {
        move || {
            let tab = active_tab.value();
            if tab <= 1 {
                ordered_tab(tab);
            }
        }
    };

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

    // Initial render with Tab A
    composition
        .render(key, &mut render)
        .expect("initial render");
    let order_a = RENDER_ORDER.with(|o| o.borrow().clone());
    assert_eq!(order_a, vec!["A_0", "A_1", "A_2"]);

    // Switch to Tab B
    active_tab.set_value(1);
    composition.render(key, &mut render).expect("switch to B");
    let order_b = RENDER_ORDER.with(|o| o.borrow().clone());
    assert_eq!(order_b, vec!["B_0", "B_1", "B_2"]);

    // Switch back to Tab A - order should be preserved
    active_tab.set_value(0);
    composition
        .render(key, &mut render)
        .expect("switch back to A");
    let order_a_again = RENDER_ORDER.with(|o| o.borrow().clone());
    assert_eq!(
        order_a_again,
        vec!["A_0", "A_1", "A_2"],
        "Order should be preserved after tab switch"
    );
}

// ════════════════════════════════════════════════════════════════════════════
// SlotTable Integration Tests
// ════════════════════════════════════════════════════════════════════════════

#[test]
fn composition_works_with_slot_table() {
    exercise_basic_slot_table_composition();
}

fn composable_params_are_preserved_during_recomposition() {
    thread_local! {
        static PARAM_VALUES: RefCell<Vec<usize>> = const { RefCell::new(Vec::new()) };
    }

    #[composable]
    fn param_leaf(value: usize) {
        PARAM_VALUES.with(|values| values.borrow_mut().push(value));
    }

    #[composable]
    fn param_root(value_state: MutableState<usize>) {
        param_leaf(value_state.get());
    }

    let runtime = Runtime::new(Arc::new(TestScheduler));
    let value_state = MutableState::with_runtime(1usize, runtime.handle());
    let mut composition = test_composition_with_runtime(runtime);
    let key = location_key(file!(), line!(), column!());

    PARAM_VALUES.with(|values| values.borrow_mut().clear());
    composition
        .render(key, &mut || param_root(value_state))
        .expect("initial render with selected backend");

    value_state.set(2);
    while composition
        .process_invalid_scopes()
        .expect("recompose composable parameter leaf")
    {}

    PARAM_VALUES.with(|values| {
        assert_eq!(
            values.borrow().as_slice(),
            &[1, 2],
            "slot table lost composable parameter state during recomposition",
        );
    });
}

#[test]
fn slot_table_preserves_composable_params_during_recomposition() {
    composable_params_are_preserved_during_recomposition();
}

fn exercise_basic_slot_table_composition() {
    let key = 12345u64;
    let applier = test_applier();
    let runtime = Runtime::new(Arc::new(TestScheduler));
    let mut composition = Composition::with_runtime(applier, runtime.clone());

    // Track recompositions
    let recranpose_count = Rc::new(Cell::new(0));
    let recranpose_count_clone = Rc::clone(&recranpose_count);

    // Test basic composition with groups and remembered values
    composition
        .render(key, || {
            with_current_composer(|composer| {
                composer.with_group(1, |composer| {
                    recranpose_count_clone.set(recranpose_count_clone.get() + 1);

                    // Test remember
                    let value = composer.remember(|| 123);
                    value.with(|v| assert_eq!(*v, 123));

                    // Test nested group
                    composer.with_group(2, |composer| {
                        let nested = composer.remember(|| "hello".to_string());
                        nested.with(|n| assert_eq!(n, "hello"));
                    });
                });
            });
        })
        .expect("first render");

    assert_eq!(recranpose_count.get(), 1, "Should have composed once");

    // Test recomposition preserves remembered values
    composition
        .render(key, || {
            with_current_composer(|composer| {
                composer.with_group(1, |composer| {
                    recranpose_count_clone.set(recranpose_count_clone.get() + 1);

                    // Remembered value should be preserved
                    let value = composer.remember(|| 456); // Different init, but should return 123
                    value.with(|v| assert_eq!(*v, 123, "Remembered value should be preserved"));

                    composer.with_group(2, |composer| {
                        let nested = composer.remember(|| "world".to_string());
                        nested.with(|n| {
                            assert_eq!(n, "hello", "Nested remembered value should be preserved")
                        });
                    });
                });
            });
        })
        .expect("second render");

    assert_eq!(recranpose_count.get(), 2, "Should have composed twice");
}