ipfrs-storage 0.2.0

Storage backends and block management for IPFRS content-addressed 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
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
//! Storage migration planner: schedules and tracks block movements between tiers.
//!
//! This module provides [`StorageMigrationPlanner`] which plans migrations of
//! blocks between storage tiers (NVMe → SSD → HDD → Archive), computes cost
//! estimates, resolves dependency ordering, and supports rollback of failed tasks.

use std::collections::HashMap;

// ─────────────────────────────────────────────────────────────────────────────
// Public types
// ─────────────────────────────────────────────────────────────────────────────

/// Ordered storage tiers from fastest/most-expensive to slowest/cheapest.
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum StorageTier {
    /// NVMe — fastest and most expensive.
    Nvme = 0,
    /// Solid-state drive.
    Ssd = 1,
    /// Hard-disk drive.
    Hdd = 2,
    /// Long-term archival storage — slowest and cheapest.
    Archive = 3,
}

/// Whether a migration moves a block toward a faster or slower tier.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum MigrationDirection {
    /// Move to a faster (lower-numbered) tier.
    Promote,
    /// Move to a slower (higher-numbered) tier, or stay at the same tier.
    Demote,
}

/// A single unit of migration work: moving one block from one tier to another.
#[derive(Clone, Debug)]
pub struct MigrationTask {
    /// Unique identifier for this task.
    pub task_id: u64,
    /// The block being migrated.
    pub block_id: u64,
    /// Content identifier of the block.
    pub cid: String,
    /// Tier the block is currently stored on.
    pub from_tier: StorageTier,
    /// Tier the block should be moved to.
    pub to_tier: StorageTier,
    /// Whether the migration promotes or demotes the block.
    pub direction: MigrationDirection,
    /// Block size in bytes.
    pub size_bytes: u64,
    /// Estimated migration duration in milliseconds.
    pub estimated_cost_ms: u64,
    /// IDs of tasks that must reach [`MigrationStatus::Completed`] before this
    /// task may start.
    pub depends_on: Vec<u64>,
}

impl MigrationTask {
    /// Returns `true` when the task moves the block toward a faster tier.
    pub fn is_promotion(&self) -> bool {
        self.direction == MigrationDirection::Promote
    }
}

/// Lifecycle state of a [`MigrationTask`].
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum MigrationStatus {
    /// Waiting to be started (dependencies may or may not be resolved).
    Pending,
    /// Currently executing.
    InProgress,
    /// Finished successfully.
    Completed,
    /// Finished with an error.
    Failed,
    /// Was in [`Failed`](MigrationStatus::Failed) and has been rolled back.
    RolledBack,
}

/// A task together with its current execution metadata.
#[derive(Clone, Debug)]
pub struct MigrationRecord {
    /// The task being tracked.
    pub task: MigrationTask,
    /// Current lifecycle state.
    pub status: MigrationStatus,
    /// Logical clock value at which this task transitioned to
    /// [`InProgress`](MigrationStatus::InProgress).
    pub started_at_tick: Option<u64>,
    /// Logical clock value at which this task reached a terminal state.
    pub completed_at_tick: Option<u64>,
}

/// Aggregate counters produced by [`StorageMigrationPlanner::stats`].
#[derive(Clone, Debug, Default)]
pub struct PlannerStats {
    /// Total number of tasks ever planned.
    pub total_tasks: usize,
    /// Tasks in [`Pending`](MigrationStatus::Pending) state.
    pub pending: usize,
    /// Tasks in [`InProgress`](MigrationStatus::InProgress) state.
    pub in_progress: usize,
    /// Tasks in [`Completed`](MigrationStatus::Completed) state.
    pub completed: usize,
    /// Tasks in [`Failed`](MigrationStatus::Failed) or
    /// [`RolledBack`](MigrationStatus::RolledBack) state.
    pub failed: usize,
    /// Sum of `size_bytes` for every completed task.
    pub total_bytes_migrated: u64,
    /// Number of completed promotion tasks.
    pub promotions: u64,
    /// Number of completed demotion tasks.
    pub demotions: u64,
}

// ─────────────────────────────────────────────────────────────────────────────
// Core planner
// ─────────────────────────────────────────────────────────────────────────────

/// Plans and tracks migrations of blocks across storage tiers.
///
/// # Example
///
/// ```
/// use ipfrs_storage::migration_planner::{StorageMigrationPlanner, StorageTier};
///
/// let mut planner = StorageMigrationPlanner::new();
/// let id = planner.plan_migration(1, "bafyexample".into(), StorageTier::Nvme, StorageTier::Hdd, 4_000_000, vec![]);
/// assert!(planner.start_task(id, 0));
/// assert!(planner.complete_task(id, 10));
/// ```
pub struct StorageMigrationPlanner {
    /// All records keyed by their `task_id`.
    pub records: HashMap<u64, MigrationRecord>,
    /// Counter used to assign the next unique task identifier.
    pub next_task_id: u64,
}

impl StorageMigrationPlanner {
    /// Creates an empty planner with no tasks.
    pub fn new() -> Self {
        Self {
            records: HashMap::new(),
            next_task_id: 0,
        }
    }

    /// Plans a migration and returns the new task's ID.
    ///
    /// The direction is determined by comparing `from` and `to`:
    /// - `from > to` → [`Promote`](MigrationDirection::Promote)
    /// - `from <= to` → [`Demote`](MigrationDirection::Demote)
    ///
    /// The cost estimate uses the formula `size_bytes / 1_000_000 + 10` (ms).
    pub fn plan_migration(
        &mut self,
        block_id: u64,
        cid: String,
        from: StorageTier,
        to: StorageTier,
        size_bytes: u64,
        depends_on: Vec<u64>,
    ) -> u64 {
        let task_id = self.next_task_id;
        self.next_task_id += 1;

        let direction = if from > to {
            MigrationDirection::Promote
        } else {
            MigrationDirection::Demote
        };

        let estimated_cost_ms = size_bytes / 1_000_000 + 10;

        let task = MigrationTask {
            task_id,
            block_id,
            cid,
            from_tier: from,
            to_tier: to,
            direction,
            size_bytes,
            estimated_cost_ms,
            depends_on,
        };

        let record = MigrationRecord {
            task,
            status: MigrationStatus::Pending,
            started_at_tick: None,
            completed_at_tick: None,
        };

        self.records.insert(task_id, record);
        task_id
    }

    /// Transitions a task from [`Pending`](MigrationStatus::Pending) to
    /// [`InProgress`](MigrationStatus::InProgress).
    ///
    /// Returns `false` if:
    /// - the task does not exist,
    /// - the task is not in `Pending` state, or
    /// - any dependency is not yet [`Completed`](MigrationStatus::Completed).
    pub fn start_task(&mut self, task_id: u64, current_tick: u64) -> bool {
        // Collect dependency ids first to avoid holding an immutable borrow.
        let depends_on: Vec<u64> = match self.records.get(&task_id) {
            Some(r) if r.status == MigrationStatus::Pending => r.task.depends_on.clone(),
            _ => return false,
        };

        // Verify every dependency has completed.
        for dep_id in &depends_on {
            match self.records.get(dep_id) {
                Some(dep) if dep.status == MigrationStatus::Completed => {}
                _ => return false,
            }
        }

        if let Some(record) = self.records.get_mut(&task_id) {
            record.status = MigrationStatus::InProgress;
            record.started_at_tick = Some(current_tick);
            true
        } else {
            false
        }
    }

    /// Transitions a task from [`InProgress`](MigrationStatus::InProgress) to
    /// [`Completed`](MigrationStatus::Completed).
    ///
    /// Returns `false` if the task is not found or not `InProgress`.
    pub fn complete_task(&mut self, task_id: u64, current_tick: u64) -> bool {
        match self.records.get_mut(&task_id) {
            Some(r) if r.status == MigrationStatus::InProgress => {
                r.status = MigrationStatus::Completed;
                r.completed_at_tick = Some(current_tick);
                true
            }
            _ => false,
        }
    }

    /// Transitions a task to [`Failed`](MigrationStatus::Failed).
    ///
    /// Accepted from either [`Pending`](MigrationStatus::Pending) or
    /// [`InProgress`](MigrationStatus::InProgress). Returns `false` if the task
    /// is not found or already in a terminal state.
    pub fn fail_task(&mut self, task_id: u64) -> bool {
        match self.records.get_mut(&task_id) {
            Some(r)
                if r.status == MigrationStatus::Pending
                    || r.status == MigrationStatus::InProgress =>
            {
                r.status = MigrationStatus::Failed;
                true
            }
            _ => false,
        }
    }

    /// Transitions a task from [`Failed`](MigrationStatus::Failed) to
    /// [`RolledBack`](MigrationStatus::RolledBack).
    ///
    /// Returns `false` if the task is not found or not in `Failed` state.
    pub fn rollback_task(&mut self, task_id: u64) -> bool {
        match self.records.get_mut(&task_id) {
            Some(r) if r.status == MigrationStatus::Failed => {
                r.status = MigrationStatus::RolledBack;
                true
            }
            _ => false,
        }
    }

    /// Returns the IDs of all tasks that are [`Pending`](MigrationStatus::Pending)
    /// and whose every dependency has [`Completed`](MigrationStatus::Completed).
    ///
    /// The list is sorted in ascending order of `task_id`.
    pub fn ready_tasks(&self) -> Vec<u64> {
        let mut ready: Vec<u64> = self
            .records
            .values()
            .filter(|r| {
                if r.status != MigrationStatus::Pending {
                    return false;
                }
                r.task.depends_on.iter().all(|dep_id| {
                    self.records
                        .get(dep_id)
                        .is_some_and(|d| d.status == MigrationStatus::Completed)
                })
            })
            .map(|r| r.task.task_id)
            .collect();

        ready.sort_unstable();
        ready
    }

    /// Returns an immutable reference to the record for `task_id`, or `None`.
    pub fn get_record(&self, task_id: u64) -> Option<&MigrationRecord> {
        self.records.get(&task_id)
    }

    /// Computes aggregate statistics over all tracked records.
    pub fn stats(&self) -> PlannerStats {
        let mut stats = PlannerStats {
            total_tasks: self.records.len(),
            ..Default::default()
        };

        for record in self.records.values() {
            match record.status {
                MigrationStatus::Pending => stats.pending += 1,
                MigrationStatus::InProgress => stats.in_progress += 1,
                MigrationStatus::Completed => {
                    stats.completed += 1;
                    stats.total_bytes_migrated += record.task.size_bytes;
                    match record.task.direction {
                        MigrationDirection::Promote => stats.promotions += 1,
                        MigrationDirection::Demote => stats.demotions += 1,
                    }
                }
                MigrationStatus::Failed | MigrationStatus::RolledBack => stats.failed += 1,
            }
        }

        stats
    }
}

impl Default for StorageMigrationPlanner {
    fn default() -> Self {
        Self::new()
    }
}

// ─────────────────────────────────────────────────────────────────────────────
// Tests
// ─────────────────────────────────────────────────────────────────────────────

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

    // ── Helper ───────────────────────────────────────────────────────────────

    fn make_planner() -> StorageMigrationPlanner {
        StorageMigrationPlanner::new()
    }

    fn add_simple(
        p: &mut StorageMigrationPlanner,
        from: StorageTier,
        to: StorageTier,
        size: u64,
    ) -> u64 {
        p.plan_migration(1, "bafytest".into(), from, to, size, vec![])
    }

    // ── plan_migration ───────────────────────────────────────────────────────

    #[test]
    fn plan_migration_returns_sequential_ids() {
        let mut p = make_planner();
        let id0 = add_simple(&mut p, StorageTier::Nvme, StorageTier::Hdd, 0);
        let id1 = add_simple(&mut p, StorageTier::Nvme, StorageTier::Hdd, 0);
        assert_eq!(id0, 0);
        assert_eq!(id1, 1);
    }

    #[test]
    fn plan_migration_demote_direction() {
        let mut p = make_planner();
        let id = add_simple(&mut p, StorageTier::Nvme, StorageTier::Hdd, 0);
        let record = p.get_record(id).expect("record must exist");
        assert_eq!(record.task.direction, MigrationDirection::Demote);
        assert!(!record.task.is_promotion());
    }

    #[test]
    fn plan_migration_promote_direction() {
        let mut p = make_planner();
        let id = add_simple(&mut p, StorageTier::Archive, StorageTier::Ssd, 0);
        let record = p.get_record(id).expect("record must exist");
        assert_eq!(record.task.direction, MigrationDirection::Promote);
        assert!(record.task.is_promotion());
    }

    #[test]
    fn plan_migration_same_tier_is_demote() {
        let mut p = make_planner();
        let id = add_simple(&mut p, StorageTier::Ssd, StorageTier::Ssd, 0);
        let record = p.get_record(id).expect("record must exist");
        assert_eq!(record.task.direction, MigrationDirection::Demote);
    }

    #[test]
    fn plan_migration_cost_estimate_small() {
        let mut p = make_planner();
        // 500_000 bytes → 500_000 / 1_000_000 = 0, + 10 = 10
        let id = add_simple(&mut p, StorageTier::Nvme, StorageTier::Archive, 500_000);
        let record = p.get_record(id).unwrap();
        assert_eq!(record.task.estimated_cost_ms, 10);
    }

    #[test]
    fn plan_migration_cost_estimate_large() {
        let mut p = make_planner();
        // 5_000_000 bytes → 5 + 10 = 15
        let id = add_simple(&mut p, StorageTier::Ssd, StorageTier::Hdd, 5_000_000);
        let record = p.get_record(id).unwrap();
        assert_eq!(record.task.estimated_cost_ms, 15);
    }

    #[test]
    fn plan_migration_status_is_pending() {
        let mut p = make_planner();
        let id = add_simple(&mut p, StorageTier::Nvme, StorageTier::Hdd, 0);
        let record = p.get_record(id).unwrap();
        assert_eq!(record.status, MigrationStatus::Pending);
        assert!(record.started_at_tick.is_none());
        assert!(record.completed_at_tick.is_none());
    }

    #[test]
    fn plan_migration_stores_block_fields() {
        let mut p = make_planner();
        let id = p.plan_migration(
            42,
            "bafycid".into(),
            StorageTier::Hdd,
            StorageTier::Nvme,
            1_000,
            vec![],
        );
        let record = p.get_record(id).unwrap();
        assert_eq!(record.task.block_id, 42);
        assert_eq!(record.task.cid, "bafycid");
        assert_eq!(record.task.from_tier, StorageTier::Hdd);
        assert_eq!(record.task.to_tier, StorageTier::Nvme);
        assert_eq!(record.task.size_bytes, 1_000);
    }

    // ── start_task ───────────────────────────────────────────────────────────

    #[test]
    fn start_task_sets_in_progress() {
        let mut p = make_planner();
        let id = add_simple(&mut p, StorageTier::Ssd, StorageTier::Hdd, 0);
        assert!(p.start_task(id, 5));
        let record = p.get_record(id).unwrap();
        assert_eq!(record.status, MigrationStatus::InProgress);
        assert_eq!(record.started_at_tick, Some(5));
    }

    #[test]
    fn start_task_unknown_id_returns_false() {
        let mut p = make_planner();
        assert!(!p.start_task(999, 0));
    }

    #[test]
    fn start_task_already_in_progress_returns_false() {
        let mut p = make_planner();
        let id = add_simple(&mut p, StorageTier::Ssd, StorageTier::Hdd, 0);
        assert!(p.start_task(id, 1));
        assert!(!p.start_task(id, 2));
    }

    #[test]
    fn start_task_blocked_by_pending_dependency() {
        let mut p = make_planner();
        let dep = add_simple(&mut p, StorageTier::Nvme, StorageTier::Ssd, 0);
        let child = p.plan_migration(
            2,
            "baf".into(),
            StorageTier::Ssd,
            StorageTier::Hdd,
            0,
            vec![dep],
        );
        assert!(!p.start_task(child, 0));
    }

    #[test]
    fn start_task_blocked_by_in_progress_dependency() {
        let mut p = make_planner();
        let dep = add_simple(&mut p, StorageTier::Nvme, StorageTier::Ssd, 0);
        let child = p.plan_migration(
            2,
            "baf".into(),
            StorageTier::Ssd,
            StorageTier::Hdd,
            0,
            vec![dep],
        );
        p.start_task(dep, 0);
        assert!(!p.start_task(child, 1));
    }

    #[test]
    fn start_task_unblocked_when_dependency_completes() {
        let mut p = make_planner();
        let dep = add_simple(&mut p, StorageTier::Nvme, StorageTier::Ssd, 0);
        let child = p.plan_migration(
            2,
            "baf".into(),
            StorageTier::Ssd,
            StorageTier::Hdd,
            0,
            vec![dep],
        );
        p.start_task(dep, 0);
        p.complete_task(dep, 1);
        assert!(p.start_task(child, 2));
    }

    // ── complete_task ────────────────────────────────────────────────────────

    #[test]
    fn complete_task_sets_completed() {
        let mut p = make_planner();
        let id = add_simple(&mut p, StorageTier::Ssd, StorageTier::Archive, 0);
        p.start_task(id, 0);
        assert!(p.complete_task(id, 10));
        let record = p.get_record(id).unwrap();
        assert_eq!(record.status, MigrationStatus::Completed);
        assert_eq!(record.completed_at_tick, Some(10));
    }

    #[test]
    fn complete_task_not_in_progress_returns_false() {
        let mut p = make_planner();
        let id = add_simple(&mut p, StorageTier::Ssd, StorageTier::Archive, 0);
        assert!(!p.complete_task(id, 1));
    }

    #[test]
    fn complete_task_unknown_id_returns_false() {
        let mut p = make_planner();
        assert!(!p.complete_task(999, 0));
    }

    // ── fail_task ────────────────────────────────────────────────────────────

    #[test]
    fn fail_task_from_pending() {
        let mut p = make_planner();
        let id = add_simple(&mut p, StorageTier::Nvme, StorageTier::Hdd, 0);
        assert!(p.fail_task(id));
        assert_eq!(p.get_record(id).unwrap().status, MigrationStatus::Failed);
    }

    #[test]
    fn fail_task_from_in_progress() {
        let mut p = make_planner();
        let id = add_simple(&mut p, StorageTier::Nvme, StorageTier::Hdd, 0);
        p.start_task(id, 0);
        assert!(p.fail_task(id));
        assert_eq!(p.get_record(id).unwrap().status, MigrationStatus::Failed);
    }

    #[test]
    fn fail_task_from_completed_returns_false() {
        let mut p = make_planner();
        let id = add_simple(&mut p, StorageTier::Nvme, StorageTier::Hdd, 0);
        p.start_task(id, 0);
        p.complete_task(id, 1);
        assert!(!p.fail_task(id));
    }

    #[test]
    fn fail_task_unknown_id_returns_false() {
        let mut p = make_planner();
        assert!(!p.fail_task(999));
    }

    // ── rollback_task ────────────────────────────────────────────────────────

    #[test]
    fn rollback_task_from_failed_succeeds() {
        let mut p = make_planner();
        let id = add_simple(&mut p, StorageTier::Nvme, StorageTier::Hdd, 0);
        p.fail_task(id);
        assert!(p.rollback_task(id));
        assert_eq!(
            p.get_record(id).unwrap().status,
            MigrationStatus::RolledBack
        );
    }

    #[test]
    fn rollback_task_from_pending_returns_false() {
        let mut p = make_planner();
        let id = add_simple(&mut p, StorageTier::Nvme, StorageTier::Hdd, 0);
        assert!(!p.rollback_task(id));
    }

    #[test]
    fn rollback_task_from_completed_returns_false() {
        let mut p = make_planner();
        let id = add_simple(&mut p, StorageTier::Nvme, StorageTier::Hdd, 0);
        p.start_task(id, 0);
        p.complete_task(id, 1);
        assert!(!p.rollback_task(id));
    }

    #[test]
    fn rollback_task_idempotency_returns_false() {
        // After RolledBack, a second rollback attempt must fail.
        let mut p = make_planner();
        let id = add_simple(&mut p, StorageTier::Nvme, StorageTier::Hdd, 0);
        p.fail_task(id);
        p.rollback_task(id);
        assert!(!p.rollback_task(id));
    }

    // ── ready_tasks ──────────────────────────────────────────────────────────

    #[test]
    fn ready_tasks_no_deps_all_pending() {
        let mut p = make_planner();
        let id0 = add_simple(&mut p, StorageTier::Nvme, StorageTier::Hdd, 0);
        let id1 = add_simple(&mut p, StorageTier::Ssd, StorageTier::Archive, 0);
        let ready = p.ready_tasks();
        assert_eq!(ready, vec![id0, id1]);
    }

    #[test]
    fn ready_tasks_sorted_ascending() {
        let mut p = make_planner();
        for _ in 0..5 {
            add_simple(&mut p, StorageTier::Nvme, StorageTier::Hdd, 0);
        }
        let ready = p.ready_tasks();
        let mut sorted = ready.clone();
        sorted.sort_unstable();
        assert_eq!(ready, sorted);
    }

    #[test]
    fn ready_tasks_excludes_in_progress() {
        let mut p = make_planner();
        let id0 = add_simple(&mut p, StorageTier::Nvme, StorageTier::Hdd, 0);
        let id1 = add_simple(&mut p, StorageTier::Ssd, StorageTier::Archive, 0);
        p.start_task(id0, 0);
        let ready = p.ready_tasks();
        assert_eq!(ready, vec![id1]);
    }

    #[test]
    fn ready_tasks_excludes_completed() {
        let mut p = make_planner();
        let id0 = add_simple(&mut p, StorageTier::Nvme, StorageTier::Hdd, 0);
        p.start_task(id0, 0);
        p.complete_task(id0, 1);
        assert!(p.ready_tasks().is_empty());
    }

    #[test]
    fn ready_tasks_with_dependency_chain() {
        let mut p = make_planner();
        let a = add_simple(&mut p, StorageTier::Nvme, StorageTier::Ssd, 0);
        let b = p.plan_migration(
            2,
            "b".into(),
            StorageTier::Ssd,
            StorageTier::Hdd,
            0,
            vec![a],
        );
        let c = p.plan_migration(
            3,
            "c".into(),
            StorageTier::Hdd,
            StorageTier::Archive,
            0,
            vec![b],
        );

        // Only `a` is ready initially.
        assert_eq!(p.ready_tasks(), vec![a]);

        p.start_task(a, 0);
        p.complete_task(a, 1);
        // Now `b` is ready.
        assert_eq!(p.ready_tasks(), vec![b]);

        p.start_task(b, 2);
        p.complete_task(b, 3);
        // Now `c` is ready.
        assert_eq!(p.ready_tasks(), vec![c]);
    }

    // ── stats ────────────────────────────────────────────────────────────────

    #[test]
    fn stats_counts_by_status() {
        let mut p = make_planner();
        let a = add_simple(&mut p, StorageTier::Nvme, StorageTier::Hdd, 1_000);
        let b = add_simple(&mut p, StorageTier::Ssd, StorageTier::Archive, 2_000);
        let c = add_simple(&mut p, StorageTier::Hdd, StorageTier::Nvme, 3_000);
        let d = add_simple(&mut p, StorageTier::Archive, StorageTier::Ssd, 500);

        p.start_task(a, 0);
        p.complete_task(a, 1);
        p.start_task(b, 2);
        p.fail_task(b);
        // c stays Pending; d stays Pending

        let s = p.stats();
        assert_eq!(s.total_tasks, 4);
        assert_eq!(s.completed, 1);
        assert_eq!(s.failed, 1);
        assert_eq!(s.pending, 2);
        assert_eq!(s.in_progress, 0);
        let _ = c;
        let _ = d;
    }

    #[test]
    fn stats_total_bytes_migrated_accumulates() {
        let mut p = make_planner();
        let a = add_simple(&mut p, StorageTier::Nvme, StorageTier::Hdd, 1_000_000);
        let b = add_simple(&mut p, StorageTier::Ssd, StorageTier::Archive, 2_000_000);

        p.start_task(a, 0);
        p.complete_task(a, 1);
        p.start_task(b, 2);
        p.complete_task(b, 3);

        assert_eq!(p.stats().total_bytes_migrated, 3_000_000);
    }

    #[test]
    fn stats_bytes_not_counted_for_failed() {
        let mut p = make_planner();
        let a = add_simple(&mut p, StorageTier::Nvme, StorageTier::Hdd, 5_000_000);
        p.start_task(a, 0);
        p.fail_task(a);

        assert_eq!(p.stats().total_bytes_migrated, 0);
    }

    #[test]
    fn stats_promotions_and_demotions() {
        let mut p = make_planner();
        // Demote
        let d0 = add_simple(&mut p, StorageTier::Nvme, StorageTier::Hdd, 0);
        let d1 = add_simple(&mut p, StorageTier::Ssd, StorageTier::Archive, 0);
        // Promote
        let p0 = add_simple(&mut p, StorageTier::Hdd, StorageTier::Ssd, 0);

        for id in [d0, d1, p0] {
            p.start_task(id, 0);
            p.complete_task(id, 1);
        }

        let s = p.stats();
        assert_eq!(s.demotions, 2);
        assert_eq!(s.promotions, 1);
    }

    #[test]
    fn stats_rolledback_counted_as_failed() {
        let mut p = make_planner();
        let id = add_simple(&mut p, StorageTier::Nvme, StorageTier::Hdd, 0);
        p.fail_task(id);
        p.rollback_task(id);

        let s = p.stats();
        assert_eq!(s.failed, 1);
        assert_eq!(s.completed, 0);
    }

    #[test]
    fn stats_empty_planner() {
        let p = make_planner();
        let s = p.stats();
        assert_eq!(s.total_tasks, 0);
        assert_eq!(s.pending, 0);
        assert_eq!(s.total_bytes_migrated, 0);
    }

    // ── get_record ───────────────────────────────────────────────────────────

    #[test]
    fn get_record_missing_returns_none() {
        let p = make_planner();
        assert!(p.get_record(0).is_none());
    }

    // ── edge cases ───────────────────────────────────────────────────────────

    #[test]
    fn multiple_deps_all_must_complete() {
        let mut p = make_planner();
        let a = add_simple(&mut p, StorageTier::Nvme, StorageTier::Ssd, 0);
        let b = add_simple(&mut p, StorageTier::Nvme, StorageTier::Hdd, 0);
        let c = p.plan_migration(
            10,
            "c".into(),
            StorageTier::Ssd,
            StorageTier::Archive,
            0,
            vec![a, b],
        );

        // Both a and b pending: c not ready.
        assert!(!p.ready_tasks().contains(&c));

        p.start_task(a, 0);
        p.complete_task(a, 1);
        // Only a done: c still not ready.
        assert!(!p.ready_tasks().contains(&c));

        p.start_task(b, 2);
        p.complete_task(b, 3);
        // Both done: c ready.
        assert!(p.ready_tasks().contains(&c));
    }

    #[test]
    fn default_impl_is_empty() {
        let p = StorageMigrationPlanner::default();
        assert_eq!(p.stats().total_tasks, 0);
    }
}