nsh-core 0.7.2

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

#[cfg(all(not(target_arch = "wasm32"), feature = "updater"))]
use std::{
    cell::{Cell, RefCell},
    path::PathBuf,
    rc::Rc,
    sync::Arc,
};

use crate::element::{Callback, JsError, JsObject, Later, System, codes, module};

/// Takes a piece of a body: its byte offset in the resource, the bytes, and
/// the resource's total size if known; an error stops it.
pub type UpdaterSink<'a> = &'a mut dyn FnMut(u64, &[u8], Option<u64>) -> Result<(), String>;

/// What the platform gives the module: the package's update configuration
/// and HTTP. Both block, and are called off the engine thread.
pub trait UpdaterHost: Send + Sync + 'static {
    /// The package's configuration file; `None` when it has none (a
    /// development run, an app without `"updater"` in `nsh.json`).
    fn config(&self) -> Option<Vec<u8>>;

    /// Gets `url` from byte `from` on, following redirects, handing each
    /// piece of the body to `sink` with its offset in the resource, until it
    /// fails or the body ends. With `from > 0` the host asks for the rest
    /// (`Range: bytes=<from>-`); a server that ignores the range sends the
    /// whole resource from offset `0`, and one that cannot satisfy it
    /// (`416`) sends no pieces. Offsets count the raw resource's bytes, so
    /// the host asks for no transparent compression.
    fn get(&self, url: &str, from: u64, sink: UpdaterSink<'_>) -> Result<(), String>;

    /// The signed package this run's code came from; `None` (the default):
    /// the bundle the app shipped with.
    fn running_package(&self) -> Option<PackageVersion> {
        None
    }

    /// Whether the app's launcher runs signed packages, so that a package
    /// [`verify_package`](Self::verify_package) accepts runs at the next
    /// start. `false` (the default): the host runs none.
    fn runs_packages(&self) -> bool {
        false
    }

    /// Verifies the package downloaded to `file` as the app's launcher
    /// will before running it: signed by a release key one of `roots`
    /// certifies, for app `app_id`, its build newer than `newer_than` and
    /// than the builds accepted in the trust state under `updates` (the
    /// app's updates directory), for this runtime. Records nothing. The
    /// default refuses: the host runs no packages.
    fn verify_package(
        &self,
        file: &std::path::Path,
        updates: &std::path::Path,
        app_id: &str,
        roots: &[String],
        newer_than: u64,
    ) -> Result<PackageVersion, String> {
        let _ = (file, updates, app_id, roots, newer_than);
        Err("this host runs no packages".into())
    }
}

/// A signed package of the app's code: its version and build
/// ([`UpdaterHost::running_package`]).
#[derive(Debug, Clone, Default, PartialEq, Eq)]
#[non_exhaustive]
pub struct PackageVersion {
    /// Its version string (`nsh.json` `"version"`).
    pub version: String,
    /// Its build number.
    pub build: u64,
}

/// Bytes downloaded, as `downloadUpdate`'s callback gets them.
#[derive(Debug, Clone, Copy, Default, PartialEq, JsObject)]
#[non_exhaustive]
pub(crate) struct UpdateProgress {
    /// Bytes received so far.
    pub(crate) received: f64,
    /// All of them, when known.
    pub(crate) total: Option<f64>,
}

/// Options of `checkForUpdate`.
#[derive(Debug, Clone, Default, JsObject)]
#[non_exhaustive]
pub(crate) struct UpdateCheckOptions {
    /// The name of a channel in `nsh.json`'s `"updater": { "channels" }`
    /// to check instead of the default source. Only names the package
    /// declares are accepted: the app's code chooses, the package says
    /// where.
    pub(crate) channel: Option<String>,
    /// The language the app shows (a BCP 47 tag such as `zh-TW`): `notes`
    /// is then the manifest's notes in it, or in its language (`zh`),
    /// downloading only that file, else the default notes.
    pub(crate) locale: Option<String>,
}

/// Options of `checkForPackage`.
#[derive(Debug, Clone, Default, JsObject)]
#[non_exhaustive]
pub(crate) struct PackageCheckOptions {
    /// As `checkForUpdate`'s: the language `notes` is in, when the
    /// manifest has it.
    pub(crate) locale: Option<String>,
}

/// A newer version of the app.
#[derive(Debug, Clone, Default, PartialEq, JsObject)]
#[non_exhaustive]
pub(crate) struct AppUpdate {
    /// Its version.
    pub(crate) version: String,
    /// What changed, as the manifest says it: in the check's `locale` when
    /// the manifest has it.
    pub(crate) notes: Option<String>,
    /// Where the user can get it by hand: the archive, or a store's page on
    /// iOS and Android. For `openUrl` when `canInstall` is false.
    pub(crate) url: String,
    /// Whether `downloadUpdate`, `installAndRestart` and `installOnQuit` work
    /// here: a package on the desktop, installed where the user can write.
    /// False on iOS and Android, and under a directory only the system
    /// writes.
    pub(crate) can_install: bool,
    /// Whether only the installed app is behind: the package this run's code
    /// came from is already this version or a later one, so the update
    /// brings a newer runtime and native shell, not newer app code. Apps that
    /// update by package can offer it as the quieter "update the app" step.
    pub(crate) shell: bool,
}

/// A newer build of the app's code, as a signed package.
#[derive(Debug, Clone, Default, PartialEq, JsObject)]
#[non_exhaustive]
pub(crate) struct PackageUpdate {
    /// Its version (`nsh.json` `"version"`).
    pub(crate) version: String,
    /// Its build number, newer than the running one.
    pub(crate) build: f64,
    /// What changed, as the manifest says it: in the check's `locale` when
    /// the manifest has it.
    pub(crate) notes: Option<String>,
    /// Its size in bytes, when the manifest says it.
    pub(crate) size: Option<f64>,
}

/// The signed package this run's code came from.
#[derive(Debug, Clone, Default, PartialEq, JsObject)]
#[non_exhaustive]
pub(crate) struct RunningPackage {
    /// Its version (`nsh.json` `"version"`).
    pub(crate) version: String,
    /// Its build number.
    pub(crate) build: f64,
}

/// The installed app the code runs in, and the runtime built into it.
#[derive(Debug, Clone, Default, PartialEq, JsObject)]
#[non_exhaustive]
pub(crate) struct ShellInfo {
    /// The installed app's version as it was packaged (`nsh.json`
    /// `"version"`; on iOS its `CFBundleShortVersionString`).
    pub(crate) version: Option<String>,
    /// The installed app's build number as it was packaged (`nsh.json`
    /// `"build"`; on iOS its `CFBundleVersion`).
    pub(crate) build: Option<f64>,
    /// The version of the NSH runtime built into the installed app.
    pub(crate) nsh_version: String,
    /// Whether the installed app runs signed packages of its code
    /// (`checkForPackage`): its launcher runs them and its `nsh.json` trusts
    /// package roots.
    pub(crate) packages: bool,
}

impl ShellInfo {
    /// From the shipped update configuration (`nsh-updater.json`), if any,
    /// and whether the launcher runs packages.
    fn new(config: Option<&[u8]>, runs_packages: bool) -> ShellInfo {
        // (version, build, trusts package roots); nothing updates on the web
        #[cfg(all(not(target_arch = "wasm32"), feature = "updater"))]
        let shipped = config
            .and_then(|bytes| nsh_updater::Config::parse(bytes).ok())
            .map(|config| {
                let roots = !config.package_roots().is_empty();
                (config.version, config.build.unwrap_or(1), roots)
            });
        #[cfg(any(target_arch = "wasm32", not(feature = "updater")))]
        let shipped: Option<(String, u64, bool)> = config.and(None);
        ShellInfo {
            version: shipped.as_ref().map(|(version, _, _)| version.clone()),
            build: shipped.as_ref().map(|(_, build, _)| *build as f64),
            nsh_version: env!("CARGO_PKG_VERSION").to_owned(),
            packages: runs_packages && shipped.is_some_and(|(_, _, roots)| roots),
        }
    }
}

/// Checking for a newer version of the app and installing it.
#[module]
pub(crate) struct Updater;

#[module(flat)]
impl Updater {
    /// Asks the source in `nsh.json`'s `"updater"` for a newer version:
    /// its `url` or `github`, or with `channel`, the manifest of that name
    /// in its `"channels"`. Resolves with the update, or `null` when the
    /// app is up to date, has no `"updater"`, or runs where apps do not
    /// update (the web, a development run). Rejects when the package
    /// declares no channel of that name, and when the source cannot be
    /// reached or its manifest is not acceptable: a bad signature, another
    /// app's id, a URL that is not HTTPS. `downloadUpdate`,
    /// `installAndRestart` and `installOnQuit` use what the last check
    /// found, whichever source.
    pub(crate) fn check_for_update(
        options: Option<UpdateCheckOptions>,
        sys: System,
    ) -> Later<Option<AppUpdate>> {
        let options = options.unwrap_or_default();
        Later::new(native::check(sys, options.channel, options.locale))
    }

    /// Downloads the update the last `checkForUpdate` found and checks it
    /// against the manifest, calling `onProgress` as bytes arrive. An
    /// interrupted transfer keeps its partial file keyed to the asset and
    /// resumes on the next call when the host supports HTTP `Range` requests,
    /// starting over when the server ignores the range or the file does not
    /// match. Rejects with `ERR_FEATURE_UNAVAILABLE_ON_PLATFORM` when it found
    /// none or `canInstall` is false, and when the download fails or does not
    /// match.
    pub(crate) fn download_update(
        #[args(progress)] on_progress: Option<Callback<UpdateProgress>>,
        sys: System,
    ) -> Later<()> {
        let on_progress = on_progress.map(|callback| {
            Box::new(move |progress| callback.post(progress)) as Box<dyn Fn(UpdateProgress)>
        });
        Later::new(native::download(sys, on_progress))
    }

    /// Replaces the installed app with the downloaded update and restarts
    /// it as `Application.restart()` does: this run quits, and the new
    /// version starts once it has exited, so the promise settles only on
    /// failure. Rejects when nothing was downloaded, or when the app cannot
    /// be replaced, which leaves it as it was.
    pub(crate) fn install_and_restart(sys: System) -> Later<()> {
        Later::new(native::install(sys))
    }

    /// Replaces the installed app with the downloaded update when the app
    /// quits, without starting it again: the user's next start opens the
    /// new version. The update is unpacked and checked now, as
    /// `installAndRestart` checks it, and the promise resolves once it
    /// waits; quitting then only moves it into place. Rejects when nothing
    /// was downloaded or the update is refused, which leaves the app as it
    /// was. Calling it again replaces what waits, and `installAndRestart`
    /// installs at once instead. Only a quit of the app installs it: if the
    /// app ends otherwise (a crash, being killed), it is gone, and the next
    /// `checkForUpdate` offers the update again. When moving it into place
    /// fails as the app quits (the app was moved meanwhile, say), the app
    /// stays as it was and quits all the same, the failure logged.
    pub(crate) fn install_on_quit(sys: System) -> Later<()> {
        Later::new(native::install_on_quit(sys))
    }

    /// Checks for a newer version as `checkForUpdate` does and asks the
    /// user about it in NSH's built-in prompt, as macOS apps' Sparkle does:
    /// the version, its notes and Skip This Version, Remind Me Later and
    /// Install Update, then the download's progress and Install and
    /// Relaunch or Install on Quit. Where the app cannot install itself
    /// (`canInstall` false: iOS, Android) Install Update opens the update's
    /// `url`. The user's Skip This Version and "Automatically download and
    /// install updates" are kept in the app's data directory: a `quiet`
    /// check (at startup, in the background) shows nothing for a skipped
    /// version, and installs an update as the app quits without asking
    /// once the user chose to; it shows nothing either when the app is up
    /// to date or the check fails. Without `quiet` (a "Check for Updates…"
    /// menu item) the user hears that too. Resolves once the prompt shows,
    /// or there is nothing to show; never rejects.
    pub(crate) fn prompt_for_update(
        options: Option<UpdatePromptOptions>,
        sys: System,
    ) -> Later<()> {
        let options = options.unwrap_or_default();
        Later::new(async move {
            prompt::run(sys, options).await;
            Ok::<_, JsError>(())
        })
    }

    /// Asks the source in `nsh.json`'s `"updater"` for a newer build of the
    /// app's code: a signed package newer than the running build (the
    /// running package's, else the one the app shipped with) that this
    /// runtime runs. Resolves with it, or `null` when there is none, the
    /// app trusts no package roots (`"updater": { "packages": { "roots" } }`)
    /// or runs where apps do not update (the web, a development run).
    /// Rejects as `checkForUpdate` does.
    pub(crate) fn check_for_package(
        options: Option<PackageCheckOptions>,
        sys: System,
    ) -> Later<Option<PackageUpdate>> {
        let locale = options.and_then(|options| options.locale);
        Later::new(native::check_package(sys, locale))
    }

    /// Downloads the package the last `checkForPackage` found, checks it
    /// against the manifest and verifies it as the app's next start will:
    /// signed by a release key a trusted root certifies, for this app, newer
    /// than the running build, for this runtime. It then runs from the
    /// app's next start (`restartToPackage`). Calls `onProgress` as bytes
    /// arrive. An interrupted transfer keeps its partial file keyed to the
    /// package and resumes on the next call when the host supports HTTP
    /// `Range` requests, starting over when the server ignores the range or
    /// the file does not match. Rejects with
    /// `ERR_FEATURE_UNAVAILABLE_ON_PLATFORM` when the check found none, and
    /// with an error saying why when the download fails or the package is
    /// refused (a refused package leaves nothing behind).
    pub(crate) fn download_package(
        #[args(progress)] on_progress: Option<Callback<UpdateProgress>>,
        sys: System,
    ) -> Later<()> {
        Later::new(native::download_package(sys, on_progress))
    }

    /// Restarts the app into the downloaded package as
    /// `Application.restart()` does: this run quits and the app starts
    /// again, so the promise settles only on failure. Rejects when nothing
    /// was downloaded, and with `ERR_FEATURE_UNAVAILABLE_ON_PLATFORM` where
    /// an app cannot restart itself (iOS, Android): the package runs from
    /// the app's next start there.
    pub(crate) fn restart_to_package(sys: System) -> Later<()> {
        Later::new(native::restart_to_package(sys))
    }

    /// The signed package this run's code came from: its version and build;
    /// `null` for the code the app shipped with.
    pub(crate) fn running_package(sys: System) -> Option<RunningPackage> {
        let running = sys.updater.as_ref()?.running_package()?;
        Some(RunningPackage {
            version: running.version,
            build: running.build as f64,
        })
    }

    /// The installed app this code runs in, its shell: the version and build
    /// it was packaged with (on iOS its `CFBundleShortVersionString` and
    /// `CFBundleVersion`), the NSH runtime built into it (`nshVersion`) and
    /// whether it runs signed packages. A package replaces the app's code,
    /// not its shell: `runningPackage()` says which code runs, this what runs
    /// it. `version` and `build` are `null` for an app without `"updater"`
    /// in `nsh.json`, and where apps do not update (the web, a development
    /// run); `packages` is false there.
    pub(crate) fn shell_info(sys: System) -> ShellInfo {
        let host = sys.updater.as_ref();
        ShellInfo::new(
            host.and_then(|host| host.config()).as_deref(),
            host.is_some_and(|host| host.runs_packages()),
        )
    }
}

mod prompt;

pub use prompt::{UpdatePrompt, UpdatePromptOptions, prompt_for_update, set_update_presenter};

/// Why no package can be downloaded.
fn no_package() -> JsError {
    JsError::Error("no package to download: checkForPackage found none".into())
        .with_code(codes::UNAVAILABLE)
}

/// Why nothing can be downloaded or installed.
fn unavailable() -> JsError {
    JsError::Error(
        "no update this run can install: check `canInstall` of `checkForUpdate`'s update, or \
         send the user to its `url`"
            .into(),
    )
    .with_code(codes::UNAVAILABLE)
}

#[cfg(all(not(target_arch = "wasm32"), feature = "updater"))]
mod native {
    use std::collections::BTreeMap;

    use nsh_updater::{Available, Config};

    use super::*;

    /// What the VM's last check found and its download left, whether an
    /// operation is in progress, and the package the last package check
    /// found.
    #[derive(Default)]
    struct Found(
        RefCell<Option<Rc<Pending>>>,
        Cell<bool>,
        RefCell<Option<Arc<PendingPackage>>>,
    );

    /// A package the last `checkForPackage` found.
    struct PendingPackage {
        host: Arc<dyn UpdaterHost>,
        available: nsh_updater::AvailablePackage,
        /// The app's updates directory.
        dir: PathBuf,
        app_id: String,
        roots: Vec<String>,
        running_build: u64,
    }

    struct Operation<'a>(&'a Cell<bool>);

    impl Drop for Operation<'_> {
        fn drop(&mut self) {
            self.0.set(false);
        }
    }

    fn begin(found: &Found) -> Result<Operation<'_>, JsError> {
        if found.1.replace(true) {
            return Err(JsError::Error(
                "an update operation is already in progress".into(),
            ));
        }
        Ok(Operation(&found.1))
    }

    impl crate::element::Local for Found {}

    struct Pending {
        host: Arc<dyn UpdaterHost>,
        available: Available,
        dir: PathBuf,
        archive: RefCell<Option<PathBuf>>,
        #[cfg(any(
            all(target_os = "linux", not(target_env = "ohos")),
            target_os = "macos",
            windows
        ))]
        installation: nsh_updater::install::Installation,
    }

    /// Runs `work` off the engine thread.
    async fn blocking<T: Send + 'static>(
        work: impl FnOnce() -> Result<T, String> + Send + 'static,
    ) -> Result<T, JsError> {
        super::super::fs::blocking(move || work().map_err(JsError::Error)).await
    }

    pub(super) async fn check(
        sys: System,
        channel: Option<String>,
        locale: Option<String>,
    ) -> Result<Option<AppUpdate>, JsError> {
        let found = sys.locals.get::<Found>();
        let _operation = begin(&found)?;
        // A failed check keeps what the last one found, and its download: a
        // network blip must not lose a verified archive waiting for a restart.
        let Some(host) = sys.updater.clone() else {
            *found.0.borrow_mut() = None;
            return Ok(None);
        };
        let worker = host.clone();
        let checked = blocking(move || {
            let Some(bytes) = worker.config() else {
                return Ok(None);
            };
            let config = Config::parse(&bytes).map_err(|e| e.to_string())?;
            // Only the source changes: the id, version and key are the
            // package's, and the installation is found by its own file.
            let config = match &channel {
                Some(name) => config.channel(name).map_err(|e| e.to_string())?,
                None => config,
            };
            #[cfg(any(
                all(target_os = "linux", not(target_env = "ohos")),
                target_os = "macos",
                windows
            ))]
            if let Some(installation) =
                nsh_updater::install::Installation::current(&config.id, &bytes)
            {
                // Windows leftovers also need removal when no newer update exists.
                installation.remove_leftovers();
            }
            let manifest = get_all(&*worker, &config.manifest_url())?;
            let signature = match config.signature_url() {
                Some(url) => Some(get_all(&*worker, &url)?),
                None => None,
            };
            let available = config
                .check(&manifest, signature.as_deref(), &nsh_updater::platform())
                .map_err(|e| e.to_string())?;
            let running = worker.running_package();
            Ok(available.map(|mut available| {
                let shell = package_covers(running.as_ref(), &available.version);
                if let Some(notes) = localized(&*worker, &available.localized_notes, locale) {
                    available.notes = Some(notes);
                }
                (bytes, config, available, shell)
            }))
        })
        .await?;
        let Some((bytes, config, available, shell)) = checked else {
            *found.0.borrow_mut() = None;
            return Ok(None);
        };
        // Only the desktops install an update (`can_install` below).
        #[cfg_attr(
            not(any(
                all(target_os = "linux", not(target_env = "ohos")),
                target_os = "macos",
                windows
            )),
            allow(unused_mut)
        )]
        let mut update = AppUpdate {
            version: available.version.clone(),
            notes: available.notes.clone(),
            url: available.url.clone(),
            can_install: false,
            shell,
        };
        #[cfg(any(
            all(target_os = "linux", not(target_env = "ohos")),
            target_os = "macos",
            windows
        ))]
        {
            let id = config.id.clone();
            let installation = blocking(move || {
                Ok(nsh_updater::install::Installation::current(&id, &bytes)
                    .filter(|installation| installation.can_install()))
            })
            .await?;
            let mut found = found.0.borrow_mut();
            if let (Some(installation), Some(dir), true) = (
                installation,
                sys.data_dir.clone(),
                available.sha256.is_some(),
            ) {
                update.can_install = true;
                // The same version again keeps its download; a newer one
                // replaces it, so a restart always installs the latest.
                let same = found.as_ref().is_some_and(|pending| {
                    pending.available.version == available.version
                        && pending.available.sha256 == available.sha256
                });
                if !same {
                    *found = Some(Rc::new(Pending {
                        host,
                        available,
                        dir: dir.join("updates"),
                        archive: RefCell::new(None),
                        installation,
                    }));
                }
            } else {
                *found = None;
            }
        }
        #[cfg(not(any(
            all(target_os = "linux", not(target_env = "ohos")),
            target_os = "macos",
            windows
        )))]
        let _ = (bytes, config, host, &found);
        Ok(Some(update))
    }

    /// The app's display name and version as it was packaged
    /// (`nsh-updater.json`), when it has them.
    pub(super) async fn shipped(sys: &System) -> (Option<String>, Option<String>) {
        let Some(host) = sys.updater.clone() else {
            return (None, None);
        };
        let shipped = blocking(move || {
            Ok(host
                .config()
                .and_then(|bytes| Config::parse(&bytes).ok())
                .map(|config| (config.name, config.version)))
        })
        .await;
        match shipped {
            Ok(Some((name, version))) => (name, Some(version)),
            _ => (None, None),
        }
    }

    /// The notes in `locale` among `files`, downloaded and checked; `None`
    /// keeps the default notes, also when the file cannot be had: notes in
    /// another language must not fail a check.
    fn localized(
        host: &dyn UpdaterHost,
        files: &BTreeMap<String, nsh_updater::NotesFile>,
        locale: Option<String>,
    ) -> Option<String> {
        let file = nsh_updater::notes_for(files, &locale?)?;
        let opened = get_all(host, &file.url)
            .and_then(|bytes| nsh_updater::open_notes(file, bytes).map_err(|e| e.to_string()));
        match opened {
            Ok(notes) => Some(notes),
            Err(e) => {
                log::warn!("updater: notes: {e}");
                None
            }
        }
    }

    /// A small file, whole. A manifest is a few kilobytes.
    fn get_all(host: &dyn UpdaterHost, url: &str) -> Result<Vec<u8>, String> {
        const MAX: usize = 1 << 20;
        let mut bytes = Vec::new();
        host.get(url, 0, &mut |_, chunk, _| {
            if bytes.len() + chunk.len() > MAX {
                return Err("over 1 MiB".to_owned());
            }
            bytes.extend_from_slice(chunk);
            Ok(())
        })
        .map_err(|e| format!("{url}: {e}"))?;
        Ok(bytes)
    }

    fn pending(sys: &System) -> Result<Rc<Pending>, JsError> {
        sys.locals
            .get::<Found>()
            .0
            .borrow()
            .clone()
            .ok_or_else(unavailable)
    }

    /// Safe filename fragment keying a partial or verified download to the
    /// asset's manifest SHA-256: lowercase hex as-is for normal hashes, or
    /// hashed otherwise so an unusual manifest field can never escape `dir`.
    fn part_key(sha256: &str) -> String {
        if !sha256.is_empty() && sha256.len() <= 64 && sha256.bytes().all(|b| b.is_ascii_hexdigit())
        {
            sha256.to_ascii_lowercase()
        } else {
            nsh_updater::sha256(sha256.as_bytes())
        }
    }

    /// Removes stale `download-*` files in `dir` that do not belong to the
    /// asset currently being downloaded (leaving OTA package files untouched).
    fn cleanup_stale_downloads(dir: &std::path::Path, keep_part: &str, keep_dest: &str) {
        let Ok(entries) = std::fs::read_dir(dir) else {
            return;
        };
        for entry in entries.flatten() {
            let name = entry.file_name();
            let Some(name) = name.to_str() else { continue };
            if name.starts_with("download-")
                && name != keep_part
                && name != keep_dest
                && name.strip_suffix(".lock") != Some(keep_part)
            {
                let _ = std::fs::remove_file(entry.path());
            }
        }
    }

    /// Removes stale OTA `.part` files in `dir` that do not match `keep_part`
    /// (including legacy `next.nshpkg.part`), leaving `next.nshpkg`,
    /// `trying.nshpkg`, `current.nshpkg`, `trust/` and `packages/` untouched.
    fn cleanup_stale_package_parts(dir: &std::path::Path, keep_part: &str) {
        let Ok(entries) = std::fs::read_dir(dir) else {
            return;
        };
        for entry in entries.flatten() {
            let name = entry.file_name();
            let Some(name) = name.to_str() else { continue };
            let legacy = name == format!("{NEXT}.part");
            let other = name.starts_with("next-")
                && (name.ends_with(".nshpkg.part") || name.ends_with(".nshpkg.part.lock"));
            if (legacy || other)
                && name != keep_part
                && name.strip_suffix(".lock") != Some(keep_part)
            {
                let _ = std::fs::remove_file(entry.path());
            }
        }
    }

    /// An [`nsh_updater::Download`] that counts what it took, so a kept
    /// `.part` file can be hashed again and resumed from its end.
    #[derive(Debug)]
    struct Download {
        check: nsh_updater::Download,
        received: u64,
    }

    impl Download {
        fn new(sha256: &str, size: Option<u64>) -> Download {
            Download {
                check: nsh_updater::Download::new(sha256, size),
                received: 0,
            }
        }

        /// Seeded with the bytes already in `partial`; fails if they exceed
        /// the manifest's size or cannot be read.
        fn resume(
            sha256: &str,
            size: Option<u64>,
            partial: &mut impl std::io::Read,
        ) -> Result<Download, String> {
            let mut download = Download::new(sha256, size);
            let mut buf = [0u8; 16 * 1024];
            loop {
                match partial.read(&mut buf) {
                    Ok(0) => return Ok(download),
                    Ok(n) => {
                        download.update(&buf[..n]).map_err(|e| e.to_string())?;
                    }
                    Err(e) if e.kind() == std::io::ErrorKind::Interrupted => continue,
                    Err(e) => return Err(e.to_string()),
                }
            }
        }

        fn received(&self) -> u64 {
            self.received
        }

        fn update(&mut self, chunk: &[u8]) -> Result<u64, nsh_updater::Error> {
            self.received = self.check.update(chunk)?;
            Ok(self.received)
        }

        fn finish(self) -> Result<(), nsh_updater::Error> {
            self.check.finish()
        }
    }

    /// Opens an existing `.part` file and seeds a [`Download`] from it, or
    /// creates a fresh empty `.part` file when none exists or the existing one
    /// is unusable (for instance, larger than `size`). Returns the open file
    /// positioned at its end, the seeded [`Download`], and whether non-empty
    /// bytes were loaded from disk.
    fn open_or_create_part(
        part: &std::path::Path,
        sha256: &str,
        size: Option<u64>,
    ) -> Result<(std::fs::File, Download, bool), String> {
        use std::io::Seek as _;

        if let Ok(mut file) = std::fs::OpenOptions::new()
            .read(true)
            .write(true)
            .open(part)
        {
            match Download::resume(sha256, size, &mut file) {
                Ok(check) => {
                    let received = check.received();
                    file.seek(std::io::SeekFrom::Start(received))
                        .map_err(|e| e.to_string())?;
                    file.set_len(received).map_err(|e| e.to_string())?;
                    return Ok((file, check, received > 0));
                }
                Err(_) => {
                    drop(file);
                    let _ = std::fs::remove_file(part);
                }
            }
        }
        let (file, check) = reset_part(part, sha256, size)?;
        Ok((file, check, false))
    }

    /// Removes any existing `part` and creates a fresh empty file beside a
    /// fresh [`Download`].
    fn reset_part(
        part: &std::path::Path,
        sha256: &str,
        size: Option<u64>,
    ) -> Result<(std::fs::File, Download), String> {
        let _ = std::fs::remove_file(part);
        let file = std::fs::OpenOptions::new()
            .read(true)
            .write(true)
            .create(true)
            .truncate(true)
            .open(part)
            .map_err(|e| e.to_string())?;
        Ok((file, Download::new(sha256, size)))
    }

    struct DownloadTarget<'a> {
        host: &'a dyn UpdaterHost,
        url: &'a str,
        part: &'a std::path::Path,
        dest: &'a std::path::Path,
        remove_invalid_dest: bool,
        sha256: &'a str,
        size: Option<u64>,
    }

    enum TransferStep {
        /// Body reached EOF (`Full` or `Partial`); caller syncs, finishes and verifies.
        Finished {
            file: std::fs::File,
            check: Download,
            resumed: bool,
        },
        /// The existing partial file verified against the manifest and was
        /// moved into `dest` (`416` when the manifest omitted `size`).
        Completed,
        /// The existing partial file was discarded; caller restarts once from `0`.
        RestartFresh,
    }

    #[derive(Clone, Copy, PartialEq, Eq)]
    enum ResponseKind {
        None,
        Full,
        Partial,
        Unsatisfiable,
    }

    /// Streams one transfer pass (possibly multiple ranged slices if the host
    /// caps individual `206` responses) into `file`.
    fn transfer_into_part(
        target: &DownloadTarget<'_>,
        mut file: std::fs::File,
        mut check: Download,
        mut resumed: bool,
        progress: &mut impl FnMut(u64, Option<u64>) -> bool,
        verify: &impl Fn(&std::path::Path) -> Result<(), String>,
    ) -> Result<TransferStep, String> {
        use std::io::{Seek as _, Write as _};

        // Upper bound on server-capped `206` slices in one call.
        const MAX_RANGE_SLICES: usize = 1024;
        let mut slices = 0usize;

        loop {
            let offset = check.received();
            let mut kind = ResponseKind::None;
            let mut server_total: Option<u64> = None;
            let mut exceeded_size = false;

            let transfer = target
                .host
                .get(target.url, offset, &mut |at, chunk, total| {
                    if chunk.is_empty() {
                        return Ok(());
                    }
                    if kind == ResponseKind::None {
                        if at == 0 {
                            kind = ResponseKind::Full;
                            if check.received() > 0 {
                                file.set_len(0).map_err(|e| e.to_string())?;
                                file.seek(std::io::SeekFrom::Start(0))
                                    .map_err(|e| e.to_string())?;
                                check = Download::new(target.sha256, target.size);
                                resumed = false;
                            }
                        } else if at == offset {
                            kind = ResponseKind::Partial;
                            // Show the kept bytes before the new ones arrive.
                            if !progress(offset, target.size.or(total)) {
                                return Err("download cancelled".to_owned());
                            }
                        } else {
                            return Err(format!("body starts at byte {at}, not {offset}"));
                        }
                        server_total = total;
                    } else if at != check.received() {
                        return Err(format!(
                            "body piece at byte {at}, expected {}",
                            check.received()
                        ));
                    }
                    let received = match check.update(chunk) {
                        Ok(received) => received,
                        Err(e) => {
                            exceeded_size = true;
                            return Err(e.to_string());
                        }
                    };
                    file.write_all(chunk).map_err(|e| e.to_string())?;
                    if !progress(received, target.size.or(server_total)) {
                        return Err("download cancelled".to_owned());
                    }
                    Ok(())
                });
            // No body for a resumed request: the server could not satisfy
            // the range (`416`).
            if transfer.is_ok() && kind == ResponseKind::None && offset > 0 {
                kind = ResponseKind::Unsatisfiable;
            }

            if let Err(err) = transfer {
                let _ = file.flush();
                let received = check.received();
                drop(file);
                if exceeded_size {
                    let _ = std::fs::remove_file(target.part);
                    if resumed {
                        return Ok(TransferStep::RestartFresh);
                    }
                    return Err(err);
                }
                // Keep partial progress on transport / HTTP errors or cancellation
                // so the next call resumes; drop empty 0-byte placeholders.
                if received == 0 {
                    let _ = std::fs::remove_file(target.part);
                }
                return Err(err);
            }

            match kind {
                ResponseKind::Unsatisfiable => {
                    let received = check.received();
                    let _ = file.sync_all();
                    drop(file);
                    if offset > 0 && target.size.is_none_or(|s| s == received) {
                        // A `416` when the manifest omitted `size` can mean
                        // the partial file already holds the entire asset.
                        if check.finish().is_ok() {
                            if let Err(err) = verify(target.part) {
                                let _ = std::fs::remove_file(target.part);
                                return Err(err);
                            }
                            let _ = progress(received, target.size.or(Some(received)));
                            std::fs::rename(target.part, target.dest).map_err(|e| e.to_string())?;
                            return Ok(TransferStep::Completed);
                        }
                    }
                    let _ = std::fs::remove_file(target.part);
                    if resumed {
                        return Ok(TransferStep::RestartFresh);
                    }
                    return Err("HTTP 416".to_owned());
                }
                ResponseKind::Partial => {
                    let expected = target.size.or(server_total);
                    if check.received() > offset
                        && expected.is_some_and(|total| check.received() < total)
                        && slices < MAX_RANGE_SLICES
                    {
                        slices += 1;
                        continue;
                    }
                    return Ok(TransferStep::Finished {
                        file,
                        check,
                        resumed,
                    });
                }
                ResponseKind::Full => {
                    return Ok(TransferStep::Finished {
                        file,
                        check,
                        resumed,
                    });
                }
                ResponseKind::None => {
                    let received = check.received();
                    drop(file);
                    if received == 0 {
                        let _ = std::fs::remove_file(target.part);
                    }
                    return Err("empty response".to_owned());
                }
            }
        }
    }

    /// Locks `<part>.lock`, held until the returned file drops; fails at
    /// once when another download of the same asset holds it.
    fn lock_part(part: &std::path::Path) -> Result<std::fs::File, String> {
        let mut path = part.as_os_str().to_owned();
        path.push(".lock");
        let file = std::fs::OpenOptions::new()
            .write(true)
            .create(true)
            .truncate(false)
            .open(&path)
            .map_err(|e| e.to_string())?;
        match file.try_lock() {
            Ok(()) => Ok(file),
            Err(std::fs::TryLockError::WouldBlock) => {
                Err("another download of this update is in progress".to_owned())
            }
            Err(std::fs::TryLockError::Error(e)) => Err(e.to_string()),
        }
    }

    /// Downloads `target.url` into `target.part` (resuming an existing partial
    /// file when present), verifies its size, SHA-256 and `verify`, and moves
    /// it atomically to `target.dest`.
    fn download_resumable(
        target: &DownloadTarget<'_>,
        mut progress: impl FnMut(u64, Option<u64>) -> bool,
        verify: impl Fn(&std::path::Path) -> Result<(), String>,
    ) -> Result<(), String> {
        // One download of an asset at a time, also across processes: the
        // `.part` is shared, so a second one would interleave its bytes.
        let _lock = lock_part(target.part)?;
        // Fast path: the final file is already on disk and matches this asset.
        if let Ok(mut existing) = std::fs::File::open(target.dest) {
            let matched = Download::resume(target.sha256, target.size, &mut existing)
                .ok()
                .and_then(|check| {
                    let received = check.received();
                    check.finish().ok().map(|()| received)
                });
            drop(existing);
            if let Some(received) = matched {
                let _ = std::fs::remove_file(target.part);
                if let Err(err) = verify(target.dest) {
                    let _ = std::fs::remove_file(target.dest);
                    return Err(err);
                }
                let _ = progress(received, target.size.or(Some(received)));
                return Ok(());
            } else if target.remove_invalid_dest {
                let _ = std::fs::remove_file(target.dest);
            }
        }

        let (mut file, mut check, mut resumed) =
            open_or_create_part(target.part, target.sha256, target.size)?;

        // Fast path: a previous run wrote all expected bytes before crashing
        // or failing before the final rename.
        if resumed && target.size == Some(check.received()) {
            let received = check.received();
            drop(file);
            if check.finish().is_ok() {
                if let Err(err) = verify(target.part) {
                    let _ = std::fs::remove_file(target.part);
                    return Err(err);
                }
                let _ = progress(received, Some(received));
                std::fs::rename(target.part, target.dest).map_err(|e| e.to_string())?;
                return Ok(());
            }
            (file, check) = reset_part(target.part, target.sha256, target.size)?;
            resumed = false;
        }

        loop {
            match transfer_into_part(target, file, check, resumed, &mut progress, &verify)? {
                TransferStep::Completed => return Ok(()),
                TransferStep::RestartFresh => {
                    (file, check) = reset_part(target.part, target.sha256, target.size)?;
                    resumed = false;
                }
                TransferStep::Finished {
                    file: done_file,
                    check: done_check,
                    resumed: used_partial,
                } => {
                    let sync = done_file.sync_all().map_err(|e| e.to_string());
                    drop(done_file);
                    sync?;
                    if let Err(err) = done_check.finish() {
                        let _ = std::fs::remove_file(target.part);
                        if used_partial {
                            // The kept partial bytes or the remote file may have
                            // changed; fall back once to a clean full download.
                            (file, check) = reset_part(target.part, target.sha256, target.size)?;
                            resumed = false;
                            continue;
                        }
                        return Err(err.to_string());
                    }
                    if let Err(err) = verify(target.part) {
                        let _ = std::fs::remove_file(target.part);
                        return Err(err);
                    }
                    std::fs::rename(target.part, target.dest).map_err(|e| e.to_string())?;
                    return Ok(());
                }
            }
        }
    }

    pub(super) async fn download(
        sys: System,
        on_progress: Option<Box<dyn Fn(UpdateProgress)>>,
    ) -> Result<(), JsError> {
        let found = sys.locals.get::<Found>();
        let _operation = begin(&found)?;
        let pending = pending(&sys)?;
        let host = pending.host.clone();
        let dir = pending.dir.clone();
        let url = pending.available.url.clone();
        let sha256 = pending.available.sha256.clone().unwrap_or_default();
        let size = pending.available.size;
        #[cfg(any(
            all(target_os = "linux", not(target_env = "ohos")),
            target_os = "macos",
            windows
        ))]
        let delta = pending
            .available
            .delta
            .clone()
            .map(|delta| (delta, pending.installation.clone()));
        let (tx, mut rx) = tokio::sync::mpsc::unbounded_channel();
        let mut work = std::pin::pin!(blocking(move || {
            std::fs::create_dir_all(&dir).map_err(|e| e.to_string())?;
            let key = part_key(&sha256);
            let part_name = format!("download-{key}.part");
            let dest_name = format!("download-{key}");
            cleanup_stale_downloads(&dir, &part_name, &dest_name);
            // The patch from the running version first, the archive when
            // it cannot make the new app.
            #[cfg(any(
                all(target_os = "linux", not(target_env = "ohos")),
                target_os = "macos",
                windows
            ))]
            if let Some((delta, installation)) = &delta {
                let progress = |received, total| tx.send((received, total)).is_ok();
                match patched(&*host, installation, &dir, delta, progress) {
                    Ok(path) => return Ok(path),
                    Err(e) => {
                        log::warn!("updater: downloading the whole app, the patch failed: {e}")
                    }
                }
            }
            let part = dir.join(&part_name);
            let dest = dir.join(&dest_name);
            let target = DownloadTarget {
                host: &*host,
                url: &url,
                part: &part,
                dest: &dest,
                remove_invalid_dest: true,
                sha256: &sha256,
                size,
            };
            download_resumable(
                &target,
                |received, total| tx.send((received, total)).is_ok(),
                |_| Ok(()),
            )?;
            Ok(dest)
        }));
        let post = |received: u64, total: Option<u64>| {
            if let Some(on_progress) = &on_progress {
                on_progress(UpdateProgress {
                    received: received as f64,
                    total: total.map(|t| t as f64),
                });
            }
        };
        let result = loop {
            tokio::select! {
                result = &mut work => break result,
                Some((received, total)) = rx.recv() => post(received, total),
            }
        };
        while let Ok((received, total)) = rx.try_recv() {
            post(received, total);
        }
        let path = result?;
        if let Some(previous) = pending.archive.borrow_mut().replace(path.clone())
            && previous != path
        {
            let _ = std::fs::remove_file(previous);
        }
        Ok(())
    }

    /// Makes the new app from the patch of the running version
    /// ([`nsh_updater::delta`]): the installed app as the patch's reference,
    /// checked against the manifest, the patch downloaded (resumable) and
    /// applied; the `.tar` it made, checked too.
    #[cfg(any(
        all(target_os = "linux", not(target_env = "ohos")),
        target_os = "macos",
        windows
    ))]
    fn patched(
        host: &dyn UpdaterHost,
        installation: &nsh_updater::install::Installation,
        dir: &std::path::Path,
        delta: &nsh_updater::Delta,
        progress: impl FnMut(u64, Option<u64>) -> bool,
    ) -> Result<PathBuf, String> {
        use std::{fs, io::BufWriter};
        let key = part_key(&delta.result);
        // What patches to other versions left.
        if let Ok(entries) = fs::read_dir(dir) {
            for entry in entries.flatten() {
                let name = entry.file_name();
                if let Some(name) = name.to_str()
                    && name.starts_with("delta-")
                    && !name.starts_with(&format!("delta-{key}"))
                {
                    let _ = fs::remove_file(entry.path());
                }
            }
        }
        let base_path = dir.join(format!("delta-{key}.base"));
        let result = (|| {
            let file = fs::File::create(&base_path).map_err(|e| e.to_string())?;
            let base = installation
                .delta_base(BufWriter::new(file))
                .map_err(|e| e.to_string())?;
            if base != delta.base.to_ascii_lowercase() {
                return Err(format!(
                    "the installed app is not version {} as released",
                    delta.from
                ));
            }
            let part = dir.join(format!("delta-{key}.patch.part"));
            let patch = dir.join(format!("delta-{key}.patch"));
            let target = DownloadTarget {
                host,
                url: &delta.url,
                part: &part,
                dest: &patch,
                remove_invalid_dest: true,
                sha256: &delta.sha256,
                size: Some(delta.size),
            };
            download_resumable(&target, progress, |_| Ok(()))?;
            let base = fs::read(&base_path).map_err(|e| e.to_string())?;
            let out_path = dir.join(format!("delta-{key}.tar"));
            let applied = (|| {
                let out = BufWriter::new(fs::File::create(&out_path)?);
                nsh_updater::delta::apply(&base, fs::File::open(&patch)?, out, &delta.result)
            })();
            let _ = fs::remove_file(&patch);
            if let Err(e) = applied {
                let _ = fs::remove_file(&out_path);
                return Err(e.to_string());
            }
            Ok(out_path)
        })();
        let _ = fs::remove_file(&base_path);
        result
    }

    /// The app's updates directory, as `nsh_engine::updates` has it.
    const UPDATES: &str = "updates";
    /// The downloaded package the app's next start runs.
    const NEXT: &str = "next.nshpkg";

    pub(super) async fn check_package(
        sys: System,
        locale: Option<String>,
    ) -> Result<Option<PackageUpdate>, JsError> {
        let found = sys.locals.get::<Found>();
        let _operation = begin(&found)?;
        let (Some(host), Some(data_dir)) = (sys.updater.clone(), sys.data_dir.clone()) else {
            *found.2.borrow_mut() = None;
            return Ok(None);
        };
        let worker = host.clone();
        let checked = blocking(move || {
            let Some(bytes) = worker.config() else {
                return Ok(None);
            };
            let config = Config::parse(&bytes).map_err(|e| e.to_string())?;
            if config.package_roots().is_empty() {
                return Ok(None);
            }
            let running_build = worker
                .running_package()
                .map(|running| running.build)
                .unwrap_or(config.build.unwrap_or(1));
            let manifest = get_all(&*worker, &config.manifest_url())?;
            let signature = match config.signature_url() {
                Some(url) => Some(get_all(&*worker, &url)?),
                None => None,
            };
            let available = config
                .check_package(
                    &manifest,
                    signature.as_deref(),
                    running_build,
                    env!("CARGO_PKG_VERSION"),
                )
                .map_err(|e| e.to_string())?;
            Ok(available.map(|mut available| {
                if let Some(notes) = localized(&*worker, &available.localized_notes, locale) {
                    available.notes = Some(notes);
                }
                (config, available, running_build)
            }))
        })
        .await?;
        let Some((config, available, running_build)) = checked else {
            *found.2.borrow_mut() = None;
            return Ok(None);
        };
        let update = PackageUpdate {
            version: available.version.clone(),
            build: available.build as f64,
            notes: available.notes.clone(),
            size: available.size.map(|size| size as f64),
        };
        *found.2.borrow_mut() = Some(Arc::new(PendingPackage {
            host,
            available,
            dir: data_dir.join(UPDATES),
            roots: config.package_roots().to_vec(),
            app_id: config.id,
            running_build,
        }));
        Ok(Some(update))
    }

    pub(super) async fn download_package(
        sys: System,
        on_progress: Option<Callback<UpdateProgress>>,
    ) -> Result<(), JsError> {
        let found = sys.locals.get::<Found>();
        let _operation = begin(&found)?;
        let pending = found.2.borrow().clone().ok_or_else(no_package)?;
        let (tx, mut rx) = tokio::sync::mpsc::unbounded_channel();
        let mut work = std::pin::pin!(blocking(move || {
            let pending = &*pending;
            let dir = &pending.dir;
            std::fs::create_dir_all(dir).map_err(|e| e.to_string())?;
            let available = &pending.available;
            let part_name = format!("next-{}.nshpkg.part", part_key(&available.sha256));
            cleanup_stale_package_parts(dir, &part_name);
            let part = dir.join(&part_name);
            let dest = dir.join(NEXT);
            let target = DownloadTarget {
                host: &*pending.host,
                url: &available.url,
                part: &part,
                dest: &dest,
                remove_invalid_dest: false,
                sha256: &available.sha256,
                size: available.size,
            };
            download_resumable(
                &target,
                |received, total| tx.send((received, total)).is_ok(),
                |file| {
                    pending
                        .host
                        .verify_package(
                            file,
                            dir,
                            &pending.app_id,
                            &pending.roots,
                            pending.running_build,
                        )
                        .map(|_| ())
                },
            )
            .map_err(|e| format!("package refused: {e}"))
        }));
        let post = |received: u64, total: Option<u64>| {
            if let Some(callback) = &on_progress {
                callback.post(UpdateProgress {
                    received: received as f64,
                    total: total.map(|t| t as f64),
                });
            }
        };
        let result = loop {
            tokio::select! {
                result = &mut work => break result,
                Some((received, total)) = rx.recv() => post(received, total),
            }
        };
        while let Ok((received, total)) = rx.try_recv() {
            post(received, total);
        }
        result
    }

    pub(super) async fn restart_to_package(sys: System) -> Result<(), JsError> {
        let found = sys.locals.get::<Found>();
        let _operation = begin(&found)?;
        let downloaded = sys
            .data_dir
            .as_ref()
            .is_some_and(|dir| dir.join(UPDATES).join(NEXT).exists());
        if !downloaded {
            return Err(JsError::Error(
                "nothing is downloaded: call downloadPackage first".into(),
            ));
        }
        restart_into_package().await
    }

    /// As `Application.restart()`: the app's launcher runs the package
    /// when it starts again.
    #[cfg(any(
        all(target_os = "linux", not(target_env = "ohos")),
        target_os = "macos",
        windows
    ))]
    async fn restart_into_package() -> Result<(), JsError> {
        crate::element::desktop::restart();
        std::future::pending().await
    }

    #[cfg(not(any(
        all(target_os = "linux", not(target_env = "ohos")),
        target_os = "macos",
        windows
    )))]
    async fn restart_into_package() -> Result<(), JsError> {
        Err(JsError::Error(
            "an app cannot restart itself here: the package runs from the app's next start".into(),
        )
        .with_code(codes::UNAVAILABLE))
    }

    #[cfg(any(
        all(target_os = "linux", not(target_env = "ohos")),
        target_os = "macos",
        windows
    ))]
    pub(super) async fn install(sys: System) -> Result<(), JsError> {
        let found = sys.locals.get::<Found>();
        let _operation = begin(&found)?;
        let pending = pending(&sys)?;
        let archive = pending.archive.borrow().clone().ok_or_else(|| {
            JsError::Error("nothing is downloaded: call downloadUpdate first".into())
        })?;
        let installation = pending.installation.clone();
        // Installed now instead of as the app quits (`installOnQuit`).
        crate::restart::set_at_exit(None);
        // The restart starts the app at the path the user knows: once the
        // directory or `.app` is swapped, this process' executable is the
        // old one, moved aside and deleted.
        crate::restart::remember_program();
        let program = blocking(move || {
            let program = installation.install(&archive).map_err(|e| e.to_string())?;
            let _ = std::fs::remove_file(&archive);
            Ok(program)
        })
        .await?;
        // The update may have renamed the executable or its `.app`.
        crate::restart::set_program(program);
        // As `Application.restart()`: the app quits as the user's quit does
        // (storage written, windows closed), and the new version starts
        // once this process has exited (`crate::restart`).
        crate::element::desktop::restart();
        std::future::pending().await
    }

    #[cfg(not(any(
        all(target_os = "linux", not(target_env = "ohos")),
        target_os = "macos",
        windows
    )))]
    pub(super) async fn install(_: System) -> Result<(), JsError> {
        Err(unavailable())
    }

    #[cfg(any(
        all(target_os = "linux", not(target_env = "ohos")),
        target_os = "macos",
        windows
    ))]
    pub(super) async fn install_on_quit(sys: System) -> Result<(), JsError> {
        let found = sys.locals.get::<Found>();
        let _operation = begin(&found)?;
        let pending = pending(&sys)?;
        let archive = pending.archive.borrow().clone().ok_or_else(|| {
            JsError::Error("nothing is downloaded: call downloadUpdate first".into())
        })?;
        let installation = pending.installation.clone();
        // What waited before goes first: it is staged in the same place.
        crate::restart::set_at_exit(None);
        let staged = blocking({
            let archive = archive.clone();
            move || installation.stage(&archive).map_err(|e| e.to_string())
        })
        .await?;
        let version = pending.available.version.clone();
        crate::restart::set_at_exit(Some(Box::new(move || {
            // A restart after the swap starts the app at the path the
            // user knows, as `install` has it.
            crate::restart::remember_program();
            match staged.finish() {
                Ok(program) => {
                    // The update may have renamed the executable or its `.app`.
                    crate::restart::set_program(program);
                    log::info!("installed version {version} as the app quit")
                }
                Err(e) => log::error!("cannot install version {version} as the app quits: {e}"),
            }
            let _ = std::fs::remove_file(&archive);
        })));
        Ok(())
    }

    #[cfg(not(any(
        all(target_os = "linux", not(target_env = "ohos")),
        target_os = "macos",
        windows
    )))]
    pub(super) async fn install_on_quit(_: System) -> Result<(), JsError> {
        Err(unavailable())
    }
    /// Whether the package this run's code came from is already the
    /// release `offered` or a later one. The app's own version is the one it
    /// shipped as, so a run on a package of the latest release still sees the
    /// release as newer: its code is current and only the shell is behind
    /// (`AppUpdate::shell`).
    fn package_covers(running: Option<&PackageVersion>, offered: &str) -> bool {
        running.is_some_and(|running| nsh_updater::version_at_least(&running.version, offered))
    }

    #[cfg(test)]
    mod tests {
        use super::*;

        #[test]
        fn a_running_package_of_the_release_or_later_covers_it() {
            let running = |version: &str| PackageVersion {
                version: version.into(),
                build: 35,
            };
            assert!(!package_covers(None, "0.2.1"));
            assert!(package_covers(Some(&running("0.2.1")), "0.2.1"));
            assert!(package_covers(Some(&running("0.2.10")), "0.2.9"));
            // A release the package is older than comes as the whole app (a newer runtime).
            assert!(!package_covers(Some(&running("0.2.1")), "0.3.0"));
        }

        #[test]
        fn shell_info_reads_the_shipped_configuration() {
            let config = br#"{"id":"dev.noop.app","version":"0.2.1","github":"o/r","build":35,
                "packages":{"roots":["SJl1kuuXiy0WqZM03S0Q16OIGNX7e+RTI8H5Flk8JLo="]}}"#;
            let info = ShellInfo::new(Some(config), true);
            assert_eq!(info.version.as_deref(), Some("0.2.1"));
            assert_eq!(info.build, Some(35.0));
            assert_eq!(info.nsh_version, env!("CARGO_PKG_VERSION"));
            assert!(info.packages);
            // A launcher that runs no packages, or a configuration that trusts no roots.
            assert!(!ShellInfo::new(Some(config), false).packages);
            let plain = br#"{"id":"dev.noop.app","version":"0.2.1","github":"o/r"}"#;
            let info = ShellInfo::new(Some(plain), true);
            assert_eq!(info.build, Some(1.0));
            assert!(!info.packages);
            // No configuration: a development run, an app without `"updater"`.
            let info = ShellInfo::new(None, true);
            assert_eq!(
                (info.version, info.build, info.packages),
                (None, None, false)
            );
        }

        /// A package of version 1.0.0 whose `"updater"` has a default
        /// source and a `beta` channel, each serving a signed manifest; it
        /// records the URLs asked for.
        struct Channels(std::sync::Mutex<Vec<String>>);

        const SECRET: &str = "AQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQE=";
        const PUBLIC: &str = "iojj3XQJ8ZX9UtstPLpdcspnCb8dlBIb83SIAbQPb1w=";
        const STABLE: &str = "https://x.dev/latest.json";
        const BETA: &str = "https://x.dev/beta/latest.json";

        fn served(version: &str) -> Vec<u8> {
            format!(
                r#"{{"id":"dev.noop.app","version":"{version}","platforms":{{"{}":{{"url":"a.tar.gz","sha256":"ab","size":2}}}}}}"#,
                nsh_updater::platform()
            )
            .into_bytes()
        }

        const ZH_TW: &str = "https://x.dev/beta/notes.zh-TW.md";
        const JA: &str = "https://x.dev/beta/notes.ja.md";

        /// The beta manifest: notes, and in zh-TW and ja beside it (ja's
        /// file does not match its hash).
        fn beta() -> Vec<u8> {
            let mut manifest: serde_json::Value =
                serde_json::from_slice(&served("1.1.0-beta.1")).unwrap();
            manifest["notes"] = "Fixes".into();
            manifest["localizedNotes"] = serde_json::json!({
                "zh-TW": { "url": "notes.zh-TW.md", "sha256": nsh_updater::sha256("修復".as_bytes()) },
                "ja": { "url": "notes.ja.md", "sha256": nsh_updater::sha256("修正".as_bytes()) },
            });
            serde_json::to_vec(&manifest).unwrap()
        }

        impl UpdaterHost for Channels {
            fn config(&self) -> Option<Vec<u8>> {
                Some(
                    format!(
                        r#"{{"id":"dev.noop.app","version":"1.0.0","url":"{STABLE}","channels":{{"beta":"{BETA}"}},"publicKey":"{PUBLIC}"}}"#
                    )
                    .into_bytes(),
                )
            }

            fn get(&self, url: &str, _: u64, sink: UpdaterSink<'_>) -> Result<(), String> {
                self.0.lock().unwrap().push(url.to_owned());
                let manifest = match url.strip_suffix(".sig").unwrap_or(url) {
                    STABLE => served("1.0.0"),
                    BETA => beta(),
                    ZH_TW => return sink(0, "修復".as_bytes(), None),
                    JA => return sink(0, b"tampered", None),
                    _ => return Err("not found".into()),
                };
                let body = if url.ends_with(".sig") {
                    nsh_updater::sign(SECRET, &manifest).unwrap().into_bytes()
                } else {
                    manifest
                };
                sink(0, &body, Some(body.len() as u64))
            }
        }

        #[test]
        fn a_check_reads_the_channel_the_package_declares() {
            let host = Arc::new(Channels(Default::default()));
            let sys = System::default().with_updater(Some(host.clone()));
            let checked = |channel: Option<&str>| {
                tokio::runtime::Builder::new_current_thread()
                    .build()
                    .unwrap()
                    .block_on(check(sys.clone(), channel.map(str::to_owned), None))
            };
            // The default source has nothing newer; the beta channel has.
            assert!(checked(None).unwrap().is_none());
            let update = checked(Some("beta")).unwrap().unwrap();
            assert_eq!(update.version, "1.1.0-beta.1");
            assert_eq!(update.url, "https://x.dev/beta/a.tar.gz");
            assert_eq!(
                *host.0.lock().unwrap(),
                [
                    STABLE.to_owned(),
                    format!("{STABLE}.sig"),
                    BETA.to_owned(),
                    format!("{BETA}.sig"),
                ]
            );
            // A name the package does not declare reaches no host.
            host.0.lock().unwrap().clear();
            assert!(checked(Some("nightly")).is_err());
            assert!(checked(Some("https://evil.dev/latest.json")).is_err());
            assert!(host.0.lock().unwrap().is_empty());
        }

        #[test]
        fn a_check_downloads_only_the_notes_of_its_locale() {
            let host = Arc::new(Channels(Default::default()));
            let sys = System::default().with_updater(Some(host.clone()));
            let notes = |locale: Option<&str>| {
                host.0.lock().unwrap().clear();
                let update = tokio::runtime::Builder::new_current_thread()
                    .build()
                    .unwrap()
                    .block_on(check(
                        sys.clone(),
                        Some("beta".into()),
                        locale.map(str::to_owned),
                    ))
                    .unwrap()
                    .unwrap();
                let fetched = host.0.lock().unwrap().len();
                (update.notes.unwrap(), fetched)
            };
            assert_eq!(notes(None), ("Fixes".to_owned(), 2));
            assert_eq!(notes(Some("zh-TW")), ("修復".to_owned(), 3));
            // No file for it, or one that fails its hash: the default notes.
            assert_eq!(notes(Some("de")), ("Fixes".to_owned(), 2));
            assert_eq!(notes(Some("ja")), ("Fixes".to_owned(), 3));
        }

        /// Nothing waits to install when no check found an update the app
        /// can install, and the rejection says so.
        #[test]
        fn install_on_quit_needs_a_downloaded_update() {
            let sys = System::default().with_updater(Some(Arc::new(Channels(Default::default()))));
            let error = tokio::runtime::Builder::new_current_thread()
                .build()
                .unwrap()
                .block_on(install_on_quit(sys))
                .unwrap_err();
            assert_eq!(error.code(), Some(codes::UNAVAILABLE));
        }

        #[test]
        fn operations_are_serial_and_release_on_error_or_cancellation() {
            let found = Found::default();
            let operation = begin(&found).unwrap();
            assert!(begin(&found).is_err());
            drop(operation);
            let operation = begin(&found).unwrap();
            assert!(begin(&found).is_err());
            drop(operation);
            assert!(begin(&found).is_ok());
        }

        struct TempDir(PathBuf);

        impl TempDir {
            fn new(name: &str) -> Self {
                static NEXT: std::sync::atomic::AtomicU32 = std::sync::atomic::AtomicU32::new(0);
                let n = NEXT.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
                let dir = std::env::temp_dir().join(format!(
                    "nsh-core-updater-{name}-{}-{n}",
                    std::process::id()
                ));
                let _ = std::fs::remove_dir_all(&dir);
                std::fs::create_dir_all(&dir).unwrap();
                Self(dir)
            }

            fn path(&self) -> &std::path::Path {
                &self.0
            }
        }

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

        /// Scripted `UpdaterHost` for testing range resume, fallback, 416,
        /// server-capped slices and OTA package verification.
        struct ScriptedHost {
            payload: Vec<u8>,
            /// Each payload `get` pops one scripted action from the front.
            script: std::sync::Mutex<std::collections::VecDeque<ScriptedResponse>>,
            /// Byte offsets requested in order.
            offsets: std::sync::Mutex<Vec<u64>>,
            /// Optional package manifest + verify result for `download_package`.
            package_config: Option<Vec<u8>>,
            package_manifest: Option<Vec<u8>>,
            verify_error: Option<String>,
            verify_calls: std::sync::atomic::AtomicUsize,
        }

        enum ScriptedResponse {
            /// Send `bytes` from `from` (or `0` when `from == 0`), then fail with `err`.
            DropAfter { bytes: usize, err: &'static str },
            /// Honor `Range` (`206` when `from > 0`, `200` when `from == 0`) for the rest of `payload`.
            Complete,
            /// Honor `Range` (`206`) for at most `max_bytes` from `from` (server-capped slice).
            CappedSlice(usize),
            /// Ignore `Range` and send full `payload` as `200 OK`.
            IgnoreRangeFull,
            /// Reply `416 Range Not Satisfiable`.
            Unsatisfiable,
            /// Fail immediately before sending headers.
            ImmediateErr(&'static str),
        }

        impl ScriptedHost {
            fn new(payload: &[u8], script: impl IntoIterator<Item = ScriptedResponse>) -> Self {
                Self {
                    payload: payload.to_vec(),
                    script: std::sync::Mutex::new(script.into_iter().collect()),
                    offsets: std::sync::Mutex::new(Vec::new()),
                    package_config: None,
                    package_manifest: None,
                    verify_error: None,
                    verify_calls: std::sync::atomic::AtomicUsize::new(0),
                }
            }

            fn offsets(&self) -> Vec<u64> {
                self.offsets.lock().unwrap().clone()
            }
        }

        impl UpdaterHost for ScriptedHost {
            fn config(&self) -> Option<Vec<u8>> {
                self.package_config.clone()
            }

            fn get(&self, url: &str, from: u64, sink: UpdaterSink<'_>) -> Result<(), String> {
                if let Some(manifest) = &self.package_manifest
                    && !url.ends_with(".nshpkg")
                {
                    if url.ends_with(".sig") {
                        let sig = nsh_updater::sign(SECRET, manifest).unwrap();
                        return sink(0, sig.as_bytes(), Some(sig.len() as u64));
                    }
                    return sink(0, manifest, Some(manifest.len() as u64));
                }
                self.offsets.lock().unwrap().push(from);
                let action = self
                    .script
                    .lock()
                    .unwrap()
                    .pop_front()
                    .expect("scripted response");
                let total = Some(self.payload.len() as u64);
                let start = from as usize;
                match action {
                    ScriptedResponse::DropAfter { bytes, err } => {
                        let end = (start + bytes).min(self.payload.len());
                        if start < end {
                            sink(from, &self.payload[start..end], total)?;
                        }
                        Err(err.to_owned())
                    }
                    ScriptedResponse::Complete => sink(from, &self.payload[start..], total),
                    ScriptedResponse::CappedSlice(max_bytes) => {
                        let end = (start + max_bytes).min(self.payload.len());
                        sink(from, &self.payload[start..end], total)
                    }
                    ScriptedResponse::IgnoreRangeFull => sink(0, &self.payload, total),
                    ScriptedResponse::Unsatisfiable => Ok(()),
                    ScriptedResponse::ImmediateErr(err) => Err(err.to_owned()),
                }
            }

            fn verify_package(
                &self,
                _file: &std::path::Path,
                _updates: &std::path::Path,
                _app_id: &str,
                _roots: &[String],
                _newer_than: u64,
            ) -> Result<PackageVersion, String> {
                self.verify_calls
                    .fetch_add(1, std::sync::atomic::Ordering::Relaxed);
                if let Some(err) = &self.verify_error {
                    return Err(err.clone());
                }
                Ok(PackageVersion {
                    version: "1.0.1".into(),
                    build: 2,
                })
            }
        }

        #[test]
        fn interrupted_download_resumes_from_partial_file() {
            let dir = TempDir::new("resume");
            let payload = b"0123456789abcdef";
            let sha256 = nsh_updater::sha256(payload);
            let part = dir.path().join(format!("download-{sha256}.part"));
            let dest = dir.path().join(format!("download-{sha256}"));
            let host = ScriptedHost::new(
                payload,
                [
                    ScriptedResponse::DropAfter {
                        bytes: 6,
                        err: "connection reset",
                    },
                    ScriptedResponse::DropAfter {
                        bytes: 5,
                        err: "timed out",
                    },
                    ScriptedResponse::Complete,
                ],
            );
            let target = DownloadTarget {
                host: &host,
                url: "https://x.dev/app.tar.gz",
                part: &part,
                dest: &dest,
                remove_invalid_dest: true,
                sha256: &sha256,
                size: Some(payload.len() as u64),
            };

            // First interruption keeps the 6 bytes received.
            let err = download_resumable(&target, |_, _| true, |_| Ok(())).unwrap_err();
            assert!(err.contains("connection reset"), "{err}");
            assert_eq!(std::fs::read(&part).unwrap(), &payload[..6]);
            assert!(!dest.exists());

            // Second interruption appends 5 more bytes and keeps 11 bytes.
            let mut progress = Vec::new();
            let err = download_resumable(
                &target,
                |r, t| {
                    progress.push((r, t));
                    true
                },
                |_| Ok(()),
            )
            .unwrap_err();
            assert!(err.contains("timed out"), "{err}");
            assert_eq!(std::fs::read(&part).unwrap(), &payload[..11]);
            assert_eq!(progress, [(6, Some(16)), (11, Some(16))]);

            // Third attempt finishes from byte 11 and atomically moves into `dest`.
            progress.clear();
            download_resumable(
                &target,
                |r, t| {
                    progress.push((r, t));
                    true
                },
                |_| Ok(()),
            )
            .unwrap();
            assert_eq!(host.offsets(), [0, 6, 11]);
            assert_eq!(progress, [(11, Some(16)), (16, Some(16))]);
            assert!(!part.exists());
            assert_eq!(std::fs::read(&dest).unwrap(), payload);
        }

        #[test]
        fn downloads_resume_from_existing_bytes() {
            let full = b"hello resumable world";
            let hash = nsh_updater::sha256(full);
            let size = Some(full.len() as u64);

            // Partial prefix + appended tail verifies identically to a single pass.
            let mut cursor = std::io::Cursor::new(&full[..6]);
            let mut resumed = Download::resume(&hash, size, &mut cursor).unwrap();
            assert_eq!(resumed.received(), 6);
            assert_eq!(resumed.update(&full[6..]).unwrap(), full.len() as u64);
            assert_eq!(resumed.received(), full.len() as u64);
            assert!(resumed.finish().is_ok());

            // An empty partial starts at 0.
            let mut empty = std::io::Cursor::new(&b""[..]);
            let mut fresh = Download::resume(&hash, size, &mut empty).unwrap();
            assert_eq!(fresh.received(), 0);
            fresh.update(full).unwrap();
            assert!(fresh.finish().is_ok());

            // A partial already holding all bytes finishes immediately.
            let mut complete = std::io::Cursor::new(&full[..]);
            let done = Download::resume(&hash, size, &mut complete).unwrap();
            assert_eq!(done.received(), full.len() as u64);
            assert!(done.finish().is_ok());

            // A partial larger than the manifest size is refused before any range request.
            let mut oversized = std::io::Cursor::new(b"hello resumable world!");
            assert!(Download::resume(&hash, size, &mut oversized).is_err());

            // A corrupted partial prefix fails verification once the tail arrives.
            let mut corrupt = std::io::Cursor::new(b"Hxllo ");
            let mut bad = Download::resume(&hash, size, &mut corrupt).unwrap();
            bad.update(&full[6..]).unwrap();
            assert!(bad.finish().is_err());
        }

        #[test]
        fn a_second_download_of_the_same_asset_is_refused() {
            let dir = TempDir::new("part-lock");
            let part = dir.path().join("download-abc.part");
            let held = lock_part(&part).unwrap();
            let err = lock_part(&part).unwrap_err();
            assert!(err.contains("in progress"), "{err}");
            drop(held);
            lock_part(&part).unwrap();
        }

        #[test]
        fn resume_falls_back_when_server_ignores_range() {
            let dir = TempDir::new("ignore-range");
            let payload = b"abcdefghijkl";
            let sha256 = nsh_updater::sha256(payload);
            let part = dir.path().join(format!("download-{sha256}.part"));
            let dest = dir.path().join(format!("download-{sha256}"));
            std::fs::write(&part, &payload[..5]).unwrap();

            let host = ScriptedHost::new(payload, [ScriptedResponse::IgnoreRangeFull]);
            let target = DownloadTarget {
                host: &host,
                url: "https://x.dev/app.tar.gz",
                part: &part,
                dest: &dest,
                remove_invalid_dest: true,
                sha256: &sha256,
                size: Some(payload.len() as u64),
            };
            let mut progress = Vec::new();
            download_resumable(
                &target,
                |r, t| {
                    progress.push((r, t));
                    true
                },
                |_| Ok(()),
            )
            .unwrap();

            assert_eq!(host.offsets(), [5]);
            // Progress never jumped to 5 before resetting to 0.
            assert_eq!(progress, [(12, Some(12))]);
            assert!(!part.exists());
            assert_eq!(std::fs::read(&dest).unwrap(), payload);
        }

        #[test]
        fn resume_recovers_from_corrupt_partial_file() {
            let dir = TempDir::new("corrupt-part");
            let payload = b"0123456789";
            let sha256 = nsh_updater::sha256(payload);
            let part = dir.path().join(format!("download-{sha256}.part"));
            let dest = dir.path().join(format!("download-{sha256}"));
            std::fs::write(&part, b"XXXXX").unwrap();

            let host = ScriptedHost::new(
                payload,
                [ScriptedResponse::Complete, ScriptedResponse::Complete],
            );
            let target = DownloadTarget {
                host: &host,
                url: "https://x.dev/app.tar.gz",
                part: &part,
                dest: &dest,
                remove_invalid_dest: true,
                sha256: &sha256,
                size: Some(payload.len() as u64),
            };
            download_resumable(&target, |_, _| true, |_| Ok(())).unwrap();
            // First tried resuming from 5, saw SHA-256 mismatch, deleted part, restarted from 0.
            assert_eq!(host.offsets(), [5, 0]);
            assert!(!part.exists());
            assert_eq!(std::fs::read(&dest).unwrap(), payload);
        }

        #[test]
        fn resume_handles_416_for_complete_and_stale_partials() {
            let dir = TempDir::new("416");
            let payload = b"already-complete";
            let sha256 = nsh_updater::sha256(payload);
            let part = dir.path().join(format!("download-{sha256}.part"));
            let dest = dir.path().join(format!("download-{sha256}"));

            // Manifest omits `size`, partial is already complete: 416 verifies and finishes.
            std::fs::write(&part, payload).unwrap();
            let host = ScriptedHost::new(payload, [ScriptedResponse::Unsatisfiable]);
            let target = DownloadTarget {
                host: &host,
                url: "https://x.dev/app.tar.gz",
                part: &part,
                dest: &dest,
                remove_invalid_dest: true,
                sha256: &sha256,
                size: None,
            };
            download_resumable(&target, |_, _| true, |_| Ok(())).unwrap();
            assert_eq!(host.offsets(), [payload.len() as u64]);
            assert!(!part.exists());
            assert_eq!(std::fs::read(&dest).unwrap(), payload);

            // Corrupt/oversized partial + 416: deletes part and retries once from 0.
            std::fs::remove_file(&dest).unwrap();
            std::fs::write(&part, b"stale-oversized-bytes-on-disk").unwrap();
            let host = ScriptedHost::new(
                payload,
                [ScriptedResponse::Unsatisfiable, ScriptedResponse::Complete],
            );
            let target = DownloadTarget {
                host: &host,
                ..target
            };
            download_resumable(&target, |_, _| true, |_| Ok(())).unwrap();
            assert_eq!(host.offsets(), [29, 0]);
            assert!(!part.exists());
            assert_eq!(std::fs::read(&dest).unwrap(), payload);
        }

        #[test]
        fn resume_reuses_already_complete_part_or_dest_without_network() {
            let dir = TempDir::new("reuse-complete");
            let payload = b"cached-archive";
            let sha256 = nsh_updater::sha256(payload);
            let part = dir.path().join(format!("download-{sha256}.part"));
            let dest = dir.path().join(format!("download-{sha256}"));

            // Complete `.part` with known `size` needs zero HTTP requests.
            std::fs::write(&part, payload).unwrap();
            let host = ScriptedHost::new(payload, []);
            let target = DownloadTarget {
                host: &host,
                url: "https://x.dev/app.tar.gz",
                part: &part,
                dest: &dest,
                remove_invalid_dest: true,
                sha256: &sha256,
                size: Some(payload.len() as u64),
            };
            download_resumable(&target, |_, _| true, |_| Ok(())).unwrap();
            assert!(host.offsets().is_empty());
            assert!(!part.exists());
            assert_eq!(std::fs::read(&dest).unwrap(), payload);

            // Calling again with `dest` already verified also needs zero HTTP requests.
            download_resumable(&target, |_, _| true, |_| Ok(())).unwrap();
            assert!(host.offsets().is_empty());
        }

        #[test]
        fn resume_continues_across_server_capped_206_slices() {
            let dir = TempDir::new("capped-206");
            let payload = b"0123456789abcdef";
            let sha256 = nsh_updater::sha256(payload);
            let part = dir.path().join(format!("download-{sha256}.part"));
            let dest = dir.path().join(format!("download-{sha256}"));
            std::fs::write(&part, &payload[..4]).unwrap();

            let host = ScriptedHost::new(
                payload,
                [
                    ScriptedResponse::CappedSlice(4),
                    ScriptedResponse::CappedSlice(4),
                    ScriptedResponse::CappedSlice(4),
                ],
            );
            let target = DownloadTarget {
                host: &host,
                url: "https://x.dev/app.tar.gz",
                part: &part,
                dest: &dest,
                remove_invalid_dest: true,
                sha256: &sha256,
                size: Some(payload.len() as u64),
            };
            download_resumable(&target, |_, _| true, |_| Ok(())).unwrap();
            assert_eq!(host.offsets(), [4, 8, 12]);
            assert_eq!(std::fs::read(&dest).unwrap(), payload);
        }

        #[test]
        fn stale_partials_are_cleaned_without_touching_other_update_files() {
            let dir = TempDir::new("cleanup");
            std::fs::write(dir.path().join("download-old.part"), b"old").unwrap();
            std::fs::write(dir.path().join("download-old.part.lock"), b"").unwrap();
            std::fs::write(dir.path().join("download-old"), b"old-done").unwrap();
            std::fs::write(dir.path().join("download-keep.part"), b"keep").unwrap();
            std::fs::write(dir.path().join("download-keep.part.lock"), b"").unwrap();
            std::fs::write(dir.path().join("next.nshpkg"), b"pkg").unwrap();
            std::fs::write(dir.path().join("next.nshpkg.part"), b"legacy").unwrap();
            std::fs::write(dir.path().join("next-old.nshpkg.part"), b"old-pkg").unwrap();
            std::fs::write(dir.path().join("next-old.nshpkg.part.lock"), b"").unwrap();
            std::fs::write(dir.path().join("next-keep.nshpkg.part"), b"keep-pkg").unwrap();
            std::fs::write(dir.path().join("next-keep.nshpkg.part.lock"), b"").unwrap();

            cleanup_stale_downloads(dir.path(), "download-keep.part", "download-keep");
            assert!(!dir.path().join("download-old.part").exists());
            assert!(!dir.path().join("download-old.part.lock").exists());
            assert!(!dir.path().join("download-old").exists());
            assert!(dir.path().join("download-keep.part").exists());
            assert!(dir.path().join("download-keep.part.lock").exists());
            assert!(dir.path().join("next.nshpkg").exists());
            assert!(dir.path().join("next-keep.nshpkg.part").exists());

            cleanup_stale_package_parts(dir.path(), "next-keep.nshpkg.part");
            assert!(!dir.path().join("next.nshpkg.part").exists());
            assert!(!dir.path().join("next-old.nshpkg.part").exists());
            assert!(!dir.path().join("next-old.nshpkg.part.lock").exists());
            assert!(dir.path().join("next-keep.nshpkg.part").exists());
            assert!(dir.path().join("next-keep.nshpkg.part.lock").exists());
            assert!(dir.path().join("next.nshpkg").exists());

            // Unusual hashes never produce path separators or escape `dir`.
            let key = part_key("../../etc/passwd");
            assert!(!key.contains('/') && !key.contains('.'));
            assert_eq!(key.len(), 64);
        }

        #[test]
        fn resume_ignores_empty_chunks_and_rejects_misaligned_offsets() {
            enum Mode {
                EmptyFullThenFail,
                MisalignedSecondChunk,
                EmptyPartialThenFinish,
            }

            struct ChunkHost {
                payload: Vec<u8>,
                modes: std::sync::Mutex<std::collections::VecDeque<Mode>>,
            }

            impl UpdaterHost for ChunkHost {
                fn config(&self) -> Option<Vec<u8>> {
                    None
                }

                fn get(&self, _url: &str, from: u64, sink: UpdaterSink<'_>) -> Result<(), String> {
                    let total = Some(self.payload.len() as u64);
                    let start = from as usize;
                    match self.modes.lock().unwrap().pop_front().unwrap() {
                        Mode::EmptyFullThenFail => {
                            sink(0, b"", total)?;
                            Err("connection reset".into())
                        }
                        Mode::MisalignedSecondChunk => {
                            sink(from, &self.payload[start..start + 2], total)?;
                            sink(from, &self.payload[start + 2..], total)
                        }
                        Mode::EmptyPartialThenFinish => {
                            sink(from, b"", total)?;
                            sink(from, &self.payload[start..], total)
                        }
                    }
                }
            }

            let dir = TempDir::new("chunk-offsets");
            let payload = b"0123456789";
            let sha256 = nsh_updater::sha256(payload);
            let part = dir.path().join(format!("download-{sha256}.part"));
            let dest = dir.path().join(format!("download-{sha256}"));
            std::fs::write(&part, &payload[..4]).unwrap();

            let host = ChunkHost {
                payload: payload.to_vec(),
                modes: std::sync::Mutex::new(
                    [
                        Mode::EmptyFullThenFail,
                        Mode::MisalignedSecondChunk,
                        Mode::EmptyPartialThenFinish,
                    ]
                    .into_iter()
                    .collect(),
                ),
            };
            let target = DownloadTarget {
                host: &host,
                url: "https://x.dev/app.tar.gz",
                part: &part,
                dest: &dest,
                remove_invalid_dest: true,
                sha256: &sha256,
                size: Some(payload.len() as u64),
            };

            let err = download_resumable(&target, |_, _| true, |_| Ok(())).unwrap_err();
            assert!(err.contains("connection reset"), "{err}");
            assert_eq!(std::fs::read(&part).unwrap(), &payload[..4]);

            let err = download_resumable(&target, |_, _| true, |_| Ok(())).unwrap_err();
            assert!(err.contains("expected 6"), "{err}");
            assert_eq!(std::fs::read(&part).unwrap(), &payload[..6]);

            let mut progress = Vec::new();
            download_resumable(
                &target,
                |r, t| {
                    progress.push((r, t));
                    true
                },
                |_| Ok(()),
            )
            .unwrap();
            assert_eq!(progress, [(6, Some(10)), (10, Some(10))]);
            assert_eq!(std::fs::read(&dest).unwrap(), payload);
        }

        #[test]
        fn download_package_resumes_and_cleans_up_refused_packages() {
            let dir = TempDir::new("package-resume");
            let payload = b"signed-nshpkg-bytes";
            let sha256 = nsh_updater::sha256(payload);
            let config = format!(
                r#"{{"id":"dev.noop.app","version":"1.0.0","build":1,"url":"{STABLE}","publicKey":"{PUBLIC}","packages":{{"roots":["{PUBLIC}"]}}}}"#
            )
            .into_bytes();
            let manifest = serde_json::to_vec(&serde_json::json!({
                "id": "dev.noop.app",
                "version": "1.0.1",
                "platforms": {},
                "package": {
                    "version": "1.0.1",
                    "build": 2,
                    "url": "app-2.nshpkg",
                    "sha256": sha256,
                    "size": payload.len(),
                    "minRuntime": "0.1.0",
                },
            }))
            .unwrap();

            // Pre-existing older `next.nshpkg` must survive an interrupted newer download.
            let updates = dir.path().join(UPDATES);
            std::fs::create_dir_all(&updates).unwrap();
            std::fs::write(updates.join(NEXT), b"older-build").unwrap();

            let mut host = ScriptedHost::new(
                payload,
                [
                    ScriptedResponse::DropAfter {
                        bytes: 7,
                        err: "network lost",
                    },
                    ScriptedResponse::ImmediateErr("HTTP 503"),
                    ScriptedResponse::Complete,
                ],
            );
            host.package_config = Some(config.clone());
            host.package_manifest = Some(manifest.clone());
            let host = Arc::new(host);

            let mut sys = System::default().with_updater(Some(host.clone()));
            sys.data_dir = Some(dir.path().to_path_buf());
            let rt = tokio::runtime::Builder::new_current_thread()
                .build()
                .unwrap();

            let offered = rt
                .block_on(check_package(sys.clone(), None))
                .unwrap()
                .unwrap();
            assert_eq!(offered.build, 2.0);

            // Attempt 1 drops after 7 bytes: part stays, older `next.nshpkg` stays.
            let err = rt
                .block_on(download_package(sys.clone(), None))
                .unwrap_err();
            assert!(format!("{err:?}").contains("network lost"), "{err:?}");
            let part = updates.join(format!("next-{sha256}.nshpkg.part"));
            assert_eq!(std::fs::read(&part).unwrap(), &payload[..7]);
            assert_eq!(std::fs::read(updates.join(NEXT)).unwrap(), b"older-build");

            // Attempt 2 hits HTTP 503: part still stays at 7 bytes.
            let err = rt
                .block_on(download_package(sys.clone(), None))
                .unwrap_err();
            assert!(format!("{err:?}").contains("HTTP 503"), "{err:?}");
            assert_eq!(std::fs::read(&part).unwrap(), &payload[..7]);

            // Attempt 3 resumes from byte 7, verifies, and atomically replaces `next.nshpkg`.
            rt.block_on(download_package(sys.clone(), None)).unwrap();
            assert_eq!(host.offsets(), [0, 7, 7]);
            assert!(!part.exists());
            assert_eq!(std::fs::read(updates.join(NEXT)).unwrap(), payload);

            // A package refused by `verify_package` removes the part and does not re-download.
            let dir2 = TempDir::new("package-refused");
            let mut bad_host = ScriptedHost::new(payload, [ScriptedResponse::Complete]);
            bad_host.package_config = Some(config);
            bad_host.package_manifest = Some(manifest);
            bad_host.verify_error = Some("not trusted".into());
            let bad_host = Arc::new(bad_host);
            let mut sys2 = System::default().with_updater(Some(bad_host.clone()));
            sys2.data_dir = Some(dir2.path().to_path_buf());
            rt.block_on(check_package(sys2.clone(), None))
                .unwrap()
                .unwrap();
            let err = rt.block_on(download_package(sys2, None)).unwrap_err();
            assert!(format!("{err:?}").contains("not trusted"), "{err:?}");
            assert_eq!(bad_host.offsets(), [0]);
            assert_eq!(
                bad_host
                    .verify_calls
                    .load(std::sync::atomic::Ordering::Relaxed),
                1
            );
            assert!(
                !dir2
                    .path()
                    .join(UPDATES)
                    .join(format!("next-{sha256}.nshpkg.part"))
                    .exists()
            );
            assert!(!dir2.path().join(UPDATES).join(NEXT).exists());
        }
    }
}

/// Nothing updates on the web (a deploy updates its apps), nor in a build
/// without the `updater` feature.
#[cfg(any(target_arch = "wasm32", not(feature = "updater")))]
mod native {
    use super::*;

    /// Fails when the app ships an update configuration a native runtime
    /// without the feature cannot act on: saying so beats reporting "no
    /// update" forever. Without one (a development run, an app without
    /// `"updater"`) there is nothing to check, as with the feature.
    #[cfg(not(target_arch = "wasm32"))]
    async fn configured(sys: &System) -> Result<(), JsError> {
        let Some(host) = sys.updater.clone() else {
            return Ok(());
        };
        if super::super::fs::blocking(move || Ok(host.config().is_some())).await? {
            return Err(JsError::Error(
                "this runtime was built without the NSH platform crate's feature `updater`".into(),
            )
            .with_code(codes::UNAVAILABLE));
        }
        Ok(())
    }

    #[cfg(target_arch = "wasm32")]
    async fn configured(_: &System) -> Result<(), JsError> {
        Ok(())
    }

    pub(super) async fn check(
        sys: System,
        _: Option<String>,
        _: Option<String>,
    ) -> Result<Option<AppUpdate>, JsError> {
        configured(&sys).await?;
        Ok(None)
    }

    pub(super) async fn shipped(_: &System) -> (Option<String>, Option<String>) {
        (None, None)
    }

    pub(super) async fn download(
        _: System,
        _: Option<Box<dyn Fn(UpdateProgress)>>,
    ) -> Result<(), JsError> {
        Err(unavailable())
    }

    pub(super) async fn install(_: System) -> Result<(), JsError> {
        Err(unavailable())
    }

    pub(super) async fn install_on_quit(_: System) -> Result<(), JsError> {
        Err(unavailable())
    }

    pub(super) async fn check_package(
        sys: System,
        _: Option<String>,
    ) -> Result<Option<PackageUpdate>, JsError> {
        configured(&sys).await?;
        Ok(None)
    }

    pub(super) async fn download_package(
        _: System,
        _: Option<Callback<UpdateProgress>>,
    ) -> Result<(), JsError> {
        Err(no_package())
    }

    pub(super) async fn restart_to_package(_: System) -> Result<(), JsError> {
        Err(no_package())
    }

    #[cfg(all(test, not(target_arch = "wasm32")))]
    mod tests {
        use std::sync::Arc;

        use super::*;

        struct Host(Option<&'static str>);

        impl UpdaterHost for Host {
            fn config(&self) -> Option<Vec<u8>> {
                self.0.map(|config| config.as_bytes().to_vec())
            }

            fn get(&self, _: &str, _: u64, _: UpdaterSink<'_>) -> Result<(), String> {
                Err("offline".into())
            }
        }

        #[test]
        fn only_a_configured_app_learns_the_feature_is_off() {
            let rt = tokio::runtime::Builder::new_current_thread()
                .build()
                .unwrap();
            let with = |host: Option<Host>| {
                System::default()
                    .with_updater(host.map(|host| Arc::new(host) as Arc<dyn UpdaterHost>))
            };
            // A development run or an app without `"updater"`: no update.
            for sys in [with(None), with(Some(Host(None)))] {
                assert!(
                    rt.block_on(check(sys.clone(), None, None))
                        .unwrap()
                        .is_none()
                );
                assert!(rt.block_on(check_package(sys, None)).unwrap().is_none());
            }
            // An app that ships a configuration hears why it cannot update.
            let sys = with(Some(Host(Some("{}"))));
            let err = rt.block_on(check(sys.clone(), None, None)).unwrap_err();
            assert_eq!(err.code(), Some(codes::UNAVAILABLE));
            let err = rt.block_on(check_package(sys, None)).unwrap_err();
            assert_eq!(err.code(), Some(codes::UNAVAILABLE));
        }
    }
}