nsh-cli 0.6.1

The nsh command line tool: build, package and run apps.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
//! The web as a target: `nsh package --target web` writes a static site
//! (as a frontend build does), and the dev server serves a web host page
//! (`nsh run --target web`).
//!
//! A packaged site, `<out>/<name>/`:
//!
//! ```text
//! index.html                       the app's page (the only file that changes in place)
//! manifest.webmanifest             name, icons, colors (installing it as an app)
//! nsh.<digest>.js                  the DOM runtime
//! nsh.<digest>.wasm                the engine, the core and the components
//! nsh-bindgen.<digest>.js          wasm-bindgen's glue, for crates that call JS (`crate::bindgen`)
//! snippets.<digest>/<crate>-<hash>/ the JS snippets the glue imports, under their own paths
//! app.<digest>.js                  the app's browser ES module
//! browser.<digest>/               the browser binding modules and licenses
//! host.<digest>.js                 the page's host (`nsh.json` `web.host`), when the app has one
//! <chunk>.<digest>.js              what `import()` loads, fetched when first imported
//! assets/<path>.<digest>.<ext>     `asset:/<path>`, loaded from its URL when used
//! icons/<size>.<digest>.png        favicon, apple-touch-icon, the manifest's
//! ```
//!
//! Every file but the page and the manifest has a digest of its content in
//! its name, so hosts cache them for good and a release only replaces the
//! page. The page names the others and maps `asset:/<path>` to its file;
//! fonts are fetched before the app starts, as text is measured with them
//! synchronously.
//!
//! The app's TSX runs as browser ES modules on the browser's JavaScript
//! engine; the development host's page does the same with development
//! bundles and hot updates.

use std::{
    collections::{BTreeMap, BTreeSet},
    fmt::Write as _,
    fs,
    net::SocketAddr,
    path::{Path, PathBuf},
    sync::OnceLock,
    time::Duration,
};

use anyhow::{Context as _, bail};
use sha2::{Digest as _, Sha256};

use crate::{
    build::Built,
    package::{AppConfig, PackageOptions},
    runtime::{self, WebRuntime},
    ui,
};

mod compress;
mod prune;
mod split;

/// The packaged page; `{{…}}` are filled in by [`page`].
const PAGE: &str = include_str!("web/index.html");
/// The page's own styles, a file of the site so a strict `style-src`
/// ('self') applies them; `{{colors}}` is filled in by [`page_style`].
const PAGE_STYLE: &str = include_str!("web/index.css");
/// The page's scripts: files of their own, so a strict `script-src` allows
/// them ('self'), the first one a classic script the page runs as it is
/// parsed.
const PAGE_PREFETCH: &str = include_str!("web/prefetch.js");
const PAGE_START: &str = include_str!("web/start.js");
/// The development host's page, which the dev server serves at `/`.
const DEV_PAGE: &str = include_str!("web/dev.html");

/// The UI sets the web runtime serves: nsh's, and for an app with
/// Rust components of its own the app's set too (its runtime is built with
/// the sets this command line has, `rust_app::RustApp::web`).
fn ui_sets() -> Vec<String> {
    crate::ui_sets()
        .iter()
        .map(|set| set.specifier.clone())
        .collect()
}

/// Hex digits of the SHA-256 in content-addressed names.
const DIGEST_LEN: usize = 10;

/// Ports `--run` tries first (the next free one is used).
const PORT: u16 = 4173;

/// Writes the site `<out>/<name>/` and, with `--run`, serves it on this
/// machine (the address is printed for the user to open).
pub(crate) fn package(
    options: &PackageOptions<'_>,
    config: &AppConfig,
    name: &str,
    id: &str,
    built: &Built,
) -> anyhow::Result<()> {
    // Before the runtime is built and the previous package removed.
    let base = page_base(config.web.base.as_deref())?;
    let served = ui_sets();
    let missing: Vec<&str> = built
        .ui_sets
        .iter()
        .map(String::as_str)
        .filter(|set| !served.iter().any(|s| s == set))
        .collect();
    if !missing.is_empty() {
        bail!(
            "the web runtime has no {}, which the app imports",
            missing.join(", ")
        );
    }
    let runtime = runtime::package_web()?;
    let dir = options.out.join(name);
    let step = ui::Step::start(format!("packaging {}", ui::path(&dir)));
    if dir.exists() {
        fs::remove_dir_all(&dir).with_context(|| format!("remove {}", dir.display()))?;
    }
    let asset_base = options.asset_url.map(asset_url).transpose()?;
    // The files' URLs are absolute under `web.base`: the page is served at
    // every route of the app, with no `<base href>` ([`page`]).
    let site = Site::new(
        &dir,
        asset_base
            .as_ref()
            .map(|base| base.prefix.clone())
            .or_else(|| base.clone()),
    )?;
    let chunk_sides;
    let js = fs::read_to_string(&runtime.js)
        .with_context(|| format!("read {}", runtime.js.display()))?;
    let browser_path =
        publish_browser_host(&site, runtime.js.parent().context("runtime has no parent")?)?;
    let js = js.replace(
        "./browser/runtime.js",
        &format!("./{browser_path}/runtime.js"),
    );
    // About a third of its size: the runtime is downloaded before anything
    // shows (the app's code is minified as it is bundled).
    let js = nsh_bundler::minify_module("nsh.js", &js).map_err(anyhow::Error::msg)?;
    let js = site.add("nsh.js", js.as_bytes())?;
    // Linked for the app: the parts of `nsh/base`, the standard modules
    // and the app's own UI sets it does not use are left out.
    let linkable =
        fs::read(&runtime.wasm).with_context(|| format!("read {}", runtime.wasm.display()))?;
    // The gates the entry needs are linked into `nsh.wasm`; each other one
    // an app's chunks use is a side module of its own, which the page
    // loads before the first chunk that needs it runs (`chunkSides`), after
    // the side modules of the code it shares with other gates (`sides`).
    let mut sides = serde_json::Map::new();
    let mut published = BTreeSet::new();
    // An app with Rust components of its own has a runtime built for it,
    // linkable as nsh's own, with a gate per UI set (`nsh_ui_set_*`).
    let (linked, has_math, has_emoji) = {
        let plan = GatePlan::of(built);
        if let Some(whole) = &plan.whole {
            ui::warn(whole);
        }
        // `EmojiPicker`'s emoji, fetched by the first picker, wherever it is.
        let has_emoji =
            plan.main.iter().any(|g| g == EMOJI_GATE) || plan.lazy().contains(&EMOJI_GATE);
        let exports = prune::exports(&linkable)?;
        let keep: Vec<&str> = exports
            .iter()
            .map(String::as_str)
            .filter(|name| !is_gate(name) || plan.main.iter().any(|g| g == name))
            .collect();
        let lazy: Vec<&str> = plan
            .lazy()
            .into_iter()
            .filter(|gate| exports.iter().any(|e| e == gate))
            .collect();
        let split = link(&linkable, runtime.glue.is_some(), &keep, &lazy)?;
        for side in &split.sides {
            let file = side_file_name(&side.gates);
            let url = site.add(&format!("{file}.wasm"), &side.bytes)?;
            if let Some(gate) = &side.gate {
                published.insert(gate.clone());
            }
            sides.insert(side.name.clone(), side_entry(side, &url));
        }
        chunk_sides = plan.chunk_sides(&published);
        // A runtime published whole has the typesetter in its main module.
        let has_math = published.contains(MATH_GATE)
            || (split.sides.is_empty() && exports.iter().any(|e| e == MATH_GATE));
        (split.main, has_math, has_emoji)
    };
    let wasm = site.add("nsh.wasm", &linked)?;
    // What the runtime fetches when it first needs it: the syntaxes it
    // highlights code with, which every page would download otherwise.
    let mut data = serde_json::Map::new();
    if let Some(path) = &runtime.syntaxes {
        let bytes = fs::read(path).with_context(|| format!("read {}", path.display()))?;
        data.insert(
            "syntaxes".into(),
            site.add(runtime::SYNTAXES, &bytes)?.into(),
        );
    }
    if has_math {
        data.insert(
            "math".into(),
            site.add(MATH_FONTS_FILE, nsh_base::MATH_FONTS)?.into(),
        );
    }
    if has_emoji {
        data.insert(
            "emoji".into(),
            site.add(EMOJI_FILE, nsh_base::EMOJI_DATA.as_bytes())?
                .into(),
        );
    }
    // The wasm-bindgen glue of a runtime whose crates call JS through
    // wasm-bindgen (`crate::bindgen`).
    let glue = runtime
        .glue
        .as_deref()
        .map(|path| publish_glue(&site, path))
        .transpose()?;
    // A Rust app has no code of its own besides its runtime: the page
    // mounts its root. Chunk id -> URL: the page hands the map to the
    // runtime, which fetches lazy chunks on demand. Static entry
    // dependencies are staged before startup.
    let modules = if built.rust_root {
        AppModules::default()
    } else {
        publish_modules(&site, &built.code, &built.chunks, built.native_use.as_ref())?
    };
    let AppModules {
        code,
        chunks,
        linked,
    } = modules;
    let mut assets = serde_json::Map::new();
    let mut fonts = Vec::new();
    for asset in &built.assets {
        let bytes = fs::read(&asset.source).with_context(|| format!("read {}", asset.path))?;
        let url = site.add(&format!("assets/{}", asset.path), &bytes)?;
        if is_font(&asset.path) {
            fonts.push(asset.path.clone());
        }
        assets.insert(asset.path.clone(), url.into());
    }
    // The page imports it as it is: one module, its imports absolute URLs.
    let host = match &config.web.host {
        Some(path) => {
            let path = crate::package::web_host_module(options.entry, path)?;
            let bytes = fs::read(&path)
                .with_context(|| format!("read nsh.json web.host {}", path.display()))?;
            Some(site.add("host.js", &bytes)?)
        }
        None => None,
    };
    let icons = match config.icon(crate::package::PackageTarget::Web) {
        Some(icon) => Icons::write(&site, icon)?,
        None => Icons::default(),
    };
    let (fonts, chunk_fonts) = font_loads(&fonts, &built.code, &built.chunks);
    let appearance = Appearance::of(config);
    fs::write(
        dir.join("manifest.webmanifest"),
        manifest(name, id, appearance, &icons),
    )?;
    let mut app = serde_json::json!({
        "id": id,
        "wasm": wasm,
        "code": code,
        "capabilities": built.capabilities_json(),
        "rust": built.rust_root,
        "assets": assets,
        "data": data,
        "fonts": fonts,
        "chunkFonts": chunk_fonts,
        "chunks": chunks,
        "sides": sides,
        "chunkSides": chunk_sides,
        "nativeExports": native_exports(built.native_use.as_ref()),
        "entryChunks": built.entry_imports,
        "host": host.map(|host| site.module(&host)),
    });
    if let Some(glue) = &glue {
        app["glue"] = glue.as_str().into();
    }
    app["runtime"] = site.module(&js).into();
    if linked {
        app["linked"] = true.into();
    }
    let style = site.add("nsh-page.css", page_style(appearance).as_bytes())?;
    let prefetch = site.add("nsh-prefetch.js", PAGE_PREFETCH.as_bytes())?;
    let start = site.add("nsh-start.js", PAGE_START.as_bytes())?;
    if let Some(history) = config.web.history {
        app["history"] = history.into();
    }
    // The site's base, which the app's relative URLs resolve against
    // (the player's `baseURL`): the page is served at the app's routes.
    if let Some(base) = &base {
        app["base"] = base.as_str().into();
    }
    let html = page(&PageParts {
        name,
        appearance,
        base: base.as_deref(),
        icons: &icons,
        runtime: &site.module(&js),
        wasm: &wasm,
        glue: glue.as_deref(),
        code: &code,
        fonts: &fonts
            .iter()
            .map(|path| assets[path].as_str().unwrap_or_default().to_owned())
            .collect::<Vec<_>>(),
        app: &app,
        connect: connect_src(
            built.capabilities.as_ref(),
            &page_connect(&config.web.connect)?,
        )
        // The page fetches its files from another origin there.
        .map(|sources| match &asset_base {
            Some(base) => format!("{sources} {}", base.origin),
            None => sources,
        })
        .as_deref(),
        style: &style,
        prefetch: &prefetch,
        start: &start,
    });
    fs::write(dir.join("index.html"), html)?;
    // What static hosts send browsers that accept it (`.br`, `.gz`).
    compress::precompress(&dir)?;
    step.done(format!(
        "packaged {}{}",
        ui::path(&dir),
        ui::dim(format!(", {}", ui::bytes(dir_size(&dir))))
    ));
    // What a server with a strict policy allows: the page's scripts and
    // styles are files ('self'), and so are a linked app's modules; the
    // runtime shows in-memory images and `svg()`s in their color as `blob:`
    // URLs and reads clipboard images as `data:` URIs.
    let whole = !(linked || built.rust_root);
    ui::detail(format!(
        "under a Content-Security-Policy it needs script-src 'self' 'wasm-unsafe-eval'{}; img-src 'self' blob: data:{}",
        if whole { " blob: 'unsafe-inline'" } else { "" },
        if whole {
            " (script-src: it loads a native module whole)"
        } else {
            ""
        }
    ));
    if options.run {
        serve(&dir, base.as_deref().unwrap_or("/"))?;
    }
    Ok(())
}

/// The page's entry for side module `side`, published at `url` (its
/// `sides`): `{ url, gate?, needs, onDemand }`. The gates of the entry and
/// of the chunks it imports statically are in the main module
/// ([`GatePlan::of`]), so a side module only holds code that a chunk
/// imported later (`chunkSides`, the page's `prepareChunk`) or the first
/// formula (`loadData("math")`) needs: every one is `onDemand`, which
/// `prefetchSides` and `preloadSides` leave alone, and is fetched by the
/// load that needs it, after the side modules it `needs`.
fn side_entry(side: &prune::split::Side, url: &str) -> serde_json::Value {
    let mut entry = serde_json::json!({ "url": url, "needs": side.needs, "onDemand": true });
    if let Some(gate) = &side.gate {
        entry["gate"] = gate.as_str().into();
    }
    entry
}

const TEXT_VIEW_GATE: &str = "nsh_part_text_view";
/// The gate of `TextView`'s formula typesetter; no chunk names it.
const MATH_GATE: &str = "nsh_feature_math";
/// The KaTeX fonts of the typesetter, which the page fetches with its code.
const MATH_FONTS_FILE: &str = "nsh-math.bin";
/// The gate of `EmojiPicker`.
const EMOJI_GATE: &str = "nsh_part_emoji_picker";
/// `EmojiPicker`'s emoji, which the page fetches for the first picker.
const EMOJI_FILE: &str = "nsh-emoji.bin";

/// The modules published for an app from its linkable runtime: the main
/// module with `keep` and side modules for `lazy` ([`split::split`]). A
/// runtime wasm-bindgen ran on (`bindgen`) is published whole, every gate
/// in it: wasm-bindgen rewrites the module and keeps the `linking` and
/// `reloc.*` sections of the one it read, which no longer describe it.
pub(crate) fn link(
    linkable: &[u8],
    bindgen: bool,
    keep: &[&str],
    lazy: &[&str],
) -> anyhow::Result<prune::split::Split> {
    if bindgen {
        return Ok(prune::split::Split {
            main: prune::unlinked(linkable)?,
            sides: Vec::new(),
        });
    }
    split::split(linkable, keep, lazy)
}

/// `nsh.json` `web.base`: a path from the host's root, ending in `/` (one
/// is added), in the form browsers send it: segments that are
/// [sent as written](is_sent_as_written). The prefix of the page's URLs, the
/// address `--run` prints and the path [`nsh_dev::SiteServer::start_at`]
/// matches requests against are then one string, which a browser neither
/// encodes (`/应用/` is `/%E5%BA%94%E7%94%A8/`) nor normalizes (`/a/%2e/`
/// is `/a/`); it has no empty segment either (`/a//`), which hosts collapse.
fn page_base(base: Option<&str>) -> anyhow::Result<Option<String>> {
    let Some(base) = base else { return Ok(None) };
    let segments = base
        .strip_prefix('/')
        .map(|path| path.strip_suffix('/').unwrap_or(path));
    let valid = base == "/" || segments.is_some_and(|path| path.split('/').all(is_sent_as_written));
    if !valid {
        bail!(
            "nsh.json web.base: {base:?} is not a path from the host's root as browsers send it \
             (ASCII letters, digits, `-`, `.`, `_`, `~` and %XX escapes), as \"/apps/counter/\""
        );
    }
    Ok(Some(if base.ends_with('/') {
        base.to_owned()
    } else {
        format!("{base}/")
    }))
}

/// Whether browsers send a path segment as it is written: not empty, not a
/// dot segment, and only unreserved characters (RFC 3986) and
/// percent-escapes, none of a `.` (the URL parser reads `%2e` as a dot).
fn is_sent_as_written(segment: &str) -> bool {
    if segment.is_empty() || segment == "." || segment == ".." {
        return false;
    }
    let bytes = segment.as_bytes();
    let mut i = 0;
    while i < bytes.len() {
        match bytes[i] {
            b'A'..=b'Z' | b'a'..=b'z' | b'0'..=b'9' | b'-' | b'.' | b'_' | b'~' => i += 1,
            b'%' => match bytes.get(i + 1..i + 3) {
                Some(hex)
                    if hex.iter().all(u8::is_ascii_hexdigit)
                        && !hex.eq_ignore_ascii_case(b"2e") =>
                {
                    i += 3
                }
                _ => return false,
            },
            _ => return false,
        }
    }
    true
}

/// Whether `name` is the export of a part of `nsh/base`, of a standard
/// module, of a feature (shaders) or of one of the app's own UI sets
/// ([`ui_set_gate`]), which a runtime linked for an app keeps only when it
/// uses them.
fn is_gate(name: &str) -> bool {
    ["nsh_part_", "nsh_std_", "nsh_feature_", UI_SET_GATE]
        .iter()
        .any(|prefix| name.starts_with(prefix))
}

/// What the app imports by name from each native module: the browser host
/// declares these names at start (`nativeExports`), so a chunk importing a
/// name a side module registers later still links. More than the app
/// gets is harmless: a name nothing registers stays `undefined`. The
/// host makes these modules with only these names, so an app that loads a
/// module by a computed specifier (`import(name)`) declares none.
fn native_exports(uses: Option<&nsh_bundler::NativeUse>) -> serde_json::Value {
    let mut out = serde_json::Map::new();
    if uses.is_some_and(|u| u.computed_import) {
        return out.into();
    }
    for (module, imported) in uses.map(|u| &u.imports).into_iter().flatten() {
        if let nsh_bundler::Imported::Names(names) = imported
            && !names.is_empty()
        {
            out.insert(module.clone(), names.iter().cloned().collect());
        }
    }
    out.into()
}

/// The app's modules as [`publish_modules`] wrote them.
#[derive(Default)]
struct AppModules {
    /// The entry's URL (empty for a Rust app, which has none).
    code: String,
    /// Each chunk's URL by its id.
    chunks: serde_json::Map<String, serde_json::Value>,
    /// Whether they import one another and the native modules by their
    /// files' URLs (the runtime's `linked`).
    linked: bool,
}

/// Writes the app's modules: its entry `code` (which chunks import as
/// `bundle.js`) and its `chunks`. Linked, their static imports name the
/// files they are published as, the entry and the chunks by relative URLs
/// and each native module by its facade, a file too ([`facade`]): the
/// browser resolves them with no import map, an inline script a strict
/// `script-src` refuses. A module's name carries the digest of everything
/// it imports, directly or not, so a file keeps its name only while what
/// it links to does. Not linked when the app imports a native module
/// whole (a namespace, `import()`), whose names only the runtime knows:
/// the runtime makes the map and the facades as the app starts.
fn publish_modules(
    site: &Site,
    code: &str,
    chunks: &[crate::build::BuiltChunk],
    uses: Option<&nsh_bundler::NativeUse>,
) -> anyhow::Result<AppModules> {
    const ENTRY: &str = "bundle.js";
    // Each native module the app imports by name, by its facade's path.
    let by_name = uses.filter(|uses| {
        !uses.computed_import
            && uses
                .imports
                .values()
                .all(|imported| matches!(imported, nsh_bundler::Imported::Names(_)))
    });
    let facades = match by_name {
        Some(uses) => {
            let mut facades = BTreeMap::new();
            for (specifier, imported) in &uses.imports {
                let nsh_bundler::Imported::Names(names) = imported else {
                    continue;
                };
                let names: Vec<&str> = names.iter().map(String::as_str).collect();
                let slug: String = specifier
                    .chars()
                    .map(|c| if c.is_ascii_alphanumeric() { c } else { '-' })
                    .collect();
                let path = site.put(
                    &format!("facades/{slug}.js"),
                    facade(specifier, &names).as_bytes(),
                )?;
                facades.insert(specifier.clone(), path);
            }
            Some(facades)
        }
        None => None,
    };
    let Some(facades) = facades else {
        let mut published = serde_json::Map::new();
        for chunk in chunks {
            let stem = chunk.id.strip_suffix(".js").unwrap_or(&chunk.id);
            let url = site.add(&format!("{stem}.js"), chunk.code.as_bytes())?;
            published.insert(chunk.id.clone(), url.into());
        }
        return Ok(AppModules {
            code: site.add("app.js", code.as_bytes())?,
            chunks: published,
            linked: false,
        });
    };
    // The modules by id, chunks' as they import them (`./Screen.js` is
    // `Screen.js`), and the ids of the modules each imports statically.
    let mut modules: BTreeMap<String, (&str, String)> = BTreeMap::new();
    modules.insert(ENTRY.to_owned(), (code, "app.js".to_owned()));
    for chunk in chunks {
        let id = chunk.id.trim_start_matches("./");
        let stem = id.strip_suffix(".js").unwrap_or(id);
        modules.insert(id.to_owned(), (&chunk.code, format!("{stem}.js")));
    }
    let mut imports: BTreeMap<&str, Vec<String>> = BTreeMap::new();
    for (id, (code, _)) in &modules {
        let mut found = Vec::new();
        nsh_bundler::rewrite_imports(id, code, |source| {
            if let Some(target) = join_path(id, source).filter(|t| modules.contains_key(t)) {
                found.push(target);
            }
            None
        })
        .map_err(anyhow::Error::msg)?;
        imports.insert(id, found);
    }
    // Every module's name, from the digest of the modules it reaches and
    // the facades.
    let mut names = BTreeMap::new();
    for id in modules.keys() {
        let mut reached = BTreeSet::from([id.as_str()]);
        let mut queue = vec![id.as_str()];
        while let Some(next) = queue.pop() {
            for target in &imports[next] {
                if reached.insert(target) {
                    queue.push(target);
                }
            }
        }
        let mut hasher = Sha256::new();
        for reached in &reached {
            hasher.update(reached.as_bytes());
            hasher.update([0]);
            hasher.update(modules[*reached].0.as_bytes());
            hasher.update([0]);
        }
        for (specifier, path) in &facades {
            hasher.update(specifier.as_bytes());
            hasher.update([0]);
            hasher.update(path.as_bytes());
            hasher.update([0]);
        }
        names.insert(
            id.as_str(),
            with_digest(&modules[id].1, &hex_digest(&hasher.finalize())),
        );
    }
    let mut published = AppModules {
        linked: true,
        ..AppModules::default()
    };
    for (id, (code, _)) in &modules {
        let name = &names[id.as_str()];
        let linked = nsh_bundler::rewrite_imports(id, code, |source| {
            let target = match join_path(id, source) {
                Some(target) if modules.contains_key(&target) => &names[target.as_str()],
                _ => facades.get(source)?,
            };
            Some(url_path(&relative_path(name, target)))
        })
        .map_err(anyhow::Error::msg)?;
        site.write(name, linked.as_bytes())?;
        let url = site.url(name);
        if id == ENTRY {
            published.code = url;
        }
    }
    for chunk in chunks {
        let name = &names[chunk.id.trim_start_matches("./")];
        published
            .chunks
            .insert(chunk.id.clone(), site.url(name).into());
    }
    Ok(published)
}

/// The id of the module `source` names from module `from` (both paths of
/// the site), or `None` for a source that is not a relative path.
fn join_path(from: &str, source: &str) -> Option<String> {
    if !source.starts_with("./") && !source.starts_with("../") {
        return None;
    }
    let mut parts: Vec<&str> = from.split('/').collect();
    parts.pop();
    for segment in source.split('/') {
        match segment {
            "." | "" => {}
            ".." => {
                parts.pop()?;
            }
            segment => parts.push(segment),
        }
    }
    Some(parts.join("/"))
}

/// The relative URL path of file `to` from file `from` (paths of the
/// site), as a module imports it: `./x.js`, `../x.js`.
fn relative_path(from: &str, to: &str) -> String {
    let mut from: Vec<&str> = from.split('/').collect();
    from.pop();
    let to: Vec<&str> = to.split('/').collect();
    let (dirs, file) = to.split_at(to.len() - 1);
    let common = from.iter().zip(dirs).take_while(|(a, b)| a == b).count();
    let mut out = if common == from.len() {
        "./".to_owned()
    } else {
        "../".repeat(from.len() - common)
    };
    for part in dirs[common..].iter().chain(file) {
        out.push_str(part);
        out.push('/');
    }
    out.pop();
    out
}

/// The module that stands for native module `specifier`, exporting `names`
/// as live bindings, read from the realm's namespace when it runs and again
/// when a later gate registers them (`browser/runtime.js`, `facadeURL`).
fn facade(specifier: &str, names: &[&str]) -> String {
    let key = serde_json::Value::from(specifier).to_string();
    let mut out = String::from("const host=globalThis.__nsh_browser_realm.modules;\n");
    let mut updates = String::new();
    for (index, name) in names.iter().enumerate() {
        let name = serde_json::Value::from(*name).to_string();
        let value = format!("host.get({key})[{name}]");
        let _ = writeln!(out, "let e{index}={value};export{{e{index} as {name}}};");
        let _ = write!(updates, "e{index}={value};");
    }
    if !updates.is_empty() {
        let _ = writeln!(
            out,
            "globalThis.__nsh_browser_realm.watch(()=>{{{updates}}});"
        );
    }
    out
}

/// Which gates ([`is_gate`]) a runtime published for an app links into its
/// main module and which each chunk loads as side modules.
#[derive(Debug, Default, PartialEq, Eq)]
struct GatePlan {
    /// Those of the entry and the chunks it imports statically: called
    /// before the app starts.
    main: Vec<String>,
    /// By chunk id, the gates its own code needs that `main` lacks (a
    /// chunk's static imports list theirs: the page prepares them too).
    chunks: BTreeMap<String, Vec<String>>,
    /// Why `main` keeps every gate when the bundler could have told which
    /// ones the app uses ([`whole_because`]), and in which code.
    whole: Option<Whole>,
}

/// What made a runtime linked for an app keep every gate: `why`, found in
/// `code` (the app's entry, or a chunk it imports statically).
#[derive(Debug, PartialEq, Eq)]
struct Whole {
    why: &'static str,
    code: String,
}

impl std::fmt::Display for Whole {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "the engine keeps every part of nsh/base, zlib and the shader compiler: {} ({}). Import NSH's modules by name to leave out what the app doesn't use",
            self.why, self.code
        )
    }
}

impl GatePlan {
    /// The plan for `built`: by what each chunk uses of the native
    /// modules; everything in the main module when that is not known for
    /// every chunk (a development or Rust app's build).
    fn of(built: &Built) -> Self {
        let per_chunk = built.entry_native_use.as_ref().and_then(|entry| {
            let chunks: Option<Vec<(&str, &nsh_bundler::NativeUse)>> = built
                .chunks
                .iter()
                .map(|c| c.native_use.as_ref().map(|u| (c.id.as_str(), u)))
                .collect();
            Some((entry, chunks?))
        });
        let Some((entry, chunks)) = per_chunk else {
            let whole = built
                .native_use
                .as_ref()
                .and_then(whole_because)
                .map(|why| Whole {
                    why,
                    code: "the app".to_owned(),
                });
            return Self {
                main: linked_gates(built.native_use.as_ref()),
                chunks: BTreeMap::new(),
                whole,
            };
        };
        let mut whole = whole_because(entry).map(|why| Whole {
            why,
            code: "the app's entry".to_owned(),
        });
        let mut first = entry.clone();
        for (id, uses) in &chunks {
            if built.entry_imports.iter().any(|e| e == id) {
                first.merge(uses);
                if whole.is_none() {
                    whole = whole_because(uses).map(|why| Whole {
                        why,
                        code: format!("its chunk {id}"),
                    });
                }
            }
        }
        let main = linked_gates(Some(&first));
        let mut plan = BTreeMap::new();
        for (id, uses) in chunks {
            if built.entry_imports.iter().any(|e| e == id) {
                continue;
            }
            let gates: Vec<String> = linked_gates(Some(uses))
                .into_iter()
                .filter(|g| !main.contains(g))
                .collect();
            if !gates.is_empty() {
                plan.insert(id.to_owned(), gates);
            }
        }
        Self {
            main,
            chunks: plan,
            whole,
        }
    }

    /// Every gate some chunk loads later, each once, sorted.
    fn lazy(&self) -> Vec<&str> {
        let mut set: BTreeSet<&str> = self.chunks.values().flatten().map(String::as_str).collect();
        // `TextView`'s formulas: asked for by the first one, wherever the
        // view is.
        if set.contains(TEXT_VIEW_GATE) || self.main.iter().any(|g| g == TEXT_VIEW_GATE) {
            set.insert(MATH_GATE);
        }
        set.into_iter().collect()
    }

    /// The page's map, chunk id -> the side modules (named by their gates)
    /// it loads, of those `published`; a gate without a side module (the
    /// main module has it) is left out.
    fn chunk_sides(
        &self,
        published: &BTreeSet<String>,
    ) -> serde_json::Map<String, serde_json::Value> {
        let mut map = serde_json::Map::new();
        for (id, gates) in &self.chunks {
            let list: Vec<serde_json::Value> = gates
                .iter()
                .filter(|gate| published.contains(*gate))
                .map(|gate| gate.as_str().into())
                .collect();
            if !list.is_empty() {
                map.insert(id.clone(), list.into());
            }
        }
        map
    }
}

/// The gates ([`is_gate`]) a runtime linked for an app that uses `uses` of
/// the native modules keeps: `nsh/base`'s core, the part of each other
/// name it imports from `nsh/base` (`nsh/base/plot` is the plot part's),
/// `zlib`'s operations when it imports `zlib`, and shaders when it may
/// make a shader view (`NativeUse::shader`, or an import of one of
/// [`SHADER_EXPORTS`]). What the bundler cannot tell apart (no analysis,
/// `import * as` of a namespace used other than by reading its members by
/// name, an `import()` of a computed specifier) keeps everything,
/// so a runtime never lacks what an app needs.
/// An app's own UI sets (`nsh.json` `"rust"`) keep the gate of each one it
/// imports ([`ui_set_gate`]).
pub(crate) fn linked_gates(uses: Option<&nsh_bundler::NativeUse>) -> Vec<String> {
    linked_gates_of(uses, &app_set_specifiers())
}

/// Why [`linked_gates`] keeps every gate for code that uses `uses`, when
/// the bundler read it: what the code does that it cannot tell apart.
/// `None` when it keeps only what the code uses. [`linked_gates_of`]
/// keeps everything by this, so the warning and the pruning agree.
fn whole_because(uses: &nsh_bundler::NativeUse) -> Option<&'static str> {
    if uses.computed_import {
        return Some("it loads a module whose name is worked out as it runs, `import(name)`");
    }
    match uses.imports.get(nsh_base::SPECIFIER) {
        Some(nsh_bundler::Imported::All) => Some(
            "it takes nsh/base whole, a namespace (`import * as`) used other than by reading its members by name, or `import(\"nsh/base\")`",
        ),
        _ => None,
    }
}

/// The prefix of the gates of an app's own UI sets ([`ui_set_gate`]).
/// Not `nsh_set_`: the runtime's `nsh_set_base_url` and `nsh_set_dark`
/// would read as gates.
const UI_SET_GATE: &str = "nsh_ui_set_";

/// The export of the app's web runtime that registers the UI set imported
/// as `specifier` (`#[native_module("app/charts")]`): `nsh_ui_set_app_charts`,
/// every character but ASCII letters and digits written `_`. The generated
/// runtime exports it (`rust_app.rs`, `nsh_platform_web::ui_set!`).
pub(crate) fn ui_set_gate(specifier: &str) -> String {
    let name: String = specifier
        .chars()
        .map(|c| if c.is_ascii_alphanumeric() { c } else { '_' })
        .collect();
    format!("{UI_SET_GATE}{name}")
}

/// The specifiers of the app's own UI sets: those the command serves
/// beyond nsh's own.
fn app_set_specifiers() -> Vec<String> {
    let own: Vec<&str> = crate::own_sets()
        .iter()
        .filter_map(|r| r.specifier())
        .collect();
    crate::ui_sets()
        .iter()
        .map(|set| set.specifier.clone())
        .filter(|s| !own.contains(&s.as_str()) && !s.starts_with("nsh/"))
        .collect()
}

/// [`linked_gates`] for an app whose own UI sets are `app_sets`: each one
/// it imports keeps its gate, and keeping everything keeps all of them.
fn linked_gates_of(uses: Option<&nsh_bundler::NativeUse>, app_sets: &[String]) -> Vec<String> {
    let all = || {
        let mut gates: Vec<String> = nsh_base::parts::ALL
            .iter()
            .map(|(name, _)| format!("nsh_part_{name}"))
            .collect();
        gates.push("nsh_std_zlib".into());
        gates.push("nsh_std_crypto".into());
        gates.push("nsh_std_url".into());
        gates.push("nsh_feature_shader".into());
        gates.extend(app_sets.iter().map(|s| ui_set_gate(s)));
        gates
    };
    let Some(uses) = uses else { return all() };
    if whole_because(uses).is_some() {
        return all();
    }
    let mut gates = Vec::new();
    if needs_base_core(uses, app_sets) {
        gates.push("nsh_part_core".to_owned());
    }
    if uses.global_crypto {
        gates.push("nsh_std_crypto".into());
    }
    if uses.shader {
        gates.push("nsh_feature_shader".into());
    }
    let mut add = |gate: String| {
        if !gates.contains(&gate) {
            gates.push(gate);
        }
    };
    for (module, imported) in &uses.imports {
        match module.as_str() {
            "zlib" | "node:zlib" => add("nsh_std_zlib".into()),
            "crypto" | "node:crypto" => add("nsh_std_crypto".into()),
            // `domainToASCII` and `domainToUnicode`; `URL` is the browser's.
            "url" | "node:url" => add("nsh_std_url".into()),
            // Shaders are `nsh`'s shader elements.
            "nsh" => match imported {
                nsh_bundler::Imported::All => add("nsh_feature_shader".into()),
                nsh_bundler::Imported::Names(names) => {
                    if names.iter().any(|n| SHADER_EXPORTS.contains(&n.as_str())) {
                        add("nsh_feature_shader".into());
                    }
                }
            },
            "nsh/base/plot" => add("nsh_part_plot".into()),
            // Taken whole, it kept everything above ([`whole_because`]).
            nsh_base::SPECIFIER => {
                if let nsh_bundler::Imported::Names(names) = imported {
                    for name in names {
                        if let Some(part) = part_of(name) {
                            add(format!("nsh_part_{part}"));
                        }
                    }
                }
            }
            module if app_sets.iter().any(|s| s == module) => add(ui_set_gate(module)),
            _ => {}
        }
    }
    gates
}

/// Whether an app that uses `uses` needs `nsh/base`'s core part: it
/// imports `nsh/base` or one of its parts, one of its own UI sets (whose
/// Rust components may build on the core's controls), or may give a view
/// a scroll bar theme of its own (the core draws built-in bars). An app
/// that only imports `nsh` gets the platform's scroll indicators either
/// way, and `AlertDialog` and `Toast` are `nsh/base`'s.
fn needs_base_core(uses: &nsh_bundler::NativeUse, app_sets: &[String]) -> bool {
    uses.scrollbar_theme
        || uses.imports.keys().any(|module| {
            module == nsh_base::SPECIFIER
                || module.starts_with(&format!("{}/", nsh_base::SPECIFIER))
                || app_sets.iter().any(|s| s == module)
        })
}

/// `nsh`'s exports whose views a shader draws: an import of one keeps the
/// shader compiler.
const SHADER_EXPORTS: &[&str] = &[
    "Shader",
    "ShaderEffect",
    "ShaderTransition",
    "ScreenEffect",
    "ScreenTransition",
];

/// The part of `nsh/base` beyond its core that exports `name`.
fn part_of(name: &str) -> Option<&'static str> {
    static PARTS: OnceLock<Vec<(&'static str, BTreeSet<String>)>> = OnceLock::new();
    let parts = PARTS.get_or_init(|| {
        let core = core_exports();
        nsh_base::parts::ALL
            .iter()
            .filter(|(name, _)| *name != "core")
            .map(|(name, part)| {
                let set =
                    nsh_base::registry_of([nsh_base::parts::core as nsh_base::parts::Part, *part]);
                let names = set_exports(&set)
                    .into_iter()
                    .filter(|n| !core.contains(n))
                    .collect();
                (*name, names)
            })
            .collect()
    });
    parts
        .iter()
        .find(|(_, names)| names.contains(name))
        .map(|(part, _)| *part)
}

/// The names a UI set exports: components, classes, modules and values.
fn set_exports(set: &nsh_core::element::Registry) -> BTreeSet<String> {
    let mut names: BTreeSet<String> = set.specs().iter().map(|s| s.export.to_string()).collect();
    names.extend(set.class_specs().iter().map(|c| c.name.to_owned()));
    names.extend(set.module_specs().iter().map(|m| m.name.to_owned()));
    names.extend(set.native_value_names().iter().map(|n| (*n).to_owned()));
    names
}

/// What `nsh/base`'s core part exports (`nsh_base::parts::core`, with
/// the navigation and `Input` every set has).
fn core_exports() -> &'static BTreeSet<String> {
    static EXPORTS: OnceLock<BTreeSet<String>> = OnceLock::new();
    EXPORTS.get_or_init(|| {
        set_exports(&nsh_base::registry_of([
            nsh_base::parts::core as nsh_base::parts::Part
        ]))
    })
}

/// Serves the site `dir` on this machine under `base` (`web.base`), as a
/// CDN would (`nsh_dev::SiteServer`), until stopped; prints its address.
fn serve(dir: &Path, base: &str) -> anyhow::Result<()> {
    let mut server = None;
    for port in (PORT..PORT + 10).chain([0]) {
        if let Ok(s) = nsh_dev::SiteServer::start_at(
            SocketAddr::from(([127, 0, 0, 1], port)),
            dir.into(),
            base,
        ) {
            server = Some(s);
            break;
        }
    }
    let server = server.context("no free port to serve the site on")?;
    let url = format!("http://127.0.0.1:{}{base}", server.local_addr().port());
    ui::note(format!("serving {} at {url}", ui::path(dir)));
    let _stoppable = ui::Stoppable::new();
    while !ui::stop_requested() {
        std::thread::sleep(Duration::from_millis(200));
    }
    Ok(())
}

/// The browser host's modules and their licenses next to `nsh.js` in
/// `root` (its `browser/`), by name: what the page imports them as. None
/// without the directory (a runtime that only runs a Rust app).
fn browser_host_paths(root: &Path) -> anyhow::Result<Vec<PathBuf>> {
    let dir = root.join("browser");
    if !dir.is_dir() {
        return Ok(Vec::new());
    }
    let mut paths = fs::read_dir(&dir)
        .with_context(|| format!("read {}", dir.display()))?
        .map(|entry| entry.map(|e| e.path()))
        .collect::<Result<Vec<_>, _>>()?;
    paths.retain(|path| {
        path.extension()
            .is_some_and(|ext| ext == "js" || ext == "txt")
    });
    paths.sort();
    Ok(paths)
}

/// The dev host's copies of the browser host's files ([`browser_host_paths`]),
/// served under `browser/`.
fn browser_host_files(root: &Path) -> anyhow::Result<Vec<(String, Vec<u8>)>> {
    browser_host_paths(root)?
        .into_iter()
        .map(|path| {
            let name = path
                .file_name()
                .context("host file has no name")?
                .to_string_lossy()
                .into_owned();
            let bytes = fs::read(&path).with_context(|| format!("read {}", path.display()))?;
            Ok((format!("browser/{name}"), bytes))
        })
        .collect()
}

fn minify_browser_host(name: &str, source: &str) -> anyhow::Result<String> {
    let code = nsh_bundler::minify_module(name, source).map_err(anyhow::Error::msg)?;
    // Module-mode compression removes strict directives. The bootstrap is
    // the one classic script: restore its ESM factories' strict semantics
    // after the final minification, before computing its content digest.
    Ok(if name == "bootstrap.js" {
        format!("\"use strict\";\n{code}")
    } else {
        code
    })
}

/// Version the complete facade directory together so relative ES imports
/// remain valid, and every public file keeps immutable cache semantics.
fn publish_browser_host(site: &Site, root: &Path) -> anyhow::Result<String> {
    let mut files = Vec::new();
    let mut content = Vec::new();
    for path in browser_host_paths(root)? {
        let name = path
            .file_name()
            .context("host file has no name")?
            .to_string_lossy()
            .into_owned();
        let code = fs::read_to_string(&path)?;
        let code = if name.ends_with(".js") {
            minify_browser_host(&name, &code)?
        } else {
            code
        };
        content.extend_from_slice(name.as_bytes());
        content.push(0);
        content.extend_from_slice(code.as_bytes());
        files.push((name, code));
    }
    let directory = format!("browser.{}", digest(&content));
    for (name, code) in files {
        site.write(&format!("{directory}/{name}"), code.as_bytes())?;
    }
    Ok(directory)
}

/// Adds the wasm-bindgen glue `path` (`crate::bindgen`) to `site`, named by
/// its content as the runtime is, and returns its URL. The snippets it
/// imports go under one directory named by all of them and their paths
/// (`snippets.<digest>/`), where they keep the paths they have under
/// `snippets/`: their imports of one another resolve as in development.
fn publish_glue(site: &Site, path: &Path) -> anyhow::Result<String> {
    let glue = fs::read_to_string(path).with_context(|| format!("read {}", path.display()))?;
    let mut snippets = Vec::new();
    for (name, file) in crate::bindgen::snippets(path)? {
        let bytes = fs::read(&file).with_context(|| format!("read {}", file.display()))?;
        snippets.push((name, bytes));
    }
    let glue = if snippets.is_empty() {
        glue
    } else {
        let mut all = Vec::new();
        for (name, bytes) in &snippets {
            all.extend(name.as_bytes());
            all.push(0);
            all.extend((bytes.len() as u64).to_le_bytes());
            all.extend(bytes);
        }
        let dir = format!("snippets.{}", digest(&all));
        for (name, bytes) in &snippets {
            let name = name.strip_prefix("snippets/").unwrap_or(name);
            site.write(&format!("{dir}/{name}"), bytes)?;
        }
        crate::bindgen::link_snippets(&glue, &dir)
    };
    let glue =
        nsh_bundler::minify_module(crate::bindgen::GLUE, &glue).map_err(anyhow::Error::msg)?;
    site.add(crate::bindgen::GLUE, glue.as_bytes())
}

/// The first [`DIGEST_LEN`] hex digits of the SHA-256 of `bytes`.
fn digest(bytes: &[u8]) -> String {
    hex_digest(&Sha256::digest(bytes))
}

/// The first [`DIGEST_LEN`] hex digits of `digest`, a SHA-256.
fn hex_digest(digest: &[u8]) -> String {
    let mut hex = String::with_capacity(DIGEST_LEN);
    for byte in digest.iter().take(DIGEST_LEN.div_ceil(2)) {
        let _ = write!(hex, "{byte:02x}");
    }
    hex.truncate(DIGEST_LEN);
    hex
}

/// Files by their path on the server.
pub(crate) type Files = Vec<(String, Vec<u8>)>;

/// The dev server's web host: its page and the runtime's files, for
/// `nsh_dev::DevServer::serve_web`. `nsh.version` names the runtime the
/// page was served with: an app with Rust components of its own gets a new
/// one when its Rust code changes (the session starts again), and a page
/// that reconnects to a server with another one loads again.
/// `rust`: a Rust app's runtime (`nsh.json` `"root"`), whose page runs its
/// root rather than the bundles the server sends. The page still names its
/// host module as `{{host}}`: [`with_page_host`] fills it in for an app.
pub(crate) fn dev_host(runtime: &WebRuntime, rust: bool) -> anyhow::Result<(String, Files)> {
    let read = |path: &Path| fs::read(path).with_context(|| format!("read {}", path.display()));
    let ui_sets = serde_json::to_string(&ui_sets())?;
    let js = read(&runtime.js)?;
    let wasm = read(&runtime.wasm)?;
    let glue = runtime.glue.as_deref().map(read).transpose()?;
    // `nsh.js` fetches the syntaxes next to itself when code is highlighted.
    let syntaxes = runtime.syntaxes.as_deref().map(read).transpose()?;
    // The snippets the glue imports, at the paths it imports them by.
    let snippets = match &runtime.glue {
        Some(path) => crate::bindgen::snippets(path)?
            .into_iter()
            .map(|(name, file)| Ok((name, read(&file)?)))
            .collect::<anyhow::Result<Vec<_>>>()?,
        None => Vec::new(),
    };
    // The browser host's modules, which `nsh.js` imports from `./browser/`.
    let browser = browser_host_files(runtime.js.parent().context("runtime has no parent")?)?;
    let mut parts = vec![digest(&js), digest(&wasm)];
    parts.extend(
        browser
            .iter()
            .map(|(name, bytes)| digest(&[name.as_bytes(), bytes].concat())),
    );
    parts.extend(glue.as_deref().map(digest));
    parts.extend(
        snippets
            .iter()
            .map(|(name, bytes)| digest(&[name.as_bytes(), bytes].concat())),
    );
    let version = digest(&parts.concat().into_bytes());
    let page = DEV_PAGE
        .replace(
            "{{protocol}}",
            &nsh_dev::protocol::PROTOCOL_VERSION.to_string(),
        )
        .replace("{{ui_sets}}", &ui_sets)
        .replace("{{runtime}}", &serde_json::to_string(&version)?)
        .replace("{{rust}}", if rust { "true" } else { "false" })
        .replace(
            "{{glue}}",
            if glue.is_some() {
                "\"./nsh-bindgen.js\""
            } else {
                "null"
            },
        );
    let mut files = vec![
        ("nsh.js".to_owned(), js),
        ("nsh.wasm".to_owned(), wasm),
        ("nsh.version".to_owned(), version.into_bytes()),
    ];
    files.extend(browser);
    if let Some(glue) = glue {
        files.push((crate::bindgen::GLUE.to_owned(), glue));
    }
    files.extend(snippets);
    if let Some(syntaxes) = syntaxes {
        files.push((runtime::SYNTAXES.to_owned(), syntaxes));
    }
    files.push((MATH_FONTS_FILE.to_owned(), nsh_base::MATH_FONTS.to_vec()));
    files.push((
        EMOJI_FILE.to_owned(),
        nsh_base::EMOJI_DATA.as_bytes().to_vec(),
    ));
    Ok((page, files))
}

/// The dev host's page and files ([`dev_host`]) for the app at `entry`:
/// with its page's host module (`nsh.json` `web.host`) as `nsh-host.js`,
/// read now (a change takes another session), or none, and its
/// `web.history`.
pub(crate) fn with_page_host(
    (page, mut files): (String, Files),
    entry: &Path,
) -> anyhow::Result<(String, Files)> {
    let web = crate::package::read_config(entry)?.web;
    let history = match web.history {
        Some(history) => history.to_string(),
        None => "null".to_owned(),
    };
    let module = web
        .host
        .map(|host| crate::package::web_host_module(entry, &host))
        .transpose()?;
    let host = match module {
        Some(path) => {
            let bytes = fs::read(&path)
                .with_context(|| format!("read nsh.json web.host {}", path.display()))?;
            files.retain(|(name, _)| name != PAGE_HOST);
            files.push((PAGE_HOST.to_owned(), bytes));
            format!("\"./{PAGE_HOST}\"")
        }
        None => "null".to_owned(),
    };
    Ok((
        page.replace("{{host}}", &host)
            .replace("{{history}}", &history),
        files,
    ))
}

/// Where the dev host serves the page's host module ([`with_page_host`]).
const PAGE_HOST: &str = "nsh-host.js";

/// The app's appearance (`nsh.json` `appearance`): the page's background
/// and theme color before the app draws.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Appearance {
    Light,
    Dark,
    System,
}

impl Appearance {
    fn of(config: &AppConfig) -> Appearance {
        match config.appearance.as_deref() {
            Some("light") => Appearance::Light,
            Some("dark") => Appearance::Dark,
            _ => Appearance::System,
        }
    }

    /// The background in light and dark mode.
    fn colors(self) -> (&'static str, &'static str) {
        match self {
            Appearance::Light => (LIGHT, LIGHT),
            Appearance::Dark => (DARK, DARK),
            Appearance::System => (LIGHT, DARK),
        }
    }
}

const LIGHT: &str = "#ffffff";
const DARK: &str = "#000000";

/// `--asset-url`: where a site's files are served from.
struct AssetUrl {
    /// What their URLs start with, ending in `/`.
    prefix: String,
    /// Its origin, which the page's `connect-src` allows.
    origin: String,
}

/// Checks `--asset-url` before anything is built ([`asset_url`]).
pub(crate) fn check_asset_url(url: &str) -> anyhow::Result<()> {
    asset_url(url).map(drop)
}

/// Reads `--asset-url`: an http(s) URL (`https://cdn.example.com/app/`)
/// without credentials, a query or a fragment. The path the page is served
/// under is `nsh.json`'s, not this.
fn asset_url(given: &str) -> anyhow::Result<AssetUrl> {
    let wrong = || {
        anyhow::anyhow!(
            "--asset-url is an http(s) URL (https://cdn.example.com/app/), not {given:?}"
        )
    };
    let url = url::Url::parse(given).map_err(|_| wrong())?;
    // An IPv6 host has no form in the CSP's `connect-src` it goes into.
    if !matches!(url.scheme(), "http" | "https")
        || !matches!(url.host(), Some(url::Host::Domain(_) | url::Host::Ipv4(_)))
        || !url.username().is_empty()
        || url.password().is_some()
        || url.query().is_some()
        || url.fragment().is_some()
    {
        return Err(wrong());
    }
    let prefix = url.as_str().to_owned();
    let prefix = if prefix.ends_with('/') {
        prefix
    } else {
        format!("{prefix}/")
    };
    Ok(AssetUrl {
        prefix,
        origin: url.origin().ascii_serialization(),
    })
}

/// A directory files are added to under content-addressed names.
struct Site {
    dir: PathBuf,
    /// Where the files are served from, ending in `/`: `--asset-url`, else
    /// nsh.json `web.base` (a path from the host's root), or `None`: next
    /// to the page.
    base: Option<String>,
}

impl Site {
    fn new(dir: &Path, base: Option<String>) -> anyhow::Result<Site> {
        fs::create_dir_all(dir).with_context(|| format!("create {}", dir.display()))?;
        Ok(Site {
            dir: dir.into(),
            base,
        })
    }

    /// Writes `bytes` as `path` with their digest in its name
    /// ([`hashed_name`]); returns the URL the page names it by
    /// ([`Site::url`]).
    fn add(&self, path: &str, bytes: &[u8]) -> anyhow::Result<String> {
        Ok(self.url(&self.put(path, bytes)?))
    }

    /// Writes `bytes` as `path` with their digest in its name
    /// ([`hashed_name`]); returns that name, a path of the site.
    fn put(&self, path: &str, bytes: &[u8]) -> anyhow::Result<String> {
        let name = hashed_name(path, bytes);
        self.write(&name, bytes)?;
        Ok(name)
    }

    /// The URL of the site's file `name`: under `base`, else relative to
    /// the page.
    fn url(&self, name: &str) -> String {
        format!(
            "{}{}",
            self.base.as_deref().unwrap_or_default(),
            url_path(name)
        )
    }

    /// A URL [`Site::add`] gave as a module specifier the page imports: a
    /// relative one starts with `./`.
    fn module(&self, url: &str) -> String {
        match self.base {
            Some(_) => url.to_owned(),
            None => format!("./{url}"),
        }
    }

    /// Writes `bytes` as `path`, a name that changes with them already (in
    /// a directory named by its content).
    fn write(&self, path: &str, bytes: &[u8]) -> anyhow::Result<()> {
        let file = self.dir.join(path);
        if let Some(parent) = file.parent() {
            fs::create_dir_all(parent)?;
        }
        fs::write(&file, bytes).with_context(|| format!("write {}", file.display()))
    }
}

/// `dir/name.ext` → `dir/name.<digest>.ext` (`name.<digest>` without an
/// extension), the digest being the first [`DIGEST_LEN`] hex digits of
/// the SHA-256 of `bytes`.
fn hashed_name(path: &str, bytes: &[u8]) -> String {
    with_digest(path, &digest(bytes))
}

/// `dir/name.ext` → `dir/name.<hex>.ext` (`name.<hex>` without an
/// extension).
fn with_digest(path: &str, hex: &str) -> String {
    let (dir, file) = match path.rsplit_once('/') {
        Some((dir, file)) => (format!("{dir}/"), file),
        None => (String::new(), path),
    };
    match file.rsplit_once('.') {
        Some((stem, ext)) if !stem.is_empty() => format!("{dir}{stem}.{hex}.{ext}"),
        _ => format!("{dir}{file}.{hex}"),
    }
}

/// `path` as a relative URL: what is not unreserved (or `/`) escaped.
fn url_path(path: &str) -> String {
    let mut out = String::with_capacity(path.len());
    for byte in path.bytes() {
        match byte {
            b'A'..=b'Z' | b'a'..=b'z' | b'0'..=b'9' | b'-' | b'.' | b'_' | b'~' | b'/' | b'@' => {
                out.push(byte as char)
            }
            _ => {
                let _ = write!(out, "%{byte:02X}");
            }
        }
    }
    out
}

/// When the page fetches each font (`fonts`, paths): those only chunks
/// register (`font("asset:/<path>")` in their code, as the bundler writes
/// font imports) come with the chunk that needs them, by chunk id; the
/// others (the entry's, the UI sets', which the engine registers as it
/// starts) before the app starts. Fonts are read synchronously, so each
/// must be there before the code registering it runs.
fn font_loads(
    fonts: &[String],
    entry: &str,
    chunks: &[crate::build::BuiltChunk],
) -> (Vec<String>, serde_json::Map<String, serde_json::Value>) {
    let mut eager = Vec::new();
    let mut by_chunk = serde_json::Map::new();
    for font in fonts {
        let uri = format!("asset:/{font}");
        let users: Vec<&crate::build::BuiltChunk> =
            chunks.iter().filter(|c| c.code.contains(&uri)).collect();
        if entry.contains(&uri) || users.is_empty() {
            eager.push(font.clone());
            continue;
        }
        for chunk in users {
            let list = by_chunk
                .entry(chunk.id.clone())
                .or_insert_with(|| serde_json::Value::Array(Vec::new()));
            if let serde_json::Value::Array(list) = list {
                list.push(font.clone().into());
            }
        }
    }
    (eager, by_chunk)
}

/// Fonts are read synchronously when the app starts or imports them: the
/// page fetches them first.
fn is_font(path: &str) -> bool {
    let ext = path.rsplit_once('.').map(|(_, e)| e.to_ascii_lowercase());
    matches!(
        ext.as_deref(),
        Some("ttf" | "otf" | "ttc" | "woff" | "woff2")
    )
}

/// The app's icon at the sizes browsers use, by URL.
#[derive(Default)]
struct Icons {
    /// The favicon (32 px).
    favicon: Option<String>,
    /// `apple-touch-icon` (180 px).
    touch: Option<String>,
    /// The manifest's (192 and 512 px).
    manifest: Vec<(u32, String)>,
}

impl Icons {
    fn write(site: &Site, icon: &Path) -> anyhow::Result<Icons> {
        let image = image::open(icon).with_context(|| format!("read {}", icon.display()))?;
        let png = |size: u32| -> anyhow::Result<String> {
            let scaled = image.resize_to_fill(size, size, image::imageops::FilterType::Lanczos3);
            let mut bytes = Vec::new();
            scaled
                .write_to(
                    &mut std::io::Cursor::new(&mut bytes),
                    image::ImageFormat::Png,
                )
                .context("encode the icon")?;
            site.add(&format!("icons/{size}.png"), &bytes)
        };
        Ok(Icons {
            favicon: Some(png(32)?),
            touch: Some(png(180)?),
            manifest: vec![(192, png(192)?), (512, png(512)?)],
        })
    }
}

/// `manifest.webmanifest`: what a browser installs the app as. It stays
/// beside the page under `--asset-url` too: its name has no digest, and
/// its `start_url` and `scope` are the page's.
fn manifest(name: &str, id: &str, appearance: Appearance, icons: &Icons) -> String {
    let (light, dark) = appearance.colors();
    let background = if appearance == Appearance::Dark {
        dark
    } else {
        light
    };
    let icons: Vec<_> = icons
        .manifest
        .iter()
        .map(|(size, url)| {
            serde_json::json!({ "src": url, "sizes": format!("{size}x{size}"), "type": "image/png" })
        })
        .collect();
    let manifest = serde_json::json!({
        "id": id,
        "name": name,
        "short_name": name,
        "start_url": "./",
        "scope": "./",
        "display": "standalone",
        "background_color": background,
        "theme_color": background,
        "icons": icons,
    });
    serde_json::to_string_pretty(&manifest).unwrap_or_default()
}

/// `nsh.json` `web.connect`, checked as `capabilities.network.hosts` are:
/// patterns of letters, digits, `.`, `-`, `:` and brackets (nothing that
/// could end a CSP source).
fn page_connect(hosts: &[String]) -> anyhow::Result<Vec<nsh_core::access::HostPattern>> {
    hosts
        .iter()
        .map(|host| {
            nsh_core::access::HostPattern::new(host)
                .map_err(|e| anyhow::anyhow!("nsh.json web.connect: {e}"))
        })
        .collect()
}

/// The page's `connect-src` when the app restricts the hosts it reaches
/// (`capabilities.network`): the browser then refuses every request, and
/// every redirect hop, to another host, which nsh cannot see (the browser
/// follows redirects before telling). `'self'` stays: the page loads the
/// runtime, the code and the fonts with `fetch`, and the app's relative
/// URLs reach its own origin (nsh still checks those against the
/// declaration). `page`: the hosts the page's host module reaches
/// (`web.connect`), allowed to the page alone: nsh checks the app's
/// requests against its declaration before the browser sees them. `None`:
/// no declaration, or one (or `page`) that allows any host.
fn connect_src(
    capabilities: Option<&nsh_core::access::Capabilities>,
    page: &[nsh_core::access::HostPattern],
) -> Option<String> {
    let network = &capabilities?.network;
    let mut sources = vec!["'self'".to_owned()];
    let mut add = |scheme: &str, host: &str, port: Option<u16>| {
        let host = if host.parse::<std::net::Ipv6Addr>().is_ok() {
            format!("[{host}]")
        } else {
            host.to_owned()
        };
        let authority = if let Some(port) = port {
            format!("{host}:{port}")
        } else if host.ends_with(']') || !host.contains(':') {
            format!("{host}:*")
        } else {
            host.to_owned()
        };
        let source = format!("{scheme}://{authority}");
        if !sources.contains(&source) {
            sources.push(source);
        }
    };
    for host in network.hosts.iter().chain(page) {
        let host = host.as_str();
        if host == "*" {
            return None;
        }
        for scheme in ["http", "https", "ws", "wss"] {
            add(scheme, host, None);
        }
    }
    for grant in &network.http {
        let get = grant.methods.is_empty() || grant.methods.iter().any(|method| method == "GET");
        for scheme in ["http", "https", "ws", "wss"] {
            if !grant.schemes.is_empty() && !grant.schemes.iter().any(|s| s == scheme) {
                continue;
            }
            if matches!(scheme, "http" | "https") {
                add(scheme, grant.host.as_str(), grant.port);
            }
            // NSH checks a WebSocket handshake as GET over http/https,
            // or against a legacy ws/wss grant. CSP needs explicit schemes.
            if get {
                let websocket = match scheme {
                    "http" | "ws" => "ws",
                    _ => "wss",
                };
                add(websocket, grant.host.as_str(), grant.port);
            }
        }
    }
    Some(sources.join(" "))
}

/// What [`page`] fills in.
struct PageParts<'a> {
    name: &'a str,
    appearance: Appearance,
    /// nsh.json `web.base` ([`page_base`]), which the page's own URLs
    /// (the manifest's) start with.
    base: Option<&'a str>,
    icons: &'a Icons,
    /// The module specifier of `nsh.js`.
    runtime: &'a str,
    wasm: &'a str,
    /// URL of the wasm-bindgen glue, if the runtime has one.
    glue: Option<&'a str>,
    code: &'a str,
    /// URLs of the fonts fetched before the app starts.
    fonts: &'a [String],
    /// What the page's script reads.
    app: &'a serde_json::Value,
    /// The page's `connect-src` ([`connect_src`]).
    connect: Option<&'a str>,
    /// URL of the page's styles ([`page_style`]).
    style: &'a str,
    /// URLs of the page's scripts ([`PAGE_PREFETCH`], [`PAGE_START`]).
    prefetch: &'a str,
    start: &'a str,
}

/// The app's `index.html`. It holds no inline code or styles and no
/// `<base href>`, which a strict `style-src`, `script-src` and `base-uri`
/// refuse: under `web.base` it names every file by an absolute URL, so it
/// loads at any of the app's paths, and the player takes the base from
/// the app's JSON (`base`).
fn page(parts: &PageParts<'_>) -> String {
    let (light, dark) = parts.appearance.colors();
    let mut head = String::new();
    if let Some(sources) = parts.connect {
        let _ = writeln!(
            head,
            "<meta http-equiv=\"Content-Security-Policy\" content=\"connect-src {sources}\">"
        );
    }
    match parts.appearance {
        Appearance::System => {
            let _ = writeln!(
                head,
                "<meta name=\"color-scheme\" content=\"light dark\">\n\
                 <meta name=\"theme-color\" content=\"{light}\" media=\"(prefers-color-scheme: light)\">\n\
                 <meta name=\"theme-color\" content=\"{dark}\" media=\"(prefers-color-scheme: dark)\">"
            );
        }
        Appearance::Light | Appearance::Dark => {
            let scheme = if parts.appearance == Appearance::Dark {
                "dark"
            } else {
                "light"
            };
            let _ = writeln!(
                head,
                "<meta name=\"color-scheme\" content=\"{scheme}\">\n\
                 <meta name=\"theme-color\" content=\"{light}\">"
            );
        }
    }
    let _ = writeln!(
        head,
        "<link rel=\"manifest\" href=\"{}manifest.webmanifest\">",
        parts.base.unwrap_or_default()
    );
    if let Some(favicon) = &parts.icons.favicon {
        let _ = writeln!(
            head,
            "<link rel=\"icon\" type=\"image/png\" sizes=\"32x32\" href=\"{favicon}\">"
        );
    }
    if let Some(touch) = &parts.icons.touch {
        let _ = writeln!(head, "<link rel=\"apple-touch-icon\" href=\"{touch}\">");
    }
    // The runtime imports the glue as it loads (`nsh.js`' `glue`).
    if let Some(glue) = parts.glue {
        let _ = writeln!(head, "<link rel=\"modulepreload\" href=\"{glue}\">");
    }
    // The runtime, imported by the page's script once it has run.
    let _ = writeln!(
        head,
        "<link rel=\"modulepreload\" href=\"{}\">",
        parts.runtime
    );
    // Requested while the page is parsed, before the runtime has loaded
    // ([`PAGE_PREFETCH`]).
    let urls: Vec<&str> = [parts.wasm, parts.code]
        .into_iter()
        .filter(|url| !url.is_empty())
        .chain(parts.fonts.iter().map(String::as_str))
        .collect();
    let urls = serde_json::json!(urls).to_string().replace("</", "<\\/");
    let _ = writeln!(
        head,
        "<script type=\"application/json\" id=\"nsh-fetch\">{urls}</script>\n<script src=\"{}\"></script>",
        parts.prefetch
    );
    // JSON in a script: `</script>` in a string must not end it.
    let app = parts.app.to_string().replace("</", "<\\/");
    PAGE.replace("{{title}}", &escape_html(parts.name))
        .replace("{{head}}", head.trim_end())
        .replace("{{style}}", parts.style)
        .replace("{{start}}", parts.start)
        .replace("{{app}}", &app)
}

/// The page's styles ([`PAGE_STYLE`]) with its colors until the app draws
/// (and on its error screen).
fn page_style(appearance: Appearance) -> String {
    let (light, dark) = appearance.colors();
    let colors = |background: &str| {
        let text = if background == DARK {
            "#f2f2f7"
        } else {
            "#1c1c1e"
        };
        format!("html, body {{ background: {background}; color: {text}; }}")
    };
    let colors = match appearance {
        Appearance::System => format!(
            "{}\n@media (prefers-color-scheme: dark) {{ {} }}",
            colors(light),
            colors(dark)
        ),
        Appearance::Light | Appearance::Dark => colors(light),
    };
    PAGE_STYLE.replace("{{colors}}", &colors)
}

fn escape_html(text: &str) -> String {
    text.replace('&', "&amp;")
        .replace('<', "&lt;")
        .replace('>', "&gt;")
        .replace('"', "&quot;")
}

fn dir_size(dir: &Path) -> usize {
    let Ok(entries) = fs::read_dir(dir) else {
        return 0;
    };
    entries
        .flatten()
        .map(|entry| match entry.file_type() {
            Ok(t) if t.is_dir() => dir_size(&entry.path()),
            _ if compress::is_copy(&entry.path()) => 0,
            _ => entry.metadata().map_or(0, |m| m.len() as usize),
        })
        .sum()
}

/// The file name (without extension) of a side module holding the code of
/// `gates`: what it is for, in kebab case, without the gates' kind
/// (`nsh_part_rich_editor` -> `nsh-rich-editor`); a module several gates
/// share names them with `~` (`nsh-editor~text-view`, unreserved in URLs), the first three and
/// how many more. An app's UI set keeps `app-`, so it never meets NSH's.
fn side_file_name(gates: &[String]) -> String {
    fn short(gate: &str) -> String {
        let name = ["nsh_part_", "nsh_std_", "nsh_feature_"]
            .iter()
            .find_map(|p| gate.strip_prefix(p))
            .map(str::to_owned)
            .or_else(|| gate.strip_prefix("nsh_ui_set_").map(|s| format!("app_{s}")))
            .unwrap_or_else(|| gate.strip_prefix("nsh_").unwrap_or(gate).to_owned());
        name.replace('_', "-")
    }
    let mut parts: Vec<String> = gates.iter().take(3).map(|g| short(g)).collect();
    if gates.len() > 3 {
        parts.push(format!("{}-more", gates.len() - 3));
    }
    format!("nsh-{}", parts.join("~"))
}

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

    #[test]
    fn names_carry_the_digest_of_the_content() {
        let a = hashed_name("assets/img/logo@2x.png", b"one");
        assert!(a.starts_with("assets/img/logo@2x."), "{a}");
        assert!(a.ends_with(".png"), "{a}");
        assert_eq!(a.len(), "assets/img/logo@2x..png".len() + DIGEST_LEN);
        assert_eq!(a, hashed_name("assets/img/logo@2x.png", b"one"));
        assert_ne!(a, hashed_name("assets/img/logo@2x.png", b"two"));
        assert_eq!(
            hashed_name("LICENSE", b"x").len(),
            "LICENSE.".len() + DIGEST_LEN
        );
        assert!(hashed_name(".env", b"x").starts_with(".env."));
    }

    #[test]
    fn the_published_bootstrap_is_a_strict_classic_script() {
        let dir = std::env::temp_dir().join(format!("nsh-web-bootstrap-{}", std::process::id()));
        let root = dir.join("host");
        fs::create_dir_all(root.join("browser")).unwrap();
        // Export a detached-call probe without letting module-mode compression
        // fold away the call that observes strictness in the published script.
        let source = "\"use strict\"; globalThis.bootstrapThis = function () { return this; };";
        fs::write(root.join("browser/bootstrap.js"), source).unwrap();
        let site = Site::new(&dir.join("site"), None).unwrap();
        let published = publish_browser_host(&site, &root).unwrap();
        let code = fs::read_to_string(site.dir.join(published).join("bootstrap.js")).unwrap();
        let host = nsh_js::Host::new().unwrap();
        host.eval_script("bootstrap.js", &code).unwrap();
        assert!(
            host.eval_script("check.js", "(0, bootstrapThis)() === undefined")
                .unwrap()
        );
        fs::remove_dir_all(&dir).unwrap();
    }

    #[test]
    fn the_dev_page_runs_the_apps_page_host() {
        let dir = std::env::temp_dir().join(format!("nsh-web-dev-host-{}", std::process::id()));
        fs::create_dir_all(&dir).unwrap();
        let entry = dir.join("App.tsx");
        let base = || {
            (
                "const HOST = {{host}}; const HISTORY = {{history}};".to_owned(),
                Files::new(),
            )
        };
        // No nsh.json, or one without `web.host`: no host, and the
        // player's own default for the history.
        let (page, files) = with_page_host(base(), &entry).unwrap();
        assert_eq!(page, "const HOST = null; const HISTORY = null;");
        assert!(files.is_empty());
        fs::write(dir.join("host.js"), "export function createHost() {}").unwrap();
        fs::write(
            dir.join("nsh.json"),
            r#"{ "web": { "host": "host.js", "history": false } }"#,
        )
        .unwrap();
        let (page, files) = with_page_host(base(), &entry).unwrap();
        assert_eq!(
            page,
            "const HOST = \"./nsh-host.js\"; const HISTORY = false;"
        );
        assert_eq!(
            files,
            [(
                "nsh-host.js".to_owned(),
                b"export function createHost() {}".to_vec()
            )]
        );
        // Outside the app's directory: refused, as `nsh package` does.
        fs::write(
            dir.join("nsh.json"),
            r#"{ "web": { "host": "../host.js" } }"#,
        )
        .unwrap();
        assert!(with_page_host(base(), &entry).is_err());
        fs::remove_dir_all(&dir).unwrap();
    }

    fn uses(imports: &[(&str, Option<&[&str]>)]) -> nsh_bundler::NativeUse {
        let mut uses = nsh_bundler::NativeUse::default();
        for (module, names) in imports {
            let imported = match names {
                Some(names) => {
                    nsh_bundler::Imported::Names(names.iter().map(|n| n.to_string()).collect())
                }
                None => nsh_bundler::Imported::All,
            };
            uses.imports.insert(module.to_string(), imported);
        }
        uses
    }

    #[test]
    fn native_exports_are_the_names_imported_unless_a_specifier_is_computed() {
        let mut app = uses(&[("nsh", Some(&["View"])), ("nsh/base", None)]);
        assert_eq!(
            native_exports(Some(&app)),
            serde_json::json!({ "nsh": ["View"] })
        );
        app.computed_import = true;
        assert_eq!(native_exports(Some(&app)), serde_json::json!({}));
    }

    fn chunk(id: &str, code: &str) -> crate::build::BuiltChunk {
        crate::build::BuiltChunk {
            id: id.into(),
            code: code.into(),
            bytecode: Vec::new(),
            source_map: None,
            native_use: None,
        }
    }

    /// The sources of the static imports of the module at `path`, a file
    /// of `dir`, resolved as the browser resolves them: paths of `dir`.
    fn linked_imports(dir: &Path, path: &str) -> Vec<String> {
        let code = fs::read_to_string(dir.join(path)).unwrap();
        let mut sources = Vec::new();
        nsh_bundler::rewrite_imports(path, &code, |source| {
            sources.push(source.to_owned());
            None
        })
        .unwrap();
        sources
            .iter()
            .map(|source| {
                join_path(path, source).unwrap_or_else(|| panic!("{path}: bare {source}"))
            })
            .collect()
    }

    /// A strict `script-src` runs an app whose native imports are all by
    /// name: its modules import one another and the facades of the native
    /// modules, files too, by their URLs, so the realm needs no import map
    /// (inline) and no `blob:` URL. One importing a module whole leaves
    /// the map to the runtime.
    #[test]
    fn modules_link_to_their_files_when_every_native_import_is_by_name() {
        let dir = std::env::temp_dir().join(format!("nsh-web-imports-{}", std::process::id()));
        let _ = fs::remove_dir_all(&dir);
        let site = Site::new(&dir, None).unwrap();
        let entry = "import{View as a}from\"nsh\";import{s}from\"./shared.js\";\
                     export const e=1;const L=()=>__nsh_import(`Later.js`,[`shared.js`]);";
        let chunks = |later: &str| {
            vec![
                chunk(
                    "shared.js",
                    "import{e}from\"./bundle.js\";export const s=e;",
                ),
                chunk("Later.js", later),
                chunk(
                    "screens/Deep.js",
                    "import{s}from\"../shared.js\";import{View}from\"nsh\";export{s,View};",
                ),
                chunk("leaf.js", "export const l=1;"),
            ]
        };
        let later = "import{s}from\"./shared.js\";import x from\"nsh\";export default [s,x];";
        let mut app = uses(&[
            ("nsh", Some(&["View", "default"])),
            ("nsh/effects", Some(&[])),
        ]);
        let published = publish_modules(&site, entry, &chunks(later), Some(&app)).unwrap();
        assert!(published.linked);
        let code = published.code.clone();
        assert!(code.starts_with("app.") && code.ends_with(".js"), "{code}");
        let url = |id: &str| published.chunks[id].as_str().unwrap().to_owned();
        // Every module imports files of the site, at the paths they have.
        let files = [
            &code,
            &url("shared.js"),
            &url("Later.js"),
            &url("screens/Deep.js"),
        ];
        for file in files {
            for import in linked_imports(&dir, file) {
                assert!(dir.join(&import).is_file(), "{file}: {import}");
            }
        }
        assert_eq!(
            linked_imports(&dir, &code),
            [
                linked_imports(&dir, &url("Later.js"))[1].clone(),
                url("shared.js")
            ]
        );
        assert_eq!(
            linked_imports(&dir, &url("shared.js")),
            std::slice::from_ref(&code)
        );
        let deep = linked_imports(&dir, &url("screens/Deep.js"));
        assert_eq!(deep[0], url("shared.js"));
        assert!(deep[1].starts_with("facades/nsh."), "{deep:?}");
        let source = fs::read_to_string(dir.join(url("screens/Deep.js"))).unwrap();
        assert!(source.contains("from\"../shared."), "{source}");
        let facade = fs::read_to_string(dir.join(&deep[1])).unwrap();
        assert!(
            facade.contains("let e0=host.get(\"nsh\")[\"View\"];export{e0 as \"View\"};"),
            "{facade}"
        );
        assert!(facade.contains("export{e1 as \"default\"}"), "{facade}");
        assert!(facade.contains(".watch(()=>{e0="), "{facade}");
        // Lazy chunks are still loaded by id.
        let source = fs::read_to_string(dir.join(&code)).unwrap();
        assert!(
            source.contains("__nsh_import(`Later.js`,[`shared.js`])"),
            "{source}"
        );
        // The same app, the same names; a module changed renames only the
        // modules that reach it (`Later.js` is loaded, not imported).
        let again = publish_modules(&site, entry, &chunks(later), Some(&app)).unwrap();
        assert_eq!(
            (&again.code, &again.chunks),
            (&published.code, &published.chunks)
        );
        let changed =
            publish_modules(&site, entry, &chunks("export default 2;"), Some(&app)).unwrap();
        assert_eq!(changed.code, published.code);
        assert_eq!(changed.chunks["shared.js"], published.chunks["shared.js"]);
        assert_ne!(changed.chunks["Later.js"], published.chunks["Later.js"]);
        // Files of another origin (`--asset-url`) are named by their URLs
        // and import one another relatively.
        let cdn = Site::new(&dir, Some("https://cdn.example.com/a/".into())).unwrap();
        let on_cdn = publish_modules(&cdn, entry, &chunks(later), Some(&app)).unwrap();
        assert_eq!(on_cdn.code, format!("https://cdn.example.com/a/{code}"));
        // A module imported whole: the runtime makes the map.
        app.imports
            .insert("nsh/base".into(), nsh_bundler::Imported::All);
        let whole = publish_modules(&site, entry, &chunks(later), Some(&app)).unwrap();
        assert!(!whole.linked);
        assert_eq!(
            fs::read_to_string(dir.join(&whole.code)).unwrap(),
            entry,
            "kept as bundled"
        );
        app.imports.remove("nsh/base");
        app.computed_import = true;
        assert!(
            !publish_modules(&site, entry, &chunks(later), Some(&app))
                .unwrap()
                .linked
        );
        let _ = fs::remove_dir_all(&dir);
    }

    #[test]
    fn relative_paths_are_what_modules_import() {
        assert_eq!(join_path("a.js", "./b.js").as_deref(), Some("b.js"));
        assert_eq!(join_path("x/a.js", "../b.js").as_deref(), Some("b.js"));
        assert_eq!(join_path("x/a.js", "./y/b.js").as_deref(), Some("x/y/b.js"));
        assert_eq!(join_path("a.js", "../b.js"), None);
        assert_eq!(join_path("a.js", "nsh"), None);
        assert_eq!(relative_path("a.1.js", "b.2.js"), "./b.2.js");
        assert_eq!(relative_path("x/a.1.js", "b.2.js"), "../b.2.js");
        assert_eq!(
            relative_path("a.1.js", "facades/nsh.2.js"),
            "./facades/nsh.2.js"
        );
        assert_eq!(relative_path("x/y/a.js", "x/z/b.js"), "../z/b.js");
        for (from, to) in [
            ("a.js", "x/y/b.js"),
            ("x/y/a.js", "b.js"),
            ("x/a.js", "x/b.js"),
        ] {
            assert_eq!(
                join_path(from, &relative_path(from, to)).as_deref(),
                Some(to)
            );
        }
    }

    /// A namespace read by name (`base.EmojiPicker`, so that shells without
    /// the export still run the app) declares the names read, which the
    /// browser host's facade exports, and links their parts only.
    #[test]
    fn a_namespace_read_by_name_declares_and_links_what_it_reads() {
        let natives = |s: &str| s == "nsh" || s.starts_with("nsh/");
        let mut read = nsh_bundler::NativeUse::default();
        read.add(
            r#"import * as base from "nsh/base"; import { View } from "nsh";
            const EmojiPicker = base.EmojiPicker;
            export const Picker = (p) => (EmojiPicker ? _$c(EmojiPicker, p) : _$c(View, {}));"#,
            natives,
        );
        assert_eq!(
            native_exports(Some(&read)),
            serde_json::json!({ "nsh": ["View"], "nsh/base": ["EmojiPicker"] })
        );
        assert_eq!(whole_because(&read), None);
        assert_eq!(
            linked_gates_of(Some(&read), &[]),
            ["nsh_part_core", EMOJI_GATE]
        );
        // Passed on, it may be read for anything: every export, every gate.
        let mut passed = nsh_bundler::NativeUse::default();
        passed.add(
            r#"import * as base from "nsh/base"; base.EmojiPicker; use(base);"#,
            natives,
        );
        assert_eq!(native_exports(Some(&passed)), serde_json::json!({}));
        assert!(whole_because(&passed).is_some());
        let gates = linked_gates_of(Some(&passed), &[]);
        assert!(gates.contains(&"nsh_part_editor".to_owned()));
        assert!(gates.contains(&"nsh_feature_shader".to_owned()));
    }

    #[test]
    fn an_app_set_gate_is_named_by_its_specifier() {
        assert_eq!(ui_set_gate("app/charts"), "nsh_ui_set_app_charts");
        assert_eq!(ui_set_gate("@acme/ui-kit"), "nsh_ui_set__acme_ui_kit");
        assert!(is_gate(&ui_set_gate("app/charts")));
        // The runtime's own setters are not gates.
        assert!(!is_gate("nsh_set_base_url"));
        assert!(!is_gate("nsh_set_dark"));
    }

    #[test]
    fn a_linked_runtime_keeps_the_app_sets_the_app_imports() {
        let sets = ["app/charts".to_owned(), "app/storage".to_owned()];
        let gates = linked_gates_of(
            Some(&uses(&[
                ("app/charts", Some(&["Chart"])),
                ("nsh", Some(&["View"])),
            ])),
            &sets,
        );
        assert!(gates.contains(&"nsh_ui_set_app_charts".to_owned()));
        assert!(!gates.contains(&"nsh_ui_set_app_storage".to_owned()));
        // Not known: every set.
        let all = linked_gates_of(None, &sets);
        assert!(all.contains(&"nsh_ui_set_app_charts".to_owned()));
        assert!(all.contains(&"nsh_ui_set_app_storage".to_owned()));
        let mut computed = nsh_bundler::NativeUse::default();
        computed.computed_import = true;
        assert!(
            linked_gates_of(Some(&computed), &sets).contains(&"nsh_ui_set_app_storage".to_owned())
        );
    }

    #[test]
    fn a_linked_runtime_keeps_the_parts_and_modules_the_app_uses() {
        let gates = |imports: &[(&str, Option<&[&str]>)]| {
            let mut gates = linked_gates(Some(&uses(imports)));
            gates.sort();
            gates
        };
        let core: &[&str] = &["Button", "Switch", "Tabs", "Tab", "NavStack", "AlertDialog"];
        // Only `nsh`: no core part, no standard modules.
        assert!(gates(&[("nsh", Some(&["mount", "View"]))]).is_empty());
        assert_eq!(
            gates(&[
                ("nsh", Some(&["mount"])),
                ("nsh/base", Some(core)),
                ("buffer", None),
                ("crypto", Some(&["createHash"])),
                ("url", Some(&["domainToASCII"])),
            ]),
            ["nsh_part_core", "nsh_std_crypto", "nsh_std_url"]
        );
        // A scroll bar theme of a view's own is drawn by the core part.
        let mut themed = uses(&[("nsh", Some(&["ScrollView"]))]);
        themed.scrollbar_theme = true;
        assert_eq!(linked_gates(Some(&themed)), ["nsh_part_core"]);
        // The globals of `crypto` need its operations.
        let mut global = uses(&[("nsh", Some(&["View"]))]);
        global.global_crypto = true;
        assert_eq!(linked_gates(Some(&global)), ["nsh_std_crypto"]);
        assert_eq!(
            gates(&[("nsh/base", Some(&["Button", "Editor"]))]),
            ["nsh_part_core", "nsh_part_editor"]
        );
        // Menus, popups, pickers, the command palette and toasts are
        // parts apart from the core.
        assert_eq!(
            gates(&[(
                "nsh/base",
                Some(&["Button", "Select", "Menu", "Tooltip", "Toast", "toast"])
            )]),
            [
                "nsh_part_core",
                "nsh_part_menu",
                "nsh_part_overlays",
                "nsh_part_select",
                "nsh_part_toast"
            ]
        );
        assert_eq!(
            gates(&[("nsh/base", Some(&["Combobox", "Command"]))]),
            ["nsh_part_combobox", "nsh_part_command", "nsh_part_core"]
        );
        assert_eq!(
            gates(&[
                ("nsh/base", Some(&["TextView"])),
                ("zlib", Some(&["gzipSync"]))
            ]),
            ["nsh_part_core", "nsh_part_text_view", "nsh_std_zlib"]
        );
        assert_eq!(
            gates(&[("nsh/base/plot", Some(&["Plot"]))]),
            ["nsh_part_core", "nsh_part_plot"]
        );
        assert_eq!(
            gates(&[("nsh", Some(&["mount", "Shader"]))]),
            ["nsh_feature_shader"]
        );
        let every = nsh_base::parts::ALL.len() + 4;
        assert_eq!(gates(&[("nsh/base", None)]).len(), every);
        let mut computed = uses(&[]);
        computed.computed_import = true;
        assert_eq!(linked_gates(Some(&computed)).len(), every);
        assert_eq!(linked_gates(None).len(), every);
    }

    #[test]
    fn a_runtime_wasm_bindgen_ran_on_is_published_whole() {
        // `run`, a function `() -> ()` that returns at once.
        let mut module = b"\0asm\x01\0\0\0".to_vec();
        module.extend([1, 4, 1, 0x60, 0, 0]);
        module.extend([3, 2, 1, 0]);
        module.extend([7, 7, 1, 3, b'r', b'u', b'n', 0, 0]);
        module.extend([10, 4, 1, 2, 0, 0x0b]);
        // The linker's sections of the module wasm-bindgen read, which it
        // keeps: their symbol names function 7, which it removed.
        let mut runtime = module.clone();
        runtime.extend([0, 19, 7]);
        runtime.extend(b"linking");
        runtime.extend([2, 8, 8, 1, 0, 0, 7, 3, b'r', b'u', b'n']);
        runtime.extend([0, 13, 10]);
        runtime.extend(b"reloc.CODE");
        runtime.extend([3, 0]);
        let error = link(&runtime, false, &["run"], &[]).err().unwrap();
        assert_eq!(
            error.to_string(),
            "a symbol names function 7, which the web runtime does not have"
        );
        let published = link(&runtime, true, &["run"], &[]).unwrap();
        assert_eq!(published.main, module);
        assert!(published.sides.is_empty());
    }

    #[test]
    fn a_plan_that_keeps_every_gate_says_why_and_where() {
        let chunk = |id: &str, uses: nsh_bundler::NativeUse| crate::build::BuiltChunk {
            id: id.to_owned(),
            code: String::new(),
            bytecode: Vec::new(),
            source_map: None,
            native_use: Some(uses),
        };
        let mut computed = uses(&[("nsh", Some(&["View"]))]);
        computed.computed_import = true;
        let mut built = crate::build::rust_root();
        built.rust_root = false;
        built.entry_native_use = Some(uses(&[("nsh", Some(&["View"]))]));
        built.entry_imports = vec!["shared.js".into()];
        built.chunks = vec![
            chunk("shared.js", computed.clone()),
            chunk("Later.js", uses(&[("nsh/base", None)])),
        ];
        let whole = GatePlan::of(&built).whole.unwrap();
        assert_eq!(whole.code, "its chunk shared.js");
        assert!(whole.why.contains("import(name)"));
        assert!(whole.to_string().contains("Import NSH's modules by name"));

        // The entry's own reason comes first; a chunk loaded later doesn't
        // make the engine whole (its gates are side modules).
        built.entry_native_use = Some(uses(&[("nsh/base", None)]));
        let whole = GatePlan::of(&built).whole.unwrap();
        assert_eq!(whole.code, "the app's entry");
        assert!(whole.why.contains("nsh/base whole"));
        built.entry_native_use = Some(uses(&[("nsh", Some(&["View"]))]));
        built.chunks[0] = chunk("shared.js", uses(&[]));
        assert_eq!(GatePlan::of(&built).whole, None);

        // Not read at all (a development or Rust app's build): nothing to say.
        built.entry_native_use = None;
        built.native_use = None;
        assert_eq!(GatePlan::of(&built).whole, None);
    }

    #[test]
    fn chunks_load_the_gates_the_entry_lacks_as_side_modules() {
        let chunk = |id: &str, uses: Option<nsh_bundler::NativeUse>| crate::build::BuiltChunk {
            id: id.to_owned(),
            code: String::new(),
            bytecode: Vec::new(),
            source_map: None,
            native_use: uses,
        };
        let mut built = crate::build::rust_root();
        built.rust_root = false;
        built.entry_native_use = Some(uses(&[("nsh/base", Some(&["Button"]))]));
        built.entry_imports = vec!["shared.js".into()];
        built.chunks = vec![
            chunk("shared.js", Some(uses(&[("zlib", Some(&["gzipSync"]))]))),
            chunk(
                "Editor.js",
                Some(uses(&[("nsh/base", Some(&["Editor", "Button"]))])),
            ),
            chunk(
                "Docs.js",
                Some(uses(&[
                    ("nsh/base", Some(&["Editor", "TextView"])),
                    ("zlib", None),
                ])),
            ),
            chunk("Plain.js", Some(uses(&[("nsh", Some(&["View"]))]))),
        ];
        let mut all = uses(&[]);
        for c in &built.chunks {
            all.merge(c.native_use.as_ref().unwrap());
        }
        built.native_use = Some(all);
        let plan = GatePlan::of(&built);
        assert_eq!(plan.main, ["nsh_part_core", "nsh_std_zlib"]);
        assert_eq!(
            plan.chunks,
            BTreeMap::from([
                ("Editor.js".to_owned(), vec!["nsh_part_editor".to_owned()]),
                (
                    "Docs.js".to_owned(),
                    vec![
                        "nsh_part_editor".to_owned(),
                        "nsh_part_text_view".to_owned()
                    ]
                ),
            ])
        );
        assert_eq!(
            plan.lazy(),
            ["nsh_feature_math", "nsh_part_editor", "nsh_part_text_view"]
        );
        // The page's map: only gates with a published side module.
        let published = BTreeSet::from(["nsh_part_editor".to_owned()]);
        assert_eq!(
            serde_json::Value::Object(plan.chunk_sides(&published)),
            serde_json::json!({
                "Docs.js": ["nsh_part_editor"],
                "Editor.js": ["nsh_part_editor"],
            })
        );

        assert_eq!(plan.whole, None);

        // A chunk whose use is unknown: everything is the entry's.
        built.chunks.push(chunk("Unknown.js", None));
        let plan = GatePlan::of(&built);
        assert!(plan.chunks.is_empty());
        assert_eq!(plan.main, linked_gates(built.native_use.as_ref()));
        built.entry_native_use = None;
        assert!(GatePlan::of(&built).chunks.is_empty());
    }

    #[test]
    fn side_modules_are_fetched_only_by_the_load_that_needs_them() {
        let side = |name: &str, gate: Option<&str>, needs: &[&str]| prune::split::Side {
            name: name.to_owned(),
            gate: gate.map(str::to_owned),
            gates: vec![gate.unwrap_or("nsh_part_fields").to_owned()],
            needs: needs.iter().map(|n| (*n).to_owned()).collect(),
            bytes: Vec::new(),
        };
        // A gate's module keeps its gate and the modules it needs, which
        // `loadSide` loads first.
        assert_eq!(
            side_entry(
                &side(
                    "nsh_part_fields",
                    Some("nsh_part_fields"),
                    &["nsh_shared_1"]
                ),
                "nsh-fields.0123456789.wasm"
            ),
            serde_json::json!({
                "url": "nsh-fields.0123456789.wasm",
                "gate": "nsh_part_fields",
                "needs": ["nsh_shared_1"],
                "onDemand": true,
            })
        );
        // A shared one is only ever loaded as what a gate's module needs.
        assert_eq!(
            side_entry(
                &side("nsh_shared_1", None, &[]),
                "nsh-shared.0123456789.wasm"
            ),
            serde_json::json!({
                "url": "nsh-shared.0123456789.wasm",
                "needs": [],
                "onDemand": true,
            })
        );
        assert_eq!(
            side_entry(&side("nsh_feature_math", Some(MATH_GATE), &[]), "m.wasm")["onDemand"],
            true
        );
    }

    #[test]
    fn the_dev_host_serves_the_syntaxes_next_to_nsh_js() {
        let dir = std::env::temp_dir().join(format!("nsh-web-syntaxes-{}", std::process::id()));
        fs::create_dir_all(&dir).unwrap();
        let syntaxes = dir.join(runtime::SYNTAXES);
        fs::write(&syntaxes, "dump").unwrap();
        let runtime = WebRuntime {
            wasm: dir.join("nsh-dev.wasm"),
            js: dir.join("nsh.js"),
            glue: None,
            syntaxes: Some(syntaxes),
        };
        fs::write(&runtime.js, "js").unwrap();
        fs::write(&runtime.wasm, "one").unwrap();
        let (_, files) = dev_host(&runtime, false).unwrap();
        let (_, bytes) = files
            .iter()
            .find(|(p, _)| p == "nsh-syntaxes.bin")
            .expect("the syntaxes are served");
        assert_eq!(bytes, b"dump");
        let _ = fs::remove_dir_all(&dir);
    }

    /// The syntaxes copied from the workspace's two-face are a dump that
    /// the web runtime's syntect reads (`nsh-base`'s `syntax.rs`).
    #[test]
    fn the_syntaxes_file_is_a_dump_syntect_reads() {
        let dir = std::env::temp_dir().join(format!("nsh-web-dump-{}", std::process::id()));
        fs::create_dir_all(&dir).unwrap();
        let manifest = Path::new(env!("CARGO_MANIFEST_DIR")).join("../../Cargo.toml");
        let path = runtime::write_syntaxes(&manifest, &dir).unwrap();
        let set: syntect::parsing::SyntaxSet =
            syntect::dumps::from_uncompressed_data(&fs::read(path).unwrap()).unwrap();
        for language in ["rs", "tsx", "json"] {
            assert!(set.find_syntax_by_token(language).is_some(), "{language}");
        }
        let _ = fs::remove_dir_all(&dir);
    }

    #[test]
    fn the_dev_page_knows_its_runtime() {
        let dir = std::env::temp_dir().join(format!("nsh-web-dev-{}", std::process::id()));
        fs::create_dir_all(&dir).unwrap();
        let runtime = WebRuntime {
            wasm: dir.join("nsh-dev.wasm"),
            js: dir.join("nsh.js"),
            glue: None,
            syntaxes: None,
        };
        fs::write(&runtime.js, "js").unwrap();
        fs::write(&runtime.wasm, "one").unwrap();
        let (page, files) = dev_host(&runtime, false).unwrap();
        let version = |files: &Files| {
            let (_, bytes) = files.iter().find(|(p, _)| p == "nsh.version").unwrap();
            String::from_utf8(bytes.clone()).unwrap()
        };
        let first = version(&files);
        // A page reloads when the server's runtime is not the one it has.
        assert!(
            page.contains(&format!("const RUNTIME = \"{first}\";")),
            "{page}"
        );
        // The page's host is the app's (`with_page_host`).
        assert!(
            !page
                .replace("{{host}}", "")
                .replace("{{history}}", "")
                .contains("{{"),
            "{page}"
        );
        // `nsh/base` with its submodules, each a module the page serves.
        assert!(
            page.contains("const UI_SETS = [\"nsh/base\",\"nsh/base/plot\"];"),
            "{page}"
        );
        fs::write(&runtime.wasm, "two").unwrap();
        assert_ne!(version(&dev_host(&runtime, false).unwrap().1), first);
        assert!(
            dev_host(&runtime, true)
                .unwrap()
                .0
                .contains("const RUST = true;")
        );
        let _ = fs::remove_dir_all(&dir);
    }

    #[test]
    fn wasm_bindgen_glue_is_served_and_loaded_by_the_dev_page() {
        let dir = std::env::temp_dir().join(format!("nsh-web-glue-{}", std::process::id()));
        fs::create_dir_all(&dir).unwrap();
        let mut runtime = WebRuntime {
            wasm: dir.join("nsh-dev.wasm"),
            js: dir.join("nsh.js"),
            glue: None,
            syntaxes: None,
        };
        fs::write(&runtime.js, "js").unwrap();
        fs::write(&runtime.wasm, "one").unwrap();
        let (plain, plain_files) = dev_host(&runtime, false).unwrap();
        assert!(plain.contains("const GLUE = null;"), "{plain}");
        let names = |files: &Files| files.iter().map(|(p, _)| p.clone()).collect::<Vec<_>>();
        assert_eq!(
            names(&plain_files),
            [
                "nsh.js",
                "nsh.wasm",
                "nsh.version",
                MATH_FONTS_FILE,
                EMOJI_FILE
            ]
        );
        let glue = dir.join("nsh-dev-bindgen.js");
        fs::write(&glue, "export {};").unwrap();
        let snippet = crate::bindgen::output_dir(&glue).join("snippets/app-1/inline0.js");
        fs::create_dir_all(snippet.parent().unwrap()).unwrap();
        fs::write(&snippet, "export const a = 1;").unwrap();
        runtime.glue = Some(glue.clone());
        let (page, files) = dev_host(&runtime, false).unwrap();
        assert!(
            page.contains("const GLUE = \"./nsh-bindgen.js\";"),
            "{page}"
        );
        assert!(page.contains("glue: GLUE ?? undefined"), "{page}");
        // The glue's snippets at the paths it imports them by.
        assert_eq!(
            names(&files),
            [
                "nsh.js",
                "nsh.wasm",
                "nsh.version",
                "nsh-bindgen.js",
                "snippets/app-1/inline0.js",
                MATH_FONTS_FILE,
                EMOJI_FILE
            ]
        );
        // Another glue is another runtime: an open page loads again.
        let version = |files: &Files| {
            files
                .iter()
                .find(|(p, _)| p == "nsh.version")
                .unwrap()
                .1
                .clone()
        };
        fs::write(&glue, "export { a };").unwrap();
        assert_ne!(
            version(&files),
            version(&dev_host(&runtime, false).unwrap().1)
        );
        assert_ne!(version(&files), version(&plain_files));
        // So is another snippet.
        let before = version(&dev_host(&runtime, false).unwrap().1);
        fs::write(&snippet, "export const a = 2;").unwrap();
        assert_ne!(before, version(&dev_host(&runtime, false).unwrap().1));
        let _ = fs::remove_dir_all(&dir);
    }

    #[test]
    fn wasm_bindgen_snippets_are_published_under_one_directory_named_by_them() {
        let dir = std::env::temp_dir().join(format!("nsh-web-snippets-{}", std::process::id()));
        let _ = fs::remove_dir_all(&dir);
        let glue = dir.join("runtime/nsh-bindgen.js");
        let snippets = crate::bindgen::output_dir(&glue).join("snippets/app-1");
        fs::create_dir_all(snippets.join("js")).unwrap();
        fs::write(
            &glue,
            "import { twice } from './snippets/app-1/inline0.js';\n\
             export function __nsh_imports() { return { twice }; }\n",
        )
        .unwrap();
        // A snippet that imports another one, as in development.
        fs::write(
            snippets.join("inline0.js"),
            "import { shout } from './js/shout.js';\n\
             export function twice(s) { return shout(s + s); }\n",
        )
        .unwrap();
        fs::write(
            snippets.join("js/shout.js"),
            "export function shout(s) { return s.toUpperCase(); }\n",
        )
        .unwrap();
        let publish = |name: &str| {
            let site = Site::new(&dir.join(name), None).unwrap();
            let url = publish_glue(&site, &glue).unwrap();
            let published = fs::read_to_string(site.dir.join(&url)).unwrap();
            let snippets = fs::read_dir(&site.dir)
                .unwrap()
                .map(|e| e.unwrap().file_name().to_string_lossy().into_owned())
                .filter(|name| name.starts_with("snippets."))
                .collect::<Vec<_>>();
            (site.dir.clone(), url, published, snippets)
        };
        let (site, url, published, found) = publish("one");
        assert!(
            url.starts_with("nsh-bindgen.") && url.ends_with(".js"),
            "{url}"
        );
        let [snippets] = found.as_slice() else {
            panic!("one directory of snippets: {found:?}");
        };
        assert_eq!(snippets.len(), "snippets.".len() + DIGEST_LEN);
        assert!(
            published.contains(&format!("./{snippets}/app-1/inline0.js")),
            "{published}"
        );
        // The snippets keep their paths and their imports of one another.
        let inline = fs::read_to_string(site.join(snippets).join("app-1/inline0.js")).unwrap();
        assert!(inline.contains("from './js/shout.js'"), "{inline}");
        assert!(site.join(snippets).join("app-1/js/shout.js").exists());
        // Another snippet is another directory, and another glue.
        fs::write(
            crate::bindgen::output_dir(&glue).join("snippets/app-1/js/shout.js"),
            "export function shout(s) { return s.toLowerCase(); }\n",
        )
        .unwrap();
        let (_, other_url, _, other) = publish("two");
        assert_ne!(other, found);
        assert_ne!(other_url, url);
        // Without snippets, only the glue.
        fs::remove_dir_all(crate::bindgen::output_dir(&glue)).unwrap();
        let (_, _, plain, none) = publish("three");
        assert!(none.is_empty(), "{none:?}");
        assert!(plain.contains("./snippets/app-1/inline0.js"), "{plain}");
        let _ = fs::remove_dir_all(&dir);
    }

    #[test]
    fn wasm_bindgen_glue_is_preloaded_by_the_package_page() {
        let icons = Icons::default();
        let app = serde_json::Value::Null;
        let parts = |glue| PageParts {
            name: "App",
            appearance: Appearance::System,
            base: None,
            icons: &icons,
            runtime: "./nsh.0123456789.js",
            wasm: "nsh.0123456789.wasm",
            glue,
            code: "app.1.js",
            fonts: &[],
            app: &app,
            connect: None,
            style: "nsh-page.1.css",
            prefetch: "nsh-prefetch.1.js",
            start: "nsh-start.1.js",
        };
        let with = page(&parts(Some("nsh-bindgen.0123456789.js")));
        assert!(
            with.contains("<link rel=\"modulepreload\" href=\"nsh-bindgen.0123456789.js\">"),
            "{with}"
        );
        assert!(PAGE_START.contains("glue: app.glue"));
        // Without wasm-bindgen the page names no glue.
        let without = page(&parts(None));
        assert!(
            !without.contains("modulepreload\" href=\"nsh-bindgen"),
            "{without}"
        );
        assert!(!without.contains("bindgen"), "{without}");
    }

    #[test]
    fn an_asset_url_is_an_http_url() {
        let read = |url: &str| asset_url(url).map(|a| (a.prefix, a.origin));
        assert_eq!(
            read("https://cdn.example.com/app/").unwrap(),
            (
                "https://cdn.example.com/app/".into(),
                "https://cdn.example.com".into()
            )
        );
        assert_eq!(
            read("http://127.0.0.1:8791").unwrap(),
            (
                "http://127.0.0.1:8791/".into(),
                "http://127.0.0.1:8791".into()
            )
        );
        assert_eq!(
            read("https://cdn.example.com/my app/").unwrap().0,
            "https://cdn.example.com/my%20app/"
        );
        for wrong in [
            "/app/",
            "app/",
            "//cdn.example.com/",
            "ftp://cdn.example.com/",
            "https://user:pw@cdn.example.com/",
            "https://cdn.example.com/?v=1",
            "https://cdn.example.com/#x",
            "http://[::1]:8794/",
        ] {
            assert!(read(wrong).is_err(), "{wrong}");
        }
    }

    #[test]
    fn a_site_under_an_asset_url_names_its_files_there() {
        let dir = std::env::temp_dir().join(format!("nsh-asset-url-{}", std::process::id()));
        let relative = Site::new(&dir.join("relative"), None).unwrap();
        let url = relative.add("app.js", b"x").unwrap();
        assert!(!url.contains('/') && url.starts_with("app."), "{url}");
        assert_eq!(relative.module(&url), format!("./{url}"));
        let based = Site::new(
            &dir.join("based"),
            Some("https://cdn.example.com/app/".into()),
        )
        .unwrap();
        let url = based.add("assets/a b.png", b"x").unwrap();
        assert!(
            url.starts_with("https://cdn.example.com/app/assets/a%20b."),
            "{url}"
        );
        assert_eq!(based.module(&url), url);
        // The file itself is written in the site's directory, as without one.
        assert!(
            based
                .dir
                .join("assets")
                .read_dir()
                .unwrap()
                .next()
                .is_some()
        );
        let _ = fs::remove_dir_all(&dir);
    }

    #[test]
    fn urls_escape_what_names_may_hold() {
        assert_eq!(url_path("assets/a b/中.png"), "assets/a%20b/%E4%B8%AD.png");
        assert_eq!(url_path("assets/@nsh-base/x.ttf"), "assets/@nsh-base/x.ttf");
    }

    #[test]
    fn the_base_is_a_path_from_the_root() {
        assert_eq!(page_base(None).unwrap(), None);
        assert_eq!(page_base(Some("/")).unwrap().as_deref(), Some("/"));
        assert_eq!(
            page_base(Some("/apps/counter")).unwrap().as_deref(),
            Some("/apps/counter/")
        );
        assert_eq!(
            page_base(Some("/apps/counter/")).unwrap().as_deref(),
            Some("/apps/counter/")
        );
        // What the page, `--run` and the server take is what a browser
        // resolves the base to.
        let host = url::Url::parse("http://127.0.0.1:4173/sports/football").unwrap();
        for base in [
            "/",
            "/apps/counter/",
            "/%E5%BA%94%E7%94%A8/counter/",
            "/v1.2/a-b_c~d/",
            "/apps/a%2fb/",
        ] {
            let page = page_base(Some(base)).unwrap().unwrap();
            assert_eq!(host.join(&page).unwrap().path(), page, "{base}");
        }
        for base in [
            "apps/",
            "//",
            "//cdn.example.com/",
            "/apps//counter/",
            "/apps/counter//",
            "https://example.com/",
            "/a/../b/",
            "/a/./b/",
            "/apps/%2e/counter/",
            "/apps/%2E%2E/counter/",
            "/apps/.%2e/counter/",
            "/应用/counter/",
            "/a%zz/",
            "/a%e/",
            "/a b/",
            "/a?x/",
            "/a#x/",
            "/a\\b/",
            "/a\"><script>/",
        ] {
            assert!(page_base(Some(base)).is_err(), "{base}");
        }
        let html = page(&PageParts {
            name: "A",
            appearance: Appearance::Light,
            base: Some("/apps/counter/"),
            icons: &Icons::default(),
            runtime: "./nsh.0123456789.js",
            wasm: "nsh.0123456789.wasm",
            glue: None,
            code: "app.1.js",
            fonts: &[],
            app: &serde_json::json!({}),
            connect: None,
            style: "nsh-page.1.css",
            prefetch: "nsh-prefetch.1.js",
            start: "nsh-start.1.js",
        });
        // No `<base href>` (a strict `base-uri` refuses it): the page's own
        // URLs start with the base.
        assert!(!html.contains("<base"), "{html}");
        assert!(
            html.contains("<link rel=\"manifest\" href=\"/apps/counter/manifest.webmanifest\">")
        );
    }

    #[test]
    fn the_page_names_the_files_and_cannot_be_broken_out_of() {
        let app = serde_json::json!({ "code": "app.1.js", "x": "</script><b>" });
        let html = page(&PageParts {
            name: "A <b> & \"c\"",
            appearance: Appearance::System,
            base: None,
            icons: &Icons::default(),
            runtime: "./nsh.0123456789.js",
            wasm: "nsh.0123456789.wasm",
            glue: None,
            code: "app.1.js",
            fonts: &["assets/f.1.ttf".to_owned()],
            app: &app,
            connect: Some("'self' api.example.com:*"),
            style: "nsh-page.1.css",
            prefetch: "nsh-prefetch.1.js",
            start: "nsh-start.1.js",
        });
        assert!(html.contains("<title>A &lt;b&gt; &amp; &quot;c&quot;</title>"));
        assert!(html.contains("<link rel=\"modulepreload\" href=\"./nsh.0123456789.js\">"));
        let fetches = html
            .lines()
            .find(|line| line.starts_with("<script type=\"application/json\" id=\"nsh-fetch\">"))
            .unwrap_or_default();
        for url in ["nsh.0123456789.wasm", "app.1.js", "assets/f.1.ttf"] {
            assert!(fetches.contains(&format!("\"{url}\"")), "{fetches}");
        }
        // No inline code: a strict `script-src` ('self') runs the page.
        for script in html.split("<script").skip(1) {
            let tag = &script[..script.find('>').unwrap()];
            let body = &script[tag.len() + 1..script.find("</script>").unwrap()];
            assert!(
                tag.contains(" src=\"") || tag.contains("type=\"application/json\""),
                "{tag}"
            );
            assert!(!tag.contains(" src=\"") || body.is_empty(), "{tag}");
        }
        assert!(html.contains("<script src=\"nsh-prefetch.1.js\"></script>"));
        assert!(html.contains("<script type=\"module\" src=\"nsh-start.1.js\"></script>"));
        // No inline styles either: a strict `style-src` ('self') styles the
        // page, whose own rules are a file.
        assert!(!html.contains("<style"), "{html}");
        assert!(!html.contains("<base"), "{html}");
        assert!(!html.contains(" style=\""), "{html}");
        assert!(html.contains("<link rel=\"stylesheet\" href=\"nsh-page.1.css\">"));
        assert!(PAGE_START.contains("wasm: take(app.wasm)"));
        assert!(!html.contains("rel=\"preload\""));
        assert!(html.contains("media=\"(prefers-color-scheme: dark)\""));
        assert!(!html.contains("</script><b>"));
        assert!(!html.contains("{{"), "{html}");
        assert!(html.contains(
            "<meta http-equiv=\"Content-Security-Policy\" content=\"connect-src 'self' api.example.com:*\">"
        ));
    }

    #[test]
    fn the_page_styles_follow_the_appearance() {
        let system = page_style(Appearance::System);
        assert!(!system.contains("{{"), "{system}");
        assert!(
            system.contains("@media (prefers-color-scheme: dark)"),
            "{system}"
        );
        assert!(system.contains("#status"), "{system}");
        let light = page_style(Appearance::Light);
        assert!(!light.contains("@media (prefers-color-scheme"), "{light}");
        assert!(light.contains(&format!("background: {}", Appearance::Light.colors().0)));
    }

    #[test]
    fn requests_are_limited_to_the_declared_hosts() {
        let caps =
            |json: serde_json::Value| nsh_core::access::Capabilities::from_json(&json).unwrap();
        assert_eq!(connect_src(None, &[]), None);
        assert_eq!(
            connect_src(Some(&caps(serde_json::json!({ "clipboard": true }))), &[]).as_deref(),
            Some("'self'")
        );
        let declared = caps(serde_json::json!({ "network": {
            "hosts": ["api.example.com", "*.cdn.example.com", "localhost:8080"],
            "http": [{ "host": "api.example.com", "methods": ["GET"] }],
        } }));
        assert_eq!(
            connect_src(Some(&declared), &[]).as_deref(),
            Some(
                "'self' http://api.example.com:* https://api.example.com:* ws://api.example.com:* wss://api.example.com:* http://*.cdn.example.com:* https://*.cdn.example.com:* ws://*.cdn.example.com:* wss://*.cdn.example.com:* http://localhost:8080 https://localhost:8080 ws://localhost:8080 wss://localhost:8080"
            )
        );
        let constrained = caps(serde_json::json!({ "network": {"http": [
            { "host": "secure.example.com", "methods": ["GET"], "schemes": ["https"], "port": 8443 },
            { "host": "upload.example.com", "methods": ["POST"], "schemes": ["https"], "port": 9443 },
            { "host": "legacy.example.com", "methods": ["GET"], "schemes": ["ws"], "port": 9000 }
        ]} }));
        assert_eq!(
            connect_src(Some(&constrained), &[]).as_deref(),
            Some(
                "'self' https://secure.example.com:8443 wss://secure.example.com:8443 https://upload.example.com:9443 ws://legacy.example.com:9000"
            )
        );
        let wildcard = caps(serde_json::json!({ "network": {"http": [
            { "host": "*", "methods": ["GET"], "schemes": ["https"], "port": 443 }
        ]} }));
        assert_eq!(
            connect_src(Some(&wildcard), &[]).as_deref(),
            Some("'self' https://*:443 wss://*:443")
        );
        let ipv6 = caps(serde_json::json!({ "network": {"http": [
            { "host": "::1", "methods": ["GET"], "schemes": ["http"], "port": 9877 },
            { "host": "[::2]", "methods": ["GET"], "schemes": ["https"] }
        ]} }));
        assert_eq!(
            connect_src(Some(&ipv6), &[]).as_deref(),
            Some("'self' http://[::1]:9877 ws://[::1]:9877 https://[::2]:* wss://[::2]:*")
        );
        let any = caps(serde_json::json!({ "network": { "hosts": ["*"] } }));
        assert_eq!(connect_src(Some(&any), &[]), None);
        // The page's host module's hosts (`web.connect`) join the app's; any
        // host there lifts the restriction too; none without a declaration.
        let page = page_connect(&["auth.example.com".into()]).unwrap();
        assert_eq!(
            connect_src(Some(&caps(serde_json::json!({}))), &page).as_deref(),
            Some(
                "'self' http://auth.example.com:* https://auth.example.com:* ws://auth.example.com:* wss://auth.example.com:*"
            )
        );
        assert_eq!(connect_src(None, &page), None);
        let anywhere = page_connect(&["*".into()]).unwrap();
        assert_eq!(
            connect_src(Some(&caps(serde_json::json!({}))), &anywhere),
            None
        );
        // Nothing that could end a CSP source.
        for bad in ["a.com; script-src *", "'unsafe-inline'", "a b", ""] {
            assert!(page_connect(&[bad.into()]).is_err(), "{bad}");
        }
    }

    #[test]
    fn fonts_only_chunks_register_come_with_them() {
        let chunk = |id: &str, code: &str| crate::build::BuiltChunk {
            id: id.to_owned(),
            code: code.to_owned(),
            bytecode: Vec::new(),
            source_map: None,
            native_use: None,
        };
        let fonts = ["a.ttf", "b.ttf", "@ui/c.ttf"].map(String::from);
        let chunks = [
            chunk(
                "Text.js",
                "font(`asset:/b.ttf`, `B`); font(`asset:/a.ttf`, `A`)",
            ),
            chunk("Other.js", "font(`asset:/b.ttf`, `B`)"),
        ];
        let (eager, by_chunk) = font_loads(&fonts, "font(`asset:/a.ttf`, `A`)", &chunks);
        // The entry's, and the UI set's (which no code names), first.
        assert_eq!(eager, ["a.ttf", "@ui/c.ttf"]);
        assert_eq!(
            serde_json::Value::Object(by_chunk),
            serde_json::json!({ "Text.js": ["b.ttf"], "Other.js": ["b.ttf"] })
        );
    }

    #[test]
    fn manifests_follow_the_appearance() {
        let icons = Icons {
            manifest: vec![(192, "icons/192.a.png".to_owned())],
            ..Icons::default()
        };
        let json: serde_json::Value = serde_json::from_str(&manifest(
            "Demo",
            "com.example.demo",
            Appearance::Dark,
            &icons,
        ))
        .unwrap();
        assert_eq!(json["background_color"], "#000000");
        assert_eq!(json["display"], "standalone");
        assert_eq!(json["icons"][0]["sizes"], "192x192");
    }

    /// The `Accept` header of a browser loading a page.
    const BROWSER: &str = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";

    /// The strings after each `prefix` in `text`, up to the quote that
    /// opened them (the prefix's).
    fn quoted_after(text: &str, prefix: &str) -> Vec<String> {
        let quote = prefix
            .chars()
            .find(|c| matches!(c, '"' | '\''))
            .expect("a prefix with a quote");
        text.split(prefix)
            .skip(1)
            .filter_map(|rest| rest.split_once(quote).map(|(s, _)| s.to_owned()))
            .collect()
    }

    /// The relative URLs of the modules a module imports (`from "./x.js"`,
    /// `import("./x.js")`) and of the scripts it loads by its own URL
    /// (`new URL("./realm.js", import.meta.url)`), which the browser
    /// resolves against the module's URL; bare specifiers go through the
    /// import map.
    fn module_imports(js: &str) -> Vec<String> {
        let mut specifiers = Vec::new();
        for prefix in [
            "from \"",
            "from '",
            "import(\"",
            "import('",
            "new URL(\"",
            "new URL('",
        ] {
            specifiers.extend(quoted_after(js, prefix));
        }
        specifiers.retain(|s| {
            (s.starts_with("./") || s.starts_with("../") || s.starts_with('/')) && !s.contains("${")
        });
        specifiers
    }

    /// The URLs a page names for the browser to load: its tags' `href` and
    /// `src` (but `<base>`'s), and in its scripts the modules they import,
    /// the relative URLs they hold (`"./nsh.wasm"`), the files the head
    /// requests (`fetches`) and the packaged app's files (`const app`).
    fn page_references(html: &str) -> Vec<String> {
        let mut urls = Vec::new();
        for tag in html.split('<').skip(1) {
            let tag = &tag[..tag.find('>').unwrap_or(tag.len())];
            if ["link ", "script ", "img "]
                .iter()
                .any(|t| tag.starts_with(t))
            {
                urls.extend(quoted_after(tag, " href=\""));
                urls.extend(quoted_after(tag, " src=\""));
            }
        }
        for script in html.split("<script").skip(1) {
            let tag = &script[..script.find('>').unwrap()];
            let body = &script[tag.len() + 1..script.find("</script>").unwrap()];
            // The files the head requests (`nsh-fetch`) and the packaged
            // app's, read by their keys (`nsh-app`): its chunk ids
            // (`"./chunk.js"`) are not URLs.
            if tag.contains("id=\"nsh-fetch\"") {
                urls.extend(serde_json::from_str::<Vec<String>>(body).unwrap());
                continue;
            }
            let app = tag
                .contains("id=\"nsh-app\"")
                .then(|| serde_json::from_str::<serde_json::Value>(body).unwrap());
            if app.is_none() {
                urls.extend(module_imports(body));
                urls.extend(
                    quoted_after(body, "\"./")
                        .into_iter()
                        .filter(|s| !s.ends_with('/'))
                        .map(|s| format!("./{s}")),
                );
            }
            if let Some(app) = app {
                let strings = |value: &serde_json::Value| value.as_str().map(str::to_owned);
                for key in ["runtime", "wasm", "glue", "code", "host"] {
                    urls.extend(strings(&app[key]).filter(|url| !url.is_empty()));
                }
                for key in ["assets", "data", "chunks"] {
                    urls.extend(
                        app[key]
                            .as_object()
                            .into_iter()
                            .flatten()
                            .filter_map(|(_, url)| strings(url)),
                    );
                }
                urls.extend(
                    app["sides"]
                        .as_object()
                        .into_iter()
                        .flatten()
                        .filter_map(|(_, side)| strings(&side["url"])),
                );
            }
        }
        urls
    }

    /// What a browser fetches to start the page at `url`: the page, asked
    /// for as a browser asks for one, every file it names
    /// ([`page_references`]), resolved as the browser resolves them
    /// (against its `<base href>`, else its own URL), and every module
    /// those import, against the module's URL. Each URL with its status.
    fn load_page(url: &str) -> Vec<(String, u16)> {
        let agent: ureq::Agent = ureq::Agent::config_builder()
            .http_status_as_error(false)
            .max_redirects(0)
            .build()
            .into();
        let get = |url: &url::Url, accept: &str| {
            let mut response = agent
                .get(url.as_str())
                .header("Accept", accept)
                .call()
                .unwrap();
            let body = response
                .body_mut()
                .with_config()
                .limit(64 << 20)
                .read_to_vec()
                .unwrap();
            (
                response.status().as_u16(),
                String::from_utf8_lossy(&body).into_owned(),
            )
        };
        let url = url::Url::parse(url).unwrap();
        let (status, html) = get(&url, BROWSER);
        assert_eq!(status, 200, "{url}");
        assert!(html.starts_with("<!doctype html>"), "{url}: {html}");
        let base = match quoted_after(&html, "<base href=\"").first() {
            Some(href) => url.join(href).unwrap(),
            None => url.clone(),
        };
        let mut queue: std::collections::VecDeque<url::Url> = page_references(&html)
            .iter()
            .map(|reference| base.join(reference).unwrap())
            .collect();
        let mut seen = BTreeSet::new();
        let mut loaded = Vec::new();
        while let Some(url) = queue.pop_front() {
            if !seen.insert(url.to_string()) {
                continue;
            }
            let (status, body) = get(&url, "*/*");
            if status == 200 && url.path().ends_with(".js") {
                queue.extend(module_imports(&body).iter().map(|s| url.join(s).unwrap()));
            }
            loaded.push((url.to_string(), status));
        }
        loaded
    }

    #[test]
    fn the_dev_page_loads_its_files_at_the_apps_paths() {
        let dir = std::env::temp_dir().join(format!("nsh-web-dev-paths-{}", std::process::id()));
        let _ = fs::remove_dir_all(&dir);
        fs::create_dir_all(&dir).unwrap();
        // The runtime's own modules, with its browser host's, and a glue
        // importing a snippet.
        let web = Path::new(env!("CARGO_MANIFEST_DIR")).join("../nsh-platform-web/web");
        let glue = dir.join("nsh-dev-bindgen.js");
        fs::write(
            &glue,
            "import { a } from './snippets/app-1/inline0.js';\nexport { a };",
        )
        .unwrap();
        let snippet = crate::bindgen::output_dir(&glue).join("snippets/app-1/inline0.js");
        fs::create_dir_all(snippet.parent().unwrap()).unwrap();
        fs::write(&snippet, "export const a = 1;").unwrap();
        let runtime = WebRuntime {
            wasm: dir.join("nsh-dev.wasm"),
            js: web.join("nsh.js"),
            glue: Some(glue),
            syntaxes: None,
        };
        fs::write(&runtime.wasm, "wasm").unwrap();
        // The app's page host (`web.host`).
        fs::write(dir.join("host.js"), "export function createHost() {}").unwrap();
        fs::write(dir.join("nsh.json"), r#"{ "web": { "host": "host.js" } }"#).unwrap();
        let (page, files) =
            with_page_host(dev_host(&runtime, false).unwrap(), &dir.join("App.tsx")).unwrap();
        let server =
            nsh_dev::DevServer::start("127.0.0.1:0".parse().unwrap(), "", None, |_| {}).unwrap();
        server.serve_web(page, files);
        let origin = format!("http://{}", server.local_addr());
        for path in [
            "/",
            "/football",
            "/sports/football",
            "/sports/football/?tab=live",
        ] {
            let loaded = load_page(&format!("{origin}{path}"));
            let missing: Vec<_> = loaded.iter().filter(|(_, status)| *status != 200).collect();
            assert!(missing.is_empty(), "{path}: {missing:?}");
            for file in [
                "/nsh.js",
                "/nsh.wasm",
                "/nsh.version",
                "/nsh-bindgen.js",
                "/snippets/app-1/inline0.js",
                "/nsh-host.js",
                "/browser/runtime.js",
                "/browser/bootstrap.js",
                "/browser/realm.js",
            ] {
                let file = format!("{origin}{file}");
                assert!(
                    loaded.iter().any(|(url, _)| *url == file),
                    "{path}: {file} is not among {loaded:?}"
                );
            }
        }
        drop(server);
        let _ = fs::remove_dir_all(&dir);
    }

    #[test]
    fn a_packaged_page_loads_its_files_at_the_apps_paths() {
        let dir = std::env::temp_dir().join(format!("nsh-web-site-paths-{}", std::process::id()));
        // A site as `package` writes it: the page and the files it names,
        // the runtime importing its browser host, the app importing a chunk
        // and a native module's facade.
        let files = [
            (
                "nsh.0123456789.js",
                "import(\"./browser.0123456789/runtime.js\");",
            ),
            (
                "browser.0123456789/runtime.js",
                "import { a } from \"./dom.js\";",
            ),
            ("browser.0123456789/dom.js", "export const a = 1;"),
            ("nsh.1111111111.wasm", "wasm"),
            ("nsh-bindgen.2222222222.js", "export {};"),
            (
                "app.3333333333.js",
                "import { View } from \"./facades/nsh.cccccccccc.js\"; import(\"./chunk-a.aaaaaaaaaa.js\");",
            ),
            ("chunk-a.aaaaaaaaaa.js", "export {};"),
            ("facades/nsh.cccccccccc.js", "export const View = 1;"),
            ("host.7777777777.js", "export function createHost() {}"),
            ("assets/fonts/Inter.4444444444.ttf", "ttf"),
            ("assets/img/logo.8888888888.png", "png"),
            ("nsh-syntaxes.9999999999.bin", "syntaxes"),
            ("nsh-side-plot.bbbbbbbbbb.wasm", "wasm"),
            ("icons/32.5555555555.png", "png"),
            ("icons/180.6666666666.png", "png"),
            ("manifest.webmanifest", "{}"),
            ("nsh-page.1.css", ""),
            ("nsh-prefetch.1.js", ""),
            ("nsh-start.1.js", ""),
        ];
        // The page `package` writes for `web.base` (its files under it, as
        // `Site::url` names them), or without one.
        let write = |base: Option<&str>| {
            let _ = fs::remove_dir_all(&dir);
            for (path, content) in files {
                let path = dir.join(path);
                fs::create_dir_all(path.parent().unwrap()).unwrap();
                fs::write(path, content).unwrap();
            }
            let at = |path: &str| format!("{}{path}", base.unwrap_or_default());
            let runtime = match base {
                Some(_) => at("nsh.0123456789.js"),
                None => "./nsh.0123456789.js".to_owned(),
            };
            let app = serde_json::json!({
                "runtime": runtime,
                "wasm": at("nsh.1111111111.wasm"),
                "glue": at("nsh-bindgen.2222222222.js"),
                "code": at("app.3333333333.js"),
                "host": at("host.7777777777.js"),
                "assets": {
                    "fonts/Inter.ttf": at("assets/fonts/Inter.4444444444.ttf"),
                    "img/logo.png": at("assets/img/logo.8888888888.png"),
                },
                "data": { "syntaxes": at("nsh-syntaxes.9999999999.bin") },
                "fonts": ["fonts/Inter.ttf"],
                "chunkFonts": {},
                "chunks": { "chunk-a.js": at("chunk-a.aaaaaaaaaa.js") },
                "sides": { "plot": { "url": at("nsh-side-plot.bbbbbbbbbb.wasm"), "needs": [] } },
                "linked": true,
            });
            let icons = Icons {
                favicon: Some(at("icons/32.5555555555.png")),
                touch: Some(at("icons/180.6666666666.png")),
                manifest: Vec::new(),
            };
            let page = page(&PageParts {
                name: "Sports",
                appearance: Appearance::System,
                base,
                icons: &icons,
                runtime: &runtime,
                wasm: &at("nsh.1111111111.wasm"),
                glue: Some(&at("nsh-bindgen.2222222222.js")),
                code: &at("app.3333333333.js"),
                fonts: &[at("assets/fonts/Inter.4444444444.ttf")],
                app: &app,
                connect: None,
                style: &at("nsh-page.1.css"),
                prefetch: &at("nsh-prefetch.1.js"),
                start: &at("nsh-start.1.js"),
            });
            fs::write(dir.join("index.html"), page).unwrap();
        };
        // What the page at `path` loads: every file of the site, at `base`.
        let loads_the_site = |origin: &str, base: &str, path: &str| {
            let loaded = load_page(&format!("{origin}{path}"));
            let mut expected: Vec<_> = files
                .iter()
                .map(|(file, _)| format!("{origin}{base}{file}"))
                .collect();
            expected.sort();
            let mut urls: Vec<_> = loaded.iter().map(|(url, _)| url.clone()).collect();
            urls.sort();
            assert_eq!(urls, expected, "{path}");
            let missing: Vec<_> = loaded.iter().filter(|(_, status)| *status != 200).collect();
            assert!(missing.is_empty(), "{path}: {missing:?}");
        };
        // `web.base`, at the host's root and under a path, one of them
        // percent-encoded (`/应用/counter/`), as `package` takes it: the
        // page names its files by absolute URLs, with no `<base href>` for
        // a strict `base-uri` to refuse, and loads them at every path.
        for base in ["/", "/apps/counter", "/%E5%BA%94%E7%94%A8/counter/"] {
            let base: &str = &page_base(Some(base)).unwrap().unwrap();
            write(Some(base));
            let server =
                nsh_dev::SiteServer::start_at("127.0.0.1:0".parse().unwrap(), dir.clone(), base)
                    .unwrap();
            let origin = format!("http://{}", server.local_addr());
            for path in [
                "",
                "football",
                "sports/football",
                "sports/football/?tab=live",
            ] {
                loads_the_site(&origin, base, &format!("{base}{path}"));
            }
        }
        // Without one, the page's files are relative to it: it loads where
        // it is and one segment below, and at deeper paths it misses them.
        write(None);
        let server =
            nsh_dev::SiteServer::start("127.0.0.1:0".parse().unwrap(), dir.clone()).unwrap();
        let origin = format!("http://{}", server.local_addr());
        loads_the_site(&origin, "/", "/");
        loads_the_site(&origin, "/", "/football");
        let nested = load_page(&format!("{origin}/sports/football"));
        assert!(
            nested
                .iter()
                .any(|(url, status)| url.ends_with("/sports/nsh.0123456789.js") && *status == 404),
            "{nested:?}"
        );
        drop(server);
        let _ = fs::remove_dir_all(&dir);
    }
}