oximedia-archive 0.1.7

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
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
//! Dry-run planning and rollback capability for format migrations.
//!
//! This module extends the [`migration`](crate::migration) module with two
//! critical features for safe long-term preservation workflows:
//!
//! ## Dry-run mode
//! Before performing any irreversible migration (transcoding, format conversion),
//! the operator can request a *plan* — a detailed description of every action
//! that would be taken, including:
//! - Which files would be touched
//! - Estimated output sizes and CPU time
//! - Risk assessment per format
//! - Any pre-condition failures (missing tools, insufficient disk space)
//!
//! ## Rollback
//! After a migration has been executed, the [`RollbackJournal`] records the
//! original state of each file so that the migration can be undone.  A rollback
//! replaces the migrated file(s) with the originals (which must have been
//! preserved, e.g. in the OARC sidecar or a designated backup directory).
//!
//! The journal is serialisable to JSON so that it survives process restarts.

use crate::{ArchiveError, ArchiveResult};
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::path::PathBuf;

// ---------------------------------------------------------------------------
// Dry-run plan
// ---------------------------------------------------------------------------

/// The overall status of a pre-condition check.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum PreconditionStatus {
    /// The pre-condition is satisfied.
    Ok,
    /// The pre-condition failed; the action cannot proceed without intervention.
    Failed,
    /// The pre-condition could not be evaluated (e.g. tool not installed).
    Skipped,
}

/// A single pre-condition check recorded in a dry-run plan.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PreconditionCheck {
    /// Short identifier (e.g. `"disk_space"`, `"tool_available"`).
    pub name: String,
    /// Human-readable message explaining the outcome.
    pub message: String,
    /// Check outcome.
    pub status: PreconditionStatus,
}

impl PreconditionCheck {
    /// Create a new passing pre-condition.
    #[must_use]
    pub fn ok(name: impl Into<String>, message: impl Into<String>) -> Self {
        Self {
            name: name.into(),
            message: message.into(),
            status: PreconditionStatus::Ok,
        }
    }

    /// Create a new failing pre-condition.
    #[must_use]
    pub fn failed(name: impl Into<String>, message: impl Into<String>) -> Self {
        Self {
            name: name.into(),
            message: message.into(),
            status: PreconditionStatus::Failed,
        }
    }

    /// Create a skipped pre-condition.
    #[must_use]
    pub fn skipped(name: impl Into<String>, message: impl Into<String>) -> Self {
        Self {
            name: name.into(),
            message: message.into(),
            status: PreconditionStatus::Skipped,
        }
    }
}

/// A single planned action for one source file.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PlannedAction {
    /// Absolute path to the source file.
    pub source_path: PathBuf,

    /// Proposed output path after migration.
    pub target_path: PathBuf,

    /// Source format name (e.g. `"DV"`).
    pub source_format: String,

    /// Target format name (e.g. `"DPX"`).
    pub target_format: String,

    /// Whether quality loss is expected.
    pub quality_loss: bool,

    /// Whether the migration is reversible (lossless source backup exists).
    pub reversible: bool,

    /// Pre-condition checks for this action.
    pub preconditions: Vec<PreconditionCheck>,

    /// Estimated output size in bytes.
    pub estimated_output_bytes: Option<u64>,

    /// Estimated CPU time in fractional seconds.
    pub estimated_cpu_secs: Option<f64>,

    /// Human-readable notes.
    pub notes: String,
}

impl PlannedAction {
    /// Create a new planned action.
    #[must_use]
    pub fn new(
        source_path: impl Into<PathBuf>,
        target_path: impl Into<PathBuf>,
        source_format: impl Into<String>,
        target_format: impl Into<String>,
    ) -> Self {
        Self {
            source_path: source_path.into(),
            target_path: target_path.into(),
            source_format: source_format.into(),
            target_format: target_format.into(),
            quality_loss: false,
            reversible: true,
            preconditions: Vec::new(),
            estimated_output_bytes: None,
            estimated_cpu_secs: None,
            notes: String::new(),
        }
    }

    /// Add a pre-condition check.
    pub fn add_precondition(&mut self, check: PreconditionCheck) {
        self.preconditions.push(check);
    }

    /// Return `true` if all pre-conditions are `Ok` or `Skipped`.
    #[must_use]
    pub fn preconditions_satisfied(&self) -> bool {
        self.preconditions
            .iter()
            .all(|c| c.status != PreconditionStatus::Failed)
    }
}

/// A complete dry-run migration plan covering all candidate files.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DryRunPlan {
    /// When the plan was generated.
    pub generated_at: DateTime<Utc>,

    /// Human-readable name for this migration campaign.
    pub campaign_name: String,

    /// All planned actions.
    pub actions: Vec<PlannedAction>,

    /// Global notes or warnings.
    pub global_notes: Vec<String>,
}

impl DryRunPlan {
    /// Create an empty plan.
    #[must_use]
    pub fn new(campaign_name: impl Into<String>) -> Self {
        Self {
            generated_at: Utc::now(),
            campaign_name: campaign_name.into(),
            actions: Vec::new(),
            global_notes: Vec::new(),
        }
    }

    /// Add an action.
    pub fn add_action(&mut self, action: PlannedAction) {
        self.actions.push(action);
    }

    /// Add a global note.
    pub fn add_note(&mut self, note: impl Into<String>) {
        self.global_notes.push(note.into());
    }

    /// Total number of actions in the plan.
    #[must_use]
    pub fn action_count(&self) -> usize {
        self.actions.len()
    }

    /// Number of actions whose pre-conditions are fully satisfied.
    #[must_use]
    pub fn ready_count(&self) -> usize {
        self.actions
            .iter()
            .filter(|a| a.preconditions_satisfied())
            .count()
    }

    /// Number of actions blocked by at least one failed pre-condition.
    #[must_use]
    pub fn blocked_count(&self) -> usize {
        self.actions
            .iter()
            .filter(|a| !a.preconditions_satisfied())
            .count()
    }

    /// Sum of estimated output sizes across all ready actions.
    #[must_use]
    pub fn total_estimated_output_bytes(&self) -> u64 {
        self.actions
            .iter()
            .filter(|a| a.preconditions_satisfied())
            .filter_map(|a| a.estimated_output_bytes)
            .fold(0u64, |acc, n| acc.saturating_add(n))
    }

    /// Sum of estimated CPU seconds across all ready actions.
    #[must_use]
    pub fn total_estimated_cpu_secs(&self) -> f64 {
        self.actions
            .iter()
            .filter(|a| a.preconditions_satisfied())
            .filter_map(|a| a.estimated_cpu_secs)
            .sum()
    }

    /// Serialize the plan to a JSON string.
    pub fn to_json(&self) -> ArchiveResult<String> {
        serde_json::to_string_pretty(self).map_err(|e| ArchiveError::Validation(e.to_string()))
    }

    /// Deserialize a plan from a JSON string.
    pub fn from_json(json: &str) -> ArchiveResult<Self> {
        serde_json::from_str(json).map_err(|e| ArchiveError::Validation(e.to_string()))
    }
}

// ---------------------------------------------------------------------------
// Rollback journal
// ---------------------------------------------------------------------------

/// The outcome of a single executed migration action, recorded in the journal.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum ActionOutcome {
    /// The action completed successfully.
    Success,
    /// The action failed; the target was not written.
    Failed,
    /// The action was skipped (pre-conditions not met).
    Skipped,
    /// The action was rolled back.
    RolledBack,
}

/// A record of one executed migration action in the rollback journal.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct JournalEntry {
    /// Unique entry identifier.
    pub entry_id: String,

    /// Path of the source file at the time of migration.
    pub source_path: PathBuf,

    /// Path of the migrated (output) file.
    pub target_path: PathBuf,

    /// Path where the original file was backed up before migration.
    /// `None` if the source was not preserved separately.
    pub backup_path: Option<PathBuf>,

    /// BLAKE3 checksum of the original source file (before migration).
    pub source_checksum: Option<String>,

    /// BLAKE3 checksum of the migrated output file.
    pub target_checksum: Option<String>,

    /// When the action was executed.
    pub executed_at: DateTime<Utc>,

    /// Outcome of the action.
    pub outcome: ActionOutcome,

    /// Error message if the action failed.
    pub error_message: Option<String>,
}

impl JournalEntry {
    /// Create a new journal entry in the `Success` state.
    #[must_use]
    pub fn success(
        entry_id: impl Into<String>,
        source_path: impl Into<PathBuf>,
        target_path: impl Into<PathBuf>,
    ) -> Self {
        Self {
            entry_id: entry_id.into(),
            source_path: source_path.into(),
            target_path: target_path.into(),
            backup_path: None,
            source_checksum: None,
            target_checksum: None,
            executed_at: Utc::now(),
            outcome: ActionOutcome::Success,
            error_message: None,
        }
    }

    /// Create a new journal entry in the `Failed` state.
    #[must_use]
    pub fn failed(
        entry_id: impl Into<String>,
        source_path: impl Into<PathBuf>,
        target_path: impl Into<PathBuf>,
        error: impl Into<String>,
    ) -> Self {
        Self {
            entry_id: entry_id.into(),
            source_path: source_path.into(),
            target_path: target_path.into(),
            backup_path: None,
            source_checksum: None,
            target_checksum: None,
            executed_at: Utc::now(),
            outcome: ActionOutcome::Failed,
            error_message: Some(error.into()),
        }
    }

    /// Return `true` if the migration can be rolled back for this entry.
    ///
    /// Rollback requires: outcome is `Success` and a backup path was recorded.
    #[must_use]
    pub fn can_rollback(&self) -> bool {
        self.outcome == ActionOutcome::Success && self.backup_path.is_some()
    }
}

/// Journal of executed migration actions with rollback support.
///
/// The journal is serialisable to JSON so it survives process restarts.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RollbackJournal {
    /// Name of the migration campaign.
    pub campaign_name: String,

    /// When the campaign started.
    pub started_at: DateTime<Utc>,

    /// All recorded entries.
    pub entries: Vec<JournalEntry>,

    /// Arbitrary tags for querying (e.g. `{"operator": "alice"}`).
    pub tags: HashMap<String, String>,
}

impl RollbackJournal {
    /// Create a new, empty journal.
    #[must_use]
    pub fn new(campaign_name: impl Into<String>) -> Self {
        Self {
            campaign_name: campaign_name.into(),
            started_at: Utc::now(),
            entries: Vec::new(),
            tags: HashMap::new(),
        }
    }

    /// Record a journal entry.
    pub fn record(&mut self, entry: JournalEntry) {
        self.entries.push(entry);
    }

    /// Return all entries eligible for rollback (Success + backup_path set).
    #[must_use]
    pub fn rollback_candidates(&self) -> Vec<&JournalEntry> {
        self.entries.iter().filter(|e| e.can_rollback()).collect()
    }

    /// Build a rollback plan: for each eligible entry, describe the
    /// `(target_path → backup_path)` file-move that would undo the migration.
    ///
    /// Returns a list of `(from, to)` tuples describing the restore operations.
    #[must_use]
    pub fn build_rollback_plan(&self) -> Vec<RollbackStep> {
        self.entries
            .iter()
            .filter(|e| e.can_rollback())
            .map(|e| RollbackStep {
                entry_id: e.entry_id.clone(),
                migrated_path: e.target_path.clone(),
                restore_from: e
                    .backup_path
                    .clone()
                    .expect("can_rollback guarantees backup_path is Some"),
                source_checksum: e.source_checksum.clone(),
            })
            .collect()
    }

    /// Apply a rollback by simulating the file-move operations.
    ///
    /// In a real implementation this would call `std::fs::rename` or copy
    /// bytes; here the method validates the plan and marks entries as
    /// `RolledBack` in memory.  Callers are responsible for the actual I/O.
    pub fn mark_rolled_back(&mut self, entry_id: &str) -> ArchiveResult<()> {
        let entry = self
            .entries
            .iter_mut()
            .find(|e| e.entry_id == entry_id)
            .ok_or_else(|| ArchiveError::Validation(format!("entry '{}' not found", entry_id)))?;

        if !entry.can_rollback() {
            return Err(ArchiveError::Validation(format!(
                "entry '{}' cannot be rolled back (outcome={:?}, backup={:?})",
                entry_id, entry.outcome, entry.backup_path
            )));
        }
        entry.outcome = ActionOutcome::RolledBack;
        Ok(())
    }

    /// Number of successfully rolled-back entries.
    #[must_use]
    pub fn rolled_back_count(&self) -> usize {
        self.entries
            .iter()
            .filter(|e| e.outcome == ActionOutcome::RolledBack)
            .count()
    }

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

    /// Number of successful (not yet rolled back) entries.
    #[must_use]
    pub fn success_count(&self) -> usize {
        self.entries
            .iter()
            .filter(|e| e.outcome == ActionOutcome::Success)
            .count()
    }

    /// Serialize the journal to a pretty-printed JSON string.
    pub fn to_json(&self) -> ArchiveResult<String> {
        serde_json::to_string_pretty(self).map_err(|e| ArchiveError::Validation(e.to_string()))
    }

    /// Deserialize a journal from a JSON string.
    pub fn from_json(json: &str) -> ArchiveResult<Self> {
        serde_json::from_str(json).map_err(|e| ArchiveError::Validation(e.to_string()))
    }
}

/// A single step in a rollback plan.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RollbackStep {
    /// Journal entry id.
    pub entry_id: String,
    /// The migrated output file to be removed / replaced.
    pub migrated_path: PathBuf,
    /// The backup of the original file to restore from.
    pub restore_from: PathBuf,
    /// Expected checksum of the restored file (for verification after restore).
    pub source_checksum: Option<String>,
}

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

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

    // ── DryRunPlan ──────────────────────────────────────────────────────────

    #[test]
    fn test_dry_run_plan_empty() {
        let plan = DryRunPlan::new("2026-Q1 DV Migration");
        assert_eq!(plan.action_count(), 0);
        assert_eq!(plan.ready_count(), 0);
        assert_eq!(plan.blocked_count(), 0);
    }

    #[test]
    fn test_dry_run_plan_ready_action() {
        let mut plan = DryRunPlan::new("test");
        let mut action = PlannedAction::new("/src/a.dv", "/dst/a.dpx", "DV", "DPX");
        action.add_precondition(PreconditionCheck::ok("disk_space", "50 GB free"));
        action.estimated_output_bytes = Some(2_000_000_000);
        action.estimated_cpu_secs = Some(120.0);
        plan.add_action(action);
        assert_eq!(plan.ready_count(), 1);
        assert_eq!(plan.blocked_count(), 0);
        assert_eq!(plan.total_estimated_output_bytes(), 2_000_000_000);
        assert!((plan.total_estimated_cpu_secs() - 120.0).abs() < 1e-9);
    }

    #[test]
    fn test_dry_run_plan_blocked_action() {
        let mut plan = DryRunPlan::new("test");
        let mut action = PlannedAction::new("/src/b.dv", "/dst/b.dpx", "DV", "DPX");
        action.add_precondition(PreconditionCheck::failed("disk_space", "Only 1 GB free"));
        plan.add_action(action);
        assert_eq!(plan.ready_count(), 0);
        assert_eq!(plan.blocked_count(), 1);
    }

    #[test]
    fn test_dry_run_plan_mixed_preconditions() {
        let mut plan = DryRunPlan::new("test");
        let mut action = PlannedAction::new("/src/c.dv", "/dst/c.dpx", "DV", "DPX");
        action.add_precondition(PreconditionCheck::ok("tool", "ffmpeg present"));
        action.add_precondition(PreconditionCheck::failed("space", "insufficient space"));
        plan.add_action(action);
        assert_eq!(
            plan.blocked_count(),
            1,
            "one failed check blocks the action"
        );
    }

    #[test]
    fn test_dry_run_plan_skipped_precondition_not_blocking() {
        let mut plan = DryRunPlan::new("test");
        let mut action = PlannedAction::new("/src/d.dv", "/dst/d.dpx", "DV", "DPX");
        action.add_precondition(PreconditionCheck::skipped("optional", "tool unavailable"));
        plan.add_action(action);
        assert_eq!(plan.ready_count(), 1, "skipped check does not block");
    }

    #[test]
    fn test_dry_run_plan_json_roundtrip() {
        let mut plan = DryRunPlan::new("json-test");
        plan.add_note("This is a test campaign");
        let json = plan.to_json().expect("serialize");
        let restored = DryRunPlan::from_json(&json).expect("deserialize");
        assert_eq!(restored.campaign_name, "json-test");
        assert_eq!(restored.global_notes.len(), 1);
    }

    // ── RollbackJournal ─────────────────────────────────────────────────────

    #[test]
    fn test_journal_empty() {
        let journal = RollbackJournal::new("empty campaign");
        assert_eq!(journal.entry_count(), 0);
        assert!(journal.rollback_candidates().is_empty());
    }

    #[test]
    fn test_journal_success_entry_with_backup_is_candidate() {
        let mut journal = RollbackJournal::new("test");
        let mut entry = JournalEntry::success("e1", "/src/a.dv", "/dst/a.dpx");
        entry.backup_path = Some(PathBuf::from("/backup/a.dv"));
        journal.record(entry);
        assert_eq!(journal.rollback_candidates().len(), 1);
    }

    #[test]
    fn test_journal_success_entry_without_backup_not_candidate() {
        let mut journal = RollbackJournal::new("test");
        let entry = JournalEntry::success("e2", "/src/b.dv", "/dst/b.dpx");
        // no backup_path set
        journal.record(entry);
        assert_eq!(journal.rollback_candidates().len(), 0);
    }

    #[test]
    fn test_journal_failed_entry_not_candidate() {
        let mut journal = RollbackJournal::new("test");
        let mut entry = JournalEntry::failed("e3", "/src/c.dv", "/dst/c.dpx", "transcode error");
        entry.backup_path = Some(PathBuf::from("/backup/c.dv"));
        journal.record(entry);
        assert_eq!(
            journal.rollback_candidates().len(),
            0,
            "failed actions cannot be rolled back"
        );
    }

    #[test]
    fn test_journal_mark_rolled_back_ok() {
        let mut journal = RollbackJournal::new("test");
        let mut entry = JournalEntry::success("e4", "/src/d.dv", "/dst/d.dpx");
        entry.backup_path = Some(PathBuf::from("/backup/d.dv"));
        journal.record(entry);

        journal.mark_rolled_back("e4").expect("should rollback");
        assert_eq!(journal.rolled_back_count(), 1);
        assert_eq!(journal.success_count(), 0);
    }

    #[test]
    fn test_journal_mark_rolled_back_unknown_id_errors() {
        let mut journal = RollbackJournal::new("test");
        let result = journal.mark_rolled_back("nonexistent");
        assert!(result.is_err());
    }

    #[test]
    fn test_journal_build_rollback_plan() {
        let mut journal = RollbackJournal::new("test");
        let mut entry = JournalEntry::success("e5", "/src/e.dv", "/dst/e.dpx");
        entry.backup_path = Some(PathBuf::from("/backup/e.dv"));
        entry.source_checksum = Some("abc123".to_string());
        journal.record(entry);

        let plan = journal.build_rollback_plan();
        assert_eq!(plan.len(), 1);
        assert_eq!(plan[0].entry_id, "e5");
        assert_eq!(plan[0].restore_from, PathBuf::from("/backup/e.dv"));
        assert_eq!(plan[0].source_checksum.as_deref(), Some("abc123"));
    }

    #[test]
    fn test_journal_json_roundtrip() {
        let mut journal = RollbackJournal::new("json-roundtrip");
        journal
            .tags
            .insert("operator".to_string(), "alice".to_string());
        let json = journal.to_json().expect("serialize");
        let restored = RollbackJournal::from_json(&json).expect("deserialize");
        assert_eq!(restored.campaign_name, "json-roundtrip");
        assert_eq!(
            restored.tags.get("operator").map(|s| s.as_str()),
            Some("alice")
        );
    }

    // --- New tests for migration dry-run and rollback (implementation items) ---

    #[test]
    fn test_planned_action_default_flags() {
        let action = PlannedAction::new("/src/a.dv", "/dst/a.mxf", "DV", "MXF");
        assert!(!action.quality_loss, "quality_loss should default to false");
        assert!(action.reversible, "reversible should default to true");
        assert!(action.notes.is_empty(), "notes should default to empty");
        assert!(
            action.preconditions.is_empty(),
            "preconditions should default to empty"
        );
    }

    #[test]
    fn test_planned_action_preconditions_satisfied_empty() {
        // No preconditions → satisfied
        let action = PlannedAction::new("/src/a.dv", "/dst/a.mxf", "DV", "MXF");
        assert!(action.preconditions_satisfied());
    }

    #[test]
    fn test_planned_action_multiple_ok_preconditions() {
        let mut action = PlannedAction::new("/src/a.dv", "/dst/a.mxf", "DV", "MXF");
        action.add_precondition(PreconditionCheck::ok("disk", "100 GB free"));
        action.add_precondition(PreconditionCheck::ok("tool", "oximedia available"));
        action.add_precondition(PreconditionCheck::skipped("gpu", "no GPU available"));
        assert!(action.preconditions_satisfied());
    }

    #[test]
    fn test_dry_run_plan_global_notes() {
        let mut plan = DryRunPlan::new("notes-test");
        plan.add_note("note 1");
        plan.add_note("note 2");
        assert_eq!(plan.global_notes.len(), 2);
        assert_eq!(plan.global_notes[0], "note 1");
    }

    #[test]
    fn test_dry_run_plan_estimated_bytes_only_ready() {
        let mut plan = DryRunPlan::new("test");
        // Blocked action — should NOT be included in estimate
        let mut blocked = PlannedAction::new("/src/b.dv", "/dst/b.mxf", "DV", "MXF");
        blocked.add_precondition(PreconditionCheck::failed("space", "no space"));
        blocked.estimated_output_bytes = Some(500_000_000);
        plan.add_action(blocked);

        // Ready action — SHOULD be included
        let mut ready = PlannedAction::new("/src/a.dv", "/dst/a.mxf", "DV", "MXF");
        ready.estimated_output_bytes = Some(200_000_000);
        plan.add_action(ready);

        assert_eq!(plan.total_estimated_output_bytes(), 200_000_000);
    }

    #[test]
    fn test_journal_entry_can_rollback_requires_success_and_backup() {
        // Failed entry with backup → cannot rollback
        let mut e = JournalEntry::failed("e1", "/src/a.dv", "/dst/a.mxf", "error");
        e.backup_path = Some(PathBuf::from("/backup/a.dv"));
        assert!(!e.can_rollback());

        // Success entry without backup → cannot rollback
        let e2 = JournalEntry::success("e2", "/src/b.dv", "/dst/b.mxf");
        assert!(!e2.can_rollback());

        // Success entry with backup → can rollback
        let mut e3 = JournalEntry::success("e3", "/src/c.dv", "/dst/c.mxf");
        e3.backup_path = Some(PathBuf::from("/backup/c.dv"));
        assert!(e3.can_rollback());
    }

    #[test]
    fn test_journal_rollback_idempotent_state() {
        let mut journal = RollbackJournal::new("test");
        let mut entry = JournalEntry::success("e1", "/src/a.dv", "/dst/a.mxf");
        entry.backup_path = Some(PathBuf::from("/backup/a.dv"));
        journal.record(entry);

        // After rolling back, the entry is marked RolledBack
        journal.mark_rolled_back("e1").expect("rollback ok");
        assert_eq!(journal.rolled_back_count(), 1);
        // Trying to rollback again should fail (outcome is now RolledBack, not Success)
        let second = journal.mark_rolled_back("e1");
        assert!(
            second.is_err(),
            "cannot rollback an already-rolled-back entry"
        );
    }

    #[test]
    fn test_journal_multiple_entries_only_eligible_in_plan() {
        let mut journal = RollbackJournal::new("test");

        // Entry 1: success + backup (eligible)
        let mut e1 = JournalEntry::success("e1", "/src/a.dv", "/dst/a.mxf");
        e1.backup_path = Some(PathBuf::from("/backup/a.dv"));
        journal.record(e1);

        // Entry 2: success, no backup (not eligible)
        journal.record(JournalEntry::success("e2", "/src/b.dv", "/dst/b.mxf"));

        // Entry 3: failed + backup (not eligible)
        let mut e3 = JournalEntry::failed("e3", "/src/c.dv", "/dst/c.mxf", "error");
        e3.backup_path = Some(PathBuf::from("/backup/c.dv"));
        journal.record(e3);

        assert_eq!(journal.rollback_candidates().len(), 1);
        let plan = journal.build_rollback_plan();
        assert_eq!(plan.len(), 1);
        assert_eq!(plan[0].entry_id, "e1");
    }

    #[test]
    fn test_rollback_step_fields() {
        let mut journal = RollbackJournal::new("test");
        let mut entry = JournalEntry::success("e42", "/src/master.mxf", "/dst/master.dpx");
        entry.backup_path = Some(PathBuf::from("/vault/master.mxf.bak"));
        entry.source_checksum = Some("blake3hex".to_string());
        journal.record(entry);

        let steps = journal.build_rollback_plan();
        assert_eq!(steps.len(), 1);
        assert_eq!(steps[0].entry_id, "e42");
        assert_eq!(steps[0].migrated_path, PathBuf::from("/dst/master.dpx"));
        assert_eq!(
            steps[0].restore_from,
            PathBuf::from("/vault/master.mxf.bak")
        );
        assert_eq!(steps[0].source_checksum.as_deref(), Some("blake3hex"));
    }

    #[test]
    fn test_dry_run_plan_total_cpu_secs_only_ready() {
        let mut plan = DryRunPlan::new("cpu-test");

        let mut blocked = PlannedAction::new("/src/b.dv", "/dst/b.mxf", "DV", "MXF");
        blocked.add_precondition(PreconditionCheck::failed("space", "no space"));
        blocked.estimated_cpu_secs = Some(500.0);
        plan.add_action(blocked);

        let mut ready = PlannedAction::new("/src/a.dv", "/dst/a.mxf", "DV", "MXF");
        ready.estimated_cpu_secs = Some(30.0);
        plan.add_action(ready);

        let total = plan.total_estimated_cpu_secs();
        assert!(
            (total - 30.0).abs() < 1e-9,
            "blocked action CPU secs should not be counted"
        );
    }
}