recall-server 0.4.13

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

use std::path::{Path, PathBuf};
use std::process::Command;
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::Arc;
use std::time::{Duration, SystemTime, UNIX_EPOCH};

use axum::body::{Body, Bytes};
use axum::http::{HeaderMap, Request, StatusCode};
use recall_server::audit::merkle;
use recall_server::merge::Status;
use recall_server::{now, Config, Server, Store};
use recall_wire::devices::{self, revoke_authkey_path, revoke_device_path};
use recall_wire::jobs as wire_jobs;
use recall_wire::signature::{self, encode_public_key, SigningKey, Target};
use recall_wire::{
    AuditCheckpoint, AuditConsistencyResponse, AuditEntriesResponse, AuthkeyCreated, Device,
    EnrollApproved, EnrollPending,
};
use serde::de::DeserializeOwned;
use serde_json::{json, Value};
use tempfile::TempDir;
use tower::ServiceExt;

const TOKEN: &str = "audit-test-token";
const HOST: &str = "recall.test";

struct Harness {
    server: Server,
    store: Arc<Store>,
    dir: TempDir,
}

fn harness_with(tweak: impl FnOnce(&mut Config)) -> Harness {
    let dir = tempfile::tempdir().unwrap();
    let store = Arc::new(Store::open(dir.path().join("recall.db")).unwrap());
    let mut cfg = Config {
        token: TOKEN.to_string(),
        merge_enabled: false,
        rate_limit_max: 100_000,
        ..Config::default()
    };
    tweak(&mut cfg);
    let server = Server::new(cfg, store.clone());
    // Signatures made in the first seconds after a start are refused (see
    // `server/auth.rs`); these tests sign at once.
    server.backdate_start(600);
    Harness { server, store, dir }
}

fn harness() -> Harness {
    harness_with(|_| {})
}

/// A machine: a key pair, and the device id it was given.
struct Machine {
    key: SigningKey,
    id: String,
}

impl Machine {
    fn new(seed: u8) -> Self {
        Self {
            key: SigningKey::from_bytes(&[seed; 32]),
            id: String::new(),
        }
    }
    fn public_key(&self) -> String {
        encode_public_key(&self.key.verifying_key())
    }
}

fn fresh_nonce() -> String {
    static NEXT: AtomicU64 = AtomicU64::new(0);
    format!("nonce-{}", NEXT.fetch_add(1, Ordering::Relaxed))
}

fn ok<T: DeserializeOwned>((status, body): (StatusCode, Bytes)) -> T {
    assert_eq!(status, StatusCode::OK, "{}", String::from_utf8_lossy(&body));
    serde_json::from_slice(&body).unwrap()
}

impl Harness {
    async fn send(&self, req: Request<Body>) -> (StatusCode, HeaderMap, Bytes) {
        let resp = self.server.router().oneshot(req).await.unwrap();
        let status = resp.status();
        let headers = resp.headers().clone();
        let body = axum::body::to_bytes(resp.into_body(), usize::MAX)
            .await
            .unwrap();
        (status, headers, body)
    }

    async fn call(
        &self,
        method: &str,
        uri: &str,
        token: Option<&str>,
        body: Option<Value>,
    ) -> (StatusCode, Bytes) {
        let mut req = Request::builder()
            .method(method)
            .uri(uri)
            .header("content-type", "application/json");
        if let Some(t) = token {
            req = req.header("authorization", format!("Bearer {t}"));
        }
        let body = body.map(|b| Body::from(b.to_string())).unwrap_or_default();
        let (status, _, bytes) = self.send(req.body(body).unwrap()).await;
        (status, bytes)
    }

    /// A request `machine` signs, as the client signs one.
    async fn signed_raw(
        &self,
        machine: &Machine,
        method: &str,
        uri: &str,
        body: Vec<u8>,
    ) -> (StatusCode, HeaderMap, Bytes) {
        let (path, query) = match uri.split_once('?') {
            Some((p, q)) => (p, Some(q)),
            None => (uri, None),
        };
        let target = Target {
            method,
            authority: HOST,
            path,
            query,
        };
        let created = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap()
            .as_secs() as i64;
        let h = signature::sign_request(
            &machine.key,
            &machine.id,
            &target,
            "1",
            &body,
            created,
            &fresh_nonce(),
        )
        .unwrap();
        let req = Request::builder()
            .method(method)
            .uri(uri)
            .header("host", HOST)
            .header("content-type", "application/json")
            .header(recall_wire::PROTOCOL_HEADER, "1")
            .header(signature::CONTENT_DIGEST_HEADER, h.content_digest)
            .header(signature::SIGNATURE_INPUT_HEADER, h.signature_input)
            .header(signature::SIGNATURE_HEADER, h.signature)
            .body(Body::from(body))
            .unwrap();
        self.send(req).await
    }

    async fn signed(
        &self,
        machine: &Machine,
        method: &str,
        uri: &str,
        body: Option<Value>,
    ) -> (StatusCode, Bytes) {
        let body = body.map(|b| b.to_string().into_bytes()).unwrap_or_default();
        let (status, _, bytes) = self.signed_raw(machine, method, uri, body).await;
        (status, bytes)
    }

    /// Enrols `machine` by code, approved by the operator with `scope`.
    async fn enrol(&self, machine: &mut Machine, name: &str, scope: &str) {
        let pending: EnrollPending = ok(self
            .call(
                "POST",
                devices::ENROLL_PATH,
                None,
                Some(json!({"name": name, "public_key": machine.public_key(), "agent": "recall/test"})),
            )
            .await);
        let device: Device = ok(self
            .call(
                "POST",
                devices::APPROVE_PATH,
                Some(TOKEN),
                Some(json!({"user_code": pending.user_code, "scope": scope})),
            )
            .await);
        machine.id = device.id;
    }

    async fn authkey(&self, ephemeral: bool) -> AuthkeyCreated {
        ok(self
            .call(
                "POST",
                devices::AUTHKEYS_PATH,
                Some(TOKEN),
                Some(json!({"tag": "cloud", "expires_in_days": 90, "ephemeral": ephemeral})),
            )
            .await)
    }

    /// Enrols `machine` with an authkey.
    async fn enrol_with(&self, machine: &mut Machine, authkey: &str) {
        let approved: EnrollApproved = ok(self
            .call(
                "POST",
                devices::ENROLL_PATH,
                None,
                Some(json!({
                    "name": "ignored",
                    "public_key": machine.public_key(),
                    "agent": "recall/test",
                    "authkey": authkey,
                })),
            )
            .await);
        machine.id = approved.device_id;
    }

    /// Ages every ephemeral device past the sweep's idle limit, directly in
    /// the database, for what a test cannot wait a day for.
    fn age_ephemeral_devices(&self) {
        let conn = rusqlite::Connection::open(self.dir.path().join("recall.db")).unwrap();
        conn.execute(
            "UPDATE devices SET created_at = '2000-01-01T00:00:00.000Z', last_seen = NULL \
             WHERE ephemeral = 1",
            [],
        )
        .unwrap();
    }

    /// The tree's current size, read straight from the store.
    fn size(&self) -> u64 {
        self.store.audit_checkpoint().0
    }

    /// Every leaf, parsed.
    async fn leaves(&self) -> Vec<Value> {
        self.leaf_strings()
            .await
            .iter()
            .map(|l| serde_json::from_str(l).unwrap())
            .collect()
    }

    async fn leaf_strings(&self) -> Vec<String> {
        let n = self.size();
        let mut out = Vec::new();
        while (out.len() as u64) < n {
            let page: AuditEntriesResponse = ok(self
                .call(
                    "GET",
                    &format!(
                        "/v1/audit/entries?start={}&end={}",
                        out.len(),
                        n.min(out.len() as u64 + 1000)
                    ),
                    Some(TOKEN),
                    None,
                )
                .await);
            assert!(!page.entries.is_empty());
            out.extend(page.entries);
        }
        out
    }

    /// An export as the verifier reads one: the checkpoint, then each leaf.
    async fn export(&self) -> String {
        let cp: AuditCheckpoint = ok(self
            .call("GET", "/v1/audit/checkpoint", Some(TOKEN), None)
            .await);
        let leaves = self.leaf_strings().await;
        assert_eq!(leaves.len() as u64, cp.tree_size);
        export_of(&leaves)
    }
}

/// An export of `leaves`, with a checkpoint line computed over them — what a
/// server that wrote those leaves would have answered.
fn export_of(leaves: &[String]) -> String {
    let hashes: Vec<merkle::Hash> = leaves
        .iter()
        .map(|l| merkle::hash_leaf(l.as_bytes()))
        .collect();
    let root = merkle::root(&hashes);
    use base64::Engine;
    let mut out = format!(
        "{} {}\n",
        leaves.len(),
        base64::engine::general_purpose::STANDARD.encode(root)
    );
    for l in leaves {
        out.push_str(l);
        out.push('\n');
    }
    out
}

fn checkpoint_of(leaves: &[String]) -> String {
    let line = export_of(leaves);
    let (size, root) = line.lines().next().unwrap().split_once(' ').unwrap();
    format!("--checkpoint={size}:{root}")
}

fn script() -> PathBuf {
    let script = Path::new(env!("CARGO_MANIFEST_DIR")).join("../../scripts/audit-verify.py");
    assert!(script.exists(), "no {}", script.display());
    script
}

/// Runs the verifier over `export` with `args`: its exit code and all it
/// printed. The built-in Ed25519 unless `args` says otherwise, so what is
/// tested does not depend on what this machine has installed.
fn verify(export: &str, args: &[&str]) -> (i32, String) {
    verify_with(export, args, &[])
}

fn verify_with(export: &str, args: &[&str], env: &[(&str, &str)]) -> (i32, String) {
    let dir = tempfile::tempdir().unwrap();
    let path = dir.path().join("audit.jsonl");
    std::fs::write(&path, export).unwrap();
    let mut cmd = Command::new("python3");
    cmd.arg(script()).arg(&path);
    if !args.iter().any(|a| a.starts_with("--ed25519")) {
        cmd.arg("--ed25519=builtin");
    }
    cmd.args(args);
    for (k, v) in env {
        cmd.env(k, v);
    }
    let out = cmd.output().expect("python3 must be on PATH");
    let (code, out) = (
        out.status.code().unwrap_or(-1),
        format!(
            "{}{}",
            String::from_utf8_lossy(&out.stdout),
            String::from_utf8_lossy(&out.stderr)
        ),
    );
    agrees_with_the_script(export, args, code, &out);
    (code, out)
}

/// `recall audit verify`'s verifier, `recall_wire::audit::verify`, gives
/// the script's verdict on the same export and the same saved checkpoints:
/// every honest log and every forgery in this file is also a test of the
/// second verifier, and a check dropped from either one shows up here as
/// the two disagreeing. Not asked when the script was told to skip
/// signatures, which the Rust one never does, or only to test its own
/// Ed25519, or could not run at all.
fn agrees_with_the_script(export: &str, args: &[&str], code: i32, out: &str) {
    let skipped = |a: &&str| matches!(*a, "--no-signatures" | "--self-test");
    if !matches!(code, 0 | 1) || args.iter().any(skipped) {
        return;
    }
    let saved: Vec<(u64, merkle::Hash)> = args
        .iter()
        .filter_map(|a| a.strip_prefix("--checkpoint="))
        .map(|c| {
            recall_wire::audit::verify::parse_checkpoint_arg(c)
                .unwrap_or_else(|| panic!("not a checkpoint: {c}"))
        })
        .collect();
    let verdict = recall_wire::audit::verify::verify_export(export.as_bytes(), &saved);
    assert_eq!(
        verdict.ok(),
        code == 0,
        "the script said {code}:\n{out}\nand recall audit verify said {:#?}",
        verdict.problems
    );
}

// ---------------------------------------------------------------------------
// what appends a leaf, and what does not
// ---------------------------------------------------------------------------

/// Every authenticated route the design's PR 1 table names appends exactly
/// one leaf; the two unauthenticated enrolment steps, the audit routes
/// themselves, and a refused request append none, and neither does
/// repeating a revoke that changes nothing.
#[tokio::test]
async fn every_authenticated_route_appends_exactly_one_leaf() {
    let h = harness();
    assert_eq!(h.size(), 0, "nothing until the server serves");

    let n = h.size();
    let (status, _) = h
        .call(
            "POST",
            "/sync",
            Some(TOKEN),
            Some(json!({"project_key":"a/b","file_path":"m.md","content":"hi"})),
        )
        .await;
    assert_eq!(status, StatusCode::OK);
    assert_eq!(h.size(), n + 1, "push");

    let n = h.size();
    let (status, _) = h
        .call("GET", "/sync?project_key=a/b", Some(TOKEN), None)
        .await;
    assert_eq!(status, StatusCode::OK);
    assert_eq!(h.size(), n + 1, "pull");

    let n = h.size();
    let (status, _) = h
        .call(
            "POST",
            "/sync",
            Some(TOKEN),
            Some(json!({"project_key":"a/b","file_path":"m.md","deleted":true})),
        )
        .await;
    assert_eq!(status, StatusCode::OK);
    assert_eq!(h.size(), n + 1, "delete");

    let laptop = Machine::new(1);
    let n = h.size();
    let pending: EnrollPending = ok(h
        .call(
            "POST",
            devices::ENROLL_PATH,
            None,
            Some(json!({"name":"laptop","public_key": laptop.public_key(), "agent":"t"})),
        )
        .await);
    assert_eq!(h.size(), n, "enrolling (unauthenticated) appends nothing");

    let n = h.size();
    let device: Device = ok(h
        .call(
            "POST",
            devices::APPROVE_PATH,
            Some(TOKEN),
            Some(json!({"user_code": pending.user_code})),
        )
        .await);
    assert_eq!(h.size(), n + 1, "approve");

    let phone = Machine::new(2);
    let pending2: EnrollPending = ok(h
        .call(
            "POST",
            devices::ENROLL_PATH,
            None,
            Some(json!({"name":"phone","public_key": phone.public_key(), "agent":"t"})),
        )
        .await);
    let n = h.size();
    let (status, _) = h
        .call(
            "POST",
            devices::DENY_PATH,
            Some(TOKEN),
            Some(json!({"user_code": pending2.user_code})),
        )
        .await;
    assert_eq!(status, StatusCode::OK);
    assert_eq!(h.size(), n + 1, "deny");

    let n = h.size();
    let key = h.authkey(true).await;
    assert_eq!(h.size(), n + 1, "authkey_create");

    let n = h.size();
    let mut cloud = Machine::new(3);
    h.enrol_with(&mut cloud, &key.key).await;
    assert_eq!(h.size(), n + 1, "an authkey enrolment is an enroll leaf");

    let n = h.size();
    let (status, _) = h
        .call("POST", &revoke_authkey_path(&key.id), Some(TOKEN), None)
        .await;
    assert_eq!(status, StatusCode::OK);
    assert_eq!(h.size(), n + 1, "authkey_revoke");

    // The same revoke again changes nothing: no second leaf.
    let n = h.size();
    let (status, _) = h
        .call("POST", &revoke_authkey_path(&key.id), Some(TOKEN), None)
        .await;
    assert_eq!(status, StatusCode::OK);
    assert_eq!(h.size(), n, "revoking a key twice appends nothing");

    // Its devices, asked for now, are a change: one leaf, naming them.
    let n = h.size();
    let (status, _) = h
        .call(
            "POST",
            &revoke_authkey_path(&key.id),
            Some(TOKEN),
            Some(json!({"revoke_devices": true})),
        )
        .await;
    assert_eq!(status, StatusCode::OK);
    assert_eq!(h.size(), n + 1, "the cascade after the fact");
    let leaves = h.leaves().await;
    let last = leaves.last().unwrap();
    assert_eq!(last["action"], "authkey_revoke");
    assert_eq!(last["subject"]["revoked_devices"], json!([cloud.id]));
    let n = h.size();
    h.call(
        "POST",
        &revoke_authkey_path(&key.id),
        Some(TOKEN),
        Some(json!({"revoke_devices": true})),
    )
    .await;
    assert_eq!(h.size(), n, "and nothing when nothing is left to revoke");

    let n = h.size();
    let (status, _) = h
        .call("POST", &revoke_device_path(&device.id), Some(TOKEN), None)
        .await;
    assert_eq!(status, StatusCode::OK);
    assert_eq!(h.size(), n + 1, "revoke");

    let n = h.size();
    let (status, _) = h
        .call("POST", &revoke_device_path(&device.id), Some(TOKEN), None)
        .await;
    assert_eq!(status, StatusCode::OK);
    assert_eq!(h.size(), n, "revoking a device twice appends nothing");

    let n = h.size();
    for uri in [
        "/v1/audit/checkpoint".to_string(),
        format!("/v1/audit/entries?start=0&end={n}"),
        format!("/v1/audit/consistency?first=1&second={n}"),
    ] {
        let (status, _) = h.call("GET", &uri, Some(TOKEN), None).await;
        assert_eq!(status, StatusCode::OK, "{uri}");
        assert_eq!(h.size(), n, "{uri} appends nothing");
    }

    let (status, _) = h
        .call("GET", "/sync?project_key=a/b", Some("wrong-token"), None)
        .await;
    assert_eq!(status, StatusCode::UNAUTHORIZED);
    assert_eq!(h.size(), n, "an unauthorized request appends nothing");

    let (status, _) = h
        .call(
            "POST",
            devices::APPROVE_PATH,
            Some(TOKEN),
            Some(json!({"user_code":"ZZZZ-ZZZZ"})),
        )
        .await;
    assert_eq!(status, StatusCode::NOT_FOUND);
    assert_eq!(h.size(), n, "a refused approve appends nothing");

    // Every leaf the operator's requests made carries no request, and the
    // whole log verifies.
    let (code, out) = verify(&h.export().await, &[]);
    assert_eq!(code, 0, "{out}");
}

/// The `start` leaf is appended by the server that got its port and is
/// serving, not by building one.
#[tokio::test]
async fn the_start_leaf_is_appended_once_serving() {
    let h = harness();
    assert_eq!(h.size(), 0, "Server::new appends nothing");

    let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
    let addr = listener.local_addr().unwrap();
    let server = Server::new(
        Config {
            token: TOKEN.to_string(),
            merge_enabled: false,
            ..Config::default()
        },
        h.store.clone(),
    );
    let serving = tokio::spawn(async move {
        server
            .serve_with_shutdown(listener, async {
                tokio::time::sleep(Duration::from_millis(200)).await;
            })
            .await
    });
    drop(tokio::net::TcpStream::connect(addr).await.unwrap());
    tokio::time::timeout(Duration::from_secs(5), serving)
        .await
        .unwrap()
        .unwrap()
        .unwrap();

    let leaves = h.leaves().await;
    assert_eq!(leaves.len(), 1);
    assert_eq!(leaves[0]["action"], "start");
    assert_eq!(leaves[0]["actor"], json!({"kind": "server"}));
    assert_eq!(leaves[0]["request"], Value::Null);
}

/// The per-device sweep appends one leaf per device it removes, and none
/// when nothing is idle.
#[tokio::test]
async fn the_sweep_appends_one_leaf_per_device_removed() {
    let h = harness();
    let key = h.authkey(true).await;
    let (mut a, mut b) = (Machine::new(3), Machine::new(4));
    h.enrol_with(&mut a, &key.key).await;
    h.enrol_with(&mut b, &key.key).await;
    h.age_ephemeral_devices();

    let n = h.size();
    let (removed, _) = h.server.sweep_devices().unwrap();
    assert_eq!(removed, 2);
    assert_eq!(h.size(), n + 2, "two devices removed, two leaves");
    let leaves = h.leaves().await;
    let mut swept: Vec<&str> = leaves[n as usize..]
        .iter()
        .map(|l| {
            assert_eq!(l["action"], "sweep");
            l["subject"]["device_id"].as_str().unwrap()
        })
        .collect();
    swept.sort();
    let mut want = vec![a.id.as_str(), b.id.as_str()];
    want.sort();
    assert_eq!(swept, want);

    let n = h.size();
    let (removed, _) = h.server.sweep_devices().unwrap();
    assert_eq!(removed, 0);
    assert_eq!(h.size(), n, "nothing left to sweep, nothing appended");
}

// ---------------------------------------------------------------------------
// what a leaf says
// ---------------------------------------------------------------------------

/// Enrolling with an authkey appends exactly one leaf: `enroll`, by the key
/// (its id and tag, never the key), carrying the device's public key.
#[tokio::test]
async fn an_authkey_enrolment_appends_one_leaf_carrying_the_public_key() {
    let h = harness();
    let key = h.authkey(true).await;
    let mut cloud = Machine::new(9);
    let n = h.size();
    h.enrol_with(&mut cloud, &key.key).await;
    assert_eq!(h.size(), n + 1);

    let leaves = h.leaf_strings().await;
    let leaf: Value = serde_json::from_str(leaves.last().unwrap()).unwrap();
    assert_eq!(leaf["action"], "enroll");
    assert_eq!(
        leaf["actor"],
        json!({"kind": "authkey", "id": key.id, "tag": "cloud"})
    );
    assert_eq!(leaf["subject"]["device_id"], json!(cloud.id));
    assert_eq!(leaf["subject"]["public_key"], json!(cloud.public_key()));
    assert_eq!(leaf["subject"]["authkey_id"], json!(key.id));
    assert_eq!(leaf["subject"]["ephemeral"], json!(true));
    assert_eq!(leaf["subject"]["scope"], json!("sync"));
    assert_eq!(leaf["request"], Value::Null);
    for l in &leaves {
        assert!(!l.contains(&key.key), "the authkey itself is in the log");
    }
}

/// A device's own requests are recorded with what it signed, and the leaf
/// that made it with its public key — enough to check the signature from
/// the log alone. Checked here with `recall_wire`'s own verifier; the
/// offline one below checks the same.
#[tokio::test]
async fn a_signed_leaf_keeps_its_request_and_its_device_leaf_the_key() {
    let h = harness();
    let mut laptop = Machine::new(5);
    h.enrol(&mut laptop, "laptop", "admin").await;
    let (status, _) = h
        .signed(
            &laptop,
            "POST",
            "/sync",
            Some(json!({"project_key":"a/b","file_path":"m.md","content":"hi"})),
        )
        .await;
    assert_eq!(status, StatusCode::OK);
    let deny_body = json!({"user_code": "ZZZZ-ZZZZ"}).to_string();
    let (status, _, _) = h
        .signed_raw(&laptop, "POST", devices::DENY_PATH, deny_body.into_bytes())
        .await;
    assert_eq!(status, StatusCode::NOT_FOUND, "refused, so no leaf");

    let leaves = h.leaves().await;
    let approve = leaves.iter().find(|l| l["action"] == "approve").unwrap();
    assert_eq!(approve["subject"]["device_id"], json!(laptop.id));
    assert_eq!(approve["subject"]["public_key"], json!(laptop.public_key()));
    let key =
        signature::parse_public_key(approve["subject"]["public_key"].as_str().unwrap()).unwrap();

    let push = leaves.iter().find(|l| l["action"] == "push").unwrap();
    assert_eq!(push["actor"]["kind"], "device");
    assert_eq!(push["actor"]["id"], json!(laptop.id));
    let request = &push["request"];
    assert_eq!(
        request["body"],
        Value::Null,
        "a push does not keep its body"
    );
    let base = request["signature_base"].as_str().unwrap();
    assert!(base.contains(&format!("keyid=\"{}\"", laptop.id)), "{base}");
    assert!(base.contains(&format!(
        "\"content-digest\": sha-256=:{}:",
        request["body_sha256"].as_str().unwrap()
    )));
    use base64::Engine;
    let sig = base64::engine::general_purpose::STANDARD
        .decode(request["signature"].as_str().unwrap())
        .unwrap();
    signature::verify(&key, base, &sig).expect("the kept signature verifies");
}

/// The device-management actions keep their body beside the signature, so
/// what was asked for is bound to what the leaf says was done; their body
/// is capped, since it is kept.
#[tokio::test]
async fn a_device_management_leaf_keeps_its_small_body() {
    let h = harness();
    let mut admin = Machine::new(6);
    h.enrol(&mut admin, "admin", "admin").await;
    let body = json!({"tag": "ci", "expires_in_days": 1, "ephemeral": false}).to_string();
    let (status, _, resp) = h
        .signed_raw(
            &admin,
            "POST",
            devices::AUTHKEYS_PATH,
            body.clone().into_bytes(),
        )
        .await;
    assert_eq!(status, StatusCode::OK, "{}", String::from_utf8_lossy(&resp));
    let leaves = h.leaves().await;
    let leaf = leaves.last().unwrap();
    assert_eq!(leaf["action"], "authkey_create");
    assert_eq!(leaf["request"]["body"], json!(body));
    let created: AuthkeyCreated = serde_json::from_slice(&resp).unwrap();
    assert!(
        !leaf.to_string().contains(&created.key),
        "the key made in answer is not in the log"
    );

    let n = h.size();
    let (status, body) = h
        .call(
            "POST",
            devices::AUTHKEYS_PATH,
            Some(TOKEN),
            Some(json!({"tag": "x", "expires_in_days": 1, "padding": "p".repeat(9000)})),
        )
        .await;
    assert_eq!(status, StatusCode::PAYLOAD_TOO_LARGE);
    assert_eq!(
        serde_json::from_slice::<Value>(&body).unwrap(),
        json!({"error": "request body too large"})
    );
    assert_eq!(h.size(), n);
}

/// A push says whether the server merged it, which is why its
/// `stored_sha256` may not be the hash of what was sent.
#[tokio::test]
async fn a_merged_push_says_so() {
    let dir = tempfile::tempdir().unwrap();
    let bin = dir.path().join("claude");
    std::fs::write(
        &bin,
        "#!/bin/sh\ncat > /dev/null\nprintf '%s' '{\"is_error\":false,\"result\":\"A and B\"}'\n",
    )
    .unwrap();
    use std::os::unix::fs::PermissionsExt;
    std::fs::set_permissions(&bin, std::fs::Permissions::from_mode(0o755)).unwrap();
    let h = harness_with(|c| {
        c.merge_enabled = true;
        c.claude_bin = bin.to_str().unwrap().to_string();
        c.merge_timeout = Duration::from_secs(20);
    });
    h.server.set_claude_status(Status {
        checked_at: now(),
        available: true,
        logged_in: true,
        error: String::new(),
    });
    for content in ["A", "B"] {
        let (status, _) = h
            .call(
                "POST",
                "/sync",
                Some(TOKEN),
                Some(json!({"project_key": "a/b", "file_path": "m.md", "content": content})),
            )
            .await;
        assert_eq!(status, StatusCode::OK);
    }
    let leaves = h.leaves().await;
    assert_eq!(leaves[0]["subject"]["merged"], json!(false));
    assert_eq!(leaves[1]["subject"]["merged"], json!(true));
    assert_eq!(
        leaves[1]["subject"]["stored_sha256"],
        json!(recall_wire::content_sha256("A and B"))
    );
}

/// What a push names goes into its leaf, so it is held to what a client
/// sends: a base that is a SHA-256 (kept lowercase), a key and a path no
/// longer than a path.
#[tokio::test]
async fn what_a_leaf_names_is_bounded() {
    let h = harness();
    let push = |body: Value| h.call("POST", "/sync", Some(TOKEN), Some(body));
    let base = recall_wire::content_sha256("x").to_uppercase();
    let (status, _) =
        push(json!({"project_key":"a/b","file_path":"m.md","content":"y","base_sha256": base}))
            .await;
    assert_eq!(status, StatusCode::OK);
    let leaves = h.leaves().await;
    assert_eq!(
        leaves[0]["subject"]["base_sha256"],
        json!(recall_wire::content_sha256("x")),
        "stored lowercase"
    );

    let n = h.size();
    for (body, want) in [
        (
            json!({"project_key":"a/b","file_path":"m.md","content":"y","base_sha256":"abc"}),
            "base_sha256 must be 64 hexadecimal characters",
        ),
        (
            json!({"project_key":"k".repeat(4097),"file_path":"m.md","content":"y"}),
            "project_key must be at most 4096 bytes",
        ),
        (
            json!({"project_key":"a/b","file_path":"f".repeat(4097),"content":"y"}),
            "file_path must be at most 4096 bytes",
        ),
    ] {
        let (status, resp) = push(body).await;
        assert_eq!(status, StatusCode::BAD_REQUEST);
        assert_eq!(
            serde_json::from_slice::<Value>(&resp).unwrap(),
            json!({ "error": want })
        );
    }
    let (status, _) = h
        .call(
            "GET",
            &format!("/sync?project_key={}", "k".repeat(4097)),
            Some(TOKEN),
            None,
        )
        .await;
    assert_eq!(status, StatusCode::BAD_REQUEST);
    assert_eq!(h.size(), n, "no leaf for any of them");
}

/// Every pull leaves the client the checkpoint that includes its own leaf.
#[tokio::test]
async fn a_pull_carries_the_checkpoint_it_leaves() {
    let h = harness();
    let (status, headers, _) = h
        .send(
            Request::builder()
                .uri("/sync?project_key=a/b")
                .header("authorization", format!("Bearer {TOKEN}"))
                .body(Body::empty())
                .unwrap(),
        )
        .await;
    assert_eq!(status, StatusCode::OK);
    let header = headers
        .get(recall_wire::audit::CHECKPOINT_HEADER)
        .expect("a checkpoint header")
        .to_str()
        .unwrap();
    let cp: AuditCheckpoint = ok(h
        .call("GET", "/v1/audit/checkpoint", Some(TOKEN), None)
        .await);
    assert_eq!(header, cp.to_header_value());
    assert_eq!(cp.tree_size, 1, "the pull's own leaf");
}

/// The tree comes back after a restart: the same checkpoint, and the next
/// leaf takes the next seq.
#[tokio::test]
async fn the_log_survives_a_restart() {
    let h = harness();
    for i in 0..5 {
        h.call(
            "POST",
            "/sync",
            Some(TOKEN),
            Some(json!({"project_key":"a/b","file_path": format!("{i}.md"),"content":"x"})),
        )
        .await;
    }
    let before = h.store.audit_checkpoint();
    let reopened = Store::open(h.dir.path().join("recall.db")).unwrap();
    assert_eq!(reopened.audit_checkpoint(), before);
}

// ---------------------------------------------------------------------------
// who may read it
// ---------------------------------------------------------------------------

/// The leaves name every project, file, device and authkey, so only the
/// operator or an admin device reads them; the checkpoint and the proofs,
/// hashes only, are for any credential.
#[tokio::test]
async fn entries_needs_an_admin_and_the_hashes_do_not() {
    let h = harness();
    let mut sync = Machine::new(7);
    h.enrol(&mut sync, "sync", "sync").await;
    let mut admin = Machine::new(8);
    h.enrol(&mut admin, "admin", "admin").await;
    let n = h.size();

    let entries = format!("/v1/audit/entries?start=0&end={n}");
    let (status, body) = h.signed(&sync, "GET", &entries, None).await;
    assert_eq!(
        status,
        StatusCode::FORBIDDEN,
        "{}",
        String::from_utf8_lossy(&body)
    );
    let (status, _) = h.signed(&admin, "GET", &entries, None).await;
    assert_eq!(status, StatusCode::OK);
    let (status, _) = h.call("GET", &entries, Some(TOKEN), None).await;
    assert_eq!(status, StatusCode::OK);

    let (status, _) = h.signed(&sync, "GET", "/v1/audit/checkpoint", None).await;
    assert_eq!(status, StatusCode::OK);
    let proof: AuditConsistencyResponse = ok(h
        .signed(
            &sync,
            "GET",
            &format!("/v1/audit/consistency?first=1&second={n}"),
            None,
        )
        .await);
    assert_eq!((proof.first, proof.second), (1, n));
    assert_eq!(h.size(), n, "reading appends nothing");
}

/// `end` past the log, a range the wrong way round, and a page over 1,000
/// are refused; a page whose leaves come to more than 2 MiB stops early and
/// says where.
#[tokio::test]
async fn entries_is_bounded_by_range_count_and_bytes() {
    let h = harness();
    // Leaves of about 8 KiB each: a key and a path of the longest allowed.
    let key = "k".repeat(4096);
    for i in 0..300 {
        let (status, _) = h
            .call(
                "POST",
                "/sync",
                Some(TOKEN),
                Some(json!({"project_key": key, "file_path": format!("{i:04}{}", "f".repeat(4000)), "content": "x"})),
            )
            .await;
        assert_eq!(status, StatusCode::OK);
    }
    let n = h.size();
    for uri in [
        format!("/v1/audit/entries?start=0&end={}", n + 1),
        "/v1/audit/entries?start=5&end=4".to_string(),
        "/v1/audit/entries?start=0".to_string(),
    ] {
        let (status, _) = h.call("GET", &uri, Some(TOKEN), None).await;
        assert_eq!(status, StatusCode::BAD_REQUEST, "{uri}");
    }
    for i in n..1001 {
        let _ = i;
        h.call("GET", "/sync?project_key=a", Some(TOKEN), None)
            .await;
    }
    let (status, _) = h
        .call(
            "GET",
            "/v1/audit/entries?start=0&end=1001",
            Some(TOKEN),
            None,
        )
        .await;
    assert_eq!(status, StatusCode::BAD_REQUEST, "1,001 is over the page");

    let page: AuditEntriesResponse = ok(h
        .call(
            "GET",
            "/v1/audit/entries?start=0&end=300",
            Some(TOKEN),
            None,
        )
        .await);
    let bytes: usize = page.entries.iter().map(String::len).sum();
    assert!(page.entries.len() < 300, "{} leaves", page.entries.len());
    assert!(bytes <= recall_wire::audit::MAX_PAGE_BYTES, "{bytes} bytes");
    assert_eq!(page.end, page.start + page.entries.len() as u64);
    let next: AuditEntriesResponse = ok(h
        .call(
            "GET",
            &format!("/v1/audit/entries?start={}&end=300", page.end),
            Some(TOKEN),
            None,
        )
        .await);
    let first: Value = serde_json::from_str(&next.entries[0]).unwrap();
    assert_eq!(
        first["seq"],
        json!(page.end),
        "the next page goes on from end"
    );
}

// ---------------------------------------------------------------------------
// verifying offline
// ---------------------------------------------------------------------------

/// A log with every kind of leaf — a device approved by code, a device an
/// authkey enrolled, both signing, the second swept — verifies offline:
/// every signature against the key its own device leaf carries, even for a
/// device no longer in the `devices` table.
async fn a_full_log() -> (Harness, Vec<String>) {
    let h = harness();
    let mut laptop = Machine::new(11);
    h.enrol(&mut laptop, "laptop", "admin").await;
    let push = json!({"project_key":"a/b","file_path":"m.md","content":"hi"});
    assert_eq!(
        h.signed(&laptop, "POST", "/sync", Some(push)).await.0,
        StatusCode::OK
    );
    assert_eq!(
        h.signed(&laptop, "GET", "/sync?project_key=a%2Fb", None)
            .await
            .0,
        StatusCode::OK
    );
    let delete = json!({"project_key":"a/b","file_path":"m.md","deleted":true});
    assert_eq!(
        h.signed(&laptop, "POST", "/sync", Some(delete)).await.0,
        StatusCode::OK
    );

    let key: AuthkeyCreated = ok(h
        .signed(
            &laptop,
            "POST",
            devices::AUTHKEYS_PATH,
            Some(json!({"tag": "cloud", "expires_in_days": 90, "max_devices": 3})),
        )
        .await);
    let mut cloud = Machine::new(12);
    h.enrol_with(&mut cloud, &key.key).await;
    assert_eq!(
        h.signed(&cloud, "GET", "/sync?project_key=a/b", None)
            .await
            .0,
        StatusCode::OK
    );
    let push = json!({"project_key":"a/b","file_path":"c.md","content":"from the cloud"});
    assert_eq!(
        h.signed(&cloud, "POST", "/sync", Some(push)).await.0,
        StatusCode::OK
    );

    let mut phone = Machine::new(13);
    let pending: EnrollPending = ok(h
        .call(
            "POST",
            devices::ENROLL_PATH,
            None,
            Some(json!({"name": "phone", "public_key": phone.public_key()})),
        )
        .await);
    let approved: Device = ok(h
        .signed(
            &laptop,
            "POST",
            devices::APPROVE_PATH,
            Some(json!({"user_code": pending.user_code.to_lowercase(), "scope": "sync"})),
        )
        .await);
    phone.id = approved.id;
    assert_eq!(
        h.signed(&laptop, "POST", &revoke_device_path(&phone.id), None)
            .await
            .0,
        StatusCode::OK
    );
    let other: EnrollPending = ok(h
        .call(
            "POST",
            devices::ENROLL_PATH,
            None,
            Some(json!({"name": "tablet", "public_key": Machine::new(14).public_key()})),
        )
        .await);
    assert_eq!(
        h.signed(
            &laptop,
            "POST",
            devices::DENY_PATH,
            Some(json!({"user_code": other.user_code}))
        )
        .await
        .0,
        StatusCode::OK
    );

    h.age_ephemeral_devices();
    assert_eq!(h.server.sweep_devices().unwrap().0, 1);
    assert_eq!(
        h.signed(
            &laptop,
            "POST",
            &revoke_authkey_path(&key.id),
            Some(json!({"revoke_devices": false}))
        )
        .await
        .0,
        StatusCode::OK
    );
    let leaves = h.leaf_strings().await;
    (h, leaves)
}

#[tokio::test]
async fn a_signed_log_verifies_offline_even_after_a_sweep() {
    let (h, leaves) = a_full_log().await;
    let actions: Vec<String> = leaves
        .iter()
        .map(|l| {
            serde_json::from_str::<Value>(l).unwrap()["action"]
                .as_str()
                .unwrap()
                .to_string()
        })
        .collect();
    for want in [
        "approve",
        "push",
        "pull",
        "delete",
        "authkey_create",
        "enroll",
        "revoke",
        "deny",
        "sweep",
        "authkey_revoke",
    ] {
        assert!(
            actions.iter().any(|a| a == want),
            "no {want} in {actions:?}"
        );
    }
    let swept: Value = serde_json::from_str(
        leaves
            .iter()
            .find(|l| l.contains("\"action\":\"sweep\""))
            .unwrap(),
    )
    .unwrap();
    assert!(h
        .store
        .device(swept["subject"]["device_id"].as_str().unwrap())
        .unwrap()
        .is_none());

    let export = h.export().await;
    let (code, out) = verify(&export, &[]);
    assert_eq!(code, 0, "{out}");
    assert!(
        out.contains("10 signed leaves, every signature checked"),
        "{out}"
    );

    // And with every earlier checkpoint as one saved along the way.
    let mut args = Vec::new();
    for size in [1, 5, leaves.len() - 1] {
        args.push(checkpoint_of(&leaves[..size]));
    }
    let args: Vec<&str> = args.iter().map(String::as_str).collect();
    let (code, out) = verify(&export, &args);
    assert_eq!(code, 0, "{out}");
}

/// The tree check alone: a changed byte, a dropped leaf, two swapped ones,
/// or a saved checkpoint the log does not extend.
#[tokio::test]
async fn an_export_verifies_offline_and_tampering_is_caught() {
    let (h, _) = a_full_log().await;
    let export = h.export().await;
    assert_eq!(verify(&export, &[]).0, 0);

    let changed_byte = export.replacen("c.md", "d.md", 1);
    assert_eq!(verify(&changed_byte, &[]).0, 1, "a changed byte");

    let lines: Vec<&str> = export.lines().collect();
    let mut dropped = lines[..3].to_vec();
    dropped.extend_from_slice(&lines[4..]);
    assert_eq!(
        verify(&(dropped.join("\n") + "\n"), &[]).0,
        1,
        "a missing leaf"
    );

    let mut swapped = lines.clone();
    swapped.swap(3, 4);
    assert_eq!(
        verify(&(swapped.join("\n") + "\n"), &[]).0,
        1,
        "two swapped"
    );

    let size = lines.len() - 1;
    let bogus = format!("--checkpoint={size}:AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=");
    let (code, out) = verify(&export, &[&bogus]);
    assert_eq!(code, 1, "a checkpoint the log does not extend: {out}");
    assert!(out.contains("does not extend"), "{out}");
}

/// Each way a server could make a log that is internally consistent — its
/// own root recomputed, every earlier checkpoint still extended — yet says
/// something a device never asked for, and the verifier's answer to it.
#[tokio::test]
async fn forged_exports_are_refused() {
    let (_h, honest) = a_full_log().await;
    let saved = checkpoint_of(&honest);
    let parsed: Vec<Value> = honest
        .iter()
        .map(|l| serde_json::from_str(l).unwrap())
        .collect();
    let find = |action: &str, kind: &str| -> Value {
        parsed
            .iter()
            .find(|l| l["action"] == action && l["actor"]["kind"] == kind)
            .unwrap_or_else(|| panic!("no {action} by a {kind}"))
            .clone()
    };
    let (push, pull, approve) = (
        find("push", "device"),
        find("pull", "device"),
        find("approve", "operator"),
    );

    // The honest log with `extra` appended (renumbered to follow it, and
    // dated as the last leaf is), its root recomputed: a server that went on
    // writing, never rewriting, so the saved checkpoint still holds.
    let last_at = parsed.last().unwrap()["at"].clone();
    let appended = |extra: Vec<Value>| -> String {
        let mut leaves = honest.clone();
        for mut leaf in extra {
            leaf["seq"] = json!(leaves.len());
            leaf["at"] = last_at.clone();
            leaves.push(serde_json::to_string(&leaf).unwrap());
        }
        export_of(&leaves)
    };
    // The honest log with the leaf at `leaf`'s own seq replaced by it: a
    // rewrite, so no saved checkpoint survives it, but one a server with
    // no checkpoint against it could get away with were signatures and
    // requests not checked.
    let in_place = |leaf: Value| -> String {
        let mut leaves = honest.clone();
        let at = leaf["seq"].as_u64().unwrap() as usize;
        leaves[at] = serde_json::to_string(&leaf).unwrap();
        export_of(&leaves)
    };
    let refused = |name: &str, export: String, args: &[&str], want: &str| {
        let (code, out) = verify(&export, args);
        assert_eq!(code, 1, "{name} was accepted: {out}");
        assert!(out.contains(want), "{name}: wanted {want:?} in {out}");
    };

    // A device's action with no request at all.
    let mut a = push.clone();
    a["action"] = json!("delete");
    a["subject"]["file_path"] = json!("topics/secret.md");
    a["subject"]["deleted"] = json!(true);
    a["request"] = Value::Null;
    refused(
        "no request",
        appended(vec![a]),
        &[&saved],
        "a device's request is not an object",
    );

    // A genuine signed request copied onto another action and subject.
    let mut b = push.clone();
    b["action"] = json!("delete");
    b["subject"]["file_path"] = json!("topics/secret.md");
    b["subject"]["deleted"] = json!(true);
    refused(
        "replayed onto another subject",
        appended(vec![b]),
        &[&saved],
        "a replay",
    );

    // The same genuine request, again and again.
    refused(
        "the same request ten times",
        appended(vec![push.clone(); 10]),
        &[&saved],
        "a replay",
    );

    // A body_sha256 that is not the digest the device signed.
    let mut c = push.clone();
    c["request"]["body_sha256"] = json!("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=");
    refused(
        "a digest the device never signed",
        in_place(c),
        &[],
        "content-digest",
    );

    // A pull's signed request under a push.
    let mut d = push.clone();
    d["request"] = pull["request"].clone();
    refused(
        "a pull's signature on a push",
        in_place(d),
        &[],
        "not a push",
    );

    // A pull whose subject is not the project the device asked for.
    let mut d2 = pull.clone();
    d2["subject"]["project_key"] = json!("someone/else");
    refused(
        "another project's pull",
        in_place(d2),
        &[],
        "does not name the pulled project",
    );

    // The operator approving an existing device id again, under a key the
    // server holds: every later "signature" would then check against it.
    let mut e = approve.clone();
    e["subject"]["user_code"] = json!("BCDF-GHJK");
    refused(
        "a second approve",
        appended(vec![e]),
        &[&saved],
        "approved or enrolled before",
    );

    // A keyid that is not the actor.
    let mut f = push.clone();
    f["actor"]["id"] = json!("dev_someoneelse");
    refused(
        "a keyid that is not the actor",
        in_place(f),
        &[],
        "not the actor",
    );

    // A signature changed by one bit: the Ed25519 check itself.
    let mut g = push.clone();
    let sig = g["request"]["signature"].as_str().unwrap().to_string();
    let flipped = if sig.starts_with('A') { "B" } else { "A" };
    g["request"]["signature"] = json!(format!("{flipped}{}", &sig[1..]));
    refused("a bad signature", in_place(g), &[], "does not verify");

    // A request signed before the device that signed it existed.
    let approve_at = honest
        .iter()
        .position(|l| l.contains("\"action\":\"approve\""))
        .unwrap();
    let push_at = honest
        .iter()
        .position(|l| l.contains("\"action\":\"push\""))
        .unwrap();
    assert!(approve_at < push_at);
    let mut early: Vec<Value> = parsed.clone();
    early.swap(approve_at, push_at);
    let early: Vec<String> = early
        .into_iter()
        .enumerate()
        .map(|(i, mut l)| {
            l["seq"] = json!(i);
            serde_json::to_string(&l).unwrap()
        })
        .collect();
    refused(
        "signed before approval",
        export_of(&early),
        &[],
        "no earlier approve or enroll leaf",
    );

    // A seq that is true rather than 1, or 3.0 rather than 3: the same
    // number to a loose reader.
    for (from, to) in [
        ("\"seq\":1,", "\"seq\":true,"),
        ("\"seq\":3,", "\"seq\":3.0,"),
    ] {
        let mut loose = honest.clone();
        let i = loose.iter().position(|l| l.contains(from)).unwrap();
        loose[i] = loose[i].replacen(from, to, 1);
        refused(to, export_of(&loose), &[], "seq is");
    }

    // A seq that is off by one, correctly hashed: the tree cannot tell.
    let mut renumbered = honest.clone();
    renumbered[2] = renumbered[2].replacen("\"seq\":2,", "\"seq\":7,", 1);
    refused(
        "a wrong seq",
        export_of(&renumbered),
        &[],
        "seq is 7 at position 2",
    );

    // A key given twice, which readers resolve differently.
    let mut dup = honest.clone();
    let i = dup
        .iter()
        .position(|l| l.contains("\"action\":\"pull\""))
        .unwrap();
    dup[i] = dup[i].replacen(
        "\"action\":\"pull\"",
        "\"action\":\"pull\",\"action\":\"push\"",
        1,
    );
    refused("a key given twice", export_of(&dup), &[], "appears twice");

    // An operator's leaf dressed with a request, and an authkey's.
    let mut h1 = approve.clone();
    h1["request"] = push["request"].clone();
    refused(
        "an operator with a request",
        appended(vec![h1]),
        &[],
        "signs nothing",
    );

    // A checkpoint line claiming more leaves than follow it.
    let export = export_of(&honest);
    let (first, rest) = export.split_once('\n').unwrap();
    let (size, root) = first.split_once(' ').unwrap();
    let more = format!("{} {root}\n{rest}", size.parse::<u64>().unwrap() + 1);
    refused(
        "a size that is not the count",
        more,
        &[],
        "checkpoint says tree_size",
    );

    // A request that is not an object at all.
    let mut j = push.clone();
    j["request"] = json!("x");
    refused(
        "a string for a request",
        appended(vec![j]),
        &[],
        "is not an object",
    );

    // `at` going back.
    let mut k = parsed.last().unwrap().clone();
    k["at"] = json!("2000-01-01T00:00:00.000Z");
    refused(
        "at going back",
        in_place(k),
        &[],
        "earlier than the leaf before it",
    );

    // A body that asked for something else than the leaf says was done.
    let mut m = find("approve", "device");
    m["subject"]["scope"] = json!("admin");
    refused(
        "an approve for another scope",
        in_place(m),
        &[],
        "another scope",
    );

    // A revoke of another device than the one the signed path names.
    let mut r = find("revoke", "device");
    r["subject"]["device_id"] = approve["subject"]["device_id"].clone();
    refused(
        "a revoke of another device",
        in_place(r),
        &[],
        "not a revoke",
    );

    // The honest log still verifies, so every refusal above is the forgery.
    assert_eq!(verify(&export_of(&honest), &[&saved]).0, 0);
}

/// The verifier never skips signatures unless told to: asked for a
/// `cryptography` it cannot load, it stops with exit 2; left to choose, it
/// uses its own Ed25519; only `--no-signatures` skips them, and says so.
#[tokio::test]
async fn the_verifier_never_skips_signatures_quietly() {
    let (_h, honest) = a_full_log().await;
    let mut forged = honest.clone();
    let at = forged
        .iter()
        .position(|l| l.contains("\"action\":\"push\""))
        .unwrap();
    let mut leaf: Value = serde_json::from_str(&forged[at]).unwrap();
    let sig = leaf["request"]["signature"].as_str().unwrap().to_string();
    let flipped = if sig.starts_with('A') { "B" } else { "A" };
    leaf["request"]["signature"] = json!(format!("{flipped}{}", &sig[1..]));
    forged[at] = serde_json::to_string(&leaf).unwrap();
    let bad = export_of(&forged);

    // A `cryptography` that cannot be imported, as on a machine without it.
    let fake = tempfile::tempdir().unwrap();
    std::fs::create_dir(fake.path().join("cryptography")).unwrap();
    std::fs::write(
        fake.path().join("cryptography/__init__.py"),
        "raise ImportError('not installed')\n",
    )
    .unwrap();
    let env = [("PYTHONPATH", fake.path().to_str().unwrap())];

    let (code, out) = verify_with(&bad, &["--ed25519=cryptography"], &env);
    assert_eq!(code, 2, "asked for, missing: {out}");
    let (code, out) = verify_with(&bad, &["--ed25519=auto"], &env);
    assert_eq!(code, 1, "left to choose, it still checks: {out}");
    assert!(out.contains("using the built-in Ed25519"), "{out}");
    let (code, out) = verify_with(&export_of(&honest), &["--ed25519=auto"], &env);
    assert_eq!(code, 0, "{out}");

    let (code, out) = verify(&bad, &["--no-signatures"]);
    assert_eq!(code, 0, "{out}");
    assert!(out.contains("NOT checked (--no-signatures)"), "{out}");

    let (code, out) = verify("", &["--self-test"]);
    assert_eq!(code, 0, "RFC 8032's vectors: {out}");
}

// ---------------------------------------------------------------------------
// the merge queue
// ---------------------------------------------------------------------------

/// A log with every job action in it, each the way it happens: a stale push
/// queued for a worker; the worker's claim, its lease running out, and the
/// server's `job_result` saying so; the worker's second claim and its merge;
/// another push queued, the worker revoked, and the server failing that job
/// with nothing left to merge it; an admin device's retry. Answers the job
/// ids beside the leaves.
async fn a_queue_log() -> (Harness, Vec<String>, String, String) {
    let h = harness_with(|cfg| cfg.merge_enabled = true);
    // Checked, and not logged in: nothing is merged inline, and a queue left
    // with no worker is failed rather than merged here.
    h.server.set_claude_status(Status {
        checked_at: "2026-10-02T09:13:40.002Z".into(),
        available: true,
        logged_in: false,
        error: String::new(),
    });
    let mut worker = Machine::new(21);
    h.enrol(&mut worker, "worker", "worker").await;
    let mut laptop = Machine::new(22);
    h.enrol(&mut laptop, "laptop", "admin").await;
    let older = recall_wire::content_sha256("an older version");
    let push = |content: &str| {
        json!({"project_key": "acme/app", "file_path": "topics/auth.md", "content": content,
               "source_env": "laptop", "base_sha256": older})
    };
    assert_eq!(
        h.call("POST", "/sync", Some(TOKEN), Some(push("A")))
            .await
            .0,
        StatusCode::OK
    );
    let queued: Value = ok(h.signed(&laptop, "POST", "/sync", Some(push("B"))).await);
    let first = queued["merge_job"].as_str().unwrap().to_string();

    let claim = json!({"kinds": ["merge"], "wait_seconds": 0, "lease_seconds": 60});
    let claimed: Value = ok(h
        .signed(&worker, "POST", wire_jobs::CLAIM_PATH, Some(claim.clone()))
        .await);
    assert_eq!(claimed["job"]["id"], json!(first));
    // Its lease runs out, as if a minute had passed.
    let conn = rusqlite::Connection::open(h.dir.path().join("recall.db")).unwrap();
    conn.execute(
        "UPDATE jobs SET lease_expires_at = '2000-01-01T00:00:00.000Z' WHERE id = ?1",
        [&first],
    )
    .unwrap();
    // The next claim finds it run out, and puts the job back to wait out
    // its retry delay, which the next update waits out too.
    let claimed: Value = ok(h
        .signed(&worker, "POST", wire_jobs::CLAIM_PATH, Some(claim.clone()))
        .await);
    assert_eq!(claimed["job"], Value::Null);
    conn.execute(
        "UPDATE jobs SET not_before = '2000-01-01T00:00:00.000Z' WHERE id = ?1",
        [&first],
    )
    .unwrap();
    let claimed: Value = ok(h
        .signed(&worker, "POST", wire_jobs::CLAIM_PATH, Some(claim))
        .await);
    assert_eq!(claimed["job"]["attempt"], json!(2));
    let lease = claimed["job"]["lease_id"].as_str().unwrap();
    let settled: Value = ok(h
        .signed(
            &worker,
            "POST",
            &wire_jobs::result_path(&first),
            Some(json!({"lease_id": lease, "merge": {"content": "A and B"}})),
        )
        .await);
    assert_eq!(settled["state"], json!("done"));

    let queued: Value = ok(h.signed(&laptop, "POST", "/sync", Some(push("C"))).await);
    let second = queued["merge_job"].as_str().unwrap().to_string();
    assert_eq!(
        h.signed(&laptop, "POST", &revoke_device_path(&worker.id), None)
            .await
            .0,
        StatusCode::OK
    );
    // The revocation starts a drain in the background.
    for _ in 0..200 {
        if !h.store.jobs(Some("failed"), 10).unwrap().is_empty() {
            break;
        }
        tokio::time::sleep(Duration::from_millis(10)).await;
    }
    assert_eq!(
        h.signed(&laptop, "POST", &wire_jobs::retry_path(&second), None)
            .await
            .0,
        StatusCode::OK
    );
    let leaves = h.leaf_strings().await;
    (h, leaves, first, second)
}

/// Every job action appends its leaf: who acted, which job, what it came
/// to; and a push queued for the worker names its job.
#[tokio::test]
async fn every_job_action_appends_its_leaf() {
    let (_h, leaves, first, second) = a_queue_log().await;
    let parsed: Vec<Value> = leaves
        .iter()
        .map(|l| serde_json::from_str(l).unwrap())
        .collect();
    let jobs: Vec<String> = parsed
        .iter()
        .filter(|l| {
            l["action"].as_str().unwrap().starts_with("job_")
                || l["subject"]["merge_job"].is_string()
        })
        .map(|l| {
            let job = l["subject"]["job_id"]
                .as_str()
                .or(l["subject"]["merge_job"].as_str())
                .unwrap();
            let job = if job == first {
                "first"
            } else if job == second {
                "second"
            } else {
                job
            };
            format!(
                "{} {} {job}",
                l["action"].as_str().unwrap(),
                l["actor"]["kind"].as_str().unwrap()
            )
        })
        .collect();
    assert_eq!(
        jobs,
        [
            "push device first",
            "job_claim device first",
            "job_result server first",
            "job_claim device first",
            "job_result device first",
            "push device second",
            "job_result server second",
            "job_retry device second",
        ],
        "{parsed:#?}"
    );
    let of = |action: &str, kind: &str, job: &str| -> &Value {
        parsed
            .iter()
            .find(|l| {
                l["action"] == action && l["actor"]["kind"] == kind && l["subject"]["job_id"] == job
            })
            .unwrap()
    };
    let claim = of("job_claim", "device", &first);
    assert_eq!(claim["subject"]["attempt"], json!(1));
    assert_eq!(claim["subject"]["file_path"], json!("topics/auth.md"));
    assert!(claim["request"]["signature_base"]
        .as_str()
        .unwrap()
        .contains("/v1/jobs/claim"));
    assert!(
        !leaves.iter().any(|l| l.contains("lse_")),
        "a lease id is in the log"
    );
    let expired = of("job_result", "server", &first);
    assert_eq!(expired["subject"]["state"], json!("queued"));
    assert_eq!(expired["request"], Value::Null);
    let merged = of("job_result", "device", &first);
    assert_eq!(merged["subject"]["state"], json!("done"));
    assert_eq!(
        merged["subject"]["stored_sha256"],
        json!(recall_wire::content_sha256("A and B"))
    );
    assert_eq!(
        merged["request"]["body"],
        Value::Null,
        "the merged file is not kept"
    );
    let failed = of("job_result", "server", &second);
    assert_eq!(failed["subject"]["state"], json!("failed"));
    assert_eq!(failed["subject"]["stored_sha256"], Value::Null);
}

/// A log with the queue in it verifies offline, and each way a server could
/// misstate a job is refused.
#[tokio::test]
async fn a_queue_log_verifies_offline_and_forged_job_leaves_are_refused() {
    let (h, honest, first, _) = a_queue_log().await;
    let (code, out) = verify(&h.export().await, &[]);
    assert_eq!(code, 0, "{out}");

    let parsed: Vec<Value> = honest
        .iter()
        .map(|l| serde_json::from_str(l).unwrap())
        .collect();
    let position = |action: &str, kind: &str, n: usize| -> usize {
        parsed
            .iter()
            .enumerate()
            .filter(|(_, l)| l["action"] == action && l["actor"]["kind"] == kind)
            .nth(n)
            .unwrap_or_else(|| panic!("no {action} by a {kind}"))
            .0
    };
    // `honest` with the leaf at `at` changed by `edit`, or, given no edit,
    // removed and the rest renumbered; the root recomputed.
    let forged = |at: usize, edit: Option<&dyn Fn(&mut Value)>| -> String {
        let mut leaves: Vec<Value> = parsed.clone();
        match edit {
            Some(edit) => edit(&mut leaves[at]),
            None => {
                leaves.remove(at);
            }
        }
        let leaves: Vec<String> = leaves
            .into_iter()
            .enumerate()
            .map(|(seq, mut l)| {
                l["seq"] = json!(seq);
                serde_json::to_string(&l).unwrap()
            })
            .collect();
        export_of(&leaves)
    };
    let refused = |name: &str, export: String, want: &str| {
        let (code, out) = verify(&export, &[]);
        assert_eq!(code, 1, "{name} was accepted: {out}");
        assert!(out.contains(want), "{name}: wanted {want:?} in {out}");
    };

    refused(
        "the lease that ran out, left out",
        forged(position("job_result", "server", 0), None),
        &format!("{first} was claimed while dev_"),
    );
    refused(
        "a worker's claim that was never leased to it, left out",
        forged(position("job_claim", "device", 1), None),
        "which does not hold it",
    );
    refused(
        "the worker approved as a sync device",
        forged(
            position("approve", "operator", 0),
            Some(&|l: &mut Value| l["subject"]["scope"] = json!("sync")),
        ),
        "whose scope is sync",
    );
    refused(
        "an attempt miscounted",
        forged(
            position("job_claim", "device", 1),
            Some(&|l: &mut Value| l["subject"]["attempt"] = json!(1)),
        ),
        "at attempt 1, not 2",
    );
    refused(
        "a worker's result moved onto another job",
        forged(
            position("job_result", "device", 0),
            Some(&|l: &mut Value| l["subject"]["job_id"] = json!("job_other")),
        ),
        "not a job_result",
    );
    refused(
        "a retry of a job that had not failed",
        forged(
            position("job_result", "server", 1),
            Some(&|l: &mut Value| l["subject"]["state"] = json!("queued")),
        ),
        "was retried while queued",
    );
    refused(
        "a merged push that queued a job too",
        forged(
            position("push", "device", 0),
            Some(&|l: &mut Value| l["subject"]["merged"] = json!(true)),
        ),
        "a merged push queued a merge job",
    );
    refused(
        "a job no push queued",
        forged(
            position("push", "device", 0),
            Some(&|l: &mut Value| l["subject"]["merge_job"] = Value::Null),
        ),
        "which no push or result queued",
    );
}

// ---------------------------------------------------------------------------
// evaluations
// ---------------------------------------------------------------------------

/// A log with evaluations in it: one an admin device asks for and a worker
/// makes, one on the schedule, whose attempt fails, and one the operator
/// asks for with the contradiction check. Answers the admin device's run's
/// job.
async fn an_evaluation_log() -> (Harness, Vec<String>, String) {
    let h = harness();
    let mut worker = Machine::new(31);
    h.enrol(&mut worker, "worker", "worker").await;
    let mut laptop = Machine::new(32);
    h.enrol(&mut laptop, "laptop", "admin").await;
    let push = json!({"project_key": "acme/app", "file_path": "MEMORY.md",
                      "content": "- [Gone](gone.md)\n", "source_env": "laptop"});
    let _: Value = ok(h.signed(&laptop, "POST", "/sync", Some(push)).await);

    let asked: Value = ok(h
        .signed(
            &laptop,
            "POST",
            recall_wire::evaluations::EVALUATIONS_PATH,
            Some(json!({"projects": ["acme/app", "acme/app"]})),
        )
        .await);
    let job = asked["job"].as_str().unwrap().to_string();
    let claim = json!({"kinds": ["evaluate"], "wait_seconds": 0, "lease_seconds": 60});
    let claimed: Value = ok(h
        .signed(&worker, "POST", wire_jobs::CLAIM_PATH, Some(claim.clone()))
        .await);
    assert_eq!(claimed["job"]["id"], json!(job));
    let lease = claimed["job"]["lease_id"].as_str().unwrap();
    let report = json!({"lease_id": lease, "evaluate": {
        "findings": [{"id": "f1", "kind": "dead_link", "severity": "medium",
                      "project_key": "acme/app", "file_path": "MEMORY.md",
                      "lines": [1, 1], "related": []}],
        "details": {"findings": {"f1": {"excerpt": "- [Gone](gone.md)\n"}}}}});
    let _: Value = ok(h
        .signed(&worker, "POST", &wire_jobs::result_path(&job), Some(report))
        .await);

    // On the schedule: its first attempt fails, and its second, once the
    // retry delay is past, makes the report.
    assert!(h.server.run_scheduled_evaluation().unwrap().is_some());
    let claimed: Value = ok(h
        .signed(&worker, "POST", wire_jobs::CLAIM_PATH, Some(claim.clone()))
        .await);
    let second = claimed["job"]["id"].as_str().unwrap().to_string();
    let lease = claimed["job"]["lease_id"].as_str().unwrap();
    let _: Value = ok(h
        .signed(
            &worker,
            "POST",
            &wire_jobs::result_path(&second),
            Some(json!({"lease_id": lease, "error": "claude timed out"})),
        )
        .await);
    rusqlite::Connection::open(h.dir.path().join("recall.db"))
        .unwrap()
        .execute(
            "UPDATE jobs SET not_before = '2000-01-01T00:00:00.000Z' WHERE id = ?1",
            [&second],
        )
        .unwrap();
    let claimed: Value = ok(h
        .signed(&worker, "POST", wire_jobs::CLAIM_PATH, Some(claim))
        .await);
    assert_eq!(claimed["job"]["attempt"], json!(2));
    let lease = claimed["job"]["lease_id"].as_str().unwrap();
    let _: Value = ok(h
        .signed(
            &worker,
            "POST",
            &wire_jobs::result_path(&second),
            Some(json!({"lease_id": lease, "evaluate": {"findings": [], "details": {}}})),
        )
        .await);

    let _: Value = ok(h
        .call(
            "POST",
            recall_wire::evaluations::EVALUATIONS_PATH,
            Some(TOKEN),
            Some(json!({"contradictions": true})),
        )
        .await);
    let leaves = h.leaf_strings().await;
    (h, leaves, job)
}

/// A kept body the server reads as no body at all must be one both
/// verifiers read the same way: only spaces, tabs, carriage returns and
/// line feeds. A no-break space, a form feed, a vertical tab or a line
/// separator is refused as not JSON, never kept in a leaf the verifiers
/// would then refuse forever.
#[tokio::test]
async fn a_blank_body_is_blank_to_the_verifiers_too() {
    let h = harness();
    let mut worker = Machine::new(33);
    h.enrol(&mut worker, "worker", "worker").await;
    let mut laptop = Machine::new(34);
    h.enrol(&mut laptop, "laptop", "admin").await;
    let key = h.authkey(true).await;
    for body in ["\u{a0}", "\x0c", "\x0b", "\u{2028}", " \x0c\n"] {
        for path in [
            recall_wire::evaluations::EVALUATIONS_PATH.to_string(),
            revoke_authkey_path(&key.id),
        ] {
            let (status, _, _) = h
                .signed_raw(&laptop, "POST", &path, body.as_bytes().to_vec())
                .await;
            assert_eq!(status, StatusCode::BAD_REQUEST, "{path} took {body:?}");
        }
    }
    // What both read as blank still is.
    let (status, _, _) = h
        .signed_raw(
            &laptop,
            "POST",
            recall_wire::evaluations::EVALUATIONS_PATH,
            b" \t\r\n".to_vec(),
        )
        .await;
    assert_eq!(status, StatusCode::OK);
    let (status, _, _) = h
        .signed_raw(
            &laptop,
            "POST",
            &revoke_authkey_path(&key.id),
            b"\r\n".to_vec(),
        )
        .await;
    assert_eq!(status, StatusCode::OK);
    let (code, out) = verify(&h.export().await, &[]);
    assert_eq!(code, 0, "{out}");
}

/// Asking for an evaluation appends one `evaluate` leaf, which names the
/// run, its job and what was asked, and keeps an admin device's body; the
/// job's claim and result are the leaves any job's are, and none of them
/// holds what the report found.
#[tokio::test]
async fn an_evaluation_appends_its_leaves_and_no_finding() {
    let (_h, leaves, job) = an_evaluation_log().await;
    let parsed: Vec<Value> = leaves
        .iter()
        .map(|l| serde_json::from_str(l).unwrap())
        .collect();
    let actions: Vec<String> = parsed
        .iter()
        .filter(|l| l["action"] == "evaluate" || l["subject"]["job_id"] == json!(job))
        .map(|l| {
            format!(
                "{} {}",
                l["action"].as_str().unwrap(),
                l["actor"]["kind"].as_str().unwrap()
            )
        })
        .collect();
    assert_eq!(
        actions,
        [
            "evaluate device",
            "job_claim device",
            "job_result device",
            "evaluate server",
            "evaluate operator",
        ]
    );
    let asked = parsed.iter().find(|l| l["action"] == "evaluate").unwrap();
    assert_eq!(asked["subject"]["projects"], json!(["acme/app"]));
    assert_eq!(asked["subject"]["job_id"], json!(job));
    assert_eq!(
        asked["request"]["body"],
        json!(r#"{"projects":["acme/app","acme/app"]}"#)
    );
    let result = parsed
        .iter()
        .find(|l| l["action"] == "job_result" && l["subject"]["job_id"] == json!(job))
        .unwrap();
    assert_eq!(result["subject"]["state"], json!("done"));
    assert_eq!(result["subject"]["stored_sha256"], Value::Null);
    assert!(
        !leaves
            .iter()
            .any(|l| l.contains("dead_link") || l.contains("Gone")),
        "a finding is in the log"
    );
}

/// A log with evaluations verifies offline, and each way a server could
/// misstate one is refused.
#[tokio::test]
async fn an_evaluation_log_verifies_offline_and_forgeries_are_refused() {
    let (h, honest, job) = an_evaluation_log().await;
    let (code, out) = verify(&h.export().await, &[]);
    assert_eq!(code, 0, "{out}");

    let parsed: Vec<Value> = honest
        .iter()
        .map(|l| serde_json::from_str(l).unwrap())
        .collect();
    let first = parsed
        .iter()
        .position(|l| l["action"] == "evaluate")
        .unwrap();
    let forged = |at: usize, edit: Option<&dyn Fn(&mut Value)>| -> String {
        let mut leaves: Vec<Value> = parsed.clone();
        match edit {
            Some(edit) => edit(&mut leaves[at]),
            None => {
                leaves.remove(at);
            }
        }
        let leaves: Vec<String> = leaves
            .into_iter()
            .enumerate()
            .map(|(seq, mut l)| {
                l["seq"] = json!(seq);
                serde_json::to_string(&l).unwrap()
            })
            .collect();
        export_of(&leaves)
    };
    let refused = |name: &str, export: String, want: &str| {
        let (code, out) = verify(&export, &[]);
        assert_eq!(code, 1, "{name} was accepted: {out}");
        assert!(out.contains(want), "{name}: wanted {want:?} in {out}");
    };
    refused(
        "a job no evaluation queued",
        forged(first, None),
        &format!("{job}, which no push or result queued"),
    );
    refused(
        "other projects than the body asked for",
        forged(
            first,
            Some(&|l: &mut Value| l["subject"]["projects"] = json!(["acme/web"])),
        ),
        "the body asked for other projects",
    );
    refused(
        "the contradiction check, which the body did not ask for",
        forged(
            first,
            Some(&|l: &mut Value| l["subject"]["contradictions"] = json!(true)),
        ),
        "the body asked for another contradictions",
    );
    refused(
        "a run an authkey asked for",
        forged(
            first,
            Some(&|l: &mut Value| {
                l["actor"] = json!({"kind": "authkey", "id": "ak_x", "tag": "cloud"});
                l["request"] = Value::Null;
            }),
        ),
        "a authkey cannot evaluate",
    );
    refused(
        "the laptop approved as a sync device",
        forged(
            parsed
                .iter()
                .position(|l| l["action"] == "approve" && l["subject"]["name"] == "laptop")
                .unwrap(),
            Some(&|l: &mut Value| l["subject"]["scope"] = json!("sync")),
        ),
        "does not have the admin scope",
    );
}

// ---------------------------------------------------------------------------
// the host's admin commands
// ---------------------------------------------------------------------------

/// Runs `recall-server admin` on the harness's database file, the way the
/// owner runs it on the host beside the running server. Answers what it
/// printed; it must succeed.
fn admin(h: &Harness, args: &[&str]) -> String {
    let out = Command::new(env!("CARGO_BIN_EXE_recall-server"))
        .arg("admin")
        .args(args)
        .env_clear()
        .env("RECALL_DB_PATH", h.dir.path().join("recall.db"))
        .env("RECALL_BACKUP_DIR", h.dir.path().join("backups"))
        .env("RECALL_MERGE_TIMEOUT_MS", "1")
        .stdin(std::process::Stdio::null())
        .output()
        .expect("recall-server runs");
    let said = format!(
        "{}{}",
        String::from_utf8_lossy(&out.stdout),
        String::from_utf8_lossy(&out.stderr)
    );
    assert_eq!(out.status.code(), Some(0), "{args:?}: {said}");
    said
}

/// A log with the host's rename, restore and remove in it, each made by
/// another process while the server runs and appends before and after: a
/// stale push queued for the worker, which leases it; the rename, which
/// closes that job, and the worker's result for it, which is answered as
/// recorded and appends nothing; a push under the new key, queued too; a
/// restore of the old key from the backup the rename took; a remove of the
/// new key, which closes the second job; and a pull. Answers the leaves,
/// the size of the log before the rename, and the two jobs.
async fn an_admin_log() -> (Harness, Vec<String>, u64, String, String) {
    let h = harness_with(|cfg| cfg.merge_enabled = true);
    h.server.set_claude_status(Status {
        checked_at: "2026-10-02T09:13:40.002Z".into(),
        available: true,
        logged_in: false,
        error: String::new(),
    });
    let mut worker = Machine::new(31);
    h.enrol(&mut worker, "worker", "worker").await;
    let mut laptop = Machine::new(32);
    h.enrol(&mut laptop, "laptop", "admin").await;
    let older = recall_wire::content_sha256("an older version");
    let push = |key: &str, content: &str| {
        json!({"project_key": key, "file_path": "topics/auth.md", "content": content,
               "source_env": "laptop", "base_sha256": older})
    };
    assert_eq!(
        h.call("POST", "/sync", Some(TOKEN), Some(push("acme/app", "A")))
            .await
            .0,
        StatusCode::OK
    );
    let queued: Value = ok(h
        .signed(&laptop, "POST", "/sync", Some(push("acme/app", "B")))
        .await);
    let first = queued["merge_job"].as_str().unwrap().to_string();
    let claim = json!({"kinds": ["merge"], "wait_seconds": 0, "lease_seconds": 600});
    let claimed: Value = ok(h
        .signed(&worker, "POST", wire_jobs::CLAIM_PATH, Some(claim))
        .await);
    assert_eq!(claimed["job"]["id"], json!(first));
    let lease = claimed["job"]["lease_id"].as_str().unwrap().to_string();

    let before = h.size();
    let said = admin(&h, &["rename", "acme/app", "acme/renamed", "--yes"]);
    assert!(
        said.contains(&format!("leaf {before} (admin_rename)")),
        "{said}"
    );
    assert_eq!(h.size(), before, "the server has not looked yet");

    // The worker's result lands on a job the rename closed.
    let settled: Value = ok(h
        .signed(
            &worker,
            "POST",
            &wire_jobs::result_path(&first),
            Some(json!({"lease_id": lease, "merge": {"content": "A and B"}})),
        )
        .await);
    assert_eq!(settled["state"], json!("done"));
    assert_eq!(settled["applied"], json!(false));
    assert_eq!(
        h.size(),
        before,
        "a result already recorded appends nothing"
    );

    let queued: Value = ok(h
        .signed(&laptop, "POST", "/sync", Some(push("acme/renamed", "C")))
        .await);
    let second = queued["merge_job"].as_str().unwrap().to_string();
    assert_eq!(h.size(), before + 2, "the rename's leaf, then the push's");

    let backup = std::fs::read_dir(h.dir.path().join("backups/admin"))
        .unwrap()
        .next()
        .unwrap()
        .unwrap()
        .path();
    admin(
        &h,
        &["restore", backup.to_str().unwrap(), "acme/app", "--yes"],
    );
    admin(&h, &["remove", "acme/renamed", "--yes"]);
    let (status, _) = h
        .call("GET", "/sync?project_key=acme/app", Some(TOKEN), None)
        .await;
    assert_eq!(status, StatusCode::OK);
    let leaves = h.leaf_strings().await;
    (h, leaves, before, first, second)
}

/// The host's changes are leaves in the one log the server keeps: each
/// takes the next seq, and the server's own next leaf follows it rather
/// than forking the tree, so the server's checkpoint and the file's agree.
#[tokio::test]
async fn the_hosts_changes_take_their_place_in_the_servers_log() {
    let (h, leaves, before, first, second) = an_admin_log().await;
    let parsed: Vec<Value> = leaves
        .iter()
        .map(|l| serde_json::from_str(l).unwrap())
        .collect();
    let after: Vec<String> = parsed[before as usize..]
        .iter()
        .map(|l| {
            format!(
                "{} {}",
                l["action"].as_str().unwrap(),
                l["actor"]["kind"].as_str().unwrap()
            )
        })
        .collect();
    assert_eq!(
        after,
        [
            "admin_rename host",
            "push device",
            "admin_restore host",
            "admin_remove host",
            "pull operator"
        ]
    );
    for (i, l) in parsed.iter().enumerate() {
        assert_eq!(l["seq"], json!(i));
    }
    let rename = &parsed[before as usize]["subject"];
    assert_eq!(rename["from"], "acme/app");
    assert_eq!(rename["to"], "acme/renamed");
    assert_eq!(rename["rows"], 1);
    assert_eq!(rename["jobs_closed"], json!([first]));
    let remove = &parsed[before as usize + 3]["subject"];
    assert_eq!(remove["jobs_closed"], json!([second]));
    let restore = &parsed[before as usize + 2]["subject"];
    assert_eq!(restore["added"], 1);
    assert_eq!(restore["source"], rename["backup"]);

    assert_eq!(
        h.store.audit_checkpoint(),
        Store::open(h.dir.path().join("recall.db"))
            .unwrap()
            .audit_checkpoint(),
        "one tree"
    );
}

/// A log with the host's changes in it verifies offline through the real
/// script, and each way of misstating one is refused.
#[tokio::test]
async fn a_log_with_the_hosts_changes_verifies_offline_and_forgeries_are_refused() {
    let (h, honest, before, first, _) = an_admin_log().await;
    let (code, out) = verify(&h.export().await, &[]);
    assert_eq!(code, 0, "{out}");

    let parsed: Vec<Value> = honest
        .iter()
        .map(|l| serde_json::from_str(l).unwrap())
        .collect();
    let (rename, restore, remove) = (before as usize, before as usize + 2, before as usize + 3);
    let forged = |at: usize, edit: &dyn Fn(&mut Value)| -> String {
        let mut leaves = parsed.clone();
        edit(&mut leaves[at]);
        let leaves: Vec<String> = leaves
            .iter()
            .map(|l| serde_json::to_string(l).unwrap())
            .collect();
        export_of(&leaves)
    };
    let refused = |name: &str, export: String, want: &str| {
        let (code, out) = verify(&export, &[]);
        assert_eq!(code, 1, "{name} was accepted: {out}");
        assert!(out.contains(want), "{name}: wanted {want:?} in {out}");
    };

    refused(
        "a rename that left its key's job open",
        forged(rename, &|l| l["subject"]["jobs_closed"] = json!([])),
        &format!("left {first} open"),
    );
    refused(
        "a rename closing a job no push queued",
        forged(rename, &|l| {
            l["subject"]["jobs_closed"] = json!([first.clone(), "job_never"])
        }),
        "closed job_never, which no push or result queued",
    );
    refused(
        "a rename of another key closing this key's job",
        forged(rename, &|l| l["subject"]["from"] = json!("other/key")),
        "a job of 'acme/app'",
    );
    refused(
        "a remove closing a job already closed",
        forged(remove, &|l| {
            l["subject"]["jobs_closed"]
                .as_array_mut()
                .unwrap()
                .push(json!(first.clone()))
        }),
        &format!("closed {first} once it was done"),
    );
    refused(
        "a rename credited to the server",
        forged(rename, &|l| l["actor"] = json!({"kind": "server"})),
        "a server cannot admin_rename",
    );
    refused(
        "a restore carrying a request",
        forged(restore, &|l| {
            l["request"] = json!({"body_sha256": "", "signature_base": "", "signature": "",
                                  "body": null})
        }),
        "the host signs nothing",
    );
    refused(
        "a rename that moved nothing",
        forged(rename, &|l| l["subject"]["rows"] = json!(0)),
        "changed no row",
    );
    refused(
        "a restore that changed nothing",
        forged(restore, &|l| l["subject"]["added"] = json!(0)),
        "changed no row",
    );
    refused(
        "a negative count",
        forged(remove, &|l| l["subject"]["rows"] = json!(-1)),
        "subject.rows is not a count",
    );
    refused(
        "a backup named by its path",
        forged(remove, &|l| {
            l["subject"]["backup"] = json!("/backups/admin/recall-x.db")
        }),
        "subject.backup is not a file name",
    );
    refused(
        "a rename onto its own key",
        forged(rename, &|l| l["subject"]["to"] = json!("acme/app")),
        "a rename onto the key it renames",
    );
    refused(
        "a remove without its backup",
        forged(remove, &|l| {
            l["subject"].as_object_mut().unwrap().remove("backup");
        }),
        "the subject has the keys",
    );
    refused(
        "a job closed twice in one leaf",
        forged(rename, &|l| {
            l["subject"]["jobs_closed"] = json!([first.clone(), first.clone()])
        }),
        "names a job twice",
    );
}