runnerd-web 0.0.7

The web dashboard runnerd serves on loopback: a client of the daemon's API
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
//! The dashboard against a fake daemon on a real Unix socket: who gets in, and that every page renders from API
//! data.

use std::path::PathBuf;
use std::sync::atomic::{AtomicU32, Ordering};

/// A fresh directory under the system temp dir, removed on drop.
pub(crate) struct TempDir(pub(crate) PathBuf);

impl TempDir {
    pub(crate) fn new() -> TempDir {
        static NEXT: AtomicU32 = AtomicU32::new(0);
        let n = NEXT.fetch_add(1, Ordering::Relaxed);
        let path =
            std::env::temp_dir().join(format!("runnerd-web-test-{}-{n}", std::process::id()));
        let _ = std::fs::remove_dir_all(&path);
        std::fs::create_dir_all(&path).unwrap();
        TempDir(path)
    }
}

impl Drop for TempDir {
    fn drop(&mut self) {
        let _ = std::fs::remove_dir_all(&self.0);
    }
}

#[path = "../examples/demo/fake.rs"]
mod fake;

use std::net::SocketAddr;
use std::time::Duration;

use axum::body::Bytes;
use axum::http::{Request, StatusCode, header};
use http_body_util::{BodyExt, Full};
use hyper_util::rt::TokioIo;
use runnerd_core::api::{
    Event, EventEnvelope, HostSample, JobState, Sample, SeedMemory, ShellMemory,
};

use crate::views::{repository_id, slot_id};

/// 2026-10-22T14:03:07Z.
const NOW: u64 = 1_792_677_787;
const LEDGER_RUNNING: u64 = 120_055_991_700;

/// The dashboard serving a fake daemon's fixtures, and a browser for it.
struct Dashboard {
    addr: SocketAddr,
    dir: TempDir,
    fake: fake::Fake,
}

struct Reply {
    status: StatusCode,
    headers: axum::http::HeaderMap,
    body: String,
}

impl Dashboard {
    /// Sets the caps the running job's record says its container ran with.
    fn runner_caps(&self, memory: Option<u64>, cpus: Option<u64>) {
        let mut fixtures = self.fake.fixtures.lock().unwrap();
        let job = fixtures
            .jobs
            .iter_mut()
            .find(|job| job.summary.id == LEDGER_RUNNING)
            .unwrap();
        let runner = job.summary.runner.as_mut().unwrap();
        runner.memory_cap = memory;
        runner.cpu_cap = cpus;
    }

    async fn start() -> Dashboard {
        let dir = TempDir::new();
        let socket = dir.0.join("runnerd.sock");
        let fake = fake::Fake::new(fake::Fixtures::new(NOW));
        fake::serve(&socket, fake.clone());
        let listener = crate::listen("127.0.0.1:0").await.unwrap().unwrap();
        let addr = listener.local_addr().unwrap();
        crate::spawn(listener, socket, std::future::pending::<()>());
        // The login file is written before the first request is served.
        for _ in 0..100 {
            if crate::login_file(&dir.0.join("runnerd.sock")).exists() {
                break;
            }
            tokio::time::sleep(Duration::from_millis(10)).await;
        }
        Dashboard { addr, dir, fake }
    }

    fn login_url(&self) -> String {
        std::fs::read_to_string(crate::login_file(&self.dir.0.join("runnerd.sock")))
            .unwrap()
            .trim()
            .to_owned()
    }

    /// Signs in with the one-time URL and returns the session cookie.
    async fn sign_in(&self) -> String {
        let url = self.login_url();
        let path = url.strip_prefix(&format!("http://{}", self.addr)).unwrap();
        let reply = self.request("GET", path, &[]).await;
        assert_eq!(reply.status, StatusCode::SEE_OTHER);
        let cookie = reply.headers[header::SET_COOKIE].to_str().unwrap();
        assert!(cookie.contains("HttpOnly") && cookie.contains("SameSite=Strict"));
        cookie.split(';').next().unwrap().to_owned()
    }

    async fn connect(&self) -> hyper::client::conn::http1::SendRequest<Full<Bytes>> {
        let stream = tokio::net::TcpStream::connect(self.addr).await.unwrap();
        let (sender, connection) = hyper::client::conn::http1::handshake(TokioIo::new(stream))
            .await
            .unwrap();
        tokio::spawn(connection);
        sender
    }

    fn build(&self, method: &str, path: &str, headers: &[(&str, &str)]) -> Request<Full<Bytes>> {
        self.build_with(method, path, headers, Bytes::new())
    }

    fn build_with(
        &self,
        method: &str,
        path: &str,
        headers: &[(&str, &str)],
        body: Bytes,
    ) -> Request<Full<Bytes>> {
        let mut request = Request::builder()
            .method(method)
            .uri(path)
            .header(header::HOST, self.addr.to_string());
        for (name, value) in headers {
            request = request.header(*name, *value);
        }
        request.body(Full::new(body)).unwrap()
    }

    async fn request(&self, method: &str, path: &str, headers: &[(&str, &str)]) -> Reply {
        self.send(self.build(method, path, headers)).await
    }

    /// A write from the dashboard's own page, as Datastar sends it: the cookie, the origin, the signals as JSON.
    async fn write(
        &self,
        cookie: &str,
        method: &str,
        path: &str,
        signals: &serde_json::Value,
    ) -> Reply {
        let origin = format!("http://{}", self.addr);
        let headers = [
            ("cookie", cookie),
            ("origin", origin.as_str()),
            ("content-type", "application/json"),
            ("datastar-request", "true"),
        ];
        let body = Bytes::from(signals.to_string());
        self.send(self.build_with(method, path, &headers, body))
            .await
    }

    async fn send(&self, request: Request<Full<Bytes>>) -> Reply {
        let mut sender = self.connect().await;
        let response = sender.send_request(request).await.unwrap();
        let status = response.status();
        let headers = response.headers().clone();
        let body = response.into_body().collect().await.unwrap().to_bytes();
        Reply {
            status,
            headers,
            body: String::from_utf8(body.to_vec()).unwrap(),
        }
    }

    async fn page(&self, cookie: &str, path: &str) -> Reply {
        self.request("GET", path, &[("cookie", cookie)]).await
    }

    /// Opens a live stream and returns a reader of its body.
    async fn stream(&self, cookie: &str, path: &str) -> hyper::body::Incoming {
        let mut sender = self.connect().await;
        let response = sender
            .send_request(self.build("GET", path, &[("cookie", cookie)]))
            .await
            .unwrap();
        assert_eq!(response.status(), StatusCode::OK);
        // The connection lives as long as the stream is read.
        std::mem::forget(sender);
        response.into_body()
    }
}

/// Reads the stream until `found` holds for what arrived, or fails after 5 s.
async fn read_until(body: &mut hyper::body::Incoming, found: impl Fn(&str) -> bool) -> String {
    let mut text = String::new();
    tokio::time::timeout(Duration::from_secs(5), async {
        while !found(&text) {
            let frame = body.frame().await.unwrap().unwrap();
            if let Ok(data) = frame.into_data() {
                text.push_str(&String::from_utf8_lossy(&data));
            }
        }
    })
    .await
    .unwrap_or_else(|_| panic!("the stream never sent what was expected; it sent:\n{text}"));
    text
}

#[tokio::test]
async fn without_the_session_cookie_every_page_and_write_is_401() {
    let dashboard = Dashboard::start().await;
    for (method, path) in [
        ("GET", "/"),
        ("GET", "/jobs/1"),
        ("GET", "/sizing"),
        ("GET", "/live/overview"),
        ("POST", "/v1/repositories/x/pause"),
        ("POST", "/v1/repositories/halyard/ledger/pause"),
    ] {
        let reply = dashboard.request(method, path, &[]).await;
        assert_eq!(reply.status, StatusCode::UNAUTHORIZED, "{method} {path}");
    }
    let wrong = dashboard
        .request(
            "POST",
            "/v1/repositories/halyard/ledger/pause",
            &[("cookie", "runnerd-1=0")],
        )
        .await;
    assert_eq!(wrong.status, StatusCode::UNAUTHORIZED);
    assert!(!dashboard.fake.fixtures.lock().unwrap().status.repositories[0].paused);
    // The assets need no session: the sign-in page uses them.
    let style = dashboard.request("GET", "/assets/style.css", &[]).await;
    assert_eq!(style.status, StatusCode::OK);
}

#[tokio::test]
async fn an_asset_is_kept_until_it_changes_and_revalidates_without_its_body() {
    let dashboard = Dashboard::start().await;
    let cookie = dashboard.sign_in().await;
    let page = dashboard.page(&cookie, "/").await.body;
    let from = page.find("/assets/style.css?v=").unwrap();
    let url = &page[from..from + page[from..].find('"').unwrap()];
    let style = dashboard.request("GET", url, &[]).await;
    assert_eq!(
        style.headers[header::CACHE_CONTROL],
        "public, max-age=31536000, immutable"
    );
    let etag = style.headers[header::ETAG].to_str().unwrap().to_owned();
    let again = dashboard
        .request("GET", url, &[("if-none-match", &etag)])
        .await;
    assert_eq!(again.status, StatusCode::NOT_MODIFIED);
    assert!(again.body.is_empty(), "a 304 has no body");
    // An older release's URL may name other bytes: checked each time.
    let old = dashboard
        .request("GET", "/assets/style.css?v=0.0.5", &[])
        .await;
    assert_eq!(old.headers[header::CACHE_CONTROL], "no-cache");
}

#[tokio::test]
async fn a_write_from_another_origin_is_403_even_with_the_cookie() {
    let dashboard = Dashboard::start().await;
    let cookie = dashboard.sign_in().await;
    for origin in [
        Some("http://evil.example"),
        Some("http://127.0.0.1:1"),
        None,
    ] {
        let mut headers = vec![("cookie", cookie.as_str())];
        if let Some(origin) = origin {
            headers.push(("origin", origin));
        }
        let reply = dashboard
            .request("POST", "/v1/repositories/halyard/ledger/pause", &headers)
            .await;
        assert_eq!(reply.status, StatusCode::FORBIDDEN, "origin {origin:?}");
    }
    assert!(!dashboard.fake.fixtures.lock().unwrap().status.repositories[0].paused);
}

#[tokio::test]
async fn a_resume_from_the_dashboard_reaches_the_daemon_and_returns_the_row_with_pause() {
    let dashboard = Dashboard::start().await;
    let cookie = dashboard.sign_in().await;
    let origin = format!("http://{}", dashboard.addr);
    dashboard.fake.fixtures.lock().unwrap().status.repositories[0].paused = true;
    let reply = dashboard
        .request(
            "POST",
            "/v1/repositories/halyard/ledger/resume",
            &[("cookie", &cookie), ("origin", &origin)],
        )
        .await;
    assert_eq!(reply.status, StatusCode::OK);
    assert!(!dashboard.fake.fixtures.lock().unwrap().status.repositories[0].paused);
    assert!(
        reply
            .body
            .contains(&format!(r#"id="{}""#, repository_id("halyard/ledger"))),
        "{}",
        reply.body
    );
    assert!(
        reply.body.contains(">Pause<") && !reply.body.contains("badge neutral"),
        "{}",
        reply.body
    );
    // A repository the daemon does not know comes back as a notice Datastar shows.
    let unknown = dashboard
        .request(
            "POST",
            "/v1/repositories/halyard/nope/pause",
            &[("cookie", &cookie), ("origin", &origin)],
        )
        .await;
    assert!(
        unknown.body.contains(r#"id="notice""#)
            && unknown.body.contains("not a configured repository")
    );
}

#[tokio::test]
async fn repositories_whose_names_differ_only_in_punctuation_keep_their_own_rows() {
    let dashboard = Dashboard::start().await;
    {
        let mut fixtures = dashboard.fake.fixtures.lock().unwrap();
        let mut dash = fixtures.status.repositories[0].clone();
        dash.name = "halyard/b-c".to_owned();
        dash.paused = true;
        let mut underscore = dash.clone();
        underscore.name = "halyard/b_c".to_owned();
        fixtures.status.repositories = vec![dash, underscore];
    }
    let cookie = dashboard.sign_in().await;
    let overview = dashboard.page(&cookie, "/").await.body;
    let (dash, underscore) = (repository_id("halyard/b-c"), repository_id("halyard/b_c"));
    for id in [&dash, &underscore] {
        assert_eq!(
            overview.matches(&format!(r#"id="{id}""#)).count(),
            1,
            "one row has the id {id}"
        );
        let busy = format!(r#"data-indicator="_busy_{}""#, id.replace('-', "_"));
        assert_eq!(
            overview.matches(&busy).count(),
            1,
            "{id}'s pause button has its own busy signal"
        );
    }
    // The resume's reply replaces the row with its id: it must be that repository's.
    let origin = format!("http://{}", dashboard.addr);
    let reply = dashboard
        .request(
            "POST",
            "/v1/repositories/halyard/b_c/resume",
            &[("cookie", &cookie), ("origin", &origin)],
        )
        .await;
    assert!(
        reply.body.contains(&format!(r#"id="{underscore}""#))
            && !reply.body.contains(&format!(r#"id="{dash}""#)),
        "{}",
        reply.body
    );
}

#[tokio::test]
async fn a_login_link_signs_in_once() {
    let dashboard = Dashboard::start().await;
    let used = dashboard.login_url();
    dashboard.sign_in().await;
    let path = used
        .strip_prefix(&format!("http://{}", dashboard.addr))
        .unwrap();
    let again = dashboard.request("GET", path, &[]).await;
    assert_eq!(again.status, StatusCode::UNAUTHORIZED);
    assert_ne!(dashboard.login_url(), used);
}

#[tokio::test]
async fn every_page_renders_from_the_daemons_data() {
    let dashboard = Dashboard::start().await;
    let cookie = dashboard.sign_in().await;
    let overview = dashboard.page(&cookie, "/").await;
    assert_eq!(overview.status, StatusCode::OK);
    for expected in [
        "runnerd.slice",
        "halyard/ledger",
        "halyard/storefront",
        "Integration tests (Testcontainers)",
        "MemoryMax 33G",
        "peak",
    ] {
        assert!(
            overview.body.contains(expected),
            "overview lacks {expected:?}"
        );
    }
    let csp = overview.headers[header::CONTENT_SECURITY_POLICY]
        .to_str()
        .unwrap();
    assert!(csp.contains("default-src 'none'") && csp.contains("connect-src 'self'"));

    let job = dashboard
        .page(&cookie, &format!("/jobs/{LEDGER_RUNNING}"))
        .await;
    assert_eq!(job.status, StatusCode::OK);
    for expected in [
        "Set up JDK 21",
        "sampled peak",
        "Waiting for a runner",
        "Set up job: line 1",
        "/live/jobs/",
        // Named as every client names a job, so the tab says which workflow's build it is.
        "<title>CI / build · runnerd</title>",
    ] {
        assert!(job.body.contains(expected), "job page lacks {expected:?}");
    }

    let history = dashboard.page(&cookie, "/history").await;
    assert_eq!(history.status, StatusCode::OK);
    assert!(history.body.contains("Jobs per day") && history.body.contains("playwright"));

    let caches = dashboard.page(&cookie, "/caches").await;
    assert_eq!(caches.status, StatusCode::OK);
    assert!(caches.body.contains("runnerd-pnpm") && caches.body.contains("emptied above 5G"));

    let log = dashboard
        .page(&cookie, &format!("/jobs/{LEDGER_RUNNING}/log"))
        .await;
    assert!(log.body.starts_with("##[group]Set up job\n"));

    let missing = dashboard.page(&cookie, "/jobs/7").await;
    assert_eq!(missing.status, StatusCode::NOT_FOUND);
}

#[tokio::test]
async fn queued_jobs_lead_the_overviews_jobs_oldest_first_once_each_linked_to_github_and_their_repository()
 {
    let dashboard = Dashboard::start().await;
    {
        let mut fixtures = dashboard.fake.fixtures.lock().unwrap();
        let mut older = fixtures.status.queued[0].clone();
        older.id -= 5;
        older.name = "lint".to_owned();
        older.url = Some("https://github.com/halyard/storefront/actions/runs/1/job/2".to_owned());
        older.queued_at -= 300;
        fixtures.status.queued.insert(0, older);
        // The history keeps the newer one queued too.
        let mut recorded = fixtures.jobs[0].clone();
        recorded.summary.id = fixtures.status.queued[1].id;
        recorded.summary.state = JobState::Queued;
        fixtures.jobs.insert(0, recorded);
    }
    let cookie = dashboard.sign_in().await;
    let overview = dashboard.page(&cookie, "/").await;
    let jobs = &overview.body[overview.body.find(r#"id="recent""#).unwrap()..];
    let lint = jobs.find("https://github.com/halyard/storefront/actions/runs/1/job/2");
    let visual_diff = jobs.find("/job/120055991710");
    assert!(
        lint.is_some() && lint < visual_diff,
        "the oldest queued job comes first: {jobs}"
    );
    assert_eq!(
        jobs.matches("120055991710").count(),
        1,
        "a queued job the history also has shows once: {jobs}"
    );
    assert!(
        jobs.contains(r#"<li class="queued" data-class:linked="$_repo == 'halyard/storefront'""#),
        "a queued row lights up with its repository: {jobs}"
    );
}

#[tokio::test]
async fn a_slot_card_counts_its_running_step_by_place_as_runnerctl_watch_does() {
    let dashboard = Dashboard::start().await;
    let (place, steps, name) = {
        let fixtures = dashboard.fake.fixtures.lock().unwrap();
        let job = fixtures
            .jobs
            .iter()
            .find(|job| job.summary.id == LEDGER_RUNNING)
            .unwrap();
        let place = job
            .steps
            .iter()
            .position(|step| step.status == "in_progress")
            .unwrap();
        (place + 1, job.steps.len(), job.steps[place].name.clone())
    };
    let cookie = dashboard.sign_in().await;
    let overview = dashboard.page(&cookie, "/").await.body;
    let expected = format!(r#"<span class="step-count">{place}/{steps}</span> {name}"#);
    assert!(
        overview.contains(&expected),
        "the card reads {expected}: {overview}"
    );
}

#[tokio::test]
async fn the_budget_names_the_mirror_and_each_seed_container() {
    let dashboard = Dashboard::start().await;
    {
        let mut fixtures = dashboard.fake.fixtures.lock().unwrap();
        let budget = &mut fixtures.status.budget;
        budget.seed_memory = vec![SeedMemory {
            profile: "jvm".to_owned(),
            memory: 9 << 30,
        }];
        budget.memory_in_use += 9 << 30;
    }
    let cookie = dashboard.sign_in().await;
    let overview = dashboard.page(&cookie, "/").await;
    for expected in [
        "<strong>512M</strong> held by the mirror",
        "<strong>9G</strong> held by jvm's seed container",
        r#"<span class="name">jvm seed</span>"#,
    ] {
        assert!(
            overview.body.contains(expected),
            "{expected}: {}",
            overview.body
        );
    }
}

#[tokio::test]
async fn the_budget_names_a_shell_open_in_a_record() {
    let dashboard = Dashboard::start().await;
    {
        let mut fixtures = dashboard.fake.fixtures.lock().unwrap();
        let budget = &mut fixtures.status.budget;
        budget.shell_memory = vec![ShellMemory {
            job: 42,
            memory: 9 << 30,
        }];
        budget.memory_in_use += 9 << 30;
    }
    let cookie = dashboard.sign_in().await;
    let overview = dashboard.page(&cookie, "/").await;
    assert!(
        overview
            .body
            .contains("<strong>9G</strong> held by the shell in job 42's record"),
        "the shell's memory is not shown as free: {}",
        overview.body
    );
}

/// The HTML from the start of the element whose `data-on:pointerenter` selects `key` to that attribute.
fn selector_of<'page>(page: &'page str, key: &str, tag: &str) -> &'page str {
    let at = page
        .find(&format!("data-on:pointerenter=\"$_slot = '{key}'\""))
        .unwrap_or_else(|| panic!("nothing selects {key}"));
    let start = page[..at].rfind(tag).unwrap();
    &page[start..at]
}

#[tokio::test]
async fn a_budget_block_opens_its_job_or_its_slots_card_and_lights_up_the_card() {
    let dashboard = Dashboard::start().await;
    let cookie = dashboard.sign_in().await;
    let overview = dashboard.page(&cookie, "/").await.body;
    let jvm_1 = slot_id("jvm", 1);
    let jvm_2 = slot_id("jvm", 2);
    assert!(
        selector_of(&overview, &jvm_1, "<a ")
            .contains(&format!(r#"href="/jobs/{LEDGER_RUNNING}""#)),
        "jvm 1's block opens the ledger build it runs"
    );
    assert!(
        selector_of(&overview, &jvm_2, "<a ").contains(&format!(r##"href="#{jvm_2}""##)),
        "the idle jvm 2's block points at its card"
    );
    for (profile, slot) in [("jvm", 1), ("jvm", 2), ("node", 1), ("node", 2)] {
        let card = slot_id(profile, slot);
        assert!(
            overview.contains(&format!(r#"<li id="{card}""#))
                && overview.contains(&format!("data-class:linked=\"$_slot == '{card}'")),
            "{profile} {slot}'s card exists and lights up with its block"
        );
    }
    // Every block's line is in the readout, toggled by a class: hiding one with `display: none` would let the
    // readout shrink and grow, and move the bar under the pointer.
    let lens = &overview[overview.find(r#"<p class="lens""#).unwrap()..];
    let lens = &lens[..lens.find("</p>").unwrap()];
    for (profile, slot) in [("jvm", 1), ("jvm", 2), ("node", 1), ("node", 2)] {
        assert!(
            lens.contains(&format!(
                "data-class:on=\"$_slot == '{}'\"",
                slot_id(profile, slot)
            )),
            "the readout has a line for {profile} {slot}"
        );
    }
    assert!(
        !lens.contains("display: none") && !lens.contains("data-show"),
        "the readout's lines keep their place while hidden"
    );
    // A block is named by what it shows and described by its line of the readout.
    for (key, line) in [
        (&jvm_1, "of 9G in jvm 1, build on halyard/ledger"),
        (&jvm_2, "9G jvm 2, idle"),
    ] {
        assert!(
            selector_of(&overview, key, "<a ")
                .contains(&format!(r#"aria-describedby="lens-{key}""#)),
            "{key}'s block is described by its line"
        );
        let described = &overview[overview.find(&format!(r#"<span id="lens-{key}""#)).unwrap()..];
        let text = described[..described.find("</span>").unwrap()]
            .replace("<strong>", "")
            .replace("</strong>", "");
        assert!(text.ends_with(line), "{key}'s line reads {line:?}");
    }
    assert!(
        overview.contains(r#"of 33G free""#),
        "the free remainder is named for screen readers"
    );
}

#[tokio::test]
async fn profiles_whose_names_differ_only_in_punctuation_keep_their_own_slots() {
    let dashboard = Dashboard::start().await;
    {
        let mut fixtures = dashboard.fake.fixtures.lock().unwrap();
        fixtures.status.profiles[0].name = "node-js".to_owned();
        fixtures.status.profiles[1].name = "node_js".to_owned();
    }
    let cookie = dashboard.sign_in().await;
    let overview = dashboard.page(&cookie, "/").await.body;
    let dash = slot_id("node-js", 1);
    let underscore = slot_id("node_js", 1);
    assert_ne!(dash, underscore);
    for id in [&dash, &underscore] {
        assert_eq!(
            overview.matches(&format!(r#"id="{id}""#)).count(),
            1,
            "one card has the id {id}"
        );
        assert!(
            selector_of(&overview, id, "<a ").contains(&format!(r##"href="#{id}""##)),
            "the block for {id} points at its own card"
        );
    }
}

#[tokio::test]
async fn the_selected_blocks_readout_follows_its_jobs_new_sample() {
    let dashboard = Dashboard::start().await;
    let cookie = dashboard.sign_in().await;
    let mut stream = dashboard.stream(&cookie, "/live/overview").await;
    read_until(&mut stream, |text| text.contains(r#"id="recent""#)).await;
    let at = NOW + 2;
    dashboard
        .fake
        .events
        .send(EventEnvelope {
            id: None,
            at,
            event: Event::JobSample {
                job: LEDGER_RUNNING,
                sample: Sample {
                    at,
                    cpu: 2000,
                    memory: 5 << 30,
                    network_received: 0,
                    network_sent: 0,
                },
            },
        })
        .unwrap();
    // The page keeps only which block is selected; its line comes in the patch, rendered from the new sample.
    let line = format!(
        "data-class:on=\"$_slot == '{}'\"><strong>5G</strong> of 9G in jvm 1",
        slot_id("jvm", 1)
    );
    read_until(&mut stream, |text| text.contains(&line)).await;
}

#[tokio::test]
async fn a_job_whose_samples_were_deleted_still_shows_its_memory_peak() {
    let dashboard = Dashboard::start().await;
    let peak = {
        let mut fixtures = dashboard.fake.fixtures.lock().unwrap();
        fixtures.samples.insert(LEDGER_RUNNING, Vec::new());
        let job = fixtures
            .jobs
            .iter()
            .find(|job| job.summary.id == LEDGER_RUNNING)
            .unwrap();
        job.summary.metrics.memory_peak.unwrap()
    };
    let cookie = dashboard.sign_in().await;
    let job = dashboard
        .page(&cookie, &format!("/jobs/{LEDGER_RUNNING}"))
        .await
        .body;
    assert!(
        job.contains(&format!(
            "<dt>Memory peak over the job</dt><dd>{}</dd>",
            crate::format::bytes(peak)
        )),
        "the job's kept peak shows without samples"
    );
}

#[tokio::test]
async fn a_step_marks_its_own_stretch_of_the_memory_and_cpu_charts() {
    let dashboard = Dashboard::start().await;
    // The first step loses its samples, so it gets no band and the bands are not one per step.
    let (start, from, to) = {
        let mut fixtures = dashboard.fake.fixtures.lock().unwrap();
        let job = fixtures
            .jobs
            .iter()
            .find(|job| job.summary.id == LEDGER_RUNNING)
            .unwrap()
            .clone();
        let first = &job.steps[0];
        let (gone_from, gone_to) = (first.started_at.unwrap(), first.completed_at.unwrap());
        let samples = fixtures.samples.get_mut(&LEDGER_RUNNING).unwrap();
        samples.retain(|sample| !(gone_from..=gone_to).contains(&sample.at));
        let unit_tests = job
            .steps
            .iter()
            .find(|step| step.name == "Unit tests")
            .unwrap();
        (
            samples.first().unwrap().at,
            unit_tests.started_at.unwrap(),
            unit_tests.completed_at.unwrap(),
        )
    };
    let cookie = dashboard.sign_in().await;
    let job = dashboard
        .page(&cookie, &format!("/jobs/{LEDGER_RUNNING}"))
        .await
        .body;
    assert!(
        !job[..job.find(r#"<span class="step-name">Set up job"#).unwrap()]
            .ends_with(r#"data-class:linked="$_step == 1">"#),
        "the step without samples is not selectable"
    );
    let name = job.find(r#"<span class="step-name">Unit tests"#).unwrap();
    let focus = r#"data-on:focus="$_step = "#;
    let enter = job[..name].rfind(focus).unwrap() + focus.len();
    let number: usize = job[enter..]
        .split(|c: char| !c.is_ascii_digit())
        .next()
        .unwrap()
        .parse()
        .unwrap();
    assert_eq!(number, 6, "Unit tests is the sixth step");
    // In seconds since the chart's start, its first sample.
    let stretch = format!(
        r#"data-class:on="$_step == {number}" style="--from: {}; --to: {}""#,
        from - start,
        to - start
    );
    assert_eq!(
        job.matches(&stretch).count(),
        2,
        "the step's band on the memory and on the CPU chart spans its own start and end"
    );
}

#[tokio::test]
async fn a_job_chart_shows_the_caps_its_container_ran_with_not_the_profiles_now() {
    let dashboard = Dashboard::start().await;
    let cookie = dashboard.sign_in().await;
    // jvm is configured with 9G and 8 CPUs now; the job's container ran with 12G and 6.
    dashboard.runner_caps(Some(12 << 30), Some(6000));
    let job = dashboard
        .page(&cookie, &format!("/jobs/{LEDGER_RUNNING}"))
        .await;
    for expected in ["slot cap 12G", "6 CPUs"] {
        assert!(job.body.contains(expected), "job page lacks {expected:?}");
    }
    assert!(
        !job.body.contains("jvm now"),
        "the job page shows today's config next to the job's own caps"
    );
}

#[tokio::test]
async fn a_job_recorded_without_its_caps_is_charted_against_the_profiles_limits_now() {
    let dashboard = Dashboard::start().await;
    let cookie = dashboard.sign_in().await;
    dashboard.runner_caps(None, None);
    let job = dashboard
        .page(&cookie, &format!("/jobs/{LEDGER_RUNNING}"))
        .await;
    for expected in ["jvm now 9G", "jvm now 8 CPUs", "limits in the config now"] {
        assert!(job.body.contains(expected), "job page lacks {expected:?}");
    }
    assert!(
        !job.body.contains("slot cap"),
        "the job page presents today's config as the job's caps"
    );
}

#[tokio::test]
async fn history_shows_only_the_repository_and_outcome_filtered_for() {
    let dashboard = Dashboard::start().await;
    let cookie = dashboard.sign_in().await;
    let failed = dashboard
        .page(
            &cookie,
            "/history?repository=halyard%2Fledger&outcome=failure",
        )
        .await;
    let rows: Vec<&str> = failed.body.split("<tr class=").skip(1).collect();
    assert_ne!(rows.len(), 0);
    for row in &rows {
        assert!(
            row.contains("halyard/ledger") && row.contains("badge failure"),
            "{row}"
        );
    }
    let everything = dashboard
        .page(&cookie, "/history?repository=&outcome=")
        .await;
    assert!(everything.body.split("<tr class=").count() > rows.len() + 1);
}

#[tokio::test]
async fn the_overview_stream_patches_the_slice_when_a_sample_arrives() {
    let dashboard = Dashboard::start().await;
    let cookie = dashboard.sign_in().await;
    let mut stream = dashboard.stream(&cookie, "/live/overview").await;
    let first = read_until(&mut stream, |text| text.contains(r#"id="recent""#)).await;
    assert!(first.contains("event: datastar-patch-elements") && first.contains(r#"id="budget""#));
    let at = NOW + 2;
    let sample = HostSample {
        at,
        cpu: 31_000,
        memory: 21 << 30,
    };
    dashboard
        .fake
        .fixtures
        .lock()
        .unwrap()
        .host
        .push(sample.clone());
    dashboard
        .fake
        .events
        .send(EventEnvelope {
            id: None,
            at,
            event: Event::HostSample { sample },
        })
        .unwrap();
    let update = read_until(&mut stream, |text| text.contains(r#"id="host-cpu-now""#)).await;
    assert!(update.contains("31 cores"), "{update}");
}

/// The signals a page starts with, as the JSON Datastar sends them.
fn page_signals(page: &str) -> serde_json::Value {
    let from = page.find(r#"<body data-signals=""#).unwrap() + r#"<body data-signals=""#.len();
    let signals = &page[from..from + page[from..].find('"').unwrap()];
    // The page writes a JavaScript object.
    let json = signals
        .replace("{shown: {at:", r#"{"shown": {"at":"#)
        .replace(", log:", r#", "log":"#)
        .replace('\'', "\"");
    serde_json::from_str(&json).unwrap()
}

/// The `datastar` query parameter of a stream request carrying `signals`.
fn query(signals: &serde_json::Value) -> String {
    serde_urlencoded::to_string([("datastar", signals.to_string())]).unwrap()
}

/// The `datastar` query parameter a page's stream request carries.
fn signals_of(page: &str) -> String {
    query(&page_signals(page))
}

#[tokio::test]
async fn a_stream_opened_by_the_page_it_just_rendered_sends_none_of_its_regions_again() {
    let dashboard = Dashboard::start().await;
    let cookie = dashboard.sign_in().await;
    let page = dashboard.page(&cookie, "/").await.body;
    let mut stream = dashboard
        .stream(&cookie, &format!("/live/overview?{}", signals_of(&page)))
        .await;
    let first = read_until(&mut stream, |text| text.contains("datastar-patch-signals")).await;
    assert!(
        !first.contains("<section"),
        "the page has every region already: {first}"
    );
    // A page that names no regions gets them all.
    let mut fresh = dashboard.stream(&cookie, "/live/overview").await;
    let first = read_until(&mut fresh, |text| text.contains("datastar-patch-signals")).await;
    assert_eq!(first.matches("<section").count(), 5, "{first}");
}

#[tokio::test]
async fn the_overview_shows_a_repository_resumed_from_another_client() {
    let dashboard = Dashboard::start().await;
    dashboard.fake.fixtures.lock().unwrap().status.repositories[0].paused = true;
    let cookie = dashboard.sign_in().await;
    let mut stream = dashboard.stream(&cookie, "/live/overview").await;
    let first = read_until(&mut stream, |text| text.contains(r#"id="recent""#)).await;
    assert!(
        first.contains("badge neutral"),
        "halyard/ledger shows paused: {first}"
    );
    let origin = format!("http://{}", dashboard.addr);
    let reply = dashboard
        .request(
            "POST",
            "/v1/repositories/halyard/ledger/resume",
            &[("cookie", &cookie), ("origin", &origin)],
        )
        .await;
    assert_eq!(reply.status, StatusCode::OK, "{}", reply.body);
    let update = read_until(&mut stream, |text| text.contains(r#"id="repositories""#)).await;
    assert!(
        update.contains(&format!(r#"id="{}""#, repository_id("halyard/ledger")))
            && !update.contains("badge neutral"),
        "{update}"
    );
}

#[tokio::test]
async fn a_running_jobs_stream_appends_new_log_lines_and_updates_its_charts() {
    let dashboard = Dashboard::start().await;
    let cookie = dashboard.sign_in().await;
    let lines = dashboard.fake.fixtures.lock().unwrap().logs[&LEDGER_RUNNING].len() as u64;
    let mut stream = dashboard
        .stream(&cookie, &format!("/live/jobs/{LEDGER_RUNNING}"))
        .await;
    read_until(&mut stream, |text| text.contains(r#"id="metrics""#)).await;
    let at = NOW + 2;
    for event in [
        Event::LogLine {
            job: LEDGER_RUNNING,
            line: lines,
            text: "BUILD SUCCESSFUL in 2m 28s".to_owned(),
        },
        Event::JobSample {
            job: LEDGER_RUNNING,
            sample: Sample {
                at,
                cpu: 7000,
                memory: 8 << 30,
                network_received: 0,
                network_sent: 0,
            },
        },
    ] {
        dashboard
            .fake
            .events
            .send(EventEnvelope {
                id: None,
                at,
                event,
            })
            .unwrap();
    }
    let update = read_until(&mut stream, |text| {
        text.contains("BUILD SUCCESSFUL") && text.contains(r#"id="metrics""#)
    })
    .await;
    assert!(
        update.contains("selector #log-lines") && update.contains("mode append"),
        "{update}"
    );
    assert!(update.contains("sampled peak 8G"), "{update}");
}

#[tokio::test]
async fn a_job_stream_the_daemon_refuses_shows_why_and_ends_without_retrying() {
    let dashboard = Dashboard::start().await;
    let cookie = dashboard.sign_in().await;
    let mut stream = dashboard.stream(&cookie, "/live/jobs/999").await;
    let mut text = String::new();
    let end = tokio::time::timeout(Duration::from_secs(5), async {
        loop {
            match stream.frame().await {
                Some(Ok(frame)) => {
                    text.push_str(&String::from_utf8_lossy(
                        &frame.into_data().unwrap_or_default(),
                    ));
                }
                Some(Err(_)) => return "failed",
                None => return "ended",
            }
        }
    })
    .await
    .unwrap();
    assert!(text.contains("toast failed"), "{text}");
    // A retry would fail the same way and replace the notice.
    assert_eq!(end, "ended", "{text}");
}

#[tokio::test]
async fn a_job_stream_that_misses_log_lines_fails_so_the_page_connects_again() {
    let dashboard = Dashboard::start().await;
    let cookie = dashboard.sign_in().await;
    let lines = dashboard.fake.fixtures.lock().unwrap().logs[&LEDGER_RUNNING].len() as u64;
    let mut stream = dashboard
        .stream(&cookie, &format!("/live/jobs/{LEDGER_RUNNING}"))
        .await;
    read_until(&mut stream, |text| text.contains(r#"id="metrics""#)).await;
    dashboard
        .fake
        .events
        .send(EventEnvelope {
            id: None,
            at: NOW + 2,
            event: Event::LogLine {
                job: LEDGER_RUNNING,
                line: lines + 1,
                text: "BUILD SUCCESSFUL in 2m 28s".to_owned(),
            },
        })
        .unwrap();
    // Datastar retries a stream that fails, not one that ends.
    let end = tokio::time::timeout(Duration::from_secs(5), async {
        loop {
            match stream.frame().await {
                Some(Ok(frame)) => {
                    let data = frame.into_data().unwrap_or_default();
                    assert!(
                        !String::from_utf8_lossy(&data).contains("BUILD SUCCESSFUL"),
                        "a line after a gap is not appended"
                    );
                }
                Some(Err(_)) => return "failed",
                None => return "ended",
            }
        }
    })
    .await
    .unwrap();
    assert_eq!(end, "failed");
}

/// The log lines a browser shows, as `(number, text)`: what the job page rendered, then each of the job stream's
/// patches to `#log-lines` applied the way Datastar applies them.
struct ShownLog(Vec<(Option<u64>, String)>);

impl ShownLog {
    /// The `<li value="N">text</li>` items in `html`; the number is `None` on an `<li>` without one.
    fn items(html: &str) -> Vec<(Option<u64>, String)> {
        html.split("<li")
            .skip(1)
            .map(|item| {
                let (attributes, rest) = item.split_once('>').unwrap();
                let number = attributes
                    .split_once("value=\"")
                    .map(|(_, value)| value.split('"').next().unwrap().parse().unwrap());
                (number, rest.split_once("</li>").unwrap().0.to_owned())
            })
            .collect()
    }

    fn of_page(page: &str) -> ShownLog {
        let list = page.split_once(r#"id="log-lines""#).unwrap().1;
        ShownLog(Self::items(list.split_once("</ol>").unwrap().0))
    }

    /// Applies the patches to `#log-lines` among the stream's `events`.
    fn apply(&mut self, events: &str) {
        for event in events.split("\n\n") {
            let data: Vec<&str> = event
                .lines()
                .filter_map(|line| line.strip_prefix("data: "))
                .collect();
            let field = |name: &str| {
                data.iter()
                    .find_map(|line| line.strip_prefix(name).map(str::trim))
            };
            let elements: String = data
                .iter()
                .filter_map(|line| line.strip_prefix("elements "))
                .collect();
            match (field("selector "), field("mode ")) {
                (Some("#log-lines"), Some("append")) => self.0.extend(Self::items(&elements)),
                (Some(selector), Some("remove")) => {
                    // The only removal the stream sends: all but the last `keep` lines.
                    let keep: usize = selector
                        .strip_prefix("#log-lines > li:nth-last-child(n+")
                        .and_then(|rest| rest.strip_suffix(')'))
                        .unwrap()
                        .parse::<usize>()
                        .unwrap()
                        - 1;
                    let excess = self.0.len().saturating_sub(keep);
                    self.0.drain(..excess);
                }
                _ if elements.contains(r#"id="log-lines""#) => {
                    self.0 = ShownLog::of_page(&elements).0;
                }
                _ => {}
            }
        }
    }
}

impl Dashboard {
    /// Adds a line to the running job's log, as the daemon stores and announces it.
    fn log(&self, text: &str) {
        let mut fixtures = self.fake.fixtures.lock().unwrap();
        let lines = fixtures.logs.entry(LEDGER_RUNNING).or_default();
        let line = lines.len() as u64;
        lines.push(text.to_owned());
        let job = fixtures
            .jobs
            .iter_mut()
            .find(|job| job.summary.id == LEDGER_RUNNING)
            .unwrap();
        job.summary.log.lines = line + 1;
        let _ = self.fake.events.send(EventEnvelope {
            id: None,
            at: NOW,
            event: Event::LogLine {
                job: LEDGER_RUNNING,
                line,
                text: text.to_owned(),
            },
        });
    }

    /// The job page and the URL its live stream connects to, as the page names it.
    async fn job_page(&self, cookie: &str) -> (String, String) {
        let page = self
            .page(cookie, &format!("/jobs/{LEDGER_RUNNING}"))
            .await
            .body;
        let url = page
            .split_once("@get(&#39;")
            .or_else(|| page.split_once("@get('"))
            .unwrap()
            .1
            .split(['&', '\''])
            .next()
            .unwrap()
            .to_owned();
        (page, url)
    }
}

#[tokio::test]
async fn a_job_left_open_on_a_noisy_log_keeps_only_its_last_lines() {
    let dashboard = Dashboard::start().await;
    let cookie = dashboard.sign_in().await;
    let (page, url) = dashboard.job_page(&cookie).await;
    let mut shown = ShownLog::of_page(&page);
    let mut stream = dashboard.stream(&cookie, &url).await;
    let mut events = read_until(&mut stream, |text| text.contains(r#"id="metrics""#)).await;
    let tail = usize::try_from(crate::views::job::LOG_TAIL).unwrap();
    // In batches, so the fake's event channel never drops one.
    for batch in 0..6 {
        let last = format!("noisy line {}", batch * 200 + 199);
        for n in batch * 200..batch * 200 + 200 {
            dashboard.log(&format!("noisy line {n}"));
        }
        events.push_str(&read_until(&mut stream, |text| text.contains(&last)).await);
    }
    // A sample after the last line: its chart comes after whatever the stream sends for the lines.
    let _ = dashboard.fake.events.send(EventEnvelope {
        id: None,
        at: NOW + 2,
        event: Event::JobSample {
            job: LEDGER_RUNNING,
            sample: Sample {
                at: NOW + 2,
                cpu: 1000,
                memory: 13 << 30,
                network_received: 0,
                network_sent: 0,
            },
        },
    });
    events.push_str(&read_until(&mut stream, |text| text.contains("sampled peak 13G")).await);
    shown.apply(&events);
    assert!(
        shown.0.len() <= tail,
        "the page shows {} lines",
        shown.0.len()
    );
    assert_eq!(
        shown.0.last().map(|line| line.1.as_str()),
        Some("noisy line 1199")
    );
}

#[tokio::test]
async fn a_job_stream_that_reconnects_shows_each_log_line_once() {
    let dashboard = Dashboard::start().await;
    let cookie = dashboard.sign_in().await;
    let (page, url) = dashboard.job_page(&cookie).await;
    let mut shown = ShownLog::of_page(&page);
    let mut first = dashboard.stream(&cookie, &url).await;
    read_until(&mut first, |text| text.contains(r#"id="metrics""#)).await;
    for n in 0..5 {
        dashboard.log(&format!("before the drop {n}"));
    }
    shown.apply(&read_until(&mut first, |text| text.contains("before the drop 4")).await);
    drop(first);
    // Datastar retries the URL the page named.
    dashboard.log("while away");
    let mut second = dashboard.stream(&cookie, &url).await;
    shown.apply(&read_until(&mut second, |text| text.contains(r#"id="metrics""#)).await);
    let texts: Vec<&str> = shown.0.iter().map(|line| line.1.as_str()).collect();
    for n in 0..5 {
        let text = format!("before the drop {n}");
        assert_eq!(
            texts.iter().filter(|shown| **shown == text).count(),
            1,
            "{texts:?}"
        );
    }
    assert_eq!(texts.last(), Some(&"while away"));
    let mut numbers: Vec<Option<u64>> = shown.0.iter().map(|line| line.0).collect();
    numbers.dedup();
    assert_eq!(
        numbers.len(),
        shown.0.len(),
        "each line has its own number: {texts:?}"
    );
}

#[tokio::test]
async fn a_job_stream_that_reconnects_with_the_pages_signals_sends_only_the_lines_after_its_last() {
    let dashboard = Dashboard::start().await;
    let cookie = dashboard.sign_in().await;
    let (page, url) = dashboard.job_page(&cookie).await;
    let mut shown = ShownLog::of_page(&page);
    let signals = signals_of(&page);
    let mut first = dashboard.stream(&cookie, &format!("{url}?{signals}")).await;
    let connected = read_until(&mut first, |text| text.contains("datastar-patch-signals")).await;
    assert!(
        !connected.contains(r#"id="log-lines""#),
        "the page has the tail already: {connected}"
    );
    for n in 0..5 {
        dashboard.log(&format!("before the drop {n}"));
    }
    let lines = read_until(&mut first, |text| text.contains("before the drop 4")).await;
    shown.apply(&lines);
    drop(first);
    // The last line appended set the page's `shown.log`, which Datastar sends when it retries.
    let last = lines.rsplit(r#"data-signals:shown.log=""#).next().unwrap();
    let last: u64 = last[..last.find('"').unwrap()].parse().unwrap();
    let mut retried = page_signals(&page);
    retried["shown"]["log"] = last.into();
    dashboard.log("while away");
    let mut second = dashboard
        .stream(&cookie, &format!("{url}?{}", query(&retried)))
        .await;
    let again = read_until(&mut second, |text| text.contains("while away")).await;
    assert!(!again.contains(r#"id="log-lines""#), "{again}");
    shown.apply(&again);
    let texts: Vec<&str> = shown.0.iter().map(|line| line.1.as_str()).collect();
    for n in 0..5 {
        let text = format!("before the drop {n}");
        assert_eq!(
            texts.iter().filter(|shown| **shown == text).count(),
            1,
            "{texts:?}"
        );
    }
    assert_eq!(texts.last(), Some(&"while away"));
}

#[tokio::test]
async fn the_dashboard_refuses_to_listen_beyond_loopback() {
    let error = crate::listen("0.0.0.0:0").await.unwrap_err();
    assert!(error.to_string().contains("loopback only"));
    assert!(crate::listen("off").await.unwrap().is_none());
}

/// The fixtures' config with jvm's memory raised from 9G to `memory`.
fn with_jvm_memory(dashboard: &Dashboard, memory: &str) -> String {
    let config = dashboard.fake.fixtures.lock().unwrap().config.clone();
    let raised = config.replacen("memory = \"9G\"", &format!("memory = \"{memory}\""), 1);
    assert_ne!(raised, config);
    raised
}

#[tokio::test]
async fn a_config_over_the_budget_is_not_saved_and_the_page_shows_both_numbers() {
    let dashboard = Dashboard::start().await;
    let cookie = dashboard.sign_in().await;
    let before = dashboard.fake.fixtures.lock().unwrap().config.clone();
    // 2 × 12G + 2 × 7G + 512M = 38.5G, over runnerd.slice's 33G.
    let toml = with_jvm_memory(&dashboard, "12G");
    let reply = dashboard
        .write(
            &cookie,
            "PUT",
            "/config",
            &serde_json::json!({ "toml": toml }),
        )
        .await;
    assert_eq!(reply.status, StatusCode::OK);
    assert!(
        reply.body.contains(r#"id="config-result""#) && reply.body.contains("Not saved"),
        "{}",
        reply.body
    );
    assert!(
        reply.body.contains("38.5G") && reply.body.contains("33G"),
        "{}",
        reply.body
    );
    assert_eq!(dashboard.fake.fixtures.lock().unwrap().config, before);
}

#[tokio::test]
async fn a_config_that_fits_is_saved_and_checking_one_saves_nothing() {
    let dashboard = Dashboard::start().await;
    let cookie = dashboard.sign_in().await;
    let toml = with_jvm_memory(&dashboard, "8G");
    let checked = dashboard
        .write(
            &cookie,
            "POST",
            "/config/check",
            &serde_json::json!({ "toml": toml }),
        )
        .await;
    assert!(checked.body.contains("Fits the budget"), "{}", checked.body);
    assert_ne!(dashboard.fake.fixtures.lock().unwrap().config, toml);
    let saved = dashboard
        .write(
            &cookie,
            "PUT",
            "/config",
            &serde_json::json!({ "toml": toml }),
        )
        .await;
    assert!(saved.body.contains("Saved and reloaded"), "{}", saved.body);
    assert_eq!(dashboard.fake.fixtures.lock().unwrap().config, toml);
}

#[tokio::test]
async fn the_config_page_shows_the_file_its_budget_and_the_doctor() {
    let dashboard = Dashboard::start().await;
    let cookie = dashboard.sign_in().await;
    let page = dashboard.page(&cookie, "/config").await;
    assert_eq!(page.status, StatusCode::OK);
    for expected in [
        "halyard/ledger",
        "/home/forge/.config/runnerd/runnerd.toml",
        "Fits the budget",
        "jvm 2",
        "node 2 7G",
        "MemoryHigh=30G",
    ] {
        assert!(
            page.body.contains(expected),
            "config page lacks {expected:?}"
        );
    }
}

#[tokio::test]
async fn sizing_advice_opens_in_the_config_editor_and_is_written_only_when_saved() {
    let dashboard = Dashboard::start().await;
    let cookie = dashboard.sign_in().await;
    let sizing = dashboard.page(&cookie, "/sizing").await;
    assert_eq!(sizing.status, StatusCode::OK);
    for expected in [
        "lower memory to 2.5G from 7G, its largest peak + 15%; 5 slots fit in the same 14G",
        "keep 9G: its largest peak + 15% is 9G",
        r#"href="/config?resize=node""#,
        "Review memory 2.5G, 5 slots in the config",
        "integration tests",
        "halyard/storefront · CI",
        "waits for 10 jobs",
    ] {
        assert!(
            sizing.body.contains(expected),
            "sizing page lacks {expected:?}"
        );
    }
    assert!(
        !sizing.body.contains("/config?resize=jvm"),
        "jvm has nothing to apply"
    );

    let before = dashboard.fake.fixtures.lock().unwrap().config.clone();
    let editor = dashboard.page(&cookie, "/config?resize=node").await;
    assert_eq!(editor.status, StatusCode::OK);
    let advised = before.replace("memory = \"7G\"\nslots = 2", "memory = \"2.5G\"\nslots = 5");
    assert_ne!(advised, before);
    for expected in [
        "memory = &quot;2.5G&quot;\nslots = 5",
        "Nothing is written until you save it.",
        // 2 × 9G + 5 × 2.5G + 512M.
        "31G",
    ] {
        assert!(editor.body.contains(expected), "editor lacks {expected:?}");
    }
    assert_eq!(
        dashboard.fake.fixtures.lock().unwrap().config,
        before,
        "opening the editor writes nothing"
    );
    let saved = dashboard
        .write(
            &cookie,
            "PUT",
            "/config",
            &serde_json::json!({ "toml": advised }),
        )
        .await;
    assert!(saved.body.contains("Saved and reloaded"), "{}", saved.body);
    assert_eq!(dashboard.fake.fixtures.lock().unwrap().config, advised);
}

#[tokio::test]
async fn pausing_a_repository_with_a_running_job_asks_first_and_cancels_only_when_told() {
    let dashboard = Dashboard::start().await;
    let cookie = dashboard.sign_in().await;
    let asked = dashboard
        .write(
            &cookie,
            "POST",
            "/v1/repositories/halyard/ledger/pause",
            &serde_json::json!({}),
        )
        .await;
    assert!(
        asked.body.contains("Cancel and pause") && asked.body.contains("pause?force=true"),
        "{}",
        asked.body
    );
    assert!(
        !asked.body.contains("pause with force"),
        "the question asks rather than repeating the CLI's hint: {}",
        asked.body
    );
    assert!(
        asked.body.contains(">Keep it running<"),
        "the question's dismissal speaks of the job: {}",
        asked.body
    );
    assert!(
        asked.body.contains(&LEDGER_RUNNING.to_string()),
        "the question names the job: {}",
        asked.body
    );
    assert!(!dashboard.fake.fixtures.lock().unwrap().status.repositories[0].paused);
    let forced = dashboard
        .write(
            &cookie,
            "POST",
            "/v1/repositories/halyard/ledger/pause?force=true",
            &serde_json::json!({}),
        )
        .await;
    assert!(
        forced
            .body
            .contains(&format!(r#"id="{}""#, repository_id("halyard/ledger")))
            && forced.body.contains("stopped job"),
        "{}",
        forced.body
    );
    let fixtures = dashboard.fake.fixtures.lock().unwrap();
    assert!(fixtures.status.repositories[0].paused);
    assert_eq!(
        fixtures.controls,
        [
            "pause halyard/ledger force=false",
            "pause halyard/ledger force=true"
        ]
    );
}

#[tokio::test]
async fn reseed_and_empty_from_the_caches_page_reach_the_daemon() {
    let dashboard = Dashboard::start().await;
    let cookie = dashboard.sign_in().await;
    let page = dashboard.page(&cookie, "/caches").await;
    assert!(
        page.body.contains("/v1/profiles/jvm/reseed")
            && page.body.contains("/v1/caches/runnerd-pnpm/clean")
    );
    let reseed = dashboard
        .write(
            &cookie,
            "POST",
            "/v1/profiles/jvm/reseed",
            &serde_json::json!({}),
        )
        .await;
    assert!(reseed.body.contains("Reseeding jvm"), "{}", reseed.body);
    let clean = dashboard
        .write(
            &cookie,
            "POST",
            "/v1/caches/runnerd-pnpm/clean",
            &serde_json::json!({}),
        )
        .await;
    assert!(
        clean.body.contains("Emptied runnerd-pnpm"),
        "{}",
        clean.body
    );
    assert_eq!(
        dashboard.fake.fixtures.lock().unwrap().controls,
        ["reseed jvm", "clean runnerd-pnpm"]
    );
}

#[tokio::test]
async fn a_failed_jobs_record_shows_its_shell_command_and_goes_from_the_caches_page() {
    let dashboard = Dashboard::start().await;
    let cookie = dashboard.sign_in().await;
    let (failed, others) = {
        let fixtures = dashboard.fake.fixtures.lock().unwrap();
        let jobs: Vec<u64> = fixtures.records.records.iter().map(|r| r.job).collect();
        (jobs[0], jobs.len() - 1)
    };
    let job = dashboard
        .page(&cookie, &format!("/jobs/{failed}"))
        .await
        .body;
    assert!(
        job.contains("Open a shell") && job.contains(&format!("runnerctl shell {failed}")),
        "{job}"
    );
    let caches = dashboard.page(&cookie, "/caches").await.body;
    assert!(
        caches.contains(&format!("@delete('/v1/records/{failed}')")),
        "{caches}"
    );

    let removed = dashboard
        .write(
            &cookie,
            "DELETE",
            &format!("/v1/records/{failed}"),
            &serde_json::json!({}),
        )
        .await;
    assert!(
        removed.body.contains("Removed 1 record"),
        "{}",
        removed.body
    );
    assert!(
        !removed.body.contains(&format!("/v1/records/{failed}'")),
        "the section shows the record gone: {}",
        removed.body
    );
    let job = dashboard
        .page(&cookie, &format!("/jobs/{failed}"))
        .await
        .body;
    assert!(
        !job.contains("runnerctl shell") && job.contains("removed by hand"),
        "{job}"
    );

    let all = dashboard
        .write(
            &cookie,
            "DELETE",
            "/v1/records?all=true",
            &serde_json::json!({}),
        )
        .await;
    assert!(
        all.body.contains(&format!("Removed {others} records")),
        "{}",
        all.body
    );
    assert!(all.body.contains("None kept"), "{}", all.body);
}

#[tokio::test]
async fn the_overview_offers_pause_all_until_every_repository_is_paused_then_resume_all() {
    let dashboard = Dashboard::start().await;
    let cookie = dashboard.sign_in().await;
    let pause_all = "@post('/v1/repositories/pause')";
    let resume_all = "@post('/v1/repositories/resume')";
    let none = dashboard.page(&cookie, "/").await.body;
    assert!(
        none.contains(pause_all) && !none.contains(resume_all),
        "nothing paused offers only pause all"
    );
    dashboard.fake.fixtures.lock().unwrap().status.repositories[0].paused = true;
    let some = dashboard.page(&cookie, "/").await.body;
    assert!(
        some.contains(pause_all) && some.contains(resume_all),
        "one of two paused offers both"
    );
    dashboard.fake.fixtures.lock().unwrap().status.repositories[1].paused = true;
    let all = dashboard.page(&cookie, "/").await.body;
    assert!(
        !all.contains(pause_all) && all.contains(resume_all),
        "everything paused offers only resume all"
    );
    let resumed = dashboard
        .write(
            &cookie,
            "POST",
            "/v1/repositories/resume",
            &serde_json::json!({}),
        )
        .await;
    assert!(
        resumed.body.contains(r#"id="repositories""#) && resumed.body.contains(pause_all),
        "resume all returns the rows, offering pause all again: {}",
        resumed.body
    );
    let fixtures = dashboard.fake.fixtures.lock().unwrap();
    assert!(
        fixtures
            .status
            .repositories
            .iter()
            .all(|repository| !repository.paused),
        "every repository runs again"
    );
    assert_eq!(fixtures.controls, ["resume all force=false"]);
}

#[tokio::test]
async fn every_buttons_name_starts_with_the_label_it_shows() {
    let dashboard = Dashboard::start().await;
    // One repository paused, so the overview offers both pause all and resume all.
    dashboard.fake.fixtures.lock().unwrap().status.repositories[0].paused = true;
    let cookie = dashboard.sign_in().await;
    let overview = dashboard.page(&cookie, "/").await.body;
    let buttons: Vec<&str> = overview.split("<button").skip(1).collect();
    assert!(buttons.len() > 3, "{} buttons", buttons.len());
    for button in buttons {
        let (tag, rest) = button.split_once('>').unwrap();
        let Some(name) = tag.split(r#"aria-label=""#).nth(1) else {
            continue;
        };
        let name = &name[..name.find('"').unwrap()];
        let label: String = rest[..rest.find("</button>").unwrap()]
            .split('<')
            .map(|text| text.split_once('>').map_or(text, |(_, text)| text))
            .collect();
        if !label.trim().is_empty() {
            assert!(
                name.starts_with(label.trim()),
                "a button showing {label:?} is named {name:?}"
            );
        }
    }
}

#[tokio::test]
async fn a_single_repository_has_only_its_own_pause() {
    let dashboard = Dashboard::start().await;
    dashboard
        .fake
        .fixtures
        .lock()
        .unwrap()
        .status
        .repositories
        .truncate(1);
    let cookie = dashboard.sign_in().await;
    let overview = dashboard.page(&cookie, "/").await.body;
    assert!(
        !overview.contains("@post('/v1/repositories/pause')"),
        "no pause all beside the one pause"
    );
}

#[tokio::test]
async fn pausing_every_repository_while_jobs_run_asks_first_naming_them_and_cancels_only_when_told()
{
    let dashboard = Dashboard::start().await;
    let cookie = dashboard.sign_in().await;
    let storefront_running = dashboard
        .fake
        .fixtures
        .lock()
        .unwrap()
        .jobs
        .iter()
        .find(|job| {
            job.summary.state == JobState::Running && job.summary.repository == "halyard/storefront"
        })
        .unwrap()
        .summary
        .id;
    let asked = dashboard
        .write(
            &cookie,
            "POST",
            "/v1/repositories/pause",
            &serde_json::json!({}),
        )
        .await;
    for job in [LEDGER_RUNNING, storefront_running] {
        assert!(
            asked.body.contains(&job.to_string()),
            "the question names job {job}: {}",
            asked.body
        );
    }
    assert!(
        asked
            .body
            .contains("@post('/v1/repositories/pause?force=true')")
            && !asked.body.contains("pause with force"),
        "the question offers the forced pause of every repository: {}",
        asked.body
    );
    assert!(
        asked.body.contains(">Keep them running<"),
        "the question's dismissal speaks of the jobs: {}",
        asked.body
    );
    assert!(
        !dashboard
            .fake
            .fixtures
            .lock()
            .unwrap()
            .status
            .repositories
            .iter()
            .any(|repository| repository.paused),
        "a refused pause pauses none"
    );
    let forced = dashboard
        .write(
            &cookie,
            "POST",
            "/v1/repositories/pause?force=true",
            &serde_json::json!({}),
        )
        .await;
    assert!(
        forced.body.contains(r#"id="repositories""#)
            && forced.body.contains("@post('/v1/repositories/resume')")
            && forced.body.contains("stopped job"),
        "{}",
        forced.body
    );
    let fixtures = dashboard.fake.fixtures.lock().unwrap();
    assert!(
        fixtures
            .status
            .repositories
            .iter()
            .all(|repository| repository.paused),
        "every repository is paused"
    );
    assert_eq!(
        fixtures.controls,
        ["pause all force=false", "pause all force=true"]
    );
}

#[tokio::test]
async fn resuming_every_repository_when_the_history_cannot_record_it_says_so() {
    let dashboard = Dashboard::start().await;
    let cookie = dashboard.sign_in().await;
    dashboard.fake.fixtures.lock().unwrap().unrecorded = true;
    let reply = dashboard
        .write(
            &cookie,
            "POST",
            "/v1/repositories/resume",
            &serde_json::json!({}),
        )
        .await;
    assert_eq!(reply.status, StatusCode::OK);
    assert!(
        reply.body.contains(r#"id="notice""#)
            && reply.body.contains("toast failed")
            && reply
                .body
                .contains("cannot record it, so a restart pauses it again"),
        "{}",
        reply.body
    );
}