zesven 1.1.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
//! Integration tests for async API functionality.
//!
//! These tests verify the async archive reading and writing functionality
//! with Tokio runtime.

#![cfg(feature = "async")]

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 Encrypted Header Support - Missing Implementation
// =============================================================================
//
// A test for async archive opening with wrong password is not included because
// the async API does not yet support header-encrypted archives.
//
// Current limitation: `AsyncArchive::open_with_password` uses `read_archive_header`
// without password support, while header-encrypted archives require
// `read_archive_header_with_offset_and_password`.
//
// When the async API properly supports header decryption, add a test that:
// 1. Creates an encrypted archive with header encryption
// 2. Attempts to open with wrong password - should fail
// 3. Opens with correct password - should succeed
// 4. Verifies entry can be extracted
//
// See also: multivolume.rs for a related encryption limitation in multi-volume API.

/// 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)
// ============================================================================

// Note: AsyncCopyDecoder and build_async_decoder tests are in src/async_codec.rs
// 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);
        }
    }
}