rustmotion 0.6.1

A CLI tool that renders motion design videos from JSON scenarios. No browser, no Node.js — just a single Rust binary.
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
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
//! Smoke tests: every component deserializes from minimal JSON and can be measured.

#[cfg(test)]
mod component_smoke {
    use crate::components::Component;

    /// Minimal JSON for each component type.
    const COMPONENT_JSONS: &[(&str, &str)] = &[
        ("text", r#"{"type":"text","content":"hello"}"#),
        ("shape", r#"{"type":"shape","shape":"rect"}"#),
        ("icon", r#"{"type":"icon","icon":"check"}"#),
        ("svg", r#"{"type":"svg","content":"<svg></svg>"}"#),
        ("counter", r#"{"type":"counter","from":0,"to":100}"#),
        (
            "cursor",
            r#"{"type":"cursor","cursor_style":"pointer","path_easing":"linear"}"#,
        ),
        ("codeblock", r#"{"type":"codeblock","code":"fn main() {}"}"#),
        ("badge", r#"{"type":"badge","text":"New"}"#),
        ("callout", r#"{"type":"callout","text":"Hello"}"#),
        (
            "chart",
            r#"{"type":"chart","chart_type":"bar","data":[{"value":10}]}"#,
        ),
        (
            "chart",
            r#"{"type":"chart","chart_type":"funnel","direction":"horizontal","data":[{"value":10}]}"#,
        ),
        ("countdown", r#"{"type":"countdown","seconds":60}"#),
        ("divider", r#"{"type":"divider"}"#),
        ("gauge", r#"{"type":"gauge","value":50}"#),
        (
            "gradient_text",
            r##"{"type":"gradient_text","content":"hello","colors":["#FF0000","#0000FF"]}"##,
        ),
        ("heatmap", r#"{"type":"heatmap","data":[[1,2],[3,4]]}"#),
        ("kbd", r#"{"type":"kbd","key":"Ctrl+C"}"#),
        (
            "list",
            r#"{"type":"list","items":[{"text":"one"},{"text":"two"}]}"#,
        ),
        ("marquee", r#"{"type":"marquee","content":"scrolling"}"#),
        (
            "particle",
            r#"{"type":"particle","particle_type":"confetti"}"#,
        ),
        ("progress", r#"{"type":"progress","value":0.5}"#),
        (
            "qr_code",
            r#"{"type":"qr_code","content":"https://example.com"}"#,
        ),
        ("success_check", r#"{"type":"success_check"}"#),
        ("number_wheel", r#"{"type":"number_wheel","value":"30222"}"#),
        (
            "pointer",
            r#"{"type":"pointer","path":[{"time":0.0,"x":0.0,"y":0.0},{"time":1.0,"x":200.0,"y":120.0}]}"#,
        ),
        ("rating", r#"{"type":"rating","value":3.5}"#),
        ("skeleton", r#"{"type":"skeleton"}"#),
        ("slider", r#"{"type":"slider","value":50}"#),
        ("sparkline", r#"{"type":"sparkline","data":[1,2,3,4,5]}"#),
        ("stat", r#"{"type":"stat","value":"42","label":"Users"}"#),
        (
            "stepper",
            r#"{"type":"stepper","orientation":"vertical","steps":[{"label":"Step 1"},{"label":"Step 2"}]}"#,
        ),
        ("switch", r#"{"type":"switch"}"#),
        (
            "rich_text",
            r#"{"type":"rich_text","spans":[{"text":"hello"}]}"#,
        ),
        (
            "table",
            r#"{"type":"table","headers":["A","B"],"rows":[["1","2"]]}"#,
        ),
        (
            "tag_cloud",
            r#"{"type":"tag_cloud","tags":[{"text":"rust","weight":1.0}]}"#,
        ),
        (
            "terminal",
            r#"{"type":"terminal","lines":[{"text":"$ echo hello"}]}"#,
        ),
        (
            "timeline",
            r#"{"type":"timeline","steps":[{"label":"Start"}]}"#,
        ),
        (
            "treemap",
            r#"{"type":"treemap","data":[{"value":10,"label":"A"}]}"#,
        ),
        // Containers
        (
            "flex",
            r#"{"type":"flex","children":[{"type":"text","content":"hi"}]}"#,
        ),
        (
            "grid",
            r#"{"type":"grid","children":[{"type":"text","content":"hi"}]}"#,
        ),
        (
            "card",
            r#"{"type":"card","children":[{"type":"text","content":"hi"}]}"#,
        ),
        (
            "container",
            r#"{"type":"container","children":[{"type":"text","content":"hi"}]}"#,
        ),
        (
            "positioned",
            r#"{"type":"positioned","children":[{"type":"text","content":"hi","x":0,"y":0}]}"#,
        ),
        // Media & diagram components (minimal required fields)
        ("image", r#"{"type":"image","src":"a.png"}"#),
        ("video", r#"{"type":"video","src":"a.mp4"}"#),
        ("gif", r#"{"type":"gif","src":"a.gif"}"#),
        (
            "caption",
            r#"{"type":"caption","words":[{"text":"hi","start":0.0,"end":1.0}]}"#,
        ),
        (
            "connector",
            r#"{"type":"connector","from":{"x":0,"y":0},"to":{"x":10,"y":10}}"#,
        ),
        ("avatar", r#"{"type":"avatar","src":"a.png"}"#),
        (
            "avatar_group",
            r#"{"type":"avatar_group","avatars":[{"src":"a.png"}]}"#,
        ),
        ("arrow", r#"{"type":"arrow","x2":10,"y2":10}"#),
        ("comparison", r#"{"type":"comparison"}"#),
        (
            "dot_map",
            r#"{"type":"dot_map","points":[{"lat":48.8,"lng":2.3}]}"#,
        ),
        ("line", r#"{"type":"line","x2":10,"y2":10}"#),
        ("lottie", r#"{"type":"lottie"}"#),
        (
            "mockup",
            r#"{"type":"mockup","device":"browser","src":"a.png"}"#,
        ),
        ("notification", r#"{"type":"notification","title":"Hi"}"#),
        ("pill_nav", r#"{"type":"pill_nav","items":["A","B"]}"#),
        ("tooltip", r#"{"type":"tooltip","text":"hi"}"#),
        // Audio reactive components
        ("audio_spectrum", r#"{"type":"audio_spectrum"}"#),
        ("waveform", r#"{"type":"waveform"}"#),
    ];

    #[test]
    fn all_components_deserialize() {
        let mut failures = Vec::new();
        for (name, json) in COMPONENT_JSONS {
            match serde_json::from_str::<Component>(json) {
                Ok(_) => {}
                Err(e) => failures.push(format!("{name}: {e}")),
            }
        }
        if !failures.is_empty() {
            panic!(
                "Failed to deserialize {} component(s):\n{}",
                failures.len(),
                failures.join("\n")
            );
        }
    }

    /// Input-only `type` aliases and the canonical tag they must serialize to.
    const COMPONENT_ALIASES: &[(&str, &str)] =
        &[("container", "div"), ("progress_bar", "progress")];

    /// The exact `shape` spellings SKILL.md documents must parse.
    ///
    /// `ShapeType` is externally tagged, and the skill used to describe the
    /// parameterised variants as `star` "(with `points`)", which reads as a
    /// sibling field and fails with `invalid type: unit variant, expected
    /// struct variant`. The generator writes what the skill says, so a wrong
    /// line there is not a documentation nit — it is a stream of invalid
    /// scenarios. Pin the spellings.
    #[test]
    fn skill_documented_shape_spellings_parse() {
        use rustmotion_core::schema::ShapeType;

        for spelling in [
            r#""rect""#,
            r#""circle""#,
            r#""rounded_rect""#,
            r#""ellipse""#,
            r#""triangle""#,
            r#"{ "star": { "points": 6 } }"#,
            r#"{ "polygon": { "sides": 6 } }"#,
            r#"{ "path": { "data": "M0 0 L10 10" } }"#,
        ] {
            serde_json::from_str::<ShapeType>(spelling).unwrap_or_else(|e| {
                panic!("SKILL.md documents `{spelling}`, which does not parse: {e}")
            });
        }

        // …and the shape the skill used to imply must still be rejected, so a
        // future edit cannot quietly reintroduce it.
        assert!(
            serde_json::from_str::<ShapeType>(r#""star""#).is_err(),
            "a bare `star` must stay an error: it is what the old wording produced"
        );
    }

    #[test]
    fn all_components_serde_round_trip() {
        // deserialize → serialize → deserialize → serialize: the canonical
        // form must itself deserialize and re-serialize identically. Locks
        // tags, aliases and field names against silent schema drift (the
        // `container` alias was once lost by a bare rename to `div`).
        let mut failures = Vec::new();
        for (name, json) in COMPONENT_JSONS {
            let parsed: Component = match serde_json::from_str(json) {
                Ok(c) => c,
                Err(e) => {
                    failures.push(format!("{name}: does not deserialize: {e}"));
                    continue;
                }
            };
            let canonical = match serde_json::to_value(&parsed) {
                Ok(v) => v,
                Err(e) => {
                    failures.push(format!("{name}: does not serialize: {e}"));
                    continue;
                }
            };
            match serde_json::from_value::<Component>(canonical.clone()) {
                Ok(reparsed) => {
                    let again = serde_json::to_value(&reparsed).unwrap();
                    if canonical != again {
                        failures.push(format!("{name}: unstable serialization"));
                    }
                }
                Err(e) => {
                    failures.push(format!("{name}: canonical form does not deserialize: {e}"));
                }
            }
        }
        if !failures.is_empty() {
            panic!(
                "{} component(s) fail serde round-trip:\n{}",
                failures.len(),
                failures.join("\n")
            );
        }
    }

    #[test]
    fn component_aliases_map_to_canonical_tags() {
        for (alias, canonical) in COMPONENT_ALIASES {
            let (_, json) = COMPONENT_JSONS
                .iter()
                .find(|(n, _)| n == canonical || n == alias)
                .unwrap_or_else(|| panic!("no corpus entry for {canonical}"));
            let mut value: serde_json::Value = serde_json::from_str(json).unwrap();
            value["type"] = serde_json::Value::String(alias.to_string());
            let parsed: Component = serde_json::from_value(value)
                .unwrap_or_else(|e| panic!("alias {alias} does not deserialize: {e}"));
            let tag = serde_json::to_value(&parsed).unwrap()["type"]
                .as_str()
                .unwrap()
                .to_string();
            assert_eq!(
                &tag, canonical,
                "alias {alias} must serialize to canonical tag {canonical}"
            );
        }
    }

    #[test]
    fn unknown_enum_value_fails_the_typed_parse() {
        // Converted stringly-typed fields (stepper.orientation,
        // chart.direction, cursor.cursor_style/path_easing) now reject
        // unknown values at the typed parse — a blocking validate error
        // instead of a silent fallback (philosophy of #33).
        let bad = r#"{"type":"stepper","orientation":"diagonal","steps":[{"label":"A"}]}"#;
        assert!(serde_json::from_str::<Component>(bad).is_err());
    }

    #[test]
    fn corpus_covers_every_component_tag() {
        // Every `type` tag advertised by the generated JSON schema must have
        // a corpus entry, so the deserialize/round-trip tests above cannot
        // silently lose coverage when a component is added.
        let schema = serde_json::to_value(schemars::schema_for!(Component)).unwrap();
        let one_of = schema["oneOf"]
            .as_array()
            .expect("Component schema should be a oneOf over tagged variants");
        let schema_tags: Vec<String> = one_of
            .iter()
            .filter_map(|v| v["properties"]["type"]["enum"][0].as_str())
            .map(str::to_string)
            .collect();
        assert!(
            !schema_tags.is_empty(),
            "no tags extracted from schema — schemars layout changed?"
        );

        let covered: std::collections::HashSet<String> = COMPONENT_JSONS
            .iter()
            .map(|(name, json)| {
                let parsed: Component =
                    serde_json::from_str(json).unwrap_or_else(|e| panic!("{name}: {e}"));
                serde_json::to_value(&parsed).unwrap()["type"]
                    .as_str()
                    .unwrap()
                    .to_string()
            })
            .collect();

        let missing: Vec<&String> = schema_tags
            .iter()
            .filter(|t| !covered.contains(*t))
            .collect();
        assert!(
            missing.is_empty(),
            "component tags with no minimal-JSON corpus entry: {missing:?}"
        );
    }

    #[test]
    fn all_components_paint_through_new_pipeline() {
        // Build a tiny scene per component (one absolutely-positioned child)
        // and run it through box_builder + run_layout + paint_tree with the
        // LegacyPaintDispatcher. The point is to catch panics in the bridge:
        // missing intrinsic measurers, misclassified containers, downcast
        // failures, etc. A non-zero pixel readback isn't required.
        use crate::components::{ChildComponent, PositionMode};
        use rustmotion_components::box_builder::build_scene;
        use rustmotion_components::legacy_dispatch::LegacyPaintDispatcher;
        use rustmotion_core::css::taffy_bridge::ConversionContext;
        use rustmotion_core::engine::layout_pass::run_layout;
        use rustmotion_core::engine::paint_pass::{paint_tree, PaintFrame};

        let mut surface =
            skia_safe::surfaces::raster_n32_premul((400, 300)).expect("raster surface");
        let canvas = surface.canvas();
        let frame = PaintFrame {
            time: 0.5,
            scenario_time: 0.5,
            frame_index: 15,
            fps: 30,
            video_width: 400,
            video_height: 300,
            scene_duration: 1.0,
            camera: None,
        };

        let mut failures = Vec::new();
        for (name, json) in COMPONENT_JSONS {
            let component: Component = match serde_json::from_str(json) {
                Ok(c) => c,
                Err(_) => continue,
            };
            let child = ChildComponent {
                component,
                position: Some(PositionMode::Absolute { x: 10.0, y: 10.0 }),
                x: None,
                y: None,
                z_index: None,
                bleed: false,
            };
            let scene = vec![child];
            let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
                let built = build_scene(&scene, (400.0, 300.0));
                let layout = run_layout(&built.root, (400.0, 300.0), &ConversionContext::default());
                let dispatcher = LegacyPaintDispatcher::for_scene(&built);
                paint_tree(canvas, &built.root, &layout, &frame, &dispatcher);
            }));
            if result.is_err() {
                failures.push(name);
            }
        }
        if !failures.is_empty() {
            panic!(
                "New pipeline panicked on {} component(s): {:?}",
                failures.len(),
                failures
            );
        }
    }

    #[test]
    fn all_components_paint_inside_flex_card() {
        // Same idea as `all_components_paint_through_new_pipeline` but each
        // component is wrapped in a flex card with no fixed size, forcing
        // taffy to use the component's intrinsic measurer (or its size field)
        // to determine the parent's dimensions. This catches regressions
        // where a component reports zero size and the card collapses.
        use crate::components::{ChildComponent, PositionMode};
        use rustmotion_components::box_builder::build_scene;
        use rustmotion_components::card::Card;
        use rustmotion_components::legacy_dispatch::LegacyPaintDispatcher;
        use rustmotion_core::css::style::{CssStyle, Edges, FlexDirection, Gap};
        use rustmotion_core::css::taffy_bridge::ConversionContext;
        use rustmotion_core::css::units::LengthPercentage;
        use rustmotion_core::engine::layout_pass::run_layout;
        use rustmotion_core::engine::paint_pass::{paint_tree, PaintFrame};

        let mut surface =
            skia_safe::surfaces::raster_n32_premul((400, 300)).expect("raster surface");
        let canvas = surface.canvas();
        let frame = PaintFrame {
            time: 0.0,
            scenario_time: 0.0,
            frame_index: 0,
            fps: 30,
            video_width: 400,
            video_height: 300,
            scene_duration: 1.0,
            camera: None,
        };

        let mut failures = Vec::new();
        for (name, json) in COMPONENT_JSONS {
            let component: Component = match serde_json::from_str(json) {
                Ok(c) => c,
                Err(_) => continue,
            };
            let inner = ChildComponent {
                component,
                position: None,
                x: None,
                y: None,
                z_index: None,
                bleed: false,
            };
            let card_style = CssStyle {
                flex_direction: Some(FlexDirection::Column),
                padding: Some(Edges::Uniform(LengthPercentage::Px(8.0))),
                gap: Some(Gap::Uniform(LengthPercentage::Px(4.0))),
                ..Default::default()
            };
            let card_child = ChildComponent {
                component: Component::Card(Card {
                    children: vec![inner],
                    timing: Default::default(),
                    style: card_style,
                    timeline: Vec::new(),
                    stagger: None,
                    time_scale: None,
                    time_offset: None,
                }),
                position: Some(PositionMode::Absolute { x: 10.0, y: 10.0 }),
                x: None,
                y: None,
                z_index: None,
                bleed: false,
            };
            let scene = vec![card_child];
            let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
                let built = build_scene(&scene, (400.0, 300.0));
                let layout = run_layout(&built.root, (400.0, 300.0), &ConversionContext::default());
                let dispatcher = LegacyPaintDispatcher::for_scene(&built);
                paint_tree(canvas, &built.root, &layout, &frame, &dispatcher);
            }));
            if result.is_err() {
                failures.push(name);
            }
        }
        if !failures.is_empty() {
            panic!(
                "New pipeline panicked for {} component(s) inside flex card: {:?}",
                failures.len(),
                failures
            );
        }
    }

    /// Routes the scene through the new pipeline
    /// (box_builder + run_layout + paint_tree).
    fn render_new(children: &[crate::components::ChildComponent], w: u32, h: u32) -> Vec<u8> {
        render_new_at(children, w, h, 0.5, 1.0)
    }

    /// Render the scene through the new pipeline at a specific time within a
    /// scene of `scene_duration` seconds. Resolves animations into CSS via
    /// `build_scene_with_anim` so the box tree carries transform/opacity/
    /// filter overrides from the animator.
    fn render_new_at(
        children: &[crate::components::ChildComponent],
        w: u32,
        h: u32,
        time: f64,
        scene_duration: f64,
    ) -> Vec<u8> {
        use rustmotion_components::box_builder::{build_scene_with_anim, BuildAnimationCtx};
        use rustmotion_components::legacy_dispatch::LegacyPaintDispatcher;
        use rustmotion_core::css::taffy_bridge::ConversionContext;
        use rustmotion_core::engine::layout_pass::run_layout;
        use rustmotion_core::engine::paint_pass::{paint_tree, PaintFrame};

        let mut surface =
            skia_safe::surfaces::raster_n32_premul((w as i32, h as i32)).expect("raster surface");
        let canvas = surface.canvas();
        canvas.clear(skia_safe::Color4f::new(0.0, 0.0, 0.0, 0.0));

        let built = build_scene_with_anim(
            children,
            (w as f32, h as f32),
            BuildAnimationCtx {
                time,
                scenario_time: time,
                scene_duration,
                fps: 30,
            },
        );
        let layout = run_layout(
            &built.root,
            (w as f32, h as f32),
            &ConversionContext::default(),
        );
        let dispatcher = LegacyPaintDispatcher::for_scene(&built);
        let frame = PaintFrame {
            time,
            scenario_time: time,
            frame_index: (time * 30.0) as u32,
            fps: 30,
            video_width: w,
            video_height: h,
            scene_duration,
            camera: None,
        };
        paint_tree(canvas, &built.root, &layout, &frame, &dispatcher);

        let row_bytes = w as usize * 4;
        let mut pixels = vec![0u8; row_bytes * h as usize];
        let info = skia_safe::ImageInfo::new(
            (w as i32, h as i32),
            skia_safe::ColorType::RGBA8888,
            skia_safe::AlphaType::Premul,
            None,
        );
        surface.read_pixels(&info, &mut pixels, row_bytes, (0, 0));
        pixels
    }

    /// Counts non-transparent pixels in an RGBA buffer (alpha > 0).
    fn nonzero_pixels(buf: &[u8]) -> usize {
        buf.chunks_exact(4).filter(|p| p[3] > 0).count()
    }

    #[test]
    fn new_pipeline_produces_pixels_for_text_in_card() {
        // The new pipeline should render at least *something* when given a
        // non-trivial scene (a card containing centered text). This is a
        // coarse smoke test — it doesn't assert pixel-perfect parity with
        // legacy (taffy and the old flex.rs differ on edge cases), only
        // that the new pipeline isn't producing a fully-blank canvas.
        let json = serde_json::json!({
            "type": "card",
            "style": { "padding": 24, "background": "#1a1a2e" },
            "children": [
                { "type": "text", "content": "Hello", "style": { "color": "#ffffff", "font-size": 48 } }
            ]
        });
        let component: Component = serde_json::from_value(json).expect("deserialize");
        let child = crate::components::ChildComponent {
            component,
            position: None,
            x: None,
            y: None,
            z_index: None,
            bleed: false,
        };
        let scene = vec![child];
        let new_buf = render_new(&scene, 400, 300);
        let lit = nonzero_pixels(&new_buf);
        assert!(
            lit > 100,
            "new pipeline produced too few non-zero pixels: {lit}"
        );
    }

    #[test]
    fn fade_in_attenuates_pixels_in_new_pipeline() {
        // A red shape with FadeIn over 1.0s. At t=0.05 the shape is ~5%
        // opacity, at t=0.95 it's ~95%. The new pipeline must drive opacity
        // through the box-tree CSS overrides, so the late frame should have
        // significantly more "lit" red than the early frame. Without the
        // animator → CssStyle bridge, both frames would look identical.
        let json = serde_json::json!({
            "type": "shape",
            "shape": "rect",
            "fill": "#ff3366",
            "style": {
                "width": "100px",
                "height": "80px",
                "animation": [{ "name": "fade_in", "duration": 1.0 }]
            }
        });
        let component: Component = serde_json::from_value(json).expect("deserialize");
        let child = crate::components::ChildComponent {
            component,
            position: Some(crate::components::PositionMode::Absolute { x: 60.0, y: 40.0 }),
            x: None,
            y: None,
            z_index: None,
            bleed: false,
        };
        let scene = vec![child];
        let early = render_new_at(&scene, 400, 300, 0.05, 1.0);
        let late = render_new_at(&scene, 400, 300, 0.95, 1.0);
        // Total red intensity (sum of red channel) — alpha-attenuated pixels
        // contribute less to this sum even when premul keeps the count up.
        let red_sum: fn(&[u8]) -> u64 = |buf| buf.chunks_exact(4).map(|p| p[0] as u64).sum();
        let early_red = red_sum(&early);
        let late_red = red_sum(&late);
        assert!(
            late_red > early_red * 3,
            "FadeIn did not amplify red over time (early={early_red}, late={late_red})"
        );
    }

    /// Build a one-child scene with a red 100x80 rect carrying `extra` fields
    /// merged into the component JSON, absolutely positioned at (60, 40).
    fn red_rect_scene(extra: serde_json::Value) -> Vec<crate::components::ChildComponent> {
        let mut json = serde_json::json!({
            "type": "shape",
            "shape": "rect",
            "fill": "#ff3366",
            "style": { "width": "100px", "height": "80px" }
        });
        for (k, v) in extra.as_object().unwrap() {
            json[k] = v.clone();
        }
        let component: Component = serde_json::from_value(json).expect("deserialize");
        vec![crate::components::ChildComponent {
            component,
            position: Some(crate::components::PositionMode::Absolute { x: 60.0, y: 40.0 }),
            x: None,
            y: None,
            z_index: None,
            bleed: false,
        }]
    }

    fn red_sum(buf: &[u8]) -> u64 {
        buf.chunks_exact(4).map(|p| p[0] as u64).sum()
    }

    #[test]
    fn start_at_hides_component_before_its_window() {
        // start_at gates *visibility* in the paint pass: nothing painted
        // before the window opens, normal paint after. It was silently
        // ignored by the box pipeline before this test existed.
        let scene = red_rect_scene(serde_json::json!({ "start_at": 2.0 }));
        let before = render_new_at(&scene, 400, 300, 1.0, 4.0);
        let after = render_new_at(&scene, 400, 300, 3.0, 4.0);
        assert_eq!(
            red_sum(&before),
            0,
            "component painted before its start_at window"
        );
        assert!(red_sum(&after) > 0, "component absent after start_at");
    }

    #[test]
    fn end_at_hides_component_after_its_window() {
        let scene = red_rect_scene(serde_json::json!({ "end_at": 2.0 }));
        let before = render_new_at(&scene, 400, 300, 1.0, 4.0);
        let after = render_new_at(&scene, 400, 300, 3.0, 4.0);
        assert!(red_sum(&before) > 0, "component absent before end_at");
        assert_eq!(
            red_sum(&after),
            0,
            "component painted after its end_at window"
        );
    }

    #[test]
    fn css_text_shadow_paints_through_the_bridge() {
        // `text-shadow` set inside `style` (the CSS dialect form) must paint
        // like the component-level field: blue text with a red css shadow
        // must produce red pixels. Before the bridge, css text_shadow was
        // parsed and silently dropped.
        let json = serde_json::json!({
            "type": "text",
            "content": "SHADOW",
            "style": {
                "font-size": "60px",
                "color": "#0000ff",
                "text-shadow": [{ "offset-x": 8, "offset-y": 8, "color": "#ff0000" }]
            }
        });
        let component: Component = serde_json::from_value(json).expect("deserialize");
        let child = crate::components::ChildComponent {
            component,
            position: Some(crate::components::PositionMode::Absolute { x: 60.0, y: 40.0 }),
            x: None,
            y: None,
            z_index: None,
            bleed: false,
        };
        let buf = render_new_at(&[child], 400, 300, 0.5, 1.0);
        assert!(
            red_sum(&buf) > 500,
            "css text-shadow painted no red pixels (red_sum={})",
            red_sum(&buf)
        );
    }

    #[test]
    fn css_filter_blur_softens_edges() {
        // A hard-edged red rect has almost no intermediate red values
        // (antialiasing only); `filter: blur(12px)` must smear the edges into
        // thousands of intermediate pixels. Before the fix, css `filter` was
        // written by the animator (blur_in/blur_out) and static styles but
        // consumed nowhere.
        let sharp = red_rect_scene(serde_json::json!({}));
        let blurred = red_rect_scene(serde_json::json!({
            "style": {
                "width": "100px",
                "height": "80px",
                "filter": [{ "fn": "blur", "radius": 12 }]
            }
        }));
        let count_mid = |buf: &[u8]| {
            buf.chunks_exact(4)
                .filter(|p| p[0] > 20 && p[0] < 220)
                .count()
        };
        let sharp_mid = count_mid(&render_new_at(&sharp, 400, 300, 0.5, 1.0));
        let blur_mid = count_mid(&render_new_at(&blurred, 400, 300, 0.5, 1.0));
        assert!(
            blur_mid > sharp_mid * 5 && blur_mid > 1000,
            "filter: blur did not soften edges (sharp={sharp_mid}, blurred={blur_mid})"
        );
    }

    #[test]
    fn css_filter_invert_flips_colors() {
        // Pure red #ff3366 inverted → #00cc99: red collapses, green rises.
        // Locks the color-matrix translation convention (0..255 space) —
        // a wrong convention would leave red pixels untouched or black.
        let inverted = red_rect_scene(serde_json::json!({
            "style": {
                "width": "100px",
                "height": "80px",
                "filter": [{ "fn": "invert", "value": 1.0 }]
            }
        }));
        let buf = render_new_at(&inverted, 400, 300, 0.5, 1.0);
        let flipped = buf
            .chunks_exact(4)
            .filter(|p| p[0] < 50 && p[1] > 150)
            .count();
        assert!(
            flipped > 3000,
            "invert(1) did not flip the rect's colors (flipped px = {flipped})"
        );
    }

    #[test]
    fn container_stagger_offsets_child_animations() {
        // flex `stagger: 0.2` with three children fading in over 0.2s each.
        // At t=0.25: child 0 finished, child 1 early in its (ease-out) fade,
        // child 2 not started (delay 0.4). The stagger field existed in the
        // schema but was wired to nothing.
        let json = serde_json::json!({
            "type": "flex",
            "stagger": 0.2,
            "style": { "flex-direction": "column", "gap": "10px", "width": "300px" },
            "children": [
                { "type": "shape", "shape": "rect", "fill": "#ff3366",
                  "style": { "width": "100px", "height": "40px",
                             "animation": [{ "name": "fade_in", "duration": 0.2 }] } },
                { "type": "shape", "shape": "rect", "fill": "#ff3366",
                  "style": { "width": "100px", "height": "40px",
                             "animation": [{ "name": "fade_in", "duration": 0.2 }] } },
                { "type": "shape", "shape": "rect", "fill": "#ff3366",
                  "style": { "width": "100px", "height": "40px",
                             "animation": [{ "name": "fade_in", "duration": 0.2 }] } }
            ]
        });
        let component: Component = serde_json::from_value(json).expect("deserialize");
        let child = crate::components::ChildComponent {
            component,
            position: Some(crate::components::PositionMode::Absolute { x: 0.0, y: 0.0 }),
            x: None,
            y: None,
            z_index: None,
            bleed: false,
        };
        let buf = render_new_at(&[child], 300, 200, 0.25, 2.0);
        // Children flow in a column with a 10px gap: bands 0..40, 50..90, 100..140.
        let band_red = |y0: usize, y1: usize| -> u64 {
            (y0..y1)
                .flat_map(|y| (0..300).map(move |x| (y * 300 + x) * 4))
                .map(|i| buf[i] as u64)
                .sum()
        };
        let (b0, b1, b2) = (band_red(0, 40), band_red(50, 90), band_red(100, 140));
        assert!(b0 > 0, "first child must be visible at t=0.25");
        assert!(
            b1 > 0 && b1 * 4 < b0 * 3,
            "second child must be partially faded (b0={b0}, b1={b1})"
        );
        assert_eq!(
            b2, 0,
            "third child must not have started (stagger delay 0.4 > t=0.25)"
        );
    }

    #[test]
    fn style_state_snaps_without_transition() {
        // A timeline step carrying a `style` state hard-cuts at `at` when no
        // transition is configured: full red before, 20% right after.
        let scene = red_rect_scene(serde_json::json!({
            "timeline": [{ "at": 1.0, "style": { "opacity": 0.2 } }]
        }));
        let before = red_sum(&render_new_at(&scene, 400, 300, 0.5, 4.0));
        let after = red_sum(&render_new_at(&scene, 400, 300, 1.5, 4.0));
        assert!(before > 0, "rect must be visible before the state");
        let ratio = after as f64 / before as f64;
        assert!(
            (ratio - 0.2).abs() < 0.08,
            "state must snap to opacity 0.2 (ratio={ratio:.3})"
        );
    }

    #[test]
    fn style_state_smooths_with_transition() {
        // Same state change with `transition: {duration: 1, easing: linear}`:
        // untouched at t=0.5, halfway (~0.6) at t=1.5, settled (0.2) at t=2.5.
        let scene = red_rect_scene(serde_json::json!({
            "timeline": [{ "at": 1.0, "style": { "opacity": 0.2 } }],
            "style": {
                "width": "100px",
                "height": "80px",
                "transition": { "duration": 1.0, "easing": "linear" }
            }
        }));
        let full = red_sum(&render_new_at(&scene, 400, 300, 0.5, 4.0)) as f64;
        let mid = red_sum(&render_new_at(&scene, 400, 300, 1.5, 4.0)) as f64;
        let settled = red_sum(&render_new_at(&scene, 400, 300, 2.5, 4.0)) as f64;
        assert!(full > 0.0);
        let mid_ratio = mid / full;
        let settled_ratio = settled / full;
        assert!(
            (mid_ratio - 0.6).abs() < 0.1,
            "mid-transition must sit near opacity 0.6 (ratio={mid_ratio:.3})"
        );
        assert!(
            (settled_ratio - 0.2).abs() < 0.08,
            "transition must settle at opacity 0.2 (ratio={settled_ratio:.3})"
        );
    }

    #[test]
    fn color_state_transitions_smoothly_on_text() {
        // Text color red → blue at t=1 with a 1s transition: pure red before,
        // both channels mid-way, pure blue after.
        let json = serde_json::json!({
            "type": "text",
            "content": "COLOR",
            "timeline": [{ "at": 1.0, "style": { "color": "#0000ff" } }],
            "style": {
                "font-size": "72px",
                "color": "#ff0000",
                "transition": { "duration": 1.0, "easing": "linear" }
            }
        });
        let component: Component = serde_json::from_value(json).expect("deserialize");
        let child = crate::components::ChildComponent {
            component,
            position: Some(crate::components::PositionMode::Absolute { x: 40.0, y: 40.0 }),
            x: None,
            y: None,
            z_index: None,
            bleed: false,
        };
        let scene = vec![child];
        let blue_sum = |buf: &[u8]| -> u64 { buf.chunks_exact(4).map(|p| p[2] as u64).sum() };
        let before = render_new_at(&scene, 400, 300, 0.5, 4.0);
        let mid = render_new_at(&scene, 400, 300, 1.5, 4.0);
        let after = render_new_at(&scene, 400, 300, 2.5, 4.0);
        assert!(red_sum(&before) > 500 && blue_sum(&before) < red_sum(&before) / 10);
        assert!(
            red_sum(&mid) > 500 && blue_sum(&mid) > 500,
            "mid-transition must blend red and blue (r={}, b={})",
            red_sum(&mid),
            blue_sum(&mid)
        );
        assert!(blue_sum(&after) > 500 && red_sum(&after) < blue_sum(&after) / 10);
    }

    #[test]
    fn keyframes_effect_honors_its_delay() {
        // KeyframesConfig.delay was silently ignored by the resolver: an
        // opacity ramp 0→1 over 1s with delay 2 must keep the rect invisible
        // at t=1 and fully visible at t=3.5.
        let scene = red_rect_scene(serde_json::json!({
            "style": {
                "width": "100px",
                "height": "80px",
                "animation": [{
                    "name": "keyframes",
                    "delay": 2.0,
                    "keyframes": [{
                        "property": "opacity",
                        "keyframes": [
                            { "time": 0.0, "value": 0.0 },
                            { "time": 1.0, "value": 1.0 }
                        ]
                    }]
                }]
            }
        }));
        let early = red_sum(&render_new_at(&scene, 400, 300, 1.0, 4.0));
        let late = red_sum(&render_new_at(&scene, 400, 300, 3.5, 4.0));
        assert_eq!(early, 0, "keyframes must not start before their delay");
        assert!(late > 0, "keyframes must complete after delay + ramp");
    }

    #[test]
    fn timeline_steps_trigger_delayed_animations() {
        // A timeline step resolves as its animations with `delay += at`:
        // fade_in(1s) at t=2 must be ~5% opaque at t=2.05 and ~95% at t=2.95.
        let scene = red_rect_scene(serde_json::json!({
            "timeline": [{ "at": 2.0, "animation": [{ "name": "fade_in", "duration": 1.0 }] }]
        }));
        let early = render_new_at(&scene, 400, 300, 2.05, 4.0);
        let late = render_new_at(&scene, 400, 300, 2.95, 4.0);
        let (early_red, late_red) = (red_sum(&early), red_sum(&late));
        assert!(
            late_red > early_red * 3,
            "timeline fade_in did not amplify red over its window (early={early_red}, late={late_red})"
        );
    }

    /// X centroid of lit pixels, weighted by red intensity.
    /// Returns None when no pixels are lit.
    fn red_centroid_x(buf: &[u8], width: u32) -> Option<f64> {
        let mut sum_wx = 0.0_f64;
        let mut sum_w = 0.0_f64;
        for (i, p) in buf.chunks_exact(4).enumerate() {
            let x = (i as u32 % width) as f64;
            let w = p[0] as f64;
            sum_wx += w * x;
            sum_w += w;
        }
        if sum_w == 0.0 {
            None
        } else {
            Some(sum_wx / sum_w)
        }
    }

    #[test]
    fn scale_in_grows_lit_area_in_new_pipeline() {
        // ScaleIn animates `scale` from 0.0 → 1.08 → 1.0 and opacity 0 → 1.
        // At t=0.05 the shape is microscopic; at t=0.95 it's at full size.
        // This exercises the transform: scale(...) path of the CSS bridge.
        let json = serde_json::json!({
            "type": "shape",
            "shape": "rect",
            "fill": "#ff3366",
            "style": {
                "width": "100px",
                "height": "80px",
                "animation": [{ "name": "scale_in", "duration": 1.0 }]
            }
        });
        let component: Component = serde_json::from_value(json).expect("deserialize");
        let child = crate::components::ChildComponent {
            component,
            position: Some(crate::components::PositionMode::Absolute { x: 150.0, y: 110.0 }),
            x: None,
            y: None,
            z_index: None,
            bleed: false,
        };
        let scene = vec![child];
        let early = render_new_at(&scene, 400, 300, 0.05, 1.0);
        let late = render_new_at(&scene, 400, 300, 0.95, 1.0);
        let early_lit = nonzero_pixels(&early);
        let late_lit = nonzero_pixels(&late);
        // At t=0.95 the box is ~100x80 = 8000 px. At t=0.05, scale<0.1 and
        // opacity<0.2, so almost nothing is drawn. The bridge is doing its
        // job if the late frame fills out an order of magnitude more area.
        assert!(
            late_lit > early_lit * 10,
            "ScaleIn did not grow over time (early_lit={early_lit}, late_lit={late_lit})"
        );
        assert!(late_lit > 5000, "ScaleIn final frame too small: {late_lit}");
    }

    #[test]
    fn slide_in_left_translates_centroid_in_new_pipeline() {
        // SlideInLeft animates position.x from -200 → 0 (relative to the
        // shape's anchor). The new pipeline must turn that into
        // transform: translateX(...) so the rendered centroid moves
        // rightward with time.
        let json = serde_json::json!({
            "type": "shape",
            "shape": "rect",
            "fill": "#ff3366",
            "style": {
                "width": "60px",
                "height": "60px",
                "animation": [{ "name": "slide_in_left", "duration": 1.0 }]
            }
        });
        let component: Component = serde_json::from_value(json).expect("deserialize");
        let child = crate::components::ChildComponent {
            component,
            position: Some(crate::components::PositionMode::Absolute { x: 250.0, y: 120.0 }),
            x: None,
            y: None,
            z_index: None,
            bleed: false,
        };
        let scene = vec![child];
        let early = render_new_at(&scene, 500, 300, 0.05, 1.0);
        let late = render_new_at(&scene, 500, 300, 0.95, 1.0);
        let early_cx = red_centroid_x(&early, 500).expect("early frame has lit pixels");
        let late_cx = red_centroid_x(&late, 500).expect("late frame has lit pixels");
        // EaseOutCubic at t=0.05 leaves the shape close to the start (-200
        // offset → centroid near x≈80). At t=0.95 it's near origin
        // (centroid ≈ 280). A delta of at least 100 px guarantees the
        // translate is being honoured by paint_pass.
        let dx = late_cx - early_cx;
        assert!(
            dx > 100.0,
            "SlideInLeft centroid did not move right (early_cx={early_cx:.1}, late_cx={late_cx:.1}, dx={dx:.1})"
        );
    }

    // ──────────────────────────────────────────────────────────────────────────
    // Intrinsic-measure tests for Terminal, Table, Codeblock
    //
    // Each test wraps the component in a flex card with NO explicit width/height,
    // runs the full layout pass, and asserts that the component's BoxLayout has
    // sensible non-zero dimensions derived from its content.
    // ──────────────────────────────────────────────────────────────────────────

    /// Helper: build a scene containing one flex card wrapping one child (the
    /// component under test, no explicit size), run layout at 1920×1080, and
    /// return the BoxLayout of the *first child of the card* (the component).
    fn layout_for_unsized_component_in_flex_card(
        component_json: serde_json::Value,
    ) -> rustmotion_core::engine::layout_pass::BoxLayout {
        use crate::components::{ChildComponent, PositionMode};
        use rustmotion_components::box_builder::build_scene;
        use rustmotion_components::card::Card;
        use rustmotion_core::css::style::{CssStyle, FlexDirection};
        use rustmotion_core::css::taffy_bridge::ConversionContext;
        use rustmotion_core::engine::layout_pass::run_layout;

        let component: Component = serde_json::from_value(component_json).expect("deserialize");
        let inner = ChildComponent {
            component,
            position: None,
            x: None,
            y: None,
            z_index: None,
            bleed: false,
        };

        let card_style = CssStyle {
            flex_direction: Some(FlexDirection::Column),
            ..Default::default()
        };
        let card_child = ChildComponent {
            component: Component::Card(Card {
                children: vec![inner],
                timing: Default::default(),
                style: card_style,
                timeline: Vec::new(),
                stagger: None,
                time_scale: None,
                time_offset: None,
            }),
            position: Some(PositionMode::Absolute { x: 0.0, y: 0.0 }),
            x: None,
            y: None,
            z_index: None,
            bleed: false,
        };
        let scene = vec![card_child];
        let built = build_scene(&scene, (1920.0, 1080.0));
        let layout = run_layout(&built.root, (1920.0, 1080.0), &ConversionContext::default());

        // The scene root is node 0 (the implicit scene root), the card is node 1,
        // and the component is node 2 (first child of the card).
        // Walk the tree to find node 2.
        let component_node_id = {
            // root → card_child (id=1) → card's inner child (the component, id=2)
            built
                .root
                .children
                .first() // card_child box node
                .and_then(|card_node| card_node.children.first()) // component box node
                .map(|n| n.id)
                .expect("component node id must exist")
        };
        layout
            .get(component_node_id)
            .copied()
            .expect("component must have a layout entry")
    }

    #[test]
    fn terminal_intrinsic_height_reflects_line_count() {
        // Terminal with 3 lines, default font size (14px), line-height ratio
        // 22/14 ≈ 1.57 → line_height = ceil(14 * 22/14) = 22.
        // Expected minimum height: chrome (36) + padding_top (16) + 3 * 22 + padding_bottom (16) = 134.
        // We assert ≥ 3 * line_height_min = 3 * 22 = 66 (conservative: chrome may be excluded
        // in some configs, let the intrinsic beat 0).
        let json = serde_json::json!({
            "type": "terminal",
            "lines": [
                { "text": "echo hello", "line_type": "command" },
                { "text": "hello", "line_type": "output" },
                { "text": "echo world", "line_type": "command" }
            ]
        });
        let layout = layout_for_unsized_component_in_flex_card(json);
        assert!(
            layout.height > 0.0,
            "terminal height should be > 0, got {}",
            layout.height
        );
        // With chrome (36) + 2*padding (32) + 3 lines * line_height (22) ≥ 134
        let min_expected = 3.0 * 22.0; // conservative: at least 3 line-heights
        assert!(
            layout.height >= min_expected,
            "terminal height {} should be ≥ {} (3 × line_height)",
            layout.height,
            min_expected
        );
        assert!(
            layout.width > 0.0,
            "terminal width should be > 0, got {}",
            layout.width
        );
    }

    #[test]
    fn table_intrinsic_height_reflects_row_count() {
        // Table with 1 header row + 2 data rows. Default font_size = 14, row_height = 14 * 2.5 = 35.
        // Total height = 3 rows * 35 = 105.
        let json = serde_json::json!({
            "type": "table",
            "headers": ["Name", "Value"],
            "rows": [
                ["Alice", "42"],
                ["Bob", "99"]
            ]
        });
        let layout = layout_for_unsized_component_in_flex_card(json);
        assert!(
            layout.height > 0.0,
            "table height should be > 0, got {}",
            layout.height
        );
        // 3 rows (1 header + 2 data) × row_height (35) = 105
        let expected_min = 3.0 * 35.0;
        assert!(
            layout.height >= expected_min,
            "table height {} should be ≥ {} (3 × row_height)",
            layout.height,
            expected_min
        );
        assert!(
            layout.width > 0.0,
            "table width should be > 0, got {}",
            layout.width
        );
    }

    #[test]
    fn codeblock_intrinsic_height_reflects_line_count() {
        // Codeblock with 3 lines of code, default font_size = 14.
        // Default padding = 16px each side. line_height = style.line_height_for(14) ≈ 14 * 1.5 = 21.
        // Expected: 3 lines * line_height + pad_top + pad_bottom ≥ 3 * 14 = 42.
        let json = serde_json::json!({
            "type": "codeblock",
            "code": "fn main() {\n    println!(\"hello\");\n}"
        });
        let layout = layout_for_unsized_component_in_flex_card(json);
        assert!(
            layout.height > 0.0,
            "codeblock height should be > 0, got {}",
            layout.height
        );
        let min_expected = 3.0 * 14.0; // very conservative: at least 3 × font_size
        assert!(
            layout.height >= min_expected,
            "codeblock height {} should be ≥ {} (3 × font_size)",
            layout.height,
            min_expected
        );
        assert!(
            layout.width > 0.0,
            "codeblock width should be > 0, got {}",
            layout.width
        );
    }

    // ─── Time Remapping Tests ────────────────────────────────────────────────────

    /// Build a scene with one flex container (time_scale, time_offset) wrapping
    /// a red rect with fade_in, absolutely positioned at (0, 0).
    fn flex_fade_in_scene(
        time_scale: Option<f64>,
        time_offset: Option<f64>,
        fade_duration: f64,
    ) -> Vec<crate::components::ChildComponent> {
        let json = serde_json::json!({
            "type": "flex",
            "time_scale": time_scale,
            "time_offset": time_offset,
            "style": { "width": "400px", "height": "300px" },
            "children": [{
                "type": "shape",
                "shape": "rect",
                "fill": "#ff0000",
                "style": {
                    "width": "200px",
                    "height": "200px",
                    "animation": [{ "name": "fade_in", "duration": fade_duration }]
                }
            }]
        });
        let component: Component = serde_json::from_value(json).expect("deserialize flex");
        vec![crate::components::ChildComponent {
            component,
            position: Some(crate::components::PositionMode::Absolute { x: 0.0, y: 0.0 }),
            x: None,
            y: None,
            z_index: None,
            bleed: false,
        }]
    }

    #[test]
    fn time_scale_slows_fade_in_animation() {
        // A flex container with time_scale 0.5 wraps a red shape with fade_in duration=1s.
        // At t_global=1s the child should be at t_local=0.5s → half-faded.
        // At t_global=2s the child should be at t_local=1.0s → fully visible.
        // Without remap, at t_global=1s the fade would be complete.
        // With scale 0.5, it should still be in-progress at t=1s.
        let at_1s = render_new_at(
            &flex_fade_in_scene(Some(0.5), None, 1.0),
            400,
            300,
            1.0,
            4.0,
        );
        let at_2s = render_new_at(
            &flex_fade_in_scene(Some(0.5), None, 1.0),
            400,
            300,
            2.0,
            4.0,
        );

        let red_at_1 = red_sum(&at_1s);
        let red_at_2 = red_sum(&at_2s);
        assert!(
            red_at_2 > red_at_1 + 1000,
            "time_scale=0.5: at t=1s (half-faded) red_sum={} should be less than at t=2s (fully visible) red_sum={}",
            red_at_1, red_at_2
        );
        assert!(
            red_at_2 > 5000,
            "at t=2s (t_local=1.0s, fade complete) shape should be fully visible, red_sum={}",
            red_at_2
        );
    }

    #[test]
    fn time_offset_delays_animation_start() {
        // A flex container with time_offset=1.0 wraps a shape with fade_in duration=0.5s.
        // t_local = (t_global - 1.0) * 1.0
        // At t_global=0.9s: t_local=-0.1s → nothing visible yet.
        // At t_global=1.6s: t_local=0.6s → fade complete (duration=0.5).
        let before = render_new_at(
            &flex_fade_in_scene(None, Some(1.0), 0.5),
            400,
            300,
            0.9,
            4.0,
        );
        let after = render_new_at(
            &flex_fade_in_scene(None, Some(1.0), 0.5),
            400,
            300,
            1.6,
            4.0,
        );

        let red_before = red_sum(&before);
        let red_after = red_sum(&after);

        assert!(
            red_before < 1000,
            "time_offset=1.0: at t=0.9s animation hasn't started, expected near-zero red, got {}",
            red_before
        );
        assert!(
            red_after > 5000,
            "time_offset=1.0: at t=1.6s fade should be complete, expected high red, got {}",
            red_after
        );
    }

    #[test]
    fn start_at_window_respected_under_time_scale() {
        // Child with start_at=1.0 inside flex with time_scale=0.5.
        // start_at is in local time. In global: t_global = start_at / scale = 1.0/0.5 = 2.0s.
        // At t_global=1.5s: child should still be invisible (local time=0.75 < start_at=1.0).
        // At t_global=2.5s: child should be visible (local time=1.25 > start_at=1.0).
        let json = serde_json::json!({
            "type": "flex",
            "time_scale": 0.5,
            "style": { "width": "400px", "height": "300px" },
            "children": [{
                "type": "shape",
                "shape": "rect",
                "fill": "#ff0000",
                "start_at": 1.0,
                "style": { "width": "200px", "height": "200px" }
            }]
        });
        let make_scene = || {
            let component: Component = serde_json::from_value(json.clone()).expect("deserialize");
            vec![crate::components::ChildComponent {
                component,
                position: Some(crate::components::PositionMode::Absolute { x: 0.0, y: 0.0 }),
                x: None,
                y: None,
                z_index: None,
                bleed: false,
            }]
        };

        let invisible = render_new_at(&make_scene(), 400, 300, 1.5, 6.0);
        let visible = render_new_at(&make_scene(), 400, 300, 2.5, 6.0);

        assert!(
            red_sum(&invisible) < 500,
            "start_at=1.0 with scale=0.5: at t_global=1.5 (t_local=0.75) shape should be invisible, red_sum={}",
            red_sum(&invisible)
        );
        assert!(
            red_sum(&visible) > 5000,
            "start_at=1.0 with scale=0.5: at t_global=2.5 (t_local=1.25 > start_at=1.0) shape should be visible, red_sum={}",
            red_sum(&visible)
        );
    }

    #[test]
    fn cascaded_time_scale_compounds() {
        // Outer flex: time_scale=0.5. Inner flex: time_scale=0.5.
        // Effective scale on the shape = 0.5 * 0.5 = 0.25.
        // Shape has fade_in duration=1s.
        // t_local = t_global * 0.25 (both offsets are 0).
        // At t_global=3.0s: t_local=0.75s → still fading (not at 1s yet).
        // At t_global=4.5s: t_local=1.125s → fade complete.
        let json = serde_json::json!({
            "type": "flex",
            "time_scale": 0.5,
            "style": { "width": "400px", "height": "300px" },
            "children": [{
                "type": "flex",
                "time_scale": 0.5,
                "children": [{
                    "type": "shape",
                    "shape": "rect",
                    "fill": "#ff0000",
                    "style": {
                        "width": "100px",
                        "height": "100px",
                        "animation": [{ "name": "fade_in", "duration": 1.0 }]
                    }
                }]
            }]
        });
        let make_scene = || {
            let component: Component = serde_json::from_value(json.clone()).expect("deserialize");
            vec![crate::components::ChildComponent {
                component,
                position: Some(crate::components::PositionMode::Absolute { x: 0.0, y: 0.0 }),
                x: None,
                y: None,
                z_index: None,
                bleed: false,
            }]
        };

        // At t_global=3s: t_local = 3 * 0.25 = 0.75s → fade still incomplete
        let at_3s = render_new_at(&make_scene(), 400, 300, 3.0, 6.0);
        // At t_global=4.5s: t_local = 4.5 * 0.25 = 1.125s → fade complete
        let at_4_5s = render_new_at(&make_scene(), 400, 300, 4.5, 6.0);

        let red_3 = red_sum(&at_3s);
        let red_4_5 = red_sum(&at_4_5s);

        assert!(
            red_4_5 > red_3 + 500,
            "cascaded scale 0.5×0.5=0.25: at t=3s red={} should be less than t=4.5s red={}",
            red_3,
            red_4_5
        );
        assert!(
            red_4_5 > 2000,
            "cascaded scale: at t=4.5s (t_local=1.125s) fade should be complete, red_sum={}",
            red_4_5
        );
    }

    #[test]
    fn time_scale_affects_internal_paint_ctx_time() {
        // A line with draw_in duration=1s inside a flex with time_scale=0.5.
        // At t_global=1s (t_local=0.5s): the line should be about half-drawn.
        // Without remapping, the line would be fully drawn at t=1s.
        // We compare draw extent vs a reference without remap.
        let make_scene = |time_scale: Option<f64>| {
            let json = serde_json::json!({
                "type": "flex",
                "time_scale": time_scale,
                "children": [{
                    "type": "line",
                    "x1": 0.0,
                    "y1": 150.0,
                    "x2": 400.0,
                    "y2": 150.0,
                    "width": 8.0,
                    "color": "#ff0000",
                    "style": {
                        "animation": [{ "name": "draw_in", "duration": 1.0 }]
                    }
                }]
            });
            let component: Component = serde_json::from_value(json).expect("deserialize flex+line");
            vec![crate::components::ChildComponent {
                component,
                position: Some(crate::components::PositionMode::Absolute { x: 0.0, y: 0.0 }),
                x: None,
                y: None,
                z_index: None,
                bleed: false,
            }]
        };

        // At t_global=1s: without remap the line is fully drawn; with scale=0.5 it should be ~half.
        let without_remap = render_new_at(&make_scene(None), 400, 300, 1.0, 2.0);
        let with_remap = render_new_at(&make_scene(Some(0.5)), 400, 300, 1.0, 2.0);

        let red_no_remap = red_sum(&without_remap);
        let red_remapped = red_sum(&with_remap);

        assert!(
            red_no_remap > red_remapped + 500,
            "line draw_in with scale=0.5: at t=1s remapped line (red={}) should have less extent \
             than non-remapped (red={})",
            red_remapped,
            red_no_remap
        );
        assert!(
            red_remapped > 200,
            "line draw_in with scale=0.5: at t=1s (t_local=0.5s) line should be partially drawn, red_sum={}",
            red_remapped
        );
    }

    // ─── time-container chantier: composition + freeze (issue #164) ──────────

    #[test]
    fn nested_time_scale_and_offset_compose_and_a_frozen_global_time_freezes_the_whole_subtree() {
        // Direct answer to "a card with time_scale: 2 containing a flex with
        // time_offset: -1: what does the grandchild see?" — per the
        // documented composition rule (`t_local = (t_parent - offset) *
        // scale`, applied per level,
        // .claude/skills/rustmotion/rules/time-remapping.md):
        //   card:  t_card = (T - 0) * 2        = 2T
        //   flex:  t_flex = (t_card - (-1)) * 1 = 2T + 1
        // A shape with a 4s fade_in nested two levels deep should be 25%
        // faded at T=0 (t_local=1) and 50% faded at T=0.5 (t_local=2).
        let json = serde_json::json!({
            "type": "card",
            "time_scale": 2.0,
            "style": { "width": "400px", "height": "300px" },
            "children": [{
                "type": "flex",
                "time_offset": -1.0,
                "children": [{
                    "type": "shape",
                    "shape": "rect",
                    "fill": "#ff0000",
                    "style": {
                        "width": "200px",
                        "height": "200px",
                        "animation": [{ "name": "fade_in", "duration": 4.0 }]
                    }
                }]
            }]
        });
        let make_scene = || {
            let component: Component = serde_json::from_value(json.clone()).expect("deserialize");
            vec![crate::components::ChildComponent {
                component,
                position: Some(crate::components::PositionMode::Absolute { x: 0.0, y: 0.0 }),
                x: None,
                y: None,
                z_index: None,
                bleed: false,
            }]
        };

        let at_t0 = render_new_at(&make_scene(), 400, 300, 0.0, 6.0);
        let at_t_half = render_new_at(&make_scene(), 400, 300, 0.5, 6.0);
        let red_t0 = red_sum(&at_t0);
        let red_t_half = red_sum(&at_t_half);
        assert!(
            red_t_half > red_t0 + 500,
            "card time_scale=2 > flex time_offset=-1: grandchild local time is 2*T+1; T=0.5 \
             (local=2, 50% faded, red={}) must be more opaque than T=0 (local=1, 25% faded, red={})",
            red_t_half,
            red_t0
        );

        // Simulate what a Scene-level `freeze_at: 0.5` does *upstream* of
        // this box tree: `scene.rs` clamps the GLOBAL time to `freeze_at`
        // once, before it ever reaches `build_scene_with_anim` — a nested
        // time_scale/time_offset subtree never sees a global time past the
        // freeze point in the first place. Clamping *before* the affine
        // composition is equivalent to clamping *after* it whenever every
        // ancestor's `time_scale` is positive (which the validator already
        // enforces — see `validate_schema.rs`'s `time_scale must be > 0`).
        // So: two different raw global times that both get clamped to the
        // same freeze point must render identically, however deep the
        // nesting — this is *why* freeze_at needs no changes to
        // `box_builder.rs`'s time_remap composition at all, only a single,
        // upstream clamp of the scene's own global time (see `scene.rs`'s
        // `SceneTime`).
        let freeze_at = 0.5;
        let frozen_a = render_new_at(&make_scene(), 400, 300, 1.5_f64.min(freeze_at), 6.0);
        let frozen_b = render_new_at(&make_scene(), 400, 300, 2.5_f64.min(freeze_at), 6.0);
        assert_eq!(
            frozen_a, frozen_b,
            "two different global times both clamped to the same freeze_at before reaching a \
             nested time_scale/time_offset subtree must render pixel-identical"
        );
        assert_eq!(
            frozen_a, at_t_half,
            "clamping T to freeze_at=0.5 must render exactly like rendering at T=0.5 directly \
             (frozen == the frame at the freeze point, not some other value)"
        );
    }
}

// ──────────────────────────────────────────────────────────────────────────────
// SVG draw-on (draw_progress) pixel tests
// ──────────────────────────────────────────────────────────────────────────────
//
// The SVG under test contains two horizontal strokes spatially separated:
//   - path 1: a line from (10,30) to (90,30)   → top half of the 100×100 box
//   - path 2: a line from (10,70) to (90,70)   → bottom half of the 100×100 box
//
// Both paths have an explicit stroke (white, width 4) so the draw-on mode
// picks up the SVG stroke color rather than the fill-fallback.
//
// The component is rendered in a 100×100 pixel canvas.
#[cfg(test)]
mod svg_draw_on_tests {
    use crate::components::{ChildComponent, Component, PositionMode};
    use rustmotion_components::box_builder::{build_scene_with_anim, BuildAnimationCtx};
    use rustmotion_components::legacy_dispatch::LegacyPaintDispatcher;
    use rustmotion_core::css::taffy_bridge::ConversionContext;
    use rustmotion_core::engine::layout_pass::run_layout;
    use rustmotion_core::engine::paint_pass::{paint_tree, PaintFrame};

    // SVG with two horizontal stroked paths, vertically separated.
    // Path 1 at y=30 (top region), Path 2 at y=70 (bottom region).
    const TWO_STROKE_SVG: &str = r#"<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100">
  <line x1="10" y1="30" x2="90" y2="30" stroke="white" stroke-width="4"/>
  <line x1="10" y1="70" x2="90" y2="70" stroke="white" stroke-width="4"/>
</svg>"#;

    // SVG with two filled rects (no stroke) — draw mode should trace their contour.
    const TWO_FILL_SVG: &str = r#"<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100">
  <rect x="10" y="10" width="80" height="20" fill="white"/>
  <rect x="10" y="70" width="80" height="20" fill="white"/>
</svg>"#;

    /// Render an SVG component with the given JSON fields at the specified
    /// draw_progress (via animation) and return the raw RGBA8888 pixels.
    fn render_svg_at(svg_data: &str, extra_fields: serde_json::Value, progress: f64) -> Vec<u8> {
        let mut json = serde_json::json!({
            "type": "svg",
            "data": svg_data,
            "style": { "width": "100px", "height": "100px" }
        });
        // Merge extra_fields
        if let serde_json::Value::Object(map) = extra_fields {
            for (k, v) in map {
                json[k] = v;
            }
        }
        let component: Component = serde_json::from_value(json).expect("svg deserialize");
        let child = ChildComponent {
            component,
            position: Some(PositionMode::Absolute { x: 0.0, y: 0.0 }),
            x: None,
            y: None,
            z_index: None,
            bleed: false,
        };
        let scene = vec![child];

        let w = 100u32;
        let h = 100u32;
        let scene_duration = 1.0f64;

        let mut surface =
            skia_safe::surfaces::raster_n32_premul((w as i32, h as i32)).expect("surface");
        let canvas = surface.canvas();
        canvas.clear(skia_safe::Color4f::new(0.0, 0.0, 0.0, 0.0));

        let built = build_scene_with_anim(
            &scene,
            (w as f32, h as f32),
            BuildAnimationCtx {
                time: progress,
                scenario_time: progress,
                scene_duration,
                fps: 30,
            },
        );
        let layout = run_layout(
            &built.root,
            (w as f32, h as f32),
            &ConversionContext::default(),
        );
        let dispatcher = LegacyPaintDispatcher::for_scene(&built);
        let frame = PaintFrame {
            time: progress,
            scenario_time: progress,
            frame_index: (progress * 30.0) as u32,
            fps: 30,
            video_width: w,
            video_height: h,
            scene_duration,
            camera: None,
        };
        paint_tree(canvas, &built.root, &layout, &frame, &dispatcher);

        let row_bytes = w as usize * 4;
        let mut pixels = vec![0u8; row_bytes * h as usize];
        let info = skia_safe::ImageInfo::new(
            (w as i32, h as i32),
            skia_safe::ColorType::RGBA8888,
            skia_safe::AlphaType::Premul,
            None,
        );
        surface.read_pixels(&info, &mut pixels, row_bytes, (0, 0));
        pixels
    }

    /// Count non-transparent pixels in RGBA buffer.
    fn lit(buf: &[u8]) -> usize {
        buf.chunks_exact(4).filter(|p| p[3] > 0).count()
    }

    /// Count non-transparent pixels in horizontal band [y0, y1) of a 100-wide canvas.
    fn lit_band(buf: &[u8], y0: usize, y1: usize) -> usize {
        (y0..y1)
            .flat_map(|y| (0..100usize).map(move |x| (y * 100 + x) * 4))
            .filter(|&i| buf[i + 3] > 0)
            .count()
    }

    // ── T1: draw_progress=0 → zero pixels ────────────────────────────────────
    #[test]
    fn draw_progress_zero_paints_nothing() {
        // No animation configured but draw_progress is forced to 0 via anim at t=0.
        // We use a draw_in animation so progress maps to draw_progress.
        let buf = render_svg_at(
            TWO_STROKE_SVG,
            serde_json::json!({
                "style": {
                    "width": "100px",
                    "height": "100px",
                    "animation": [{ "name": "draw_in", "duration": 1.0 }]
                }
            }),
            // t=0: draw_progress animates from 0.0 at delay=0.
            0.0,
        );
        assert_eq!(
            lit(&buf),
            0,
            "draw_progress=0 must produce zero lit pixels (got {})",
            lit(&buf)
        );
    }

    // ── T2: draw_progress=0.5 → only first path visible (overlap=0) ──────────
    #[test]
    fn draw_progress_half_shows_first_path_only() {
        // Two equal-length strokes → at progress=0.5 the first is fully drawn,
        // the second has not started (sequential, overlap=0).
        let buf = render_svg_at(
            TWO_STROKE_SVG,
            serde_json::json!({
                "draw_overlap": 0.0,
                "style": {
                    "width": "100px",
                    "height": "100px",
                    "animation": [{ "name": "draw_in", "duration": 1.0 }]
                }
            }),
            0.5,
        );
        // Path 1 is near y=30, path 2 near y=70.
        let top = lit_band(&buf, 25, 35);
        let bot = lit_band(&buf, 65, 75);
        assert!(
            top > 0,
            "first path (y≈30) must be visible at draw_progress≈0.5 (top={top})"
        );
        assert_eq!(
            bot, 0,
            "second path (y≈70) must be absent at draw_progress≈0.5 (bot={bot})"
        );
    }

    // ── T3: draw_progress=1.0 → resvg full render (filled pixels) ───────────
    #[test]
    fn draw_progress_one_renders_complete_svg() {
        // At progress=1.0 we fall through to resvg so fills are visible.
        // TWO_STROKE_SVG only has strokes — just assert both bands are lit.
        let buf = render_svg_at(
            TWO_STROKE_SVG,
            serde_json::json!({
                "style": {
                    "width": "100px",
                    "height": "100px",
                    "animation": [{ "name": "draw_in", "duration": 1.0 }]
                }
            }),
            // At t slightly before 1.0 draw_progress may still be <1.0.
            // Use t=1.0 (end of 1s animation) to get progress=1.0.
            1.0,
        );
        let top = lit_band(&buf, 25, 35);
        let bot = lit_band(&buf, 65, 75);
        assert!(
            top > 0,
            "top band must be lit at draw_progress=1 (top={top})"
        );
        assert!(
            bot > 0,
            "bot band must be lit at draw_progress=1 (bot={bot})"
        );
    }

    // ── T4: draw_overlap=1.0 → both paths drawn in parallel at 0.5 ──────────
    #[test]
    fn draw_overlap_one_draws_all_paths_in_parallel() {
        // With overlap=1.0 every path's window spans the full [0,1] range.
        // At progress=0.5, both paths are 50% drawn.
        let buf = render_svg_at(
            TWO_STROKE_SVG,
            serde_json::json!({
                "draw_overlap": 1.0,
                "style": {
                    "width": "100px",
                    "height": "100px",
                    "animation": [{ "name": "draw_in", "duration": 1.0 }]
                }
            }),
            0.5,
        );
        let top = lit_band(&buf, 25, 35);
        let bot = lit_band(&buf, 65, 75);
        assert!(
            top > 0,
            "with overlap=1 both paths must be partially drawn at 0.5; top={top}"
        );
        assert!(
            bot > 0,
            "with overlap=1 both paths must be partially drawn at 0.5; bot={bot}"
        );
    }

    // ── T5: fill-only SVG → contour visible in draw mode ─────────────────────
    #[test]
    fn fill_only_svg_draws_contour_in_draw_mode() {
        // TWO_FILL_SVG has no stroke; draw mode should trace the contour using
        // the fill color and draw_stroke_width.
        let buf = render_svg_at(
            TWO_FILL_SVG,
            serde_json::json!({
                "draw_stroke_width": 3.0,
                "draw_overlap": 0.0,
                "style": {
                    "width": "100px",
                    "height": "100px",
                    "animation": [{ "name": "draw_in", "duration": 1.0 }]
                }
            }),
            // At t=0.5, at least some pixels should be visible (first rect partially drawn).
            0.5,
        );
        assert!(
            lit(&buf) > 0,
            "fill-only SVG should produce lit pixels in draw mode at 0.5 (got {})",
            lit(&buf)
        );
    }

    // ── T6: static render (no draw, no animation) → resvg bitmap unchanged ───
    #[test]
    fn static_svg_renders_without_draw_mode() {
        // A simple filled-rect SVG with no draw fields. The resvg path must
        // produce filled pixels in the fill color.
        let filled_svg = r#"<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100">
  <rect x="10" y="10" width="80" height="80" fill="red"/>
</svg>"#;
        let buf = render_svg_at(
            filled_svg,
            serde_json::json!({}), // no extra fields → static resvg path
            0.5,
        );
        // resvg fills the rect red. Count red-dominant pixels.
        let red_pixels = buf
            .chunks_exact(4)
            .filter(|p| p[3] > 0 && p[0] > p[2] && p[0] > p[1])
            .count();
        assert!(
            red_pixels > 3000,
            "static render must produce many red-dominant pixels (got {red_pixels})"
        );
    }

    // ── T7: draw: true → static draw trace visible without animation ──────────
    #[test]
    fn draw_true_shows_full_trace_without_animation() {
        // draw: true forces draw-on mode with progress=1.0 when no animation is active.
        // That should fall through to the resvg render (showing the complete SVG).
        let buf = render_svg_at(
            TWO_STROKE_SVG,
            serde_json::json!({ "draw": true }),
            0.5, // time doesn't matter, no animation
        );
        let top = lit_band(&buf, 25, 35);
        let bot = lit_band(&buf, 65, 75);
        assert!(top > 0, "draw:true must show top path (top={top})");
        assert!(bot > 0, "draw:true must show bot path (bot={bot})");
    }
}

#[cfg(test)]
mod audio_tests {
    use std::sync::Arc;

    use rustmotion_core::engine::renderer::audio_analysis::{audio_analysis_cache, AudioAnalysis};

    /// Build a minimal PCM WAV file in memory: mono i16, 44100 Hz.
    /// The first `sine_samples` samples are a 440 Hz sine wave;
    /// the rest are silence up to `total_samples`.
    fn make_sine_wav(
        total_samples: u32,
        sine_samples: u32,
        freq: f32,
        sample_rate: u32,
    ) -> Vec<u8> {
        let data_size = total_samples * 2; // i16 mono
        let mut wav = Vec::<u8>::new();
        wav.extend_from_slice(b"RIFF");
        wav.extend_from_slice(&(36 + data_size).to_le_bytes());
        wav.extend_from_slice(b"WAVE");
        wav.extend_from_slice(b"fmt ");
        wav.extend_from_slice(&16u32.to_le_bytes());
        wav.extend_from_slice(&1u16.to_le_bytes()); // PCM
        wav.extend_from_slice(&1u16.to_le_bytes()); // mono
        wav.extend_from_slice(&sample_rate.to_le_bytes());
        wav.extend_from_slice(&(sample_rate * 2).to_le_bytes()); // byte rate
        wav.extend_from_slice(&2u16.to_le_bytes()); // block align
        wav.extend_from_slice(&16u16.to_le_bytes()); // bits per sample
        wav.extend_from_slice(b"data");
        wav.extend_from_slice(&data_size.to_le_bytes());
        for i in 0..total_samples {
            let s = if i < sine_samples {
                let t = i as f32 / sample_rate as f32;
                (2.0 * std::f32::consts::PI * freq * t).sin()
            } else {
                0.0
            };
            let pcm = (s * 32767.0) as i16;
            wav.extend_from_slice(&pcm.to_le_bytes());
        }
        wav
    }
    fn nanos() -> u128 {
        std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap()
            .as_nanos()
    }

    /// Wrap a component JSON into a `ChildComponent` absolutely positioned at
    /// the canvas origin.
    fn child_at_origin(json: serde_json::Value) -> crate::components::ChildComponent {
        let component: crate::components::Component =
            serde_json::from_value(json).expect("component json");
        crate::components::ChildComponent {
            component,
            position: Some(crate::components::PositionMode::Absolute { x: 0.0, y: 0.0 }),
            x: None,
            y: None,
            z_index: None,
            bleed: false,
        }
    }

    /// Paint a single child through the full box_tree → layout → paint
    /// pipeline at `time` and return the RGBA8888 pixels.
    fn paint_scene(
        child: crate::components::ChildComponent,
        w: i32,
        h: i32,
        time: f64,
        fps: u32,
    ) -> Vec<u8> {
        use rustmotion_components::box_builder::{build_scene_with_anim, BuildAnimationCtx};
        use rustmotion_components::legacy_dispatch::LegacyPaintDispatcher;
        use rustmotion_core::css::taffy_bridge::ConversionContext;
        use rustmotion_core::engine::layout_pass::run_layout;
        use rustmotion_core::engine::paint_pass::{paint_tree, PaintFrame};

        let mut surface = skia_safe::surfaces::raster_n32_premul((w, h)).expect("raster surface");
        let canvas = surface.canvas();
        canvas.clear(skia_safe::Color4f::new(0.0, 0.0, 0.0, 0.0));
        let scene = vec![child];
        let built = build_scene_with_anim(
            &scene,
            (w as f32, h as f32),
            BuildAnimationCtx {
                time,
                scenario_time: time,
                scene_duration: 10.0,
                fps,
            },
        );
        let layout = run_layout(
            &built.root,
            (w as f32, h as f32),
            &ConversionContext::default(),
        );
        let dispatcher = LegacyPaintDispatcher::for_scene(&built);
        paint_tree(
            canvas,
            &built.root,
            &layout,
            &PaintFrame {
                time,
                scenario_time: time,
                frame_index: (time * fps as f64) as u32,
                fps,
                video_width: w as u32,
                video_height: h as u32,
                scene_duration: 10.0,
                camera: None,
            },
            &dispatcher,
        );
        let row_bytes = (w * 4) as usize;
        let mut pixels = vec![0u8; row_bytes * h as usize];
        let info = skia_safe::ImageInfo::new(
            (w, h),
            skia_safe::ColorType::RGBA8888,
            skia_safe::AlphaType::Premul,
            None,
        );
        surface.read_pixels(&info, &mut pixels, row_bytes, (0, 0));
        pixels
    }

    /// Count non-transparent pixels in the column range `[x0, x1)`.
    fn lit_in_columns(pixels: &[u8], width: usize, x0: usize, x1: usize) -> usize {
        let height = pixels.len() / (width * 4);
        let mut count = 0;
        for y in 0..height {
            for x in x0..x1 {
                if pixels[(y * width + x) * 4 + 3] > 0 {
                    count += 1;
                }
            }
        }
        count
    }

    /// The analysis must describe the **mix**, not the source.
    ///
    /// `volume`, `volume_keyframes` and the fades are what comes out of the
    /// speakers, and since #182 the studio plays exactly that. A waveform
    /// drawing the raw file's envelope while a keyframe takes the sound to
    /// zero contradicts what the viewer hears.
    #[test]
    fn the_analysis_follows_the_mixed_envelope_not_the_raw_file() {
        let sample_rate = 44100u32;
        // 2 s of unbroken sine: any variation in the analysis comes from the
        // envelope, never from the source.
        let wav_path = std::env::temp_dir().join(format!("rustmotion_test_mix_{}.wav", nanos()));
        std::fs::write(
            &wav_path,
            make_sine_wav(sample_rate * 2, sample_rate * 2, 440.0, sample_rate),
        )
        .expect("write fixture");
        let wav_str = wav_path.to_str().unwrap().to_string();

        // Full for the first second, silent for the second.
        let json = serde_json::json!({
            "video": {"width": 32, "height": 32, "fps": 30},
            "audio": [{
                "src": wav_str,
                "volume_keyframes": [
                    {"time": 0.0, "volume": 1.0},
                    {"time": 1.0, "volume": 1.0},
                    {"time": 1.05, "volume": 0.0},
                    {"time": 2.0, "volume": 0.0}
                ]
            }],
            "scenes": [{"duration": 2.0, "children": []}]
        })
        .to_string();
        let scenario =
            crate::loader::load_scenario_from_source(None, Some(&json)).expect("load scenario");
        assert!(crate::encode::audio_analysis::analyze_scenario_audio(&scenario).is_empty());

        let analysis = audio_analysis_cache().get(&wav_str).unwrap().clone();
        std::fs::remove_file(&wav_path).ok();

        assert!(
            analysis.amplitude_at(0.5) > 0.5,
            "inside the audible half the envelope is open"
        );
        assert!(
            analysis.amplitude_at(1.5) < 0.05,
            "the keyframes take the track to silence — the visualisation must \
             follow it, not keep drawing the sine underneath"
        );
    }

    /// The envelope is part of the cache key: two scenarios naming the same
    /// file with different mixes must not share an analysis.
    #[test]
    fn changing_the_envelope_re_analyses_the_same_file() {
        let sample_rate = 44100u32;
        let wav_path = std::env::temp_dir().join(format!("rustmotion_test_env_{}.wav", nanos()));
        std::fs::write(
            &wav_path,
            make_sine_wav(sample_rate, sample_rate, 440.0, sample_rate),
        )
        .expect("write fixture");
        let wav_str = wav_path.to_str().unwrap().to_string();

        let with_volume = |v: f32| {
            let json = serde_json::json!({
                "video": {"width": 32, "height": 32, "fps": 30},
                "audio": [{"src": wav_str, "volume": v}],
                "scenes": [{"duration": 1.0, "children": []}]
            })
            .to_string();
            let scenario =
                crate::loader::load_scenario_from_source(None, Some(&json)).expect("load");
            crate::encode::audio_analysis::analyze_scenario_audio(&scenario);
            audio_analysis_cache().get(&wav_str).unwrap().amplitude[10]
        };

        // Amplitudes are normalised per analysis, so a flat gain change cannot
        // be read off the values — assert the *entry* was replaced instead.
        let loud = with_volume(1.0);
        let quiet = with_volume(0.0);
        std::fs::remove_file(&wav_path).ok();

        assert!(loud > 0.5, "the full-volume take is audible, got {loud}");
        assert_eq!(
            quiet, 0.0,
            "at volume 0 the analysis must be silent — a stale entry would \
             still report {loud}"
        );
    }

    /// The failure the whole rewrite exists for: a scenario naming an asset
    /// beside itself must load identically whatever directory the process
    /// runs from. Authoring from the scenario's folder worked; the studio,
    /// which runs from the repository root, resolved nothing.
    #[test]
    fn a_relative_asset_resolves_against_the_scenario_not_the_cwd() {
        let dir = std::env::temp_dir().join(format!("rustmotion_cwd_{}", nanos()));
        std::fs::create_dir_all(dir.join("assets")).expect("scratch");
        std::fs::write(
            dir.join("assets/t.wav"),
            make_sine_wav(4410, 4410, 440.0, 44100),
        )
        .expect("fixture");

        let scenario_path = dir.join("scene.json");
        std::fs::write(
            &scenario_path,
            serde_json::json!({
                "video": {"width": 32, "height": 32, "fps": 30},
                "audio": [{"src": "assets/t.wav"}],
                "scenes": [{"duration": 0.1, "children": []}]
            })
            .to_string(),
        )
        .expect("write scenario");

        let loaded = crate::loader::load_scenario_with_vars(&scenario_path, None)
            .expect("scenario must load");
        let src = &loaded.audio[0].src;

        assert!(
            std::path::Path::new(src).is_absolute(),
            "the asset path must not stay relative to the process: {src}"
        );
        assert!(
            std::path::Path::new(src).is_file(),
            "and it must point at the file beside the scenario: {src}"
        );
        std::fs::remove_dir_all(&dir).ok();
    }

    /// A track placed at `start` must be *read* from `start` too.
    ///
    /// The mux places the file at `track.start` on the scenario timeline and
    /// copies it from its own sample 0; the analysis indexes the file from its
    /// own frame 0 while every painter asks in scenario time. The two only
    /// lined up when `start == 0` — a soundtrack gated to one scene played its
    /// opening while the waveform drew the file's content at that timestamp.
    #[test]
    fn a_track_start_offsets_the_analysis_lookup() {
        let sample_rate = 44100u32;
        // 1 s of sine then 1 s of silence, placed at t = 5 and cut at t = 6.5.
        let wav_path = std::env::temp_dir().join(format!("rustmotion_test_offset_{}.wav", nanos()));
        std::fs::write(
            &wav_path,
            make_sine_wav(sample_rate * 2, sample_rate, 440.0, sample_rate),
        )
        .expect("write fixture");
        let wav_str = wav_path.to_str().unwrap().to_string();

        let json = serde_json::json!({
            "video": {"width": 32, "height": 32, "fps": 30},
            "audio": [{"src": wav_str, "start": 5.0, "end": 6.5}],
            "scenes": [{"duration": 8.0, "children": []}]
        })
        .to_string();
        let scenario =
            crate::loader::load_scenario_from_source(None, Some(&json)).expect("load scenario");
        assert!(crate::encode::audio_analysis::analyze_scenario_audio(&scenario).is_empty());

        let analysis = audio_analysis_cache().get(&wav_str).unwrap().clone();
        std::fs::remove_file(&wav_path).ok();

        assert_eq!(
            analysis.amplitude_at(4.9),
            0.0,
            "before `start` the track is not playing"
        );
        assert!(
            analysis.amplitude_at(5.2) > 0.5,
            "0.2 s after `start` is 0.2 s into the file — inside the sine"
        );
        assert!(
            analysis.amplitude_at(6.2) < 0.1,
            "1.2 s after `start` is 1.2 s into the file — inside the silence"
        );
        assert_eq!(
            analysis.amplitude_at(6.6),
            0.0,
            "past `end` the track is cut, so the visualisation must go flat \
             rather than keep drawing an envelope nobody hears"
        );

        // The smoothed accessors take the same path, or a bound component
        // would disagree with the waveform next to it.
        assert_eq!(analysis.amplitude_smoothed(4.9, 3), 0.0);
        assert_eq!(analysis.band_at(4.9, 4), 0.0);
        assert_eq!(analysis.band_smoothed(4.9, 4, 3), 0.0);
    }

    /// A `waveform` must read the scenario's clock, not its own scene's.
    ///
    /// The analysis is indexed on the scenario timeline. A scene starting at
    /// t=2 s used to be handed the analysis at that many seconds *into itself*,
    /// so it drew a moment of the track that had already played.
    ///
    /// The fixture separates the two clocks: 3 s of sine then 1 s of silence,
    /// behind a 2 s filler scene. At frame 105 the scenario is at 3.5 s — in
    /// the silence — while the scene is only 1.5 s old — still in the sine.
    /// Reading the wrong clock draws a waveform where there is no sound.
    #[test]
    fn a_waveform_reads_the_scenario_clock_not_the_scene_clock() {
        let sample_rate = 44100u32;
        let wav_path = std::env::temp_dir().join(format!("rustmotion_test_clock_{}.wav", nanos()));
        std::fs::write(
            &wav_path,
            make_sine_wav(sample_rate * 4, sample_rate * 3, 440.0, sample_rate),
        )
        .expect("write fixture");
        let wav_str = wav_path.to_str().unwrap().to_string();

        let json = serde_json::json!({
            "video": {"width": 200, "height": 80, "fps": 30, "background": "#000000"},
            "audio": [{"src": wav_str}],
            "scenes": [
                {"duration": 2.0, "children": []},
                {"duration": 2.0, "children": [
                    {"type": "waveform", "track": wav_str, "color": "#ffffff",
                     "draw_style": "filled", "window": 0.5,
                     "style": {"width": 200, "height": 80}}
                ]}
            ]
        })
        .to_string();
        let scenario =
            crate::loader::load_scenario_from_source(None, Some(&json)).expect("load scenario");
        crate::encode::audio_analysis::analyze_scenario_audio(&scenario);

        let tasks = crate::encode::build_frame_tasks(&scenario);
        let lit_at = |frame: usize| {
            crate::encode::video::render_frame_task(&scenario.video, &scenario, &tasks[frame])
                .expect("render")
                .chunks_exact(4)
                .filter(|p| p[0] > 40)
                .count()
        };

        // t = 2.5 s: both clocks land in the sine, so this only proves the
        // analysis is loaded and the component draws at all.
        let audible = lit_at(75);
        // t = 3.5 s: the scenario is in the silence, the scene is not. This is
        // the frame that separates the two.
        let silent = lit_at(105);
        std::fs::remove_file(&wav_path).ok();

        assert!(
            audible > 300,
            "sanity: with sound at that moment the waveform must be drawn, got {audible} lit pixels"
        );
        assert!(
            silent * 3 < audible,
            "the track is silent at 3.5 s of the scenario — a scene-local clock \
             would read 1.5 s, still inside the sine, and draw a waveform for \
             sound nobody hears. audible={audible} silent={silent}"
        );
    }

    /// A track that cannot be decoded must be *reported*, not swallowed:
    /// silence here leaves `waveform`/`audio_spectrum` on their flat fallback
    /// with nothing anywhere saying why.
    #[test]
    fn analyze_scenario_audio_reports_an_undecodable_track() {
        let missing = std::env::temp_dir()
            .join(format!("rustmotion_test_absent_{}.wav", nanos()))
            .to_str()
            .unwrap()
            .to_string();

        let json = serde_json::json!({
            "video": {"width": 32, "height": 32, "fps": 30},
            "audio": [{"src": missing}],
            "scenes": [{"duration": 1.0, "children": []}]
        })
        .to_string();
        let scenario =
            crate::loader::load_scenario_from_source(None, Some(&json)).expect("load scenario");

        let failures = crate::encode::audio_analysis::analyze_scenario_audio(&scenario);
        assert_eq!(failures.len(), 1, "the missing track must be reported");
        assert_eq!(failures[0].src, missing);
        assert!(
            !failures[0].reason.is_empty(),
            "a failure must carry a reason, got {failures:?}"
        );
        assert!(
            audio_analysis_cache().get(&missing).is_none(),
            "a failed decode must not leave an entry behind"
        );
    }

    /// The cache is keyed by path, so a track re-exported under the same name
    /// used to keep serving the first envelope for the life of the process —
    /// exactly what a studio session does when the mix is updated.
    #[test]
    fn analyze_scenario_audio_reruns_when_the_file_changes() {
        let sample_rate = 44100u32;
        let wav_path =
            std::env::temp_dir().join(format!("rustmotion_test_refresh_{}.wav", nanos()));
        let wav_str = wav_path.to_str().unwrap().to_string();

        // First: a full second of sine — amplitude high throughout.
        std::fs::write(
            &wav_path,
            make_sine_wav(sample_rate, sample_rate, 440.0, sample_rate),
        )
        .expect("write first fixture");

        let json = serde_json::json!({
            "video": {"width": 32, "height": 32, "fps": 30},
            "audio": [{"src": wav_str}],
            "scenes": [{"duration": 1.0, "children": []}]
        })
        .to_string();
        let scenario =
            crate::loader::load_scenario_from_source(None, Some(&json)).expect("load scenario");
        assert!(crate::encode::audio_analysis::analyze_scenario_audio(&scenario).is_empty());
        let late_before = audio_analysis_cache().get(&wav_str).unwrap().amplitude[25];

        // Rewrite the same path with sine only in the first half. mtime has
        // 1 ns resolution on the platforms we target, but the length differs
        // too, so the fingerprint changes either way.
        std::fs::write(
            &wav_path,
            make_sine_wav(sample_rate * 2, sample_rate / 2, 440.0, sample_rate),
        )
        .expect("write second fixture");

        assert!(crate::encode::audio_analysis::analyze_scenario_audio(&scenario).is_empty());
        let late_after = audio_analysis_cache().get(&wav_str).unwrap().amplitude[25];
        std::fs::remove_file(&wav_path).ok();

        assert!(
            late_before > 0.5,
            "frame 25 of the first take is inside the sine, got {late_before}"
        );
        assert!(
            late_after < 0.1,
            "frame 25 of the second take is silence — a stale analysis would \
             still report {late_before}, got {late_after}"
        );
    }

    #[test]
    fn analyze_scenario_audio_computes_amplitude_and_440hz_band() {
        let sample_rate = 44100u32;
        // 1.0 s total: 0.5 s of 440 Hz sine, then 0.5 s of silence.
        let total_samples = sample_rate;
        let sine_samples = sample_rate / 2;
        let wav = make_sine_wav(total_samples, sine_samples, 440.0, sample_rate);

        let wav_path =
            std::env::temp_dir().join(format!("rustmotion_test_analysis_{}.wav", nanos()));
        std::fs::write(&wav_path, &wav).expect("write wav fixture");
        let wav_str = wav_path.to_str().unwrap().to_string();

        let json = serde_json::json!({
            "video": {"width": 32, "height": 32, "fps": 30},
            "audio": [{"src": wav_str}],
            "scenes": [{"duration": 1.0, "children": []}]
        })
        .to_string();
        let scenario =
            crate::loader::load_scenario_from_source(None, Some(&json)).expect("load scenario");
        crate::encode::audio_analysis::analyze_scenario_audio(&scenario);

        let analysis = audio_analysis_cache()
            .get(&wav_str)
            .expect("analysis must be cached under the track src")
            .clone();
        std::fs::remove_file(&wav_path).ok();

        assert_eq!(analysis.frame_rate, 30);
        assert!(
            analysis.amplitude.len() >= 29,
            "1 s at 30 fps should give ~30 frames, got {}",
            analysis.amplitude.len()
        );

        // Amplitude: ~1.0 during the sine (frames 0..14), ~0 during silence.
        let sine_max = analysis.amplitude[..14]
            .iter()
            .cloned()
            .fold(0.0f32, f32::max);
        let silence_max = analysis.amplitude[16..]
            .iter()
            .cloned()
            .fold(0.0f32, f32::max);
        assert!(
            sine_max > 0.9,
            "normalized amplitude during the sine should be ~1.0, got {sine_max}"
        );
        assert!(
            silence_max < 0.05,
            "amplitude during silence should be ~0, got {silence_max}"
        );

        // Band energy concentrated in the log band containing 440 Hz.
        let lo = 20.0f32.log2();
        let hi = 16000.0f32.log2();
        let expected_band = (((440.0f32.log2() - lo) / ((hi - lo) / 16.0)) as usize).min(15);
        let frame = &analysis.bands[5]; // mid-sine frame
        let (argmax, max_v) =
            frame.iter().enumerate().fold(
                (0usize, 0.0f32),
                |acc, (i, &v)| if v > acc.1 { (i, v) } else { acc },
            );
        assert_eq!(
            argmax, expected_band,
            "band {expected_band} should carry the 440 Hz energy (argmax was {argmax}: {frame:?})"
        );
        assert!(max_v > 0.5, "440 Hz band should be hot, got {max_v}");
        let second = frame
            .iter()
            .enumerate()
            .filter(|(i, _)| *i != expected_band)
            .map(|(_, &v)| v)
            .fold(0.0f32, f32::max);
        assert!(
            frame[expected_band] > second * 3.0,
            "440 Hz band ({}) should dominate the runner-up ({second})",
            frame[expected_band]
        );
    }

    #[test]
    fn audio_spectrum_hot_band_renders_taller_bar() {
        let key = format!("test-spectrum-hot-{}", nanos());
        let mut bands = vec![[0.0f32; 16]; 30];
        for fr in &mut bands {
            fr[15] = 1.0; // hottest band = highest frequencies → right-most bar
        }
        audio_analysis_cache().insert(
            key.clone(),
            Arc::new(AudioAnalysis {
                frame_rate: 30,
                amplitude: vec![1.0; 30],
                bands,
                start: 0.0,
                end: None,
            }),
        );

        let child = child_at_origin(serde_json::json!({
            "type": "audio_spectrum",
            "track": key,
            "bars": 16
        }));
        // Default intrinsic size 400x120; bar_w = (400 - 15*2)/16 ≈ 23.1,
        // bar 15 spans x ≈ 377..400.
        let pixels = paint_scene(child, 400, 200, 0.5, 30);
        let hot_bar = lit_in_columns(&pixels, 400, 378, 400);
        let cold_bar = lit_in_columns(&pixels, 400, 0, 23);
        assert!(
            hot_bar > cold_bar * 10,
            "hot band bar ({hot_bar} lit px) should tower over a cold bar ({cold_bar} lit px)"
        );

        // Missing track → graceful degradation: min_height bars only, glued
        // to the bottom edge of the 120px box.
        let missing = child_at_origin(serde_json::json!({
            "type": "audio_spectrum",
            "track": format!("test-spectrum-missing-{}", nanos()),
            "bars": 16
        }));
        let pixels = paint_scene(missing, 400, 200, 0.5, 30);
        let mut lit_total = 0usize;
        let mut lit_above_baseline = 0usize;
        for y in 0..200usize {
            for x in 0..400usize {
                if pixels[(y * 400 + x) * 4 + 3] > 0 {
                    lit_total += 1;
                    if y < 115 {
                        lit_above_baseline += 1;
                    }
                }
            }
        }
        assert!(lit_total > 0, "min_height bars should still render");
        assert_eq!(
            lit_above_baseline, 0,
            "empty cache must render nothing above the min-height baseline"
        );
    }

    #[test]
    fn waveform_ramp_renders_increasing_pixels_along_x() {
        let key = format!("test-waveform-ramp-{}", nanos());
        let n = 60usize; // 2 s at 30 fps
        let amplitude: Vec<f32> = (0..n).map(|i| i as f32 / (n - 1) as f32).collect();
        audio_analysis_cache().insert(
            key.clone(),
            Arc::new(AudioAnalysis {
                frame_rate: 30,
                amplitude,
                bands: vec![[0.0f32; 16]; 60],
                start: 0.0,
                end: None,
            }),
        );

        let child = child_at_origin(serde_json::json!({
            "type": "waveform",
            "track": key,
            "draw_style": "filled",
            "window": 2.0
        }));
        // At t=1.0 the 2 s window covers the whole ramp: amplitude (and the
        // filled area under the curve) grows from left to right.
        let pixels = paint_scene(child, 400, 200, 1.0, 30);
        let left = lit_in_columns(&pixels, 400, 0, 133);
        let right = lit_in_columns(&pixels, 400, 267, 400);
        assert!(
            left > 0,
            "left third should have some lit pixels (outline at minimum)"
        );
        assert!(
            right > left * 2,
            "ramping amplitude: right third ({right} lit px) should clearly exceed left third ({left} lit px)"
        );
    }

    #[test]
    fn audio_reactive_opacity_binding_differs_between_loud_and_quiet() {
        let key = format!("test-ar-binding-{}", nanos());
        // Loud at t=0 (frame 0), silent afterwards.
        let mut amplitude = vec![0.0f32; 90];
        amplitude[0] = 1.0;
        audio_analysis_cache().insert(
            key.clone(),
            Arc::new(AudioAnalysis {
                frame_rate: 30,
                amplitude,
                bands: vec![[0.0f32; 16]; 90],
                start: 0.0,
                end: None,
            }),
        );

        let make_child = || {
            child_at_origin(serde_json::json!({
                "type": "shape",
                "shape": "rect",
                "fill": "#ff0000",
                "style": {
                    "width": "100px",
                    "height": "100px",
                    "audio-reactive": {
                        "track": key,
                        "source": "amplitude",
                        "property": "opacity",
                        "min": 0.0,
                        "max": 1.0
                    }
                }
            }))
        };
        let red_sum = |pixels: &[u8]| pixels.chunks_exact(4).map(|p| p[0] as u64).sum::<u64>();

        let loud = paint_scene(make_child(), 200, 200, 0.0, 30);
        let quiet = paint_scene(make_child(), 200, 200, 0.5, 30);
        let (loud_red, quiet_red) = (red_sum(&loud), red_sum(&quiet));
        assert!(
            loud_red > 100_000,
            "loud frame should render the red rect (red_sum={loud_red})"
        );
        assert!(
            loud_red > quiet_red.saturating_mul(10).max(1),
            "red_sum must differ sharply between loud ({loud_red}) and quiet ({quiet_red}) frames"
        );
    }
}

// ─── Motion blur / Trail effect tests ─────────────────────────────────────────

#[cfg(test)]
mod motion_blur_trail {
    //! TDD tests for motion_blur and trail animation effects.
    //!
    //! These tests drive the pipeline through `render_new_at` and inspect raw
    //! pixel buffers to verify the ghost-based temporal sampling.

    use crate::components::{ChildComponent, Component, PositionMode};
    use rustmotion_components::box_builder::{build_scene_with_anim, BuildAnimationCtx};
    use rustmotion_components::legacy_dispatch::LegacyPaintDispatcher;
    use rustmotion_core::css::taffy_bridge::ConversionContext;
    use rustmotion_core::engine::layout_pass::run_layout;
    use rustmotion_core::engine::paint_pass::{paint_tree, PaintFrame};

    const FPS: u32 = 30;

    /// Render the scene at a specific time through the new pipeline.
    /// Returns RGBA8888 (premul) pixels.
    fn render_at(
        children: &[ChildComponent],
        w: u32,
        h: u32,
        time: f64,
        scene_duration: f64,
    ) -> Vec<u8> {
        let mut surface =
            skia_safe::surfaces::raster_n32_premul((w as i32, h as i32)).expect("surface");
        let canvas = surface.canvas();
        canvas.clear(skia_safe::Color4f::new(0.0, 0.0, 0.0, 0.0));
        let built = build_scene_with_anim(
            children,
            (w as f32, h as f32),
            BuildAnimationCtx {
                time,
                scenario_time: time,
                scene_duration,
                fps: FPS,
            },
        );
        let layout = run_layout(
            &built.root,
            (w as f32, h as f32),
            &ConversionContext::default(),
        );
        let dispatcher = LegacyPaintDispatcher::for_scene(&built);
        let frame = PaintFrame {
            time,
            scenario_time: time,
            frame_index: (time * FPS as f64) as u32,
            fps: FPS,
            video_width: w,
            video_height: h,
            scene_duration,
            camera: None,
        };
        paint_tree(canvas, &built.root, &layout, &frame, &dispatcher);
        let row_bytes = w as usize * 4;
        let mut pixels = vec![0u8; row_bytes * h as usize];
        let info = skia_safe::ImageInfo::new(
            (w as i32, h as i32),
            skia_safe::ColorType::RGBA8888,
            skia_safe::AlphaType::Premul,
            None,
        );
        surface.read_pixels(&info, &mut pixels, row_bytes, (0, 0));
        pixels
    }

    /// Count unique x-columns (0..w) that have at least one lit pixel (R > threshold).
    fn lit_column_span(pixels: &[u8], w: usize, h: usize, r_threshold: u8) -> usize {
        let mut hit = vec![false; w];
        for y in 0..h {
            for x in 0..w {
                let r = pixels[(y * w + x) * 4];
                if r > r_threshold {
                    hit[x] = true;
                }
            }
        }
        hit.iter().filter(|&&b| b).count()
    }

    /// Find the maximum red channel value across all pixels.
    fn max_red(pixels: &[u8]) -> u8 {
        pixels.chunks_exact(4).map(|p| p[0]).max().unwrap_or(0)
    }

    /// Make a red Shape with slide_in_left + motion_blur, positioned at the center.
    fn make_motion_blur_scene(samples: u32, intensity: f32) -> Vec<ChildComponent> {
        let json = serde_json::json!({
            "type": "shape",
            "shape": "rect",
            "fill": "#ff0000",
            "style": {
                "width": "80px",
                "height": "80px",
                "animation": [
                    { "name": "slide_in_left", "duration": 1.0 },
                    { "name": "motion_blur", "intensity": intensity, "samples": samples }
                ]
            }
        });
        let component: Component = serde_json::from_value(json).expect("motion_blur json");
        vec![ChildComponent {
            component,
            position: Some(PositionMode::Absolute { x: 200.0, y: 110.0 }),
            x: None,
            y: None,
            z_index: None,
            bleed: false,
        }]
    }

    /// Make a red Shape with slide_in_left + trail effect.
    fn make_trail_scene(copies: u32, spacing: f64, falloff: f32) -> Vec<ChildComponent> {
        let json = serde_json::json!({
            "type": "shape",
            "shape": "rect",
            "fill": "#ff0000",
            "style": {
                "width": "80px",
                "height": "80px",
                "animation": [
                    { "name": "slide_in_left", "duration": 1.0 },
                    { "name": "trail", "copies": copies, "spacing": spacing, "falloff": falloff }
                ]
            }
        });
        let component: Component = serde_json::from_value(json).expect("trail json");
        vec![ChildComponent {
            component,
            position: Some(PositionMode::Absolute { x: 200.0, y: 110.0 }),
            x: None,
            y: None,
            z_index: None,
            bleed: false,
        }]
    }

    // ──────────────────────────────────────────────────────────────────────────
    // TDD RED TESTS — these will fail until the implementation is in place.
    // ──────────────────────────────────────────────────────────────────────────

    /// Motion blur broadens the x-span of lit pixels compared to no-blur.
    #[test]
    fn motion_blur_broadens_horizontal_span() {
        let w = 500u32;
        let h = 300u32;
        // t=0.5 → shape mid-animation → significant motion → ghosts should
        // land to the left of the principal node.
        let without = make_motion_blur_scene(1, 1.0); // samples=1 → no-op (degenerate)
        let with_blur = make_motion_blur_scene(6, 1.0);

        let buf_without = render_at(&without, w, h, 0.5, 1.0);
        let buf_with = render_at(&with_blur, w, h, 0.5, 1.0);

        let span_without = lit_column_span(&buf_without, w as usize, h as usize, 30);
        let span_with = lit_column_span(&buf_with, w as usize, h as usize, 30);

        assert!(
            span_with > span_without,
            "motion_blur (samples=6) should broaden horizontal span: without={span_without}, with={span_with}"
        );
    }

    /// samples=1 is the degenerate case — behaves identically to no blur.
    /// We relax this: samples=1 produces one ghost but ghost and principal
    /// overlap at t_0 - 0 = t_0. Result should be visually equivalent
    /// (total span ≤ span_without + 5 pixels of rounding noise).
    #[test]
    fn motion_blur_samples_1_is_degenerate() {
        let w = 500u32;
        let h = 300u32;
        let no_effect = {
            // A plain shape with no motion_blur at all
            let json = serde_json::json!({
                "type": "shape",
                "shape": "rect",
                "fill": "#ff0000",
                "style": {
                    "width": "80px",
                    "height": "80px",
                    "animation": [{ "name": "slide_in_left", "duration": 1.0 }]
                }
            });
            let component: Component = serde_json::from_value(json).unwrap();
            vec![ChildComponent {
                component,
                position: Some(PositionMode::Absolute { x: 200.0, y: 110.0 }),
                x: None,
                y: None,
                z_index: None,
                bleed: false,
            }]
        };
        let with_1 = make_motion_blur_scene(1, 1.0);

        let buf_no = render_at(&no_effect, w, h, 0.5, 1.0);
        let buf_1 = render_at(&with_1, w, h, 0.5, 1.0);

        let span_no = lit_column_span(&buf_no, w as usize, h as usize, 30);
        let span_1 = lit_column_span(&buf_1, w as usize, h as usize, 30);

        assert!(
            span_1 <= span_no + 5,
            "motion_blur samples=1 should match no-blur (span_no={span_no}, span_1={span_1})"
        );
    }

    /// Static component (no transform animation) + motion_blur → ghosts overlap
    /// exactly. The render should look the same as without blur except possibly
    /// for a reduced opacity of the principal (opacity accumulation).
    /// We verify: no horizontal broadening and the image is not black.
    #[test]
    fn motion_blur_static_component_no_broadening() {
        let w = 500u32;
        let h = 300u32;

        let no_blur = {
            let json = serde_json::json!({
                "type": "shape",
                "shape": "rect",
                "fill": "#ff0000",
                "style": { "width": "80px", "height": "80px" }
            });
            let component: Component = serde_json::from_value(json).unwrap();
            vec![ChildComponent {
                component,
                position: Some(PositionMode::Absolute { x: 200.0, y: 110.0 }),
                x: None,
                y: None,
                z_index: None,
                bleed: false,
            }]
        };
        let with_blur = {
            let json = serde_json::json!({
                "type": "shape",
                "shape": "rect",
                "fill": "#ff0000",
                "style": {
                    "width": "80px",
                    "height": "80px",
                    "animation": [
                        { "name": "motion_blur", "intensity": 1.0, "samples": 6 }
                    ]
                }
            });
            let component: Component = serde_json::from_value(json).unwrap();
            vec![ChildComponent {
                component,
                position: Some(PositionMode::Absolute { x: 200.0, y: 110.0 }),
                x: None,
                y: None,
                z_index: None,
                bleed: false,
            }]
        };

        let buf_no = render_at(&no_blur, w, h, 0.5, 1.0);
        let buf_blur = render_at(&with_blur, w, h, 0.5, 1.0);

        let span_no = lit_column_span(&buf_no, w as usize, h as usize, 30);
        let span_blur = lit_column_span(&buf_blur, w as usize, h as usize, 30);

        // No broadening (ghosts superimpose).
        assert!(
            span_blur <= span_no + 5,
            "static + motion_blur should not broaden: span_no={span_no}, span_blur={span_blur}"
        );

        // Image must not be black (ghosts accumulate enough opacity).
        let max_r = max_red(&buf_blur);
        assert!(
            max_r > 50,
            "static + motion_blur must still render visibly (max_r={max_r})"
        );
    }

    /// Trail with copies=3 should produce a wider horizontal span than without,
    /// and the leading edge (rightmost in slide_in_left animation) should be
    /// brighter than the trailing edge.
    #[test]
    fn trail_produces_multiple_distinct_blobs() {
        let w = 600u32;
        let h = 300u32;

        // At t=0.5 the shape is at mid-slide. Trail ghosts are at
        // t-spacing, t-2*spacing, t-3*spacing → further left.
        let no_trail = {
            let json = serde_json::json!({
                "type": "shape",
                "shape": "rect",
                "fill": "#ff0000",
                "style": {
                    "width": "60px",
                    "height": "60px",
                    "animation": [{ "name": "slide_in_left", "duration": 1.0 }]
                }
            });
            let component: Component = serde_json::from_value(json).unwrap();
            vec![ChildComponent {
                component,
                position: Some(PositionMode::Absolute { x: 300.0, y: 120.0 }),
                x: None,
                y: None,
                z_index: None,
                bleed: false,
            }]
        };
        let with_trail = make_trail_scene(3, 0.1, 0.6);

        let buf_no = render_at(&no_trail, w, h, 0.5, 1.0);
        let buf_trail = render_at(&with_trail, w, h, 0.5, 1.0);

        let span_no = lit_column_span(&buf_no, w as usize, h as usize, 20);
        let span_trail = lit_column_span(&buf_trail, w as usize, h as usize, 20);

        assert!(
            span_trail > span_no,
            "trail (copies=3) should broaden horizontal span: span_no={span_no}, span_trail={span_trail}"
        );
    }
}

// ─── Post-effects pipeline tests ──────────────────────────────────────────────

#[cfg(test)]
mod post_effects_pipeline {
    use crate::encode::video::{build_frame_tasks, render_frame_task, FrameTask};
    use crate::loader::load_scenario_from_source;

    /// Render frame 0 of the first scene in a scenario JSON string.
    fn render_first_frame(json: &str) -> Vec<u8> {
        let scenario = load_scenario_from_source(None, Some(json)).expect("load");
        let tasks = build_frame_tasks(&scenario);
        let task = tasks
            .iter()
            .find(|t| matches!(t, FrameTask::Normal { .. }))
            .expect("normal task");
        render_frame_task(&scenario.video, &scenario, task).expect("render")
    }

    #[test]
    fn vignette_makes_corners_darker_than_center() {
        // A scene with strong vignette on a light background — corners must be darker.
        // Using JSON string concatenation to avoid raw string + hex conflicts.
        let bg = "#ffffff";
        let with_vignette = format!(
            r#"{{"video":{{"width":100,"height":100,"background":"{bg}"}},"scenes":[{{"duration":1.0,"effects":[{{"type":"vignette","intensity":0.9,"radius":0.3}}],"children":[]}}]}}"#
        );
        let without_vignette = format!(
            r#"{{"video":{{"width":100,"height":100,"background":"{bg}"}},"scenes":[{{"duration":1.0,"children":[]}}]}}"#
        );

        let buf_v = render_first_frame(&with_vignette);
        let buf_plain = render_first_frame(&without_vignette);

        // Top-left corner pixel (0, 0) — RGBA at byte 0
        let corner_r_vignette = buf_v[0] as u16;
        let corner_r_plain = buf_plain[0] as u16;
        assert!(
            corner_r_vignette < corner_r_plain,
            "vignette corner must be darker: vignette={corner_r_vignette} plain={corner_r_plain}"
        );

        // Centre pixel (50, 50) in a 100×100 image
        let center_base = (50 * 100 + 50) * 4;
        let center_r_vignette = buf_v[center_base] as u16;
        let center_r_plain = buf_plain[center_base] as u16;
        assert!(
            center_r_vignette >= center_r_plain.saturating_sub(5),
            "vignette center should be approximately unchanged: vignette={center_r_vignette} plain={center_r_plain}"
        );
    }

    #[test]
    fn scene_effects_field_defaults_to_empty() {
        // A scene without an effects key must parse without error and render cleanly.
        let json =
            r#"{"video":{"width":32,"height":32},"scenes":[{"duration":1.0,"children":[]}]}"#;
        let scenario = load_scenario_from_source(None, Some(json)).expect("load");
        let scene = &scenario.views[0].scenes[0];
        assert!(
            scene.effects.is_empty(),
            "effects must default to empty Vec"
        );
    }

    #[test]
    fn grain_effect_changes_buffer_vs_no_effect() {
        // Grain at high intensity should change pixels on a plain background.
        let bg = "#808080";
        let with_grain = format!(
            r#"{{"video":{{"width":32,"height":32,"background":"{bg}"}},"scenes":[{{"duration":1.0,"effects":[{{"type":"grain","intensity":0.5,"seed":42,"animated":false}}],"children":[]}}]}}"#
        );
        let without = format!(
            r#"{{"video":{{"width":32,"height":32,"background":"{bg}"}},"scenes":[{{"duration":1.0,"children":[]}}]}}"#
        );
        let a = render_first_frame(&with_grain);
        let b = render_first_frame(&without);
        assert_ne!(a, b, "grain effect must change the buffer");
    }

    #[test]
    fn post_effect_schema_deserializes_all_variants() {
        use rustmotion_core::schema::scenario::PostEffect;
        let cases = [
            r#"{"type":"grain","intensity":0.2,"seed":10,"animated":true}"#,
            r#"{"type":"vignette","intensity":0.6,"radius":0.8}"#,
            r#"{"type":"pixelate","size":8}"#,
            r#"{"type":"progressive_blur","direction":"bottom","start":0.5,"max_radius":12.0}"#,
            r#"{"type":"progressive_blur","direction":"top","start":0.3,"max_radius":8.0}"#,
        ];
        for case in &cases {
            serde_json::from_str::<PostEffect>(case)
                .unwrap_or_else(|e| panic!("failed: {e}\nJSON: {case}"));
        }
    }

    #[test]
    fn post_effect_unknown_type_fails() {
        // Unknown type tags must produce a deserialization error.
        use rustmotion_core::schema::scenario::PostEffect;
        let bad = r#"{"type":"unknown_effect"}"#;
        let result = serde_json::from_str::<PostEffect>(bad);
        assert!(
            result.is_err(),
            "unknown effect type must fail to deserialize"
        );
    }
}

#[cfg(test)]
mod camera_focal_tests {
    //! Issue #89: camera focal point (`camera.origin`) — the zoom/rotation
    //! pivot becomes configurable and keyframable instead of the hard-coded
    //! frame centre.

    use crate::engine::render::render_frame_v2;
    use crate::schema::{Scene, VideoConfig};

    fn config(w: u32, h: u32) -> VideoConfig {
        serde_json::from_value(serde_json::json!({ "width": w, "height": h, "fps": 30 }))
            .expect("config")
    }

    /// Render one frame of a scene described as JSON; returns RGBA bytes.
    pub(super) fn render_scene_json(
        scene_json: serde_json::Value,
        w: u32,
        h: u32,
        frame: u32,
    ) -> Vec<u8> {
        let scene: Scene = serde_json::from_value(scene_json).expect("scene json");
        let children = crate::engine::render::deserialize_children(&scene);
        let t = frame as f64 / 30.0;
        render_frame_v2(&config(w, h), &scene, frame, t, 120, &children).expect("render")
    }

    /// Centroid (x, y) of pixels dominated by the given channel (0=r, 2=b).
    pub(super) fn channel_centroid(buf: &[u8], w: u32, h: u32, channel: usize) -> (f32, f32) {
        let (mut sx, mut sy, mut n) = (0.0f64, 0.0f64, 0.0f64);
        for y in 0..h {
            for x in 0..w {
                let i = ((y * w + x) * 4) as usize;
                let v = buf[i + channel];
                let others: u16 = (0..3)
                    .filter(|c| *c != channel)
                    .map(|c| buf[i + c] as u16)
                    .sum();
                if v > 180 && others < 160 {
                    sx += x as f64;
                    sy += y as f64;
                    n += 1.0;
                }
            }
        }
        if n == 0.0 {
            (-1.0, -1.0)
        } else {
            ((sx / n) as f32, (sy / n) as f32)
        }
    }

    fn red_rect_scene_with_camera(camera: serde_json::Value) -> serde_json::Value {
        serde_json::json!({
            "duration": 4.0,
            "camera": camera,
            "children": [{
                "type": "shape",
                "shape": "rect",
                "fill": "#ff0000",
                "position": "absolute",
                "x": 60, "y": 40,
                "style": { "width": "100px", "height": "80px" }
            }]
        })
    }

    #[test]
    fn zoom_origin_top_left_differs_predictably_from_center() {
        // Rect at (60..160, 40..120) in a 400x300 frame, zoom 2x.
        // Origin (0,0): every point p maps to 2p → rect at (120..320, 80..240),
        // centroid ~(220, 160).
        // Center origin (200,150): p → 2p - (200,150) → rect at
        // (-80..120, -70..90), visible part (0..120, 0..90), centroid ~(60, 45).
        let buf_center = render_scene_json(
            red_rect_scene_with_camera(serde_json::json!({ "zoom": 2.0 })),
            400,
            300,
            0,
        );
        let buf_tl = render_scene_json(
            red_rect_scene_with_camera(
                serde_json::json!({ "zoom": 2.0, "origin": { "x": 0, "y": 0 } }),
            ),
            400,
            300,
            0,
        );

        let (cx_c, cy_c) = channel_centroid(&buf_center, 400, 300, 0);
        let (cx_tl, cy_tl) = channel_centroid(&buf_tl, 400, 300, 0);

        assert!(
            (cx_tl - 219.5).abs() < 4.0 && (cy_tl - 159.5).abs() < 4.0,
            "top-left origin zoom: expected centroid ~(220,160), got ({cx_tl},{cy_tl})"
        );
        assert!(
            (cx_c - 59.5).abs() < 4.0 && (cy_c - 44.5).abs() < 4.0,
            "center origin zoom: expected centroid ~(60,45), got ({cx_c},{cy_c})"
        );
    }

    #[test]
    fn origin_at_center_is_byte_identical_to_absent() {
        let buf_absent = render_scene_json(
            red_rect_scene_with_camera(serde_json::json!({ "zoom": 2.0, "rotation": 17.0 })),
            400,
            300,
            0,
        );
        let buf_center = render_scene_json(
            red_rect_scene_with_camera(serde_json::json!({
                "zoom": 2.0, "rotation": 17.0, "origin": { "x": 200, "y": 150 }
            })),
            400,
            300,
            0,
        );
        assert_eq!(
            buf_absent, buf_center,
            "origin at frame centre must be byte-identical to absent origin"
        );
    }

    #[test]
    fn keyframed_origin_moves_visible_content_at_fixed_zoom() {
        // Zoom fixed at 2; origin animates (0,0) → frame centre (200,150)
        // over 2s. The pivot change alone must move the rendered content
        // between frames while keeping the rect visible in both.
        let scene = red_rect_scene_with_camera(serde_json::json!({
            "zoom": 2.0,
            "keyframes": [
                { "property": "origin.x", "values": [ { "time": 0.0, "value": 0.0 }, { "time": 2.0, "value": 200.0 } ] },
                { "property": "origin.y", "values": [ { "time": 0.0, "value": 0.0 }, { "time": 2.0, "value": 150.0 } ] }
            ]
        }));
        let buf_t0 = render_scene_json(scene.clone(), 400, 300, 0);
        let buf_t2 = render_scene_json(scene, 400, 300, 60); // 60 / 30fps = 2s

        let (x0, y0) = channel_centroid(&buf_t0, 400, 300, 0);
        let (x2, y2) = channel_centroid(&buf_t2, 400, 300, 0);
        assert!(x0 >= 0.0 && x2 >= 0.0, "red must be visible in both frames");
        let dist = ((x2 - x0).powi(2) + (y2 - y0).powi(2)).sqrt();
        assert!(
            dist > 50.0,
            "keyframed origin must move content: t0=({x0},{y0}) t2=({x2},{y2}) dist={dist}"
        );
    }
}

#[cfg(test)]
mod parallax_tests {
    //! Issue #90: multi-plane parallax — `style.depth` scales the scene
    //! camera per top-level plane (0 = locked, 1 = normal, >1 = amplified).

    use super::camera_focal_tests::{channel_centroid, render_scene_json};

    fn rect(color: &str, x: f32, y: f32, depth: Option<f64>) -> serde_json::Value {
        let mut style = serde_json::json!({ "width": "80px", "height": "60px" });
        if let Some(d) = depth {
            style["depth"] = serde_json::json!(d);
        }
        serde_json::json!({
            "type": "shape", "shape": "rect", "fill": color,
            "position": "absolute", "x": x, "y": y,
            "style": style
        })
    }

    fn scene(camera: serde_json::Value, children: Vec<serde_json::Value>) -> serde_json::Value {
        serde_json::json!({ "duration": 4.0, "camera": camera, "children": children })
    }

    #[test]
    fn depth_zero_locks_plane_while_depth_one_pans() {
        // Camera pans x 0 → 100 over 2s. Blue rect depth 0 must not move;
        // red rect (depth 1, activated by the blue's explicit depth) moves
        // left by 100.
        let cam = serde_json::json!({
            "keyframes": [
                { "property": "x", "values": [ { "time": 0.0, "value": 0.0 }, { "time": 2.0, "value": 100.0 } ] }
            ]
        });
        let children = vec![
            rect("#0000ff", 40.0, 40.0, Some(0.0)),
            rect("#ff0000", 240.0, 150.0, Some(1.0)),
        ];
        let s = scene(cam, children);
        let t0 = render_scene_json(s.clone(), 400, 300, 0);
        let t2 = render_scene_json(s, 400, 300, 60);

        let (bx0, by0) = channel_centroid(&t0, 400, 300, 2);
        let (bx2, by2) = channel_centroid(&t2, 400, 300, 2);
        let (rx0, _) = channel_centroid(&t0, 400, 300, 0);
        let (rx2, _) = channel_centroid(&t2, 400, 300, 0);

        assert!(
            (bx0 - bx2).abs() < 0.5 && (by0 - by2).abs() < 0.5,
            "depth-0 plane must not move: ({bx0},{by0}) vs ({bx2},{by2})"
        );
        assert!(
            (rx0 - rx2 - 100.0).abs() < 2.0,
            "depth-1 plane must pan by -100: {rx0} -> {rx2}"
        );
    }

    #[test]
    fn depth_two_moves_twice_as_much() {
        // Camera pans x 0 → 50: depth-1 red moves 50, depth-2 green moves 100.
        let cam = serde_json::json!({
            "keyframes": [
                { "property": "x", "values": [ { "time": 0.0, "value": 0.0 }, { "time": 2.0, "value": 50.0 } ] }
            ]
        });
        let children = vec![
            rect("#ff0000", 200.0, 60.0, Some(1.0)),
            rect("#00ff00", 200.0, 180.0, Some(2.0)),
        ];
        let s = scene(cam, children);
        let t0 = render_scene_json(s.clone(), 400, 300, 0);
        let t2 = render_scene_json(s, 400, 300, 60);

        let (rx0, _) = channel_centroid(&t0, 400, 300, 0);
        let (rx2, _) = channel_centroid(&t2, 400, 300, 0);
        let (gx0, _) = channel_centroid(&t0, 400, 300, 1);
        let (gx2, _) = channel_centroid(&t2, 400, 300, 1);

        let red_shift = rx0 - rx2;
        let green_shift = gx0 - gx2;
        assert!(
            (red_shift - 50.0).abs() < 2.0,
            "depth 1 must shift by 50, got {red_shift}"
        );
        assert!(
            (green_shift - 100.0).abs() < 2.0,
            "depth 2 must shift by 100 (2x), got {green_shift}"
        );
    }

    #[test]
    fn depth_one_everywhere_is_byte_identical_to_no_depth() {
        // Explicit depth 1.0 on every child (per-plane camera path) must
        // produce the same bytes as no depth at all (global camera path).
        let cam = serde_json::json!({ "x": 30.0, "y": 10.0, "zoom": 1.5, "rotation": 8.0 });
        let plain = scene(
            cam.clone(),
            vec![
                rect("#ff0000", 100.0, 60.0, None),
                rect("#0000ff", 220.0, 150.0, None),
            ],
        );
        let with_depth = scene(
            cam,
            vec![
                rect("#ff0000", 100.0, 60.0, Some(1.0)),
                rect("#0000ff", 220.0, 150.0, Some(1.0)),
            ],
        );
        let a = render_scene_json(plain, 400, 300, 0);
        let b = render_scene_json(with_depth, 400, 300, 0);
        assert_eq!(
            a, b,
            "depth 1.0 everywhere must be byte-identical to the global camera path"
        );
    }

    #[test]
    fn zoom_does_not_scale_locked_plane() {
        // Camera zoom 2: the depth-0 blue rect keeps its exact size/position
        // (identical pixels to a no-camera render), the depth-1 red grows.
        let with_cam = scene(
            serde_json::json!({ "zoom": 2.0 }),
            vec![
                rect("#0000ff", 20.0, 20.0, Some(0.0)),
                rect("#ff0000", 250.0, 160.0, Some(1.0)),
            ],
        );
        let no_cam = serde_json::json!({
            "duration": 4.0,
            "children": [ rect("#0000ff", 20.0, 20.0, Some(0.0)) ]
        });
        let buf_cam = render_scene_json(with_cam, 400, 300, 0);
        let buf_ref = render_scene_json(no_cam, 400, 300, 0);

        // Blue region (locked plane) identical to the camera-less reference.
        let blue = |buf: &[u8]| -> Vec<u8> {
            let mut out = Vec::new();
            for y in 10..100u32 {
                for x in 10..120u32 {
                    let i = ((y * 400 + x) * 4) as usize;
                    out.extend_from_slice(&buf[i..i + 4]);
                }
            }
            out
        };
        assert_eq!(
            blue(&buf_cam),
            blue(&buf_ref),
            "depth-0 plane must be unaffected by camera zoom"
        );

        // The red rect (depth 1) must be zoomed: with center-origin zoom 2 a
        // rect at (250..330, 160..220) maps to (300..460, 170..290) clipped —
        // its centroid moves right and its visible area differs from 80x60.
        let (rx, _) = channel_centroid(&buf_cam, 400, 300, 0);
        assert!(
            rx > 330.0,
            "depth-1 plane must be zoomed toward bottom-right, centroid x = {rx}"
        );
    }
}

#[cfg(test)]
mod parallax_hitmap_tests {
    //! Studio hit-map under per-plane parallax: rects must follow their
    //! plane's (depth-scaled) camera transform via the canvas matrix.

    use crate::engine::render::render_scene_hits;
    use crate::schema::{Scene, VideoConfig};

    #[test]
    fn hit_rects_follow_their_plane_depth() {
        let config: VideoConfig =
            serde_json::from_value(serde_json::json!({ "width": 400, "height": 300, "fps": 30 }))
                .expect("config");
        // Static camera pan x=100; blue locked (depth 0), red normal (depth 1).
        let scene: Scene = serde_json::from_value(serde_json::json!({
            "duration": 4.0,
            "camera": { "x": 100.0 },
            "children": [
                { "type": "shape", "shape": "rect", "fill": "#0000ff",
                  "position": "absolute", "x": 40, "y": 40,
                  "style": { "width": "80px", "height": "60px", "depth": 0.0 } },
                { "type": "shape", "shape": "rect", "fill": "#ff0000",
                  "position": "absolute", "x": 240, "y": 150,
                  "style": { "width": "80px", "height": "60px", "depth": 1.0 } }
            ]
        }))
        .expect("scene");

        let hits = render_scene_hits(&config, &scene, 0);
        assert_eq!(hits.len(), 2, "expected two component hits");

        // Paint order matches child order: [0] = blue (depth 0), [1] = red.
        let blue = &hits[0].rect;
        let red = &hits[1].rect;
        assert!(
            (blue.x - 40.0).abs() < 0.5,
            "depth-0 hit rect must ignore the camera pan, x = {}",
            blue.x
        );
        assert!(
            (red.x - 140.0).abs() < 0.5,
            "depth-1 hit rect must follow the pan (240 - 100), x = {}",
            red.x
        );
    }
}

// ─── World-view regressions (audit lot "transitions", constats 2/7/8) ────────

#[cfg(test)]
mod world_view_regressions {
    use crate::encode::video::{build_frame_tasks, render_frame_task, FrameTask};
    use crate::engine::render::{
        render_scene_bg_scaled, render_scene_fg_scaled, render_scene_frame_scaled,
        render_scene_hits,
    };
    use crate::loader::load_scenario_from_source;
    use crate::schema::ResolvedScenario;

    fn scenario(json: &str) -> ResolvedScenario {
        load_scenario_from_source(None, Some(json)).expect("load")
    }

    fn avg_luma(buf: &[u8]) -> f64 {
        let mut sum = 0u64;
        let mut n = 0u64;
        for px in buf.chunks_exact(4) {
            sum += px[0] as u64 + px[1] as u64 + px[2] as u64;
            n += 3;
        }
        sum as f64 / n as f64
    }

    // Constat 2: the world-view background crossfade must genuinely fade the
    // outgoing scene's background during the pan, and must not jump when the
    // pan ends and the renderer switches from the two-scene crossfade branch
    // to the single-active-scene branch.
    #[test]
    fn outgoing_world_background_fades_gradually_instead_of_holding_then_jumping() {
        let json = r##"{
            "video": { "width": 320, "height": 180, "fps": 30, "background": "#000000" },
            "composition": [
                { "type": "world", "camera_pan_duration": 0.8, "camera_easing": "linear",
                  "scenes": [
                    { "duration": 2.0, "children": [],
                      "background": { "preset": "halo", "zones": [
                        { "color": "#FFFFFF80", "x": 0.5, "y": 0.5, "radius": 3.0 }
                      ] } },
                    { "duration": 2.0, "children": [],
                      "background": { "preset": "halo", "zones": [
                        { "color": "#00000000", "x": 0.5, "y": 0.5, "radius": 3.0 }
                      ] } }
                  ] }
            ]
        }"##;
        let scenario = scenario(json);
        let tasks = build_frame_tasks(&scenario);
        let fps = scenario.video.fps;

        // Pan window: boundary at t=2.0, half=0.4s -> [1.6, 2.4]. Sample a
        // margin either side too, at frame granularity (the actual render
        // grain), from t=1.5 to t=2.5.
        let render_at = |t: f64| {
            let f = (t * fps as f64).round() as usize;
            let task = tasks
                .iter()
                .find(|task| matches!(task, FrameTask::WorldFrame { frame_in_view, .. } if *frame_in_view as usize == f))
                .unwrap_or_else(|| panic!("no WorldFrame task for frame {f} (t={t})"));
            render_frame_task(&scenario.video, &scenario, task).unwrap()
        };

        let mut samples = Vec::new();
        let start_f = (1.5 * fps as f64).round() as i32;
        let end_f = (2.5 * fps as f64).round() as i32;
        for f in start_f..=end_f {
            let t = f as f64 / fps as f64;
            samples.push((f, avg_luma(&render_at(t))));
        }

        // No single-frame jump: the old bug held scene A's halo at full
        // alpha for the entire pan, then a hard cut when the "single active
        // scene" branch took over at the pan's end.
        let mut max_jump = 0.0_f64;
        let mut worst = (0, 0);
        for w in samples.windows(2) {
            let jump = (w[1].1 - w[0].1).abs();
            if jump > max_jump {
                max_jump = jump;
                worst = (w[0].0, w[1].0);
            }
        }
        assert!(
            max_jump < 15.0,
            "avg-luma jump of {max_jump:.1} between frames {worst:?} — background must fade \
             gradually, not hold then cut. Samples: {samples:?}"
        );

        // Genuine fade, not a flat hold: the value partway through the pan
        // must sit strictly between the pre-pan and post-pan levels, not
        // equal either endpoint (the "never fades" half of the bug).
        let pre = samples.first().unwrap().1;
        let post = samples.last().unwrap().1;
        let mid = samples[samples.len() / 2].1;
        assert!(
            (mid - pre).abs() > 1.0 && (mid - post).abs() > 1.0,
            "mid-pan luma {mid:.1} must differ meaningfully from both pre-pan {pre:.1} and \
             post-pan {post:.1} — background never faded if it matches either endpoint"
        );
    }

    // Constat 7: `freeze_at` on a world-view scene must stop its animated
    // content exactly like it does in a slide view, not keep advancing.
    #[test]
    fn freeze_at_stops_animation_inside_a_world_view() {
        let json = r##"{
            "video": { "width": 200, "height": 200, "fps": 30, "background": "#000000" },
            "composition": [
                { "type": "world", "scenes": [
                    { "duration": 2.0, "freeze_at": 0.5, "children": [
                        { "type": "counter", "from": 0, "to": 200,
                          "style": { "font-size": 48, "color": "#ffffff" } }
                    ] }
                ] }
            ]
        }"##;
        let scenario = scenario(json);
        let tasks = build_frame_tasks(&scenario);
        // 2.0s @ 30fps = 60 WorldFrame tasks; freeze_at=0.5s = frame 15.
        assert_eq!(tasks.len(), 60);

        let render = |i: usize| render_frame_task(&scenario.video, &scenario, &tasks[i]).unwrap();
        let before_freeze = render(5); // t ~= 0.167s, counter still climbing
        let after_freeze_a = render(45); // t = 1.5s, well past freeze_at
        let after_freeze_b = render(55); // t ~= 1.833s, also well past freeze_at

        assert_ne!(
            before_freeze, after_freeze_a,
            "counter must have visibly changed before the freeze point"
        );
        assert_eq!(
            after_freeze_a, after_freeze_b,
            "frames 45 and 55 are both past freeze_at=0.5s and must be pixel-identical \
             (the counter must have stopped, not kept incrementing)"
        );
    }

    // Issue #164: `freeze_at` was applied by hand in five different render
    // paths inside `crates/rustmotion/src/engine/render/scene.rs`, and
    // nothing ever asserted the five agree — PR #152 only tested the world
    // path it was repairing (the test above). This is that missing test,
    // written before the `SceneTime` consolidation refactor: a scene whose
    // animated content spans everything a "frozen" frame must actually hold
    // still — the component tree (a counter), the per-scene camera (an x
    // keyframe), and the animated background (`gradient_shift`) — so a path
    // that forgot the clamp anywhere would show it.
    #[test]
    fn freeze_at_produces_the_same_frame_on_every_render_path() {
        let slide_json = r##"{
            "video": { "width": 200, "height": 200, "fps": 30, "background": "#000000" },
            "scenes": [
                { "duration": 2.0, "freeze_at": 0.5,
                  "background": { "preset": "gradient_shift",
                                   "colors": ["#101020", "#4422aa"], "speed": 60 },
                  "camera": { "keyframes": [
                      { "property": "x", "values": [
                          { "time": 0.0, "value": 0.0 }, { "time": 2.0, "value": 80.0 }
                      ] }
                  ] },
                  "children": [
                      { "type": "counter", "from": 0, "to": 200,
                        "style": { "font-size": 48, "color": "#ffffff" } }
                  ] }
            ]
        }"##;
        let slide = scenario(slide_json);
        let config = &slide.video;
        let scenes = slide.all_scenes_vec();
        let scene = scenes[0];

        // Frame 5 (t~0.167s) is before freeze_at=0.5s: content still moving.
        // Frames 45 (t=1.5s) and 55 (t~1.833s) are both well past it.
        let (pre, post_a, post_b) = (5u32, 45u32, 55u32);

        let render_full =
            |f: u32| render_scene_frame_scaled(config, scene, f, f as f64 / 30.0, 60, 1.0).unwrap();
        let render_bg = |f: u32| render_scene_bg_scaled(config, scene, f, 1.0).unwrap();
        let render_fg =
            |f: u32| render_scene_fg_scaled(config, scene, f, f as f64 / 30.0, 60, 1.0).unwrap();

        let pixel_paths: [(&str, &dyn Fn(u32) -> Vec<u8>); 3] = [
            ("render_scene_frame_scaled", &render_full),
            ("render_scene_bg_scaled", &render_bg),
            ("render_scene_fg_scaled", &render_fg),
        ];
        for (name, render) in pixel_paths {
            let before = render(pre);
            let after_a = render(post_a);
            let after_b = render(post_b);
            assert_ne!(
                before, after_a,
                "{name}: frame {pre} (pre-freeze) must differ from frame {post_a} (post-freeze)"
            );
            assert_eq!(
                after_a, after_b,
                "{name}: frames {post_a} and {post_b} are both past freeze_at=0.5s and must be \
                 pixel-identical"
            );
        }

        // render_scene_hits: a different output shape (bounding boxes, not
        // pixels) but the same claim — the camera pan (and therefore every
        // hit rect) must stop moving past the freeze point.
        let hit_rects = |f: u32| -> Vec<_> {
            render_scene_hits(config, scene, f)
                .into_iter()
                .map(|h| h.rect)
                .collect::<Vec<_>>()
        };
        let hits_pre = hit_rects(pre);
        let hits_post_a = hit_rects(post_a);
        let hits_post_b = hit_rects(post_b);
        assert_ne!(
            hits_pre, hits_post_a,
            "render_scene_hits: hit rects at frame {pre} (pre-freeze, camera still panning) \
             must differ from frame {post_a}"
        );
        assert_eq!(
            hits_post_a, hits_post_b,
            "render_scene_hits: hit rects at frames {post_a} and {post_b} (both past \
             freeze_at) must be identical — the camera pan must have stopped"
        );

        // render_world_frame_scaled: same scene, wrapped in a world view so
        // the world-specific freeze copy (the one PR #152 added — `.min()`
        // instead of the other four's `if`) is exercised too.
        let world_json = r##"{
            "video": { "width": 200, "height": 200, "fps": 30, "background": "#000000" },
            "composition": [
                { "type": "world", "scenes": [
                    { "duration": 2.0, "freeze_at": 0.5,
                      "background": { "preset": "gradient_shift",
                                       "colors": ["#101020", "#4422aa"], "speed": 60 },
                      "camera": { "keyframes": [
                          { "property": "x", "values": [
                              { "time": 0.0, "value": 0.0 }, { "time": 2.0, "value": 80.0 }
                          ] }
                      ] },
                      "children": [
                          { "type": "counter", "from": 0, "to": 200,
                            "style": { "font-size": 48, "color": "#ffffff" } }
                      ] }
                ] }
            ]
        }"##;
        let world = scenario(world_json);
        let tasks = build_frame_tasks(&world);
        let world_render = |frame_in_view: u32| -> Vec<u8> {
            let task = tasks
                .iter()
                .find(
                    |t| matches!(t, FrameTask::WorldFrame { frame_in_view: f, .. } if *f == frame_in_view),
                )
                .unwrap_or_else(|| panic!("no WorldFrame task for frame {frame_in_view}"));
            render_frame_task(&world.video, &world, task).unwrap()
        };
        let w_before = world_render(pre);
        let w_after_a = world_render(post_a);
        let w_after_b = world_render(post_b);
        assert_ne!(
            w_before, w_after_a,
            "render_world_frame_scaled: frame {pre} (pre-freeze) must differ from frame {post_a}"
        );
        assert_eq!(
            w_after_a, w_after_b,
            "render_world_frame_scaled: frames {post_a} and {post_b} (both past freeze_at) \
             must be pixel-identical"
        );
    }

    // Constat 8a: `scene.effects` (post-effects) must apply on WorldFrame
    // tasks, not just Normal/SlideTransition ones.
    #[test]
    fn post_effects_apply_on_world_frames() {
        let json = r##"{
            "video": { "width": 100, "height": 100, "background": "#ffffff" },
            "composition": [
                { "type": "world", "scenes": [
                    { "duration": 1.0, "children": [],
                      "effects": [ { "type": "vignette", "intensity": 0.9, "radius": 0.3 } ] }
                ] }
            ]
        }"##;
        let scenario = scenario(json);
        let tasks = build_frame_tasks(&scenario);
        let task = tasks
            .iter()
            .find(|t| matches!(t, FrameTask::WorldFrame { .. }))
            .expect("world frame task");
        let buf = render_frame_task(&scenario.video, &scenario, task).unwrap();

        let corner_r = buf[0] as u16;
        let center_base = (50 * 100 + 50) * 4;
        let center_r = buf[center_base] as u16;
        assert!(
            corner_r < center_r,
            "vignette must darken the corner of a WorldFrame: corner={corner_r} center={center_r}"
        );
    }

    // Constat 8b: a ViewTransition composite must carry the incoming view's
    // effects too, by symmetry with SlideTransition (so an effect present on
    // both sides' Normal frames doesn't disappear for the transition and pop
    // back).
    #[test]
    fn post_effects_apply_on_view_transition_frames() {
        let json = r##"{
            "video": { "width": 100, "height": 100, "fps": 10, "background": "#ffffff" },
            "composition": [
                { "type": "slide", "scenes": [
                    { "duration": 0.5, "children": [],
                      "effects": [ { "type": "vignette", "intensity": 0.9, "radius": 0.3 } ] }
                ] },
                { "type": "slide", "transition": { "type": "fade", "duration": 0.3 },
                  "scenes": [
                    { "duration": 0.5, "children": [],
                      "effects": [ { "type": "vignette", "intensity": 0.9, "radius": 0.3 } ] }
                ] }
            ]
        }"##;
        let scenario = scenario(json);
        let tasks = build_frame_tasks(&scenario);
        let task = tasks
            .iter()
            .find(|t| matches!(t, FrameTask::ViewTransition { .. }))
            .expect("view transition task");
        let buf = render_frame_task(&scenario.video, &scenario, task).unwrap();

        let corner_r = buf[0] as u16;
        let center_base = (50 * 100 + 50) * 4;
        let center_r = buf[center_base] as u16;
        assert!(
            corner_r < center_r,
            "vignette must darken the corner of a ViewTransition frame: corner={corner_r} center={center_r}"
        );
    }
}