runnerd-cli 0.0.19

Command-line client for the runnerd daemon
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
//! The TUI through its reducer and `TestBackend` frames: a job moving through its slot, steps and the latest jobs;
//! every pane at 80×24; text at 80 to 136 columns that drops whole fields or ends in `…` rather than being cut
//! mid-word; no colour with `NO_COLOR`; a refused pause, a removed record and written sizing advice only on `y`;
//! repositories changed elsewhere; the history's filters; and, against a fake daemon, an event stream that drops, shows
//! "reconnecting" and resumes after the last event ID.

#![allow(
    clippy::unwrap_used,
    clippy::expect_used,
    clippy::panic,
    reason = "tests fail by panicking"
)]

use std::collections::BTreeMap;
use std::convert::Infallible;
use std::sync::{Arc, Mutex};
use std::time::Duration;

use axum::Router;
use axum::body::{Body, Bytes};
use axum::extract::State as Shared;
use axum::http::{HeaderMap, StatusCode};
use axum::response::{IntoResponse, Response};
use axum::routing::get;
use futures_util::{StreamExt, stream};
use ratatui::Terminal;
use ratatui::backend::TestBackend;
use ratatui::crossterm::event::{KeyCode, KeyEvent};
use ratatui::style::Color;
use runnerd_core::api::{
    Admission, AdmissionPolicy, Basis, Budget, CpuAdmission, Event, EventEnvelope, EventQuery,
    HostBudget, HostMemory, Job, JobMetrics, JobRunner, JobState, JobSummary, LogSummary,
    ProfileStatus, QueuedJob, Record, RecordLimits, Records, RepositoryStatus, RunnerStatus,
    Sample, SeedMemory, ShellMemory, SlotStatus, Status, Step, Timeline, Waiting,
};
use runnerd_core::api::{Advice, JobSizing, Peaks, ProfileSizing, Resize, Sizing, Spread, Starts};

use unicode_width::UnicodeWidthStr;

use super::app::{App, Connection, Effect, Msg, Pane, Refused, Removal, Resizing, View};
use super::draw::{Theme, draw};
use super::feed;
use crate::testing::Fake;
use crate::watch;

const NOW: u64 = 1100;

mod parity;

fn profile(name: &str, slots: usize, memory: u64) -> ProfileStatus {
    ProfileStatus {
        name: name.to_owned(),
        image: format!("runnerd-runner:{name}"),
        cpus: 8,
        memory,
        reseeding: false,
        slots: (1..=slots)
            .map(|slot| SlotStatus { slot, runner: None })
            .collect(),
    }
}

fn repository(name: &str, profile: &str) -> RepositoryStatus {
    RepositoryStatus {
        name: name.to_owned(),
        profile: profile.to_owned(),
        labels: vec!["self-hosted".to_owned()],
        pools: vec![runnerd_core::api::PoolStatus {
            profile: profile.to_owned(),
            labels: vec!["self-hosted".to_owned()],
            queued_jobs: 0,
        }],
        paused: false,
        queued_jobs: 0,
    }
}

/// A host with a jvm profile of two 9G slots and a node profile of one 7G slot, nothing running.
fn app() -> App {
    let live = watch::State {
        status: Status {
            version: "0.0.1".to_owned(),
            shutting_down: false,
            budget: Budget {
                admission: AdmissionPolicy::Caps,
                held: false,
                slice: "ci.slice".to_owned(),
                memory_max: Some(33 << 30),
                memory_in_use: 0,
                mirror_memory: 0,
                seed_memory: Vec::new(),
                shell_memory: Vec::new(),
                slice_cpus: None,
                host: None,
                cpu: None,
            },
            profiles: vec![profile("jvm", 2, 9 << 30), profile("node", 1, 7 << 30)],
            repositories: vec![
                repository("halyard/ledger", "jvm"),
                repository("halyard/storefront", "node"),
            ],
            queued: Vec::new(),
        },
        running: BTreeMap::new(),
        samples: BTreeMap::new(),
        host: None,
        recent: Vec::new(),
    };
    App::new(live, None, Vec::new(), Vec::new())
}

fn summary(id: u64, state: JobState) -> JobSummary {
    JobSummary {
        id,
        repository: "halyard/ledger".to_owned(),
        run_id: 4001,
        run_attempt: 1,
        workflow: Some("CI".to_owned()),
        name: "build".to_owned(),
        branch: Some("main".to_owned()),
        url: None,
        labels: vec!["self-hosted".to_owned()],
        state,
        conclusion: None,
        runner: (state != JobState::Queued).then(|| JobRunner {
            name: "runnerd-ab12-jvm-1".to_owned(),
            profile: "jvm".to_owned(),
            slot: 1,
            memory_cap: Some(9 << 30),
            cpu_cap: Some(8000),
        }),
        timeline: Timeline {
            queued_at: 1000,
            container_started_at: (state != JobState::Queued).then_some(1004),
            started_at: (state != JobState::Queued).then_some(1009),
            ..Timeline::default()
        },
        metrics: JobMetrics::default(),
        log: LogSummary::default(),
    }
}

fn step(number: u32, name: &str, started: u64, completed: Option<u64>) -> Step {
    Step {
        number,
        name: name.to_owned(),
        status: if completed.is_some() {
            "completed"
        } else {
            "in_progress"
        }
        .to_owned(),
        conclusion: completed.map(|_| "success".to_owned()),
        started_at: Some(started),
        completed_at: completed,
    }
}

fn runner(busy: bool) -> RunnerStatus {
    RunnerStatus {
        repository: "halyard/ledger".to_owned(),
        name: "runnerd-ab12-jvm-1".to_owned(),
        runner_id: 3,
        container: "c0ffee".to_owned(),
        busy,
        started_at: 1004,
        reserved: 9 << 30,
        warm: false,
    }
}

fn event(id: Option<u64>, event: Event) -> Msg {
    Msg::Event(EventEnvelope { id, at: NOW, event })
}

fn key(code: KeyCode) -> Msg {
    Msg::Key(KeyEvent::from(code))
}

/// The jobs `effects` open.
fn opened(effects: &[Effect]) -> Vec<u64> {
    effects
        .iter()
        .filter_map(|effect| match effect {
            Effect::OpenJob(id) => Some(*id),
            _ => None,
        })
        .collect()
}

/// The screen as text, one line per row.
fn screen(app: &App, width: u16, height: u16, colour: bool) -> String {
    let mut terminal = Terminal::new(TestBackend::new(width, height)).unwrap();
    terminal
        .draw(|frame| draw(frame, app, NOW, Theme { colour }))
        .unwrap();
    let buffer = terminal.backend().buffer();
    let mut text = String::new();
    for y in 0..height {
        let mut x = 0;
        while x < width {
            // A wide character covers the cell after it, which ratatui leaves blank.
            let symbol = buffer[(x, y)].symbol();
            text.push_str(symbol);
            x += u16::try_from(symbol.width()).unwrap().max(1);
        }
        text.push('\n');
    }
    text
}

/// A job of halyard/ledger queued, taken by jvm slot 1, through two steps of a three-step job, with samples.
fn running_job(app: &mut App) {
    let steps = [
        event(
            Some(1),
            Event::JobQueued {
                job: summary(42, JobState::Queued),
            },
        ),
        event(
            Some(2),
            Event::SlotChanged {
                profile: "jvm".to_owned(),
                slot: SlotStatus {
                    slot: 1,
                    runner: Some(runner(true)),
                },
            },
        ),
        event(
            Some(3),
            Event::JobStarted {
                job: summary(42, JobState::Running),
            },
        ),
        event(
            Some(4),
            Event::JobStep {
                job: 42,
                step: step(1, "Set up job", 1009, Some(1012)),
            },
        ),
        event(
            Some(5),
            Event::JobStep {
                job: 42,
                step: step(2, "Unit tests", 1012, None),
            },
        ),
        event(
            Some(6),
            Event::JobStep {
                job: 42,
                step: Step {
                    started_at: None,
                    status: "queued".to_owned(),
                    conclusion: None,
                    ..step(3, "Complete job", 0, None)
                },
            },
        ),
    ];
    for msg in steps {
        app.update(msg);
    }
    for (at, memory) in [(1090, 2 << 30), (1092, 3 << 30), (1094, 4 << 30)] {
        app.update(event(
            None,
            Event::JobSample {
                job: 42,
                sample: Sample {
                    at,
                    cpu: 3140,
                    memory,
                    network_received: 0,
                    network_sent: 0,
                },
            },
        ));
    }
}

#[test]
fn a_job_moves_through_its_slot_its_steps_and_into_the_latest_jobs() {
    let mut app = app();
    running_job(&mut app);

    let overview = screen(&app, 120, 40, false);
    assert!(overview.contains("jvm · slot 1"), "{overview}");
    assert!(overview.contains("CI / build"), "{overview}");
    assert!(overview.contains("2/3 Unit tests"), "{overview}");
    assert!(overview.contains("mem 4G"), "{overview}");
    assert!(overview.contains("1m 31s"), "ran since 1009: {overview}");

    // Enter on the selected slot opens its job.
    assert_eq!(opened(&app.update(key(KeyCode::Enter))), [42]);
    assert_eq!(app.view, View::Job);
    app.update(Msg::LogLine {
        job: 42,
        text: "##[group]Unit tests".to_owned(),
    });
    let job = screen(&app, 120, 40, false);
    let head: Vec<&str> = job.lines().skip(1).take(2).map(str::trim_end).collect();
    assert_eq!(
        head,
        ["■ halyard/ledger", "CI / build  RUNNING"],
        "the repository, then the job as every other view names it: {job}"
    );
    for expected in [
        "Set up job",
        "3s",
        "Unit tests",
        "1m 28s…",
        "Complete job",
        "–",
        "Waiting for a runner 4s",
        "Runner starting 5s",
        "capped at 9G and 8 CPUs",
        "##[group]Unit tests",
    ] {
        assert!(job.contains(expected), "{expected}: {job}");
    }

    let mut finished = summary(42, JobState::Completed);
    finished.conclusion = Some("success".to_owned());
    finished.timeline.finished_at = Some(1095);
    // The finished job is read again, for its steps' last state and its totals.
    assert!(
        app.update(event(Some(7), Event::JobFinished { job: finished }))
            .contains(&Effect::RefreshJob(42))
    );
    assert!(screen(&app, 120, 40, false).contains("SUCCESS"));
    assert_eq!(app.update(key(KeyCode::Esc)), vec![Effect::CloseJob]);
    let overview = screen(&app, 120, 40, false);
    assert!(overview.contains("SUCCESS"), "{overview}");
    assert!(app.live.running.is_empty() && app.series.is_empty());
}

#[test]
fn every_pane_fits_a_terminal_of_80_by_24() {
    let mut app = app();
    running_job(&mut app);
    let overview = screen(&app, 80, 24, true);
    for expected in [
        "Budget",
        "33G reserved in ci.slice",
        "Slots",
        "jvm 1",
        "jvm 2",
        "node 1",
        "2/3 Unit tests",
        "Memory",
        "CPU",
        "Repositories",
        "halyard/storefront",
        "Latest jobs",
        "q quit",
    ] {
        assert!(overview.contains(expected), "{expected}: {overview}");
    }

    app.update(key(KeyCode::Enter));
    for line in 0..100 {
        app.update(Msg::LogLine {
            job: 42,
            text: format!("line {line}"),
        });
    }
    let job = screen(&app, 80, 24, true);
    for expected in [
        "Set up job",
        "Unit tests",
        "Complete job",
        "mem",
        "Log",
        "line 99",
    ] {
        assert!(job.contains(expected), "{expected}: {job}");
    }

    let (generation, _) = history_request(&app.update(key(KeyCode::Char('2'))));
    app.update(history_page(
        generation,
        vec![summary(42, JobState::Running)],
        None,
    ));
    let history = screen(&app, 80, 24, true);
    assert!(
        history.contains("OUTCOME") && history.contains("RUNNING"),
        "{history}"
    );

    // Smaller still: nothing panics, the header and footer stay.
    app.update(key(KeyCode::Char('1')));
    let tiny = screen(&app, 30, 6, true);
    assert!(tiny.contains("runnerd"), "{tiny}");
}

#[test]
fn the_failed_figure_is_red_only_for_failures_of_the_runner() {
    // The colour of the count's cell in "2 failed" on the health line.
    let colour_of_failed = |runner_failed| {
        let mut app = app();
        app.activity = Some(runnerd_core::api::Activity {
            runner_failed,
            ..day()
        });
        let mut terminal = Terminal::new(TestBackend::new(160, 40)).unwrap();
        terminal
            .draw(|frame| draw(frame, &app, NOW, Theme { colour: true }))
            .unwrap();
        let buffer = terminal.backend().buffer();
        let row: Vec<&str> = (0..160).map(|x| buffer[(x, 1)].symbol()).collect();
        let x = row
            .windows(8)
            .position(|cells| cells.concat() == "2 failed")
            .unwrap();
        buffer[(u16::try_from(x).unwrap(), 1)].fg
    };
    assert_ne!(
        colour_of_failed(0),
        Color::LightRed,
        "failing tests are the repository's"
    );
    assert_eq!(
        colour_of_failed(1),
        Color::LightRed,
        "a job lost with its runner"
    );
}

#[test]
fn without_colour_nothing_is_coloured() {
    let mut app = app();
    running_job(&mut app);
    let mut terminal = Terminal::new(TestBackend::new(120, 40)).unwrap();
    terminal
        .draw(|frame| draw(frame, &app, NOW, Theme { colour: false }))
        .unwrap();
    let buffer = terminal.backend().buffer();
    assert!(
        buffer
            .content
            .iter()
            .all(|cell| cell.fg == Color::Reset && cell.bg == Color::Reset)
    );
}

#[test]
fn a_refused_pause_is_forced_only_after_y() {
    let mut app = app();
    running_job(&mut app);
    // The selected slot runs a job of halyard/ledger.
    assert_eq!(
        app.update(key(KeyCode::Char('p'))),
        vec![Effect::Pause {
            repository: Some("halyard/ledger".to_owned()),
            force: false
        }]
    );
    app.update(Msg::Busy(Refused {
        repository: Some("halyard/ledger".to_owned()),
        message: "halyard/ledger runs job 42 on runnerd-ab12-jvm-1".to_owned(),
        jobs: 1,
    }));
    assert!(screen(&app, 120, 40, false).contains("runs job 42"));
    assert_eq!(app.update(key(KeyCode::Char('n'))), Vec::new());
    assert!(app.confirm.is_none());

    app.update(Msg::Busy(Refused {
        repository: Some("halyard/ledger".to_owned()),
        message: "halyard/ledger runs job 42".to_owned(),
        jobs: 1,
    }));
    assert_eq!(
        app.update(key(KeyCode::Char('y'))),
        vec![Effect::Pause {
            repository: Some("halyard/ledger".to_owned()),
            force: true
        }]
    );
}

fn records(jobs: &[u64]) -> Records {
    Records {
        records: jobs
            .iter()
            .map(|job| Record {
                job: *job,
                repository: "halyard/ledger".to_owned(),
                profile: "jvm".to_owned(),
                workflow: Some("build".to_owned()),
                name: Some("test".to_owned()),
                created_at: NOW - 60,
                bytes: 300 << 20,
                image: format!("runnerd-record:{job}"),
            })
            .collect(),
        limits: RecordLimits {
            max_total: 20 << 30,
            max_count: 10,
            max_age: 7 * 24 * 3600,
            min_free: 10 << 30,
        },
        shells: vec![43],
    }
}

#[test]
fn records_near_max_total_say_so_first_on_a_narrow_heading() {
    let mut app = app();
    app.update(key(KeyCode::Char('3')));
    let mut near = records(&[42, 43]);
    near.limits.max_total = 700 << 20;
    app.update(Msg::Records(near));
    let shown = screen(&app, 80, 24, false);
    assert!(
        shown.contains("near max_total, so new ones remove the oldest"),
        "{shown}"
    );
}

#[test]
fn a_record_is_removed_only_after_y_and_the_list_follows_new_ones() {
    let mut app = app();
    assert_eq!(app.update(key(KeyCode::Char('3'))), vec![Effect::Records]);
    app.update(Msg::Records(records(&[42, 43])));
    app.update(key(KeyCode::Char('j')));
    let listed = screen(&app, 80, 24, false);
    for expected in [
        "halyard/ledger",
        "build / test",
        "300M",
        "shell open",
        "runnerctl shell 43",
    ] {
        assert!(listed.contains(expected), "{expected}: {listed}");
    }

    assert_eq!(app.update(key(KeyCode::Char('d'))), Vec::new());
    assert!(
        screen(&app, 120, 24, false).contains("y removes the record of job 43"),
        "d asks first"
    );
    assert_eq!(app.update(key(KeyCode::Char('n'))), Vec::new());
    assert_eq!(app.removal, None);
    app.update(key(KeyCode::Char('d')));
    assert_eq!(
        app.update(key(KeyCode::Char('y'))),
        vec![Effect::RemoveRecords(Removal::Job(43))]
    );
    app.update(key(KeyCode::Char('D')));
    assert_eq!(
        app.update(key(KeyCode::Char('y'))),
        vec![Effect::RemoveRecords(Removal::All)]
    );

    let created = Event::RecordCreated {
        record: records(&[44]).records.remove(0),
    };
    assert_eq!(app.update(event(Some(9), created)), vec![Effect::Records]);
}

/// jvm keeps its 9G; node's builds peak at 2.1G, so 3.25G caps, with 1G of page cache room, fit 4 of node's 2 × 7G.
fn sizing() -> Sizing {
    let spread = |p50, p95, p99, max| Spread { p50, p95, p99, max };
    let advised = |name: &str, memory: u64, peak: u64, advice| {
        let peaks = Peaks {
            jobs: 40,
            at_cap: 0,
            memory: spread(peak / 2, peak, peak, peak),
            cpu: spread(2000, 6000, 7000, 7000),
            on_cap: 0,
        };
        ProfileSizing {
            name: name.to_owned(),
            memory,
            cpus: 8,
            slots: 2,
            peaks: peaks.clone(),
            wait: spread(20, 300, 400, 400),
            starts: Starts {
                warm: 30,
                warm_p50: 2,
                cold: 10,
                cold_p50: 24,
            },
            busy: 70,
            wants_slots: true,
            advice,
            jobs: vec![JobSizing {
                repository: "halyard/storefront".to_owned(),
                workflow: Some("CI".to_owned()),
                name: "build".to_owned(),
                peaks,
                wait: spread(20, 300, 400, 400),
                advice,
            }],
            revert: None,
        }
    };
    Sizing {
        auto_plan: None,
        auto: false,
        changes: Vec::new(),
        since: NOW - 600,
        until: NOW,
        margin: 15,
        sample_interval: 2,
        min_jobs: 10,
        memory_max: Some(33 << 30),
        admission: AdmissionPolicy::Caps,
        profiles: vec![
            advised("jvm", 9 << 30, 7800 << 20, Advice::Keep { memory: 9 << 30 }),
            advised(
                "node",
                7 << 30,
                2150 << 20,
                Advice::Lower {
                    memory: 3328 << 20,
                    slots: 4,
                },
            ),
        ],
    }
}

#[test]
fn sizing_advice_is_written_into_the_config_only_after_y() {
    let mut app = app();
    assert_eq!(app.update(key(KeyCode::Char('4'))), vec![Effect::Sizing]);
    app.update(Msg::Sizing(sizing()));
    assert_eq!(
        app.update(key(KeyCode::Char('a'))),
        Vec::new(),
        "jvm has nothing to apply"
    );
    assert_eq!(app.resize, None);
    app.update(key(KeyCode::Char('j')));
    let shown = screen(&app, 120, 24, false);
    for expected in [
        "Sizing",
        "node: lower memory to 3.25G from 7G, its largest peak + 1G for page cache; 4 slots fit in the same 14G",
        "halyard/storefront",
        "CI / build",
        "a writes memory = \"3.25G\", slots = 4",
    ] {
        assert!(shown.contains(expected), "{expected}: {shown}");
    }

    app.update(key(KeyCode::Char('a')));
    assert!(
        screen(&app, 120, 24, false).contains("y writes memory = \"3.25G\", slots = 4 for node"),
        "a asks first"
    );
    assert_eq!(app.update(key(KeyCode::Char('n'))), Vec::new());
    app.update(key(KeyCode::Char('a')));
    assert_eq!(
        app.update(key(KeyCode::Char('y'))),
        vec![Effect::Resize(Resizing {
            profile: "node".to_owned(),
            resize: Resize {
                memory: 3328 << 20,
                slots: 4,
                cpus: None
            }
        })]
    );
    // A finished job changes the percentiles: the view reads them again.
    let finished = Event::JobFinished {
        job: summary(42, JobState::Completed),
    };
    assert!(
        app.update(event(Some(9), finished))
            .contains(&Effect::Sizing)
    );
    let narrow = screen(&app, 80, 24, false);
    assert!(
        narrow.contains("WAIT P95") && narrow.contains("same 14G"),
        "the sizing view fits 80 columns, the advice on two lines: {narrow}"
    );
    let wide = screen(&app, 140, 24, false);
    assert!(
        wide.contains("P50 WARM/COLD") && wide.contains("2s / 24s"),
        "wide, the waits of warm and cold starts show: {wide}"
    );
}

fn queued(id: u64, repository: &str, name: &str, at: u64) -> QueuedJob {
    QueuedJob {
        id,
        repository: repository.to_owned(),
        profile: None,
        run_id: 4002,
        run_attempt: 1,
        workflow: Some("E2E".to_owned()),
        name: name.to_owned(),
        branch: Some("main".to_owned()),
        url: None,
        queued_at: at,
        waiting: None,
    }
}

#[test]
fn the_queue_lists_each_waiting_job_oldest_first_with_why_and_opens_and_pauses_like_the_others() {
    let mut app = app();
    app.live.status.queued = vec![
        QueuedJob {
            waiting: Some(Waiting::NoSlot {
                profile: "node".to_owned(),
            }),
            ..queued(51, "halyard/storefront", "visual-diff", 1041)
        },
        QueuedJob {
            waiting: Some(Waiting::Paused),
            ..queued(52, "halyard/storefront", "a11y", 1090)
        },
    ];
    app.live.recent = vec![summary(42, JobState::Completed)];
    for width in [80, 120] {
        let shown = screen(&app, width, 24, false);
        let rows = rows_with(&shown, "E2E / ");
        assert_eq!(rows.len(), 2, "{shown}");
        assert!(
            rows[0].trim_start().starts_with("59s E2E / visual-diff")
                && rows[0].trim_end().ends_with("The node slot is busy"),
            "the oldest first, with how long it waited and why: {rows:?}"
        );
        assert!(
            rows[1].trim_end().ends_with("halyard/storefront is paused"),
            "{rows:?}"
        );
        assert!(
            shown.contains("2 waiting, oldest 59s"),
            "the health line counts them: {shown}"
        );
    }

    app.update(key(KeyCode::Tab));
    assert_eq!(
        app.update(key(KeyCode::Char('p'))),
        vec![Effect::Pause {
            repository: Some("halyard/storefront".to_owned()),
            force: false
        }]
    );
    assert_eq!(opened(&app.update(key(KeyCode::Enter))), [51]);
    let job = screen(&app, 120, 40, false);
    let head: Vec<&str> = job.lines().skip(1).take(2).map(str::trim_end).collect();
    assert_eq!(
        head,
        ["■ halyard/storefront", "E2E / visual-diff  QUEUED"],
        "{job}"
    );
    assert!(
        job.contains("Waiting for a runner 59s…  The node slot is busy"),
        "its page says why it waits: {job}"
    );
    assert!(
        !job.contains("Memory") && !job.contains("Steps"),
        "nothing to chart or list before a runner has it: {job}"
    );
}

#[test]
fn a_queued_job_left_open_shows_what_its_runner_reserved_once_it_starts() {
    let mut app = app();
    app.live.status.queued = vec![queued(51, "halyard/ledger", "build", 1000)];
    app.update(key(KeyCode::Tab));
    assert_eq!(opened(&app.update(key(KeyCode::Enter))), [51]);
    let effects = app.update(event(
        Some(9),
        Event::JobStarted {
            job: summary(51, JobState::Running),
        },
    ));
    assert!(
        effects.contains(&Effect::RefreshJob(51)),
        "the started job is read again for its admission: {effects:?}"
    );
    let summary = app.job.as_ref().unwrap().job.summary.clone();
    app.update(Msg::Job(Job {
        admission: Some(measured()),
        summary,
        steps: vec![],
        record: None,
    }));
    let job = screen(&app, 120, 40, false);
    for expected in ["reserved 6.5G", "reserved 3.6 CPUs"] {
        assert!(job.contains(expected), "{expected}: {job}");
    }
}

#[test]
fn the_selected_job_stays_selected_when_jobs_above_it_start_or_finish() {
    let mut app = app();
    app.live.status.queued = vec![
        queued(51, "halyard/ledger", "build", 1000),
        queued(52, "halyard/storefront", "build", 1000),
    ];
    app.live.recent = vec![summary(42, JobState::Completed)];
    app.update(key(KeyCode::Tab));
    app.update(key(KeyCode::Down));
    app.update(event(
        Some(9),
        Event::JobStarted {
            job: summary(51, JobState::Running),
        },
    ));
    assert_eq!(
        app.update(key(KeyCode::Char('p'))),
        vec![Effect::Pause {
            repository: Some("halyard/storefront".to_owned()),
            force: false
        }]
    );
    assert_eq!(opened(&app.update(key(KeyCode::Enter))), [52]);
    app.update(key(KeyCode::Esc));

    // The last queued job starts: the queue is gone, and so is its selection.
    app.update(event(
        Some(10),
        Event::JobStarted {
            job: summary(52, JobState::Running),
        },
    ));
    assert_eq!(app.pane, Pane::Slots, "an empty queue keeps no selection");
    app.update(key(KeyCode::Tab));
    app.update(key(KeyCode::Tab));
    assert_eq!(app.pane, Pane::Latest, "tab passes the empty queue");
    let mut newer = summary(43, JobState::Completed);
    newer.timeline.finished_at = Some(1090);
    app.update(event(Some(11), Event::JobFinished { job: newer }));
    assert_eq!(
        opened(&app.update(key(KeyCode::Enter))),
        [42],
        "a job finishing above the selected one leaves it selected"
    );
}

#[test]
fn the_budget_names_the_mirror_and_each_seed_container() {
    let mut app = app();
    let budget = &mut app.live.status.budget;
    budget.mirror_memory = 4 << 30;
    budget.seed_memory = vec![SeedMemory {
        profile: "jvm".to_owned(),
        memory: 9 << 30,
    }];
    budget.memory_in_use = 13 << 30;
    let shown = screen(&app, 160, 40, false);
    let names = shown.lines().nth(4).unwrap();
    assert!(
        names.contains("mirror 4G") && names.contains("jvm seed 9G"),
        "{shown}"
    );

    // A 512M mirror is a cell or two at common widths: the heading names it.
    app.live.status.budget.mirror_memory = 512 << 20;
    let shown = screen(&app, 120, 40, false);
    let mut lines = shown.lines().skip(2);
    let (heading, bar) = (lines.next().unwrap(), lines.next().unwrap());
    assert!(heading.ends_with("· mirror 512M"), "{shown}");
    assert!(bar.contains("▒ "), "the mirror's block shows: {shown}");

    // Without a MemoryMax the bar's whole is every block together, so the seed container still has its block.
    app.live.status.budget.memory_max = None;
    let shown = screen(&app, 120, 40, false);
    let names = shown.lines().nth(4).unwrap();
    assert!(names.contains("jvm seed 9G"), "{shown}");
}

#[test]
fn the_bar_shows_no_free_cell_when_core_says_nothing_is_free() {
    // Nothing runs, and the host leaves the slice 4G: it holds all 8G past the three slots' 25G.
    let mut app = app();
    app.live.status.budget.host = Some(HostBudget {
        memory: HostMemory {
            at: NOW,
            total: 64 << 30,
            available: 8 << 30,
            slice: 0,
            some: None,
            full: None,
            holders: Vec::new(),
        },
        limit: 4 << 30,
        spare: 4 << 30,
        stalled: false,
    });
    assert_eq!(app.live.status.budget_parts().free, None);
    let shown = screen(&app, 80, 24, false);
    let bar = shown.lines().nth(3).unwrap();
    assert!(!bar.contains('·'), "no free cell: {shown}");
    assert!(bar.ends_with('╱'), "the host's part ends the bar: {shown}");

    // Held under measured admission, nothing runs and all 33G is closed.
    let budget = &mut app.live.status.budget;
    budget.admission = AdmissionPolicy::Measured;
    budget.held = true;
    let shown = screen(&app, 80, 24, false);
    let mut lines = shown.lines().skip(3);
    let (bar, names) = (lines.next().unwrap(), lines.next().unwrap());
    assert!(!bar.contains('·'), "no free cell while held: {shown}");
    assert!(names.contains("closed past 90% 33G"), "{shown}");
}

#[test]
fn a_budget_label_never_cuts_a_figure_at_any_width() {
    let mut app = app();
    running_job(&mut app);
    app.update(event(
        None,
        Event::JobSample {
            job: 42,
            sample: Sample {
                at: 1096,
                cpu: 3140,
                memory: 3328 << 20,
                network_received: 0,
                network_sent: 0,
            },
        },
    ));
    for width in 60..=160 {
        let shown = screen(&app, width, 40, false);
        let names = shown.lines().nth(4).unwrap();
        assert!(
            !names.contains('…'),
            "at {width} columns a label is cut: {names}"
        );
    }
}

#[test]
fn the_budget_names_a_shell_open_in_a_record() {
    let mut app = app();
    let budget = &mut app.live.status.budget;
    budget.shell_memory = vec![ShellMemory {
        job: 42,
        memory: 9 << 30,
    }];
    budget.memory_in_use += 9 << 30;
    let shown = screen(&app, 160, 40, false);
    let names = shown.lines().nth(4).unwrap();
    assert!(names.contains("job 42 shell 9G"), "{shown}");
}

#[test]
fn a_profile_a_reload_adds_or_removes_comes_and_goes_and_the_selection_stays_on_a_slot() {
    let mut app = app();
    app.update(event(
        Some(5),
        Event::ProfileChanged {
            profile: profile("rust", 1, 4 << 30),
        },
    ));
    assert!(
        screen(&app, 120, 40, false).contains("rust · slot 1"),
        "the new profile's slot shows"
    );
    for _ in 0..3 {
        app.update(key(KeyCode::Char('j')));
    }
    app.update(event(
        Some(6),
        Event::ProfileRemoved {
            profile: "rust".to_owned(),
        },
    ));
    assert!(
        !screen(&app, 120, 40, false).contains("rust · slot 1"),
        "the removed profile's slot is gone"
    );
    assert_eq!(
        app.update(key(KeyCode::Char('R'))),
        vec![Effect::Reseed {
            profile: "node".to_owned()
        }],
        "the selection moved to the last slot left"
    );
}

#[test]
fn shift_p_and_u_pause_and_resume_every_repository_and_a_refused_pause_of_all_is_forced_after_y() {
    let mut app = app();
    assert_eq!(
        app.update(key(KeyCode::Char('P'))),
        vec![Effect::Pause {
            repository: None,
            force: false
        }]
    );
    app.update(Msg::Busy(Refused {
        repository: None,
        message: "halyard/ledger runs job 120055991700 on runnerd-3f9a-forge-jvm-1-1792721996; \
                  halyard/storefront runs job 120055991650 on runnerd-3f9a-forge-node-1-1792722114"
            .to_owned(),
        jobs: 2,
    }));
    let asked = screen(&app, 136, 40, false);
    let prompt = asked.lines().last().unwrap();
    assert!(
        prompt.starts_with(
            " y cancels the runs of 2 jobs and pauses, any other key keeps them running: "
        ),
        "{prompt}"
    );
    assert!(
        prompt.trim_end().ends_with('…'),
        "a message too long for the line is cut with …: {prompt}"
    );
    assert_eq!(
        app.update(key(KeyCode::Char('y'))),
        vec![Effect::Pause {
            repository: None,
            force: true
        }]
    );
    assert_eq!(
        app.update(key(KeyCode::Char('U'))),
        vec![Effect::Resume { repository: None }]
    );
}

#[test]
fn repositories_paused_added_or_removed_elsewhere_show_without_reading_the_status() {
    let mut app = app();
    let mut storefront = repository("halyard/storefront", "node");
    storefront.paused = true;
    storefront.queued_jobs = 3;
    app.update(event(
        Some(1),
        Event::RepositoryChanged {
            repository: storefront,
        },
    ));
    app.update(event(
        Some(2),
        Event::RepositoryChanged {
            repository: repository("halyard/docs", "node"),
        },
    ));
    app.update(event(
        Some(3),
        Event::RepositoryRemoved {
            repository: "halyard/ledger".to_owned(),
        },
    ));
    let shown = screen(&app, 120, 40, false);
    assert!(
        shown.contains("halyard/storefront  node  3 queued  paused"),
        "{shown}"
    );
    assert!(shown.contains("halyard/docs  node  0 queued"), "{shown}");
    assert!(!shown.contains("halyard/ledger"), "{shown}");
}

/// Records each event stream request's `Last-Event-ID`. The first stream sends event 7 and ends; the second
/// request fails, so the stream fails for good; the third serves event 8.
async fn dropping_then_back(
    Shared(seen): Shared<Arc<Mutex<Vec<Option<String>>>>>,
    headers: HeaderMap,
) -> Response {
    let request = {
        let mut seen = seen.lock().unwrap();
        seen.push(
            headers
                .get("last-event-id")
                .map(|id| id.to_str().unwrap().to_owned()),
        );
        seen.len()
    };
    let budget = |id: u64, in_use: u64| {
        let envelope = EventEnvelope {
            id: Some(id),
            at: NOW,
            event: Event::BudgetChanged {
                budget: Budget {
                    admission: AdmissionPolicy::Caps,
                    held: false,
                    slice: "ci.slice".to_owned(),
                    memory_max: None,
                    memory_in_use: in_use,
                    mirror_memory: 0,
                    seed_memory: Vec::new(),
                    shell_memory: Vec::new(),
                    slice_cpus: None,
                    host: None,
                    cpu: None,
                },
            },
        };
        format!(
            "event: budget.changed\nid: {id}\ndata: {}\n\n",
            serde_json::to_string(&envelope).unwrap()
        )
    };
    match request {
        1 => budget(7, 70).into_response(),
        2 => StatusCode::SERVICE_UNAVAILABLE.into_response(),
        _ => Body::from_stream(
            stream::iter([Ok::<_, Infallible>(Bytes::from(budget(8, 80)))])
                .chain(stream::pending()),
        )
        .into_response(),
    }
}

#[tokio::test]
async fn a_dropped_event_stream_shows_reconnecting_and_resumes_after_the_last_event() {
    let seen = Arc::new(Mutex::new(Vec::new()));
    let fake = Fake::serve(
        Router::new()
            .route("/v1/events", get(dropping_then_back))
            .with_state(seen.clone()),
    );
    let client = fake.client(Duration::from_secs(5), Duration::from_secs(5));
    let events = client.events(EventQuery::default(), None).await.unwrap();
    let (send, mut receive) = tokio::sync::mpsc::unbounded_channel();
    tokio::spawn(feed(client, events, send, Duration::from_millis(50)));

    let mut app = app();
    let mut saw_reconnecting = false;
    tokio::time::timeout(Duration::from_secs(10), async {
        while app.live.status.budget.memory_in_use != 80 {
            app.update(receive.recv().await.unwrap());
            if let Connection::Reconnecting(_) = app.connection {
                saw_reconnecting = true;
                assert!(screen(&app, 100, 24, false).contains("reconnecting"));
            }
        }
    })
    .await
    .expect("the feed should come back");
    assert!(saw_reconnecting);
    assert_eq!(app.connection, Connection::Live);
    assert!(screen(&app, 100, 24, false).contains("live"));
    let seen = seen.lock().unwrap();
    assert_eq!(seen.first(), Some(&None));
    assert_eq!(seen.last(), Some(&Some("7".to_owned())));
}

/// The history request among the effects: its generation and query.
fn history_request(effects: &[Effect]) -> (u64, runnerd_core::api::JobQuery) {
    effects
        .iter()
        .find_map(|effect| match effect {
            Effect::History { generation, query } => Some((*generation, query.clone())),
            _ => None,
        })
        .unwrap_or_else(|| panic!("expected a history request, not {effects:?}"))
}

fn history_query(effects: &[Effect]) -> runnerd_core::api::JobQuery {
    history_request(effects).1
}

/// The daemon's answer to a history request.
fn history_page(generation: u64, jobs: Vec<JobSummary>, next: Option<u64>) -> Msg {
    Msg::History {
        generation,
        page: Ok(runnerd_core::api::JobPage { jobs, next }),
    }
}

#[test]
fn the_history_filters_ask_for_the_first_page_again() {
    let mut app = app();
    let (generation, first) = history_request(&app.update(key(KeyCode::Char('2'))));
    assert_eq!((first.conclusion, first.before), (None, None));
    app.update(history_page(
        generation,
        vec![summary(42, JobState::Completed)],
        Some(42),
    ));
    let next = history_query(&app.update(key(KeyCode::Char(']'))));
    assert_eq!(next.before, Some(42));
    let failed = history_query(&app.update(key(KeyCode::Char('c'))));
    assert_eq!(failed.conclusion.as_deref(), Some("failure"));
    assert_eq!(failed.before, None, "a new filter starts at the first page");
    let ledger = history_query(&app.update(key(KeyCode::Char('s'))));
    assert_eq!(ledger.repository.as_deref(), Some("halyard/ledger"));
    assert_eq!(ledger.conclusion.as_deref(), Some("failure"));
}

#[test]
fn opening_a_job_from_the_latest_jobs_follows_only_its_log() {
    let mut app = app();
    let mut done = summary(41, JobState::Completed);
    done.conclusion = Some("failure".to_owned());
    done.timeline.finished_at = Some(1050);
    app.update(event(Some(1), Event::JobFinished { job: done }));
    // Past the empty queue and the repositories.
    app.update(key(KeyCode::Tab));
    app.update(key(KeyCode::Tab));
    assert_eq!(opened(&app.update(key(KeyCode::Enter))), [41]);
    app.update(Msg::LogLine {
        job: 99,
        text: "another job's line".to_owned(),
    });
    app.update(Msg::LogLine {
        job: 41,
        text: "##[error]Process completed with exit code 1.".to_owned(),
    });
    app.update(Msg::Job(Job {
        admission: None,
        summary: app.job.as_ref().unwrap().job.summary.clone(),
        steps: vec![step(1, "Set up job", 1009, Some(1012))],
        record: None,
    }));
    let job = screen(&app, 120, 40, false);
    assert!(job.contains("FAILURE"), "{job}");
    assert!(job.contains("exit code 1"), "{job}");
    assert!(!job.contains("another job's line"), "{job}");
}

/// A runner admitted by measured peaks: 6.5G and 3.6 CPUs reserved.
fn measured() -> Admission {
    Admission {
        policy: AdmissionPolicy::Measured,
        reserved: 6656 << 20,
        basis: Basis::Cap,
        free: None,
        slice: None,
        cpu: Some(CpuAdmission {
            reserved: 3600,
            basis: Basis::Cap,
            free: 38_700,
            waited: None,
        }),
    }
}

#[test]
fn an_open_jobs_charts_say_what_it_reserved_beside_its_caps() {
    let mut app = app();
    running_job(&mut app);
    app.update(key(KeyCode::Enter));
    let measured = measured();
    let caps = Admission {
        policy: AdmissionPolicy::Caps,
        reserved: 9 << 30,
        cpu: None,
        ..measured
    };
    let cases: [(_, &[&str], &str); 3] = [
        (
            Some(measured),
            &["cap 9G", "reserved 6.5G", "cap 8 CPUs", "reserved 3.6 CPUs"],
            "cap and reserved",
        ),
        // The reservation is the cap's, not a line of its own.
        (
            Some(caps),
            &["cap and reserved 9G", "cap 8 CPUs"],
            "─ reserved",
        ),
        (None, &["cap 9G", "cap 8 CPUs"], "reserved"),
    ];
    for (admission, expected, unexpected) in cases {
        let summary = app.job.as_ref().unwrap().job.summary.clone();
        app.update(Msg::Job(Job {
            admission,
            summary,
            steps: vec![],
            record: None,
        }));
        let job = screen(&app, 120, 40, false);
        for expected in expected {
            assert!(job.contains(expected), "{expected}: {job}");
        }
        assert!(!job.contains(unexpected), "no {unexpected:?}: {job}");
    }
}

fn sample(at: u64) -> Sample {
    Sample {
        at,
        cpu: 1000,
        memory: at,
        network_received: 0,
        network_sent: 0,
    }
}

#[test]
fn an_open_job_keeps_only_its_newest_samples() {
    let mut app = app();
    running_job(&mut app);
    app.update(key(KeyCode::Enter));
    // A job that has run for hours: far more samples than the overview keeps.
    app.update(Msg::Samples(runnerd_core::api::SampleSeries {
        job: 42,
        interval: 2,
        samples: (0..5000).map(sample).collect(),
    }));
    for at in 5000..6000 {
        app.update(event(
            None,
            Event::JobSample {
                job: 42,
                sample: sample(at),
            },
        ));
    }
    let samples = &app.job.as_ref().unwrap().samples;
    assert_eq!(samples.len(), 300);
    assert_eq!(samples.iter().last().map(|sample| sample.at), Some(5999));
    assert_eq!(samples.iter().next().map(|sample| sample.at), Some(5700));
}

#[test]
fn a_late_answer_to_an_earlier_history_filter_is_ignored() {
    let mut app = app();
    let (first, _) = history_request(&app.update(key(KeyCode::Char('2'))));
    app.update(history_page(
        first,
        vec![summary(40, JobState::Completed)],
        Some(40),
    ));
    let (failures, _) = history_request(&app.update(key(KeyCode::Char('c'))));
    // The rows and their continuation belong to the old filter: no next page while the new one loads.
    assert_eq!(app.update(key(KeyCode::Char(']'))), Vec::new());
    let (successes, query) = history_request(&app.update(key(KeyCode::Char('c'))));
    assert_eq!(query.conclusion.as_deref(), Some("success"));

    // The answers come back in the other order.
    let mut succeeded = summary(43, JobState::Completed);
    succeeded.conclusion = Some("success".to_owned());
    let mut failed = summary(44, JobState::Completed);
    failed.conclusion = Some("failure".to_owned());
    app.update(history_page(successes, vec![succeeded], None));
    app.update(history_page(failures, vec![failed], Some(44)));

    let page = app.history.page.as_ref().unwrap();
    assert_eq!(page.jobs.iter().map(|job| job.id).collect::<Vec<_>>(), [43]);
    let history = screen(&app, 120, 40, false);
    assert!(history.contains("outcome success"), "{history}");
    assert!(
        history.contains("SUCCESS") && !history.contains("FAILURE"),
        "{history}"
    );
}

/// A daemon where job 42 runs until the event stream breaks, and has finished by the time it is back. The first
/// stream sends only a live sample, so the client has no event ID to resume from.
#[derive(Default)]
struct Outage {
    streams: std::sync::atomic::AtomicU32,
    /// `Last-Event-ID` of each stream opened after the outage.
    resumed_after: Mutex<Vec<Option<String>>>,
}

impl Outage {
    fn finished(&self) -> bool {
        self.streams.load(std::sync::atomic::Ordering::Relaxed) > 1
    }
}

async fn outage_events(Shared(outage): Shared<Arc<Outage>>, headers: HeaderMap) -> Response {
    let stream = outage
        .streams
        .fetch_add(1, std::sync::atomic::Ordering::Relaxed);
    match stream {
        0 => {
            let envelope = EventEnvelope {
                id: None,
                at: NOW,
                event: Event::HostSample {
                    sample: runnerd_core::api::HostSample {
                        at: NOW,
                        cpu: 10,
                        memory: 1 << 20,
                    },
                },
            };
            format!(
                "event: host.sample\ndata: {}\n\n",
                serde_json::to_string(&envelope).unwrap()
            )
            .into_response()
        }
        1 => StatusCode::SERVICE_UNAVAILABLE.into_response(),
        _ => {
            outage.resumed_after.lock().unwrap().push(
                headers
                    .get("last-event-id")
                    .map(|id| id.to_str().unwrap().to_owned()),
            );
            Body::from_stream(stream::pending::<Result<Bytes, Infallible>>()).into_response()
        }
    }
}

async fn outage_jobs(
    Shared(outage): Shared<Arc<Outage>>,
    axum::extract::Query(query): axum::extract::Query<runnerd_core::api::JobQuery>,
) -> axum::Json<runnerd_core::api::JobPage> {
    let job = if outage.finished() {
        let mut done = summary(42, JobState::Completed);
        done.conclusion = Some("success".to_owned());
        done.timeline.finished_at = Some(1095);
        done
    } else {
        summary(42, JobState::Running)
    };
    let jobs = match query.state {
        None => vec![job],
        Some(state) if state == job.state => vec![job],
        Some(_) => Vec::new(),
    };
    axum::Json(runnerd_core::api::JobPage { jobs, next: None })
}

#[tokio::test]
async fn a_reconnect_with_no_event_to_resume_after_reads_the_state_again() {
    let outage = Arc::new(Outage::default());
    let fake = Fake::serve(
        Router::new()
            .route("/v1/events", get(outage_events))
            .route(
                "/v1/status",
                get(|| async { axum::Json(app().live.status) }),
            )
            .route("/v1/jobs", get(outage_jobs))
            .with_state(outage.clone()),
    );
    let client = fake.client(Duration::from_secs(5), Duration::from_secs(5));
    let events = client.events(EventQuery::default(), None).await.unwrap();
    let mut app = app();
    running_job(&mut app);
    assert!(app.live.running.contains_key(&42));
    let (send, mut receive) = tokio::sync::mpsc::unbounded_channel();
    tokio::spawn(feed(client, events, send, Duration::from_millis(50)));

    let mut was_reconnecting = false;
    tokio::time::timeout(Duration::from_secs(10), async {
        loop {
            app.update(receive.recv().await.unwrap());
            match app.connection {
                Connection::Reconnecting(_) => was_reconnecting = true,
                Connection::Live if was_reconnecting => break,
                Connection::Live => {}
            }
        }
    })
    .await
    .expect("the feed should come back");
    // Let anything sent with the reconnect arrive.
    tokio::time::sleep(Duration::from_millis(200)).await;
    while let Ok(msg) = receive.try_recv() {
        app.update(msg);
    }
    assert_eq!(*outage.resumed_after.lock().unwrap(), [None]);
    assert!(
        app.live.running.is_empty(),
        "job 42 finished during the outage"
    );
    assert_eq!(
        app.live
            .recent
            .first()
            .map(|job| (job.id, job.conclusion.as_deref())),
        Some((42, Some("success")))
    );
    let overview = screen(&app, 120, 40, false);
    assert!(overview.contains("SUCCESS"), "{overview}");
    assert!(!overview.contains("Unit tests"), "{overview}");
}

#[test]
fn a_job_read_again_as_finished_leaves_the_slots() {
    let mut app = app();
    running_job(&mut app);
    app.update(key(KeyCode::Enter));
    let mut done = summary(42, JobState::Completed);
    done.conclusion = Some("success".to_owned());
    app.update(Msg::Job(Job {
        admission: None,
        summary: done,
        steps: Vec::new(),
        record: None,
    }));
    assert!(app.live.running.is_empty() && app.series.is_empty());
    assert!(screen(&app, 120, 40, false).contains("SUCCESS"));
}

/// halyard/ledger's job `id`, which succeeded `ago` seconds before now.
fn finished(id: u64, ago: u64) -> JobSummary {
    let mut job = summary(id, JobState::Completed);
    job.conclusion = Some("success".to_owned());
    job.timeline.finished_at = Some(NOW - ago);
    job.timeline.started_at = Some(0);
    job
}

/// The rows of `screen` that contain `text`.
fn rows_with<'screen>(screen: &'screen str, text: &str) -> Vec<&'screen str> {
    screen.lines().filter(|row| row.contains(text)).collect()
}

#[test]
fn how_long_ago_a_job_finished_reads_in_full_at_every_width() {
    let mut app = app();
    app.live.recent = vec![finished(7, 17 * 60 + 31), finished(8, 5)];
    for width in [80, 96, 104, 120, 136] {
        let overview = screen(&app, width, 40, false);
        assert!(
            overview.contains("17m 31s ago"),
            "the age reads in full at {width}: {overview}"
        );
        assert!(
            overview.contains("just now"),
            "the age reads in full at {width}: {overview}"
        );
    }
    let (generation, _) = history_request(&app.update(key(KeyCode::Char('2'))));
    app.update(history_page(
        generation,
        vec![finished(7, 17 * 60 + 31)],
        None,
    ));
    for width in [80, 96, 120, 136] {
        let history = screen(&app, width, 24, false);
        assert!(
            history.contains("17m 31s ago"),
            "the age reads in full at {width}: {history}"
        );
    }
}

#[test]
fn every_slot_card_title_keeps_its_slot_number_and_a_rule() {
    let mut app = app();
    app.live.status.profiles[1] = profile("node", 2, 7 << 30);
    for width in [96, 104, 120, 136] {
        let overview = screen(&app, width, 40, false);
        for card in [
            "jvm · slot 1",
            "jvm · slot 2",
            "node · slot 1",
            "node · slot 2",
        ] {
            assert!(
                overview.contains(&format!("{card} ─")) || overview.contains(&format!("{card} ━")),
                "{card} then its rule at {width}: {overview}"
            );
        }
        // The size reads the same on every card.
        let sizes = rows_with(&overview, " · slot ")
            .iter()
            .map(|row| row.matches("CPUs").count())
            .sum::<usize>();
        assert!(
            sizes == 0 || sizes == 4,
            "every card or none shows its CPUs at {width}: {overview}"
        );
    }
}

#[test]
fn the_open_job_header_drops_whole_details_to_fit() {
    let mut app = app();
    running_job(&mut app);
    app.live.running.get_mut(&42).unwrap().summary.run_id = 40_018_663_900;
    app.update(key(KeyCode::Enter));
    let whole = [
        "branch main",
        "run 40018663900",
        "slot jvm",
        "1",
        "capped at 9G and 8 CPUs",
        "waited 9s",
        "ran 1m 31s",
    ];
    for width in [80, 96, 104, 120] {
        let job = screen(&app, width, 40, false);
        let header = rows_with(&job, "slot jvm · 1")[0].trim_end();
        assert!(
            header.ends_with("ran 1m 31s"),
            "the run time stays at {width}: {header}"
        );
        for detail in header.split(" · ") {
            assert!(
                whole.contains(&detail),
                "{detail:?} is whole at {width}: {header}"
            );
        }
    }
}

#[test]
fn text_that_does_not_fit_ends_in_an_ellipsis_and_leaves_its_neighbour_a_space() {
    let mut app = app();
    running_job(&mut app);
    let running = app.live.running.get_mut(&42).unwrap();
    running.summary.timeline.started_at = Some(NOW - (16 * 60 + 40));
    running.steps[1].name = "Integration tests (Testcontainers)".to_owned();
    app.live.status.profiles[1] = profile("node", 2, 7 << 30);
    app.live.status.repositories[1].queued_jobs = 1;
    app.live.recent = vec![finished(7, 60)];

    let small = screen(&app, 80, 24, false);
    let storefront = rows_with(&small, "halyard/storefront")[0];
    assert!(
        storefront.contains("1 queued "),
        "a space before the latest jobs: {small}"
    );
    let slot = rows_with(&small, "jvm 1  ")[0];
    assert!(
        slot.contains("… 16m 40s "),
        "a space before the run time: {small}"
    );

    let wide = screen(&app, 136, 40, false);
    let step = rows_with(&wide, "2/3 Integration tests")[0];
    assert!(
        step.contains("(Testcont…"),
        "the step's name ends in an ellipsis: {wide}"
    );

    let mut feature = finished(7, 60);
    feature.branch = Some("feat/checkout-flow".to_owned());
    let (generation, _) = history_request(&app.update(key(KeyCode::Char('2'))));
    app.update(history_page(generation, vec![feature], None));
    let history = screen(&app, 120, 24, false);
    assert!(
        history.contains(" feat/checkout… "),
        "the branch ends in an ellipsis: {history}"
    );
}

#[test]
fn wide_characters_are_fitted_by_the_cells_they_take() {
    let wide = "界".repeat(20);
    let mut app = app();
    app.live.status.profiles[1] = profile("ビルド", 2, 7 << 30);
    running_job(&mut app);
    app.live.running.get_mut(&42).unwrap().summary.branch = Some(wide.clone());

    for width in [96, 120, 136] {
        let overview = screen(&app, width, 40, false);
        for card in ["ビルド · slot 1", "ビルド · slot 2"] {
            assert!(
                overview.contains(&format!("{card} ─")),
                "{card} then its rule at {width}: {overview}"
            );
        }
    }

    app.update(key(KeyCode::Enter));
    let whole = [
        format!("branch {wide}"),
        "run 4001".to_owned(),
        "slot jvm".to_owned(),
        "1".to_owned(),
        "capped at 9G and 8 CPUs".to_owned(),
        "waited 9s".to_owned(),
        "ran 1m 31s".to_owned(),
    ];
    for width in [80, 104, 120] {
        let job = screen(&app, width, 24, false);
        let header = rows_with(&job, "slot jvm · 1")[0].trim_end();
        assert!(
            header.ends_with("ran 1m 31s"),
            "the run time stays at {width}: {header}"
        );
        for detail in header.split(" · ") {
            assert!(
                whole.iter().any(|whole| whole == detail),
                "{detail:?} is whole at {width}: {header}"
            );
        }
    }

    let mut job = finished(7, 60);
    job.repository.clone_from(&wide);
    job.branch = Some(wide.clone());
    let (generation, _) = history_request(&app.update(key(KeyCode::Char('2'))));
    app.update(history_page(generation, vec![job], None));
    let history = screen(&app, 120, 24, false);
    let row = rows_with(&history, "SUCCESS")[0];
    assert_eq!(
        row.matches("界…").count(),
        2,
        "the repository and the branch end in an ellipsis: {history}"
    );
}

#[test]
fn an_emoji_branch_cut_in_the_history_ends_in_an_ellipsis() {
    // `❤️` is a character and a variation selector: two cells, though each character alone takes one or none.
    let mut app = app();
    let mut job = finished(7, 60);
    job.branch = Some("❤️".repeat(20));
    let (generation, _) = history_request(&app.update(key(KeyCode::Char('2'))));
    app.update(history_page(generation, vec![job], None));
    for width in [120, 136] {
        let history = screen(&app, width, 24, false);
        let row = rows_with(&history, "SUCCESS")[0];
        assert!(
            row.contains("…"),
            "the branch ends in an ellipsis at {width}: {history}"
        );
    }
}

#[test]
fn the_open_job_metrics_drop_whole_figures_to_fit() {
    let mut app = app();
    running_job(&mut app);
    app.live.running.get_mut(&42).unwrap().summary.metrics = JobMetrics {
        cpu_time: Some(1_454_000),
        cpu_peak: Some(7_960),
        cpu_average: Some(3_310),
        memory_peak: Some(7 << 30),
        memory_average: Some(5 << 30),
        network_received: Some(329 << 20),
        network_sent: Some(37 << 20),
        mirror_bytes: 110 << 20,
        ..JobMetrics::default()
    };
    app.update(key(KeyCode::Enter));
    let full = screen(&app, 240, 40, false);
    let figures: Vec<&str> = rows_with(&full, "mem peak")[0].trim().split("  ").collect();
    assert!(figures.contains(&"mirror 110M"), "{full}");
    for (width, height) in [(80, 24), (96, 24), (96, 30), (104, 30)] {
        let job = screen(&app, width, height, false);
        let row = rows_with(&job, "mem peak")[0].trim();
        for figure in row.split("  ") {
            assert!(
                figures.contains(&figure),
                "{figure:?} is whole at {width}×{height}: {row}"
            );
        }
    }
}

/// A daemon whose first event stream sends only a live sample and ends; the stream the client opens again at
/// once succeeds, live only. The budget changed in between, and no event says so.
async fn cursorless_gap(
    Shared(streams): Shared<Arc<Mutex<Vec<Option<String>>>>>,
    headers: HeaderMap,
) -> Response {
    let opened = {
        let mut streams = streams.lock().unwrap();
        streams.push(
            headers
                .get("last-event-id")
                .map(|id| id.to_str().unwrap().to_owned()),
        );
        streams.len()
    };
    let sample = EventEnvelope {
        id: None,
        at: NOW,
        event: Event::HostSample {
            sample: runnerd_core::api::HostSample {
                at: NOW,
                cpu: 10,
                memory: 1 << 20,
            },
        },
    };
    let data = Bytes::from(format!(
        "event: host.sample\ndata: {}\n\n",
        serde_json::to_string(&sample).unwrap()
    ));
    if opened == 1 {
        Body::from(data).into_response()
    } else {
        Body::from_stream(stream::iter([Ok::<_, Infallible>(data)]).chain(stream::pending()))
            .into_response()
    }
}

/// The status: 1G in use once a second event stream was opened, nothing before.
async fn gap_status(
    Shared(streams): Shared<Arc<Mutex<Vec<Option<String>>>>>,
) -> axum::Json<Status> {
    let mut status = app().live.status;
    if streams.lock().unwrap().len() > 1 {
        status.budget.memory_in_use = 1 << 30;
    }
    axum::Json(status)
}

#[tokio::test]
async fn a_stream_reopened_at_once_with_no_event_to_resume_after_reads_the_state_again() {
    let streams = Arc::new(Mutex::new(Vec::new()));
    let fake = Fake::serve(
        Router::new()
            .route("/v1/events", get(cursorless_gap))
            .route("/v1/status", get(gap_status))
            .route(
                "/v1/jobs",
                get(|| async {
                    axum::Json(runnerd_core::api::JobPage {
                        jobs: Vec::new(),
                        next: None,
                    })
                }),
            )
            .with_state(streams.clone()),
    );
    let client = fake.client(Duration::from_secs(5), Duration::from_secs(5));
    let events = client.events(EventQuery::default(), None).await.unwrap();
    let mut app = app();
    let (send, mut receive) = tokio::sync::mpsc::unbounded_channel();
    tokio::spawn(feed(client, events, send, Duration::from_millis(50)));

    tokio::time::timeout(Duration::from_secs(5), async {
        while app.live.status.budget.memory_in_use != 1 << 30 {
            app.update(receive.recv().await.unwrap());
        }
    })
    .await
    .expect("the state should be read again after the gap");
    assert_eq!(app.connection, Connection::Live);
    assert!(
        screen(&app, 120, 40, false).contains("1G of 33G reserved"),
        "the reread status shows"
    );
    assert_eq!(*streams.lock().unwrap(), [None, None]);
}

#[test]
fn the_help_shows_every_key_in_an_80_by_24_terminal() {
    let mut app = app();
    app.update(key(KeyCode::Char('?')));
    let shown = screen(&app, 80, 24, false);
    for expected in ["overview", "write the advice", "this help", "suspend"] {
        assert!(shown.contains(expected), "{expected}: {shown}");
    }
}

#[test]
fn an_empty_records_view_says_in_full_how_records_are_kept() {
    let mut app = app();
    app.update(key(KeyCode::Char('3')));
    app.update(Msg::Records(records(&[])));
    let shown: String = screen(&app, 80, 24, false)
        .split_whitespace()
        .collect::<Vec<_>>()
        .join(" ");
    assert!(
        shown.contains("as a record, to open a shell where it failed"),
        "the hint wraps instead of being cut: {shown}"
    );
}

/// A day in which jobs that waited 10 s, 20 s and 300 s started, and 2 of 30 finished jobs failed.
fn day() -> runnerd_core::api::Activity {
    let started = |at, waited| runnerd_core::api::Started { at, waited };
    runnerd_core::api::Activity {
        since: 0,
        until: NOW,
        started: vec![started(200, 10), started(600, 20), started(900, 300)],
        finished: 30,
        failed: 2,
        runner_failed: 0,
    }
}

#[test]
fn the_overview_opens_with_ci_health_and_keeps_its_figures_whole_at_80_columns() {
    let mut app = app();
    app.activity = Some(day());
    app.live.status.queued = vec![queued(51, "halyard/storefront", "visual-diff", 1041)];
    let wide = screen(&app, 160, 40, false);
    let health = wide.lines().nth(1).unwrap();
    for expected in [
        "0/3 running",
        "1 waiting, oldest 59s",
        "24 h: wait p95 5m 00s",
        "2 failed of 30 finished",
    ] {
        assert!(health.contains(expected), "{expected}: {health}");
    }
    assert!(health.contains('█'), "the longest hour's bar: {health}");

    let small = screen(&app, 80, 24, false);
    let health = small.lines().nth(1).unwrap();
    assert!(
        health.contains("24 h: wait p95 5m 00s") && health.contains("2 failed"),
        "at 80 columns the figures stay and the details go whole: {health}"
    );
    assert!(!health.contains('…'), "nothing cut mid-figure: {health}");
}

#[test]
fn the_health_line_counts_the_warning_insights_and_says_where_to_read_them() {
    use runnerd_core::api::{Insight, InsightKind, Insights, Severity};
    let insight = |key: &str, severity| Insight {
        key: key.to_owned(),
        severity,
        repository: "halyard/api".to_owned(),
        workflow: Some("CI".to_owned()),
        job: Some("test".to_owned()),
        sentence: "halyard/api: CI / test failed and then passed on a re-run in 1 of its 3 runs."
            .to_owned(),
        kind: InsightKind::Flaky { flaky: 1, total: 3 },
        runs: Vec::new(),
    };
    let mut app = app();
    app.activity = Some(day());
    app.update(Msg::Insights(Box::new(Insights {
        since: 0,
        until: NOW,
        insights: vec![
            insight("flaky:a", Severity::Warning),
            insight("flaky:b", Severity::Warning),
            insight("waits:halyard/api", Severity::Notice),
        ],
        waits: Vec::new(),
    })));

    let wide = screen(&app, 160, 40, false);
    let health = wide.lines().nth(1).unwrap();
    assert!(
        health.contains("2 insights: runnerctl insights"),
        "notices are not counted: {health}"
    );
    let small = screen(&app, 80, 24, false);
    let health = small.lines().nth(1).unwrap();
    assert!(
        !health.contains("runnerctl insights") && health.contains("2 failed"),
        "the pointer goes first at 80 columns: {health}"
    );
}

#[test]
fn the_day_s_figures_are_read_again_as_jobs_start_and_end_and_once_a_minute_otherwise() {
    let mut app = app();
    app.update(Msg::Activity {
        generation: 1,
        activity: day(),
    });
    let host = |at| {
        event(
            None,
            Event::HostSample {
                sample: runnerd_core::api::HostSample {
                    at,
                    cpu: 0,
                    memory: 0,
                },
            },
        )
    };
    assert!(
        !app.update(host(NOW + 30)).iter().any(asks_activity),
        "read a moment ago"
    );
    assert!(app.update(host(NOW + 60)).iter().any(asks_activity));
    assert!(
        !app.update(host(NOW + 62)).iter().any(asks_activity),
        "asked once, not with every sample while the answer comes"
    );
    assert!(
        app.update(event(
            Some(1),
            Event::JobStarted {
                job: summary(42, JobState::Running)
            }
        ))
        .iter()
        .any(asks_activity),
        "a start changes the waits"
    );
}

#[test]
fn the_history_filters_by_the_workflows_its_repository_has_run() {
    let mut app = app();
    let (generation, _) = history_request(&app.update(key(KeyCode::Char('2'))));
    let mut e2e = summary(43, JobState::Completed);
    e2e.repository = "halyard/storefront".to_owned();
    e2e.workflow = Some("E2E".to_owned());
    app.update(history_page(
        generation,
        vec![summary(42, JobState::Completed), e2e],
        None,
    ));
    let ci = history_query(&app.update(key(KeyCode::Char('w'))));
    assert_eq!(
        ci.workflow.as_deref(),
        Some("CI"),
        "by name, across repositories"
    );
    let e2e = history_query(&app.update(key(KeyCode::Char('w'))));
    assert_eq!(e2e.workflow.as_deref(), Some("E2E"));
    assert!(screen(&app, 120, 24, false).contains("workflow E2E"));
    let any = history_query(&app.update(key(KeyCode::Char('w'))));
    assert_eq!(any.workflow, None, "then any again");

    app.update(key(KeyCode::Char('w')));
    let ledger = history_query(&app.update(key(KeyCode::Char('s'))));
    assert_eq!(
        (ledger.repository.as_deref(), ledger.workflow.as_deref()),
        (Some("halyard/ledger"), Some("CI")),
        "a workflow the repository runs stays"
    );
    let storefront = history_query(&app.update(key(KeyCode::Char('s'))));
    assert_eq!(
        (storefront.repository.as_deref(), storefront.workflow),
        (Some("halyard/storefront"), None),
        "one it never ran would show nothing, so it goes"
    );
    let only = history_query(&app.update(key(KeyCode::Char('w'))));
    assert_eq!(
        only.workflow.as_deref(),
        Some("E2E"),
        "only its own workflows"
    );
}

#[test]
fn a_job_s_page_compares_it_with_the_same_job_s_latest_runs() {
    let mut app = app();
    running_job(&mut app);
    let effects = app.update(key(KeyCode::Enter));
    let Some(Effect::Runs { job, query }) = effects
        .iter()
        .find(|effect| matches!(effect, Effect::Runs { .. }))
        .cloned()
    else {
        panic!("asks for its runs: {effects:?}");
    };
    assert_eq!(
        (
            query.repository.as_deref(),
            query.workflow.as_deref(),
            query.name.as_deref()
        ),
        (Some("halyard/ledger"), Some("CI"), Some("build"))
    );
    let run = |id, ran: u64, conclusion: &str| {
        let mut run = finished(id, 100);
        run.timeline.queued_at = 0;
        run.timeline.started_at = Some(10);
        run.timeline.finished_at = Some(10 + ran);
        run.conclusion = Some(conclusion.to_owned());
        run.metrics.memory_peak = Some(6 << 30);
        run
    };
    let mut other_workflow = run(30, 999, "failure");
    other_workflow.workflow = Some("Nightly".to_owned());
    let mut newest_first = vec![
        summary(42, JobState::Running),
        run(33, 300, "success"),
        run(32, 100, "failure"),
        run(31, 200, "success"),
        other_workflow,
    ];
    app.update(Msg::Runs {
        job,
        page: Ok(runnerd_core::api::JobPage {
            jobs: std::mem::take(&mut newest_first),
            next: None,
        }),
    });
    let shown = screen(&app, 120, 40, false);
    let heading = rows_with(&shown, "Latest 4 runs");
    assert_eq!(
        heading.len(),
        1,
        "another workflow's job of that name is left out: {shown}"
    );
    for expected in [
        "median run 3m 20s",
        "wait 10s",
        "1 of 3 failed",
        "largest peak 6G",
    ] {
        assert!(heading[0].contains(expected), "{expected}: {shown}");
    }
    let bars = rows_with(&shown, "  waited ")[0];
    assert!(
        bars.starts_with("ran ") && bars.contains("peak "),
        "{shown}"
    );
    assert!(
        screen(&app, 80, 24, false).contains("Latest 4 runs"),
        "it stays at 80 by 24"
    );
}

#[test]
fn sizing_says_what_auto_sizing_changes_next_and_when_or_why_it_will_not() {
    use runnerd_core::api::{AutoPlan, SizingChange, SizingHold};
    let mut app = app();
    app.update(key(KeyCode::Char('4')));
    let mut sizing = sizing();
    sizing.auto = true;
    sizing.auto_plan = Some(AutoPlan {
        every: 300,
        interval: 86_400,
        raise_interval: 3600,
        max_step: 25,
        next: vec![SizingChange {
            at: NOW,
            profile: "node".to_owned(),
            from: Resize {
                memory: 7 << 30,
                slots: 2,
                cpus: None,
            },
            to: Resize {
                memory: 5376 << 20,
                slots: 2,
                cpus: None,
            },
            reason: "lower memory".to_owned(),
            uncertain: false,
        }],
        held: vec![SizingHold {
            profile: "jvm".to_owned(),
            changed: NOW - 600,
            until: NOW + 7200,
            lower_until: NOW + 86_400 - 600,
        }],
        blocked: None,
        refused: Vec::new(),
    });
    app.update(Msg::Sizing(sizing.clone()));
    for width in [80, 120] {
        let shown = screen(&app, width, 24, false);
        assert!(
            shown.contains("looks every 5 minutes") && shown.contains("25% at a time"),
            "the rules, wrapped rather than cut: {shown}"
        );
        let jvm = rows_with(&shown, "■ jvm")[0];
        assert!(jvm.contains("No change for 2h 00m"), "{shown}");
        let node = rows_with(&shown, "■ node")[0];
        assert!(
            node.contains("Next look, within 5 minutes: memory 7G → 5.25G, slots 2"),
            "{shown}"
        );
    }

    let heading = rows_with(&screen(&app, 120, 24, false), "of history")[0].to_owned();
    assert!(
        heading.contains("peaks sampled every 2 s"),
        "the sampling caveat survives a 120-column heading: {heading}"
    );

    sizing.auto_plan.as_mut().unwrap().blocked =
        Some("runnerd.toml changed on disk since runnerd read it".to_owned());
    app.update(Msg::Sizing(sizing.clone()));
    let shown = screen(&app, 120, 24, false);
    assert!(
        shown.contains("No look changes anything now: runnerd.toml changed on disk"),
        "{shown}"
    );
    assert!(
        rows_with(&shown, "■ node")[0].contains("Once that is fixed: memory 7G → 5.25G"),
        "{shown}"
    );

    // A refused CPU raise shows beside the memory change the same look makes, here and in runnerctl sizing.
    let plan = runnerd_fixtures::auto_plan(NOW);
    let refusal = plan.refusal("node").unwrap();
    sizing.auto_plan = Some(plan);
    app.update(Msg::Sizing(sizing.clone()));
    let shown = screen(&app, 120, 40, false);
    assert!(
        rows_with(&shown, "■ node")[0].contains("Next look, within 5 minutes: memory 7G → 5.25G"),
        "{shown}"
    );
    assert!(
        shown.contains(&refusal[..60]),
        "the refusal shows beside it: {shown}"
    );
    let printed = crate::human::sizing(&sizing, NOW, crate::human::Paint { colour: false });
    assert!(printed.contains(&refusal), "{printed}");
}

/// Whether an effect reads the day's activity.
fn asks_activity(effect: &Effect) -> bool {
    matches!(effect, Effect::Activity { .. })
}

/// The generation of the activity read `effects` ask for.
fn activity_generation(effects: &[Effect]) -> u64 {
    effects
        .iter()
        .find_map(|effect| match effect {
            Effect::Activity { generation } => Some(*generation),
            _ => None,
        })
        .expect("an activity read")
}

#[test]
fn an_activity_read_that_answers_after_a_newer_one_is_ignored() {
    let mut app = app();
    let started = activity_generation(&app.update(event(
        Some(1),
        Event::JobStarted {
            job: summary(42, JobState::Running),
        },
    )));
    let mut failed = summary(42, JobState::Completed);
    failed.conclusion = Some("failure".to_owned());
    failed.timeline.finished_at = Some(1090);
    let finished =
        activity_generation(&app.update(event(Some(2), Event::JobFinished { job: failed })));
    let mut after = day();
    after.failed = 3;
    after.finished = 31;
    app.update(Msg::Activity {
        generation: finished,
        activity: after,
    });
    app.update(Msg::Activity {
        generation: started,
        activity: day(),
    });
    let health = screen(&app, 160, 40, false);
    assert!(
        health.contains("3 failed of 31 finished"),
        "the older read does not take the failure back: {health}"
    );
}

#[test]
fn a_frame_without_columns_or_rows_draws_nothing_and_does_not_panic() {
    let mut app = app();
    running_job(&mut app);
    app.activity = Some(day());
    app.live.status.queued = vec![queued(51, "halyard/storefront", "visual-diff", 1041)];
    for view in ['1', '2', '3', '4'] {
        app.update(key(KeyCode::Char(view)));
        for (width, height) in [(0, 24), (80, 0), (0, 0), (1, 1)] {
            screen(&app, width, height, true);
        }
    }
    app.update(key(KeyCode::Char('1')));
    app.update(key(KeyCode::Enter));
    for (width, height) in [(0, 24), (80, 0), (0, 0), (1, 1)] {
        screen(&app, width, height, true);
    }
}

#[test]
fn a_reason_read_before_a_job_or_slot_changed_is_not_shown() {
    let mut app = app();
    let mut a = queued(51, "halyard/ledger", "build", 1000);
    a.workflow = Some("CI".to_owned());
    let mut b = queued(52, "halyard/ledger", "test", 1001);
    b.waiting = Some(Waiting::BehindStart { job: 51 });
    app.live.status.queued = vec![a, b];
    let before = app.live.status.clone();
    let shown = screen(&app, 120, 24, false);
    assert!(
        shown.contains("Next after build, whose runner starts first"),
        "{shown}"
    );

    // A periodic read is under way when A starts.
    let host = event(
        None,
        Event::HostSample {
            sample: runnerd_core::api::HostSample {
                at: NOW,
                cpu: 0,
                memory: 0,
            },
        },
    );
    assert!(app.update(host).contains(&Effect::Reasons));
    app.update(event(
        Some(9),
        Event::JobStarted {
            job: summary(51, JobState::Running),
        },
    ));
    let shown = screen(&app, 120, 24, false);
    assert!(
        !shown.contains("Next after"),
        "a start may change every reason: none shows until read again: {shown}"
    );
    assert_eq!(
        app.update(Msg::Reasons(Some(Box::new(before)))),
        vec![Effect::Reasons],
        "the read from before the start is dropped and another made"
    );
    let shown = screen(&app, 120, 24, false);
    assert!(!shown.contains("Next after"), "{shown}");
    assert!(
        app.update(Msg::Reasons(None)).is_empty(),
        "a read after the start that fails asks for nothing until the next sample or change"
    );
    let shown = screen(&app, 120, 24, false);
    assert!(
        !shown.contains("Next after"),
        "a failed read leaves no reason rather than the old one: {shown}"
    );
}

#[test]
fn a_queued_job_s_reason_is_read_again_one_read_at_a_time_since_no_event_carries_it() {
    let mut app = app();
    let queued_event = |id| {
        event(
            Some(id),
            Event::JobQueued {
                job: summary(id, JobState::Queued),
            },
        )
    };
    assert_eq!(app.update(queued_event(51)), vec![Effect::Reasons]);
    assert!(
        app.update(queued_event(52)).is_empty(),
        "a burst makes one read at a time"
    );
    assert_eq!(
        app.update(Msg::Reasons(None)),
        vec![Effect::Reasons],
        "the second job queued during the read: read again"
    );
    let mut read = app.live.status.clone();
    read.queued[0].waiting = Some(Waiting::NoSlot {
        profile: "jvm".to_owned(),
    });
    // The read only knows the first job; the queue keeps the second from its event.
    read.queued.truncate(1);
    assert!(
        app.update(Msg::Reasons(Some(Box::new(read)))).is_empty(),
        "nothing changed during this read"
    );
    assert_eq!(
        app.live.status.queued.len(),
        2,
        "{:?}",
        app.live.status.queued
    );
    let shown = screen(&app, 120, 24, false);
    assert!(shown.contains("All 2 jvm slots are busy"), "{shown}");

    let host = |at| {
        event(
            None,
            Event::HostSample {
                sample: runnerd_core::api::HostSample {
                    at,
                    cpu: 0,
                    memory: 0,
                },
            },
        )
    };
    assert!(app.update(host(NOW)).contains(&Effect::Reasons));
    app.update(Msg::Reasons(None));
    assert!(
        !app.update(host(NOW + 5)).contains(&Effect::Reasons),
        "read again every 10 s while nothing changes"
    );
    assert!(app.update(host(NOW + 10)).contains(&Effect::Reasons));
}

#[test]
fn the_open_job_s_own_bar_among_its_runs_follows_it_through_start_and_a_missed_finish() {
    let mut app = app();
    let mut waiting = queued(42, "halyard/ledger", "build", 1000);
    waiting.workflow = Some("CI".to_owned());
    app.live.status.queued = vec![waiting];
    app.update(key(KeyCode::Tab));
    let effects = app.update(key(KeyCode::Enter));
    let Some(Effect::Runs { job, .. }) = effects
        .iter()
        .find(|effect| matches!(effect, Effect::Runs { .. }))
        .cloned()
    else {
        panic!("{effects:?}");
    };
    let earlier = |id| {
        let mut run = finished(id, 500);
        run.timeline.queued_at = 0;
        run.timeline.started_at = Some(10);
        run.timeline.finished_at = Some(110);
        run
    };
    app.update(Msg::Runs {
        job,
        page: Ok(runnerd_core::api::JobPage {
            jobs: vec![summary(42, JobState::Queued), earlier(32), earlier(31)],
            next: None,
        }),
    });
    // The newest run's bar is the last before the waits.
    let ran_bar = |app: &App| {
        let shown = screen(app, 120, 40, false);
        let row = rows_with(&shown, "  waited ")[0].to_owned();
        let before = row.split("  waited ").next().unwrap().to_owned();
        (before.chars().last().unwrap(), shown)
    };
    assert_eq!(ran_bar(&app).0, ' ', "queued, it has not run");

    app.update(event(
        Some(1),
        Event::JobStarted {
            job: summary(42, JobState::Running),
        },
    ));
    let (bar, shown) = ran_bar(&app);
    assert_ne!(bar, ' ', "started, it shows its run so far: {shown}");

    app.update(Msg::Disconnected("runnerd is not running".to_owned()));
    let effects = app.update(Msg::Reconnected);
    assert!(effects.contains(&Effect::RefreshJob(42)), "{effects:?}");
    assert!(
        effects
            .iter()
            .any(|effect| matches!(effect, Effect::Runs { job: 42, .. })),
        "its runs are read again too: {effects:?}"
    );
    let mut done = summary(42, JobState::Completed);
    done.conclusion = Some("failure".to_owned());
    done.timeline.finished_at = Some(1090);
    app.update(Msg::Job(Job {
        admission: None,
        summary: done,
        steps: Vec::new(),
        record: None,
    }));
    let shown = screen(&app, 120, 40, false);
    assert!(
        rows_with(&shown, "Latest 3 runs")[0].contains("1 of 3 failed"),
        "the finish read again counts among its runs: {shown}"
    );
}

#[test]
fn the_open_job_s_peak_among_its_runs_rises_with_its_samples_until_its_record_is_final() {
    let mut app = app();
    running_job(&mut app);
    // Opened after its record's peak was 2G, with no samples seen here yet.
    app.series.clear();
    app.live
        .running
        .get_mut(&42)
        .unwrap()
        .summary
        .metrics
        .memory_peak = Some(2 << 30);
    let effects = app.update(key(KeyCode::Enter));
    let Some(Effect::Runs { job, .. }) = effects
        .iter()
        .find(|effect| matches!(effect, Effect::Runs { .. }))
        .cloned()
    else {
        panic!("{effects:?}");
    };
    let mut current = summary(42, JobState::Running);
    current.metrics.memory_peak = Some(2 << 30);
    let mut earlier = finished(31, 500);
    earlier.metrics.memory_peak = Some(6 << 30);
    app.update(Msg::Runs {
        job,
        page: Ok(runnerd_core::api::JobPage {
            jobs: vec![current, earlier],
            next: None,
        }),
    });
    let sample = |memory| {
        event(
            None,
            Event::JobSample {
                job: 42,
                sample: Sample {
                    at: 1096,
                    cpu: 1000,
                    memory,
                    network_received: 0,
                    network_sent: 0,
                },
            },
        )
    };
    app.update(sample(8 << 30));
    // The peaks' bars, oldest first: the earlier run's 6G, then this one's.
    let peaks = |app: &App| {
        let shown = screen(app, 120, 40, false);
        let row = rows_with(&shown, "  peak ")[0].to_owned();
        let heading = rows_with(&shown, "Latest 2 runs")[0].to_owned();
        let bars: Vec<char> = row
            .split("  peak ")
            .nth(1)
            .unwrap()
            .trim_end()
            .chars()
            .collect();
        (heading, bars)
    };
    let (heading, bars) = peaks(&app);
    assert!(heading.contains("largest peak 8G"), "{heading}");
    assert_eq!(bars, ['▆', '█'], "this run's 8G tops the earlier 6G");

    // A lower sample, or a refresh of the record while it runs, does not lower the mark.
    app.update(sample(1 << 30));
    let mut refreshed = summary(42, JobState::Running);
    refreshed.metrics.memory_peak = Some(3 << 30);
    app.update(Msg::Job(Job {
        admission: None,
        summary: refreshed,
        steps: Vec::new(),
        record: None,
    }));
    assert!(
        peaks(&app).0.contains("largest peak 8G"),
        "{:?}",
        peaks(&app)
    );

    // The final record's totals win.
    let mut done = summary(42, JobState::Completed);
    done.conclusion = Some("success".to_owned());
    done.timeline.finished_at = Some(1098);
    done.metrics.memory_peak = Some(7 << 30);
    app.update(Msg::Job(Job {
        admission: None,
        summary: done,
        steps: Vec::new(),
        record: None,
    }));
    assert!(
        peaks(&app).0.contains("largest peak 7G"),
        "{:?}",
        peaks(&app)
    );
}

#[test]
fn a_warm_runner_reads_as_warm_not_as_a_runner_stuck_waiting_for_its_job() {
    let mut app = app();
    app.update(event(
        Some(1),
        Event::SlotChanged {
            profile: "jvm".to_owned(),
            slot: SlotStatus {
                slot: 1,
                runner: Some(RunnerStatus {
                    warm: true,
                    ..runner(false)
                }),
            },
        },
    ));
    let shown = screen(&app, 160, 40, false);
    let health = shown.lines().nth(1).unwrap();
    assert!(
        health.contains("0/3 running") && health.contains("1 warm"),
        "the warm runner runs nothing and is not starting: {health}"
    );
    assert!(!health.contains("starting"), "{health}");
    assert!(
        shown.contains("A warm runner for halyard/ledger, waiting for a job"),
        "its slot card says so: {shown}"
    );
    assert!(!shown.contains("waiting for its job"), "{shown}");
    let narrow = screen(&app, 80, 20, false);
    assert!(
        narrow.contains("warm runner for halyard/ledger, waiting for a job"),
        "its slot row says so: {narrow}"
    );
}

#[test]
fn a_step_shows_the_duration_it_has_and_none_it_lacks() {
    let mut app = app();
    running_job(&mut app);
    app.update(key(KeyCode::Enter));
    let summary = app.job.as_ref().unwrap().job.summary.clone();
    let mut cancelled = step(2, "Deploy", 1010, Some(1070));
    cancelled.conclusion = Some("cancelled".to_owned());
    app.update(Msg::Job(Job {
        admission: None,
        summary,
        steps: vec![
            Step {
                completed_at: None,
                status: "completed".to_owned(),
                ..step(1, "Lint", 1009, Some(1012))
            },
            cancelled,
        ],
        record: None,
    }));
    let job = screen(&app, 120, 40, false);
    let lint = rows_with(&job, "Lint")[0];
    assert!(
        lint.trim_end().ends_with('–') && !lint.contains("0s"),
        "a step that ended without a completion time has no duration: {job}"
    );
    let deploy = rows_with(&job, "Deploy")[0];
    assert!(
        deploy.trim_end().ends_with("1m 00s"),
        "a cancelled step keeps its duration: {job}"
    );
}

#[test]
fn a_sizing_row_of_a_job_without_a_name_says_a_job() {
    let mut app = app();
    app.update(key(KeyCode::Char('4')));
    let mut unnamed = sizing();
    for profile in &mut unnamed.profiles {
        profile.jobs[0].workflow = None;
        profile.jobs[0].name.clear();
    }
    app.update(Msg::Sizing(unnamed));
    let shown = screen(&app, 120, 24, false);
    assert!(
        rows_with(&shown, "halyard/storefront")
            .iter()
            .all(|row| row.contains("a job")),
        "as runnerctl sizing names it: {shown}"
    );
}