oximedia-archive 0.1.2

Media archive verification and long-term preservation system
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
//! Sidecar file generation with human-readable checksum manifests.
//!
//! Generates `.sha256`, `.md5`, `.blake3`, and combined `.checksums` sidecar
//! files alongside archived media, following conventions used by media archives,
//! BagIt, and digital preservation workflows.

use crate::{ArchiveError, ArchiveResult};
use serde::{Deserialize, Serialize};
use std::fmt::Write as FmtWrite;
use std::path::{Path, PathBuf};

// ---------------------------------------------------------------------------
// SidecarFormat
// ---------------------------------------------------------------------------

/// Output format for a sidecar checksum file.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SidecarFormat {
    /// Plain `sha256sum`-compatible format: `<hex>  <filename>`.
    Sha256Sum,
    /// Plain `md5sum`-compatible format.
    Md5Sum,
    /// BLAKE3 bsum-compatible format.
    Blake3Sum,
    /// Combined multi-algorithm JSON manifest.
    JsonManifest,
    /// Combined multi-algorithm human-readable text manifest.
    TextManifest,
}

impl SidecarFormat {
    /// File extension (without leading dot) for this sidecar format.
    #[must_use]
    pub fn file_extension(&self) -> &str {
        match self {
            Self::Sha256Sum => "sha256",
            Self::Md5Sum => "md5",
            Self::Blake3Sum => "blake3",
            Self::JsonManifest => "checksums.json",
            Self::TextManifest => "checksums.txt",
        }
    }
}

// ---------------------------------------------------------------------------
// ChecksumEntry — one file's checksums
// ---------------------------------------------------------------------------

/// All available checksums for a single file.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct ChecksumEntry {
    /// Relative path of the file (relative to the archive root).
    pub path: String,
    /// File size in bytes.
    pub size_bytes: u64,
    /// SHA-256 hex digest (if computed).
    pub sha256: Option<String>,
    /// MD5 hex digest (if computed).
    pub md5: Option<String>,
    /// BLAKE3 hex digest (if computed).
    pub blake3: Option<String>,
    /// CRC32 hex digest (if computed).
    pub crc32: Option<String>,
    /// ISO-8601 timestamp of when the checksum was recorded.
    pub recorded_at: String,
}

impl ChecksumEntry {
    /// Create a new entry with the given path and size.
    #[must_use]
    pub fn new(path: impl Into<String>, size_bytes: u64) -> Self {
        let now = chrono::Utc::now().to_rfc3339();
        Self {
            path: path.into(),
            size_bytes,
            sha256: None,
            md5: None,
            blake3: None,
            crc32: None,
            recorded_at: now,
        }
    }

    /// Render a single line in `sha256sum` format.
    ///
    /// Returns `None` if no SHA-256 digest is available.
    #[must_use]
    pub fn sha256sum_line(&self) -> Option<String> {
        self.sha256
            .as_ref()
            .map(|h| format!("{}  {}", h, self.path))
    }

    /// Render a single line in `md5sum` format.
    #[must_use]
    pub fn md5sum_line(&self) -> Option<String> {
        self.md5
            .as_ref()
            .map(|h| format!("{}  {}", h, self.path))
    }

    /// Render a single line in BLAKE3 bsum format.
    #[must_use]
    pub fn blake3sum_line(&self) -> Option<String> {
        self.blake3
            .as_ref()
            .map(|h| format!("{}  {}", h, self.path))
    }

    /// Render a human-readable multi-algorithm text block for this entry.
    #[must_use]
    pub fn to_text_block(&self) -> String {
        let mut out = String::new();
        let _ = writeln!(out, "File:    {}", self.path);
        let _ = writeln!(out, "Size:    {} bytes", self.size_bytes);
        if let Some(ref h) = self.sha256 {
            let _ = writeln!(out, "SHA-256: {h}");
        }
        if let Some(ref h) = self.blake3 {
            let _ = writeln!(out, "BLAKE3:  {h}");
        }
        if let Some(ref h) = self.md5 {
            let _ = writeln!(out, "MD5:     {h}");
        }
        if let Some(ref h) = self.crc32 {
            let _ = writeln!(out, "CRC32:   {h}");
        }
        let _ = writeln!(out, "Recorded:{}", self.recorded_at);
        out
    }
}

// ---------------------------------------------------------------------------
// SidecarManifest — a collection of checksum entries
// ---------------------------------------------------------------------------

/// A manifest of checksum entries for an archive or directory.
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct SidecarManifest {
    /// Archive or directory name (informational).
    pub archive_name: String,
    /// ISO-8601 timestamp of manifest generation.
    pub generated_at: String,
    /// Tool version string.
    pub generator: String,
    /// All entries.
    pub entries: Vec<ChecksumEntry>,
}

impl SidecarManifest {
    /// Create a new empty manifest.
    #[must_use]
    pub fn new(archive_name: impl Into<String>) -> Self {
        Self {
            archive_name: archive_name.into(),
            generated_at: chrono::Utc::now().to_rfc3339(),
            generator: "oximedia-archive".to_string(),
            entries: Vec::new(),
        }
    }

    /// Add an entry.
    pub fn add(&mut self, entry: ChecksumEntry) {
        self.entries.push(entry);
    }

    /// Number of entries.
    #[must_use]
    pub fn entry_count(&self) -> usize {
        self.entries.len()
    }

    /// Total bytes across all entries.
    #[must_use]
    pub fn total_bytes(&self) -> u64 {
        self.entries.iter().map(|e| e.size_bytes).sum()
    }

    /// Render the manifest in the given format.
    ///
    /// Returns the sidecar file content as a `String`.
    pub fn render(&self, format: SidecarFormat) -> ArchiveResult<String> {
        match format {
            SidecarFormat::Sha256Sum => Ok(self.render_sha256sum()),
            SidecarFormat::Md5Sum => Ok(self.render_md5sum()),
            SidecarFormat::Blake3Sum => Ok(self.render_blake3sum()),
            SidecarFormat::JsonManifest => self.render_json(),
            SidecarFormat::TextManifest => Ok(self.render_text()),
        }
    }

    fn render_sha256sum(&self) -> String {
        let mut out = String::new();
        let _ = writeln!(out, "# SHA-256 checksums generated by oximedia-archive");
        let _ = writeln!(out, "# Generated: {}", self.generated_at);
        let _ = writeln!(out, "# Archive:   {}", self.archive_name);
        out.push('\n');
        for entry in &self.entries {
            if let Some(line) = entry.sha256sum_line() {
                let _ = writeln!(out, "{line}");
            }
        }
        out
    }

    fn render_md5sum(&self) -> String {
        let mut out = String::new();
        let _ = writeln!(out, "# MD5 checksums generated by oximedia-archive");
        let _ = writeln!(out, "# Generated: {}", self.generated_at);
        let _ = writeln!(out, "# Archive:   {}", self.archive_name);
        out.push('\n');
        for entry in &self.entries {
            if let Some(line) = entry.md5sum_line() {
                let _ = writeln!(out, "{line}");
            }
        }
        out
    }

    fn render_blake3sum(&self) -> String {
        let mut out = String::new();
        let _ = writeln!(out, "# BLAKE3 checksums generated by oximedia-archive");
        let _ = writeln!(out, "# Generated: {}", self.generated_at);
        let _ = writeln!(out, "# Archive:   {}", self.archive_name);
        out.push('\n');
        for entry in &self.entries {
            if let Some(line) = entry.blake3sum_line() {
                let _ = writeln!(out, "{line}");
            }
        }
        out
    }

    fn render_json(&self) -> ArchiveResult<String> {
        serde_json::to_string_pretty(self).map_err(|e| {
            ArchiveError::Validation(format!("sidecar JSON serialization failed: {e}"))
        })
    }

    fn render_text(&self) -> String {
        let mut out = String::new();
        let _ = writeln!(out, "========================================");
        let _ = writeln!(out, " CHECKSUM MANIFEST");
        let _ = writeln!(out, " Archive:   {}", self.archive_name);
        let _ = writeln!(out, " Generated: {}", self.generated_at);
        let _ = writeln!(out, " Tool:      {}", self.generator);
        let _ = writeln!(
            out,
            " Files:     {} ({} bytes total)",
            self.entries.len(),
            self.total_bytes()
        );
        let _ = writeln!(out, "========================================");
        out.push('\n');
        for entry in &self.entries {
            out.push_str(&entry.to_text_block());
            out.push('\n');
        }
        out
    }

    /// Deserialize from a JSON string.
    pub fn from_json(json: &str) -> ArchiveResult<Self> {
        serde_json::from_str(json).map_err(|e| {
            ArchiveError::Validation(format!("sidecar JSON deserialization failed: {e}"))
        })
    }
}

// ---------------------------------------------------------------------------
// SidecarGenerator — writes sidecar files to disk
// ---------------------------------------------------------------------------

/// Configuration for sidecar generation.
#[derive(Debug, Clone)]
pub struct SidecarConfig {
    /// Generate SHA-256 `.sha256` sidecar.
    pub generate_sha256: bool,
    /// Generate MD5 `.md5` sidecar.
    pub generate_md5: bool,
    /// Generate BLAKE3 `.blake3` sidecar.
    pub generate_blake3: bool,
    /// Generate combined JSON manifest.
    pub generate_json: bool,
    /// Generate combined text manifest.
    pub generate_text: bool,
    /// Place sidecars next to the source file (instead of in a dedicated dir).
    pub inline: bool,
    /// Directory to place sidecars in when `inline` is false.
    pub sidecar_dir: Option<PathBuf>,
}

impl Default for SidecarConfig {
    fn default() -> Self {
        Self {
            generate_sha256: true,
            generate_md5: false,
            generate_blake3: true,
            generate_json: true,
            generate_text: true,
            inline: false,
            sidecar_dir: None,
        }
    }
}

/// Writes sidecar files for a `SidecarManifest`.
pub struct SidecarGenerator {
    config: SidecarConfig,
}

impl SidecarGenerator {
    /// Create a new generator with the given config.
    #[must_use]
    pub fn new(config: SidecarConfig) -> Self {
        Self { config }
    }

    /// Create with default config.
    #[must_use]
    pub fn with_defaults() -> Self {
        Self::new(SidecarConfig::default())
    }

    /// Write all configured sidecar formats for `manifest` to disk.
    ///
    /// `base_path` is the directory containing the archived files.
    /// Returns paths of all written sidecar files.
    pub fn write_sidecars(
        &self,
        manifest: &SidecarManifest,
        base_path: &Path,
    ) -> ArchiveResult<Vec<PathBuf>> {
        let sidecar_dir = if self.config.inline {
            base_path.to_path_buf()
        } else {
            match &self.config.sidecar_dir {
                Some(d) => d.clone(),
                None => base_path.join("sidecars"),
            }
        };

        std::fs::create_dir_all(&sidecar_dir)?;

        let archive_stem = sanitize_filename(&manifest.archive_name);
        let mut written = Vec::new();

        if self.config.generate_sha256 {
            let content = manifest.render(SidecarFormat::Sha256Sum)?;
            let path = sidecar_dir.join(format!("{archive_stem}.sha256"));
            std::fs::write(&path, content.as_bytes())?;
            written.push(path);
        }

        if self.config.generate_md5 {
            let content = manifest.render(SidecarFormat::Md5Sum)?;
            let path = sidecar_dir.join(format!("{archive_stem}.md5"));
            std::fs::write(&path, content.as_bytes())?;
            written.push(path);
        }

        if self.config.generate_blake3 {
            let content = manifest.render(SidecarFormat::Blake3Sum)?;
            let path = sidecar_dir.join(format!("{archive_stem}.blake3"));
            std::fs::write(&path, content.as_bytes())?;
            written.push(path);
        }

        if self.config.generate_json {
            let content = manifest.render(SidecarFormat::JsonManifest)?;
            let path = sidecar_dir.join(format!("{archive_stem}.checksums.json"));
            std::fs::write(&path, content.as_bytes())?;
            written.push(path);
        }

        if self.config.generate_text {
            let content = manifest.render(SidecarFormat::TextManifest)?;
            let path = sidecar_dir.join(format!("{archive_stem}.checksums.txt"));
            std::fs::write(&path, content.as_bytes())?;
            written.push(path);
        }

        Ok(written)
    }

    /// Compute checksums for a file's byte content and return a `ChecksumEntry`.
    ///
    /// Computes whichever algorithms are enabled in the config.
    pub fn compute_entry(
        &self,
        path: &str,
        data: &[u8],
    ) -> ChecksumEntry {
        let mut entry = ChecksumEntry::new(path, data.len() as u64);

        if self.config.generate_sha256 {
            use sha2::Digest as _;
            let mut hasher = sha2::Sha256::new();
            hasher.update(data);
            entry.sha256 = Some(hex::encode(hasher.finalize()));
        }

        if self.config.generate_md5 {
            use md5::Digest as _;
            let mut hasher = md5::Md5::new();
            hasher.update(data);
            entry.md5 = Some(hex::encode(hasher.finalize()));
        }

        if self.config.generate_blake3 {
            entry.blake3 = Some(blake3::hash(data).to_hex().to_string());
        }

        entry
    }
}

// ---------------------------------------------------------------------------
// Parse sha256sum-format sidecar files
// ---------------------------------------------------------------------------

/// Parse a sha256sum-format file into `(hex_digest, filename)` pairs.
///
/// Lines starting with `#` are treated as comments and skipped. Empty lines
/// are ignored.
pub fn parse_sha256sum_file(content: &str) -> Vec<(String, String)> {
    content
        .lines()
        .filter(|l| !l.trim().is_empty() && !l.trim_start().starts_with('#'))
        .filter_map(|line| {
            // Format: `<hex>  <filename>` (two spaces) or `<hex> <filename>` (one space)
            let trimmed = line.trim();
            let split_pos = trimmed.find("  ").or_else(|| trimmed.find(' '))?;
            let hex = trimmed[..split_pos].to_string();
            let filename = trimmed[split_pos..].trim().to_string();
            if hex.is_empty() || filename.is_empty() {
                None
            } else {
                Some((hex, filename))
            }
        })
        .collect()
}

// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------

fn sanitize_filename(name: &str) -> String {
    name.chars()
        .map(|c| match c {
            '/' | '\\' | ':' | '*' | '?' | '"' | '<' | '>' | '|' => '_',
            c => c,
        })
        .collect()
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;

    fn make_entry(path: &str, size: u64, sha256: &str) -> ChecksumEntry {
        ChecksumEntry {
            path: path.to_string(),
            size_bytes: size,
            sha256: Some(sha256.to_string()),
            md5: Some("abc123".to_string()),
            blake3: Some("def456".to_string()),
            crc32: Some("00aabbcc".to_string()),
            recorded_at: "2026-01-01T00:00:00Z".to_string(),
        }
    }

    // --- SidecarFormat ---

    #[test]
    fn test_sidecar_format_extensions() {
        assert_eq!(SidecarFormat::Sha256Sum.file_extension(), "sha256");
        assert_eq!(SidecarFormat::Md5Sum.file_extension(), "md5");
        assert_eq!(SidecarFormat::Blake3Sum.file_extension(), "blake3");
        assert_eq!(SidecarFormat::JsonManifest.file_extension(), "checksums.json");
        assert_eq!(SidecarFormat::TextManifest.file_extension(), "checksums.txt");
    }

    // --- ChecksumEntry ---

    #[test]
    fn test_entry_sha256sum_line() {
        let e = make_entry("clip.mxf", 1024, "abcdef01234567");
        let line = e.sha256sum_line().expect("sha256sum line");
        assert_eq!(line, "abcdef01234567  clip.mxf");
    }

    #[test]
    fn test_entry_md5sum_line() {
        let e = make_entry("clip.mxf", 1024, "aaa");
        let line = e.md5sum_line().expect("md5sum line");
        assert!(line.contains("abc123"));
        assert!(line.contains("clip.mxf"));
    }

    #[test]
    fn test_entry_blake3sum_line() {
        let e = make_entry("clip.mxf", 1024, "aaa");
        let line = e.blake3sum_line().expect("blake3sum line");
        assert!(line.contains("def456"));
    }

    #[test]
    fn test_entry_to_text_block() {
        let e = make_entry("video.mxf", 4096, "sha256hex");
        let text = e.to_text_block();
        assert!(text.contains("video.mxf"));
        assert!(text.contains("4096 bytes"));
        assert!(text.contains("sha256hex"));
    }

    #[test]
    fn test_entry_no_checksums() {
        let e = ChecksumEntry::new("empty.bin", 0);
        assert!(e.sha256sum_line().is_none());
        assert!(e.md5sum_line().is_none());
        assert!(e.blake3sum_line().is_none());
    }

    // --- SidecarManifest ---

    #[test]
    fn test_manifest_new_empty() {
        let m = SidecarManifest::new("my-archive");
        assert_eq!(m.entry_count(), 0);
        assert_eq!(m.total_bytes(), 0);
        assert_eq!(m.archive_name, "my-archive");
    }

    #[test]
    fn test_manifest_add_and_count() {
        let mut m = SidecarManifest::new("archive");
        m.add(make_entry("a.mxf", 1000, "aaa"));
        m.add(make_entry("b.mxf", 2000, "bbb"));
        assert_eq!(m.entry_count(), 2);
        assert_eq!(m.total_bytes(), 3000);
    }

    #[test]
    fn test_manifest_render_sha256sum() {
        let mut m = SidecarManifest::new("test-arc");
        m.add(make_entry("clip.mxf", 1024, "deadbeef"));
        let content = m.render(SidecarFormat::Sha256Sum).expect("render sha256");
        assert!(content.contains("deadbeef  clip.mxf"));
        assert!(content.contains("# SHA-256"));
    }

    #[test]
    fn test_manifest_render_md5sum() {
        let mut m = SidecarManifest::new("test-arc");
        m.add(make_entry("a.mxf", 100, "sha"));
        let content = m.render(SidecarFormat::Md5Sum).expect("render md5");
        assert!(content.contains("abc123  a.mxf"));
    }

    #[test]
    fn test_manifest_render_blake3sum() {
        let mut m = SidecarManifest::new("test-arc");
        m.add(make_entry("a.mxf", 100, "sha"));
        let content = m.render(SidecarFormat::Blake3Sum).expect("render blake3");
        assert!(content.contains("def456  a.mxf"));
    }

    #[test]
    fn test_manifest_render_json_roundtrip() {
        let mut m = SidecarManifest::new("json-arc");
        m.add(make_entry("video.mkv", 8192, "sha256abc"));
        let json = m.render(SidecarFormat::JsonManifest).expect("render json");

        let restored = SidecarManifest::from_json(&json).expect("from json");
        assert_eq!(restored.archive_name, "json-arc");
        assert_eq!(restored.entry_count(), 1);
        assert_eq!(restored.entries[0].path, "video.mkv");
        assert_eq!(restored.entries[0].sha256.as_deref(), Some("sha256abc"));
    }

    #[test]
    fn test_manifest_render_text_contains_header() {
        let m = SidecarManifest::new("text-arc");
        let content = m.render(SidecarFormat::TextManifest).expect("render text");
        assert!(content.contains("CHECKSUM MANIFEST"));
        assert!(content.contains("text-arc"));
    }

    #[test]
    fn test_manifest_render_text_contains_entries() {
        let mut m = SidecarManifest::new("arc");
        m.add(make_entry("film.mkv", 2048, "film_sha"));
        let content = m.render(SidecarFormat::TextManifest).expect("render text");
        assert!(content.contains("film.mkv"));
        assert!(content.contains("film_sha"));
    }

    // --- SidecarGenerator ---

    #[test]
    fn test_generator_compute_entry_sha256() {
        let cfg = SidecarConfig {
            generate_sha256: true,
            generate_md5: false,
            generate_blake3: false,
            generate_json: false,
            generate_text: false,
            inline: true,
            sidecar_dir: None,
        };
        let gen = SidecarGenerator::new(cfg);
        let data = b"hello sidecar world";
        let entry = gen.compute_entry("test.bin", data);
        assert_eq!(entry.size_bytes, data.len() as u64);
        assert!(entry.sha256.is_some());
        assert!(entry.md5.is_none());
        assert!(entry.blake3.is_none());
    }

    #[test]
    fn test_generator_compute_entry_all_algorithms() {
        let gen = SidecarGenerator::with_defaults();
        let data = b"multi-algorithm checksum data";
        let entry = gen.compute_entry("multi.bin", data);
        assert!(entry.sha256.is_some());
        assert!(entry.blake3.is_some());
        // default config has md5 disabled
        assert!(entry.md5.is_none());
    }

    #[test]
    fn test_generator_sha256_known_vector() {
        let cfg = SidecarConfig {
            generate_sha256: true,
            generate_md5: false,
            generate_blake3: false,
            generate_json: false,
            generate_text: false,
            inline: true,
            sidecar_dir: None,
        };
        let gen = SidecarGenerator::new(cfg);
        let entry = gen.compute_entry("abc.bin", b"abc");
        assert_eq!(
            entry.sha256.as_deref(),
            Some("ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad")
        );
    }

    #[test]
    fn test_generator_write_sidecars() {
        let dir = std::env::temp_dir().join("oximedia_sidecar_write_test");
        std::fs::create_dir_all(&dir).ok();

        let cfg = SidecarConfig {
            generate_sha256: true,
            generate_md5: false,
            generate_blake3: true,
            generate_json: true,
            generate_text: true,
            inline: false,
            sidecar_dir: Some(dir.join("sidecars")),
        };
        let gen = SidecarGenerator::new(cfg);

        let mut manifest = SidecarManifest::new("my-archive");
        manifest.add(make_entry("clip.mxf", 1024, "aaabbbccc"));

        let written = gen.write_sidecars(&manifest, &dir).expect("write sidecars");
        assert_eq!(written.len(), 4); // sha256, blake3, json, text

        for path in &written {
            assert!(path.exists(), "sidecar not written: {}", path.display());
            let content = std::fs::read_to_string(path).expect("read sidecar");
            assert!(!content.is_empty());
        }

        std::fs::remove_dir_all(&dir).ok();
    }

    #[test]
    fn test_generator_write_inline_sidecars() {
        let dir = std::env::temp_dir().join("oximedia_sidecar_inline_test");
        std::fs::create_dir_all(&dir).ok();

        let cfg = SidecarConfig {
            generate_sha256: true,
            generate_md5: false,
            generate_blake3: false,
            generate_json: false,
            generate_text: false,
            inline: true,
            sidecar_dir: None,
        };
        let gen = SidecarGenerator::new(cfg);

        let mut manifest = SidecarManifest::new("inline-arc");
        manifest.add(make_entry("file.bin", 512, "sha"));

        let written = gen.write_sidecars(&manifest, &dir).expect("write inline");
        assert_eq!(written.len(), 1);
        assert!(written[0].starts_with(&dir));

        std::fs::remove_dir_all(&dir).ok();
    }

    // --- parse_sha256sum_file ---

    #[test]
    fn test_parse_sha256sum_basic() {
        let content = "abcdef1234567890  file.mxf\n1234567890abcdef  dir/clip.mkv\n";
        let entries = parse_sha256sum_file(content);
        assert_eq!(entries.len(), 2);
        assert_eq!(entries[0].0, "abcdef1234567890");
        assert_eq!(entries[0].1, "file.mxf");
        assert_eq!(entries[1].1, "dir/clip.mkv");
    }

    #[test]
    fn test_parse_sha256sum_skips_comments() {
        let content = "# This is a comment\nabc123  file.bin\n# Another comment\n";
        let entries = parse_sha256sum_file(content);
        assert_eq!(entries.len(), 1);
        assert_eq!(entries[0].1, "file.bin");
    }

    #[test]
    fn test_parse_sha256sum_skips_empty_lines() {
        let content = "abc  a.bin\n\n\ndef  b.bin\n";
        let entries = parse_sha256sum_file(content);
        assert_eq!(entries.len(), 2);
    }

    #[test]
    fn test_parse_sha256sum_empty_input() {
        let entries = parse_sha256sum_file("");
        assert!(entries.is_empty());
    }

    #[test]
    fn test_sanitize_filename_replaces_special_chars() {
        let name = "archive:v1/test*file";
        let sanitized = sanitize_filename(name);
        assert!(!sanitized.contains(':'));
        assert!(!sanitized.contains('/'));
        assert!(!sanitized.contains('*'));
    }

    // --- Round-trip: generate entry from data, then verify via sha256sum parse ---

    #[test]
    fn test_sidecar_roundtrip_via_sha256sum() {
        let gen = SidecarGenerator::new(SidecarConfig {
            generate_sha256: true,
            generate_md5: false,
            generate_blake3: false,
            generate_json: false,
            generate_text: false,
            inline: true,
            sidecar_dir: None,
        });
        let data = b"round-trip test content for sidecar";
        let entry = gen.compute_entry("round.bin", data);

        let mut manifest = SidecarManifest::new("rt-arc");
        manifest.add(entry.clone());

        let sha256content = manifest.render(SidecarFormat::Sha256Sum).expect("render");
        let parsed = parse_sha256sum_file(&sha256content);
        assert_eq!(parsed.len(), 1);
        assert_eq!(parsed[0].0, entry.sha256.as_deref().expect("sha256 set"));
        assert_eq!(parsed[0].1, "round.bin");
    }
}