avio 0.17.0

Video and audio editing engine: build a Timeline of clips, edit with undo/redo, and render to a file
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
//! Undo/redo history over the immutable [`Timeline`] document.
//!
//! An [`Editor`] holds a linear history of `Timeline` versions and a cursor into
//! it. [`Editor::apply`] runs a [`Command`] (via the pure
//! [`apply`](crate::apply)) and pushes the resulting version;
//! [`Editor::undo`] / [`Editor::redo`] move the cursor. This is the stateful
//! Do/Undo/Redo layer on top of the value-based edit API; the model itself stays
//! immutable.
//!
//! History is stored as full snapshots — one `Timeline` clone per edit. This is
//! simple and correct but grows with the number of edits; structural-sharing /
//! diff-based history is a later optimisation (#1352).
//!
//! The id counters live here, outside the snapshotted `Timeline`, as a session
//! high-water mark: an `undo` restores an older snapshot (and its older
//! counters), so seating the high-water before each edit keeps a later
//! `AddClip` / `AddTrack` from re-minting an id a discarded branch already used.
//! This is what makes the "never reused" guarantee of ADR-0001
//! (`docs/adr/0001-clip-and-track-identity.md`) hold across undo.

use crate::edit::{Command, EditError};
use crate::timeline::Timeline;

/// A stateful editing session: an undo/redo history of [`Timeline`] versions.
///
/// The version at the internal cursor is the current one. [`apply`](Self::apply)
/// discards any redo tail and pushes a new version; [`undo`](Self::undo) /
/// [`redo`](Self::redo) move the cursor without dropping versions.
///
/// For a real host: [`amend`](Self::amend) / [`replace_current`](Self::replace_current)
/// update the current version in place **without** adding an undo step, and
/// [`begin_group`](Self::begin_group) … [`commit_group`](Self::commit_group)
/// coalesce a gesture's many edits into a single undo step.
#[derive(Debug)]
pub struct Editor {
    /// Version history; `history[cursor]` is current. Always non-empty.
    history: Vec<Timeline>,
    /// Index of the current version within `history`.
    cursor: usize,
    /// Session high-water for the next clip/track id, never rewound by `undo`.
    /// See the module docs.
    next_clip_id: u64,
    next_track_id: u64,
    /// An in-progress coalesced gesture, or `None`. See [`Editor::begin_group`].
    group: Option<Group>,
}

/// An in-progress coalesced gesture: edits fold into `working` until commit.
#[derive(Debug)]
struct Group {
    /// The evolving version, seeded from the current version at `begin_group`.
    working: Timeline,
    /// Whether any edit changed `working` (so `commit_group` knows to push a step).
    dirty: bool,
}

impl Editor {
    /// Starts an editing session at `initial`.
    ///
    /// This also seats an externally built or mutated [`Timeline`] as the current
    /// version: the session's id high-water starts from `initial`'s counters.
    #[must_use]
    pub fn new(initial: Timeline) -> Self {
        let next_clip_id = initial.next_clip_id;
        let next_track_id = initial.next_track_id;
        Self {
            history: vec![initial],
            cursor: 0,
            next_clip_id,
            next_track_id,
            group: None,
        }
    }

    /// The current [`Timeline`] version. While a group is active this is the
    /// gesture's in-progress version.
    #[must_use]
    pub fn current(&self) -> &Timeline {
        match &self.group {
            Some(g) => &g.working,
            None => &self.history[self.cursor],
        }
    }

    /// Applies `command` to the current version, seating the session id high-water
    /// first (so an edit after `undo` never re-mints a discarded id) and raising
    /// it from the result. Returns the new version. Does not touch history.
    fn edit_current(&mut self, command: &Command) -> Result<Timeline, EditError> {
        let mut seed = self.current().clone();
        seed.next_clip_id = self.next_clip_id;
        seed.next_track_id = self.next_track_id;
        let next = crate::edit::apply(&seed, command)?;
        self.next_clip_id = next.next_clip_id;
        self.next_track_id = next.next_track_id;
        Ok(next)
    }

    /// Applies `command` and makes the result current.
    ///
    /// Outside a group this pushes a new undo step (discarding any redo tail);
    /// inside a group it folds into the gesture's in-progress version instead. On
    /// an invalid edit nothing changes.
    ///
    /// # Errors
    ///
    /// Returns the [`EditError`] from [`apply`](crate::apply) when the command
    /// cannot be applied; no state is modified in that case.
    pub fn apply(&mut self, command: &Command) -> Result<&Timeline, EditError> {
        // Compute the new version first: if this fails, `?` returns before any
        // state mutation, so a rejected edit never disturbs the history or group.
        let next = self.edit_current(command)?;
        if let Some(g) = &mut self.group {
            g.working = next;
            g.dirty = true;
        } else {
            self.history.truncate(self.cursor + 1);
            self.history.push(next);
            self.cursor += 1;
        }
        Ok(self.current())
    }

    /// Applies `command` to the current version **in place**, without adding an
    /// undo step (for live drags / in-place refinement of the current version).
    ///
    /// # Errors
    ///
    /// Returns the [`EditError`] from [`apply`](crate::apply); nothing changes then.
    pub fn amend(&mut self, command: &Command) -> Result<&Timeline, EditError> {
        let next = self.edit_current(command)?;
        self.replace_current(next);
        Ok(self.current())
    }

    /// Seats `timeline` as the current version **in place**, without adding an undo
    /// step. Outside a group this replaces the current version and drops the redo
    /// tail; inside a group it replaces the gesture's in-progress version. The id
    /// high-water is raised to cover `timeline` (never rewound).
    pub fn replace_current(&mut self, timeline: Timeline) {
        self.next_clip_id = self.next_clip_id.max(timeline.next_clip_id);
        self.next_track_id = self.next_track_id.max(timeline.next_track_id);
        if let Some(g) = &mut self.group {
            g.working = timeline;
            g.dirty = true;
        } else {
            self.history.truncate(self.cursor + 1);
            self.history[self.cursor] = timeline;
        }
    }

    /// Begins coalescing subsequent edits into a single undo step.
    ///
    /// Until [`commit_group`](Self::commit_group) or [`cancel_group`](Self::cancel_group),
    /// [`apply`](Self::apply) folds into the gesture's in-progress version instead
    /// of pushing steps, and [`undo`](Self::undo) / [`redo`](Self::redo) are
    /// disabled. Groups do not nest: calling this while a group is active is a
    /// no-op.
    pub fn begin_group(&mut self) {
        if self.group.is_none() {
            self.group = Some(Group {
                // Equivalent to `history[cursor]` under this guard, but reads as
                // "seed from the current version" and cannot be confused with the
                // in-group case.
                working: self.current().clone(),
                dirty: false,
            });
        }
    }

    /// Ends the current group, pushing its accumulated edits as **one** undo step.
    /// A group with no edits produces no step. A no-op if no group is active.
    pub fn commit_group(&mut self) {
        if let Some(g) = self.group.take()
            && g.dirty
        {
            self.history.truncate(self.cursor + 1);
            self.history.push(g.working);
            self.cursor += 1;
        }
    }

    /// Discards the current group and its in-progress edits, leaving the history
    /// unchanged. A no-op if no group is active.
    pub fn cancel_group(&mut self) {
        self.group = None;
    }

    /// Moves back one version and returns it, or `None` at the start of history or
    /// while a group is active (commit or cancel the group first).
    pub fn undo(&mut self) -> Option<&Timeline> {
        if self.group.is_some() || self.cursor == 0 {
            return None;
        }
        self.cursor -= 1;
        Some(&self.history[self.cursor])
    }

    /// Moves forward one version and returns it, or `None` at the end of history or
    /// while a group is active.
    pub fn redo(&mut self) -> Option<&Timeline> {
        if self.group.is_some() || self.cursor + 1 >= self.history.len() {
            return None;
        }
        self.cursor += 1;
        Some(&self.history[self.cursor])
    }

    /// Whether [`undo`](Self::undo) would move to an earlier version (never while a
    /// group is active).
    #[must_use]
    pub fn can_undo(&self) -> bool {
        self.group.is_none() && self.cursor > 0
    }

    /// Whether [`redo`](Self::redo) would move to a later version (never while a
    /// group is active).
    #[must_use]
    pub fn can_redo(&self) -> bool {
        self.group.is_none() && self.cursor + 1 < self.history.len()
    }
}

#[cfg(test)]
#[allow(clippy::unwrap_used)]
mod tests {
    use super::*;
    use crate::Clip;

    fn timeline(fps: f64) -> Timeline {
        Timeline::builder()
            .canvas(1920, 1080)
            .frame_rate(fps)
            .video_track(vec![Clip::new("a.mp4")])
            .build()
            .unwrap()
    }

    fn set_fps(fps: f64) -> Command {
        Command::SetFrameRate { fps }
    }

    #[test]
    fn editor_new_should_start_with_no_undo_or_redo() {
        let ed = Editor::new(timeline(30.0));
        assert!(!ed.can_undo());
        assert!(!ed.can_redo());
        assert!((ed.current().frame_rate() - 30.0).abs() < f64::EPSILON);
    }

    #[test]
    fn editor_apply_should_advance_and_enable_undo() {
        let mut ed = Editor::new(timeline(30.0));
        let cur = ed.apply(&set_fps(24.0)).unwrap();
        assert!((cur.frame_rate() - 24.0).abs() < f64::EPSILON);
        assert!(ed.can_undo());
        assert!(!ed.can_redo());
    }

    #[test]
    fn editor_undo_should_restore_previous_version() {
        let mut ed = Editor::new(timeline(30.0));
        ed.apply(&set_fps(24.0)).unwrap();
        let prev = ed.undo().unwrap();
        assert!((prev.frame_rate() - 30.0).abs() < f64::EPSILON);
        assert!(!ed.can_undo());
        assert!(ed.can_redo());
    }

    #[test]
    fn editor_undo_redo_should_round_trip_an_added_marker() {
        use crate::Marker;
        use std::time::Duration;

        let mut ed = Editor::new(timeline(30.0));
        let after = ed
            .apply(&Command::AddMarker {
                marker: Marker::new(Duration::from_secs(1)),
            })
            .unwrap();
        assert_eq!(after.markers().len(), 1);

        let undone = ed.undo().unwrap();
        assert!(undone.markers().is_empty(), "undo removes the added marker");

        let redone = ed.redo().unwrap();
        assert_eq!(redone.markers().len(), 1, "redo restores the marker");
    }

    #[test]
    fn editor_undo_should_restore_all_grouped_members_in_one_step() {
        use std::time::Duration;

        let base = Timeline::builder()
            .canvas(1920, 1080)
            .frame_rate(30.0)
            .video_track(vec![
                Clip::new("a.mp4"),
                Clip::new("b.mp4").offset(Duration::from_secs(10)),
            ])
            .build()
            .unwrap();
        let a = base.video_tracks()[0].clips[0].id;
        let b = base.video_tracks()[0].clips[1].id;
        let mut ed = Editor::new(base);
        ed.apply(&Command::GroupClips { clips: vec![a, b] })
            .unwrap();
        let moved = ed
            .apply(&Command::MoveClip {
                clip: a,
                offset: Duration::from_secs(5),
            })
            .unwrap();
        assert_eq!(
            moved.video_tracks()[0].clips[0].offset,
            Duration::from_secs(5)
        );
        assert_eq!(
            moved.video_tracks()[0].clips[1].offset,
            Duration::from_secs(15)
        );

        // A single undo reverts the whole grouped move (both members restored).
        let undone = ed.undo().unwrap();
        assert_eq!(undone.video_tracks()[0].clips[0].offset, Duration::ZERO);
        assert_eq!(
            undone.video_tracks()[0].clips[1].offset,
            Duration::from_secs(10)
        );

        let redone = ed.redo().unwrap();
        assert_eq!(
            redone.video_tracks()[0].clips[1].offset,
            Duration::from_secs(15)
        );
    }

    #[test]
    fn editor_redo_should_reapply_the_undone_version() {
        let mut ed = Editor::new(timeline(30.0));
        ed.apply(&set_fps(24.0)).unwrap();
        ed.undo().unwrap();
        let next = ed.redo().unwrap();
        assert!((next.frame_rate() - 24.0).abs() < f64::EPSILON);
        assert!(!ed.can_redo());
    }

    #[test]
    fn editor_undo_at_start_should_return_none() {
        let mut ed = Editor::new(timeline(30.0));
        assert!(ed.undo().is_none());
        assert!((ed.current().frame_rate() - 30.0).abs() < f64::EPSILON);
    }

    #[test]
    fn editor_redo_at_end_should_return_none() {
        let mut ed = Editor::new(timeline(30.0));
        ed.apply(&set_fps(24.0)).unwrap();
        assert!(ed.redo().is_none());
    }

    #[test]
    fn editor_new_edit_after_undo_should_truncate_redo() {
        let mut ed = Editor::new(timeline(30.0));
        ed.apply(&set_fps(24.0)).unwrap();
        ed.apply(&set_fps(48.0)).unwrap();
        ed.undo().unwrap(); // back to the 24.0 version; 48.0 is now the redo tail
        let cur = ed.apply(&set_fps(60.0)).unwrap(); // must discard the 48.0 redo
        assert!((cur.frame_rate() - 60.0).abs() < f64::EPSILON);
        assert!(!ed.can_redo());
        assert!(ed.redo().is_none());
    }

    #[test]
    fn editor_apply_error_should_leave_history_unchanged() {
        let mut ed = Editor::new(timeline(30.0));
        ed.apply(&set_fps(24.0)).unwrap();
        let err = ed.apply(&set_fps(0.0)).unwrap_err(); // invalid: fps <= 0
        assert_eq!(err, EditError::InvalidFrameRate(0.0));
        // History untouched: still one edit deep, no redo, current is the 24.0 version.
        assert!(ed.can_undo());
        assert!(!ed.can_redo());
        assert!((ed.current().frame_rate() - 24.0).abs() < f64::EPSILON);
    }

    #[test]
    fn editor_should_preserve_clip_id_across_undo_redo() {
        let mut ed = Editor::new(timeline(30.0));
        let track = ed.current().video_tracks()[0].id;
        ed.apply(&Command::AddClip {
            track,
            clip: Box::new(Clip::new("x.mp4")),
        })
        .unwrap();
        let id = ed.current().video_tracks()[0].clips[1].id;
        ed.undo().unwrap();
        let after = ed.redo().unwrap();
        assert_eq!(
            after.video_tracks()[0].clips[1].id,
            id,
            "undo/redo must not renumber the clip"
        );
    }

    #[test]
    fn editor_should_not_reuse_ids_across_undo() {
        let mut ed = Editor::new(timeline(30.0));
        let track = ed.current().video_tracks()[0].id;
        ed.apply(&Command::AddClip {
            track,
            clip: Box::new(Clip::new("a.mp4")),
        })
        .unwrap();
        let first = ed.current().video_tracks()[0].clips[1].id;
        // Discard that edit; without the session high-water the counter would
        // rewind and the next AddClip would re-mint `first` for a different clip.
        ed.undo().unwrap();
        let after = ed
            .apply(&Command::AddClip {
                track,
                clip: Box::new(Clip::new("b.mp4")),
            })
            .unwrap();
        let second = after.video_tracks()[0].clips[1].id;
        assert_ne!(
            first, second,
            "an id used by a discarded branch must not be reused"
        );
    }

    #[test]
    fn editor_apply_batch_should_be_one_undo_step() {
        let mut ed = Editor::new(timeline(30.0));
        let track = ed.current().video_tracks()[0].id;
        ed.apply(&Command::Batch(vec![
            Command::AddClip {
                track,
                clip: Box::new(Clip::new("a.mp4")),
            },
            Command::AddClip {
                track,
                clip: Box::new(Clip::new("b.mp4")),
            },
        ]))
        .unwrap();
        assert_eq!(ed.current().video_tracks()[0].clips.len(), 3);
        // One undo removes the whole batch.
        let prev = ed.undo().unwrap();
        assert_eq!(prev.video_tracks()[0].clips.len(), 1);
    }

    #[test]
    fn editor_group_should_coalesce_to_one_undo_step() {
        let mut ed = Editor::new(timeline(30.0));
        ed.begin_group();
        ed.apply(&set_fps(24.0)).unwrap();
        ed.apply(&set_fps(48.0)).unwrap();
        assert!(
            (ed.current().frame_rate() - 48.0).abs() < f64::EPSILON,
            "the working version reflects grouped edits live"
        );
        ed.commit_group();
        assert!((ed.current().frame_rate() - 48.0).abs() < f64::EPSILON);
        // One undo step: undo restores the pre-gesture version (30), not 24.
        let prev = ed.undo().unwrap();
        assert!((prev.frame_rate() - 30.0).abs() < f64::EPSILON);
        assert!(!ed.can_undo());
    }

    #[test]
    fn editor_empty_group_should_add_no_step() {
        let mut ed = Editor::new(timeline(30.0));
        ed.apply(&set_fps(24.0)).unwrap();
        ed.begin_group();
        ed.commit_group(); // no edits in the group
        assert!(!ed.can_redo());
        // Only the single real edit remains: one undo returns to 30.
        let prev = ed.undo().unwrap();
        assert!((prev.frame_rate() - 30.0).abs() < f64::EPSILON);
        assert!(!ed.can_undo());
    }

    #[test]
    fn editor_cancel_group_should_discard_edits() {
        let mut ed = Editor::new(timeline(30.0));
        ed.begin_group();
        ed.apply(&set_fps(24.0)).unwrap();
        ed.cancel_group();
        assert!(
            (ed.current().frame_rate() - 30.0).abs() < f64::EPSILON,
            "cancel reverts to the pre-group version"
        );
        assert!(!ed.can_undo(), "cancel added no step");
    }

    #[test]
    fn editor_undo_redo_should_be_disabled_during_a_group() {
        let mut ed = Editor::new(timeline(30.0));
        ed.apply(&set_fps(24.0)).unwrap();
        ed.begin_group();
        ed.apply(&set_fps(48.0)).unwrap();
        assert!(!ed.can_undo());
        assert!(ed.undo().is_none());
        assert!(ed.redo().is_none());
        ed.commit_group();
        // After commit, undo works again and restores the pre-gesture version.
        assert!(ed.can_undo());
        let prev = ed.undo().unwrap();
        assert!((prev.frame_rate() - 24.0).abs() < f64::EPSILON);
    }

    #[test]
    fn editor_amend_should_update_current_without_growing_history() {
        let mut ed = Editor::new(timeline(30.0));
        ed.apply(&set_fps(24.0)).unwrap(); // step 1
        ed.amend(&set_fps(48.0)).unwrap(); // in place: no new step
        assert!((ed.current().frame_rate() - 48.0).abs() < f64::EPSILON);
        assert!(ed.can_undo());
        assert!(!ed.can_redo());
        // One undo goes past the amended value to the pre-step-1 version (30).
        let prev = ed.undo().unwrap();
        assert!((prev.frame_rate() - 30.0).abs() < f64::EPSILON);
        assert!(!ed.can_undo());
    }

    #[test]
    fn editor_replace_current_should_seat_value_and_drop_redo() {
        let mut ed = Editor::new(timeline(30.0));
        ed.apply(&set_fps(24.0)).unwrap();
        ed.apply(&set_fps(48.0)).unwrap();
        ed.undo().unwrap(); // back to 24, redo tail = [48]
        assert!(ed.can_redo());
        ed.replace_current(timeline(60.0));
        assert!((ed.current().frame_rate() - 60.0).abs() < f64::EPSILON);
        assert!(!ed.can_redo(), "seating a value drops the redo tail");
    }

    #[test]
    fn editor_group_should_keep_ids_monotonic() {
        let mut ed = Editor::new(timeline(30.0));
        let track = ed.current().video_tracks()[0].id;
        ed.begin_group();
        ed.apply(&Command::AddClip {
            track,
            clip: Box::new(Clip::new("a.mp4")),
        })
        .unwrap();
        ed.apply(&Command::AddClip {
            track,
            clip: Box::new(Clip::new("b.mp4")),
        })
        .unwrap();
        ed.commit_group();
        let clips = &ed.current().video_tracks()[0].clips;
        assert_eq!(clips.len(), 3);
        assert_ne!(clips[1].id, clips[2].id);
    }

    #[test]
    fn editor_amend_during_group_should_fold_into_the_gesture() {
        let mut ed = Editor::new(timeline(30.0));
        ed.begin_group();
        ed.apply(&set_fps(24.0)).unwrap();
        ed.amend(&set_fps(48.0)).unwrap(); // folds into the working version, not a step
        assert!((ed.current().frame_rate() - 48.0).abs() < f64::EPSILON);
        ed.commit_group();
        // Still one undo step: undo restores the pre-gesture version.
        let prev = ed.undo().unwrap();
        assert!((prev.frame_rate() - 30.0).abs() < f64::EPSILON);
        assert!(!ed.can_undo());
    }

    #[test]
    fn editor_nested_begin_group_should_not_restart_the_gesture() {
        let mut ed = Editor::new(timeline(30.0));
        ed.begin_group();
        ed.apply(&set_fps(24.0)).unwrap(); // working now at 24
        ed.begin_group(); // no-op: must not reseed working from history (still 30)
        assert!(
            (ed.current().frame_rate() - 24.0).abs() < f64::EPSILON,
            "a second begin_group must not discard in-progress edits"
        );
        ed.commit_group();
        let prev = ed.undo().unwrap();
        assert!((prev.frame_rate() - 30.0).abs() < f64::EPSILON);
    }

    #[test]
    fn editor_set_clip_should_be_undoable() {
        let mut ed = Editor::new(timeline(30.0));
        let id = ed.current().video_tracks()[0].clips[0].id;
        let mut patch = Clip::new("patched.mp4");
        patch.brightness = 0.5;
        ed.apply(&Command::SetClip {
            clip: id,
            value: Box::new(patch),
        })
        .unwrap();
        assert_eq!(
            ed.current().video_tracks()[0].clips[0]
                .source_path()
                .and_then(std::path::Path::to_str),
            Some("patched.mp4")
        );
        let prev = ed.undo().unwrap();
        assert_eq!(
            prev.video_tracks()[0].clips[0]
                .source_path()
                .and_then(std::path::Path::to_str),
            Some("a.mp4"),
            "undo restores the pre-patch clip"
        );
    }

    #[test]
    fn editor_split_clip_should_be_undoable() {
        use std::time::Duration;
        let mut ed = Editor::new(timeline(30.0));
        let id = ed.current().video_tracks()[0].clips[0].id;
        ed.apply(&Command::SplitClip {
            clip: id,
            at: Duration::from_secs(1),
        })
        .unwrap();
        assert_eq!(ed.current().video_tracks()[0].clips.len(), 2);
        let prev = ed.undo().unwrap();
        assert_eq!(
            prev.video_tracks()[0].clips.len(),
            1,
            "undo restores the single clip"
        );
    }

    #[test]
    fn editor_move_clip_to_track_should_be_undoable() {
        use std::time::Duration;
        let t = Timeline::builder()
            .canvas(1920, 1080)
            .frame_rate(30.0)
            .video_track(vec![Clip::new("a.mp4")])
            .video_track(vec![])
            .build()
            .unwrap();
        let mut ed = Editor::new(t);
        let clip_id = ed.current().video_tracks()[0].clips[0].id;
        let to = ed.current().video_tracks()[1].id;
        ed.apply(&Command::MoveClipToTrack {
            clip: clip_id,
            to,
            offset: Duration::from_secs(2),
        })
        .unwrap();
        assert!(ed.current().video_tracks()[0].clips.is_empty());
        let prev = ed.undo().unwrap();
        assert_eq!(
            prev.video_tracks()[0].clips.len(),
            1,
            "undo restores the clip on the original track"
        );
    }

    #[test]
    fn editor_ripple_delete_should_be_one_undoable_step() {
        use std::time::Duration;
        let a = Clip::new("a.mp4")
            .trim(Duration::ZERO, Duration::from_secs(4))
            .offset(Duration::ZERO);
        let b = Clip::new("b.mp4").offset(Duration::from_secs(4));
        let t = Timeline::builder()
            .canvas(1920, 1080)
            .frame_rate(30.0)
            .video_track(vec![a, b])
            .build()
            .unwrap();
        let mut ed = Editor::new(t);
        let a_id = ed.current().video_tracks()[0].clips[0].id;
        ed.apply(&Command::RippleDelete { clip: a_id }).unwrap();
        assert_eq!(ed.current().video_tracks()[0].clips.len(), 1);
        assert_eq!(
            ed.current().video_tracks()[0].clips[0].offset,
            Duration::ZERO,
            "b shifted left to close the gap"
        );
        // One undo restores both the clip and the pre-ripple offsets.
        let prev = ed.undo().unwrap();
        assert_eq!(prev.video_tracks()[0].clips.len(), 2);
        assert_eq!(
            prev.video_tracks()[0].clips[1].offset,
            Duration::from_secs(4)
        );
    }
}