diff --git a/crates/engine/src/sweep.rs b/crates/engine/src/sweep.rs index ef25da0..7e8fef9 100644 --- a/crates/engine/src/sweep.rs +++ b/crates/engine/src/sweep.rs @@ -1,7 +1,19 @@ -//! The re-timing sweep: every mutating op, against every grouping shape, -//! under the universal laws. The "there might be other bugs" catcher -- a -//! red cell here is an op whose shape has drifted from the law, found -//! before a user finds it. +//! The re-timing sweep: every op that moves or removes timeline time, against +//! every grouping shape, under the universal laws -- `delete_in`/`delete_sub_in`, +//! `set_speed`/`set_speed_live`, `split`/`regroup`, `move_clip`/`trim`, +//! `move_sub`/`trim_sub`, `paste`, `ripple_delete`/`cut_regions`, +//! `lift`/`lift_sub`, `place`/`place_take`/`place_sub`, `delete(idx)` and +//! `retime`. The "there might be other bugs" catcher -- a red cell here is an +//! op whose shape has drifted from the law, found before a user finds it. +//! +//! Left out, by name rather than by omission: `group`/`ungroup`/`group_all` +//! move no time at all (id bookkeeping alone -- their own law is +//! `links_are_consistent`, asked directly by their unit tests and by +//! [`group_all_refuses_a_merge_that_doubles_up_a_lane`](crate::project::tests::group_all_refuses_a_merge_that_doubles_up_a_lane)), +//! and `remove_lane`/`move_lane`/`remove_source`/`remove_subtitles`/`append_clip` +//! touch the lane *list* or the source table, never a placement's span -- +//! outside this module's law and covered by their own unit tests in +//! `project.rs`. //! //! The laws are the four nothing downstream can live without: //! @@ -21,7 +33,7 @@ //! so its cells assert the no-move shape directly instead of through a map. use crate::map::TimelineMap; -use crate::project::{Lane, LaneKind, Speed, SubClip}; +use crate::project::{Edge, Lane, LaneKind, Rate, Speed, SubClip}; use crate::scale::FitPolicy; use crate::subtitle::SubtitleTrack; use crate::{Clip, Project}; @@ -151,17 +163,30 @@ fn the_sweep_delete_from_every_anchor() { assert!(deleted, "{shape}/{anchor}: the delete happened"); laws(&p, &format!("{shape}/{anchor} after delete")); if shape == "ungrouped" { - // The lone-clip law: the span leaves EVERY lane. - assert!(p.lane(Lane::A1).is_empty() || p.lane(Lane::V1).is_empty(), "{shape}/{anchor}"); + // `Project::single` links its V1 and A1 clips from the start + // (the source is grouped with itself), so the lone-clip law + // really is a conjunction: the span leaves EVERY lane. + assert!(p.lane(Lane::A1).is_empty() && p.lane(Lane::V1).is_empty(), "{shape}/{anchor}"); } else { // The group law: every member gone, from every anchor. assert!(p.lane(Lane::V1).is_empty(), "{shape}/{anchor}: V1 emptied"); assert!(p.lane(Lane::A1).is_empty(), "{shape}/{anchor}: A1 emptied"); if let Some(s1) = s1 { - assert!( - p.sub_lane(s1).is_empty() || p.sub_lane(s1).iter().all(|s| s.start >= 60), - "{shape}/{anchor}: the caption gone with the group" - ); + // Only "aligned"'s one caption is grouped; "offset" + // groups just its first caption, leaving the ungrouped + // follower to close up behind the hole the delete cuts. + match shape { + "aligned" => assert!( + p.sub_lane(s1).is_empty(), + "{shape}/{anchor}: the caption gone with the group" + ), + "offset" => assert_eq!( + p.sub_lane(s1).iter().map(|s| (s.start, s.end())).collect::>(), + vec![(60, 90)], + "{shape}/{anchor}: the grouped caption gone, the follower closed up behind it" + ), + _ => unreachable!("the sweep names three grouped shapes with captions"), + } } } undo_round_trip(p, pre, &format!("{shape}/{anchor} delete")); @@ -291,3 +316,178 @@ fn the_sweep_paste_is_the_named_exception() { undo_round_trip(p, pre, &format!("{shape} paste")); } } + +/// The scoped and whole-timeline ripples: [`Project::ripple_delete`] and +/// [`Project::cut_regions`] both close a hole across every lane they touch, +/// clips and captions alike -- a span safely inside every shape's geometry +/// (never on a clip or caption edge, so the cell asks the general law and not +/// one shape's particular boundary). +#[test] +fn the_sweep_ripple_delete_and_cut_regions_close_every_lane() { + for shape in SHAPES { + let (mut p, _s1) = project(shape); + let pre = p.parts(); + assert!(p.ripple_delete(70, 10), "{shape}: the ripple cuts"); + laws(&p, &format!("{shape} after ripple_delete")); + undo_round_trip(p, pre, &format!("{shape} ripple_delete")); + + let (mut p, _s1) = project(shape); + let pre = p.parts(); + assert!( + p.cut_regions(&[(70, 10)], &p.lanes()).is_ok(), + "{shape}: the scoped cut lands" + ); + laws(&p, &format!("{shape} after cut_regions")); + undo_round_trip(p, pre, &format!("{shape} cut_regions")); + } +} + +/// The two single-lane, group-blind lifts: [`Project::lift`] and +/// [`Project::lift_sub`] take one placement off its own lane and leave a gap +/// -- no other lane moves, grouped or not, which the law asks by checking +/// every OTHER lane is untouched. +#[test] +fn the_sweep_lift_and_lift_sub_leave_a_gap_alone() { + for shape in SHAPES { + let (mut p, s1) = project(shape); + let before_a1 = p.lane(Lane::A1).to_vec(); + let pre = p.parts(); + assert!(p.lift(Lane::V1, 0), "{shape}: the lift takes V1's clip"); + assert!(p.lane(Lane::V1).is_empty(), "{shape}: V1 left a gap"); + assert_eq!(p.lane(Lane::A1).to_vec(), before_a1, "{shape}: A1 never asked"); + laws(&p, &format!("{shape} after lift")); + undo_round_trip(p, pre, &format!("{shape} lift")); + + let Some(s1) = s1 else { continue }; + let (mut p, s1) = (project(shape).0, s1); + let before_v1 = p.lane(Lane::V1).to_vec(); + let pre = p.parts(); + assert!(p.lift_sub(s1, 0), "{shape}: the lift takes the caption"); + assert_eq!(p.lane(Lane::V1).to_vec(), before_v1, "{shape}: V1 never asked"); + laws(&p, &format!("{shape} after lift_sub")); + undo_round_trip(p, pre, &format!("{shape} lift_sub")); + } +} + +/// The caption's own two edits: [`Project::move_sub`] carries its group's +/// clips along it, one delta for all of them (exactly as a clip's +/// [`Project::move_clip`] does), and [`Project::trim_sub`] drags its group's +/// edge along with its own window, one delta each member clamps to its own +/// room (`Project::follow_group`) -- the same law, not a caption exception. +#[test] +fn the_sweep_move_sub_and_trim_sub_keep_their_own_law() { + for shape in ["aligned", "offset"] { + let (mut p, s1) = project(shape); + let s1 = s1.expect("aligned and offset both carry a caption"); + let pre = p.parts(); + let v1_before = p.lane(Lane::V1)[0]; + let sub_before = p.sub_lane(s1)[0].start; + let to = sub_before + 5; + assert!(p.move_sub(s1, 0, s1, to).is_ok(), "{shape}: room to drag"); + laws(&p, &format!("{shape} after move_sub")); + let v1_after = p.lane(Lane::V1)[0]; + assert_eq!( + i64::from(v1_after.start) - i64::from(v1_before.start), + i64::from(p.sub_lane(s1)[0].start) - i64::from(sub_before), + "{shape}: the grouped clip rode the same delta as the caption" + ); + undo_round_trip(p, pre, &format!("{shape} move_sub")); + + // A caption in a group drags its group's edge along with its own + // (`Project::follow_group`) -- exactly like `move_sub` above, not the + // "own window alone" case this cell first assumed. + let (mut p, s1) = project(shape); + let s1 = s1.unwrap(); + let pre = p.parts(); + let end = p.sub_lane(s1)[0].end(); + assert!( + p.trim_sub(s1, 0, Edge::End, end - 5, 30.0).is_ok(), + "{shape}: room to trim" + ); + laws(&p, &format!("{shape} after trim_sub")); + undo_round_trip(p, pre, &format!("{shape} trim_sub")); + } +} + +/// The three overwrite doors -- [`Project::place`], [`Project::place_take`] +/// and [`Project::place_sub`] -- are [`the_sweep_paste_is_the_named_exception`]'s +/// own shape: they land over whatever is there and nothing else on the lane +/// ripples, media or captions. +#[test] +fn the_sweep_place_doors_overwrite_without_rippling() { + for shape in SHAPES { + let (mut p, _s1) = project(shape); + let pre = p.parts(); + assert!(p.place(Lane::V1, 10, clip(10, 0, 20)), "{shape}: place lands"); + laws(&p, &format!("{shape} after place")); + undo_round_trip(p, pre, &format!("{shape} place")); + + let (mut p, _s1) = project(shape); + // `add_lane` costs no undo step of its own (it is metadata until + // something places on it), so `pre` is read after it -- the lane + // list itself is part of what undo restores. + let v2 = p.add_lane(LaneKind::Video); + let pre = p.parts(); + assert!(p.place_take(v2, 10, clip(10, 0, 20)), "{shape}: place_take lands"); + laws(&p, &format!("{shape} after place_take")); + undo_round_trip(p, pre, &format!("{shape} place_take")); + + let (mut p, _s1) = project(shape); + let s1 = p.add_lane(LaneKind::Subtitle); + let pre = p.parts(); + assert!(p.place_sub(s1, 5, caption(5, 10)).is_ok(), "{shape}: place_sub lands"); + laws(&p, &format!("{shape} after place_sub")); + undo_round_trip(p, pre, &format!("{shape} place_sub")); + } +} + +/// [`Project::delete`] is sugar for [`Project::delete_in`] on `V1` -- the +/// single-lane front-end door the app's own delete key is wired to. One cell +/// pinning the delegation rather than the whole delete sweep again. +#[test] +fn the_sweep_delete_by_index_is_delete_in_on_v1() { + let (mut a, _) = project("clips"); + let (mut b, _) = project("clips"); + assert_eq!(a.delete(0), b.delete_in(Lane::V1, 0)); + laws(&a, "delete(idx)"); + assert_eq!(a.parts(), b.parts(), "delete(idx) and delete_in(V1, idx) agree"); +} + +/// [`Project::set_speed_live`] is the drag sample of [`Project::set_speed`]: +/// every sample only regrades the rate, and the whole gesture undoes in the +/// one step the press took ([`a_whole_colour_drag_undoes_in_one_step`]'s law, +/// for a rate instead of a grade). +#[test] +fn the_sweep_speed_live_drags_in_one_step() { + for shape in SHAPES { + let (mut p, _s1) = project(shape); + let pre = p.parts(); + assert!(p.set_speed(Lane::V1, 0, Speed::from_permille(1100)).is_ok(), "{shape}: the press"); + for permille in [1200, 1300, 1400] { + assert!( + p.set_speed_live(Lane::V1, 0, Speed::from_permille(permille)).is_ok(), + "{shape}: a live sample" + ); + laws(&p, &format!("{shape} after a live speed sample")); + } + undo_round_trip(p, pre, &format!("{shape} speed drag")); + } +} + +/// [`Project::retime`] moves every frame number without being an edit anyone +/// made ([`Project::retime`]'s own words) -- no history step to its name, so +/// the sweep asks it the order and consistency laws alone, never the undo +/// round trip the rest of this module leans on. +#[test] +fn the_sweep_retime_keeps_the_lanes_ordered() { + for shape in SHAPES { + let (mut p, _s1) = project(shape); + let steps = p.history_len(); + // Old timeline frames per new one: 1:2 doubles every span, a slow-down + // wide enough that the geometry the sweep builds still fits. + let k = Rate::from_fps(1.0, 2.0).expect("a namable rate"); + p.retime(k, &[10_000]); + assert_eq!(p.history_len(), steps, "retime costs no undo step"); + laws(&p, &format!("{shape} after retime")); + } +}