decdn-cache 0.1.0

Cache engine wrapping iroh-blobs with origin pull-through for deCDN
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
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
use super::*;
use crate::range_pull::encode_verified_range;
use std::future::Future;
use std::pin::Pin;
use std::sync::atomic::{AtomicU64, AtomicUsize, Ordering};

use crate::origin::{Origin, OriginFetch, OriginKind};
use std::assert_matches;

/// A trivial in-memory origin for tests. Stores exactly one blob.
#[derive(Debug)]
struct StubOrigin {
    data: Bytes,
    hash: Hash,
}

impl StubOrigin {
    fn new(payload: &[u8]) -> Self {
        Self {
            hash: Hash::new(payload),
            data: Bytes::from(payload.to_vec()),
        }
    }
}

impl Origin for StubOrigin {
    fn kind(&self) -> OriginKind {
        // Stand in for an HTTP origin in tests so callers reasoning
        // about preview-side `origin_kinds` behaviour see a
        // non-empty `Vec<OriginKind>` entry (the field is
        // a vec, not an `Option`). The choice is arbitrary —
        // `Origin::kind` is a tag, not a behavioural switch.
        OriginKind::Http
    }

    fn fetch(
        &self,
        hash: Hash,
        _max_bytes: u64,
    ) -> Pin<Box<dyn Future<Output = Result<OriginFetch, crate::OriginPullError>> + Send + '_>>
    {
        let result = if hash == self.hash {
            Ok(OriginFetch::found_one_shot(self.data.clone()))
        } else {
            Ok(OriginFetch::NotFound)
        };
        Box::pin(async move { result })
    }
}

/// A stub origin that also answers [`Origin::size`] and
/// [`Origin::fetch_outboard`], for outboard-fetch tests. The
/// outboard answer is configurable so a test can exercise the
/// `NotFound`/`Unsupported` degrade.
#[derive(Debug)]
struct OutboardStubOrigin {
    data: Bytes,
    hash: Hash,
    outboard: Option<Bytes>,
}

impl OutboardStubOrigin {
    fn new(payload: &[u8], outboard: Option<Bytes>) -> Self {
        Self {
            hash: Hash::new(payload),
            data: Bytes::from(payload.to_vec()),
            outboard,
        }
    }
}

impl Origin for OutboardStubOrigin {
    fn kind(&self) -> OriginKind {
        OriginKind::Http
    }

    fn fetch(
        &self,
        hash: Hash,
        _max_bytes: u64,
    ) -> Pin<Box<dyn Future<Output = Result<OriginFetch, crate::OriginPullError>> + Send + '_>>
    {
        let result = if hash == self.hash {
            Ok(OriginFetch::found_one_shot(self.data.clone()))
        } else {
            Ok(OriginFetch::NotFound)
        };
        Box::pin(async move { result })
    }

    fn size(
        &self,
        hash: Hash,
    ) -> Pin<Box<dyn Future<Output = Result<Option<u64>, crate::OriginPullError>> + Send + '_>>
    {
        let matches = hash == self.hash;
        let len = u64::try_from(self.data.len()).unwrap_or(u64::MAX);
        Box::pin(async move { Ok(matches.then_some(len)) })
    }

    fn fetch_outboard(
        &self,
        hash: Hash,
        _outboard_max_bytes: u64,
    ) -> Pin<Box<dyn Future<Output = Result<OutboardFetch, crate::OriginPullError>> + Send + '_>>
    {
        let result = if hash == self.hash {
            match &self.outboard {
                Some(ob) => OutboardFetch::Found(ob.clone()),
                None => OutboardFetch::NotFound,
            }
        } else {
            OutboardFetch::NotFound
        };
        Box::pin(async move { Ok(result) })
    }
}

/// Serves several blobs from one origin so a single engine can hold
/// multiple distinct hashes (a second `CacheEngine::open` on the same
/// dir would deadlock on iroh-blobs' single-writer file lock).
#[derive(Debug)]
struct MultiStubOrigin {
    blobs: std::collections::HashMap<Hash, Bytes>,
}

impl MultiStubOrigin {
    fn new(payloads: &[&[u8]]) -> Self {
        let blobs = payloads
            .iter()
            .map(|p| (Hash::new(p), Bytes::from(p.to_vec())))
            .collect();
        Self { blobs }
    }
}

impl Origin for MultiStubOrigin {
    fn kind(&self) -> OriginKind {
        OriginKind::Http
    }

    fn fetch(
        &self,
        hash: Hash,
        _max_bytes: u64,
    ) -> Pin<Box<dyn Future<Output = Result<OriginFetch, crate::OriginPullError>> + Send + '_>>
    {
        let result = self
            .blobs
            .get(&hash)
            .map_or(Ok(OriginFetch::NotFound), |b| {
                Ok(OriginFetch::found_one_shot(b.clone()))
            });
        Box::pin(async move { result })
    }
}

/// Origin that answers `size()` (a `HEAD`/`HeadObject` stand-in) for one
/// hash and COUNTS the calls, so a test can prove `origin_probe_size`
/// memoises rather than re-probing the backend on every probe. An optional
/// delay exercises the per-probe timeout.
#[derive(Debug)]
struct CountingSizeOrigin {
    hash: Hash,
    size: u64,
    calls: Arc<AtomicUsize>,
    delay: Option<Duration>,
}

impl CountingSizeOrigin {
    fn new(hash: Hash, size: u64) -> (Self, Arc<AtomicUsize>) {
        let calls = Arc::new(AtomicUsize::new(0));
        let origin = Self {
            hash,
            size,
            calls: Arc::clone(&calls),
            delay: None,
        };
        (origin, calls)
    }

    fn slow(hash: Hash, size: u64, delay: Duration) -> (Self, Arc<AtomicUsize>) {
        let (mut origin, calls) = Self::new(hash, size);
        origin.delay = Some(delay);
        (origin, calls)
    }
}

impl Origin for CountingSizeOrigin {
    fn kind(&self) -> OriginKind {
        OriginKind::Http
    }

    fn fetch(
        &self,
        _hash: Hash,
        _max_bytes: u64,
    ) -> Pin<Box<dyn Future<Output = Result<OriginFetch, crate::OriginPullError>> + Send + '_>>
    {
        // Never exercised by the probe path (existence + size only).
        Box::pin(async { Ok(OriginFetch::NotFound) })
    }

    fn size(
        &self,
        hash: Hash,
    ) -> Pin<Box<dyn Future<Output = Result<Option<u64>, crate::OriginPullError>> + Send + '_>>
    {
        let matches = hash == self.hash;
        let size = self.size;
        let calls = Arc::clone(&self.calls);
        let delay = self.delay;
        Box::pin(async move {
            // Count the *attempt* before any await, so a probe that the
            // caller times out still registers as a backend hit.
            calls.fetch_add(1, Ordering::SeqCst);
            if let Some(d) = delay {
                tokio::time::sleep(d).await;
            }
            Ok(matches.then_some(size))
        })
    }
}

/// Origin whose `size()` (a `HEAD`/`HeadObject` stand-in) always fails
/// with a transport error, and COUNTS the calls — the `Fault`-arm
/// counterpart to [`CountingSizeOrigin`].
#[derive(Debug)]
struct FailingSizeOrigin {
    calls: Arc<AtomicUsize>,
}

impl FailingSizeOrigin {
    fn new() -> (Self, Arc<AtomicUsize>) {
        let calls = Arc::new(AtomicUsize::new(0));
        (
            Self {
                calls: Arc::clone(&calls),
            },
            calls,
        )
    }
}

impl Origin for FailingSizeOrigin {
    fn kind(&self) -> OriginKind {
        OriginKind::Http
    }

    fn fetch(
        &self,
        _hash: Hash,
        _max_bytes: u64,
    ) -> Pin<Box<dyn Future<Output = Result<OriginFetch, crate::OriginPullError>> + Send + '_>>
    {
        Box::pin(async { Ok(OriginFetch::NotFound) })
    }

    fn size(
        &self,
        _hash: Hash,
    ) -> Pin<Box<dyn Future<Output = Result<Option<u64>, crate::OriginPullError>> + Send + '_>>
    {
        let calls = Arc::clone(&self.calls);
        Box::pin(async move {
            calls.fetch_add(1, Ordering::SeqCst);
            Err(crate::OriginPullError::Transient(anyhow::anyhow!(
                "synthetic HEAD outage (connection refused)"
            )))
        })
    }
}

/// A per-origin transport error on the live `HEAD` — not just a timeout —
/// is `Fault`, never `Absent` (#1766 follow-up): `origin_probe_presence`
/// walks the origin chain itself rather than going through
/// [`CacheEngine::origin_size`], whose swallow-and-advance per-origin
/// error handling would otherwise launder a connection-refused/5xx/DNS
/// outage into a false `Ok(None)`. Also confirms the fault is never
/// memoised: a second probe re-walks the chain (call count 1 -> 2).
#[tokio::test]
async fn origin_probe_presence_transport_error_is_fault_not_absent() -> anyhow::Result<()> {
    let tmp = tempfile::tempdir()?;
    let hash = Hash::new(b"faulting origin probe");
    let (origin, calls) = FailingSizeOrigin::new();
    let engine =
        CacheEngine::open(tmp.path(), vec![Arc::new(origin) as Arc<dyn Origin>], 10).await?;

    assert_eq!(
        engine.origin_probe_presence(hash).await,
        OriginPresence::Fault,
        "a per-origin transport error must be Fault, not Absent",
    );
    assert_eq!(calls.load(Ordering::SeqCst), 1, "one backend HEAD attempt");
    assert_eq!(
        engine.origin_probe_presence(hash).await,
        OriginPresence::Fault,
        "a fault is memoised under the fault TTL (#1789 item 6)",
    );
    assert_eq!(
        calls.load(Ordering::SeqCst),
        1,
        "the second probe served the cached fault rather than re-walking \
         the origin chain — a steady state re-probes once per fault TTL, \
         not once per request",
    );
    assert_eq!(
        engine.origin_probe_size(hash).await,
        None,
        "the thin size wrapper still folds Fault to None",
    );
    Ok(())
}

/// A present remote object is discovered by a live `HEAD` and the answer is
/// memoised: a second probe for the same hash issues NO further backend call.
#[tokio::test]
async fn origin_probe_size_hits_origin_then_memoises_positive() -> anyhow::Result<()> {
    let tmp = tempfile::tempdir()?;
    let hash = Hash::new(b"remote object");
    let (origin, calls) = CountingSizeOrigin::new(hash, 4096);
    let engine =
        CacheEngine::open(tmp.path(), vec![Arc::new(origin) as Arc<dyn Origin>], 10).await?;

    assert_eq!(
        engine.origin_probe_size(hash).await,
        Some(4096),
        "live HEAD finds it"
    );
    assert_eq!(calls.load(Ordering::SeqCst), 1, "one backend HEAD");
    assert_eq!(
        engine.origin_probe_size(hash).await,
        Some(4096),
        "served from memo"
    );
    assert_eq!(
        calls.load(Ordering::SeqCst),
        1,
        "memo hit issues no second HEAD"
    );
    Ok(())
}

/// A 404 is memoised too — the whole point of the negative cache is that a
/// random-hash probe flood does not re-`HeadObject` the origin every time.
#[tokio::test]
async fn origin_probe_size_memoises_absent() -> anyhow::Result<()> {
    let tmp = tempfile::tempdir()?;
    let present = Hash::new(b"present");
    let absent = Hash::new(b"absent");
    let (origin, calls) = CountingSizeOrigin::new(present, 100);
    let engine =
        CacheEngine::open(tmp.path(), vec![Arc::new(origin) as Arc<dyn Origin>], 10).await?;

    assert_eq!(
        engine.origin_probe_size(absent).await,
        None,
        "not in origin"
    );
    assert_eq!(engine.origin_probe_size(absent).await, None, "still absent");
    assert_eq!(calls.load(Ordering::SeqCst), 1, "negative answer is cached");
    Ok(())
}

/// A refused (denied/blacklisted/evicted) hash is never advertised, and the
/// guard short-circuits BEFORE any backend probe.
#[tokio::test]
async fn origin_probe_size_refused_never_probes() -> anyhow::Result<()> {
    let tmp = tempfile::tempdir()?;
    let hash = Hash::new(b"denied object");
    let (origin, calls) = CountingSizeOrigin::new(hash, 100);
    let engine =
        CacheEngine::open(tmp.path(), vec![Arc::new(origin) as Arc<dyn Origin>], 10).await?;
    engine.set_chain_denied_one(hash, true);

    assert_eq!(
        engine.origin_probe_size(hash).await,
        None,
        "refused stays hidden"
    );
    assert_eq!(
        calls.load(Ordering::SeqCst),
        0,
        "no HEAD for a refused hash"
    );
    Ok(())
}

/// A slow origin must not stall the probe: the live HEAD is bounded by
/// `origin_probe_timeout_ms` and a timeout folds to `None` (safe — never
/// slashable).
#[tokio::test]
async fn origin_probe_size_times_out_to_absent() -> anyhow::Result<()> {
    let tmp = tempfile::tempdir()?;
    let hash = Hash::new(b"slow object");
    let (origin, _calls) = CountingSizeOrigin::slow(hash, 100, Duration::from_millis(400));
    let engine =
        CacheEngine::open(tmp.path(), vec![Arc::new(origin) as Arc<dyn Origin>], 10).await?;
    // Tight timeout so the 400 ms origin overruns it.
    engine.set_origin_probe_config(OriginProbePolicy {
        positive_ttl: Duration::from_secs(15),
        negative_ttl: Duration::from_secs(2),
        fault_ttl: Duration::from_secs(5),
        timeout: Duration::from_millis(20),
        capacity: 16,
    });

    assert_eq!(
        engine.origin_probe_size(hash).await,
        None,
        "a HEAD slower than the ceiling folds to absent",
    );
    Ok(())
}

/// `origin_probe_presence` distinguishes `Present`/`Absent`/`Fault`
/// (#1766): a hash the origin holds is `Present(size)`, a hash it does not
/// is `Absent`, and a `HEAD` slower than the probe ceiling is `Fault` —
/// NOT `Absent`, so a caller can never sign an authoritative `NotFound`
/// off a backend blip.
#[tokio::test]
async fn origin_probe_presence_distinguishes_present_absent_and_fault() -> anyhow::Result<()> {
    // `Present`/`Absent` against a fast origin.
    let tmp = tempfile::tempdir()?;
    let present = Hash::new(b"present for presence");
    let absent = Hash::new(b"absent for presence");
    let (present_origin, _present_calls) = CountingSizeOrigin::new(present, 777);
    let engine = CacheEngine::open(
        tmp.path(),
        vec![Arc::new(present_origin) as Arc<dyn Origin>],
        10,
    )
    .await?;
    assert_eq!(
        engine.origin_probe_presence(present).await,
        OriginPresence::Present(777),
        "a live HEAD hit is Present, not folded away",
    );
    assert_eq!(
        engine.origin_probe_presence(absent).await,
        OriginPresence::Absent,
        "a genuine miss is Absent",
    );

    // `Fault` against a separate engine whose only origin overruns the
    // probe ceiling (a shared origin would delay the Present/Absent
    // probes above too, since `CountingSizeOrigin`'s delay is unconditional).
    let tmp2 = tempfile::tempdir()?;
    let slow = Hash::new(b"slow for presence");
    let (slow_origin, slow_calls) = CountingSizeOrigin::slow(slow, 100, Duration::from_millis(400));
    let fault_engine = CacheEngine::open(
        tmp2.path(),
        vec![Arc::new(slow_origin) as Arc<dyn Origin>],
        10,
    )
    .await?;
    // Tight timeout so the 400 ms origin overruns it.
    fault_engine.set_origin_probe_config(OriginProbePolicy {
        positive_ttl: Duration::from_secs(15),
        negative_ttl: Duration::from_secs(2),
        fault_ttl: Duration::from_secs(5),
        timeout: Duration::from_millis(20),
        capacity: 16,
    });
    assert_eq!(
        fault_engine.origin_probe_presence(slow).await,
        OriginPresence::Fault,
        "a HEAD slower than the ceiling is Fault, not Absent",
    );
    assert_eq!(
        slow_calls.load(Ordering::SeqCst),
        1,
        "one backend attempt for the first faulting probe",
    );
    assert_eq!(
        fault_engine.origin_probe_presence(slow).await,
        OriginPresence::Fault,
        "a memoised fault still answers Fault",
    );
    assert_eq!(
        slow_calls.load(Ordering::SeqCst),
        1,
        "the second probe served the cached fault instead of re-hitting \
         the backend (#1789 item 6)",
    );
    Ok(())
}

/// `origin_probe_size` stays a thin `Present -> Some`, `Absent`/`Fault ->
/// None` wrapper (#1766): callers that only care about advertising size
/// (probe `has_blob`, DHT announce) must not change behavior when a fault
/// is now distinguishable one layer down.
#[tokio::test]
async fn origin_probe_size_folds_fault_to_none_like_absent() -> anyhow::Result<()> {
    let tmp = tempfile::tempdir()?;
    let hash = Hash::new(b"slow object for size wrapper");
    let (origin, _calls) = CountingSizeOrigin::slow(hash, 100, Duration::from_millis(400));
    let engine =
        CacheEngine::open(tmp.path(), vec![Arc::new(origin) as Arc<dyn Origin>], 10).await?;
    engine.set_origin_probe_config(OriginProbePolicy {
        positive_ttl: Duration::from_secs(15),
        negative_ttl: Duration::from_secs(2),
        fault_ttl: Duration::from_secs(5),
        timeout: Duration::from_millis(20),
        capacity: 16,
    });

    assert_eq!(
        engine.origin_probe_size(hash).await,
        None,
        "a fault still folds to None through the size wrapper",
    );
    Ok(())
}

/// `total_bytes` must sum the on-disk footprint — it is the eviction
/// driver's entire input, so a wrong sum silently mis-sizes every decision.
#[tokio::test]
async fn total_bytes_sums_populated_blobs() -> anyhow::Result<()> {
    let tmp = tempfile::tempdir()?;
    let payload = b"total-bytes payload";
    let hash = Hash::new(payload);
    let engine = CacheEngine::open(
        tmp.path(),
        vec![Arc::new(StubOrigin::new(payload)) as Arc<dyn Origin>],
        10,
    )
    .await?;

    anyhow::ensure!(engine.total_bytes().await? == 0, "empty cache is 0 bytes");
    let _ = engine.get(hash).await?;

    let total = engine.total_bytes().await?;
    anyhow::ensure!(
        total >= payload.len() as u64,
        "total_bytes {total} should cover the {}-byte blob",
        payload.len()
    );
    let sizes = engine.size_snapshot().await?;
    anyhow::ensure!(
        sizes.get(&hash).copied() == Some(payload.len() as u64),
        "size_snapshot should report the blob's exact size"
    );
    Ok(())
}

/// The load-bearing distinction from `evict`: capacity eviction must NOT
/// write the durable takedown log, or every LRU victim would be permanently
/// un-servable and the log would grow without bound (#1173).
#[tokio::test]
async fn release_for_eviction_does_not_write_the_evicted_log() -> anyhow::Result<()> {
    let tmp = tempfile::tempdir()?;
    let payload = b"soft evict payload";
    let hash = Hash::new(payload);
    let engine = CacheEngine::open(
        tmp.path(),
        vec![Arc::new(StubOrigin::new(payload)) as Arc<dyn Origin>],
        10,
    )
    .await?;
    let _ = engine.get(hash).await?;

    engine.release_for_eviction(hash).await?;

    anyhow::ensure!(
        !engine.is_evicted(hash),
        "soft evict must not enter the logical-evicted set"
    );
    anyhow::ensure!(
        !tmp.path().join("evicted.log").exists(),
        "soft evict must not create the durable takedown log"
    );
    // The access-time entry is forgotten, so the LRU driver won't re-pick it.
    anyhow::ensure!(
        engine.last_accessed(hash).is_none(),
        "soft evict should forget the access-time entry"
    );
    anyhow::ensure!(
        !engine.eviction_candidates().contains_key(&hash),
        "released hash should leave the candidate set"
    );
    Ok(())
}

/// The origin-held index (#1130) advertises fs-origin content by directory
/// enumeration, includes only *present* pins, and excludes refused hashes.
#[tokio::test]
async fn rescan_origins_indexes_fs_and_present_pins() -> anyhow::Result<()> {
    use crate::origin::FilesystemOrigin;

    async fn seed(base: &Path, payload: Vec<u8>) -> anyhow::Result<(Hash, u64)> {
        let hash = Hash::new(&payload);
        let hex = hash.to_hex();
        let shard = hex.get(..2).unwrap_or("");
        let dir = base.join(shard);
        tokio::fs::create_dir_all(&dir).await?;
        tokio::fs::write(dir.join(hex.as_str()), &payload).await?;
        let len = u64::try_from(payload.len()).unwrap_or(u64::MAX);
        Ok((hash, len))
    }

    let origin_dir = tempfile::tempdir()?;
    let base = tokio::fs::canonicalize(origin_dir.path()).await?;
    let (h1, len1) = seed(&base, vec![1u8; 5000]).await?;
    let (h2, len2) = seed(&base, vec![2u8; 9000]).await?;

    let cache_dir = tempfile::tempdir()?;
    let origin = Arc::new(FilesystemOrigin::new(&base).await?) as Arc<dyn Origin>;
    let engine = CacheEngine::open(cache_dir.path(), vec![origin], 10).await?;

    // Pin one present hash and one absent hash; only the present one is held.
    let absent = Hash::new(b"never-on-disk");
    engine.set_pinned(&PinnedHashes::new(
        [from_store_hash(h1), from_store_hash(absent)]
            .into_iter()
            .collect(),
    ));

    engine.rescan_origins().await;

    let held: HashSet<Hash> = engine.origin_held_snapshot().hashes;
    anyhow::ensure!(
        held.contains(&h1) && held.contains(&h2),
        "fs blobs must be enumerated into the held index",
    );
    anyhow::ensure!(!held.contains(&absent), "absent pin must not be held");
    anyhow::ensure!(engine.origin_held_size(h1) == Some(len1), "h1 size wrong");
    anyhow::ensure!(engine.origin_held_size(h2) == Some(len2), "h2 size wrong");
    anyhow::ensure!(
        engine.origin_held_size(absent).is_none(),
        "absent hash must not be servable from origin",
    );

    // A hash denied AFTER the last rescan must drop out of the *live* reads
    // immediately — the snapshot still lists it, but `refuses` is the
    // authority. This guards the #1130 blacklist-compliance interaction:
    // without it the probe would sign has_blob:true for a just-blacklisted
    // blob (g_node_04). No rescan between the deny and the assertions.
    engine.set_denied(&crate::DeniedHashes::new(
        [from_store_hash(h1)].into_iter().collect(),
    ));
    anyhow::ensure!(
        engine.origin_held_size(h1).is_none(),
        "a hash denied since the last rescan must not be servable from origin",
    );
    anyhow::ensure!(
        !engine.origin_held_snapshot().hashes.contains(&h1),
        "a hash denied since the last rescan must not be announced",
    );

    // And it also drops from the index proper on the next rescan.
    engine.set_denied(&crate::DeniedHashes::new(
        [from_store_hash(h1), from_store_hash(h2)]
            .into_iter()
            .collect(),
    ));
    engine.rescan_origins().await;
    anyhow::ensure!(
        engine.origin_held_size(h2).is_none(),
        "denied hash must not be advertised",
    );
    Ok(())
}

/// Origin that enumerates a fixed set and whose `size()` can be switched
/// from answering to faulting, standing in for a `HeadObject` throttle
/// window that opens between two rescans.
#[derive(Debug)]
struct ThrottlableOrigin {
    held: Vec<(Hash, u64)>,
    faulting: Arc<AtomicBool>,
    /// The fault is `Permanent` rather than `Transient` — a revoked ACL or a
    /// symlink escape, which no later rescan resolves.
    permanent: Arc<AtomicBool>,
    enumerate_fails: Arc<AtomicBool>,
    /// The origin still *lists* `held` but no longer serves it, so `size`
    /// answers an authoritative `Ok(None)`.
    holds: Arc<AtomicBool>,
    /// Milliseconds each `size` takes, so a test can keep a pass in flight
    /// while other triggers arrive.
    slow_ms: Arc<AtomicU64>,
    size_calls: Arc<AtomicUsize>,
}

impl ThrottlableOrigin {
    fn new(held: Vec<(Hash, u64)>) -> (Self, ThrottleControls) {
        let controls = ThrottleControls {
            faulting: Arc::new(AtomicBool::new(false)),
            permanent: Arc::new(AtomicBool::new(false)),
            enumerate_fails: Arc::new(AtomicBool::new(false)),
            holds: Arc::new(AtomicBool::new(true)),
            slow_ms: Arc::new(AtomicU64::new(0)),
            size_calls: Arc::new(AtomicUsize::new(0)),
        };
        (
            Self {
                held,
                faulting: Arc::clone(&controls.faulting),
                permanent: Arc::clone(&controls.permanent),
                enumerate_fails: Arc::clone(&controls.enumerate_fails),
                holds: Arc::clone(&controls.holds),
                slow_ms: Arc::clone(&controls.slow_ms),
                size_calls: Arc::clone(&controls.size_calls),
            },
            controls,
        )
    }
}

/// The switches and the probe counter a test drives [`ThrottlableOrigin`] by.
struct ThrottleControls {
    faulting: Arc<AtomicBool>,
    permanent: Arc<AtomicBool>,
    enumerate_fails: Arc<AtomicBool>,
    holds: Arc<AtomicBool>,
    slow_ms: Arc<AtomicU64>,
    size_calls: Arc<AtomicUsize>,
}

impl Origin for ThrottlableOrigin {
    fn kind(&self) -> OriginKind {
        OriginKind::S3
    }

    fn fetch(
        &self,
        _hash: Hash,
        _max_bytes: u64,
    ) -> Pin<Box<dyn Future<Output = Result<OriginFetch, crate::OriginPullError>> + Send + '_>>
    {
        Box::pin(async { Ok(OriginFetch::NotFound) })
    }

    fn size(
        &self,
        hash: Hash,
    ) -> Pin<Box<dyn Future<Output = Result<Option<u64>, crate::OriginPullError>> + Send + '_>>
    {
        Box::pin(async move {
            self.size_calls.fetch_add(1, Ordering::SeqCst);
            let slow = self.slow_ms.load(Ordering::SeqCst);
            if slow > 0 {
                tokio::time::sleep(Duration::from_millis(slow)).await;
            }
            if self.faulting.load(Ordering::SeqCst) {
                return Err(if self.permanent.load(Ordering::SeqCst) {
                    crate::OriginPullError::Permanent(anyhow::anyhow!(
                        "synthetic HeadObject access denied"
                    ))
                } else {
                    crate::OriginPullError::Transient(anyhow::anyhow!(
                        "synthetic HeadObject throttle (503 SlowDown)"
                    ))
                });
            }
            if !self.holds.load(Ordering::SeqCst) {
                return Ok(None);
            }
            Ok(self
                .held
                .iter()
                .find(|(h, _)| *h == hash)
                .map(|(_, len)| *len))
        })
    }

    fn enumerate(
        &self,
    ) -> Pin<Box<dyn Future<Output = Result<Vec<Hash>, crate::OriginPullError>> + Send + '_>> {
        Box::pin(async {
            if self.enumerate_fails.load(Ordering::SeqCst) {
                return Err(crate::OriginPullError::Transient(anyhow::anyhow!(
                    "synthetic ListObjectsV2 outage"
                )));
            }
            Ok(self.held.iter().map(|(h, _)| *h).collect())
        })
    }
}

/// A faulted size probe must not evict a hash from the origin-held index.
///
/// The index is what probe and DHT-announce advertise. A transport fault is
/// not an authoritative absence, so dropping on one makes a throttle window
/// the origin recovers from in seconds cost a whole rescan interval of
/// announce coverage — with every downstream signal reporting success.
#[tokio::test]
async fn rescan_origins_carries_a_faulted_probe_forward() -> anyhow::Result<()> {
    let indexed = Hash::new(b"throttle-indexed");
    let fresh = Hash::new(b"throttle-fresh");
    let (origin, controls) = ThrottlableOrigin::new(vec![(indexed, 5000), (fresh, 9000)]);
    let origin = Arc::new(origin) as Arc<dyn Origin>;

    let tmp = tempfile::tempdir()?;
    let cm = Arc::new(CacheMetrics::default());
    let engine = CacheEngine::open_full(
        tmp.path(),
        vec![origin],
        10,
        // `fresh` is also pinned, so it reaches the probe loop twice — once
        // from the listing and once from the pin set. It must be probed once.
        crate::PinnedHashes::new([from_store_hash(fresh)].into_iter().collect()),
        crate::RetryPolicy::default(),
        CircuitBreakerPolicy::default(),
        Some(Arc::clone(&cm)),
        Duration::ZERO,
    )
    .await?;

    // First rescan resolves `indexed` only: `fresh` is enumerated but the
    // origin is asked about it after the throttle opens.
    engine.set_denied(&crate::DeniedHashes::new(
        [from_store_hash(fresh)].into_iter().collect(),
    ));
    engine.rescan_origins().await;
    anyhow::ensure!(
        engine.origin_held_size(indexed) == Some(5000),
        "the healthy rescan must index the enumerated hash",
    );
    anyhow::ensure!(
        engine.origin_held_snapshot().probe_faults == 0,
        "a healthy rescan reports no faults",
    );

    // The throttle opens, and `fresh` becomes a candidate for the first time.
    engine.set_denied(&crate::DeniedHashes::new(HashSet::new()));
    controls.faulting.store(true, Ordering::SeqCst);
    controls.size_calls.store(0, Ordering::SeqCst);
    engine.rescan_origins().await;

    anyhow::ensure!(
        controls.size_calls.load(Ordering::SeqCst) == 2,
        "each candidate is probed once however many times it is listed, or a \
         duplicate inflates the fault count: {} probes for 2 candidates",
        controls.size_calls.load(Ordering::SeqCst),
    );

    anyhow::ensure!(
        engine.origin_held_size(indexed) == Some(5000),
        "a faulted probe must keep the previous index entry, not drop it",
    );
    anyhow::ensure!(
        engine.origin_held_size(fresh).is_none(),
        "a candidate first seen inside the fault window has nothing to carry forward",
    );
    anyhow::ensure!(
        engine.origin_held_snapshot().probe_faults == 2,
        "both faulted probes must be reported to the DHT seed paths, got {}",
        engine.origin_held_snapshot().probe_faults,
    );

    // The rescan is the metric's only source, so the counter and the
    // per-rescan report must agree.
    anyhow::ensure!(
        cm.origin_probe_failures.get() == 2,
        "faulted probes must surface on their counter, got {}",
        cm.origin_probe_failures.get(),
    );

    // A hash denied since the last rescan must not reach the announce set,
    // even though the index still lists it. `origin_held_snapshot` is what
    // the DHT seed reads, so the live refusal filter has to be applied there
    // and not only on the per-hash lookup.
    engine.set_denied(&crate::DeniedHashes::new(
        [from_store_hash(indexed)].into_iter().collect(),
    ));
    anyhow::ensure!(
        !engine.origin_held_snapshot().hashes.contains(&indexed),
        "a hash denied since the last rescan must not be announced",
    );
    engine.set_denied(&crate::DeniedHashes::new(HashSet::new()));

    // The throttle closes: the next rescan resolves both.
    controls.faulting.store(false, Ordering::SeqCst);
    engine.rescan_origins().await;
    anyhow::ensure!(
        engine.origin_held_size(fresh) == Some(9000),
        "a recovered origin must index what the fault window missed",
    );
    anyhow::ensure!(
        engine.origin_held_snapshot().probe_faults == 0,
        "a recovered rescan clears the fault report",
    );
    Ok(())
}

/// Carry-forward must not make an index entry immortal.
///
/// The counterpart risk of keeping a faulted candidate: a hash the origin
/// genuinely stopped holding has to leave, or the node advertises content it
/// cannot serve and every request for it becomes a refusal. Only a fault
/// carries an entry forward — an authoritative `Ok(None)` drops it.
#[tokio::test]
async fn rescan_origins_evicts_a_hash_the_origin_no_longer_holds() -> anyhow::Result<()> {
    let goes_away = Hash::new(b"origin-drops-it");
    let (origin, controls) = ThrottlableOrigin::new(vec![(goes_away, 4242)]);
    let origin = Arc::new(origin) as Arc<dyn Origin>;

    let tmp = tempfile::tempdir()?;
    let engine = CacheEngine::open(tmp.path(), vec![origin], 10).await?;

    engine.rescan_origins().await;
    anyhow::ensure!(
        engine.origin_held_size(goes_away) == Some(4242),
        "the healthy rescan must index it",
    );

    // The origin still lists it but no longer holds it: `size` answers
    // `Ok(None)`, which is authoritative, not a fault.
    controls.holds.store(false, Ordering::SeqCst);
    engine.rescan_origins().await;

    anyhow::ensure!(
        engine.origin_held_size(goes_away).is_none(),
        "an authoritative absence must drop the entry, not carry it",
    );
    anyhow::ensure!(
        engine.origin_held_snapshot().probe_faults == 0,
        "a clean `not held` answer is not a fault",
    );
    Ok(())
}

/// Triggers arriving during a pass collapse into one rerun rather than
/// queueing.
///
/// A rescan gets slower exactly when the origin is faulting, which is when
/// the periodic trigger is most likely to fire on top of one. Queueing every
/// trigger would stack a waiter per tick and then run that backlog of
/// obsolete passes back to back, against an origin already struggling.
#[tokio::test(flavor = "multi_thread")]
async fn concurrent_rescans_collapse_into_one_rerun() -> anyhow::Result<()> {
    let listed = Hash::new(b"rescan-single-flight");
    let (origin, controls) = ThrottlableOrigin::new(vec![(listed, 11)]);
    let origin = Arc::new(origin) as Arc<dyn Origin>;

    let tmp = tempfile::tempdir()?;
    let engine = Arc::new(CacheEngine::open(tmp.path(), vec![origin], 10).await?);

    // Slow enough that the later triggers land while the first pass is
    // still probing.
    controls.slow_ms.store(50, Ordering::SeqCst);

    // Four triggers at once: one claims the slot, the rest collapse into a
    // single queued rerun — two passes over the one candidate, not four.
    controls.size_calls.store(0, Ordering::SeqCst);
    let mut joins = Vec::new();
    for _ in 0..4 {
        let engine = Arc::clone(&engine);
        joins.push(tokio::spawn(async move { engine.rescan_origins().await }));
    }
    for j in joins {
        j.await?;
    }

    let probes = controls.size_calls.load(Ordering::SeqCst);
    anyhow::ensure!(
        (1..=2).contains(&probes),
        "four triggers must collapse into at most one rerun, so at most two \
         passes probe the single candidate; saw {probes}",
    );
    anyhow::ensure!(
        engine.origin_held_size(listed) == Some(11),
        "and the index is still published",
    );

    // The slot is released, so a later trigger still runs.
    controls.slow_ms.store(0, Ordering::SeqCst);
    controls.size_calls.store(0, Ordering::SeqCst);
    engine.rescan_origins().await;
    anyhow::ensure!(
        controls.size_calls.load(Ordering::SeqCst) == 1,
        "a rescan after the burst must still run, or the slot is stranded",
    );
    Ok(())
}

/// A permanent fault must not carry an entry forward.
///
/// A revoked ACL or a symlink escape reads the same on every rescan, so
/// carrying it would hold the hash in the announce set until an operator
/// intervened — advertising content the serve path refuses, indefinitely.
#[tokio::test]
async fn rescan_origins_does_not_carry_a_permanent_fault() -> anyhow::Result<()> {
    let revoked = Hash::new(b"origin-acl-revoked");
    let (origin, controls) = ThrottlableOrigin::new(vec![(revoked, 777)]);
    let origin = Arc::new(origin) as Arc<dyn Origin>;

    let tmp = tempfile::tempdir()?;
    let cm = Arc::new(CacheMetrics::default());
    let engine = CacheEngine::open_full(
        tmp.path(),
        vec![origin],
        10,
        crate::PinnedHashes::empty(),
        crate::RetryPolicy::default(),
        CircuitBreakerPolicy::default(),
        Some(Arc::clone(&cm)),
        Duration::ZERO,
    )
    .await?;

    engine.rescan_origins().await;
    anyhow::ensure!(
        engine.origin_held_size(revoked) == Some(777),
        "the healthy rescan must index it",
    );

    controls.faulting.store(true, Ordering::SeqCst);
    controls.permanent.store(true, Ordering::SeqCst);
    engine.rescan_origins().await;

    let report = engine.origin_held_snapshot();
    anyhow::ensure!(
        !report.hashes.contains(&revoked),
        "a permanent fault must drop the entry rather than advertise content \
         the serve path will refuse for the process lifetime",
    );
    anyhow::ensure!(
        report.probe_faults == 1,
        "it is still a probe that could not answer, so it still counts",
    );
    Ok(())
}

/// An origin that cannot be listed drops every hash discoverable only
/// through it, and must say so.
///
/// This is the more severe half of the same silent shrink: no candidate is
/// produced, so there is no per-hash fault and nothing to carry forward. A
/// seed reading only the probe-fault count would call the truncated set
/// healthy.
#[tokio::test]
async fn rescan_origins_reports_an_origin_it_could_not_list() -> anyhow::Result<()> {
    let listed = Hash::new(b"listing-outage");
    let (origin, controls) = ThrottlableOrigin::new(vec![(listed, 1234)]);
    let origin = Arc::new(origin) as Arc<dyn Origin>;

    let tmp = tempfile::tempdir()?;
    let cm = Arc::new(CacheMetrics::default());
    let engine = CacheEngine::open_full(
        tmp.path(),
        vec![origin],
        10,
        crate::PinnedHashes::empty(),
        crate::RetryPolicy::default(),
        CircuitBreakerPolicy::default(),
        Some(Arc::clone(&cm)),
        Duration::ZERO,
    )
    .await?;

    engine.rescan_origins().await;
    anyhow::ensure!(
        engine.origin_held_size(listed) == Some(1234),
        "the healthy rescan must index the listed hash",
    );

    controls.enumerate_fails.store(true, Ordering::SeqCst);
    engine.rescan_origins().await;

    let report = engine.origin_held_snapshot();
    anyhow::ensure!(
        report.enumerate_failures == 1,
        "the failed listing must be reported, got {}",
        report.enumerate_failures,
    );
    anyhow::ensure!(
        report.probe_faults == 0,
        "no candidate was produced, so there is no probe to fault",
    );
    anyhow::ensure!(
        !report.hashes.contains(&listed),
        "an unlisted, unpinned hash genuinely leaves the announce set — the \
         point is that the report says so",
    );
    anyhow::ensure!(
        cm.origin_enumerate_failures.get() == 1,
        "the failed listing must surface on its counter, got {}",
        cm.origin_enumerate_failures.get(),
    );
    Ok(())
}

/// A pinned hash must be refused with `Ok(0)` and left completely untouched.
/// The driver relies on the `0` to avoid crediting a no-op as freed bytes.
#[tokio::test]
async fn release_for_eviction_refuses_pinned_hash() -> anyhow::Result<()> {
    let tmp = tempfile::tempdir()?;
    let payload = b"pinned payload";
    let hash = Hash::new(payload);
    let engine = CacheEngine::open(
        tmp.path(),
        vec![Arc::new(StubOrigin::new(payload)) as Arc<dyn Origin>],
        10,
    )
    .await?;
    let _ = engine.get(hash).await?;

    engine.set_pinned(&PinnedHashes::new(
        [from_store_hash(hash)].into_iter().collect(),
    ));

    let released = engine.release_for_eviction(hash).await?;
    anyhow::ensure!(released == 0, "pinned hash must report 0 released");
    anyhow::ensure!(
        engine.last_accessed(hash).is_some(),
        "pinned refusal must not forget the access-time entry"
    );
    anyhow::ensure!(engine.has(hash).await?, "pinned blob must still be present");
    Ok(())
}

/// The pin exemption is carved out for a deny-listed hash: "deny wins over
/// pin" on the space path too, so a pinned + governance-denied hash is
/// reclaimable here rather than held on disk (unservable) until the takedown
/// `evict()` runs.
#[tokio::test]
async fn release_for_eviction_reclaims_a_deny_listed_pinned_hash() -> anyhow::Result<()> {
    let tmp = tempfile::tempdir()?;
    let payload = b"pinned then denied";
    let hash = Hash::new(payload);
    let engine = CacheEngine::open(
        tmp.path(),
        vec![Arc::new(StubOrigin::new(payload)) as Arc<dyn Origin>],
        10,
    )
    .await?;
    let _ = engine.get(hash).await?;
    engine.set_pinned(&PinnedHashes::new(
        [from_store_hash(hash)].into_iter().collect(),
    ));

    // Pinned + clean: still exempt from space reclaim.
    anyhow::ensure!(
        engine.release_for_eviction(hash).await? == 0,
        "a pinned clean hash stays exempt"
    );
    anyhow::ensure!(
        engine.last_accessed(hash).is_some(),
        "the clean exemption must keep the access-time entry"
    );

    // Governance denies it → the pin no longer exempts it.
    anyhow::ensure!(
        engine.set_chain_denied_one(hash, true),
        "deny must change the set"
    );
    engine.release_for_eviction(hash).await?;
    anyhow::ensure!(
        engine.last_accessed(hash).is_none(),
        "a pinned + governance-denied hash must go through the reclaim path (which \
         forgets the access-time entry), not the pin early-return"
    );
    Ok(())
}

#[tokio::test]
async fn get_cache_hit_records_access_time() -> anyhow::Result<()> {
    let tmp = tempfile::tempdir()?;
    let payload = b"hello cache hit";
    let hash = Hash::new(payload);
    let origin = StubOrigin::new(payload);

    let engine =
        CacheEngine::open(tmp.path(), vec![Arc::new(origin) as Arc<dyn Origin>], 10).await?;

    // Prime the cache via pull-through.
    let _ = engine.get(hash).await?;

    // Clear the access time so the next get proves a cache-hit path.
    engine.inner.access_times.clear();

    // Read again — this time it's a local hit.
    let _ = engine.get(hash).await?;

    anyhow::ensure!(
        engine.last_accessed(hash).is_some(),
        "expected Some(Instant) after cache-hit get"
    );
    Ok(())
}

#[tokio::test]
async fn pull_through_records_access_time() -> anyhow::Result<()> {
    let tmp = tempfile::tempdir()?;
    let payload = b"hello pull-through";
    let hash = Hash::new(payload);
    let origin = StubOrigin::new(payload);

    let engine =
        CacheEngine::open(tmp.path(), vec![Arc::new(origin) as Arc<dyn Origin>], 10).await?;

    // First get triggers pull-through.
    let _ = engine.get(hash).await?;

    anyhow::ensure!(
        engine.last_accessed(hash).is_some(),
        "expected Some(Instant) after pull-through get"
    );
    Ok(())
}

/// The `FillMode` ↔ return-shape correspondence, asserted where the private
/// types are visible — the integration tests in `tests/pull_through.rs`
/// cannot see `PullThroughOutcome`, so they can only observe that the fill
/// happened, not which arm produced it.
///
/// This is the property the enum exists to carry, and it must hold in BOTH
/// directions: `ReturnBytes` always yields `Some`, `CommitOnly` always yields
/// `None`. Before `FillMode` the second half was false on the buffered arm,
/// which handed its drain buffer back regardless — an exception that made the
/// wrappers partial and the mode untrustworthy to read.
///
/// Deliberately run through the BUFFERED arm: `StubOrigin` advertises a size
/// hint well under `buffered_max_bytes`, so `should_buffer` routes here. The
/// streaming arm never had the defect.
#[tokio::test]
async fn fill_mode_determines_the_return_shape_in_both_directions() -> anyhow::Result<()> {
    let payload = b"drained, not streamed";
    let hash = Hash::new(payload);

    let tmp_fill = tempfile::tempdir()?;
    let engine = CacheEngine::open(
        tmp_fill.path(),
        vec![Arc::new(StubOrigin::new(payload)) as Arc<dyn Origin>],
        10,
    )
    .await?;
    let committed = engine
        .pull_through(hash, false, FillMode::CommitOnly)
        .await?;
    anyhow::ensure!(
        committed.is_none(),
        "CommitOnly must yield no payload, got {committed:?}"
    );

    let tmp_bytes = tempfile::tempdir()?;
    let engine = CacheEngine::open(
        tmp_bytes.path(),
        vec![Arc::new(StubOrigin::new(payload)) as Arc<dyn Origin>],
        10,
    )
    .await?;
    let returned = engine
        .pull_through(hash, false, FillMode::ReturnBytes)
        .await?;
    anyhow::ensure!(
        returned.as_deref() == Some(payload.as_slice()),
        "ReturnBytes must yield the payload, got {returned:?}"
    );
    Ok(())
}

#[tokio::test]
async fn subscribe_inserts_emits_on_pull_through_success() -> anyhow::Result<()> {
    let tmp = tempfile::tempdir()?;
    let payload = b"hello dht hook";
    let hash = Hash::new(payload);
    let origin = StubOrigin::new(payload);

    let engine =
        CacheEngine::open(tmp.path(), vec![Arc::new(origin) as Arc<dyn Origin>], 10).await?;
    let mut rx = engine.subscribe_inserts();

    // Successful pull-through must announce the hash to the
    // subscriber. The DHT republish scheduler (#320)
    // consumes this stream to drive `Store` fan-out to the K+3
    // closest peers.
    let _ = engine.get(hash).await?;
    let announced = tokio::time::timeout(std::time::Duration::from_secs(1), rx.recv())
        .await
        .map_err(|_| anyhow::anyhow!("timed out waiting for subscribe_inserts emit"))?
        .map_err(|e| anyhow::anyhow!("recv: {e}"))?;
    anyhow::ensure!(
        announced == hash,
        "expected announced hash to equal committed hash"
    );
    Ok(())
}

#[tokio::test]
async fn subscribe_inserts_does_not_emit_on_cache_hit() -> anyhow::Result<()> {
    // A `get` that hits the local store (no pull-through, no new
    // commit) must NOT emit on the channel — only fresh commits
    // do, because the consumer's job is to schedule a NEW publish
    // cycle for newly-cached blobs.
    let tmp = tempfile::tempdir()?;
    let payload = b"hello cached-hit";
    let hash = Hash::new(payload);
    let origin = StubOrigin::new(payload);

    let engine =
        CacheEngine::open(tmp.path(), vec![Arc::new(origin) as Arc<dyn Origin>], 10).await?;

    // First get: pull-through, should emit. Drain that emission so
    // the channel is empty before the second get.
    let mut rx = engine.subscribe_inserts();
    let _ = engine.get(hash).await?;
    let _ = tokio::time::timeout(std::time::Duration::from_secs(1), rx.recv())
        .await
        .map_err(|_| anyhow::anyhow!("first pull-through emission missing"))?
        .map_err(|e| anyhow::anyhow!("recv first: {e}"))?;

    // Second get: cache hit. No emission expected.
    let _ = engine.get(hash).await?;
    let r = tokio::time::timeout(std::time::Duration::from_millis(100), rx.recv()).await;
    anyhow::ensure!(
        r.is_err(),
        "cache hit must not emit on subscribe_inserts; got {r:?}"
    );
    Ok(())
}

#[tokio::test]
async fn second_get_updates_access_time() -> anyhow::Result<()> {
    let tmp = tempfile::tempdir()?;
    let payload = b"hello update";
    let hash = Hash::new(payload);
    let origin = StubOrigin::new(payload);

    let engine =
        CacheEngine::open(tmp.path(), vec![Arc::new(origin) as Arc<dyn Origin>], 10).await?;

    // First access (pull-through).
    let _ = engine.get(hash).await?;
    let first = engine
        .last_accessed(hash)
        .ok_or_else(|| anyhow::anyhow!("expected Some after first get"))?;

    // Burn a tiny bit of real wall-clock time so Instant::now() advances.
    tokio::time::sleep(std::time::Duration::from_millis(5)).await;

    // Second access (cache hit).
    let _ = engine.get(hash).await?;
    let second = engine
        .last_accessed(hash)
        .ok_or_else(|| anyhow::anyhow!("expected Some after second get"))?;

    anyhow::ensure!(
        second > first,
        "access time should advance: first={first:?}, second={second:?}"
    );
    Ok(())
}

#[tokio::test]
async fn observe_hit_forwards_to_frequency_estimator() -> anyhow::Result<()> {
    use crate::policy::FrequencyEstimator;
    use std::sync::atomic::{AtomicU32, Ordering};

    #[derive(Debug, Default)]
    struct Counter(AtomicU32);
    impl FrequencyEstimator for Counter {
        fn observe(&self, _h: Hash) {
            self.0.fetch_add(1, Ordering::Relaxed);
        }
        fn estimate(&self, _h: Hash) -> u32 {
            self.0.load(Ordering::Relaxed)
        }
    }

    let tmp = tempfile::tempdir()?;
    let payload = b"hello frequency";
    let hash = Hash::new(payload);
    let origin = StubOrigin::new(payload);

    let engine =
        CacheEngine::open(tmp.path(), vec![Arc::new(origin) as Arc<dyn Origin>], 10).await?;

    let counter = Arc::new(Counter::default());
    engine.set_frequency_estimator(counter.clone());

    let _ = engine.get(hash).await?;

    anyhow::ensure!(counter.estimate(hash) >= 1, "observe should fire on access");
    Ok(())
}

/// ADR 040 serve-hit signal: the fill path emits NO hit sighting (so a
/// fill-and-serve miss never double-counts), and each served request emits
/// exactly one sighting through the serve chokepoint's
/// [`CacheEngine::observe_hit`]. A hot RESIDENT blob served repeatedly must
/// therefore accumulate frequency and become promotable — the case that was
/// inverted before serve paths emitted the signal.
#[tokio::test]
async fn serve_hit_signal_fires_once_per_serve_and_promotes_a_hot_resident_blob()
-> anyhow::Result<()> {
    use crate::policy::{FrequencyEstimator, ProbationAdmission, Segment, TinyLfuEstimator};

    let tmp = tempfile::tempdir()?;
    let payload = b"hot resident blob";
    let hash = Hash::new(payload);
    let engine = CacheEngine::open(
        tmp.path(),
        vec![Arc::new(StubOrigin::new(payload)) as Arc<dyn Origin>],
        10,
    )
    .await?;

    let promotion_threshold = 3u32;
    let freq: Arc<dyn FrequencyEstimator> = Arc::new(TinyLfuEstimator::new(4096));
    engine.set_frequency_estimator(freq.clone());
    engine.set_admission_policy(Arc::new(ProbationAdmission {
        freq: freq.clone(),
        promotion_threshold,
    }));

    // Fill as a miss. The fill path reads the estimate for admission (0 ->
    // Probation) but must NOT emit the hit signal, so estimate stays 0 — this
    // is what keeps a fill-and-serve miss at one sighting, not two.
    engine.populate_local(hash).await?;
    anyhow::ensure!(
        freq.estimate(hash) == 0,
        "the fill alone must not observe (no double-count with the serve)"
    );
    anyhow::ensure!(
        engine.segment_of(hash) == Segment::Probation,
        "first sight lands in probation"
    );

    // Serve the resident blob repeatedly. Each served request is exactly one
    // sighting via the serve chokepoint's `observe_hit`.
    for i in 1..=promotion_threshold {
        engine.observe_hit(hash);
        anyhow::ensure!(
            freq.estimate(hash) == i,
            "each serve must be exactly one sighting"
        );
    }
    anyhow::ensure!(
        freq.estimate(hash) >= promotion_threshold,
        "a hot resident blob served repeatedly must become promotable"
    );
    Ok(())
}

#[tokio::test]
async fn default_admission_is_main_segment() -> anyhow::Result<()> {
    let tmp = tempfile::tempdir()?;
    let payload = b"hello admission";
    let hash = Hash::new(payload);
    let origin = StubOrigin::new(payload);

    let engine =
        CacheEngine::open(tmp.path(), vec![Arc::new(origin) as Arc<dyn Origin>], 10).await?;

    let ctx = crate::policy::AdmissionContext {
        hash,
        known_size: None,
    };
    assert_eq!(
        engine.admission_segment_for_test(&ctx),
        crate::policy::Segment::Main
    );
    Ok(())
}

#[tokio::test]
async fn access_times_snapshot_contains_accessed_hash() -> anyhow::Result<()> {
    let tmp = tempfile::tempdir()?;
    let payload = b"hello snapshot";
    let hash = Hash::new(payload);
    let origin = StubOrigin::new(payload);

    let engine =
        CacheEngine::open(tmp.path(), vec![Arc::new(origin) as Arc<dyn Origin>], 10).await?;

    let _ = engine.get(hash).await?;

    let snap = engine.access_times_snapshot();
    anyhow::ensure!(
        snap.contains_key(&hash),
        "snapshot should contain the accessed hash"
    );
    Ok(())
}

/// A serve completion's access record must not wait on a scan of the rest of
/// the access map.
///
/// `observe_hit` -> `record_access` is the terminal bookkeeping of every
/// completed serve, and `eviction_candidates` walks every entry on the
/// eviction sweep. The map is sharded, so the two meet on one shard at a
/// time: with a scan pinned inside one shard, a record for a hash that lives
/// in another shard still lands. Under one map-wide lock that record waits
/// for the whole scan, and the bounded wait below expires.
#[tokio::test]
async fn record_access_does_not_wait_on_a_scan_of_another_shard() -> anyhow::Result<()> {
    let tmp = tempfile::tempdir()?;
    let engine = CacheEngine::open(tmp.path(), Vec::new(), 10).await?;

    // Stand in for a scan sitting inside one shard by holding a guard on
    // that shard. `get_mut` takes the write guard rather than the read guard
    // `eviction_candidates`' walk takes, which is the stronger hold: if a
    // record can land against an exclusive guard on another shard, it can
    // land against a shared one. What is pinned is the shard, which is the
    // property under test.
    let scanned = Hash::new(b"the shard under scan");
    engine.inner.access_times.insert(scanned, Instant::now());
    let Some(scan_guard) = engine.inner.access_times.get_mut(&scanned) else {
        anyhow::bail!("the seeded access-time entry must be present");
    };

    // Find a hash that lives outside the pinned shard: `try_get` reports
    // `Locked` for that shard alone and `Absent` for every other one.
    let completing = (0..1024u32).map(|i| Hash::new(i.to_le_bytes())).find(|h| {
        !matches!(
            engine.inner.access_times.try_get(h),
            dashmap::try_result::TryResult::Locked
        )
    });
    let Some(completing) = completing else {
        anyhow::bail!("no candidate hash landed outside the pinned shard");
    };

    // Record the completion from another thread so a wait is observable as a
    // timeout rather than a hung test.
    let (tx, rx) = std::sync::mpsc::channel::<()>();
    let recorder = {
        let engine = engine.clone();
        std::thread::spawn(move || {
            engine.observe_hit(completing);
            let _ = tx.send(());
        })
    };
    let landed = rx.recv_timeout(Duration::from_secs(10)).is_ok();

    // Release the scan and reap the recorder before asserting, so a failure
    // reports rather than leaks the thread.
    drop(scan_guard);
    let joined = recorder.join().is_ok();

    anyhow::ensure!(
        landed,
        "a serve completion's access record waited on a scan of another shard"
    );
    anyhow::ensure!(joined, "the recording thread panicked");
    anyhow::ensure!(
        engine.last_accessed(completing).is_some(),
        "the recorded access must be readable once the scan releases"
    );
    Ok(())
}

/// A concurrent record must not make the eviction sweep *lose* a candidate.
///
/// The sharded walk is not point-in-time, by design: a record landing
/// mid-walk may or may not appear. What it must never do is drop a
/// hash that was already in the map when the walk started, because
/// `eviction_candidates` is the only source of eviction candidates — a hash
/// silently skipped by every sweep is a blob that is never reclaimed, which
/// is unbounded disk growth. `DashMap::iter` holds each shard's read guard
/// for that shard's traversal, so a concurrent insert can add to a shard the
/// walk has not reached but cannot remove from one it has. This pins that.
///
/// A round only counts once its result carries a hash the writer produced
/// *after the walk began* — the writer's counter is sampled either side of
/// the walk, and only that window's keys are accepted as witnesses. A key
/// written before the walk started proves nothing: the walk would find it in
/// a quiet map too. Scheduling decides whether a given round lands one, so
/// rounds repeat until one does; the no-candidate-lost invariant is checked
/// on every round either way.
///
/// Each round writes into its own [`ROUND_STRIDE`]-wide key range, and the
/// range is asserted wide enough to hold that round's writes. Sharing one
/// range would make the witness vacuous from round two on: nothing evicts
/// the previous round's keys from `access_times`, so every later round would
/// "find" keys that were already there before its walk started, and a run
/// where the walk never overlapped the writer would report success.
#[tokio::test]
async fn a_scan_never_loses_a_candidate_to_a_concurrent_record() -> anyhow::Result<()> {
    const WRITER_BASE: u32 = 1_000_000;
    /// Keys per round. Rounds must not share keys (see the docstring), and
    /// a round that outran this would start reusing the next round's range.
    const ROUND_STRIDE: u32 = 10_000_000;
    let tmp = tempfile::tempdir()?;
    let engine = CacheEngine::open(tmp.path(), Vec::new(), 10).await?;

    // Seed enough hashes to spread across every shard and to give the walk
    // enough work that a concurrent writer can get inside it.
    let seeded: Vec<Hash> = (0..4096u32).map(|i| Hash::new(i.to_le_bytes())).collect();
    for h in &seeded {
        engine.inner.access_times.insert(*h, Instant::now());
    }

    let mut overlapped = false;
    for round in 0..16u32 {
        let base = WRITER_BASE.saturating_add(round.saturating_mul(ROUND_STRIDE));
        // Hammer the map with fresh hashes for the duration of the walk.
        let stop = Arc::new(AtomicBool::new(false));
        let written = Arc::new(AtomicU64::new(0));
        let writer = {
            let engine = engine.clone();
            let stop = Arc::clone(&stop);
            let written = Arc::clone(&written);
            std::thread::spawn(move || {
                let mut i = base;
                while !stop.load(Ordering::Relaxed) {
                    engine.observe_hit(Hash::new(i.to_le_bytes()));
                    written.fetch_add(1, Ordering::Relaxed);
                    i = i.saturating_add(1);
                }
            })
        };

        // Do not start the walk until the writer is provably running, or the
        // scan can finish before the thread is even scheduled. Bounded, so a
        // writer that dies before its first record fails the test instead of
        // hanging it with the panic trapped in an unjoined thread.
        let spin_deadline = Instant::now() + Duration::from_secs(10);
        while written.load(Ordering::Relaxed) == 0 {
            anyhow::ensure!(
                Instant::now() < spin_deadline,
                "the recording thread never recorded an access"
            );
            std::thread::yield_now();
        }

        // The writer bumps its counter *after* the insert lands, so a key
        // index below `before` is certainly already in the map and one at or
        // above `after` is certainly not yet. Index `before` itself is the
        // ambiguous one, which is why the witness range below skips it.
        let before = written.load(Ordering::Relaxed);
        let candidates = engine.eviction_candidates();
        let after = written.load(Ordering::Relaxed);
        stop.store(true, Ordering::Relaxed);
        anyhow::ensure!(writer.join().is_ok(), "the recording thread panicked");

        let missing = seeded
            .iter()
            .filter(|h| !candidates.contains_key(h))
            .count();
        anyhow::ensure!(
            missing == 0,
            "the sweep dropped {missing} of {} pre-existing candidates",
            seeded.len()
        );

        anyhow::ensure!(
            after < u64::from(ROUND_STRIDE),
            "round wrote {after} keys, overrunning its {ROUND_STRIDE}-key range"
        );

        // Did this round's walk actually see a record that landed inside it?
        // The writer bumps its counter *after* the insert lands, so at the
        // instant `before` was read the key at index `before` may already be
        // in the map — skip it and start at the first index that cannot be.
        let during: HashSet<Hash> = (before.saturating_add(1)..after)
            .filter_map(|n| u32::try_from(n).ok())
            .map(|n| Hash::new(base.saturating_add(n).to_le_bytes()))
            .collect();
        overlapped |= candidates.iter().any(|(h, _)| during.contains(h));
        if overlapped {
            break;
        }
    }
    anyhow::ensure!(
        overlapped,
        "no round overlapped the writer, so the walk was never concurrent"
    );
    Ok(())
}

#[tokio::test]
async fn last_accessed_returns_none_for_unknown_hash() -> anyhow::Result<()> {
    let tmp = tempfile::tempdir()?;
    let engine = CacheEngine::open(tmp.path(), Vec::new(), 10).await?;
    let unknown = Hash::new(b"never accessed");

    anyhow::ensure!(
        engine.last_accessed(unknown).is_none(),
        "expected None for a hash that was never accessed"
    );
    Ok(())
}

#[tokio::test]
async fn iter_hashes_returns_empty_on_empty_store() -> anyhow::Result<()> {
    let tmp = tempfile::tempdir()?;
    let engine = CacheEngine::open(tmp.path(), Vec::new(), 10).await?;
    let hashes = engine.iter_hashes().await?;
    anyhow::ensure!(
        hashes.is_empty(),
        "expected empty iter_hashes on a fresh store, got {hashes:?}"
    );
    Ok(())
}

#[tokio::test]
async fn iter_hashes_returns_all_committed_blobs() -> anyhow::Result<()> {
    let tmp = tempfile::tempdir()?;
    let payloads: &[&[u8]] = &[b"iter-a", b"iter-b", b"iter-c"];
    let origin = MultiStubOrigin::new(payloads);

    let engine =
        CacheEngine::open(tmp.path(), vec![Arc::new(origin) as Arc<dyn Origin>], 10).await?;

    let mut expected: HashSet<Hash> = HashSet::new();
    for p in payloads {
        let h = Hash::new(*p);
        let _ = engine.get(h).await?;
        expected.insert(h);
    }

    // Use a HashSet for the comparison — iroh-blobs `list()` order is
    // not contractually stable.
    let actual: HashSet<Hash> = engine.iter_hashes().await?.into_iter().collect();
    anyhow::ensure!(actual == expected, "expected {expected:?}, got {actual:?}");
    Ok(())
}

#[tokio::test]
async fn iter_hashes_excludes_evicted_blobs() -> anyhow::Result<()> {
    let tmp = tempfile::tempdir()?;
    let payloads: &[&[u8]] = &[b"evict-a", b"evict-b", b"evict-c", b"evict-d"];
    let origin = MultiStubOrigin::new(payloads);

    let engine =
        CacheEngine::open(tmp.path(), vec![Arc::new(origin) as Arc<dyn Origin>], 10).await?;

    let hashes: Vec<Hash> = payloads.iter().map(|p| Hash::new(*p)).collect();
    for h in &hashes {
        let _ = engine.get(*h).await?;
    }

    // Evict half — odd indices.
    let mut kept: Vec<Hash> = Vec::new();
    let mut evicted: Vec<Hash> = Vec::new();
    for (i, h) in hashes.iter().enumerate() {
        if i % 2 == 0 {
            kept.push(*h);
        } else {
            evicted.push(*h);
        }
    }
    for h in &evicted {
        engine.evict(*h).await?;
    }

    let actual: HashSet<Hash> = engine.iter_hashes().await?.into_iter().collect();
    let kept_set: HashSet<Hash> = kept.iter().copied().collect();

    anyhow::ensure!(
        actual == kept_set,
        "iter_hashes must return exactly the non-evicted set; expected {kept_set:?}, got {actual:?}"
    );
    Ok(())
}

/// Pinned blobs MUST appear in `iter_hashes`: pinning protects against
/// LRU eviction, not against DHT republish. They're the most valuable
/// content to surface, so they have to seed the cold-start scheduler
/// alongside everything else. Locks the contract against a future
/// "exclude pinned for symmetry with `access_times_snapshot`" refactor.
#[tokio::test]
async fn iter_hashes_includes_pinned_blobs() -> anyhow::Result<()> {
    let tmp = tempfile::tempdir()?;
    let payload_pinned: &[u8] = b"pin-a";
    let payload_plain: &[u8] = b"pin-b";
    let origin = MultiStubOrigin::new(&[payload_pinned, payload_plain]);

    let engine =
        CacheEngine::open(tmp.path(), vec![Arc::new(origin) as Arc<dyn Origin>], 10).await?;

    let h_pinned = Hash::new(payload_pinned);
    let h_plain = Hash::new(payload_plain);
    let _ = engine.get(h_pinned).await?;
    let _ = engine.get(h_plain).await?;

    let s = [from_store_hash(h_pinned)].into_iter().collect();
    let diff = engine.set_pinned(&PinnedHashes::new(s));
    // Without this guard, a future bug where set_pinned silently
    // no-ops would let the test pass on the strength of pre-pin
    // presence alone.
    anyhow::ensure!(
        diff.added == 1 && diff.removed == 0,
        "set_pinned must apply the pin to make this test meaningful, got {diff:?}"
    );

    let actual: HashSet<Hash> = engine.iter_hashes().await?.into_iter().collect();
    let expected: HashSet<Hash> = [h_pinned, h_plain].into_iter().collect();
    anyhow::ensure!(
        actual == expected,
        "iter_hashes must include pinned blobs (they're the highest-value DHT advertisements); expected {expected:?}, got {actual:?}"
    );
    Ok(())
}

/// An origin that sleeps before returning, counting how many times
/// `fetch` was invoked. Used to verify coalescing of concurrent pulls.
#[derive(Debug)]
struct SlowCountingOrigin {
    data: Bytes,
    hash: Hash,
    fetch_count: AtomicUsize,
    delay: std::time::Duration,
}

impl SlowCountingOrigin {
    fn new(payload: &[u8], delay: std::time::Duration) -> Self {
        Self {
            hash: Hash::new(payload),
            data: Bytes::from(payload.to_vec()),
            fetch_count: AtomicUsize::new(0),
            delay,
        }
    }
}

impl Origin for SlowCountingOrigin {
    fn kind(&self) -> OriginKind {
        OriginKind::Http
    }

    fn fetch(
        &self,
        hash: Hash,
        _max_bytes: u64,
    ) -> Pin<Box<dyn Future<Output = Result<OriginFetch, crate::OriginPullError>> + Send + '_>>
    {
        self.fetch_count.fetch_add(1, Ordering::SeqCst);
        let result = if hash == self.hash {
            Ok(OriginFetch::found_one_shot(self.data.clone()))
        } else {
            Ok(OriginFetch::NotFound)
        };
        let delay = self.delay;
        Box::pin(async move {
            tokio::time::sleep(delay).await;
            result
        })
    }
}

#[tokio::test]
async fn concurrent_gets_coalesce_into_single_origin_fetch() -> anyhow::Result<()> {
    let tmp = tempfile::tempdir()?;
    let payload = b"coalesce me";
    let hash = Hash::new(payload);
    let origin = Arc::new(SlowCountingOrigin::new(
        payload,
        std::time::Duration::from_millis(50),
    ));

    let cm = Arc::new(CacheMetrics::default());
    let engine = CacheEngine::open_full(
        tmp.path(),
        vec![origin.clone() as Arc<dyn Origin>],
        10,
        crate::PinnedHashes::empty(),
        crate::RetryPolicy::default(),
        CircuitBreakerPolicy::default(),
        Some(Arc::clone(&cm)),
        Duration::ZERO,
    )
    .await?;

    // Spawn several concurrent gets for the same hash.
    let mut handles = Vec::new();
    for _ in 0..5 {
        let e = engine.clone();
        handles.push(tokio::spawn(async move { e.get(hash).await }));
    }

    // Await all — they should all succeed.
    for handle in handles {
        let result = handle
            .await
            .map_err(|e| anyhow::anyhow!("task join: {e}"))?;
        anyhow::ensure!(result.is_ok(), "expected Ok, got {result:?}");
    }

    // The origin should have been called at most once (coalesced).
    let count = origin.fetch_count.load(Ordering::SeqCst);
    anyhow::ensure!(count == 1, "expected exactly 1 origin fetch, got {count}");

    // Counter accounting under coalescing (#418): exactly one task
    // becomes the owner and counts as a miss; the other four are
    // waiter-retry hits. A regression that moved the waiter-hit
    // bump out of `engine.rs::get`'s waiter branch would silently
    // mis-classify every coalesced workload as a miss-storm.
    anyhow::ensure!(
        cm.misses.get() == 1,
        "owner pulls once → exactly 1 miss, got {}",
        cm.misses.get()
    );
    anyhow::ensure!(
        cm.hits.get() == 4,
        "4 waiters retry into a hit, got {} hits",
        cm.hits.get()
    );
    anyhow::ensure!(
        cm.pull_through_bytes.get() == payload.len() as u64,
        "single origin fetch → pull_through_bytes == payload.len()"
    );
    anyhow::ensure!(
        cm.bytes_returned.get() == (payload.len() as u64) * 5,
        "all 5 callers got the bytes back, so bytes_returned == 5 * payload.len()"
    );
    Ok(())
}

#[tokio::test]
async fn inflight_map_is_empty_after_pull_completes() -> anyhow::Result<()> {
    let tmp = tempfile::tempdir()?;
    let payload = b"cleanup check";
    let hash = Hash::new(payload);
    let origin = StubOrigin::new(payload);

    let engine =
        CacheEngine::open(tmp.path(), vec![Arc::new(origin) as Arc<dyn Origin>], 10).await?;

    let _ = engine.get(hash).await?;

    let inflight_len = inflight_len(&engine);
    anyhow::ensure!(
        inflight_len == 0,
        "inflight map should be empty after pull, had {inflight_len} entries"
    );
    Ok(())
}

/// Cancelling the owner mid-pull must not leave the inflight entry
/// orphaned — otherwise every subsequent `get()` for the same hash hangs
/// on a `Notify` that never fires. The `InflightGuard`'s `Drop` impl
/// wakes waiters and clears the entry even on cancellation.
#[tokio::test]
async fn cancelled_owner_does_not_orphan_inflight_entry() -> anyhow::Result<()> {
    let tmp = tempfile::tempdir()?;
    let payload = b"cancel test";
    let hash = Hash::new(payload);
    let origin = SlowCountingOrigin::new(payload, std::time::Duration::from_secs(10));

    let engine =
        CacheEngine::open(tmp.path(), vec![Arc::new(origin) as Arc<dyn Origin>], 10).await?;

    // Spawn the owner with a tiny timeout so it gets cancelled mid-pull.
    let owner_engine = engine.clone();
    let owner = tokio::spawn(async move {
        tokio::time::timeout(std::time::Duration::from_millis(50), owner_engine.get(hash)).await
    });
    // Wait for the owner task to finish (timeout fires → future dropped).
    let _ = owner.await?;

    // The inflight map must be empty — InflightGuard::drop ran on cancel.
    let inflight_len = inflight_len(&engine);
    anyhow::ensure!(
        inflight_len == 0,
        "inflight map should be empty after cancellation, had {inflight_len} entries"
    );
    Ok(())
}

// ----- In-flight mutex poisoning (#1517) -----

/// Poison `inner.inflight` the only way a `std::sync::Mutex` can be
/// poisoned: panic while holding the guard.
///
/// This is synthetic by construction — the workspace anti-panic policy
/// (`unwrap_used` / `expect_used` / `panic` denied) is precisely why no
/// production path can do this, and precisely why a real firing would be
/// a bug worth an `error!` rather than a condition worth tuning.
#[allow(clippy::panic)]
fn poison_inflight(engine: &CacheEngine) {
    let inner = Arc::clone(&engine.inner);
    let handle = std::thread::spawn(move || {
        let _guard = inner.inflight.lock();
        panic!("deliberately poisoning the inflight mutex");
    });
    // Panicked by construction, so `join` returns the panic payload.
    drop(handle.join());
}

/// Read the map length without laundering poison into a false `0` —
/// `lock().ok()` would report an empty map on a poisoned mutex and make
/// the orphan assertion below vacuously true.
fn inflight_len(engine: &CacheEngine) -> usize {
    engine
        .inner
        .inflight
        .lock()
        .unwrap_or_else(PoisonError::into_inner)
        .len()
}

/// The load-bearing half of #1517: a poisoned coalescing mutex must not
/// cost origin egress. Before the fix both `get` call sites discarded the
/// `PoisonError` and fell through to a *direct* pull, and std poison is
/// sticky for the process lifetime — so one panic permanently turned every
/// concurrent request for a missing blob into its own origin fetch. On a
/// metered `http`/`s3` origin that is an unbounded egress multiplier; on
/// the `Peer` origin reached via `populate` it is a USDC double-spend.
///
/// `fetch_count == 1` under a poisoned mutex is the whole assertion.
#[tokio::test]
async fn poisoned_inflight_mutex_still_coalesces_and_is_counted() -> anyhow::Result<()> {
    let tmp = tempfile::tempdir()?;
    let payload = b"coalesce under poison";
    let hash = Hash::new(payload);
    let origin = Arc::new(SlowCountingOrigin::new(
        payload,
        std::time::Duration::from_millis(50),
    ));

    let cm = Arc::new(CacheMetrics::default());
    let engine = CacheEngine::open_full(
        tmp.path(),
        vec![origin.clone() as Arc<dyn Origin>],
        10,
        crate::PinnedHashes::empty(),
        crate::RetryPolicy::default(),
        CircuitBreakerPolicy::default(),
        Some(Arc::clone(&cm)),
        Duration::ZERO,
    )
    .await?;

    poison_inflight(&engine);

    let mut handles = Vec::new();
    for _ in 0..5 {
        let e = engine.clone();
        handles.push(tokio::spawn(async move { e.get(hash).await }));
    }
    for handle in handles {
        let result = handle
            .await
            .map_err(|e| anyhow::anyhow!("task join: {e}"))?;
        anyhow::ensure!(result.is_ok(), "expected Ok, got {result:?}");
    }

    let count = origin.fetch_count.load(Ordering::SeqCst);
    anyhow::ensure!(
        count == 1,
        "coalescing must survive poison — expected exactly 1 origin fetch, got {count}"
    );
    // Exactly one: `lock_inflight` clears the poison, so a single
    // panic is a single bump no matter how many locks follow it.
    // Asserting the exact value is what pins that — `> 0` would pass
    // just as happily if the clear were dropped and the counter
    // climbed with request volume.
    let bumps = cm.inflight_mutex_poisoned.get();
    anyhow::ensure!(
        bumps == 1,
        "one poisoning must count exactly once, got {bumps}"
    );
    anyhow::ensure!(
        inflight_len(&engine) == 0,
        "the owner's guard must still clear its entry under poison"
    );
    Ok(())
}

/// Two separate poisonings count twice while the log stays latched at one.
///
/// Two poisonings, not one, is the whole point: because `lock_inflight`
/// clears the poison, a single panic yields a single bump, so only a
/// *second* panic can show the counter advancing past the latch. This is
/// also the test that pins "counts poisonings, not locks-since-a-poisoning"
/// — drop the `clear_poison` and the first `get` alone drives the counter
/// to 2, which the exact-value assertions below reject.
///
/// The latch itself is asserted through the private `AtomicBool` rather
/// than by capturing log output: no crate in the workspace carries a
/// `tracing` capture layer in dev-deps, and that is a weak proxy — it
/// cannot catch a mutation that logs unconditionally *and* sets the flag.
/// Recorded rather than papered over; the counter assertions are the ones
/// carrying real weight here.
#[tokio::test]
async fn separate_poisonings_each_count_while_the_log_latches() -> anyhow::Result<()> {
    let tmp = tempfile::tempdir()?;
    let payload = b"latch check";
    let hash = Hash::new(payload);
    let origin = StubOrigin::new(payload);

    let cm = Arc::new(CacheMetrics::default());
    let engine = CacheEngine::open_full(
        tmp.path(),
        vec![Arc::new(origin) as Arc<dyn Origin>],
        10,
        crate::PinnedHashes::empty(),
        crate::RetryPolicy::default(),
        CircuitBreakerPolicy::default(),
        Some(Arc::clone(&cm)),
        Duration::ZERO,
    )
    .await?;

    poison_inflight(&engine);

    anyhow::ensure!(
        !engine.inner.inflight_poison_logged.load(Ordering::Relaxed),
        "the latch must not be set before any lock is taken"
    );

    // One miss takes the lock twice (claim the entry, then release it in
    // `InflightGuard::drop`) but sees the poison only on the first, since
    // that lock clears it.
    let _ = engine.get(hash).await?;
    let after_first = cm.inflight_mutex_poisoned.get();
    anyhow::ensure!(
        after_first == 1,
        "one poisoning, one bump — the poison must have been cleared; got {after_first}"
    );
    anyhow::ensure!(
        engine.inner.inflight_poison_logged.load(Ordering::Relaxed),
        "the first poisoned lock must latch the log"
    );

    // A second, independent panic. A second `get` would add nothing on its
    // own — it is a cache hit and returns above the coalescing loop without
    // locking at all — so poison directly and take one more lock.
    poison_inflight(&engine);
    drop(engine.inner.lock_inflight());
    let after_second = cm.inflight_mutex_poisoned.get();

    anyhow::ensure!(
        after_second == 2,
        "the counter must count the second poisoning too, got {after_second}"
    );
    anyhow::ensure!(
        engine.inner.inflight_poison_logged.load(Ordering::Relaxed),
        "the latch must stay set — the second poisoning must not re-log"
    );
    Ok(())
}

/// `populate_inner`'s coalescing loop is a near-verbatim *copy* of `get`'s,
/// not a shared helper, and the poison handling #1517 requires lives in each
/// copy separately, so each copy can regress on its own. Covering `get`
/// therefore does not cover this, and a mutation that reverts only
/// `populate_inner` survives a `get`-only suite.
///
/// This is also the copy that matters most: `populate` (unlike
/// `populate_local`) walks the `Peer` origin, so a lost claim here is the
/// duplicate *paid* upstream pull — the USDC double-spend #1517 names.
/// `get`, by contrast, has no production caller in the daemon; the serve
/// path fills via `populate`/`populate_local`.
#[tokio::test]
async fn poisoned_populate_still_coalesces_into_one_fill() -> anyhow::Result<()> {
    let tmp = tempfile::tempdir()?;
    let payload = b"populate under poison";
    let hash = Hash::new(payload);
    let origin = Arc::new(SlowCountingOrigin::new(
        payload,
        std::time::Duration::from_millis(50),
    ));

    let cm = Arc::new(CacheMetrics::default());
    let engine = CacheEngine::open_full(
        tmp.path(),
        vec![origin.clone() as Arc<dyn Origin>],
        10,
        crate::PinnedHashes::empty(),
        crate::RetryPolicy::default(),
        CircuitBreakerPolicy::default(),
        Some(Arc::clone(&cm)),
        Duration::ZERO,
    )
    .await?;

    poison_inflight(&engine);

    let mut handles = Vec::new();
    for _ in 0..5 {
        let e = engine.clone();
        handles.push(tokio::spawn(async move { e.populate(hash).await }));
    }
    for handle in handles {
        let result = handle
            .await
            .map_err(|e| anyhow::anyhow!("task join: {e}"))?;
        anyhow::ensure!(result.is_ok(), "expected Ok, got {result:?}");
    }

    let count = origin.fetch_count.load(Ordering::SeqCst);
    anyhow::ensure!(
        count == 1,
        "populate must coalesce under poison — expected 1 origin fetch, got {count}"
    );
    let bumps = cm.inflight_mutex_poisoned.get();
    anyhow::ensure!(bumps == 1, "one poisoning, one bump; got {bumps}");
    anyhow::ensure!(inflight_len(&engine) == 0, "the claim must be released");
    Ok(())
}

/// The drop-path half of #1517. If `InflightGuard::drop` swallowed the
/// `PoisonError` and skipped the removal, then — since `notify_waiters`
/// only wakes *current* waiters — the leaked entry would make every later
/// request for that hash park on a `Notify` that never fires again — a
/// permanent hang, which is the exact failure the guard exists to prevent.
///
/// The unpoisoned cancellation case is covered by
/// [`cancelled_owner_does_not_orphan_inflight_entry`]; this is its poisoned
/// twin, and it reads the map with `into_inner` so poison cannot fake a
/// pass.
#[tokio::test]
async fn poisoned_mutex_does_not_orphan_inflight_entry() -> anyhow::Result<()> {
    let tmp = tempfile::tempdir()?;
    let payload = b"poisoned cancel";
    let hash = Hash::new(payload);
    let origin_handle = Arc::new(SlowCountingOrigin::new(
        payload,
        std::time::Duration::from_secs(10),
    ));

    let engine = CacheEngine::open(
        tmp.path(),
        vec![origin_handle.clone() as Arc<dyn Origin>],
        10,
    )
    .await?;

    poison_inflight(&engine);

    let owner_engine = engine.clone();
    let owner = tokio::spawn(async move {
        tokio::time::timeout(std::time::Duration::from_millis(50), owner_engine.get(hash)).await
    });
    let _ = owner.await?;

    // Prove the owner actually claimed the entry before asserting it was
    // released. Without this the test passes vacuously whenever the 50 ms
    // timeout lands before the pull starts — `get` does `refuses()` and
    // `has()` (fs store I/O) first — and an empty map then proves nothing,
    // even under a leaking drop impl (#1517). A loaded CI box makes that a
    // silent false pass rather than a visible flake.
    let claimed = origin_handle.fetch_count.load(Ordering::SeqCst);
    anyhow::ensure!(
        claimed == 1,
        "owner must have reached the pull (and so held the entry), got {claimed} fetches"
    );

    // An empty map is the assertion proper, and it reads through
    // `into_inner` so poison cannot fake it. Under a leaking drop impl (#1517)
    // the entry survives here and `len == 1`. There is deliberately no
    // "now issue another get and time it" probe: this origin sleeps for ten
    // seconds by design, so any such timeout would measure the stub rather
    // than the orphaned `Notify`.
    let len = inflight_len(&engine);
    anyhow::ensure!(
        len == 0,
        "InflightGuard::drop must clear the entry under poison, had {len}"
    );
    Ok(())
}

// ----- Pinning (#276) -----

#[tokio::test]
async fn pinned_hash_is_excluded_from_eviction_candidates() -> anyhow::Result<()> {
    let tmp = tempfile::tempdir()?;
    let pinned_payload = b"pinned blob";
    let evictable_payload = b"evictable blob";
    let pinned_hash = Hash::new(pinned_payload);
    let evictable_hash = Hash::new(evictable_payload);

    // Build the engine with pinned_hash in the pinning set. The
    // leaf `PinnedHashes` is keyed on the config-vocabulary hash, so
    // convert the store hash at the boundary (#578) — this also
    // regression-covers the leaf↔store conversion in `open_full`.
    let pinned_set = [from_store_hash(pinned_hash)].into_iter().collect();
    let engine =
        CacheEngine::open_with_pinned(tmp.path(), Vec::new(), 10, PinnedHashes::new(pinned_set))
            .await?;

    // Touch both hashes via direct access-time insertion (we don't
    // need actual blob content for this test).
    engine
        .inner
        .access_times
        .insert(pinned_hash, Instant::now());
    engine
        .inner
        .access_times
        .insert(evictable_hash, Instant::now());

    let raw = engine.access_times_snapshot();
    anyhow::ensure!(raw.len() == 2, "raw snapshot must include pinned");

    let candidates = engine.eviction_candidates();
    anyhow::ensure!(
        candidates.len() == 1,
        "candidates should exclude pinned, got {} entries",
        candidates.len()
    );
    anyhow::ensure!(
        candidates.contains_key(&evictable_hash),
        "evictable hash should be a candidate"
    );
    anyhow::ensure!(
        !candidates.contains_key(&pinned_hash),
        "pinned hash must NOT be a candidate"
    );
    anyhow::ensure!(engine.is_pinned(pinned_hash));
    anyhow::ensure!(!engine.is_pinned(evictable_hash));
    Ok(())
}

/// A pinned hash that is ALSO governance-denied stays an eviction candidate —
/// "deny wins over pin" on the LRU path, not only the takedown `evict()`. A
/// pinned clean hash is still excluded. The pin flag itself is unchanged.
#[tokio::test]
async fn deny_listed_pinned_hash_is_an_eviction_candidate() -> anyhow::Result<()> {
    let tmp = tempfile::tempdir()?;
    let clean_hash = Hash::new(b"pinned clean blob");
    let denied_hash = Hash::new(b"pinned denied blob");

    let pinned_set = [from_store_hash(clean_hash), from_store_hash(denied_hash)]
        .into_iter()
        .collect();
    let engine =
        CacheEngine::open_with_pinned(tmp.path(), Vec::new(), 10, PinnedHashes::new(pinned_set))
            .await?;
    engine.inner.access_times.insert(clean_hash, Instant::now());
    engine
        .inner
        .access_times
        .insert(denied_hash, Instant::now());

    anyhow::ensure!(
        engine.set_chain_denied_one(denied_hash, true),
        "deny must change the set"
    );

    let candidates = engine.eviction_candidates();
    anyhow::ensure!(
        candidates.contains_key(&denied_hash),
        "a pinned + governance-denied hash must remain an eviction candidate"
    );
    anyhow::ensure!(
        !candidates.contains_key(&clean_hash),
        "a pinned clean hash must still be excluded"
    );
    anyhow::ensure!(
        engine.is_pinned(denied_hash) && engine.is_pinned(clean_hash),
        "the pin flag itself is unchanged — only the eviction carve-out differs"
    );
    Ok(())
}

/// The carve-out fires for the LOCAL (`content.denied_hashes` → `set_denied`)
/// deny half too, not only the on-chain half — the `is_denied` side of the
/// `is_denied || is_chain_denied` disjunction in `eviction_candidates`.
#[tokio::test]
async fn local_denied_pinned_hash_is_also_an_eviction_candidate() -> anyhow::Result<()> {
    let tmp = tempfile::tempdir()?;
    let hash = Hash::new(b"pinned + locally denied");
    let engine = CacheEngine::open_with_pinned(
        tmp.path(),
        Vec::new(),
        10,
        PinnedHashes::new([from_store_hash(hash)].into_iter().collect()),
    )
    .await?;
    engine.inner.access_times.insert(hash, Instant::now());
    engine.set_denied(&denied(&[hash]));
    anyhow::ensure!(
        engine.eviction_candidates().contains_key(&hash),
        "a pinned + locally-denied hash must also be an eviction candidate"
    );
    Ok(())
}

#[tokio::test]
async fn set_pinned_atomically_updates_filter() -> anyhow::Result<()> {
    let tmp = tempfile::tempdir()?;
    let h1 = Hash::new(b"one");
    let h2 = Hash::new(b"two");

    let engine = CacheEngine::open(tmp.path(), Vec::new(), 10).await?;
    engine.inner.access_times.insert(h1, Instant::now());
    engine.inner.access_times.insert(h2, Instant::now());

    // No pinning yet — both candidates.
    anyhow::ensure!(engine.eviction_candidates().len() == 2);

    // Pin h1 (convert the store hash to the leaf config-vocabulary
    // hash at the boundary, #578).
    let s = [from_store_hash(h1)].into_iter().collect();
    let diff = engine.set_pinned(&PinnedHashes::new(s));
    anyhow::ensure!(
        diff.added == 1 && diff.removed == 0,
        "expected diff (added=1, removed=0), got {diff:?}"
    );

    let candidates = engine.eviction_candidates();
    anyhow::ensure!(candidates.len() == 1, "h1 should now be excluded");
    anyhow::ensure!(candidates.contains_key(&h2));

    // Replace with empty set — h1 becomes a candidate again.
    let diff2 = engine.set_pinned(&PinnedHashes::empty());
    anyhow::ensure!(
        diff2.added == 0 && diff2.removed == 1,
        "expected diff (added=0, removed=1), got {diff2:?}"
    );
    anyhow::ensure!(engine.eviction_candidates().len() == 2);
    Ok(())
}

// ----- Operator-evict (#279) -----

/// `evict` must take a previously-cached hash off the served set. After
/// evict, `has` reports false and `get` returns `NotFound` rather than
/// silently re-pulling from the origin (which would defeat the point of
/// the takedown use case behind issue #279).
#[tokio::test]
async fn evict_blocks_subsequent_serve() -> anyhow::Result<()> {
    let tmp = tempfile::tempdir()?;
    let payload = b"evict me";
    let hash = Hash::new(payload);
    let origin = StubOrigin::new(payload);

    let engine =
        CacheEngine::open(tmp.path(), vec![Arc::new(origin) as Arc<dyn Origin>], 10).await?;

    // Prime the cache so we evict a real, served blob — covers the
    // hot path the operator would actually be evicting.
    let _ = engine.get(hash).await?;
    anyhow::ensure!(
        engine.has(hash).await?,
        "expected blob present before evict"
    );

    engine.evict(hash).await?;

    anyhow::ensure!(engine.is_evicted(hash), "evict flag not set");
    anyhow::ensure!(
        !engine.has(hash).await?,
        "has() should report absent after evict"
    );
    match engine.get(hash).await {
        Err(CacheError::NotFound { .. }) => Ok(()),
        other => Err(anyhow::anyhow!(
            "expected NotFound after evict, got {other:?}"
        )),
    }
}

/// Eviction must survive a process restart — operators running DMCA
/// takedowns rely on the evict being durable. The on-disk
/// `evicted.log` is replayed by a fresh `open()`. Without this test,
/// a regression that dropped the persistence path (e.g. moved to
/// in-memory-only) would let evicted content silently resume serving
/// after a restart.
#[tokio::test]
async fn evict_persists_across_open() -> anyhow::Result<()> {
    let tmp = tempfile::tempdir()?;
    let payload = b"persist me";
    let hash = Hash::new(payload);

    // First open: prime + evict.
    {
        let origin = Arc::new(StubOrigin::new(payload));
        let engine = CacheEngine::open(tmp.path(), vec![origin as Arc<dyn Origin>], 10).await?;
        let _ = engine.get(hash).await?;
        engine.evict(hash).await?;
        engine.shutdown().await?;
    }

    // Second open: same cache_dir, no origin so a re-pull would fail
    // loudly. The evicted set must reload from disk.
    let engine2 = CacheEngine::open(tmp.path(), Vec::new(), 10).await?;
    anyhow::ensure!(
        engine2.is_evicted(hash),
        "evict flag should reload from <cache_dir>/evicted.log"
    );
    anyhow::ensure!(
        !engine2.has(hash).await?,
        "has() should report absent after restart"
    );
    Ok(())
}

/// Commit every payload into a cache at `dir`, then shut the engine down so
/// the next `open` sees them as blobs already on disk.
async fn commit_then_close(dir: &Path, payloads: &[&[u8]]) -> anyhow::Result<()> {
    let origin = Arc::new(MultiStubOrigin::new(payloads)) as Arc<dyn Origin>;
    let engine = CacheEngine::open(dir, vec![origin], 10).await?;
    for payload in payloads {
        let _ = engine.get(Hash::new(payload)).await?;
    }
    engine.shutdown().await?;
    Ok(())
}

/// A blob on disk before a restart is an eviction candidate right after
/// `open`, with no traffic. Otherwise a node restarted above its size limit
/// can release only the working set it serves after the restart.
#[tokio::test]
async fn a_blob_on_disk_before_open_is_an_eviction_candidate() -> anyhow::Result<()> {
    let tmp = tempfile::tempdir()?;
    let payload: &[u8] = b"cold before restart";
    let hash = Hash::new(payload);
    commit_then_close(tmp.path(), &[payload]).await?;

    let engine = CacheEngine::open(tmp.path(), Vec::new(), 10).await?;
    anyhow::ensure!(
        engine.eviction_candidates().contains_key(&hash),
        "a pre-open blob must be an eviction candidate without a touch"
    );
    // The seeded hash's protecting tag survives the reopen, so the driver
    // can actually release it.
    anyhow::ensure!(
        engine.release_for_eviction(hash).await? > 0,
        "a seeded pre-open blob must be releasable"
    );
    anyhow::ensure!(
        !engine.eviction_candidates().contains_key(&hash),
        "a released blob must leave the candidate set"
    );
    Ok(())
}

/// An operator eviction survives a restart: the open-time seed walks only
/// non-refused hashes, so an evicted blob whose bytes are still on disk does
/// not come back as an eviction candidate.
#[tokio::test]
async fn an_evicted_pre_open_blob_is_not_seeded() -> anyhow::Result<()> {
    let tmp = tempfile::tempdir()?;
    let evicted_payload: &[u8] = b"evicted before restart";
    let kept_payload: &[u8] = b"kept across restart";
    let evicted = Hash::new(evicted_payload);
    let kept = Hash::new(kept_payload);
    {
        let origin =
            Arc::new(MultiStubOrigin::new(&[evicted_payload, kept_payload])) as Arc<dyn Origin>;
        let engine = CacheEngine::open(tmp.path(), vec![origin], 10).await?;
        let _ = engine.get(evicted).await?;
        let _ = engine.get(kept).await?;
        engine.evict(evicted).await?;
        engine.shutdown().await?;
    }

    let engine = CacheEngine::open(tmp.path(), Vec::new(), 10).await?;
    // `eviction_candidates` never consults `is_evicted`, so only the raw map
    // can show an evicted hash re-entering through the seed.
    anyhow::ensure!(
        !engine.access_times_snapshot().contains_key(&evicted),
        "an evicted hash must not enter access_times through the seed"
    );
    let candidates = engine.eviction_candidates();
    anyhow::ensure!(
        !candidates.contains_key(&evicted),
        "an evicted hash must not be a candidate"
    );
    anyhow::ensure!(
        candidates.contains_key(&kept),
        "a kept hash must still be seeded"
    );
    Ok(())
}

/// The open-time seed ranks every pre-open blob behind anything accessed
/// after open, so the LRU driver releases cold content first.
#[tokio::test]
async fn a_touched_blob_ranks_after_an_untouched_pre_open_blob() -> anyhow::Result<()> {
    use crate::policy::{EvictionContext, EvictionPolicy, LruEviction};

    let tmp = tempfile::tempdir()?;
    let cold_payload: &[u8] = b"untouched after restart";
    let hot_payload: &[u8] = b"touched after restart";
    let cold = Hash::new(cold_payload);
    let hot = Hash::new(hot_payload);
    commit_then_close(tmp.path(), &[cold_payload, hot_payload]).await?;

    let engine = CacheEngine::open(tmp.path(), Vec::new(), 10).await?;
    engine.observe_hit(hot);

    let candidates = engine.eviction_candidates();
    let cold_at = candidates
        .iter()
        .find_map(|(h, t)| (*h == cold).then_some(*t))
        .ok_or_else(|| anyhow::anyhow!("cold blob missing from candidates"))?;
    let hot_at = candidates
        .iter()
        .find_map(|(h, t)| (*h == hot).then_some(*t))
        .ok_or_else(|| anyhow::anyhow!("hot blob missing from candidates"))?;
    anyhow::ensure!(
        cold_at < hot_at,
        "the untouched pre-open blob must rank older than the touched one"
    );

    let sizes = HashMap::new();
    let segments = HashMap::new();
    let plan = LruEviction.plan(&EvictionContext {
        candidates: &candidates,
        sizes: &sizes,
        segments: &segments,
        total_bytes: 1,
        target_bytes: 0,
        budget: 1,
        cache_bytes: 0,
    });
    anyhow::ensure!(
        plan.evict == vec![cold],
        "LRU must release the untouched pre-open blob first, got {:?}",
        plan.evict
    );
    Ok(())
}

/// The open-time seed is not an access: the operator preview reports no
/// last access for a pre-open blob until something touches it.
#[tokio::test]
async fn a_seeded_blob_reports_no_last_access() -> anyhow::Result<()> {
    let tmp = tempfile::tempdir()?;
    let payload: &[u8] = b"seeded, not accessed";
    let hash = Hash::new(payload);
    commit_then_close(tmp.path(), &[payload]).await?;

    let engine = CacheEngine::open(tmp.path(), Vec::new(), 10).await?;
    anyhow::ensure!(
        engine.last_accessed(hash).is_none(),
        "a seeded entry must not read as an access"
    );
    anyhow::ensure!(
        engine.inspect(hash).await?.last_accessed_us_ago.is_none(),
        "the preview must not report the seed as an access"
    );

    engine.observe_hit(hash);
    anyhow::ensure!(
        engine.last_accessed(hash).is_some(),
        "a touch after open must read as an access"
    );
    anyhow::ensure!(
        engine.inspect(hash).await?.last_accessed_us_ago.is_some(),
        "the preview must report a touch after open"
    );
    Ok(())
}

/// Seeding does not bypass pinning: a pinned blob from before the restart
/// stays out of the candidate set.
#[tokio::test]
async fn a_pinned_pre_open_blob_is_not_a_candidate() -> anyhow::Result<()> {
    let tmp = tempfile::tempdir()?;
    let pinned_payload: &[u8] = b"pinned across restart";
    let free_payload: &[u8] = b"unpinned across restart";
    let pinned = Hash::new(pinned_payload);
    let free = Hash::new(free_payload);
    commit_then_close(tmp.path(), &[pinned_payload, free_payload]).await?;

    let engine = CacheEngine::open_with_pinned(
        tmp.path(),
        Vec::new(),
        10,
        crate::PinnedHashes::new([from_store_hash(pinned)].into_iter().collect()),
    )
    .await?;
    let candidates = engine.eviction_candidates();
    anyhow::ensure!(
        !candidates.contains_key(&pinned),
        "a pinned pre-open blob must not be an eviction candidate"
    );
    anyhow::ensure!(
        candidates.contains_key(&free),
        "an unpinned pre-open blob must still be an eviction candidate"
    );
    Ok(())
}

/// Under `TinyLfuEviction`, a blob touched after open ranks after an untouched
/// pre-open blob of equal frequency: the seed is older than any post-open
/// access.
#[tokio::test]
async fn tinylfu_ranks_a_touched_blob_after_an_untouched_pre_open_blob() -> anyhow::Result<()> {
    use crate::policy::{EvictionContext, EvictionPolicy, TinyLfuEstimator, TinyLfuEviction};

    let tmp = tempfile::tempdir()?;
    let cold_payload: &[u8] = b"tinylfu: untouched after restart";
    let hot_payload: &[u8] = b"tinylfu: touched after restart";
    let cold = Hash::new(cold_payload);
    let hot = Hash::new(hot_payload);
    commit_then_close(tmp.path(), &[cold_payload, hot_payload]).await?;

    let engine = CacheEngine::open(tmp.path(), Vec::new(), 10).await?;
    let freq: Arc<dyn crate::policy::FrequencyEstimator> = Arc::new(TinyLfuEstimator::new(4096));
    engine.set_frequency_estimator(Arc::clone(&freq));
    // Recency only, so both blobs stay at frequency 0 and age alone decides.
    engine.touch_recency(hot);

    let candidates = engine.eviction_candidates();
    let sizes = HashMap::new();
    let segments = HashMap::new();
    let plan = TinyLfuEviction::new(freq, 2, 10).plan(&EvictionContext {
        candidates: &candidates,
        sizes: &sizes,
        segments: &segments,
        total_bytes: 1,
        target_bytes: 0,
        budget: 1,
        cache_bytes: 0,
    });
    anyhow::ensure!(
        plan.evict == vec![cold],
        "TinyLFU must release the untouched pre-open blob first, got {:?}",
        plan.evict
    );
    Ok(())
}

/// A partial blob on disk at open is seeded like a complete one: it is an
/// eviction candidate with no traffic, and its protecting tag survives the
/// reopen so the driver can release it.
#[tokio::test]
async fn a_partial_blob_on_disk_before_open_is_an_eviction_candidate() -> anyhow::Result<()> {
    let tmp = tempfile::tempdir()?;
    let group = crate::CHUNK_GROUP_BYTES;
    let total = 4 * group;
    let (root, plaintext, outboard) = synth_blob(usize::try_from(total)?);
    let (hash, ranges, bao) = bao_for(root, &plaintext, outboard, group, group, total);
    {
        let engine = CacheEngine::open(tmp.path(), Vec::new(), 16).await?;
        engine.admit_bao(hash, ranges, bao).await?;
        anyhow::ensure!(
            !engine.present_ranges(hash).await?.is_complete(),
            "the admitted blob must be partial"
        );
        engine.shutdown().await?;
    }

    let engine = CacheEngine::open(tmp.path(), Vec::new(), 16).await?;
    anyhow::ensure!(
        engine.eviction_candidates().contains_key(&hash),
        "a pre-open partial must be an eviction candidate without a touch"
    );
    anyhow::ensure!(
        engine.last_accessed(hash).is_none(),
        "a seeded partial must not read as accessed"
    );
    anyhow::ensure!(
        engine.release_for_eviction(hash).await? > 0,
        "a seeded pre-open partial must be releasable"
    );
    Ok(())
}

/// Malformed lines in `evicted.log` (manual edit gone wrong, partial
/// write from an old crash) must not stop the engine from opening;
/// they get logged + skipped, and the well-formed lines still load.
#[tokio::test]
async fn evicted_log_skips_malformed_lines() -> anyhow::Result<()> {
    let tmp = tempfile::tempdir()?;
    let log_path = tmp.path().join("evicted.log");
    let good = Hash::new(b"good entry");
    std::fs::write(
        &log_path,
        format!("\n# operator note\n{good}\nnot-a-hash\n{good}\n"),
    )?;

    let engine = CacheEngine::open(tmp.path(), Vec::new(), 10).await?;
    anyhow::ensure!(engine.is_evicted(good), "valid hash line not loaded");
    Ok(())
}

#[tokio::test]
async fn evict_unknown_hash_is_a_no_op() -> anyhow::Result<()> {
    let tmp = tempfile::tempdir()?;
    let engine = CacheEngine::open(tmp.path(), Vec::new(), 10).await?;
    let unknown = Hash::new(b"never seen");
    // Evicting a hash we've never cached is fine — operators may run
    // `decdn node evict` ahead of time as a precaution.
    engine.evict(unknown).await?;
    anyhow::ensure!(engine.is_evicted(unknown), "evict flag not set");
    Ok(())
}

/// `serve_audit` folds presence + size + eviction into one store contact
/// (#1789 item 7 part B) and matches the `has`/`inspect` pairing the
/// delivery path would otherwise make: a complete blob is `Serveable` with
/// its size, an absent hash is `Unavailable`, and an evicted hash is
/// `Unavailable` with `withdrawn` set so the serve path tells an eviction
/// from a plain miss without a second call.
#[tokio::test]
async fn serve_audit_reports_presence_size_and_eviction() -> anyhow::Result<()> {
    let tmp = tempfile::tempdir()?;
    let payload = b"a served blob";
    let origin = StubOrigin::new(payload);
    let engine =
        CacheEngine::open(tmp.path(), vec![Arc::new(origin) as Arc<dyn Origin>], 10).await?;
    let hash = Hash::new(payload);
    let unseen = Hash::new(b"never cached");

    anyhow::ensure!(
        engine.serve_audit(unseen).await? == ServeAudit::Unavailable { withdrawn: false },
        "an absent hash is unavailable and not evicted"
    );

    engine.populate(hash).await?;
    anyhow::ensure!(
        engine.serve_audit(hash).await?
            == ServeAudit::Serveable {
                size: payload.len() as u64
            },
        "a complete blob is serveable and carries its size"
    );

    engine.evict(hash).await?;
    anyhow::ensure!(
        engine.serve_audit(hash).await? == ServeAudit::Unavailable { withdrawn: true },
        "evicted content is unavailable with the eviction surfaced"
    );
    Ok(())
}

/// A complete blob that a gate refuses is `Unavailable` and carries NO
/// size, so a caller cannot advertise the wire size of content the serve
/// path would refuse. `evicted` stays false: a chain-denied hash is a
/// different refusal from an eviction and the miss path must not report it
/// as `EvictedSinceProbe`.
#[tokio::test]
async fn serve_audit_withholds_the_size_of_a_denied_blob() -> anyhow::Result<()> {
    let tmp = tempfile::tempdir()?;
    let payload = b"denied but on disk";
    let origin = StubOrigin::new(payload);
    let engine =
        CacheEngine::open(tmp.path(), vec![Arc::new(origin) as Arc<dyn Origin>], 10).await?;
    let hash = Hash::new(payload);
    engine.populate(hash).await?;

    engine.set_chain_denied_one(hash, true);
    let audit = engine.serve_audit(hash).await?;
    anyhow::ensure!(
        audit == ServeAudit::Unavailable { withdrawn: false },
        "a chain-denied blob is unavailable, un-evicted, and sizeless"
    );
    anyhow::ensure!(
        audit.hit_size().is_none(),
        "a refused blob never yields a wire size"
    );
    Ok(())
}

/// A genuinely empty blob is `Serveable { size: 0 }`. The delivery path
/// keys its "fill then size it yourself" branch on the absence of a size,
/// so a zero-length blob must not read as a miss.
#[tokio::test]
async fn serve_audit_reports_a_zero_length_blob_as_serveable() -> anyhow::Result<()> {
    let tmp = tempfile::tempdir()?;
    let origin = StubOrigin::new(b"");
    let engine =
        CacheEngine::open(tmp.path(), vec![Arc::new(origin) as Arc<dyn Origin>], 10).await?;
    let hash = Hash::new(b"");
    engine.populate(hash).await?;

    let audit = engine.serve_audit(hash).await?;
    anyhow::ensure!(
        audit == ServeAudit::Serveable { size: 0 },
        "an empty blob is serveable at size 0"
    );
    anyhow::ensure!(
        audit.hit_size() == Some(0),
        "the delivery path reads 0, not a missing size"
    );
    Ok(())
}

/// Evicting the same hash twice must not append a duplicate line to
/// `<cache_dir>/evicted.log`. Without this contract a stuck
/// automation that mass-replays the same DMCA-takedown hash would
/// grow the log unboundedly. The first evict appends one line, the
/// second short-circuits via the `contains(&hash)` check at the top
/// of `evict()`.
#[tokio::test]
async fn evict_is_idempotent_and_does_not_grow_log() -> anyhow::Result<()> {
    let tmp = tempfile::tempdir()?;
    let log_path = tmp.path().join("evicted.log");
    let engine = CacheEngine::open(tmp.path(), Vec::new(), 10).await?;
    let hash = Hash::new(b"dup-evict");

    engine.evict(hash).await?;
    let after_first = std::fs::read_to_string(&log_path)?;
    let lines_first = after_first.lines().count();

    engine.evict(hash).await?;
    engine.evict(hash).await?;
    let after_third = std::fs::read_to_string(&log_path)?;
    let lines_third = after_third.lines().count();

    anyhow::ensure!(
        lines_first == 1 && lines_third == 1,
        "expected 1 log line both times, got first={lines_first}, third={lines_third}"
    );
    Ok(())
}

/// The lock-free `evicted` set (#1789 item 5) must never lose a write and
/// never present a torn view.
///
/// CONCURRENT WRITERS are the point: several tasks evict disjoint hashes at
/// once, and every one of them must be evicted at the end. A publish that
/// read a snapshot, cloned it and stored it without serializing — the
/// obvious "one less allocation" rewrite of
/// [`MonotoneHashSet::insert_if_absent`] — drops whichever writer lost the
/// race, and `evict` would have returned `Ok(())` while the hash kept
/// serving. That is a takedown failure, so it is asserted directly.
///
/// Readers run alongside on real worker threads and assert monotonicity: a
/// hash observed evicted stays evicted. The reader half fails safe (it can
/// only under-observe under scheduling pressure), so it also asserts it saw
/// something, otherwise a starved reader would assert nothing at all.
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn evicted_set_stays_consistent_under_concurrent_evict() -> anyhow::Result<()> {
    let tmp = tempfile::tempdir()?;
    let engine = Arc::new(empty_engine(tmp.path()).await?);
    let hashes: Vec<Hash> = (0..64)
        .map(|i| Hash::new(format!("concurrent-evict-{i}").as_bytes()))
        .collect();
    let stop = Arc::new(AtomicBool::new(false));

    // Eight writers over disjoint slices of the hash set, all evicting at
    // once. A lost update leaves one of the slices un-evicted.
    let mut writers = Vec::new();
    for chunk in hashes.chunks(8) {
        let engine = Arc::clone(&engine);
        let chunk: Vec<Hash> = chunk.to_vec();
        writers.push(tokio::spawn(async move {
            for h in &chunk {
                engine.evict(*h).await?;
            }
            Ok::<_, anyhow::Error>(())
        }));
    }

    let mut readers = Vec::new();
    for _ in 0..8 {
        let engine = Arc::clone(&engine);
        let hashes = hashes.clone();
        let stop = Arc::clone(&stop);
        readers.push(tokio::spawn(async move {
            let mut saw_evicted = HashSet::new();
            for _ in 0..50_000 {
                if stop.load(Ordering::Relaxed) {
                    break;
                }
                for h in &hashes {
                    if engine.is_evicted(*h) {
                        saw_evicted.insert(*h);
                    }
                }
                tokio::task::yield_now().await;
            }
            Ok::<_, anyhow::Error>(saw_evicted)
        }));
    }

    for w in writers {
        w.await
            .map_err(|e| anyhow::anyhow!("writer task panicked: {e}"))??;
    }
    stop.store(true, Ordering::Relaxed);

    for h in &hashes {
        anyhow::ensure!(
            engine.is_evicted(*h),
            "{h} was evicted by a concurrent writer but is not in the set — lost update"
        );
    }

    let mut any_observed = false;
    for r in readers {
        let saw = r
            .await
            .map_err(|e| anyhow::anyhow!("reader task panicked: {e}"))??;
        any_observed |= !saw.is_empty();
        for h in &saw {
            anyhow::ensure!(
                engine.is_evicted(*h),
                "reader once saw {h} evicted but it is not evicted at the end — torn view"
            );
        }
    }
    anyhow::ensure!(
        any_observed,
        "no reader observed any eviction — the monotonicity half asserted nothing"
    );
    Ok(())
}

/// Hand-edited uppercase hex in `evicted.log` must be tolerated by
/// `parse_hex_hash` — the persisted format is canonically lowercase
/// (`Hash::Display` calls `to_hex()`), but operators pasting from
/// access logs / takedown notices may use either case. Without this,
/// a mixed-case hand-edit would silently get dropped at next open
/// and the takedown would resume serving content.
#[tokio::test]
async fn evicted_log_accepts_uppercase_hex() -> anyhow::Result<()> {
    let tmp = tempfile::tempdir()?;
    let log_path = tmp.path().join("evicted.log");
    let hash = Hash::new(b"upper-hex");
    let upper = hash.to_string().to_uppercase();
    std::fs::write(&log_path, format!("{upper}\n"))?;

    let engine = CacheEngine::open(tmp.path(), Vec::new(), 10).await?;
    anyhow::ensure!(
        engine.is_evicted(hash),
        "uppercase hex line should load as the same hash",
    );
    Ok(())
}

/// `evict()` must surface a persistence failure as `Err` rather than
/// silently degrading to in-memory-only — for DMCA-driven evicts the
/// operator must be able to tell whether the takedown is durable.
/// Forcing the failure: place a *directory* at the `evicted.log`
/// path so `OpenOptions::open(...)` fails (`EISDIR`) when
/// `append_evicted_log` runs. Avoids relying on filesystem
/// permission games that may not work uniformly across CI hosts.
#[tokio::test]
async fn evict_returns_err_when_persistence_fails() -> anyhow::Result<()> {
    let tmp = tempfile::tempdir()?;
    let engine = CacheEngine::open(tmp.path(), Vec::new(), 10).await?;
    // Engine opened cleanly with no log file yet. Now plant a
    // directory at the path the engine will try to append to.
    std::fs::create_dir(tmp.path().join("evicted.log"))?;

    let hash = Hash::new(b"persist-fail");
    match engine.evict(hash).await {
        Err(CacheError::Store(_)) => Ok(()),
        other => Err(anyhow::anyhow!(
            "expected Store error from persistence failure, got {other:?}"
        )),
    }?;

    // And the in-memory set must NOT have been updated — otherwise
    // an operator seeing the error would (correctly) assume the
    // takedown didn't land, but the running node would actually have
    // already stopped serving. Either contract is reasonable on its
    // own; mixing them is the worst case.
    anyhow::ensure!(
        !engine.is_evicted(hash),
        "in-memory set must not commit when persistence fails",
    );
    Ok(())
}

// ----- inspect / dry-run preview (#379) -----

/// `inspect` on a freshly-opened cache must report all the
/// "absent" sentinels: no size, no last access, not pinned, not
/// evicted, not served. Locks the wire-shape contract so a
/// regression that defaulted `served` to `true` (or that swallowed
/// the iroh-blobs `NotFound` arm) is caught on every CI run.
#[tokio::test]
async fn inspect_unknown_hash_reports_absent() -> anyhow::Result<()> {
    let tmp = tempfile::tempdir()?;
    let engine = CacheEngine::open(tmp.path(), Vec::new(), 10).await?;
    let unknown = Hash::new(b"never seen");

    let preview = engine.inspect(unknown).await?;
    anyhow::ensure!(preview.size_bytes.is_none(), "expected size_bytes=None");
    anyhow::ensure!(
        preview.last_accessed_us_ago.is_none(),
        "expected last_accessed_us_ago=None"
    );
    anyhow::ensure!(!preview.pinned, "expected pinned=false");
    anyhow::ensure!(!preview.already_evicted, "expected already_evicted=false");
    anyhow::ensure!(!preview.served, "expected served=false");
    Ok(())
}

/// After a successful pull-through `get`, `inspect` reports the
/// concrete blob size, a finite `last_accessed_us_ago`, and
/// `served=true`. Asserts the size matches the payload exactly so
/// a regression that returned the partial-blob size (or a wrong
/// match arm in the `BlobStatus` decode) fails loudly.
#[tokio::test]
async fn inspect_after_get_reports_size_and_served() -> anyhow::Result<()> {
    let tmp = tempfile::tempdir()?;
    let payload = b"inspect me";
    let hash = Hash::new(payload);
    let origin = StubOrigin::new(payload);

    let engine =
        CacheEngine::open(tmp.path(), vec![Arc::new(origin) as Arc<dyn Origin>], 10).await?;
    let _ = engine.get(hash).await?;

    let preview = engine.inspect(hash).await?;
    anyhow::ensure!(
        preview.size_bytes == Some(payload.len() as u64),
        "expected size_bytes={:?}, got {:?}",
        Some(payload.len() as u64),
        preview.size_bytes,
    );
    anyhow::ensure!(
        preview.last_accessed_us_ago.is_some(),
        "expected Some(last_accessed_us_ago) after get()"
    );
    anyhow::ensure!(preview.served, "expected served=true");
    anyhow::ensure!(!preview.already_evicted, "expected already_evicted=false");
    anyhow::ensure!(!preview.pinned, "expected pinned=false");
    Ok(())
}

/// `inspect` after `evict` must still report the on-disk
/// `size_bytes` (until iroh-blobs' periodic GC sweep reclaims, #518)
/// but flip `already_evicted` to `true` and `served` to `false`.
/// The size-still-reported part
/// is the load-bearing assertion: dry-run callers want to see
/// disk-reclaim potential, not a clean `None` that hides the bytes.
#[tokio::test]
async fn inspect_after_evict_keeps_size_but_flips_served() -> anyhow::Result<()> {
    let tmp = tempfile::tempdir()?;
    let payload = b"evicted blob";
    let hash = Hash::new(payload);
    let origin = StubOrigin::new(payload);

    let engine =
        CacheEngine::open(tmp.path(), vec![Arc::new(origin) as Arc<dyn Origin>], 10).await?;
    let _ = engine.get(hash).await?;
    engine.evict(hash).await?;

    let preview = engine.inspect(hash).await?;
    anyhow::ensure!(
        preview.size_bytes == Some(payload.len() as u64),
        "expected size_bytes still reported post-evict, got {:?}",
        preview.size_bytes,
    );
    anyhow::ensure!(preview.already_evicted, "expected already_evicted=true");
    anyhow::ensure!(!preview.served, "expected served=false post-evict");
    // Eviction clears the access-time entry, so this should now be None.
    anyhow::ensure!(
        preview.last_accessed_us_ago.is_none(),
        "expected last_accessed cleared by evict, got {:?}",
        preview.last_accessed_us_ago,
    );
    Ok(())
}

/// `inspect` reflects the operator-pinned set without needing a
/// blob to be cached. The pinned flag must be observable for
/// hashes the operator hasn't fetched yet — that's the whole
/// point of pre-flight dry-run: confirm policy state before
/// committing to evict.
#[tokio::test]
async fn inspect_reports_pinned_flag() -> anyhow::Result<()> {
    let tmp = tempfile::tempdir()?;
    let pinned_hash = Hash::new(b"pinned");
    let other_hash = Hash::new(b"other");
    // Convert the store hash to the leaf config-vocabulary hash at
    // the `PinnedHashes` boundary (#578).
    let set = [from_store_hash(pinned_hash)].into_iter().collect();
    let engine =
        CacheEngine::open_with_pinned(tmp.path(), Vec::new(), 10, PinnedHashes::new(set)).await?;

    let pinned_preview = engine.inspect(pinned_hash).await?;
    anyhow::ensure!(pinned_preview.pinned, "expected pinned=true");
    anyhow::ensure!(
        !pinned_preview.served,
        "pinned-but-uncached blob should not be served"
    );

    let other_preview = engine.inspect(other_hash).await?;
    anyhow::ensure!(!other_preview.pinned, "unrelated hash must not be pinned");
    Ok(())
}

/// `inspect` (#439) reports the configured origin's backend kind so
/// admin dry-run callers can estimate origin egress cost before
/// committing to an eviction. Engine constructed with no origin
/// reports an empty vec.
#[tokio::test]
async fn inspect_reports_origin_kinds_when_origin_configured() -> anyhow::Result<()> {
    let tmp = tempfile::tempdir()?;
    let origin = StubOrigin::new(b"egress-cost preview");
    let engine =
        CacheEngine::open(tmp.path(), vec![Arc::new(origin) as Arc<dyn Origin>], 10).await?;
    let preview = engine.inspect(Hash::new(b"never-fetched")).await?;
    anyhow::ensure!(
        preview.origin_kinds == vec![OriginKind::Http],
        "expected [Http], got {:?}",
        preview.origin_kinds,
    );
    Ok(())
}

#[tokio::test]
async fn inspect_reports_no_origin_kinds_when_cache_only() -> anyhow::Result<()> {
    let tmp = tempfile::tempdir()?;
    let engine = CacheEngine::open(tmp.path(), Vec::new(), 10).await?;
    let preview = engine.inspect(Hash::new(b"absent")).await?;
    anyhow::ensure!(
        preview.origin_kinds.is_empty(),
        "expected empty Vec for cache-only mode, got {:?}",
        preview.origin_kinds,
    );
    Ok(())
}

// -------------------------------------------------------------------
// Cache hit/miss + bytes counters (#418)
// -------------------------------------------------------------------

#[tokio::test]
async fn hits_plus_misses_equals_total_gets() -> anyhow::Result<()> {
    let tmp = tempfile::tempdir()?;
    let payload = b"hello invariant";
    let hash = Hash::new(payload);
    let unknown = Hash::new(b"never present");
    let origin = StubOrigin::new(payload);
    let cm = Arc::new(CacheMetrics::default());
    let engine = CacheEngine::open_full(
        tmp.path(),
        vec![Arc::new(origin) as Arc<dyn Origin>],
        10,
        crate::PinnedHashes::empty(),
        crate::RetryPolicy::default(),
        CircuitBreakerPolicy::default(),
        Some(Arc::clone(&cm)),
        Duration::ZERO,
    )
    .await?;

    // 1 miss (pull-through), 2 hits, 1 miss (origin NotFound), 1 miss (evicted).
    let _ = engine.get(hash).await?; // miss
    let _ = engine.get(hash).await?; // hit
    let _ = engine.get(hash).await?; // hit
    let _ = engine.get(unknown).await; // miss (origin NotFound)
    engine.evict(hash).await?;
    let _ = engine.get(hash).await; // miss (evicted)

    anyhow::ensure!(cm.hits.get() == 2, "hits = {}", cm.hits.get());
    anyhow::ensure!(cm.misses.get() == 3, "misses = {}", cm.misses.get());
    anyhow::ensure!(
        cm.hits.get() + cm.misses.get() == 5,
        "across cache-domain outcomes (no store-I/O errors), every get bumps exactly one of hits/misses"
    );
    // Only the priming Found bumped pull_through_bytes; the unknown
    // get took the origin-NotFound branch which returns before the
    // bump. Pin both, so a stray bump in either error path fails
    // the test.
    anyhow::ensure!(
        cm.pull_through_bytes.get() == payload.len() as u64,
        "pull_through_bytes = {}, expected {}",
        cm.pull_through_bytes.get(),
        payload.len()
    );
    Ok(())
}

#[tokio::test]
async fn pull_through_success_bumps_bytes_returned() -> anyhow::Result<()> {
    let tmp = tempfile::tempdir()?;
    let payload = b"hello pull-through bytes returned";
    let hash = Hash::new(payload);
    let origin = StubOrigin::new(payload);
    let cm = Arc::new(CacheMetrics::default());
    let engine = CacheEngine::open_full(
        tmp.path(),
        vec![Arc::new(origin) as Arc<dyn Origin>],
        10,
        crate::PinnedHashes::empty(),
        crate::RetryPolicy::default(),
        CircuitBreakerPolicy::default(),
        Some(Arc::clone(&cm)),
        Duration::ZERO,
    )
    .await?;

    let bytes = engine.get(hash).await?;
    anyhow::ensure!(
        cm.bytes_returned.get() == bytes.len() as u64,
        "bytes_returned should match payload length after a successful pull-through"
    );
    Ok(())
}

#[tokio::test]
async fn pull_through_bumps_pull_through_bytes() -> anyhow::Result<()> {
    let tmp = tempfile::tempdir()?;
    let payload = b"hello pull-through bytes";
    let hash = Hash::new(payload);
    let origin = StubOrigin::new(payload);
    let cm = Arc::new(CacheMetrics::default());
    let engine = CacheEngine::open_full(
        tmp.path(),
        vec![Arc::new(origin) as Arc<dyn Origin>],
        10,
        crate::PinnedHashes::empty(),
        crate::RetryPolicy::default(),
        CircuitBreakerPolicy::default(),
        Some(Arc::clone(&cm)),
        Duration::ZERO,
    )
    .await?;

    let bytes = engine.get(hash).await?;
    anyhow::ensure!(cm.misses.get() == 1, "first get is a miss");
    anyhow::ensure!(cm.hits.get() == 0, "no hits on first get");
    anyhow::ensure!(
        cm.pull_through_bytes.get() == bytes.len() as u64,
        "pull_through_bytes should equal payload length on a Found origin"
    );
    Ok(())
}

#[tokio::test]
async fn populate_fills_without_bumping_bytes_returned() -> anyhow::Result<()> {
    let tmp = tempfile::tempdir()?;
    let payload = b"populate must not count as bytes returned";
    let hash = Hash::new(payload);
    let origin = StubOrigin::new(payload);
    let cm = Arc::new(CacheMetrics::default());
    let engine = CacheEngine::open_full(
        tmp.path(),
        vec![Arc::new(origin) as Arc<dyn Origin>],
        10,
        crate::PinnedHashes::empty(),
        crate::RetryPolicy::default(),
        CircuitBreakerPolicy::default(),
        Some(Arc::clone(&cm)),
        Duration::ZERO,
    )
    .await?;

    engine.populate(hash).await?;
    anyhow::ensure!(engine.has(hash).await?, "populate must fill the store");
    // Origin egress IS counted (the bytes really left an origin)...
    anyhow::ensure!(
        cm.pull_through_bytes.get() == payload.len() as u64,
        "populate must still bump origin-egress pull_through_bytes"
    );
    // ...but it is NOT a `get` caller, so no served-bytes / hit accounting.
    anyhow::ensure!(
        cm.bytes_returned.get() == 0,
        "populate must NOT bump bytes_returned (#831: internal fill, not client egress)"
    );
    anyhow::ensure!(cm.hits.get() == 0, "populate must not count a hit");

    // A populate on an already-present hash is a no-op (no second pull).
    engine.populate(hash).await?;
    anyhow::ensure!(
        cm.pull_through_bytes.get() == payload.len() as u64,
        "a populate for an already-present blob must not re-pull"
    );
    anyhow::ensure!(cm.bytes_returned.get() == 0, "still no bytes_returned");
    Ok(())
}

#[tokio::test]
async fn no_origin_increments_misses_only() -> anyhow::Result<()> {
    let tmp = tempfile::tempdir()?;
    let cm = Arc::new(CacheMetrics::default());
    let engine = CacheEngine::open_full(
        tmp.path(),
        Vec::new(),
        10,
        crate::PinnedHashes::empty(),
        crate::RetryPolicy::default(),
        CircuitBreakerPolicy::default(),
        Some(Arc::clone(&cm)),
        Duration::ZERO,
    )
    .await?;

    let hash = Hash::new(b"missing payload");
    let Err(err) = engine.get(hash).await else {
        anyhow::bail!("expected NoOrigin, got Ok");
    };
    anyhow::ensure!(
        matches!(err, CacheError::NoOrigin { .. }),
        "expected NoOrigin"
    );
    anyhow::ensure!(cm.misses.get() == 1, "exactly one miss for a NoOrigin get");
    anyhow::ensure!(cm.hits.get() == 0, "no hits");
    anyhow::ensure!(
        cm.pull_through_bytes.get() == 0,
        "no origin bytes since origin not configured"
    );
    anyhow::ensure!(
        cm.bytes_returned.get() == 0,
        "no bytes returned on error path"
    );
    Ok(())
}

#[tokio::test]
async fn evicted_hash_increments_misses() -> anyhow::Result<()> {
    let tmp = tempfile::tempdir()?;
    let payload = b"hello evicted miss";
    let hash = Hash::new(payload);
    let origin = StubOrigin::new(payload);
    let cm = Arc::new(CacheMetrics::default());
    let engine = CacheEngine::open_full(
        tmp.path(),
        vec![Arc::new(origin) as Arc<dyn Origin>],
        10,
        crate::PinnedHashes::empty(),
        crate::RetryPolicy::default(),
        CircuitBreakerPolicy::default(),
        Some(Arc::clone(&cm)),
        Duration::ZERO,
    )
    .await?;

    // Prime then evict so the next get hits the evicted branch in get().
    let _ = engine.get(hash).await?;
    engine.evict(hash).await?;
    let misses_before = cm.misses.get();

    let Err(err) = engine.get(hash).await else {
        anyhow::bail!("expected NotFound, got Ok");
    };
    anyhow::ensure!(
        matches!(err, CacheError::NotFound { .. }),
        "evicted get must surface NotFound"
    );
    anyhow::ensure!(
        cm.misses.get() == misses_before + 1,
        "misses should bump by exactly 1 on an evicted-hash get"
    );
    Ok(())
}

// ---- Governance deny-set (ADR 011 §StreamRequest Response) ----

async fn empty_engine(tmp: &std::path::Path) -> anyhow::Result<CacheEngine> {
    Ok(CacheEngine::open_with_pinned(tmp, Vec::new(), 10, crate::PinnedHashes::empty()).await?)
}

/// The governance set has to feed `refuses`, or the takedown suppresses
/// nothing before the (separate, slower) eviction lands.
#[tokio::test]
async fn chain_denied_hash_is_refused() -> anyhow::Result<()> {
    let tmp = tempfile::tempdir()?;
    let engine = empty_engine(tmp.path()).await?;
    let hash = Hash::new(b"governance takedown");

    anyhow::ensure!(!engine.refuses(hash), "nothing refused before the deny");
    anyhow::ensure!(engine.set_chain_denied_one(hash, true), "set changed");
    anyhow::ensure!(engine.is_chain_denied(hash));
    anyhow::ensure!(engine.refuses(hash), "a governance deny must refuse");
    anyhow::ensure!(
        !engine.is_denied(hash),
        "and must NOT masquerade as a local denylist entry — the two feed \
         different operator metrics"
    );
    anyhow::ensure!(!engine.has(hash).await?, "refused hashes report absent");
    Ok(())
}

/// A no-op replay must be reported as such: the watcher re-scans a block
/// range after a restart and re-delivers events it already applied, and
/// every one of those would otherwise log as a fresh takedown.
#[tokio::test]
async fn set_chain_denied_one_reports_whether_it_changed_anything() -> anyhow::Result<()> {
    let tmp = tempfile::tempdir()?;
    let engine = empty_engine(tmp.path()).await?;
    let hash = Hash::new(b"replayed");

    anyhow::ensure!(engine.set_chain_denied_one(hash, true));
    anyhow::ensure!(
        !engine.set_chain_denied_one(hash, true),
        "replay is a no-op"
    );
    anyhow::ensure!(engine.set_chain_denied_one(hash, false));
    anyhow::ensure!(!engine.set_chain_denied_one(hash, false));
    anyhow::ensure!(
        !engine.refuses(hash),
        "a de-listed hash stops being refused"
    );
    Ok(())
}

/// The reason the two deny-sets are separate slots: their lifecycles are
/// independent. A config reload must not drop a governance takedown, and the
/// watcher must not drop the operator's own list.
#[tokio::test]
async fn local_and_chain_deny_sets_do_not_clobber_each_other() -> anyhow::Result<()> {
    let tmp = tempfile::tempdir()?;
    let engine = empty_engine(tmp.path()).await?;
    let local = Hash::new(b"local entry");
    let governance = Hash::new(b"governance entry");

    engine.set_denied(&denied(&[local]));
    engine.set_chain_denied_one(governance, true);

    // A reload that drops the local entry leaves the governance one standing.
    engine.set_denied(&crate::DeniedHashes::empty());
    anyhow::ensure!(!engine.refuses(local), "local entry lifted by the reload");
    anyhow::ensure!(
        engine.refuses(governance),
        "a config reload must not lift a governance takedown"
    );

    // ...and a governance removal leaves a re-added local entry standing.
    engine.set_denied(&denied(&[local]));
    engine.set_chain_denied_one(governance, false);
    anyhow::ensure!(engine.refuses(local));
    anyhow::ensure!(!engine.refuses(governance));
    Ok(())
}

/// A hash on BOTH lists must survive removal from one. A single shared set
/// would drop it and silently resume serving content still under a takedown.
#[tokio::test]
async fn hash_on_both_deny_sets_survives_removal_from_one() -> anyhow::Result<()> {
    let tmp = tempfile::tempdir()?;
    let engine = empty_engine(tmp.path()).await?;
    let hash = Hash::new(b"both lists");

    engine.set_denied(&denied(&[hash]));
    engine.set_chain_denied_one(hash, true);
    engine.set_chain_denied_one(hash, false);
    anyhow::ensure!(engine.refuses(hash), "the local entry still stands");
    Ok(())
}

/// The watcher's boot restore replaces wholesale — it is reloading a
/// projection, not merging events into one.
#[tokio::test]
async fn set_chain_denied_replaces_wholesale() -> anyhow::Result<()> {
    let tmp = tempfile::tempdir()?;
    let engine = empty_engine(tmp.path()).await?;
    let stale = Hash::new(b"stale");
    let restored = Hash::new(b"restored");

    engine.set_chain_denied_one(stale, true);
    engine.set_chain_denied([restored].into_iter().collect());
    anyhow::ensure!(!engine.refuses(stale));
    anyhow::ensure!(engine.refuses(restored));
    Ok(())
}

fn denied(hashes: &[Hash]) -> crate::DeniedHashes {
    crate::DeniedHashes::new(hashes.iter().map(|h| from_store_hash(*h)).collect())
}

// ---- Probe-triggered eviction hold (#318, ADR 005) ----

#[tokio::test]
async fn try_probe_hold_unavailable_when_blob_absent() -> anyhow::Result<()> {
    let tmp = tempfile::tempdir()?;
    let engine = CacheEngine::open(tmp.path(), vec![], 10).await?;
    let absent = Hash::new(b"never fetched");
    anyhow::ensure!(
        engine.try_probe_hold(absent).await? == ProbeHoldOutcome::Unavailable,
        "absent blob must not be holdable (an absent blob is never advertised)"
    );
    Ok(())
}

#[tokio::test]
async fn try_probe_hold_true_when_cached_and_excluded_from_eviction() -> anyhow::Result<()> {
    let tmp = tempfile::tempdir()?;
    let payload = b"holdable blob";
    let hash = Hash::new(payload);
    let engine = CacheEngine::open(
        tmp.path(),
        vec![Arc::new(StubOrigin::new(payload)) as Arc<dyn Origin>],
        10,
    )
    .await?;
    let _ = engine.get(hash).await?;

    anyhow::ensure!(
        engine.try_probe_hold(hash).await? == ProbeHoldOutcome::Held,
        "cached blob should hold"
    );
    anyhow::ensure!(engine.probe_hold_slots_used() == 1, "one slot used");
    anyhow::ensure!(
        !engine
            .eviction_candidates()
            .into_inner()
            .contains_key(&hash),
        "held hash must be invisible to the LRU driver"
    );
    Ok(())
}

#[tokio::test]
async fn probe_hold_is_shared_per_blob_not_per_probe() -> anyhow::Result<()> {
    let tmp = tempfile::tempdir()?;
    let payload = b"popular blob";
    let hash = Hash::new(payload);
    let engine = CacheEngine::open(
        tmp.path(),
        vec![Arc::new(StubOrigin::new(payload)) as Arc<dyn Origin>],
        10,
    )
    .await?;
    let _ = engine.get(hash).await?;
    engine.set_max_probe_holds(1);

    // Many "peers" probing the same hash share one slot.
    for _ in 0..3 {
        anyhow::ensure!(engine.try_probe_hold(hash).await? == ProbeHoldOutcome::Held);
    }
    anyhow::ensure!(
        engine.probe_hold_slots_used() == 1,
        "shared per-blob slot must not grow with probe volume"
    );
    Ok(())
}

#[tokio::test]
async fn probe_hold_budget_exhaustion_reports_exhausted() -> anyhow::Result<()> {
    let tmp = tempfile::tempdir()?;
    let a: &[u8] = b"blob a";
    let b: &[u8] = b"blob bee";
    let (ha, hb) = (Hash::new(a), Hash::new(b));
    // One engine, one multi-blob origin: opening a second engine on the
    // same dir would deadlock on iroh-blobs' single-writer lock.
    let origin = Arc::new(MultiStubOrigin::new(&[a, b]));
    let engine = CacheEngine::open(tmp.path(), vec![origin as Arc<dyn Origin>], 10).await?;
    let _ = engine.get(ha).await?;
    let _ = engine.get(hb).await?;
    engine.set_max_probe_holds(1);

    anyhow::ensure!(
        engine.try_probe_hold(ha).await? == ProbeHoldOutcome::Held,
        "first hold fits budget"
    );
    anyhow::ensure!(
        engine.try_probe_hold(hb).await? == ProbeHoldOutcome::BudgetExhausted,
        "second distinct hold must be refused when budget is exhausted"
    );
    Ok(())
}

#[tokio::test]
async fn probe_hold_disabled_when_budget_zero() -> anyhow::Result<()> {
    let tmp = tempfile::tempdir()?;
    let payload = b"unhold me";
    let hash = Hash::new(payload);
    let engine = CacheEngine::open(
        tmp.path(),
        vec![Arc::new(StubOrigin::new(payload)) as Arc<dyn Origin>],
        10,
    )
    .await?;
    let _ = engine.get(hash).await?;
    engine.set_max_probe_holds(0);
    // `max == 0` is an operator config decision (holds turned off), not
    // budget pressure — it must report a cause distinct from
    // `BudgetExhausted` (#739) so the "increase max_probe_holds" alert
    // isn't tripped by an intentional disable.
    anyhow::ensure!(
        engine.try_probe_hold(hash).await? == ProbeHoldOutcome::HoldsDisabled,
        "max_probe_holds=0 must disable has_blob:true entirely"
    );
    Ok(())
}

#[tokio::test]
async fn runtime_disable_overrides_existing_probe_hold() -> anyhow::Result<()> {
    let tmp = tempfile::tempdir()?;
    let payload = b"held then disabled";
    let hash = Hash::new(payload);
    let engine = CacheEngine::open(
        tmp.path(),
        vec![Arc::new(StubOrigin::new(payload)) as Arc<dyn Origin>],
        10,
    )
    .await?;
    let _ = engine.get(hash).await?;
    engine.set_max_probe_holds(1);
    anyhow::ensure!(
        engine.try_probe_hold(hash).await? == ProbeHoldOutcome::Held,
        "hold should be granted while the budget is positive"
    );
    // Lowering the budget to 0 while a hold is live must take effect
    // immediately: a re-probe for the already-held blob must NOT refresh
    // the hold and re-sign has_blob:true (#739). `max == 0` means "never
    // sign has_blob:true", unconditionally.
    engine.set_max_probe_holds(0);
    anyhow::ensure!(
        engine.try_probe_hold(hash).await? == ProbeHoldOutcome::HoldsDisabled,
        "a runtime disable must override an existing live hold"
    );
    Ok(())
}

#[tokio::test]
async fn absent_blob_is_unavailable_even_when_holds_disabled() -> anyhow::Result<()> {
    // Precedence pin (#739): the `max == 0` early return must sit *after*
    // the `has()` check, so an absent blob is a true negative
    // (`Unavailable`), never a `HoldsDisabled` config-disable event. Guards
    // against a future reorder that moves the cheap `max == 0` load above
    // the store lookup and silently inflates
    // `probe_hold_unavailable{reason="disabled"}` with
    // probes for content the node never had.
    let tmp = tempfile::tempdir()?;
    let absent = Hash::new(b"never fetched");
    let engine = CacheEngine::open(tmp.path(), vec![], 10).await?;
    engine.set_max_probe_holds(0);
    anyhow::ensure!(
        engine.try_probe_hold(absent).await? == ProbeHoldOutcome::Unavailable,
        "absent blob must win over holds-disabled"
    );
    Ok(())
}

#[tokio::test]
async fn operator_evict_overrides_probe_hold() -> anyhow::Result<()> {
    let tmp = tempfile::tempdir()?;
    let payload = b"dmca target";
    let hash = Hash::new(payload);
    let engine = CacheEngine::open(
        tmp.path(),
        vec![Arc::new(StubOrigin::new(payload)) as Arc<dyn Origin>],
        10,
    )
    .await?;
    let _ = engine.get(hash).await?;
    anyhow::ensure!(
        engine.try_probe_hold(hash).await? == ProbeHoldOutcome::Held,
        "held before evict"
    );

    engine.evict(hash).await?;
    anyhow::ensure!(
        engine.try_probe_hold(hash).await? == ProbeHoldOutcome::Unavailable,
        "operator evict (DMCA) must win over a probe hold"
    );
    Ok(())
}

#[tokio::test]
async fn expired_probe_hold_is_swept_and_re_evictable() -> anyhow::Result<()> {
    let tmp = tempfile::tempdir()?;
    let payload = b"expiring blob";
    let hash = Hash::new(payload);
    let engine = CacheEngine::open(
        tmp.path(),
        vec![Arc::new(StubOrigin::new(payload)) as Arc<dyn Origin>],
        10,
    )
    .await?;
    let _ = engine.get(hash).await?;
    engine.observe_hit(hash); // make it an LRU candidate

    // Inject an already-expired hold directly (the real 35s duration is
    // impractical to sleep on a real clock).
    {
        let mut g = engine
            .inner
            .probe_holds
            .lock()
            .unwrap_or_else(PoisonError::into_inner);
        let past = tokio::time::Instant::now()
            .checked_sub(Duration::from_secs(1))
            .unwrap_or_else(tokio::time::Instant::now);
        g.insert(hash, past);
    }

    anyhow::ensure!(
        engine.probe_hold_slots_used() == 0,
        "expired hold must be swept from the slot count"
    );
    anyhow::ensure!(
        engine
            .eviction_candidates()
            .into_inner()
            .contains_key(&hash),
        "an expired hold must no longer shield the hash from LRU"
    );
    Ok(())
}

#[tokio::test]
async fn hit_increments_hits_and_bytes_returned() -> anyhow::Result<()> {
    let tmp = tempfile::tempdir()?;
    let payload = b"hello hit metrics";
    let hash = Hash::new(payload);
    let origin = StubOrigin::new(payload);
    let cm = Arc::new(CacheMetrics::default());
    let engine = CacheEngine::open_full(
        tmp.path(),
        vec![Arc::new(origin) as Arc<dyn Origin>],
        10,
        crate::PinnedHashes::empty(),
        crate::RetryPolicy::default(),
        CircuitBreakerPolicy::default(),
        Some(Arc::clone(&cm)),
        Duration::ZERO,
    )
    .await?;

    // First get is a pull-through (miss); prime the cache.
    let _ = engine.get(hash).await?;
    let hits_before = cm.hits.get();
    let bytes_before = cm.bytes_returned.get();

    // Second get must be a local hit.
    let bytes = engine.get(hash).await?;
    anyhow::ensure!(
        cm.hits.get() == hits_before + 1,
        "hits should increment by 1 on a cache hit"
    );
    anyhow::ensure!(
        cm.bytes_returned.get() == bytes_before + bytes.len() as u64,
        "bytes_returned should increase by bytes.len() on a cache hit"
    );
    Ok(())
}

/// A blob spanning several chunk groups plus a partial final group, so
/// the bao tree has real interior nodes (matches
/// `decdn-bao-range::streaming`'s test rationale).
fn local_outboard_pull_test_blob() -> Vec<u8> {
    let size = 5 * crate::CHUNK_GROUP_BYTES + 123;
    (0..size).map(|i| (i % 251) as u8).collect()
}

// -- origin_range_wire / origin_fetch_outboard_bytes --

/// A test origin that serves chunk-group-aligned ranges plus a configurable
/// `{H}.obao4` outboard, so the raw origin fetch+encode surface can be
/// exercised without an HTTP/S3/fs backend. `data`/`outboard`/`size` are held
/// independently so a test can serve bytes that do NOT hash to `hash` (a
/// corrupt / misconfigured OWN origin, the local-origin-fault case).
/// `support_range == false` models an origin with no `206`/outboard support,
/// i.e. the [`crate::OriginRangeFetch::Unsupported`] degrade. `max_req`
/// records the largest data span any single range fetch asked for.
#[derive(Debug)]
struct RangeStubOrigin {
    hash: Hash,
    data: Bytes,
    outboard: Option<Bytes>,
    size: u64,
    support_range: bool,
    max_req: AtomicU64,
    /// Windows starting at or past this offset come back one byte short.
    short_from: u64,
    /// Windows starting at or past this offset are declined.
    decline_from: u64,
    /// Windows starting at or past this offset fail with a transport error.
    fail_from: u64,
    /// A window starting at or past this offset panics the fetch.
    panic_from: u64,
    /// Windows starting at or past this offset wait on `gate`.
    gate_from: u64,
    gate: Arc<tokio::sync::Semaphore>,
    /// When true, [`Origin::fetch_outboard`] returns a transport
    /// [`OriginPullError`] instead of a clean decline — the degraded-origin
    /// case the serviceability probe must surface as a fault (#1129), not as a
    /// clean `Ok(None)` absence.
    fault_outboard: bool,
    /// Calls to [`Origin::fetch_outboard`] so far.
    outboard_fetches: AtomicUsize,
    /// When true, [`Origin::fetch_outboard`] waits on `gate`.
    gate_outboard: bool,
    /// Calls to [`Origin::fetch_range_data`] so far, shared so a test can
    /// read it after the engine takes the origin.
    range_fetches: Arc<AtomicUsize>,
}

impl RangeStubOrigin {
    /// An origin that serves `payload` and its genuine outboard for `hash`.
    fn serving(hash: Hash, payload: &[u8], outboard: Bytes) -> Self {
        Self {
            hash,
            data: Bytes::from(payload.to_vec()),
            outboard: Some(outboard),
            size: u64::try_from(payload.len()).unwrap_or(u64::MAX),
            support_range: true,
            max_req: AtomicU64::new(0),
            short_from: u64::MAX,
            decline_from: u64::MAX,
            fail_from: u64::MAX,
            panic_from: u64::MAX,
            gate_from: u64::MAX,
            gate: Arc::new(tokio::sync::Semaphore::new(0)),
            fault_outboard: false,
            outboard_fetches: AtomicUsize::new(0),
            gate_outboard: false,
            range_fetches: Arc::new(AtomicUsize::new(0)),
        }
    }

    /// An origin that knows `hash`'s size but FAULTS its outboard fetch with a
    /// transport error — a degraded own origin, distinct from a clean absence.
    fn outboard_faulting(hash: Hash, size: u64) -> Self {
        Self {
            hash,
            data: Bytes::new(),
            outboard: None,
            size,
            support_range: false,
            max_req: AtomicU64::new(0),
            short_from: u64::MAX,
            decline_from: u64::MAX,
            fail_from: u64::MAX,
            panic_from: u64::MAX,
            gate_from: u64::MAX,
            gate: Arc::new(tokio::sync::Semaphore::new(0)),
            fault_outboard: true,
            outboard_fetches: AtomicUsize::new(0),
            gate_outboard: false,
            range_fetches: Arc::new(AtomicUsize::new(0)),
        }
    }
}

impl Origin for RangeStubOrigin {
    fn kind(&self) -> OriginKind {
        OriginKind::Http
    }

    fn fetch(
        &self,
        hash: Hash,
        _max_bytes: u64,
    ) -> Pin<Box<dyn Future<Output = Result<OriginFetch, crate::OriginPullError>> + Send + '_>>
    {
        let result = if hash == self.hash {
            Ok(OriginFetch::found_one_shot(self.data.clone()))
        } else {
            Ok(OriginFetch::NotFound)
        };
        Box::pin(async move { result })
    }

    fn size(
        &self,
        hash: Hash,
    ) -> Pin<Box<dyn Future<Output = Result<Option<u64>, crate::OriginPullError>> + Send + '_>>
    {
        let out = (hash == self.hash).then_some(self.size);
        Box::pin(async move { Ok(out) })
    }

    fn fetch_outboard(
        &self,
        hash: Hash,
        _outboard_max_bytes: u64,
    ) -> Pin<Box<dyn Future<Output = Result<OutboardFetch, crate::OriginPullError>> + Send + '_>>
    {
        self.outboard_fetches.fetch_add(1, Ordering::SeqCst);
        if self.fault_outboard && hash == self.hash {
            return Box::pin(async move {
                Err(crate::OriginPullError::Transient(anyhow::anyhow!(
                    "stub outboard transport fault"
                )))
            });
        }
        let result = match (&self.outboard, hash == self.hash) {
            (Some(ob), true) => OutboardFetch::Found(ob.clone()),
            _ => OutboardFetch::NotFound,
        };
        let gated = self.gate_outboard;
        Box::pin(async move {
            if gated {
                let _held = self.gate.acquire().await;
            }
            Ok(result)
        })
    }

    fn fetch_range_data(
        &self,
        hash: Hash,
        req: crate::OriginRangeRequest,
    ) -> Pin<
        Box<
            dyn Future<Output = Result<crate::OriginRangeFetch, crate::OriginPullError>>
                + Send
                + '_,
        >,
    > {
        self.max_req.fetch_max(req.len(), Ordering::SeqCst);
        self.range_fetches.fetch_add(1, Ordering::SeqCst);
        assert!(
            req.fetch_start < self.panic_from,
            "stub origin panics on request"
        );
        if req.fetch_start >= self.fail_from {
            return Box::pin(async {
                Err(crate::OriginPullError::Transient(anyhow::anyhow!(
                    "stub range transport fault"
                )))
            });
        }
        let gated = req.fetch_start >= self.gate_from;
        let result = if req.fetch_start >= self.decline_from {
            crate::OriginRangeFetch::Unsupported
        } else if hash == self.hash && self.support_range {
            let s = usize::try_from(req.fetch_start).unwrap_or(usize::MAX);
            let mut e = usize::try_from(req.fetch_end).unwrap_or(usize::MAX);
            if req.fetch_start >= self.short_from {
                e = e.saturating_sub(1);
            }
            match self.data.get(s..e) {
                Some(span) => crate::OriginRangeFetch::Ranged {
                    data: Bytes::copy_from_slice(span),
                },
                None => crate::OriginRangeFetch::NotFound,
            }
        } else {
            crate::OriginRangeFetch::Unsupported
        };
        Box::pin(async move {
            if gated {
                let _held = self.gate.acquire().await;
            }
            Ok(result)
        })
    }
}

/// A blob of a little over two range-pull windows, so a whole-blob range
/// crosses window boundaries.
fn multi_window_test_blob() -> Vec<u8> {
    let size = 2 * crate::RANGE_PULL_WINDOW_BYTES + 5 * crate::CHUNK_GROUP_BYTES + 123;
    (0..size).map(|i| (i % 251) as u8).collect()
}

/// Drain an origin-range wire to its end: the bytes, and the fault it ended on.
async fn drain_wire(mut wire: OriginRangeWire) -> (Vec<u8>, Option<CacheError>) {
    let mut out = Vec::new();
    while let Some(item) = wire.next_chunk().await {
        match item {
            Ok(chunk) => out.extend_from_slice(&chunk),
            Err(fault) => {
                assert!(wire.next_chunk().await.is_none(), "a fault is terminal");
                return (out, Some(fault));
            }
        }
    }
    (out, None)
}

/// A blob of a little over two windows, its hash, and a stub origin
/// serving it with its genuine outboard, plus the whole-blob aligned range.
fn multi_window_stub() -> (Vec<u8>, Hash, RangeStubOrigin, AlignedRange) {
    use bao_tree::io::outboard::PreOrderMemOutboard;

    let data = multi_window_test_blob();
    let ob = PreOrderMemOutboard::create(&data, crate::range_pull::IROH_BLOCK_SIZE);
    let hash = Hash::from(*ob.root.as_bytes());
    let total = u64::try_from(data.len()).unwrap_or(u64::MAX);
    let origin = RangeStubOrigin::serving(hash, &data, Bytes::from(ob.data));
    let aligned = crate::range_pull::align_range(0, 0, total).expect("aligns");
    (data, hash, origin, aligned)
}

async fn stub_engine(
    origins: Vec<RangeStubOrigin>,
) -> anyhow::Result<(CacheEngine, tempfile::TempDir)> {
    let tmp = tempfile::tempdir()?;
    let origins = origins
        .into_iter()
        .map(|o| Arc::new(o) as Arc<dyn Origin>)
        .collect();
    let engine = CacheEngine::open(tmp.path(), origins, 64).await?;
    Ok((engine, tmp))
}

/// The serviceability probe confirms an origin that serves the outboard and
/// ranges with one chunk-group read, then answers the next probe from the
/// latch without a read.
#[tokio::test]
async fn origin_range_serviceable_probes_one_group_then_latches() -> anyhow::Result<()> {
    let (data, hash, origin, _) = multi_window_stub();
    let total = u64::try_from(data.len())?;
    let fetches = Arc::clone(&origin.range_fetches);
    let (engine, _tmp) = stub_engine(vec![origin]).await?;
    anyhow::ensure!(engine.origin_range_serviceable(hash, total).await?);
    anyhow::ensure!(
        fetches.load(Ordering::SeqCst) == 1,
        "the first probe reads one window"
    );
    anyhow::ensure!(engine.origin_range_serviceable(hash, total).await?);
    anyhow::ensure!(
        fetches.load(Ordering::SeqCst) == 1,
        "a confirmed origin is not probed again"
    );
    Ok(())
}

/// An origin that publishes the outboard but declines ranges is not
/// serviceable, so the caller degrades before it signs.
#[tokio::test]
async fn origin_range_serviceable_is_false_without_range_support() -> anyhow::Result<()> {
    let (data, hash, mut origin, _) = multi_window_stub();
    let total = u64::try_from(data.len())?;
    origin.support_range = false;
    let (engine, _tmp) = stub_engine(vec![origin]).await?;
    anyhow::ensure!(!engine.origin_range_serviceable(hash, total).await?);
    Ok(())
}

/// A wrong-length first window cannot verify: the probe reports it as a hard
/// fault, not as serviceable.
#[tokio::test]
async fn origin_range_serviceable_faults_on_a_short_first_window() -> anyhow::Result<()> {
    let (data, hash, mut origin, _) = multi_window_stub();
    let total = u64::try_from(data.len())?;
    origin.short_from = 0;
    let (engine, _tmp) = stub_engine(vec![origin]).await?;
    let probed = engine.origin_range_serviceable(hash, total).await;
    anyhow::ensure!(
        matches!(probed, Err(CacheError::VerifyFailed { .. })),
        "a short first window must fault, got {probed:?}"
    );
    Ok(())
}

/// A blob of one chunk group has an empty outboard, which the outboard cache
/// never holds. The latch still keys on the origin that served it, so the
/// second probe reads no range.
#[tokio::test]
async fn origin_range_serviceable_latches_a_small_blob() -> anyhow::Result<()> {
    use bao_tree::io::outboard::PreOrderMemOutboard;

    let data: Vec<u8> = (0..1000u32).map(|i| (i % 251) as u8).collect();
    let ob = PreOrderMemOutboard::create(&data, crate::range_pull::IROH_BLOCK_SIZE);
    anyhow::ensure!(ob.data.is_empty(), "one chunk group has an empty outboard");
    let hash = Hash::from(*ob.root.as_bytes());
    let origin = RangeStubOrigin::serving(hash, &data, Bytes::from(ob.data));
    let fetches = Arc::clone(&origin.range_fetches);
    let (engine, _tmp) = stub_engine(vec![origin]).await?;
    let total = u64::try_from(data.len())?;
    anyhow::ensure!(engine.origin_range_serviceable(hash, total).await?);
    anyhow::ensure!(engine.origin_range_serviceable(hash, total).await?);
    anyhow::ensure!(
        fetches.load(Ordering::SeqCst) == 1,
        "a confirmed origin is probed once, got {}",
        fetches.load(Ordering::SeqCst)
    );
    Ok(())
}

/// A first origin that publishes the outboard but declines ranges does not
/// shadow a second origin that serves both; the probe confirms the second.
#[tokio::test]
async fn origin_range_serviceable_walks_past_a_range_declining_origin() -> anyhow::Result<()> {
    let (data, hash, mut declining, _) = multi_window_stub();
    let total = u64::try_from(data.len())?;
    declining.support_range = false;
    let (_, _, serving, _) = multi_window_stub();
    let declined = Arc::clone(&declining.range_fetches);
    let served = Arc::clone(&serving.range_fetches);
    let (engine, _tmp) = stub_engine(vec![declining, serving]).await?;
    anyhow::ensure!(engine.origin_range_serviceable(hash, total).await?);
    anyhow::ensure!(
        served.load(Ordering::SeqCst) == 1,
        "the second origin serves the probe"
    );
    let before = declined.load(Ordering::SeqCst);
    anyhow::ensure!(engine.origin_range_serviceable(hash, total).await?);
    anyhow::ensure!(
        declined.load(Ordering::SeqCst) == before && served.load(Ordering::SeqCst) == 1,
        "a second probe reads no range from either origin"
    );
    Ok(())
}

/// A first origin that serves a wrong-length first window does not shadow a
/// healthy second origin: the probe confirms the second, and a draw opens
/// on it and completes.
#[tokio::test]
async fn a_short_first_window_advances_to_a_healthy_origin() -> anyhow::Result<()> {
    let (data, hash, mut short, aligned) = multi_window_stub();
    let total = u64::try_from(data.len())?;
    short.short_from = 0;
    let (_, _, healthy, _) = multi_window_stub();
    let (engine, _tmp) = stub_engine(vec![short, healthy]).await?;
    anyhow::ensure!(engine.origin_range_serviceable(hash, total).await?);
    let wire = engine
        .origin_range_wire(hash, &aligned)
        .await?
        .expect("opens on the healthy origin");
    let (_, fault) = drain_wire(wire).await;
    anyhow::ensure!(
        fault.is_none(),
        "the healthy origin's wire is whole: {fault:?}"
    );
    Ok(())
}

/// A draw that waits on a full pool past the warning threshold keeps its
/// place and opens once a permit frees.
#[tokio::test]
async fn a_permit_wait_past_the_warning_keeps_waiting() -> anyhow::Result<()> {
    use std::time::Duration;

    let (_, hash, mut origin, aligned) = multi_window_stub();
    origin.gate_from = crate::RANGE_PULL_WINDOW_BYTES;
    let (engine, _tmp) = stub_engine(vec![origin]).await?;
    let mut wires = Vec::new();
    for _ in 0..crate::MAX_CONCURRENT_RANGE_PULLS {
        let mut wire = engine
            .origin_range_wire(hash, &aligned)
            .await?
            .expect("opens");
        anyhow::ensure!(matches!(wire.next_chunk().await, Some(Ok(_))));
        wires.push(wire);
    }
    tokio::time::pause();
    let waiting = engine.origin_range_wire(hash, &aligned);
    tokio::pin!(waiting);
    anyhow::ensure!(
        tokio::time::timeout(RANGE_PULL_PERMIT_WARN_AFTER * 2, &mut waiting)
            .await
            .is_err(),
        "the warning must not end the wait"
    );
    tokio::time::resume();
    drop(wires.pop());
    let opened = tokio::time::timeout(Duration::from_secs(10), waiting).await??;
    anyhow::ensure!(
        opened.is_some(),
        "the waiting draw opens once a permit frees"
    );
    Ok(())
}

/// An origin that stops serving (declines) a later window ends the wire on
/// an `OriginError`, not a `VerifyFailed` and not a clean end.
#[tokio::test]
async fn origin_range_wire_mid_stream_decline_is_origin_error() -> anyhow::Result<()> {
    let (_, hash, mut origin, aligned) = multi_window_stub();
    origin.decline_from = crate::RANGE_PULL_WINDOW_BYTES;
    let (engine, _tmp) = stub_engine(vec![origin]).await?;
    let wire = engine
        .origin_range_wire(hash, &aligned)
        .await?
        .expect("opens");
    let (_, fault) = drain_wire(wire).await;
    anyhow::ensure!(
        matches!(fault, Some(CacheError::OriginError { .. })),
        "a mid-stream decline must be an OriginError, got {fault:?}"
    );
    Ok(())
}

/// A transport fault on a later window ends the wire on an `OriginError`.
#[tokio::test]
async fn origin_range_wire_mid_stream_transport_fault_is_origin_error() -> anyhow::Result<()> {
    let (_, hash, mut origin, aligned) = multi_window_stub();
    origin.fail_from = crate::RANGE_PULL_WINDOW_BYTES;
    let (engine, _tmp) = stub_engine(vec![origin]).await?;
    let wire = engine
        .origin_range_wire(hash, &aligned)
        .await?
        .expect("opens");
    let (_, fault) = drain_wire(wire).await;
    anyhow::ensure!(
        matches!(fault, Some(CacheError::OriginError { .. })),
        "a mid-stream transport fault must be an OriginError, got {fault:?}"
    );
    Ok(())
}

/// A panic inside the encode task never reads as a clean end, and never as
/// an origin or store fault: the wire ends on an `Internal` fault that
/// carries the panic's message, and the task still releases its permit.
#[tokio::test]
async fn origin_range_wire_panicking_encode_ends_on_an_internal_fault() -> anyhow::Result<()> {
    use std::time::Duration;

    let (_, hash, mut origin, aligned) = multi_window_stub();
    origin.panic_from = crate::RANGE_PULL_WINDOW_BYTES;
    let (engine, _tmp) = stub_engine(vec![origin]).await?;
    for _ in 0..=crate::MAX_CONCURRENT_RANGE_PULLS {
        let wire = tokio::time::timeout(
            Duration::from_secs(10),
            engine.origin_range_wire(hash, &aligned),
        )
        .await
        .map_err(|_| anyhow::anyhow!("a panicked encode must release its permit"))??
        .expect("opens");
        let (_, fault) = drain_wire(wire).await;
        let Some(CacheError::Internal(source)) = &fault else {
            anyhow::bail!("a panicked encode must end on an Internal fault, got {fault:?}");
        };
        anyhow::ensure!(
            format!("{source:#}").contains("stub origin panics on request"),
            "the fault must carry the panic message, got {source:#}"
        );
    }
    Ok(())
}

/// Dropping a wire releases its permit, and the own-origin pool is separate
/// from the range-pull pool: a full own-origin pool does not stall a range
/// pull. A draw that finds the pool full is counted as a permit wait.
#[tokio::test]
async fn origin_range_wire_releases_its_permit_on_drop() -> anyhow::Result<()> {
    use std::time::Duration;

    let (_, hash, mut origin, aligned) = multi_window_stub();
    // Every wire reads one chunk, so its encode parks on the full wire
    // channel, holding its permit.
    origin.gate_from = crate::RANGE_PULL_WINDOW_BYTES;
    let tmp = tempfile::tempdir()?;
    let cm = Arc::new(CacheMetrics::default());
    let engine = CacheEngine::open_full(
        tmp.path(),
        vec![Arc::new(origin) as Arc<dyn Origin>],
        64,
        PinnedHashes::empty(),
        crate::RetryPolicy::default(),
        CircuitBreakerPolicy::default(),
        Some(Arc::clone(&cm)),
        Duration::ZERO,
    )
    .await?;

    let mut wires = Vec::new();
    for _ in 0..crate::MAX_CONCURRENT_RANGE_PULLS {
        let mut wire = engine
            .origin_range_wire(hash, &aligned)
            .await?
            .expect("opens");
        anyhow::ensure!(matches!(wire.next_chunk().await, Some(Ok(_))));
        wires.push(wire);
    }
    anyhow::ensure!(
        tokio::time::timeout(
            Duration::from_millis(200),
            engine.origin_range_wire(hash, &aligned)
        )
        .await
        .is_err(),
        "a full own-origin pool must make the next open wait",
    );
    anyhow::ensure!(
        cm.range_pull_permit_waits.get() == 1,
        "the waiting draw is counted once, got {}",
        cm.range_pull_permit_waits.get()
    );
    drop(wires);
    let reopened = tokio::time::timeout(
        Duration::from_secs(10),
        engine.origin_range_wire(hash, &aligned),
    )
    .await??;
    anyhow::ensure!(
        reopened.is_some(),
        "dropped wires must release their permits"
    );
    Ok(())
}

/// Dropping a wire whose encode is parked inside an origin window fetch
/// aborts the encode and frees its permit. The fetch never returns, so only
/// the abort ends the task: a dropped receiver alone does not.
#[tokio::test]
async fn dropping_a_wire_parked_in_an_origin_fetch_frees_its_permit() -> anyhow::Result<()> {
    use std::time::Duration;

    let (_, hash, mut origin, aligned) = multi_window_stub();
    origin.gate_from = crate::RANGE_PULL_WINDOW_BYTES;
    let (engine, _tmp) = stub_engine(vec![origin]).await?;
    let pool = Arc::clone(&engine.inner.own_origin_range_pulls);
    let mut wire = engine
        .origin_range_wire(hash, &aligned)
        .await?
        .expect("opens");
    // Drain the first window until the encode parks on the gated second fetch.
    while tokio::time::timeout(Duration::from_millis(200), wire.next_chunk())
        .await
        .is_ok()
    {}
    anyhow::ensure!(
        pool.available_permits() == crate::MAX_CONCURRENT_RANGE_PULLS - 1,
        "the parked encode must hold its permit"
    );
    drop(wire);
    tokio::time::timeout(Duration::from_secs(10), async {
        while pool.available_permits() < crate::MAX_CONCURRENT_RANGE_PULLS {
            tokio::task::yield_now().await;
        }
    })
    .await
    .map_err(|_| anyhow::anyhow!("a dropped wire must abort its parked encode"))?;
    Ok(())
}

/// The 0-byte blob streams its (empty) wire and ends cleanly.
#[tokio::test]
async fn origin_range_wire_empty_blob_ends_cleanly() -> anyhow::Result<()> {
    use bao_tree::io::outboard::PreOrderMemOutboard;

    let ob = PreOrderMemOutboard::create([], crate::range_pull::IROH_BLOCK_SIZE);
    let root: [u8; 32] = *ob.root.as_bytes();
    let hash = Hash::from(root);
    let outboard = Bytes::from(ob.data.clone());
    let origin = RangeStubOrigin::serving(hash, &[], outboard.clone());
    let (engine, _tmp) = stub_engine(vec![origin]).await?;
    let aligned = crate::range_pull::align_range(0, 0, 0)?;
    let wire = engine
        .origin_range_wire(hash, &aligned)
        .await?
        .expect("opens");
    let (wire, fault) = drain_wire(wire).await;
    anyhow::ensure!(fault.is_none(), "the empty blob must not fault: {fault:?}");
    let reference = encode_verified_range(root, &aligned, &[], outboard)?;
    anyhow::ensure!(wire.as_slice() == &reference[8..]);
    Ok(())
}

/// The origin chain advances past an origin that declines the first window
/// and past one whose outboard read faults, to one that serves.
#[tokio::test]
async fn origin_range_wire_falls_back_to_a_serving_origin() -> anyhow::Result<()> {
    let (data, hash, serving, aligned) = multi_window_stub();
    let (_, _, mut declining, _) = multi_window_stub();
    declining.support_range = false;
    let faulting = RangeStubOrigin::outboard_faulting(hash, aligned.blob_size());
    let (engine, _tmp) = stub_engine(vec![faulting, declining, serving]).await?;
    let wire = engine
        .origin_range_wire(hash, &aligned)
        .await?
        .expect("opens");
    let (wire, fault) = drain_wire(wire).await;
    anyhow::ensure!(
        fault.is_none(),
        "the serving origin must not fault: {fault:?}"
    );

    let root = *hash.as_bytes();
    let ob = bao_tree::io::outboard::PreOrderMemOutboard::create(
        &data,
        crate::range_pull::IROH_BLOCK_SIZE,
    );
    let reference = encode_verified_range(root, &aligned, &data, Bytes::from(ob.data))?;
    anyhow::ensure!(wire.as_slice() == &reference[8..], "the wire is byte-exact");
    Ok(())
}

/// Only transport faults and no serving origin: the last fault is the error.
#[tokio::test]
async fn origin_range_wire_reports_a_transport_fault_when_no_origin_serves() -> anyhow::Result<()> {
    let (_, hash, _, aligned) = multi_window_stub();
    let faulting = RangeStubOrigin::outboard_faulting(hash, aligned.blob_size());
    let (engine, _tmp) = stub_engine(vec![faulting]).await?;
    let err = engine
        .origin_range_wire(hash, &aligned)
        .await
        .err()
        .ok_or_else(|| anyhow::anyhow!("expected an OriginError"))?;
    anyhow::ensure!(matches!(err, CacheError::OriginError { .. }), "got {err:?}");
    Ok(())
}

/// A correct origin: `origin_range_wire` streams a multi-window range as
/// the same header-less wire a whole-span encode produces, asks the origin
/// for at most one window per fetch (#2065), and the wire admits under `H`.
#[tokio::test]
async fn origin_range_wire_streams_admittable_wire_in_windows() -> anyhow::Result<()> {
    use bao_tree::io::outboard::PreOrderMemOutboard;

    let data = multi_window_test_blob();
    let ob = PreOrderMemOutboard::create(&data, crate::range_pull::IROH_BLOCK_SIZE);
    let root: [u8; 32] = *ob.root.as_bytes();
    let outboard = Bytes::from(ob.data.clone());
    let hash = Hash::from(root);
    let total = u64::try_from(data.len()).unwrap_or(u64::MAX);

    let origin = Arc::new(RangeStubOrigin::serving(hash, &data, outboard.clone()));
    let tmp = tempfile::tempdir()?;
    let engine =
        CacheEngine::open(tmp.path(), vec![Arc::clone(&origin) as Arc<dyn Origin>], 64).await?;

    // Start mid-blob so the range is not window-aligned to the blob start.
    let aligned = crate::range_pull::align_range(3 * crate::CHUNK_GROUP_BYTES, 0, total)
        .map_err(|e| anyhow::anyhow!("align: {e}"))?;
    let Some(wire) = engine.origin_range_wire(hash, &aligned).await? else {
        anyhow::bail!("expected Some(wire) — the origin serves the range + outboard");
    };
    let (wire, fault) = drain_wire(wire).await;
    anyhow::ensure!(
        fault.is_none(),
        "a correct origin must not fault: {fault:?}"
    );

    let start = usize::try_from(aligned.fetch_start())?;
    let reference = encode_verified_range(root, &aligned, &data[start..], outboard)?;
    anyhow::ensure!(
        wire.as_slice() == &reference[8..],
        "the streamed wire must equal the header-less whole-span encode"
    );
    anyhow::ensure!(
        origin.max_req.load(Ordering::SeqCst) <= crate::RANGE_PULL_WINDOW_BYTES,
        "each origin read must be at most one window"
    );

    // Round-trip: admit the header-less wire into a FRESH engine, which
    // verifies it against `H`.
    let tmp2 = tempfile::tempdir()?;
    let engine2 = CacheEngine::open(tmp2.path(), vec![], 64).await?;
    let drained = engine2
        .admit_bao_stream(
            hash,
            aligned.chunk_ranges().clone(),
            total,
            Bytes::from(wire),
            None,
        )
        .await
        .map_err(|(_reader, e)| e)?;
    anyhow::ensure!(drained.is_empty(), "the wire is fully drained by admit");
    Ok(())
}

/// A corrupt own origin (a window that does NOT hash to `H`, served with the
/// genuine outboard) ends the wire on a HARD [`CacheError::VerifyFailed`] —
/// never a degrade — even when the corruption is past the first window.
#[tokio::test]
async fn origin_range_wire_hard_faults_on_mismatch() -> anyhow::Result<()> {
    use bao_tree::io::outboard::PreOrderMemOutboard;

    let genuine = multi_window_test_blob();
    let ob = PreOrderMemOutboard::create(&genuine, crate::range_pull::IROH_BLOCK_SIZE);
    let root: [u8; 32] = *ob.root.as_bytes();
    let outboard = Bytes::from(ob.data.clone());
    let hash = Hash::from(root);
    let total = u64::try_from(genuine.len()).unwrap_or(u64::MAX);

    // Same length; the second window's bytes differ, so it will not verify.
    let window = usize::try_from(crate::RANGE_PULL_WINDOW_BYTES)?;
    let mut corrupt = genuine.clone();
    for b in &mut corrupt[window..window + 1024] {
        *b ^= 0xFF;
    }
    let mut origin = RangeStubOrigin::serving(hash, &genuine, outboard);
    origin.data = Bytes::from(corrupt);
    let tmp = tempfile::tempdir()?;
    let engine =
        CacheEngine::open(tmp.path(), vec![Arc::new(origin) as Arc<dyn Origin>], 64).await?;

    let aligned =
        crate::range_pull::align_range(0, 0, total).map_err(|e| anyhow::anyhow!("align: {e}"))?;
    let Some(wire) = engine.origin_range_wire(hash, &aligned).await? else {
        anyhow::bail!("expected Some(wire) — the first window serves");
    };
    let (_, fault) = drain_wire(wire).await;
    anyhow::ensure!(
        matches!(fault, Some(CacheError::VerifyFailed { expected }) if expected == hash),
        "a corrupt own origin must be a hard VerifyFailed, got {fault:?}"
    );
    Ok(())
}

/// A later window the origin returns short cannot verify against `H`, so the
/// wire ends on a HARD [`CacheError::VerifyFailed`].
#[tokio::test]
async fn origin_range_wire_hard_faults_on_short_later_window() -> anyhow::Result<()> {
    use bao_tree::io::outboard::PreOrderMemOutboard;

    let data = multi_window_test_blob();
    let ob = PreOrderMemOutboard::create(&data, crate::range_pull::IROH_BLOCK_SIZE);
    let hash = Hash::from(*ob.root.as_bytes());
    let total = u64::try_from(data.len()).unwrap_or(u64::MAX);
    let aligned =
        crate::range_pull::align_range(0, 0, total).map_err(|e| anyhow::anyhow!("align: {e}"))?;

    let mut origin = RangeStubOrigin::serving(hash, &data, Bytes::from(ob.data.clone()));
    origin.short_from = crate::RANGE_PULL_WINDOW_BYTES;
    let tmp = tempfile::tempdir()?;
    let engine =
        CacheEngine::open(tmp.path(), vec![Arc::new(origin) as Arc<dyn Origin>], 64).await?;
    let Some(wire) = engine.origin_range_wire(hash, &aligned).await? else {
        anyhow::bail!("expected Some(wire) — the first window serves");
    };
    let (_, fault) = drain_wire(wire).await;
    anyhow::ensure!(
        matches!(fault, Some(CacheError::VerifyFailed { expected }) if expected == hash),
        "a short later window must be a hard VerifyFailed, got {fault:?}"
    );
    Ok(())
}

/// A short FIRST window is a hard [`CacheError::VerifyFailed`] at open,
/// before any wire.
#[tokio::test]
async fn origin_range_wire_hard_faults_on_short_first_window() -> anyhow::Result<()> {
    use bao_tree::io::outboard::PreOrderMemOutboard;

    let data = multi_window_test_blob();
    let ob = PreOrderMemOutboard::create(&data, crate::range_pull::IROH_BLOCK_SIZE);
    let hash = Hash::from(*ob.root.as_bytes());
    let total = u64::try_from(data.len()).unwrap_or(u64::MAX);
    let aligned =
        crate::range_pull::align_range(0, 0, total).map_err(|e| anyhow::anyhow!("align: {e}"))?;

    let mut origin = RangeStubOrigin::serving(hash, &data, Bytes::from(ob.data.clone()));
    origin.short_from = 0;
    let tmp = tempfile::tempdir()?;
    let engine =
        CacheEngine::open(tmp.path(), vec![Arc::new(origin) as Arc<dyn Origin>], 64).await?;
    let err = engine
        .origin_range_wire(hash, &aligned)
        .await
        .err()
        .ok_or_else(|| anyhow::anyhow!("expected VerifyFailed, got Ok"))?;
    anyhow::ensure!(
        matches!(err, CacheError::VerifyFailed { expected } if expected == hash),
        "a short first window must be a hard VerifyFailed, got {err:?}"
    );
    Ok(())
}

/// A wrong-length outboard cannot verify, so it is a HARD
/// [`CacheError::VerifyFailed`] before any wire is produced, and it is never
/// cached.
#[tokio::test]
async fn origin_range_wire_hard_faults_on_wrong_length_outboard() -> anyhow::Result<()> {
    let data = local_outboard_pull_test_blob();
    let hash = Hash::new(&data);
    let total = u64::try_from(data.len()).unwrap_or(u64::MAX);
    let origin = RangeStubOrigin::serving(hash, &data, Bytes::from_static(&[0u8; 64]));
    let tmp = tempfile::tempdir()?;
    let engine =
        CacheEngine::open(tmp.path(), vec![Arc::new(origin) as Arc<dyn Origin>], 64).await?;

    let aligned =
        crate::range_pull::align_range(0, 0, total).map_err(|e| anyhow::anyhow!("align: {e}"))?;
    let err = engine
        .origin_range_wire(hash, &aligned)
        .await
        .err()
        .ok_or_else(|| anyhow::anyhow!("expected VerifyFailed, got Ok"))?;
    anyhow::ensure!(
        matches!(err, CacheError::VerifyFailed { expected } if expected == hash),
        "a wrong-length outboard must be a hard VerifyFailed, got {err:?}"
    );
    anyhow::ensure!(
        engine.cached_outboard(hash, total).is_none(),
        "a wrong-length outboard must not be cached"
    );
    anyhow::ensure!(
        engine
            .origin_fetch_outboard_bytes(hash, total)
            .await?
            .is_none(),
        "the serviceability probe declines a wrong-length outboard"
    );
    Ok(())
}

/// Every draw of one hash reuses the outboard the first draw read: the origin
/// serves `{H}.obao4` once, however many draws follow.
#[tokio::test]
async fn origin_range_wire_reads_the_outboard_once_per_hash() -> anyhow::Result<()> {
    let (data, hash, origin, aligned) = multi_window_stub();
    let origin = Arc::new(origin);
    let tmp = tempfile::tempdir()?;
    let engine =
        CacheEngine::open(tmp.path(), vec![Arc::clone(&origin) as Arc<dyn Origin>], 64).await?;
    let total = u64::try_from(data.len())?;
    anyhow::ensure!(
        engine
            .origin_fetch_outboard_bytes(hash, total)
            .await?
            .is_some()
    );
    for _ in 0..3 {
        let Some(wire) = engine.origin_range_wire(hash, &aligned).await? else {
            anyhow::bail!("expected Some(wire)");
        };
        let (_, fault) = drain_wire(wire).await;
        anyhow::ensure!(fault.is_none(), "a genuine origin ends cleanly: {fault:?}");
    }
    anyhow::ensure!(
        origin.outboard_fetches.load(Ordering::SeqCst) == 1,
        "the probe and three draws must read the outboard once, read {}",
        origin.outboard_fetches.load(Ordering::SeqCst)
    );
    Ok(())
}

/// Concurrent cold misses of one hash read the outboard from the origin
/// once: the later probes wait for the first read and take its cached copy.
#[tokio::test]
async fn concurrent_cold_probes_read_the_outboard_once() -> anyhow::Result<()> {
    let (data, hash, mut origin, _) = multi_window_stub();
    origin.gate_outboard = true;
    let origin = Arc::new(origin);
    let gate = Arc::clone(&origin.gate);
    let tmp = tempfile::tempdir()?;
    let engine =
        CacheEngine::open(tmp.path(), vec![Arc::clone(&origin) as Arc<dyn Origin>], 64).await?;
    let total = u64::try_from(data.len())?;
    let probes: Vec<_> = (0..8)
        .map(|_| {
            let engine = engine.clone();
            tokio::spawn(async move { engine.origin_fetch_outboard_bytes(hash, total).await })
        })
        .collect();
    // Let every probe reach the origin read or the flight lock, then open
    // the gate for the one read in flight.
    tokio::time::sleep(Duration::from_millis(50)).await;
    gate.add_permits(64);
    for probe in probes {
        anyhow::ensure!(probe.await??.is_some(), "every probe gets the outboard");
    }
    anyhow::ensure!(
        origin.outboard_fetches.load(Ordering::SeqCst) == 1,
        "eight concurrent probes must read the outboard once, read {}",
        origin.outboard_fetches.load(Ordering::SeqCst)
    );
    Ok(())
}

/// `pull_through_bytes` counts the outboard once, at the origin read, plus
/// each data window; the probe and later draws that reuse the cached copy do
/// not count it again.
#[tokio::test]
async fn pull_through_bytes_counts_the_outboard_once() -> anyhow::Result<()> {
    let (data, hash, origin, aligned) = multi_window_stub();
    let outboard_len = u64::try_from(origin.outboard.as_ref().map_or(0, Bytes::len))?;
    let tmp = tempfile::tempdir()?;
    let cm = Arc::new(CacheMetrics::default());
    let engine = CacheEngine::open_full(
        tmp.path(),
        vec![Arc::new(origin) as Arc<dyn Origin>],
        64,
        crate::PinnedHashes::default(),
        crate::RetryPolicy::default(),
        CircuitBreakerPolicy::default(),
        Some(Arc::clone(&cm)),
        Duration::ZERO,
    )
    .await?;
    let total = u64::try_from(data.len())?;
    anyhow::ensure!(
        engine
            .origin_fetch_outboard_bytes(hash, total)
            .await?
            .is_some()
    );
    let draws = 2u64;
    for _ in 0..draws {
        let Some(wire) = engine.origin_range_wire(hash, &aligned).await? else {
            anyhow::bail!("expected Some(wire)");
        };
        let (_, fault) = drain_wire(wire).await;
        anyhow::ensure!(fault.is_none(), "clean drain: {fault:?}");
    }
    let span = aligned.fetch_end() - aligned.fetch_start();
    let got = cm.pull_through_bytes.get();
    anyhow::ensure!(
        got == outboard_len + draws * span,
        "want one outboard ({outboard_len}) plus {draws} spans of {span}, got {got}"
    );
    Ok(())
}

/// A draw that fails bao verification evicts the cached outboard, so the next
/// draw reads it from the origin again instead of reusing a possibly bad copy.
#[tokio::test]
async fn a_verify_fault_evicts_the_cached_outboard() -> anyhow::Result<()> {
    let (genuine, hash, mut origin, aligned) = multi_window_stub();
    let window = usize::try_from(crate::RANGE_PULL_WINDOW_BYTES)?;
    let mut corrupt = genuine;
    for b in &mut corrupt[window..window + 1024] {
        *b ^= 0xFF;
    }
    origin.data = Bytes::from(corrupt);
    let origin = Arc::new(origin);
    let tmp = tempfile::tempdir()?;
    let engine =
        CacheEngine::open(tmp.path(), vec![Arc::clone(&origin) as Arc<dyn Origin>], 64).await?;
    for round in 1..=2 {
        let Some(wire) = engine.origin_range_wire(hash, &aligned).await? else {
            anyhow::bail!("expected Some(wire) — the first window serves");
        };
        let (_, fault) = drain_wire(wire).await;
        anyhow::ensure!(
            matches!(fault, Some(CacheError::VerifyFailed { .. })),
            "a corrupt window is a hard VerifyFailed, got {fault:?}"
        );
        anyhow::ensure!(
            origin.outboard_fetches.load(Ordering::SeqCst) == round,
            "each draw after a verify fault re-reads the outboard"
        );
    }
    Ok(())
}

/// A stuck outboard fetch runs past its budget and advances the chain to
/// the next origin. With no other origin, the probe reports the timeout as
/// a fault (the #1129 latch), not as a clean absence.
#[tokio::test]
async fn a_stuck_outboard_fetch_times_out_and_advances_the_chain() -> anyhow::Result<()> {
    let (_, hash, serving, aligned) = multi_window_stub();
    let total = aligned.blob_size();
    let mut stuck =
        RangeStubOrigin::serving(hash, &[], serving.outboard.clone().unwrap_or_default());
    stuck.gate_outboard = true;
    let (engine, _tmp) = stub_engine(vec![stuck, serving]).await?;
    engine.set_origin_read_budget(Duration::from_millis(50), u64::MAX);
    anyhow::ensure!(
        engine
            .origin_fetch_outboard_bytes(hash, total)
            .await?
            .is_some(),
        "the second origin serves the outboard after the first times out"
    );

    let (_, hash, serving, _) = multi_window_stub();
    let mut stuck = RangeStubOrigin::serving(hash, &[], serving.outboard.unwrap_or_default());
    stuck.gate_outboard = true;
    let (engine, _tmp) = stub_engine(vec![stuck]).await?;
    engine.set_origin_read_budget(Duration::from_millis(50), u64::MAX);
    let err = engine.origin_fetch_outboard_bytes(hash, total).await.err();
    anyhow::ensure!(
        matches!(&err, Some(CacheError::OriginError { source, .. })
            if source.to_string().contains("budget")),
        "a lone stuck origin is a timeout fault, got {err:?}"
    );
    Ok(())
}

/// A first window that runs past its budget advances the chain to the next
/// origin, which serves the range.
#[tokio::test]
async fn a_stuck_first_window_advances_the_chain() -> anyhow::Result<()> {
    let (data, hash, serving, aligned) = multi_window_stub();
    let mut stuck =
        RangeStubOrigin::serving(hash, &data, serving.outboard.clone().unwrap_or_default());
    stuck.gate_from = 0;
    let (engine, _tmp) = stub_engine(vec![stuck, serving]).await?;
    engine.set_origin_read_budget(Duration::from_millis(50), u64::MAX);
    let Some(wire) = engine.origin_range_wire(hash, &aligned).await? else {
        anyhow::bail!("expected Some(wire) from the second origin");
    };
    let (_, fault) = drain_wire(wire).await;
    anyhow::ensure!(
        fault.is_none(),
        "the second origin serves cleanly: {fault:?}"
    );
    Ok(())
}

/// An origin whose outboard is cached but that has no data does not lend
/// that outboard to another origin's data: the next origin serves its own
/// outboard and data, so an orphaned, corrupt outboard cannot fail every
/// draw.
#[tokio::test]
async fn an_origin_without_data_does_not_lend_its_outboard() -> anyhow::Result<()> {
    let (_, hash, serving, aligned) = multi_window_stub();
    let total = aligned.blob_size();
    let genuine = serving.outboard.clone().unwrap_or_default();
    // Same length, wrong bytes: passes the length gate, fails verification.
    let corrupt = Bytes::from(vec![0u8; genuine.len()]);
    let mut orphan = RangeStubOrigin::serving(hash, &[], corrupt);
    orphan.support_range = false;
    let (engine, _tmp) = stub_engine(vec![orphan, serving]).await?;
    // The probe caches the orphan's copy: it is first in the chain.
    anyhow::ensure!(
        engine
            .origin_fetch_outboard_bytes(hash, total)
            .await?
            .is_some()
    );
    for _ in 0..2 {
        let Some(wire) = engine.origin_range_wire(hash, &aligned).await? else {
            anyhow::bail!("expected Some(wire) from the second origin");
        };
        let (_, fault) = drain_wire(wire).await;
        anyhow::ensure!(
            fault.is_none(),
            "the second origin serves cleanly: {fault:?}"
        );
    }
    anyhow::ensure!(
        engine.cached_outboard(hash, total) == Some(genuine),
        "the serving origin's outboard replaces the orphan's in the cache"
    );
    Ok(())
}

/// A window fetch that outlasts its budget ends the wire on an origin
/// transport fault, not a hang.
#[tokio::test]
async fn a_stuck_window_fetch_times_out() -> anyhow::Result<()> {
    let (_, hash, mut origin, aligned) = multi_window_stub();
    // Every window past the first waits on a gate that never opens.
    origin.gate_from = crate::RANGE_PULL_WINDOW_BYTES;
    let (engine, _tmp) = stub_engine(vec![origin]).await?;
    engine.set_origin_read_budget(Duration::from_millis(50), u64::MAX);
    let Some(wire) = engine.origin_range_wire(hash, &aligned).await? else {
        anyhow::bail!("expected Some(wire) — the first window serves");
    };
    let (_, fault) = tokio::time::timeout(Duration::from_secs(10), drain_wire(wire)).await?;
    anyhow::ensure!(
        matches!(&fault, Some(CacheError::OriginError { source, .. })
            if source.to_string().contains("budget")),
        "a stuck window must end on a timeout OriginError, got {fault:?}"
    );
    Ok(())
}

/// An origin with no range support degrades to `Ok(None)` — the caller then
/// falls through to a whole-blob path.
#[tokio::test]
async fn origin_range_wire_none_when_unsupported() -> anyhow::Result<()> {
    let data = local_outboard_pull_test_blob();
    let hash = Hash::new(&data);
    let total = u64::try_from(data.len()).unwrap_or(u64::MAX);
    let mut origin = RangeStubOrigin::serving(hash, &data, Bytes::new());
    origin.outboard = None;
    origin.support_range = false;
    let tmp = tempfile::tempdir()?;
    let engine =
        CacheEngine::open(tmp.path(), vec![Arc::new(origin) as Arc<dyn Origin>], 64).await?;

    let aligned =
        crate::range_pull::align_range(0, 0, total).map_err(|e| anyhow::anyhow!("align: {e}"))?;
    anyhow::ensure!(
        engine.origin_range_wire(hash, &aligned).await?.is_none(),
        "an unsupported origin must degrade origin_range_wire to Ok(None)"
    );
    Ok(())
}

/// `origin_fetch_outboard_bytes` returns the outboard when an origin
/// publishes it, and `None` when none do.
#[tokio::test]
async fn origin_fetch_outboard_bytes_found_and_absent() -> anyhow::Result<()> {
    use bao_tree::io::outboard::PreOrderMemOutboard;

    let data = local_outboard_pull_test_blob();
    let ob = PreOrderMemOutboard::create(&data, crate::range_pull::IROH_BLOCK_SIZE);
    let outboard = Bytes::from(ob.data.clone());
    let hash = Hash::new(&data);
    let total = u64::try_from(data.len()).unwrap_or(u64::MAX);

    let serving = RangeStubOrigin::serving(hash, &data, outboard.clone());
    let tmp = tempfile::tempdir()?;
    let engine =
        CacheEngine::open(tmp.path(), vec![Arc::new(serving) as Arc<dyn Origin>], 64).await?;
    anyhow::ensure!(
        engine.origin_fetch_outboard_bytes(hash, total).await? == Some(outboard),
        "a publishing origin must return its outboard bytes"
    );

    let bare = OutboardStubOrigin::new(&data, None);
    let tmp2 = tempfile::tempdir()?;
    let engine2 =
        CacheEngine::open(tmp2.path(), vec![Arc::new(bare) as Arc<dyn Origin>], 64).await?;
    anyhow::ensure!(
        engine2
            .origin_fetch_outboard_bytes(hash, total)
            .await?
            .is_none(),
        "no origin publishes the outboard; must be Ok(None)"
    );
    Ok(())
}

/// A genuine transport fault fetching the outboard (a degraded own origin) is
/// surfaced as `Err`, NOT collapsed into a clean `Ok(None)` absence — so the
/// serviceability caller can latch it into `fault_seen` (#1129) and terminate a
/// resulting miss as `InternalError` rather than a bare `NotFound`.
#[tokio::test]
async fn origin_fetch_outboard_bytes_surfaces_a_transport_fault() -> anyhow::Result<()> {
    let data = local_outboard_pull_test_blob();
    let hash = Hash::new(&data);
    let total = u64::try_from(data.len()).unwrap_or(u64::MAX);

    let faulting = RangeStubOrigin::outboard_faulting(hash, total);
    let tmp = tempfile::tempdir()?;
    let engine =
        CacheEngine::open(tmp.path(), vec![Arc::new(faulting) as Arc<dyn Origin>], 64).await?;
    anyhow::ensure!(
        engine
            .origin_fetch_outboard_bytes(hash, total)
            .await
            .is_err(),
        "an outboard transport fault must surface as Err, not a clean Ok(None)"
    );
    Ok(())
}

// -- #1607: admit_bao tags its partial so it survives GC --

fn synth_blob(len: usize) -> ([u8; 32], Vec<u8>, bytes::Bytes) {
    let mut plaintext = vec![0u8; len];
    let mut x: u32 = 0x9e37_79b9;
    for b in &mut plaintext {
        x ^= x << 13;
        x ^= x >> 17;
        x ^= x << 5;
        *b = x.to_le_bytes()[0];
    }
    let ob = bao_tree::io::outboard::PreOrderMemOutboard::create(
        &plaintext,
        crate::range_pull::IROH_BLOCK_SIZE,
    );
    (*ob.root.as_bytes(), plaintext, bytes::Bytes::from(ob.data))
}

fn bao_for(
    root: [u8; 32],
    plaintext: &[u8],
    outboard: bytes::Bytes,
    off: u64,
    len: u64,
    total: u64,
) -> (Hash, bao_tree::ChunkRanges, bytes::Bytes) {
    let aligned = crate::range_pull::align_range(off, len, total).unwrap();
    let s = aligned.fetch_start() as usize;
    let e = aligned.fetch_end() as usize;
    let encoded =
        crate::range_pull::encode_verified_range(root, &aligned, &plaintext[s..e], outboard)
            .unwrap();
    (Hash::from(root), aligned.chunk_ranges().clone(), encoded)
}

async fn count_tags_for(engine: &CacheEngine, hash: Hash) -> usize {
    let mut stream = engine.inner.store.tags().list().await.unwrap();
    let mut n = 0usize;
    while let Some(info) = stream.next().await {
        if info.unwrap().hash == hash {
            n += 1;
        }
    }
    n
}

#[tokio::test]
async fn admit_bao_tags_the_partial() {
    let tmp = tempfile::tempdir().unwrap();
    let engine = CacheEngine::open(tmp.path(), vec![], 16).await.unwrap();
    let group = crate::CHUNK_GROUP_BYTES;
    let total = 4 * group;
    let (root, plaintext, outboard) = synth_blob(total as usize);

    // Admit one interior group -> a genuine partial.
    let (hash, ranges, bao) = bao_for(root, &plaintext, outboard.clone(), group, group, total);
    engine.admit_bao(hash, ranges, bao).await.unwrap();
    assert!(
        !engine.present_ranges(hash).await.unwrap().is_complete(),
        "still partial"
    );
    assert_eq!(
        count_tags_for(&engine, hash).await,
        1,
        "partial admit creates exactly one protecting tag"
    );

    // Idempotent: admit a second group -> still exactly one tag.
    let (h2, r2, b2) = bao_for(root, &plaintext, outboard, 2 * group, group, total);
    engine.admit_bao(h2, r2, b2).await.unwrap();
    assert_eq!(
        count_tags_for(&engine, hash).await,
        1,
        "re-admit does not proliferate tags"
    );
}

#[tokio::test]
async fn protect_partial_skips_store_write_when_memoized() {
    let tmp = tempfile::tempdir().unwrap();
    let engine = CacheEngine::open(tmp.path(), vec![], 16).await.unwrap();
    let group = crate::CHUNK_GROUP_BYTES;
    let total = 4 * group;
    let (root, plaintext, outboard) = synth_blob(total as usize);

    // First admit writes the protecting tag and memoizes it.
    let (hash, ranges, bao) = bao_for(root, &plaintext, outboard, group, group, total);
    engine.admit_bao(hash, ranges, bao).await.unwrap();
    assert_eq!(count_tags_for(&engine, hash).await, 1);
    assert!(engine.inner.partial_protected.contains_key(&hash));

    // Delete the tag directly at the store, leaving the memo intact. A
    // memoized `protect_partial` must short-circuit and NOT re-create it —
    // proving it skipped the redundant store write on re-admit.
    let name = format!("decdn-partial-{hash}");
    engine
        .inner
        .store
        .tags()
        .delete(name.as_bytes())
        .await
        .unwrap();
    assert_eq!(count_tags_for(&engine, hash).await, 0);
    engine.protect_partial(hash).await.unwrap();
    assert_eq!(
        count_tags_for(&engine, hash).await,
        0,
        "a memoized protect_partial must skip the store write"
    );
}

#[tokio::test]
async fn dropping_tags_reinvalidates_protect_memo() {
    let tmp = tempfile::tempdir().unwrap();
    let engine = CacheEngine::open(tmp.path(), vec![], 16).await.unwrap();
    let group = crate::CHUNK_GROUP_BYTES;
    let total = 4 * group;
    let (root, plaintext, outboard) = synth_blob(total as usize);

    let (hash, ranges, bao) = bao_for(root, &plaintext, outboard, group, group, total);
    engine.admit_bao(hash, ranges, bao).await.unwrap();
    assert!(engine.inner.partial_protected.contains_key(&hash));

    // The tag-drop path clears the memo, so a re-admit re-protects rather
    // than trusting a stale entry for a tag that no longer exists.
    engine.drop_named_tags_for(hash).await.unwrap();
    assert_eq!(count_tags_for(&engine, hash).await, 0);
    assert!(
        !engine.inner.partial_protected.contains_key(&hash),
        "dropping the tag must invalidate the memo"
    );
    engine.protect_partial(hash).await.unwrap();
    assert_eq!(
        count_tags_for(&engine, hash).await,
        1,
        "protect_partial re-creates the tag after the memo is invalidated"
    );
}

// -- admit_bao_stream — O(chunk-group) streaming range admit --

#[tokio::test]
async fn admit_bao_stream_admits_a_partial_range() {
    let tmp = tempfile::tempdir().unwrap();
    let engine = CacheEngine::open(tmp.path(), vec![], 16).await.unwrap();
    let group = crate::CHUNK_GROUP_BYTES;
    let total = 4 * group;
    let (root, plaintext, outboard) = synth_blob(total as usize);
    let (hash, ranges, bao) = bao_for(root, &plaintext, outboard, group, group, total);

    // `bao_for` (via `encode_verified_range`) prepends the 8-byte LE size
    // header the in-memory `import_bao_bytes` path expects. The wire
    // `admit_bao_stream` consumes is header-less (ADR 038) — the size
    // comes from `total_bytes` instead — so strip it here to synthesize
    // that header-less wire for the reader.
    assert!(bao.len() > 8, "bao_for output must carry the 8-byte header");
    let header_less = bao.slice(8..);

    let reader = engine
        .admit_bao_stream(hash, ranges.clone(), total, header_less, None)
        .await
        .map_err(|(_reader, e)| e)
        .unwrap();
    assert_eq!(reader.len(), 0, "the reader is fully drained");

    let present = engine.present_ranges(hash).await.unwrap();
    assert!(!present.is_complete(), "still partial");
    assert!(!present.is_empty(), "the admitted range is present");
    assert_eq!(
        count_tags_for(&engine, hash).await,
        1,
        "streaming admit creates exactly one protecting tag"
    );
    assert!(
        engine.eviction_candidates().contains_key(&hash),
        "a streamed partial is an eviction candidate with no serve after it (#2157)"
    );
}

#[tokio::test]
async fn admit_bao_stream_captures_proof_into_the_session_during_import() {
    // With a serve leg attached, the decode pass captures the range's proof
    // nodes straight into the shared session — no post-admit `export_bao`
    // read-back. Prove the captured set equals exactly what the read-back would
    // have recovered, so a serve leg reads back an identical outboard.
    use bao_tree::io::fsm::Outboard;
    let tmp = tempfile::tempdir().unwrap();
    let engine = CacheEngine::open(tmp.path(), vec![], 16).await.unwrap();
    let group = crate::CHUNK_GROUP_BYTES;
    let total = 4 * group;
    let (root, plaintext, outboard) = synth_blob(total as usize);
    let (hash, ranges, bao) = bao_for(root, &plaintext, outboard, group, group, total);
    let header_less = bao.slice(8..);

    let session = crate::FillSession::new(bao_tree::blake3::Hash::from(root), total);
    engine
        .admit_bao_stream(hash, ranges.clone(), total, header_less, Some(&session))
        .await
        .map_err(|(_reader, e)| e)
        .unwrap();

    // The proof nodes an `export_bao` read-back would recover for this range.
    let expected = engine.outboard_pairs(hash, &ranges).await.unwrap();
    assert!(
        !expected.is_empty(),
        "the admitted range spans interior proof nodes"
    );
    // Every one was captured into the session during import: a reader minted
    // from the session loads each without awaiting a further fill.
    let mut reader = session.outboard_reader();
    for (node, pair) in expected {
        assert_eq!(
            reader.load(node).await.unwrap(),
            Some(pair),
            "node {node:?} was captured during import"
        );
    }
}

/// #2328: once the pull has faulted, a serve reader with the store fallback
/// reads every node the store holds content under from the store's outboard.
/// A node over absent content still fails, and so does a reader without the
/// fallback.
#[tokio::test]
async fn a_dead_reader_reads_a_node_over_held_content_from_the_store() {
    use bao_tree::io::fsm::Outboard;
    let tmp = tempfile::tempdir().unwrap();
    let engine = CacheEngine::open(tmp.path(), vec![], 16).await.unwrap();
    let group = crate::CHUNK_GROUP_BYTES;
    let total = 8 * group;
    let (root, plaintext, outboard) = synth_blob(total as usize);
    let (hash, held, bao) = bao_for(root, &plaintext, outboard, group, group, total);
    engine
        .admit_bao_stream(hash, held.clone(), total, bao.slice(8..), None)
        .await
        .map_err(|(_reader, e)| e)
        .unwrap();

    let session = crate::FillSession::new(bao_tree::blake3::Hash::from(root), total);
    session.mark_ended(Err(crate::FillError::new("upstream died")));

    let truth = bao_tree::io::outboard::PreOrderMemOutboard::create(
        &plaintext,
        crate::range_pull::IROH_BLOCK_SIZE,
    );
    let tree = truth.tree;
    let internal: Vec<_> = tree
        .pre_order_nodes_iter()
        .filter(|n| tree.pre_order_offset(*n).is_some())
        .collect();
    let over_held = |n: &bao_tree::TreeNode| {
        !(&bao_tree::ChunkRanges::from(n.chunk_range()) & &held).is_empty()
    };
    assert!(internal.iter().any(over_held) && !internal.iter().all(over_held));

    let mut bare = session.outboard_reader();
    assert!(
        bare.load(tree.root()).await.is_err(),
        "without the fallback a dead node fails"
    );

    let mut reader = session
        .outboard_reader()
        .with_store_fallback(engine.clone());
    for node in internal {
        let loaded = reader.load(node).await;
        if over_held(&node) {
            let want = bao_tree::io::sync::Outboard::load(&truth, node).unwrap();
            assert_eq!(loaded.unwrap(), want, "node {node:?} reads from the store");
        } else {
            let err = loaded.expect_err("the store holds nothing under this node");
            assert!(err.to_string().contains("upstream pull failed"), "{err}");
        }
    }
}

#[tokio::test]
async fn admit_bao_stream_captures_a_leafs_proof_before_the_admit_finishes() {
    // A serve leg sharing the fill reads the first leaf's proof nodes as soon
    // as that leaf arrives, while the rest of the draw is still in flight.
    // The feed stalls for good after the first leaf, so the admit never
    // finishes, yet the root proof node is already in the session.
    use bao_tree::io::fsm::Outboard;

    /// Header-less bao wire reader that yields its bytes, then parks forever.
    struct StallingReader(bytes::Bytes);
    impl iroh_io::AsyncStreamReader for StallingReader {
        async fn read_bytes(&mut self, len: usize) -> std::io::Result<bytes::Bytes> {
            if self.0.is_empty() {
                std::future::pending::<()>().await;
            }
            Ok(self.0.split_to(self.0.len().min(len)))
        }
        async fn read<const L: usize>(&mut self) -> std::io::Result<[u8; L]> {
            if self.0.len() < L {
                std::future::pending::<()>().await;
            }
            let g = self.0.split_to(L);
            let mut out = [0u8; L];
            out.copy_from_slice(&g);
            Ok(out)
        }
    }

    let tmp = tempfile::tempdir().unwrap();
    let engine = CacheEngine::open(tmp.path(), vec![], 16).await.unwrap();
    let group = crate::CHUNK_GROUP_BYTES;
    let total = 4 * group;
    let (root, plaintext, outboard) = synth_blob(total as usize);
    let (hash, ranges, bao) = bao_for(root, &plaintext, outboard, 0, total, total);
    let wire = bao.slice(8..);

    // The pre-order wire for four leaves opens with the root pair and the left
    // child pair, then leaf 0. Feed exactly that much.
    let first_leaf_end = 2 * 64 + usize::try_from(group).unwrap();
    let reader = StallingReader(wire.slice(..first_leaf_end));
    let root_pair = (
        bao_tree::blake3::Hash::from_bytes(wire[..32].try_into().unwrap()),
        bao_tree::blake3::Hash::from_bytes(wire[32..64].try_into().unwrap()),
    );
    let root_node = bao_tree::BaoTree::new(total, crate::range_pull::IROH_BLOCK_SIZE).root();

    let session = crate::FillSession::new(bao_tree::blake3::Hash::from(root), total);
    let mut outboard_reader = session.outboard_reader();
    let admit = engine.admit_bao_stream(hash, ranges, total, reader, Some(&session));
    tokio::pin!(admit);
    let loaded = tokio::select! {
        _ = &mut admit => panic!("a stalled feed cannot finish the admit"),
        loaded = tokio::time::timeout(
            std::time::Duration::from_secs(5),
            outboard_reader.load(root_node),
        ) => loaded,
    };
    assert_eq!(
        loaded
            .expect("the root proof node is captured while the admit is in flight")
            .unwrap(),
        Some(root_pair),
    );
}

#[tokio::test]
async fn admit_bao_stream_handles_zero_total_bytes() {
    let tmp = tempfile::tempdir().unwrap();
    let engine = CacheEngine::open(tmp.path(), vec![], 16).await.unwrap();

    // The canonical empty blob is a no-op success: nothing to decode or admit.
    engine
        .admit_bao_stream(Hash::EMPTY, ChunkRanges::empty(), 0, Bytes::new(), None)
        .await
        .map_err(|(_reader, e)| e)
        .expect("admitting the empty blob is a no-op success");

    // A zero size under any OTHER hash is an upstream inconsistency (the signed
    // total_bytes disagrees with a non-empty content hash) — a Feed fault.
    let (_reader, err) = engine
        .admit_bao_stream(
            Hash::from([9u8; 32]),
            ChunkRanges::empty(),
            0,
            Bytes::new(),
            None,
        )
        .await
        .expect_err("zero size under a non-empty hash is rejected");
    assert_matches!(err, CacheError::Feed(_), "expected Feed, got {err:?}");
}

#[tokio::test]
async fn admit_bao_stream_rejects_corrupt_bao() {
    let tmp = tempfile::tempdir().unwrap();
    let engine = CacheEngine::open(tmp.path(), vec![], 16).await.unwrap();
    let group = crate::CHUNK_GROUP_BYTES;
    let total = 4 * group;
    let (root, plaintext, outboard) = synth_blob(total as usize);
    let (hash, ranges, bao) = bao_for(root, &plaintext, outboard, group, group, total);
    let mut corrupt = bao.slice(8..).to_vec();
    let flip_at = corrupt.len() / 2;
    let byte = corrupt.get_mut(flip_at).expect("non-empty header-less bao");
    *byte ^= 0xFF;

    let (_reader, err) = engine
        .admit_bao_stream(hash, ranges, total, Bytes::from(corrupt), None)
        .await
        .unwrap_err();
    assert_matches!(
        err,
        CacheError::VerifyFailed { expected } if expected == hash,
        "expected VerifyFailed, got {err:?}"
    );
    assert!(
        engine.present_ranges(hash).await.unwrap().is_empty(),
        "nothing admitted from a corrupt bao"
    );
    assert_eq!(
        count_tags_for(&engine, hash).await,
        0,
        "a rejected import must not tag a partial"
    );
}

// -- coverage: cached-block derivation (#1506) --

#[tokio::test]
async fn coverage_of_absent_hash_is_empty() {
    let tmp = tempfile::tempdir().unwrap();
    let engine = CacheEngine::open(tmp.path(), vec![], 16).await.unwrap();
    let cov = engine.coverage(Hash::from([7u8; 32])).await.unwrap();
    assert!(cov.is_empty(), "an absent hash has no covered blocks");
    let (_, size) = engine.coverage_sized(Hash::from([7u8; 32])).await.unwrap();
    assert_eq!(size, None, "an absent hash has no size to advertise");
}

#[tokio::test]
async fn coverage_of_complete_blob_is_full() {
    let tmp = tempfile::tempdir().unwrap();
    let engine = CacheEngine::open(tmp.path(), vec![], 16).await.unwrap();
    let group = crate::CHUNK_GROUP_BYTES;
    let total = 3 * group;
    let (root, plaintext, outboard) = synth_blob(total as usize);
    let (hash, ranges, bao) = bao_for(root, &plaintext, outboard, 0, total, total);
    engine.admit_bao(hash, ranges, bao).await.unwrap();
    assert!(
        engine.present_ranges(hash).await.unwrap().is_complete(),
        "whole blob admitted in one range"
    );

    let cov = engine.coverage(hash).await.unwrap();
    assert_eq!(
        cov,
        decdn_protocol::Coverage::full(decdn_protocol::num_blocks(total)),
        "a complete blob covers every discovery block it spans"
    );
}

#[tokio::test]
async fn coverage_of_partial_blob_covers_only_present_blocks() {
    let tmp = tempfile::tempdir().unwrap();
    let engine = CacheEngine::open(tmp.path(), vec![], 16).await.unwrap();
    let group = crate::CHUNK_GROUP_BYTES;
    // Total spans two 64 MiB discovery blocks: block 0 is fully in range,
    // block 1 covers the trailing 3 `group`s. iroh-blobs only reports a
    // `Partial` blob's size once the FINAL chunk is present (that is what
    // fixes the tree's total chunk count), so this admits block 0 in
    // full, then separately admits the blob's last group (establishing
    // the validated size) while leaving block 1's middle group missing —
    // block 1 stays not-covered even though the size is now known.
    let total = decdn_protocol::DISCOVERY_BLOCK_BYTES + 3 * group;
    let (root, plaintext, outboard) = synth_blob(total as usize);

    let (hash, block0_ranges, block0_bao) = bao_for(
        root,
        &plaintext,
        outboard.clone(),
        0,
        decdn_protocol::DISCOVERY_BLOCK_BYTES,
        total,
    );
    engine
        .admit_bao(hash, block0_ranges, block0_bao)
        .await
        .unwrap();

    let (_, tail_ranges, tail_bao) =
        bao_for(root, &plaintext, outboard, total - group, group, total);
    engine.admit_bao(hash, tail_ranges, tail_bao).await.unwrap();

    assert!(
        !engine.present_ranges(hash).await.unwrap().is_complete(),
        "block 1's middle group was never admitted"
    );

    let cov = engine.coverage(hash).await.unwrap();
    assert!(cov.covers(0), "block 0 was admitted in full");
    assert!(!cov.covers(1), "block 1's middle group is missing");
}

// -- On a front-prefix partial, the observe() bitfield knows the full --
// -- blob size while status() still reports it unknown (#1506). --

#[tokio::test]
async fn spike_bitfield_size_known_before_status_size_on_front_partial() {
    let tmp = tempfile::tempdir().unwrap();
    let engine = CacheEngine::open(tmp.path(), vec![], 16).await.unwrap();
    // Front-fill only: admit block 0 and NEVER admit the trailing group,
    // so iroh-blobs never sees the blob's final chunk and `status()`
    // never learns the size.
    let total = decdn_protocol::DISCOVERY_BLOCK_BYTES + 3 * crate::CHUNK_GROUP_BYTES;
    let (root, plaintext, outboard) = synth_blob(total as usize);

    let (hash, block0_ranges, block0_bao) = bao_for(
        root,
        &plaintext,
        outboard,
        0,
        decdn_protocol::DISCOVERY_BLOCK_BYTES,
        total,
    );
    engine
        .admit_bao(hash, block0_ranges, block0_bao)
        .await
        .unwrap();

    let status = engine.inner.store.blobs().status(hash).await.unwrap();
    assert_matches!(
        status,
        iroh_blobs::api::blobs::BlobStatus::Partial { size: None },
        "front-only partial must NOT have a status()-known size yet, got {status:?}"
    );

    let bitfield = engine.inner.store.blobs().observe(hash).await.unwrap();
    assert_eq!(
        bitfield.size(),
        total,
        "observe()'s bitfield must already know the full synthetic blob size"
    );
}

/// A front-prefix partial (block 0 present, no tail, `status()` size
/// still unknown) must still advertise coverage for block 0: the size
/// used to derive discovery blocks comes from the `observe()` bitfield
/// (via [`CacheEngine::present_ranges`]), not from `status()`, which
/// iroh-blobs leaves `None` until the blob's last chunk validates.
#[tokio::test]
async fn coverage_of_front_partial_covers_block_zero_before_status_knows_size() {
    let tmp = tempfile::tempdir().unwrap();
    let engine = CacheEngine::open(tmp.path(), vec![], 16).await.unwrap();
    let total = decdn_protocol::DISCOVERY_BLOCK_BYTES + 3 * crate::CHUNK_GROUP_BYTES;
    let (root, plaintext, outboard) = synth_blob(total as usize);

    let (hash, block0_ranges, block0_bao) = bao_for(
        root,
        &plaintext,
        outboard,
        0,
        decdn_protocol::DISCOVERY_BLOCK_BYTES,
        total,
    );
    engine
        .admit_bao(hash, block0_ranges, block0_bao)
        .await
        .unwrap();

    let status = engine.inner.store.blobs().status(hash).await.unwrap();
    assert_matches!(
        status,
        iroh_blobs::api::blobs::BlobStatus::Partial { size: None },
        "front-only partial must NOT have a status()-known size yet, got {status:?}"
    );

    let cov = engine.coverage(hash).await.unwrap();
    assert!(
        cov.covers(0),
        "block 0 is fully present; coverage must not depend on status()'s size"
    );
    // The partial advertises its size with its blocks (#2195), read from the
    // same bitfield, before `status()` knows it.
    let (sized_cov, size) = engine.coverage_sized(hash).await.unwrap();
    assert_eq!(sized_cov, cov, "the sized read reports the same blocks");
    assert_eq!(
        size,
        Some(total),
        "a front partial reports the full blob size"
    );
}

// -- #2186: a ranged admit that completes a discovery block announces it --

/// The next `subscribe_inserts` emission, if one is queued. The admit sends
/// before it returns, so an emission it makes is already queued here.
fn next_insert(rx: &mut broadcast::Receiver<Hash>) -> Option<Hash> {
    rx.try_recv().ok()
}

/// ADR 022 §STORE Flow: a node publishes H once it verifies its first
/// 64 MiB block, on any verified partial. The ranged `admit_bao` path must
/// therefore announce the hash when an admit completes a discovery block.
#[tokio::test]
async fn subscribe_inserts_emits_when_admit_bao_completes_a_discovery_block() {
    let tmp = tempfile::tempdir().unwrap();
    let engine = CacheEngine::open(tmp.path(), vec![], 16).await.unwrap();
    let mut rx = engine.subscribe_inserts();
    let total = decdn_protocol::DISCOVERY_BLOCK_BYTES + 3 * crate::CHUNK_GROUP_BYTES;
    let (root, plaintext, outboard) = synth_blob(total as usize);
    let (hash, ranges, bao) = bao_for(
        root,
        &plaintext,
        outboard,
        0,
        decdn_protocol::DISCOVERY_BLOCK_BYTES,
        total,
    );

    engine.admit_bao(hash, ranges, bao).await.unwrap();

    assert_eq!(
        next_insert(&mut rx),
        Some(hash),
        "an admit that completes block 0 must announce the hash"
    );
}

/// The serve leg and the node-origin pull leg fill through
/// `admit_bao_stream`, so it must announce a completed block too.
#[tokio::test]
async fn subscribe_inserts_emits_when_admit_bao_stream_completes_a_discovery_block() {
    let tmp = tempfile::tempdir().unwrap();
    let engine = CacheEngine::open(tmp.path(), vec![], 16).await.unwrap();
    let mut rx = engine.subscribe_inserts();
    let total = decdn_protocol::DISCOVERY_BLOCK_BYTES + 3 * crate::CHUNK_GROUP_BYTES;
    let (root, plaintext, outboard) = synth_blob(total as usize);
    let (hash, ranges, bao) = bao_for(
        root,
        &plaintext,
        outboard,
        0,
        decdn_protocol::DISCOVERY_BLOCK_BYTES,
        total,
    );

    engine
        .admit_bao_stream(hash, ranges, total, bao.slice(8..), None)
        .await
        .map_err(|(_reader, e)| e)
        .unwrap();

    assert_eq!(
        next_insert(&mut rx),
        Some(hash),
        "a streamed admit that completes block 0 must announce the hash"
    );
}

/// An admit that leaves every block it touches incomplete announces
/// nothing: the hash has no coverage to advertise yet.
#[tokio::test]
async fn subscribe_inserts_silent_for_an_admit_that_completes_no_block() {
    let tmp = tempfile::tempdir().unwrap();
    let engine = CacheEngine::open(tmp.path(), vec![], 16).await.unwrap();
    let mut rx = engine.subscribe_inserts();
    let group = crate::CHUNK_GROUP_BYTES;
    let total = decdn_protocol::DISCOVERY_BLOCK_BYTES + 3 * group;
    let (root, plaintext, outboard) = synth_blob(total as usize);
    let (hash, ranges, bao) = bao_for(root, &plaintext, outboard, group, group, total);

    engine.admit_bao(hash, ranges, bao).await.unwrap();

    assert_eq!(
        next_insert(&mut rx),
        None,
        "one interior group completes no discovery block"
    );
}

/// A blob under 64 MiB is one discovery block, so it announces exactly
/// when the admit that completes the blob lands.
#[tokio::test]
async fn subscribe_inserts_emits_for_a_small_blob_only_once_it_completes() {
    let tmp = tempfile::tempdir().unwrap();
    let engine = CacheEngine::open(tmp.path(), vec![], 16).await.unwrap();
    let mut rx = engine.subscribe_inserts();
    let group = crate::CHUNK_GROUP_BYTES;
    let total = 4 * group;
    let (root, plaintext, outboard) = synth_blob(total as usize);

    let (hash, front, front_bao) = bao_for(root, &plaintext, outboard.clone(), 0, 2 * group, total);
    engine.admit_bao(hash, front, front_bao).await.unwrap();
    assert_eq!(
        next_insert(&mut rx),
        None,
        "half a single-block blob completes nothing"
    );

    let (_, back, back_bao) = bao_for(root, &plaintext, outboard, 2 * group, 2 * group, total);
    engine.admit_bao(hash, back, back_bao).await.unwrap();
    assert_eq!(
        next_insert(&mut rx),
        Some(hash),
        "the admit that completes the only block must announce the hash"
    );
}

/// The short last block announces like any other: its span ends at the
/// blob's last chunk, not a full 64 MiB past its start.
#[tokio::test]
async fn subscribe_inserts_emits_when_an_admit_completes_the_short_last_block() {
    let tmp = tempfile::tempdir().unwrap();
    let engine = CacheEngine::open(tmp.path(), vec![], 16).await.unwrap();
    let mut rx = engine.subscribe_inserts();
    let group = crate::CHUNK_GROUP_BYTES;
    let block = decdn_protocol::DISCOVERY_BLOCK_BYTES;
    let total = block + 3 * group;
    let (root, plaintext, outboard) = synth_blob(total as usize);

    let (hash, tail, tail_bao) = bao_for(root, &plaintext, outboard, block, 3 * group, total);
    engine.admit_bao(hash, tail, tail_bao).await.unwrap();

    assert_eq!(
        next_insert(&mut rx),
        Some(hash),
        "the admit that completes block 1 must announce the hash"
    );
}

/// A middle-of-file fill — no front chunk, no tail chunk — still knows the
/// blob's size from the admit, so completing an interior block announces.
#[tokio::test]
async fn subscribe_inserts_emits_when_an_admit_completes_an_interior_block() {
    let tmp = tempfile::tempdir().unwrap();
    let engine = CacheEngine::open(tmp.path(), vec![], 16).await.unwrap();
    let mut rx = engine.subscribe_inserts();
    let block = decdn_protocol::DISCOVERY_BLOCK_BYTES;
    let total = 2 * block + crate::CHUNK_GROUP_BYTES;
    let (root, plaintext, outboard) = synth_blob(total as usize);

    let (hash, middle, middle_bao) = bao_for(root, &plaintext, outboard, block, block, total);
    engine.admit_bao(hash, middle, middle_bao).await.unwrap();

    assert_eq!(
        next_insert(&mut rx),
        Some(hash),
        "the admit that completes interior block 1 must announce the hash"
    );
}

/// A streamed admit that fails after the store already took a whole block
/// still announces it. The store keeps the verified items, and a gap-fill
/// retry admits only the missing ranges, so no later admit touches that
/// block again.
#[tokio::test]
async fn admit_bao_stream_announces_a_block_it_completed_before_failing() {
    let tmp = tempfile::tempdir().unwrap();
    let engine = CacheEngine::open(tmp.path(), vec![], 16).await.unwrap();
    let mut rx = engine.subscribe_inserts();
    let group = crate::CHUNK_GROUP_BYTES;
    let block = decdn_protocol::DISCOVERY_BLOCK_BYTES;
    let total = block + 3 * group;
    let (root, plaintext, outboard) = synth_blob(total as usize);
    let (hash, ranges, bao) = bao_for(root, &plaintext, outboard, 0, total, total);

    // The pre-order wire carries block 0's subtree first, so cutting the
    // last group off truncates the feed inside block 1.
    let wire = bao.slice(8..);
    let truncated = wire.slice(..wire.len() - usize::try_from(group).unwrap());
    let failed = engine
        .admit_bao_stream(hash, ranges, total, truncated, None)
        .await;
    let Err((_reader, err)) = failed else {
        panic!("a truncated feed must fail the admit");
    };
    // The sender's short delivery, not this node's store.
    assert_matches!(err, CacheError::Feed(_), "{err:?}");
    assert!(
        engine.coverage(hash).await.unwrap().covers(0),
        "fixture precondition: block 0 landed before the feed ended"
    );

    assert_eq!(
        next_insert(&mut rx),
        Some(hash),
        "a block that landed before the failure must still be announced"
    );
}

/// The cold-start seed and the lag sweep walk `iter_hashes`, so a partial
/// that ranged fills left behind must appear there.
#[tokio::test]
async fn iter_hashes_includes_partial_blobs() {
    let tmp = tempfile::tempdir().unwrap();
    let engine = CacheEngine::open(tmp.path(), vec![], 16).await.unwrap();
    let group = crate::CHUNK_GROUP_BYTES;
    let total = 4 * group;
    let (root, plaintext, outboard) = synth_blob(total as usize);
    let (hash, ranges, bao) = bao_for(root, &plaintext, outboard, group, group, total);
    engine.admit_bao(hash, ranges, bao).await.unwrap();
    assert!(!engine.present_ranges(hash).await.unwrap().is_complete());

    let hashes = engine.iter_hashes().await.unwrap();
    assert_eq!(hashes, vec![hash], "a held partial is a held blob");
}

/// A middle-range partial (no front, no tail) leaves `status()`'s size
/// unknown, yet its bytes sit on disk. `size_snapshot` — the eviction
/// footprint and the `decdn_cache_bytes` gauge — must count them, and the
/// eviction driver must be able to release them (#2157).
#[tokio::test]
async fn size_snapshot_counts_middle_range_partial_by_present_bytes() -> anyhow::Result<()> {
    let tmp = tempfile::tempdir()?;
    let engine = CacheEngine::open(tmp.path(), vec![], 16).await?;
    let group = crate::CHUNK_GROUP_BYTES;
    let total = 4 * group;
    let (root, plaintext, outboard) = synth_blob(usize::try_from(total)?);

    let (hash, ranges, bao) = bao_for(root, &plaintext, outboard, group, 2 * group, total);
    engine.admit_bao(hash, ranges, bao).await?;

    let status = engine.inner.store.blobs().status(hash).await?;
    anyhow::ensure!(
        matches!(
            status,
            iroh_blobs::api::blobs::BlobStatus::Partial { size: None }
        ),
        "a middle-range partial must leave status()'s size unknown, got {status:?}"
    );

    let sizes = engine.size_snapshot().await?;
    anyhow::ensure!(
        sizes.get(&hash).copied() == Some(2 * group),
        "size_snapshot must count the middle partial's present bytes, got {:?}",
        sizes.get(&hash)
    );
    anyhow::ensure!(
        engine.total_bytes().await? == 2 * group,
        "total_bytes must agree with size_snapshot"
    );
    anyhow::ensure!(
        engine.eviction_candidates().contains_key(&hash),
        "a counted partial must be an eviction candidate with no serve after it"
    );
    Ok(())
}

/// A partial that holds its tail has a validated `status()` size equal to
/// the whole blob, but only some of those bytes are on disk. The footprint
/// counts the present bytes, including the short last chunk (#2157).
#[tokio::test]
async fn size_snapshot_counts_tail_bearing_partial_by_present_bytes_not_total() -> anyhow::Result<()>
{
    let tmp = tempfile::tempdir()?;
    let engine = CacheEngine::open(tmp.path(), vec![], 16).await?;
    let group = crate::CHUNK_GROUP_BYTES;
    let tail_start = 3 * group;
    let tail_len = 123;
    let total = tail_start + tail_len;
    let (root, plaintext, outboard) = synth_blob(usize::try_from(total)?);

    let (hash, head_ranges, head_bao) =
        bao_for(root, &plaintext, outboard.clone(), 0, group, total);
    engine.admit_bao(hash, head_ranges, head_bao).await?;
    let (_, tail_ranges, tail_bao) =
        bao_for(root, &plaintext, outboard, tail_start, tail_len, total);
    engine.admit_bao(hash, tail_ranges, tail_bao).await?;

    let status = engine.inner.store.blobs().status(hash).await?;
    anyhow::ensure!(
        matches!(
            status,
            iroh_blobs::api::blobs::BlobStatus::Partial { size: Some(s) } if s == total
        ),
        "the tail validates the size, got {status:?}"
    );

    let sizes = engine.size_snapshot().await?;
    anyhow::ensure!(
        sizes.get(&hash).copied() == Some(group + tail_len),
        "size_snapshot must count present bytes, not the validated total {total}, got {:?}",
        sizes.get(&hash)
    );
    Ok(())
}

/// The walk re-observes a partial only once its memoized count is a TTL
/// old: within the TTL it reuses the count, after it it measures the new
/// bytes. A hash that stops being partial leaves the memo.
#[tokio::test]
async fn snapshot_reuses_a_partial_count_within_the_ttl() -> anyhow::Result<()> {
    let tmp = tempfile::tempdir()?;
    let engine = CacheEngine::open(tmp.path(), vec![], 16).await?;
    let group = crate::CHUNK_GROUP_BYTES;
    let total = 4 * group;
    let (root, plaintext, outboard) = synth_blob(usize::try_from(total)?);
    let store = &engine.inner.store;
    let memo: PartialSizeMemo = Mutex::new(HashMap::new());
    let t0 = Instant::now();

    let (hash, ranges, bao) = bao_for(root, &plaintext, outboard.clone(), group, group, total);
    engine.admit_bao(hash, ranges, bao).await?;
    let first = snapshot_blob_sizes(store, &memo, t0, None).await?;
    anyhow::ensure!(
        first.get(&hash).copied() == Some(group),
        "first walk observes"
    );

    let (_, ranges, bao) = bao_for(root, &plaintext, outboard, 2 * group, group, total);
    engine.admit_bao(hash, ranges, bao).await?;
    let within = snapshot_blob_sizes(store, &memo, t0 + PARTIAL_SIZE_TTL / 2, None).await?;
    anyhow::ensure!(
        within.get(&hash).copied() == Some(group),
        "within the TTL the walk reuses the memoized count, got {:?}",
        within.get(&hash)
    );
    let after = snapshot_blob_sizes(store, &memo, t0 + PARTIAL_SIZE_TTL, None).await?;
    anyhow::ensure!(
        after.get(&hash).copied() == Some(2 * group),
        "after the TTL the walk re-observes, got {:?}",
        after.get(&hash)
    );

    memo.lock()
        .unwrap_or_else(PoisonError::into_inner)
        .insert(Hash::new(b"no longer partial"), (1, t0));
    snapshot_blob_sizes(store, &memo, t0 + PARTIAL_SIZE_TTL, None).await?;
    let kept: Vec<Hash> = memo
        .lock()
        .unwrap_or_else(PoisonError::into_inner)
        .keys()
        .copied()
        .collect();
    anyhow::ensure!(
        kept == vec![hash],
        "the walk drops non-partial memo entries, got {kept:?}"
    );
    Ok(())
}

/// A hash that GC removes between `status()` and `observe()` must read as
/// 0 bytes, not fail: the store answers `observe` on a missing entry with
/// an empty bitfield.
#[tokio::test]
async fn observed_present_bytes_of_an_absent_hash_is_zero() -> anyhow::Result<()> {
    let tmp = tempfile::tempdir()?;
    let engine = CacheEngine::open(tmp.path(), vec![], 16).await?;
    let bytes = observed_present_bytes(engine.inner.store.blobs(), Hash::new(b"absent")).await?;
    anyhow::ensure!(bytes == 0, "an absent hash holds no bytes, got {bytes}");
    Ok(())
}

#[test]
fn present_byte_count_clamps_to_size() {
    use bao_tree::ChunkNum;
    assert_eq!(present_byte_count(&ChunkRanges::empty(), 5000), 0);
    assert_eq!(
        present_byte_count(&ChunkRanges::all(), 5000),
        5000,
        "a complete bitfield counts the whole blob, short last chunk included"
    );
    assert_eq!(
        present_byte_count(&ChunkRanges::from(ChunkNum(1)..ChunkNum(3)), 5000),
        2048,
        "a closed span counts 1 KiB per chunk"
    );
    assert_eq!(
        present_byte_count(&ChunkRanges::from(ChunkNum(4)..), 5000),
        5000 - 4096,
        "an open tail ends at the blob size"
    );
    assert_eq!(
        present_byte_count(&ChunkRanges::from(ChunkNum(4)..ChunkNum(5)), 5000),
        904,
        "a closed span over the short last chunk counts its true length"
    );
    assert_eq!(
        present_byte_count(&ChunkRanges::from(ChunkNum(10)..ChunkNum(12)), 5000),
        0,
        "a span wholly past the size counts nothing"
    );
    assert_eq!(
        present_byte_count(
            &(ChunkRanges::from(ChunkNum(0)..ChunkNum(1)) | ChunkRanges::from(ChunkNum(4)..)),
            5000
        ),
        1024 + 904,
        "disjoint spans sum"
    );
    assert_eq!(
        present_byte_count(&ChunkRanges::from(ChunkNum(1)..), 0),
        0,
        "an unknown size contributes nothing past it"
    );
}

/// Advertise (`coverage`) and serve (`partial_hit_size`, mirrored here via
/// `present_ranges` + `missing_ranges` — the exact sequence it runs)
/// must AGREE on a front-prefix partial (#1506).
///
/// Both size the blob from the `observe()` bitfield. `status()`'s size is
/// not usable here: iroh-blobs leaves it `None` for a `Partial` blob until
/// its FINAL chunk validates, so on a front-prefix partial (block 0
/// present, no tail) a `status()`-sized serve gate declines a block that
/// `coverage` already advertises as covered.
#[tokio::test]
async fn partial_hit_size_source_agrees_with_coverage_on_front_partial() {
    let tmp = tempfile::tempdir().unwrap();
    let engine = CacheEngine::open(tmp.path(), vec![], 16).await.unwrap();
    let total = decdn_protocol::DISCOVERY_BLOCK_BYTES + 3 * crate::CHUNK_GROUP_BYTES;
    let (root, plaintext, outboard) = synth_blob(total as usize);

    let (hash, block0_ranges, block0_bao) = bao_for(
        root,
        &plaintext,
        outboard,
        0,
        decdn_protocol::DISCOVERY_BLOCK_BYTES,
        total,
    );
    engine
        .admit_bao(hash, block0_ranges, block0_bao)
        .await
        .unwrap();

    let status = engine.inner.store.blobs().status(hash).await.unwrap();
    assert_matches!(
        status,
        iroh_blobs::api::blobs::BlobStatus::Partial { size: None },
        "front-only partial must NOT have a status()-known size yet, got {status:?}"
    );

    // Advertise side: coverage() says block 0 is covered.
    let cov = engine.coverage(hash).await.unwrap();
    assert!(cov.covers(0), "advertise: block 0 is fully present");

    // Serve side: partial_hit_size's exact logic — bitfield size, then a
    // fully-present check for a range inside block 0.
    let present = engine.present_ranges(hash).await.unwrap();
    let size = present.size();
    assert_ne!(size, 0, "serve: the bitfield must know the blob's size");
    let byte_offset = 0;
    let byte_len = decdn_protocol::DISCOVERY_BLOCK_BYTES;
    let missing = engine
        .missing_ranges(hash, byte_offset, byte_len, size)
        .await
        .unwrap();
    assert!(
        missing.is_empty(),
        "serve: block 0's byte range is fully present, so partial_hit_size must return \
         Some(size), agreeing with advertise's coverage(0)"
    );
}

/// An origin that admits the blob into the store itself (as the ported
/// `NodeOrigin` does) and returns `AlreadyAdmitted`; the engine must then
/// serve it from the store without re-ingesting.
///
/// The origin needs a handle to the same `CacheEngine` it is registered
/// on to call `admit_bao_stream`, but `CacheEngine::open` needs the
/// origin list up front — so the handle is late-bound through a
/// `OnceLock` set right after `open` returns, mirroring how the node
/// wires its own origin against the engine it is constructed for.
#[tokio::test]
async fn already_admitted_short_circuits_and_serves_from_store() -> anyhow::Result<()> {
    /// Header-less bao wire reader for [`CacheEngine::admit_bao_stream`]
    /// in [`already_admitted_short_circuits_and_serves_from_store`].
    struct AdmitReader(bytes::Bytes);
    impl iroh_io::AsyncStreamReader for AdmitReader {
        async fn read_bytes(&mut self, len: usize) -> std::io::Result<bytes::Bytes> {
            Ok(self.0.split_to(self.0.len().min(len)))
        }
        async fn read<const L: usize>(&mut self) -> std::io::Result<[u8; L]> {
            if self.0.len() < L {
                return Err(std::io::Error::new(
                    std::io::ErrorKind::UnexpectedEof,
                    "short",
                ));
            }
            let g = self.0.split_to(L);
            let mut out = [0u8; L];
            out.copy_from_slice(&g);
            Ok(out)
        }
    }

    /// An origin whose `fetch` admits the blob into its own engine (via
    /// a late-bound handle — see the test doc comment) and returns
    /// `AlreadyAdmitted`, exactly as the ported `NodeOrigin` will.
    #[derive(Debug)]
    struct AdmittingOrigin {
        engine: std::sync::Arc<std::sync::OnceLock<CacheEngine>>,
        hash: Hash,
        total: u64,
        wire: bytes::Bytes,
        ranges: bao_tree::ChunkRanges,
    }

    impl Origin for AdmittingOrigin {
        fn kind(&self) -> OriginKind {
            OriginKind::Peer
        }

        fn fetch(
            &self,
            hash: Hash,
            _max_bytes: u64,
        ) -> Pin<Box<dyn Future<Output = Result<OriginFetch, crate::OriginPullError>> + Send + '_>>
        {
            Box::pin(async move {
                if hash != self.hash {
                    return Ok(OriginFetch::NotFound);
                }
                let engine = self
                    .engine
                    .get()
                    .expect("engine set by the caller right after open");
                engine
                    .admit_bao_stream(
                        self.hash,
                        self.ranges.clone(),
                        self.total,
                        AdmitReader(self.wire.clone()),
                        None,
                    )
                    .await
                    .map_err(|(_reader, e)| {
                        crate::OriginPullError::Permanent(anyhow::Error::from(e))
                    })?;
                Ok(OriginFetch::AlreadyAdmitted)
            })
        }
    }

    let group = crate::CHUNK_GROUP_BYTES;
    let total = 5 * group + 321;
    let (root, plaintext, outboard) = synth_blob(total as usize);
    let (hash, ranges, wire) = bao_for(root, &plaintext, outboard, 0, total, total);
    assert!(
        wire.len() > 8,
        "bao_for output must carry the 8-byte header"
    );
    let wire = wire.slice(8..);

    let engine_cell = std::sync::Arc::new(std::sync::OnceLock::<CacheEngine>::new());
    let origin = std::sync::Arc::new(AdmittingOrigin {
        engine: engine_cell.clone(),
        hash,
        total,
        wire,
        ranges: ranges.clone(),
    }) as Arc<dyn Origin>;

    let tmp = tempfile::tempdir()?;
    let engine = CacheEngine::open(tmp.path(), vec![origin], 16).await?;
    engine_cell
        .set(engine.clone())
        .map_err(|_| anyhow::anyhow!("engine cell already set"))?;

    // populate (CommitOnly) → blob present without re-ingest.
    engine.populate(hash).await?;
    anyhow::ensure!(
        engine.has(hash).await?,
        "blob must be present after AlreadyAdmitted populate"
    );
    // get (ReturnBytes) → bytes read back from the store equal the content.
    let got = engine.get(hash).await?;
    anyhow::ensure!(
        got.as_ref() == plaintext.as_slice(),
        "served bytes must equal the blob"
    );
    Ok(())
}

#[tokio::test]
async fn tagged_partial_survives_gc_untagged_is_reclaimed() {
    use iroh_blobs::api::blobs::BlobStatus;
    let group = crate::CHUNK_GROUP_BYTES;
    let total = 4 * group;
    let tmp = tempfile::tempdir().unwrap();
    // Short GC interval so the store's internal run_gc loop sweeps quickly.
    let engine = CacheEngine::open_full(
        tmp.path(),
        vec![],
        16,
        PinnedHashes::empty(),
        RetryPolicy::disabled(),
        CircuitBreakerPolicy::default(),
        Some(std::sync::Arc::new(CacheMetrics::default())),
        std::time::Duration::from_millis(200),
    )
    .await
    .unwrap();

    // Tagged: normal admit_bao (protect_partial fires).
    let (root_a, pt_a, ob_a) = synth_blob(total as usize);
    let (ha, ra, ba) = bao_for(root_a, &pt_a, ob_a, group, group, total);
    engine.admit_bao(ha, ra, ba).await.unwrap();

    // Control: same shape, distinct hash, imported WITHOUT a tag.
    let (root_b, pt_b, ob_b) = synth_blob((total + group) as usize); // different len -> different root
    let (hb, rb, bb) = bao_for(root_b, &pt_b, ob_b, group, group, total + group);
    engine
        .inner
        .store
        .blobs()
        .import_bao_bytes(hb, rb, bb)
        .await
        .unwrap();

    assert_matches!(
        engine.inner.store.blobs().status(ha).await.unwrap(),
        BlobStatus::Partial { .. }
    );
    assert_matches!(
        engine.inner.store.blobs().status(hb).await.unwrap(),
        BlobStatus::Partial { .. }
    );

    // Poll for the control's reclaim rather than sleeping a fixed span:
    // fails fast once GC sweeps (typically the first 200ms interval), and
    // only fails if GC never reclaims the untagged control within a generous
    // budget — robust on slow/loaded CI and independent of the exact GC
    // interval. The control's `NotFound` gates the test, so a genuine GC
    // failure still fails loud; it can never silently pass.
    let deadline = std::time::Duration::from_secs(15);
    let poll = std::time::Duration::from_millis(50);
    let start = std::time::Instant::now();
    loop {
        let reclaimed = matches!(
            engine.inner.store.blobs().status(hb).await.unwrap(),
            BlobStatus::NotFound
        );
        if reclaimed {
            break;
        }
        assert!(
            start.elapsed() < deadline,
            "control never reclaimed within {deadline:?}: GC did not run"
        );
        tokio::time::sleep(poll).await;
    }

    // The tagged partial must STILL be present after the control was swept —
    // proving the tag (not timing) is what protected it.
    assert_matches!(
        engine.inner.store.blobs().status(ha).await.unwrap(),
        BlobStatus::Partial { .. },
        "tagged partial survives GC"
    );
}

/// Drain a serve-leg export stream to completion, prefixed with its
/// already-pulled `first` item. Every remaining item MUST be `Ok`: an
/// in-flight reader started before an evict has to keep delivering correct
/// bytes even after the blob is evicted and GC-swept out of the store.
/// Consumes (and thus drops) the stream, releasing its handle so the disk
/// space can free.
async fn drain_serve_leg(
    first: Bytes,
    mut stream: Pin<Box<dyn futures_util::Stream<Item = CacheResult<Bytes>> + Send>>,
) -> Bytes {
    let mut out = bytes::BytesMut::from(&first[..]);
    while let Some(item) = stream.next().await {
        let bytes = item.expect("in-flight serve-leg reader must deliver bytes despite evict + GC");
        out.extend_from_slice(&bytes);
    }
    out.freeze()
}

/// Poll the store until `hash` reports `NotFound`, or fail after `deadline`.
/// Used to gate on a GC sweep having reclaimed a blob without pinning the
/// test to the exact 200ms interval — robust on slow/loaded CI.
async fn wait_reclaimed(engine: &CacheEngine, hash: Hash, deadline: Duration) {
    use iroh_blobs::api::blobs::BlobStatus;
    let start = std::time::Instant::now();
    loop {
        if matches!(
            engine.inner.store.blobs().status(hash).await.unwrap(),
            BlobStatus::NotFound
        ) {
            return;
        }
        assert!(
            start.elapsed() < deadline,
            "blob {hash} never reclaimed within {deadline:?}: GC did not run"
        );
        tokio::time::sleep(Duration::from_millis(50)).await;
    }
}

/// Multi-observer coalescing (#1656) composes with operator eviction
/// (#279). A coalesced serve-miss fans one upstream pull out to N serve legs;
/// each serve leg reads the filling partial through its own in-flight
/// `export_bao_range_stream` handle. This test reduces that to the cache-level
/// invariant the node layer relies on: **two in-flight readers over one
/// partial, evicted mid-serve, both still finish delivering byte-for-byte
/// correct bytes even after the GC sweep has logically removed the blob.**
///
/// The load-bearing assumption: reader-pinning is
/// UNCHANGED by coalescing — N serve-leg readers survive an evict + GC sweep
/// exactly as one reader would, because each holds its own live export handle.
/// Eviction is a *logical* takedown: it drops the partial's protecting tag and blocks
/// NEW serves (`has` reports absent), but it does not tear down readers already
/// in flight.
///
/// One subtlety this pins precisely: the store flips the blob to `NotFound`
/// the instant the GC sweep deletes it — the logical delete does NOT wait for
/// the last reader. The in-flight readers still complete correctly because the
/// bytes they need stay reachable through their open handles until they drop
/// (the disk space is what frees only after the last handle closes).
#[tokio::test]
async fn evict_mid_serve_lets_coalesced_readers_finish_then_reclaims() {
    let group = crate::CHUNK_GROUP_BYTES;
    let total = 8 * group;
    let tmp = tempfile::tempdir().unwrap();
    // Short GC interval so the store's internal run_gc loop sweeps within the
    // test window (same knob as `tagged_partial_survives_gc_...`).
    let engine = CacheEngine::open_full(
        tmp.path(),
        vec![],
        16,
        PinnedHashes::empty(),
        RetryPolicy::disabled(),
        CircuitBreakerPolicy::default(),
        Some(Arc::new(CacheMetrics::default())),
        Duration::from_millis(200),
    )
    .await
    .unwrap();

    // The coalesced-fill target: a genuine partial (groups [0, 6g) of an 8g
    // blob), tagged by `admit_bao` exactly as the pull leg tags it.
    let (root, plaintext, outboard) = synth_blob(total as usize);
    let (hash, ranges, bao) = bao_for(root, &plaintext, outboard, 0, 6 * group, total);
    engine.admit_bao(hash, ranges, bao).await.unwrap();
    assert!(
        !engine.present_ranges(hash).await.unwrap().is_complete(),
        "the fill target is a genuine partial"
    );
    assert_eq!(
        count_tags_for(&engine, hash).await,
        1,
        "the partial carries its protecting tag"
    );

    // The exact wire each serve leg must deliver, captured before the evict.
    let expected = engine
        .export_bao_range(hash, 0, 6 * group, total)
        .await
        .unwrap();

    // Two coalesced serve legs: each opens an in-flight verified-range stream
    // and pulls its first frame, so both hold a live export handle when the
    // evict lands.
    let mut leg_a = engine
        .export_bao_range_stream(hash, 0, 6 * group, total)
        .await
        .unwrap();
    let mut leg_b = engine
        .export_bao_range_stream(hash, 0, 6 * group, total)
        .await
        .unwrap();
    let first_a = leg_a.next().await.expect("leg A first frame").unwrap();
    let first_b = leg_b.next().await.expect("leg B first frame").unwrap();

    // Evict mid-serve. Logical takedown: tag dropped, new serves blocked.
    engine.evict(hash).await.unwrap();
    assert!(engine.is_evicted(hash), "evict flag set");
    assert_eq!(
        count_tags_for(&engine, hash).await,
        0,
        "evict drops the protecting tag"
    );
    assert!(
        !engine.has(hash).await.unwrap(),
        "a NEW serve is blocked immediately after evict"
    );

    // Wait — while BOTH serve legs are still held — for the target itself to
    // report `NotFound`. This is the documented subtlety made an assertion:
    // the GC sweep deletes the untagged blob and flips its logical status the
    // instant it runs, without waiting for the in-flight readers. Gating on
    // the target (not a proxy) both proves a sweep ran after the tag drop and
    // pins that the delete does not defer to the last reader.
    wait_reclaimed(&engine, hash, Duration::from_secs(15)).await;

    // The heart of the composition: BOTH in-flight readers, started before the
    // evict, still drain to completion with byte-for-byte identical bytes even
    // though the blob was already logically removed by the sweep above. Each
    // reader's open export handle keeps its bytes reachable — reader-pinning
    // held for two readers exactly as it would for one.
    let served_a = drain_serve_leg(first_a, leg_a).await;
    let served_b = drain_serve_leg(first_b, leg_b).await;
    assert_eq!(
        served_a, expected,
        "serve leg A delivered the full range intact"
    );
    assert_eq!(
        served_b, expected,
        "serve leg B delivered the full range intact"
    );
}