zesven 2.0.0

A pure Rust implementation of the 7z archive format
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
//! Integration tests for async API functionality.
//!
//! These tests verify the async archive reading and writing functionality
//! with Tokio runtime.

// Every test here writes an archive first, which needs a codec.
#![cfg(all(feature = "async", feature = "lzma2"))]

use std::io::Cursor;

use zesven::format::property_id;
use zesven::{
    ArchivePath, AsyncArchive, AsyncExtractOptions, AsyncProgressCallback, AsyncWriter,
    CancellationToken, WriteOptions,
};

/// Helper to create a minimal valid 7z archive in memory.
fn make_empty_archive() -> Vec<u8> {
    let mut data = Vec::new();

    // Signature
    data.extend_from_slice(&[0x37, 0x7A, 0xBC, 0xAF, 0x27, 0x1C]);
    // Version (0.4)
    data.extend_from_slice(&[0x00, 0x04]);

    // Start header CRC (placeholder)
    let start_header_crc_pos = data.len();
    data.extend_from_slice(&[0x00, 0x00, 0x00, 0x00]);

    // Next header offset (0 - header immediately follows)
    data.extend_from_slice(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]);

    // Header data: HEADER marker followed by END
    let header_data = vec![property_id::HEADER, property_id::END];

    // Next header size
    let header_size = header_data.len() as u64;
    data.extend_from_slice(&header_size.to_le_bytes());

    // Next header CRC
    let header_crc = crc32fast::hash(&header_data);
    data.extend_from_slice(&header_crc.to_le_bytes());

    // Compute start header CRC (covers bytes 12-31: offset, size, crc)
    let start_header_crc = crc32fast::hash(&data[12..32]);
    data[start_header_crc_pos..start_header_crc_pos + 4]
        .copy_from_slice(&start_header_crc.to_le_bytes());

    // Append header data
    data.extend_from_slice(&header_data);

    data
}

// ============================================================================
// AsyncArchive Tests
// ============================================================================

#[tokio::test]
async fn test_async_archive_open_empty() {
    let data = make_empty_archive();
    let cursor = Cursor::new(data);
    let archive = AsyncArchive::open(cursor).await.unwrap();

    assert!(archive.is_empty());
    assert_eq!(archive.len(), 0);
}

#[tokio::test]
async fn test_async_archive_info() {
    let data = make_empty_archive();
    let cursor = Cursor::new(data);
    let archive = AsyncArchive::open(cursor).await.unwrap();

    let info = archive.info();
    assert_eq!(info.entry_count, 0);
    assert!(!info.is_solid);
    assert!(!info.has_encrypted_entries);
}

#[tokio::test]
async fn test_async_archive_entries() {
    let data = make_empty_archive();
    let cursor = Cursor::new(data);
    let archive = AsyncArchive::open(cursor).await.unwrap();

    assert!(archive.entries().is_empty());
    assert!(archive.entry("nonexistent").is_none());
}

// ============================================================================
// AsyncArchive Error Path Tests
// ============================================================================
//
// These tests verify that the async API correctly propagates error types.
// We test two key error scenarios:
// 1. Parse-time errors (invalid signature) - error during archive opening
// 2. Extract-time errors (truncated data) - error during extraction
//
// Additional error scenarios (corrupted CRC, malformed headers) are tested
// extensively in tests/malformed_archives.rs for the sync API. These tests
// serve as regression guards for the async wrapper.
// ============================================================================

#[tokio::test]
async fn test_async_archive_open_invalid_signature() {
    // Random bytes that don't form a valid 7z signature
    let data: &[u8] = &[0xDE, 0xAD, 0xBE, 0xEF, 0xCA, 0xFE, 0xBA, 0xBE];
    let cursor = Cursor::new(data);

    match AsyncArchive::open(cursor).await {
        Err(zesven::Error::InvalidFormat(_)) => {} // Expected
        Err(e) => panic!(
            "Expected InvalidFormat error for invalid signature, got: {:?}",
            e
        ),
        Ok(_) => panic!("Should fail for invalid signature"),
    }
}

#[tokio::test]
async fn test_async_archive_extract_truncated_data() {
    // Create a valid archive first
    let buffer = Cursor::new(Vec::new());
    let mut writer = AsyncWriter::create(buffer).await.unwrap();

    // Add file with substantial content
    let content = b"This content will be truncated in the test archive";
    writer
        .add_bytes(ArchivePath::new("test.txt").unwrap(), content)
        .await
        .unwrap();

    let (_, cursor) = writer.finish_into_inner().await.unwrap();
    let mut archive_bytes = cursor.into_inner();

    // Truncate the archive data (remove last 20 bytes to corrupt compressed data)
    if archive_bytes.len() > 30 {
        archive_bytes.truncate(archive_bytes.len() - 20);
    }

    // Opening might succeed (header may still be readable)
    // but extraction should fail
    let read_cursor = Cursor::new(archive_bytes);
    match AsyncArchive::open(read_cursor).await {
        Ok(mut archive) => {
            // If archive opened, extraction should fail
            let temp_dir = tempfile::tempdir().unwrap();
            let result = archive
                .extract(temp_dir.path(), (), &AsyncExtractOptions::default())
                .await;

            // Should get an error - either Io, InvalidFormat, or corruption-related
            assert!(
                result.is_err(),
                "Extraction of truncated archive should fail"
            );
        }
        Err(_) => {
            // Also acceptable - archive failed to open due to truncation
        }
    }
}

// =============================================================================
// Async Archive Encryption
// =============================================================================

/// Builds an archive in memory with the synchronous writer.
#[cfg(feature = "aes")]
fn build_archive(options: WriteOptions, entries: &[(&str, &[u8])]) -> Vec<u8> {
    use zesven::Writer;

    let mut bytes = Vec::new();
    {
        let mut writer = Writer::create(Cursor::new(&mut bytes))
            .expect("create writer")
            .options(options);
        for (path, data) in entries {
            writer
                .add_bytes(ArchivePath::new(path).expect("valid path"), data)
                .expect("add entry");
        }
        let _ = writer.finish().expect("finish");
    }
    bytes
}

/// Header-encrypted archives must open, list and extract through the async API.
#[cfg(feature = "aes")]
#[tokio::test]
async fn test_async_archive_header_encrypted() {
    use zesven::crypto::{NoncePolicy, Password};

    let payload = b"async encrypted payload";
    let bytes = build_archive(
        WriteOptions::new()
            .password(Password::new("hunter2"))
            .encrypt_header(true)
            .nonce_policy(NoncePolicy::random_with_params(4, 8)),
        &[("secret.txt", payload.as_slice())],
    );

    let archive = AsyncArchive::open_with_password(Cursor::new(bytes.clone()), "hunter2")
        .await
        .expect("header-encrypted archive must open with the right password");
    assert_eq!(archive.entries().len(), 1);
    assert_eq!(archive.entries()[0].path.as_str(), "secret.txt");

    // Without the password the file names are not even readable.
    assert!(
        AsyncArchive::open(Cursor::new(bytes)).await.is_err(),
        "a header-encrypted archive must not open without a password"
    );
}

/// A filtered folder has more than one coder; decoding only the first returns
/// data of the right length and the wrong contents.
#[cfg(feature = "aes")]
#[tokio::test]
async fn test_async_archive_extracts_filtered_and_encrypted() {
    use zesven::WriteFilter;
    use zesven::crypto::{NoncePolicy, Password};

    let payload: Vec<u8> = (0..4096u32)
        .map(|i| if i % 16 == 0 { 0xE8 } else { (i % 251) as u8 })
        .collect();
    let bytes = build_archive(
        WriteOptions::new()
            .password(Password::new("hunter2"))
            .filter(WriteFilter::BcjX86)
            .nonce_policy(NoncePolicy::random_with_params(4, 8)),
        &[("program.bin", payload.as_slice())],
    );

    let temp = tempfile::TempDir::new().expect("temp dir");
    let mut archive = AsyncArchive::open_with_password(Cursor::new(bytes), "hunter2")
        .await
        .expect("open");
    let _ = archive
        .extract(temp.path(), (), &AsyncExtractOptions::default())
        .await
        .expect("extraction of a filtered encrypted folder must succeed");

    let extracted = tokio::fs::read(temp.path().join("program.bin"))
        .await
        .expect("read extracted file");
    assert_eq!(
        extracted, payload,
        "the filter and the cipher must both be applied"
    );
}

/// Test that dropping an async writer without finishing doesn't panic.
///
/// This verifies that the async writer handles early termination gracefully,
/// which is important for cancellation scenarios.
#[tokio::test]
async fn test_async_writer_drop_without_finish() {
    let buffer = Cursor::new(Vec::new());
    let mut writer = AsyncWriter::create(buffer).await.unwrap();

    // Add some content but don't finish
    writer
        .add_bytes(ArchivePath::new("file.txt").unwrap(), b"content")
        .await
        .unwrap();

    // Drop the writer without calling finish()
    // This should not panic - test passes if we reach this point
    drop(writer);
}

// ============================================================================
// AsyncWriter Tests
// ============================================================================

#[tokio::test]
async fn test_async_writer_create() {
    let buffer = Cursor::new(Vec::new());
    let _writer = AsyncWriter::create(buffer).await.unwrap();
}

#[tokio::test]
async fn test_async_writer_empty_archive() {
    let buffer = Cursor::new(Vec::new());
    let writer = AsyncWriter::create(buffer).await.unwrap();

    let result = writer.finish().await.unwrap();
    assert_eq!(result.entries_written, 0);
    assert_eq!(result.directories_written, 0);
}

#[tokio::test]
async fn test_async_writer_add_bytes() {
    let buffer = Cursor::new(Vec::new());
    let mut writer = AsyncWriter::create(buffer).await.unwrap();

    let path = ArchivePath::new("test.txt").unwrap();
    writer
        .add_bytes(path, b"Hello, async world!")
        .await
        .unwrap();

    let result = writer.finish().await.unwrap();
    assert_eq!(result.entries_written, 1);
    assert_eq!(result.total_size, 19);
}

#[tokio::test]
async fn test_async_writer_add_multiple_entries() {
    let buffer = Cursor::new(Vec::new());
    let mut writer = AsyncWriter::create(buffer).await.unwrap();

    writer
        .add_bytes(ArchivePath::new("file1.txt").unwrap(), b"Content 1")
        .await
        .unwrap();
    writer
        .add_bytes(ArchivePath::new("file2.txt").unwrap(), b"Content 2")
        .await
        .unwrap();
    writer
        .add_bytes(ArchivePath::new("file3.txt").unwrap(), b"Content 3")
        .await
        .unwrap();

    let result = writer.finish().await.unwrap();
    assert_eq!(result.entries_written, 3);
}

#[tokio::test]
async fn test_async_writer_with_directory() {
    let buffer = Cursor::new(Vec::new());
    let mut writer = AsyncWriter::create(buffer).await.unwrap();

    use zesven::write::EntryMeta;
    let dir_path = ArchivePath::new("mydir").unwrap();
    writer
        .add_directory(dir_path, EntryMeta::directory())
        .await
        .unwrap();

    let result = writer.finish().await.unwrap();
    assert_eq!(result.entries_written, 0);
    assert_eq!(result.directories_written, 1);
}

#[tokio::test]
async fn test_async_writer_with_options() {
    use zesven::codec::CodecMethod;

    let buffer = Cursor::new(Vec::new());
    let writer = AsyncWriter::create(buffer).await.unwrap().options(
        WriteOptions::new()
            .method(CodecMethod::Copy)
            .level(0)
            .unwrap(),
    );

    let result = writer.finish().await.unwrap();
    assert_eq!(result.entries_written, 0);
}

// ============================================================================
// AsyncExtractOptions Tests
// ============================================================================

#[tokio::test]
async fn test_async_extract_options_default() {
    let options = AsyncExtractOptions::default();
    assert!(!options.is_cancelled());
}

#[tokio::test]
async fn test_async_extract_options_cancellation() {
    let token = CancellationToken::new();
    let options = AsyncExtractOptions::new().cancel_token(token.clone());

    assert!(!options.is_cancelled());
    token.cancel();
    assert!(options.is_cancelled());
}

#[tokio::test]
async fn test_async_extract_options_builder() {
    use std::num::NonZeroUsize;
    use zesven::read::{OverwritePolicy, PathSafety, Threads};

    let options = AsyncExtractOptions::new()
        .overwrite(OverwritePolicy::Skip)
        .path_safety(PathSafety::Relaxed)
        .threads(Threads::Count(NonZeroUsize::new(4).unwrap()));

    assert_eq!(options.overwrite, OverwritePolicy::Skip);
    assert_eq!(options.path_safety, PathSafety::Relaxed);
    assert_eq!(
        options.threads,
        Threads::Count(NonZeroUsize::new(4).unwrap())
    );
}

// ============================================================================
// Round-Trip Tests
// ============================================================================

#[tokio::test]
async fn test_async_round_trip_single_file() {
    // Write archive
    let buffer = Cursor::new(Vec::new());
    let mut writer = AsyncWriter::create(buffer).await.unwrap();

    let content = b"Hello, async round-trip test!";
    writer
        .add_bytes(ArchivePath::new("test.txt").unwrap(), content)
        .await
        .unwrap();

    // Use finish_into_inner to get access to the archive bytes
    let (result, cursor) = writer.finish_into_inner().await.unwrap();
    assert!(result.total_size > 0);
    assert_eq!(result.entries_written, 1);

    // Get the archive bytes from the cursor
    let archive_bytes = cursor.into_inner();
    assert!(!archive_bytes.is_empty());

    // Read back and verify content
    let read_cursor = Cursor::new(archive_bytes);
    let mut archive = AsyncArchive::open(read_cursor).await.unwrap();

    // Verify entry exists
    let entries = archive.entries();
    assert_eq!(entries.len(), 1);
    assert_eq!(entries[0].path.as_str(), "test.txt");

    // Extract to temp directory and verify content
    let temp_dir = tempfile::tempdir().unwrap();
    let _ = archive
        .extract(temp_dir.path(), (), &AsyncExtractOptions::default())
        .await
        .unwrap();

    // Read extracted file and verify content
    let extracted_content = tokio::fs::read(temp_dir.path().join("test.txt"))
        .await
        .unwrap();
    assert_eq!(extracted_content, content);
}

#[tokio::test]
async fn test_async_round_trip_multiple_files() {
    // Write archive with multiple files
    let buffer = Cursor::new(Vec::new());
    let mut writer = AsyncWriter::create(buffer).await.unwrap();

    let files = [
        ("file1.txt", b"First file content".as_slice()),
        ("file2.txt", b"Second file content".as_slice()),
        ("subdir/file3.txt", b"Third file in subdirectory".as_slice()),
    ];

    for (path, content) in &files {
        writer
            .add_bytes(ArchivePath::new(path).unwrap(), content)
            .await
            .unwrap();
    }

    // Finish and get archive bytes
    let (result, cursor) = writer.finish_into_inner().await.unwrap();
    assert_eq!(result.entries_written, 3);

    // Read back
    let archive_bytes = cursor.into_inner();
    let read_cursor = Cursor::new(archive_bytes);
    let mut archive = AsyncArchive::open(read_cursor).await.unwrap();

    // Verify entries
    let entries = archive.entries();
    assert_eq!(entries.len(), 3);

    // Extract and verify each file
    let temp_dir = tempfile::tempdir().unwrap();
    let _ = archive
        .extract(temp_dir.path(), (), &AsyncExtractOptions::default())
        .await
        .unwrap();

    for (path, expected_content) in &files {
        let file_path = temp_dir.path().join(path);
        let actual_content = tokio::fs::read(&file_path).await.unwrap();
        assert_eq!(
            actual_content.as_slice(),
            *expected_content,
            "Content mismatch for {}",
            path
        );
    }
}

// ============================================================================
// Cancellation Tests
// ============================================================================

#[tokio::test]
async fn test_cancellation_before_extract() {
    // Create an archive with content to extract
    let buffer = Cursor::new(Vec::new());
    let mut writer = AsyncWriter::create(buffer).await.unwrap();

    // Add multiple files to increase chance of cancellation being checked
    for i in 0..10 {
        writer
            .add_bytes(
                ArchivePath::new(&format!("file{}.txt", i)).unwrap(),
                format!("Content for file {}", i).as_bytes(),
            )
            .await
            .unwrap();
    }

    let (_, cursor) = writer.finish_into_inner().await.unwrap();
    let archive_bytes = cursor.into_inner();

    let read_cursor = Cursor::new(archive_bytes);
    let mut archive = AsyncArchive::open(read_cursor).await.unwrap();

    let token = CancellationToken::new();
    token.cancel(); // Cancel before extraction

    let options = AsyncExtractOptions::new().cancel_token(token);
    let temp_dir = tempfile::tempdir().unwrap();

    let result = archive.extract(temp_dir.path(), (), &options).await;

    // With a pre-cancelled token, extraction should return Cancelled error
    assert!(
        matches!(result, Err(zesven::Error::Cancelled)),
        "Expected Cancelled error with pre-cancelled token, got: {:?}",
        result
    );
}

#[tokio::test]
async fn test_extract_with_cancellation_precancelled() {
    // Tests extract_with_cancellation with a pre-cancelled token for deterministic behavior.
    // This verifies the cancellation check in the select! macro works correctly.

    let buffer = Cursor::new(Vec::new());
    let mut writer = AsyncWriter::create(buffer).await.unwrap();

    for i in 0..10 {
        writer
            .add_bytes(
                ArchivePath::new(&format!("file{}.txt", i)).unwrap(),
                format!("Content for file {}", i).as_bytes(),
            )
            .await
            .unwrap();
    }

    let (_, cursor) = writer.finish_into_inner().await.unwrap();
    let archive_bytes = cursor.into_inner();

    let read_cursor = Cursor::new(archive_bytes);
    let mut archive = AsyncArchive::open(read_cursor).await.unwrap();

    let token = CancellationToken::new();
    token.cancel(); // Pre-cancel for deterministic test

    let options = AsyncExtractOptions::default();
    let temp_dir = tempfile::tempdir().unwrap();

    let result = archive
        .extract_with_cancellation(temp_dir.path(), (), &options, token)
        .await;

    // With pre-cancelled token, extract_with_cancellation should return Cancelled
    assert!(
        matches!(result, Err(zesven::Error::Cancelled)),
        "Expected Cancelled error with pre-cancelled token, got: {:?}",
        result
    );
}

// ============================================================================
// Progress Callback Tests
// ============================================================================

#[tokio::test]
async fn test_channel_progress_reporter() {
    use std::sync::Arc;
    use zesven::{ChannelProgressReporter, ProgressEvent};

    let (reporter, mut rx) = ChannelProgressReporter::new(10);
    let reporter = Arc::new(reporter);

    // Test sending events
    reporter.on_entry_start("test.txt", 100).await;
    reporter.on_progress(50, 100).await;
    reporter.on_entry_complete("test.txt", true).await;

    // Verify events received
    let event1 = rx.recv().await.unwrap();
    assert!(matches!(
        event1,
        ProgressEvent::EntryStart {
            name,
            size: 100
        } if name == "test.txt"
    ));

    let event2 = rx.recv().await.unwrap();
    assert!(matches!(
        event2,
        ProgressEvent::Progress {
            bytes_extracted: 50,
            total_bytes: 100
        }
    ));

    let event3 = rx.recv().await.unwrap();
    assert!(matches!(
        event3,
        ProgressEvent::EntryComplete {
            name,
            success: true
        } if name == "test.txt"
    ));
}

/// Tests that progress events are correctly reported during actual extraction.
///
/// This integration test verifies that the AsyncProgressCallback implementation
/// receives all expected events (EntryStart, EntryComplete) during a real
/// extraction operation, not just in isolated callback testing.
#[tokio::test]
async fn test_async_extraction_with_progress_callback() {
    use std::sync::Arc;
    use zesven::{ChannelProgressReporter, ProgressEvent};

    // Create archive with multiple files
    let buffer = Cursor::new(Vec::new());
    let mut writer = AsyncWriter::create(buffer).await.unwrap();

    let files = [
        ("file1.txt", b"Content for file one".as_slice()),
        ("file2.txt", b"Content for file two".as_slice()),
        ("subdir/file3.txt", b"Content in subdirectory".as_slice()),
    ];

    for (path, content) in &files {
        writer
            .add_bytes(ArchivePath::new(path).unwrap(), content)
            .await
            .unwrap();
    }

    let (_, cursor) = writer.finish_into_inner().await.unwrap();
    let archive_bytes = cursor.into_inner();

    // Use the built-in ChannelProgressReporter to collect events
    let (reporter, mut rx) = ChannelProgressReporter::new(100);
    let reporter = Arc::new(reporter);

    // Open and extract with progress callback
    let read_cursor = Cursor::new(archive_bytes);
    let mut archive = AsyncArchive::open(read_cursor).await.unwrap();

    let options = AsyncExtractOptions::new().progress(reporter);
    let temp_dir = tempfile::tempdir().unwrap();

    let result = archive
        .extract(temp_dir.path(), (), &options)
        .await
        .unwrap();
    assert_eq!(result.entries_extracted, 3);

    // Collect all events from the channel
    let mut events = Vec::new();
    while let Ok(event) = rx.try_recv() {
        events.push(event);
    }

    // Count event types
    let start_events: Vec<_> = events
        .iter()
        .filter(|e| matches!(e, ProgressEvent::EntryStart { .. }))
        .collect();
    let complete_events: Vec<_> = events
        .iter()
        .filter(|e| matches!(e, ProgressEvent::EntryComplete { .. }))
        .collect();

    // Should have EntryStart and EntryComplete for each file
    assert_eq!(
        start_events.len(),
        3,
        "Expected 3 EntryStart events, got {}",
        start_events.len()
    );
    assert_eq!(
        complete_events.len(),
        3,
        "Expected 3 EntryComplete events, got {}",
        complete_events.len()
    );

    // Verify all entries completed successfully
    for event in &complete_events {
        if let ProgressEvent::EntryComplete { success, .. } = event {
            assert!(success, "All entries should complete successfully");
        }
    }

    // Verify the files we expected were reported
    let reported_names: Vec<_> = start_events
        .iter()
        .filter_map(|e| {
            if let ProgressEvent::EntryStart { name, .. } = e {
                Some(name.as_str())
            } else {
                None
            }
        })
        .collect();

    for (expected_path, _) in &files {
        assert!(
            reported_names.contains(expected_path),
            "Expected '{}' in progress events, got {:?}",
            expected_path,
            reported_names
        );
    }
}

// ============================================================================
// Password Provider Tests (requires aes feature)
// ============================================================================

// as unit tests, since they test internal implementation details.

#[cfg(feature = "aes")]
mod password_tests {
    use zesven::Password;
    use zesven::async_password::{
        AsyncPassword, AsyncPasswordProvider, InteractivePasswordProvider,
    };

    #[tokio::test]
    async fn test_async_password_with_value() {
        let provider = AsyncPassword::new("test_password");
        let password = provider.get_password().await;
        assert!(password.is_some());
        assert_eq!(password.unwrap().as_str(), "test_password");
    }

    #[tokio::test]
    async fn test_async_password_none() {
        let provider = AsyncPassword::none();
        let password = provider.get_password().await;
        assert!(password.is_none());
    }

    #[tokio::test]
    async fn test_interactive_password_provider() {
        let (tx, provider) = InteractivePasswordProvider::new();

        tokio::spawn(async move {
            tokio::time::sleep(std::time::Duration::from_millis(10)).await;
            tx.send(Some(Password::new("interactive_password"))).ok();
        });

        let password = provider.get_password().await;
        assert!(password.is_some());
        assert_eq!(password.unwrap().as_str(), "interactive_password");
    }

    #[tokio::test]
    async fn test_interactive_password_provider_cancelled() {
        let (tx, provider) = InteractivePasswordProvider::new();
        drop(tx); // Drop sender to simulate cancellation

        let password = provider.get_password().await;
        assert!(password.is_none());
    }
}

// ============================================================================
// Concurrency Tests
// ============================================================================

#[tokio::test]
async fn test_concurrent_writes() {
    // Test that multiple async writers can work concurrently
    let handles: Vec<_> = (0..4)
        .map(|i| {
            tokio::spawn(async move {
                let buffer = Cursor::new(Vec::new());
                let mut writer = AsyncWriter::create(buffer).await.unwrap();

                writer
                    .add_bytes(
                        ArchivePath::new(&format!("file{}.txt", i)).unwrap(),
                        format!("Content from task {}", i).as_bytes(),
                    )
                    .await
                    .unwrap();

                writer.finish().await.unwrap()
            })
        })
        .collect();

    for handle in handles {
        let result = handle.await.unwrap();
        assert_eq!(result.entries_written, 1);
    }
}

#[tokio::test]
async fn test_concurrent_reads() {
    // Create test archives
    let archives: Vec<_> = (0..4).map(|_| make_empty_archive()).collect();

    // Read them concurrently
    let handles: Vec<_> = archives
        .into_iter()
        .map(|data| {
            tokio::spawn(async move {
                let cursor = Cursor::new(data);
                let archive = AsyncArchive::open(cursor).await.unwrap();
                archive.len()
            })
        })
        .collect();

    for handle in handles {
        let count = handle.await.unwrap();
        assert_eq!(count, 0); // Empty archives
    }
}

// ============================================================================
// No-blocking Verification Tests
// ============================================================================

#[tokio::test]
async fn test_async_operations_dont_block() {
    // Run with current thread runtime to detect blocking
    // If any operation blocks, this will timeout or hang

    let result = tokio::time::timeout(std::time::Duration::from_secs(5), async {
        // Create and open empty archive
        let data = make_empty_archive();
        let cursor = Cursor::new(data);
        let _archive = AsyncArchive::open(cursor).await.unwrap();

        // Create writer and write content
        let buffer = Cursor::new(Vec::new());
        let mut writer = AsyncWriter::create(buffer).await.unwrap();
        writer
            .add_bytes(ArchivePath::new("test.txt").unwrap(), b"test content")
            .await
            .unwrap();
        let _ = writer.finish().await.unwrap();

        true
    })
    .await;

    assert!(result.is_ok());
}

// ============================================================================
// Cancellation During Active Extraction Tests
// ============================================================================

#[tokio::test]
async fn test_cancellation_during_larger_extraction() {
    // Create an archive with larger content to give cancellation time to trigger
    let buffer = Cursor::new(Vec::new());
    let mut writer = AsyncWriter::create(buffer).await.unwrap();

    // Add multiple files with substantial content (10 files provides adequate
    // cancellation opportunity while keeping test execution fast)
    for i in 0..10 {
        let content = format!(
            "File {} content with some padding to make it larger: {}",
            i,
            "x".repeat(1000)
        );
        writer
            .add_bytes(
                ArchivePath::new(&format!("file{:02}.txt", i)).unwrap(),
                content.as_bytes(),
            )
            .await
            .unwrap();
    }

    let (_, cursor) = writer.finish_into_inner().await.unwrap();
    let archive_bytes = cursor.into_inner();

    // Test 1: Cancel token triggered during extraction
    let read_cursor = Cursor::new(archive_bytes.clone());
    let mut archive = AsyncArchive::open(read_cursor).await.unwrap();

    let token = CancellationToken::new();
    let token_clone = token.clone();
    let options = AsyncExtractOptions::new().cancel_token(token_clone);
    let temp_dir = tempfile::tempdir().unwrap();

    // Spawn task that cancels after a tiny delay
    let cancel_handle = tokio::spawn(async move {
        tokio::time::sleep(std::time::Duration::from_micros(100)).await;
        token.cancel();
    });

    let result = archive.extract(temp_dir.path(), (), &options).await;

    // Wait for cancel task to complete
    let _ = cancel_handle.await;

    // Either extraction completed before cancellation, or cancellation was processed
    match result {
        Ok(extract_result) => {
            // Extraction completed successfully - valid outcome
            assert!(extract_result.entries_extracted > 0 || extract_result.entries_failed == 0);
        }
        Err(zesven::Error::Cancelled) => {
            // Cancellation was detected - valid outcome
        }
        Err(e) => {
            panic!("Expected Ok or Cancelled, got unexpected error: {:?}", e);
        }
    }
}

/// An empty entry must not swallow the entry that follows it.
///
/// The async writer gave every entry a folder, including empty ones. An empty
/// entry is recorded as kEmptyStream and carries no stream at all, so the extra
/// folder shifted the pairing: the next file came back empty and its own
/// contents were unreachable.
#[tokio::test]
async fn test_async_empty_entry_does_not_consume_the_next_one() {
    let mut writer = AsyncWriter::create(Cursor::new(Vec::new())).await.unwrap();
    writer
        .add_bytes(ArchivePath::new("empty.bin").unwrap(), b"")
        .await
        .unwrap();
    writer
        .add_bytes(ArchivePath::new("good.bin").unwrap(), b"GOOD")
        .await
        .unwrap();
    let (_result, cursor) = writer.finish_into_inner().await.unwrap();

    let mut archive = zesven::read::Archive::open(Cursor::new(cursor.into_inner())).unwrap();
    assert!(archive.extract_to_vec("empty.bin").unwrap().is_empty());
    assert_eq!(archive.extract_to_vec("good.bin").unwrap(), b"GOOD");
}

/// Deterministic mode in the async writer checks order rather than sorting.
///
/// Sorting the file list at the end rearranged names over streams that had
/// already been written, exactly as on the blocking path.
#[tokio::test]
async fn test_async_deterministic_mode_requires_sorted_entries() {
    let mut writer = AsyncWriter::create(Cursor::new(Vec::new()))
        .await
        .unwrap()
        .options(WriteOptions::new().deterministic(true));

    writer
        .add_bytes(ArchivePath::new("z.txt").unwrap(), b"CONTENT-Z")
        .await
        .unwrap();
    let out_of_order = writer
        .add_bytes(ArchivePath::new("a.txt").unwrap(), b"CONTENT-A")
        .await;

    assert!(
        out_of_order.is_err(),
        "adding an earlier-sorting path must fail rather than reorder the archive",
    );
}

/// Deterministic mode checks directories too, not only files.
///
/// The check lived in the byte-adding path alone, so a directory could be
/// added out of order and the entry after it was measured against the wrong
/// name.
#[tokio::test]
async fn test_async_deterministic_mode_checks_directories() {
    use zesven::write::EntryMeta;

    let mut writer = AsyncWriter::create(Cursor::new(Vec::new()))
        .await
        .unwrap()
        .options(WriteOptions::new().deterministic(true));

    writer
        .add_directory(ArchivePath::new("z").unwrap(), EntryMeta::directory())
        .await
        .unwrap();
    let out_of_order = writer
        .add_bytes(ArchivePath::new("a.txt").unwrap(), b"CONTENT-A")
        .await;

    assert!(
        out_of_order.is_err(),
        "a directory must advance the order like any other entry",
    );
}

/// Options the async writer does not implement are refused, not ignored.
///
/// It emits its own header and applies none of these, but accepted them and
/// wrote an archive without them: a caller who asked for a filtered, solid or
/// commented archive got a plain one and no indication of it.
#[tokio::test]
async fn test_async_writer_refuses_options_it_cannot_apply() {
    use zesven::WriteFilter;

    let cases = [
        ("filter", WriteOptions::new().filter(WriteFilter::delta(4))),
        ("solid", WriteOptions::new().solid()),
        ("comment", WriteOptions::new().comment("hello")),
    ];

    for (name, options) in cases {
        let mut writer = AsyncWriter::create(Cursor::new(Vec::new()))
            .await
            .unwrap()
            .options(options);

        assert!(
            writer
                .add_bytes(ArchivePath::new("data.bin").unwrap(), b"DATA")
                .await
                .is_err(),
            "{name} was accepted and then not applied",
        );
    }
}

/// Encryption is refused on the flag, not on the presence of a password.
///
/// The check tested `is_encrypted()`, which asks whether a password is set, so
/// `encrypt_data(true)` on its own passed and the data went out in the clear.
#[cfg(feature = "aes")]
#[tokio::test]
async fn test_async_writer_refuses_encryption_without_a_password() {
    let mut writer = AsyncWriter::create(Cursor::new(Vec::new()))
        .await
        .unwrap()
        .options(WriteOptions::new().encrypt_data(true));

    let refused = writer
        .add_bytes(ArchivePath::new("secret.bin").unwrap(), b"SECRET")
        .await;

    assert!(
        refused.is_err(),
        "encryption was requested, unimplemented, and written in the clear",
    );
}

/// Changing the method mid-archive must not corrupt the entries already written.
///
/// The async writer described every folder with whichever method was set last,
/// so an entry written with LZMA2 and followed by a switch to `Copy` was
/// declared as stored and failed its checksum. It kept its own folder model,
/// which is why fixing the blocking writer left this standing.
#[tokio::test]
async fn test_async_changing_the_method_does_not_corrupt_earlier_entries() {
    use zesven::codec::CodecMethod;

    let first = vec![b'A'; 256 * 1024];

    let mut writer = AsyncWriter::create(Cursor::new(Vec::new()))
        .await
        .unwrap()
        .options(WriteOptions::new().level(1).unwrap());
    writer
        .add_bytes(ArchivePath::new("a.bin").unwrap(), &first)
        .await
        .unwrap();

    writer = writer.options(
        WriteOptions::new()
            .level(1)
            .unwrap()
            .method(CodecMethod::Copy),
    );
    writer
        .add_bytes(ArchivePath::new("b.bin").unwrap(), b"SMALL")
        .await
        .unwrap();
    let (_result, cursor) = writer.finish_into_inner().await.unwrap();

    let mut archive = zesven::read::Archive::open(Cursor::new(cursor.into_inner())).unwrap();
    assert_eq!(archive.extract_to_vec("a.bin").unwrap(), first);
    assert_eq!(archive.extract_to_vec("b.bin").unwrap(), b"SMALL");
}

/// A sink that fails once must leave the async writer unusable.
///
/// A transient failure - a full disk that is then freed, a socket that
/// reconnects - left bytes in the sink belonging to no folder. The writer went
/// on accepting entries and `finish` produced an archive that opened and then
/// failed to extract.
#[tokio::test]
async fn test_async_partial_write_poisons_the_writer() {
    use std::io::SeekFrom;
    use std::pin::Pin;
    use std::task::{Context, Poll};

    use tokio::io::{AsyncSeek, AsyncWrite};

    /// Accepts a fixed number of bytes, fails once, then accepts again.
    struct FailsOnce {
        inner: Cursor<Vec<u8>>,
        budget: usize,
        recovered: bool,
    }

    impl AsyncWrite for FailsOnce {
        fn poll_write(
            self: Pin<&mut Self>,
            _cx: &mut Context<'_>,
            buf: &[u8],
        ) -> Poll<std::io::Result<usize>> {
            let me = self.get_mut();
            if me.budget == 0 && !me.recovered {
                me.recovered = true;
                return Poll::Ready(Err(std::io::Error::other("disk full")));
            }
            if me.recovered {
                return std::io::Write::write(&mut me.inner, buf).into();
            }
            let n = buf.len().min(me.budget);
            me.budget -= n;
            std::io::Write::write(&mut me.inner, &buf[..n]).into()
        }
        fn poll_flush(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<std::io::Result<()>> {
            Poll::Ready(Ok(()))
        }
        fn poll_shutdown(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<std::io::Result<()>> {
            Poll::Ready(Ok(()))
        }
    }

    impl AsyncSeek for FailsOnce {
        fn start_seek(self: Pin<&mut Self>, position: SeekFrom) -> std::io::Result<()> {
            std::io::Seek::seek(&mut self.get_mut().inner, position).map(|_| ())
        }
        fn poll_complete(
            self: Pin<&mut Self>,
            _cx: &mut Context<'_>,
        ) -> Poll<std::io::Result<u64>> {
            Poll::Ready(Ok(std::io::Seek::stream_position(
                &mut self.get_mut().inner,
            )
            .unwrap()))
        }
    }

    // Incompressible, so the entry cannot shrink inside the budget.
    let mut data = vec![0u8; 1 << 20];
    let mut state = 0x2545_F491_4F6C_DD1Du64;
    for byte in data.iter_mut() {
        state ^= state << 13;
        state ^= state >> 7;
        state ^= state << 17;
        *byte = state as u8;
    }

    let mut writer = AsyncWriter::create(FailsOnce {
        inner: Cursor::new(Vec::new()),
        budget: 32 + 4096,
        recovered: false,
    })
    .await
    .unwrap()
    .options(WriteOptions::new().level(1).unwrap());

    assert!(
        writer
            .add_bytes(ArchivePath::new("big.bin").unwrap(), &data)
            .await
            .is_err(),
        "the sink failed, so the add must fail",
    );
    assert!(
        writer
            .add_bytes(ArchivePath::new("after.bin").unwrap(), b"AFTER")
            .await
            .is_err(),
        "the writer kept accepting entries after a partial write",
    );
    assert!(
        writer.finish_into_inner().await.is_err(),
        "an archive was produced from a failed write",
    );
}

/// Turning deterministic mode on mid-archive enforces the order from there on.
#[tokio::test]
async fn test_async_deterministic_mode_enabled_midway_enforces_order() {
    let mut writer = AsyncWriter::create(Cursor::new(Vec::new()))
        .await
        .unwrap()
        .options(WriteOptions::new().deterministic(false));
    writer
        .add_bytes(ArchivePath::new("z.txt").unwrap(), b"CONTENT-Z")
        .await
        .unwrap();

    writer = writer.options(WriteOptions::new().deterministic(true));
    assert!(
        writer
            .add_bytes(ArchivePath::new("a.txt").unwrap(), b"CONTENT-A")
            .await
            .is_err(),
        "'a.txt' sorts before the entry already written, and the setting is on",
    );
}

/// Writing to a path must produce a file this crate can open again.
///
/// Every other case here writes into a `Cursor`, which holds no buffer of its
/// own. `create_path` wraps the file in a buffered async sink, and dropping one
/// of those discards whatever it still holds rather than flushing it - so the
/// archive was left with 32 zero bytes where its signature belongs.
#[tokio::test]
async fn test_async_writer_to_a_path_produces_a_readable_archive() {
    let dir = tempfile::TempDir::new().unwrap();
    let path = dir.path().join("archive.7z");

    let payload = b"the signature is written last, after a seek to the start\n".repeat(32);
    let mut writer = AsyncWriter::create_path(&path)
        .await
        .unwrap()
        .options(WriteOptions::new());
    writer
        .add_bytes(ArchivePath::new("data.txt").unwrap(), &payload)
        .await
        .unwrap();
    let result = writer.finish().await.unwrap();
    assert_eq!(result.entries_written, 1);

    let mut archive = zesven::read::Archive::open_path(&path).unwrap();
    assert_eq!(archive.extract_to_vec("data.txt").unwrap(), payload);
}

/// A cancelled write must leave the writer unusable, like a failed one.
///
/// Cancellation is not an error path: a future dropped by `timeout` or a losing
/// `select!` branch simply never resumes, so nothing after the await runs. With
/// the state set from the error path only, a write cancelled with part of the
/// entry already in the sink left the writer accepting entries, and `finish`
/// produced an archive whose next entry failed its checksum.
#[tokio::test]
async fn test_async_cancelled_write_poisons_the_writer() {
    use std::io::SeekFrom;
    use std::pin::Pin;
    use std::sync::Arc;
    use std::sync::atomic::{AtomicBool, Ordering};
    use std::task::{Context, Poll};

    use tokio::io::{AsyncSeek, AsyncWrite};
    use tokio::sync::oneshot;

    /// Takes one short write, announces it, and then stalls until told to heal.
    ///
    /// The announcement is what cancels the write, so the test does not depend
    /// on any duration: no waker is registered for the stall, so the only way
    /// out of that future is the losing `select!` branch being dropped.
    struct StallsUntilHealed {
        inner: Cursor<Vec<u8>>,
        announce: Option<oneshot::Sender<()>>,
        healthy: Arc<AtomicBool>,
    }

    impl AsyncWrite for StallsUntilHealed {
        fn poll_write(
            self: Pin<&mut Self>,
            _cx: &mut Context<'_>,
            buf: &[u8],
        ) -> Poll<std::io::Result<usize>> {
            let me = self.get_mut();
            if me.healthy.load(Ordering::SeqCst) {
                // Recovered, as a reconnected socket or a freed disk would be.
                return std::io::Write::write(&mut me.inner, buf).into();
            }
            if let Some(announce) = me.announce.take() {
                // Part of the entry lands in the sink...
                let n = buf.len().min(64);
                let written = std::io::Write::write(&mut me.inner, &buf[..n]);
                let _ = announce.send(());
                return written.into();
            }
            // ...and the rest of it never does.
            Poll::Pending
        }
        fn poll_flush(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<std::io::Result<()>> {
            Poll::Ready(Ok(()))
        }
        fn poll_shutdown(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<std::io::Result<()>> {
            Poll::Ready(Ok(()))
        }
    }

    impl AsyncSeek for StallsUntilHealed {
        fn start_seek(self: Pin<&mut Self>, position: SeekFrom) -> std::io::Result<()> {
            std::io::Seek::seek(&mut self.get_mut().inner, position).map(|_| ())
        }
        fn poll_complete(
            self: Pin<&mut Self>,
            _cx: &mut Context<'_>,
        ) -> Poll<std::io::Result<u64>> {
            Poll::Ready(Ok(std::io::Seek::stream_position(
                &mut self.get_mut().inner,
            )
            .unwrap()))
        }
    }

    // Incompressible, so the entry is longer than the sink's first short write.
    let mut data = vec![0u8; 256 * 1024];
    let mut state = 0x2545_F491_4F6C_DD1Du64;
    for byte in data.iter_mut() {
        state ^= state << 13;
        state ^= state >> 7;
        state ^= state << 17;
        *byte = state as u8;
    }

    let (announce, announced) = oneshot::channel();
    let healthy = Arc::new(AtomicBool::new(false));
    let mut writer = AsyncWriter::create(StallsUntilHealed {
        inner: Cursor::new(Vec::new()),
        announce: Some(announce),
        healthy: healthy.clone(),
    })
    .await
    .unwrap()
    .options(WriteOptions::new().level(1).unwrap());

    tokio::select! {
        _ = writer.add_bytes(ArchivePath::new("big.bin").unwrap(), &data) => {
            panic!("the sink stalled, so the write cannot have completed")
        }
        _ = announced => {}
    }
    healthy.store(true, Ordering::SeqCst);

    assert!(
        writer
            .add_bytes(ArchivePath::new("after.bin").unwrap(), b"AFTER")
            .await
            .is_err(),
        "the writer kept accepting entries after a cancelled write",
    );
    assert!(
        writer.finish_into_inner().await.is_err(),
        "an archive was produced after a cancelled write",
    );
}

/// Finishing must not hold the runtime while the header is built.
///
/// Header encoding is a synchronous loop over every entry: a hundred thousand
/// of them take tens of milliseconds, and run inline they block a
/// current-thread runtime for all of it - no other task is polled, no timer
/// fires, no connection is accepted. It belongs on the blocking pool, like
/// compression.
#[tokio::test]
async fn test_async_finish_leaves_the_runtime_free() {
    use std::sync::Arc;
    use std::sync::atomic::{AtomicU64, Ordering};

    use zesven::write::EntryMeta;

    let mut writer = AsyncWriter::create(Cursor::new(Vec::new()))
        .await
        .unwrap()
        .options(WriteOptions::new());

    // Directories, so the archive costs nothing to compress and the header is
    // the only expensive part of finishing.
    for i in 0..50_000 {
        writer
            .add_directory(
                ArchivePath::new(&format!("d{i:06}")).unwrap(),
                EntryMeta::directory(),
            )
            .await
            .unwrap();
    }

    let polls = Arc::new(AtomicU64::new(0));
    let counter = polls.clone();
    let ticker = tokio::spawn(async move {
        loop {
            counter.fetch_add(1, Ordering::Relaxed);
            tokio::task::yield_now().await;
        }
    });

    let before = polls.load(Ordering::Relaxed);
    let result = writer.finish().await.unwrap();
    let during = polls.load(Ordering::Relaxed) - before;
    ticker.abort();

    assert_eq!(result.directories_written, 50_000);
    // Encoding inline yields only at the handful of awaits around it. On the
    // blocking pool the ticker runs throughout, which is thousands of turns.
    assert!(
        during > 100,
        "the runtime was blocked while the header was built: {during} turns",
    );
}

/// The write result must count what the blocking writer counts.
///
/// Anti-items are removals rather than files, and the async writer counted them
/// as files: the same archive was reported as holding one more entry than the
/// blocking writer reported for it. The archives were correct either way -
/// both this crate's reader and 7-Zip see the anti-items - so only the figures
/// handed back to the caller were wrong.
#[tokio::test]
async fn test_async_write_result_matches_the_blocking_writer() {
    use zesven::write::{EntryMeta, Writer};

    let mut writer = AsyncWriter::create(Cursor::new(Vec::new()))
        .await
        .unwrap()
        .options(WriteOptions::new());
    writer
        .add_bytes(ArchivePath::new("kept.txt").unwrap(), b"KEPT")
        .await
        .unwrap();
    writer
        .add_directory(ArchivePath::new("dir").unwrap(), EntryMeta::directory())
        .await
        .unwrap();
    writer
        .add_stream(
            ArchivePath::new("gone.txt").unwrap(),
            &mut &b""[..],
            EntryMeta::anti_item(),
        )
        .await
        .unwrap();
    writer
        .add_directory(
            ArchivePath::new("gone-dir").unwrap(),
            EntryMeta::anti_directory(),
        )
        .await
        .unwrap();
    let (asynchronous, _sink) = writer.finish_into_inner().await.unwrap();

    let mut writer = Writer::create(Cursor::new(Vec::new()))
        .unwrap()
        .options(WriteOptions::new());
    writer
        .add_bytes(ArchivePath::new("kept.txt").unwrap(), b"KEPT")
        .unwrap();
    writer
        .add_directory(ArchivePath::new("dir").unwrap(), EntryMeta::directory())
        .unwrap();
    writer
        .add_anti_item(ArchivePath::new("gone.txt").unwrap())
        .unwrap();
    writer
        .add_anti_directory(ArchivePath::new("gone-dir").unwrap())
        .unwrap();
    let (blocking, _sink) = writer.finish_into_inner().unwrap();

    assert_eq!(
        (
            asynchronous.entries_written,
            asynchronous.directories_written,
            asynchronous.total_size,
            asynchronous.volume_count,
        ),
        (
            blocking.entries_written,
            blocking.directories_written,
            blocking.total_size,
            blocking.volume_count,
        ),
        "the two writers disagree about what they wrote",
    );
    // Sizes rather than equality: the two archives are not byte-identical, but
    // each must report the length of the one it actually produced.
    assert_eq!(
        asynchronous.volume_sizes.len(),
        blocking.volume_sizes.len(),
        "one writer reported volume sizes and the other did not",
    );
}