scsh 1.41.19

Scoped Skills Helper — preflight a git repo and run its scoped skills in ephemeral containers.
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
use super::cast::cast_player_page;
use super::client_js::live_client_js;
use super::escape::esc;
use super::session::session_page;
use super::session_export::session_export_page;
use crate::daemon::model::{DaemonMode, ProcKind, ProcRecord, ProcStatus, Session, Store};

/// A one-proc store for the cast player page tests: the proc has a registered cast and
/// the given status.
fn store_with_cast_proc(status: ProcStatus) -> Store {
  let mut store = Store::new(DaemonMode::Persistent, 7274, 1);
  store.sessions.insert(
    "castab".into(),
    Session {
      id: "castab".into(),
      started_at: 1,
      ended_at: None,
      profile: Some("default".into()),
      kind: None,
      repo: "/tmp/repo".into(),
      branch: "main".into(),
      last_seen_at: 1,
      client_connected: true,
      run_pid: None,
      skills: vec![],
      procs: vec![ProcRecord {
        index: 0,
        previous_attempt: None,
        kind: ProcKind::Skill,
        label: "claude: add".into(),
        status,
        note: None,
        detail: None,
        fail_reason: None,
        container_name: None,
        container_runtime: None,
        cast_path: Some("/tmp/x.cast".into()),
        diff_path: None,
        skill_source: None,
        route: None,
        result_path: None,
        annotate_target: None,
        harness: Some("claude".into()),
        skill_name: Some("add".into()),
        model: None,
        started_at: Some(1),
        elapsed: None,
        lines: vec![],
      }],
      workflow: None,
      parent_session: None,
      supervisor: Default::default(),
    },
  );
  store
}

fn session_procs_html(html: &str) -> &str {
  let needle = r#"<div class="procs" id="session-procs">"#;
  let start = html.find(needle).expect("session-procs") + needle.len();
  let tail = &html[start..];
  // The procs div is the body's last element; the page footer is the script block.
  let end = tail.find("<script").expect("script block after procs");
  &tail[..end]
}

#[test]
fn esc_handles_basic_html() {
  assert_eq!(esc("<a>"), "&lt;a&gt;");
}

#[test]
fn running_container_names_its_runtime_without_guessing() {
  let mut store = store_with_cast_proc(ProcStatus::Running);
  let proc = &mut store.sessions.get_mut("castab").unwrap().procs[0];
  proc.container_name = Some("scsh-abcdef-run-add".into());
  proc.container_runtime = Some("container".into());
  let html = session_page(&store, "castab").unwrap();
  assert!(html.contains(r#"class="container-runtime-name">Apple Containers</span> · container: scsh-abcdef-run-add"#));
  assert!(html.contains("function containerRuntimeName"), "live updates must preserve the explicit runtime label");
  assert!(html.contains("Not recorded (legacy run)"), "old stored jobs must stay honest instead of guessing");
}

#[test]
fn browser_player_is_first_party_and_carries_no_third_party_license() {
  // The whole point of the first-party beecast-player: neither the session browser nor
  // the exported pages (same crate family) ship ANY third-party code.
  let js = super::PLAYER_JS;
  let css = super::PLAYER_CSS;
  assert!(js.contains("BeeCastPlayer"), "the first-party player global must be defined");
  assert!(js.contains("BeeCastVT"), "the DOM-free core must be bundled first");
  assert!(js.contains("Clean-room implementation"), "the clean-room statement rides in the asset");
  for banned in ["asciinema-player", "AsciinemaPlayer", "@license", "Apache"] {
    assert!(!js.contains(banned), "browser player JS must not carry '{banned}'");
    assert!(!css.contains(banned), "browser player CSS must not carry '{banned}'");
  }
}

/// Run the DOM-free VT core's behavior tests under Node (parsing all three asciicast
/// versions plus the terminal state machine). Skips silently when `node` is not on PATH —
/// the Rust-side structural tests above still gate the asset itself.
#[test]
fn vt_core_node_selftest() {
  if crate::runtime::which("node").is_none() {
    return;
  }
  let dir = std::env::temp_dir().join(format!("scsh-vt-selftest-{}", std::process::id()));
  std::fs::create_dir_all(&dir).unwrap();
  let bundle = dir.join("player.js");
  std::fs::write(&bundle, super::PLAYER_JS).unwrap();
  let script = format!(
    r#"
const assert = require('assert');
require({bundle:?});
const VT = globalThis.BeeCastVT;

// v3: intervals sum; term size from header; resize + marker events survive; # comments skip.
let c = VT.parseCast('{{"version":3,"term":{{"cols":10,"rows":3}}}}\n# note\n[0.5,"o","hi"]\n[0.5,"m","chapter"]\n[1.0,"r","20x5"]\n');
assert.strictEqual(c.cols, 10); assert.strictEqual(c.rows, 3);
assert.strictEqual(c.events.length, 3);
assert.strictEqual(c.duration, 2);
assert.strictEqual(c.events[2].t, 2);

// v2: absolute times.
c = VT.parseCast('{{"version":2,"width":80,"height":24}}\n[0.5,"o","a"]\n[2.0,"o","b"]\n');
assert.strictEqual(c.duration, 2); assert.strictEqual(c.events[1].t, 2);

// v1: one JSON doc, stdout deltas.
c = VT.parseCast('{{"version":1,"width":5,"height":2,"stdout":[[0.1,"x"],[0.2,"y"]]}}');
assert.strictEqual(c.cols, 5); assert.strictEqual(c.events.length, 2);
assert(Math.abs(c.duration - 0.3) < 1e-9);

// Plain text + CR/LF.
let t = new VT.Term(10, 3);
t.write('hello\r\nworld');
assert.deepStrictEqual(t.textLines(), ['hello', 'world', '']);

// CUP + overwrite mid-screen.
t.write('\x1b[1;3Hga');
assert.strictEqual(t.textLines()[0], 'hegao');

// ED 2 clears everything.
t.write('\x1b[2J');
assert.deepStrictEqual(t.textLines(), ['', '', '']);

// SGR runs merge; colors land on cells.
t = new VT.Term(10, 1);
t.write('\x1b[31mred\x1b[0m ok');
const runs = t.snapshot().rows[0];
assert.strictEqual(runs[0].text, 'red'); assert.strictEqual(runs[0].fg, 1);
assert.strictEqual(runs[1].fg, null);

// 256-color + truecolor.
t = new VT.Term(4, 1);
t.write('\x1b[38;5;196mX\x1b[38;2;1;2;3mY');
const r2 = t.snapshot().rows[0];
assert.strictEqual(r2[0].fg, 196);
assert.strictEqual(r2[1].fg, '#010203');
assert.strictEqual(VT.color256(196), '#ff0000');
assert.strictEqual(VT.color256(232), '#080808');

// Deferred wrap: printing in the last column does not wrap until the next char.
t = new VT.Term(3, 2);
t.write('abc');
assert.strictEqual(t.snapshot().cursor.y, 0);
t.write('d');
assert.deepStrictEqual(t.textLines(), ['abc', 'd']);

// Scroll region: LF at the region bottom scrolls only the region.
t = new VT.Term(5, 4);
t.write('aa\r\nbb\r\ncc\r\ndd');
t.write('\x1b[2;3r\x1b[3;1H\n');
const lines = t.textLines();
assert.strictEqual(lines[0], 'aa');
assert.strictEqual(lines[1], 'cc');
assert.strictEqual(lines[3], 'dd');

// Alternate screen: primary content comes back on exit.
t = new VT.Term(5, 2);
t.write('main');
t.write('\x1b[?1049h\x1b[Halt');
assert.strictEqual(t.textLines()[0], 'alt');
t.write('\x1b[?1049l');
assert.strictEqual(t.textLines()[0], 'main');

// DEC special graphics: tmux border characters.
t = new VT.Term(4, 1);
t.write('\x1b(0qqx\x1b(B');
assert.strictEqual(t.textLines()[0], '──│');

// Cursor hide/show.
t = new VT.Term(2, 1);
t.write('\x1b[?25l');
assert.strictEqual(t.snapshot().cursor.visible, false);
t.write('\x1b[?25h');
assert.strictEqual(t.snapshot().cursor.visible, true);

// OSC titles are consumed, never printed.
t = new VT.Term(8, 1);
t.write('\x1b]0;title\x07ok');
assert.strictEqual(t.textLines()[0], 'ok');

console.log('vt selftest OK');
"#,
    bundle = bundle
  );
  let out = std::process::Command::new("node")
    .arg("-")
    .arg("--input-type=commonjs")
    .stdin(std::process::Stdio::piped())
    .stdout(std::process::Stdio::piped())
    .stderr(std::process::Stdio::piped())
    .spawn()
    .and_then(|mut child| {
      use std::io::Write;
      child.stdin.take().unwrap().write_all(script.as_bytes())?;
      child.wait_with_output()
    })
    .expect("node runs");
  let _ = std::fs::remove_dir_all(&dir);
  assert!(
    out.status.success() && String::from_utf8_lossy(&out.stdout).contains("vt selftest OK"),
    "vt selftest failed:\nstdout: {}\nstderr: {}",
    String::from_utf8_lossy(&out.stdout),
    String::from_utf8_lossy(&out.stderr)
  );
}

#[test]
fn skipped_workflow_step_renders_as_a_dim_slashed_row() {
  let mut store = store_with_cast_proc(ProcStatus::Skipped);
  {
    let p = &mut store.sessions.get_mut("castab").unwrap().procs[0];
    p.cast_path = None; // a skipped step never ran, so it has no recording
    p.detail = Some("skipped — its when: gate is false".into());
    p.note = Some("step 2/2 · needs probe_credentials".into());
  }
  let html = session_page(&store, "castab").expect("session renders");
  let procs = session_procs_html(&html);
  assert!(procs.contains(r#"class="chamfer proc skipped""#), "got: {procs}");
  assert!(!procs.contains("class=\"glyph\""), "proc rows no longer carry a status glyph: {procs}");
  assert!(procs.contains(">skipped</span>"), "skipped elapsed phrase: {procs}");
  // A skipped step is FINISHED, so its collapsed row shows the outcome (the skip reason),
  // not the transient step note — same rule that puts a finished skill's answer in the row.
  assert!(
    procs.contains(r#"<span class="note dim">skipped — its when: gate is false</span>"#),
    "skip reason in the collapsed row: {procs}"
  );
  assert!(!procs.contains("data-proc-stop"), "skipped step has no Force stop: {procs}");
  // Live updates speak the same phrases; workflow graph keeps its own icon map.
  let js = live_client_js();
  assert!(js.contains("function elapsedPhrase"));
  assert!(js.contains("'skipped'"));
  assert!(js.contains("function wfDisplayState"), "workflow graph live state");
}

#[test]
fn start_panel_offers_project_creation_and_the_client_wires_it() {
  let store = Store::new(DaemonMode::Persistent, 7274, 1);
  let html = super::index_page(&store);
  for id in ["project-name", "project-create"] {
    assert!(html.contains(&format!("id=\"{id}\"")), "index page should contain #{id}");
  }
  assert!(html.contains("~/.scsh/projects/"), "the panel explains where projects live");
  assert!(html.contains("start-controls"), "Run rows use start-controls for full-width layout");
  assert!(html.contains("start-actions"), "Run action buttons are grouped for right alignment");
  assert!(html.contains(".start-controls"), "start-controls CSS ships");
  assert!(html.contains(".start-actions"), "start-actions CSS ships");
  let js = live_client_js();
  assert!(js.contains("/api/v1/projects/create"), "client js posts project creation");
  assert!(js.contains("function createProject"), "client js wires the button");
  assert!(js.contains("function handleRepoOpened"), "open and create share the response path");
  assert!(js.contains("function showToast"), "invalid-name feedback is a toast");
  assert!(!js.contains("suggestOpenExistingProject"), "existing names open via 200 create-or-open, not a toast");
  assert!(js.contains("projectNameOk"), "client rejects dots/slashes before POST");
  assert!(html.contains("no dots/slashes"), "placeholder documents the name rules");
  assert!(html.contains(".toast"), "toast styles ship with the page");
  assert!(html.contains(r#"class="section-label">Definitions"#), "defs panel is labeled Definitions");
  assert!(js.contains("list.scrollIntoView"), "open/create scrolls the actionable definitions list into view");
  assert!(html.contains("#defs-list { padding-top: 1.25rem; }"), "the first definition keeps a blank top inset");
}

/// Syntax-check the whole live client script under Node. Catches redeclared `const`/`let`,
/// stray braces, dropped function headers, and other parse errors that would abort the
/// inline `<script>` and leave the session browser dead (e.g. `Identifier 'det' has already
/// been declared`). Skips silently when `node` is not on PATH — same pattern as the VT
/// selftest.
#[test]
fn live_client_js_parses_under_node() {
  if crate::runtime::which("node").is_none() {
    return;
  }
  let dir = std::env::temp_dir().join(format!("scsh-live-js-check-{}", std::process::id()));
  std::fs::create_dir_all(&dir).unwrap();
  let path = dir.join("live-client.js");
  std::fs::write(&path, live_client_js()).unwrap();
  let out = std::process::Command::new("node").arg("--check").arg(&path).output().expect("spawn node --check");
  assert!(
    out.status.success(),
    "live_client_js must parse: {}\n{}",
    String::from_utf8_lossy(&out.stderr),
    String::from_utf8_lossy(&out.stdout)
  );
  let _ = std::fs::remove_dir_all(&dir);
}

#[test]
fn running_cast_preview_starts_near_the_end() {
  let js = live_client_js();
  // A still-running proc's player opens in DECLARED-LIVE mode: parked at the growing edge,
  // the seek bar pinned full-width in live green (player.setLive) — not a near-end
  // autoplay whose playhead jitters as the duration grows.
  assert!(js.contains("box._live = true; createCastPlayer(box, 'end')"), "running casts open live at the edge");
  assert!(!js.contains("near-end"), "the jittery near-end preview is gone");
  assert!(js.contains("beecast-livechange"), "scsh tracks the player's live state");
}

#[test]
fn fullscreen_cast_refresh_preserves_the_active_player_dom() {
  let js = live_client_js();
  let create = js
    .split("function createCastPlayer")
    .nth(1)
    .and_then(|tail| tail.split("function focusCastPlayer").next())
    .expect("createCastPlayer body");
  let defer_at = create.find("if (castOwnsFullscreen(box))").expect("fullscreen preservation guard");
  let dispose_at = create.find("box._player.dispose()").expect("player replacement path");
  assert!(defer_at < dispose_at, "fullscreen must be checked before the active DOM node is disposed");
  assert!(create.contains("box._deferredPlayerRefresh = { startAt, autoplay }"), "repeated refreshes coalesce");

  let fullscreen_change = js
    .split("function onCastFullscreenChange")
    .nth(1)
    .and_then(|tail| tail.split("function procHtml").next())
    .expect("fullscreenchange body");
  assert!(fullscreen_change.contains("if (!refresh || castOwnsFullscreen(box)) return;"));
  assert!(fullscreen_change.contains("createCastPlayer(box, refresh.startAt, refresh.autoplay)"));
  assert!(js.contains("document.addEventListener('fullscreenchange', onCastFullscreenChange)"));
  assert!(js.contains("document.addEventListener('webkitfullscreenchange', onCastFullscreenChange)"));
}

#[test]
fn ui_review_fixes_hold() {
  // 1. Agent-route badges: the chamfer overlay must not swallow the text (the
  //    empty-rectangle bug — .agent-badge's inner span needs the z-index lift too).
  let html = super::index_page(&Store::new(DaemonMode::Persistent, 7274, 1));
  assert!(
    html.contains(".badge > span, .session-status > span, .agent-badge > span"),
    "agent-badge text must sit above the chamfer overlay"
  );
  // 2. Clicking something that renders inputs further down scrolls there.
  let js = live_client_js();
  assert!(js.matches("scrollIntoView").count() >= 2, "def form + defs panel scroll into view");
  // 3. A finished proc's collapsed row shows its ANSWER, not the stale run note.
  let mut store = store_with_cast_proc(ProcStatus::Ok);
  {
    let p = &mut store.sessions.get_mut("castab").unwrap().procs[0];
    p.detail = Some("2 + 3 = 5".into());
    p.note = Some("claude run…".into());
  }
  let page = session_page(&store, "castab").expect("session renders");
  assert!(page.contains(r#"<span class="note dim">2 + 3 = 5</span>"#), "the answer rides the collapsed row");
  assert!(!page.contains(r#"<span class="note dim">claude run…</span>"#), "the stale note does not");
  // 4. The meta island is purple and owns the action buttons (top-right corner).
  assert!(page.contains(r#"<div class="chamfer card card--accent-left-purple"><div class="session-actions">"#));
  // 5. Proc islands wear status on the left accent stripe (and tint the label). The
  //    row is a chamfer: the stripe is painted by the outer layer, wrapping the cut
  //    corners, and the ::before surface insets past it.
  assert!(html.contains("details.proc.ok { --accent: var(--green); }"));
  assert!(html.contains("details.proc.running { --accent: var(--orange); }"));
  assert!(html.contains("var(--proc-border, var(--border)) 0);"));
  assert!(html.contains("details.proc.running summary .label { color: var(--orange); }"));
  assert!(
    html.contains(".wf-node.wf-running { --accent: var(--orange); --node-border: rgba(210, 153, 34, 0.55); }"),
    "graph running matches proc orange"
  );
  assert!(html.contains(".wf-node.wf-stalled { --accent: var(--purple); }"), "abandoned/stalled is purple");
  assert!(html.contains(".wf-node.wf-stopped { --accent: var(--red); }"), "stopped shares fail red");
  assert!(
    html.contains(".wf-node.wf-terminating { --accent: var(--orange);"),
    "terminating shares running orange"
  );
  // Status glyphs speak the diamond language of the chamfered style (same 45° family
  // as the connection dot): hollow diamond waiting, filled diamond running — no circles.
  assert_eq!(super::proc::status_glyph(crate::daemon::model::ProcStatus::Running), "");
  assert_eq!(super::proc::status_glyph(crate::daemon::model::ProcStatus::Waiting), "");
  assert!(js.contains("running:'◆'"), "the JS graph mirror uses the filled diamond");
  assert!(js.contains("waiting:'◇'"), "the JS graph mirror uses the hollow diamond");
  assert!(!js.contains("") && !js.contains(""), "no circle glyphs remain");
  // Running is orange EVERYWHERE — the fleet-row glyph included; cyan stays for waiting.
  assert!(html.contains(".running .glyph { color: var(--orange); }"), "fleet running diamond is orange");
  assert!(html.contains(".waiting .glyph { color: var(--cyan); }"), "waiting keeps cyan");
  // The running pulse is a brightness swell — the chamfer clip would swallow a
  // box-shadow halo — and the zoom cluster is chamfered like every other control.
  assert!(html.contains("50% { filter: brightness(1.45); }"), "running nodes pulse via brightness");
  assert!(!html.contains(".wf-node.wf-flash"), "the dead box-shadow flash rule is gone");
  assert!(html.contains(r#"class="chamfer" data-wf-zoom-fit"#), "zoom buttons are chamfered");
  assert!(js.contains("stalled:'Abandoned'"), "legend label is Abandoned");
  assert!(js.contains("done:'Succeeded'"), "successful graph tasks are called Succeeded, not Done");
  assert!(js.contains("failed:'Job failed'"), "graph carries an explicit overall failure verdict");
  {
    let p = &mut store.sessions.get_mut("castab").unwrap().procs[0];
    p.elapsed = Some(18.0);
  }
  let page_with_elapsed = session_page(&store, "castab").expect("session renders");
  assert!(
    page_with_elapsed.contains(r#"data-proc-elapsed="0">done in 18s</span>"#),
    "ok rows say done in N: {page_with_elapsed}"
  );
  // Scoped to the proc markup: the client JS legitimately writes glyph spans (fleet sync).
  assert!(!session_procs_html(&page_with_elapsed).contains(r#"class="glyph""#), "no status glyph on proc rows");
  // 6. The builtin source badge wears purple.
  assert!(html.contains(".badge--purple"), "purple badge class ships");
  assert!(live_client_js().contains(r#"chamfer badge badge--purple"><span>builtin"#), "builtin badge is purple");
}

#[test]
fn cached_recording_duration_is_not_presented_as_attempt_runtime() {
  let mut legacy = store_with_cast_proc(ProcStatus::Ok);
  {
    let proc = &mut legacy.sessions.get_mut("castab").unwrap().procs[0];
    proc.elapsed = Some(480.946);
    proc.detail = Some("grade: good  (cached · 2026-07-16 16:28 UTC)".into());
  }
  let legacy_page = session_page(&legacy, "castab").expect("legacy cache hit renders");
  let legacy_procs = session_procs_html(&legacy_page);
  assert!(legacy_procs.contains(r#"data-proc-elapsed="0">cache hit</span>"#), "got: {legacy_procs}");
  assert!(!legacy_procs.contains("done in 8m") && !legacy_procs.contains("idle 8m"), "got: {legacy_procs}");

  let mut current = legacy;
  {
    let proc = &mut current.sessions.get_mut("castab").unwrap().procs[0];
    proc.elapsed = Some(0.25);
    proc.detail =
      Some("grade: good  (cached · 2026-07-16 16:28 UTC · source run took 8m 0s)".into());
  }
  let current_page = session_page(&current, "castab").expect("current cache hit renders");
  let current_procs = session_procs_html(&current_page);
  assert!(current_procs.contains(r#"data-proc-elapsed="0">cache hit in 0s</span>"#), "got: {current_procs}");
  assert!(current_procs.contains(r#"<span class="idle"></span>"#), "cache hit has no idle clock: {current_procs}");
}

#[test]
fn cards_are_chamfered_islands() {
  let html = super::index_page(&Store::new(DaemonMode::Persistent, 7274, 1));
  // Every island card is a chamfered surface: the outer layer is the border (or the
  // accent stripe, painted wide enough to wrap the cut corner), the ::before is the fill.
  assert!(html.contains(r#"<div class="chamfer card card--accent-left-green">"#), "Run card is chamfered");
  assert!(html.contains(r#"<div class="chamfer card card--accent-left-cyan">"#), "Jobs card is chamfered");
  assert!(html.contains(".chamfer > * { position: relative; z-index: 1; }"), "children lift above the overlay");
  assert!(html.contains("var(--accent) 0 calc(var(--cut) + var(--accent-w)),"), "accent stripe wraps the chamfer");
  assert!(html.contains(".card--accent-left-cyan { --accent: var(--cyan); }"));
  assert!(!html.contains(".card--accent-left-cyan { border-left"), "no rounded-era accent borders remain");
  // clip-path clips box-shadows: the expanded graph modal must cast a drop-shadow instead.
  assert!(html.contains("filter: drop-shadow(0 24px 80px rgba(0,0,0,0.65));"));
  assert!(!html.contains("box-shadow: 0 24px 80px"));
}

#[test]
fn a_retried_route_is_visibly_a_retry() {
  use crate::daemon::workflow::{WorkflowMeta, WorkflowNodeMeta};
  fn skill(index: usize, name: &str, route: &str, status: ProcStatus, fail: Option<&str>) -> ProcRecord {
    ProcRecord {
      index,
      previous_attempt: None,
      kind: ProcKind::Skill,
      label: format!("claude: {name}"),
      status,
      note: None,
      detail: Some("done".into()),
      fail_reason: fail.map(Into::into),
      container_name: None,
      container_runtime: None,
      cast_path: None,
      diff_path: None,
      skill_source: Some("add".into()),
      route: Some(route.into()),
      result_path: None,
      annotate_target: None,
      harness: Some("claude".into()),
      skill_name: Some(name.into()),
      model: None,
      started_at: Some(1),
      elapsed: Some(2.0),
      lines: vec![],
    }
  }
  let mut retry = skill(2, "add-claude", "claude", ProcStatus::Ok, None);
  retry.previous_attempt = Some(0);
  let mut store = Store::new(DaemonMode::Persistent, 7274, 1);
  store.sessions.insert(
    "retryv".into(),
    Session {
      id: "retryv".into(),
      started_at: 1,
      ended_at: Some(10),
      profile: Some("default".into()),
      kind: Some("profile".into()),
      repo: "/tmp/repo".into(),
      branch: "main".into(),
      last_seen_at: 10,
      client_connected: false,
      run_pid: None,
      skills: vec![],
      procs: vec![
        skill(0, "add-claude", "claude", ProcStatus::Fail, Some(crate::failure::reason::CONTAINER_TIMEOUT)),
        skill(1, "add-opencode", "opencode", ProcStatus::Ok, None),
        retry,
      ],
      workflow: Some(WorkflowMeta {
        nodes: vec![
          WorkflowNodeMeta {
            id: "add-claude".into(),
            proc_index: Some(2),
            order: 0,
            needs: vec![],
            conditional: false,
            when_summary: None,
          },
          WorkflowNodeMeta {
            id: "add-opencode".into(),
            proc_index: Some(1),
            order: 1,
            needs: vec![],
            conditional: false,
            when_summary: None,
          },
        ],
      }),
      parent_session: None,
      supervisor: Default::default(),
    },
  );
  let html = session_page(&store, "retryv").expect("session renders");
  // The failed attempt cross-links its retry; the retry wears an attempt chip.
  assert!(
    html.contains(r##"<a class="proc-retry-link" href="#proc-2""##),
    "failed attempt links its retry: {html}"
  );
  assert!(html.contains("superseded — see attempt 2 ↓"), "link names the retry's ordinal");
  assert!(html.contains(r#"attempt-chip"><span>attempt 2</span>"#), "retry row wears an attempt chip");
  assert!(
    html.contains(r##"<a class="proc-original-link" href="#proc-0""##),
    "replacement links directly back to the original attempt"
  );
  assert_eq!(html.matches(r#"id="task-add-claude""#).count(), 1, "only the authoritative attempt owns task id");
  assert!(html.contains(r#"id="proc-2" data-index="2" data-workflow-step="add-claude""#));
  assert!(
    html.contains("alias.closest('details.proc')") && html.contains("taskAnchor.remove()"),
    "live task aliases move to the replacement while navigation still opens its proc panel"
  );
  // The graph node (bound to the newest attempt) says it is a second attempt.
  assert!(html.contains(r#"<span class="wf-attempt"> · attempt 2</span>"#), "node state line shows the attempt");
  assert!(html.contains("Attempt 2 of 2 — an earlier attempt failed and was retried"), "tooltip explains");
  // The fleet comparison shows one row per route — the superseded attempt is gone.
  let fleet = html.split(r#"class="chamfer fleet""#).nth(1).and_then(|s| s.split("</section>").next()).expect("fleet");
  assert!(fleet.contains(r#"data-proc="2""#), "the retry represents its route");
  assert!(!fleet.contains(r#"data-proc="0""#), "the superseded attempt is not a fleet row");
  assert!(fleet.contains("2 ok, 0 fail"), "the rollup counts authoritative attempts only: {fleet}");
  // The single-attempt route carries neither chip nor link.
  let procs = session_procs_html(&html);
  assert_eq!(procs.matches("attempt-chip").count(), 1, "chip on the retry row only");
  assert_eq!(procs.matches("proc-retry-link").count(), 1, "link on the superseded row only");
}

#[test]
fn client_lifecycle_ignores_superseded_retry_failures() {
  // The JS fallback derivation mirrors Session::proc_is_superseded: a failed attempt
  // whose route was re-run by a later proc must not turn the job badge red.
  let js = live_client_js();
  assert!(js.contains("function procIsSuperseded"), "supersession helper ships");
  assert!(
    js.contains("p.status === 'fail' && !procIsSuperseded(session, p)"),
    "the failed set excludes superseded attempts"
  );
}

#[test]
fn overlays_are_chamfered() {
  // The floating chrome — confirm dialog, toast, instant tooltip — shares the chamfer
  // language, with drop-shadows (a clip-path swallows box-shadows) for depth.
  let html = super::index_page(&Store::new(DaemonMode::Persistent, 7274, 1));
  let js = live_client_js();
  assert!(js.contains("panel.className = 'chamfer scsh-dialog';"), "confirm dialog is chamfered");
  assert!(js.contains("el.className = 'chamfer toast';"), "toast is chamfered");
  assert!(js.contains("tip.className = 'chamfer ui-tip';"), "tooltip is chamfered");
  assert!(html.contains(".toast::before { background: #1c2128; }"));
  assert!(html.contains(".ui-tip::before { background: #1c2128; }"));
  let session = session_page(&store_with_cast_proc(ProcStatus::Running), "castab").expect("session renders");
  assert!(session.contains(".scsh-dialog::before { background: var(--surface); }"));
  assert!(session.contains("filter: drop-shadow(0 16px 40px rgba(0, 0, 0, 0.55));"));
}

#[test]
fn session_header_carries_breadcrumbs_and_honest_kind() {
  // The top island: location path on the left (bold, plain text), daemon status right.
  let mut store = store_with_cast_proc(ProcStatus::Running);
  {
    let s = store.sessions.get_mut("castab").unwrap();
    s.kind = Some("workflow".into());
    s.profile = Some("arith".into());
  }
  let html = session_page(&store, "castab").expect("session renders");
  assert!(
    html.contains(r#"<a href="/">scsh</a><span class="crumb-sep">›</span><a href="/jobs">jobs</a><span class="crumb-sep">›</span><a class="job-id" href="/job/castab">castab</a>"#),
    "breadcrumb permalinks in the top island (the id in a fixed font)"
  );
  // The status dot sits at the very RIGHT edge of the island.
  assert!(
    html.contains(r#"{}<span class="dot" aria-hidden="true"></span></span></div>"#.trim_start_matches("{}")),
    "dot last in the island"
  );
  assert!(html.contains(r#"<span class="daemon-right">"#), "daemon status keeps the island's right side");
  assert!(!html.contains("<h1>"), "the body no longer duplicates the path as an h1");
  // Kind/profile/lifecycle live on the page lede — not repeated in the purple island.
  assert!(html.contains(r#"class="page-lede""#), "got lede: {html}");
  assert!(html.contains("workflow <strong>arith</strong>"), "lede names the workflow: {html}");
  assert!(!html.contains(r#"class="session-kind""#), "purple island no longer repeats kind: {html}");
  assert!(!html.contains(r#"<ul class="skills">"#), "purple island drops skills list: {html}");
  // A session with no kind (persisted by an older build) still reads as a profile.
  let mut old = store_with_cast_proc(ProcStatus::Running);
  old.sessions.get_mut("castab").unwrap().profile = Some("default".into());
  let html = session_page(&old, "castab").expect("session renders");
  assert!(html.contains("profile <strong>default</strong>"), "lede defaults kind to profile: {html}");
  assert!(!html.contains(r#"class="session-kind""#), "no session-kind on default profile: {html}");
  // The Jobs tab is home and keeps the retained index crumb hidden; the Run URL renders it
  // on first paint so returning through a breadcrumb never drops the tab name.
  let html = super::index_page(&store);
  assert!(html.contains(r#"<span id="index-crumb-tail" hidden>"#), "Jobs (home) hides the retained index crumb: {html}");
  let html = super::index::index_page_for(&store, None, super::index::IndexTab::Run);
  assert!(
    html.contains(
      r#"<span class="crumbs"><a href="/">scsh</a><span id="index-crumb-tail"><span class="crumb-sep">›</span><a id="index-crumb" href="/run">run</a></span></span>"#
    ),
    "Run stays present in the top island on /run: {html}"
  );
}

#[test]
fn stop_strip_and_kill_buttons_ignore_zombie_sessions() {
  // A dead client's session keeps procs "running" forever. Force stop is omitted —
  // there is nothing left to stop.
  let store = store_with_cast_proc(ProcStatus::Running);
  let html = super::index_page(&store);
  assert!(!html.contains(r#"data-harness-stop=""#), "zombie sessions must not raise stop-all buttons");
  let page = session_page(&store, "castab").expect("session renders");
  assert!(!page.contains(r#"data-proc-stop="0""#), "zombie omits per-proc Force stop: {page}");
  assert!(!page.contains(r#"data-proc-restart="0""#), "zombie omits per-proc Force restart: {page}");
  assert!(!page.contains(r#"id="session-stop""#), "zombie omits job Force stop: {page}");

  // The same session, seen moments ago, gets an enabled kill and a restart.
  let mut live = store_with_cast_proc(ProcStatus::Running);
  live.sessions.get_mut("castab").unwrap().last_seen_at = crate::daemon::paths::now_unix_secs();
  let html = super::index_page(&live);
  assert!(html.contains(r#"data-harness-stop="claude""#), "live sessions raise the stop-all button");
  let page = session_page(&live, "castab").expect("session renders");
  assert!(page.contains(r#"data-proc-stop="0""#), "live sessions offer per-proc kill");
  assert!(!page.contains(r#"data-proc-stop="0" disabled"#), "live kill is enabled: {page}");
  assert!(page.contains(r#"data-proc-restart="0""#), "live skill runs offer per-proc Force restart: {page}");
  assert!(page.contains(r#"id="session-stop""#), "live job offers Force stop");

  let proc = &mut live.sessions.get_mut("castab").unwrap().procs[0];
  proc.fail_reason = Some(crate::failure::reason::STOP_REQUESTED.into());
  proc.detail = Some("terminating all claude containers from the session browser".into());
  let html = super::index_page(&live);
  assert!(html.contains("Terminating all claude (1)…"), "harness strip acknowledges teardown: {html}");
  assert!(!html.contains(r#"data-harness-stop="claude""#), "a terminating harness cannot be stopped twice");
  let page = session_page(&live, "castab").expect("terminating session renders");
  assert!(page.contains(r#"class="chamfer proc terminating""#), "proc island turns orange while stopping: {page}");
  assert!(!page.contains(r#"data-proc-stop="0""#), "a terminating proc cannot be stopped twice: {page}");
  assert!(!page.contains(r#"data-proc-restart="0""#), "a terminating proc cannot be restarted: {page}");

  // A pending browser restart also parks both buttons and turns the island orange.
  let proc = &mut live.sessions.get_mut("castab").unwrap().procs[0];
  proc.fail_reason = Some(crate::failure::reason::RESTART_REQUESTED.into());
  proc.detail = Some("terminating container from the session browser; a fresh attempt follows".into());
  let page = session_page(&live, "castab").expect("restarting session renders");
  assert!(page.contains(r#"class="chamfer proc terminating""#), "proc island turns orange while restarting: {page}");
  assert!(!page.contains(r#"data-proc-stop="0""#), "a restarting proc cannot be stopped: {page}");
  assert!(!page.contains(r#"data-proc-restart="0""#), "a restarting proc cannot be restarted twice: {page}");
}

#[test]
fn session_meta_agrees_with_lifecycle_badge() {
  // WEB-UI §6 / ENG §13: channels must not disagree. A job that missed its phase-aware
  // liveness deadline must not say "failed" in the badge and "still running" in Ended.
  let zombie = store_with_cast_proc(ProcStatus::Running);
  let page = session_page(&zombie, "castab").expect("session renders");
  assert!(
    page.contains("· failed"),
    "zombie lifecycle on lede: {page}"
  );
  assert!(!page.contains(r#"class="session-kind""#), "no island status chip: {page}");
  // Ended shows the last-seen timestamp (effective end), not the status phrase.
  assert!(page.contains(r#"data-session-ended>"#), "Ended present: {page}");
  assert!(!page.contains(r#"data-session-ended>still running</dd>"#), "Ended must not contradict: {page}");
  assert!(
    page.contains(r#"data-session-ended>19700101-003001 UTC</dd>"#),
    "a started job uses the 30-minute idle deadline: {page}"
  );
  // Repo above Branch.
  let repo = page.find("<dt>Repo</dt>").expect("Repo");
  let branch = page.find("<dt>Branch</dt>").expect("Branch");
  assert!(repo < branch, "Repo should sit above Branch: {page}");
  assert!(page.contains(r#"data-session-started>"#), "meta is server-rendered on first paint");
  assert!(page.contains(r#"data-last-seen="1""#), "last_seen seeds the client lifecycle");

  let mut live = store_with_cast_proc(ProcStatus::Running);
  live.sessions.get_mut("castab").unwrap().last_seen_at = crate::daemon::paths::now_unix_secs();
  let page = session_page(&live, "castab").expect("session renders");
  assert!(page.contains(r#"data-session-ended>still running</dd>"#), "live Ended: {page}");
  assert!(page.contains(r#"id="session-stop""#), "live Force stop stays available");
}

#[test]
fn index_page_shows_colored_harness_chips_per_proc() {
  let mut store = store_with_cast_proc(ProcStatus::Running);
  let full_repo = "/Users/dima/github/dimacurrentai/a-repository-name-that-is-long-enough-to-be-truncated";
  store.sessions.get_mut("castab").unwrap().repo = full_repo.into();
  // A second, finished proc on another harness: its chip renders dimmed.
  {
    let session = store.sessions.get_mut("castab").unwrap();
    let mut done = session.procs[0].clone();
    done.index = 1;
    done.status = ProcStatus::Ok;
    done.harness = Some("grok".into());
    done.label = "grok: add".into();
    session.procs.push(done);
    // Build procs never get a chip — only skill runs count.
    let mut build = session.procs[0].clone();
    build.index = 2;
    build.kind = ProcKind::Build;
    build.harness = Some("codex".into());
    session.procs.push(build);
    for index in 3..11 {
      let mut extra = session.procs[1].clone();
      extra.index = index;
      extra.harness = Some("cursor".into());
      session.procs.push(extra);
    }
  }
  let html = super::index_page(&store);
  // A running chip's tip is just `harness · skill`; its start time rides in
  // data-tip-running, from which the tip module ticks a live "running for …" line.
  // Flat jobs have no workflow task aliases, so each chip uses its permanent proc anchor.
  assert!(
    html.contains(r#"<a class="chamfer hchip hchip--claude" href="/job/castab#proc-0"#)
      && html.contains(r#"data-tip="claude · add" data-tip-running="1">C</a>"#),
    "got: {html}"
  );
  // A finished chip's tip is two lines: `harness · skill`, then the plain status word.
  assert!(
    html.contains(r#"<a class="chamfer hchip hchip--grok hchip--done" href="/job/castab#proc-1"#)
      && html.contains("data-tip=\"grok · add\ndone\">G</a>"),
    "got: {html}"
  );
  assert!(!html.contains(r#"class="hchip hchip--codex"#), "build procs must not render a chip");
  assert!(
    html.contains(r#"class="session-procs-cell"><span class="chip-count""#),
    "the total precedes the bounded chip sample"
  );
  assert!(html.contains(r#"<span class="chip-overflow">+ 2</span>"#), "only eight harness chips are shown: {html}");
  assert!(
    html.contains(&format!(r#"class="repo-copy" data-copy-value="{full_repo}" data-tip="{full_repo}""#)),
    "truncated repo path copies and discloses its full, unabridged value: {html}"
  );
  // The stylesheet distinguishes the same letter by harness color, and the client JS
  // mirrors the markup for live re-renders.
  assert!(html.contains(".hchip--claude"));
  assert!(html.contains(".hchip--codex"));
  assert!(html.contains("function harnessChipsHtml"));
  assert!(html.contains("function procRunHref"), "live chips preserve the same deep links");
  assert!(html.contains("function syncProcFromLocation"), "flat-job links open their selected run");
  assert!(html.contains("navigator.clipboard.writeText(value)"), "repo copy uses the Clipboard API");
  assert!(html.contains("'Copied!'"), "successful copies confirm through the tooltip");
  assert!(html.contains("if (!copy._scshCopyTimer)"), "live ticks preserve copy confirmation long enough to read");
}

#[test]
fn job_page_renders_the_loop_convergence_table() {
  let _env = crate::runtime::test_env_lock();
  let dir = std::env::temp_dir().join(format!("scsh-conv-{}", crate::runtime::random_nonce_6()));
  std::fs::create_dir_all(&dir).unwrap();
  // Three cycles of a scoring loop: rising, then nearly flat — the xrnvno trajectory that
  // previously had to be reconstructed from casts by hand.
  let cycle = |index: usize, iteration: usize, mean: f64, verdict: &str, approved: bool| {
    let path = dir.join(format!("c{iteration}.json"));
    std::fs::write(
      &path,
      format!(
        r#"{{"verdict":"{verdict}","approved":{approved},"feedback":{{"mean":{mean},
        "counts":{{"excellent":7,"good":6,"average":2}}}}}}"#
      ),
    )
    .unwrap();
    ProcRecord {
      index,
      previous_attempt: None,
      kind: ProcKind::Skill,
      label: format!("codex: collect (cycle {iteration})"),
      status: ProcStatus::Ok,
      note: None,
      detail: Some("scored".into()),
      fail_reason: None,
      container_name: None,
      container_runtime: None,
      cast_path: None,
      diff_path: None,
      skill_source: Some("collect".into()),
      route: None,
      result_path: Some(path.to_string_lossy().into_owned()),
      annotate_target: None,
      harness: Some("codex".into()),
      skill_name: Some(format!("collect-while-collect-{iteration}")),
      model: None,
      started_at: Some(1),
      elapsed: Some(2.0),
      lines: vec![],
    }
  };
  let mut store = Store::new(DaemonMode::Persistent, 7274, 1);
  let mut session = store_with_cast_proc(ProcStatus::Ok).sessions.remove("castab").unwrap();
  session.id = "convrg".into();
  session.procs =
    vec![cycle(0, 1, 3.60, "not met", false), cycle(1, 2, 4.10, "not met", false), cycle(2, 3, 4.12, "met", true)];
  store.sessions.insert("convrg".into(), session);

  let html = session_page(&store, "convrg").expect("session renders");
  let panel = html
    .split(r#"class="chamfer fleet fleet-rounds""#)
    .nth(1)
    .and_then(|s| s.split("</section>").next())
    .expect("convergence panel renders");
  assert!(panel.contains("· <code>collect</code> · 3 cycles"), "the panel names the reporting step: {panel}");
  assert!(panel.contains("3.60 → 4.12"), "the title states the whole trajectory: {panel}");
  assert!(panel.contains(r#"<span class="round-mean">3.60</span>"#), "first cycle has no delta: {panel}");
  assert!(panel.contains(r#"<span class="round-up">▲ +0.50</span>"#), "a rising cycle is green and signed: {panel}");
  // 4.10 → 4.12 still moves; only sub-0.005 noise reads as flat.
  assert!(panel.contains(r#"<span class="round-up">▲ +0.02</span>"#), "small real moves still show: {panel}");
  assert!(panel.contains("excellent ×7 · good ×6 · average ×2"), "grades read highest-first: {panel}");
  assert!(panel.contains(r#"<span class="round-met">met</span>"#), "a met bar is called out: {panel}");
  assert!(panel.contains(r#"data-proc="2""#), "each cycle jumps to its own step: {panel}");
  assert!(html.contains(".round-up { color: var(--green); }"), "the trend colors ship in the stylesheet");

  // A job whose loop reports nothing scoreable renders no convergence panel at all.
  let plain = store_with_cast_proc(ProcStatus::Ok);
  assert!(
    !session_page(&plain, "castab").expect("renders").contains("fleet-rounds"),
    "no round reports, no panel"
  );
  let _ = std::fs::remove_dir_all(&dir);
}

#[test]
fn jobs_table_paginates_beyond_the_first_page() {
  use super::index::JOBS_PAGE_SIZE;
  // The page embeds the client JS, which mirrors the pagination markup as string
  // literals — every assertion below scopes to the served tbody, not the whole page.
  fn tbody(html: &str) -> &str {
    let start = html.find("id=\"sessions-body\"").expect("jobs tbody");
    &html[start..start + html[start..].find("</tbody>").expect("tbody closes")]
  }
  // A single page renders every row plainly — no hidden rows, no button.
  let html = super::index_page(&store_with_cast_proc(ProcStatus::Ok));
  assert!(!tbody(&html).contains("jobs-overflow"), "one job needs no pagination: {}", tbody(&html));
  assert!(!tbody(&html).contains("jobs-more-row"), "one job raises no Show-more row: {}", tbody(&html));

  // Fifty-seven jobs past the page boundary: they are all served, the overflow is
  // class-hidden, and one button row reveals the next page ("Show 50 more of 57").
  let mut store = store_with_cast_proc(ProcStatus::Ok);
  let base = store.sessions.remove("castab").unwrap();
  for i in 0..(JOBS_PAGE_SIZE + 57) {
    let mut s = base.clone();
    s.id = format!("pg{i:04}");
    s.started_at = 1000 + i as u64;
    s.last_seen_at = s.started_at;
    s.ended_at = Some(s.started_at + 1);
    store.sessions.insert(s.id.clone(), s);
  }
  let html = super::index::index_page_for(&store, None, super::index::IndexTab::Jobs);
  let rows = tbody(&html);
  assert_eq!(
    rows.matches("<tr class=\"jobs-overflow\" data-session-id").count(),
    57,
    "rows past the first page are served hidden"
  );
  assert_eq!(
    rows.matches("<tr data-session-id").count(),
    JOBS_PAGE_SIZE,
    "exactly one page of rows is revealed on first paint"
  );
  assert_eq!(rows.matches("jobs-more-row").count(), 1, "one Show-more row: {rows}");
  assert!(rows.contains("<span>Show 50 more of 57</span>"), "the button counts a full step and the total: {rows}");
  // Fewer hidden than a page: the button names the exact remainder, with no "of" tail.
  let mut small = store_with_cast_proc(ProcStatus::Ok);
  let base = small.sessions.remove("castab").unwrap();
  for i in 0..(JOBS_PAGE_SIZE + 3) {
    let mut s = base.clone();
    s.id = format!("sm{i:04}");
    s.started_at = 1000 + i as u64;
    s.last_seen_at = s.started_at;
    s.ended_at = Some(s.started_at + 1);
    small.sessions.insert(s.id.clone(), s);
  }
  let html = super::index::index_page_for(&small, None, super::index::IndexTab::Jobs);
  assert!(tbody(&html).contains("<span>Show 3 more</span>"), "a short remainder is named exactly: {}", tbody(&html));
  // The live renderer mirrors the page size, the row markup, and the reveal wiring.
  let js = live_client_js();
  assert_eq!(JOBS_PAGE_SIZE, 50, "the JS mirror below pins the same number");
  assert!(js.contains("const JOBS_PAGE = 50"), "client page size mirrors JOBS_PAGE_SIZE");
  assert!(js.contains("function jobsLoadMoreRowHtml"), "client mirrors the Show-more row");
  assert!(js.contains("initJobsLoadMore"), "clicking the button reveals the next page in place");
  assert!(js.contains("i >= jobsVisible"), "live re-renders keep the revealed count");
}

#[test]
fn index_page_carries_the_setup_panel_and_its_client_wiring() {
  let store = Store::new(DaemonMode::Persistent, 7274, 1);
  let html = super::index_page(&store);
  assert!(html.contains("data-tab=\"setup\""), "nav label tab is Setup");
  assert!(html.contains(">Setup</button>"), "nav shows Setup, not Containers");
  assert!(!html.contains(">Containers</button>"), "Containers nav label is gone");
  assert!(html.contains("id=\"tab-setup\""), "setup panel id");
  assert!(html.contains("Harness setup"), "harness setup heading");
  assert!(html.contains("id=\"setup-cards\""), "harness cards container");
  assert!(html.contains("Images setup"), "images setup island");
  assert!(html.contains("card--accent-left-purple"), "images island uses purple accent");
  assert!(!html.contains("Advanced image management"), "no advanced disclosure");
  // Advanced still has the image table controls.
  for id in [
    "images-body",
    "images-build-selected",
    "images-build-stale",
    "images-build-all",
    "images-rebuild-base",
    "images-force",
  ] {
    assert!(html.contains(&format!("id=\"{id}\"")), "index page should contain #{id}");
  }
  // First paint already lists every harness + known image (§13: no empty limbo).
  assert!(html.contains("checking…"), "skeleton starts in checking…");
  for name in ["Claude", "Codex", "Grok", "Opencode", "Cursor"] {
    assert!(html.contains(name), "harness card {name} on first paint");
  }
  let cursor_card = html.find(">Cursor</strong>").expect("Cursor skeleton card");
  let opencode_card = html.find(">Opencode</strong>").expect("Opencode skeleton card");
  assert!(cursor_card < opencode_card, "OpenCode belongs after every native harness");
  assert!(html.contains("scsh-base:latest"), "base image row on first paint");
  for tag in
    ["scsh-opencode:latest", "scsh-claude:latest", "scsh-codex:latest", "scsh-grok:latest", "scsh-cursor:latest"]
  {
    assert!(html.contains(tag), "harness image {tag} on first paint");
  }
  let js = live_client_js();
  assert!(js.contains("/api/v1/setup"), "client js should fetch the setup API");
  assert!(js.contains("/api/v1/setup/tests"), "client js posts model probes");
  assert!(js.contains("/api/v1/images/build"), "client js should post builds");
  assert!(js.contains("function refreshSetup"), "setup refresh");
  assert!(js.contains("function startSetupTests"), "setup test starter");
  assert!(js.contains("setup-test-all"), "Test all defaults control");
  assert!(js.contains("data-setup-test"), "per-card Test selected");
  assert!(js.contains("setupCustomModels"), "custom models persist in ui prefs");
  assert!(js.contains("function markImagesChecking"), "refresh keeps rows visible while checking");
  assert!(js.contains("id === 'images'"), "images tab id remains a compatibility alias");
  assert!(!js.contains("loading…"), "must not replace the table with a blank loading row");
  assert!(js.contains("data-image-build"), "per-row build buttons are rendered");
  assert!(js.contains("data-setup-build"), "card Build/Update actions");
  assert!(html.contains("id=\"setup-test-all\""), "Test all defaults on the toolbar");
  // Subscription quota is click-to-start only: the card ships with the page, but the
  // quota JOB (one run per harness) is spawned exclusively from the button's POST.
  assert!(html.contains("id=\"setup-quota-btn\""), "Check quota button on the Setup tab");
  assert!(html.contains("id=\"setup-quota-body\""), "quota table body placeholder");
  assert!(js.contains("/api/v1/setup/quota"), "client js starts the quota job on click");
  assert!(js.contains("method: 'POST'"), "quota check starts a job, not a passive GET");
  assert!(js.contains("function pollQuotaJob"), "quota job progress poller");
  assert!(js.contains("/quota"), "aggregated per-run results are read after the job ends");
  assert!(js.contains("function renderQuota"), "quota renderer");
  assert!(!js.contains("fetchQuota()"), "quota is never fetched automatically — only from the button");
  assert!(js.contains("setup-models-hint"), "models section explains how to test");
  assert!(js.contains("ready to test"), "summary uses ready-to-test wording");
  assert!(js.contains("setupModelStatusHtml"), "model rows hide raw not_tested");
  assert!(js.contains("setup-ready"), "ready-to-test badge styling");
  assert!(js.contains("function startImageBuildOne"), "per-row build buttons are wired");
  assert!(html.contains("btn--orange btn--sm\" id=\"images-build-stale\"><span>Build stale"));
  assert!(html.contains("btn--purple btn--sm\" id=\"images-build-all\"><span>Build all"));
  assert!(js.contains("startImagesBuild('stale')"));
  assert!(js.contains("startImagesBuild('all')"));
  assert!(html.contains("image-action-cell"), "skeleton rows reserve the per-row action cell");
}

/// An installed-but-stopped container engine is a distinct state from "no runtime found",
/// and the Setup tab has to name it — with the command that fixes it — instead of showing
/// five red "Needs build" cards over buttons that cannot work.
#[test]
fn setup_panel_surfaces_a_stopped_container_engine() {
  let store = Store::new(DaemonMode::Persistent, 7274, 1);
  let html = super::index_page(&store);
  assert!(html.contains("id=\"setup-engine\""), "banner mount point above the cards");
  assert!(html.contains("id=\"setup-engine\" class=\"chamfer blockers\" hidden"), "starts hidden");
  let js = live_client_js();
  assert!(js.contains("function renderEngineBanner"), "banner renderer");
  assert!(js.contains("is installed but not running"), "wording matches the CLI preflight");
  assert!(js.contains("start_command"), "banner shows the exact command to type");
  assert!(js.contains("setup-engine-retry"), "banner offers a refresh once started");
  assert!(js.contains("'blocked'"), "cards drop their actions when the engine is down");
  assert!(js.contains("readiness unknown until the engine starts"), "summary stays honest");
  // The stopped state must never render as a red "missing"/"needs build" claim.
  assert!(js.contains("session-status cancelled\"><span>unknown"), "unknown image badge is not red");
}

#[test]
fn index_page_carries_the_repositories_panel_and_its_client_wiring() {
  let store = Store::new(DaemonMode::Persistent, 7274, 1);
  let html = super::index_page(&store);
  for id in
    ["repo-path", "repo-pick", "repo-open", "repo-blockers", "defs-panel", "defs-list", "def-form", "repos-body"]
  {
    assert!(html.contains(&format!("id=\"{id}\"")), "index page should contain #{id}");
  }
  assert!(html.contains("<span>New Project</span>"), "create button is title-cased");
  assert!(html.contains("#repo-note:empty { display: none; }"), "empty note must not push Open off the shared right edge");
  assert!(html.contains("summary .note { order: 9; flex-basis: 100%;"), "the proc answer always gets its own summary line");
  // The four tabs, and their panels — Jobs is leftmost and the default landing tab.
  for (tab, panel) in [("jobs", "tab-jobs"), ("run", "tab-run"), ("projects", "tab-projects"), ("setup", "tab-setup")] {
    assert!(html.contains(&format!("data-tab=\"{tab}\"")), "index page should have the {tab} tab");
    assert!(html.contains(&format!("id=\"{panel}\"")), "index page should have panel #{panel}");
  }
  let nav = html.find("<nav class=\"tabs\" role=\"tablist\">").expect("tabs nav");
  let jobs_btn = html[nav..].find("data-tab=\"jobs\">Jobs</button>").expect("Jobs tab");
  let run_btn = html[nav..].find("data-tab=\"run\">Run</button>").expect("Run tab");
  assert!(jobs_btn < run_btn, "Jobs should be leftmost");
  assert!(
    html.contains("<section class=\"tab-panel active\" id=\"tab-jobs\" role=\"tabpanel\" aria-labelledby=\"tabbtn-jobs\">"),
    "Jobs panel active by default"
  );
  assert!(html.contains("class=\"tab active\" data-tab=\"jobs\">Jobs</button>"), "Jobs tab active by default");
  let js = live_client_js();
  assert!(js.contains("saved || 'jobs'"), "client default tab is Jobs");
  assert!(js.contains("pathForTab"), "tabs use path URLs, not #tab= hashes");
  assert!(!js.contains("'/#tab='"), "no hash-based tab navigation");
  assert!(js.contains("'/projects'"), "Projects tab path is /projects");
  assert!(super::index::IndexTab::from_path("/projects") == Some(super::index::IndexTab::Projects));
  assert!(super::index::IndexTab::from_path("/setup") == Some(super::index::IndexTab::Setup));
  assert!(super::index::IndexTab::from_path("/images") == Some(super::index::IndexTab::Setup));
  assert!(super::index::IndexTab::from_path("/jobs") == Some(super::index::IndexTab::Jobs));
  assert!(super::index::IndexTab::from_path("/") == Some(super::index::IndexTab::Jobs));
  assert!(super::index::IndexTab::from_path("/run") == Some(super::index::IndexTab::Run));
  assert!(js.contains("/api/v1/repos/open"), "client js opens a repo");
  assert!(js.contains("/api/v1/repos/pick"), "client js pops the folder picker");
  assert!(js.contains("/api/v1/jobs/start"), "client js starts a job");
  assert!(js.contains("function renderRepoJobs"), "client js renders jobs by repository");
  assert!(js.contains("function renderInternalJobs"), "client js renders Internal section");
  assert!(js.contains("Chapters pending ⬇"), "export label for pending chapters");
  assert!(js.contains("function syncChaptersPending"), "live chapters-pending sync");
  assert!(
    js.contains("SESSION_ID && liveSessions ? liveSessions[SESSION_ID]"),
    "pollForChapters must not index null liveSessions (TypeError reading session id)"
  );
  assert!(!js.contains("SESSION_ID && liveSessions[SESSION_ID]"), "no bare liveSessions[SESSION_ID] without null check");
  assert!(js.contains("const live = life === 'running'"), "Queued only while the job is live");
  assert!(js.contains("OPEN_REPO_RUNNABLE"), "client js gates Start on the repo being runnable");
  assert!(js.contains("p.type === 'text'"), "multiline definition params render distinctly");
  assert!(js.contains("<textarea"), "feature briefs use a text area rather than a one-line input");
  assert!(js.contains("missing.name + ' is required'"), "empty required prose is rejected before start");
  assert!(js.contains("function initTabs"), "client js wires the tabs");
  assert!(js.contains("function syncIndexCrumb"), "tab navigation keeps the top-island crumb in sync");
  assert!(js.contains("tail.hidden = !visible"), "tab navigation retains rather than recreates crumb nodes");
  assert!(js.contains("history.pushState"), "tab clicks push history (WEB-UI §1)");
  assert!(js.contains("popstate"), "Back/Forward restore the active tab");
  assert!(js.contains("function sessionEndedLabel"), "Ended label shares lifecycle with the badge");
  assert!(js.contains("function activateProcPanel"), "fleet and workflow share arrival cues");
  assert!(js.contains("localStorage"), "UI prefs persist (WEB-UI §7)");
  assert!(!js.contains("fonts.googleapis.com"), "no Google Fonts in the live client");
}

#[test]
fn session_proc_html_has_no_stray_backslashes() {
  let mut store = Store::new(DaemonMode::Persistent, 7274, 1);
  store.sessions.insert(
    "test".into(),
    Session {
      id: "test".into(),
      started_at: 1,
      ended_at: None,
      profile: Some("default".into()),
      kind: None,
      repo: "/tmp/repo".into(),
      branch: "main".into(),
      last_seen_at: crate::daemon::paths::now_unix_secs(), // live: Force stop only renders for running sessions
      client_connected: false,
      run_pid: None,
      skills: vec![],
      procs: vec![ProcRecord {
        index: 0,
        previous_attempt: None,
        kind: ProcKind::Skill,
        label: "opencode: add".into(),
        status: ProcStatus::Running,
        note: None,
        detail: None,
        fail_reason: None,
        container_name: None,
        container_runtime: None,
        cast_path: None,
        diff_path: None,
        skill_source: None,
        route: None,
        result_path: None,
        annotate_target: None,
        harness: Some("opencode".into()),
        skill_name: Some("add".into()),
        model: None,
        started_at: Some(1),
        elapsed: None,
        lines: vec![crate::daemon::model::OutputLine { at: 0.5, text: "building…".into() }],
      }],
      workflow: None,
      parent_session: None,
      supervisor: Default::default(),
    },
  );
  let html = session_page(&store, "test").expect("session page");
  let procs = session_procs_html(&html);
  assert!(!html.contains("\\\n"), "raw-string line continuations must not leak backslashes");
  assert!(!procs.contains("\\\n"), "proc markup must not leak backslashes");
  assert!(!procs.contains("autoscroll-ctl"), "no Auto-scroll checkbox");
  assert!(!procs.contains("Auto-scroll to bottom"), "no Auto-scroll checkbox");
  assert!(html.contains(r#"id="session-stop""#), "running session should offer Force stop");
  assert!(html.contains("Force stop"));
}

#[test]
fn session_page_shows_the_commits_diff_chip_only_when_packed() {
  let mut store = Store::new(DaemonMode::Persistent, 7274, 1);
  store.sessions.insert(
    "difjob".into(),
    Session {
      id: "difjob".into(),
      started_at: 1,
      ended_at: Some(10),
      profile: Some("default".into()),
      kind: None,
      repo: "/tmp/repo".into(),
      branch: "main".into(),
      last_seen_at: 10,
      client_connected: false,
      run_pid: None,
      skills: vec![],
      procs: vec![
        ProcRecord {
          index: 0,
          previous_attempt: None,
          kind: ProcKind::Skill,
          label: "opencode: add".into(),
          status: ProcStatus::Ok,
          note: None,
          detail: None,
          fail_reason: None,
          container_name: None,
          container_runtime: None,
          cast_path: None,
          diff_path: Some("/tmp/scsh-home/sessions/difjob/diffs/add-p0.html".into()),
          skill_source: None,
          route: None,
          result_path: None,
          annotate_target: None,
          harness: Some("opencode".into()),
          skill_name: Some("add".into()),
          model: None,
          started_at: Some(1),
          elapsed: Some(2.0),
          lines: vec![],
        },
        ProcRecord {
          index: 1,
          previous_attempt: None,
          kind: ProcKind::Skill,
          label: "claude: add".into(),
          status: ProcStatus::Ok,
          note: None,
          detail: None,
          fail_reason: None,
          container_name: None,
          container_runtime: None,
          cast_path: None,
          diff_path: None,
          skill_source: None,
          route: None,
          result_path: None,
          annotate_target: None,
          harness: Some("claude".into()),
          skill_name: Some("add".into()),
          model: None,
          started_at: Some(1),
          elapsed: Some(2.0),
          lines: vec![],
        },
      ],
      workflow: None,
      parent_session: None,
      supervisor: Default::default(),
    },
  );
  let html = session_page(&store, "difjob").expect("session page");
  assert!(html.contains(r#"href="/diff/difjob/all""#), "the top run island links the end-to-end diff");
  let procs = session_procs_html(&html);
  // The step whose commits were packed links its review page; the other has no chip.
  assert!(procs.contains(r#"href="/diff/difjob/0""#), "packed step links its diff: {procs}");
  assert!(procs.contains("⇄ commits diff"), "the chip is labeled: {procs}");
  assert!(!procs.contains(r#"href="/diff/difjob/1""#), "unpacked step has no diff link: {procs}");
  assert_eq!(procs.matches("data-proc-diff").count(), 1, "exactly one chip: {procs}");
  // Plain click navigates in THIS tab; cmd/ctrl+click keeps its native new-tab meaning.
  assert!(!procs.contains("target="), "no target override on the diff chip: {procs}");
}

#[test]
fn ended_session_hides_force_stop_button() {
  let mut store = Store::new(DaemonMode::Persistent, 7274, 1);
  store.sessions.insert(
    "done01".into(),
    Session {
      id: "done01".into(),
      started_at: 1,
      ended_at: Some(10),
      profile: Some("default".into()),
      kind: None,
      repo: "/tmp/repo".into(),
      branch: "main".into(),
      last_seen_at: 10,
      client_connected: false,
      run_pid: None,
      skills: vec![],
      procs: vec![],
      workflow: None,
      parent_session: None,
      supervisor: Default::default(),
    },
  );
  let html = session_page(&store, "done01").expect("session page");
  // Settled jobs omit Force stop — no grayed-out stub.
  assert!(!html.contains(r#"id="session-stop""#), "ended session hides Force stop: {html}");
  assert!(html.contains(r#"class="page-lede""#), "session page has a plain-language lede");
  assert!(
    html.contains("· completed ·") || html.contains("completed"),
    "lede carries the completed lifecycle: {html}"
  );
  assert!(
    !html.contains(r#"class="session-kind""#),
    "ended session has no session-kind heading in the island: {html}"
  );
  assert!(
    !html.contains(r#"session-actions"><span class="chamfer session-status"#),
    "no badge in the top-right actions slot"
  );
  assert!(!html.contains(r#"<ul class="skills">"#), "no skills list in purple island");
}

#[test]
fn offline_export_carries_lede_and_full_meta() {
  // Parity with the live job page: the export shows the lede (kind · lifecycle · task
  // count) and the FULL meta — ended and duration included — so the offline copy answers
  // "did it succeed, and how long did it take" without the daemon.
  let session = Session {
    id: "exp01".into(),
    started_at: 1,
    ended_at: Some(10),
    profile: Some("code-review".into()),
    kind: Some("profile".into()),
    repo: "/tmp/repo".into(),
    branch: "main".into(),
    last_seen_at: 10,
    client_connected: false,
    run_pid: None,
    skills: vec![],
    procs: vec![],
    workflow: None,
    parent_session: None,
    supervisor: Default::default(),
  };
  let html = session_export_page(&session, &[], 100);
  assert!(html.contains(r#"class="page-lede""#), "export carries the live page's lede: {html}");
  assert!(
    html.contains("profile <strong>code-review</strong> · completed · 0 tasks"),
    "lede shows kind, profile, lifecycle, and task count: {html}"
  );
  assert!(!html.contains("0 tasks."), "task count is not punctuated as a sentence: {html}");
  assert!(html.contains(r#"<dl class="session-meta">"#), "export keeps session-meta: {html}");
  assert!(html.contains("<code>exp01</code>"), "job id in meta: {html}");
  assert!(html.contains("<dt>Ended</dt>"), "export meta shows when the job ended: {html}");
  assert!(html.contains("<dt>Duration</dt><dd>9s</dd>"), "export meta shows how long the job took: {html}");
  assert!(html.contains("accessibility: 'snapshot'"), "export player opts enable a11y snapshot");
  // Offline snapshots must not carry live Force stop chrome (markup or styles).
  assert!(!html.contains("Force stop"), "export must not mention Force stop: {html}");
  assert!(!html.contains("session-stop"), "export must not ship #session-stop: {html}");
  assert!(!html.contains("proc-kill"), "export must not ship .proc-kill: {html}");
  assert!(!html.contains("proc-restart"), "export must not ship .proc-restart: {html}");
  assert!(!html.contains("scsh-dialog"), "export must not ship the Force stop dialog: {html}");
}

#[test]
fn a_fresh_job_lede_counts_planned_tasks_not_zero() {
  use crate::daemon::model::SkillMeta;
  // A job started from the web UI pre-populates its planned skills; procs register only
  // once the spawned run comes up and reports. The lede reads the plan meanwhile — the
  // header of a freshly opened job page must never say "running · 0 tasks".
  let session = Session {
    id: "fresh1".into(),
    started_at: 1,
    ended_at: None,
    profile: Some("arith".into()),
    kind: Some("workflow".into()),
    repo: "/tmp/repo".into(),
    branch: "main".into(),
    last_seen_at: 1,
    client_connected: false,
    run_pid: Some(42),
    skills: vec![
      SkillMeta { name: "add".into(), harness: "claude".into() },
      SkillMeta { name: "multiply".into(), harness: "codex".into() },
      SkillMeta { name: "summarize".into(), harness: "grok".into() },
    ],
    procs: vec![],
    workflow: None,
    parent_session: None,
    supervisor: Default::default(),
  };
  let lede = super::session::session_lede_html(&session, session.lifecycle_status(2));
  assert!(lede.contains("workflow <strong>arith</strong>"), "kind and profile lead the lede: {lede}");
  assert!(lede.contains("3 tasks"), "the planned task count shows before any proc registers: {lede}");
  assert!(!lede.contains("0 task"), "never 0 tasks on a fresh job: {lede}");
}

#[test]
fn force_restart_rides_beside_force_stop_only_where_it_can_act() {
  let mut live = store_with_cast_proc(ProcStatus::Running);
  live.sessions.get_mut("castab").unwrap().last_seen_at = crate::daemon::paths::now_unix_secs();
  let page = session_page(&live, "castab").expect("session renders");
  assert!(page.contains(r#"id="session-restart""#), "a live repository job offers Force restart: {page}");
  let restart = page.find(r#"id="session-restart""#).unwrap();
  let stop = page.find(r#"id="session-stop""#).expect("Force stop still rides");
  assert!(restart < stop, "restart (orange) rides before stop (red)");
  let button_open = page[..restart].rfind("<button").unwrap();
  assert!(page[button_open..restart].contains("btn--orange"), "Force restart is the orange action");

  // An image build has no start recipe to replay — Force stop only.
  live.sessions.get_mut("castab").unwrap().repo = crate::daemon::server::IMAGE_BUILDS_REPO.into();
  let page = session_page(&live, "castab").expect("build session renders");
  assert!(!page.contains(r#"id="session-restart""#), "builds cannot be force-restarted: {page}");
  assert!(page.contains(r#"id="session-stop""#), "…but can still be force-stopped");

  // A COMPLETED job offers neither — controls that can never act again are noise.
  let mut done = store_with_cast_proc(ProcStatus::Ok);
  done.sessions.get_mut("castab").unwrap().ended_at = Some(2);
  let page = session_page(&done, "castab").expect("settled session renders");
  assert!(!page.contains(r#"id="session-restart""#), "no restart on a settled job: {page}");
  assert!(!page.contains(r#"id="session-stop""#), "no stop on a settled job: {page}");
  assert!(!page.contains(r#"id="session-resume""#), "no resume on a completed job: {page}");
}

#[test]
fn a_failed_job_offers_resume_and_scratch_restart() {
  let mut failed = store_with_cast_proc(ProcStatus::Fail);
  {
    let s = failed.sessions.get_mut("castab").unwrap();
    s.ended_at = Some(2);
    s.kind = Some("workflow".into());
    s.procs[0].fail_reason = Some(crate::failure::reason::HARNESS_NONZERO.into());
  }
  let page = session_page(&failed, "castab").expect("failed session renders");
  assert!(page.contains(r#"id="session-resume""#), "a failed workflow job offers Restart remaining: {page}");
  assert!(page.contains(r#"id="session-restart-scratch""#), "…and Restart from scratch");
  assert!(!page.contains(r#"id="session-stop""#), "no Force stop on a settled job: {page}");
  let resume = page.find(r#"id="session-resume""#).unwrap();
  let scratch = page.find(r#"id="session-restart-scratch""#).unwrap();
  assert!(resume < scratch, "resume (the cheap, usually-right action) rides first");

  // A failed flat job has independent routes — nothing to resume, scratch restart only.
  failed.sessions.get_mut("castab").unwrap().kind = None;
  let page = session_page(&failed, "castab").expect("failed flat session renders");
  assert!(!page.contains(r#"id="session-resume""#), "flat jobs have no resume: {page}");
  assert!(page.contains(r#"id="session-restart-scratch""#), "…but restart from scratch: {page}");

  // An image-build failure has no recipe to replay — no restart controls at all.
  failed.sessions.get_mut("castab").unwrap().repo = crate::daemon::server::IMAGE_BUILDS_REPO.into();
  let page = session_page(&failed, "castab").expect("failed build session renders");
  assert!(!page.contains(r#"id="session-restart-scratch""#), "builds cannot be restarted: {page}");
}

#[test]
fn force_restart_is_wired_live() {
  let js = live_client_js();
  assert!(js.contains("function restartSessionJob"), "the button has a handler");
  assert!(js.contains("'/api/v1/jobs/restart'"), "it calls the restart endpoint");
  assert!(js.contains("Force restart this job?"), "a confirm gate rides in front of the kill");
  assert!(js.contains("initSessionRestart();"), "the handler is wired on page load");
  assert!(js.contains("window.location.href = '/job/' + data.session"), "success lands on the NEW job's page");
  // The settled-failure buttons post their explicit modes and confirm first.
  assert!(js.contains("'session-resume'"), "the failed-job page wires Restart remaining");
  assert!(js.contains("'session-restart-scratch'"), "…and Restart from scratch");
  assert!(js.contains("'resume'"), "resume posts mode resume");
  assert!(js.contains("'scratch'"), "scratch posts mode scratch");
  assert!(js.contains("Restart the remaining steps?"), "resume confirms before spawning");
}

#[test]
fn global_skills_are_startable_from_the_run_page() {
  let js = live_client_js();
  assert!(js.contains("function selectGlobalProfile"), "picking a global profile opens a start form");
  assert!(js.contains("function startGlobalJob"), "…and its Start button starts a job");
  assert!(js.contains("profile: name"), "the start request posts a profile, not a def");
  assert!(js.contains("scsh installskills --global"), "the section says where global skills come from");
  assert!(js.contains(r#"badge--cyan"><span>global"#), "global cards wear the cyan source badge");
  assert!(js.contains("renderDefs(resp.defs || [], resp.global || [])"), "open replies feed the global list");
}

#[test]
fn start_forms_leave_retry_policy_to_the_daemon() {
  let js = live_client_js();
  assert!(!js.contains("job-retries"), "retry policy is daemon-owned, not a demo job parameter");
  assert!(!js.contains("retries: jobRetries()"), "definition starts use the daemon default");
}

#[test]
fn job_restart_history_appears_only_after_supervision_acts() {
  let mut store = store_with_cast_proc(ProcStatus::Running);
  let session = store.sessions.get_mut("castab").unwrap();
  session.supervisor = crate::daemon::model::SupervisorState::fresh(25);
  let fresh = session_page(&store, "castab").expect("fresh supervised session renders");
  let fresh_markup = fresh.split("<script").next().unwrap();
  assert!(!fresh_markup.contains("Job restarts</dt>"), "an unused policy is not job history");

  let session = store.sessions.get_mut("castab").unwrap();
  session.supervisor.next_retry_at = Some(u64::MAX);
  let scheduled = session_page(&store, "castab").expect("scheduled restart renders");
  assert!(scheduled.contains("Job restarts</dt><dd data-session-supervisor>1 of 25 · scheduled in"));

  let session = store.sessions.get_mut("castab").unwrap();
  session.supervisor.next_retry_at = None;
  session.supervisor.job_attempt = 2;
  let replacement = session_page(&store, "castab").expect("replacement run renders");
  assert!(replacement.contains("Job restarts</dt><dd data-session-supervisor>1 of 25 · running replacement"));
  let js = live_client_js();
  assert!(js.contains("function syncSupervisorMeta"), "the initially absent row can appear on a live tick");
  assert!(js.contains("syncSupervisorMeta(session, nowUnix);"));
}

#[test]
fn the_job_lede_ticks_live() {
  let js = live_client_js();
  assert!(js.contains("function syncSessionLede"), "the lede has a live tick mirror");
  assert!(
    js.contains("Math.max((session.procs || []).length, (session.skills || []).length)"),
    "the live count also reads planned skills, mirroring session_lede_html"
  );
  assert!(js.contains("syncSessionLede(session, nowUnix);"), "renderSession keeps the lede current");
}

#[test]
fn offline_export_advertises_chapter_keys_only_when_chapters_exist() {
  use super::session_export::CastExport;
  let store = store_with_cast_proc(ProcStatus::Ok);
  let session = store.sessions.get("castab").unwrap();
  let no_chapters = [CastExport::Cast {
    ndjson: "{\"version\":3,\"term\":{\"cols\":10,\"rows\":3}}\n[0.1,\"o\",\"hello\"]\n".into(),
    summary: None,
    chapters: vec![],
    diff_html: None,
  }];
  let html = session_export_page(session, &no_chapters, 100);
  assert!(!html.contains("c chapters"), "an empty chapter panel must not be advertised: {html}");

  let with_chapters = [CastExport::Cast {
    ndjson: "{\"version\":3,\"term\":{\"cols\":10,\"rows\":3}}\n[0.1,\"o\",\"hello\"]\n".into(),
    summary: None,
    chapters: vec![(0.0, "Start".into())],
    diff_html: None,
  }];
  let html = session_export_page(session, &with_chapters, 100);
  assert!(html.contains("[/] chapter · c chapters"), "real chapters advertise their keyboard controls: {html}");
}

#[test]
fn offline_export_embeds_commits_diff_when_present() {
  use super::session_export::CastExport;
  let session = Session {
    id: "expdf".into(),
    started_at: 1,
    ended_at: Some(10),
    profile: Some("default".into()),
    kind: Some("profile".into()),
    repo: "/tmp/repo".into(),
    branch: "main".into(),
    last_seen_at: 10,
    client_connected: false,
    run_pid: None,
    skills: vec![],
    procs: vec![ProcRecord {
      index: 0,
      previous_attempt: None,
      kind: ProcKind::Skill,
      label: "opencode: add".into(),
      status: ProcStatus::Ok,
      note: None,
      detail: Some("ok".into()),
      fail_reason: None,
      container_name: None,
      container_runtime: None,
      cast_path: None,
      diff_path: Some("/tmp/diff.html".into()),
      skill_source: None,
      route: None,
      result_path: None,
      annotate_target: None,
      harness: Some("opencode".into()),
      skill_name: Some("add".into()),
      model: None,
      started_at: Some(1),
      elapsed: Some(1.0),
      lines: vec![],
    }],
    workflow: None,
    parent_session: None,
    supervisor: Default::default(),
  };
  let hostile = r#"<html><body></script><p>diff</p></body></html>"#;
  let exports = [CastExport::Note { text: "no recording".into(), diff_html: Some(hostile.into()) }];
  let html = session_export_page(&session, &exports, 100);
  assert!(html.contains(r#"<span class="proc-diff""#), "summary carries static commits-diff chip");
  assert!(html.contains(r#"<details class="chamfer proc-diff">"#), "body embeds the packed diff");
  assert!(html.contains("srcdoc="), "diff rides in an iframe srcdoc");
  assert!(
    html.contains(r#"sandbox="allow-scripts allow-same-origin""#),
    "packdiff 0.6.2 needs scripts + same-origin for WASM/localStorage: {html}"
  );
  assert!(html.contains("<\\/"), "hostile </ is broken for srcdoc like CASTS");
  assert!(!html.contains("</script><p>diff"), "raw </script> must not appear unescaped");
}

#[test]
fn offline_export_renders_unrecorded_procs_as_note_rows() {
  use super::session_export::CastExport;
  use crate::daemon::model::OutputLine;
  // A proc that ran WITHOUT a recording shows full timestamped log lines on the live page;
  // the export must carry the same lines statically (same markup/classes) instead of
  // collapsing them to a one-line "no recording" note.
  let session = Session {
    id: "explog".into(),
    started_at: 1,
    ended_at: Some(10),
    profile: Some("default".into()),
    kind: Some("profile".into()),
    repo: "/tmp/repo".into(),
    branch: "main".into(),
    last_seen_at: 10,
    client_connected: false,
    run_pid: None,
    skills: vec![],
    procs: vec![ProcRecord {
      index: 0,
      previous_attempt: None,
      kind: ProcKind::Build,
      label: "build: claude".into(),
      status: ProcStatus::Ok,
      note: None,
      detail: None,
      fail_reason: None,
      container_name: None,
      container_runtime: None,
      cast_path: None,
      diff_path: None,
      skill_source: None,
      route: None,
      result_path: None,
      annotate_target: None,
      harness: Some("claude".into()),
      skill_name: None,
      model: None,
      started_at: Some(1),
      elapsed: Some(9.0),
      lines: vec![
        OutputLine { at: 0.5, text: "Step 1/4 : FROM ubuntu".into() },
        OutputLine { at: 2.0, text: "<hostile> & escaped".into() },
      ],
    }],
    workflow: None,
    parent_session: None,
    supervisor: Default::default(),
  };
  let note = "no recording — skipped/failed before output";
  let exports = [CastExport::Note { text: note.into(), diff_html: None }];
  let html = session_export_page(&session, &exports, 100);
  // There is no text-log format anywhere — the cast is the output format — so an
  // unrecorded proc exports as its note row alone, even when log lines were streamed.
  assert!(!html.contains(r#"<div class="chamfer output">"#), "no text-log output box in exports: {html}");
  assert!(!html.contains("Step 1/4 : FROM ubuntu"), "streamed lines never render as text: {html}");
  assert!(html.contains(note), "the note explains why there is nothing to embed: {html}");
}

#[test]
fn offline_export_includes_workflow_graph() {
  use super::session_export::CastExport;
  use crate::daemon::workflow::{WorkflowMeta, WorkflowNodeMeta};
  // The live job page renders the workflow DAG above the proc rows; the export must carry
  // the same server-rendered graph (frozen at export time), start/finish terminals and
  // task anchors included, so the offline copy shows how the job was wired.
  let session = Session {
    id: "expwf".into(),
    started_at: 1,
    ended_at: Some(10),
    profile: Some("arith".into()),
    kind: Some("workflow".into()),
    repo: "/tmp/repo".into(),
    branch: "main".into(),
    last_seen_at: 10,
    client_connected: false,
    run_pid: None,
    skills: vec![],
    procs: vec![
      ProcRecord {
        index: 0,
        previous_attempt: None,
        kind: ProcKind::Skill,
        label: "claude: add".into(),
        status: ProcStatus::Ok,
        note: None,
        detail: None,
        fail_reason: None,
        container_name: None,
        container_runtime: None,
        cast_path: None,
        diff_path: None,
        skill_source: Some("add".into()),
        route: None,
        result_path: None,
        annotate_target: None,
        harness: Some("claude".into()),
        skill_name: Some("add".into()),
        model: None,
        started_at: Some(1),
        elapsed: Some(4.0),
        lines: vec![],
      },
      ProcRecord {
        index: 1,
        previous_attempt: None,
        kind: ProcKind::Skill,
        label: "codex: summarize".into(),
        status: ProcStatus::Ok,
        note: None,
        detail: None,
        fail_reason: None,
        container_name: None,
        container_runtime: None,
        cast_path: None,
        diff_path: None,
        skill_source: Some("summarize".into()),
        route: None,
        result_path: None,
        annotate_target: None,
        harness: Some("codex".into()),
        skill_name: Some("summarize".into()),
        model: None,
        started_at: Some(5),
        elapsed: Some(5.0),
        lines: vec![],
      },
    ],
    workflow: Some(WorkflowMeta {
      nodes: vec![
        WorkflowNodeMeta {
          id: "add".into(),
          proc_index: Some(0),
          order: 0,
          needs: vec![],
          conditional: false,
          when_summary: None,
        },
        WorkflowNodeMeta {
          id: "summarize".into(),
          proc_index: Some(1),
          order: 1,
          needs: vec!["add".into()],
          conditional: false,
          when_summary: None,
        },
      ],
    }),
    parent_session: None,
    supervisor: Default::default(),
  };
  let exports = [
    CastExport::Note { text: "no recording".into(), diff_html: None },
    CastExport::Note { text: "no recording".into(), diff_html: None },
  ];
  let html = session_export_page(&session, &exports, 100);
  assert!(html.contains(r#"id="workflow-graph""#), "export carries the workflow card: {html}");
  assert!(html.contains(r#"class="chamfer wf-bookend wf-start""#), "graph keeps its start bookend");
  assert!(html.contains(r#"class="chamfer wf-bookend wf-finish""#), "graph keeps its finish bookend");
  assert!(html.contains(r#"data-workflow-step="add""#) && html.contains(r#"data-workflow-step="summarize""#));
  // The graph's jump links resolve offline: proc rows carry the same task anchors as live.
  assert!(html.contains("href=\"#task-add\""), "node links target task anchors");
  assert!(html.contains(r#"<details open class="chamfer proc ok" id="proc-0" data-index="0" data-workflow-step="add""#));
  assert!(html.contains(r#"<span class="proc-task-anchor" id="task-add" aria-hidden="true"></span>"#), "proc task alias: {html}");
  // The graph CSS rides in the shared stylesheet the export inlines.
  assert!(html.contains(".wf-bookend"), "bookend CSS is inlined in the export");
}

/// The standalone play page accepts BOTH deep-link forms: '#t=' (primary — what its copy
/// button writes) and '?t=' (what beecast-generated offline pages link with), so links
/// work across surfaces. Also parse-gates the page's inline script under Node, same
/// pattern as `live_client_js_parses_under_node`.
#[test]
fn cast_play_page_accepts_query_time_deep_links() {
  let page = cast_player_page(&store_with_cast_proc(ProcStatus::Ok), "castab", 0).expect("player page");
  assert!(page.contains("location.hash.match(/^#t=([0-9:.]+)$/)"), "hash form stays primary");
  assert!(
    page.contains("new URLSearchParams(location.search).get('t')"),
    "query form is parsed from location.search: {page}"
  );
  assert!(page.contains("/^[0-9:.]+$/.test(q)"), "query values pass the same seconds/mm:ss shape check");
  assert!(page.contains("'#t=' + t"), "the copy button still writes the hash form");
  if crate::runtime::which("node").is_none() {
    return;
  }
  let script = page.rsplit("<script>").next().expect("inline boot script");
  let script = script.split("</script>").next().expect("script end");
  let dir = std::env::temp_dir().join(format!("scsh-cast-js-check-{}", std::process::id()));
  std::fs::create_dir_all(&dir).unwrap();
  let path = dir.join("cast-page.js");
  std::fs::write(&path, script).unwrap();
  let out = std::process::Command::new("node").arg("--check").arg(&path).output().expect("spawn node --check");
  assert!(
    out.status.success(),
    "cast page inline script must parse: {}\n{}",
    String::from_utf8_lossy(&out.stderr),
    String::from_utf8_lossy(&out.stdout)
  );
  let _ = std::fs::remove_dir_all(&dir);
}

#[test]
fn session_page_renders_fleet_comparison_for_shared_skill_source() {
  let mut store = Store::new(DaemonMode::Persistent, 7274, 1);
  store.sessions.insert(
    "fleet1".into(),
    Session {
      id: "fleet1".into(),
      started_at: 1,
      ended_at: Some(10),
      profile: Some("default".into()),
      kind: Some("profile".into()),
      repo: "/tmp/repo".into(),
      branch: "main".into(),
      last_seen_at: 10,
      client_connected: false,
      run_pid: None,
      skills: vec![],
      procs: vec![
        ProcRecord {
          index: 0,
          previous_attempt: None,
          kind: ProcKind::Skill,
          label: "opencode: add-opencode".into(),
          status: ProcStatus::Ok,
          note: None,
          detail: Some("2 + 3 = 5".into()),
          fail_reason: None,
          container_name: None,
          container_runtime: None,
          cast_path: None,
          diff_path: None,
          skill_source: Some("add".into()),
          route: Some("opencode".into()),
          result_path: None,
          annotate_target: None,
          harness: Some("opencode".into()),
          skill_name: Some("add-opencode".into()),
          model: None,
          started_at: Some(1),
          elapsed: Some(1.0),
          lines: vec![],
        },
        ProcRecord {
          index: 1,
          previous_attempt: None,
          kind: ProcKind::Skill,
          label: "claude: add-claude".into(),
          status: ProcStatus::Ok,
          note: None,
          detail: Some("2 + 3 = 5".into()),
          fail_reason: None,
          container_name: None,
          container_runtime: None,
          cast_path: None,
          diff_path: None,
          skill_source: Some("add".into()),
          route: Some("claude".into()),
          result_path: None,
          annotate_target: None,
          harness: Some("claude".into()),
          skill_name: Some("add-claude".into()),
          model: None,
          started_at: Some(1),
          elapsed: Some(1.2),
          lines: vec![],
        },
      ],
      workflow: None,
      parent_session: None,
      supervisor: Default::default(),
    },
  );
  let html = session_page(&store, "fleet1").expect("session page");
  assert!(html.contains(r#"class="fleets""#), "fleet section present: {html}");
  assert!(html.contains(r#"class="fleet-compare""#), "comparison table present");
  assert!(html.contains(r#"data-skill-source="add""#), "grouped by skill_source");
  assert!(html.contains(r#"class="chamfer fleet-jump" data-proc="0""#), "jump to first route");
  assert!(html.contains(r#"class="chamfer fleet-jump" data-proc="1""#), "jump to second route");
  assert!(html.contains("· 2 routes"), "true route matrices keep route terminology");
  assert!(html.contains("<th>Route</th>"), "true route matrices keep the Route column");
  // The table is live: tick snapshots re-sync each row's status/glyph/duration/result and
  // the rollup line in place (server-rendered grade/issue text is richer and is kept).
  let js = live_client_js();
  assert!(js.contains("function syncFleetSections"), "fleet sections sync from ticks");
  assert!(js.contains("syncFleetSections(session, nowUnix);"), "renderSession keeps fleets current");
  assert!(js.contains("running:'◆',waiting:'◇'"), "fleet glyphs share the diamond language");
  assert!(js.contains(".fleet-grade, .fleet-issues"), "richer server-rendered results are preserved");
  let fleets_at = html.find(r#"class="fleets""#).expect("fleets");
  let first_proc_at = html.find(r#"data-index="0""#).expect("first proc");
  let last_proc_at = html.find(r#"data-index="1""#).expect("last proc");
  assert!(first_proc_at < last_proc_at && last_proc_at < fleets_at, "comparison follows the work it summarizes");

  // Repeated workflow steps are cycles, not model/harness routes.
  {
    let session = store.sessions.get_mut("fleet1").unwrap();
    for (i, proc) in session.procs.iter_mut().enumerate() {
      proc.skill_source = Some("review_docs".into());
      proc.skill_name = Some(format!("review_docs-while-decide-{}", i + 1));
      proc.route = proc.skill_name.clone();
    }
  }
  let cycles = session_page(&store, "fleet1").expect("cycle page");
  assert!(cycles.contains("· 2 cycle iterations"), "loop repetitions are named as cycle iterations");
  assert!(cycles.contains("<th>Cycle iteration</th>"), "cycle table names its first column honestly");
  assert!(cycles.contains("all cycle iterations agree"), "cycle rollup avoids route terminology");
  assert!(cycles.contains("rgba(88,166,255,0.09)"), "comparison islands carry a light-blue tint");
  assert_eq!(
    cycles.matches("<strong>skill</strong> <code>review_docs</code>").count(),
    2,
    "run metadata shows the authored action, not generated while-loop wiring"
  );
  assert!(
    live_client_js().contains("skillName = p.skill_source"),
    "live-added loop rows use the same human-facing action name"
  );
}

#[test]
fn session_page_renders_job_level_fleet_verdict_across_skills() {
  let dir = std::env::temp_dir().join(format!("scsh-verdict-{}", crate::runtime::random_nonce_6()));
  std::fs::create_dir_all(&dir).unwrap();
  let graded = |name: &str, grade: &str, issues: u64| -> String {
    let path = dir.join(format!("{name}.json"));
    std::fs::write(&path, format!(r#"{{"result":{{"grade":"{grade}","issues_found":{issues}}},"issues":[]}}"#))
      .unwrap();
    path.to_string_lossy().into_owned()
  };
  let route = |index: usize, source: &str, route_name: &str, result_path: String| ProcRecord {
    index,
    previous_attempt: None,
    kind: ProcKind::Skill,
    label: format!("{source}-{route_name}"),
    status: ProcStatus::Ok,
    note: None,
    detail: None,
    fail_reason: None,
    container_name: None,
    container_runtime: None,
    cast_path: None,
    diff_path: None,
    skill_source: Some(source.into()),
    route: Some(route_name.into()),
    result_path: Some(result_path),
    annotate_target: None,
    harness: Some("claude".into()),
    skill_name: Some(format!("{source}-{route_name}")),
    model: None,
    started_at: Some(1),
    elapsed: Some(1.0),
    lines: vec![],
  };
  let mut store = Store::new(DaemonMode::Persistent, 7274, 1);
  store.sessions.insert(
    "verd1".into(),
    Session {
      id: "verd1".into(),
      started_at: 1,
      ended_at: Some(10),
      profile: Some("default".into()),
      kind: Some("profile".into()),
      repo: "/tmp/repo".into(),
      branch: "main".into(),
      last_seen_at: 10,
      client_connected: false,
      run_pid: None,
      skills: vec![],
      procs: vec![
        route(0, "conventions-reviewer", "opus", graded("c-opus", "excellent", 1)),
        route(1, "conventions-reviewer", "codex", graded("c-codex", "good", 2)),
        route(2, "testing-reviewer", "opus", graded("t-opus", "excellent", 0)),
        route(3, "testing-reviewer", "codex", graded("t-codex", "good", 0)),
      ],
      workflow: None,
      parent_session: None,
      supervisor: Default::default(),
    },
  );
  let html = session_page(&store, "verd1").expect("session page");
  assert!(html.contains("data-fleet-verdict"), "job-level verdict present: {html}");
  assert!(html.contains("Fleet verdict"), "verdict panel is titled");
  assert!(html.contains("· 2 skills · 4 routes"), "verdict counts skills and routes");
  assert!(html.contains("4 ok, 0 fail"), "verdict counts settled routes");
  assert!(html.contains("excellent ×2 · good ×2 · mean 4.50 · 3 findings"), "grade histogram, mean, findings: {html}");
  // The verdict is the CLOSING recap: it renders after the last per-skill comparison.
  let last_group = html.rfind("data-skill-source=").expect("groups");
  let verdict_at = html.find("data-fleet-verdict").expect("verdict");
  assert!(verdict_at > last_group, "verdict follows every comparison it summarizes");
  // Live: the counts span ticks from snapshots; the grade half keeps the server render.
  let js = live_client_js();
  assert!(js.contains("section.fleet-verdict .fv-counts"), "verdict counts tick live");
  // A single fleet renders no job-level verdict — its own summary line is the recap.
  store.sessions.get_mut("verd1").unwrap().procs.truncate(2);
  let single = session_page(&store, "verd1").expect("single-fleet page");
  assert!(!single.contains("data-fleet-verdict"), "one group needs no cross-fleet recap");
  let _ = std::fs::remove_dir_all(dir);
}

#[test]
fn fleet_routes_stack_completed_before_running_before_waiting() {
  let mut store = Store::new(DaemonMode::Persistent, 7274, 1);
  store.sessions.insert(
    "fleet2".into(),
    Session {
      id: "fleet2".into(),
      started_at: 1,
      ended_at: None,
      profile: Some("default".into()),
      kind: Some("profile".into()),
      repo: "/tmp/repo".into(),
      branch: "main".into(),
      last_seen_at: crate::daemon::paths::now_unix_secs(),
      client_connected: true,
      run_pid: Some(1),
      skills: vec![],
      procs: vec![
        ProcRecord {
          index: 0,
          previous_attempt: None,
          kind: ProcKind::Skill,
          label: "claude: add-waiting".into(),
          status: ProcStatus::Waiting,
          note: None,
          detail: None,
          fail_reason: None,
          container_name: None,
          container_runtime: None,
          cast_path: None,
          diff_path: None,
          skill_source: Some("add".into()),
          route: Some("waiting-route".into()),
          result_path: None,
          annotate_target: None,
          harness: Some("claude".into()),
          skill_name: Some("add-waiting-route".into()),
          model: None,
          started_at: None,
          elapsed: None,
          lines: vec![],
        },
        ProcRecord {
          index: 1,
          previous_attempt: None,
          kind: ProcKind::Skill,
          label: "claude: add-done".into(),
          status: ProcStatus::Ok,
          note: None,
          detail: Some("ok".into()),
          fail_reason: None,
          container_name: None,
          container_runtime: None,
          cast_path: None,
          diff_path: None,
          skill_source: Some("add".into()),
          route: Some("done-route".into()),
          result_path: None,
          annotate_target: None,
          harness: Some("claude".into()),
          skill_name: Some("add-done-route".into()),
          model: None,
          started_at: Some(1),
          elapsed: Some(1.0),
          lines: vec![],
        },
        ProcRecord {
          index: 2,
          previous_attempt: None,
          kind: ProcKind::Skill,
          label: "claude: add-running".into(),
          status: ProcStatus::Running,
          note: None,
          detail: None,
          fail_reason: None,
          container_name: None,
          container_runtime: None,
          cast_path: None,
          diff_path: None,
          skill_source: Some("add".into()),
          route: Some("running-route".into()),
          result_path: None,
          annotate_target: None,
          harness: Some("claude".into()),
          skill_name: Some("add-running-route".into()),
          model: None,
          started_at: Some(1),
          elapsed: None,
          lines: vec![],
        },
      ],
      workflow: None,
      parent_session: None,
      supervisor: Default::default(),
    },
  );
  let html = session_page(&store, "fleet2").expect("fleet page");
  let done_at = html.find("done-route").expect("done route");
  let running_at = html.find("running-route").expect("running route");
  let waiting_at = html.find("waiting-route").expect("waiting route");
  assert!(done_at < running_at && running_at < waiting_at, "Completed → Running → Waiting: {html}");
}

#[test]
fn client_js_wires_fleet_jumps_and_accessibility_snapshot() {
  let js = live_client_js();
  assert!(js.contains("function parseIndexFilter"), "client parses /project and /repo");
  assert!(js.contains("function repoFilterHref"), "client builds filter hrefs");
  assert!(js.contains("repo-filter-link"), "live Projects rows stay clickable");
}

#[test]
fn client_js_wires_force_stop() {
  let js = live_client_js();
  assert!(js.contains("/api/v1/session/stop"), "client js posts session stop");
  assert!(js.contains("function forceStopSession"), "client js defines forceStopSession");
  assert!(js.contains("function scshConfirm"), "Force stop uses an in-app confirm dialog");
  assert!(js.contains("scsh-dialog"), "dialog markup class ships");
  assert!(!js.contains("confirm("), "no browser confirm() for Force stop");
  assert!(!js.contains("alert("), "Force stop errors use toast, not alert()");
  assert!(js.contains("Terminating all ' + harness"), "stop-all acknowledges the accepted request in its button");
  assert!(js.contains("Stop requested for all ' + harness"), "stop-all immediately confirms through the live toast");
  assert!(js.contains("Stopped ' + n + ' ' + harness + ' task"), "stop-all reports its final stopped count");
  assert!(js.contains("p.fail_reason === 'stop_requested'"), "live tasks expose the terminating transition");
}

/// A Force restart needs the owning `scsh run` to respawn the route, so the browser must not
/// offer one it knows the daemon will refuse with "the run client is gone".
#[test]
fn client_js_blocks_a_restart_with_no_run_client_to_respawn_from() {
  let js = live_client_js();
  assert!(js.contains("RUN_CLIENT_GONE"), "the client tracks whether the run process is still attached");
  assert!(js.contains("session.client_connected === false"), "read from the session payload the daemon already sends");
  assert!(js.contains("function setRestartBlocked"), "one place decides whether a restart is offerable");
  assert!(js.contains("Restart unavailable"), "the blocked control says so on its face, not only on hover");
  assert!(js.contains("nothing is left to respawn this route"), "and explains why, matching the daemon's refusal");
  assert!(js.contains("restart the whole job instead"), "with the action that does work");
  // Blocked, not hidden: a control that vanishes reads as a missing feature.
  assert!(js.contains("btn.disabled = !!blocked"), "the button is disabled rather than removed");
  // Every path that can put a restart button on the page gates through the helper: the live
  // update of a row that already has one, the row that grows one mid-run, and the rows
  // procHtml paints (first load, and each newly appended proc) which never see updateProcFields.
  assert!(js.contains("setRestartBlocked(restartEl, RUN_CLIENT_GONE)"), "existing row re-gated on each update");
  assert_eq!(
    js.matches("setRestartBlocked(btn, RUN_CLIENT_GONE)").count(),
    2,
    "the freshly created button and the procHtml-painted ones both gate"
  );
}

/// Accessibility hardening: confirm-dialog focus management, the full ARIA tab pattern,
#[test]
fn stats_tab_renders_the_flaky_route_dashboard() {
  let _env = crate::runtime::test_env_lock();
  let file = std::env::temp_dir().join(format!("scsh-stats-tab-{}.jsonl", crate::runtime::random_nonce_6()));
  let prev = std::env::var_os(crate::stats::STATS_FILE_ENV);
  std::env::set_var(crate::stats::STATS_FILE_ENV, &file);
  let rec = |outcome: &str, secs: f64| crate::stats::StatRecord {
    ts: 1000,
    kind: "skill".into(),
    session: "abc".into(),
    repo: "/r".into(),
    branch: "b".into(),
    profile: Some("code-review".into()),
    skill: Some("testing-reviewer-cursor-auto".into()),
    skill_source: Some("testing-reviewer".into()),
    harness: Some("cursor".into()),
    model: Some("auto".into()),
    effort: None,
    outcome: Some(outcome.into()),
    fail_reason: (outcome == "fail").then(|| "container_timeout".to_string()),
    attempts: 1,
    duration_secs: secs,
    commits: 0,
    loc_added: 0,
    loc_deleted: 0,
    skills_total: None,
    skills_failed: None,
  };
  crate::stats::record(&rec("ok", 10.0));
  crate::stats::record(&rec("fail", 60.0));

  let store = Store::new(DaemonMode::Persistent, 7274, 1);
  let html = super::index_page_for(&store, None, super::IndexTab::Stats);
  assert!(html.contains(r#"data-tab="stats""#), "Stats tab button present: {html}");
  assert!(html.contains("Route reliability"), "route table present");
  assert!(html.contains("Skill × route"), "skill × route table present");
  assert!(html.contains("cursor · auto"), "route key rendered");
  assert!(html.contains("testing-reviewer — cursor · auto"), "skill × route key rendered");
  assert!(html.contains(r#"class="stats-fail-high""#), "a 50% failure rate wears the loud color");
  assert!(html.contains("<code>container_timeout</code> ×1"), "top failure reason named: {html}");
  assert!(html.contains("durations exclude cache hits"), "the cache-exclusion contract is stated");

  // Without any stats file the tab explains itself instead of rendering an empty table.
  std::fs::remove_file(&file).unwrap();
  let empty = super::index_page_for(&store, None, super::IndexTab::Stats);
  assert!(empty.contains("No run statistics yet"), "empty state: {empty}");
  assert!(!empty.contains(r#"<table class="stats-table">"#), "no empty table");

  match prev {
    Some(v) => std::env::set_var(crate::stats::STATS_FILE_ENV, v),
    None => std::env::remove_var(crate::stats::STATS_FILE_ENV),
  }
}

/// live-region copy feedback, reduced-motion-gated scrolling and micro-transitions, and
/// breadcrumb truncation on narrow viewports.
#[test]
fn dashboard_a11y_contracts_hold() {
  let js = live_client_js();
  // 1. The confirm dialog remembers the previously focused element, restores it on
  //    close, and traps Tab inside the panel while it is open.
  assert!(js.contains("const prevFocus = document.activeElement;"), "dialog records the opener's focus");
  assert!(js.contains("prevFocus.focus()"), "dialog restores focus on close");
  assert!(js.contains("ev.key === 'Tab'"), "dialog traps Tab inside the modal");
  // 2. Tabs carry the complete ARIA pattern: a tablist container, labelled tabpanels,
  //    arrow-key navigation, and a roving tabindex.
  let html = super::index_page(&Store::new(DaemonMode::Persistent, 7274, 1));
  assert!(html.contains(r#"<nav class="tabs" role="tablist">"#), "the tabs nav is a tablist");
  assert_eq!(html.matches(r#"role="tabpanel""#).count(), 5, "every panel is a tabpanel");
  assert!(html.contains(r#"aria-labelledby="tabbtn-run""#), "panels are labelled by their tabs");
  assert!(html.contains(r#"aria-selected="true""#), "the active tab is selected server-side");
  assert!(js.contains("'ArrowRight'"), "arrow keys walk the tablist");
  assert!(js.contains("x.tabIndex = on ? 0 : -1;"), "roving tabindex follows the active tab");
  // 3. Every scroll respects prefers-reduced-motion; none is hardcoded smooth.
  assert!(js.contains("list.scrollIntoView"), "open/create still scrolls Definitions into view");
  assert!(!js.contains("behavior: 'smooth'"), "no hardcoded smooth scrolling");
  // 4. The cast page announces "copied" to screen readers, like the dashboard toast.
  let cast = cast_player_page(&store_with_cast_proc(ProcStatus::Ok), "castab", 0).expect("player page");
  assert!(cast.contains(r#"<span id="copied" role="status">"#), "copy feedback is a live region");
  // 5. Long breadcrumbs truncate inside the fixed-height sticky status bar instead of
  //    overflowing phone-width viewports.
  assert!(
    html.contains("min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;"),
    "crumbs truncate with an ellipsis"
  );
  // 6. Decorative micro-transitions (buttons, chips, the toast) sit behind the same
  //    reduced-motion gate as the workflow pulse.
  let gate = html.find("@media (prefers-reduced-motion: no-preference)").expect("reduced-motion gate ships");
  for rule in [".btn, button.btn { transition:", ".hchip { transition:", ".toast { transition:"] {
    assert_eq!(html.matches(rule).count(), 1, "{rule} appears exactly once");
    assert!(html.find(rule).unwrap() > gate, "{rule} is gated on reduced motion");
  }
}

#[test]
fn client_js_mirrors_the_commits_diff_chip() {
  // Integration (and the packdiff pack) happens after a step finished, so the chip usually
  // arrives on a live tick: the client must render the same markup session.rs serves.
  let js = live_client_js();
  assert!(js.contains("function procDiffBtnHtml"), "client js builds the diff chip");
  assert!(js.contains("p.diff_path"), "client js keys the chip on the proc's diff_path");
  assert!(js.contains("⇄ commits diff"), "the live chip carries the same label");
  assert!(js.contains("data-job-diff"), "the live run island gains the whole-job diff button");
  assert!(js.contains("actions = document.createElement('div')"), "a closed slim row gains its action island live");
  assert!(js.contains("initProcDiffs"), "chips present at page render are wired too");
}

#[test]
fn recorded_proc_embeds_cast_player_instead_of_text_output() {
  let mut store = Store::new(DaemonMode::Persistent, 7274, 1);
  store.sessions.insert(
    "castab".into(),
    Session {
      id: "castab".into(),
      started_at: 1,
      ended_at: None,
      profile: Some("default".into()),
      kind: None,
      repo: "/tmp/repo".into(),
      branch: "main".into(),
      last_seen_at: 1,
      client_connected: true,
      run_pid: None,
      skills: vec![],
      procs: vec![ProcRecord {
        index: 2,
        previous_attempt: None,
        kind: ProcKind::Skill,
        label: "claude: add".into(),
        status: ProcStatus::Ok,
        note: None,
        detail: None,
        fail_reason: None,
        container_name: None,
        container_runtime: None,
        cast_path: Some("/tmp/x.cast".into()),
        diff_path: None,
        skill_source: None,
        route: None,
        result_path: None,
        annotate_target: None,
        harness: Some("claude".into()),
        skill_name: Some("add".into()),
        model: None,
        started_at: Some(1),
        elapsed: Some(3.0),
        lines: vec![],
      }],
      workflow: None,
      parent_session: None,
      supervisor: Default::default(),
    },
  );
  let html = session_page(&store, "castab").expect("session page");
  // The page loads the player assets and embeds a player box wired to the cast endpoint.
  assert!(html.contains(r#"<link rel="stylesheet" href="/assets/scsh-cast-player.css">"#), "player css");
  assert!(html.contains(r#"<script src="/assets/scsh-cast-player.js"></script>"#), "player js");
  let procs = session_procs_html(&html);
  assert!(procs.contains(r#"<div class="cast" data-cast-url="/cast/castab/2""#), "cast embed");
  // Fullscreen lives in the player's own control bar now (⛶ + the f key) — the page
  // toolbar carries no fullscreen button of its own. Opening a
  // section focuses its player, so space and f work with no click first.
  assert!(!procs.contains("data-cast-fs"), "no page-side fullscreen button");
  assert!(procs.contains("f fullscreen"), "the keys hint teaches f");
  // Streaming drives itself (WS growth appends + the finish reload), so there is no manual
  // Reload button; chapters are the player's own chrome (☰ panel + c key + seek ticks) —
  // no scsh-side chip row or fullscreen sidebar. The external hint starts empty and is
  // populated only after the sidecar proves chapters exist.
  assert!(!procs.contains("data-cast-reload"), "no manual reload in a streaming toolbar");
  assert!(!procs.contains("c chapters"), "do not advertise chapters before markers exist");
  assert!(procs.contains("data-chapter-keys"), "the live client owns the conditional chapter hint");
  let js = live_client_js();
  assert!(!js.contains("data-cast-reload"), "client js builds no reload button");
  assert!(!js.contains("cast-chapters"), "no scsh-side chapter chips");
  assert!(!js.contains("cast-fs-chapters"), "no scsh-side fullscreen chapters sidebar");
  assert!(js.contains("markers"), "chapters reach the player as markers");
  assert!(js.contains("function setChapterKeys") && js.contains("c chapters"), "the hint appears with real markers");
  assert!(js.contains("function focusCastPlayer"), "open sections hand the player the keyboard");
  assert!(js.contains("if (det.open) focusCastPlayer(box)"), "focus follows the section toggle");
  // Run snapshot sits in the proc island's top-right (above Force stop), cyan chamfer —
  // not inside the cast toolbar (toolbar keeps only `.cast` download + keys hint).
  assert!(!procs.contains("data-cast-link"), "no link-at-time in the inline toolbar");
  assert!(procs.contains(r#"class="chamfer btn btn--cyan btn--sm proc-snapshot""#), "run snapshot link");
  assert!(procs.contains(r#"href="/cast/castab/2/export.html" data-cast-export"#), "run snapshot href");
  assert!(
    !procs.contains(r#"cast-toolbar"><a href="/cast/castab/2/export.html"#),
    "snapshot is outside the cast toolbar"
  );
  assert!(procs.contains(r#"<a href="/cast/castab/2?dl=1" download>"#), "download link");
  // A recorded proc shows the player, NOT the text output.
  assert!(!procs.contains(r#"<div class="chamfer output">"#), "no text output for recorded proc");
  assert!(!procs.contains("autoscroll-ctl"), "no autoscroll control for recorded proc");
}

#[test]
fn session_proc_html_has_no_autoscroll_checkbox() {
  let mut store = Store::new(DaemonMode::Persistent, 7274, 1);
  store.sessions.insert(
    "test".into(),
    Session {
      id: "test".into(),
      started_at: 1,
      ended_at: None,
      profile: Some("default".into()),
      kind: None,
      repo: "/tmp/repo".into(),
      branch: "main".into(),
      last_seen_at: 1,
      client_connected: true,
      run_pid: None,
      skills: vec![],
      procs: vec![ProcRecord {
        index: 0,
        previous_attempt: None,
        kind: ProcKind::Skill,
        label: "opencode: add".into(),
        status: ProcStatus::Running,
        note: None,
        detail: None,
        fail_reason: None,
        container_name: None,
        container_runtime: None,
        cast_path: None,
        diff_path: None,
        skill_source: None,
        route: None,
        result_path: None,
        annotate_target: None,
        harness: Some("opencode".into()),
        skill_name: Some("add".into()),
        model: None,
        started_at: Some(1),
        elapsed: None,
        // The auto-scroll control belongs to a text-output box, so the proc must have
        // streamed at least one line for the control to render.
        lines: vec![crate::daemon::model::OutputLine { at: 0.2, text: "cloning…".into() }],
      }],
      workflow: None,
      parent_session: None,
      supervisor: Default::default(),
    },
  );
  let html = session_page(&store, "test").expect("session page");
  let procs = session_procs_html(&html);
  // The text-log body is gone entirely: streamed lines never render, so neither does any
  // of the retired terminal chrome around them.
  assert!(!procs.contains(r#"<div class="chamfer output">"#), "no text output box: {procs}");
  assert!(!procs.contains("autoscroll-ctl"), "Auto-scroll checkbox removed");
  assert!(!procs.contains("Auto-scroll to bottom"), "Auto-scroll checkbox removed");
  let js = live_client_js();
  assert!(!js.contains("Auto-scroll to bottom"), "client JS has no Auto-scroll checkbox");
  assert!(!js.contains("followOutput"), "the text-follow machinery is gone with the text body");
}

/// A one-proc store whose proc is an `Annotate` row: no recording, no log lines — the
/// shape the post-run annotation pass registers while it summarizes a cast.
fn store_with_annotate_proc(status: ProcStatus) -> Store {
  let mut store = Store::new(DaemonMode::Persistent, 7274, 1);
  store.sessions.insert(
    "annjob".into(),
    Session {
      id: "annjob".into(),
      started_at: 1,
      ended_at: None,
      profile: Some("annotate".into()),
      kind: Some("annotate".into()),
      repo: "(internal)".into(),
      branch: "".into(),
      // Fresh heartbeat: per-proc Force stop renders only for sessions still alive.
      last_seen_at: crate::daemon::paths::now_unix_secs(),
      client_connected: true,
      run_pid: None,
      skills: vec![],
      procs: vec![ProcRecord {
        index: 0,
        previous_attempt: None,
        kind: ProcKind::Annotate,
        label: "annotate · add-20260711-114749-utc-ufakca".into(),
        status,
        note: Some("summarizing…".into()),
        detail: None,
        fail_reason: None,
        container_name: None,
        container_runtime: None,
        cast_path: None,
        diff_path: None,
        skill_source: None,
        route: None,
        result_path: None,
        annotate_target: Some("/tmp/casts/add-20260711-114749-utc-ufakca.cast".into()),
        harness: Some("cursor".into()),
        skill_name: None,
        model: Some("composer".into()),
        started_at: Some(1),
        elapsed: None,
        lines: vec![],
      }],
      workflow: None,
      parent_session: None,
      supervisor: Default::default(),
    },
  );
  store
}

#[test]
fn annotate_rows_render_slim_without_the_retired_terminal_chrome() {
  // An annotate proc without a recording has no streamed lines. Its row keeps the
  // summary line and status, but must NOT wear the recorded-terminal chrome — no
  // auto-scroll checkbox and no empty "No output." box. Force stop appears only while
  // the row is live (finished rows drop the button entirely).
  for status in [ProcStatus::Running, ProcStatus::Ok, ProcStatus::Fail] {
    let html = session_page(&store_with_annotate_proc(status), "annjob").expect("session page");
    let procs = session_procs_html(&html);
    assert!(procs.contains("annotate · add-20260711-114749-utc-ufakca"), "row renders: {procs}");
    if status == ProcStatus::Running {
      assert!(procs.contains("Stop annotation"), "annotation has its own stop action while live");
    } else {
      assert!(!procs.contains("Stop annotation"), "no stop action on a settled row ({status:?})");
    }
    assert!(!procs.contains("autoscroll-ctl"), "no auto-scroll control on a slim row ({status:?}): {procs}");
    assert!(!procs.contains(r#"<div class="chamfer output">"#), "no output box on a slim row ({status:?}): {procs}");
    assert!(!procs.contains("No output"), "no empty-output placeholder ({status:?}): {procs}");
  }
  // The live-update path renders the same slim shape, so a WS tick never grows the
  // chrome back: the client has no text-body path at all — a proc is a cast or a slim row.
  let js = live_client_js();
  assert!(!js.contains("chamfer output"), "the client never builds a text output box");
  assert!(!js.contains("No output yet."), "the retired empty-output placeholder is gone from the client");
}

#[test]
fn annotation_control_links_to_the_job_and_persists_its_state() {
  let js = live_client_js();
  assert!(js.contains("meta.annotation_job"), "the player reads the annotation job id");
  assert!(js.contains("#proc-' + Number(meta.annotation_proc"), "the link targets the annotating run");
  assert!(js.contains("annotation-link--' + status"), "running/ok/fail each retain a status class");
  assert!(js.contains("annotation-dots"), "running annotation gets animated dots");
  assert!(js.contains("SESSION_START_TIMEOUT_SECS = 30"), "startup has one short deadline");
  assert!(
    js.contains("SESSION_IDLE_TIMEOUT_SECS = 30 * 60"),
    "started work gets a 30-minute idle allowance"
  );
  assert_eq!(
    crate::daemon::model::SESSION_IDLE_TIMEOUT_SECS,
    crate::config::DEFAULT_INACTIVITY_TIMEOUT_SECS,
    "browser and harnesses must agree on running-idle timeout"
  );
  assert!(js.contains("session.lifecycle && session.lifecycle !== 'running'"), "terminal lifecycle comes from the daemon");
  assert!(
    js.contains("sessionLifecycle(candidate, Date.now() / 1000).class === 'running'"),
    "annotation rendering consumes the shared job lifecycle"
  );
  assert!(js.contains("CHAPTERS_WAIT_SECS"), "the poll window is still bounded");
  assert!(js.contains("renderAnnotationLink(box, meta)"), "a late-registering job links up mid-poll");
}

#[test]
fn annotation_child_sessions_belong_to_the_parent_job_not_job_lists() {
  let mut store = store_with_cast_proc(ProcStatus::Ok);
  let mut annotation = store.sessions["castab"].clone();
  annotation.id = "annjob".into();
  annotation.profile = Some("annotate-cast".into());
  annotation.parent_session = Some("castab".into());
  annotation.procs[0].kind = ProcKind::Annotate;
  annotation.procs[0].cast_path = None;
  annotation.procs[0].annotate_target = Some("/tmp/x.cast".into());
  store.sessions.insert(annotation.id.clone(), annotation);

  let html = super::index_page(&store);
  assert!(html.contains(r#"href="/job/castab""#), "parent remains a listed job");
  assert!(!html.contains(r#"href="/job/annjob""#), "annotation child is absent from Jobs and Projects");

  let js = live_client_js();
  assert!(js.contains("if (!s || s.parent_session) return;"), "live Jobs updates omit annotation children");
  assert!(js.contains("s.parent_session || !s.repo"), "live Projects updates omit annotation children");
  assert!(
    js.contains("jobId !== session.id && candidate.parent_session !== session.id"),
    "the hidden child still supplies annotation state to its parent job"
  );
}

#[test]
fn empty_cast_shows_placeholder_instead_of_player_error() {
  // Both the session-page embed and the standalone player page fetch the cast text first
  // and render a calm placeholder when it has no complete event lines yet, instead of
  // handing the player an empty/404 cast (which errors).
  let js = live_client_js();
  assert!(js.contains("Recording in progress — no frames yet."));
  assert!(js.contains("No recorded frames."));
  assert!(js.contains("cast-placeholder"));
  assert!(js.contains("{ data: text }"), "player mounts over the already-fetched text");
  let page = cast_player_page(&store_with_cast_proc(ProcStatus::Running), "castab", 0).expect("player page");
  assert!(page.contains("Recording in progress — no frames yet."));
  assert!(page.contains("cast-placeholder"));
  assert!(page.contains("const LIVE = true;"));
  let done = cast_player_page(&store_with_cast_proc(ProcStatus::Ok), "castab", 0).expect("player page");
  assert!(done.contains("const LIVE = false;"));
}

#[test]
fn cast_growth_notifications_append_in_place() {
  // The session page routes WS messages by type: cast_growth appends the newly recorded
  // suffix to the mounted player IN PLACE (no re-creation, no seek, no reload banner) —
  // smooth live following. Everything else stays on the tick path.
  let js = live_client_js();
  assert!(js.contains("if (msg.type === 'cast_growth') { onCastGrowth(msg); return; }"));
  assert!(js.contains("onWsMessage(JSON.parse(ev.data))"));
  assert!(js.contains("function followCastGrowth"));
  assert!(js.contains("box._player.append(text.slice(prev))"));
  assert!(!js.contains("Recording grew: +"), "the reload banner is gone — growth is invisible and smooth");
  // The standalone player page listens on its own WS connection — but only while the proc
  // runs — and follows growth the same way.
  let page = cast_player_page(&store_with_cast_proc(ProcStatus::Running), "castab", 0).expect("player page");
  assert!(page.contains("'cast_growth'"));
  assert!(page.contains("const SESSION = 'castab';"));
  assert!(page.contains("const PROC = 0;"));
  assert!(page.contains("player.append(text.slice(loadedChars))"));
  assert!(!page.contains("Recording grew: +"));
  assert!(page.contains("if (!castRunning) return;"), "no WS connect once the proc finished");
  // The player bundle itself carries the live-follow API the pages rely on.
  assert!(super::PLAYER_JS.contains("Player.prototype.append"), "the vendored player must have append");
  assert!(super::PLAYER_JS.contains("appendCast"), "the DOM-free core must parse appends");
}

#[test]
fn live_follows_from_player_toolbar_not_scsh_chrome() {
  // Session-page embed: no external Live button — the player owns ● Live when running.
  let running =
    super::proc::cast_embed_html("castab", &store_with_cast_proc(ProcStatus::Running).sessions["castab"].procs[0]);
  assert!(!running.contains("data-cast-live"), "session embed has no scsh Live button");
  let done = super::proc::cast_embed_html("castab", &store_with_cast_proc(ProcStatus::Ok).sessions["castab"].procs[0]);
  assert!(!done.contains("data-cast-live"));
  let js = live_client_js();
  assert!(js.contains("function setCastLive(box, on)"));
  assert!(js.contains("box._player.setLive(true)"));
  assert!(js.contains("controls: running ? { live: true } : true"), "running casts enable player Live control");
  assert!(js.contains("live: !!(box._live || running)"), "running casts start declared-live");
  // Standalone page: likewise no page-chrome Live toggle.
  let page = cast_player_page(&store_with_cast_proc(ProcStatus::Running), "castab", 0).expect("player page");
  assert!(!page.contains("live-toggle"), "standalone page has no external Live button");
  assert!(page.contains("controls: wantLive ? { live: true } : true"));
  let finished = cast_player_page(&store_with_cast_proc(ProcStatus::Ok), "castab", 0).expect("player page");
  assert!(!finished.contains("live-toggle"));
}

#[test]
fn export_html_download_renders_on_both_pages_and_hides_without_frames() {
  // Standalone player page: the download link points at the export endpoint, starts
  // hidden, and rides the same no-frames state as the placeholder.
  let page = cast_player_page(&store_with_cast_proc(ProcStatus::Ok), "castab", 0).expect("player page");
  assert!(page.contains(r#"<a id="dl-html" href="/cast/castab/0/export.html" download hidden>⬇ download .html</a>"#));
  assert!(page.contains("document.getElementById('dl-html').hidden = !stats.events;"));
  // Session-page embed: run snapshot lives in `.proc-actions` (hidden until frames);
  // client JS unhides it when the cast has events.
  let session = session_page(&store_with_cast_proc(ProcStatus::Ok), "castab").expect("session page");
  let procs = session_procs_html(&session);
  assert!(
    procs.contains(r#"class="chamfer btn btn--cyan btn--sm proc-snapshot""#)
      && procs.contains(r#"href="/cast/castab/0/export.html" data-cast-export download hidden"#)
      && procs.contains("<span>Run snapshot ⬇</span>"),
    "run snapshot button: {procs}"
  );
  let js = live_client_js();
  assert!(js.contains("ensureProcSnapshot"));
  assert!(js.contains("exportLink.hidden = !stats.events;"));
  assert!(js.contains("Incomplete run ⬇"), "live cast export says incomplete run while running");
  assert!(js.contains("Run snapshot ⬇"), "finished cast export says run snapshot");
}

#[test]
fn session_page_header_offers_the_session_export_download() {
  // Every session gets a whole-job download: cast-less procs remain useful note rows.
  let html = session_page(&store_with_cast_proc(ProcStatus::Ok), "castab").expect("session page");
  assert!(
    html.contains(r#"href="/job/castab/export.html" download"#) && html.contains("session-export"),
    "session export button"
  );
  // No recorded proc anywhere still retains the button and exports the job metadata.
  let mut store = store_with_cast_proc(ProcStatus::Ok);
  store.sessions.get_mut("castab").unwrap().procs[0].cast_path = None;
  let bare = session_page(&store, "castab").expect("session page");
  // (The `.session-export` CSS rule is in the shared shell, so match the anchor itself.)
  assert!(bare.contains("session-export"), "job snapshot remains available without a recording");
}

#[test]
fn live_client_js_counts_alive_clients_and_shutdown() {
  let js = live_client_js();
  assert!(js.contains("alive_clients"));
  assert!(js.contains("shutting down in"));
}

#[test]
fn live_client_js_skips_index_render_without_sessions() {
  let js = live_client_js();
  assert!(js.contains("if (!body || sessions == null) return"));
  // renderIndex (and the jobs-per-repo view) run only when a snapshot is present.
  assert!(js.contains("if (snapshot) {"));
  assert!(js.contains("renderIndex(snapshot, nowUnix)"));
}

/// Clicking a run inside a job must leave the address bar holding that run's permalink — and
/// must do it by rewriting the URL, not by navigating, so reading through a job's runs does not
/// bury the page the reader arrived from under one Back press per run.
#[test]
fn live_client_js_addresses_a_clicked_run_without_pushing_history() {
  let js = live_client_js();
  assert!(js.contains("function addressProcRow(det)"), "a clicked run row updates the address bar");
  assert!(
    js.contains("history.replaceState(history.state, '', location.pathname + location.search + hash)"),
    "addressing, not navigation: replaceState leaves no Back-button entry"
  );
  assert!(
    !js.contains("history.pushState(history.state, '', location.pathname + location.search + hash)"),
    "a run click must never push a history entry"
  );
  // Bound on click, because `toggle` also fires for restoreOpenProcs and live re-renders —
  // either would steal the address bar from whatever the reader is actually looking at.
  assert!(
    js.contains("root.addEventListener('click', (ev) => {\n    const summary = ev.target.closest && ev.target.closest('details.proc > summary');"),
    "the permalink follows a real click on a run's summary"
  );
  assert!(js.contains("if (ev.target.closest('a')) return;"), "cross-links inside a summary still navigate");
}

/// One rule for a run's address, shared by the click handler and the scroll-spy, so the hash a
/// click writes is byte-identical to the one scrolling to that row would write.
#[test]
fn live_client_js_has_a_single_run_permalink_rule() {
  let js = live_client_js();
  assert!(js.contains("function procPermalinkHash(det)"), "one canonical hash per run row");
  assert_eq!(
    js.matches("'#task-' + encodeURIComponent(step)").count(),
    1,
    "the workflow-step address is derived in exactly one place"
  );
  assert!(
    js.contains("candidates.push({ el: det, hash: procPermalinkHash(det) });"),
    "the scroll-spy addresses runs through the same rule as a click"
  );
}

/// Scrolling back to the top drops the fragment: the bare job URL is the honest permalink for
/// the whole job, and a stale `#task-…` would claim the reader is deep in a run they have left.
#[test]
fn live_client_js_clears_the_fragment_at_the_top_of_a_job() {
  let js = live_client_js();
  assert!(js.contains("if (window.scrollY <= TOP_EPSILON) {"), "the top of the page is a distinct address");
  assert!(
    js.contains("if (location.hash) history.replaceState(history.state, '', location.pathname + location.search);"),
    "at the top the URL loses its fragment entirely"
  );
  assert!(js.contains("const TOP_EPSILON = 2;"), "overscroll and subpixel offsets must not flicker the fragment");
}

/// The offline snapshot has no server and no `client_js`: its in-page navigation is plain
/// fragment anchors, so a graph node must stay a REAL `#task-…` link that a browser can follow
/// unaided. The live page only intercepts those clicks — it must never be what creates them.
#[test]
fn workflow_graph_nodes_are_real_fragment_links_for_the_offline_export() {
  let source = include_str!("workflow.rs");
  assert!(
    source.contains(r##"let href = format!("#task-{}", esc(&node.id));"##),
    "every graph node carries a plain in-page fragment href"
  );
  let js = live_client_js();
  assert!(
    js.contains("const a = ev.target.closest('a.wf-node');"),
    "the live page enhances those links rather than replacing them"
  );
}

#[test]
fn live_client_js_shows_connecting_on_ws_close() {
  let js = live_client_js();
  assert!(js.contains("setDaemonStatus('connecting', 'connecting…', null)"));
  assert!(!js.contains("daemon unreachable"));
}

#[test]
fn wrap_page_connecting_status_uses_blue() {
  use super::layout::wrap_page;
  let html = wrap_page("scsh sessions", 7274, None, None, "", "<p>body</p>");
  assert!(html.contains("class=\"chamfer daemon-status connecting\""));
  assert!(html.contains(".daemon-status.connecting .dot { background: var(--cyan);"));
  assert!(!html.contains("fonts.googleapis.com"), "offline-first: no CDN fonts (WEB-UI §5)");
  assert!(html.contains("position: sticky"), "status chrome is pinned");
  assert!(html.contains("--daemon-status-height"), "tabs stick flush under a shared status height");
  assert!(!html.contains("top: 3.1rem"), "no hard-coded sticky gap above the tabs");
  assert!(html.contains("width: 100%"), "status chrome spans the viewport");
  assert!(html.contains(r#"class="page-shell""#), "content sits in a centered column under the bar");
  // The bar is a chamfered island in a sticky full-width backdrop; the connection dot
  // is a 45°-rotated square (diamond), not a circle.
  assert!(html.contains(r#"class="daemon-status-wrap""#), "island sits in the sticky wrap");
  assert!(html.contains(".daemon-status .dot {"));
  assert!(html.contains("transform: rotate(45deg);"), "diamond dot");
  assert!(!html.contains("border-radius: 50%"), "no round dots remain");
}

#[test]
fn every_daemon_page_carries_the_inline_favicon() {
  use super::layout::wrap_page;
  // A data: URI, so the dashboard and the standalone player page stay request-free.
  let html = wrap_page("scsh sessions", 7274, None, None, "", "<p>body</p>");
  assert!(html.contains("<link rel=\"icon\" href=\"data:image/svg+xml,"), "dashboard favicon");
  let player = cast_player_page(&store_with_cast_proc(ProcStatus::Ok), "castab", 0).expect("player page");
  assert!(player.contains("<link rel=\"icon\" href=\"data:image/svg+xml,"), "player-page favicon");
  assert!(!player.contains("fonts.googleapis.com"), "cast player page is offline-first too");
}

#[test]
fn wrap_page_serves_valid_css_braces() {
  use super::layout::wrap_page;
  let html = wrap_page("scsh sessions", 7274, None, None, "Hello lede", "<p>body</p>");
  assert!(html.contains(":root {"));
  assert!(!html.contains(":root {{"));
  assert!(html.contains(".daemon-status {"));
  assert!(html.contains(r#"class="page-lede""#), "lede renders in the content column");
  assert!(html.contains("Hello lede"));
  // Status bar is the first body child so it pins full-width at the top; lede follows under it.
  let status_at = html.find(r#"id="daemon-status""#).expect("status bar");
  let shell_at = html.find(r#"class="page-shell""#).expect("page shell");
  let lede_at = html.find(r#"class="page-lede""#).expect("lede");
  assert!(status_at < shell_at && shell_at < lede_at, "chrome, then shell, then lede");
}

#[test]
fn review_round_four_fixes_hold() {
  use crate::daemon::model::OpenRepo;
  // (1) The Projects tab is populated server-side — jobs grouped by repository, plus a
  // "no jobs yet" row for repos opened with none — so it shows on first paint instead of
  // waiting for a full WebSocket snapshot a quiet daemon never sends.
  let mut store = store_with_cast_proc(ProcStatus::Ok);
  store.sessions.get_mut("castab").unwrap().ended_at = Some(5);
  store.open_repo(OpenRepo { path: "/work/empty".into(), opened_at: 9, clean: true });
  let html = super::index_page(&store);
  assert!(html.contains(r#"class="repo-filter-link""#), "project/repo names are filter links");
  assert!(html.contains(r#"title="/tmp/repo""#) || html.contains(r#"title="/tmp/repo">"#), "got: {html}");
  // Prefer a stable substring: the filter href for a non-project path.
  assert!(html.contains("/repo/tmp/repo") || html.contains("href=\"/repo/tmp/repo\""), "repo filter href: {html}");
  // Jobs are grouped by the task they ran, with a compact age stamp per job (the exact
  // age depends on the wall clock, so pin up to the stamp). The link — color and
  // underline — covers EXACTLY the six-letter id, in a fixed font (.job-id): never the
  // badge or the age stamp.
  assert!(
    html.contains(
      r#"<div class="repo-jobgroup"><span class="repo-jobgroup-name">default</span><div class="repo-job"><span class="chamfer session-status completed"><span>completed</span></span> <a class="job-id" href="/job/castab">castab</a> <span class="dim">"#
    ),
    "got: {html}"
  );
  assert!(
    html.contains(r#"title="/work/empty""#)
      && html.contains(r#"href="/repo/work/empty""#)
      && html.contains(r#"no jobs yet"#),
    "got: {html}"
  );
  // (2) Chips and counts carry instant data-tip tooltips, served by the shared floating tip
  // (native title tooltips were reset by every live table re-render).
  assert!(html.contains(r#"<span class="chip-count" data-tip="1 run in this job">1</span>"#), "got: {html}");
  assert!(html.contains(".ui-tip"), "tooltip CSS ships");
  assert!(html.contains("initTips"), "tooltip delegation ships");
  assert!(!super::index_page(&store).contains(r#"hchip--claude hchip--done" title="#), "chips use data-tip, not title");
  // (3) The UI speaks "jobs": table header, breadcrumb, empty states.
  assert!(html.contains("<th>Job</th>"), "got: {html}");
  assert!(!html.contains("<th>Session</th>"));
  // (4) A finished recording advertises WHEN it ended, and the chapters poll is bounded by
  // it — no more eternal "summarizing…" on casts that will never gain chapters.
  let mut ended = store_with_cast_proc(ProcStatus::Ok);
  ended.sessions.get_mut("castab").unwrap().procs[0].elapsed = Some(30.0);
  let shtml = session_page(&ended, "castab").expect("session renders");
  assert!(shtml.contains(r#" data-status="ok" data-kind="skill" data-ended="31">"#), "got: {shtml}");
  assert!(shtml.contains("CHAPTERS_WAIT_SECS"), "bounded summarizing window ships");
  // A still-running recording has no end yet (the session-meta dl has its own unrelated
  // data-ended, so pin the cast box's tag specifically).
  let running = session_page(&store_with_cast_proc(ProcStatus::Running), "castab").expect("session renders");
  assert!(running.contains(r#" data-status="running" data-kind="skill">"#), "got: {running}");
  assert!(!running.contains(r#" data-status="running" data-ended"#), "got: {running}");
  // (5) The per-container button reads "Force stop", not "kill" and without a leading ✕.
  let mut live = store_with_cast_proc(ProcStatus::Running);
  live.sessions.get_mut("castab").unwrap().last_seen_at = crate::daemon::paths::now_unix_secs();
  let shtml = session_page(&live, "castab").expect("session renders");
  assert!(shtml.contains(">Force stop</button>") || shtml.contains("<span>Force stop</span>"), "got: {shtml}");
  assert!(!shtml.contains("✕ Force stop"), "no leading ✕ on Force stop");
  assert!(!shtml.contains("✕ kill"));
  assert!(shtml.contains("<span>Incomplete job ⬇</span>"), "running job export says incomplete job: {shtml}");
  assert!(shtml.contains(r#"class="chamfer btn btn--cyan btn--sm session-export""#), "export matches Force stop size");
  assert!(
    shtml.find("session-export").unwrap() < shtml.find("id=\"session-stop\"").unwrap(),
    "job snapshot sits above Force stop in the actions stack"
  );
  assert!(shtml.contains("Job snapshot ⬇") || shtml.contains("Incomplete job ⬇"), "snapshot wording is explicit");
  // Finished job with a cast but no chapters sidecar → chapters pending (not incomplete).
  let done = session_page(&store_with_cast_proc(ProcStatus::Ok), "castab").expect("done session");
  assert!(
    done.contains("<span>Chapters pending ⬇</span>"),
    "finished job missing sidecar uses chapters pending: {done}"
  );
  assert!(done.contains("1 cast finalizing chapters"), "pending counter on job page: {done}");
  assert!(!done.contains("<span>Incomplete job ⬇</span>"), "finished job meta export is not incomplete");
  assert!(!done.contains("<span>Incomplete run ⬇</span>"), "finished job has no incomplete-run label");
  assert!(!done.contains(r#"<ul class="skills">"#), "no skills list on job page island");
  // Settled: cast + sidecar on disk → job snapshot.
  let dir = std::env::temp_dir().join(format!("scsh-chap-ready-{}", crate::runtime::random_nonce_6()));
  std::fs::create_dir_all(&dir).unwrap();
  let cast = dir.join("ready.cast");
  std::fs::write(&cast, "{\"version\":3}\n").unwrap();
  std::fs::write(dir.join("ready.chapters.json"), r#"{"summary":"ok","chapters":[]}"#).unwrap();
  let mut settled = store_with_cast_proc(ProcStatus::Ok);
  {
    let s = settled.sessions.get_mut("castab").unwrap();
    s.ended_at = Some(10);
    s.procs[0].cast_path = Some(cast.to_string_lossy().into_owned());
  }
  let settled_html = session_page(&settled, "castab").expect("settled session");
  assert!(
    settled_html.contains("<span>Job snapshot ⬇</span>"),
    "settled job export label: {settled_html}"
  );
  assert!(
    !settled_html.contains(r#"id="chapters-pending""#),
    "no pending line when sidecar exists: {settled_html}"
  );
  let _ = std::fs::remove_dir_all(&dir);
}

#[test]
fn review_round_five_fixes_hold() {
  // Projects: running jobs sort above completed ones, grouped by the task that ran, each
  // line stamped with a compact age; the launch tab reads "Run".
  let now = crate::daemon::paths::now_unix_secs();
  let mut store = store_with_cast_proc(ProcStatus::Ok);
  store.sessions.get_mut("castab").unwrap().ended_at = Some(5);
  {
    let done = store.sessions.get("castab").unwrap().clone();
    let mut live = done.clone();
    live.id = "livejb".into();
    live.ended_at = None;
    live.last_seen_at = now;
    live.profile = Some("arith".into());
    live.procs[0].status = ProcStatus::Running;
    store.sessions.insert("livejb".into(), live);
  }
  let html = super::index_page(&store);
  let arith = html.find(r#"<span class="repo-jobgroup-name">arith</span>"#).expect("arith group");
  let default = html.find(r#"<span class="repo-jobgroup-name">default</span>"#).expect("default group");
  assert!(arith < default, "the group with a running job sorts above the finished one: {html}");
  assert!(html.contains(r#"<span class="chamfer session-status running"><span>running</span></span> <a class="job-id" href="/job/livejb">livejb</a> <span class="dim">"#), "got: {html}");
  assert!(html.contains(r#"data-tab="run">Run</button>"#), "got: {html}");
  assert!(!html.contains("New job"));
  assert!(!html.contains("Start a job"));
  // Image-build sessions land under Projects → Internal, not the main Projects table.
  {
    let mut img = store_with_cast_proc(ProcStatus::Ok);
    img.sessions.get_mut("castab").unwrap().repo = crate::daemon::server::IMAGE_BUILDS_REPO.into();
    img.sessions.get_mut("castab").unwrap().profile = Some("build-images".into());
    img.sessions.get_mut("castab").unwrap().ended_at = Some(5);
    let html = super::index_page(&img);
    assert!(html.contains(r#"id="internal-jobs-card""#), "Internal section present: {html}");
    assert!(html.contains(r#"<p class="section-label">Internal</p>"#), "Internal label: {html}");
    assert!(html.contains(r#"<span class="repo-jobgroup-name">build-images</span>"#), "grouped by profile: {html}");
    assert!(html.contains(r#"href="/job/castab""#), "job link in Internal: {html}");
    assert!(
      !html.contains(r#"data-repo="(image builds)""#),
      "image builds excluded from Projects table: {html}"
    );
  }
  // Short ages are single-unit; both renderers ship the same helper and group markup.
  assert_eq!(super::format::format_short_age(45), "45s");
  assert_eq!(super::format::format_short_age(200), "3m");
  assert_eq!(super::format::format_short_age(7300), "2h");
  assert_eq!(super::format::format_short_age(200_000), "2d");
  assert!(html.contains("function formatShortAge"), "JS mirror ships");
  assert!(html.contains(".repo-jobgroup"), "group CSS ships");
  // The inline player pane has NO forced height — the player sizes its own box to the
  // recording's aspect at full width, so the pane is exactly as tall as the terminal wants
  // (the page-side sizeCastPane workaround is gone).
  let shtml = session_page(&store, "castab").expect("session renders");
  assert!(!shtml.contains("sizeCastPane"), "the pane-sizing workaround must stay gone");
  assert!(!shtml.contains(".cast-player { width: 100%; height:"), "no forced pane height");
  assert!(
    shtml.contains(r#".cast-player [part~="screen-box"]"#)
      && shtml.contains("width: 100%; min-width: 0; max-width: 100%;"),
    "wide terminals must be budgeted to the visible player pane: {shtml}"
  );
  assert!(!shtml.contains("fullscreenEl: box"), "fullscreen must contain only the player, not the scsh cast card");
  assert!(!shtml.contains(".cast:fullscreen"), "the outer cast card is never the fullscreen element");
  assert!(
    shtml.contains("button.proc-kill { background: var(--red); }") && shtml.contains("color: var(--text); border: none;"),
    "Force stop must keep its red chamfered border despite button.btn specificity: {shtml}"
  );
  assert!(
    shtml.contains("button.proc-restart { background: var(--orange); }"),
    "Force restart must keep its orange chamfered border despite button.btn specificity: {shtml}"
  );
}

#[test]
fn project_and_repo_filter_urls_normalize_extra_slashes() {
  use super::index::{parse_index_filter, IndexFilter};
  assert_eq!(parse_index_filter("/project//demo-1/"), Some(IndexFilter::Project("demo-1".into())));
  assert_eq!(parse_index_filter("/project/demo-1"), Some(IndexFilter::Project("demo-1".into())));
  assert_eq!(parse_index_filter("/project/"), None);
  assert_eq!(parse_index_filter("/project"), None);
  assert_eq!(parse_index_filter("/repo///Users/dima/foo/"), Some(IndexFilter::Repo("/Users/dima/foo".into())));
  assert_eq!(parse_index_filter("/repo/Users/dima/foo"), Some(IndexFilter::Repo("/Users/dima/foo".into())));
  assert_eq!(parse_index_filter("/repo/tmp/my%20repo"), Some(IndexFilter::Repo("/tmp/my repo".into())));
  assert_eq!(parse_index_filter("/repo/"), None);
}

#[test]
fn filtered_index_page_shows_only_matching_repo_and_opens_projects_tab() {
  use super::index::{index_page_with_filter, IndexFilter};
  let mut store = store_with_cast_proc(ProcStatus::Ok);
  store.sessions.get_mut("castab").unwrap().repo = "/tmp/repo".into();
  store.sessions.get_mut("castab").unwrap().ended_at = Some(5);
  {
    let mut other = store.sessions.get("castab").unwrap().clone();
    other.id = "other1".into();
    other.repo = "/tmp/other".into();
    store.sessions.insert("other1".into(), other);
  }
  let html = index_page_with_filter(&store, Some(IndexFilter::Repo("/tmp/repo".into())));
  assert!(html.contains(r#"class="tab active" data-tab="projects""#), "Projects tab active when filtered: {html}");
  assert!(html.contains("filter-banner"), "filter banner present");
  assert!(html.contains("Show all"), "clear-filter link");
  assert!(html.contains("href=\"/projects\""), "Show all clears to /projects");
  assert!(html.contains("castab"), "matching job shown");
  assert!(!html.contains(">other1<") && !html.contains("/job/other1"), "other repo's job hidden");
  assert!(html.contains(r#"class="repo-filter-link""#));
}

#[test]
fn review_round_six_fixes_hold() {
  let store = store_with_cast_proc(ProcStatus::Ok);
  let html = super::index_page(&store);
  // Durations can never render backwards: stale tick frames are dropped, and a superseded
  // WebSocket is fully retired before a reconnect (the "oscillating Duration" bug).
  assert!(html.contains("lastTickSecs"), "monotonic tick guard ships");
  assert!(html.contains("Retire any superseded socket"), "socket retirement ships");
  // The runtime switcher is a segmented control above the images table, not loose buttons
  // in the action strip; tips are multi-line and can tick a live running-for line.
  assert!(html.contains(r#"<div id="images-runtimes" class="images-runtimes"></div>"#), "got: {html}");
  assert!(html.contains(".seg-opt"), "segmented-control CSS ships");
  // The group is a chamfered ring: the outer layer shows through the padding/gaps, and
  // the corner options clip their own inner chamfer so the ring survives the diagonals.
  assert!(html.contains(r#"'<span class="chamfer seg" data-tip="#), "runtime switcher group is chamfered");
  assert!(html.contains("--seg-inner: calc(var(--cut) - var(--bw) * 0.5858);"));
  assert!(html.contains(".seg-opt:first-child {"), "corner options carry their own clip");
  // Active tabs are underlined by a trapezoid (45° ends), not a plain border.
  assert!(html.contains(".tab.active::after {"));
  assert!(html.contains("clip-path: polygon(3px 0%, calc(100% - 3px) 0%, 100% 100%, 0% 100%);"));
  assert!(!html.contains("border-bottom: 2px solid transparent"), "no rounded-era tab underline");
  assert!(html.contains("data-tip-running"), "live-ticking tip support ships");
  assert!(html.contains("white-space: pre-line"), "multi-line tip CSS ships");
  // Both JS chip-count writers share one renderer, so live re-syncs keep the tooltip.
  assert!(html.contains("function chipCountHtml"), "shared chip-count renderer ships");
}

#[test]
fn workflow_graph_renders_builtin_shapes() {
  use crate::daemon::workflow::{WorkflowMeta, WorkflowNodeMeta};
  fn skill_proc(index: usize, id: &str, harness: &str, status: ProcStatus) -> ProcRecord {
    ProcRecord {
      index,
      previous_attempt: None,
      kind: ProcKind::Skill,
      label: format!("{harness}: {id}"),
      status,
      note: None,
      detail: None,
      fail_reason: None,
      container_name: None,
      container_runtime: None,
      cast_path: None,
      diff_path: None,
      skill_source: Some(id.into()),
      route: None,
      result_path: None,
      annotate_target: None,
      harness: Some(harness.into()),
      skill_name: Some(id.into()),
      model: None,
      started_at: Some(1),
      elapsed: Some(1.0),
      lines: vec![],
    }
  }
  // arith: add + multiply → summarize
  let mut store = Store::new(DaemonMode::Persistent, 7274, 1);
  store.sessions.insert(
    "arith1".into(),
    Session {
      id: "arith1".into(),
      started_at: 1,
      ended_at: Some(10),
      profile: Some("arith".into()),
      kind: Some("workflow".into()),
      repo: "/tmp/r".into(),
      branch: "main".into(),
      skills: vec![],
      procs: vec![
        skill_proc(0, "add", "claude", ProcStatus::Ok),
        skill_proc(1, "multiply", "codex", ProcStatus::Ok),
        skill_proc(2, "summarize", "grok", ProcStatus::Ok),
      ],
      last_seen_at: 10,
      client_connected: false,
      run_pid: None,
      workflow: Some(WorkflowMeta {
        nodes: vec![
          WorkflowNodeMeta {
            id: "add".into(),
            proc_index: Some(0),
            order: 0,
            needs: vec![],
            conditional: false,
            when_summary: None,
          },
          WorkflowNodeMeta {
            id: "multiply".into(),
            proc_index: Some(1),
            order: 1,
            needs: vec![],
            conditional: false,
            when_summary: None,
          },
          WorkflowNodeMeta {
            id: "summarize".into(),
            proc_index: Some(2),
            order: 2,
            needs: vec!["add".into(), "multiply".into()],
            conditional: false,
            when_summary: None,
          },
        ],
      }),
      parent_session: None,
      supervisor: Default::default(),
    },
  );
  store.sessions.get_mut("arith1").unwrap().procs[2].elapsed = Some(198.0);
  let html = session_page(&store, "arith1").expect("page");
  assert!(html.contains(r#"id="workflow-graph""#), "workflow card present");
  assert!(html.contains("3 tasks · "), "summary starts with task count");
  assert!(html.contains(r#">3 succeeded</a>"#), "summary counts successful tasks unambiguously");
  assert!(html.contains(r#"class="wf-jump""#), "status counters are jump links");
  assert!(html.contains("Jump to first succeeded task"), "success counter links to a successful node");
  assert!(
    html.contains("href=\"#task-add\"") || html.contains("href=\"#task-multiply\""),
    "done jump targets a real node"
  );
  assert!(!html.contains("dependencies</p>"), "summary must not say N dependencies");
  assert!(!html.contains(r#"class="workflow-summary dim">3 tasks · 2 dependencies"#));
  assert!(html.contains(r#"data-workflow-step="add""#));
  assert!(html.contains(r#"data-workflow-step="multiply""#));
  assert!(html.contains(r#"data-workflow-step="summarize""#));
  assert!(html.contains(r#"id="task-add""#));
  assert!(html.contains("href=\"#task-summarize\""));
  assert!(html.contains(r#"class="chamfer wf-bookend wf-start""#), "Start bookend on multi-node graphs too");
  assert!(html.contains(r#"class="chamfer wf-bookend wf-finish""#), "Finish bookend");
  // Two DAG fan-in edges + Start→add + Start→multiply + summarize→Finish.
  let graph = html.split(r#"id="workflow-graph""#).nth(1).expect("graph card");
  let graph = graph.split("</svg>").next().expect("svg");
  assert_eq!(graph.matches("marker-end=\"url(#wf-arrow)\"").count(), 5);
  assert!(html.contains(r#"class="wf-arrowhead""#), "open chevron arrowheads, not filled triangles");
  let curved = graph
    .split(r#"class="wf-edge" d=""#)
    .filter_map(|part| part.split('"').next())
    .find(|path| path.contains(" C"))
    .expect("fan-in graph has a curved cross-row edge");
  assert!(curved.starts_with('M') && curved.contains(" L") && curved.rsplit_once(" L").is_some(), "{curved}");
  assert!(curved.split(" C").nth(1).unwrap_or("").contains(" L"), "arrow has a horizontal entry runway: {curved}");
  // Fan-in ports land at distinct y on summarize (not a single shared tip).
  let mut ends: Vec<(String, String)> = Vec::new();
  for part in graph.split(r#"class="wf-edge" d=""#) {
    if !part.contains(r#"marker-end="url(#wf-arrow)""#) {
      continue;
    }
    let Some(d) = part.split('"').next() else {
      continue;
    };
    // Path ends `… x2,y2` — last comma-separated pair.
    let Some((x, y)) = d.rsplit_once(',') else {
      continue;
    };
    let x = x.rsplit([' ', 'C']).next().unwrap_or(x);
    ends.push((x.to_string(), y.to_string()));
  }
  assert_eq!(ends.len(), 5, "expected five edges, got {ends:?}");
  // The two edges into summarize share an end x and differ in y.
  let mut by_x: std::collections::BTreeMap<String, Vec<String>> = std::collections::BTreeMap::new();
  for (x, y) in ends {
    by_x.entry(x).or_default().push(y);
  }
  let fan_in = by_x.values().find(|ys| ys.len() == 2).expect("summarize fan-in pair missing");
  assert_ne!(fan_in[0], fan_in[1], "fan-in edges must enter at different heights: {fan_in:?}");

  // All-successful graph: show one overall verdict and only the Succeeded legend state.
  assert!(html.contains(r#"workflow-outcome--completed"#));
  assert!(html.contains(">Job succeeded</span>"));
  assert!(html.contains("> Succeeded</li>"));
  assert!(!html.contains("> Done</li>"));
  assert!(html.contains(r#"<li class="wf-leg wf-leg-done""#));
  assert!(!html.contains(r#"<li class="wf-leg wf-leg-running""#));
  assert!(!html.contains(r#"<li class="wf-leg wf-leg-waiting""#));
  assert!(!html.contains(r#"<li class="wf-leg wf-leg-failed""#));
  assert!(!html.contains(r#"<li class="wf-leg wf-leg-stalled""#));
  assert!(!html.contains(r#"<li class="wf-leg wf-leg-skipped""#));
  assert!(
    html.contains(r#"<span class="wf-state-label">Succeeded</span><span class="wf-state-elapsed"> · 3m18s</span>"#),
    "duration sits beside status in compact clock form"
  );
  let graph_card = html.split(r#"id="workflow-graph""#).nth(1).unwrap();
  let graph_head = graph_card.split(r#"<div class="workflow-visual">"#).next().unwrap();
  let graph_visual = graph_card.split(r#"<div class="workflow-visual">"#).nth(1).unwrap();
  assert!(!graph_head.contains("workflow-legend"), "task legend must not read as part of the job-level header");
  assert!(
    graph_visual.find("workflow-legend") < graph_visual.find("workflow-scroll"),
    "legend overlays the visual before its scroll viewport"
  );

  {
    let session = store.sessions.get_mut("arith1").unwrap();
    session.ended_at = None;
    session.client_connected = true;
    session.last_seen_at = crate::daemon::paths::now_unix_secs();
  }
  let finalizing = session_page(&store, "arith1").expect("finalizing page");
  assert!(finalizing.contains(">Finalizing recordings</span>"), "all tasks done while casts settle is explicit");

  // fruits fan-out — live session so Waiting→Queued (deps met) is not collapsed to Stalled
  let now = crate::daemon::paths::now_unix_secs();
  store.sessions.insert(
    "fruit1".into(),
    Session {
      id: "fruit1".into(),
      started_at: now.saturating_sub(5),
      ended_at: None,
      profile: Some("fruits".into()),
      kind: Some("workflow".into()),
      repo: "/tmp/r".into(),
      branch: "main".into(),
      skills: vec![],
      procs: vec![
        skill_proc(0, "categorize", "claude", ProcStatus::Ok),
        skill_proc(1, "sort_fruits", "claude", ProcStatus::Waiting),
        skill_proc(2, "sort_vegetables", "claude", ProcStatus::Waiting),
      ],
      last_seen_at: now,
      client_connected: true,
      run_pid: Some(1),
      workflow: Some(WorkflowMeta {
        nodes: vec![
          WorkflowNodeMeta {
            id: "categorize".into(),
            proc_index: Some(0),
            order: 0,
            needs: vec![],
            conditional: false,
            when_summary: None,
          },
          WorkflowNodeMeta {
            id: "sort_fruits".into(),
            proc_index: Some(1),
            order: 1,
            needs: vec!["categorize".into()],
            conditional: false,
            when_summary: None,
          },
          WorkflowNodeMeta {
            id: "sort_vegetables".into(),
            proc_index: Some(2),
            order: 2,
            needs: vec!["categorize".into()],
            conditional: false,
            when_summary: None,
          },
        ],
      }),
      parent_session: None,
      supervisor: Default::default(),
    },
  );
  let fruits = session_page(&store, "fruit1").expect("fruits");
  assert!(
    fruits.contains(r#">1 succeeded</a>"#) && fruits.contains(r#">2 queued</a>"#),
    "queued stays separate from waiting in the headline"
  );
  assert!(fruits.contains("data-tip="), "nodes carry instant tooltips");
  assert!(
    fruits.contains("Queued — dependencies finished; waiting for the scheduler to start this task"),
    "queued tip explains why the node is idle"
  );
  // Queued is visually distinct from Waiting — the whole point of the split. The node
  // carries the queued state class (its own cyan accent) and the legend uses a different
  // glyph (◈) than Waiting's ◇, so the two never read as the same grey idle chip.
  assert!(fruits.contains(r#"data-wf-state="queued""#), "queued nodes carry their own state class");
  assert!(fruits.contains("wf-leg-queued"), "the legend has a distinct queued entry");
  assert!(fruits.contains(''), "queued uses the filled-inner diamond, not Waiting's hollow ◇");
  // 2 dependency edges + start → categorize + both sorts → finish.
  assert_eq!(
    fruits
      .split(r#"id="workflow-graph""#)
      .nth(1)
      .unwrap()
      .split("</svg>")
      .next()
      .unwrap()
      .matches("marker-end=\"url(#wf-arrow)\"")
      .count(),
    5 // categorize→2 sorts + Start→categorize + 2 sinks→Finish
  );
  assert!(fruits.contains(r#"data-workflow-step="categorize""#));

  // code-review conditional gate
  store.sessions.insert(
    "rev001".into(),
    Session {
      id: "rev001".into(),
      started_at: 1,
      ended_at: None,
      profile: Some("code-review".into()),
      kind: Some("workflow".into()),
      repo: "/tmp/r".into(),
      branch: "main".into(),
      skills: vec![],
      procs: vec![
        skill_proc(0, "probe_credentials", "claude", ProcStatus::Ok),
        skill_proc(1, "review", "claude", ProcStatus::Skipped),
      ],
      last_seen_at: 1,
      client_connected: true,
      run_pid: Some(1),
      workflow: Some(WorkflowMeta {
        nodes: vec![
          WorkflowNodeMeta {
            id: "probe_credentials".into(),
            proc_index: Some(0),
            order: 0,
            needs: vec![],
            conditional: false,
            when_summary: None,
          },
          WorkflowNodeMeta {
            id: "review".into(),
            proc_index: Some(1),
            order: 1,
            needs: vec!["probe_credentials".into()],
            conditional: true,
            when_summary: Some("Runs only if probe_credentials.ok = true".into()),
          },
        ],
      }),
      parent_session: None,
      supervisor: Default::default(),
    },
  );
  let review = session_page(&store, "rev001").expect("review");
  assert!(review.contains(r#"class="chamfer wf-gate""#), "gate marker");
  assert!(review.contains(">when</span>"), "gate label is the word when, not a diamond");
  assert!(
    review.contains("Runs only when its gate passes"),
    "gate tooltip is generic — no raw gate literals in the browser"
  );
  // Node ids may appear; gate *expressions* must not.
  assert!(!review.contains("probe_credentials.ok"), "no gate operand leakage");
  assert!(!review.contains("Runs only if"), "no authored when_summary in the page");
  assert!(!review.contains("Conditional task"), "no cryptic Conditional task label");
  // (Waiting/ready node icons ARE diamonds now; the gate itself is pinned to the word
  // "when" above, so no separate no-diamond check on the page.)
  assert!(review.contains(r#"wf-skipped"#));
  // 1 dependency edge + start → probe_credentials + review → finish.
  assert_eq!(
    review
      .split(r#"id="workflow-graph""#)
      .nth(1)
      .unwrap()
      .split("</svg>")
      .next()
      .unwrap()
      .matches("marker-end=\"url(#wf-arrow)\"")
      .count(),
    3 // Start→root + one DAG edge + sink→Finish
  );

  // Waiting tip names the blocker (WEB-UI §4 disclosure; not a bare "1 waiting on").
  let now = crate::daemon::paths::now_unix_secs();
  store.sessions.insert(
    "wait1".into(),
    Session {
      id: "wait1".into(),
      started_at: now.saturating_sub(5),
      ended_at: None,
      profile: Some("arith".into()),
      kind: Some("workflow".into()),
      repo: "/tmp/r".into(),
      branch: "main".into(),
      skills: vec![],
      procs: vec![
        skill_proc(0, "add", "claude", ProcStatus::Running),
        skill_proc(1, "summarize", "grok", ProcStatus::Waiting),
      ],
      last_seen_at: now,
      client_connected: true,
      run_pid: Some(1),
      workflow: Some(WorkflowMeta {
        nodes: vec![
          WorkflowNodeMeta {
            id: "add".into(),
            proc_index: Some(0),
            order: 0,
            needs: vec![],
            conditional: false,
            when_summary: None,
          },
          WorkflowNodeMeta {
            id: "summarize".into(),
            proc_index: Some(1),
            order: 1,
            needs: vec!["add".into()],
            conditional: false,
            when_summary: None,
          },
        ],
      }),
      parent_session: None,
      supervisor: Default::default(),
    },
  );
  let waiting = session_page(&store, "wait1").expect("waiting");
  assert!(waiting.contains("Waiting on:"), "waiting tip explains blockers");
  assert!(waiting.contains("waiting on add"), "meta line names the blocker");
  assert!(waiting.contains("margin: auto"), "graph stage centers on both axes when it fits");

  // Force-stopped is distinct from a natural failure (✕ vs ✗) but shares the fail/red accent.
  store.sessions.insert(
    "stop1".into(),
    Session {
      id: "stop1".into(),
      started_at: now.saturating_sub(30),
      ended_at: Some(now),
      profile: Some("demo-pr".into()),
      kind: Some("definition".into()),
      repo: "/tmp/r".into(),
      branch: "main".into(),
      skills: vec![],
      procs: vec![
        {
          let mut p = skill_proc(0, "cursor-build", "cursor", ProcStatus::Fail);
          p.fail_reason = Some(crate::failure::reason::FORCE_STOPPED.into());
          p.detail = Some("stopped from the session browser".into());
          p
        },
        {
          let mut p = skill_proc(1, "claude-run", "claude", ProcStatus::Fail);
          p.fail_reason = Some(crate::failure::reason::HARNESS_NONZERO.into());
          p
        },
      ],
      last_seen_at: now,
      client_connected: false,
      run_pid: None,
      workflow: Some(WorkflowMeta {
        nodes: vec![
          WorkflowNodeMeta {
            id: "cursor-build".into(),
            proc_index: Some(0),
            order: 0,
            needs: vec![],
            conditional: false,
            when_summary: None,
          },
          WorkflowNodeMeta {
            id: "claude-run".into(),
            proc_index: Some(1),
            order: 1,
            needs: vec![],
            conditional: false,
            when_summary: None,
          },
        ],
      }),
      parent_session: None,
      supervisor: Default::default(),
    },
  );
  let stopped = session_page(&store, "stop1").expect("stopped page");
  let summary =
    stopped.split(r#"class="workflow-summary dim">"#).nth(1).and_then(|s| s.split("</p>").next()).unwrap_or("?");
  assert!(stopped.contains(r#"wf-stopped"#), "stopped node class; summary={summary}");
  assert!(stopped.contains("Stopped"), "stopped label; summary={summary}");
  assert!(summary.contains("stopped"), "summary counts stopped separately: {summary}");
  assert!(summary.contains("failed"), "natural failure stays failed: {summary}");
  assert!(stopped.contains("wf-leg-stopped"), "legend lists stopped");
  assert!(stopped.contains(r#"workflow-outcome--failed"#), "mixed terminal states have one failed job verdict");
  assert!(stopped.contains(">Job failed</span>"), "overall failure is explicit above the task legend");

  {
    let session = store.sessions.get_mut("stop1").unwrap();
    session.ended_at = None;
    session.client_connected = true;
    session.procs[0].status = ProcStatus::Running;
    session.procs[0].fail_reason = Some(crate::failure::reason::STOP_REQUESTED.into());
    session.procs[0].detail = Some("terminating container from the session browser".into());
  }
  let terminating = session_page(&store, "stop1").expect("terminating page");
  assert!(terminating.contains(r#"wf-terminating"#), "graph task turns orange while teardown runs");
  assert!(terminating.contains("Terminating"), "graph task names the intermediate state");

  // Flat skill session (no authored DAG): still gets a job graph from its skill proc,
  // bookended Start → task → Finish so even a single-run job shows arrows.
  let flat = store_with_cast_proc(ProcStatus::Ok);
  let flat_html = session_page(&flat, "castab").expect("flat");
  assert!(flat_html.contains("· 1 task</p>"), "single-task lede has no terminal punctuation");
  assert!(!flat_html.contains("· 1 task.</p>"), "single-task lede is not punctuated as a sentence");
  assert!(flat_html.contains(r#"id="workflow-graph""#), "every job with skills gets a graph");
  assert!(flat_html.contains("Job graph"), "card title is Job graph");
  assert!(flat_html.contains("card--accent-left-orange workflow-card"), "graph island uses the orange accent");
  assert!(!flat_html.contains("card--accent-left-cyan workflow-card"), "graph no longer competes with cyan summaries");
  assert!(flat_html.contains(r#"data-workflow-step="add""#) || flat_html.contains("wf-node"), "skill node present");
  assert!(flat_html.contains(r#"class="chamfer wf-bookend wf-start""#), "Start bookend");
  assert!(flat_html.contains(r#"class="chamfer wf-bookend wf-finish""#), "Finish bookend");
  assert!(flat_html.contains("wf-start-play"), "play-triangle start glyph");
  assert!(flat_html.contains("wf-finish-flag"), "checkered finish flag");
  assert!(flat_html.contains("scrollbar-width: none"), "graph remains scrollable without visible scrollbar chrome");
  assert!(flat_html.contains(".workflow-scroll::-webkit-scrollbar"), "WebKit scrollbar chrome is hidden too");
  assert!(flat_html.contains("data-wf-zoom-fit>Fit</button>"), "server-rendered graph includes Fit");
  assert!(flat_html.contains("data-wf-expand"), "server-rendered graph includes the large-view control");
  assert!(flat_html.contains(">Full screen</button>"), "large-view control has a clear label");
  assert!(flat_html.contains("height: 29rem"), "normal graph viewport is about 60% of its former 48rem height");
  assert!(flat_html.contains(".workflow-card.wf-expanded"), "large view is an inset card, not the browser Fullscreen API");
  assert!(!flat_html.contains("wf-selected"), "task links do not leave a persistent graph-side selection state");
  assert!(!flat_html.contains("wf-start-line"), "dashed race-line start glyph is gone");
  assert!(!flat_html.contains("wf-bookend-label"), "bookends are icon-only");
  let edge_count = flat_html.matches(r#"class="wf-edge""#).count();
  assert!(edge_count >= 2, "single-run job still has Start→task and task→Finish edges: {edge_count}");
  // Same-row bookend edges must be straight horizontals (not S-curve cubics).
  let flat_graph = flat_html.split(r#"id="workflow-graph""#).nth(1).expect("flat graph");
  let flat_graph = flat_graph.split("</svg>").next().expect("flat svg");
  let flat_edges: Vec<&str> = flat_graph
    .split(r#"class="wf-edge" d=""#)
    .skip(1)
    .filter_map(|p| p.split('"').next())
    .collect();
  assert!(flat_edges.len() >= 2, "flat edges: {flat_edges:?}");
  for d in &flat_edges {
    assert!(d.contains(" L"), "same-row edge must be horizontal L, got {d}");
    assert!(!d.contains(" C"), "same-row edge must not be a cubic S-curve, got {d}");
  }

  // Client wiring
  let js = live_client_js();
  assert!(js.contains("function updateWorkflowGraph"));
  assert!(js.contains("function activateWorkflowTask"));
  assert!(js.contains("function initWorkflowGraph"));
  assert!(js.contains("function wfLegendHtml"));
  assert!(js.contains("function wfBuildGraphHtml"), "late graph creation without reload");
  assert!(js.contains("function wfLayoutWithBookends"), "live graph mirrors Start/Finish");
  assert!(js.contains("function wfLoopIslandsHtml"), "dynamic repeat iterations share a loop island");
  assert!(js.contains("function wfLoopProgressText"), "loop islands explain whether more iterations can follow");
  assert!(js.contains("session.workflow_loops"), "live loop copy reads authored iteration bounds");
  assert!(js.contains("may continue · up to "), "agent-decided loops stay visibly open-ended while running");
  assert!(js.contains("wf-loop-progress"), "loop continuation is a distinct visual element");
  assert!(js.contains("repeat|while-"), "repeat and do-while iterations share the dash loop id scheme");
  assert!(!js.contains("__repeat") && !js.contains("__while"), "the double-underscore id scheme is gone");
  assert!(js.contains("'do-while · '"), "do-while islands are labeled as do-while, not repeat");
  assert!(js.contains("' → '"), "a multi-step do-while island is named for its whole body (first → final)");
  assert!(js.contains("data-wf-zoom-in"), "graph has explicit zoom controls");
  assert!(js.contains("data-wf-zoom-fit"), "graph has a Fit control");
  assert!(js.contains("function wfFitZoom"), "Fit has one shared two-axis calculation");
  assert!(js.contains("return Math.min(widthZoom, heightZoom);"), "Fit uses the tighter bound and scales up as well as down");
  assert!(js.contains("Math.max(minimum, Math.min(maximum, next))"), "zoom-out stops at the fitted lower bound");
  assert!(js.contains("const minimum = Math.min(1, fit);"), "the zoom-out floor never exceeds 100%");
  // Fit fits the viewport AS IT IS NOW: the manual 2x ceiling lifts when the live fit
  // factor exceeds it (window resized, full screen toggled) — never a remembered size.
  assert!(js.contains("const maximum = Math.max(2, fit);"), "the ceiling follows the live fit factor");
  assert!(js.contains("const fit = wfFitZoom(scroller, stage);"), "bounds recompute from the live viewport");
  assert!(js.contains("zoomOut.disabled"), "the zoom-out control advertises when Fit is the lower bound");
  assert!(js.contains("scroller.scrollTop = 0"), "Fit resets both scroll axes before CSS centers the graph");
  assert!(js.contains("const zoomAt = "), "wheel and double-click zoom share one pointer-anchored path");
  assert!(
    js.contains("(scroller.scrollLeft + px) * ratio - px"),
    "zooming keeps the content under the pointer stationary"
  );
  assert!(
    js.contains("zoomAt(workflowZoom + (ev.deltaY < 0 ? 0.1 : -0.1), ev.clientX, ev.clientY)"),
    "pinch zoom anchors at the pointer, not the viewport center"
  );
  assert!(js.contains("addEventListener('dblclick'"), "double-clicking empty graph area zooms in at that point");
  assert!(
    js.contains("ev.target.closest('a.wf-node, a.wf-jump, button')"),
    "double-clicking a node or control keeps its own meaning"
  );
  assert!(js.contains("__scshWfInitialFitDone"), "the job page opens on the fitted graph, once per page load");
  assert!(js.contains("__scshWfExpandFitDone"), "first large-view entry per viewport size fits the graph");
  assert!(js.contains("const animateZoomAt = "), "double-click zoom glides to its target instead of jumping");
  assert!(
    js.contains("animateZoomAt(workflowZoom * 1.5, ev.clientX, ev.clientY)"),
    "double-click zooms through the animated path"
  );
  assert!(
    js.contains("addEventListener('pointerdown'") && js.contains("setPointerCapture"),
    "mouse drag on empty graph area pans the viewport, tracked through fast drags"
  );
  assert!(js.contains("ev.pointerType !== 'mouse'"), "touch keeps the container's native scrolling");
  assert!(
    js.contains("block: 'start'") && !js.contains("block: 'nearest'"),
    "task activation lands on the island's top edge"
  );
  assert!(js.contains("det.classList.add('proc-flash')"), "the landed island flashes so the eye finds it");
  assert!(
    flat_html.contains("scroll-margin-top: calc(var(--daemon-status-height)"),
    "the island's top clears the sticky status bar"
  );
  assert!(flat_html.contains("@keyframes proc-flash"), "the landing flash is a one-shot brightness pulse");
  assert!(flat_html.contains("cursor: grab"), "empty graph area advertises mouse-drag panning");
  assert!(flat_html.contains("display: flex"), "the graph viewport can center spare space on either axis");
  assert!(flat_html.contains("margin: auto"), "the stage centers only along axes with spare room");
  assert!(
    !flat_html.contains(".workflow-stage { position: relative; flex: 0 0 auto; min-height:"),
    "the stage height follows the actual graph so a small graph is vertically centered"
  );
  assert!(js.contains("data-wf-expand"), "graph has a large-view control");
  assert!(js.contains("let workflowExpanded = false"), "large view survives dynamic graph remounts");
  assert!(js.contains("aria-modal"), "large graph view exposes modal semantics");
  assert!(js.contains("current.contains(ev.target)"), "clicks inside the large graph keep it open");
  assert!(
    js.contains("current.__scshApplyWorkflowExpanded(false, false)"),
    "a click on the modal backdrop closes the large graph without stealing focus"
  );
  assert!(js.contains("ev.key !== 'Escape'"), "Escape closes the large graph view");
  assert!(js.contains("ev.key === 'Tab'"), "keyboard focus stays inside the large graph view");
  assert_eq!(
    js.matches("if (workflowExpanded) applyExpanded(false, false);").count(),
    2,
    "both graph run links and status-summary run links close large view before navigation"
  );
  assert!(!js.contains("requestFullscreen"), "large view deliberately avoids the browser Fullscreen API");
  assert!(js.contains("stage.style.zoom"), "zoom changes the graph without changing its topology");
  assert!(js.contains("let workflowZoom = 1"), "zoom survives dynamic graph remounts");
  assert!(!js.contains("window.scrollBy"), "the page viewport never moves except on direct human input");
  assert!(!js.contains("scroller.style.height"), "zoom scales inside the fixed viewport, never resizing the card");
  assert!(js.contains("scroll !== false"), "data-driven panel activation opens without scrolling the page");
  assert!(js.contains("function wfNodeTip"), "useful node tooltips");
  assert!(js.contains("function wfSummaryHtml"), "status counters are jump links");
  assert!(js.contains("a.wf-jump"), "summary jump click wiring");
  assert!(js.contains("history.pushState"), "task clicks push history");
  assert!(js.contains("pendingWorkflowStep"), "pre-registration pending selection");
  assert!(js.contains("Task details are not available yet"), "pending status copy");
}

#[test]
fn workflow_loop_island_advertises_future_iterations() {
  use crate::daemon::workflow::workflow_meta_from_def;
  use crate::harness_def::{builtin_defs, validate, DefSource};

  let session_for = |profile: &str| {
    let (_, src) = builtin_defs().into_iter().find(|(name, _)| *name == profile).expect("builtin loop");
    let def = validate(profile, src, DefSource::Builtin).expect("valid builtin loop");
    let now = crate::daemon::paths::now_unix_secs();
    Session {
      id: "loopmore".into(),
      started_at: now.saturating_sub(5),
      ended_at: None,
      profile: Some(profile.into()),
      kind: Some("workflow".into()),
      repo: "/tmp/loop-more".into(),
      branch: "main".into(),
      skills: vec![],
      procs: vec![],
      last_seen_at: now,
      client_connected: true,
      run_pid: Some(1),
      workflow: workflow_meta_from_def(&def),
      parent_session: None,
      supervisor: Default::default(),
    }
  };

  let mut store = Store::new(DaemonMode::Persistent, 7274, 1);
  let do_while = session_for("demo-loop-do-while");
  let api = crate::daemon::jsonio::session_json_api(&do_while);
  assert!(api.contains(r#""workflow_loops": [{ "id": "compare", "max_iterations": 25, "exact": false }]"#));
  store.sessions.insert("loopmore".into(), do_while);
  let open_ended = session_page(&store, "loopmore").expect("do-while page");
  assert!(open_ended.contains(r#"class="chamfer wf-loop-progress""#));
  assert!(open_ended.contains("↻ may continue · up to 24 more"));

  let mut fixed = session_for("demo-loop-repeat");
  fixed.id = "fixedlp".into();
  store.sessions.insert("fixedlp".into(), fixed);
  let exact = session_page(&store, "fixedlp").expect("repeat page");
  assert!(exact.contains("↻ 2 more iterations planned"));
}

#[test]
fn workflow_graph_bookends_runs_with_start_and_finish_terminals() {
  use crate::daemon::workflow::{WorkflowMeta, WorkflowNodeMeta};
  // A job with a single run reads start → run → finish: exactly two arrows.
  let mut store = Store::new(DaemonMode::Persistent, 7274, 1);
  store.sessions.insert(
    "solo1".into(),
    Session {
      id: "solo1".into(),
      started_at: 1,
      ended_at: Some(10),
      profile: Some("solo".into()),
      kind: Some("workflow".into()),
      repo: "/tmp/r".into(),
      branch: "main".into(),
      skills: vec![],
      procs: vec![ProcRecord {
        index: 0,
        previous_attempt: None,
        kind: ProcKind::Skill,
        label: "claude: add".into(),
        status: ProcStatus::Ok,
        note: None,
        detail: None,
        fail_reason: None,
        container_name: None,
        container_runtime: None,
        cast_path: None,
        diff_path: None,
        skill_source: Some("add".into()),
        route: None,
        result_path: None,
        annotate_target: None,
        harness: Some("claude".into()),
        skill_name: Some("add".into()),
        model: None,
        started_at: Some(1),
        elapsed: Some(1.0),
        lines: vec![],
      }],
      last_seen_at: 10,
      client_connected: false,
      run_pid: None,
      workflow: Some(WorkflowMeta {
        nodes: vec![WorkflowNodeMeta {
          id: "add".into(),
          proc_index: Some(0),
          order: 0,
          needs: vec![],
          conditional: false,
          when_summary: None,
        }],
      }),
      parent_session: None,
      supervisor: Default::default(),
    },
  );
  let html = session_page(&store, "solo1").expect("page");
  let graph = html.split(r#"id="workflow-graph""#).nth(1).expect("graph card");
  let svg = graph.split("</svg>").next().expect("svg");

  // Play-triangle Start and checkered-flag Finish bookends are present and decorative.
  assert!(graph.contains(r#"class="chamfer wf-bookend wf-start""#), "start bookend markup");
  assert!(graph.contains(r#"class="chamfer wf-bookend wf-finish""#), "finish bookend markup");
  assert!(graph.contains("wf-start-play"), "play-triangle start glyph");
  assert!(graph.contains("wf-finish-flag"), "checkered finish flag");
  // Bookends are divs, not links — they must never read (or click) as runs.
  assert!(!graph.contains(r#"wf-bookend wf-start" href"#), "start is not a link");
  assert!(!graph.contains(r#"wf-bookend wf-finish" href"#), "finish is not a link");
  assert!(!graph.contains(r#"wf-bookend wf-start" tabindex"#), "start never steals focus");

  // Exactly two arrows for a single-run job: start → add, add → finish. Same-row edges
  // draw as straight horizontals, not S-curve cubics (skip the arrowhead in <defs>).
  let edges = svg.split("</defs>").last().expect("edge paths");
  assert_eq!(edges.matches("marker-end=\"url(#wf-arrow)\"").count(), 2, "start → run → finish is two arrows");
  assert_eq!(edges.matches(" L").count(), 2, "same-row bookend edges are horizontal lines");
  assert_eq!(edges.matches(" C").count(), 0, "no cubic S-curves in a one-run job");

  // Styles ship with the page: not-interactive bookends, checkered flag at the finish.
  assert!(html.contains(".wf-bookend"), "bookend styles shipped");
  assert!(html.contains(".wf-start-play"), "play-triangle styles shipped");
  assert!(html.contains(".wf-finish-flag"), "finish flag styles shipped");

  // Client-side rebuild draws the same bookends so live updates stay consistent.
  let js = live_client_js();
  assert!(js.contains("function wfBookendHtml"), "client JS re-render draws bookends");
  assert!(js.contains("function wfLayoutWithBookends"), "client layout mirrors the bookends");
  assert!(js.contains("wf-start-play"), "client start glyph class");
  assert!(js.contains("wf-finish-flag"), "client finish flag class");
}