cdx-core 0.7.1

Core library for reading, writing, and validating Codex Document Format (.cdx) files
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
//! Archive reader for Codex documents.

use std::fs::File;
use std::io::{BufReader, Cursor, Read, Seek};
use std::path::Path;

use zip::ZipArchive;

use crate::{Error, HashAlgorithm, Hasher, Manifest, Result};

use super::{validate_path, CONTENT_PATH, DUBLIN_CORE_PATH, MANIFEST_PATH, PHANTOMS_PATH};

/// Reader for Codex document archives.
///
/// `CdxReader` opens and validates `.cdx` files, providing access to their contents.
/// The reader validates the archive structure on creation and provides lazy access
/// to individual files.
///
/// # Example
///
/// ```rust,ignore
/// use cdx_core::archive::CdxReader;
///
/// let mut reader = CdxReader::open("document.cdx")?;
///
/// // Access the manifest
/// let manifest = reader.manifest();
/// println!("Document state: {:?}", manifest.state);
///
/// // Read a file from the archive
/// let content = reader.read_file("content/document.json")?;
/// ```
pub struct CdxReader<R: Read + Seek> {
    archive: ZipArchive<R>,
    manifest: Manifest,
}

impl CdxReader<BufReader<File>> {
    /// Open a Codex document from a file path.
    ///
    /// # Errors
    ///
    /// Returns an error if:
    /// - The file cannot be opened
    /// - The file is not a valid ZIP archive
    /// - Required files are missing
    /// - The manifest is invalid
    pub fn open<P: AsRef<Path>>(path: P) -> Result<Self> {
        let file = File::open(path.as_ref()).map_err(|e| {
            if e.kind() == std::io::ErrorKind::NotFound {
                Error::FileNotFound {
                    path: path.as_ref().to_path_buf(),
                }
            } else {
                Error::Io(e)
            }
        })?;
        let reader = BufReader::new(file);
        Self::new(reader)
    }
}

impl CdxReader<Cursor<Vec<u8>>> {
    /// Open a Codex document from bytes in memory.
    ///
    /// # Errors
    ///
    /// Returns an error if:
    /// - The data is not a valid ZIP archive
    /// - Required files are missing
    /// - The manifest is invalid
    pub fn from_bytes(data: Vec<u8>) -> Result<Self> {
        let cursor = Cursor::new(data);
        Self::new(cursor)
    }
}

impl<R: Read + Seek> CdxReader<R> {
    /// Create a new reader from any `Read + Seek` source.
    ///
    /// This enables reading from files, memory buffers, network streams, etc.
    ///
    /// # Errors
    ///
    /// Returns an error if:
    /// - The source is not a valid ZIP archive
    /// - Required files are missing
    /// - The manifest is invalid
    pub fn new(reader: R) -> Result<Self> {
        let mut archive = ZipArchive::new(reader)?;

        // Validate structure
        Self::validate_structure(&archive)?;

        // Read and parse manifest
        let manifest = Self::read_manifest(&mut archive)?;

        // Validate manifest
        manifest.validate()?;

        Ok(Self { archive, manifest })
    }

    /// Validate the archive structure.
    fn validate_structure(archive: &ZipArchive<R>) -> Result<()> {
        // Check for required files
        let required_files = [MANIFEST_PATH, CONTENT_PATH, DUBLIN_CORE_PATH];

        for path in required_files {
            if archive.index_for_name(path).is_none() {
                return Err(Error::MissingFile {
                    path: path.to_string(),
                });
            }
        }

        // Manifest must be the first file in the archive (per spec)
        if let Some(first_file) = archive.file_names().next() {
            if first_file != MANIFEST_PATH {
                return Err(Error::InvalidArchiveStructure {
                    reason: format!(
                        "manifest.json must be the first file in the archive (found '{first_file}')"
                    ),
                });
            }
        }

        Ok(())
    }

    /// Strip a UTF-8 BOM (byte order mark) prefix if present.
    fn strip_utf8_bom(data: &[u8]) -> &[u8] {
        data.strip_prefix(&[0xEF, 0xBB, 0xBF]).unwrap_or(data)
    }

    /// Read a file and parse it as JSON, stripping any UTF-8 BOM prefix.
    fn read_json_file<T: serde::de::DeserializeOwned>(
        archive: &mut ZipArchive<R>,
        path: &str,
    ) -> Result<T> {
        let data = Self::read_file_internal(archive, path)?;
        let json_data = Self::strip_utf8_bom(&data);
        Ok(serde_json::from_slice(json_data)?)
    }

    /// Read and parse the manifest.
    fn read_manifest(archive: &mut ZipArchive<R>) -> Result<Manifest> {
        Self::read_json_file(archive, MANIFEST_PATH)
    }

    /// Maximum allowed file size for decompression (256 MiB).
    ///
    /// This limit protects against decompression bombs (zip bombs) where a small
    /// compressed file expands to a very large size.
    const MAX_FILE_SIZE: u64 = 256 * 1024 * 1024;

    /// Internal file reading without path validation (for known-safe paths).
    fn read_file_internal(archive: &mut ZipArchive<R>, path: &str) -> Result<Vec<u8>> {
        let file = archive.by_name(path).map_err(|e| match e {
            zip::result::ZipError::FileNotFound => Error::MissingFile {
                path: path.to_string(),
            },
            other => Error::InvalidArchive(other),
        })?;

        // Check declared size before allocating (catches honest oversized files)
        if file.size() > Self::MAX_FILE_SIZE {
            return Err(Error::FileTooLarge {
                path: path.to_string(),
                size: file.size(),
                limit: Self::MAX_FILE_SIZE,
            });
        }

        // Use try_from with fallback to 0 for platforms with smaller usize
        let capacity = usize::try_from(file.size()).unwrap_or(0);
        let mut data = Vec::with_capacity(capacity);
        // Bounded read to catch spoofed/mismatched declared sizes
        let bytes_read = file.take(Self::MAX_FILE_SIZE + 1).read_to_end(&mut data)?;
        if bytes_read as u64 > Self::MAX_FILE_SIZE {
            return Err(Error::FileTooLarge {
                path: path.to_string(),
                size: bytes_read as u64,
                limit: Self::MAX_FILE_SIZE,
            });
        }
        Ok(data)
    }

    /// Get a reference to the document manifest.
    #[must_use]
    pub fn manifest(&self) -> &Manifest {
        &self.manifest
    }

    /// Read a file from the archive.
    ///
    /// # Errors
    ///
    /// Returns an error if:
    /// - The path contains traversal patterns (security check)
    /// - The file does not exist in the archive
    /// - Reading the file fails
    pub fn read_file(&mut self, path: &str) -> Result<Vec<u8>> {
        validate_path(path)?;
        Self::read_file_internal(&mut self.archive, path)
    }

    /// Read a file and verify its hash against the expected hash.
    ///
    /// # Errors
    ///
    /// Returns an error if:
    /// - The path contains traversal patterns
    /// - The file does not exist
    /// - The hash does not match the expected value
    pub fn read_file_verified(
        &mut self,
        path: &str,
        expected_hash: &crate::DocumentId,
    ) -> Result<Vec<u8>> {
        let data = self.read_file(path)?;

        // Skip verification for pending hashes
        if expected_hash.is_pending() {
            return Ok(data);
        }

        let actual_hash = Hasher::hash(expected_hash.algorithm(), &data);

        if actual_hash != *expected_hash {
            return Err(Error::HashMismatch {
                path: path.to_string(),
                expected: expected_hash.to_string(),
                actual: actual_hash.to_string(),
            });
        }

        Ok(data)
    }

    /// Read the content file.
    ///
    /// This is a convenience method for reading `content/document.json`.
    ///
    /// # Errors
    ///
    /// Returns an error if reading the content file fails.
    pub fn read_content(&mut self) -> Result<Vec<u8>> {
        self.read_file_verified(CONTENT_PATH, &self.manifest.content.hash.clone())
    }

    /// Read the Dublin Core metadata file.
    ///
    /// This is a convenience method for reading `metadata/dublin-core.json`.
    ///
    /// # Errors
    ///
    /// Returns an error if reading the metadata file fails.
    pub fn read_dublin_core(&mut self) -> Result<Vec<u8>> {
        self.read_file(&self.manifest.metadata.dublin_core.clone())
    }

    /// Check if a file exists in the archive.
    ///
    /// # Errors
    ///
    /// Returns an error if the path contains traversal patterns.
    pub fn file_exists(&self, path: &str) -> Result<bool> {
        validate_path(path)?;
        Ok(self.archive.index_for_name(path).is_some())
    }

    /// Get the list of all file paths in the archive.
    #[must_use]
    pub fn file_names(&self) -> Vec<String> {
        self.archive.file_names().map(String::from).collect()
    }

    /// Get the number of files in the archive.
    #[must_use]
    pub fn file_count(&self) -> usize {
        self.archive.len()
    }

    /// Get the hash algorithm used by this document.
    #[must_use]
    pub fn hash_algorithm(&self) -> HashAlgorithm {
        self.manifest.hash_algorithm
    }

    /// Read phantom clusters from the archive.
    ///
    /// Returns `None` if the phantom clusters file doesn't exist.
    ///
    /// # Errors
    ///
    /// Returns an error if the file exists but cannot be parsed.
    pub fn read_phantoms(&mut self) -> Result<Option<crate::extensions::PhantomClusters>> {
        if self.archive.index_for_name(PHANTOMS_PATH).is_none() {
            return Ok(None);
        }

        let phantoms: crate::extensions::PhantomClusters =
            Self::read_json_file(&mut self.archive, PHANTOMS_PATH)?;
        Ok(Some(phantoms))
    }

    /// Verify all file hashes in the manifest.
    ///
    /// This checks:
    /// - Content file hash
    /// - Presentation file hashes (if any)
    ///
    /// # Errors
    ///
    /// Returns an error if any hash verification fails.
    pub fn verify_hashes(&mut self) -> Result<()> {
        // Verify content hash
        let content_data = self.read_file(CONTENT_PATH)?;
        if !self.manifest.content.hash.is_pending() {
            let actual = Hasher::hash(self.manifest.content.hash.algorithm(), &content_data);
            if actual != self.manifest.content.hash {
                return Err(Error::HashMismatch {
                    path: CONTENT_PATH.to_string(),
                    expected: self.manifest.content.hash.to_string(),
                    actual: actual.to_string(),
                });
            }
        }

        // Verify presentation hashes
        for pres in &self.manifest.presentation.clone() {
            if !pres.hash.is_pending() {
                let data = self.read_file(&pres.path)?;
                let actual = Hasher::hash(pres.hash.algorithm(), &data);
                if actual != pres.hash {
                    return Err(Error::HashMismatch {
                        path: pres.path.clone(),
                        expected: pres.hash.to_string(),
                        actual: actual.to_string(),
                    });
                }
            }
        }

        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::archive::CdxWriter;
    use crate::{ContentRef, DocumentId, Metadata};
    use std::io::{Cursor, Write};

    fn create_test_archive() -> Vec<u8> {
        let buffer = Cursor::new(Vec::new());
        let mut writer = CdxWriter::new(buffer).unwrap();

        // Create a minimal manifest
        let content = ContentRef {
            path: CONTENT_PATH.to_string(),
            hash: DocumentId::pending(),
            compression: None,
            merkle_root: None,
            block_count: None,
        };
        let metadata = Metadata {
            dublin_core: DUBLIN_CORE_PATH.to_string(),
            custom: None,
        };
        let manifest = Manifest::new(content, metadata);

        writer.write_manifest(&manifest).unwrap();
        writer
            .write_file(
                CONTENT_PATH,
                br#"{"version":"0.1","blocks":[]}"#,
                super::super::writer::CompressionMethod::Deflate,
            )
            .unwrap();
        writer
            .write_file(
                DUBLIN_CORE_PATH,
                br#"{"title":"Test"}"#,
                super::super::writer::CompressionMethod::Deflate,
            )
            .unwrap();

        writer.finish().unwrap().into_inner()
    }

    #[test]
    fn test_reader_from_bytes() {
        let data = create_test_archive();
        let reader = CdxReader::from_bytes(data).unwrap();
        assert_eq!(reader.manifest().codex, "0.1");
    }

    #[test]
    fn test_reader_file_list() {
        let data = create_test_archive();
        let reader = CdxReader::from_bytes(data).unwrap();
        let files = reader.file_names();
        assert!(files.contains(&MANIFEST_PATH.to_string()));
        assert!(files.contains(&CONTENT_PATH.to_string()));
        assert!(files.contains(&DUBLIN_CORE_PATH.to_string()));
    }

    #[test]
    fn test_reader_read_file() {
        let data = create_test_archive();
        let mut reader = CdxReader::from_bytes(data).unwrap();
        let content = reader.read_file(CONTENT_PATH).unwrap();
        assert!(!content.is_empty());
    }

    #[test]
    fn test_reader_file_exists() {
        let data = create_test_archive();
        let reader = CdxReader::from_bytes(data).unwrap();
        assert!(reader.file_exists(MANIFEST_PATH).unwrap());
        assert!(reader.file_exists(CONTENT_PATH).unwrap());
        assert!(!reader.file_exists("nonexistent.json").unwrap());
    }

    #[test]
    fn test_reader_path_traversal_rejected() {
        let data = create_test_archive();
        let mut reader = CdxReader::from_bytes(data).unwrap();
        assert!(reader.read_file("../secret").is_err());
        assert!(reader.file_exists("../secret").is_err());
    }

    #[test]
    fn test_reader_missing_file_error() {
        let data = create_test_archive();
        let mut reader = CdxReader::from_bytes(data).unwrap();
        let result = reader.read_file("nonexistent.json");
        assert!(matches!(result, Err(Error::MissingFile { .. })));
    }

    #[test]
    fn test_open_corrupted_zip() {
        // Random bytes that aren't a valid ZIP
        let corrupted = vec![0x50, 0x4B, 0x03, 0x04, 0xFF, 0xFF, 0xFF, 0xFF];
        let result = CdxReader::from_bytes(corrupted);
        assert!(result.is_err());
    }

    #[test]
    fn test_open_not_a_zip() {
        // Plain text, not a ZIP file
        let not_zip = b"This is not a ZIP file at all".to_vec();
        let result = CdxReader::from_bytes(not_zip);
        assert!(result.is_err());
    }

    #[test]
    fn test_open_empty_zip() {
        // Create an empty ZIP (no files)
        let buffer = Cursor::new(Vec::new());
        let writer = zip::ZipWriter::new(buffer);
        let empty_zip = writer.finish().unwrap().into_inner();

        let result = CdxReader::from_bytes(empty_zip);
        assert!(matches!(result, Err(Error::MissingFile { .. })));
    }

    #[test]
    fn test_open_missing_manifest() {
        // Create a ZIP without manifest.json
        let buffer = Cursor::new(Vec::new());
        let mut writer = zip::ZipWriter::new(buffer);
        writer
            .start_file::<&str, ()>(CONTENT_PATH, Default::default())
            .unwrap();
        writer.write_all(b"{}").unwrap();
        writer
            .start_file::<&str, ()>(DUBLIN_CORE_PATH, Default::default())
            .unwrap();
        writer.write_all(b"{}").unwrap();
        let data = writer.finish().unwrap().into_inner();

        let result = CdxReader::from_bytes(data);
        assert!(matches!(result, Err(Error::MissingFile { path }) if path == MANIFEST_PATH));
    }

    #[test]
    fn test_open_missing_content() {
        // Create a ZIP with manifest but no content file
        let buffer = Cursor::new(Vec::new());
        let mut writer = zip::ZipWriter::new(buffer);

        // Add manifest
        writer
            .start_file::<&str, ()>(MANIFEST_PATH, Default::default())
            .unwrap();
        writer.write_all(br#"{"codex":"0.1"}"#).unwrap();

        // Add Dublin Core but no content
        writer
            .start_file::<&str, ()>(DUBLIN_CORE_PATH, Default::default())
            .unwrap();
        writer.write_all(b"{}").unwrap();

        let data = writer.finish().unwrap().into_inner();

        let result = CdxReader::from_bytes(data);
        assert!(matches!(result, Err(Error::MissingFile { path }) if path == CONTENT_PATH));
    }

    #[test]
    fn test_open_invalid_manifest_json() {
        // Create a ZIP with invalid JSON in manifest
        let buffer = Cursor::new(Vec::new());
        let mut writer = zip::ZipWriter::new(buffer);

        writer
            .start_file::<&str, ()>(MANIFEST_PATH, Default::default())
            .unwrap();
        writer.write_all(b"{ invalid json }").unwrap();

        writer
            .start_file::<&str, ()>(CONTENT_PATH, Default::default())
            .unwrap();
        writer.write_all(b"{}").unwrap();

        writer
            .start_file::<&str, ()>(DUBLIN_CORE_PATH, Default::default())
            .unwrap();
        writer.write_all(b"{}").unwrap();

        let data = writer.finish().unwrap().into_inner();

        let result = CdxReader::from_bytes(data);
        assert!(result.is_err());
    }

    #[test]
    fn test_read_file_hash_mismatch() {
        let buffer = Cursor::new(Vec::new());
        let mut writer = CdxWriter::new(buffer).unwrap();

        // Create manifest with a specific hash
        let expected_hash: DocumentId =
            "sha256:0000000000000000000000000000000000000000000000000000000000000000"
                .parse()
                .unwrap();
        let content = ContentRef {
            path: CONTENT_PATH.to_string(),
            hash: expected_hash.clone(),
            compression: None,
            merkle_root: None,
            block_count: None,
        };
        let metadata = Metadata {
            dublin_core: DUBLIN_CORE_PATH.to_string(),
            custom: None,
        };
        let manifest = Manifest::new(content, metadata);

        writer.write_manifest(&manifest).unwrap();
        // Write content that doesn't match the hash
        writer
            .write_file(
                CONTENT_PATH,
                br#"{"version":"0.1","blocks":[]}"#,
                super::super::writer::CompressionMethod::Deflate,
            )
            .unwrap();
        writer
            .write_file(
                DUBLIN_CORE_PATH,
                br#"{"title":"Test"}"#,
                super::super::writer::CompressionMethod::Deflate,
            )
            .unwrap();

        let data = writer.finish().unwrap().into_inner();
        let mut reader = CdxReader::from_bytes(data).unwrap();

        let result = reader.read_file_verified(CONTENT_PATH, &expected_hash);
        assert!(matches!(result, Err(Error::HashMismatch { .. })));
    }

    #[test]
    fn test_verify_hashes_with_mismatch() {
        let buffer = Cursor::new(Vec::new());
        let mut writer = CdxWriter::new(buffer).unwrap();

        // Create manifest with a wrong hash
        let wrong_hash: DocumentId =
            "sha256:ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"
                .parse()
                .unwrap();
        let content = ContentRef {
            path: CONTENT_PATH.to_string(),
            hash: wrong_hash,
            compression: None,
            merkle_root: None,
            block_count: None,
        };
        let metadata = Metadata {
            dublin_core: DUBLIN_CORE_PATH.to_string(),
            custom: None,
        };
        let manifest = Manifest::new(content, metadata);

        writer.write_manifest(&manifest).unwrap();
        writer
            .write_file(
                CONTENT_PATH,
                br#"{"version":"0.1","blocks":[]}"#,
                super::super::writer::CompressionMethod::Deflate,
            )
            .unwrap();
        writer
            .write_file(
                DUBLIN_CORE_PATH,
                br#"{"title":"Test"}"#,
                super::super::writer::CompressionMethod::Deflate,
            )
            .unwrap();

        let data = writer.finish().unwrap().into_inner();
        let mut reader = CdxReader::from_bytes(data).unwrap();

        let result = reader.verify_hashes();
        assert!(matches!(result, Err(Error::HashMismatch { .. })));
    }

    #[test]
    fn test_read_file_verified_with_pending_hash() {
        let data = create_test_archive();
        let mut reader = CdxReader::from_bytes(data).unwrap();

        // Pending hashes should skip verification
        let pending = DocumentId::pending();
        let result = reader.read_file_verified(CONTENT_PATH, &pending);
        assert!(result.is_ok());
    }

    #[test]
    fn test_unicode_filenames() {
        let buffer = Cursor::new(Vec::new());
        let mut writer = CdxWriter::new(buffer).unwrap();

        let content = ContentRef {
            path: CONTENT_PATH.to_string(),
            hash: DocumentId::pending(),
            compression: None,
            merkle_root: None,
            block_count: None,
        };
        let metadata = Metadata {
            dublin_core: DUBLIN_CORE_PATH.to_string(),
            custom: None,
        };
        let manifest = Manifest::new(content, metadata);

        writer.write_manifest(&manifest).unwrap();
        writer
            .write_file(
                CONTENT_PATH,
                br#"{"version":"0.1","blocks":[]}"#,
                super::super::writer::CompressionMethod::Deflate,
            )
            .unwrap();
        writer
            .write_file(
                DUBLIN_CORE_PATH,
                br#"{"title":"Test"}"#,
                super::super::writer::CompressionMethod::Deflate,
            )
            .unwrap();

        // Add a file with Unicode characters
        writer
            .write_file(
                "assets/文档.txt",
                b"Unicode content",
                super::super::writer::CompressionMethod::Deflate,
            )
            .unwrap();
        writer
            .write_file(
                "assets/émoji_🎉.txt",
                b"Emoji content",
                super::super::writer::CompressionMethod::Deflate,
            )
            .unwrap();

        let data = writer.finish().unwrap().into_inner();
        let mut reader = CdxReader::from_bytes(data).unwrap();

        // Verify we can read the Unicode files
        let files = reader.file_names();
        assert!(files.contains(&"assets/文档.txt".to_string()));
        assert!(files.contains(&"assets/émoji_🎉.txt".to_string()));

        let content = reader.read_file("assets/文档.txt").unwrap();
        assert_eq!(content, b"Unicode content");

        let emoji_content = reader.read_file("assets/émoji_🎉.txt").unwrap();
        assert_eq!(emoji_content, b"Emoji content");
    }

    #[test]
    fn test_file_count() {
        let data = create_test_archive();
        let reader = CdxReader::from_bytes(data).unwrap();
        // manifest, content, dublin_core = 3 files
        assert_eq!(reader.file_count(), 3);
    }

    #[test]
    fn test_hash_algorithm() {
        let data = create_test_archive();
        let reader = CdxReader::from_bytes(data).unwrap();
        assert_eq!(reader.hash_algorithm(), HashAlgorithm::Sha256);
    }

    #[test]
    fn test_read_phantoms_none() {
        let data = create_test_archive();
        let mut reader = CdxReader::from_bytes(data).unwrap();
        // No phantoms file in the test archive
        let result = reader.read_phantoms().unwrap();
        assert!(result.is_none());
    }

    #[test]
    fn test_manifest_must_be_first_file() {
        // Create a ZIP where manifest is NOT the first file
        let buffer = Cursor::new(Vec::new());
        let mut writer = zip::ZipWriter::new(buffer);

        // Write content BEFORE manifest
        writer
            .start_file::<&str, ()>(CONTENT_PATH, Default::default())
            .unwrap();
        writer
            .write_all(br#"{"version":"0.1","blocks":[]}"#)
            .unwrap();

        // Now write manifest (not first)
        let manifest_json = r#"{
            "codex": "0.1",
            "id": "pending",
            "state": "draft",
            "created": "2024-01-01T00:00:00Z",
            "modified": "2024-01-01T00:00:00Z",
            "content": { "path": "content/document.json", "hash": "pending" },
            "metadata": { "dublinCore": "metadata/dublin-core.json" }
        }"#;
        writer
            .start_file::<&str, ()>(MANIFEST_PATH, Default::default())
            .unwrap();
        writer.write_all(manifest_json.as_bytes()).unwrap();

        writer
            .start_file::<&str, ()>(DUBLIN_CORE_PATH, Default::default())
            .unwrap();
        writer.write_all(br#"{"title":"Test"}"#).unwrap();

        let data = writer.finish().unwrap().into_inner();
        let result = CdxReader::from_bytes(data);

        let err = result.err().expect("should be an error");
        assert!(matches!(err, Error::InvalidArchiveStructure { .. }));
    }

    #[test]
    fn test_manifest_first_file_passes() {
        // Normal archive created by CdxWriter should have manifest first
        let data = create_test_archive();
        let result = CdxReader::from_bytes(data);
        assert!(result.is_ok());
    }

    #[test]
    fn test_utf8_bom_stripped_from_manifest() {
        // Create a ZIP with BOM-prefixed manifest JSON
        let buffer = Cursor::new(Vec::new());
        let mut writer = zip::ZipWriter::new(buffer);

        // Manifest with UTF-8 BOM prefix
        let manifest_json = r#"{
            "codex": "0.1",
            "id": "pending",
            "state": "draft",
            "created": "2024-01-01T00:00:00Z",
            "modified": "2024-01-01T00:00:00Z",
            "hashAlgorithm": "sha256",
            "content": { "path": "content/document.json", "hash": "pending" },
            "metadata": { "dublinCore": "metadata/dublin-core.json" }
        }"#;
        let mut bom_manifest = vec![0xEF, 0xBB, 0xBF];
        bom_manifest.extend_from_slice(manifest_json.as_bytes());

        writer
            .start_file::<&str, ()>(MANIFEST_PATH, Default::default())
            .unwrap();
        writer.write_all(&bom_manifest).unwrap();

        writer
            .start_file::<&str, ()>(CONTENT_PATH, Default::default())
            .unwrap();
        writer
            .write_all(br#"{"version":"0.1","blocks":[]}"#)
            .unwrap();

        writer
            .start_file::<&str, ()>(DUBLIN_CORE_PATH, Default::default())
            .unwrap();
        writer.write_all(br#"{"title":"Test"}"#).unwrap();

        let data = writer.finish().unwrap().into_inner();
        let reader = CdxReader::from_bytes(data);
        assert!(
            reader.is_ok(),
            "BOM-prefixed manifest should parse correctly"
        );
        assert_eq!(reader.unwrap().manifest().codex, "0.1");
    }

    #[test]
    fn test_utf8_bom_not_required() {
        // Regular archive without BOM should still work fine
        let data = create_test_archive();
        let reader = CdxReader::from_bytes(data);
        assert!(reader.is_ok());
    }
}