segment-buffer 0.5.5

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

#[derive(Serialize, Deserialize, Clone, PartialEq, Debug)]
struct TestItem {
    id: u64,
    payload: String,
}

fn test_item(n: u64) -> TestItem {
    TestItem {
        id: n,
        payload: format!("payload-{n}"),
    }
}

type TestBuffer = SegmentBuffer<TestItem>;

/// Shared test config: small batch, auto-flush effectively disabled. Only
/// `max_size_bytes` varies between tests, so it is the single parameter.
fn test_config(max_size_bytes: u64) -> SegmentConfig {
    SegmentConfig {
        flush_policy: FlushPolicy::Batch(4),
        max_size_bytes,
        compression_level: 3,
        durability: DurabilityPolicy::Segment,
        cipher: None,
    }
}

fn test_buffer(dir: &Path) -> TestBuffer {
    SegmentBuffer::open(dir, test_config(1024 * 1024)).expect("Failed to create buffer")
}

/// Buffer with `max_size_bytes=1000` so pressure percentages are exact.
fn pressure_test_buffer(dir: &Path) -> TestBuffer {
    SegmentBuffer::open(dir, test_config(1000)).expect("Failed to create pressure-test buffer")
}

fn set_disk_bytes<T>(buf: &SegmentBuffer<T>, bytes: u64) {
    buf.approx_disk_bytes
        .store(bytes, std::sync::atomic::Ordering::Relaxed);
}

// =========================================================================
// Filename parsing
// =========================================================================

#[test]
fn parse_filename_roundtrip() {
    use super::segment::parse_filename;

    let range = parse_filename("seg_000000000000_000000000255.zst").unwrap();
    assert_eq!(range.start, 0);
    assert_eq!(range.end, 255);

    let range = parse_filename("seg_000000001000_000000001099.zst").unwrap();
    assert_eq!(range.start, 1000);
    assert_eq!(range.end, 1099);

    assert!(parse_filename("not_a_segment").is_none());
    assert!(parse_filename("seg_000000000000.zst").is_none());
}

// =========================================================================
// Basic append / flush / read
// =========================================================================

#[test]
fn append_and_flush() {
    let tmp = TempDir::new().unwrap();
    let buf = test_buffer(tmp.path());

    for i in 0..3 {
        buf.append(test_item(i)).unwrap();
    }
    assert_eq!(buf.pending_count(), 3);

    buf.flush().unwrap();
    assert_eq!(buf.pending_count(), 3);

    let segments = buf.scan_segments().unwrap();
    assert_eq!(segments.len(), 1);
    assert_eq!(segments[0].start, 0);
    assert_eq!(segments[0].end, 2);
}

#[test]
fn flush_preserves_unflushed_capacity_for_next_batch() {
    let tmp = TempDir::new().unwrap();
    let buf = SegmentBuffer::open(
        tmp.path(),
        SegmentConfig {
            flush_policy: FlushPolicy::Manual,
            max_size_bytes: 1024 * 1024,
            compression_level: 3,
            durability: DurabilityPolicy::Segment,
            cipher: None,
        },
    )
    .expect("Failed to create buffer");

    // Append a batch of 100 items, then flush.
    for i in 0..100 {
        buf.append(test_item(i)).unwrap();
    }
    buf.flush().unwrap();

    // After flush, `unflushed` should have been pre-allocated with the
    // previous batch's capacity, so subsequent appends don't trigger
    // log2(N) reallocs.
    let inner = buf.inner.lock();
    assert!(
        inner.unflushed.capacity() >= 100,
        "expected recycled capacity >= 100 after flush, got {}; \
         the per-flush realloc regression returned",
        inner.unflushed.capacity()
    );
}

#[test]
fn auto_flush_at_batch_threshold() {
    let tmp = TempDir::new().unwrap();
    let buf = test_buffer(tmp.path());

    for i in 0..4 {
        buf.append(test_item(i)).unwrap();
    }

    let segments = buf.scan_segments().unwrap();
    assert_eq!(segments.len(), 1, "Should auto-flush at batch threshold");
}

#[test]
fn read_from_returns_flushed_events() {
    let tmp = TempDir::new().unwrap();
    let buf = test_buffer(tmp.path());

    for i in 0..5 {
        buf.append(test_item(i)).unwrap();
    }

    let events = buf.read_from(0, 100).unwrap();
    assert_eq!(events.len(), 5);
}

#[test]
fn read_from_partial_segment() {
    let tmp = TempDir::new().unwrap();
    let buf = test_buffer(tmp.path());

    for i in 0..4 {
        buf.append(test_item(i)).unwrap();
    }

    let events = buf.read_from(2, 100).unwrap();
    assert_eq!(events.len(), 2, "Should skip first 2 events in segment");
}

#[test]
fn read_from_with_limit() {
    let tmp = TempDir::new().unwrap();
    let buf = test_buffer(tmp.path());

    for i in 0..6 {
        buf.append(test_item(i)).unwrap();
    }

    let events = buf.read_from(0, 3).unwrap();
    assert_eq!(events.len(), 3);
}

#[test]
fn delete_acked_removes_segments() {
    let tmp = TempDir::new().unwrap();
    let buf = test_buffer(tmp.path());

    for i in 0..8 {
        buf.append(test_item(i)).unwrap();
    }

    let deleted = buf.delete_acked(3).unwrap();
    assert_eq!(deleted, 1, "Should delete segment [0-3]");

    let events = buf.read_from(0, 100).unwrap();
    assert_eq!(events.len(), 4, "Should only have events 4-7");
}

#[test]
fn delete_acked_all() {
    let tmp = TempDir::new().unwrap();
    let buf = test_buffer(tmp.path());

    for i in 0..4 {
        buf.append(test_item(i)).unwrap();
    }

    let deleted = buf.delete_acked(3).unwrap();
    assert_eq!(deleted, 1);
    assert_eq!(buf.pending_count(), 0);
}

#[test]
fn delete_acked_with_unflushed_pending_keeps_backlog_honest() {
    let tmp = TempDir::new().unwrap();
    let dir = tmp.path();
    let buf = test_buffer(dir);

    // Two items stay in memory: max_batch_events is 4, no auto-flush fires.
    buf.append(test_item(0)).unwrap();
    buf.append(test_item(1)).unwrap();
    assert_eq!(buf.pending_count(), 2);

    // Consumer reads them from memory, then acks past them. There is no
    // segment file to remove, so deleted == 0.
    let deleted = buf.delete_acked(100).unwrap();
    assert_eq!(deleted, 0, "Nothing was flushed, so no segment is removed");

    // The unflushed items remain in the backlog and are still readable.
    assert_eq!(
        buf.pending_count(),
        2,
        "Unflushed items must stay counted until flushed + acknowledged"
    );
    let events = buf.read_from(0, 100).unwrap();
    assert_eq!(events.len(), 2);
}

#[test]
fn latest_sequence() {
    let tmp = TempDir::new().unwrap();
    let buf = test_buffer(tmp.path());

    assert_eq!(buf.latest_sequence(), 0);

    buf.append(test_item(0)).unwrap();
    assert_eq!(buf.latest_sequence(), 0);

    buf.append(test_item(1)).unwrap();
    assert_eq!(buf.latest_sequence(), 1);
}

// =========================================================================
// Crash recovery
// =========================================================================

#[test]
fn crash_recovery_from_segments() {
    let tmp = TempDir::new().unwrap();
    let dir = tmp.path();

    {
        let buf = test_buffer(dir);
        for i in 0..6 {
            buf.append(test_item(i)).unwrap();
        }
        buf.flush().unwrap();
    }

    let buf2 = test_buffer(dir);
    assert_eq!(buf2.pending_count(), 6);
    assert_eq!(buf2.latest_sequence(), 5);

    let events = buf2.read_from(0, 100).unwrap();
    assert_eq!(events.len(), 6);
}

#[test]
fn crash_recovery_loses_unflushed_events() {
    let tmp = TempDir::new().unwrap();
    let dir = tmp.path();

    {
        let buf = test_buffer(dir);
        for i in 0..6 {
            buf.append(test_item(i)).unwrap();
        }
    }

    let buf2 = test_buffer(dir);
    assert_eq!(
        buf2.pending_count(),
        4,
        "Should only recover flushed events (pending batch lost on crash)"
    );
    assert_eq!(buf2.latest_sequence(), 3);
}

#[test]
fn crash_recovery_cleans_tmp_files() {
    let tmp = TempDir::new().unwrap();
    let dir = tmp.path();

    fs::write(
        dir.join("seg_000000000000_000000000003.zst.tmp"),
        b"incomplete",
    )
    .unwrap();

    let buf = test_buffer(dir);
    assert_eq!(buf.pending_count(), 0);
    assert!(!dir.join("seg_000000000000_000000000003.zst.tmp").exists());
}

// =========================================================================
// Roundtrip integrity
// =========================================================================

#[test]
fn read_includes_pending_events() {
    let tmp = TempDir::new().unwrap();
    let buf = test_buffer(tmp.path());

    for i in 0..4 {
        buf.append(test_item(i)).unwrap();
    }

    for i in 4..7 {
        buf.append(test_item(i)).unwrap();
    }

    let events = buf.read_from(0, 100).unwrap();
    assert_eq!(events.len(), 7, "Should include 4 flushed + 3 pending");
}

#[test]
fn roundtrip_preserves_event_data() {
    let tmp = TempDir::new().unwrap();
    let buf = test_buffer(tmp.path());

    let item = test_item(42);
    buf.append(item.clone()).unwrap();
    buf.flush().unwrap();

    let events = buf.read_from(0, 100).unwrap();
    assert_eq!(events.len(), 1);
    assert_eq!(events[0], item);
}

// =========================================================================
// Pressure / overload (store_pressure stays in the crate; should_accept is removed)
// =========================================================================

#[test]
fn store_pressure_returns_0_when_no_limit() {
    let tmp = TempDir::new().unwrap();
    let buf: TestBuffer = SegmentBuffer::open(tmp.path(), test_config(0)).expect("create buffer");
    assert_eq!(buf.store_pressure(), 0.0);
    assert!(!buf.is_overloaded());
}

#[test]
fn store_pressure_bounded_at_1_0_when_disk_exceeds_limit() {
    let tmp = TempDir::new().unwrap();
    let buf: TestBuffer = SegmentBuffer::open(tmp.path(), test_config(1)).expect("create buffer");
    set_disk_bytes(&buf, 999_999_999);
    let pressure = buf.store_pressure();
    assert!(
        (pressure - 1.0).abs() < f32::EPSILON,
        "Pressure should be clamped to 1.0, got {pressure}"
    );
    assert!(buf.is_overloaded());
}

#[test]
fn is_overloaded_true_above_90_percent() {
    let tmp = TempDir::new().unwrap();
    let buf = pressure_test_buffer(tmp.path());
    set_disk_bytes(&buf, 901); // 90.1%
    assert!(buf.is_overloaded());
}

#[test]
fn is_overloaded_false_at_or_below_90_percent() {
    let tmp = TempDir::new().unwrap();
    let buf = pressure_test_buffer(tmp.path());
    set_disk_bytes(&buf, 900); // exactly 90%
    assert!(
        !buf.is_overloaded(),
        "is_overloaded is pressure > 0.9, not >="
    );
}

// =========================================================================
// Concurrency stress test — 4 writers + 1 reader, 10K events
// =========================================================================

#[test]
fn concurrency_4_writers_1_reader_10k_events() {
    let tmp = TempDir::new().unwrap();
    // FlushPolicy::Manual keeps all items in-memory during the concurrent phase.
    // The purpose is to stress-test append/read correctness under contention,
    // not disk I/O. With Batch(4) this test would create 2_500 segment files.
    let buf = Arc::new(
        SegmentBuffer::open(
            tmp.path(),
            SegmentConfig {
                flush_policy: FlushPolicy::Manual,
                ..test_config(1024 * 1024)
            },
        )
        .unwrap(),
    );
    const WRITERS: usize = 4;
    const PER_WRITER: usize = 2_500;
    const TOTAL: usize = WRITERS * PER_WRITER; // 10_000

    let latest_seen = Arc::new(Mutex::new(0u64));

    thread::scope(|s| {
        // Reader thread: polls read_from until all events seen
        let buf_r = Arc::clone(&buf);
        let latest_r = Arc::clone(&latest_seen);
        s.spawn(move || loop {
            let start = *latest_r.lock();
            if start >= TOTAL as u64 {
                break;
            }
            if let Ok(events) = buf_r.read_from(start, 500) {
                if !events.is_empty() {
                    *latest_r.lock() = start + events.len() as u64;
                }
            }
            thread::sleep(Duration::from_micros(50));
        });

        // 4 writer threads, each appending 2_500 events
        for writer_id in 0..WRITERS {
            let buf_w = Arc::clone(&buf);
            s.spawn(move || {
                for i in 0..PER_WRITER {
                    let _ = buf_w.append(test_item((writer_id * PER_WRITER + i) as u64));
                }
            });
        }
    });

    // All threads joined. Flush any remaining in-memory events.
    buf.flush().unwrap();

    // Verify: all 10K events assigned, all recoverable
    assert_eq!(buf.latest_sequence(), (TOTAL - 1) as u64);
    assert_eq!(buf.pending_count(), TOTAL as u64);

    let all_events = buf.read_from(0, TOTAL * 2).unwrap();
    assert_eq!(
        all_events.len(),
        TOTAL,
        "All {TOTAL} events should be recoverable"
    );
}

// =========================================================================
// Concurrency stress test — BatchOrIntervalMin under contention
// =========================================================================

#[test]
fn concurrency_batch_or_interval_min_4_writers_10k_events() {
    // Proves that BatchOrIntervalMin is safe under concurrent append:
    // the batch_size auto-flush trigger fires during contention without
    // corrupting sequence numbers or losing items. The interval and
    // max_interval are set very high so only the batch_size trigger fires
    // (no timing flakiness).
    let tmp = TempDir::new().unwrap();
    let buf = Arc::new(
        SegmentBuffer::open(
            tmp.path(),
            SegmentConfig {
                flush_policy: FlushPolicy::BatchOrIntervalMin {
                    batch_size: 1000,
                    min_batch: 100,
                    interval: Duration::from_secs(3600),
                    max_interval: Duration::from_secs(7200),
                },
                max_size_bytes: 100 * 1024 * 1024,
                compression_level: 1,
                durability: DurabilityPolicy::Throughput,
                cipher: None,
            },
        )
        .unwrap(),
    );
    const WRITERS: usize = 4;
    const PER_WRITER: usize = 2_500;
    const TOTAL: usize = WRITERS * PER_WRITER;

    thread::scope(|s| {
        for writer_id in 0..WRITERS {
            let buf_w = Arc::clone(&buf);
            s.spawn(move || {
                for i in 0..PER_WRITER {
                    let _ = buf_w.append(test_item((writer_id * PER_WRITER + i) as u64));
                }
            });
        }
    });

    buf.flush().unwrap();

    assert_eq!(buf.latest_sequence(), (TOTAL - 1) as u64);
    assert_eq!(buf.pending_count(), TOTAL as u64);

    let all_events = buf.read_from(0, TOTAL * 2).unwrap();
    assert_eq!(all_events.len(), TOTAL);
}

// =========================================================================
// Concurrent read_from + delete_acked boundary (MPMC safety)
// =========================================================================

/// Proves the MPMC read/delete boundary documented in `DOMAIN_LANGUAGE.md`'s
/// "Consistency Model → Concurrent operation" subsection.
///
/// Under concurrent `read_from` + `delete_acked`, `read_from` may return a
/// spurious `SegmentError::Io` (`NotFound`) when the deleter removes a segment
/// between the reader's scan and its file read. This is not a bug: the deleted
/// segment was already acknowledged. The invariant this test proves is that
/// `read_from` **never returns wrong data** — every item the reader
/// successfully deserializes has the correct sequence-to-value mapping.
#[test]
fn concurrent_read_and_delete_never_corrupts() {
    let tmp = TempDir::new().unwrap();
    let buf = Arc::new(
        SegmentBuffer::open(
            tmp.path(),
            SegmentConfig {
                flush_policy: FlushPolicy::Manual,
                max_size_bytes: 100 * 1024 * 1024,
                compression_level: 1, // minimize CPU to widen the race window
                durability: DurabilityPolicy::Throughput,
                cipher: None,
            },
        )
        .unwrap(),
    );

    // Pre-populate: 50 segments × 100 items = 5 000 items on disk.
    const PER_SEG: u64 = 100;
    const SEGMENTS: u64 = 50;
    const TOTAL: u64 = PER_SEG * SEGMENTS;
    for start in (0..TOTAL).step_by(PER_SEG as usize) {
        for i in 0..PER_SEG {
            buf.append(test_item(start + i)).unwrap();
        }
        buf.flush().unwrap();
    }

    let corruption = Arc::new(std::sync::atomic::AtomicBool::new(false));

    thread::scope(|s| {
        // Reader: scans forward, verifying every item id. Retries on empty
        // (concurrent-flush window) and skips on Io error (segment deleted
        // under us — the documented boundary).
        let buf_r = Arc::clone(&buf);
        let corrupt_r = Arc::clone(&corruption);
        s.spawn(move || {
            let mut prev_id: Option<u64> = None;
            let mut pos = 0u64;
            let mut empty_retries = 0u32;
            while pos < TOTAL {
                match buf_r.read_from(pos, 500) {
                    Ok(batch) if !batch.is_empty() => {
                        empty_retries = 0;
                        for item in &batch {
                            // Every item must be in range and strictly
                            // increasing. Gaps from deleted segments are
                            // fine; reordering or duplicates are corruption.
                            if item.id >= TOTAL || prev_id.is_some_and(|p| item.id <= p) {
                                corrupt_r.store(true, std::sync::atomic::Ordering::SeqCst);
                                return;
                            }
                            prev_id = Some(item.id);
                        }
                        pos = prev_id.unwrap() + 1;
                    }
                    Ok(_) => {
                        // Empty: items moved (flush race) or deleted ahead.
                        // Retry briefly, then advance past the gap.
                        empty_retries += 1;
                        if empty_retries > 5 {
                            pos = ((pos / PER_SEG) + 1) * PER_SEG;
                            empty_retries = 0;
                        } else {
                            thread::sleep(Duration::from_micros(100));
                        }
                    }
                    Err(_) => {
                        // Io error: segment deleted between scan and read.
                        // This is the documented MPMC boundary. Skip forward.
                        pos = ((pos / PER_SEG) + 1) * PER_SEG;
                    }
                }
            }
        });

        // Deleter: removes segments from the front, racing with the reader.
        let buf_d = Arc::clone(&buf);
        s.spawn(move || {
            for acked in (PER_SEG..TOTAL).step_by(PER_SEG as usize) {
                let _ = buf_d.delete_acked(acked);
                thread::sleep(Duration::from_micros(10));
            }
        });
    });

    assert!(
        !corruption.load(std::sync::atomic::Ordering::SeqCst),
        "read_from returned wrong data under concurrent delete_acked"
    );
}

/// Proves that `read_from` under concurrent `flush` never returns corrupt
/// data. The flush-race window (Phase 1 directory scan → Phase 2 mutex gap)
/// can cause transient gaps: items that have left `unflushed` but whose
/// segment file was not yet visible to the scan. The test asserts that reads
/// never return wrong, out-of-order, or duplicate items, and that all items
/// become visible once the flusher settles.
#[test]
fn concurrent_read_and_flush_never_corrupts() {
    let tmp = TempDir::new().unwrap();
    let buf = Arc::new(
        SegmentBuffer::open(
            tmp.path(),
            SegmentConfig {
                flush_policy: FlushPolicy::Manual,
                max_size_bytes: 100 * 1024 * 1024,
                compression_level: 1, // minimize CPU to widen the race window
                durability: DurabilityPolicy::Throughput,
                cipher: None,
            },
        )
        .unwrap(),
    );

    // Half the items pre-flushed to disk, half left in `unflushed` (Manual
    // policy keeps them in memory until flush is called).
    const ON_DISK: u64 = 500;
    const IN_MEMORY: u64 = 500;
    const TOTAL: u64 = ON_DISK + IN_MEMORY;

    for i in 0..ON_DISK {
        buf.append(test_item(i)).unwrap();
    }
    buf.flush().unwrap();
    for i in ON_DISK..TOTAL {
        buf.append(test_item(i)).unwrap();
    }

    let corruption = Arc::new(std::sync::atomic::AtomicBool::new(false));

    thread::scope(|s| {
        // Reader: scans forward, verifying every item id. Tolerates transient
        // gaps (flush race) by retrying, but fails on any wrong, backwards, or
        // duplicate item.
        let buf_r = Arc::clone(&buf);
        let corrupt_r = Arc::clone(&corruption);
        s.spawn(move || {
            let mut pos = 0u64;
            let mut prev_id: Option<u64> = None;
            let mut empty_retries = 0u32;
            while pos < TOTAL {
                match buf_r.read_from(pos, 200) {
                    Ok(batch) if !batch.is_empty() => {
                        empty_retries = 0;
                        for item in &batch {
                            if item.id >= TOTAL || prev_id.is_some_and(|p| item.id <= p) {
                                corrupt_r.store(true, std::sync::atomic::Ordering::SeqCst);
                                return;
                            }
                            prev_id = Some(item.id);
                        }
                        pos = prev_id.unwrap() + 1;
                    }
                    Ok(_) => {
                        // Transient gap: items left `unflushed` but their
                        // segment was not yet scanned. Retry — the gap closes
                        // once the segment lands on disk.
                        empty_retries += 1;
                        if empty_retries > 200 {
                            break;
                        }
                        thread::sleep(Duration::from_micros(20));
                    }
                    Err(_) => {
                        thread::sleep(Duration::from_micros(20));
                    }
                }
            }
        });

        // Flusher: drains `unflushed` to disk, racing with the reader's
        // scan → lock gap.
        let buf_f = Arc::clone(&buf);
        s.spawn(move || {
            for _ in 0..20 {
                let _ = buf_f.flush();
                thread::sleep(Duration::from_micros(50));
            }
        });
    });

    assert!(
        !corruption.load(std::sync::atomic::Ordering::SeqCst),
        "read_from returned wrong data under concurrent flush"
    );

    // After the flusher settles, all items must be visible — the transient
    // gap closes once the segment is durably on disk.
    buf.flush().unwrap();
    let all = buf.read_from(0, TOTAL as usize + 10).unwrap();
    assert_eq!(
        all.len() as u64,
        TOTAL,
        "all items must be readable after flush completes"
    );
    for (i, item) in all.iter().enumerate() {
        assert_eq!(item.id, i as u64, "item at position {i} has wrong id");
    }
}

// =========================================================================
// Deterministic scan-cache TOCTOU regression test
// =========================================================================
//
// The scan-cache mtime-ordering fix (commit dc7ea7a) ensures that the
// directory mtime is captured BEFORE the readdir, not after. Without this
// ordering, a segment rename landing mid-scan pairs a post-rename mtime
// with a pre-rename (stale) segment list in the cache: the staleness guard
// then sees "no change" and serves stale data indefinitely.
//
// The test below forces the EXACT interleaving deterministically using
// `std::sync::Barrier` — no `thread::sleep`, no retry loop, no reliance on
// scheduler timing. A `HookedStore` wrapping `RealStore` injects two
// barrier wait-points into `scan()`:
//
//   1. After the readdir completes (stale snapshot captured)
//   2. Before returning that snapshot to the caller
//
// Between those two points, the mutator thread does `append + flush`,
// creating a new segment file whose rename changes the directory mtime.
// The scan returns the stale list; `scan_segments` publishes it with a
// pre-rename mtime. The NEXT `read_from` must detect the mtime change via
// `dir_mtime_changed()` and force a re-scan, recovering the missing segment.
//
// If the fix is reverted (mtime captured after the scan), the cached mtime
// would match the post-rename directory mtime, the guard would see "no
// change," and the second read would serve stale data — the assertion on
// `second.len() == 11` fails.

/// A `RealStore` wrapper whose `scan()` method uses barriers to force a
/// deterministic interleaving with a concurrent mutation. Used exclusively
/// by the scan-cache TOCTOU regression test.
///
/// The `barrier_armed` flag is set by the test harness AFTER `open_internal`
/// (whose `recover()` call does the first scan) and BEFORE the racing
/// `read_from`. This ensures exactly one scan — the second ever — passes
/// through the barrier dance. All other scans are pass-throughs to the
/// inner `RealStore`.
struct HookedStore {
    inner: store::RealStore,
    /// One-shot flag: the next `scan()` call blocks on the barriers.
    /// Set by the test after open, consumed (swapped to false) by the
    /// first scan that observes it.
    barrier_armed: std::sync::atomic::AtomicBool,
    /// Fired after `scan()` has completed its readdir (stale snapshot
    /// captured). Signals the mutator: "the directory has been read;
    /// you may now flush."
    scan_done_barrier: Arc<Barrier>,
    /// Fired after the mutator has completed its flush. Signals the
    /// scanner: "the rename has landed; you may return the stale result."
    mutation_done_barrier: Arc<Barrier>,
}

impl HookedStore {
    fn new(
        dir: PathBuf,
        scan_done_barrier: Arc<Barrier>,
        mutation_done_barrier: Arc<Barrier>,
    ) -> Self {
        Self {
            inner: store::RealStore::new(dir),
            barrier_armed: std::sync::atomic::AtomicBool::new(false),
            scan_done_barrier,
            mutation_done_barrier,
        }
    }
}

impl store::SegmentStore for HookedStore {
    fn create_dir_all(&self) -> super::Result<()> {
        self.inner.create_dir_all()
    }

    fn scan(&self) -> super::Result<Vec<super::segment::SegmentRange>> {
        // Capture the readdir result IMMEDIATELY — before any barrier.
        // This is the stale snapshot: the mutator has not yet flushed.
        let result = self.inner.scan()?;
        // Only the scan that finds the flag armed does the barrier dance.
        // swap returns the previous value; if it was true, we proceed.
        if self
            .barrier_armed
            .swap(false, std::sync::atomic::Ordering::SeqCst)
        {
            // Signal the mutator: readdir is done, flush now.
            self.scan_done_barrier.wait();
            // Wait for the mutator: flush is complete, the rename has
            // landed, the directory mtime has changed.
            self.mutation_done_barrier.wait();
        }
        // Return the stale snapshot captured before the flush.
        Ok(result)
    }

    fn clean_tmp(&self) -> super::Result<usize> {
        self.inner.clean_tmp()
    }

    fn segment_size(&self, range: super::segment::SegmentRange) -> u64 {
        self.inner.segment_size(range)
    }

    fn remove_segment(&self, range: super::segment::SegmentRange) -> super::Result<bool> {
        self.inner.remove_segment(range)
    }

    fn write_atomic(
        &self,
        range: super::segment::SegmentRange,
        payload: &[u8],
        policy: DurabilityPolicy,
    ) -> super::Result<u64> {
        self.inner.write_atomic(range, payload, policy)
    }

    fn read_bytes(&self, range: super::segment::SegmentRange) -> super::Result<Vec<u8>> {
        self.inner.read_bytes(range)
    }
}

/// Deterministic regression test for the scan-cache TOCTOU fix.
///
/// Forces the exact `scan → rename → scan-returns-stale → cache-populate`
/// interleaving via barriers, then verifies the mtime guard detects the
/// change and forces a re-scan on the next call.
///
/// **Without the fix** (mtime captured after scan): the second read serves
/// stale cached data (10 items instead of 11) and the assertion fails.
///
/// **With the fix** (mtime captured before scan): the second read detects
/// the mtime change, re-scans, and returns all 11 items.
#[test]
fn scan_cache_toctou_mtime_guard_forces_rescan_after_mid_scan_rename() {
    use std::sync::Barrier;

    let tmp = TempDir::new().unwrap();
    let dir = tmp.path().to_path_buf();

    let scan_done = Arc::new(Barrier::new(2));
    let mutation_done = Arc::new(Barrier::new(2));

    let hooked = Arc::new(HookedStore::new(
        dir.clone(),
        scan_done.clone(),
        mutation_done.clone(),
    ));
    let store_handle = hooked.clone();
    let store: Arc<dyn store::SegmentStore + Send + Sync> = hooked;

    let config = SegmentConfig {
        flush_policy: FlushPolicy::Manual,
        max_size_bytes: 100 * 1024 * 1024,
        compression_level: 1, // minimise CPU to keep the race window tight
        durability: DurabilityPolicy::Throughput,
        cipher: None,
    };

    // open_internal calls recover() which does the first scan (pass-through;
    // barrier_armed is false at this point). Lock file is None — we are the
    // sole owner, but we skip the flock to match the HookedStore pattern.
    let (buf, _report) = SegmentBuffer::<TestItem>::open_internal(dir, config, store, None)
        .expect("open_internal must succeed");

    // The mtime guard is the mechanism under test. If the filesystem does
    // not support mtime (rare: coarse-granularity FUSE / network FS), the
    // guard is disabled and this test cannot exercise it.
    assert!(
        buf.mtime_supported,
        "this test requires filesystem mtime support; \
         the open-time capability probe reported mtime as unsupported"
    );

    // Pre-populate: 10 items flushed as one segment [0..=9].
    for i in 0..10u64 {
        buf.append(test_item(i)).unwrap();
    }
    buf.flush().unwrap();
    // flush() invalidates the cache → the next read_from triggers a scan.

    // Arm the barrier: the NEXT scan (the one inside the racing read_from)
    // will block on the barriers.
    store_handle
        .barrier_armed
        .store(true, std::sync::atomic::Ordering::SeqCst);

    let buf = Arc::new(buf);

    let (first_count, second_count) = thread::scope(|s| {
        // Thread A: read_from triggers scan_segments → HookedStore.scan().
        // The HookedStore captures the stale readdir, barriers with Thread B,
        // and returns the stale list. The first read sees only [0..=9].
        // The second read must see [0..=10] via the mtime guard.
        let buf_reader = Arc::clone(&buf);
        let reader = s.spawn(move || {
            let first = buf_reader.read_from(0, 100).unwrap();
            let second = buf_reader.read_from(0, 100).unwrap();
            (first.len(), second.len())
        });

        // Thread B: append + flush during Thread A's scan.
        let buf_mutator = Arc::clone(&buf);
        s.spawn(move || {
            // Wait for Thread A's scan to complete its readdir.
            scan_done.wait();
            // Flush a new segment [10..=10]. The rename changes the dir mtime.
            buf_mutator.append(test_item(10)).unwrap();
            buf_mutator.flush().unwrap();
            // Signal Thread A: the mutation is complete.
            mutation_done.wait();
        });

        reader.join().unwrap()
    });

    // First read returned only the pre-flush segment: 10 items.
    // The scan captured the stale readdir before the flush.
    assert_eq!(
        first_count, 10,
        "first read should see only the pre-flush segments (stale scan)"
    );

    // Second read MUST see all 11 items: the mtime guard detected the
    // directory change (pre-scan mtime ≠ post-flush mtime) and forced a
    // re-scan. If the fix is reverted, this assertion fails with 10 items.
    assert_eq!(
        second_count, 11,
        "second read must recover the flushed segment via the mtime guard \
         (pre-scan mtime was stale, forcing a re-scan)"
    );
}

// =========================================================================
// Time-based auto-flush
// =========================================================================

// ---------------------------------------------------------------------
// FlushPolicy time-based decision tests (pure, no wall-clock dependency)
// ---------------------------------------------------------------------
//
// These tests call FlushPolicy::should_flush directly with synthetic time
// values instead of going through open() → append() → thread::sleep →
// check files. This eliminates CI flakiness from scheduler jitter and makes
// the exact decision boundary precisely testable.
//
// The integration path (should_flush → flush → segment file on disk) is
// already covered by auto_flush_at_batch_threshold and
// batch_or_interval_min_flushes_at_batch_size below, which trigger via
// batch_size (instantaneous, no timing dependency).

#[test]
fn batch_or_interval_flushes_after_interval() {
    let policy = FlushPolicy::BatchOrInterval {
        batch_size: 256,
        interval: Duration::from_secs(5),
    };
    // Below batch_size, before interval: no flush
    assert!(!policy.should_flush(10, Duration::from_secs(4)));
    assert!(!policy.should_flush(255, Duration::from_secs(4)));
    // Below batch_size, at interval: flush
    assert!(policy.should_flush(10, Duration::from_secs(5)));
    // Below batch_size, well past interval: flush
    assert!(policy.should_flush(1, Duration::from_secs(10)));
}

// =========================================================================
// FlushPolicy::BatchOrIntervalMin tests
// =========================================================================

#[test]
fn batch_or_interval_min_suppresses_small_flush() {
    let policy = FlushPolicy::BatchOrIntervalMin {
        batch_size: 256,
        min_batch: 10,
        interval: Duration::from_secs(5),
        max_interval: Duration::from_secs(60),
    };
    // Below min_batch, even past interval but before max_interval: NO flush
    assert!(!policy.should_flush(1, Duration::from_secs(10)));
    assert!(!policy.should_flush(9, Duration::from_secs(10)));
    assert!(!policy.should_flush(9, Duration::from_secs(59)));
    // Zero pending: never flush (unless past max_interval)
    assert!(!policy.should_flush(0, Duration::from_secs(59)));
}

#[test]
fn batch_or_interval_min_flushes_at_min_batch() {
    let policy = FlushPolicy::BatchOrIntervalMin {
        batch_size: 256,
        min_batch: 10,
        interval: Duration::from_secs(5),
        max_interval: Duration::from_secs(60),
    };
    // At min_batch AND past interval: flush
    assert!(policy.should_flush(10, Duration::from_secs(5)));
    assert!(policy.should_flush(10, Duration::from_secs(6)));
    // Well above min_batch, past interval: flush
    assert!(policy.should_flush(50, Duration::from_secs(10)));
}

#[test]
fn batch_or_interval_min_flushes_at_max_interval() {
    let policy = FlushPolicy::BatchOrIntervalMin {
        batch_size: 256,
        min_batch: 100,
        interval: Duration::from_secs(5),
        max_interval: Duration::from_secs(60),
    };
    // Below min_batch AND below batch_size, but past max_interval: flush (safety valve)
    assert!(policy.should_flush(1, Duration::from_secs(60)));
    assert!(policy.should_flush(0, Duration::from_secs(120)));
    // Before max_interval and below min_batch: no flush
    assert!(!policy.should_flush(1, Duration::from_secs(59)));
}

#[test]
fn batch_or_interval_min_flushes_at_batch_size() {
    let tmp = TempDir::new().unwrap();
    let buf: TestBuffer = SegmentBuffer::open(
        tmp.path(),
        SegmentConfig {
            flush_policy: FlushPolicy::BatchOrIntervalMin {
                batch_size: 4,
                min_batch: 100,
                interval: std::time::Duration::from_secs(30),
                max_interval: std::time::Duration::from_secs(60),
            },
            max_size_bytes: 1024 * 1024,
            compression_level: 3,
            durability: DurabilityPolicy::Segment,
            cipher: None,
        },
    )
    .expect("create buffer");

    for i in 0..4 {
        buf.append(test_item(i)).unwrap();
    }

    assert_eq!(
        buf.scan_segments().unwrap().len(),
        1,
        "Should flush immediately at batch_size regardless of min_batch/interval"
    );
}

#[test]
fn batch_or_interval_min_min_batch_zero_always_flushes_at_interval() {
    // When min_batch == 0, `pending_len >= 0` is always true, so the
    // interval trigger fires as soon as `interval` elapses — equivalent to
    // BatchOrInterval for the interval arm.
    let policy = FlushPolicy::BatchOrIntervalMin {
        batch_size: 256,
        min_batch: 0,
        interval: Duration::from_secs(5),
        max_interval: Duration::from_secs(60),
    };
    // Any pending count, at interval: flush (because 0 >= 0 is true)
    assert!(policy.should_flush(0, Duration::from_secs(5)));
    assert!(policy.should_flush(1, Duration::from_secs(5)));
    // Before interval: no flush (unless batch_size or max_interval hit)
    assert!(!policy.should_flush(0, Duration::from_secs(4)));
}

#[test]
fn batch_or_interval_min_max_equals_interval_ignores_min_batch() {
    // When max_interval == interval, the safety valve fires at the same time
    // as the interval trigger, so min_batch becomes irrelevant — every
    // interval expiry is a guaranteed flush.
    let policy = FlushPolicy::BatchOrIntervalMin {
        batch_size: 256,
        min_batch: 100,
        interval: Duration::from_secs(5),
        max_interval: Duration::from_secs(5),
    };
    // Below min_batch, but at interval (= max_interval): flush via safety valve
    assert!(policy.should_flush(1, Duration::from_secs(5)));
    assert!(policy.should_flush(0, Duration::from_secs(5)));
    // Before interval: no flush
    assert!(!policy.should_flush(50, Duration::from_secs(4)));
}

#[test]
fn batch_or_interval_min_min_batch_equals_batch_size() {
    // When min_batch == batch_size, the interval trigger requires a full
    // batch before firing — so the interval arm reduces to the batch arm,
    // and only max_interval can flush below batch_size.
    let policy = FlushPolicy::BatchOrIntervalMin {
        batch_size: 100,
        min_batch: 100,
        interval: Duration::from_secs(5),
        max_interval: Duration::from_secs(60),
    };
    // Below batch_size, past interval but before max_interval: no flush
    // (min_batch == batch_size means the interval check also requires 100)
    assert!(!policy.should_flush(99, Duration::from_secs(10)));
    assert!(!policy.should_flush(99, Duration::from_secs(59)));
    // At batch_size: flush regardless of time
    assert!(policy.should_flush(100, Duration::from_secs(0)));
    // Past max_interval: flush regardless of pending count
    assert!(policy.should_flush(0, Duration::from_secs(60)));
}

// =========================================================================
// FlushPolicy Display tests
// =========================================================================

#[test]
fn flush_policy_display_formats_each_variant() {
    assert_eq!(FlushPolicy::Batch(256).to_string(), "batch(256)");
    assert_eq!(
        FlushPolicy::Interval(Duration::from_secs(5)).to_string(),
        "interval(5s)"
    );
    assert_eq!(
        FlushPolicy::BatchOrInterval {
            batch_size: 256,
            interval: Duration::from_secs(5),
        }
        .to_string(),
        "batch_or_interval(batch=256, interval=5s)"
    );
    assert_eq!(
        FlushPolicy::BatchOrIntervalMin {
            batch_size: 256,
            min_batch: 10,
            interval: Duration::from_secs(5),
            max_interval: Duration::from_secs(60),
        }
        .to_string(),
        "batch_or_interval_min(batch=256, min=10, interval=5s, max=60s)"
    );
    assert_eq!(FlushPolicy::Manual.to_string(), "manual");
}

// =========================================================================
// Error-path tests (no encryption)
// =========================================================================

#[test]
fn corrupted_zstd_segment_returns_error_not_panic() {
    let tmp = TempDir::new().unwrap();
    let dir = tmp.path();

    let garbage_path = dir.join("seg_000000000000_000000000000.zst");
    fs::write(&garbage_path, b"this is not valid zstd data at all").unwrap();

    let buf = test_buffer(dir);
    let result = buf.read_from(0, 100);
    assert!(
        result.is_err(),
        "Corrupted zstd segment should return an error, not panic"
    );
}

#[test]
fn legacy_envelopeless_file_still_reads() {
    use super::segment;
    // Hand-build a v1-format file (no SBF1 envelope), exactly as monitor365
    // would have written it: raw zstd(CBOR), no envelope prefix.
    let tmp = TempDir::new().unwrap();
    let dir = tmp.path();

    let items = vec![test_item(7), test_item(8)];
    let mut cbor = Vec::new();
    ciborium::into_writer(&items, &mut cbor).unwrap();
    let raw_v1 = zstd::encode_all(cbor.as_slice(), 3).unwrap();

    let path = dir.join(segment::filename(7, 8));
    fs::write(&path, &raw_v1).unwrap();

    // Read via the buffer (no cipher). The envelope-less bytes should be
    // detected as legacy and decoded transparently.
    let buf = test_buffer(dir);
    let events: Vec<TestItem> = buf.read_from(7, 100).unwrap();
    assert_eq!(events.len(), 2, "legacy envelope-less file must still read");
    assert_eq!(events[0], test_item(7));
}

#[test]
fn enveloped_file_roundtrips_and_carries_magic() {
    use super::segment;
    const ENVELOPE_MAGIC: &[u8; 4] = b"SBF1";

    let tmp = TempDir::new().unwrap();
    let dir = tmp.path();
    let buf = test_buffer(dir);

    buf.append(test_item(1)).unwrap();
    buf.append(test_item(2)).unwrap();
    buf.flush().unwrap();

    // The file on disk must start with the SBF1 magic. Sequence numbers are
    // assigned by the buffer (0-based), so two appends → filename(0, 1).
    let path = dir.join(segment::filename(0, 1));
    assert!(path.exists(), "segment file should exist at {path:?}");
    let bytes = fs::read(&path).unwrap();
    assert!(
        bytes.len() >= 8,
        "enveloped file should be at least header-length"
    );
    assert_eq!(
        &bytes[..4],
        ENVELOPE_MAGIC,
        "newly-written segment must carry the SBF1 envelope magic"
    );

    // And it must round-trip cleanly.
    let events = buf.read_from(0, 100).unwrap();
    assert_eq!(events.len(), 2);
}

#[test]
fn envelope_detection_requires_zero_reserved_bytes() {
    use super::segment::{unwrap_envelope, wrap_envelope};

    // Sanity: the canonical envelope (zero reserved) is detected.
    let wrapped = wrap_envelope(b"payload");
    assert!(matches!(unwrap_envelope(&wrapped), (Some(1), _)));

    // A v1-shape block whose reserved bytes are NON-zero must NOT be treated
    // as an envelope, even though the magic matches. This is the hardening:
    // a legacy encrypted file whose AEAD nonce begins with `SBF1` followed
    // by three non-zero bytes (~2⁻³² of files) would otherwise be silently
    // mis-framed as an envelope. Requiring reserved-zero drops the false
    // positive to 2⁻⁵⁶.
    let mut looks_like_envelope = vec![b'S', b'B', b'F', b'1', 1, 0xFF, 0xFF, 0xFF];
    looks_like_envelope.extend_from_slice(b"payload");
    let (version, payload) = unwrap_envelope(&looks_like_envelope);
    assert_eq!(
        version, None,
        "magic with non-zero reserved bytes must not be detected as envelope"
    );
    assert_eq!(
        payload,
        looks_like_envelope.as_slice(),
        "non-conforming bytes must pass through unmodified as legacy"
    );
}

#[cfg(feature = "encryption")]
#[test]
fn legacy_encrypted_file_without_envelope_still_reads() {
    // The headline monitor365 byte-compatibility guarantee: a segment file
    // written by monitor365 (no SBF1 envelope, just `[nonce][ciphertext]`)
    // must read back transparently through the enveloped reader when the
    // matching cipher is configured. This was previously untested.
    use super::segment;

    let tmp = TempDir::new().unwrap();
    let dir = tmp.path();

    let items = vec![test_item(101), test_item(102), test_item(103)];

    // Encode the v1 payload exactly as monitor365 would have: CBOR → zstd →
    // AEAD-encrypt. Then write the raw payload bytes (NO envelope) under a
    // valid segment filename.
    let key = [0xABu8; 32];
    let cipher = AesGcmCipher::new(&key);
    let path = dir.join(segment::filename(101, 103));
    let mut compressor = zstd::bulk::Compressor::new(3).unwrap();
    let payload = segment::encode_payload(Some(&cipher), &mut compressor, &path, &items).unwrap();
    assert!(
        !payload.starts_with(b"SBF1"),
        "raw encrypted payload must not accidentally carry the magic"
    );
    fs::write(&path, &payload).unwrap();

    // Open the buffer with the same cipher and read the segment back.
    let buf = encrypted_buffer(dir, key);
    let events: Vec<TestItem> = buf.read_from(101, 100).unwrap();
    assert_eq!(
        events, items,
        "legacy encrypted file (no envelope) must decode transparently"
    );
}

// =========================================================================
// Encryption tests (behind `encryption` feature)
// =========================================================================

#[cfg(feature = "encryption")]
fn encrypted_buffer(dir: &Path, key: [u8; 32]) -> TestBuffer {
    SegmentBuffer::open(
        dir,
        SegmentConfig {
            flush_policy: FlushPolicy::Batch(4),
            max_size_bytes: 1024 * 1024,
            compression_level: 3,
            durability: DurabilityPolicy::Segment,
            cipher: Some(Arc::new(AesGcmCipher::new(&key))),
        },
    )
    .expect("Failed to create encrypted buffer")
}

#[cfg(feature = "encryption")]
#[test]
fn encrypted_roundtrip_preserves_event_data() {
    let tmp = TempDir::new().unwrap();
    let buf = encrypted_buffer(tmp.path(), [0u8; 32]);

    let item = test_item(99);
    buf.append(item.clone()).unwrap();
    buf.flush().unwrap();

    let events = buf.read_from(0, 100).unwrap();
    assert_eq!(events.len(), 1);
    assert_eq!(events[0], item);

    // Verify the segment file on disk is NOT plaintext. Filter to .zst
    // because the directory also contains the `.segment-buffer.lock`
    // sidecar (held open by the flock since v0.5.0); the old
    // `read_dir().next()` form grabbed whichever entry the kernel
    // returned first, which is fs-dependent and sometimes the lock file.
    let segment_path = tmp
        .path()
        .read_dir()
        .unwrap()
        .filter_map(|e| e.ok())
        .map(|e| e.path())
        .find(|p| p.extension().is_some_and(|x| x == "zst"))
        .expect("segment file must exist after flush");
    let raw = fs::read(&segment_path).unwrap();
    assert!(
        raw.len() > 12,
        "Encrypted segment should be nonce + ciphertext, not plaintext"
    );
}

#[cfg(feature = "encryption")]
#[test]
fn truncated_encrypted_segment_returns_error() {
    let tmp = TempDir::new().unwrap();
    let dir = tmp.path();

    let path = dir.join("seg_000000000000_000000000000.zst");
    fs::write(&path, [0u8; 11]).unwrap();

    let buf = encrypted_buffer(dir, [0u8; 32]);
    let result = buf.read_from(0, 100);
    assert!(
        result.is_err(),
        "Truncated encrypted segment (<12 bytes) should return an error"
    );
}

#[cfg(feature = "encryption")]
#[test]
fn encrypted_segment_nonce_only_returns_error() {
    let tmp = TempDir::new().unwrap();
    let dir = tmp.path();

    let path = dir.join("seg_000000000000_000000000000.zst");
    fs::write(&path, [0u8; 12]).unwrap();

    let buf = encrypted_buffer(dir, [0u8; 32]);
    let result = buf.read_from(0, 100);
    assert!(
        result.is_err(),
        "Encrypted segment with nonce but no ciphertext should return an error"
    );
}

#[cfg(feature = "encryption")]
#[test]
fn wrong_decryption_key_returns_error() {
    let tmp = TempDir::new().unwrap();
    let dir = tmp.path();

    {
        let buf = encrypted_buffer(dir, [0u8; 32]);
        buf.append(test_item(0)).unwrap();
        buf.flush().unwrap();
    }

    let buf = encrypted_buffer(dir, [1u8; 32]);
    let result = buf.read_from(0, 100);
    assert!(
        result.is_err(),
        "Wrong decryption key should fail to read encrypted segment"
    );
}

#[cfg(feature = "encryption")]
#[test]
fn decrypt_without_key_returns_error() {
    let tmp = TempDir::new().unwrap();
    let dir = tmp.path();

    {
        let buf = encrypted_buffer(dir, [0u8; 32]);
        buf.append(test_item(0)).unwrap();
        buf.flush().unwrap();
    }

    // Reopen WITHOUT cipher — tries to zstd-decode ciphertext → fails
    let buf = test_buffer(dir);
    let result = buf.read_from(0, 100);
    assert!(
        result.is_err(),
        "Reading encrypted segment without a cipher should fail"
    );
}

#[cfg(feature = "encryption")]
#[test]
fn wrong_key_cipher_error_carries_source_chain() {
    // The cipher error surfaced to the caller must keep the underlying AEAD
    // failure reachable via `std::error::Error::source`, so operators can
    // inspect the original decryption failure instead of just a flat string.
    let tmp = TempDir::new().unwrap();
    let dir = tmp.path();

    {
        let buf = encrypted_buffer(dir, [0u8; 32]);
        buf.append(test_item(0)).unwrap();
        buf.flush().unwrap();
    }

    let buf = encrypted_buffer(dir, [1u8; 32]);
    let err = buf.read_from(0, 100).expect_err("wrong key must error");

    let super::SegmentError::Cipher { message, .. } = &err else {
        panic!("expected Cipher variant, got {err:?}");
    };
    assert!(
        message.contains("AES-GCM decryption failed"),
        "message should name the phase, got: {message}"
    );
    // The CipherError's source chain was lost when promoted to SegmentError::Cipher
    // (the variant stores a flat String), but the underlying AEAD failure must
    // still be reachable on the CipherError itself. We exercise that path via
    // a direct cipher call.
    use super::SegmentCipher;
    let cipher = AesGcmCipher::new(&[0u8; 32]);
    let bad_payload = [0u8; 64]; // plausible size, wrong bytes
    let cipher_err = cipher.decrypt(&bad_payload).unwrap_err();
    assert!(
        std::error::Error::source(&cipher_err).is_some(),
        "CipherError from AES-GCM must expose the AEAD failure via source()"
    );
}

// =========================================================================
// XChaCha20-Poly1305 cipher (encryption feature)
// =========================================================================

#[cfg(feature = "encryption")]
fn encrypted_buffer_xchacha(dir: &Path, key: [u8; 32]) -> TestBuffer {
    SegmentBuffer::open(
        dir,
        SegmentConfig {
            flush_policy: FlushPolicy::Batch(4),
            max_size_bytes: 1024 * 1024,
            compression_level: 3,
            durability: DurabilityPolicy::Segment,
            cipher: Some(Arc::new(XChaCha20Poly1305Cipher::new(&key))),
        },
    )
    .expect("Failed to create XChaCha20-encrypted buffer")
}

#[cfg(feature = "encryption")]
#[test]
fn xchacha20_roundtrip_preserves_event_data() {
    let tmp = TempDir::new().unwrap();
    let buf = encrypted_buffer_xchacha(tmp.path(), [0u8; 32]);

    for i in 0..5 {
        buf.append(test_item(i)).unwrap();
    }
    buf.flush().unwrap();

    let events = buf.read_from(0, 100).unwrap();
    assert_eq!(events.len(), 5);
    for (i, event) in events.iter().enumerate() {
        assert_eq!(event.id, i as u64);
    }
}

#[cfg(feature = "encryption")]
#[test]
fn xchacha20_cipher_roundtrip_direct() {
    // Direct trait-level roundtrip independent of the buffer: encrypt + decrypt
    // must reproduce the input for arbitrary plaintexts.
    let cipher = XChaCha20Poly1305Cipher::new(&[0xau8; 32]);
    for plaintext in [b"".as_slice(), b"x", b"hello world", &[42u8; 4096]] {
        let ct = cipher.encrypt(plaintext).expect("encrypt");
        // The ciphertext must include the 24-byte nonce prefix.
        assert!(ct.len() >= 24, "ciphertext must include nonce prefix");
        let pt = cipher.decrypt(&ct).expect("decrypt");
        assert_eq!(pt, plaintext, "roundtrip must reproduce plaintext");
    }
}

#[cfg(feature = "encryption")]
#[test]
fn xchacha20_tamper_detection() {
    // Flip one byte of the ciphertext → AEAD tag must fail verification.
    let cipher = XChaCha20Poly1305Cipher::new(&[0xbu8; 32]);
    let mut ct = cipher.encrypt(b"secret payload").expect("encrypt");
    // Flip the last byte (inside the Poly1305 tag region).
    let last = ct.len() - 1;
    ct[last] ^= 0x01;
    let err = cipher
        .decrypt(&ct)
        .expect_err("tampered ciphertext must fail AEAD");
    assert!(
        err.to_string().contains("XChaCha20"),
        "error should name XChaCha20: got {err}"
    );
}

#[cfg(feature = "encryption")]
#[test]
fn xchacha20_short_payload_rejected() {
    // Payload shorter than the 24-byte nonce prefix must be rejected before
    // the AEAD is invoked, with a clear CipherError (not an opaque AEAD error).
    let cipher = XChaCha20Poly1305Cipher::new(&[0xcu8; 32]);
    for short_len in 0..24 {
        let payload = vec![0u8; short_len];
        let err = cipher
            .decrypt(&payload)
            .expect_err("sub-nonce payload must error");
        assert!(
            err.to_string().contains("nonce"),
            "error should mention nonce: got {err}"
        );
    }
}

#[cfg(feature = "encryption")]
#[test]
fn xchacha20_buffer_segment_roundtrip_with_delete_acked() {
    let tmp = TempDir::new().unwrap();
    let buf = encrypted_buffer_xchacha(tmp.path(), [0xdu8; 32]);

    for i in 0..4 {
        buf.append(test_item(i)).unwrap();
    }
    // Batch(4) triggers auto-flush on the 4th append.
    assert_eq!(buf.pending_count(), 4);

    // Acknowledge the first 3 items; one segment [0..=3] is too tall to
    // ack with seq=2, so the segment survives.
    let removed = buf.delete_acked(2).unwrap();
    assert_eq!(removed, 0);
    // Ack all 4: segment [0..=3] is fully covered.
    let removed = buf.delete_acked(3).unwrap();
    assert_eq!(removed, 1);
}

#[cfg(feature = "encryption")]
#[test]
fn xchacha20_recommended_cipher_installs_xchacha() {
    // The recommended_cipher() builder helper must install an XChaCha20
    // cipher (the documented direction for new buffers).
    let cfg = SegmentConfig::builder()
        .recommended_cipher([0xeu8; 32])
        .build();
    assert!(
        cfg.cipher.is_some(),
        "recommended_cipher must install a cipher"
    );
    // Smoke: the cipher works for a roundtrip via the buffer.
    let tmp = TempDir::new().unwrap();
    let buf = SegmentBuffer::<TestItem>::open(tmp.path(), cfg).unwrap();
    buf.append(test_item(7)).unwrap();
    buf.flush().unwrap();
    let items = buf.read_from(0, 100).unwrap();
    assert_eq!(items, vec![test_item(7)]);
}

// =========================================================================
// Single-process flock (M2)
// =========================================================================

/// Second `open()` on the same directory while the first buffer is alive
/// must fail fast with [`SegmentError::Locked`].
#[test]
fn flock_second_open_returns_locked_error() {
    let tmp = TempDir::new().unwrap();
    let _first = test_buffer(tmp.path());
    let result = SegmentBuffer::<TestItem>::open(tmp.path(), test_config(1024 * 1024));
    let err = result.expect_err("second open must fail");
    assert!(
        matches!(err, SegmentError::Locked { .. }),
        "expected SegmentError::Locked, got {err:?}"
    );
    let rendered = format!("{err}");
    assert!(
        rendered.contains("locked by another process"),
        "error should mention lock: got {rendered}"
    );
}

/// After the first buffer is dropped, the lock releases and a new `open()`
/// succeeds. This is the kernel-advisory-lock contract: dropping the fd
/// releases the flock.
#[test]
fn flock_open_after_drop_succeeds() {
    let tmp = TempDir::new().unwrap();
    {
        let _first = test_buffer(tmp.path());
        assert!(SegmentBuffer::<TestItem>::open(tmp.path(), test_config(1024 * 1024)).is_err());
        // _first dropped here: the flock is released.
    }
    let _second = test_buffer(tmp.path());
    // If we reached here without panicking, the second open succeeded.
}

/// A lock file is created in the directory as a side-effect of `open()`.
/// Operators can list it; recovery must ignore it (it does not match
/// `seg_*_*.zst`).
#[test]
fn flock_creates_lock_sidecar_file() {
    let tmp = TempDir::new().unwrap();
    let buf = test_buffer(tmp.path());
    // Append + flush so a segment file exists alongside the lock file.
    buf.append(test_item(0)).unwrap();
    buf.flush().unwrap();

    let entries: Vec<String> = std::fs::read_dir(tmp.path())
        .expect("dir readable")
        .filter_map(std::result::Result::ok)
        .map(|e| e.file_name().to_string_lossy().into_owned())
        .collect();
    assert!(
        entries.iter().any(|n| n == ".segment-buffer.lock"),
        "lock sidecar must exist; dir contents: {entries:?}"
    );
    assert!(
        entries
            .iter()
            .any(|n| n.starts_with("seg_") && n.ends_with(".zst")),
        "segment file must exist; dir contents: {entries:?}"
    );
    // The lock file must not be confused with a segment: scan_segments returns
    // exactly one segment (the lock is ignored).
    assert_eq!(
        buf.read_from(0, 100).unwrap().len(),
        1,
        "lock file must not show up as a segment"
    );
}

/// Different directories are independently lockable. Two buffers in two
/// directories must both succeed.
#[test]
fn flock_locks_are_per_directory() {
    let tmp_a = TempDir::new().unwrap();
    let tmp_b = TempDir::new().unwrap();
    let _a = test_buffer(tmp_a.path());
    let _b = test_buffer(tmp_b.path());
    // No panic — both opens succeeded.
}

// =========================================================================
// SegmentIter (M7)
// =========================================================================

#[test]
fn iter_from_yields_seq_item_pairs() {
    let tmp = TempDir::new().unwrap();
    let buf = test_buffer(tmp.path());
    for i in 0..5 {
        buf.append(test_item(i)).unwrap();
    }
    // Batch(4) triggers a flush on the 4th append; one item stays in memory.
    let collected: Vec<(u64, TestItem)> = buf.iter_from(0, 100).unwrap().collect();
    assert_eq!(
        collected.iter().map(|(s, _)| *s).collect::<Vec<_>>(),
        vec![0, 1, 2, 3, 4]
    );
    assert_eq!(
        collected.iter().map(|(_, i)| i.id).collect::<Vec<_>>(),
        vec![0, 1, 2, 3, 4]
    );
}

#[test]
fn iter_from_limit_zero_returns_empty() {
    let tmp = TempDir::new().unwrap();
    let buf = test_buffer(tmp.path());
    buf.append(test_item(0)).unwrap();
    let count = buf.iter_from(0, 0).unwrap().count();
    assert_eq!(count, 0);
}

#[test]
fn iter_from_respects_limit() {
    let tmp = TempDir::new().unwrap();
    let buf = test_buffer(tmp.path());
    for i in 0..10 {
        buf.append(test_item(i)).unwrap();
    }
    buf.flush().unwrap();
    let collected: Vec<u64> = buf.iter_from(2, 3).unwrap().map(|(_, i)| i.id).collect();
    assert_eq!(collected, vec![2, 3, 4]);
}

#[test]
fn iter_from_chains_with_iterator_combinators() {
    let tmp = TempDir::new().unwrap();
    let buf = test_buffer(tmp.path());
    for i in 0..10 {
        buf.append(test_item(i)).unwrap();
    }
    buf.flush().unwrap();
    // The classic Iterator combinators all work.
    let sum: u64 = buf
        .iter_from(0, 100)
        .unwrap()
        .map(|(_, i)| i.id)
        .filter(|x| x % 2 == 0)
        .sum();
    assert_eq!(sum, 2 + 4 + 6 + 8);
}

#[test]
fn iter_from_start_seq_skips_already_read_items() {
    let tmp = TempDir::new().unwrap();
    let buf = test_buffer(tmp.path());
    for i in 0..5 {
        buf.append(test_item(i)).unwrap();
    }
    buf.flush().unwrap();
    let collected: Vec<u64> = buf.iter_from(3, 100).unwrap().map(|(_, i)| i.id).collect();
    assert_eq!(collected, vec![3, 4]);
}

// =========================================================================
// mtime probe for scan cache (M13)
// =========================================================================

/// On a real filesystem (the tempdir), the probe should return `true` —
/// mtime moves when we write twice with a sleep in between. On a
/// filesystem that pins mtime to a constant, the probe returns `false`
/// and the cache guard is skipped (today's behavior).
#[test]
fn mtime_probe_returns_true_on_real_filesystem() {
    let tmp = TempDir::new().unwrap();
    // The probe runs at open(). Today's CI is on real filesystems
    // (ext4/tmpfs on Linux, apfs on macOS) where mtime is fine-grained.
    let buf = test_buffer(tmp.path());
    assert!(
        buf.mtime_supported,
        "mtime capability probe should return true on the host filesystem; \
         if this fires, the test host has a coarse-granularity or no-mtime \
         filesystem (the cache guard is correctly disabled in that case, \
         but the test assertion needs to match)"
    );
}

/// External directory mutation must be detected by the mtime guard: if
/// someone removes a segment file out from under us, the next `scan_cache`
/// hit must NOT serve the stale list (would silently drop the segment's
/// items from reads).
#[test]
fn external_segment_removal_invalidates_scan_cache() {
    let tmp = TempDir::new().unwrap();
    let buf = test_buffer(tmp.path());
    for i in 0..4 {
        buf.append(test_item(i)).unwrap();
    }
    buf.flush().unwrap();
    // Two flushes so we have two segments to reason about.
    for i in 4..8 {
        buf.append(test_item(i)).unwrap();
    }
    buf.flush().unwrap();
    // 8 items readable through the public API.
    assert_eq!(buf.read_from(0, 100).unwrap().len(), 8);

    // Simulate an external process quarantining one segment.
    let segments: Vec<_> = std::fs::read_dir(tmp.path())
        .expect("dir readable")
        .filter_map(std::result::Result::ok)
        .map(|e| e.path())
        .filter(|p| p.extension().is_some_and(|x| x == "zst"))
        .collect();
    assert_eq!(segments.len(), 2, "expected exactly two segments on disk");
    let _ = std::fs::remove_file(&segments[0]);

    // Sleep briefly so the dir mtime moves past the cached value (the
    // probe sleep is 15ms; we use 25ms here for headroom on coarse fs).
    std::thread::sleep(std::time::Duration::from_millis(25));

    // The next read must observe the removal: only one segment's items
    // (4) survive. Without the mtime guard, the stale cache would still
    // report both segments as on-disk and reads would try (and fail) to
    // open the removed file — surfacing as an Err.
    let after = buf.read_from(0, 100).unwrap().len();
    assert_eq!(
        after, 4,
        "external removal must be reflected via mtime guard"
    );
}

// =========================================================================
// Debug impl for SegmentBuffer<T>
// =========================================================================

#[test]
fn debug_impl_formats_cleanly() {
    let tmp = TempDir::new().unwrap();
    let buf = test_buffer(tmp.path());
    buf.append(test_item(0)).unwrap();
    buf.append(test_item(1)).unwrap();
    buf.append(test_item(2)).unwrap();

    let rendered = format!("{buf:?}");
    // Structural sanity: struct name + path field + every BufferStats field.
    assert!(
        rendered.starts_with("SegmentBuffer {"),
        "expected SegmentBuffer struct prefix, got: {rendered}"
    );
    // debug_struct renders field names as bare identifiers (no quotes).
    assert!(
        rendered.contains("dir: "),
        "Debug must expose the dir field, got: {rendered}"
    );
    for field in [
        "pending_count",
        "latest_sequence",
        "head_sequence",
        "next_sequence",
        "approx_disk_bytes",
        "segment_count",
        "max_size_bytes",
        "store_pressure",
    ] {
        assert!(
            rendered.contains(&format!("{field}: ")),
            "Debug must expose the `{field}` field, got: {rendered}"
        );
    }
    // pending_count reflects the three appends.
    assert!(
        rendered.contains("pending_count: 3"),
        "expected pending_count: 3, got: {rendered}"
    );
}

// =========================================================================
// Display snapshot tests — lock the format strings so a careless edit
// (e.g. changing a brace in a `thiserror` attribute) shows up as a test
// failure instead of silently shifting operator-facing log output.
// =========================================================================

#[test]
fn segment_error_io_display_format_no_path() {
    // Io constructed from a bare io::Error via `?` has site = Unknown.
    let io_err = std::io::Error::new(std::io::ErrorKind::NotFound, "missing");
    let err: SegmentError = io_err.into();
    let rendered = format!("{err}");
    // No " for ..." clause when site is Unknown.
    assert_eq!(rendered, "I/O error: missing");
}

#[test]
fn segment_error_io_display_format_with_segment_path() {
    // Io constructed with explicit Segment site renders the path clause.
    let err = SegmentError::Io {
        site: IoSite::Segment(std::path::PathBuf::from(
            "/var/data/seg_000000000000_000000000000.zst",
        )),
        source: std::io::Error::new(std::io::ErrorKind::PermissionDenied, "permission denied"),
    };
    let rendered = format!("{err}");
    assert_eq!(
        rendered,
        "I/O error for /var/data/seg_000000000000_000000000000.zst: permission denied"
    );
}

#[test]
fn segment_error_io_display_format_with_dir_site() {
    // Io with site = Dir renders a fixed clause (no path payload — the
    // directory is reachable via SegmentBuffer::path()).
    let err = SegmentError::Io {
        site: IoSite::Dir,
        source: std::io::Error::new(std::io::ErrorKind::ReadOnlyFilesystem, "read-only"),
    };
    let rendered = format!("{err}");
    assert_eq!(rendered, "I/O error for the segment directory: read-only");
}

#[test]
fn segment_error_with_path_upgrades_unknown_to_segment() {
    // with_path on an Unknown Io error upgrades the site to Segment.
    let raw: SegmentError = std::io::Error::other("boom").into();
    let upgraded = raw.with_path("/tmp/seg.zst");
    match upgraded {
        SegmentError::Io {
            site: IoSite::Segment(p),
            ..
        } => {
            assert_eq!(p, std::path::PathBuf::from("/tmp/seg.zst"));
        }
        other => panic!("expected Io with Segment site, got {other:?}"),
    }
}

#[test]
fn segment_error_with_path_leaves_segment_alone() {
    // First call site to attach context wins: calling with_path on a Segment
    // site leaves the original path intact (no clobbering).
    let err = SegmentError::Io {
        site: IoSite::Segment(std::path::PathBuf::from("/original/path.zst")),
        source: std::io::Error::other("x"),
    };
    let upgraded = err.with_path("/wrong/attempt.zst");
    match upgraded {
        SegmentError::Io {
            site: IoSite::Segment(p),
            ..
        } => {
            assert_eq!(p, std::path::PathBuf::from("/original/path.zst"));
        }
        other => panic!("expected Io with original Segment site, got {other:?}"),
    }
}

#[test]
fn segment_error_with_dir_upgrades_unknown_to_dir() {
    // with_dir on an Unknown Io error tags the site as Dir.
    let raw: SegmentError = std::io::Error::other("boom").into();
    let tagged = raw.with_dir();
    assert!(matches!(
        tagged,
        SegmentError::Io {
            site: IoSite::Dir,
            ..
        }
    ));
}

#[test]
fn segment_error_io_with_path_attaches_path() {
    // Upgrade a bare propagated io::Error to carry path context.
    let io_err: SegmentError =
        std::io::Error::new(std::io::ErrorKind::UnexpectedEof, "short read").into();
    let upgraded = io_err.with_path("/tmp/seg_000000000000_000000000000.zst");
    let rendered = format!("{upgraded}");
    assert_eq!(
        rendered,
        "I/O error for /tmp/seg_000000000000_000000000000.zst: short read"
    );
}

#[test]
fn segment_error_cbor_display_format() {
    let err = SegmentError::Cbor {
        phase: "deserialize",
        path: std::path::PathBuf::from("/var/data/seg_000000000000_000000000000.zst"),
        message: "unexpected eof".into(),
    };
    let rendered = format!("{err}");
    assert_eq!(
        rendered,
        "CBOR deserialize failed for /var/data/seg_000000000000_000000000000.zst: unexpected eof"
    );
}

#[test]
fn segment_error_cipher_display_format() {
    let err = SegmentError::Cipher {
        path: std::path::PathBuf::from("/var/data/seg_000000000000_000000000000.zst"),
        message: "AES-GCM decryption failed".into(),
    };
    let rendered = format!("{err}");
    assert_eq!(
        rendered,
        "cipher error for /var/data/seg_000000000000_000000000000.zst: AES-GCM decryption failed"
    );
}

#[test]
fn segment_error_integrity_display_format() {
    let err = SegmentError::Integrity {
        path: std::path::PathBuf::from("/var/data/seg_000000000000_000000000000.zst"),
        reason: "truncated payload",
    };
    let rendered = format!("{err}");
    assert_eq!(
        rendered,
        "integrity failure for /var/data/seg_000000000000_000000000000.zst: truncated payload"
    );
}

#[test]
fn cipher_error_msg_display_format() {
    let err = super::CipherError::msg("key not configured");
    let rendered = format!("{err}");
    // msg() preserves the message verbatim; no prefix or decoration.
    assert_eq!(rendered, "key not configured");
}

#[test]
#[cfg(feature = "encryption")]
fn cipher_error_with_source_display_format() {
    use std::error::Error as _;

    #[derive(Debug)]
    struct FakeAead;
    impl std::fmt::Display for FakeAead {
        fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
            f.write_str("aead tag mismatch")
        }
    }
    impl std::error::Error for FakeAead {}

    let err = super::CipherError::with_source("AES-GCM decryption failed", FakeAead);
    // Display intentionally hides the source chain — the message stands alone.
    // The underlying cause is reachable only via `Error::source()`.
    assert_eq!(format!("{err}"), "AES-GCM decryption failed");
    let src = err.source().expect("with_source must populate source()");
    assert_eq!(format!("{src}"), "aead tag mismatch");
}

// =========================================================================
// for_each_from re-entrancy safety (panic-free, no deadlock)
// =========================================================================

#[test]
fn for_each_from_allows_reentry_without_deadlock() {
    // The buffer mutex is never held across a for_each_from callback: on-disk
    // items are decoded before the callback and in-memory items are snapshotted
    // under the lock then released. Re-entrant read calls must therefore
    // succeed instead of panicking or deadlocking.
    let tmp = TempDir::new().unwrap();
    let buf = Arc::new(test_buffer(tmp.path()));
    for i in 0..3 {
        buf.append(test_item(i)).unwrap();
    }

    let buf_clone = Arc::clone(&buf);
    let mut counts_during_iteration = Vec::new();
    let visited = buf
        .for_each_from(0, 100, |_seq, _item| {
            counts_during_iteration.push(buf_clone.pending_count());
            let _ = buf_clone.latest_sequence();
            let _ = buf_clone.stats();
        })
        .unwrap();

    assert_eq!(visited, 3);
    // pending_count stays 3 throughout (no concurrent mutation).
    assert!(
        counts_during_iteration.iter().all(|&c| c == 3),
        "re-entrant reads must succeed and stay consistent: {counts_during_iteration:?}"
    );
}

#[test]
fn for_each_from_allows_reentrant_mutation() {
    // Mutating re-entrant calls (append) must also be safe: they acquire the
    // mutex normally because for_each_from released it before the callback.
    // Manual flush keeps the assertions deterministic (no auto-flush side
    // effects). The snapshot taken before the callbacks is unaffected by the
    // re-entrant appends, so the visited count stays at the original 3.
    let tmp = TempDir::new().unwrap();
    let buf = Arc::new(
        SegmentBuffer::open(
            tmp.path(),
            SegmentConfig {
                flush_policy: FlushPolicy::Manual,
                ..test_config(1024 * 1024)
            },
        )
        .unwrap(),
    );
    for i in 0..3 {
        buf.append(test_item(i)).unwrap();
    }

    let buf_clone = Arc::clone(&buf);
    let visited = buf
        .for_each_from(0, 3, |_seq, _item| {
            let _ = buf_clone.append(test_item(99));
        })
        .unwrap();

    assert_eq!(visited, 3, "snapshot taken before callbacks is unaffected");
    // Three re-entrant appends landed in the in-memory tail (Manual = no flush).
    assert_eq!(buf.pending_count(), 6);
    assert_eq!(buf.latest_sequence(), 5);
}

#[test]
fn for_each_from_usable_after_panicking_callback() {
    // A panicking callback must not brick the buffer. The mutex is never held
    // across the callback, so unwinding leaves the buffer consistent.
    let tmp = TempDir::new().unwrap();
    let buf = Arc::new(test_buffer(tmp.path()));
    for i in 0..3 {
        buf.append(test_item(i)).unwrap();
    }

    let _ = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
        let _ = buf.for_each_from(0, 100, |_seq, _item| {
            panic!("boom inside callback");
        });
    }));

    assert_eq!(buf.pending_count(), 3, "buffer must be usable after panic");
    assert_eq!(buf.latest_sequence(), 2);
}

// =========================================================================
// append_all batch primitive
// =========================================================================

#[test]
fn append_all_assigns_contiguous_sequences() {
    let tmp = TempDir::new().unwrap();
    let buf = test_buffer(tmp.path());

    let last = buf
        .append_all([test_item(1), test_item(2), test_item(3)])
        .unwrap();
    assert_eq!(last, 2, "last seq should be 2 (0-based)");
    assert_eq!(buf.pending_count(), 3);

    // A second batch continues the sequence.
    let last2 = buf.append_all([test_item(4), test_item(5)]).unwrap();
    assert_eq!(last2, 4);
    assert_eq!(buf.pending_count(), 5);
}

#[test]
fn append_all_empty_iterator_is_noop() {
    let tmp = TempDir::new().unwrap();
    let buf = test_buffer(tmp.path());
    buf.append(test_item(0)).unwrap();

    let last = buf.append_all(std::iter::empty::<TestItem>()).unwrap();
    assert_eq!(last, 0, "empty append_all returns current last seq");
    assert_eq!(buf.pending_count(), 1);
}

#[test]
fn append_all_visibly_cheaper_lock_count_than_loop_append() {
    // Not a perf test — a correctness test: append_all assigns contiguous
    // seqs even under concurrent writers, because the whole batch is under
    // one lock. Two concurrent append_all calls must not interleave seqs.
    let tmp = TempDir::new().unwrap();
    let buf = Arc::new(test_buffer(tmp.path()));

    thread::scope(|s| {
        let b1 = Arc::clone(&buf);
        s.spawn(move || {
            b1.append_all((0..100).map(test_item)).unwrap();
        });
        let b2 = Arc::clone(&buf);
        s.spawn(move || {
            b2.append_all((0..100).map(test_item)).unwrap();
        });
    });

    // All 200 items must be present. Seqs are contiguous but the two batches
    // may land in either order.
    assert_eq!(buf.pending_count(), 200);
    assert_eq!(buf.latest_sequence(), 199);
}

#[test]
fn append_all_auto_flush_increments_segment_count() {
    // append_all routes through the same flush() (and therefore the same
    // segment_count fetch_add(1)) codepath as single-append auto-flush when
    // the batch threshold is crossed. Assert the live segment_count reflects
    // the auto-flushed segment — closes the gap where append_all-triggered
    // auto-flush had no explicit segment_count assertion.
    let tmp = TempDir::new().unwrap();
    let buf = test_buffer(tmp.path()); // FlushPolicy::Batch(4)

    // Below threshold: no auto-flush, segment_count stays 0.
    buf.append_all([test_item(0), test_item(1), test_item(2)])
        .unwrap();
    assert_eq!(
        buf.stats().segment_count,
        0,
        "below batch threshold, append_all must not auto-flush"
    );

    // Crossing the threshold (4 items pending) triggers an auto-flush inside
    // append_all, writing all pending items as exactly one segment.
    buf.append_all([test_item(3), test_item(4), test_item(5), test_item(6)])
        .unwrap();
    assert_eq!(
        buf.stats().segment_count,
        1,
        "append_all crossing the batch threshold must auto-flush exactly one segment"
    );
}

// =========================================================================
// path() and config() accessors
// =========================================================================

#[test]
fn path_accessor_returns_directory() {
    let tmp = TempDir::new().unwrap();
    let buf = test_buffer(tmp.path());
    assert_eq!(buf.path(), tmp.path());
}

#[test]
fn config_accessor_returns_opened_config() {
    let tmp = TempDir::new().unwrap();
    let config = SegmentConfig {
        flush_policy: FlushPolicy::Batch(7),
        max_size_bytes: 42,
        compression_level: 9,
        durability: DurabilityPolicy::Throughput,
        cipher: None,
    };
    let buf = test_buffer_with_config(tmp.path(), config);
    let cfg = buf.config();
    assert_eq!(cfg.flush_policy, FlushPolicy::Batch(7));
    assert_eq!(cfg.max_size_bytes, 42);
    assert_eq!(cfg.compression_level, 9);
    assert_eq!(cfg.durability, DurabilityPolicy::Throughput);
}

fn test_buffer_with_config(dir: &Path, config: SegmentConfig) -> TestBuffer {
    SegmentBuffer::open(dir, config).expect("buffer must open")
}

// =========================================================================
// sync_disk_bytes
// =========================================================================

#[test]
fn sync_disk_bytes_recovers_after_external_truncation() {
    let tmp = TempDir::new().unwrap();
    let dir = tmp.path();
    let buf = test_buffer(dir);
    for i in 0..4 {
        buf.append(test_item(i)).unwrap();
    }
    buf.flush().unwrap();

    let before = buf.stats().approx_disk_bytes;
    assert!(before > 0, "flushed segment should have nonzero size");

    // External process truncates all segment files to zero bytes.
    for entry in fs::read_dir(dir).unwrap() {
        let path = entry.unwrap().path();
        if path.extension().is_some_and(|e| e == "zst") {
            fs::write(&path, b"").unwrap();
        }
    }

    let synced = buf.sync_disk_bytes().unwrap();
    assert_eq!(
        synced, 0,
        "external truncation must be reflected after sync"
    );
    assert_eq!(buf.stats().approx_disk_bytes, 0);
}

// =========================================================================
// Live segment_count in BufferStats — tracks the on-disk segment file count
// incrementally alongside approx_disk_bytes.
// =========================================================================

/// Helper: count `.zst` segment files actually on disk.
fn count_disk_segments(dir: &Path) -> u64 {
    fs::read_dir(dir)
        .expect("read_dir")
        .filter_map(std::result::Result::ok)
        .filter(|e| e.file_name().to_string_lossy().ends_with(".zst"))
        .count() as u64
}

#[test]
fn segment_count_zero_on_fresh_buffer() {
    let tmp = TempDir::new().unwrap();
    let buf = test_buffer(tmp.path());
    assert_eq!(
        buf.stats().segment_count,
        0,
        "fresh buffer must report 0 segments"
    );
}

#[test]
fn segment_count_increments_on_flush() {
    let tmp = TempDir::new().unwrap();
    let buf = test_buffer(tmp.path());
    for i in 0..4 {
        buf.append(test_item(i)).unwrap();
    }
    buf.flush().unwrap();
    assert_eq!(
        buf.stats().segment_count,
        1,
        "one flush must produce exactly one segment in the live count"
    );
    assert_eq!(
        count_disk_segments(tmp.path()),
        1,
        "live count must match actual disk file count"
    );
}

#[test]
fn segment_count_tracks_multiple_flushes() {
    let tmp = TempDir::new().unwrap();
    let config = SegmentConfig {
        flush_policy: FlushPolicy::Manual,
        ..test_config(1024 * 1024)
    };
    let buf = SegmentBuffer::open(tmp.path(), config).unwrap();

    for round in 1..=5u64 {
        for i in 0..3u64 {
            buf.append(test_item(round * 10 + i)).unwrap();
        }
        buf.flush().unwrap();
        assert_eq!(
            buf.stats().segment_count,
            round,
            "after {round} flushes the live segment_count must be {round}"
        );
        assert_eq!(
            count_disk_segments(tmp.path()),
            round,
            "live count must match disk after {round} flushes"
        );
    }
}

#[test]
fn segment_count_decrements_on_delete_acked() {
    let tmp = TempDir::new().unwrap();
    let config = SegmentConfig {
        flush_policy: FlushPolicy::Manual,
        ..test_config(1024 * 1024)
    };
    let buf = SegmentBuffer::open(tmp.path(), config).unwrap();

    // Three independent segments: [0..=0], [1..=1], [2..=2].
    buf.append(test_item(0)).unwrap();
    buf.flush().unwrap();
    buf.append(test_item(1)).unwrap();
    buf.flush().unwrap();
    buf.append(test_item(2)).unwrap();
    buf.flush().unwrap();
    assert_eq!(buf.stats().segment_count, 3);

    // Ack seq 0 → removes one segment.
    let removed = buf.delete_acked(0).unwrap();
    assert_eq!(removed, 1);
    assert_eq!(buf.stats().segment_count, 2);
    assert_eq!(count_disk_segments(tmp.path()), 2);

    // Ack seq 2 → removes both remaining.
    let removed = buf.delete_acked(2).unwrap();
    assert_eq!(removed, 2);
    assert_eq!(buf.stats().segment_count, 0);
    assert_eq!(count_disk_segments(tmp.path()), 0);
}

#[test]
fn segment_count_recalibrated_by_sync_disk_bytes() {
    let tmp = TempDir::new().unwrap();
    let buf = test_buffer(tmp.path());
    buf.append(test_item(0)).unwrap();
    buf.append(test_item(1)).unwrap();
    buf.flush().unwrap();
    assert_eq!(buf.stats().segment_count, 1);

    // Simulate an external process removing the segment file behind the buffer.
    for entry in fs::read_dir(tmp.path()).unwrap() {
        let path = entry.unwrap().path();
        if path.extension().is_some_and(|e| e == "zst") {
            fs::remove_file(&path).unwrap();
        }
    }

    let _synced = buf.sync_disk_bytes().unwrap();
    assert_eq!(
        buf.stats().segment_count,
        0,
        "sync_disk_bytes must recalibrate segment_count to the directory reality"
    );
}

#[test]
fn segment_count_recovered_on_reopen() {
    let tmp = TempDir::new().unwrap();
    let dir = tmp.path();

    // First instance: write and flush two segments.
    {
        let config = SegmentConfig {
            flush_policy: FlushPolicy::Manual,
            ..test_config(1024 * 1024)
        };
        let buf: TestBuffer = SegmentBuffer::open(dir, config).unwrap();
        buf.append(test_item(0)).unwrap();
        buf.flush().unwrap();
        buf.append(test_item(1)).unwrap();
        buf.flush().unwrap();
        assert_eq!(buf.stats().segment_count, 2);
    }

    // Re-open: recovery must restore the live segment_count.
    let (buf, report) =
        SegmentBuffer::<TestItem>::open_with_report(dir, test_config(1024 * 1024)).unwrap();
    assert_eq!(report.segment_count, 2, "recovery report snapshot");
    assert_eq!(
        buf.stats().segment_count,
        2,
        "live segment_count must match recovery report right after open"
    );
}

#[test]
fn segment_count_stress_4_writers_2_deleters() {
    let tmp = TempDir::new().unwrap();
    let buf = Arc::new(
        SegmentBuffer::open(
            tmp.path(),
            SegmentConfig {
                flush_policy: FlushPolicy::Manual,
                ..test_config(1024 * 1024)
            },
        )
        .unwrap(),
    );

    // Pre-seed a few segments so deleters have something to remove while
    // writers are still producing new ones.
    {
        let b = Arc::clone(&buf);
        for round in 0..3u64 {
            for j in 0..4u64 {
                b.append(test_item(round * 10 + j)).unwrap();
            }
            b.flush().unwrap();
        }
    }

    std::thread::scope(|s| {
        for writer_id in 0..4usize {
            let b = Arc::clone(&buf);
            s.spawn(move || {
                for round in 0..25 {
                    for j in 0..4u64 {
                        let id = 100u64 + writer_id as u64 * 100 + round as u64 * 4 + j;
                        b.append(test_item(id)).unwrap();
                    }
                    b.flush().unwrap();
                }
            });
        }

        for _ in 0..2usize {
            let b = Arc::clone(&buf);
            s.spawn(move || {
                for _ in 0..20 {
                    let stats = b.stats();
                    if stats.next_sequence > stats.head_sequence && stats.pending_count == 0 {
                        let _ = b.delete_acked(stats.latest_sequence);
                    }
                    std::thread::sleep(std::time::Duration::from_micros(50));
                }
            });
        }
    });

    // sync_disk_bytes recalibrates the live counters to the directory reality.
    buf.sync_disk_bytes().unwrap();
    let stats = buf.stats();
    assert_eq!(
        stats.segment_count,
        count_disk_segments(tmp.path()),
        "live segment_count must converge to the actual disk segment count"
    );
}

// under contention. Verifies correctness (all items readable) AND reports
// a throughput number so perf regressions show up in test output.
// =========================================================================

#[test]
fn stress_8_writers_2_readers_throughput() {
    use std::sync::atomic::{AtomicU64, Ordering};
    use std::time::Instant;

    let tmp = TempDir::new().unwrap();
    // FlushPolicy::Manual is critical: with Batch(4) this test would create
    // 20_000 segment files (80_000 items / 4), causing pathological I/O under
    // parallel test execution. Manual keeps everything in-memory so the test
    // stresses mutex contention, not the filesystem. The single flush() after
    // the scope writes one segment.
    let buf = Arc::new(
        SegmentBuffer::open(
            tmp.path(),
            SegmentConfig {
                flush_policy: FlushPolicy::Manual,
                ..test_config(1024 * 1024)
            },
        )
        .unwrap(),
    );
    const WRITERS: usize = 8;
    const PER_WRITER: usize = 10_000;
    const TOTAL: usize = WRITERS * PER_WRITER; // 80_000
    const READERS: usize = 2;

    // Shared read cursor — readers use it as a hint for where to poll.
    // The cursor may drift ahead (double-reads are harmless); correctness is
    // verified by the final full read, not by the cursor value.
    let read_cursor = Arc::new(Mutex::new(0u64));
    let total_read = Arc::new(AtomicU64::new(0));

    let start = Instant::now();
    thread::scope(|s| {
        // 2 reader threads: poll read_from to add read-side contention.
        for _ in 0..READERS {
            let buf_r = Arc::clone(&buf);
            let cursor_r = Arc::clone(&read_cursor);
            let total_r = Arc::clone(&total_read);
            s.spawn(move || loop {
                let current = *cursor_r.lock();
                if current >= TOTAL as u64 {
                    break;
                }
                if let Ok(events) = buf_r.read_from(current, 500) {
                    if !events.is_empty() {
                        total_r.fetch_add(events.len() as u64, Ordering::Relaxed);
                        *cursor_r.lock() = current + events.len() as u64;
                    }
                }
                std::thread::sleep(Duration::from_micros(20));
            });
        }

        // 8 writer threads.
        for writer_id in 0..WRITERS {
            let buf_w = Arc::clone(&buf);
            s.spawn(move || {
                let base = writer_id * PER_WRITER;
                for i in 0..PER_WRITER {
                    let _ = buf_w.append(test_item((base + i) as u64));
                }
            });
        }
    });

    let elapsed = start.elapsed();

    // Regression guard (AGENTS.md rule 7): under FlushPolicy::Manual the
    // concurrent append phase must create ZERO segment files. An earlier
    // Batch(4) version created 20_000 files and hung CI for hours (commit
    // 80257a0). If this fires, the flush policy or Manual semantics broke —
    // do NOT widen the bound, investigate the regression.
    let segment_files_before_flush = std::fs::read_dir(tmp.path())
        .expect("temp dir readable")
        .filter_map(std::result::Result::ok)
        .filter(|e| e.path().extension().is_some_and(|ext| ext == "zst"))
        .count();
    assert_eq!(
        segment_files_before_flush, 0,
        "FlushPolicy::Manual must not create segment files during append; \
         found {segment_files_before_flush} .zst file(s) — flush policy regression"
    );

    buf.flush().unwrap();

    // Correctness: all items assigned and readable.
    assert_eq!(buf.latest_sequence(), (TOTAL - 1) as u64);
    assert_eq!(buf.pending_count(), TOTAL as u64);
    let all_events = buf.read_from(0, TOTAL * 2).unwrap();
    assert_eq!(
        all_events.len(),
        TOTAL,
        "all {TOTAL} events must be readable after the stress run"
    );

    // Throughput: report events/sec. NOT a hard assertion (CI hardware varies)
    // — it's a reporting metric so a human can spot regressions in the test
    // output.
    let elapsed_secs = elapsed.as_secs_f64().max(0.001);
    let throughput = TOTAL as f64 / elapsed_secs;
    eprintln!(
        "stress_8w_2r: {TOTAL} events in {elapsed_secs:.3}s = {throughput:.0} events/sec \
         ({:.2} µs/event under 8-writer contention, {} items observed by readers)",
        elapsed_secs * 1_000_000.0 / TOTAL as f64,
        total_read.load(Ordering::Relaxed)
    );
}

/// 8 writers × 4 readers stress with per-append latency histogram.
///
/// Reports p50/p90/p99/p99.9 latency on the writer path so a human can spot
/// latency-tail regressions (e.g. a lock-contention change, an allocation
/// introduced on the hot path). The latency numbers are NOT hard assertions
/// (CI hardware varies) — they are reported in the test output for
/// human inspection across runs.
///
/// Reuses the rule-7 discipline from the throughput stress: `FlushPolicy::Manual`
/// so the test stresses mutex contention, not the filesystem. Reader count
/// is doubled (4 vs the throughput test's 2) so read-side contention
/// contributes to the writer-tail latency.
#[test]
fn stress_8_writers_4_readers_latency_histogram() {
    use std::sync::atomic::{AtomicU64, Ordering};
    use std::time::Instant;

    let tmp = TempDir::new().unwrap();
    let buf = Arc::new(
        SegmentBuffer::open(
            tmp.path(),
            SegmentConfig {
                flush_policy: FlushPolicy::Manual,
                ..test_config(1024 * 1024)
            },
        )
        .unwrap(),
    );
    const WRITERS: usize = 8;
    const PER_WRITER: usize = 10_000;
    const TOTAL: usize = WRITERS * PER_WRITER; // 80_000
    const READERS: usize = 4;

    let read_cursor = Arc::new(Mutex::new(0u64));
    let total_read = Arc::new(AtomicU64::new(0));
    // Per-writer latency samples. Pre-allocate so sampling overhead doesn't
    // include allocation in the measured section.
    let samples: Vec<Mutex<Vec<std::time::Duration>>> = (0..WRITERS)
        .map(|_| Mutex::new(Vec::with_capacity(PER_WRITER)))
        .collect();

    let start = Instant::now();
    thread::scope(|s| {
        // 4 reader threads: poll read_from to add read-side contention.
        for _ in 0..READERS {
            let buf_r = Arc::clone(&buf);
            let cursor_r = Arc::clone(&read_cursor);
            let total_r = Arc::clone(&total_read);
            s.spawn(move || loop {
                let current = *cursor_r.lock();
                if current >= TOTAL as u64 {
                    break;
                }
                if let Ok(events) = buf_r.read_from(current, 500) {
                    if !events.is_empty() {
                        total_r.fetch_add(events.len() as u64, Ordering::Relaxed);
                        *cursor_r.lock() = current + events.len() as u64;
                    }
                }
                std::thread::sleep(Duration::from_micros(20));
            });
        }

        // 8 writer threads, each measuring per-append latency.
        for writer_id in 0..WRITERS {
            let buf_w = Arc::clone(&buf);
            let samples_w = &samples;
            s.spawn(move || {
                let base = writer_id * PER_WRITER;
                let mut local_samples: Vec<std::time::Duration> = Vec::with_capacity(PER_WRITER);
                for i in 0..PER_WRITER {
                    let t = Instant::now();
                    let _ = buf_w.append(test_item((base + i) as u64));
                    local_samples.push(t.elapsed());
                }
                *samples_w[writer_id].lock() = local_samples;
            });
        }
    });

    let elapsed = start.elapsed();

    // Regression guard (rule 7): under Manual the concurrent append phase
    // must create ZERO segment files.
    let segment_files_before_flush = std::fs::read_dir(tmp.path())
        .expect("temp dir readable")
        .filter_map(std::result::Result::ok)
        .filter(|e| e.path().extension().is_some_and(|ext| ext == "zst"))
        .count();
    assert_eq!(
        segment_files_before_flush, 0,
        "FlushPolicy::Manual must not create segment files during append; \
         found {segment_files_before_flush} .zst file(s) — flush policy regression"
    );

    buf.flush().unwrap();
    assert_eq!(buf.latest_sequence(), (TOTAL - 1) as u64);

    // Merge per-writer samples into a single sorted Vec for percentile
    // computation. N = 80_000 samples is plenty for stable p99 estimates.
    let mut all: Vec<std::time::Duration> = Vec::with_capacity(TOTAL);
    for s in &samples {
        all.extend_from_slice(&s.lock());
    }
    all.sort();

    // Percentile helper. N is large enough that linear indexing is fine.
    let pct = |p: f64| -> std::time::Duration {
        if all.is_empty() {
            return std::time::Duration::ZERO;
        }
        let idx = ((p / 100.0) * (all.len() as f64 - 1.0)).round() as usize;
        all[idx.min(all.len() - 1)]
    };
    let elapsed_secs = elapsed.as_secs_f64().max(0.001);
    let throughput = TOTAL as f64 / elapsed_secs;
    eprintln!(
        "stress_8w_4r_latency: {TOTAL} events in {elapsed_secs:.3}s = {throughput:.0} events/sec\n\
         latency (µs): p50={:.2} p90={:.2} p99={:.2} p99.9={:.2} max={:.2}\n\
         {} items observed by readers",
        pct(50.0).as_nanos() as f64 / 1000.0,
        pct(90.0).as_nanos() as f64 / 1000.0,
        pct(99.0).as_nanos() as f64 / 1000.0,
        pct(99.9).as_nanos() as f64 / 1000.0,
        all.last().map_or(0.0, |d| d.as_nanos() as f64 / 1000.0),
        total_read.load(Ordering::Relaxed)
    );

    // Soft guard: p99 must stay under 5ms on any reasonable host (the test
    // runs in debug mode by default; release numbers are ~10x lower). This
    // is NOT a tight bound; if it fires, investigate the hot-path regression
    // before widening. Typical debug-mode p99 is ~50-500µs under 8-writer
    // contention.
    let p99 = pct(99.0);
    assert!(
        p99 < std::time::Duration::from_millis(50),
        "p99 latency {p99:?} exceeded 50ms soft guard — investigate hot-path regression"
    );
}

// =========================================================================
// DurabilityPolicy
// =========================================================================

/// Config-builder helper: vary ONLY the durability policy, keeping the
/// `test_config` defaults for everything else.
fn durability_config(max_size_bytes: u64, policy: DurabilityPolicy) -> SegmentConfig {
    SegmentConfig {
        flush_policy: FlushPolicy::Manual,
        max_size_bytes,
        compression_level: 3,
        durability: policy,
        cipher: None,
    }
}

/// All three policies must produce a readable, correct segment. This is a
/// functional roundtrip test, NOT a crash-semantics test — proving the
/// fsync branches fire correctly under a host crash requires killing the
/// process mid-flush and is out of scope for unit tests. (The fsync calls
/// are also exercised here: if a `sync_all` path is broken on the host, this
/// test surfaces it as an Err.)
#[test]
fn durability_policy_segment_roundtrip() {
    let tmp = TempDir::new().unwrap();
    let buf = SegmentBuffer::<TestItem>::open(
        tmp.path(),
        durability_config(1024 * 1024, DurabilityPolicy::Segment),
    )
    .expect("open with Segment policy");

    for i in 0..10 {
        buf.append(test_item(i)).unwrap();
    }
    buf.flush().expect("Segment flush must succeed");

    let items = buf.read_from(0, 100).unwrap();
    assert_eq!(items.len(), 10);
    for (i, item) in items.iter().enumerate() {
        assert_eq!(item.id, i as u64);
    }
}

#[test]
fn durability_policy_throughput_roundtrip() {
    let tmp = TempDir::new().unwrap();
    let buf = SegmentBuffer::<TestItem>::open(
        tmp.path(),
        durability_config(1024 * 1024, DurabilityPolicy::Throughput),
    )
    .expect("open with Throughput policy");

    for i in 0..10 {
        buf.append(test_item(i)).unwrap();
    }
    buf.flush()
        .expect("Throughput flush must succeed (no fsync, but rename is real)");

    let items = buf.read_from(0, 100).unwrap();
    assert_eq!(items.len(), 10);
    for (i, item) in items.iter().enumerate() {
        assert_eq!(item.id, i as u64);
    }
}

#[test]
fn durability_policy_maximal_roundtrip() {
    let tmp = TempDir::new().unwrap();
    let buf = SegmentBuffer::<TestItem>::open(
        tmp.path(),
        durability_config(1024 * 1024, DurabilityPolicy::Maximal),
    )
    .expect("open with Maximal policy");

    for i in 0..10 {
        buf.append(test_item(i)).unwrap();
    }
    // Maximal includes a dir.sync_all after rename; on Linux/macOS this is
    // well-defined and must succeed. If it errors here, the host filesystem
    // does not support directory fsync (Maximal is documented to require
    // Linux/macOS for the dir-sync half).
    buf.flush()
        .expect("Maximal flush must succeed on a capable filesystem");

    let items = buf.read_from(0, 100).unwrap();
    assert_eq!(items.len(), 10);
    for (i, item) in items.iter().enumerate() {
        assert_eq!(item.id, i as u64);
    }

    // The directory must contain exactly one segment file (no .tmp debris
    // left behind) — verifies the rename completed under every policy.
    let zst_count = std::fs::read_dir(tmp.path())
        .expect("temp dir readable")
        .filter_map(std::result::Result::ok)
        .filter(|e| e.path().extension().is_some_and(|ext| ext == "zst"))
        .count();
    assert_eq!(
        zst_count, 1,
        "exactly one .zst segment must exist after flush"
    );
}

/// All three policies must be recoverable: re-open the directory and the
/// segment file is visible (the rename is the atomicity boundary under
/// every policy).
#[test]
fn durability_policy_all_policies_recover_after_reopen() {
    for policy in [
        DurabilityPolicy::Maximal,
        DurabilityPolicy::Segment,
        DurabilityPolicy::Throughput,
    ] {
        let tmp = TempDir::new().unwrap();
        {
            let buf =
                SegmentBuffer::<TestItem>::open(tmp.path(), durability_config(1024 * 1024, policy))
                    .expect("open");
            buf.append(test_item(42)).unwrap();
            buf.flush().expect("flush");
        }
        let (buf, report) = SegmentBuffer::<TestItem>::open_with_report(
            tmp.path(),
            durability_config(1024 * 1024, policy),
        )
        .expect("reopen");
        assert_eq!(
            report.segment_count, 1,
            "policy {policy:?}: segment must be recovered"
        );
        assert_eq!(report.head_seq, 0);
        assert_eq!(report.next_seq, 1);
        let items = buf.read_from(0, 100).unwrap();
        assert_eq!(items.len(), 1);
        assert_eq!(items[0].id, 42);
    }
}

/// The default `SegmentConfig` must select `Segment` (the documented
/// backward-compat default for one release after the enum lands).
#[test]
fn durability_policy_default_is_segment() {
    let cfg = SegmentConfig::default();
    assert_eq!(cfg.durability, DurabilityPolicy::Segment);
}

/// The builder `.durability(...)` setter must round-trip into the built config.
#[test]
fn durability_policy_builder_roundtrip() {
    let cfg = SegmentConfig::builder()
        .durability(DurabilityPolicy::Throughput)
        .build();
    assert_eq!(cfg.durability, DurabilityPolicy::Throughput);
}

/// `SegmentConfig` is `Clone` since the cipher moved from `Box` to `Arc`.
/// A roundtrip through `.clone()` must preserve every field, including the
/// cipher (the `Arc` is shared, not duplicated).
#[test]
fn segment_config_is_clone() {
    let cfg = SegmentConfig::default();
    let cloned = cfg.clone();
    assert_eq!(cfg.flush_policy, cloned.flush_policy);
    assert_eq!(cfg.max_size_bytes, cloned.max_size_bytes);
    assert_eq!(cfg.compression_level, cloned.compression_level);
    assert_eq!(cfg.durability, cloned.durability);
    assert!(cfg.cipher.is_none() && cloned.cipher.is_none());
}

/// `SegmentConfigBuilder` is `Clone` (M12). This unblocks the pattern of
/// starting a base builder and cloning it per-buffer when constructing
/// several related buffers (e.g. a sharded producer set). The cipher
/// `Arc` is shared between clones — no key duplication.
#[test]
fn segment_config_builder_is_clone() {
    let base = SegmentConfig::builder()
        .flush_manually()
        .compression_level(7);
    let copy = base.clone();
    let cfg_a = base.build();
    let cfg_b = copy.compression_level(1).build();
    assert_eq!(cfg_a.compression_level, 7);
    assert_eq!(cfg_b.compression_level, 1);
    assert!(matches!(cfg_a.flush_policy, FlushPolicy::Manual));
    assert!(matches!(cfg_b.flush_policy, FlushPolicy::Manual));
}

#[cfg(feature = "encryption")]
#[test]
fn segment_config_clone_shares_cipher_arc() {
    let cfg = SegmentConfig::builder()
        .cipher(Arc::new(AesGcmCipher::new(&[0u8; 32])))
        .build();
    let cloned = cfg.clone();
    // Both configs reference the SAME Arc — cipher state is shared, not
    // duplicated. This is what makes `recommended_cipher()` and multi-buffer
    // setups cheap.
    let (Some(a), Some(b)) = (cfg.cipher.as_ref(), cloned.cipher.as_ref()) else {
        panic!("cipher must be Some on both configs");
    };
    assert!(
        std::ptr::addr_eq(a.as_ref() as *const _, b.as_ref() as *const _),
        "Arc must be shared, not deep-copied"
    );
}

#[cfg(feature = "encryption")]
#[test]
fn aes_gcm_new_and_from_slice_are_equivalent() {
    // The infallible `new(&[u8; 32])` and fallible `from_slice(&[u8])`
    // constructors must produce ciphers with the same underlying key
    // material: encrypt with one, decrypt with the other.
    let key = [42u8; 32];
    let infallible = AesGcmCipher::new(&key);
    let fallible = AesGcmCipher::from_slice(&key).unwrap();

    let plaintext = b"cross-constructor roundtrip";
    let ct = infallible.encrypt(plaintext).unwrap();
    let pt = fallible.decrypt(&ct).unwrap();
    assert_eq!(pt.as_slice(), plaintext);

    let ct2 = fallible.encrypt(plaintext).unwrap();
    let pt2 = infallible.decrypt(&ct2).unwrap();
    assert_eq!(pt2.as_slice(), plaintext);
}

#[cfg(feature = "encryption")]
#[test]
fn xchacha20_new_and_from_slice_are_equivalent() {
    let key = [99u8; 32];
    let infallible = XChaCha20Poly1305Cipher::new(&key);
    let fallible = XChaCha20Poly1305Cipher::from_slice(&key).unwrap();

    let plaintext = b"cross-constructor roundtrip";
    let ct = infallible.encrypt(plaintext).unwrap();
    let pt = fallible.decrypt(&ct).unwrap();
    assert_eq!(pt.as_slice(), plaintext);

    let ct2 = fallible.encrypt(plaintext).unwrap();
    let pt2 = infallible.decrypt(&ct2).unwrap();
    assert_eq!(pt2.as_slice(), plaintext);
}

// =========================================================================
// segment_size_stats — on-demand min/max/mean/p50/p90 size distribution.
// =========================================================================

#[test]
fn segment_size_stats_all_zero_when_nothing_flushed() {
    let tmp = TempDir::new().unwrap();
    let buf = SegmentBuffer::open(
        tmp.path(),
        SegmentConfig {
            flush_policy: FlushPolicy::Manual,
            ..test_config(1024 * 1024)
        },
    )
    .unwrap();
    // Append without flushing: items stay in memory, no segment files on disk.
    for i in 0..4 {
        buf.append(test_item(i)).unwrap();
    }
    let s = buf.segment_size_stats().unwrap();
    assert_eq!(s.count, 0, "no segments on disk yet");
    assert_eq!(s.min_bytes, 0);
    assert_eq!(s.max_bytes, 0);
    assert_eq!(s.mean_bytes, 0);
    assert_eq!(s.p50_bytes, 0);
    assert_eq!(s.p90_bytes, 0);
}

#[test]
fn segment_size_stats_single_segment_all_fields_equal() {
    let tmp = TempDir::new().unwrap();
    let buf = SegmentBuffer::open(
        tmp.path(),
        SegmentConfig {
            flush_policy: FlushPolicy::Manual,
            ..test_config(1024 * 1024)
        },
    )
    .unwrap();
    for i in 0..4 {
        buf.append(test_item(i)).unwrap();
    }
    buf.flush().unwrap();
    let s = buf.segment_size_stats().unwrap();
    assert_eq!(s.count, 1);
    assert!(s.max_bytes > 0, "flushed segment must have nonzero size");
    // With one segment, min == mean == p50 == p90 == max.
    assert_eq!(s.min_bytes, s.max_bytes);
    assert_eq!(s.mean_bytes, s.max_bytes);
    assert_eq!(s.p50_bytes, s.max_bytes);
    assert_eq!(s.p90_bytes, s.max_bytes);
}

#[test]
fn segment_size_stats_matches_manual_recompute_and_percentiles() {
    let tmp = TempDir::new().unwrap();
    let buf = SegmentBuffer::open(
        tmp.path(),
        SegmentConfig {
            flush_policy: FlushPolicy::Manual,
            ..test_config(1024 * 1024)
        },
    )
    .unwrap();
    // Five segments of varying item counts so the byte sizes differ.
    for n in [3u64, 1, 5, 2, 4] {
        for i in 0..n {
            buf.append(test_item(i)).unwrap();
        }
        buf.flush().unwrap();
    }
    let s = buf.segment_size_stats().unwrap();
    assert_eq!(s.count, 5);
    assert_eq!(s.count, count_disk_segments(tmp.path()));

    // Independent brute-force straight from the directory.
    let mut sizes: Vec<u64> = fs::read_dir(tmp.path())
        .unwrap()
        .filter_map(std::result::Result::ok)
        .filter(|e| e.file_name().to_string_lossy().ends_with(".zst"))
        .map(|e| e.metadata().map_or(0, |m| m.len()))
        .collect();
    sizes.sort();
    assert_eq!(s.min_bytes, *sizes.first().unwrap());
    assert_eq!(s.max_bytes, *sizes.last().unwrap());
    let total: u64 = sizes.iter().sum();
    assert_eq!(s.mean_bytes, total / sizes.len() as u64);
    // Cross-validate the nearest-rank percentiles against an independent
    // float implementation of ceil(p/100 · n).
    let n = sizes.len();
    let rank = |pct: f64| -> usize {
        let r = (pct / 100.0 * n as f64).ceil() as usize;
        r.clamp(1, n) - 1
    };
    assert_eq!(s.p50_bytes, sizes[rank(50.0)]);
    assert_eq!(s.p90_bytes, sizes[rank(90.0)]);
    // Monotonicity invariant.
    assert!(s.min_bytes <= s.p50_bytes);
    assert!(s.p50_bytes <= s.p90_bytes);
    assert!(s.p90_bytes <= s.max_bytes);
}

#[test]
fn segment_size_stats_reflects_delete_acked() {
    let tmp = TempDir::new().unwrap();
    let buf = SegmentBuffer::open(
        tmp.path(),
        SegmentConfig {
            flush_policy: FlushPolicy::Manual,
            ..test_config(1024 * 1024)
        },
    )
    .unwrap();
    // Three segments: [0,3], [4,7], [8,11] (4 items each).
    for _ in 0..3 {
        for i in 0..4u64 {
            buf.append(test_item(i)).unwrap();
        }
        buf.flush().unwrap();
    }
    let before = buf.segment_size_stats().unwrap();
    assert_eq!(before.count, 3);

    // Ack the first segment (covers seqs 0..=3).
    buf.delete_acked(3).unwrap();
    let after = buf.segment_size_stats().unwrap();
    assert_eq!(
        after.count, 2,
        "first segment must be gone after acking seq 3"
    );
    assert_eq!(after.count, count_disk_segments(tmp.path()));
    assert!(after.max_bytes <= before.max_bytes);
}

#[test]
fn segment_size_stats_count_and_mean_consistent_after_sync() {
    let tmp = TempDir::new().unwrap();
    let buf = SegmentBuffer::open(
        tmp.path(),
        SegmentConfig {
            flush_policy: FlushPolicy::Manual,
            ..test_config(1024 * 1024)
        },
    )
    .unwrap();
    for _ in 0..4 {
        for i in 0..5u64 {
            buf.append(test_item(i)).unwrap();
        }
        buf.flush().unwrap();
    }
    // Recalibrate the atomic counters, then compare scan-derived count.
    buf.sync_disk_bytes().unwrap();
    let s = buf.segment_size_stats().unwrap();
    let stats = buf.stats();
    assert_eq!(s.count, stats.segment_count);

    // mean = total / count (truncated), so mean·count <= total < mean·count + count.
    let actual_total: u64 = fs::read_dir(tmp.path())
        .unwrap()
        .filter_map(std::result::Result::ok)
        .filter(|e| e.file_name().to_string_lossy().ends_with(".zst"))
        .map(|e| e.metadata().map_or(0, |m| m.len()))
        .sum();
    let mean_times_count = s.mean_bytes.saturating_mul(s.count);
    assert!(
        mean_times_count <= actual_total,
        "mean·count must not exceed the real total"
    );
    assert!(
        actual_total < mean_times_count + s.count,
        "truncation error must be less than count"
    );
}

// =========================================================================
// percentile_of_sorted — direct edge-case tests for the private nearest-rank
// helper. Until now it was only exercised indirectly through segment_size_stats.
// These tests make the nearest-rank contract visible and pinned.
// =========================================================================

#[test]
fn percentile_of_sorted_empty_returns_zero() {
    let sorted: [u64; 0] = [];
    assert_eq!(
        SegmentBuffer::<TestItem>::percentile_of_sorted(&sorted, 50),
        0,
        "empty input must return 0"
    );
}

#[test]
fn percentile_of_sorted_pct_zero_returns_minimum() {
    // pct=0: rank = ceil(0/100 . n) = 0, clamped to 1, so first element.
    let sorted = [10u64, 20, 30, 40, 50];
    assert_eq!(
        SegmentBuffer::<TestItem>::percentile_of_sorted(&sorted, 0),
        10,
        "pct=0 must return the smallest element"
    );
}

#[test]
fn percentile_of_sorted_pct_hundred_returns_maximum() {
    // pct=100: rank = ceil(100/100 . n) = n, so last element.
    let sorted = [10u64, 20, 30, 40, 50];
    assert_eq!(
        SegmentBuffer::<TestItem>::percentile_of_sorted(&sorted, 100),
        50,
        "pct=100 must return the largest element"
    );
}

#[test]
fn percentile_of_sorted_single_element_returns_it_for_all_pct() {
    let sorted = [42u64];
    for pct in 0u32..=100 {
        assert_eq!(
            SegmentBuffer::<TestItem>::percentile_of_sorted(&sorted, pct),
            42,
            "single element must be returned for pct={pct}"
        );
    }
}

#[test]
fn percentile_of_sorted_is_monotonically_nondecreasing_in_pct() {
    let sorted = [5u64, 10, 15, 20, 25, 30, 35, 40, 45, 50];
    let mut prev = 0u64;
    for pct in 0u32..=100 {
        let val = SegmentBuffer::<TestItem>::percentile_of_sorted(&sorted, pct);
        assert!(
            val >= prev,
            "result must be non-decreasing: pct={pct} gave {val} < {prev}"
        );
        prev = val;
    }
}

#[cfg(feature = "encryption")]
#[test]
fn segment_size_stats_works_with_encrypted_segments() {
    let tmp = TempDir::new().unwrap();
    let buf = SegmentBuffer::open(
        tmp.path(),
        SegmentConfig {
            flush_policy: FlushPolicy::Manual,
            max_size_bytes: 1024 * 1024,
            compression_level: 3,
            durability: DurabilityPolicy::Segment,
            cipher: Some(Arc::new(AesGcmCipher::new(&[0u8; 32]))),
        },
    )
    .unwrap();

    // Three flushes with varying item counts so byte sizes differ.
    for n in [3u64, 1, 5] {
        for i in 0..n {
            buf.append(test_item(i)).unwrap();
        }
        buf.flush().unwrap();
    }

    let s = buf.segment_size_stats().unwrap();
    assert_eq!(s.count, 3);
    assert_eq!(s.count, count_disk_segments(tmp.path()));

    // Cross-check every field against a brute-force directory scan.
    let mut sizes: Vec<u64> = fs::read_dir(tmp.path())
        .unwrap()
        .filter_map(std::result::Result::ok)
        .filter(|e| e.file_name().to_string_lossy().ends_with(".zst"))
        .map(|e| e.metadata().map_or(0, |m| m.len()))
        .collect();
    sizes.sort();
    assert_eq!(s.min_bytes, *sizes.first().unwrap());
    assert_eq!(s.max_bytes, *sizes.last().unwrap());
    let total: u64 = sizes.iter().sum();
    assert_eq!(s.mean_bytes, total / sizes.len() as u64);
    let n = sizes.len();
    let rank = |pct: f64| -> usize {
        let r = (pct / 100.0 * n as f64).ceil() as usize;
        r.clamp(1, n) - 1
    };
    assert_eq!(s.p50_bytes, sizes[rank(50.0)]);
    assert_eq!(s.p90_bytes, sizes[rank(90.0)]);
    assert!(s.min_bytes <= s.p50_bytes);
    assert!(s.p50_bytes <= s.p90_bytes);
    assert!(s.p90_bytes <= s.max_bytes);
}