trusty-console 0.9.1

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

use super::*;
use crate::webhook::spool::{PendingListing, SPOOL_SCHEMA_VERSION, SpoolError};
use trusty_common::webhook_relay::RELAY_METHOD;

use std::collections::BTreeMap;
use std::path::Path as StdPath;

use axum::body::Body;
use axum::http::{Request, StatusCode};
use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tokio::net::UnixListener;
use tower::ServiceExt as _;
use trusty_common::console_metrics::ServiceHealth;
use trusty_common::uds::bind_hardened;
use trusty_common::webhook_hmac::sign_github_body;

const SECRET: &str = "console-ingress-test-secret"; // pragma: allowlist secret
const BODY: &[u8] = br#"{"action":"review_requested","number":42}"#;

// ─── harness ────────────────────────────────────────────────────────────────

/// How a stub target answers one relayed frame.
#[derive(Clone)]
enum StubTarget {
    /// Answer `{"result":{"ack":true}}`.
    Ack,
    /// Answer with a result object that has no `ack` field at all.
    ResultWithoutAck,
    /// Answer with a JSON-RPC error.
    RpcError,
    /// Accept the connection, read the frame, then hang up without answering.
    ConnectThenHangUp,
    /// Answer `ack` only after a delay, so a second relay has time to race the
    /// first. This is what makes the sweep-versus-request window observable.
    AckAfter(Duration),
}

/// Frames a stub captured, so a test can assert on what console actually sent.
type Captured = std::sync::Arc<tokio::sync::Mutex<Vec<serde_json::Value>>>;

/// Bind a hardened socket under `dir` and serve `count` connections.
fn spawn_target(dir: &StdPath, behaviour: StubTarget, count: usize) -> (PathBuf, Captured) {
    let socket = dir.join("sockets").join("target.sock");
    let listener: UnixListener = bind_hardened(&socket).expect("bind stub target");
    let captured: Captured = Default::default();
    let sink = captured.clone();
    tokio::spawn(async move {
        for _ in 0..count {
            let Ok((mut conn, _)) = listener.accept().await else {
                return;
            };
            let mut raw = Vec::new();
            let _ = conn.read_to_end(&mut raw).await;
            if let Ok(frame) = serde_json::from_slice::<serde_json::Value>(&raw) {
                sink.lock().await.push(frame);
            }
            if let StubTarget::AckAfter(delay) = behaviour {
                tokio::time::sleep(delay).await;
            }
            let reply: Option<&[u8]> = match behaviour {
                StubTarget::Ack | StubTarget::AckAfter(_) => Some(br#"{"result":{"ack":true}}"#),
                StubTarget::ResultWithoutAck => Some(br#"{"result":{}}"#),
                StubTarget::RpcError => {
                    Some(br#"{"error":{"code":-32000,"message":"store locked"}}"#)
                }
                StubTarget::ConnectThenHangUp => None,
            };
            if let Some(bytes) = reply {
                let _ = conn.write_all(bytes).await;
                let _ = conn.write_all(b"\n").await;
                let _ = conn.flush().await;
            }
        }
    });
    (socket, captured)
}

/// A stub that announces when it has received a frame, then waits for the test
/// to release it before answering.
///
/// Why: the previous version of the sweep-race test slept 150 ms and hoped
/// `ingest` had reached its claim by then. That asserts on a window the test
/// does not control — and it does not control it: under CI scheduling the
/// spawned task had not even persisted the entry yet, so the sweep listed an
/// empty spool and the assertion failed for a reason unrelated to claims.
/// A rendezvous removes the wall clock entirely: the target has the frame, so
/// the relay is provably in flight, so the claim is provably held.
/// What: returns the socket, the captured frames, a receiver that fires once
/// the first frame has been read, and a sender that lets the stub answer. Any
/// connection after the first is answered immediately, so a second relay — the
/// defect — is observable rather than hanging.
/// Test: `sweep_does_not_relay_an_entry_the_request_path_is_still_relaying`.
fn spawn_gated_target(
    dir: &StdPath,
) -> (
    PathBuf,
    Captured,
    tokio::sync::oneshot::Receiver<()>,
    tokio::sync::oneshot::Sender<()>,
) {
    let socket = dir.join("sockets").join("gated.sock");
    let listener: UnixListener = bind_hardened(&socket).expect("bind gated stub");
    let captured: Captured = Default::default();
    let sink = captured.clone();
    let (received_tx, received_rx) = tokio::sync::oneshot::channel();
    let (release_tx, release_rx) = tokio::sync::oneshot::channel();

    tokio::spawn(async move {
        async fn absorb(conn: &mut tokio::net::UnixStream, sink: &Captured) {
            let mut raw = Vec::new();
            let _ = conn.read_to_end(&mut raw).await;
            if let Ok(frame) = serde_json::from_slice::<serde_json::Value>(&raw) {
                sink.lock().await.push(frame);
            }
        }
        async fn ack(conn: &mut tokio::net::UnixStream) {
            let _ = conn.write_all(br#"{"result":{"ack":true}}"#).await;
            let _ = conn.write_all(b"\n").await;
            let _ = conn.flush().await;
        }

        let Ok((mut first, _)) = listener.accept().await else {
            return;
        };
        absorb(&mut first, &sink).await;
        let _ = received_tx.send(());
        let _ = release_rx.await;
        ack(&mut first).await;

        while let Ok((mut extra, _)) = listener.accept().await {
            absorb(&mut extra, &sink).await;
            ack(&mut extra).await;
        }
    });

    (socket, captured, received_rx, release_tx)
}

/// A schedule that admits every pending entry immediately.
///
/// Most cases here are asserting on relay and spool behaviour, not on timing;
/// the real 5 s grace and 30 s base would make each of them sleep. The
/// `backoff_*` and `sweep_honours_*` cases use the real policy instead.
fn no_backoff() -> BackoffPolicy {
    BackoffPolicy {
        first_attempt_grace: Duration::ZERO,
        base: Duration::ZERO,
        ceiling: Duration::ZERO,
        max_attempts: u32::MAX,
    }
}

/// An ingress whose single `review` target points at `socket`.
fn ingress_for(spool: Spool, socket: PathBuf) -> WebhookIngress {
    ingress_with(spool, socket, Duration::from_secs(2)).with_backoff(no_backoff())
}

/// [`ingress_for`] keeping the production backoff schedule and taking an
/// explicit relay timeout.
fn ingress_with(spool: Spool, socket: PathBuf, relay_timeout: Duration) -> WebhookIngress {
    WebhookIngress::new(
        spool,
        SECRET.to_string(),
        SECRET_ENV.to_string(),
        vec![Target {
            source: "review".to_string(),
            relay: UdsRelay::new(socket).with_timeout(relay_timeout),
        }],
    )
}

/// A signed request header set for `body`.
fn signed_headers(body: &[u8], delivery: &str) -> HeaderMap {
    let mut headers = HeaderMap::new();
    headers.insert(
        SIGNATURE_HEADER,
        sign_github_body(SECRET, body).parse().expect("sig header"),
    );
    headers.insert("x-github-event", "pull_request".parse().expect("event"));
    headers.insert("x-github-delivery", delivery.parse().expect("delivery"));
    headers
}

fn listing_of(spool: &Spool) -> PendingListing {
    spool.list_pending().expect("list spool")
}

/// How many uncommitted temp files the spool directory holds.
///
/// Every failure path must clean up after itself; a leaked temp file is junk
/// the health scan then has to learn to ignore.
fn temp_files_in(spool: &Spool) -> usize {
    std::fs::read_dir(spool.root())
        .expect("read spool root")
        .filter_map(Result::ok)
        .filter(|e| e.path().to_string_lossy().ends_with(".tmp"))
        .count()
}

fn sample_entry(delivery_id: &str, received_at_unix_ms: u64) -> SpoolEntry {
    SpoolEntry {
        schema_version: SPOOL_SCHEMA_VERSION,
        delivery_id: delivery_id.to_string(),
        source: "review".to_string(),
        event: "pull_request".to_string(),
        headers: BTreeMap::from([("x-github-event".to_string(), "pull_request".to_string())]),
        body_b64: BASE64.encode(BODY),
        provenance: Provenance {
            algorithm: HMAC_ALGORITHM.to_string(),
            key_id: SECRET_ENV.to_string(),
            verified: true,
        },
        received_at_unix_ms,
        attempts: 0,
        last_error: None,
        last_attempt_at_unix_ms: None,
    }
}

// ─── spool: durability ──────────────────────────────────────────────────────

#[test]
fn spool_open_creates_the_directory_at_0700() {
    use std::os::unix::fs::PermissionsExt;
    let tmp = tempfile::tempdir().expect("tempdir");
    let root = tmp.path().join("webhook-spool");
    let spool = Spool::open(&root).expect("open spool");
    let mode = std::fs::metadata(spool.root())
        .expect("stat spool root")
        .permissions()
        .mode()
        & 0o777;
    assert_eq!(mode, 0o700, "a spooled webhook body is not public data");
}

#[test]
fn spool_persists_and_reloads_an_entry_byte_exact() {
    let tmp = tempfile::tempdir().expect("tempdir");
    let spool = Spool::open(tmp.path().join("spool")).expect("open spool");
    let entry = sample_entry("d-1", 1_700_000_000_000);

    let path = spool.persist_new(&entry).expect("durable write");
    assert!(path.exists(), "the entry must be on disk before any ack");

    let listing = listing_of(&spool);
    assert_eq!(listing.pending.len(), 1);
    assert_eq!(listing.pending[0].entry, entry);
    assert_eq!(
        BASE64
            .decode(&listing.pending[0].entry.body_b64)
            .expect("decode body"),
        BODY,
        "the body must survive the round trip byte-exact — the HMAC covers it"
    );
    assert!(
        listing.undecodable.is_empty(),
        "no temp files may leak into the listing"
    );
}

#[test]
fn spool_persist_fails_when_the_root_is_not_a_directory() {
    // The spool-write-failure arm. A regular file where the directory belongs
    // makes every `File::create` inside it fail with ENOTDIR for every uid,
    // including root — so this is deterministic in any CI container.
    let tmp = tempfile::tempdir().expect("tempdir");
    let root = tmp.path().join("not-a-dir");
    std::fs::write(&root, b"blocking file").expect("write blocker");

    let spool = Spool::at(&root);
    let err = spool
        .persist_new(&sample_entry("d-1", 1))
        .expect_err("a delivery that cannot be written must not report success");
    assert!(
        matches!(
            err,
            SpoolError::Write { .. } | SpoolError::PrepareDir { .. }
        ),
        "expected a write failure, got {err:?}"
    );
}

#[test]
fn spool_persist_new_refuses_to_clobber_an_existing_entry() {
    // The entry already at that path may be one console has ALREADY
    // acknowledged. Overwriting it destroys a delivery GitHub will never
    // re-send, so `persist_new` commits with `hard_link` (atomic EEXIST) rather
    // than `rename` (silent replace).
    let tmp = tempfile::tempdir().expect("tempdir");
    let spool = Spool::open(tmp.path().join("spool")).expect("open spool");
    let first = sample_entry("d-collide", 1_700_000_000_000);
    spool.persist_new(&first).expect("first write");

    // Same millisecond, same (missing-header-derived) id: the collision case.
    let mut second = sample_entry("d-collide", 1_700_000_000_000);
    second.event = "issues".to_string();

    let err = spool
        .persist_new(&second)
        .expect_err("clobbering an existing entry must not report success");
    assert!(
        matches!(err, SpoolError::AlreadyExists { .. }),
        "expected AlreadyExists, got {err:?}"
    );

    let listing = listing_of(&spool);
    assert_eq!(listing.pending.len(), 1);
    assert_eq!(
        listing.pending[0].entry, first,
        "the entry already on disk must survive untouched"
    );
    assert_eq!(
        temp_files_in(&spool),
        0,
        "a refused write must not leave its temp file behind"
    );
}

#[test]
fn spool_persist_update_fails_when_the_final_path_is_a_directory() {
    // `record_attempt` wants clobber semantics, so it commits with `rename` —
    // which still has to fail loudly when the destination cannot be replaced.
    let tmp = tempfile::tempdir().expect("tempdir");
    let spool = Spool::open(tmp.path().join("spool")).expect("open spool");
    let entry = sample_entry("d-1", 1_700_000_000_000);
    std::fs::create_dir_all(spool.entry_path(&entry)).expect("occupy the final path");

    let err = spool
        .persist_update(&entry)
        .expect_err("a rewrite that cannot be committed must not report success");
    assert!(
        matches!(err, SpoolError::Commit { .. }),
        "expected Commit, got {err:?}"
    );
    assert_eq!(
        temp_files_in(&spool),
        0,
        "a failed commit must not leave its temp file behind"
    );
}

#[test]
fn spool_record_attempt_increments_durably() {
    let tmp = tempfile::tempdir().expect("tempdir");
    let spool = Spool::open(tmp.path().join("spool")).expect("open spool");
    let mut entry = sample_entry("d-1", 1_700_000_000_000);
    spool.persist_new(&entry).expect("durable write");

    spool
        .record_attempt(&mut entry, "no listener".to_string(), 1_700_000_005_000)
        .expect("record attempt");
    spool
        .record_attempt(&mut entry, "no listener".to_string(), 1_700_000_010_000)
        .expect("record attempt");

    let listing = listing_of(&spool);
    assert_eq!(listing.pending.len(), 1, "attempts must not fork the entry");
    let reloaded = &listing.pending[0].entry;
    assert_eq!(reloaded.attempts, 2);
    assert_eq!(reloaded.last_error.as_deref(), Some("no listener"));
    assert_eq!(reloaded.last_attempt_at_unix_ms, Some(1_700_000_010_000));
}

#[test]
fn spool_remove_acked_deletes_the_entry() {
    let tmp = tempfile::tempdir().expect("tempdir");
    let spool = Spool::open(tmp.path().join("spool")).expect("open spool");
    let entry = sample_entry("d-1", 1);
    let path = spool.persist_new(&entry).expect("durable write");

    spool.remove_acked(&path).expect("remove");
    assert!(listing_of(&spool).pending.is_empty());
}

#[test]
fn spool_remove_acked_tolerates_an_already_removed_entry() {
    let tmp = tempfile::tempdir().expect("tempdir");
    let spool = Spool::open(tmp.path().join("spool")).expect("open spool");
    let path = spool.root().join("0000000000001-gone.json");
    spool
        .remove_acked(&path)
        .expect("a missing entry is not an error");
}

#[test]
fn spool_entry_path_sanitises_a_hostile_delivery_id() {
    let tmp = tempfile::tempdir().expect("tempdir");
    let spool = Spool::open(tmp.path().join("spool")).expect("open spool");
    // The delivery header is not covered by the HMAC, so it is attacker input.
    let entry = sample_entry("../../../etc/passwd", 7);
    let path = spool.entry_path(&entry);
    assert_eq!(
        path.parent(),
        Some(spool.root()),
        "a delivery id must never escape the spool directory: {}",
        path.display()
    );
    assert!(!path.to_string_lossy().contains(".."));
}

#[test]
fn spool_list_pending_orders_oldest_first() {
    let tmp = tempfile::tempdir().expect("tempdir");
    let spool = Spool::open(tmp.path().join("spool")).expect("open spool");
    for (id, at) in [("c", 300u64), ("a", 100), ("b", 200)] {
        spool
            .persist_new(&sample_entry(id, at))
            .expect("durable write");
    }
    let ids: Vec<String> = listing_of(&spool)
        .pending
        .into_iter()
        .map(|p| p.entry.delivery_id)
        .collect();
    assert_eq!(ids, vec!["a", "b", "c"]);
}

#[test]
fn spool_list_pending_reports_an_undecodable_entry() {
    // An unreadable pending delivery is not an absent one. Silently skipping it
    // would report the spool empty while a delivery sits unrelayed.
    let tmp = tempfile::tempdir().expect("tempdir");
    let spool = Spool::open(tmp.path().join("spool")).expect("open spool");
    std::fs::write(spool.root().join("0000000000001-junk.json"), b"{not json").expect("write junk");

    let listing = listing_of(&spool);
    assert!(listing.pending.is_empty());
    assert_eq!(listing.undecodable.len(), 1);
}

// ─── relay: only an explicit ack counts ─────────────────────────────────────

#[tokio::test]
async fn relay_frame_carries_provenance_and_byte_exact_body() {
    let tmp = tempfile::tempdir().expect("tempdir");
    let (socket, captured) = spawn_target(tmp.path(), StubTarget::Ack, 1);
    let relay = UdsRelay::new(socket).with_timeout(Duration::from_secs(2));

    let entry = sample_entry("d-frame", 1_700_000_000_000);
    assert_eq!(relay.deliver(&entry).await, RelayOutcome::Acked);

    let frames = captured.lock().await;
    let frame = frames.first().expect("target received a frame");
    assert_eq!(frame["jsonrpc"], "2.0");
    assert_eq!(frame["method"], RELAY_METHOD);
    assert_eq!(frame["id"], "d-frame");
    let params = &frame["params"];
    assert_eq!(params["provenance"]["algorithm"], HMAC_ALGORITHM);
    assert_eq!(params["provenance"]["key_id"], SECRET_ENV);
    assert_eq!(params["provenance"]["verified"], true);
    assert_eq!(
        BASE64
            .decode(params["body_b64"].as_str().expect("body_b64 is a string"))
            .expect("decode relayed body"),
        BODY,
        "the target must receive the exact bytes GitHub signed"
    );
    assert!(
        !frame.to_string().contains(SECRET),
        "the secret must never leave console"
    );
}

#[tokio::test]
async fn relay_acked_response_is_the_only_ack() {
    let tmp = tempfile::tempdir().expect("tempdir");
    let (socket, _) = spawn_target(tmp.path(), StubTarget::Ack, 1);
    let relay = UdsRelay::new(socket).with_timeout(Duration::from_secs(2));
    assert!(relay.deliver(&sample_entry("d-1", 1)).await.is_acked());
}

#[tokio::test]
async fn relay_treats_a_result_without_ack_as_refused() {
    // The "connected but has not acknowledged" arm. A target that answers with
    // an empty result object has done nothing; treating the successful
    // round trip as success is the same silent loss one layer down.
    let tmp = tempfile::tempdir().expect("tempdir");
    let (socket, _) = spawn_target(tmp.path(), StubTarget::ResultWithoutAck, 1);
    let relay = UdsRelay::new(socket).with_timeout(Duration::from_secs(2));

    let outcome = relay.deliver(&sample_entry("d-1", 1)).await;
    assert!(
        matches!(outcome, RelayOutcome::Refused { .. }),
        "expected Refused, got {outcome:?}"
    );
    assert!(!outcome.is_acked());
}

#[tokio::test]
async fn relay_treats_a_jsonrpc_error_as_refused() {
    let tmp = tempfile::tempdir().expect("tempdir");
    let (socket, _) = spawn_target(tmp.path(), StubTarget::RpcError, 1);
    let relay = UdsRelay::new(socket).with_timeout(Duration::from_secs(2));

    let outcome = relay.deliver(&sample_entry("d-1", 1)).await;
    assert!(!outcome.is_acked());
    assert!(
        outcome.reason().contains("store locked"),
        "the target's own reason must survive into the durable record: {}",
        outcome.reason()
    );
}

#[tokio::test]
async fn relay_reports_unreachable_when_no_listener_is_bound() {
    // The expected state for every delivery until #5089 step 4 binds the
    // target's listener.
    let tmp = tempfile::tempdir().expect("tempdir");
    let relay = UdsRelay::new(tmp.path().join("sockets").join("absent.sock"))
        .with_timeout(Duration::from_secs(2));
    let outcome = relay.deliver(&sample_entry("d-1", 1)).await;
    assert!(
        matches!(outcome, RelayOutcome::Unreachable { .. }),
        "expected Unreachable, got {outcome:?}"
    );
}

#[tokio::test]
async fn relay_reports_unreachable_when_the_target_hangs_up_without_answering() {
    let tmp = tempfile::tempdir().expect("tempdir");
    let (socket, _) = spawn_target(tmp.path(), StubTarget::ConnectThenHangUp, 1);
    let relay = UdsRelay::new(socket).with_timeout(Duration::from_secs(2));
    let outcome = relay.deliver(&sample_entry("d-1", 1)).await;
    assert!(
        matches!(outcome, RelayOutcome::Unreachable { .. }),
        "a connection that succeeds and then dies is not a delivery: {outcome:?}"
    );
}

// ─── ingest: the ordering guarantee ─────────────────────────────────────────

#[tokio::test]
async fn ingest_rejects_an_unknown_source() {
    let tmp = tempfile::tempdir().expect("tempdir");
    let spool = Spool::open(tmp.path().join("spool")).expect("open spool");
    let ingress = ingress_for(spool, tmp.path().join("sockets").join("absent.sock"));

    let outcome = ingress
        .ingest("nope", &signed_headers(BODY, "d-1"), BODY)
        .await;
    assert!(matches!(outcome, IngestOutcome::UnknownSource { .. }));
    assert!(
        listing_of(ingress.spool()).pending.is_empty(),
        "an unroutable source must not consume spool space"
    );
}

#[tokio::test]
async fn ingest_fails_closed_when_no_secret_is_configured() {
    // ADR-0034 §2 unifies the policy to trusty-review's fail-closed answer.
    // trusty-analyze's "log a warning and process it anyway" is gone.
    let tmp = tempfile::tempdir().expect("tempdir");
    let spool = Spool::open(tmp.path().join("spool")).expect("open spool");
    let ingress = WebhookIngress::new(
        spool,
        String::new(),
        SECRET_ENV.to_string(),
        vec![Target {
            source: "review".to_string(),
            relay: UdsRelay::new(tmp.path().join("sockets").join("absent.sock")),
        }],
    );

    let outcome = ingress
        .ingest("review", &signed_headers(BODY, "d-1"), BODY)
        .await;
    assert_eq!(outcome, IngestOutcome::SecretMissing);
    assert!(
        listing_of(ingress.spool()).pending.is_empty(),
        "an unverified body must never reach the spool"
    );
}

#[tokio::test]
async fn ingest_rejects_a_forged_signature() {
    let tmp = tempfile::tempdir().expect("tempdir");
    let spool = Spool::open(tmp.path().join("spool")).expect("open spool");
    let ingress = ingress_for(spool, tmp.path().join("sockets").join("absent.sock"));

    // Signed correctly, then the body is changed by one byte in flight.
    let headers = signed_headers(BODY, "d-1");
    let mut tampered = BODY.to_vec();
    let last = tampered.len() - 1;
    tampered[last] ^= 0x01;

    let outcome = ingress.ingest("review", &headers, &tampered).await;
    assert_eq!(outcome, IngestOutcome::InvalidSignature);
    assert!(listing_of(ingress.spool()).pending.is_empty());
}

#[tokio::test]
async fn ingest_returns_spool_failed_and_never_accepts_when_the_write_fails() {
    // 🔴 The regression this step exists for. The pre-fix handlers return 202
    // and log; here the durable write is a precondition of accepting at all.
    let tmp = tempfile::tempdir().expect("tempdir");
    let blocked = tmp.path().join("not-a-dir");
    std::fs::write(&blocked, b"blocking file").expect("write blocker");
    // The stub WOULD ack — proving the refusal comes from the spool, not the
    // relay, and that no relay ever runs when the write fails.
    let (socket, captured) = spawn_target(tmp.path(), StubTarget::Ack, 1);
    let ingress = ingress_for(Spool::at(&blocked), socket);

    let outcome = ingress
        .ingest("review", &signed_headers(BODY, "d-1"), BODY)
        .await;

    assert!(
        matches!(outcome, IngestOutcome::SpoolFailed { .. }),
        "a delivery that could not be recorded must not be accepted: {outcome:?}"
    );
    assert!(
        !matches!(outcome, IngestOutcome::Accepted { .. }),
        "no ack may be issued when the spool write failed"
    );
    assert!(
        captured.lock().await.is_empty(),
        "the relay must not run before the delivery is durable"
    );
}

#[tokio::test]
async fn ingest_accepts_and_deletes_on_an_explicit_ack() {
    let tmp = tempfile::tempdir().expect("tempdir");
    let spool = Spool::open(tmp.path().join("spool")).expect("open spool");
    let (socket, _) = spawn_target(tmp.path(), StubTarget::Ack, 1);
    let ingress = ingress_for(spool, socket);

    let outcome = ingress
        .ingest("review", &signed_headers(BODY, "d-ack"), BODY)
        .await;

    match outcome {
        IngestOutcome::Accepted {
            delivery_id,
            relay,
            bookkeeping_error,
        } => {
            assert_eq!(delivery_id, "d-ack");
            assert!(relay.is_acked());
            assert_eq!(bookkeeping_error, None);
        }
        other => panic!("expected Accepted, got {other:?}"),
    }
    assert!(
        listing_of(ingress.spool()).pending.is_empty(),
        "an acknowledged delivery is the only one that may be deleted"
    );
}

#[tokio::test]
async fn relay_failure_leaves_a_pending_entry_with_an_incremented_attempt_count() {
    // 🔴 The second regression. The pre-fix handlers drop the payload here.
    let tmp = tempfile::tempdir().expect("tempdir");
    let spool = Spool::open(tmp.path().join("spool")).expect("open spool");
    // No listener: the state every delivery is in until #5089 step 4.
    let ingress = ingress_for(spool, tmp.path().join("sockets").join("absent.sock"));

    let outcome = ingress
        .ingest("review", &signed_headers(BODY, "d-pending"), BODY)
        .await;

    match &outcome {
        IngestOutcome::Accepted { relay, .. } => assert!(
            matches!(relay, RelayOutcome::Unreachable { .. }),
            "expected Unreachable, got {relay:?}"
        ),
        other => panic!("expected Accepted, got {other:?}"),
    }

    let listing = listing_of(ingress.spool());
    assert_eq!(
        listing.pending.len(),
        1,
        "a failed relay must NEVER delete the entry"
    );
    let entry = &listing.pending[0].entry;
    assert_eq!(entry.delivery_id, "d-pending");
    assert_eq!(entry.attempts, 1, "the attempt must be recorded durably");
    assert!(entry.last_error.is_some(), "and so must the reason");
    assert!(entry.last_attempt_at_unix_ms.is_some());
    assert_eq!(
        BASE64.decode(&entry.body_b64).expect("decode"),
        BODY,
        "the pending entry must still hold the original body for redelivery"
    );
}

#[tokio::test]
async fn connected_without_ack_never_deletes_the_entry() {
    // 🔴 The third arm: the connection succeeded, so a naive implementation
    // would call this done. ADR-0034 §2 makes the explicit ack the only
    // delete trigger.
    let tmp = tempfile::tempdir().expect("tempdir");
    let spool = Spool::open(tmp.path().join("spool")).expect("open spool");
    let (socket, captured) = spawn_target(tmp.path(), StubTarget::ResultWithoutAck, 1);
    let ingress = ingress_for(spool, socket);

    let outcome = ingress
        .ingest("review", &signed_headers(BODY, "d-noack"), BODY)
        .await;
    assert!(matches!(outcome, IngestOutcome::Accepted { .. }));
    assert_eq!(
        captured.lock().await.len(),
        1,
        "the target really was reached"
    );

    let listing = listing_of(ingress.spool());
    assert_eq!(
        listing.pending.len(),
        1,
        "reaching the target is not the same as the target acknowledging"
    );
    assert_eq!(listing.pending[0].entry.attempts, 1);
}

// ─── health: oldest-pending age is red, on demand ───────────────────────────

#[test]
fn health_reports_ok_on_an_empty_spool() {
    let tmp = tempfile::tempdir().expect("tempdir");
    let spool = Spool::open(tmp.path().join("spool")).expect("open spool");
    let h = health::scan_health(&spool, 1_700_000_000_000, Duration::from_secs(600), &[]);
    assert_eq!(h.status, ServiceHealth::Ok);
    assert_eq!(h.pending, 0);
    assert_eq!(h.oldest_pending_age_secs, None);
    assert_eq!(h.scan_error, None);
}

#[test]
fn health_reports_degraded_for_a_young_pending_entry() {
    let tmp = tempfile::tempdir().expect("tempdir");
    let spool = Spool::open(tmp.path().join("spool")).expect("open spool");
    let now = 1_700_000_000_000u64;
    spool
        .persist_new(&sample_entry("d-young", now - 30_000))
        .expect("durable write");

    let h = health::scan_health(&spool, now, Duration::from_secs(600), &[]);
    assert_eq!(h.status, ServiceHealth::Degraded);
    assert_eq!(h.pending, 1);
    assert_eq!(h.oldest_pending_age_secs, Some(30));
}

#[test]
fn health_reports_error_once_the_oldest_entry_passes_the_threshold() {
    // 🔴 The fourth arm. Red is a state a dashboard renders, not a warn! line.
    let tmp = tempfile::tempdir().expect("tempdir");
    let spool = Spool::open(tmp.path().join("spool")).expect("open spool");
    let now = 1_700_000_000_000u64;
    let mut aged = sample_entry("d-aged", now - 900_000); // 15 minutes
    aged.attempts = 14;
    aged.last_error = Some("no listener bound".to_string());
    spool.persist_new(&aged).expect("durable write");
    spool
        .persist_new(&sample_entry("d-young", now - 1_000))
        .expect("durable write");

    let h = health::scan_health(&spool, now, Duration::from_secs(600), &[]);
    assert_eq!(h.status, ServiceHealth::Error);
    assert_eq!(h.pending, 2);
    assert_eq!(h.oldest_pending_age_secs, Some(900));
    assert_eq!(h.oldest_pending_delivery_id.as_deref(), Some("d-aged"));
    assert_eq!(
        h.oldest_pending_last_error.as_deref(),
        Some("no listener bound")
    );
    assert_eq!(h.oldest_pending_attempts, Some(14));
    assert_eq!(h.exhausted, 0, "nothing has been given up on yet");
}

#[test]
fn health_reports_error_for_an_undecodable_entry() {
    let tmp = tempfile::tempdir().expect("tempdir");
    let spool = Spool::open(tmp.path().join("spool")).expect("open spool");
    std::fs::write(spool.root().join("0000000000001-junk.json"), b"{not json").expect("write junk");

    let h = health::scan_health(&spool, 1_700_000_000_000, Duration::from_secs(600), &[]);
    assert_eq!(
        h.status,
        ServiceHealth::Error,
        "a pending delivery that cannot be read is not a healthy spool"
    );
    assert_eq!(h.undecodable.len(), 1);
}

#[test]
fn health_reports_error_when_the_spool_cannot_be_read() {
    // A spool that cannot be listed must not read as an empty (healthy) one —
    // that is the fail-quiet shape one level down from the one being fixed.
    let tmp = tempfile::tempdir().expect("tempdir");
    let blocked = tmp.path().join("not-a-dir");
    std::fs::write(&blocked, b"blocking file").expect("write blocker");

    let h = health::scan_health(
        &Spool::at(&blocked),
        1_700_000_000_000,
        Duration::from_secs(600),
        &[],
    );
    assert_eq!(h.status, ServiceHealth::Error);
    assert!(h.scan_error.is_some(), "the scan failure must be reported");
}

// ─── retry sweep ────────────────────────────────────────────────────────────

#[tokio::test]
async fn retry_sweep_acks_and_clears_a_pending_entry() {
    let tmp = tempfile::tempdir().expect("tempdir");
    let spool = Spool::open(tmp.path().join("spool")).expect("open spool");
    let (socket, _) = spawn_target(tmp.path(), StubTarget::Ack, 1);
    let ingress = ingress_for(spool, socket);
    ingress
        .spool()
        .persist_new(&sample_entry("d-1", 1_700_000_000_000))
        .expect("durable write");

    let report = ingress.retry_pending_once().await;
    assert_eq!(report.acked, 1);
    assert_eq!(report.still_pending, 0);
    assert!(listing_of(ingress.spool()).pending.is_empty());
}

#[tokio::test]
async fn retry_sweep_leaves_an_unrelayable_entry_pending_with_more_attempts() {
    let tmp = tempfile::tempdir().expect("tempdir");
    let spool = Spool::open(tmp.path().join("spool")).expect("open spool");
    let ingress = ingress_for(spool, tmp.path().join("sockets").join("absent.sock"));
    ingress
        .spool()
        .persist_new(&sample_entry("d-1", 1_700_000_000_000))
        .expect("durable write");

    for expected in 1..=3u32 {
        let report = ingress.retry_pending_once().await;
        assert_eq!(report.still_pending, 1);
        assert_eq!(report.acked, 0);
        let listing = listing_of(ingress.spool());
        assert_eq!(
            listing.pending.len(),
            1,
            "the entry must survive every sweep"
        );
        assert_eq!(listing.pending[0].entry.attempts, expected);
    }
}

#[tokio::test]
async fn retry_sweep_reports_an_orphaned_entry_without_deleting_it() {
    let tmp = tempfile::tempdir().expect("tempdir");
    let spool = Spool::open(tmp.path().join("spool")).expect("open spool");
    let ingress = ingress_for(spool, tmp.path().join("sockets").join("absent.sock"));
    let mut orphan = sample_entry("d-orphan", 1_700_000_000_000);
    orphan.source = "retired-target".to_string();
    ingress.spool().persist_new(&orphan).expect("durable write");

    let report = ingress.retry_pending_once().await;
    assert_eq!(report.orphaned, 1);
    assert_eq!(
        listing_of(ingress.spool()).pending.len(),
        1,
        "a config change must not silently discard a delivery"
    );
}

// ─── HTTP surface ───────────────────────────────────────────────────────────

/// A router carrying only the webhook sub-surface plus the real console routes.
fn router_for(ingress: WebhookIngress) -> axum::Router {
    crate::server::build_router_with_webhooks(
        crate::server::AppState::new(Vec::new()),
        crate::routes::origin_guard::SelfOrigins::default(),
        ingress,
    )
}

async fn post_webhook(
    router: axum::Router,
    source: &str,
    body: &[u8],
    headers: HeaderMap,
) -> (StatusCode, serde_json::Value) {
    let mut req = Request::builder()
        .method("POST")
        .uri(format!("/api/webhooks/{source}"))
        .header("content-type", "application/json");
    for (name, value) in headers.iter() {
        req = req.header(name, value);
    }
    let response = router
        .oneshot(req.body(Body::from(body.to_vec())).expect("build request"))
        .await
        .expect("route the request");
    let status = response.status();
    let bytes = axum::body::to_bytes(response.into_body(), 64 * 1024)
        .await
        .expect("read body");
    let json = serde_json::from_slice(&bytes).unwrap_or(serde_json::Value::Null);
    (status, json)
}

#[tokio::test]
async fn route_returns_202_after_a_durable_write() {
    let tmp = tempfile::tempdir().expect("tempdir");
    let spool = Spool::open(tmp.path().join("spool")).expect("open spool");
    let ingress = ingress_for(spool, tmp.path().join("sockets").join("absent.sock"));

    let (status, body) = post_webhook(
        router_for(ingress.clone()),
        "review",
        BODY,
        signed_headers(BODY, "d-http"),
    )
    .await;

    assert_eq!(status, StatusCode::ACCEPTED);
    assert_eq!(body["status"], "accepted");
    assert_eq!(
        body["relay"], "pending",
        "the ack reports the relay state honestly rather than claiming success"
    );
    assert_eq!(listing_of(ingress.spool()).pending.len(), 1);
}

#[tokio::test]
async fn route_returns_500_and_no_ack_when_the_spool_write_fails() {
    // 🔴 GitHub must see a failed delivery it can redeliver from its UI.
    let tmp = tempfile::tempdir().expect("tempdir");
    let blocked = tmp.path().join("not-a-dir");
    std::fs::write(&blocked, b"blocking file").expect("write blocker");
    let ingress = ingress_for(
        Spool::at(&blocked),
        tmp.path().join("sockets").join("absent.sock"),
    );

    let (status, body) = post_webhook(
        router_for(ingress),
        "review",
        BODY,
        signed_headers(BODY, "d-http"),
    )
    .await;

    assert_eq!(
        status,
        StatusCode::INTERNAL_SERVER_ERROR,
        "a 2xx here would make the delivery permanently unrecoverable"
    );
    assert!(!status.is_success());
    assert_eq!(body["status"], serde_json::Value::Null);
}

#[tokio::test]
async fn route_returns_401_for_an_unset_secret() {
    let tmp = tempfile::tempdir().expect("tempdir");
    let spool = Spool::open(tmp.path().join("spool")).expect("open spool");
    let ingress = WebhookIngress::new(
        spool,
        String::new(),
        SECRET_ENV.to_string(),
        vec![Target {
            source: "review".to_string(),
            relay: UdsRelay::new(tmp.path().join("sockets").join("absent.sock")),
        }],
    );

    let (status, _) = post_webhook(
        router_for(ingress.clone()),
        "review",
        BODY,
        signed_headers(BODY, "d-http"),
    )
    .await;
    assert_eq!(status, StatusCode::UNAUTHORIZED);
    assert!(listing_of(ingress.spool()).pending.is_empty());
}

#[tokio::test]
async fn route_returns_401_for_a_forged_signature() {
    let tmp = tempfile::tempdir().expect("tempdir");
    let spool = Spool::open(tmp.path().join("spool")).expect("open spool");
    let ingress = ingress_for(spool, tmp.path().join("sockets").join("absent.sock"));

    let mut headers = HeaderMap::new();
    headers.insert(SIGNATURE_HEADER, "sha256=deadbeef".parse().expect("sig"));
    let (status, _) = post_webhook(router_for(ingress.clone()), "review", BODY, headers).await;
    assert_eq!(status, StatusCode::UNAUTHORIZED);
    assert!(listing_of(ingress.spool()).pending.is_empty());
}

#[tokio::test]
async fn route_returns_404_for_an_unknown_source() {
    let tmp = tempfile::tempdir().expect("tempdir");
    let spool = Spool::open(tmp.path().join("spool")).expect("open spool");
    let ingress = ingress_for(spool, tmp.path().join("sockets").join("absent.sock"));

    let (status, _) = post_webhook(
        router_for(ingress),
        "not-a-target",
        BODY,
        signed_headers(BODY, "d-http"),
    )
    .await;
    assert_eq!(status, StatusCode::NOT_FOUND);
}

#[tokio::test]
async fn webhook_route_is_not_shadowed_by_the_service_proxy() {
    // `/api/{service}/{*path}` would swallow `/api/webhooks/review` if matchit
    // preferred the capture. It does not — but a router edit could reorder
    // things, and a proxied webhook is a silently dropped one.
    let tmp = tempfile::tempdir().expect("tempdir");
    let spool = Spool::open(tmp.path().join("spool")).expect("open spool");
    let ingress = ingress_for(spool, tmp.path().join("sockets").join("absent.sock"));

    let (status, body) = post_webhook(
        router_for(ingress),
        "review",
        BODY,
        signed_headers(BODY, "d-http"),
    )
    .await;
    assert_eq!(status, StatusCode::ACCEPTED);
    assert_eq!(body["delivery_id"], "d-http");
}

async fn get_json(router: axum::Router, uri: &str) -> (StatusCode, serde_json::Value) {
    let response = router
        .oneshot(
            Request::builder()
                .uri(uri)
                .body(Body::empty())
                .expect("build request"),
        )
        .await
        .expect("route the request");
    let status = response.status();
    let bytes = axum::body::to_bytes(response.into_body(), 256 * 1024)
        .await
        .expect("read body");
    (
        status,
        serde_json::from_slice(&bytes).unwrap_or(serde_json::Value::Null),
    )
}

#[tokio::test]
async fn metrics_route_reports_ok_on_an_empty_spool() {
    let tmp = tempfile::tempdir().expect("tempdir");
    let spool = Spool::open(tmp.path().join("spool")).expect("open spool");
    let ingress = ingress_for(spool, tmp.path().join("sockets").join("absent.sock"));

    let (status, body) = get_json(router_for(ingress), "/api/console/metrics/webhooks").await;
    assert_eq!(status, StatusCode::OK);
    assert_eq!(body["service_id"], health::WEBHOOK_SERVICE_ID);
    assert_eq!(body["status"], "ok");
    assert_eq!(body["metrics"]["pending"], 0);
}

#[tokio::test]
async fn metrics_route_reports_red_for_an_aged_pending_entry() {
    // 🔴 The detection arm, end to end: the route scans the spool on THIS
    // request, so the red state does not depend on a background loop still
    // being alive.
    let tmp = tempfile::tempdir().expect("tempdir");
    let spool = Spool::open(tmp.path().join("spool")).expect("open spool");
    let ingress = ingress_for(spool, tmp.path().join("sockets").join("absent.sock"))
        // Anything already on disk is instantly past a zero threshold.
        .with_red_after(Duration::from_secs(0));
    ingress
        .spool()
        .persist_new(&sample_entry("d-stuck", 1))
        .expect("durable write");

    let (status, body) = get_json(router_for(ingress), "/api/console/metrics/webhooks").await;
    assert_eq!(status, StatusCode::OK, "a red state is data, not an outage");
    assert_eq!(body["status"], "error");
    assert_eq!(body["metrics"]["pending"], 1);
    assert_eq!(body["metrics"]["oldest_pending_delivery_id"], "d-stuck");
    assert!(body["metrics"]["oldest_pending_age_secs"].is_number());
}

#[tokio::test]
async fn metrics_route_reports_red_when_the_spool_cannot_be_read() {
    let tmp = tempfile::tempdir().expect("tempdir");
    let blocked = tmp.path().join("not-a-dir");
    std::fs::write(&blocked, b"blocking file").expect("write blocker");
    let ingress = ingress_for(
        Spool::at(&blocked),
        tmp.path().join("sockets").join("absent.sock"),
    );

    let (status, body) = get_json(router_for(ingress), "/api/console/metrics/webhooks").await;
    assert_eq!(status, StatusCode::OK);
    assert_eq!(body["status"], "error");
    assert!(body["metrics"]["scan_error"].is_string());
}

// ─── integration (`--include-ignored`) ──────────────────────────────────────

#[test]
fn default_spool_root_lives_under_the_console_data_dir() {
    // Pure assertion on the path convention — ADR-0034 puts the spool under
    // console's own state directory, which `lib.rs:476` already treats as
    // canonical. No env mutation, so this one runs in the default suite.
    let root = default_spool_root().expect("resolve the default spool root");
    assert!(
        root.ends_with(std::path::Path::new(spool::SPOOL_DIR_NAME)),
        "spool root {} must be the webhook-spool subdirectory",
        root.display()
    );
    let data_dir = trusty_common::resolve_data_dir("trusty-console").expect("resolve data dir");
    assert_eq!(
        root.parent(),
        Some(data_dir.as_path()),
        "the spool must not invent a new location convention"
    );
}

/// Serialises the tests below, which mutate process-global environment
/// variables that every concurrently-running sibling test can observe.
/// An async mutex, not a `std` one: the guard is held across `.await` for the
/// whole body, which is exactly what a blocking guard must not do.
static ENV_LOCK: tokio::sync::Mutex<()> = tokio::sync::Mutex::const_new(());

/// Set `key` to `value` (or remove it), returning the prior value.
///
/// `set_var`/`remove_var` are `unsafe` in edition 2024 because they are not
/// thread-safe; [`ENV_LOCK`] plus the `#[ignore]` tag is what makes these call
/// sites sound in this binary.
fn swap_env(key: &str, value: Option<&str>) -> Option<String> {
    let prior = std::env::var(key).ok();
    match value {
        Some(v) => unsafe { std::env::set_var(key, v) },
        None => unsafe { std::env::remove_var(key) },
    }
    prior
}

#[tokio::test]
#[ignore = "mutates TRUSTY_DATA_DIR_OVERRIDE and GITHUB_WEBHOOK_SECRET, which every \
            concurrently-running test in this binary can observe; run with --include-ignored"]
async fn integration_from_env_delivery_survives_a_console_restart() {
    // The full ADR-0034 §2 loop, against the real `from_env` wiring:
    //   verified delivery -> durable spool -> relay fails (no listener yet)
    //   -> a FRESH ingress over the same directory still finds it pending
    //   -> the target comes up and acks -> the entry is gone.
    // This is the property the pre-fix handlers lack entirely: after their
    // 202, nothing on disk records that the delivery ever existed.
    let _guard = ENV_LOCK.lock().await;
    let tmp = tempfile::tempdir().expect("tempdir");

    let prior_data_dir = swap_env(
        trusty_common::DATA_DIR_OVERRIDE_ENV,
        Some(&tmp.path().to_string_lossy()),
    );
    let prior_secret = swap_env(SECRET_ENV, Some(SECRET));
    // #5182: `from_env` now attaches a supervisor that STARTS the target, so
    // without this the test launches a real `trusty-review` from the developer's
    // PATH, which promptly acks and clears the spool the test is asserting on.
    // External mode is the operator-facing opt-out (ADR-0011): dial whatever is
    // there, never spawn.
    let prior_external = swap_env(spawn::ENV_EXTERNAL_TARGETS, Some("1"));

    let outcome = async {
        // The remaining nondeterminism is a webhook listener the operator is
        // already running on this machine. Name it rather than flake on it.
        let live_socket = trusty_common::webhook_relay::review_socket_path();
        assert!(
            tokio::net::UnixStream::connect(&live_socket).await.is_err(),
            "a trusty-review webhook listener is already serving {};              stop it before running this test, which asserts on an unreachable target",
            live_socket.display()
        );

        // ── first boot: nothing is listening ────────────────────────────────
        let ingress = WebhookIngress::from_env().expect("build ingress from env");
        assert!(
            ingress
                .spool()
                .root()
                .starts_with(tmp.path().join("trusty-console")),
            "spool must land under the overridden console data dir: {}",
            ingress.spool().root().display()
        );

        let (status, body) = post_webhook(
            router_for(ingress.clone()),
            "review",
            BODY,
            signed_headers(BODY, "d-restart"),
        )
        .await;
        assert_eq!(status, StatusCode::ACCEPTED);
        assert_eq!(body["relay"], "pending");

        let health = ingress.health().await;
        assert_eq!(health.pending, 1);
        assert_eq!(health.status, ServiceHealth::Degraded);

        // ── restart: a brand-new ingress over the same directory ─────────────
        let restarted = WebhookIngress::from_env().expect("rebuild ingress from env");
        let listing = listing_of(restarted.spool());
        assert_eq!(
            listing.pending.len(),
            1,
            "the delivery must outlive the process that accepted it"
        );
        assert_eq!(listing.pending[0].entry.delivery_id, "d-restart");
        assert_eq!(listing.pending[0].entry.attempts, 1);
        assert_eq!(
            BASE64
                .decode(&listing.pending[0].entry.body_b64)
                .expect("decode"),
            BODY
        );

        // ── the target finally comes up and acknowledges ─────────────────────
        let (socket, captured) = spawn_target(tmp.path(), StubTarget::Ack, 1);
        let with_target = ingress_for(Spool::at(restarted.spool().root().to_path_buf()), socket);
        let report = with_target.retry_pending_once().await;
        assert_eq!(report.acked, 1);
        assert_eq!(report.still_pending, 0);
        assert_eq!(captured.lock().await.len(), 1);

        let final_health = with_target.health().await;
        assert_eq!(final_health.pending, 0);
        assert_eq!(final_health.status, ServiceHealth::Ok);
    }
    .await;

    swap_env(
        trusty_common::DATA_DIR_OVERRIDE_ENV,
        prior_data_dir.as_deref(),
    );
    swap_env(SECRET_ENV, prior_secret.as_deref());
    swap_env(spawn::ENV_EXTERNAL_TARGETS, prior_external.as_deref());
    outcome
}

#[tokio::test]
#[ignore = "mutates TRUSTY_DATA_DIR_OVERRIDE and GITHUB_WEBHOOK_SECRET; \
            run with --include-ignored"]
async fn integration_from_env_fails_closed_when_the_secret_is_unset() {
    // ADR-0034 §2's unified policy, against the real env wiring: an operator
    // who never set the secret gets a 401 and an empty spool, not
    // trusty-analyze's "skip verification and process it anyway".
    let _guard = ENV_LOCK.lock().await;
    let tmp = tempfile::tempdir().expect("tempdir");

    let prior_data_dir = swap_env(
        trusty_common::DATA_DIR_OVERRIDE_ENV,
        Some(&tmp.path().to_string_lossy()),
    );
    let prior_secret = swap_env(SECRET_ENV, None);

    let outcome = async {
        let ingress = WebhookIngress::from_env().expect("build ingress from env");
        let (status, _) = post_webhook(
            router_for(ingress.clone()),
            "review",
            BODY,
            signed_headers(BODY, "d-nosecret"),
        )
        .await;
        assert_eq!(status, StatusCode::UNAUTHORIZED);
        assert!(listing_of(ingress.spool()).pending.is_empty());
        assert_eq!(ingress.health().await.status, ServiceHealth::Ok);
    }
    .await;

    swap_env(
        trusty_common::DATA_DIR_OVERRIDE_ENV,
        prior_data_dir.as_deref(),
    );
    swap_env(SECRET_ENV, prior_secret.as_deref());
    outcome
}

// ─── concurrency: one delivery, one relay ───────────────────────────────────

#[tokio::test]
async fn sweep_does_not_relay_an_entry_the_request_path_is_still_relaying() {
    // 🔴 Two defects met here. The claim used to be taken AFTER the durable
    // write, leaving a window in which the entry was on disk and unclaimed —
    // and a sweep landing in it relayed a delivery `ingest` was about to relay
    // itself. Measured at the pre-fix head: a sweep 20 ms into `ingest`
    // reported `acked: 1`, two relays for one delivery.
    //
    // The test itself was also racy: it slept 150 ms and assumed `ingest` had
    // reached its claim. Under CI scheduling it had not even persisted yet, so
    // the sweep listed an empty spool. The rendezvous below removes the wall
    // clock — the target holding the frame is proof the relay is in flight.
    let tmp = tempfile::tempdir().expect("tempdir");
    let spool = Spool::open(tmp.path().join("spool")).expect("open spool");
    let (socket, captured, received, release) = spawn_gated_target(tmp.path());
    let ingress = ingress_for(spool, socket);

    let requester = {
        let ingress = ingress.clone();
        tokio::spawn(async move {
            ingress
                .ingest("review", &signed_headers(BODY, "d-race"), BODY)
                .await
        })
    };

    // The target has the frame, so the relay is in flight and the claim is
    // held. No sleep, no guess.
    received
        .await
        .expect("the target received the relayed frame");

    let sweep = ingress.retry_pending_once().await;
    assert_eq!(
        sweep.in_flight, 1,
        "the sweep must see the entry as claimed, not free to relay: {sweep:?}"
    );
    assert_eq!(sweep.acked, 0, "and must not relay it: {sweep:?}");
    assert_eq!(sweep.still_pending, 0, "{sweep:?}");

    release.send(()).expect("release the target");
    match requester.await.expect("ingest task") {
        IngestOutcome::Accepted { relay, .. } => assert!(relay.is_acked()),
        other => panic!("expected Accepted, got {other:?}"),
    }

    assert_eq!(
        captured.lock().await.len(),
        1,
        "one delivery must produce exactly one relay"
    );
    assert!(listing_of(ingress.spool()).pending.is_empty());

    // The claim must be released once the relay settles, not leaked.
    let after = ingress.retry_pending_once().await;
    assert_eq!(after.in_flight, 0, "the claim must not outlive the relay");
}

#[tokio::test]
async fn two_concurrent_sweeps_relay_each_entry_only_once() {
    let tmp = tempfile::tempdir().expect("tempdir");
    let spool = Spool::open(tmp.path().join("spool")).expect("open spool");
    let (socket, captured) = spawn_target(
        tmp.path(),
        StubTarget::AckAfter(Duration::from_millis(400)),
        6,
    );
    let ingress = ingress_for(spool, socket);
    for id in ["d-a", "d-b"] {
        ingress
            .spool()
            .persist_new(&sample_entry(id, 1_700_000_000_000))
            .expect("durable write");
    }

    let (left, right) = tokio::join!(
        {
            let i = ingress.clone();
            async move { i.retry_pending_once().await }
        },
        {
            let i = ingress.clone();
            async move { i.retry_pending_once().await }
        }
    );

    assert_eq!(
        left.acked + right.acked,
        2,
        "both entries must be acknowledged exactly once between the two passes"
    );
    assert_eq!(
        captured.lock().await.len(),
        2,
        "two entries must produce exactly two relays across two overlapping sweeps"
    );
    assert!(listing_of(ingress.spool()).pending.is_empty());
}

#[tokio::test]
async fn two_concurrent_deliveries_are_each_spooled_and_relayed_once() {
    let tmp = tempfile::tempdir().expect("tempdir");
    let spool = Spool::open(tmp.path().join("spool")).expect("open spool");
    let (socket, captured) = spawn_target(
        tmp.path(),
        StubTarget::AckAfter(Duration::from_millis(200)),
        6,
    );
    let ingress = ingress_for(spool, socket);

    let a = {
        let i = ingress.clone();
        tokio::spawn(async move {
            i.ingest("review", &signed_headers(BODY, "d-one"), BODY)
                .await
        })
    };
    let b = {
        let i = ingress.clone();
        tokio::spawn(async move {
            i.ingest("review", &signed_headers(BODY, "d-two"), BODY)
                .await
        })
    };

    for handle in [a, b] {
        match handle.await.expect("ingest task") {
            IngestOutcome::Accepted { relay, .. } => assert!(relay.is_acked()),
            other => panic!("expected Accepted, got {other:?}"),
        }
    }
    assert_eq!(captured.lock().await.len(), 2);
    assert!(listing_of(ingress.spool()).pending.is_empty());
}

#[test]
fn claim_set_refuses_a_second_claim_on_the_same_path() {
    let claims = schedule::ClaimSet::new();
    let path = std::path::Path::new("/tmp/spool/entry.json");
    let first = claims.claim(path).expect("first claim");
    assert!(
        claims.claim(path).is_none(),
        "a claimed entry must not be claimable twice"
    );
    assert_eq!(claims.len(), 1);
    drop(first);
    assert!(
        claims.claim(path).is_some(),
        "the claim must be released on drop"
    );
}

#[test]
fn claim_set_releases_on_drop_even_when_the_holder_panics() {
    // A relay that panics must not leave its entry permanently unrelayable.
    let claims = schedule::ClaimSet::new();
    let path = std::path::Path::new("/tmp/spool/entry.json");
    let result = std::panic::catch_unwind({
        let claims = claims.clone();
        move || {
            let _held = claims.claim(path).expect("claim");
            panic!("relay blew up");
        }
    });
    assert!(result.is_err());
    assert!(claims.is_empty(), "the claim must not outlive the panic");
    assert!(claims.claim(path).is_some());
}

// ─── backoff ────────────────────────────────────────────────────────────────

#[test]
fn backoff_holds_off_a_freshly_spooled_entry() {
    // The request path may still be relaying it; the sweep must not race in.
    let policy = BackoffPolicy::default();
    let now = 1_700_000_000_000u64;
    let entry = sample_entry("d-1", now);
    assert!(!policy.is_due(&entry, now));
    assert!(!policy.is_due(&entry, now + 4_000));
    assert!(policy.is_due(&entry, now + policy.first_attempt_grace.as_millis() as u64));
}

#[test]
fn backoff_spacing_grows_with_attempts() {
    let policy = BackoffPolicy::default();
    assert_eq!(policy.delay_after(1), Duration::from_secs(30));
    assert_eq!(policy.delay_after(2), Duration::from_secs(60));
    assert_eq!(policy.delay_after(3), Duration::from_secs(120));
    assert_eq!(policy.delay_after(4), Duration::from_secs(240));
}

#[test]
fn backoff_respects_the_ceiling() {
    let policy = BackoffPolicy::default();
    for attempts in [10u32, 20, 1_000, u32::MAX - 1] {
        assert_eq!(
            policy.delay_after(attempts),
            policy.ceiling,
            "attempts={attempts} must clamp to the ceiling, never wrap to a short delay"
        );
    }
}

#[test]
fn backoff_admits_an_entry_past_its_delay() {
    let policy = BackoffPolicy::default();
    let now = 1_700_000_000_000u64;
    let mut entry = sample_entry("d-1", now - 1_000_000);
    entry.attempts = 2;
    entry.last_attempt_at_unix_ms = Some(now - 59_000);
    assert!(
        !policy.is_due(&entry, now),
        "59s < the 60s delay for attempt 2"
    );
    entry.last_attempt_at_unix_ms = Some(now - 61_000);
    assert!(policy.is_due(&entry, now));
}

#[test]
fn backoff_stops_at_max_attempts() {
    // An exhausted entry is never relayed again — and never deleted. It holds
    // the health signal red until an operator intervenes, which is the honest
    // state for an undeliverable webhook.
    let policy = BackoffPolicy::default();
    let now = 1_700_000_000_000u64;
    let mut entry = sample_entry("d-1", 1);
    entry.attempts = policy.max_attempts;
    entry.last_attempt_at_unix_ms = Some(1);
    assert!(policy.is_exhausted(&entry));
    assert!(!policy.is_due(&entry, now), "no elapsed time can revive it");
}

#[tokio::test]
async fn sweep_honours_backoff_between_ticks() {
    // 🔴 Fails against the pre-fix head, which relayed every pending entry on
    // every tick and rewrote its whole body plus two fsyncs each time.
    let tmp = tempfile::tempdir().expect("tempdir");
    let spool = Spool::open(tmp.path().join("spool")).expect("open spool");
    // Real schedule, but no first-attempt grace so the first pass runs at once.
    let policy = BackoffPolicy {
        first_attempt_grace: Duration::ZERO,
        ..BackoffPolicy::default()
    };
    let ingress = ingress_with(
        spool,
        tmp.path().join("sockets").join("absent.sock"),
        Duration::from_millis(300),
    )
    .with_backoff(policy);
    ingress
        .spool()
        .persist_new(&sample_entry("d-1", 1_700_000_000_000))
        .expect("durable write");

    let first = ingress.retry_pending_once().await;
    assert_eq!(first.still_pending, 1, "the first pass relays it");

    let second = ingress.retry_pending_once().await;
    assert_eq!(
        second.not_due, 1,
        "the second pass must respect the 30s spacing, not relay again"
    );
    assert_eq!(second.still_pending, 0);

    let listing = listing_of(ingress.spool());
    assert_eq!(
        listing.pending.len(),
        1,
        "and it is still pending, not lost"
    );
    assert_eq!(
        listing.pending[0].entry.attempts, 1,
        "a skipped pass must not bump the attempt count or rewrite the body"
    );
}

#[tokio::test]
async fn sweep_stops_relaying_an_exhausted_entry() {
    let tmp = tempfile::tempdir().expect("tempdir");
    let spool = Spool::open(tmp.path().join("spool")).expect("open spool");
    let policy = BackoffPolicy {
        first_attempt_grace: Duration::ZERO,
        base: Duration::ZERO,
        ceiling: Duration::ZERO,
        max_attempts: 3,
    };
    let ingress = ingress_with(
        spool,
        tmp.path().join("sockets").join("absent.sock"),
        Duration::from_millis(300),
    )
    .with_backoff(policy);
    ingress
        .spool()
        .persist_new(&sample_entry("d-1", 1_700_000_000_000))
        .expect("durable write");

    for _ in 0..3 {
        assert_eq!(ingress.retry_pending_once().await.still_pending, 1);
    }
    let report = ingress.retry_pending_once().await;
    assert_eq!(report.exhausted, 1);
    assert_eq!(report.still_pending, 0);

    // 🔴 Updated in review round 2. The previous version asserted the exhausted
    // entry STAYS in the live set, which is what let it be re-read and
    // re-decoded by every later sweep and metrics request forever. It is now
    // moved aside — kept, because it is still an unacknowledged webhook, but off
    // both hot paths.
    assert!(
        listing_of(ingress.spool()).pending.is_empty(),
        "an exhausted entry must leave the live set"
    );
    let census = ingress.spool().scan_metadata().expect("census");
    assert_eq!(census.live.len(), 0);
    assert_eq!(
        census.exhausted.len(),
        1,
        "giving up on relaying is not the same as discarding the delivery"
    );
    assert_eq!(census.exhausted[0].delivery_id, "d-1");

    let quarantined = ingress
        .spool()
        .load(&census.exhausted[0].path)
        .expect("the quarantined entry is still readable");
    assert_eq!(quarantined.attempts, 3);
    assert_eq!(
        BASE64.decode(&quarantined.body_b64).expect("decode"),
        BODY,
        "and it still holds the original body for a manual redelivery"
    );

    let health = ingress.health().await;
    assert_eq!(
        health.status,
        ServiceHealth::Error,
        "an entry we have given up relaying must hold the health signal red"
    );
    assert_eq!(health.exhausted, 1);
    assert_eq!(health.exhausted_delivery_ids, vec!["d-1".to_string()]);
    assert_eq!(health.pending, 0);
}

// ─── HIGH-A: exhausted entries leave the hot paths ──────────────────────────

#[test]
fn spool_quarantine_moves_an_entry_out_of_the_live_set() {
    let tmp = tempfile::tempdir().expect("tempdir");
    let spool = Spool::open(tmp.path().join("spool")).expect("open spool");
    let entry = sample_entry("d-quarantine", 1_700_000_000_000);
    let path = spool.persist_new(&entry).expect("durable write");

    let moved = spool.quarantine(&path).expect("quarantine");
    assert!(!path.exists(), "the entry must leave the live directory");
    assert!(moved.starts_with(spool.exhausted_root()));
    assert_eq!(
        spool.load(&moved).expect("still readable"),
        entry,
        "quarantine moves the delivery, it does not alter or discard it"
    );
    assert!(listing_of(&spool).pending.is_empty());
}

#[test]
fn spool_list_pending_ignores_the_exhausted_subdirectory() {
    // 🔴 Fails against `92c1ed3fc`, where nothing ever left the live set: both
    // `retry_pending_once` and `scan_health` called `list_pending`, which reads
    // and JSON-decodes every file. An entry that can never be relayed again was
    // paid for on every sweep tick and every metrics request, forever.
    let tmp = tempfile::tempdir().expect("tempdir");
    let spool = Spool::open(tmp.path().join("spool")).expect("open spool");
    let live = spool
        .persist_new(&sample_entry("d-live", 1_700_000_005_000))
        .expect("durable write");
    let dead = spool
        .persist_new(&sample_entry("d-dead", 1_700_000_000_000))
        .expect("durable write");
    spool.quarantine(&dead).expect("quarantine");

    let listing = listing_of(&spool);
    assert_eq!(
        listing.pending.len(),
        1,
        "the decode-every-file path must see only the live entry"
    );
    assert_eq!(listing.pending[0].path, live);
    assert!(listing.undecodable.is_empty());
}

#[test]
fn spool_scan_metadata_avoids_decoding_and_load_reads_one() {
    // `entry_path`'s stated rationale — the timestamp prefix exists so the age
    // scan "does not have to parse every file" — is only true if something
    // actually reads it. This is that reader.
    let tmp = tempfile::tempdir().expect("tempdir");
    let spool = Spool::open(tmp.path().join("spool")).expect("open spool");
    for (id, at) in [("d-c", 300u64), ("d-a", 100), ("d-b", 200)] {
        spool
            .persist_new(&sample_entry(id, at))
            .expect("durable write");
    }
    // Corrupt every file. A census that opened them would notice; one that
    // reads names only cannot, which is exactly the property under test.
    for dirent in std::fs::read_dir(spool.root()).expect("read spool") {
        let path = dirent.expect("dirent").path();
        if path.extension().and_then(|e| e.to_str()) == Some("json") {
            std::fs::write(&path, b"{ not json").expect("corrupt");
        }
    }

    let census = spool.scan_metadata().expect("census");
    assert_eq!(
        census
            .live
            .iter()
            .map(|m| m.delivery_id.as_str())
            .collect::<Vec<_>>(),
        vec!["d-a", "d-b", "d-c"],
        "the census must read names only, oldest first"
    );
    assert_eq!(census.live[0].received_at_unix_ms, 100);
    assert!(census.unparsable.is_empty());

    // `load` is the one place bytes are read, and it reports the corruption.
    assert!(spool.load(&census.live[0].path).is_err());
}

#[test]
fn spool_scan_metadata_separates_live_from_exhausted() {
    let tmp = tempfile::tempdir().expect("tempdir");
    let spool = Spool::open(tmp.path().join("spool")).expect("open spool");
    let dead = spool
        .persist_new(&sample_entry("d-dead", 100))
        .expect("durable write");
    spool
        .persist_new(&sample_entry("d-live", 200))
        .expect("durable write");
    spool.quarantine(&dead).expect("quarantine");

    let census = spool.scan_metadata().expect("census");
    assert_eq!(census.live.len(), 1);
    assert_eq!(census.live[0].delivery_id, "d-live");
    assert_eq!(census.exhausted.len(), 1);
    assert_eq!(census.exhausted[0].delivery_id, "d-dead");
}

#[test]
fn spool_scan_metadata_reports_a_stray_file_rather_than_dating_it_zero() {
    // A name with no timestamp prefix must not parse as age 0 — that would
    // read as the oldest entry in the spool and hijack every diagnostic.
    let tmp = tempfile::tempdir().expect("tempdir");
    let spool = Spool::open(tmp.path().join("spool")).expect("open spool");
    std::fs::write(spool.root().join("stray.json"), b"{}").expect("write stray");

    let census = spool.scan_metadata().expect("census");
    assert!(census.live.is_empty());
    assert_eq!(census.unparsable.len(), 1);
}

#[tokio::test]
async fn sweep_quarantines_an_exhausted_entry_and_stops_paying_for_it() {
    // 🔴 Fails against `92c1ed3fc`: the sweep counted `exhausted` and
    // `continue`d, so the entry stayed in the live set and every subsequent
    // pass decoded it again. The module doc says no target binds a listener
    // until step 4, so in this PR's intended configuration that is EVERY
    // delivery, permanently.
    let tmp = tempfile::tempdir().expect("tempdir");
    let spool = Spool::open(tmp.path().join("spool")).expect("open spool");
    let policy = BackoffPolicy {
        first_attempt_grace: Duration::ZERO,
        base: Duration::ZERO,
        ceiling: Duration::ZERO,
        max_attempts: 1,
    };
    let ingress = ingress_with(
        spool,
        tmp.path().join("sockets").join("absent.sock"),
        Duration::from_millis(300),
    )
    .with_backoff(policy);
    for id in ["d-1", "d-2"] {
        ingress
            .spool()
            .persist_new(&sample_entry(id, 1_700_000_000_000))
            .expect("durable write");
    }

    assert_eq!(ingress.retry_pending_once().await.still_pending, 2);
    let second = ingress.retry_pending_once().await;
    assert_eq!(second.exhausted, 2);

    let census = ingress.spool().scan_metadata().expect("census");
    assert_eq!(
        census.live.len(),
        0,
        "an exhausted entry must stop costing a decode on every pass"
    );
    assert_eq!(census.exhausted.len(), 2, "and must still be kept");

    // Every later pass sees nothing to do at all.
    let third = ingress.retry_pending_once().await;
    assert_eq!(third, SweepReport::default());
}

// ─── HIGH-3: an absent spool directory is red, not green ────────────────────

#[test]
fn health_reports_error_when_the_spool_directory_is_gone() {
    // 🔴 Fails against the pre-fix head, which answered ENOENT with an empty
    // listing: the console data dir removed (or its volume unmounted) made
    // every POST 500 while `/api/console/metrics/webhooks` reported
    // {"status":"ok","pending":0}. Ingress dead, alarm green.
    let tmp = tempfile::tempdir().expect("tempdir");
    let root = tmp.path().join("spool");
    let spool = Spool::open(&root).expect("open spool");
    spool
        .persist_new(&sample_entry("d-1", 1_700_000_000_000))
        .expect("durable write");

    std::fs::remove_dir_all(&root).expect("simulate the directory going away");

    let h = health::scan_health(&spool, 1_700_000_100_000, Duration::from_secs(600), &[]);
    assert_eq!(
        h.status,
        ServiceHealth::Error,
        "a spool whose directory vanished is broken, not empty"
    );
    assert!(
        h.scan_error.is_some(),
        "and the reason must be reported: {h:?}"
    );
}

#[test]
fn a_never_opened_spool_directory_is_still_legitimately_empty() {
    // The other side of the same rule: `Spool::at` on a path that was never
    // created has genuinely nothing pending, and must not report red.
    let tmp = tempfile::tempdir().expect("tempdir");
    let spool = Spool::at(tmp.path().join("never-created"));
    let listing = spool.list_pending().expect("an unopened spool lists empty");
    assert!(listing.pending.is_empty());
    let h = health::scan_health(&spool, 1_700_000_000_000, Duration::from_secs(600), &[]);
    assert_eq!(h.status, ServiceHealth::Ok);
    assert_eq!(h.scan_error, None);
}

#[tokio::test]
async fn metrics_route_reports_red_when_the_spool_directory_is_gone() {
    let tmp = tempfile::tempdir().expect("tempdir");
    let root = tmp.path().join("spool");
    let spool = Spool::open(&root).expect("open spool");
    let ingress = ingress_for(spool, tmp.path().join("sockets").join("absent.sock"));
    std::fs::remove_dir_all(&root).expect("simulate the directory going away");

    let (status, body) = get_json(router_for(ingress), "/api/console/metrics/webhooks").await;
    assert_eq!(status, StatusCode::OK);
    assert_eq!(body["status"], "error");
    assert!(body["metrics"]["scan_error"].is_string());
}

// ─── body limit ─────────────────────────────────────────────────────────────

#[tokio::test]
async fn route_accepts_a_body_larger_than_the_axum_default_limit() {
    // 🔴 Fails against the pre-fix head: axum's 2 MiB DefaultBodyLimit 413s a
    // 3 MiB delivery BEFORE the handler runs — no spool entry, no metric, no
    // log. GitHub payloads are legal to 25 MB and push/pull_request bodies
    // routinely exceed 2 MiB.
    let tmp = tempfile::tempdir().expect("tempdir");
    let spool = Spool::open(tmp.path().join("spool")).expect("open spool");
    let ingress = ingress_for(spool, tmp.path().join("sockets").join("absent.sock"));

    let big = format!(
        r#"{{"action":"opened","filler":"{}"}}"#,
        "x".repeat(3 * 1024 * 1024)
    );
    let body = big.as_bytes();

    let (status, json) = post_webhook(
        router_for(ingress.clone()),
        "review",
        body,
        signed_headers(body, "d-big"),
    )
    .await;

    assert_eq!(
        status,
        StatusCode::ACCEPTED,
        "a 3 MiB delivery must reach the handler, not be 413'd by the framework"
    );
    assert_eq!(json["delivery_id"], "d-big");

    let listing = listing_of(ingress.spool());
    assert_eq!(listing.pending.len(), 1);
    assert_eq!(
        BASE64
            .decode(&listing.pending[0].entry.body_b64)
            .expect("decode"),
        body,
        "and the whole 3 MiB must be spooled byte-exact"
    );
}

// ─── HIGH-B: the signal keeps saying something new after it goes red ────────

#[test]
fn health_diagnostics_track_the_live_entry_not_the_exhausted_one() {
    // 🔴 Fails against `92c1ed3fc`. Exhausted entries are by construction the
    // oldest, and `scan_health` took `listing.pending.first()`, so
    // `oldest_pending_delivery_id`, `oldest_pending_last_error` and
    // `oldest_pending_age_secs` pinned to the first poisoned delivery forever.
    // Day 1 one delivery exhausts; day 30 a genuinely new one gets stuck and
    // nothing an operator or alert rule reads moves.
    let tmp = tempfile::tempdir().expect("tempdir");
    let spool = Spool::open(tmp.path().join("spool")).expect("open spool");
    let now = 1_700_000_000_000u64;

    // Day 1: a delivery that has been given up on. Oldest thing in the spool.
    let mut dead = sample_entry("d-day-one", now - 30 * 86_400_000);
    dead.attempts = 24;
    dead.last_error = Some("no listener bound".to_string());
    let dead_path = spool.persist_new(&dead).expect("durable write");
    spool.quarantine(&dead_path).expect("quarantine");

    // Day 30: a NEW delivery is now failing. This is what an operator needs.
    let mut live = sample_entry("d-day-thirty", now - 900_000);
    live.attempts = 3;
    live.last_error = Some("target rejected the frame: code -32000 — store locked".to_string());
    live.last_attempt_at_unix_ms = Some(now - 60_000);
    spool.persist_new(&live).expect("durable write");

    let h = health::scan_health(&spool, now, Duration::from_secs(600), &[]);

    assert_eq!(h.status, ServiceHealth::Error);
    assert_eq!(
        h.oldest_pending_delivery_id.as_deref(),
        Some("d-day-thirty"),
        "the diagnostics must describe the LIVE failure, not the 30-day-old corpse"
    );
    assert_eq!(
        h.oldest_pending_last_error.as_deref(),
        Some("target rejected the frame: code -32000 — store locked")
    );
    assert_eq!(h.oldest_pending_attempts, Some(3));
    assert_eq!(h.oldest_pending_age_secs, Some(900));
    assert_eq!(h.pending, 1);

    // The exhausted one is still reported — in its own fields, where it cannot
    // crowd out a live failure.
    assert_eq!(h.exhausted, 1);
    assert_eq!(h.exhausted_delivery_ids, vec!["d-day-one".to_string()]);
    assert_eq!(h.oldest_exhausted_age_secs, Some(30 * 86_400));
}

#[test]
fn health_is_red_for_an_exhausted_entry_even_with_nothing_live() {
    let tmp = tempfile::tempdir().expect("tempdir");
    let spool = Spool::open(tmp.path().join("spool")).expect("open spool");
    let now = 1_700_000_000_000u64;
    let path = spool
        .persist_new(&sample_entry("d-dead", now - 1_000))
        .expect("durable write");
    spool.quarantine(&path).expect("quarantine");

    let h = health::scan_health(&spool, now, Duration::from_secs(600), &[]);
    assert_eq!(
        h.status,
        ServiceHealth::Error,
        "nothing clears an exhausted entry without an operator, so it stays red"
    );
    assert_eq!(h.pending, 0);
    assert_eq!(h.exhausted, 1);
    assert_eq!(h.oldest_pending_delivery_id, None);
}

#[tokio::test]
async fn metrics_route_surfaces_exhausted_separately_from_live() {
    let tmp = tempfile::tempdir().expect("tempdir");
    let spool = Spool::open(tmp.path().join("spool")).expect("open spool");
    let now = 1_700_000_000_000u64;
    let dead = spool
        .persist_new(&sample_entry("d-dead", now - 86_400_000))
        .expect("durable write");
    spool.quarantine(&dead).expect("quarantine");
    spool
        .persist_new(&sample_entry("d-live", now - 1_000))
        .expect("durable write");
    let ingress = ingress_for(spool, tmp.path().join("sockets").join("absent.sock"));

    let (status, body) = get_json(router_for(ingress), "/api/console/metrics/webhooks").await;
    assert_eq!(status, StatusCode::OK);
    assert_eq!(body["status"], "error");
    assert_eq!(body["metrics"]["pending"], 1);
    assert_eq!(body["metrics"]["exhausted"], 1);
    assert_eq!(body["metrics"]["oldest_pending_delivery_id"], "d-live");
    assert_eq!(body["metrics"]["exhausted_delivery_ids"][0], "d-dead");
}

// ─── End to end against the real receiver (#5182) ───────────────────────────
//
// Every case above answers console with a hand-rolled stub. These drive the
// production receiver — `trusty_common::webhook_relay`'s listener, the same one
// `trusty-review webhook-listen` and `trusty-analyze webhook-listen` run — so
// the two halves are proven against each other rather than against a fixture
// that agrees with whichever half was edited last.

/// Bind the real listener under `dir`, backed by `sink`, and return its socket.
async fn spawn_real_listener(
    dir: &StdPath,
    sink: std::sync::Arc<dyn trusty_common::webhook_relay::DeliverySink>,
) -> PathBuf {
    let socket = dir.join("sockets").join("real-target.sock");
    let listener = bind_hardened(&socket).expect("bind real listener");
    tokio::spawn(async move {
        trusty_common::webhook_relay::serve_until(
            &listener,
            sink,
            trusty_common::webhook_relay::ServeOptions::default(),
            std::future::pending::<()>(),
        )
        .await;
    });
    socket
}

/// A sink that always refuses, standing in for a receiver whose disk is gone.
#[derive(Debug)]
struct UndurableSink;

impl trusty_common::webhook_relay::DeliverySink for UndurableSink {
    fn take_ownership(
        &self,
        _delivery: &trusty_common::webhook_relay::RelayDelivery,
    ) -> Result<(), trusty_common::webhook_relay::SinkRejection> {
        Err(trusty_common::webhook_relay::SinkRejection::not_durable(
            "inbox is unwritable",
        ))
    }
}

#[tokio::test]
async fn delivery_reaches_the_real_target_and_is_spooled_there_before_the_ack() {
    // 🔴 The whole point of #5182. Against the pre-fix commit this fails at the
    // first assertion: nothing bound the socket, so the outcome was
    // `Unreachable` and the entry stayed pending forever.
    //
    // The ordering claim is checked the only way it can be from this side: the
    // ack has already been received when the assertions run, and the receiver's
    // inbox already holds the delivery. Were the ack sent first, the inbox could
    // still be empty here — and console would have deleted its own copy, which
    // the spool assertion below would then be unable to distinguish from
    // success. The receiver-side ordering itself is pinned in trusty-common by
    // `dispatch_acks_only_after_the_sink_has_taken_ownership`.
    let tmp = tempfile::tempdir().expect("tempdir");
    let inbox = trusty_common::webhook_relay::Inbox::open(tmp.path().join("inbox"))
        .expect("open receiver inbox");
    let socket = spawn_real_listener(tmp.path(), std::sync::Arc::new(inbox.clone())).await;
    let spool = Spool::open(tmp.path().join("spool")).expect("open spool");
    let ingress = ingress_for(spool.clone(), socket);

    let outcome = ingress
        .ingest("review", &signed_headers(BODY, "e2e-real-1"), BODY)
        .await;

    let IngestOutcome::Accepted { relay, .. } = outcome else {
        panic!("expected Accepted, got {outcome:?}");
    };
    assert_eq!(
        relay,
        RelayOutcome::Acked,
        "the real listener must acknowledge a verified delivery"
    );

    let held = inbox.list().expect("list the receiver inbox");
    assert_eq!(held.len(), 1, "the receiver must hold the delivery");
    assert_eq!(held[0].1.delivery_id, "e2e-real-1");
    assert_eq!(
        held[0].1.body_b64,
        base64::engine::general_purpose::STANDARD.encode(BODY),
        "the raw body must arrive byte-exact, so the HMAC stays re-checkable"
    );
    assert!(
        held[0].1.provenance.verified,
        "the receiver must be told what console verified"
    );

    assert!(
        listing_of(&spool).pending.is_empty(),
        "an acknowledged delivery is the one case that clears the spool"
    );
}

#[tokio::test]
async fn a_receiver_that_cannot_own_the_work_never_produces_an_ack() {
    // 🔴 The failure this change must not introduce. A receiver that answers but
    // cannot take durable responsibility must leave console's spool entry alone
    // — that entry is the only remaining copy, because GitHub does not re-send
    // an acknowledged delivery.
    //
    // Against the pre-fix commit there is no counterpart: no listener existed at
    // all, so "reached the target and it could not own the work" was not a
    // reachable state.
    let tmp = tempfile::tempdir().expect("tempdir");
    let socket = spawn_real_listener(tmp.path(), std::sync::Arc::new(UndurableSink)).await;
    let spool = Spool::open(tmp.path().join("spool")).expect("open spool");
    let ingress = ingress_for(spool.clone(), socket);

    let outcome = ingress
        .ingest("review", &signed_headers(BODY, "e2e-refuse-1"), BODY)
        .await;

    let IngestOutcome::Accepted { relay, .. } = outcome else {
        panic!("expected Accepted, got {outcome:?}");
    };
    assert!(
        !relay.is_acked(),
        "a receiver that cannot own the work must not read as acknowledged, got {relay:?}"
    );
    assert!(
        relay.reason().contains("inbox is unwritable"),
        "the durable record must carry the receiver's own words, got {:?}",
        relay.reason()
    );

    let pending = listing_of(&spool).pending;
    assert_eq!(pending.len(), 1, "the only copy must survive");
    assert_eq!(pending[0].entry.delivery_id, "e2e-refuse-1");
    assert_eq!(
        pending[0].entry.attempts, 1,
        "the failed attempt must be recorded durably, not only logged"
    );
}

#[tokio::test]
async fn the_real_listener_rejects_a_method_that_is_not_webhook_deliver() {
    // Console only ever sends `webhook.deliver`, so this drives the listener
    // directly. A listener that served anything it was handed would take
    // responsibility for payloads no sender in this workspace produces.
    let tmp = tempfile::tempdir().expect("tempdir");
    let inbox = trusty_common::webhook_relay::Inbox::open(tmp.path().join("inbox"))
        .expect("open receiver inbox");
    let socket = spawn_real_listener(tmp.path(), std::sync::Arc::new(inbox.clone())).await;

    let request = serde_json::json!({
        "jsonrpc": "2.0",
        "method": "webhook.execute",
        "id": "bad-method-1",
        "params": {
            "delivery_id": "bad-method-1",
            "source": "review",
            "event": "pull_request",
            "headers": {},
            "body_b64": "e30=",
            "provenance": {
                "algorithm": "hmac-sha256",
                "key_id": "GITHUB_WEBHOOK_SECRET",
                "verified": true
            },
            "received_at_unix_ms": 1_700_000_000_000u64,
            "attempts": 0
        }
    });
    let response: trusty_common::webhook_relay::RelayResponse =
        trusty_common::uds::send_framed_request(&socket, &request, Duration::from_secs(5))
            .await
            .expect("the listener must answer rather than hang up");

    assert!(!response.is_ack(), "an unknown method must not be acked");
    let err = response.error.expect("a refusal carries an error");
    assert_eq!(err.code, -32601, "unknown method is method-not-found");
    assert!(
        err.message.contains("webhook.execute") && err.message.contains(RELAY_METHOD),
        "the refusal must name both what arrived and what is served, got {:?}",
        err.message
    );
    assert!(
        inbox.list().expect("list").is_empty(),
        "a refused method must leave nothing durably owned"
    );
}

// ─── Supervised on-demand spawn (#5182) ─────────────────────────────────────

#[test]
fn spawn_maps_each_source_to_its_binary() {
    assert_eq!(spawn::target_binary_name("review"), "trusty-review");
    assert_eq!(spawn::target_binary_name("analyze"), "trusty-analyze");
}

#[tokio::test]
async fn spawn_adopts_a_socket_that_is_already_served() {
    // The path that keeps a supervised target cheap: something is already
    // serving, so `ensure_running` neither locates a binary nor launches one.
    // It also never resolves the binary, which is what keeps this test from
    // starting a real trusty-review on the developer's machine.
    let tmp = tempfile::tempdir().expect("tempdir");
    let inbox = trusty_common::webhook_relay::Inbox::open(tmp.path().join("inbox"))
        .expect("open receiver inbox");
    let socket = spawn_real_listener(tmp.path(), std::sync::Arc::new(inbox)).await;
    let supervisor = spawn::TargetSupervisor::new();

    let resolved = supervisor
        .ensure_running("review", &socket)
        .await
        .expect("an already-served socket must be adopted");

    assert_eq!(resolved, socket);
    assert_eq!(
        supervisor.spawned_count(),
        0,
        "adoption must not launch a child"
    );
}

#[tokio::test]
async fn relay_with_a_supervisor_still_acks_against_a_live_target() {
    // The supervisor sits in front of the dial, so an attached one must not
    // change the outcome when the target is already up.
    let tmp = tempfile::tempdir().expect("tempdir");
    let inbox = trusty_common::webhook_relay::Inbox::open(tmp.path().join("inbox"))
        .expect("open receiver inbox");
    let socket = spawn_real_listener(tmp.path(), std::sync::Arc::new(inbox.clone())).await;
    let spool = Spool::open(tmp.path().join("spool")).expect("open spool");

    let ingress = WebhookIngress::new(
        spool.clone(),
        SECRET.to_string(),
        SECRET_ENV.to_string(),
        vec![Target {
            source: "review".to_string(),
            relay: UdsRelay::new(socket)
                .with_timeout(Duration::from_secs(2))
                .with_supervisor(
                    "review",
                    std::sync::Arc::new(spawn::TargetSupervisor::new()),
                ),
        }],
    )
    .with_backoff(no_backoff());

    let outcome = ingress
        .ingest("review", &signed_headers(BODY, "supervised-1"), BODY)
        .await;

    let IngestOutcome::Accepted { relay, .. } = outcome else {
        panic!("expected Accepted, got {outcome:?}");
    };
    assert_eq!(relay, RelayOutcome::Acked);
    assert_eq!(inbox.list().expect("list").len(), 1);
    assert!(listing_of(&spool).pending.is_empty());
}

// ─── The undrained-inbox signal (#5182 review, HIGH) ────────────────────────
//
// Acking deletes console's spool entry, so `pending` drops to zero and the
// status would go green while the delivery sits unprocessed in the target's
// inbox. These pin that it does not. Nothing drains the inbox yet — that is
// https://github.com/bobmatnyc/trusty-tools/issues/5192 — so the interim
// requirement is that an operator can tell "nothing to do" from "work arrived
// and nothing is consuming it".

#[tokio::test]
async fn health_is_degraded_while_a_delivery_sits_undrained() {
    // 🔴 Against this PR without the metering, the final assertion reads `Ok`:
    // the delivery was acknowledged, console deleted its only copy, and the
    // health signal reported a clean spool with the work still undone.
    let tmp = tempfile::tempdir().expect("tempdir");
    let inbox_root = tmp.path().join("inbox");
    let inbox = trusty_common::webhook_relay::Inbox::open(&inbox_root).expect("open inbox");
    let socket = spawn_real_listener(tmp.path(), std::sync::Arc::new(inbox.clone())).await;
    let spool = Spool::open(tmp.path().join("spool")).expect("open spool");
    let ingress = ingress_for(spool.clone(), socket)
        .with_inbox_roots(vec![("review".to_string(), inbox_root.clone())]);

    assert_eq!(
        ingress.health().await.status,
        ServiceHealth::Ok,
        "an empty spool and an empty inbox is genuinely nothing to do"
    );

    let outcome = ingress
        .ingest("review", &signed_headers(BODY, "undrained-1"), BODY)
        .await;
    let IngestOutcome::Accepted { relay, .. } = outcome else {
        panic!("expected Accepted, got {outcome:?}");
    };
    assert_eq!(relay, RelayOutcome::Acked);
    assert!(
        listing_of(&spool).pending.is_empty(),
        "the ack is what makes this test meaningful — console's copy is gone"
    );

    let health = ingress.health().await;
    assert_eq!(
        health.status,
        ServiceHealth::Degraded,
        "a delivery nothing has processed must not read as healthy"
    );
    assert_eq!(health.pending, 0, "the spool really is empty");
    assert_eq!(health.undrained_total, 1);
    assert_eq!(health.undrained.len(), 1);
    assert_eq!(health.undrained[0].source, "review");
    assert_eq!(health.undrained[0].held, 1);
    assert_eq!(health.undrained[0].error, None);

    // And it clears once the target's drain (#5192) has processed the delivery.
    let held = inbox.list().expect("list");
    std::fs::remove_file(&held[0].0).expect("drain the delivery");
    assert_eq!(ingress.health().await.status, ServiceHealth::Ok);
}

#[tokio::test]
async fn health_is_error_while_a_target_holds_a_quarantined_delivery() {
    // 🔴 #5192: quarantining a poisoned delivery takes it OUT of the held
    // count, so without a separate quarantine meter the signal goes GREEN at
    // the exact moment a delivery is confirmed never to be processed. Against
    // this change with `quarantined_total` removed, the final status is `Ok`.
    let tmp = tempfile::tempdir().expect("tempdir");
    let inbox_root = tmp.path().join("inbox");
    let inbox = trusty_common::webhook_relay::Inbox::open(&inbox_root).expect("open inbox");
    let socket = spawn_real_listener(tmp.path(), std::sync::Arc::new(inbox.clone())).await;
    let spool = Spool::open(tmp.path().join("spool")).expect("open spool");
    let ingress = ingress_for(spool, socket)
        .with_inbox_roots(vec![("review".to_string(), inbox_root.clone())]);

    let outcome = ingress
        .ingest("review", &signed_headers(BODY, "poison-1"), BODY)
        .await;
    let IngestOutcome::Accepted { relay, .. } = outcome else {
        panic!("expected Accepted, got {outcome:?}");
    };
    assert_eq!(relay, RelayOutcome::Acked);

    // The target's drain gives up on it.
    let held = inbox.list().expect("list");
    trusty_common::webhook_relay::retry::quarantine(&inbox_root, &held[0].0).expect("quarantine");

    let health = ingress.health().await;
    assert_eq!(
        health.undrained_total, 0,
        "a quarantined delivery is no longer drainable work"
    );
    assert_eq!(health.quarantined_total, 1);
    assert_eq!(health.undrained[0].quarantined, 1);
    assert_eq!(
        health.status,
        ServiceHealth::Error,
        "a delivery that will never be processed without a human is not Degraded"
    );
}

#[tokio::test]
async fn health_is_error_when_a_targets_inbox_cannot_be_counted() {
    // "I could not count" is not "there is nothing". Reporting the second when
    // the first is true is the fail-quiet shape this signal exists to remove.
    let tmp = tempfile::tempdir().expect("tempdir");
    let not_a_dir = tmp.path().join("inbox-is-a-file");
    std::fs::write(&not_a_dir, b"not a directory").expect("write");
    let spool = Spool::open(tmp.path().join("spool")).expect("open spool");
    let ingress = ingress_for(spool, tmp.path().join("sockets").join("absent.sock"))
        .with_inbox_roots(vec![("review".to_string(), not_a_dir)]);

    let health = ingress.health().await;
    assert_eq!(health.status, ServiceHealth::Error);
    assert!(
        health.undrained[0].error.is_some(),
        "an uncountable inbox must carry its reason, got {:?}",
        health.undrained[0]
    );
}

#[tokio::test]
#[ignore = "mutates TRUSTY_DATA_DIR_OVERRIDE; run with --include-ignored"]
async fn from_env_meters_every_targets_inbox() {
    // `WebhookIngress::new` deliberately meters nothing, so that unit tests do
    // not read the developer's real inbox. That makes `from_env` the only place
    // the wiring can be forgotten, which is what this pins.
    let _guard = ENV_LOCK.lock().await;
    let tmp = tempfile::tempdir().expect("tempdir");
    let prior_data_dir = swap_env(
        trusty_common::DATA_DIR_OVERRIDE_ENV,
        Some(&tmp.path().to_string_lossy()),
    );
    let prior_secret = swap_env(SECRET_ENV, Some(SECRET));

    let outcome = async {
        let ingress = WebhookIngress::from_env().expect("build ingress from env");
        let health = ingress.health().await;
        let metered: Vec<&str> = health.undrained.iter().map(|u| u.source.as_str()).collect();
        assert_eq!(
            metered,
            vec!["review", "analyze"],
            "both targets must be metered, or an undrained backlog is invisible"
        );
        assert!(health.undrained.iter().all(|u| u.error.is_none()));
        assert_eq!(health.undrained_total, 0);
    }
    .await;

    swap_env(
        trusty_common::DATA_DIR_OVERRIDE_ENV,
        prior_data_dir.as_deref(),
    );
    swap_env(SECRET_ENV, prior_secret.as_deref());
    outcome
}