GitCorner

Code

ossartcrafteffectcraft🌐
665bfac0b339
//! Headless checks for the Composition viewer's interaction parity: bottom bar order, region of
//! interest drawing, rulers and guides, snapping, the shape Pen and motion-path key drags
//! (egui_kittest, UI logic only).

use crate::gpu_test::RenderSerial;
use effectcraft_engine::Session;
use effectcraft_engine::commands::shape_tool::PaintKind;
use effectcraft_engine::project::LayerId;
use effectcraft_ui_egui::EffectcraftApp;
use effectcraft_ui_egui::state::Tool;
use egui::{Event, Pos2, Rect, pos2, vec2};
use egui_kittest::Harness;
use egui_kittest::kittest::Queryable;
use serde_json::json;

fn app() -> EffectcraftApp {
    let mut s = Session::default();
    s.execute("comp.new", json!({"name": "View", "width": 640, "height": 360, "duration": 4})).unwrap();
    s.execute("layer.newSolid", json!({"name": "Plate", "color": "#406080", "width": 640, "height": 360})).unwrap();
    s.execute("layer.newSolid", json!({"name": "Box", "color": "#e04020", "width": 80, "height": 80})).unwrap();
    s.execute("edit.deselectAll", json!({})).unwrap();
    EffectcraftApp::new(s)
}

fn harness() -> Harness<'static, EffectcraftApp> {
    let mut h = Harness::builder().with_size(egui::vec2(1600.0, 1000.0)).build_eframe(|_| app());
    h.run_steps(3);
    h
}

fn rect(h: &Harness<'_, EffectcraftApp>, id: &str) -> Rect {
    let e = h.state().auto.find(id).unwrap_or_else(|| panic!("no {id}"));
    Rect::from_min_size(pos2(e.rect[0], e.rect[1]), vec2(e.rect[2], e.rect[3]))
}

fn click(h: &mut Harness<'_, EffectcraftApp>, p: Pos2) {
    h.input_mut().events.push(Event::PointerMoved(p));
    h.step();
    h.input_mut().events.push(Event::PointerButton { pos: p, button: egui::PointerButton::Primary, pressed: true, modifiers: Default::default() });
    h.step();
    h.input_mut().events.push(Event::PointerButton { pos: p, button: egui::PointerButton::Primary, pressed: false, modifiers: Default::default() });
    h.run_steps(2);
}

fn drag(h: &mut Harness<'_, EffectcraftApp>, from: Pos2, to: Pos2) {
    h.input_mut().events.push(Event::PointerMoved(from));
    h.input_mut().events.push(Event::PointerButton { pos: from, button: egui::PointerButton::Primary, pressed: true, modifiers: Default::default() });
    h.step();
    for i in 1..=8 {
        h.input_mut().events.push(Event::PointerMoved(from + (to - from) * (i as f32 / 8.0)));
        h.step();
    }
    h.input_mut().events.push(Event::PointerButton { pos: to, button: egui::PointerButton::Primary, pressed: false, modifiers: Default::default() });
    h.run_steps(2);
}

/// #1218: frame stepping is optional, and consumes the whole wheel gesture including its
/// smoothed tail. Ctrl/Cmd+wheel still zooms.
#[test]
fn mouse_wheel_over_the_monitor_scrubs_frames() {
    let mut h = harness();
    h.state_mut().session.execute("prefs.set", json!({"key": "general.viewerWheelScrubbing", "value": true})).unwrap();
    let p = rect(&h, "viewer.area").center();
    h.input_mut().events.push(Event::PointerMoved(p));
    h.step();
    let t0 = h.state().session.time();
    let line = |dy: f32, modifiers: egui::Modifiers| Event::MouseWheel {
        unit: egui::MouseWheelUnit::Line,
        delta: vec2(0.0, dy),
        modifiers,
        phase: egui::TouchPhase::Move,
        source: egui::MouseWheelSource::Wheel,
    };
    let z0 = h.state().ui.viewer.zoom;
    h.input_mut().events.push(line(1.0, egui::Modifiers::NONE));
    h.step();
    let t1 = h.state().session.time();
    for _ in 0..20 {
        assert_eq!(h.state().ui.viewer.zoom, z0, "frame wheel must never zoom");
        h.step();
    }
    assert!(t1 > t0, "wheel up advanced the playhead ({t0:?} -> {t1:?})");
    h.input_mut().events.push(line(-2.0, egui::Modifiers::NONE));
    h.step();
    assert!(h.state().session.time() < t1, "two lines back moves before the single line forward");
    let z0 = h.state().ui.viewer.zoom;
    let t2 = h.state().session.time();
    let mods = egui::Modifiers { ctrl: true, command: true, ..Default::default() };
    h.input_mut().events.push(line(3.0, mods));
    h.step();
    assert_eq!(h.state().session.time(), t2, "Ctrl+wheel must not scrub");
    assert_ne!(h.state().ui.viewer.zoom, z0, "Ctrl+wheel still zooms");
}

#[test]
fn mouse_wheel_zooms_by_default_without_stepping_frames() {
    let mut h = harness();
    assert!(!h.state().session.prefs.general.viewer_wheel_scrubbing);
    let p = rect(&h, "viewer.area").center();
    h.input_mut().events.push(Event::PointerMoved(p));
    h.step();
    let t0 = h.state().session.time();
    let z0 = h.state().ui.viewer.zoom;
    h.input_mut().events.push(Event::MouseWheel {
        unit: egui::MouseWheelUnit::Line,
        delta: vec2(0.0, 1.0),
        modifiers: egui::Modifiers::NONE,
        phase: egui::TouchPhase::Move,
        source: egui::MouseWheelSource::Wheel,
    });
    h.run_steps(20);
    assert_eq!(h.state().session.time(), t0);
    assert_ne!(h.state().ui.viewer.zoom, z0);
}

/// Comp pixel → screen point.
fn screen(h: &Harness<'_, EffectcraftApp>, p: [f32; 2]) -> Pos2 {
    let c = rect(h, "viewer.comp");
    let z = c.width() / 640.0;
    c.min + vec2(p[0] * z, p[1] * z)
}

fn selection_harness() -> Harness<'static, EffectcraftApp> {
    let mut s = Session::default();
    s.execute("comp.new", json!({"name": "Selection", "width": 640, "height": 360, "duration": 4})).unwrap();
    for name in ["Back", "Front"] {
        s.execute("layer.newSolid", json!({"name": name, "color": "#406080", "width": 80, "height": 80})).unwrap();
    }
    s.execute("edit.deselectAll", json!({})).unwrap();
    let mut h = Harness::builder().with_size(egui::vec2(1600.0, 1000.0)).build_eframe(move |_| EffectcraftApp::new(s));
    h.run_steps(3);
    h
}

#[test]
fn viewer_click_skips_hidden_and_unsoloed_layers() {
    for switch in ["video", "solo"] {
        let mut h = selection_harness();
        let comp = h.state().session.active_comp().unwrap();
        let (front, back) = (comp.layers[0].id, comp.layers[1].id);
        let at = screen(&h, [320.0, 180.0]);
        click(&mut h, at);
        assert_eq!(h.state().session.state.selected_layers, vec![front]);
        let s = &mut h.state_mut().session;
        let (target, value) = if switch == "video" { (front, false) } else { (back, true) };
        s.execute("layer.setSwitch", json!({"layers": [target.0], "switch": switch, "value": value})).unwrap();
        s.execute("edit.deselectAll", json!({})).unwrap();
        h.run_steps(40); // Start a new click rather than a double-click.
        click(&mut h, at);
        assert_eq!(h.state().session.state.selected_layers, vec![back], "{switch}");
    }
}

#[test]
fn viewer_marquee_skips_hidden_and_unsoloed_layers() {
    for switch in ["video", "solo"] {
        let mut h = selection_harness();
        let comp = h.state().session.active_comp().unwrap();
        let (front, back) = (comp.layers[0].id, comp.layers[1].id);
        let (target, value) = if switch == "video" { (front, false) } else { (back, true) };
        h.state_mut().session.execute("layer.setSwitch", json!({"layers": [target.0], "switch": switch, "value": value})).unwrap();
        h.run_steps(3);
        let (from, to) = (screen(&h, [250.0, 110.0]), screen(&h, [390.0, 250.0]));
        drag(&mut h, from, to);
        assert_eq!(h.state().session.state.selected_layers, vec![back], "{switch}");
    }
}

#[test]
fn viewer_click_does_not_pick_through_hidden_or_locked_solo_layers() {
    for switch in ["video", "lock"] {
        let mut h = selection_harness();
        let front = h.state().session.active_comp().unwrap().layers[0].id;
        let s = &mut h.state_mut().session;
        s.execute("layer.setSwitch", json!({"layers": [front.0], "switch": "solo", "value": true})).unwrap();
        s.execute("layer.setSwitch", json!({"layers": [front.0], "switch": switch, "value": switch == "lock"})).unwrap();
        h.run_steps(3);
        let at = screen(&h, [320.0, 180.0]);
        click(&mut h, at);
        assert!(h.state().session.state.selected_layers.is_empty(), "{switch}");
    }
}

/// Solo is not time-dependent (as in After Effects): outside the soloed layer's in-out range the
/// viewer shows nothing, so neither the soloed layer nor the others can be picked.
#[test]
fn viewer_click_picks_nothing_while_the_solo_layer_is_inactive() {
    let mut h = selection_harness();
    let comp = h.state().session.active_comp().unwrap();
    let (front, back) = (comp.layers[0].id, comp.layers[1].id);
    let s = &mut h.state_mut().session;
    s.execute("layer.setSwitch", json!({"layers": [front.0], "switch": "solo", "value": true})).unwrap();
    let cid = s.active_comp_id().unwrap();
    std::sync::Arc::make_mut(&mut s.project).comp_mut(cid).unwrap().layer_mut(front).unwrap().in_point = effectcraft_engine::time::Tick::from_seconds_f64(1.0);
    h.run_steps(3);
    let at = screen(&h, [320.0, 180.0]);
    click(&mut h, at);
    assert!(h.state().session.state.selected_layers.is_empty(), "{back:?} stays excluded by solo");
}

/// A click, left or right, selects the topmost layer under the pointer, even over a selected
/// layer behind it (#1535); a drag still moves a selected layer behind others (#230).
#[test]
fn viewer_click_selects_the_topmost_layer_and_a_drag_moves_the_selected_one() {
    let mut h = selection_harness();
    let comp = h.state().session.active_comp().unwrap();
    let (front, back) = (comp.layers[0].id, comp.layers[1].id);
    h.state_mut().session.execute("layer.select", json!({"layers": [back.0]})).unwrap();
    h.run_steps(3);
    let at = screen(&h, [320.0, 180.0]);
    click(&mut h, at);
    assert_eq!(h.state().session.state.selected_layers, vec![front], "a click selects the layer in front of the selected one (#1535)");
    // A right-click selects what a left click would, so the menu acts on the layer in front,
    // here where both layers cover the pointer and the layer behind is selected.
    h.state_mut().session.execute("layer.select", json!({"layers": [back.0]})).unwrap();
    h.run_steps(3);
    for pressed in [true, false] {
        h.input_mut().events.push(Event::PointerButton { pos: at, button: egui::PointerButton::Secondary, pressed, modifiers: Default::default() });
        h.step();
    }
    h.run_steps(2);
    assert_eq!(h.state().session.state.selected_layers, vec![front], "a right-click selects the layer in front");
    h.key_press(egui::Key::Escape);
    h.run_steps(3);
    h.state_mut().session.execute("layer.select", json!({"layers": [back.0]})).unwrap();
    h.run_steps(40);
    let (front0, back0) = (position_and_anchor(&h, front).0, position_and_anchor(&h, back).0);
    drag(&mut h, at, at + vec2(60.0, 0.0));
    assert_eq!(h.state().session.state.selected_layers, vec![back]);
    assert_eq!(position_and_anchor(&h, front).0, front0, "the layer in front stays");
    assert!(position_and_anchor(&h, back).0[0] > back0[0] + 10.0, "the selected layer behind moves");
    h.state_mut().session.execute("edit.deselectAll", json!({})).unwrap();
    h.run_steps(40);
    let at = screen(&h, [300.0, 180.0]);
    click(&mut h, at);
    assert_eq!(h.state().session.state.selected_layers, vec![front], "nothing selected: the topmost layer");
}

/// A click, left or right, on a path of the selected layer, where a layer in front covers it, stays
/// on that path's layer rather than selecting the layer in front, so a double-click there
/// free-transforms the path (#1535 keeps this from `pick`).
#[test]
fn viewer_double_click_on_a_covered_path_of_the_selected_layer_free_transforms_it() {
    let mut h = selection_harness();
    let comp = h.state().session.active_comp().unwrap();
    let back = comp.layers[1].id;
    let s = &mut h.state_mut().session;
    s.execute("layer.select", json!({"layers": [back.0]})).unwrap();
    // In Back's own space (80×80): its top edge runs through comp (320, 150), under Front.
    s.execute("mask.new", json!({"layer": back.0, "vertices": [[10, 10], [70, 10], [70, 70], [10, 70]], "closed": true})).unwrap();
    h.run_steps(40);
    let at = screen(&h, [320.0, 150.0]);
    h.input_mut().events.push(Event::PointerMoved(at));
    h.step();
    // A right-click there acts on the path's layer too, as a left click does.
    for pressed in [true, false] {
        h.input_mut().events.push(Event::PointerButton { pos: at, button: egui::PointerButton::Secondary, pressed, modifiers: Default::default() });
        h.step();
    }
    h.run_steps(2);
    assert_eq!(h.state().session.state.selected_layers, vec![back], "a right-click on the path keeps its layer");
    h.key_press(egui::Key::Escape);
    h.run_steps(40);
    // Two clicks a frame apart: the first selects as a click does, the second double-clicks.
    for _ in 0..2 {
        for pressed in [true, false] {
            h.input_mut().events.push(Event::PointerButton { pos: at, button: egui::PointerButton::Primary, pressed, modifiers: Default::default() });
        }
        h.step();
    }
    h.run_steps(3);
    assert_eq!(h.state().session.state.selected_layers, vec![back], "the path's layer stays selected");
    assert_eq!(h.state().session.state.selected_vertices.len(), 4, "all the path's points are selected");
    assert!(h.state().auto.find("viewer.freeTransform.handle.0").is_some(), "the free-transform box is up");
}

/// A double-click where a precomp layer covers the selected layer opens the precomp: like a single
/// click, it acts on the topmost layer, which its first click selects (#1535).
#[test]
fn viewer_double_click_opens_the_topmost_layer_over_a_selected_one() {
    let mut s = Session::default();
    let inner = s.execute("comp.new", json!({"name": "Inner", "width": 80, "height": 80, "duration": 4})).unwrap()["comp"].as_u64().unwrap();
    s.execute("comp.new", json!({"name": "Main", "width": 640, "height": 360, "duration": 4})).unwrap();
    s.execute("layer.newSolid", json!({"name": "Back", "color": "#406080", "width": 200, "height": 200})).unwrap();
    s.execute("layer.addItem", json!({"item": "Inner"})).unwrap();
    let back = s.active_comp().unwrap().layers[1].id;
    s.execute("layer.select", json!({"layers": [back.0]})).unwrap();
    let mut h = Harness::builder().with_size(egui::vec2(1600.0, 1000.0)).build_eframe(move |_| EffectcraftApp::new(s));
    h.run_steps(40);
    let at = screen(&h, [320.0, 180.0]);
    h.input_mut().events.push(Event::PointerMoved(at));
    h.step();
    // Two clicks a frame apart: the first selects as a click does, the second double-clicks.
    for _ in 0..2 {
        for pressed in [true, false] {
            h.input_mut().events.push(Event::PointerButton { pos: at, button: egui::PointerButton::Primary, pressed, modifiers: Default::default() });
        }
        h.step();
    }
    h.run_steps(3);
    assert_eq!(h.state().session.active_comp_id().map(|c| c.0), Some(inner), "the precomp in front opens");
}

/// Each composition keeps its Resolution, saved with the project as in After Effects: chosen for
/// comp A in the viewer's popup, it is still A's after showing comp B, costs no undo step, keeps
/// A's cached frames and is read back from the saved file (#417).
#[test]
fn each_comp_keeps_its_viewer_resolution() {
    use effectcraft_ui_egui::state::Resolution;
    let mut h = harness();
    let a = h.state().session.active_comp_id().unwrap();
    let content = |h: &Harness<'_, EffectcraftApp>, c| {
        let s = h.state();
        s.frames.content_of(&s.session.project, s.session.revision, c)
    };
    let (before, steps) = (content(&h, a), h.state().session.history.undo.len());
    for id in ["viewer.resolution", "viewer.resolutionItem.1"] {
        let at = rect(&h, id).center();
        click(&mut h, at);
    }
    assert_eq!(h.state().viewer_res(), Resolution::Half);
    assert_eq!(h.state().session.project.comp(a).unwrap().resolution, Resolution::Half);
    assert_eq!(h.state().session.history.undo.len(), steps, "no undo step");
    assert_eq!(content(&h, a), before, "the comp's cached frames stay valid");
    // Comp B starts at Auto and gets its own (here through the control channel's `ui.set`).
    h.state_mut().session.execute("comp.new", json!({"name": "Other", "width": 320, "height": 180, "duration": 2})).unwrap();
    h.run_steps(3);
    let b = h.state().session.active_comp_id().unwrap();
    assert_ne!(a, b);
    assert_eq!(h.state().viewer_res(), Resolution::Auto);
    let (req, _reply) = effectcraft_ui_egui::control::ControlRequest::new("ui.set", json!({"viewer": {"res": "quarter"}}));
    let ctx = h.ctx.clone();
    let now = h.state().now();
    let _ = effectcraft_ui_egui::control::handle(h.state_mut(), &ctx, &req, now);
    assert_eq!(h.state().viewer_res(), Resolution::Quarter);
    let ctx = h.ctx.clone();
    assert_eq!(effectcraft_ui_egui::control::inspect(h.state(), &ctx)["ui"]["viewer"]["res"], json!("Quarter"));
    // Back to comp A: its resolution again.
    h.state_mut().session.open_comp(a);
    h.run_steps(3);
    assert_eq!(h.state().viewer_res(), Resolution::Half);
    assert!(h.state().viewer_scale(1.0, 1.0) == 0.5);
    // Saved and opened again.
    let path = std::env::temp_dir().join(format!("ec-viewer-res-{}.ecproj", std::process::id()));
    h.state_mut().session.execute("file.saveAs", json!({"path": path.to_string_lossy()})).unwrap();
    let mut s = Session::default();
    s.execute("file.open", json!({"path": path.to_string_lossy()})).unwrap();
    let _ = std::fs::remove_file(&path);
    s.open_comp(b);
    let app = EffectcraftApp::new(s);
    assert_eq!(app.viewer_res(), Resolution::Quarter);
    assert_eq!(app.session.project.comp(a).unwrap().resolution, Resolution::Half);
}

#[test]
fn bottom_bar_in_after_effects_order() {
    let h = harness();
    let order = [
        "viewer.magnification",
        "viewer.resolution",
        "viewer.fastPreviews",
        "viewer.transparency",
        "viewer.masks",
        "viewer.roi",
        "viewer.grid",
        "viewer.channel",
        "viewer.resetExposure",
        "viewer.exposure",
        "viewer.snapshot",
        "viewer.showSnapshot",
        "viewer.timecode",
    ];
    let xs: Vec<f32> = order.iter().map(|id| rect(&h, id).min.x).collect();
    assert!(xs.windows(2).all(|w| w[0] < w[1]), "{xs:?}");
    let mag = &h.state().auto.find("viewer.magnification").unwrap().label;
    assert_eq!(mag, "Magnification");
}

#[test]
fn region_of_interest_is_drawn_in_the_viewer() {
    let mut h = harness();
    let at = rect(&h, "viewer.roi").center();
    click(&mut h, at);
    assert!(h.state().ui.viewer.roi_draw);
    let (a, b) = (screen(&h, [100.0, 50.0]), screen(&h, [300.0, 250.0]));
    drag(&mut h, a, b);
    let r = h.state().session.state.region_of_interest.expect("roi");
    assert!((r[0] - 100.0).abs() <= 2.0 && (r[1] - 50.0).abs() <= 2.0 && (r[2] - 200.0).abs() <= 3.0 && (r[3] - 200.0).abs() <= 3.0, "{r:?}");
    assert!(!h.state().ui.viewer.roi_draw);
    h.run_steps(3);
    assert!(h.state().auto.find("viewer.regionOfInterest").is_some());
    // The button clears it again.
    let at = rect(&h, "viewer.roi").center();
    click(&mut h, at);
    assert!(h.state().session.state.region_of_interest.is_none());
}

#[test]
fn rulers_make_guides_and_guides_move() {
    let mut h = harness();
    h.state_mut().ui.viewer.rulers = true;
    h.run_steps(3);
    let top = rect(&h, "viewer.ruler.top");
    let to = screen(&h, [0.0, 120.0]);
    drag(&mut h, pos2(to.x + 200.0, top.center().y), pos2(to.x + 200.0, to.y));
    let g = h.state().session.active_comp().unwrap().guides.clone();
    assert_eq!(g.len(), 1);
    assert!(!g[0].vertical && (g[0].position - 120.0).abs() <= 2.0, "{g:?}");
    // Drag the guide down with the Selection tool.
    let from = pos2(screen(&h, [500.0, 0.0]).x, screen(&h, [0.0, g[0].position as f32]).y);
    let to2 = pos2(from.x, screen(&h, [0.0, 200.0]).y);
    drag(&mut h, from, to2);
    let g = h.state().session.active_comp().unwrap().guides.clone();
    assert!((g[0].position - 200.0).abs() <= 2.0, "{g:?}");
}

#[test]
fn layer_drag_snaps_to_comp_centre_and_ctrl_disables() {
    let mut h = harness();
    let box_id = h.state().session.active_comp().unwrap().layers[0].id;
    h.state_mut().session.execute("prop.set", json!({"layer": box_id.0, "path": "transform/position", "value": [100, 100, 0]})).unwrap();
    h.run_steps(2);
    // Grab the box at its centre and drop it 3 px from the comp centre: it snaps there.
    let from = screen(&h, [100.0, 100.0]);
    let to = screen(&h, [323.0, 182.0]);
    drag(&mut h, from, to);
    let pos = |h: &Harness<'_, EffectcraftApp>| {
        let l = h.state().session.active_comp().unwrap().layer(box_id).unwrap().clone();
        l.props.prop("transform/position").unwrap().value.as_vec3()
    };
    let p = pos(&h);
    assert!((p[0] - 320.0).abs() < 0.01 && (p[1] - 180.0).abs() < 0.01, "{p:?}");
    // Snapping off: the same drag lands where the pointer is.
    h.state_mut().session.execute("view.snapping", json!({"value": false})).unwrap();
    h.state_mut().session.execute("prop.set", json!({"layer": box_id.0, "path": "transform/position", "value": [100, 100, 0]})).unwrap();
    h.run_steps(2);
    drag(&mut h, from, to);
    let p = pos(&h);
    assert!((p[0] - 320.0).abs() > 1.0, "{p:?}");
}

/// #253: a dragged layer's feature nearest the pointer snaps to other layers' edges; Cmd/Ctrl
/// held during the drag turns snapping on while the Snapping checkbox is off (and off while it
/// is on), and the Tools bar's Snapping options turn Snap Edges Extended off, so an edge only
/// snaps along the layer.
#[test]
fn layer_drag_snaps_to_other_layers_edges_ctrl_toggles_and_options_apply() {
    let mut h = harness();
    let box_id = h.state().session.active_comp().unwrap().layers[0].id;
    // A 100×100 target at x 450–550, y 50–150.
    let target = h.state_mut().session.execute("layer.newSolid", json!({"name": "Target", "color": "#20e040", "width": 100, "height": 100})).unwrap()["layer"]
        .as_u64()
        .unwrap();
    let place = |h: &mut Harness<'_, EffectcraftApp>| {
        let s = &mut h.state_mut().session;
        s.execute("prop.set", json!({"layer": target, "path": "transform/position", "value": [500, 100, 0]})).unwrap();
        s.execute("prop.set", json!({"layer": box_id.0, "path": "transform/position", "value": [100, 250, 0]})).unwrap();
        s.execute("edit.deselectAll", json!({})).unwrap();
        h.run_steps(2);
    };
    let pos = |h: &Harness<'_, EffectcraftApp>| {
        let l = h.state().session.active_comp().unwrap().layer(box_id).unwrap().clone();
        l.props.prop("transform/position").unwrap().value.as_vec3()
    };
    // Grab the box by its top right corner (140, 210) and drop it 3 px left of the target's
    // left edge, well below the target: the corner lands on the edge's line (x 450).
    let ctrl = egui::Modifiers { ctrl: true, command: true, ..Default::default() };
    let drop = |h: &mut Harness<'_, EffectcraftApp>, mods: egui::Modifiers| {
        place(h);
        let (from, to) = (screen(h, [138.0, 212.0]), screen(h, [445.0, 232.0]));
        hold_drag(h, from, to, mods);
        pos(h)
    };
    let snapped = |p: [f64; 3]| (p[0] - 410.0).abs() < 0.01;
    let p = drop(&mut h, Default::default());
    assert!(snapped(p), "snapping on: {p:?}");
    let p = drop(&mut h, ctrl);
    assert!(!snapped(p), "Ctrl turns it off: {p:?}");
    h.state_mut().session.execute("view.snapping", json!({"value": false})).unwrap();
    let p = drop(&mut h, Default::default());
    assert!(!snapped(p), "snapping off: {p:?}");
    let p = drop(&mut h, ctrl);
    assert!(snapped(p), "Ctrl turns it on: {p:?}");
    h.state_mut().session.execute("view.snapping", json!({"value": true})).unwrap();
    // Snapping options ▸ Snap Edges Extended off: below the target its edge no longer snaps.
    let menu = rect(&h, "header.snappingOptions").center();
    click(&mut h, menu);
    // After Effects' two options, without per-feature toggles.
    assert!(h.query_by_label("✓ Snap to Features in Collapsed Compositions and Text Layers").is_some());
    for gone in ["✓ Corners", "✓ Anchor Points", "✓ Mask and Shape Path Points"] {
        assert!(h.query_by_label(gone).is_none(), "{gone}");
    }
    let item = h.query_by_label("✓ Snap Edges Extended").expect("the Snapping options menu").rect().center();
    click(&mut h, item);
    assert!(!h.state().session.state.snap_features.edges_extended);
    let p = drop(&mut h, Default::default());
    assert!(!snapped(p), "edges not extended: {p:?}");
}

/// #2855: a dragged layer's edge midpoints snap to another layer's on both axes, side by side and
/// stacked, with Snap Edges Extended on and off.
#[test]
fn layer_drag_snaps_to_other_layers_midpoints() {
    let mut h = harness();
    let box_id = h.state().session.active_comp().unwrap().layers[0].id;
    // A 100×100 target at x 450–550, y 50–150, centre (500, 100).
    let target = h.state_mut().session.execute("layer.newSolid", json!({"name": "Target", "color": "#20e040", "width": 100, "height": 100})).unwrap()["layer"]
        .as_u64()
        .unwrap();
    // Grab the 80×80 box (at x 60–140, y 210–290) by the feature at `grab` and take that feature
    // to `to`: returns where the box's centre lands.
    let drop = |h: &mut Harness<'_, EffectcraftApp>, grab: [f32; 2], to: [f32; 2]| {
        let s = &mut h.state_mut().session;
        s.execute("prop.set", json!({"layer": target, "path": "transform/position", "value": [500, 100, 0]})).unwrap();
        s.execute("prop.set", json!({"layer": box_id.0, "path": "transform/position", "value": [100, 250, 0]})).unwrap();
        s.execute("edit.deselectAll", json!({})).unwrap();
        h.run_steps(2);
        let (from, to) = (screen(h, grab), screen(h, to));
        hold_drag(h, from, to, Default::default());
        let l = h.state().session.active_comp().unwrap().layer(box_id).unwrap().clone();
        let p = l.props.prop("transform/position").unwrap().value.as_vec3();
        [p[0], p[1]]
    };
    let near = |p: [f64; 2], x: f64, y: f64| (p[0] - x).abs() < 0.01 && (p[1] - y).abs() < 0.01;
    for extended in [true, false] {
        h.state_mut().session.execute("view.snappingOptions", json!({"edgesExtended": extended})).unwrap();
        // Side by side: the box's left midpoint onto the target's right midpoint (550, 100).
        let p = drop(&mut h, [60.5, 250.0], [553.0, 102.0]);
        assert!(near(p, 590.0, 100.0), "left to right midpoint, extended {extended}: {p:?}");
        // Stacked: the box's top midpoint onto the target's bottom midpoint (500, 150).
        let p = drop(&mut h, [100.0, 210.5], [502.0, 153.0]);
        assert!(near(p, 500.0, 190.0), "top to bottom midpoint, extended {extended}: {p:?}");
    }
}

/// #3229: a layer Shift-dragged along x past another layer's corner a few pixels off the axis
/// lines up with the corner's x and stays on the axis (it used to jump onto the corner), with
/// Snap Edges Extended on and off.
#[test]
fn shift_drag_snaps_a_layer_along_its_axis_only() {
    let mut h = harness();
    let box_id = h.state().session.active_comp().unwrap().layers[0].id;
    // A 100×100 target at x 450–550, y 50–150: its bottom left corner at (450, 150).
    let target = h.state_mut().session.execute("layer.newSolid", json!({"name": "Target", "color": "#20e040", "width": 100, "height": 100})).unwrap()["layer"]
        .as_u64()
        .unwrap();
    for extended in [true, false] {
        // The 80×80 box at x 60–140, y 153–233: its top right corner (140, 153) 3 px below the
        // target's corner.
        let s = &mut h.state_mut().session;
        s.execute("view.snappingOptions", json!({"edgesExtended": extended})).unwrap();
        s.execute("prop.set", json!({"layer": target, "path": "transform/position", "value": [500, 100, 0]})).unwrap();
        s.execute("prop.set", json!({"layer": box_id.0, "path": "transform/position", "value": [100, 193, 0]})).unwrap();
        s.execute("edit.deselectAll", json!({})).unwrap();
        h.run_steps(2);
        // Grab the box by that corner and Shift-drag it right (and a pixel down), the corner to
        // 2 px short of the target corner's x.
        let (from, to) = (screen(&h, [138.0, 155.0]), screen(&h, [446.0, 156.0]));
        hold_drag(&mut h, from, to, egui::Modifiers::SHIFT);
        let l = h.state().session.active_comp().unwrap().layer(box_id).unwrap().clone();
        let p = l.props.prop("transform/position").unwrap().value.as_vec3();
        assert!((p[0] - 410.0).abs() < 0.01, "the corner lines up with the target's, extended {extended}: {p:?}");
        assert!((p[1] - 193.0).abs() < 1e-6, "and the box stays on the axis, extended {extended}: {p:?}");
    }
}

/// With a shape layer selected the Rectangle tool draws a new group into its Contents (After
/// Effects' behaviour); with nothing selected it draws a new shape layer (#227).
#[test]
fn shape_tool_draws_into_the_selected_shape_layer() {
    let mut h = harness();
    let l = h.state_mut().session.execute("layer.newShape", json!({"kind": "rect", "size": [100, 100], "position": [320, 180]})).unwrap()["layer"]
        .as_u64()
        .unwrap();
    h.state_mut().ui.tool = Tool::Rectangle;
    h.run_steps(2);
    let n0 = h.state().session.active_comp().unwrap().layers.len();
    let (a, b) = (screen(&h, [100.0, 100.0]), screen(&h, [200.0, 160.0]));
    drag(&mut h, a, b);
    let comp = h.state().session.active_comp().unwrap().clone();
    assert_eq!(comp.layers.len(), n0, "no new layer");
    let contents = comp.layer(LayerId(l)).unwrap().props.sub("contents").unwrap().clone();
    let names: Vec<&str> = contents.groups().map(|g| g.name.as_str()).collect();
    assert_eq!(names, ["Rectangle 2", "Rectangle 1"]);
    // Placed where it was drawn (centred near comp (150, 130)), in the layer's space: the layer
    // sits at the comp centre.
    let g = contents.groups().next().unwrap();
    let at = g.sub("transform").unwrap().get("position").unwrap().value.components();
    assert!((at[0] + 170.0).abs() < 8.0 && (at[1] + 50.0).abs() < 8.0, "{at:?}");
    // Nothing selected: a new shape layer.
    h.state_mut().session.execute("edit.deselectAll", json!({})).unwrap();
    let (a, b) = (screen(&h, [400.0, 250.0]), screen(&h, [500.0, 300.0]));
    drag(&mut h, a, b);
    let comp = h.state().session.active_comp().unwrap();
    assert_eq!(comp.layers.len(), n0 + 1);
    assert!(matches!(comp.layers[0].source, effectcraft_engine::project::LayerSource::Shape));
    assert_ne!(comp.layers[0].id, LayerId(l));
}

/// With a shape layer selected and a shape tool active, the Tools bar's Tool Creates Mask makes
/// the tool draw a mask on the layer (and hides Fill and Stroke); Tool Creates Shape draws
/// shapes again (#227).
#[test]
fn tool_creates_mask_draws_a_mask_on_the_selected_shape_layer() {
    let mut h = harness();
    let l = h.state_mut().session.execute("layer.newShape", json!({"kind": "rect", "size": [100, 100], "position": [320, 180]})).unwrap()["layer"]
        .as_u64()
        .unwrap();
    h.state_mut().ui.tool = Tool::Star;
    h.run_steps(2);
    assert!(h.state().auto.find("header.fill").is_some());
    let at = rect(&h, "header.createsMask").center();
    click(&mut h, at);
    assert!(h.state().session.state.shape_tool.creates_mask);
    assert!(h.state().auto.find("header.fill").is_none(), "no Fill or Stroke for masks");
    let (a, b) = (screen(&h, [100.0, 100.0]), screen(&h, [200.0, 200.0]));
    drag(&mut h, a, b);
    let layer = h.state().session.active_comp().unwrap().layer(LayerId(l)).unwrap().clone();
    let masks = layer.props.sub("masks").unwrap();
    assert_eq!(masks.groups().count(), 1, "a mask");
    assert_eq!(masks.groups().next().unwrap().get("path").unwrap().value.as_path().unwrap().vertices.len(), 10, "a star");
    assert_eq!(layer.props.sub("contents").unwrap().groups().count(), 1, "no new shape");
    let at = rect(&h, "header.createsShape").center();
    click(&mut h, at);
    assert!(!h.state().session.state.shape_tool.creates_mask);
}

// ---- the shape tools' drag ghost

/// Run frames until the viewer's background frame has landed, so the comp is painted (and not
/// still black) before the drag begins: the ghost is then the only thing that changes.
fn settle_viewer(h: &mut Harness<'_, EffectcraftApp>) {
    for _ in 0..600 {
        h.step();
        if h.state().frames.inflight() == 0 && h.state().frames.last_ms.lock().map(|v| *v > 0.0).unwrap_or(false) {
            break;
        }
        std::thread::sleep(std::time::Duration::from_millis(10));
    }
    h.run_steps(4);
}

/// Press at `from`, drag to `to` with `mods` held and return the frame drawn with the button still
/// down — what the user sees mid-drag, before anything is committed. egui only takes modifiers
/// from [`egui::Event::ModifiersChanged`], so they are set before the press and stay set.
fn mid_drag(h: &mut Harness<'_, EffectcraftApp>, from: Pos2, to: Pos2, mods: egui::Modifiers) -> image::RgbaImage {
    h.input_mut().events.push(Event::ModifiersChanged(mods));
    h.input_mut().events.push(Event::PointerMoved(from));
    h.input_mut().events.push(Event::PointerButton { pos: from, button: egui::PointerButton::Primary, pressed: true, modifiers: mods });
    h.step();
    for i in 1..=8 {
        h.input_mut().events.push(Event::PointerMoved(from + (to - from) * (i as f32 / 8.0)));
        h.step();
    }
    h.render_serialized().expect("the mid-drag frame")
}

/// Release the button where the drag left it.
fn release(h: &mut Harness<'_, EffectcraftApp>, at: Pos2, mods: egui::Modifiers) {
    h.input_mut().events.push(Event::PointerButton { pos: at, button: egui::PointerButton::Primary, pressed: false, modifiers: mods });
    h.run_steps(2);
}

/// The pixel at a comp point of a rendered frame.
fn px_at(img: &image::RgbaImage, h: &Harness<'_, EffectcraftApp>, p: [f32; 2]) -> [u8; 3] {
    let s = screen(h, p);
    let p = img.get_pixel(s.x.round() as u32, s.y.round() as u32).0;
    [p[0], p[1], p[2]]
}

/// How different two pixels are, summed over the channels.
fn delta(a: [u8; 3], b: [u8; 3]) -> u32 {
    (0..3).map(|i| (a[i] as i32 - b[i] as i32).unsigned_abs()).sum()
}

/// The Tools-bar Fill as bytes, the colour a filled ghost carries.
fn fill_rgb(h: &Harness<'_, EffectcraftApp>) -> [u8; 3] {
    let c = h.state().session.state.shape_tool.fill.color;
    std::array::from_fn(|i| (c[i].clamp(0.0, 1.0) * 255.0).round() as u8)
}

/// The shape tools show the shape they will draw under the cursor while the drag is still going:
/// filled with the Tools bar's Fill, so it reads at any zoom where the 1 px outline it replaces was
/// invisible, and in the tool's own form — only the Rectangle fills the corner of the box.
#[test]
fn shape_drag_ghost_shows_the_tools_own_shape_while_dragging() {
    let (from, to) = ([140.0, 100.0], [400.0, 300.0]);
    let centre = [270.0, 200.0];
    // 4 comp px inside the box's top-left corner: inside a rectangle, outside every other kind
    // (a rounded corner of 20 px arcs past this point).
    let corner = [144.0, 104.0];
    for (tool, fills_corner) in [(Tool::Rectangle, true), (Tool::RoundedRect, false), (Tool::Ellipse, false), (Tool::Polygon, false), (Tool::Star, false)] {
        let mut h = harness();
        h.state_mut().ui.tool = tool;
        settle_viewer(&mut h);
        let (a, b) = (screen(&h, from), screen(&h, to));
        let before = h.render_serialized().expect("the frame before the drag");
        let mid = mid_drag(&mut h, a, b, egui::Modifiers::default());
        let fill = fill_rgb(&h);

        // Drawn while the button is down, and painted with the Fill rather than left to a hairline.
        let was = px_at(&before, &h, centre);
        let now = px_at(&mid, &h, centre);
        assert!(delta(was, now) > 30, "{tool:?}: no ghost drawn mid-drag ({was:?} → {now:?})");
        assert!(delta(now, fill) < delta(was, fill), "{tool:?}: the ghost is not the Fill colour ({now:?} vs {was:?})");

        // The tool's own form: only the Rectangle reaches the corner of its box.
        let was_c = px_at(&before, &h, corner);
        let now_c = px_at(&mid, &h, corner);
        assert_eq!(delta(was_c, now_c) > 30, fills_corner, "{tool:?}: the ghost is not drawn in its own shape");

        release(&mut h, b, egui::Modifiers::default());
    }
}

/// The ghost and the shape the release commits share one normalisation, so nothing it shows is a
/// lie: Shift squares the box and Ctrl/Cmd draws it from the press point (as in After Effects), both live.
#[test]
fn shape_drag_ghost_normalises_the_same_way_as_the_release() {
    let from = [140.0f64, 100.0];
    let to = [400.0f64, 260.0];
    // (mods, centre, size): Shift squares the box from its top-left, Ctrl/Cmd centres it on the press.
    for (mods, centre, size) in [
        (egui::Modifiers::default(), [270.0, 180.0], [260.0, 160.0]),
        (egui::Modifiers::SHIFT, [270.0, 230.0], [260.0, 260.0]),
        (egui::Modifiers::COMMAND, [140.0, 100.0], [520.0, 320.0]),
        (egui::Modifiers { shift: true, ..egui::Modifiers::COMMAND }, [140.0, 100.0], [520.0, 520.0]),
    ] {
        let mut h = harness();
        h.state_mut().ui.tool = Tool::Ellipse;
        settle_viewer(&mut h);
        let (a, b) = (screen(&h, from.map(|v| v as f32)), screen(&h, to.map(|v| v as f32)));
        let before = h.render_serialized().expect("the frame before the drag");
        // Measured with the button down, so this is the ghost and not the committed shape.
        let mid = mid_drag(&mut h, a, b, mods);
        let on = [centre[0] as f32 + size[0] as f32 / 4.0, centre[1] as f32 + size[1] as f32 / 4.0];
        assert!(delta(px_at(&before, &h, on), px_at(&mid, &h, on)) > 30, "{mods:?}: the ghost is not where it will land");
        release(&mut h, b, mods);

        // And the shape it commits is that box: a new shape layer carries the position, its
        // group sits at its own centre's origin.
        let comp = h.state().session.active_comp().unwrap().clone();
        let layer = comp.layers.first().expect("a new shape layer").clone();
        assert!(matches!(layer.source, effectcraft_engine::project::LayerSource::Shape), "{mods:?}: no shape layer");
        let at = layer.transform().unwrap().get("position").unwrap().value.components();
        assert!((at[0] - centre[0]).abs() < 1.0 && (at[1] - centre[1]).abs() < 1.0, "{mods:?}: at {at:?}, not {centre:?}");
        let group = layer.props.sub("contents").unwrap().groups().next().unwrap().clone();
        let path = group.sub("contents").unwrap().sub("ellipse").unwrap();
        let s = path.get("size").unwrap().value.components();
        assert!((s[0] - size[0]).abs() < 1.0 && (s[1] - size[1]).abs() < 1.0, "{mods:?}: sized {s:?}, not {size:?}");
    }
}

/// The ghost says where the shape lands: a mask on the selected layer is drawn in the mask colour
/// and not painted with the Fill, where a shape drawn into the selected shape layer is.
#[test]
fn shape_drag_ghost_shows_where_the_shape_will_land() {
    let (from, to) = ([140.0, 100.0], [400.0, 300.0]);
    let centre = [270.0, 200.0];

    // Nothing selected: the shape becomes a new layer, painted with the Fill.
    let mut h = harness();
    h.state_mut().ui.tool = Tool::Ellipse;
    settle_viewer(&mut h);
    let (a, b) = (screen(&h, from), screen(&h, to));
    let shape_mid = mid_drag(&mut h, a, b, egui::Modifiers::default());
    let shape_px = px_at(&shape_mid, &h, centre);
    let fill = fill_rgb(&h);
    assert!(delta(shape_px, fill) < delta(shape_px, [0xff, 0xc0, 0x00]), "a shape ghost is painted with the Fill, not the mask colour");
    release(&mut h, b, egui::Modifiers::default());

    // A solid selected: the same drag is a mask on it, in the mask colour.
    let mut h = harness();
    let plate = h.state().session.active_comp().unwrap().layers.iter().find(|l| l.name == "Box").unwrap().id;
    h.state_mut().session.execute("layer.select", json!({"layers": [plate.0]})).unwrap();
    h.state_mut().ui.tool = Tool::Ellipse;
    settle_viewer(&mut h);
    let (a, b) = (screen(&h, from), screen(&h, to));
    let mask_mid = mid_drag(&mut h, a, b, egui::Modifiers::default());
    let mask_px = px_at(&mask_mid, &h, centre);
    assert!(delta(mask_px, shape_px) > 20, "a mask ghost must not look like the shape ghost ({mask_px:?} vs {shape_px:?})");
    release(&mut h, b, egui::Modifiers::default());
    let comp = h.state().session.active_comp().unwrap();
    assert_eq!(comp.layer(plate).unwrap().props.sub("masks").unwrap().groups().count(), 1, "the drag drew a mask");
}

/// Dragging a layer moves its picture while the button is still down. Every pointer move replaces
/// the comp's content identity, so the exact frame on screen is re-rendered at every step: waiting
/// for it would freeze the viewer on the last completed frame for the whole drag.
#[test]
fn a_layer_drag_moves_the_picture_while_the_button_is_down() {
    let mut h = harness();
    let box_ = h.state().session.active_comp().unwrap().layers.iter().find(|l| l.name == "Box").unwrap().id;
    h.state_mut().session.execute("layer.select", json!({"layers": [box_.0]})).unwrap();
    settle_viewer(&mut h);
    let from = screen(&h, [320.0, 180.0]);
    let to = screen(&h, [520.0, 180.0]);
    h.input_mut().events.push(Event::PointerMoved(from));
    h.input_mut().events.push(Event::PointerButton { pos: from, button: egui::PointerButton::Primary, pressed: true, modifiers: Default::default() });
    h.step();
    // Where the layer is not (yet): the plate behind it.
    let plate = px_at(&h.render_serialized().unwrap(), &h, [440.0, 200.0]);

    // Drag in small steps, still holding the button, and give the renders a chance to land.
    let mut moved = false;
    for i in 1..=40 {
        let p = from + (to - from) * (i as f32 / 40.0);
        h.input_mut().events.push(Event::PointerMoved(p));
        h.step();
        std::thread::sleep(std::time::Duration::from_millis(15));
        // 30 comp px behind the pointer: inside the layer, well away from its outline, its anchor
        // icon and where it started.
        let at = px_at(&h.render_serialized().expect("mid-drag frame"), &h, [320.0 + 200.0 * (i as f32 / 40.0) - 30.0, 200.0]);
        if i >= 30 && delta(at, plate) > 30 {
            moved = true;
            break;
        }
    }
    assert!(moved, "the layer's pixels did not follow the pointer while the button was down");
    h.input_mut().events.push(Event::PointerButton { pos: to, button: egui::PointerButton::Primary, pressed: false, modifiers: Default::default() });
    h.run_steps(2);
}

/// Clicking the word "Fill" opens Fill Options: a radial gradient in Multiply at 40% paints the
/// next shape drawn (#227).
#[test]
fn fill_options_paint_the_next_shape() {
    let mut h = harness();
    h.state_mut().ui.tool = Tool::Ellipse;
    h.run_steps(2);
    let at = rect(&h, "header.fillOptions").center();
    click(&mut h, at);
    let at = rect(&h, "header.fillOptions.radial").center();
    click(&mut h, at);
    let at = rect(&h, "header.fillOptions.blend").center();
    click(&mut h, at);
    let at = h.get_by_label("Multiply").rect().center();
    click(&mut h, at);
    let at = rect(&h, "header.fillOptions.opacity").center();
    click(&mut h, at);
    h.input_mut().events.push(Event::Text("40".into()));
    h.step();
    h.input_mut().events.push(Event::Key { key: egui::Key::Enter, physical_key: None, pressed: true, repeat: false, modifiers: Default::default() });
    h.run_steps(2);
    let fill = h.state().session.state.shape_tool.fill.clone();
    assert_eq!((fill.kind, fill.blend, fill.opacity), (PaintKind::Radial, effectcraft_engine::color::BlendMode::Multiply, 40.0));
    h.input_mut().events.push(Event::Key { key: egui::Key::Escape, physical_key: None, pressed: true, repeat: false, modifiers: Default::default() });
    h.run_steps(3);
    assert!(h.state().auto.find("header.fillOptions.radial").is_none(), "closed");
    let (a, b) = (screen(&h, [100.0, 100.0]), screen(&h, [220.0, 160.0]));
    drag(&mut h, a, b);
    let layer = h.state().session.active_comp().unwrap().layers[0].clone();
    let g = layer.props.sub("contents").unwrap().groups().next().unwrap().sub("contents").unwrap().clone();
    assert_eq!(g.groups().map(|x| x.match_id.as_str()).collect::<Vec<_>>(), ["ellipse", "gfill"]);
    let gfill = g.groups().find(|x| x.match_id == "gfill").unwrap();
    let multiply = effectcraft_engine::color::BlendMode::ALL.iter().position(|m| *m == effectcraft_engine::color::BlendMode::Multiply).unwrap() as u32;
    assert_eq!(gfill.get("type").unwrap().value, effectcraft_keyframe::Value::Enum(1));
    assert_eq!(gfill.get("blend").unwrap().value, effectcraft_keyframe::Value::Enum(multiply));
    assert_eq!(gfill.get("opacity").unwrap().value.as_f64(), 40.0);
}

/// With a shape layer selected, Fill Options' None switches its fill off and Solid Color turns
/// it back on, and the Fill swatch shows the layer's kind (#732).
#[test]
fn fill_options_change_the_selected_shape_layer() {
    let mut h = harness();
    let l = h.state_mut().session.execute("layer.newShape", json!({"kind": "rect", "size": [100, 100], "position": [320, 180]})).unwrap()["layer"]
        .as_u64()
        .unwrap();
    h.run_steps(2);
    let fill_on = |h: &Harness<'_, EffectcraftApp>| {
        let layer = h.state().session.active_comp().unwrap().layer(LayerId(l)).unwrap().clone();
        layer.props.group("contents/group/contents/fill").unwrap().enabled
    };
    assert!(fill_on(&h));
    for (kind, on) in [("none", false), ("solid", true)] {
        let at = rect(&h, "header.fillOptions").center();
        click(&mut h, at);
        let at = rect(&h, &format!("header.fillOptions.{kind}")).center();
        click(&mut h, at);
        h.input_mut().events.push(Event::Key { key: egui::Key::Escape, physical_key: None, pressed: true, repeat: false, modifiers: Default::default() });
        h.run_steps(3);
        assert_eq!(fill_on(&h), on, "{kind}");
        assert_eq!(h.state().session.state.shape_tool.fill.kind.id(), kind);
    }
}

/// #1220: path tools hide the layer transform box, while path vertices remain editable.
#[test]
fn path_editing_hides_layer_bounds_and_keeps_corner_vertices_editable() {
    for mask in [false, true] {
        let mut h = harness();
        let vertices = [[200.0, 100.0], [400.0, 100.0], [400.0, 260.0], [200.0, 260.0]];
        let (layer, uid, kind) = if mask {
            let layer = layer_id(&h, "Plate");
            h.state_mut().session.execute("layer.select", json!({"layers": [layer]})).unwrap();
            let r = h
                .state_mut()
                .session
                .execute("mask.new", json!({"layer": layer, "vertices": vertices, "outTangents": [[40, 0], [0, 0], [0, 0], [0, 0]], "closed": true}))
                .unwrap();
            (layer, r["mask"].as_u64().unwrap(), "mask")
        } else {
            let r = h.state_mut().session.execute("shape.newPath", json!({"vertices": vertices, "outTangents": [[40, 0], [0, 0], [0, 0], [0, 0]], "closed": true, "fill": false, "stroke": "#ffffff", "strokeWidth": 4})).unwrap();
            (r["layer"].as_u64().unwrap(), r["path"].as_u64().unwrap(), "shapePath")
        };
        h.state_mut().session.state.selected_vertices.clear();
        h.run_steps(2);
        let handle = format!("viewer.handle.{layer}.0");
        assert!(h.state().auto.find(&handle).is_some(), "ordinary layer selection has transform handles (mask={mask})");
        for tool in [Tool::Pen, Tool::PenAdd, Tool::PenDelete, Tool::PenConvert, Tool::MaskFeather] {
            h.state_mut().ui.tool = tool;
            h.run_steps(2);
            assert!(h.state().auto.find(&handle).is_none(), "{tool:?}: hide the layer box");
            assert!(h.state().auto.find(&format!("viewer.{kind}.{uid}.vertex.0")).is_some(), "{tool:?}: keep the path point");
        }
        h.state_mut().ui.tool = Tool::Selection;
        h.run_steps(2);
        let vertex = format!("viewer.{kind}.{uid}.vertex.0");
        let from = rect(&h, &vertex).center();
        click(&mut h, from);
        h.run_steps(2); // The automation registry also keeps the preceding frame's elements.
        assert!(h.state().auto.find(&handle).is_none(), "selecting a vertex starts path editing (mask={mask})");
        let position = h.state().session.active_comp().unwrap().layer(LayerId(layer)).unwrap().props.prop("transform/position").unwrap().value.clone();
        let path = |h: &Harness<'_, EffectcraftApp>| {
            h.state()
                .session
                .active_comp()
                .unwrap()
                .layer(LayerId(layer))
                .unwrap()
                .props
                .find_group(uid)
                .unwrap()
                .get("path")
                .unwrap()
                .value
                .as_path()
                .unwrap()
                .clone()
        };
        let before = path(&h);
        drag(&mut h, from, from + vec2(15.0, 12.0));
        assert_ne!(path(&h).vertices[0], before.vertices[0], "the corner vertex can still be moved");
        assert_eq!(path(&h).vertices[1..], before.vertices[1..]);
        assert_eq!(h.state().session.active_comp().unwrap().layer(LayerId(layer)).unwrap().props.prop("transform/position").unwrap().value, position);
        h.state_mut().session.execute("edit.undo", json!({})).unwrap();
        h.run_steps(2);
        assert_eq!(path(&h), before, "moving the vertex undoes in one step");
        let z = rect(&h, "viewer.comp").width() / 640.0;
        let tangent = rect(&h, &vertex).center() + vec2(40.0 * z, 0.0);
        drag(&mut h, tangent, tangent + vec2(0.0, 20.0));
        assert_ne!(path(&h).out_tangents[0], before.out_tangents[0], "the Bézier handle is still editable");
        assert_eq!(path(&h).vertices, before.vertices, "a tangent drag does not move the path points");
        h.state_mut().session.execute("edit.undo", json!({})).unwrap();
        h.run_steps(2);
        assert_eq!(path(&h), before, "the tangent drag undoes in one step");
        h.state_mut().ui.tool = Tool::Rotate;
        h.run_steps(2);
        assert!(h.state().auto.find(&handle).is_some(), "a layer transform tool restores the box");
        h.state_mut().ui.tool = Tool::Selection;
        h.state_mut().session.state.selected_vertices.clear();
        h.run_steps(2);
        assert!(h.state().auto.find(&handle).is_some(), "ending vertex selection restores the box");
    }
}

/// #1769: the eye hides a layer's pixels, not the controls of a selected AV layer: a hidden shape
/// layer keeps its transform box, anchor and shape path, and its handles still scale it.
#[test]
fn hidden_shape_layer_keeps_its_controls_and_can_still_scale() {
    let mut h = harness();
    let (layer, path) = {
        let r = h
            .state_mut()
            .session
            .execute(
                "shape.newPath",
                json!({
                    "vertices": [[200.0, 100.0], [400.0, 100.0], [400.0, 260.0], [200.0, 260.0]],
                    "closed": true,
                    "fill": false,
                    "stroke": "#ffffff",
                    "strokeWidth": 4
                }),
            )
            .unwrap();
        (r["layer"].as_u64().unwrap(), r["path"].as_u64().unwrap())
    };
    h.state_mut().session.execute("layer.select", json!({"layers": [layer]})).unwrap();
    h.state_mut().session.state.selected_vertices.clear();
    h.state_mut().session.execute("layer.setSwitch", json!({"layers": [layer], "switch": "video", "value": false})).unwrap();
    h.run_steps(2);
    let shown = h.state().session.active_comp().unwrap().layer(LayerId(layer)).unwrap().clone();
    assert!(!shown.switches.video);
    assert!(shown.source.is_av());
    assert_eq!(h.state().session.state.selected_layers, vec![LayerId(layer)]);
    assert!(h.state().auto.find(&format!("viewer.handle.{layer}.0")).is_some(), "a hidden shape keeps its transform box");
    assert!(h.state().auto.find(&format!("viewer.anchor.{layer}")).is_some(), "and its anchor");
    assert!(h.state().auto.find(&format!("viewer.shapePath.{path}.vertex.0")).is_some(), "and its shape path");
    let scale = |h: &Harness<'_, EffectcraftApp>| {
        h.state().session.active_comp().unwrap().layer(LayerId(layer)).unwrap().props.prop("transform/scale").unwrap().value.as_vec3()
    };
    let before = scale(&h);
    let from = rect(&h, &format!("viewer.handle.{layer}.5")).center();
    drag(&mut h, from, from + vec2(24.0, 0.0));
    assert_ne!(scale(&h), before, "a hidden layer's handle still scales it");
}

/// #1964: Shift-dragging a selected mask vertex keeps it to the axis it moves along most, as a
/// Shift-dragged layer does; without Shift it moves freely.
#[test]
fn shift_drag_keeps_a_mask_vertex_to_one_axis() {
    let mut h = harness();
    let plate = layer_id(&h, "Plate");
    let corners = [[100.0, 60.0], [500.0, 60.0], [500.0, 300.0], [100.0, 300.0]];
    let mask = {
        let s = &mut h.state_mut().session;
        s.execute("layer.select", json!({"layers": [plate]})).unwrap();
        let m = s.execute("mask.new", json!({"layer": plate, "vertices": corners, "closed": true})).unwrap()["mask"].as_u64().unwrap();
        s.execute("mask.selectVertices", json!({"vertices": [{"layer": plate, "mask": m, "index": 0}]})).unwrap();
        m
    };
    h.run_steps(2);
    let vertex = format!("viewer.mask.{mask}.vertex.0");
    let before = mask_path(&h, "Plate").vertices[0];
    let from = rect(&h, &vertex).center();
    drag_path(&mut h, &[from, from + vec2(30.0, 8.0)], egui::Modifiers::SHIFT);
    let shifted = mask_path(&h, "Plate").vertices[0];
    assert!(shifted[0] > before[0], "it moved along x: {before:?} -> {shifted:?}");
    assert_eq!(shifted[1], before[1], "and not along y");
    let from = rect(&h, &vertex).center();
    drag_path(&mut h, &[from, from + vec2(30.0, 8.0)], egui::Modifiers::NONE);
    let free = mask_path(&h, "Plate").vertices[0];
    assert!(free[0] > shifted[0] && free[1] > shifted[1], "without Shift both axes move: {shifted:?} -> {free:?}");
}

/// #3229: a mask vertex Shift-dragged along x past another layer's corner a few pixels off the
/// axis lines up with the corner's x and stays on the axis (it used to jump onto the corner).
#[test]
fn shift_drag_snaps_a_mask_vertex_along_its_axis_only() {
    let mut h = harness();
    let plate = layer_id(&h, "Plate");
    // The Box (80×80 at the comp centre) has its top left corner at (280, 140); the mask's first
    // vertex starts 3 px below that row.
    let corners = [[200.0, 143.0], [240.0, 100.0], [240.0, 60.0], [200.0, 60.0]];
    let mask = {
        let s = &mut h.state_mut().session;
        s.execute("layer.select", json!({"layers": [plate]})).unwrap();
        let m = s.execute("mask.new", json!({"layer": plate, "vertices": corners, "closed": true})).unwrap()["mask"].as_u64().unwrap();
        s.execute("mask.selectVertices", json!({"vertices": [{"layer": plate, "mask": m, "index": 0}]})).unwrap();
        m
    };
    h.run_steps(2);
    // Shift-drag it right (and a pixel down) to 2 px short of the corner's x.
    let from = rect(&h, &format!("viewer.mask.{mask}.vertex.0")).center();
    let to = from + (screen(&h, [278.0, 144.0]) - screen(&h, [200.0, 143.0]));
    drag_path(&mut h, &[from, to], egui::Modifiers::SHIFT);
    let v = mask_path(&h, "Plate").vertices[0];
    assert!((v[0] - 280.0).abs() < 0.01, "the vertex lines up with the corner: {v:?}");
    assert!((v[1] - 143.0).abs() < 1e-6, "and stays on the axis: {v:?}");
}

/// #999: a mask vertex dragged against the edge of the Composition panel pans the view, and the
/// vertex keeps moving while the pointer is held there, as in After Effects.
#[test]
fn a_vertex_held_at_the_viewer_edge_pans_the_view() {
    let mut h = harness();
    let plate = layer_id(&h, "Plate");
    let corners = [[100.0, 60.0], [500.0, 60.0], [500.0, 300.0], [100.0, 300.0]];
    let mask = {
        let s = &mut h.state_mut().session;
        s.execute("layer.select", json!({"layers": [plate]})).unwrap();
        let m = s.execute("mask.new", json!({"layer": plate, "vertices": corners, "closed": true})).unwrap()["mask"].as_u64().unwrap();
        s.execute("mask.selectVertices", json!({"vertices": [{"layer": plate, "mask": m, "index": 0}]})).unwrap();
        m
    };
    h.run_steps(2);
    let pan0 = h.state().ui.viewer.pan;
    let from = rect(&h, &format!("viewer.mask.{mask}.vertex.0")).center();
    let edge = pos2(2.0, from.y);
    h.input_mut().events.push(Event::PointerMoved(from));
    h.input_mut().events.push(Event::PointerButton { pos: from, button: egui::PointerButton::Primary, pressed: true, modifiers: Default::default() });
    h.step();
    for i in 1..=6 {
        h.input_mut().events.push(Event::PointerMoved(from + (edge - from) * (i as f32 / 6.0)));
        h.step();
    }
    let reached = mask_path(&h, "Plate").vertices[0];
    for _ in 0..20 {
        h.step();
    }
    h.input_mut().events.push(Event::PointerButton { pos: edge, button: egui::PointerButton::Primary, pressed: false, modifiers: Default::default() });
    h.run_steps(2);
    let pan = h.state().ui.viewer.pan;
    assert!(pan[0] > pan0[0] && pan[1] == pan0[1], "the view panned right to show more on the left: {pan0:?} -> {pan:?}");
    let after = mask_path(&h, "Plate").vertices[0];
    assert!(after[0] < reached[0], "the vertex kept moving left with the view: {reached:?} -> {after:?}");
    // Back inside the view, nothing pans.
    let still = h.state().ui.viewer.pan;
    let mid = rect(&h, "viewer.comp").center();
    drag(&mut h, mid, mid + vec2(5.0, 0.0));
    assert_eq!(h.state().ui.viewer.pan, still);
}

/// #1769: a hidden masked layer keeps its mask path and vertices, and they remain editable.
#[test]
fn hidden_masked_layer_keeps_its_mask_path_editable() {
    let mut h = harness();
    let plate = layer_id(&h, "Plate");
    let corners = [[100.0, 60.0], [500.0, 60.0], [500.0, 300.0], [100.0, 300.0]];
    let mask = {
        let s = &mut h.state_mut().session;
        s.execute("layer.select", json!({"layers": [plate]})).unwrap();
        let m = s.execute("mask.new", json!({"layer": plate, "vertices": corners, "closed": true})).unwrap()["mask"].as_u64().unwrap();
        s.state.selected_vertices.clear();
        s.execute("layer.setSwitch", json!({"layers": [plate], "switch": "video", "value": false})).unwrap();
        m
    };
    h.run_steps(2);
    assert!(h.state().auto.find(&format!("viewer.handle.{plate}.0")).is_some(), "a hidden masked layer keeps its transform box");
    let vertex = format!("viewer.mask.{mask}.vertex.0");
    assert!(h.state().auto.find(&vertex).is_some(), "and its mask path");
    let before = mask_path(&h, "Plate");
    let from = rect(&h, &vertex).center();
    drag(&mut h, from, from + vec2(15.0, 12.0));
    let after = mask_path(&h, "Plate");
    assert_ne!(after.vertices[0], before.vertices[0], "the hidden layer's mask vertex can still be moved");
    assert_eq!(after.vertices[1..], before.vertices[1..]);
}

#[test]
fn path_tools_keep_bounds_on_layers_without_visible_paths() {
    let mut h = harness();
    let layer = layer_id(&h, "Box");
    h.state_mut().session.execute("layer.select", json!({"layers": [layer]})).unwrap();
    h.state_mut().ui.tool = Tool::Pen;
    h.run_steps(2);
    let handle = format!("viewer.handle.{layer}.0");
    assert!(h.state().auto.find(&handle).is_some(), "a layer without a path keeps its box");
    h.state_mut().session.execute("mask.new", json!({"layer": layer, "vertices": [[0, 0], [80, 0], [80, 80]], "closed": true})).unwrap();
    h.run_steps(2);
    assert!(h.state().auto.find(&handle).is_none());
    h.state_mut().ui.viewer.show_masks = false;
    h.run_steps(2);
    assert!(h.state().auto.find(&handle).is_some(), "hidden paths do not suppress the layer controls");
}

#[test]
fn shape_pen_draws_a_closed_shape_layer() {
    let mut h = harness();
    h.state_mut().ui.tool = Tool::Pen;
    h.run_steps(2);
    let n0 = h.state().session.active_comp().unwrap().layers.len();
    for p in [[200.0, 100.0], [400.0, 120.0], [300.0, 260.0], [200.0, 100.0]] {
        let at = screen(&h, p);
        click(&mut h, at);
    }
    let comp = h.state().session.active_comp().unwrap().clone();
    assert_eq!(comp.layers.len(), n0 + 1);
    let l = &comp.layers[0];
    assert!(matches!(l.source, effectcraft_engine::project::LayerSource::Shape));
    let g = l.props.sub("contents").unwrap().groups().next().unwrap();
    assert_eq!(g.name, "Shape 1");
    let path = g.sub("contents").unwrap().groups().next().unwrap();
    let sp = path.get("path").unwrap().value.as_path().unwrap().clone();
    assert_eq!(sp.vertices.len(), 3);
    assert!(sp.closed);
    assert!((sp.vertices[0][0] - (200.0 - 320.0)).abs() < 1.5, "{:?}", sp.vertices);
    // G cycles the Pen slot: Add Vertex, Delete Vertex, Convert Vertex, Mask Feather, Pen.
    let ctx = h.ctx.clone();
    let mut seen = vec![];
    for _ in 0..5 {
        effectcraft_ui_egui::menus::invoke(h.state_mut(), &ctx, "tool.pen", json!({})).unwrap();
        seen.push(h.state().ui.tool);
    }
    assert_eq!(seen, vec![Tool::PenAdd, Tool::PenDelete, Tool::PenConvert, Tool::MaskFeather, Tool::Pen]);
    // Add Vertex on the first segment.
    h.state_mut().ui.tool = Tool::PenAdd;
    h.run_steps(2);
    let at = screen(&h, [300.0, 110.0]);
    click(&mut h, at);
    let comp = h.state().session.active_comp().unwrap().clone();
    let sp = comp.layers[0]
        .props
        .sub("contents")
        .unwrap()
        .groups()
        .next()
        .unwrap()
        .sub("contents")
        .unwrap()
        .groups()
        .next()
        .unwrap()
        .get("path")
        .unwrap()
        .value
        .as_path()
        .unwrap()
        .clone();
    assert_eq!(sp.vertices.len(), 4);
}

/// Fit New Layers to Work Area with the playhead past the work area: the Pen's new shape layer
/// spans the work area and the playhead moves into it, so the path and its vertex controls show
/// and the closing click lands.
#[test]
fn pen_draws_on_a_layer_fitted_to_the_work_area_past_the_playhead() {
    let mut h = harness();
    let s = &mut h.state_mut().session;
    s.execute("prefs.set", json!({"key": "general.fitNewLayersToWorkArea", "value": true})).unwrap();
    s.execute("comp.workArea", json!({"start": 1, "end": 2})).unwrap();
    s.set_time(effectcraft_engine::time::Tick::from_seconds_f64(3.0));
    h.state_mut().ui.tool = Tool::Pen;
    h.run_steps(2);
    let n0 = h.state().session.active_comp().unwrap().layers.len();
    for p in [[200.0, 100.0], [400.0, 120.0], [300.0, 260.0]] {
        let at = screen(&h, p);
        click(&mut h, at);
    }
    let l = h.state().session.active_comp().unwrap().layers[0].clone();
    let t = h.state().session.time();
    assert!(l.in_point <= t && t < l.out_point, "the playhead is on the new layer: {t:?} in {:?}..{:?}", l.in_point, l.out_point);
    assert_eq!((l.in_point.seconds().round(), l.out_point.seconds().round()), (1.0, 2.0), "the layer keeps the work area");
    let path = l.props.sub("contents").unwrap().groups().next().unwrap().sub("contents").unwrap().groups().next().unwrap().clone();
    assert!(h.state().auto.find(&format!("viewer.shapePath.{}.vertex.0", path.uid)).is_some(), "the Pen's vertex controls show");
    let at = screen(&h, [200.0, 100.0]);
    click(&mut h, at);
    let comp = h.state().session.active_comp().unwrap().clone();
    assert_eq!(comp.layers.len(), n0 + 1);
    let sp = comp.layers[0].props.find_group(path.uid).unwrap().get("path").unwrap().value.as_path().unwrap().clone();
    assert_eq!(sp.vertices.len(), 3);
    assert!(sp.closed, "the click on the first vertex closes the path");
}

#[test]
fn motion_path_key_drag_edits_that_key() {
    let mut h = harness();
    let box_id: LayerId = h.state().session.active_comp().unwrap().layers[0].id;
    let s = &mut h.state_mut().session;
    s.execute("layer.select", json!({"layers": [box_id.0]})).unwrap();
    s.execute("prop.addKey", json!({"layer": box_id.0, "path": "transform/position", "time": 0.0, "value": [100, 100, 0]})).unwrap();
    s.execute("prop.addKey", json!({"layer": box_id.0, "path": "transform/position", "time": 2.0, "value": [500, 100, 0]})).unwrap();
    s.execute("view.snapping", json!({"value": false})).unwrap();
    s.set_time(effectcraft_engine::time::Tick::from_seconds_f64(1.0));
    h.run_steps(3);
    assert!(h.state().auto.find(&format!("viewer.motionPath.{}.1", box_id.0)).is_some());
    let from = screen(&h, [500.0, 100.0]);
    let to2 = screen(&h, [500.0, 300.0]);
    drag(&mut h, from, to2);
    let l = h.state().session.active_comp().unwrap().layer(box_id).unwrap().clone();
    let k = &l.props.prop("transform/position").unwrap().keys;
    let v1 = k[1].value.as_vec3();
    assert!((v1[1] - 300.0).abs() < 2.0 && (v1[0] - 500.0).abs() < 2.0, "{v1:?}");
    assert_eq!(k[0].value.as_vec3(), [100.0, 100.0, 0.0]);
}

/// The first mask path of layer `name`.
fn mask_path(h: &Harness<'_, EffectcraftApp>, name: &str) -> effectcraft_engine::keyframe::ShapePath {
    let l = h.state().session.active_comp().unwrap().layers.iter().find(|l| l.name == name).unwrap().clone();
    l.masks().unwrap().groups().next().unwrap().get("path").unwrap().value.as_path().unwrap().clone()
}

/// #1544: a Shift-click with the Pen puts the next point straight below, beside or diagonal to the
/// one before it; without Shift it goes where it was clicked.
#[test]
fn pen_shift_click_draws_a_straight_segment() {
    let mut h = harness();
    let plate = layer_id(&h, "Plate");
    h.state_mut().session.execute("layer.select", json!({"layers": [plate]})).unwrap();
    h.state_mut().session.execute("view.snapping", json!({"value": false})).unwrap();
    h.state_mut().ui.tool = Tool::Pen;
    h.run_steps(2);
    let first = screen(&h, [100.0, 60.0]);
    click(&mut h, first);
    let shift_click = |h: &mut Harness<'_, EffectcraftApp>, p: Pos2| {
        let shift = egui::Modifiers::SHIFT;
        h.input_mut().events.push(Event::ModifiersChanged(shift));
        h.input_mut().events.push(Event::PointerMoved(p));
        h.step();
        h.input_mut().events.push(Event::PointerButton { pos: p, button: egui::PointerButton::Primary, pressed: true, modifiers: shift });
        h.step();
        h.input_mut().events.push(Event::PointerButton { pos: p, button: egui::PointerButton::Primary, pressed: false, modifiers: shift });
        h.input_mut().events.push(Event::ModifiersChanged(egui::Modifiers::NONE));
        h.run_steps(2);
    };
    let down = screen(&h, [112.0, 250.0]);
    shift_click(&mut h, down);
    let free = screen(&h, [300.0, 290.0]);
    click(&mut h, free);
    let drawn = mask_path(&h, "Plate");
    let near = |a: [f64; 2], b: [f64; 2]| (a[0] - b[0]).abs() < 1.0 && (a[1] - b[1]).abs() < 1.0;
    assert_eq!(drawn.vertices.len(), 3, "{drawn:?}");
    assert!(near(drawn.vertices[1], [100.0, 250.0]), "straight down: {drawn:?}");
    assert!(near(drawn.vertices[2], [300.0, 290.0]), "no Shift, no constraint: {drawn:?}");
}

/// Drawing a mask with the Pen, moving a point and undoing take one action back per undo; a
/// double-click on a point selects all of them in a free-transform box, and dragging inside it
/// (or, once the box is gone, dragging a point) moves the whole mask (#290).
#[test]
fn pen_mask_undoes_step_by_step_and_double_click_moves_the_whole_mask() {
    let mut h = harness();
    let plate = layer_id(&h, "Plate");
    h.state_mut().session.execute("layer.select", json!({"layers": [plate]})).unwrap();
    h.state_mut().session.execute("view.snapping", json!({"value": false})).unwrap();
    h.state_mut().ui.tool = Tool::Pen;
    h.run_steps(2);
    let corners = [[100.0, 60.0], [500.0, 60.0], [500.0, 300.0], [100.0, 300.0]];
    for p in corners.iter().chain([&corners[0]]) {
        let at = screen(&h, [p[0] as f32, p[1] as f32]);
        click(&mut h, at);
    }
    let drawn = mask_path(&h, "Plate");
    let near = |a: [f64; 2], b: [f64; 2]| (a[0] - b[0]).abs() < 1.0 && (a[1] - b[1]).abs() < 1.0;
    assert!(drawn.closed && drawn.vertices.len() == 4 && drawn.vertices.iter().zip(&corners).all(|(v, c)| near(*v, *c)), "{drawn:?}");
    h.state_mut().ui.tool = Tool::Selection;
    h.run_steps(2);
    // Move one point: one undo step puts it back, the next reopens the path, the next removes the
    // last point.
    let (from, to) = (screen(&h, [500.0, 60.0]), screen(&h, [520.0, 80.0]));
    drag(&mut h, from, to);
    let moved = mask_path(&h, "Plate");
    assert!(near(moved.vertices[1], [520.0, 80.0]) && moved.vertices[0] == drawn.vertices[0], "{moved:?}");
    let undo = |h: &mut Harness<'_, EffectcraftApp>| {
        h.state_mut().session.execute("edit.undo", json!({})).unwrap();
        h.run_steps(2);
        mask_path(h, "Plate")
    };
    assert_eq!(undo(&mut h), drawn);
    assert!(!undo(&mut h).closed);
    assert_eq!(undo(&mut h).vertices.len(), 3);
    for _ in 0..3 {
        h.state_mut().session.execute("edit.redo", json!({})).unwrap();
    }
    h.run_steps(2);
    // Double-click a point: all of them selected, in a free-transform box.
    let at = screen(&h, [100.0, 300.0]);
    h.input_mut().events.push(Event::PointerMoved(at));
    h.step();
    for pressed in [true, false, true, false] {
        h.input_mut().events.push(Event::PointerButton { pos: at, button: egui::PointerButton::Primary, pressed, modifiers: Default::default() });
    }
    h.run_steps(3);
    assert_eq!(h.state().session.state.selected_vertices.len(), 4);
    assert!(h.state().auto.find("viewer.freeTransform.handle.0").is_some(), "the free-transform box is up");
    // Dragging inside the box moves the whole mask.
    let (from, to) = (screen(&h, [300.0, 120.0]), screen(&h, [310.0, 130.0]));
    drag(&mut h, from, to);
    let path = mask_path(&h, "Plate");
    assert!(path.vertices.iter().zip(&moved.vertices).all(|(v, c)| near(*v, [c[0] + 10.0, c[1] + 10.0])), "{path:?}");
    // A click away ends the box; the points stay selected and dragging one moves them all.
    let away = screen(&h, [600.0, 340.0]);
    click(&mut h, away);
    h.run_steps(2);
    assert!(h.state().auto.find("viewer.freeTransform.handle.0").is_none());
    let (from, to) = (screen(&h, [110.0, 310.0]), screen(&h, [100.0, 300.0]));
    drag(&mut h, from, to);
    let back = mask_path(&h, "Plate");
    assert!(back.vertices.iter().zip(&moved.vertices).all(|(v, c)| near(*v, *c)), "{back:?}");
}

/// #513: clicking away from a mask's path, even on its own layer, deselects the mask's points;
/// the layer stays selected.
#[test]
fn clicking_off_a_mask_deselects_its_points() {
    let mut h = harness();
    let plate = layer_id(&h, "Plate");
    let s = &mut h.state_mut().session;
    s.execute("layer.select", json!({"layers": [plate]})).unwrap();
    s.execute("mask.new", json!({"layer": plate, "vertices": [[100.0, 60.0], [500.0, 60.0], [500.0, 300.0], [100.0, 300.0]], "closed": true})).unwrap();
    h.state_mut().ui.tool = Tool::Selection;
    h.run_steps(2);
    let at = screen(&h, [500.0, 60.0]);
    click(&mut h, at);
    assert_eq!(h.state().session.state.selected_vertices.len(), 1, "a click on a point selects it");
    let away = screen(&h, [200.0, 250.0]);
    click(&mut h, away);
    assert!(h.state().session.state.selected_vertices.is_empty(), "a click off the path deselects its points");
    assert_eq!(h.state().session.state.selected_layers, vec![LayerId(plate)], "the layer stays selected");
}

/// A straight two-key motion path shows Bezier handles at both keys, and dragging one pulls the
/// path into a curve (#290).
#[test]
fn a_straight_motion_path_has_handles_to_curve_it() {
    let mut h = harness();
    let box_id: LayerId = h.state().session.active_comp().unwrap().layers[0].id;
    let s = &mut h.state_mut().session;
    s.execute("layer.select", json!({"layers": [box_id.0]})).unwrap();
    s.execute("prop.addKey", json!({"layer": box_id.0, "path": "transform/position", "time": 0.0, "value": [100, 100, 0]})).unwrap();
    s.execute("prop.addKey", json!({"layer": box_id.0, "path": "transform/position", "time": 2.0, "value": [400, 100, 0]})).unwrap();
    s.execute("view.snapping", json!({"value": false})).unwrap();
    s.set_time(effectcraft_engine::time::Tick::from_seconds_f64(1.0));
    h.run_steps(3);
    let (out0, in1) = (format!("viewer.motionPath.{}.0.out", box_id.0), format!("viewer.motionPath.{}.1.in", box_id.0));
    assert!(h.state().auto.find(&in1).is_some(), "the second key has a handle");
    // The first key's auto-Bezier handle lies a sixth of the way along the path, as After Effects
    // puts it: drag it down 150 px.
    let from = rect(&h, &out0).center();
    assert!(from.distance(screen(&h, [150.0, 100.0])) < 2.0, "{from:?}");
    let to = screen(&h, [150.0, 250.0]);
    drag(&mut h, from, to);
    let p = h.state().session.active_comp().unwrap().layer(box_id).unwrap().props.prop("transform/position").unwrap().clone();
    assert!(!p.keys[0].spatial_auto);
    let t = p.keys[0].spatial_out;
    assert!((t[0] - 50.0).abs() < 2.0 && (t[1] - 150.0).abs() < 2.0, "{t:?}");
    let mid = p.value_at(effectcraft_engine::time::Tick::from_seconds_f64(1.0)).as_vec3();
    assert!(mid[1] > 130.0, "the path curves: {mid:?}");
}

#[test]
fn graph_editor_transform_box_scales_selected_keys_in_time() {
    let mut h = harness();
    let box_id: LayerId = h.state().session.active_comp().unwrap().layers[0].id;
    let s = &mut h.state_mut().session;
    s.execute("layer.select", json!({"layers": [box_id.0]})).unwrap();
    for (t, v) in [(0.0, 0.0), (1.0, 50.0), (2.0, 100.0)] {
        s.execute("prop.addKey", json!({"layer": box_id.0, "path": "transform/opacity", "time": t, "value": v})).unwrap();
    }
    let uid = s.active_comp().unwrap().layer(box_id).unwrap().props.prop("transform/opacity").unwrap().uid;
    s.execute("prop.select", json!({"layer": box_id.0, "prop": uid})).unwrap();
    let keys: Vec<_> = [0.0, 1.0, 2.0].iter().map(|t| json!({"layer": box_id.0, "prop": uid, "time": t})).collect();
    s.execute("keys.select", json!({"keys": keys})).unwrap();
    h.state_mut().ui.timeline.graph_editor = true;
    h.run_steps(4);
    for id in ["timeline.graph.snap", "timeline.graph.reference", "timeline.graph.transformBox", "timeline.graph.transformBox.5"] {
        assert!(h.state().auto.find(id).is_some(), "missing {id}");
    }
    // Drag the right edge handle to the right: the keys spread out in time, the first stays.
    let r = rect(&h, "timeline.graph.transformBox.5");
    let k0 = rect(&h, &format!("timeline.graph.key.{uid}.0.0")).center();
    let k2 = rect(&h, &format!("timeline.graph.key.{uid}.0.2")).center();
    let to = r.center() + vec2((k2.x - k0.x) * 0.5, 0.0);
    drag(&mut h, r.center(), to);
    let p = h.state().session.active_comp().unwrap().layer(box_id).unwrap().props.prop("transform/opacity").unwrap().clone();
    let ts: Vec<f64> = p.keys.iter().map(|k| k.time.seconds()).collect();
    assert_eq!(ts[0], 0.0);
    assert!(ts[2] > 2.5 && ts[2] < 3.5, "{ts:?}");
    assert_eq!(p.keys.iter().map(|k| k.value.as_f64()).collect::<Vec<_>>(), vec![0.0, 50.0, 100.0]);
    let undo = h.state().session.history.undo.iter().filter(|(l, _)| l == "Transform Keyframes").count();
    assert_eq!(undo, 1, "one undo step per drag");
}

#[test]
fn timeline_alt_drag_scales_a_key_group_in_time() {
    let mut h = harness();
    let box_id: LayerId = h.state().session.active_comp().unwrap().layers[0].id;
    let s = &mut h.state_mut().session;
    s.execute("layer.select", json!({"layers": [box_id.0]})).unwrap();
    for (t, v) in [(0.0, 0.0), (1.0, 50.0), (2.0, 100.0)] {
        s.execute("prop.addKey", json!({"layer": box_id.0, "path": "transform/opacity", "time": t, "value": v})).unwrap();
    }
    let uid = s.active_comp().unwrap().layer(box_id).unwrap().props.prop("transform/opacity").unwrap().uid;
    s.execute("keys.selectAll", json!({"layers": [box_id.0]})).unwrap();
    let ctx = h.ctx.clone();
    effectcraft_ui_egui::menus::invoke(h.state_mut(), &ctx, "timeline.reveal.opacity", json!({})).unwrap();
    h.run_steps(4);
    let mut ks: Vec<Pos2> =
        h.state().auto.query(&format!("timeline.key.{uid}.")).iter().map(|e| pos2(e.rect[0] + e.rect[2] / 2.0, e.rect[1] + e.rect[3] / 2.0)).collect();
    ks.sort_by(|a, b| a.x.total_cmp(&b.x));
    assert_eq!(ks.len(), 3, "{ks:?}");
    let (k0, k2) = (ks[0], ks[2]);
    // Alt-drag the last key to where 3 s would be: the group scales by 1.5 about the first key.
    let to = pos2(k0.x + (k2.x - k0.x) * 1.5, k2.y);
    let alt = egui::Modifiers { alt: true, ..Default::default() };
    h.input_mut().events.push(Event::PointerMoved(k2));
    h.step();
    h.input_mut().events.push(Event::ModifiersChanged(alt));
    h.input_mut().events.push(Event::PointerButton { pos: k2, button: egui::PointerButton::Primary, pressed: true, modifiers: alt });
    h.step();
    for i in 1..=8 {
        h.input_mut().events.push(Event::PointerMoved(k2 + (to - k2) * (i as f32 / 8.0)));
        h.step();
    }
    h.input_mut().events.push(Event::PointerButton { pos: to, button: egui::PointerButton::Primary, pressed: false, modifiers: alt });
    h.run_steps(2);
    let p = h.state().session.active_comp().unwrap().layer(box_id).unwrap().props.prop("transform/opacity").unwrap().clone();
    let ts: Vec<f64> = p.keys.iter().map(|k| k.time.seconds()).collect();
    assert_eq!(ts.len(), 3);
    assert_eq!(ts[0], 0.0);
    assert!((ts[2] - 3.0).abs() < 0.1 && (ts[1] - 1.5).abs() < 0.1, "{ts:?}");
}

#[test]
fn mask_feather_tool_adds_and_drags_feather_points() {
    let mut h = harness();
    // A mask on the Box (80×80 at the comp centre, comp 280…360 × 140…220): layer 10…70.
    let (box_id, mask) = {
        let s = &mut h.state_mut().session;
        let b = s.active_comp().unwrap().layers.iter().find(|l| l.name == "Box").unwrap().id;
        let m = s.execute("mask.new", json!({"layer": b.0, "vertices": [[10, 10], [70, 10], [70, 70], [10, 70]], "closed": true})).unwrap()["mask"]
            .as_u64()
            .unwrap();
        s.execute("layer.select", json!({"layers": [b.0]})).unwrap();
        (b, m)
    };
    h.state_mut().ui.tool = Tool::MaskFeather;
    h.run_steps(3);
    // Press on the top edge (comp 320,150) and drag 12 px up: an outer feather point of ≈12 px.
    let from = screen(&h, [320.0, 150.0]);
    let z = rect(&h, "viewer.comp").width() / 640.0;
    drag(&mut h, from, from - vec2(0.0, 12.0 * z));
    let pts = h.state_mut().session.execute("mask.featherPoint.list", json!({"layer": box_id.0, "mask": mask})).unwrap();
    let r = pts["points"][0]["radius"].as_f64().unwrap();
    assert!((r - 12.0).abs() < 1.5, "{pts}");
    assert_eq!(pts["points"][0]["segment"], json!(0));
    // The handle is registered; dragging it inwards makes it an inner feather point.
    h.run_steps(2);
    let handle = rect(&h, &format!("viewer.mask.{mask}.feather.0")).center();
    let to = screen(&h, [320.0, 160.0]);
    drag(&mut h, handle, to);
    let pts = h.state_mut().session.execute("mask.featherPoint.list", json!({"layer": box_id.0, "mask": mask})).unwrap();
    let r = pts["points"][0]["radius"].as_f64().unwrap();
    assert!((r + 10.0).abs() < 1.5, "{pts}");
    assert_eq!(pts["points"].as_array().unwrap().len(), 1);
    // One drag = one undo step each.
    h.state_mut().session.execute("edit.undo", json!({})).unwrap();
    let pts = h.state_mut().session.execute("mask.featherPoint.list", json!({"layer": box_id.0, "mask": mask})).unwrap();
    assert!(pts["points"][0]["radius"].as_f64().unwrap() > 0.0);
}

#[test]
fn region_of_interest_resizes_by_its_handles() {
    let mut h = harness();
    h.state_mut().session.execute("view.setRegionOfInterest", json!({"rect": [100, 50, 200, 200]})).unwrap();
    h.run_steps(3);
    // Bottom-right corner (handle 2) to (400, 300): the top-left stays.
    let c = rect(&h, "viewer.regionOfInterest.handle.2").center();
    let to = screen(&h, [400.0, 300.0]);
    drag(&mut h, c, to);
    let r = h.state().session.state.region_of_interest.expect("roi");
    assert!((r[0] - 100.0).abs() <= 1.0 && (r[1] - 50.0).abs() <= 1.0 && (r[2] - 300.0).abs() <= 2.0 && (r[3] - 250.0).abs() <= 2.0, "{r:?}");
    // The left edge (handle 7) only moves x.
    h.run_steps(2);
    let c = rect(&h, "viewer.regionOfInterest.handle.7").center();
    let to = pos2(screen(&h, [150.0, 0.0]).x, c.y + 40.0);
    drag(&mut h, c, to);
    let r = h.state().session.state.region_of_interest.expect("roi");
    assert!((r[0] - 150.0).abs() <= 1.0 && (r[1] - 50.0).abs() <= 1.0 && (r[2] - 250.0).abs() <= 2.0 && (r[3] - 250.0).abs() <= 2.0, "{r:?}");
}

#[test]
fn pan_behind_snaps_the_anchor_point() {
    let mut h = harness();
    let box_id = h.state().session.active_comp().unwrap().layers[0].id;
    h.state_mut().session.execute("prop.set", json!({"layer": box_id.0, "path": "transform/position", "value": [100, 100, 0]})).unwrap();
    h.state_mut().session.execute("layer.select", json!({"layers": [box_id.0]})).unwrap();
    h.state_mut().ui.tool = Tool::PanBehind;
    h.run_steps(2);
    // Drag the anchor (at the box centre) to 3 px from the comp centre: it snaps there, and the
    // position follows so the box doesn't move.
    let (from, to) = (screen(&h, [100.0, 100.0]), screen(&h, [323.0, 182.0]));
    hold_drag(&mut h, from, to, Default::default());
    let (p, a) = position_and_anchor(&h, box_id);
    assert!((p[0] - 320.0).abs() < 0.01 && (p[1] - 180.0).abs() < 0.01, "anchor point snapped to the comp centre: {p:?}");
    assert!((a[0] - 260.0).abs() < 0.01 && (a[1] - 120.0).abs() < 0.01, "{a:?}");
}

/// #3229: Pan Behind Shift-dragging the anchor point along x past another layer's corner a few
/// pixels off the axis lines it up with the corner's x and keeps it on the axis (it used to jump
/// onto the corner).
#[test]
fn pan_behind_shift_drag_snaps_the_anchor_along_its_axis_only() {
    let mut h = harness();
    let box_id = h.state().session.active_comp().unwrap().layers[0].id;
    // A 100×100 target at x 450–550, y -3–97: its bottom left corner at (450, 97), 3 px above
    // the anchor point's row. The anchor point (100, 100) sits 10 px above the box's centre, so
    // none of the box's own lines runs along that row.
    let target = h.state_mut().session.execute("layer.newSolid", json!({"name": "Target", "color": "#20e040", "width": 100, "height": 100})).unwrap()["layer"]
        .as_u64()
        .unwrap();
    let s = &mut h.state_mut().session;
    s.execute("prop.set", json!({"layer": target, "path": "transform/position", "value": [500, 47, 0]})).unwrap();
    s.execute("prop.set", json!({"layer": box_id.0, "path": "transform/anchor", "value": [40, 30, 0]})).unwrap();
    s.execute("prop.set", json!({"layer": box_id.0, "path": "transform/position", "value": [100, 100, 0]})).unwrap();
    s.execute("layer.select", json!({"layers": [box_id.0]})).unwrap();
    h.state_mut().ui.tool = Tool::PanBehind;
    h.run_steps(2);
    // Shift-drag the anchor right (and a pixel down) to 2 px short of the corner's x. The drag
    // starts in small steps (Pan Behind picks the layer where the drag starts, still inside the
    // box) and holds the end point (the gesture applies the previous frame's pointer).
    let path = [screen(&h, [100.0, 100.0]), screen(&h, [110.0, 100.0]), screen(&h, [448.0, 101.0]), screen(&h, [448.0, 101.0])];
    drag_path(&mut h, &path, egui::Modifiers::SHIFT);
    let (p, a) = position_and_anchor(&h, box_id);
    assert!((p[0] - 450.0).abs() < 0.01 && (a[0] - 390.0).abs() < 0.01, "the anchor point lines up with the corner: {p:?} {a:?}");
    assert!((p[1] - 100.0).abs() < 1e-6 && (a[1] - 30.0).abs() < 1e-6, "and stays on the axis: {p:?} {a:?}");
}

/// Issue #147: Pan Behind snaps the anchor to its own layer's corners, edge midpoints and centre
/// (only the anchor itself is left out: the box stays put while the anchor moves).
#[test]
fn pan_behind_snaps_the_anchor_to_its_own_layer() {
    let mut h = harness();
    let box_id = h.state().session.active_comp().unwrap().layers[0].id;
    h.state_mut().session.execute("prop.set", json!({"layer": box_id.0, "path": "transform/position", "value": [100, 100, 0]})).unwrap();
    h.state_mut().session.execute("layer.select", json!({"layers": [box_id.0]})).unwrap();
    h.state_mut().ui.tool = Tool::PanBehind;
    h.run_steps(2);
    // The 80×80 box spans (60, 60)–(140, 140). Its anchor, dragged to 3 px from the top-left
    // corner, lands on it.
    let (from, to) = (screen(&h, [100.0, 100.0]), screen(&h, [63.0, 62.0]));
    hold_drag(&mut h, from, to, Default::default());
    let (p, a) = position_and_anchor(&h, box_id);
    assert!(a[0].abs() < 0.01 && a[1].abs() < 0.01, "anchor point snapped to the corner: {a:?}");
    assert!((p[0] - 60.0).abs() < 0.01 && (p[1] - 60.0).abs() < 0.01, "the box stays put: {p:?}");
    // And back to near the centre: it snaps there.
    let (from, to) = (screen(&h, [61.0, 61.0]), screen(&h, [103.0, 98.0]));
    hold_drag(&mut h, from, to, Default::default());
    let (p, a) = position_and_anchor(&h, box_id);
    assert!((a[0] - 40.0).abs() < 0.01 && (a[1] - 40.0).abs() < 0.01, "anchor point snapped to the centre: {a:?}");
    assert!((p[0] - 100.0).abs() < 0.01 && (p[1] - 100.0).abs() < 0.01, "{p:?}");
}

/// Issue #162: with Pan Behind, Alt-drag moves the anchor point alone (the layer shifts, its
/// position stays), Shift keeps the move to one axis, and Ctrl+double-clicking the tool's button
/// centres the anchor point in the layer content.
#[test]
fn pan_behind_alt_moves_the_anchor_alone_shift_constrains_and_the_tool_button_centres_it() {
    let mut h = harness();
    let box_id = h.state().session.active_comp().unwrap().layers[0].id;
    h.state_mut().session.execute("prop.set", json!({"layer": box_id.0, "path": "transform/position", "value": [100, 100, 0]})).unwrap();
    h.state_mut().session.execute("layer.select", json!({"layers": [box_id.0]})).unwrap();
    h.state_mut().session.execute("view.snapping", json!({"value": false})).unwrap();
    h.state_mut().ui.tool = Tool::PanBehind;
    h.run_steps(2);
    let close = |a: [f64; 3], b: [f64; 2]| (a[0] - b[0]).abs() < 0.01 && (a[1] - b[1]).abs() < 0.01;
    // Alt: the anchor point moves by the drag, Position stays.
    let (from, to) = (screen(&h, [100.0, 100.0]), screen(&h, [120.0, 105.0]));
    hold_drag(&mut h, from, to, egui::Modifiers::ALT);
    let (p, a) = position_and_anchor(&h, box_id);
    assert!(close(a, [60.0, 45.0]) && close(p, [100.0, 100.0]), "Alt moves the anchor point alone: {a:?} {p:?}");
    // Shift: the move keeps to the axis dragged along most (Position compensates).
    h.state_mut().session.execute("prop.set", json!({"layer": box_id.0, "path": "transform/anchor", "value": [40, 40, 0]})).unwrap();
    h.run_steps(2);
    let (from, to) = (screen(&h, [100.0, 100.0]), screen(&h, [130.0, 110.0]));
    hold_drag(&mut h, from, to, egui::Modifiers::SHIFT);
    let (p, a) = position_and_anchor(&h, box_id);
    assert!(close(a, [70.0, 40.0]) && close(p, [130.0, 100.0]), "Shift keeps to x: {a:?} {p:?}");
    // Ctrl+double-click the tool's button: Center Anchor Point in Layer Content (the layer stays).
    let ctrl = egui::Modifiers { ctrl: true, command: true, ..Default::default() };
    let b = rect(&h, "tools.PanBehind").center();
    h.input_mut().events.push(Event::ModifiersChanged(ctrl));
    h.input_mut().events.push(Event::PointerMoved(b));
    h.step();
    for _ in 0..2 {
        h.input_mut().events.push(Event::PointerButton { pos: b, button: egui::PointerButton::Primary, pressed: true, modifiers: ctrl });
        h.input_mut().events.push(Event::PointerButton { pos: b, button: egui::PointerButton::Primary, pressed: false, modifiers: ctrl });
    }
    h.run_steps(2);
    h.input_mut().events.push(Event::ModifiersChanged(Default::default()));
    h.step();
    let (p, a) = position_and_anchor(&h, box_id);
    assert!(close(a, [40.0, 40.0]) && close(p, [100.0, 100.0]), "anchor point centred: {a:?} {p:?}");
}

/// Drag from `from` to `to` with `modifiers` held, holding the end point a frame (the viewer's
/// gestures apply the pointer of the previous frame).
fn hold_drag(h: &mut Harness<'_, EffectcraftApp>, from: Pos2, to: Pos2, modifiers: egui::Modifiers) {
    h.input_mut().events.push(Event::ModifiersChanged(modifiers));
    h.input_mut().events.push(Event::PointerMoved(from));
    h.input_mut().events.push(Event::PointerButton { pos: from, button: egui::PointerButton::Primary, pressed: true, modifiers });
    h.step();
    for i in 1..=10 {
        h.input_mut().events.push(Event::PointerMoved(from + (to - from) * (i.min(8) as f32 / 8.0)));
        h.step();
    }
    h.input_mut().events.push(Event::PointerButton { pos: to, button: egui::PointerButton::Primary, pressed: false, modifiers });
    h.run_steps(2);
    h.input_mut().events.push(Event::ModifiersChanged(Default::default()));
    h.step();
}

/// A layer's Position and Anchor Point.
fn position_and_anchor(h: &Harness<'_, EffectcraftApp>, layer: LayerId) -> ([f64; 3], [f64; 3]) {
    let l = h.state().session.active_comp().unwrap().layer(layer).unwrap().clone();
    (l.props.prop("transform/position").unwrap().value.as_vec3(), l.props.prop("transform/anchor").unwrap().value.as_vec3())
}

#[test]
fn reference_axes_toggle_from_the_grid_menu() {
    let mut h = harness();
    // A 3D layer makes the axes relevant.
    let box_id = h.state().session.active_comp().unwrap().layers[0].id;
    h.state_mut().session.execute("layer.setSwitch", json!({"layers": [box_id.0], "switch": "threeD", "value": true})).unwrap();
    h.run_steps(3);
    assert!(h.state().auto.find("viewer.referenceAxes").is_some(), "on by default (Settings ▸ 3D)");
    // Issue #64: one compass, not two.
    assert_eq!(h.state().auto.query("viewer.referenceAxes").len(), 1);
    let g = rect(&h, "viewer.grid").center();
    click(&mut h, g);
    h.run_steps(2);
    let item = rect(&h, "viewer.gridItem.6").center();
    click(&mut h, item);
    h.run_steps(3);
    assert!(!h.state().session.prefs.three_d.show_reference_axes);
    assert!(h.state().auto.find("viewer.referenceAxes").is_none());
}

/// A Box layer pinned with two Position pins and a Bend pin in the middle (Bend tool active).
fn puppet_harness() -> (Harness<'static, EffectcraftApp>, u64, u64) {
    let mut h = harness();
    let s = &mut h.state_mut().session;
    // No full-frame Plate behind the Box: the comp around the Box is empty, like the canvas
    // around a character.
    s.execute("edit.clear", json!({"layers": ["Plate"]})).unwrap();
    s.execute("layer.select", json!({"layers": ["Box"]})).unwrap();
    let id = s.state.selected_layers[0].0;
    s.execute("puppet.addPin", json!({"layer": id, "position": [10, 40]})).unwrap();
    s.execute("puppet.addPin", json!({"layer": id, "position": [70, 40]})).unwrap();
    let bend = s.execute("puppet.addPin", json!({"layer": id, "kind": "bend", "position": [40, 40]})).unwrap()["pin"].as_u64().unwrap();
    h.state_mut().ui.tool = Tool::PuppetBend;
    h.run_steps(3);
    (h, id, bend)
}

fn pin_value(h: &Harness<'_, EffectcraftApp>, layer: u64, pin: u64, prop: &str) -> f64 {
    let l = h.state().session.active_comp().unwrap().layer(LayerId(layer)).unwrap().clone();
    l.props.find_group(pin).unwrap().get(prop).unwrap().value.as_f64()
}

#[test]
fn puppet_bend_pin_rotates_by_its_ring_and_scales_by_its_square() {
    let (mut h, id, bend) = puppet_harness();
    let center = rect(&h, &format!("viewer.puppetPin.{bend}")).center();
    let ring = rect(&h, &format!("viewer.puppetPin.{bend}.rotate")).center();
    let square = rect(&h, &format!("viewer.puppetPin.{bend}.scale")).center();
    // At 0° the square is on the right of the ring, the rotate target opposite it.
    assert!(square.x > center.x + 7.0 && (square.y - center.y).abs() < 0.5, "{square:?} {center:?}");
    // Drag the ring a quarter turn clockwise (left → top): +90°, one undo step.
    let undo_before = h.state().session.history.undo.len();
    drag(&mut h, ring, center + vec2(0.0, -(center.x - ring.x)));
    let r = pin_value(&h, id, bend, "rotation");
    assert!((r - 90.0).abs() < 1.0, "rotation {r}");
    assert_eq!(h.state().session.history.undo.len(), undo_before + 1);
    // The square follows the rotation (now below the pin; the bend carries the pin itself a
    // little); dragging it twice as far from the pin scales 200 %.
    let center = rect(&h, &format!("viewer.puppetPin.{bend}")).center();
    let square = rect(&h, &format!("viewer.puppetPin.{bend}.scale")).center();
    assert!(square.y > center.y + 7.0, "{square:?}");
    drag(&mut h, square, center + (square - center) * 2.0);
    let sc = pin_value(&h, id, bend, "scale");
    assert!((sc - 200.0).abs() < 2.0, "scale {sc}");
}

#[test]
fn puppet_pin_click_selects_and_delete_removes_only_the_pins() {
    let (mut h, id, bend) = puppet_harness();
    let pin = |h: &Harness<'_, EffectcraftApp>| h.state().session.state.selected_props.iter().map(|(_, u)| *u).collect::<Vec<_>>();
    // Clicking a pin selects it (instead of adding a pin on top of it).
    let at = rect(&h, &format!("viewer.puppetPin.{bend}")).center();
    click(&mut h, at);
    assert_eq!(pin(&h), vec![bend]);
    let info = h.state_mut().session.execute("puppet.info", json!({"layer": id})).unwrap();
    assert_eq!(info["meshes"][0]["pins"].as_array().unwrap().len(), 3, "no pin added");
    // Delete removes the pin; the layer stays.
    h.input_mut().events.push(Event::Key { key: egui::Key::Delete, physical_key: None, pressed: true, repeat: false, modifiers: Default::default() });
    h.run_steps(2);
    let info = h.state_mut().session.execute("puppet.info", json!({"layer": id})).unwrap();
    assert_eq!(info["meshes"][0]["pins"].as_array().unwrap().len(), 2);
    assert!(h.state().session.active_comp().unwrap().layer(LayerId(id)).is_some());
}

/// A pin's Position property and its keys' times (seconds).
fn pin_keys(h: &Harness<'_, EffectcraftApp>, layer: u64, pin: u64) -> (u64, Vec<f64>) {
    let l = h.state().session.active_comp().unwrap().layer(LayerId(layer)).unwrap().clone();
    let pos = l.props.find_group(pin).unwrap().get("position").unwrap().clone();
    (pos.uid, pos.keys.iter().map(|k| k.time.seconds()).collect())
}

/// The keys the Timeline draws for a property, left to right.
fn timeline_keys(h: &Harness<'_, EffectcraftApp>, prop: u64) -> Vec<Pos2> {
    let mut ks: Vec<Pos2> =
        h.state().auto.query(&format!("timeline.key.{prop}.")).iter().map(|e| pos2(e.rect[0] + e.rect[2] / 2.0, e.rect[1] + e.rect[3] / 2.0)).collect();
    ks.sort_by(|a, b| a.x.total_cmp(&b.x));
    ks
}

/// The pin the last click placed (placing a pin selects it).
fn placed_pin(h: &Harness<'_, EffectcraftApp>) -> u64 {
    h.state().session.state.selected_props.last().map(|(_, u)| *u).unwrap()
}

/// #273: U shows each pin's keyframed Position under its pin (Puppet ▸ Mesh 1 ▸ Deform ▸ Puppet
/// Pin 1 ▸ Position) rather than as one more "Position"; a pin moved or placed in the viewer
/// afterwards shows its keys there too (they were made but not shown); and pin keys select, move
/// and delete like any others.
#[test]
fn puppet_pin_keys_show_under_their_pins_in_the_timeline() {
    let (mut h, id, bend) = puppet_harness();
    h.state_mut().ui.tool = Tool::Puppet;
    let pins: Vec<u64> = h.state().auto.previous.iter().filter_map(|e| e.id.strip_prefix("viewer.puppetPin.")?.parse().ok()).filter(|p| *p != bend).collect();
    let ctx = h.ctx.clone();
    effectcraft_ui_egui::menus::invoke(h.state_mut(), &ctx, "anim.reveal", json!({"kind": "keyframes"})).unwrap();
    h.run_steps(3);
    assert_eq!(pins.len(), 2);
    for pin in &pins {
        let row = h.state().auto.find(&format!("timeline.group.{pin}.name")).map(|e| e.label.clone()).unwrap_or_default();
        assert!(row.starts_with("Puppet Pin"), "the pin's row: {row:?}");
        assert_eq!(timeline_keys(&h, pin_keys(&h, id, *pin).0).len(), 1, "its key at 0 s");
    }
    // Moved at 1 s: keyed there, in view.
    h.state_mut().session.set_time(effectcraft_engine::time::Tick::from_seconds_f64(1.0));
    h.run_steps(2);
    let pa = rect(&h, &format!("viewer.puppetPin.{}", pins[0])).center();
    drag(&mut h, pa, pa + vec2(20.0, 10.0));
    let (pos, times) = pin_keys(&h, id, pins[0]);
    assert_eq!(times.len(), 2);
    assert_eq!(timeline_keys(&h, pos).len(), 2, "both keys drawn");
    // A pin placed after U shows with its key too.
    let at = screen(&h, [320.0, 210.0]);
    click(&mut h, at);
    let new = placed_pin(&h);
    assert!(!pins.contains(&new) && new != bend);
    assert!(h.state().auto.find(&format!("timeline.group.{new}.name")).is_some(), "the new pin's row");
    assert_eq!(timeline_keys(&h, pin_keys(&h, id, new).0).len(), 1, "and its key");
    // Click the 1 s key, drag it half a second later, delete it.
    let ks = timeline_keys(&h, pos);
    click(&mut h, ks[1]);
    assert_eq!(h.state().session.state.selected_keys.iter().map(|k| k.prop).collect::<Vec<_>>(), [pos]);
    let half = (ks[1].x - ks[0].x) / 2.0;
    drag(&mut h, ks[1], ks[1] + vec2(half, 0.0));
    let t = pin_keys(&h, id, pins[0]).1;
    assert!((t[1] - 1.5).abs() < 0.05, "moved: {t:?}");
    h.input_mut().events.push(Event::Key { key: egui::Key::Delete, physical_key: None, pressed: true, repeat: false, modifiers: Default::default() });
    h.run_steps(2);
    assert_eq!(pin_keys(&h, id, pins[0]).1, [0.0], "deleted; the pin stays");
}

/// #273: on a layer twirled open in the Timeline, placing a pin twirls open Effects ▸ Puppet ▸
/// Mesh 1 ▸ Deform ▸ the pin, so its Position key shows; a collapsed layer stays collapsed, as
/// in After Effects.
#[test]
fn placing_a_pin_opens_its_groups_on_a_twirled_open_layer() {
    let mut h = harness();
    let s = &mut h.state_mut().session;
    s.execute("edit.clear", json!({"layers": ["Plate"]})).unwrap();
    s.execute("layer.select", json!({"layers": ["Box"]})).unwrap();
    let id = s.state.selected_layers[0].0;
    h.state_mut().ui.tool = Tool::Puppet;
    h.run_steps(2);
    let at = screen(&h, [300.0, 160.0]);
    click(&mut h, at);
    assert!(h.state().ui.timeline.open_groups.is_empty() && h.state().ui.timeline.open_layers.is_empty(), "collapsed stays collapsed");
    let twirl = rect(&h, &format!("timeline.layer.{id}.twirl")).center();
    click(&mut h, twirl);
    assert!(h.state().ui.timeline.open_layers.contains(&id));
    let at = screen(&h, [340.0, 200.0]);
    click(&mut h, at);
    let pin = placed_pin(&h);
    assert_eq!(timeline_keys(&h, pin_keys(&h, id, pin).0).len(), 1, "the new pin's Position key shows");
}

/// #284: the Puppet tools stay on the layer being rigged: a pin goes on its mesh where the
/// deformation has stretched it past the layer's own bounds (over another layer), and a click on
/// another layer neither selects it nor rigs it.
#[test]
fn puppet_pins_go_on_the_deformed_mesh_and_the_tool_stays_on_the_rigged_layer() {
    let mut h = harness();
    let s = &mut h.state_mut().session;
    s.execute("layer.select", json!({"layers": ["Box"]})).unwrap();
    let (boxl, plate) = (s.state.selected_layers[0], s.active_comp().unwrap().layers[1].id);
    s.execute("puppet.addPin", json!({"layer": boxl.0, "position": [10, 40]})).unwrap();
    let right = s.execute("puppet.addPin", json!({"layer": boxl.0, "position": [70, 40]})).unwrap()["pin"].as_u64().unwrap();
    // Stretch the Box to the right: its right edge (layer x 80, comp x 360) moves well past 400.
    s.execute("puppet.movePin", json!({"layer": boxl.0, "pin": right, "position": [140, 40]})).unwrap();
    h.state_mut().ui.tool = Tool::Puppet;
    h.run_steps(3);
    let pins = |h: &mut Harness<'_, EffectcraftApp>| {
        let info = h.state_mut().session.execute("puppet.info", json!({"layer": boxl.0})).unwrap();
        info["meshes"].as_array().unwrap().iter().map(|m| m["pins"].as_array().unwrap().len()).sum::<usize>()
    };
    let rigged = |h: &Harness<'_, EffectcraftApp>, l: LayerId| {
        h.state().session.active_comp().unwrap().layer(l).unwrap().effects().is_some_and(|f| f.groups().next().is_some())
    };
    // Comp (380, 180) is layer (100, 40): past the Box's own edge, over the Plate, on the
    // stretched mesh.
    let at = screen(&h, [380.0, 180.0]);
    click(&mut h, at);
    assert_eq!(pins(&mut h), 3, "the pin went on the deformed mesh");
    assert_eq!(h.state().session.state.selected_layers, vec![boxl]);
    assert!(!rigged(&h, plate), "the Plate got no Puppet");
    // Far from the Box, on the Plate only: nothing.
    let at = screen(&h, [40.0, 40.0]);
    click(&mut h, at);
    assert_eq!(pins(&mut h), 3);
    assert_eq!(h.state().session.state.selected_layers, vec![boxl], "the tool stays on the Box");
    assert!(!rigged(&h, plate));
}

/// #2865: a press where the deformed layer is empty, though on its original bounds, adds nothing:
/// a click leaves the rig alone and a drag from there is a pin marquee.
#[test]
fn puppet_press_where_the_deformed_layer_is_empty_is_a_marquee_not_a_copy() {
    let mut h = harness();
    let s = &mut h.state_mut().session;
    s.execute("edit.clear", json!({"layers": ["Plate"]})).unwrap();
    s.execute("layer.select", json!({"layers": ["Box"]})).unwrap();
    let id = s.state.selected_layers[0].0;
    let a = s.execute("puppet.addPin", json!({"layer": id, "position": [10, 40]})).unwrap()["pin"].as_u64().unwrap();
    let b = s.execute("puppet.addPin", json!({"layer": id, "position": [70, 40]})).unwrap()["pin"].as_u64().unwrap();
    // Carry the Box 60 px right: its left part (comp x 280..340) is now empty.
    s.execute("puppet.movePin", json!({"layer": id, "pin": a, "position": [70, 40]})).unwrap();
    s.execute("puppet.movePin", json!({"layer": id, "pin": b, "position": [130, 40]})).unwrap();
    h.state_mut().ui.tool = Tool::Puppet;
    h.run_steps(3);
    let rig = |h: &mut Harness<'_, EffectcraftApp>| {
        let info = h.state_mut().session.execute("puppet.info", json!({"layer": id})).unwrap();
        let meshes = info["meshes"].as_array().unwrap().clone();
        (meshes.len(), meshes.iter().map(|m| m["pins"].as_array().unwrap().len()).sum::<usize>())
    };
    let at = screen(&h, [285.0, 180.0]);
    click(&mut h, at);
    assert_eq!(rig(&mut h), (1, 2), "the click made nothing");
    let comp = rect(&h, "viewer.comp");
    drag(&mut h, at, comp.max - vec2(4.0, 4.0));
    assert_eq!(rig(&mut h), (1, 2), "nor did the drag");
    let mut sel: Vec<u64> = h.state().session.state.selected_props.iter().map(|(_, u)| *u).collect();
    sel.sort();
    assert_eq!(sel, [a, b], "the drag's marquee selected the pins");
    // On the deformed shape a click still pins it.
    let at = screen(&h, [380.0, 180.0]);
    click(&mut h, at);
    assert_eq!(rig(&mut h), (1, 3));
}

/// Filled circles painted this frame (flattening nested shape lists).
fn circles(h: &Harness<'_, EffectcraftApp>) -> Vec<egui::epaint::CircleShape> {
    fn walk(s: &egui::Shape, out: &mut Vec<egui::epaint::CircleShape>) {
        match s {
            egui::Shape::Circle(c) => out.push(*c),
            egui::Shape::Vec(v) => v.iter().for_each(|s| walk(s, out)),
            _ => {}
        }
    }
    let mut out = vec![];
    for c in &h.output().shapes {
        walk(&c.shape, &mut out);
    }
    out
}

/// Issue #96: a selected puppet pin is filled with its colour and ringed; an unselected one is
/// hollow (dark centre, coloured outline), like selected and unselected mask vertices. They used to
/// differ by 1 px and a slight brightness change.
#[test]
fn selected_puppet_pins_are_filled_and_unselected_hollow() {
    let (mut h, _id, bend) = puppet_harness();
    let pins: Vec<u64> = h.state().auto.previous.iter().filter_map(|e| e.id.strip_prefix("viewer.puppetPin.").and_then(|r| r.parse().ok())).collect();
    let position: Vec<u64> = pins.into_iter().filter(|p| *p != bend).collect();
    let (a, b) = (position[0], position[1]);
    let pa = rect(&h, &format!("viewer.puppetPin.{a}")).center();
    let pb = rect(&h, &format!("viewer.puppetPin.{b}")).center();
    click(&mut h, pa);
    assert_eq!(h.state().session.state.selected_props.iter().map(|(_, u)| *u).collect::<Vec<_>>(), vec![a]);
    // Pointer away from the pins (hover enlarges a pin), then look at the painted pins.
    h.input_mut().events.push(Event::PointerMoved(pos2(5.0, 5.0)));
    h.run_steps(2);
    let col = effectcraft_ui_egui::panels::puppet_tool::pin_color(effectcraft_engine::effects::puppet::PinKind::Position);
    let at = |p: Pos2| circles(&h).into_iter().filter(move |c| c.center.distance(p) < 0.5).collect::<Vec<_>>();
    let dark = |c: egui::Color32| c.r().max(c.g()).max(c.b()) < 0x60;
    let sel = at(pa);
    assert!(sel.iter().any(|c| c.fill == col), "the selected pin is filled with its colour: {sel:?}");
    assert!(sel.iter().any(|c| c.fill == egui::Color32::TRANSPARENT && c.stroke.color == egui::Color32::WHITE), "and ringed in white: {sel:?}");
    let unsel = at(pb);
    assert!(!unsel.is_empty(), "the unselected pin is drawn");
    assert!(unsel.iter().all(|c| c.fill == egui::Color32::TRANSPARENT || dark(c.fill)), "the unselected pin is hollow: {unsel:?}");
    assert!(unsel.iter().any(|c| c.stroke.color == col), "with a coloured outline: {unsel:?}");
}

#[test]
fn puppet_marquee_selects_pins_and_alt_drag_works_over_the_art() {
    let (mut h, _id, bend) = puppet_harness();
    let p1 = rect(&h, &format!("viewer.puppetPin.{bend}")).center();
    let pins: Vec<u64> = h.state().auto.previous.iter().filter_map(|e| e.id.strip_prefix("viewer.puppetPin.").and_then(|r| r.parse().ok())).collect();
    assert_eq!(pins.len(), 3, "{pins:?}");
    // A box around every pin, started on empty canvas (outside all art).
    let comp = rect(&h, "viewer.comp");
    drag(&mut h, comp.min + vec2(4.0, 4.0), comp.max - vec2(4.0, 4.0));
    let mut sel: Vec<u64> = h.state().session.state.selected_props.iter().map(|(_, u)| *u).collect();
    sel.sort();
    let mut want = pins.clone();
    want.sort();
    assert_eq!(sel, want);
    // Alt-drag a small box over the art (around the Bend pin only).
    h.input_mut().events.push(Event::PointerMoved(p1 - vec2(6.0, 6.0)));
    h.input_mut().events.push(Event::PointerButton {
        pos: p1 - vec2(6.0, 6.0),
        button: egui::PointerButton::Primary,
        pressed: true,
        modifiers: egui::Modifiers::ALT,
    });
    h.step();
    for i in 1..=6 {
        h.input_mut().events.push(Event::PointerMoved(p1 - vec2(6.0, 6.0) + vec2(12.0, 12.0) * (i as f32 / 6.0)));
        h.step();
    }
    h.input_mut().events.push(Event::PointerButton {
        pos: p1 + vec2(6.0, 6.0),
        button: egui::PointerButton::Primary,
        pressed: false,
        modifiers: egui::Modifiers::ALT,
    });
    h.run_steps(2);
    let sel: Vec<u64> = h.state().session.state.selected_props.iter().map(|(_, u)| *u).collect();
    assert_eq!(sel, vec![bend]);
}

/// Drag with `button` from `from` to `to` in steps, then release.
fn drag_with(h: &mut Harness<'_, EffectcraftApp>, button: egui::PointerButton, from: Pos2, to: Pos2) {
    h.input_mut().events.push(Event::PointerMoved(from));
    h.input_mut().events.push(Event::PointerButton { pos: from, button, pressed: true, modifiers: Default::default() });
    h.step();
    for i in 1..=8 {
        h.input_mut().events.push(Event::PointerMoved(from + (to - from) * (i as f32 / 8.0)));
        h.step();
    }
    h.input_mut().events.push(Event::PointerButton { pos: to, button, pressed: false, modifiers: Default::default() });
    h.run_steps(3);
}

#[test]
fn middle_and_hand_drags_pan_the_viewer_and_the_pan_stays_after_release() {
    let mut h = harness();
    let c = rect(&h, "viewer.comp").center();
    let before = h.state().ui.viewer.pan;
    // Middle-button drag with the Selection tool: pans, and stays panned after the release.
    drag_with(&mut h, egui::PointerButton::Middle, c, c + vec2(120.0, 60.0));
    let after = h.state().ui.viewer.pan;
    assert!((after[0] - before[0] - 120.0).abs() < 1.0 && (after[1] - before[1] - 60.0).abs() < 1.0, "{before:?} → {after:?}");
    assert_eq!(h.state().ui.tool, Tool::Selection, "the tool is unchanged");
    // The Hand tool's primary drag keeps its pan too.
    h.state_mut().ui.tool = Tool::Hand;
    h.run_steps(2);
    let c = rect(&h, "viewer.comp").center();
    drag_with(&mut h, egui::PointerButton::Primary, c, c - vec2(50.0, 30.0));
    let end = h.state().ui.viewer.pan;
    assert!((end[0] - after[0] + 50.0).abs() < 1.0 && (end[1] - after[1] + 30.0).abs() < 1.0, "{after:?} → {end:?}");
}

/// #2854: a middle-drag over a layer pans the view and leaves the layer alone, selected or not,
/// with any tool, and adds no undo step.
#[test]
fn middle_drag_over_a_layer_pans_and_leaves_the_layer_where_it_is() {
    let mut h = harness();
    let id = layer_named(&h, "Box");
    let c = rect(&h, "viewer.comp").center();
    for (select, tool) in [(false, Tool::Selection), (true, Tool::Selection), (true, Tool::Rotate), (true, Tool::Type)] {
        let selection = if select { json!({"layers": [id.0]}) } else { json!({"layers": []}) };
        h.state_mut().session.execute("layer.select", selection).unwrap();
        h.state_mut().ui.tool = tool;
        h.state_mut().ui.viewer.pan = [0.0, 0.0];
        h.run_steps(2);
        let (p0, steps) = (position_of(&h, id), h.state().session.history.undo.len());
        drag_with(&mut h, egui::PointerButton::Middle, c, c + vec2(120.0, 60.0));
        let pan = h.state().ui.viewer.pan;
        assert!((pan[0] - 120.0).abs() < 1.0 && (pan[1] - 60.0).abs() < 1.0, "{select} {tool:?}: {pan:?}");
        assert_eq!(position_of(&h, id), p0, "{select} {tool:?}: the layer stays");
        assert_eq!(h.state().session.history.undo.len(), steps, "{select} {tool:?}: no undo step");
    }
}

/// #2854: a left drag whose release the viewer never saw (the panel wasn't drawn on that frame)
/// leaves its move gesture stored. A middle press must not run it.
#[test]
fn middle_drag_does_not_run_a_gesture_left_over_from_a_missed_release() {
    let mut h = harness();
    let id = layer_named(&h, "Box");
    h.state_mut().session.execute("layer.select", json!({"layers": [id.0]})).unwrap();
    h.run_steps(2);
    let c = rect(&h, "viewer.comp").center();
    let p = egui::PointerButton::Primary;
    h.input_mut().events.push(Event::PointerMoved(c));
    h.input_mut().events.push(Event::PointerButton { pos: c, button: p, pressed: true, modifiers: Default::default() });
    h.step();
    for i in 1..=4 {
        h.input_mut().events.push(Event::PointerMoved(c + vec2(10.0 * i as f32, 0.0)));
        h.step();
    }
    let ctx = h.ctx.clone();
    effectcraft_ui_egui::menus::invoke(h.state_mut(), &ctx, "layer.openLayer", json!({"layer": id.0})).unwrap();
    h.step();
    h.input_mut().events.push(Event::PointerButton { pos: c + vec2(40.0, 0.0), button: p, pressed: false, modifiers: Default::default() });
    h.run_steps(2);
    h.state_mut().show_panel(effectcraft_ui_egui::dock::PanelKind::Composition);
    h.run_steps(3);
    let c = rect(&h, "viewer.comp").center();
    let p0 = position_of(&h, id);
    drag_with(&mut h, egui::PointerButton::Middle, c, c + vec2(120.0, 60.0));
    assert_eq!(position_of(&h, id), p0, "the stored move didn't run");
}

fn layer_named(h: &Harness<'_, EffectcraftApp>, name: &str) -> LayerId {
    h.state().session.active_comp().unwrap().layers.iter().find(|l| l.name == name).unwrap().id
}

fn position_of(h: &Harness<'_, EffectcraftApp>, id: LayerId) -> [f64; 3] {
    h.state().session.active_comp().unwrap().layer(id).unwrap().props.prop("transform/position").unwrap().value.as_vec3()
}

/// A Spacebar press or release (`repeat`: the keyboard's auto-repeat while it is held).
fn space(h: &mut Harness<'_, EffectcraftApp>, pressed: bool, repeat: bool) {
    h.input_mut().events.push(Event::Key { key: egui::Key::Space, physical_key: None, pressed, repeat, modifiers: Default::default() });
    h.step();
}

/// #227: a Spacebar tap starts and stops the preview when it is released; held, Spacebar is the
/// Hand tool, so a drag pans the viewer and neither starts nor stops the preview. Auto-repeat
/// while it is held toggles nothing more, and a space typed in a text field doesn't preview.
#[test]
fn spacebar_taps_preview_and_held_spacebar_pans_the_viewer() {
    let mut h = harness();
    let playing = |h: &Harness<'_, EffectcraftApp>| h.state().playback.playing;
    let tap = |h: &mut Harness<'_, EffectcraftApp>| {
        space(h, true, false);
        space(h, false, false);
    };
    space(&mut h, true, false);
    assert!(!playing(&h), "the press alone doesn't play");
    space(&mut h, false, false);
    assert!(playing(&h), "the release does");
    tap(&mut h);
    assert!(!playing(&h), "another tap stops");
    // Held with auto-repeat: one toggle, on the release.
    space(&mut h, true, false);
    for _ in 0..5 {
        space(&mut h, true, true);
    }
    assert!(!playing(&h));
    space(&mut h, false, false);
    assert!(playing(&h));
    tap(&mut h);

    // Held and dragged: pans the viewer (the Box under the pointer stays), and plays nothing.
    let box_pos = |h: &Harness<'_, EffectcraftApp>| h.state().session.active_comp().unwrap().layers[0].props.prop("transform/position").unwrap().value.clone();
    let (pos, before) = (box_pos(&h), h.state().ui.viewer.pan);
    let c = rect(&h, "viewer.comp").center();
    space(&mut h, true, false);
    drag_with(&mut h, egui::PointerButton::Primary, c, c + vec2(80.0, 40.0));
    space(&mut h, false, false);
    let after = h.state().ui.viewer.pan;
    assert!((after[0] - before[0] - 80.0).abs() < 1.0 && (after[1] - before[1] - 40.0).abs() < 1.0, "{before:?} → {after:?}");
    assert_eq!(box_pos(&h), pos);
    assert!(!playing(&h), "a Spacebar drag doesn't start the preview");
    // Nor stop one.
    tap(&mut h);
    let c = rect(&h, "viewer.comp").center();
    space(&mut h, true, false);
    drag_with(&mut h, egui::PointerButton::Primary, c, c - vec2(30.0, 0.0));
    space(&mut h, false, false);
    assert!(playing(&h), "a Spacebar drag doesn't stop the preview");
    tap(&mut h);
    assert!(!playing(&h));

    // In a text field Spacebar types.
    let search = rect(&h, "timeline.search").center();
    click(&mut h, search);
    tap(&mut h);
    assert!(!playing(&h), "a space typed in the Timeline search doesn't preview");
}

#[test]
fn timeline_rows_drag_to_reorder_layers() {
    let mut h = harness();
    let names = |h: &Harness<'_, EffectcraftApp>| h.state().session.active_comp().unwrap().layers.iter().map(|l| l.name.clone()).collect::<Vec<_>>();
    assert_eq!(names(&h), ["Box", "Plate"]);
    let row = |h: &Harness<'_, EffectcraftApp>, name: &str| {
        let l = h.state().session.active_comp().unwrap().layers.iter().find(|l| l.name == name).unwrap().id.0;
        rect(h, &format!("timeline.layer.{l}.row"))
    };
    // Drag "Box" (top) below "Plate": it becomes the bottom layer, in one undo step.
    let (from, plate) = (row(&h, "Box").center(), row(&h, "Plate"));
    let steps = h.state().session.history.undo.len();
    drag(&mut h, from, plate.center() + vec2(0.0, plate.height() * 0.4));
    assert_eq!(names(&h), ["Plate", "Box"]);
    assert_eq!(h.state().session.history.undo.len(), steps + 1);
    // Dropping a layer where it already is changes nothing.
    let b = row(&h, "Box").center();
    drag(&mut h, b, b + vec2(20.0, 0.0));
    assert_eq!(names(&h), ["Plate", "Box"]);
    assert_eq!(h.state().session.history.undo.len(), steps + 1);
    // And back to the top.
    let (from, top) = (row(&h, "Box").center(), row(&h, "Plate"));
    drag(&mut h, from, top.center() - vec2(0.0, top.height() * 0.4));
    assert_eq!(names(&h), ["Box", "Plate"]);
}

fn timeline_layer_order(h: &Harness<'_, EffectcraftApp>) -> Vec<u64> {
    h.state().session.active_comp().unwrap().layers.iter().map(|l| l.id.0).collect()
}

fn timeline_layer_row(h: &Harness<'_, EffectcraftApp>, layer: u64) -> Rect {
    rect(h, &format!("timeline.layer.{layer}.row"))
}

fn start_timeline_row_drag(h: &mut Harness<'_, EffectcraftApp>, from: Pos2) {
    h.input_mut().events.push(Event::PointerMoved(from));
    h.input_mut().events.push(Event::PointerButton { pos: from, button: egui::PointerButton::Primary, pressed: true, modifiers: Default::default() });
    h.step();
    h.input_mut().events.push(Event::PointerMoved(from + vec2(20.0, 0.0)));
    h.step();
    assert_eq!(h.output().platform_output.cursor_icon, egui::CursorIcon::Grabbing, "the row drag started");
}

fn release_timeline_row_drag(h: &mut Harness<'_, EffectcraftApp>, at: Pos2) {
    h.input_mut().events.push(Event::PointerMoved(at));
    h.step();
    h.input_mut().events.push(Event::PointerButton { pos: at, button: egui::PointerButton::Primary, pressed: false, modifiers: Default::default() });
    h.run_steps(2);
}

fn scroll_timeline(h: &mut Harness<'_, EffectcraftApp>, dy: f32) {
    let at = rect(h, "timeline.layerMarquee").center();
    h.input_mut().events.push(Event::PointerMoved(at));
    h.input_mut().events.push(Event::MouseWheel {
        unit: egui::MouseWheelUnit::Point,
        delta: vec2(0.0, dy),
        modifiers: Default::default(),
        phase: egui::TouchPhase::Move,
        source: egui::MouseWheelSource::Wheel,
    });
    h.run_steps(20);
}

/// Wheel-scroll far enough to stop drawing the row that owns the held drag, then drop at
/// the other end of the stack. The release must work without that row's Response (#2492).
fn timeline_reorder_after_scroll(down: bool, multiple: bool) {
    let mut h = harness();
    for i in 0..38 {
        h.state_mut().session.execute("layer.newSolid", json!({"name": format!("Layer {i}"), "color": "#406080", "width": 80, "height": 80})).unwrap();
    }
    h.state_mut().session.execute("edit.deselectAll", json!({})).unwrap();
    h.run_steps(3);
    let before = timeline_layer_order(&h);
    let count = if multiple { 2 } else { 1 };
    let moving = if down { before[..count].to_vec() } else { before[before.len() - count..].to_vec() };
    if multiple {
        h.state_mut().session.execute("layer.select", json!({"layers": moving})).unwrap();
        h.run_steps(2);
    }
    if !down {
        scroll_timeline(&mut h, -10_000.0);
    }
    let origin = if down { before[0] } else { *before.last().unwrap() };
    let from = timeline_layer_row(&h, origin).center();
    // Setup fills the bounded Undo history; reserve room to count this gesture's steps.
    h.state_mut().session.history.clear();
    let steps = h.state().session.history.undo.len();
    start_timeline_row_drag(&mut h, from);
    scroll_timeline(&mut h, if down { -10_000.0 } else { 10_000.0 });
    assert!(h.state().auto.find(&format!("timeline.layer.{origin}.row")).is_none(), "the origin row was culled");
    assert_eq!(h.output().platform_output.cursor_icon, egui::CursorIcon::Grabbing, "scrolling keeps the held drag active");
    let target = if down { *before.last().unwrap() } else { before[0] };
    let row = timeline_layer_row(&h, target);
    let to = row.center() + vec2(0.0, row.height() * if down { 0.4 } else { -0.4 });
    release_timeline_row_drag(&mut h, to);

    let mut expected: Vec<u64> = before.iter().copied().filter(|id| !moving.contains(id)).collect();
    let at = if down { expected.len() } else { 0 };
    expected.splice(at..at, moving);
    assert_eq!(timeline_layer_order(&h), expected, "the offscreen origin still completes its drop");
    assert_eq!(h.state().session.history.undo.len(), steps + 1, "one reorder, including a selected group, is one undo step");
    assert_ne!(h.output().platform_output.cursor_icon, egui::CursorIcon::Grabbing, "release clears the drag feedback");
    h.state_mut().session.execute("edit.undo", json!({})).unwrap();
    assert_eq!(timeline_layer_order(&h), before);
    h.state_mut().session.execute("edit.redo", json!({})).unwrap();
    assert_eq!(timeline_layer_order(&h), expected);
    h.state_mut().session.execute("edit.deselectAll", json!({})).unwrap();
    h.run_steps(3);

    // A later, independent drag must not reuse the old gesture or selected group.
    let i = if down { expected.len() - 1 } else { 0 };
    let j = if down { i - 1 } else { 1 };
    let from = timeline_layer_row(&h, expected[i]).center();
    let row = timeline_layer_row(&h, expected[j]);
    drag(&mut h, from, row.center() + vec2(0.0, row.height() * if down { -0.4 } else { 0.4 }));
    expected.swap(i, j);
    assert_eq!(timeline_layer_order(&h), expected, "the next drag is independent");
    assert_eq!(h.state().session.history.undo.len(), steps + 2);
}

#[test]
fn timeline_rows_drag_to_bottom_after_scrolling() {
    for multiple in [false, true] {
        timeline_reorder_after_scroll(true, multiple);
    }
}

#[test]
fn timeline_rows_drag_to_top_after_scrolling() {
    for multiple in [false, true] {
        timeline_reorder_after_scroll(false, multiple);
    }
}

/// An intermediate scroll must not turn the viewport's lower edge into the stack's end.
fn timeline_reorder_at_viewport_edge(below: bool, multiple: bool) {
    let mut h = harness();
    for i in 0..38 {
        h.state_mut().session.execute("layer.newSolid", json!({"name": format!("Layer {i}"), "color": "#406080", "width": 80, "height": 80})).unwrap();
    }
    h.state_mut().session.execute("edit.deselectAll", json!({})).unwrap();
    h.run_steps(3);
    let before = timeline_layer_order(&h);
    let moving = before[..if multiple { 2 } else { 1 }].to_vec();
    h.state_mut().session.execute("layer.select", json!({"layers": moving})).unwrap();
    h.run_steps(2);
    h.state_mut().session.history.clear();
    let from = timeline_layer_row(&h, moving[0]).center();
    start_timeline_row_drag(&mut h, from);
    scroll_timeline(&mut h, -200.0);
    let visible = |h: &Harness<'_, EffectcraftApp>| {
        before.iter().copied().filter(|id| h.state().auto.find(&format!("timeline.layer.{id}.row")).is_some()).collect::<Vec<_>>()
    };
    // Leave space inside the viewport below the last rendered layer's midpoint.
    for _ in 0..20 {
        let last = timeline_layer_row(&h, *visible(&h).last().unwrap());
        if last.center().y < rect(&h, "timeline.layerMarquee").max.y - 3.0 {
            break;
        }
        scroll_timeline(&mut h, -10.0);
    }
    let shown = visible(&h);
    assert!(!shown.contains(&moving[0]), "the drag owner is culled");
    let last = *shown.last().unwrap();
    assert_ne!(last, *before.last().unwrap(), "layers remain below the viewport");
    let row = timeline_layer_row(&h, last);
    let viewport = rect(&h, "timeline.layerMarquee");
    let y = if below { (row.center().y + row.height() * 0.4).min(viewport.max.y - 1.0) } else { row.min.y + row.height() * 0.2 };
    let to = pos2(row.center().x, y);
    assert!(viewport.contains(to));
    assert_eq!(to.y > row.center().y, below);
    let mut expected: Vec<u64> = before.iter().copied().filter(|id| !moving.contains(id)).collect();
    let at = expected.iter().position(|id| *id == last).unwrap() + usize::from(below);
    expected.splice(at..at, moving);
    release_timeline_row_drag(&mut h, to);
    assert_eq!(timeline_layer_order(&h), expected, "the drop follows the indicated row boundary");
    assert_eq!(h.state().session.history.undo.len(), 1);
    assert_ne!(h.output().platform_output.cursor_icon, egui::CursorIcon::Grabbing);
    h.state_mut().session.execute("edit.undo", json!({})).unwrap();
    assert_eq!(timeline_layer_order(&h), before);
    h.state_mut().session.execute("edit.redo", json!({})).unwrap();
    assert_eq!(timeline_layer_order(&h), expected);
}

#[test]
fn timeline_rows_drag_after_last_visible_row() {
    for multiple in [false, true] {
        timeline_reorder_at_viewport_edge(true, multiple);
    }
}

#[test]
fn timeline_rows_drag_before_last_visible_row() {
    for multiple in [false, true] {
        timeline_reorder_at_viewport_edge(false, multiple);
    }
}

/// A removed ID must not be reinterpreted as a one-based index by layer.arrange.
fn timeline_reorder_after_deletion(delete_owner: bool) {
    let mut h = harness();
    for i in 0..6 {
        h.state_mut().session.execute("layer.newSolid", json!({"name": format!("Layer {i}"), "color": "#406080", "width": 80, "height": 80})).unwrap();
    }
    let before = timeline_layer_order(&h);
    let removed = *before.last().unwrap();
    assert!(removed > 0 && removed < before.len() as u64, "the missing ID would also resolve as a surviving layer index");
    let owner = if delete_owner { removed } else { before[0] };
    let moving = if delete_owner { vec![removed] } else { vec![owner, removed] };
    h.state_mut().session.execute("layer.select", json!({"layers": moving})).unwrap();
    h.run_steps(3);
    h.state_mut().session.history.clear();
    let from = timeline_layer_row(&h, owner).center();
    start_timeline_row_drag(&mut h, from);
    if !delete_owner {
        // Changing the selection does not change the group captured by the held drag.
        h.state_mut().session.execute("layer.select", json!({"layers": [removed]})).unwrap();
        h.run_steps(2);
    }
    key(&mut h, egui::Key::Delete, Default::default());
    assert!(h.ctx.input(|i| i.pointer.primary_down()), "Delete happened during the held drag");
    let survivors: Vec<u64> = before.iter().copied().filter(|id| *id != removed).collect();
    assert_eq!(timeline_layer_order(&h), survivors, "Delete removed only the selected layer");
    let steps = h.state().session.history.undo.len();
    let row = timeline_layer_row(&h, survivors[0]);
    release_timeline_row_drag(&mut h, row.center() - vec2(0.0, row.height() * 0.4));
    assert_eq!(timeline_layer_order(&h), survivors, "a stale drag cannot move any surviving layer");
    assert_eq!(h.state().session.history.undo.len(), steps, "release adds no undo step");
    assert_ne!(h.output().platform_output.cursor_icon, egui::CursorIcon::Grabbing);
    let from = timeline_layer_row(&h, survivors[0]).center();
    let row = timeline_layer_row(&h, survivors[1]);
    drag(&mut h, from, row.center() + vec2(0.0, row.height() * 0.4));
    let mut expected = survivors;
    expected.swap(0, 1);
    assert_eq!(timeline_layer_order(&h), expected, "a later independent drag still works");
    assert_eq!(h.state().session.history.undo.len(), steps + 1);
}

#[test]
fn timeline_rows_drag_cancels_when_the_owner_is_deleted() {
    timeline_reorder_after_deletion(true);
}

#[test]
fn timeline_rows_drag_cancels_when_a_group_member_is_deleted() {
    timeline_reorder_after_deletion(false);
}

#[test]
fn timeline_rows_drag_cancels_without_a_primary_release() {
    for escape in [false, true] {
        let mut h = harness();
        let before = timeline_layer_order(&h);
        let from = timeline_layer_row(&h, before[0]).center();
        let row = timeline_layer_row(&h, before[1]);
        let to = row.center() + vec2(0.0, row.height() * 0.4);
        let steps = h.state().session.history.undo.len();
        start_timeline_row_drag(&mut h, from);
        h.input_mut().events.push(Event::PointerMoved(to));
        h.step();
        if escape {
            key(&mut h, egui::Key::Escape, Default::default());
        } else {
            // egui stops its drag on another button's release too. It isn't a layer drop.
            for pressed in [true, false] {
                h.input_mut().events.push(Event::PointerButton { pos: to, button: egui::PointerButton::Secondary, pressed, modifiers: Default::default() });
                h.step();
            }
        }
        assert_eq!(timeline_layer_order(&h), before, "cancellation must not arrange layers");
        assert_ne!(h.output().platform_output.cursor_icon, egui::CursorIcon::Grabbing);
        release_timeline_row_drag(&mut h, to);
        assert_eq!(timeline_layer_order(&h), before, "a later release must not replay a cancelled drag");
        assert_eq!(h.state().session.history.undo.len(), steps);
    }
}

#[test]
fn timeline_rows_drag_cancels_when_a_modal_opens() {
    let mut h = harness();
    let before = timeline_layer_order(&h);
    let from = timeline_layer_row(&h, before[0]).center();
    let row = timeline_layer_row(&h, before[1]);
    let to = row.center() + vec2(0.0, row.height() * 0.4);
    let steps = h.state().session.history.undo.len();
    start_timeline_row_drag(&mut h, from);
    let (modifiers, shortcut) = effectcraft_ui_egui::menus::parse_shortcut("Cmd+Alt+;").unwrap();
    key(&mut h, shortcut, modifiers);
    assert_eq!(h.state().dialog, Some(effectcraft_ui_egui::Dialog::Settings));
    assert!(!h.ctx.read_response(egui::Id::unique(("row", before[0]))).unwrap().enabled(), "the modal disables the Timeline row");
    assert!(h.ctx.input(|i| i.pointer.primary_down()), "the modal opened during the held drag");
    release_timeline_row_drag(&mut h, to);
    assert_eq!(timeline_layer_order(&h), before, "a release behind the modal must not arrange layers");
    assert_eq!(h.state().session.history.undo.len(), steps);
    key(&mut h, egui::Key::Escape, Default::default());
    assert!(h.state().dialog.is_none());
    assert_eq!(timeline_layer_order(&h), before, "closing the modal must not replay the drag");
    assert_ne!(h.output().platform_output.cursor_icon, egui::CursorIcon::Grabbing);
    let from = timeline_layer_row(&h, before[0]).center();
    let row = timeline_layer_row(&h, before[1]);
    drag(&mut h, from, row.center() + vec2(0.0, row.height() * 0.4));
    assert_eq!(timeline_layer_order(&h), [before[1], before[0]]);
    assert_eq!(h.state().session.history.undo.len(), steps + 1);
}

#[test]
fn timeline_rows_drag_does_not_replay_a_release_while_hidden() {
    use effectcraft_ui_egui::dock::PanelKind;
    let mut h = harness();
    let before = timeline_layer_order(&h);
    let from = timeline_layer_row(&h, before[0]).center();
    let steps = h.state().session.history.undo.len();
    start_timeline_row_drag(&mut h, from);
    h.state_mut().close_panel(PanelKind::Timeline);
    h.run_steps(2);
    assert!(h.state().auto.find("timeline.layerMarquee").is_none());
    release_timeline_row_drag(&mut h, from);
    h.state_mut().show_panel(PanelKind::Timeline);
    h.run_steps(3);
    assert_eq!(timeline_layer_order(&h), before);
    assert_eq!(h.state().session.history.undo.len(), steps);
    assert_ne!(h.output().platform_output.cursor_icon, egui::CursorIcon::Grabbing);
    let from = timeline_layer_row(&h, before[0]).center();
    let row = timeline_layer_row(&h, before[1]);
    drag(&mut h, from, row.center() + vec2(0.0, row.height() * 0.4));
    assert_eq!(timeline_layer_order(&h), [before[1], before[0]]);
    assert_eq!(h.state().session.history.undo.len(), steps + 1);
}

#[test]
fn timeline_rows_drag_cancels_when_the_composition_changes() {
    let mut h = harness();
    let original = h.state().session.active_comp_id().unwrap();
    let before = timeline_layer_order(&h);
    h.state_mut().session.execute("comp.new", json!({"name": "Other", "width": 640, "height": 360, "duration": 4})).unwrap();
    h.state_mut().session.execute("layer.newSolid", json!({"name": "Other layer", "color": "#406080", "width": 80, "height": 80})).unwrap();
    let other = h.state().session.active_comp_id().unwrap();
    let other_before = timeline_layer_order(&h);
    h.state_mut().session.execute("comp.open", json!({"comp": original.0})).unwrap();
    h.run_steps(3);
    let from = timeline_layer_row(&h, before[0]).center();
    let steps = h.state().session.history.undo.len();
    start_timeline_row_drag(&mut h, from);
    h.state_mut().session.execute("comp.open", json!({"comp": other.0})).unwrap();
    h.run_steps(2);
    assert_ne!(h.output().platform_output.cursor_icon, egui::CursorIcon::Grabbing);
    release_timeline_row_drag(&mut h, from);
    assert_eq!(timeline_layer_order(&h), other_before);
    h.state_mut().session.execute("comp.open", json!({"comp": original.0})).unwrap();
    h.run_steps(3);
    assert_eq!(timeline_layer_order(&h), before);
    assert_eq!(h.state().session.history.undo.len(), steps);
    assert_ne!(h.output().platform_output.cursor_icon, egui::CursorIcon::Grabbing);
}

fn layer_id(h: &Harness<'_, EffectcraftApp>, name: &str) -> u64 {
    h.state().session.active_comp().unwrap().layers.iter().find(|l| l.name == name).unwrap().id.0
}

/// Uid of a layer's property by match path (`transform/position`).
fn prop_uid(h: &Harness<'_, EffectcraftApp>, layer: &str, path: &str) -> u64 {
    let id = layer_id(h, layer);
    h.state().session.active_comp().unwrap().layer(LayerId(id)).unwrap().props.prop(path).unwrap().uid
}

/// Is the property's row on screen in the Timeline?
fn shown(h: &Harness<'_, EffectcraftApp>, uid: u64) -> bool {
    h.state().auto.find(&format!("timeline.prop.{uid}.stopwatch")).is_some()
}

fn key(h: &mut Harness<'_, EffectcraftApp>, key: egui::Key, modifiers: egui::Modifiers) {
    h.input_mut().events.push(Event::Key { key, physical_key: None, pressed: true, repeat: false, modifiers });
    h.input_mut().events.push(Event::Key { key, physical_key: None, pressed: false, repeat: false, modifiers });
    h.run_steps(2);
}

#[test]
fn timeline_twirl_arrows_open_layers_and_groups() {
    let mut h = harness();
    let id = layer_id(&h, "Box");
    let transform = {
        let comp = h.state().session.active_comp().unwrap().clone();
        comp.layer(LayerId(id)).unwrap().props.sub("transform").unwrap().uid
    };
    let at = rect(&h, &format!("timeline.layer.{id}.twirl")).center();
    click(&mut h, at);
    assert!(h.state().ui.timeline.open_layers.contains(&id), "the layer twirls open");
    let at = rect(&h, &format!("timeline.group.{transform}.twirl")).center();
    click(&mut h, at);
    for p in ["anchor", "position", "scale", "rotation", "opacity"] {
        assert!(shown(&h, prop_uid(&h, "Box", &format!("transform/{p}"))), "{p} row");
    }
    // And closed again.
    let at = rect(&h, &format!("timeline.layer.{id}.twirl")).center();
    click(&mut h, at);
    h.run_steps(2);
    assert!(!h.state().ui.timeline.open_layers.contains(&id));
    assert!(!shown(&h, prop_uid(&h, "Box", "transform/position")));
}

#[test]
fn command_clicking_a_group_twirl_opens_its_whole_tree() {
    // Cmd-click (or Alt-click) the arrow of Effects, Contents, Masks…: every group under it
    // twirls open with it, and closed again with it.
    let mut h = harness();
    let id = layer_id(&h, "Box");
    h.state_mut().session.execute("effect.apply", json!({"effect": "Color Grading", "layers": [id]})).unwrap();
    fn walk(g: &effectcraft_engine::project::PropGroup, out: &mut Vec<u64>) {
        for sg in g.groups() {
            out.push(sg.uid);
            walk(sg, out);
        }
    }
    let (effects, inner, transform) = {
        let comp = h.state().session.active_comp().unwrap().clone();
        let props = &comp.layer(LayerId(id)).unwrap().props;
        let fx = props.sub("effects").unwrap();
        let mut inner = vec![];
        walk(fx, &mut inner);
        (fx.uid, inner, props.sub("transform").unwrap().uid)
    };
    assert!(inner.len() > 1, "the effect has twirl-down groups of its own");
    let at = rect(&h, &format!("timeline.layer.{id}.twirl")).center();
    click(&mut h, at);
    let at = rect(&h, &format!("timeline.group.{effects}.twirl")).center();
    let cmd_click = |h: &mut Harness<'_, EffectcraftApp>| {
        h.input_mut().events.push(Event::ModifiersChanged(egui::Modifiers::COMMAND));
        h.input_mut().events.push(Event::PointerMoved(at));
        h.step();
        for pressed in [true, false] {
            h.input_mut().events.push(Event::PointerButton { pos: at, button: egui::PointerButton::Primary, pressed, modifiers: egui::Modifiers::COMMAND });
            h.step();
        }
        h.input_mut().events.push(Event::ModifiersChanged(Default::default()));
        h.step();
    };
    cmd_click(&mut h);
    let open = h.state().ui.timeline.open_groups.clone();
    assert!(open.contains(&effects), "Effects twirls open");
    assert!(inner.iter().all(|u| open.contains(u)), "and so does everything under it");
    assert!(!open.contains(&transform), "other groups stay as they were");
    cmd_click(&mut h);
    let open = h.state().ui.timeline.open_groups.clone();
    assert!(!open.contains(&effects) && !inner.iter().any(|u| open.contains(u)), "Cmd-click again closes the tree");
}

#[test]
fn timeline_rename_commits_on_enter_and_on_click_away() {
    // The rename field (double-click a name, or Enter on a selected layer) used to grab the
    // keyboard every frame: clicking away or Enter could never leave it. (The harness can't
    // double-click — its frames are longer than a double-click — so this opens it with Enter.)
    let mut h = harness();
    h.state_mut().session.execute("layer.newNull", json!({"name": "Ctrl"})).unwrap();
    let id = layer_id(&h, "Ctrl");
    let name = |h: &Harness<'_, EffectcraftApp>| h.state().session.active_comp().unwrap().layer(LayerId(id)).unwrap().name.clone();
    let rename = |h: &mut Harness<'_, EffectcraftApp>| {
        h.state_mut().session.execute("layer.select", json!({"layers": [id]})).unwrap();
        h.run_steps(2);
        key(h, egui::Key::Enter, Default::default());
        h.run_steps(1);
        assert!(h.ctx.memory(|m| m.focused().is_some()), "the name field has focus");
    };
    // The whole name is selected: typing replaces it; Enter commits and frees the keyboard.
    rename(&mut h);
    h.input_mut().events.push(Event::Text("Rig".into()));
    h.step();
    key(&mut h, egui::Key::Enter, Default::default());
    assert_eq!(name(&h), "Rig");
    assert!(h.ctx.memory(|m| m.focused().is_none()), "the field let go of the keyboard");
    key(&mut h, egui::Key::P, Default::default());
    assert_eq!(h.state().ui.timeline.reveal, vec!["position"], "P works after renaming");
    // Clicking away commits too.
    rename(&mut h);
    h.input_mut().events.push(Event::Text("Hand".into()));
    h.step();
    let away = rect(&h, "viewer.comp").center();
    click(&mut h, away);
    assert_eq!(name(&h), "Hand");
    assert!(h.ctx.memory(|m| m.focused().is_none()));
    // Escape cancels.
    rename(&mut h);
    h.input_mut().events.push(Event::Text("Nope".into()));
    h.step();
    key(&mut h, egui::Key::Escape, Default::default());
    assert_eq!(name(&h), "Hand");
}

/// Press `k` twice within one frame (a quick double press: the harness' frames are long).
fn key_twice(h: &mut Harness<'_, EffectcraftApp>, k: egui::Key) {
    for _ in 0..2 {
        h.input_mut().events.push(Event::Key { key: k, physical_key: None, pressed: true, repeat: false, modifiers: Default::default() });
        h.input_mut().events.push(Event::Key { key: k, physical_key: None, pressed: false, repeat: false, modifiers: Default::default() });
    }
    h.run_steps(2);
}

#[test]
fn property_shortcuts_reveal_and_key_transform_properties() {
    let mut h = harness();
    // Reveal shortcuts compare wall-clock instants; drive the app clock so a step count
    // cannot decide whether a second press is "quick" or a later one.
    h.state_mut().use_manual_clock();
    h.run_steps(1);
    let wait = |h: &mut Harness<'_, EffectcraftApp>| {
        h.state_mut().clock_mut().advance(1.0);
        h.run_steps(2);
    };
    h.state_mut().session.execute("layer.select", json!({"layers": ["Box"]})).unwrap();
    h.run_steps(2);
    let t = |h: &Harness<'_, EffectcraftApp>, p: &str| shown(h, prop_uid(h, "Box", &format!("transform/{p}")));
    let only = |h: &Harness<'_, EffectcraftApp>, want: &[&str]| {
        for p in ["anchor", "position", "scale", "rotation", "opacity"] {
            assert_eq!(t(h, p), want.contains(&p), "{p} shown? want {want:?}");
        }
    };
    let none = egui::Modifiers::default();
    // P, then T: one property at a time; Shift+S adds Scale; P again hides.
    key(&mut h, egui::Key::P, none);
    only(&h, &["position"]);
    key(&mut h, egui::Key::T, none);
    only(&h, &["opacity"]);
    key(&mut h, egui::Key::S, egui::Modifiers::SHIFT);
    only(&h, &["scale", "opacity"]);
    key(&mut h, egui::Key::A, none);
    only(&h, &["anchor"]);
    key(&mut h, egui::Key::R, none);
    only(&h, &["rotation"]);
    // Alt+Shift+P: a Position keyframe at the current time; again removes it.
    let pos_keys = |h: &Harness<'_, EffectcraftApp>| {
        let id = layer_id(h, "Box");
        h.state().session.active_comp().unwrap().layer(LayerId(id)).unwrap().props.prop("transform/position").unwrap().keys.len()
    };
    let alt_shift = egui::Modifiers { alt: true, shift: true, ..Default::default() };
    key(&mut h, egui::Key::P, alt_shift);
    assert_eq!(pos_keys(&h), 1);
    key(&mut h, egui::Key::P, alt_shift);
    assert_eq!(pos_keys(&h), 0);
    // U: only keyframed properties (Opacity animated); U again later hides; UU modified ones.
    h.state_mut().session.execute("prop.addKey", json!({"layer": "Box", "path": "transform/opacity", "time": 0, "value": 50})).unwrap();
    h.state_mut().session.execute("prop.addKey", json!({"layer": "Box", "path": "transform/opacity", "time": 1, "value": 100})).unwrap();
    h.run_steps(2);
    key(&mut h, egui::Key::U, none);
    only(&h, &["opacity"]);
    wait(&mut h);
    key(&mut h, egui::Key::U, none);
    only(&h, &[]);
    h.state_mut().session.execute("prop.set", json!({"layer": "Box", "path": "transform/rotation", "value": 30})).unwrap();
    wait(&mut h);
    key_twice(&mut h, egui::Key::U);
    assert!(t(&h, "opacity") && t(&h, "rotation"), "UU: animated and changed properties");
    assert!(!t(&h, "scale"), "an untouched property stays hidden");
    // Ctrl+`: twirl the selected layer open, then closed.
    wait(&mut h);
    key(&mut h, egui::Key::Backtick, egui::Modifiers::COMMAND);
    let id = layer_id(&h, "Box");
    assert!(h.state().ui.timeline.open_layers.contains(&id) && h.state().ui.timeline.reveal.is_empty());
    key(&mut h, egui::Key::Backtick, egui::Modifiers::COMMAND);
    assert!(!h.state().ui.timeline.open_layers.contains(&id));
}

#[test]
fn double_press_shortcuts_reveal_their_second_set() {
    let mut h = harness();
    // Reveal shortcuts compare wall-clock instants; drive the app clock so a step count
    // cannot decide whether a second press is "quick" or a later one.
    h.state_mut().use_manual_clock();
    h.run_steps(1);
    let wait = |h: &mut Harness<'_, EffectcraftApp>| {
        h.state_mut().clock_mut().advance(1.0);
        h.run_steps(2);
    };
    h.state_mut().session.execute("layer.select", json!({"layers": ["Box"]})).unwrap();
    h.state_mut().session.execute("prop.setExpression", json!({"layer": "Box", "path": "transform/scale", "expression": "value"})).unwrap();
    h.run_steps(2);
    let reveal = |h: &Harness<'_, EffectcraftApp>| h.state().ui.timeline.reveal.clone();
    // EE: properties with expressions (Scale), replacing E's effects.
    key_twice(&mut h, egui::Key::E);
    assert_eq!(reveal(&h), vec!["expressions"]);
    assert!(shown(&h, prop_uid(&h, "Box", "transform/scale")));
    wait(&mut h);
    // TT: Mask Opacity; MM: every mask property; a single M: Mask Path.
    key_twice(&mut h, egui::Key::T);
    assert_eq!(reveal(&h), vec!["maskOpacity"]);
    wait(&mut h);
    key(&mut h, egui::Key::M, Default::default());
    assert_eq!(reveal(&h), vec!["maskPath"]);
    wait(&mut h);
    key_twice(&mut h, egui::Key::M);
    assert_eq!(reveal(&h), vec!["masks"]);
    wait(&mut h);
    // RR Time Remap, AA Material Options, PP paint/puppet, SS selected properties, FF missing effects.
    for (k, want) in
        [(egui::Key::R, "timeRemap"), (egui::Key::A, "material"), (egui::Key::P, "paint"), (egui::Key::S, "selected"), (egui::Key::F, "missingEffects")]
    {
        key_twice(&mut h, k);
        assert_eq!(reveal(&h), vec![want], "{k:?}{k:?}");
        wait(&mut h);
    }
}

/// Drag through `path` with `modifiers` held (press at the first point, release at the last).
fn drag_path(h: &mut Harness<'_, EffectcraftApp>, path: &[Pos2], modifiers: egui::Modifiers) {
    h.input_mut().events.push(Event::ModifiersChanged(modifiers));
    h.input_mut().events.push(Event::PointerMoved(path[0]));
    h.input_mut().events.push(Event::PointerButton { pos: path[0], button: egui::PointerButton::Primary, pressed: true, modifiers });
    h.step();
    for w in path.windows(2) {
        for i in 1..=6 {
            h.input_mut().events.push(Event::PointerMoved(w[0] + (w[1] - w[0]) * (i as f32 / 6.0)));
            h.step();
        }
    }
    let end = *path.last().unwrap();
    h.input_mut().events.push(Event::PointerButton { pos: end, button: egui::PointerButton::Primary, pressed: false, modifiers });
    h.run_steps(2);
    // The keys come up after the button, as a hand does.
    h.input_mut().events.push(Event::ModifiersChanged(Default::default()));
    h.step();
}

/// Issue #63: a bounding-box handle scales about the anchor point so the grabbed corner follows
/// the pointer, relative to the scale the drag began with: dragging through the anchor and back
/// out recovers (it used to stick at 0), edges scale one axis, Shift keeps the proportions.
#[test]
fn handle_drags_scale_about_the_anchor_and_follow_the_pointer() {
    let mut h = harness();
    let box_id = h.state().session.active_comp().unwrap().layers[0].id;
    let set = |h: &mut Harness<'_, EffectcraftApp>, path: &str, v: serde_json::Value| {
        h.state_mut().session.execute("prop.set", json!({"layer": box_id.0, "path": path, "value": v})).unwrap();
    };
    let scale = |h: &Harness<'_, EffectcraftApp>| {
        let l = h.state().session.active_comp().unwrap().layer(box_id).unwrap().clone();
        l.props.prop("transform/scale").unwrap().value.as_vec3()
    };
    let close = |a: [f64; 3], b: [f64; 2]| (a[0] - b[0]).abs() < 1.0 && (a[1] - b[1]).abs() < 1.0;
    h.state_mut().session.execute("layer.select", json!({"layers": [box_id.0]})).unwrap();
    // The 80×80 box is centred at (320, 180) with its anchor in the middle.
    h.state_mut().session.execute("view.snapping", json!({"value": false})).unwrap();
    h.run_steps(2);
    let undo0 = h.state().session.history.undo.len();
    // Bottom-right corner (360, 220) to (400, 260): twice as far from the anchor.
    let corner = rect(&h, &format!("viewer.handle.{}.2", box_id.0)).center();
    let path = [corner, screen(&h, [400.0, 260.0])];
    drag_path(&mut h, &path, Default::default());
    assert!(close(scale(&h), [200.0, 200.0]), "{:?}", scale(&h));
    assert_eq!(h.state().session.history.undo.len(), undo0 + 1, "one undo step per drag");
    // Through the anchor (scale ≈ 0) and back out to 1.5×: the layer follows, nothing sticks.
    set(&mut h, "transform/scale", json!([100, 100, 100]));
    h.run_steps(2);
    let corner = rect(&h, &format!("viewer.handle.{}.2", box_id.0)).center();
    let path = [corner, screen(&h, [320.0, 180.0]), screen(&h, [300.0, 170.0]), screen(&h, [380.0, 240.0])];
    drag_path(&mut h, &path, Default::default());
    assert!(close(scale(&h), [150.0, 150.0]), "{:?}", scale(&h));
    // Past the anchor the layer flips, as in After Effects.
    set(&mut h, "transform/scale", json!([100, 100, 100]));
    h.run_steps(2);
    let corner = rect(&h, &format!("viewer.handle.{}.2", box_id.0)).center();
    let path = [corner, screen(&h, [280.0, 140.0])];
    drag_path(&mut h, &path, Default::default());
    assert!(close(scale(&h), [-100.0, -100.0]), "{:?}", scale(&h));
    // The right edge (handle 5) scales x only.
    set(&mut h, "transform/scale", json!([100, 100, 100]));
    h.run_steps(2);
    let edge = rect(&h, &format!("viewer.handle.{}.5", box_id.0)).center();
    let path = [edge, screen(&h, [380.0, 200.0])];
    drag_path(&mut h, &path, Default::default());
    assert!(close(scale(&h), [150.0, 100.0]), "{:?}", scale(&h));
    // Shift on a corner keeps the proportions: the pointer's place along the diagonal.
    set(&mut h, "transform/scale", json!([100, 100, 100]));
    h.run_steps(2);
    let corner = rect(&h, &format!("viewer.handle.{}.2", box_id.0)).center();
    let path = [corner, screen(&h, [400.0, 230.0])];
    drag_path(&mut h, &path, egui::Modifiers::SHIFT);
    assert!(close(scale(&h), [162.5, 162.5]), "{:?}", scale(&h));
}

/// #491: with several layers selected, a handle drag scales all of them by the same ratio, each
/// about its own anchor point, in one undo step.
#[test]
fn handle_drags_scale_every_selected_layer() {
    let mut h = harness();
    let layers = h.state().session.active_comp().unwrap().layers.iter().map(|l| l.id).collect::<Vec<_>>();
    let (box_id, plate) = (layers[0], layers[1]);
    let scale = |h: &Harness<'_, EffectcraftApp>, id: LayerId| {
        let l = h.state().session.active_comp().unwrap().layer(id).unwrap().clone();
        l.props.prop("transform/scale").unwrap().value.as_vec3()
    };
    h.state_mut().session.execute("prop.set", json!({"layer": plate.0, "path": "transform/scale", "value": [50, 50, 100]})).unwrap();
    h.state_mut().session.execute("layer.select", json!({"layers": [box_id.0, plate.0]})).unwrap();
    h.state_mut().session.execute("view.snapping", json!({"value": false})).unwrap();
    h.run_steps(2);
    let undo0 = h.state().session.history.undo.len();
    // The box's bottom-right corner (360, 220) to (400, 260): twice the box's size.
    let corner = rect(&h, &format!("viewer.handle.{}.2", box_id.0)).center();
    let to = screen(&h, [400.0, 260.0]);
    drag_path(&mut h, &[corner, to], Default::default());
    let (b, p) = (scale(&h, box_id), scale(&h, plate));
    assert!((b[0] - 200.0).abs() < 1.0 && (b[1] - 200.0).abs() < 1.0, "{b:?}");
    assert!((p[0] - 100.0).abs() < 1.0 && (p[1] - 100.0).abs() < 1.0, "the other layer doubles too: {p:?}");
    assert_eq!(h.state().session.history.undo.len(), undo0 + 1, "one undo step");
}

/// #252: with snapping on, a dragged handle snaps to the comp's corners and edges (and other
/// layers'), so a layer scales exactly to the comp; Shift keeps the proportions and still lands
/// on the comp's size.
#[test]
fn handle_drags_snap_to_the_comp_edges() {
    let mut h = harness();
    let box_id = h.state().session.active_comp().unwrap().layers[0].id.0;
    let scale = |h: &Harness<'_, EffectcraftApp>, id: u64| {
        let l = h.state().session.active_comp().unwrap().layer(LayerId(id)).unwrap().clone();
        l.props.prop("transform/scale").unwrap().value.as_vec3()
    };
    let close = |a: [f64; 3], b: [f64; 2]| (a[0] - b[0]).abs() < 0.01 && (a[1] - b[1]).abs() < 0.01;
    let drag_handle = |h: &mut Harness<'_, EffectcraftApp>, id: u64, handle: usize, to: [f32; 2], mods: egui::Modifiers| {
        h.state_mut().session.execute("prop.set", json!({"layer": id, "path": "transform/scale", "value": [100, 100, 100]})).unwrap();
        h.state_mut().session.execute("layer.select", json!({"layers": [id]})).unwrap();
        h.run_steps(2);
        let from = rect(h, &format!("viewer.handle.{id}.{handle}")).center();
        let to = screen(h, to);
        drag_path(h, &[from, to], mods);
        scale(h, id)
    };
    // The 80×80 box at the comp centre: its bottom right corner (360, 220) dropped 3 px inside
    // the comp's (640, 360) lands on it, its right edge 3 px inside the comp's right edge too.
    let s = drag_handle(&mut h, box_id, 2, [637.0, 357.0], Default::default());
    assert!(close(s, [800.0, 450.0]), "{s:?}");
    let s = drag_handle(&mut h, box_id, 5, [637.0, 200.0], Default::default());
    assert!(close(s, [800.0, 100.0]), "{s:?}");
    // Snapping off: where the pointer is.
    h.state_mut().session.execute("view.snapping", json!({"value": false})).unwrap();
    let s = drag_handle(&mut h, box_id, 2, [637.0, 357.0], Default::default());
    assert!(s[0] < 795.0, "{s:?}");
    h.state_mut().session.execute("view.snapping", json!({"value": true})).unwrap();
    // A comp-shaped layer at 110 % Shift-scaled down by its corner to 3 px outside the comp's:
    // the comp's size exactly.
    let wide = h.state_mut().session.execute("layer.newSolid", json!({"name": "Wide", "color": "#20e040", "width": 640, "height": 360})).unwrap()["layer"]
        .as_u64()
        .unwrap();
    h.state_mut().session.execute("prop.set", json!({"layer": wide, "path": "transform/scale", "value": [110, 110, 100]})).unwrap();
    h.state_mut().session.execute("layer.select", json!({"layers": [wide]})).unwrap();
    h.run_steps(2);
    let path = [rect(&h, &format!("viewer.handle.{wide}.2")).center(), screen(&h, [643.0, 362.0])];
    drag_path(&mut h, &path, egui::Modifiers::SHIFT);
    let s = scale(&h, wide);
    assert!(close(s, [100.0, 100.0]), "{s:?}");
}

/// Each bounding-box handle shows the resize cursor along the direction it scales, turning with
/// the layer (#393: every handle showed the north-west / south-east cursor).
#[test]
fn handle_cursors_follow_each_handle_and_the_layer_rotation() {
    use egui::CursorIcon::{ResizeHorizontal as H, ResizeNeSw as NeSw, ResizeNwSe as NwSe, ResizeVertical as V};
    let mut h = harness();
    let box_id = h.state().session.active_comp().unwrap().layers[0].id.0;
    let set = |h: &mut Harness<'_, EffectcraftApp>, path: &str, v: serde_json::Value| {
        h.state_mut().session.execute("prop.set", json!({"layer": box_id, "path": path, "value": v})).unwrap();
        h.run_steps(2);
    };
    let cursors = |h: &mut Harness<'_, EffectcraftApp>| {
        (0..8)
            .map(|i| {
                let c = rect(h, &format!("viewer.handle.{box_id}.{i}")).center();
                h.input_mut().events.push(Event::PointerMoved(c));
                h.run_steps(2);
                h.output().platform_output.cursor_icon
            })
            .collect::<Vec<_>>()
    };
    h.state_mut().session.execute("layer.select", json!({"layers": [box_id]})).unwrap();
    // A wide box: its corners still take the diagonal cursors.
    set(&mut h, "transform/scale", json!([300, 100, 100]));
    // Corners clockwise from the top left, then the top, right, bottom and left edges.
    assert_eq!(cursors(&mut h), [NwSe, NeSw, NwSe, NeSw, V, H, V, H]);
    set(&mut h, "transform/rotation", json!(90));
    assert_eq!(cursors(&mut h), [NeSw, NwSe, NeSw, NwSe, H, V, H, V], "turned a quarter");
    set(&mut h, "transform/rotation", json!(45));
    assert_eq!(cursors(&mut h), [V, H, V, H, NeSw, NwSe, NeSw, NwSe], "turned an eighth");
}

/// The arrow keys over the Composition panel nudge the selected layer 1 pixel at the viewer's
/// magnification (half a comp pixel at 200 %), Shift+arrow 10, one undo step each (#290).
#[test]
fn arrow_keys_nudge_the_selected_layer_at_the_viewer_magnification() {
    let mut h = harness();
    let box_id = h.state().session.active_comp().unwrap().layers[0].id;
    h.state_mut().session.execute("prop.set", json!({"layer": box_id.0, "path": "transform/position", "value": [100, 100, 0]})).unwrap();
    h.state_mut().session.execute("view.snapping", json!({"value": false})).unwrap();
    h.run_steps(2);
    let at = screen(&h, [100.0, 100.0]);
    click(&mut h, at);
    assert_eq!(h.state().session.state.selected_layers, vec![box_id]);
    let pos = |h: &Harness<'_, EffectcraftApp>| {
        h.state().session.active_comp().unwrap().layer(box_id).unwrap().props.prop("transform/position").unwrap().value.as_vec3()
    };
    let ppp = h.ctx.pixels_per_point();
    h.state_mut().ui.viewer.zoom = Some(1.0 / ppp);
    h.run_steps(2);
    let undo = h.state().session.history.undo.len();
    key(&mut h, egui::Key::ArrowRight, egui::Modifiers::NONE);
    key(&mut h, egui::Key::ArrowDown, egui::Modifiers::SHIFT);
    assert_eq!(pos(&h), [101.0, 110.0, 0.0]);
    assert_eq!(h.state().session.history.undo.len(), undo + 2, "one undo step per press");
    h.state_mut().ui.viewer.zoom = Some(2.0 / ppp);
    h.run_steps(2);
    key(&mut h, egui::Key::ArrowLeft, egui::Modifiers::NONE);
    key(&mut h, egui::Key::ArrowUp, egui::Modifiers::NONE);
    assert_eq!(pos(&h), [100.5, 109.5, 0.0], "sub-pixel steps when zoomed in");
}

/// #1288: with mask points selected the arrow keys move the points, not the layer: 1 pixel at the
/// viewer's magnification in comp space (half a layer pixel on a layer scaled to 200 %), 10 with
/// Shift, one undo step each.
#[test]
fn arrow_keys_nudge_the_selected_mask_points() {
    let mut h = harness();
    let plate = layer_id(&h, "Plate");
    let corners = [[100.0, 60.0], [500.0, 60.0], [500.0, 300.0], [100.0, 300.0]];
    let s = &mut h.state_mut().session;
    s.execute("layer.select", json!({"layers": [plate]})).unwrap();
    s.execute("prop.set", json!({"layer": plate, "path": "transform/scale", "value": [200, 200, 100]})).unwrap();
    s.execute("mask.new", json!({"layer": plate, "vertices": corners, "closed": true})).unwrap();
    let mask = s.active_comp().unwrap().layer(LayerId(plate)).unwrap().masks().unwrap().groups().next().unwrap().uid;
    s.execute("mask.selectVertices", json!({"vertices": [{"layer": plate, "mask": mask, "index": 1}, {"layer": plate, "mask": mask, "index": 2}]})).unwrap();
    let ppp = h.ctx.pixels_per_point();
    h.state_mut().ui.viewer.zoom = Some(1.0 / ppp);
    h.run_steps(2);
    let pos = |h: &Harness<'_, EffectcraftApp>| {
        h.state().session.active_comp().unwrap().layer(LayerId(plate)).unwrap().props.prop("transform/position").unwrap().value.as_vec3()
    };
    let (pos0, undo) = (pos(&h), h.state().session.history.undo.len());
    key(&mut h, egui::Key::ArrowRight, egui::Modifiers::NONE);
    key(&mut h, egui::Key::ArrowDown, egui::Modifiers::SHIFT);
    let path = mask_path(&h, "Plate");
    let near = |a: [f64; 2], b: [f64; 2]| (a[0] - b[0]).abs() < 1e-6 && (a[1] - b[1]).abs() < 1e-6;
    assert!(near(path.vertices[1], [500.5, 65.0]) && near(path.vertices[2], [500.5, 305.0]), "{:?}", path.vertices);
    assert_eq!(path.vertices[0], corners[0], "unselected points stay");
    assert_eq!(pos(&h), pos0, "the layer stays");
    assert_eq!(h.state().session.history.undo.len(), undo + 2, "one undo step per press");
}

/// A comp wider than the GPU's texture limit at Full resolution shows its frame (averaged down
/// into a texture the renderer accepts) instead of failing the upload (#201: 11000×2200).
#[test]
fn full_resolution_frames_wider_than_the_texture_limit_fit() {
    let mut s = Session::default();
    s.execute("comp.new", json!({"name": "Wide", "width": 2400, "height": 400, "duration": 1})).unwrap();
    s.execute("layer.newSolid", json!({"name": "Plate", "color": "#406080"})).unwrap();
    let mut h = Harness::builder().with_size(egui::vec2(1600.0, 1000.0)).build_eframe(|_| EffectcraftApp::new(s));
    // (egui's font atlas needs 1024.)
    h.input_mut().max_texture_side = Some(1024);
    h.state_mut().session.execute("view.res.full", json!({})).unwrap();
    let full = |h: &mut Harness<'_, EffectcraftApp>| h.state_mut().viewer_pixels().is_some_and(|px| px.size == [2400, 400]);
    for _ in 0..400 {
        h.step();
        if full(&mut h) {
            break;
        }
        std::thread::sleep(std::time::Duration::from_millis(5));
    }
    assert!(full(&mut h), "the full-size frame is shown (and stays readable): {:?}", h.state().viewer_res());
    assert_eq!(h.state().viewer_texture_size(), Some([800, 134]), "2400×400 averaged by 3");
}

/// #509: ⌘/Ctrl-drag in the Layer panel resizes the Roto Brush (as in After Effects) without
/// painting, the next stroke uses the new size, and the tool options show the Diameter.
#[test]
fn roto_brush_ctrl_drag_resizes_the_brush() {
    let mut h = harness();
    let id = h.state().session.active_comp().unwrap().layers.iter().find(|l| l.name == "Box").unwrap().id.0;
    h.state_mut().session.execute("layer.select", json!({"layers": [id]})).unwrap();
    h.state_mut().ui.layer_panel = Some(id);
    h.state_mut().show_panel(effectcraft_ui_egui::dock::PanelKind::Layer);
    h.state_mut().ui.tool = Tool::RotoBrush;
    h.run_steps(3);
    assert!(h.state().auto.find("header.roto.diameter").is_some(), "the Diameter field");
    let canvas = rect(&h, "layerPanel.canvas");
    let zoom = canvas.width() / 80.0;
    let c = canvas.center();
    h.input_mut().events.push(Event::ModifiersChanged(egui::Modifiers::COMMAND));
    drag(&mut h, c, c + vec2(20.0 * zoom, 0.0));
    h.input_mut().events.push(Event::ModifiersChanged(egui::Modifiers::NONE));
    h.run_steps(2);
    let dia = h.state().session.state.roto.diameter;
    assert!((dia - 40.0).abs() <= 1.0, "a 20 px drag makes a 40 px brush: {dia}");
    let strokes = |h: &Harness<'_, EffectcraftApp>| {
        let l = h.state().session.active_comp().unwrap().layer(LayerId(id)).unwrap().clone();
        let roto = effectcraft_engine::effects::roto::ID;
        let g = l
            .effects()
            .and_then(|fx| fx.groups().find(|g| matches!(&g.kind, effectcraft_engine::project::GroupKind::Effect { effect } if effect == roto)).cloned());
        g.map(|g| effectcraft_engine::effects::roto::data(&effectcraft_engine::effects::flatten_params(&g, &mut |p| p.value.clone())).strokes.clone())
            .unwrap_or_default()
    };
    assert!(strokes(&h).is_empty(), "resizing paints nothing");
    drag(&mut h, c - vec2(10.0 * zoom, 0.0), c + vec2(10.0 * zoom, 0.0));
    let s = strokes(&h);
    assert_eq!(s.len(), 1);
    assert!((s[0].radius - dia / 2.0).abs() < 1e-6, "{} vs {dia}", s[0].radius);
}

/// #553: the Pen slot's tools work on a motion path as in After Effects: Add Vertex (and the
/// Pen) add a keyframe where the path is clicked, Convert Vertex switches a keyframe between
/// Linear and Auto Bezier, Delete Vertex removes it; the pointer is the tool's pen.
#[test]
fn vertex_tools_edit_motion_path_keyframes() {
    let mut h = harness();
    let box_id: LayerId = h.state().session.active_comp().unwrap().layers[0].id;
    let s = &mut h.state_mut().session;
    s.execute("layer.select", json!({"layers": [box_id.0]})).unwrap();
    s.execute("prop.addKey", json!({"layer": box_id.0, "path": "transform/position", "time": 0.0, "value": [100, 100, 0]})).unwrap();
    s.execute("prop.addKey", json!({"layer": box_id.0, "path": "transform/position", "time": 2.0, "value": [500, 100, 0]})).unwrap();
    s.execute("view.snapping", json!({"value": false})).unwrap();
    s.set_time(effectcraft_engine::time::Tick::from_seconds_f64(3.0));
    let keys = |h: &Harness<'_, EffectcraftApp>| {
        h.state().session.active_comp().unwrap().layer(box_id).unwrap().props.prop("transform/position").unwrap().keys.clone()
    };
    h.state_mut().ui.tool = Tool::PenAdd;
    h.run_steps(3);
    let mid = screen(&h, [300.0, 100.0]);
    h.input_mut().events.push(Event::PointerMoved(mid));
    h.step();
    assert_eq!(h.output().platform_output.cursor_icon, egui::CursorIcon::None, "the pen is painted in place of the system pointer");
    click(&mut h, mid);
    let k = keys(&h);
    assert_eq!(k.len(), 3, "Add Vertex adds a keyframe on the path");
    let v = k[1].value.as_vec3();
    assert!((v[0] - 300.0).abs() < 10.0 && (v[1] - 100.0).abs() < 1.0, "{v:?}");
    assert!((k[1].time.seconds() - 1.0).abs() < 0.1, "{:?}", k[1].time);
    // Convert Vertex: the new key's handles go (Linear), then come back (Auto Bezier).
    h.state_mut().ui.tool = Tool::PenConvert;
    let at = screen(&h, [v[0] as f32, v[1] as f32]);
    click(&mut h, at);
    let k = keys(&h);
    assert!(!k[1].spatial_auto && k[1].spatial_in == [0.0; 3] && k[1].spatial_out == [0.0; 3], "Linear");
    click(&mut h, at);
    assert!(keys(&h)[1].spatial_auto, "Auto Bezier");
    // Delete Vertex removes it.
    h.state_mut().ui.tool = Tool::PenDelete;
    click(&mut h, at);
    assert_eq!(keys(&h).len(), 2);
    // The Pen adds one too.
    h.state_mut().ui.tool = Tool::Pen;
    let quarter = screen(&h, [200.0, 100.0]);
    click(&mut h, quarter);
    assert_eq!(keys(&h).len(), 3);
}

/// #882: at 100 % (and 200 %) on a scaled display the frame's corner sits on a physical pixel, so
/// every comp pixel covers whole screen pixels instead of being blended with its neighbour.
#[test]
fn frame_sits_on_the_physical_pixel_grid() {
    for ppp in [1.0f32, 1.25, 1.5] {
        let mut s = Session::default();
        s.execute("comp.new", json!({"name": "Grid", "width": 301, "height": 201, "duration": 1})).unwrap();
        let mut h = Harness::builder().with_size(egui::vec2(1600.0, 1000.0)).with_pixels_per_point(ppp).build_eframe(move |_| EffectcraftApp::new(s));
        h.run_steps(3);
        for (mag, pan) in [(1.0f32, [0.3f32, 0.37]), (2.0, [-0.45, 0.2])] {
            h.state_mut().ui.viewer.zoom = Some(mag / ppp);
            h.state_mut().ui.viewer.pan = pan;
            h.run_steps(2);
            let c = rect(&h, "viewer.comp");
            for v in [c.min.x * ppp, c.min.y * ppp] {
                assert!((v - v.round()).abs() < 1e-3, "ppp {ppp}, {mag}×: corner at {v} physical px");
            }
            assert!((c.width() * ppp - 301.0 * mag).abs() < 1e-2, "ppp {ppp}, {mag}×: {} physical px wide", c.width() * ppp);
        }
    }
}

/// Magnification ▸ Fit up to 100% keeps fitting as the panel changes size (Maximize Panel), like
/// Fit, and never magnifies past 100%.
#[test]
fn fit_up_to_100_follows_the_maximized_panel() {
    use effectcraft_ui_egui::dock::PanelKind;
    let pick = |h: &mut Harness<'_, EffectcraftApp>| {
        let at = rect(h, "viewer.magnification").center();
        click(h, at);
        let at = rect(h, "viewer.magnification.1").center();
        click(h, at);
        assert_eq!(h.state().ui.viewer.zoom, None, "a fit mode, not a fixed magnification");
        assert!(h.state().ui.viewer.fit_up_to_100);
        h.run_steps(2);
    };
    // A comp larger than the panel: it fills the docked panel, then the maximized one.
    let mut h = Harness::builder().with_size(egui::vec2(1600.0, 1000.0)).build_eframe(|_| {
        let mut s = Session::default();
        s.execute("comp.new", json!({"name": "Big", "width": 3840, "height": 2160, "duration": 4})).unwrap();
        EffectcraftApp::new(s)
    });
    h.run_steps(3);
    pick(&mut h);
    let docked = rect(&h, "viewer.comp").width();
    h.state_mut().toggle_maximize(PanelKind::Composition);
    h.run_steps(3);
    let area = rect(&h, "viewer.area");
    let maximized = rect(&h, "viewer.comp");
    assert!(maximized.width() > docked + 100.0, "{docked} -> {}", maximized.width());
    assert!(area.contains_rect(maximized), "{maximized:?} in {area:?}");
    h.state_mut().toggle_maximize(PanelKind::Composition);
    h.run_steps(3);
    assert_eq!(rect(&h, "viewer.comp").width(), docked, "and back when the panel is restored");
    // A comp smaller than the panel stops at 100%.
    let mut h = harness();
    pick(&mut h);
    let ppp = h.ctx.pixels_per_point();
    assert!(rect(&h, "viewer.comp").width() <= 640.0 / ppp + 0.01);
    h.state_mut().toggle_maximize(PanelKind::Composition);
    h.run_steps(3);
    assert!((rect(&h, "viewer.comp").width() - 640.0 / ppp).abs() < 0.01);
}

/// The Rotation tool keeps counting past a full turn, as in After Effects: going round the anchor
/// 1.5 turns clockwise gives 540° (1x +180.0°), going back 2 turns gives −180°, with no jump a
/// revolution back where the pointer crosses the left of the anchor. One drag is one undo step.
#[test]
fn rotate_tool_counts_revolutions_both_ways() {
    let mut h = harness();
    let box_id = h.state().session.active_comp().unwrap().layers[0].id;
    h.state_mut().session.execute("layer.select", json!({"layers": [box_id.0]})).unwrap();
    h.state_mut().ui.tool = Tool::Rotate;
    h.run_steps(2);
    let rotation = |h: &Harness<'_, EffectcraftApp>| {
        h.state().session.active_comp().unwrap().layer(box_id).unwrap().props.prop("transform/rotation").unwrap().value.as_f64()
    };
    let center = screen(&h, [320.0, 180.0]);
    // Inside the 80 px box, so the drag starts on it (not on the Plate behind).
    let r = (screen(&h, [350.0, 180.0]) - center).x;
    let at = |deg: f64| center + vec2((deg.to_radians().cos() as f32) * r, (deg.to_radians().sin() as f32) * r);
    let undo_before = h.state().session.history.undo.len();
    // Press on the box, right of its anchor; then go round in 10° steps.
    let press = center + vec2(r * 0.3, 0.0);
    h.input_mut().events.push(Event::PointerMoved(press));
    h.input_mut().events.push(Event::PointerButton { pos: press, button: egui::PointerButton::Primary, pressed: true, modifiers: Default::default() });
    h.step();
    let mut seen = vec![];
    let mut walk = |h: &mut Harness<'_, EffectcraftApp>, degs: Vec<f64>| {
        for d in degs {
            h.input_mut().events.push(Event::PointerMoved(at(d)));
            h.step();
            seen.push(rotation(h));
        }
    };
    walk(&mut h, (0..=54).map(|i| i as f64 * 10.0).collect());
    // The gesture applies the previous frame's pointer: hold a frame.
    h.step();
    let cw = rotation(&h);
    assert!((cw - 540.0).abs() < 1.0, "1.5 turns clockwise: {cw}");
    walk(&mut h, (1..=72).map(|i| 540.0 - i as f64 * 10.0).collect());
    let end = at(-180.0);
    h.input_mut().events.push(Event::PointerButton { pos: end, button: egui::PointerButton::Primary, pressed: false, modifiers: Default::default() });
    h.run_steps(2);
    let ccw = rotation(&h);
    assert!((ccw + 180.0).abs() < 1.0, "back two turns: {ccw}");
    assert!(seen.windows(2).all(|w| (w[1] - w[0]).abs() <= 20.0), "no jump: {seen:?}");
    assert_eq!(h.state().session.history.undo.len(), undo_before + 1, "one drag, one undo step");
}

/// #2769: the Rotation tool counts the turn from where the button went down, as in After Effects,
/// not from where the drag registered: a first move that sweeps 60° round the anchor turns the
/// layer 60° (it used to drop that sweep, so near the anchor the tool lagged behind the pointer).
#[test]
fn rotate_tool_counts_the_turn_from_the_press() {
    let mut h = harness();
    let box_id = h.state().session.active_comp().unwrap().layers[0].id;
    h.state_mut().session.execute("layer.select", json!({"layers": [box_id.0]})).unwrap();
    h.state_mut().ui.tool = Tool::Rotate;
    h.run_steps(2);
    let center = screen(&h, [320.0, 180.0]);
    let r = (screen(&h, [350.0, 180.0]) - center).x;
    // Press on the box right of its anchor (0°), then one move to 60° clockwise, still on the box.
    let press = center + vec2(r * 0.3, 0.0);
    let to = center + vec2(r * 60f32.to_radians().cos(), r * 60f32.to_radians().sin());
    h.input_mut().events.push(Event::PointerMoved(press));
    h.input_mut().events.push(Event::PointerButton { pos: press, button: egui::PointerButton::Primary, pressed: true, modifiers: Default::default() });
    h.step();
    h.input_mut().events.push(Event::PointerMoved(to));
    h.step();
    // The gesture applies the previous frame's pointer: hold a frame.
    h.step();
    h.input_mut().events.push(Event::PointerButton { pos: to, button: egui::PointerButton::Primary, pressed: false, modifiers: Default::default() });
    h.run_steps(2);
    let rot = h.state().session.active_comp().unwrap().layer(box_id).unwrap().props.prop("transform/rotation").unwrap().value.as_f64();
    assert!((rot - 60.0).abs() < 1.0, "the whole sweep from the press turns the layer: {rot}");
}

/// #2769: a press right on the anchor point has no direction to count the turn from, so the layer
/// doesn't jump on the first move; the turn counts from where the drag registered.
#[test]
fn rotate_tool_pressed_on_the_anchor_does_not_jump() {
    let mut h = harness();
    let box_id = h.state().session.active_comp().unwrap().layers[0].id;
    h.state_mut().session.execute("layer.select", json!({"layers": [box_id.0]})).unwrap();
    h.state_mut().ui.tool = Tool::Rotate;
    h.run_steps(2);
    let rotation = |h: &Harness<'_, EffectcraftApp>| {
        h.state().session.active_comp().unwrap().layer(box_id).unwrap().props.prop("transform/rotation").unwrap().value.as_f64()
    };
    let center = screen(&h, [320.0, 180.0]);
    let r = (screen(&h, [350.0, 180.0]) - center).x;
    let at = |deg: f32| center + vec2(r * deg.to_radians().cos(), r * deg.to_radians().sin());
    h.input_mut().events.push(Event::PointerMoved(center));
    h.input_mut().events.push(Event::PointerButton { pos: center, button: egui::PointerButton::Primary, pressed: true, modifiers: Default::default() });
    h.step();
    h.input_mut().events.push(Event::PointerMoved(at(60.0)));
    h.step();
    // The gesture applies the previous frame's pointer: hold a frame.
    h.step();
    let first = rotation(&h);
    assert!(first.abs() < 1.0, "no jump on the first move: {first}");
    h.input_mut().events.push(Event::PointerMoved(at(90.0)));
    h.step();
    h.step();
    let next = rotation(&h);
    assert!((next - 30.0).abs() < 1.0, "then turns with the pointer: {next}");
    h.input_mut().events.push(Event::PointerButton { pos: at(90.0), button: egui::PointerButton::Primary, pressed: false, modifiers: Default::default() });
    h.run_steps(2);
}

/// A harness whose Box is a child of Plate (anchor 320,180) with `plate` set on Plate first, at
/// parent-space position (200, 120). Returns the two layers' ids.
fn rotate_child_harness(plate: serde_json::Value) -> (Harness<'static, EffectcraftApp>, u64, u64) {
    let mut h = harness();
    let (parent, child) = (layer_id(&h, "Plate"), layer_id(&h, "Box"));
    let s = &mut h.state_mut().session;
    for (path, value) in plate.as_object().unwrap() {
        s.execute("prop.set", json!({"layer": parent, "path": format!("transform/{path}"), "value": value})).unwrap();
    }
    s.execute("layer.setParent", json!({"layers": [child], "parent": parent, "compensate": false})).unwrap();
    s.execute("prop.set", json!({"layer": child, "path": "transform/position", "value": [200.0, 120.0]})).unwrap();
    s.execute("layer.select", json!({"layers": [child]})).unwrap();
    h.state_mut().ui.tool = Tool::Rotate;
    h.run_steps(2);
    (h, parent, child)
}

/// Goes round comp point `pivot` with the Rotation tool, 10° clockwise a frame for 400°, starting
/// inside the 80 px box to its right. The rotations of `layers` on each frame, from the pointer's
/// first move on.
fn rotate_round(h: &mut Harness<'static, EffectcraftApp>, pivot: [f32; 2], layers: &[u64]) -> Vec<Vec<f64>> {
    let center = screen(h, pivot);
    let r = (screen(h, [pivot[0] + 30.0, pivot[1]]) - center).x;
    let at = |deg: f64| center + vec2((deg.to_radians().cos() as f32) * r, (deg.to_radians().sin() as f32) * r);
    let press = center + vec2(r * 0.3, 0.0);
    h.input_mut().events.push(Event::PointerMoved(press));
    h.input_mut().events.push(Event::PointerButton { pos: press, button: egui::PointerButton::Primary, pressed: true, modifiers: Default::default() });
    h.step();
    let mut seen = vec![];
    for i in 0..=40 {
        h.input_mut().events.push(Event::PointerMoved(at(i as f64 * 10.0)));
        h.step();
        let comp = h.state().session.active_comp().unwrap();
        seen.push(layers.iter().map(|l| comp.layer(LayerId(*l)).unwrap().props.prop("transform/rotation").unwrap().value.as_f64()).collect());
    }
    let end = at(400.0);
    h.input_mut().events.push(Event::PointerButton { pos: end, button: egui::PointerButton::Primary, pressed: false, modifiers: Default::default() });
    h.run_steps(2);
    seen
}

/// Layer `i` of `rotate_round` turned by `step`° on every frame once the drag was under way.
fn assert_turns_by(seen: &[Vec<f64>], i: usize, step: f64) {
    let v: Vec<f64> = seen.iter().map(|f| f[i]).collect();
    let live: Vec<f64> = v.iter().copied().skip_while(|x| *x == v[0]).collect();
    assert!(live.len() > 30, "layer {i} hardly turned: {v:?}");
    assert!(live.windows(2).all(|w| (w[1] - w[0] - step).abs() < 0.5), "layer {i} should turn {step}° a frame: {v:?}");
}

/// #2870: a child of a moved parent turns about its anchor where it shows, not about its raw
/// Position read as comp pixels.
#[test]
fn rotate_tool_turns_a_child_of_a_moved_parent_smoothly() {
    let (mut h, _, child) = rotate_child_harness(json!({"position": [420.0, 240.0]}));
    assert_turns_by(&rotate_round(&mut h, [300.0, 180.0], &[child]), 0, 10.0);
}

/// #2870: under a mirrored parent the value runs opposite to the pointer, so the layer on screen
/// still follows it.
#[test]
fn rotate_tool_follows_the_pointer_under_a_flipped_parent() {
    let (mut h, _, child) = rotate_child_harness(json!({"scale": [-100.0, 100.0]}));
    assert_turns_by(&rotate_round(&mut h, [440.0, 120.0], &[child]), 0, -10.0);
}

/// #2870: a parent's rotation offsets the angle but not the turn.
#[test]
fn rotate_tool_turns_a_child_of_a_rotated_parent_smoothly() {
    let (mut h, _, child) = rotate_child_harness(json!({"rotation": 90.0}));
    assert_turns_by(&rotate_round(&mut h, [380.0, 60.0], &[child]), 0, 10.0);
}

/// #2870: a parent's pixel aspect isn't inherited by its child, so it doesn't move the pivot either.
#[test]
fn rotate_tool_ignores_a_parents_pixel_aspect() {
    let mut h = harness();
    let child = layer_id(&h, "Box");
    let s = &mut h.state_mut().session;
    let parent = s.execute("layer.newSolid", json!({"name": "Wide", "color": "#000000", "width": 100, "height": 100, "pixelAspect": 2.0})).unwrap()["layer"]
        .as_u64()
        .unwrap();
    s.execute("layer.setSwitch", json!({"layers": [parent], "switch": "video", "value": false})).unwrap();
    s.execute("prop.set", json!({"layer": parent, "path": "transform/position", "value": [500.0, 300.0]})).unwrap();
    s.execute("layer.setParent", json!({"layers": [child], "parent": parent, "compensate": false})).unwrap();
    s.execute("prop.set", json!({"layer": child, "path": "transform/position", "value": [100.0, 60.0]})).unwrap();
    s.execute("layer.select", json!({"layers": [child]})).unwrap();
    h.state_mut().ui.tool = Tool::Rotate;
    h.run_steps(2);
    assert_turns_by(&rotate_round(&mut h, [550.0, 310.0], &[child]), 0, 10.0);
}

/// #2870: every selected layer turns the same way on screen, whichever way its own parent is
/// flipped: the value of a layer whose parent is mirrored relative to the grabbed one's runs opposite.
#[test]
fn rotate_tool_turns_a_mixed_selection_the_same_way_on_screen() {
    let (mut h, plate, child) = rotate_child_harness(json!({"scale": [-100.0, 100.0]}));
    let s = &mut h.state_mut().session;
    let new = |s: &mut Session, name: &str| {
        s.execute("layer.newSolid", json!({"name": name, "color": "#00ff00", "width": 20, "height": 20})).unwrap()["layer"].as_u64().unwrap()
    };
    let (free, sibling) = (new(s, "Free"), new(s, "Sibling"));
    s.execute("layer.setParent", json!({"layers": [sibling], "parent": plate, "compensate": false})).unwrap();
    s.execute("prop.set", json!({"layer": free, "path": "transform/rotation", "value": 15.0})).unwrap();
    s.execute("layer.select", json!({"layers": [child, free, sibling]})).unwrap();
    h.run_steps(2);
    let seen = rotate_round(&mut h, [440.0, 120.0], &[child, free, sibling]);
    assert_turns_by(&seen, 0, -10.0);
    assert_turns_by(&seen, 1, 10.0);
    assert_turns_by(&seen, 2, -10.0);
}

/// #2870: a 3D child turns about its anchor as the camera shows it, and with its value running
/// opposite under a flipped parent.
#[test]
fn rotate_tool_turns_a_3d_child_about_its_anchor_through_the_camera() {
    let (mut h, _, child) = rotate_child_harness(json!({"scale": [-100.0, 100.0]}));
    let s = &mut h.state_mut().session;
    s.execute("layer.setSwitch", json!({"layers": [child], "switch": "threeD", "value": true})).unwrap();
    let cam = s.execute("layer.newCamera", json!({"name": "Cam", "type": "oneNode"})).unwrap()["layer"].as_u64().unwrap();
    let at = s.active_comp().unwrap().layer(LayerId(cam)).unwrap().props.prop("transform/position").unwrap().value.as_vec3();
    // One comp-plane unit to the right moves everything in the comp plane one pixel to the left.
    s.execute("prop.set", json!({"layer": cam, "path": "transform/position", "value": [at[0] + 100.0, at[1], at[2]]})).unwrap();
    s.execute("layer.select", json!({"layers": [child]})).unwrap();
    h.run_steps(2);
    assert_turns_by(&rotate_round(&mut h, [340.0, 120.0], &[child]), 0, -10.0);
}

/// Mask vertices multi-select with the Selection tool: Shift-click adds a vertex to the
/// selection and Shift-clicking it again removes it; a plain click collapses to one (#1004).
#[test]
fn mask_shift_click_toggles_vertices() {
    use egui::Modifiers;
    let mut h = harness();
    let plate = layer_id(&h, "Plate");
    h.state_mut().session.execute("layer.select", json!({"layers": [plate]})).unwrap();
    h.state_mut()
        .session
        .execute("mask.new", json!({"layer": plate, "vertices": [[100.0, 60.0], [500.0, 60.0], [500.0, 300.0], [100.0, 300.0]], "closed": true}))
        .unwrap();
    h.state_mut().ui.tool = Tool::Selection;
    h.run_steps(2);
    let (p0, p1) = (screen(&h, [100.0, 60.0]), screen(&h, [500.0, 60.0]));
    let shift_click = |h: &mut Harness<'_, EffectcraftApp>, p: Pos2| {
        h.input_mut().events.push(Event::ModifiersChanged(Modifiers::SHIFT));
        h.step();
        h.input_mut().events.push(Event::PointerMoved(p));
        h.input_mut().events.push(Event::PointerButton { pos: p, button: egui::PointerButton::Primary, pressed: true, modifiers: Modifiers::SHIFT });
        h.step();
        h.input_mut().events.push(Event::PointerButton { pos: p, button: egui::PointerButton::Primary, pressed: false, modifiers: Modifiers::SHIFT });
        h.step();
        h.input_mut().events.push(Event::ModifiersChanged(Modifiers::NONE));
        h.run_steps(2);
    };
    click(&mut h, p0);
    assert_eq!(h.state().session.state.selected_vertices.len(), 1, "plain click selects one vertex");
    shift_click(&mut h, p1);
    assert_eq!(h.state().session.state.selected_vertices.len(), 2, "shift-click adds a second vertex");
    shift_click(&mut h, p1);
    assert_eq!(h.state().session.state.selected_vertices.len(), 1, "shift-click again removes it");
}

/// Dragging a marquee from empty viewer area selects the mask vertices inside it, additive
/// with Shift, without moving the layer (#1004).
#[test]
fn mask_marquee_selects_vertices() {
    let mut h = harness();
    let plate = layer_id(&h, "Plate");
    let bx = layer_id(&h, "Box");
    // A full-frame layer behind leaves no empty area to start a marquee from: remove it.
    h.state_mut().session.execute("layer.select", json!({"layers": [plate]})).unwrap();
    h.state_mut().session.execute("edit.clear", json!({"layers": [plate]})).unwrap();
    assert_eq!(h.state().session.active_comp().unwrap().layers.len(), 1);
    h.state_mut().session.execute("layer.select", json!({"layers": [bx]})).unwrap();
    h.state_mut()
        .session
        .execute("mask.new", json!({"layer": bx, "vertices": [[10.0, 10.0], [70.0, 10.0], [70.0, 70.0], [10.0, 70.0]], "closed": true}))
        .unwrap();
    h.state_mut().ui.tool = Tool::Selection;
    h.state_mut().session.execute("mask.selectVertices", json!({"vertices": []})).unwrap();
    h.run_steps(2);
    // Mask vertices are layer-local; map vertex 0 to comp space (no rotation, uniform scale).
    let (pp, aa) = {
        let l = h.state().session.active_comp().unwrap().layer(LayerId(bx)).unwrap().clone();
        (l.props.prop("transform/position").unwrap().value.as_vec3(), l.props.prop("transform/anchor").unwrap().value.as_vec3())
    };
    let v0 = [10.0 - aa[0] + pp[0], 10.0 - aa[1] + pp[1]];
    let pos = |h: &Harness<'_, EffectcraftApp>| {
        h.state().session.active_comp().unwrap().layer(LayerId(bx)).unwrap().props.prop("transform/position").unwrap().value.as_vec3()
    };
    let before = pos(&h);
    // Drag a box around the top-left vertex only, starting/ending on empty area.
    let (from, to) = (screen(&h, [(v0[0] - 90.0) as f32, (v0[1] - 50.0) as f32]), screen(&h, [(v0[0] + 10.0) as f32, (v0[1] + 10.0) as f32]));
    drag(&mut h, from, to);
    let sel = &h.state().session.state.selected_vertices;
    assert_eq!(sel.len(), 1, "marquee selects the enclosed vertex: {sel:?}");
    assert_eq!(sel[0].index, 0);
    assert_eq!(pos(&h), before, "the layer does not move under a vertex marquee");
    // Shift-marquee adds the vertices it encloses.
    let v1 = [70.0 - aa[0] + pp[0], 10.0 - aa[1] + pp[1]];
    let (from, to) = (screen(&h, [(v1[0] - 10.0) as f32, (v1[1] - 50.0) as f32]), screen(&h, [(v1[0] + 60.0) as f32, (v1[1] + 50.0) as f32]));
    h.input_mut().events.push(Event::ModifiersChanged(egui::Modifiers::SHIFT));
    h.step();
    h.input_mut().events.push(Event::PointerMoved(from));
    h.input_mut().events.push(Event::PointerButton { pos: from, button: egui::PointerButton::Primary, pressed: true, modifiers: egui::Modifiers::SHIFT });
    h.step();
    for i in 1..=8 {
        h.input_mut().events.push(Event::PointerMoved(from + (to - from) * (i as f32 / 8.0)));
        h.step();
    }
    h.input_mut().events.push(Event::PointerButton { pos: to, button: egui::PointerButton::Primary, pressed: false, modifiers: egui::Modifiers::SHIFT });
    h.step();
    h.input_mut().events.push(Event::ModifiersChanged(egui::Modifiers::NONE));
    h.run_steps(2);
    let sel = &h.state().session.state.selected_vertices;
    assert_eq!(sel.len(), 2, "shift-marquee adds: {sel:?}");
}

/// A null's eye in the Timeline hides it in the Composition viewer, as in After Effects: its box
/// and handles go even while it is selected and a click on its box doesn't pick it; the eye back
/// on brings them back.
#[test]
fn null_eye_hides_it_in_the_viewer() {
    let mut s = Session::default();
    s.execute("comp.new", json!({"name": "Nulls", "width": 640, "height": 360, "duration": 4})).unwrap();
    let n = s.execute("layer.newNull", json!({})).unwrap()["layer"].as_u64().unwrap();
    s.execute("layer.select", json!({"layers": [n]})).unwrap();
    let mut h = Harness::builder().with_size(egui::vec2(1600.0, 1000.0)).build_eframe(move |_| EffectcraftApp::new(s));
    h.run_steps(3);
    let handle = format!("viewer.handle.{n}.0");
    let eye = format!("timeline.layer.{n}.video");
    assert!(h.state().auto.find(&handle).is_some(), "a shown null has handles");
    // The eye is there for a null and turns its video off.
    let at = rect(&h, &eye).center();
    click(&mut h, at);
    assert!(!h.state().session.active_comp().unwrap().layer(LayerId(n)).unwrap().switches.video);
    h.run_steps(2);
    assert!(h.state().auto.find(&handle).is_none(), "a hidden null has no handles");
    // A click on its box doesn't pick it.
    h.state_mut().session.execute("edit.deselectAll", json!({})).unwrap();
    h.run_steps(40); // A new click, not a double-click.
    let centre = screen(&h, [320.0, 180.0]);
    click(&mut h, centre);
    assert!(h.state().session.state.selected_layers.is_empty());
    // The eye back on: selectable again, with its handles.
    h.run_steps(40);
    let at = rect(&h, &eye).center();
    click(&mut h, at);
    assert!(h.state().session.active_comp().unwrap().layer(LayerId(n)).unwrap().switches.video);
    h.run_steps(40);
    click(&mut h, centre);
    assert_eq!(h.state().session.state.selected_layers, vec![LayerId(n)]);
    h.run_steps(2);
    assert!(h.state().auto.find(&handle).is_some(), "handles back with the eye on");
}

/// #1642: a null stays visible after deselection, without selection handles.
#[test]
fn unselected_null_keeps_its_outline() {
    let mut s = Session::default();
    s.execute("comp.new", json!({"name": "Nulls", "width": 640, "height": 360, "duration": 4})).unwrap();
    let n = s.execute("layer.newNull", json!({})).unwrap()["layer"].as_u64().unwrap();
    s.execute("layer.select", json!({"layers": [n]})).unwrap();
    let mut h = Harness::builder().with_size(egui::vec2(1600.0, 1000.0)).build_eframe(move |_| EffectcraftApp::new(s));
    h.run_steps(3);
    // Use the selected box's actual geometry, so transformed nulls use the same outline.
    let corners: Vec<Pos2> = (0..4).map(|i| rect(&h, &format!("viewer.handle.{n}.{i}")).center()).collect();
    fn count(shape: &egui::Shape, corners: &[Pos2]) -> usize {
        match shape {
            egui::Shape::Path(p) => usize::from(p.closed && p.points == corners),
            egui::Shape::Vec(v) => v.iter().map(|s| count(s, corners)).sum(),
            _ => 0,
        }
    }
    let outlines = |h: &Harness<'_, EffectcraftApp>| h.output().shapes.iter().map(|s| count(&s.shape, &corners)).sum::<usize>();
    assert_eq!(outlines(&h), 1, "one selected outline");
    h.state_mut().session.execute("edit.deselectAll", json!({})).unwrap();
    h.run_steps(3);
    assert_eq!(outlines(&h), 1, "deselection keeps the null visible");
    assert!(h.state().auto.find(&format!("viewer.handle.{n}.0")).is_none());
    assert!(h.state().auto.find(&format!("viewer.anchor.{n}")).is_none());
    h.state_mut().ui.viewer.show_layer_controls = false;
    h.run_steps(2);
    assert_eq!(outlines(&h), 0, "Show Layer Controls hides the outline");
    h.state_mut().ui.viewer.show_layer_controls = true;
    h.state_mut().session.execute("layer.setSwitch", json!({"layers": [n], "switch": "video", "value": false})).unwrap();
    h.run_steps(2);
    assert_eq!(outlines(&h), 0, "the Video switch hides an unselected null too");
    h.state_mut().session.execute("layer.setSwitch", json!({"layers": [n], "switch": "video", "value": true})).unwrap();
    h.run_steps(2);
    assert_eq!(outlines(&h), 1, "the Video switch restores the outline");
    // Solo scope matches picking: a soloed AV layer hides unsoloed nulls, even outside its range.
    let solid = h.state_mut().session.execute("layer.newSolid", json!({"name": "Solo", "color": "#406080", "width": 40, "height": 40})).unwrap()["layer"]
        .as_u64()
        .unwrap();
    h.state_mut().session.execute("layer.setSwitch", json!({"layers": [solid], "switch": "solo", "value": true})).unwrap();
    h.state_mut().session.execute("edit.deselectAll", json!({})).unwrap();
    h.run_steps(3);
    assert_eq!(outlines(&h), 0, "an unsoloed null follows solo scope");
    let cid = h.state().session.active_comp_id().unwrap();
    std::sync::Arc::make_mut(&mut h.state_mut().session.project).comp_mut(cid).unwrap().layer_mut(LayerId(solid)).unwrap().out_point =
        effectcraft_engine::time::Tick::ZERO;
    h.run_steps(2);
    assert_eq!(outlines(&h), 0, "an inactive soloed layer still sets solo scope");
    h.state_mut().session.execute("layer.setSwitch", json!({"layers": [n], "switch": "solo", "value": true})).unwrap();
    h.run_steps(2);
    assert_eq!(outlines(&h), 1, "a soloed null keeps its outline");
}