GitCorner

Code

ossartcraftfilmcraft🌐
f67d38b35cc5
//! Headless UI tests of the Edit / Clip / File menu dialogs (M3.10): Paste Attributes, Make
//! Subclip, Frame Hold Options, Modify ▸ Audio Channels, the Close Project prompt, and nested
//! menus (Clip ▸ Video Options ▸ Time Interpolation).

use std::sync::mpsc::{Sender, channel};

use egui_kittest::Harness;
use filmcraft_engine::Session;
use filmcraft_engine::project::{ItemKind, Label};
use filmcraft_ui_egui::FilmcraftApp;
use filmcraft_ui_egui::control::ControlRequest;
use serde_json::{Value, json};

struct Driver {
    harness: Harness<'static, FilmcraftApp>,
    tx: Sender<ControlRequest>,
}

impl Driver {
    fn new() -> Self {
        let mut s = Session::default();
        s.execute("file.openDemoProject", json!({})).unwrap();
        let (tx, rx) = channel();
        let app = FilmcraftApp::new(s).with_control(rx);
        let harness = Harness::builder().with_size(egui::vec2(1600.0, 980.0)).with_max_steps(10_000).build_eframe(move |_cc| app);
        let mut d = Driver { harness, tx };
        d.frames(4);
        d
    }

    fn frames(&mut self, n: usize) {
        for _ in 0..n {
            // the control channel's clicks and keys enter through the input hook
            let ctx = self.harness.ctx.clone();
            let mut raw = std::mem::take(self.harness.input_mut());
            eframe::App::raw_input_hook(self.harness.state_mut(), &ctx, &mut raw);
            *self.harness.input_mut() = raw;
            self.harness.step();
        }
    }

    fn call(&mut self, method: &str, params: Value) -> Value {
        let (req, reply) = ControlRequest::new(method, params.clone());
        self.tx.send(req).unwrap();
        for _ in 0..600 {
            self.frames(1);
            if let Ok(v) = reply.try_recv() {
                return v;
            }
        }
        panic!("no reply to {method} {params}");
    }

    fn ok(&mut self, method: &str, params: Value) -> Value {
        let v = self.call(method, params.clone());
        assert_eq!(v["ok"], json!(true), "{method} {params} failed: {v}");
        v["result"].clone()
    }

    fn exec(&mut self, command: &str, params: Value) -> Value {
        self.ok("engine.execute", json!({"command": command, "params": params}))
    }

    fn menu(&mut self, id: &str) -> Value {
        let r = self.ok("ui.menu.invoke", json!({"id": id}));
        self.frames(3);
        r
    }

    fn click(&mut self, id: &str) {
        self.ok("ui.click", json!({"id": id}));
        self.frames(3);
    }

    fn has(&mut self, id: &str) -> bool {
        let v = self.ok("ui.elements", json!({"prefix": id}));
        v.as_array().unwrap().iter().any(|e| e["id"] == json!(id))
    }

    fn app(&mut self) -> &mut FilmcraftApp {
        self.harness.state_mut()
    }

    fn v1(&mut self, i: usize) -> filmcraft_engine::project::TrackItem {
        self.app().session.active_sequence().unwrap().video_tracks[0].items[i].clone()
    }

    fn item_named(&mut self, name: &str) -> u64 {
        self.app().session.project.items.values().find(|i| i.name == name).unwrap().id.0
    }

    /// The window is asked to close (Quit, Cmd+Q, its close button) for one frame.
    fn request_close(&mut self) {
        self.harness.input_mut().viewports.entry(egui::ViewportId::ROOT).or_default().events.push(egui::ViewportEvent::Close);
        self.frames(1);
    }

    /// The app sent `cmd` to the window in the last frame.
    fn sent(&self, cmd: &egui::ViewportCommand) -> bool {
        self.harness.output().viewport_output.get(&egui::ViewportId::ROOT).is_some_and(|v| v.commands.contains(cmd))
    }

    /// Make an unsaved change.
    fn edit(&mut self) {
        let c = self.v1(0);
        self.exec("timeline.select", json!({"clips": [c.id.0]}));
        self.menu("edit.label.yellow");
        assert!(self.app().session.is_dirty());
    }
}

#[test]
fn paste_attributes_dialog() {
    let mut d = Driver::new();
    let (a, b) = (d.v1(0), d.v1(2));
    d.exec("effects.setParam", json!({"clip": a.id.0, "effect": "motion", "param": "rotation", "value": 45.0}));
    d.exec("effects.setParam", json!({"clip": a.id.0, "effect": "opacity", "param": "opacity", "value": 25.0}));
    d.exec("effects.apply", json!({"clips": [a.id.0], "effect": "gaussian_blur"}));
    d.exec("timeline.select", json!({"clips": [a.id.0]}));
    d.exec("edit.copy", json!({}));
    d.exec("timeline.select", json!({"clips": [b.id.0]}));
    assert_eq!(d.menu("edit.pasteAttributes")["dialog"], "pasteAttributes");
    for id in
        ["pasteAttributes.motion", "pasteAttributes.opacity", "pasteAttributes.volume", "pasteAttributes.effect.gaussian_blur", "pasteAttributes.scaleTimes"]
    {
        assert!(d.has(id), "{id}");
    }
    // untick Opacity and the blur, then OK
    d.click("pasteAttributes.opacity");
    d.click("pasteAttributes.effect.gaussian_blur");
    d.click("pasteAttributes.ok");
    assert!(d.app().ui.clip_dialog.is_none(), "closed on OK");
    let got = d.v1(2);
    assert_eq!(got.effect("motion").unwrap().param("rotation").unwrap().value.as_f64(), Some(45.0));
    assert_eq!(got.effect("opacity").unwrap().param("opacity").unwrap().value.as_f64(), Some(100.0));
    assert!(got.effect("gaussian_blur").is_none());
    // Cancel leaves the clip alone; with params the command runs directly (no dialog)
    d.menu("edit.removeAttributes");
    d.click("removeAttributes.cancel");
    assert!(d.app().ui.clip_dialog.is_none());
    assert_eq!(d.v1(2).effect("motion").unwrap().param("rotation").unwrap().value.as_f64(), Some(45.0));
    d.exec("edit.removeAttributes", json!({"opacity": false}));
    assert_eq!(d.v1(2).effect("motion").unwrap().param("rotation").unwrap().value.as_f64(), Some(0.0));
}

/// #3789: the dialog says that ticked attributes are removed; an effect on the selected clip is
/// offered ticked, unticking it keeps it, and OK with the defaults removes it.
#[test]
fn remove_attributes_dialog_removes_the_clips_effect() {
    let mut d = Driver::new();
    let a = d.v1(0);
    d.exec("effects.apply", json!({"clips": [a.id.0], "effect": "focus_blur"}));
    d.exec("timeline.select", json!({"clips": [a.id.0]}));
    // unticked: the effect stays
    d.menu("edit.removeAttributes");
    d.click("removeAttributes.effect.focus_blur");
    d.click("removeAttributes.ok");
    assert!(d.v1(0).effect("focus_blur").is_some(), "an unticked effect is kept");
    // the defaults: removed
    assert_eq!(d.menu("edit.removeAttributes")["dialog"], "removeAttributes");
    let hint = d.ok("ui.elements", json!({"prefix": "removeAttributes.hint"}));
    assert_eq!(hint[0]["label"], "Ticked attributes are removed from the selected clips.");
    assert!(d.has("removeAttributes.effect.focus_blur"));
    let params = d.app().ui.clip_dialog.as_ref().unwrap().params.clone();
    assert_eq!(params["effects"], json!(["focus_blur"]), "the effect is offered ticked: {params}");
    d.click("removeAttributes.ok");
    assert!(d.app().ui.clip_dialog.is_none(), "closed on OK");
    assert!(d.v1(0).effect("focus_blur").is_none(), "the effect is removed");
}

/// The effects Essential Sound adds are not offered: Remove Attributes keeps them, so a ticked box
/// for one would promise a removal that does not happen. The same effect added by hand is offered.
#[test]
fn remove_attributes_dialog_leaves_out_essential_sound_effects() {
    let mut d = Driver::new();
    let a1 = |d: &mut Driver| d.app().session.active_sequence().unwrap().audio_tracks[0].items[0].clone();
    let clip = a1(&mut d).id;
    d.exec("essentialSound.setType", json!({"clips": [clip.0], "type": "dialogue"}));
    d.exec("essentialSound.set", json!({"clips": [clip.0], "values": {"repair.noise.on": true, "clarity.eq.on": true}}));
    let essential: Vec<String> = a1(&mut d).effects.iter().filter(|e| e.essential).map(|e| e.effect.clone()).collect();
    assert!(!essential.is_empty(), "Dialogue adds its effects");
    d.exec("timeline.select", json!({"clips": [clip.0]}));
    d.menu("edit.removeAttributes");
    let offered = d.app().ui.clip_dialog.as_ref().unwrap().params["effects"].clone();
    assert_eq!(offered, json!([]), "Essential Sound's effects are not offered: {offered}");
    for id in &essential {
        assert!(!d.has(&format!("removeAttributes.effect.{id}")), "{id}");
    }
    d.click("removeAttributes.ok");
    assert_eq!(a1(&mut d).effects.iter().filter(|e| e.essential).count(), essential.len(), "OK keeps them");
    // the same effect added from the Effects panel is offered and removed
    let manual = essential[0].clone();
    d.exec("effects.apply", json!({"clips": [clip.0], "effect": manual}));
    d.exec("timeline.select", json!({"clips": [clip.0]}));
    d.menu("edit.removeAttributes");
    assert_eq!(d.app().ui.clip_dialog.as_ref().unwrap().params["effects"], json!([manual]));
    d.click("removeAttributes.ok");
    let left = a1(&mut d);
    assert_eq!(left.effects.iter().filter(|e| e.effect == manual).count(), 1, "the Essential Sound one stays");
    assert!(left.effects.iter().filter(|e| e.effect == manual).all(|e| e.essential));
}

#[test]
fn make_subclip_frame_hold_and_audio_channel_dialogs() {
    let mut d = Driver::new();
    let ocean = d.item_named("Ocean_Sunset.mp4");
    d.exec("source.open", json!({"item": ocean}));
    d.exec("project.setMarks", json!({"item": ocean, "in": filmcraft_time::FrameRate::FPS_23_976.tick_of(24).0}));
    d.menu("clip.makeSubclip");
    assert_eq!(d.app().ui.clip_dialog.as_ref().unwrap().params["startFrame"], 24);
    for id in ["makeSubclip.name", "makeSubclip.startFrame", "makeSubclip.endFrame", "makeSubclip.restrictTrims", "makeSubclip.ok"] {
        assert!(d.has(id), "{id}");
    }
    d.ok("ui.set", json!({"clipDialog": {"name": "Waves", "endFrame": 48}}));
    d.click("makeSubclip.ok");
    let sub = d.app().session.project.items.values().find(|i| i.name == "Waves").cloned().expect("subclip made");
    let ItemKind::Subclip { range, .. } = sub.kind else { panic!() };
    assert_eq!(range.duration, filmcraft_time::FrameRate::FPS_23_976.tick_of(24));

    // Frame Hold Options on the selected clip: Hold On ▸ Playhead
    let c = d.v1(1);
    d.exec("timeline.select", json!({"clips": [c.id.0]}));
    d.exec("playhead.set", json!({"time": (c.start + filmcraft_time::FrameRate::FPS_23_976.tick_of(5)).0}));
    d.menu("clip.frameHoldOptions");
    assert_eq!(d.app().ui.clip_dialog.as_ref().unwrap().params["enabled"], false, "Hold On starts unticked for a clip without a hold");
    d.click("frameHold.enabled");
    d.click("frameHold.holdOn.playhead");
    d.click("frameHold.holdFilters");
    d.click("frameHold.ok");
    let got = d.v1(1);
    assert_eq!(got.frame_hold, Some(c.source_in + filmcraft_time::FrameRate::FPS_23_976.tick_of(5)));
    assert!(got.hold_filters);

    // Modify ▸ Audio Channels (Shift+G) on a project item: Mono makes one clip per channel
    d.exec("project.select", json!({"items": [ocean]}));
    d.menu("clip.audioChannels");
    assert!(d.has("audioChannels.clip.0.ch.1"));
    d.click("audioChannels.format.mono");
    assert_eq!(d.app().ui.clip_dialog.as_ref().unwrap().params["clips"], json!([[0], [1]]));
    d.click("audioChannels.ok");
    let m = d.app().session.project.item(filmcraft_engine::project::ItemId(ocean)).unwrap().as_media().unwrap().interpret.audio_channels.clone().unwrap();
    assert_eq!(m.clips, vec![vec![0], vec![1]]);
}

#[test]
fn close_project_prompt_and_nested_menus() {
    let mut d = Driver::new();
    // nested menu paths reach the menu list
    let menus = d.ok("ui.menu.list", json!({}));
    let item = menus.as_array().unwrap().iter().find(|m| m["id"] == "clip.timeInterpolation.frameBlending").unwrap().clone();
    assert_eq!(item["path"], json!(["Clip", "Video Options", "Time Interpolation"]));
    let labels: Vec<&str> = menus.as_array().unwrap().iter().filter(|m| m["path"] == json!(["Edit", "Label"])).filter_map(|m| m["label"].as_str()).collect();
    assert_eq!(labels.len(), 17);
    // label from the menu
    let c = d.v1(0);
    d.exec("timeline.select", json!({"clips": [c.id.0]}));
    d.menu("edit.label.yellow");
    assert_eq!(d.v1(0).label, Label::Yellow);
    // the project is dirty: Close Project asks
    assert!(d.app().session.is_dirty());
    assert_eq!(d.menu("file.closeProject")["dialog"], "closeProject");
    assert!(d.has("closeProject.save") && d.has("closeProject.dontSave"));
    d.click("closeProject.cancel");
    assert!(!d.app().session.project.items.is_empty());
    d.menu("file.closeProject");
    d.click("closeProject.dontSave");
    assert!(d.app().session.project.items.is_empty(), "closed without saving");
    assert!(d.app().ui.clip_dialog.is_none());
}

#[test]
fn subclip_in_the_source_monitor_and_project_menu() {
    let mut d = Driver::new();
    let r24 = filmcraft_time::FrameRate::FPS_23_976;
    let ocean = d.item_named("Ocean_Sunset.mp4");
    d.exec("project.setMarks", json!({"item": ocean, "in": r24.tick_of(24).0, "out": r24.tick_of(71).0}));
    d.exec("project.select", json!({"items": [ocean]}));
    let sub = d.exec("clip.makeSubclip", json!({"name": "Waves Sub"}))["item"].as_u64().unwrap();
    d.exec("source.open", json!({"item": sub}));
    d.frames(4);
    assert_eq!(d.app().session.state.source_playhead, r24.tick_of(24), "opens at the subclip's In");
    // the scrub bar spans the subclip: its left end is the In, its right end the last frame
    let bar = d.ok("ui.elements", json!({"prefix": "source.scrubBar"}))[0]["rect"].clone();
    let (x, y, w, h) = (bar[0].as_f64().unwrap(), bar[1].as_f64().unwrap(), bar[2].as_f64().unwrap(), bar[3].as_f64().unwrap());
    d.ok("ui.click", json!({"x": x + w - 1.0, "y": y + h / 2.0}));
    d.frames(3);
    assert_eq!(d.app().session.state.source_playhead, r24.tick_of(71));
    d.ok("ui.click", json!({"x": x + 1.0, "y": y + h / 2.0}));
    d.frames(3);
    assert_eq!(d.app().session.state.source_playhead, r24.tick_of(24));
    // Project panel ▸ right-click ▸ Edit Subclip… opens the dialog; Convert to Master Clip converts
    d.ok("ui.panel.show", json!({"panel": "Project"}));
    d.exec("project.view.set", json!({"view": "list"}));
    d.exec("project.select", json!({"items": [sub]}));
    d.frames(4);
    // the subclip is made in its master clip's bin: expand it
    let bin = d.app().session.project.root.parent_of(filmcraft_engine::project::ItemId(sub));
    if let Some(b) = bin.filter(|b| *b != d.app().session.project.root.id) {
        d.click(&format!("project.bin.{}.toggle", b.0));
        d.frames(3);
    }
    let row = format!("project.item.{sub}");
    if d.has(&row) {
        d.ok("ui.click", json!({"id": row, "button": "right"}));
        d.frames(3);
        assert!(d.has("project.itemMenu.editSubclip") && d.has("project.itemMenu.convertToMaster"));
        d.click("project.itemMenu.editSubclip");
        assert_eq!(d.app().ui.clip_dialog.as_ref().map(|c| c.command.clone()).as_deref(), Some("clip.editSubclip"));
        d.click("editSubclip.cancel");
        d.ok("ui.click", json!({"id": row, "button": "right"}));
        d.frames(3);
        d.click("project.itemMenu.convertToMaster");
        assert!(d.app().session.project.item(filmcraft_engine::project::ItemId(sub)).unwrap().as_media().is_some(), "converted");
    } else {
        panic!("subclip row not shown: {:?}", d.ok("ui.elements", json!({"prefix": "project.item."})));
    }
}

/// #201: the clip's right-click Speed/Duration… opens the dialog instead of applying 50%; OK
/// applies the speed typed there, Cancel leaves the clip alone.
#[test]
fn speed_duration_from_the_clip_menu_opens_the_dialog() {
    let mut d = Driver::new();
    let c = d.v1(0);
    d.exec("timeline.select", json!({"clips": [c.id.0]}));
    d.ok("ui.click", json!({"id": format!("timeline.clip.{}", c.id.0), "button": "right"}));
    d.frames(3);
    d.click("timeline.clipMenu.clip.speedDuration");
    assert_eq!(d.v1(0).speed, 1.0, "nothing applied yet");
    let dlg = d.app().ui.clip_dialog.clone().expect("the dialog opened");
    assert_eq!(dlg.command, "clip.speedDuration");
    assert_eq!(dlg.params["speed"], json!(100.0), "starts at the clip's speed");
    assert!(d.has("speedDuration.reverse") && d.has("speedDuration.interpolation.frameBlending"));
    d.click("speedDuration.cancel");
    assert!(d.app().ui.clip_dialog.is_none());
    assert_eq!(d.v1(0).speed, 1.0, "Cancel leaves it");

    d.menu("clip.speedDuration");
    if let Some(dlg) = d.app().ui.clip_dialog.as_mut() {
        dlg.params["speed"] = json!(200.0);
    }
    d.click("speedDuration.interpolation.frameBlending");
    d.click("speedDuration.ok");
    let got = d.v1(0);
    assert_eq!(got.speed, 2.0);
    assert_eq!(got.time_interpolation, filmcraft_project::TimeInterpolation::FrameBlending);
}

/// The Properties panel's speed footer opens Clip ▸ Speed/Duration… on click instead of doing
/// nothing; Cancel leaves the clip alone.
#[test]
fn the_properties_speed_button_opens_the_speed_dialog() {
    let mut d = Driver::new();
    let c = d.v1(0);
    d.exec("timeline.select", json!({"clips": [c.id.0]}));
    d.ok("ui.panel.show", json!({"panel": "Properties"}));
    d.frames(3);
    assert!(d.has("properties.speed"), "the speed button is laid out");
    // the demo project's panel is short: the footer sits below the fold (#1322)
    d.ok("ui.scroll", json!({"id": "panel.Properties", "dy": -2000.0}));
    d.frames(3);
    d.click("properties.speed");
    let dlg = d.app().ui.clip_dialog.clone().expect("the dialog opened");
    assert_eq!(dlg.command, "clip.speedDuration");
    assert_eq!(dlg.params["speed"], json!(100.0), "starts at the clip's speed");
    d.click("speedDuration.cancel");
    assert!(d.app().ui.clip_dialog.is_none());
    assert_eq!(d.v1(0).speed, 1.0, "Cancel leaves the clip alone");
}

#[test]
fn speed_duration_reopens_with_the_clips_reverse_state() {
    let mut d = Driver::new();
    let c = d.v1(0);
    d.exec("timeline.select", json!({"clips": [c.id.0]}));
    d.exec("clip.speedDuration", json!({"speed": 200, "reverse": true}));
    assert!(d.v1(0).reverse);

    d.menu("clip.speedDuration");
    let draft = d.app().ui.clip_dialog.as_ref().expect("the dialog opened");
    assert_eq!(draft.params["reverse"], true, "uses the stored direction, not the positive speed magnitude");
    assert_eq!(draft.params["speed"], 200.0);
    d.click("speedDuration.cancel");
    assert!(d.v1(0).reverse, "Cancel retains reverse playback");

    d.menu("clip.speedDuration");
    d.click("speedDuration.ok");
    assert!(d.v1(0).reverse, "accepting unchanged settings retains reverse playback");
    assert_eq!(d.v1(0).speed, 2.0);
    d.menu("clip.speedDuration");
    d.click("speedDuration.reverse");
    d.click("speedDuration.ok");
    assert!(!d.v1(0).reverse, "the checkbox can explicitly restore forward playback");
}

/// #786: the clip's right-click Label menu lists the labels by the user's names (with their
/// colours, as Edit ▸ Label does) and each choice labels the clip.
#[test]
fn the_clip_menu_label_choices_use_the_user_names() {
    let mut d = Driver::new();
    let key = filmcraft_engine::settings::label_id(Label::Iris);
    d.app().session.prefs.labels.colors.get_mut(&key).unwrap().name = "Interviews".into();
    let c = d.v1(0);
    d.exec("timeline.select", json!({"clips": [c.id.0]}));
    d.ok("ui.click", json!({"id": format!("timeline.clip.{}", c.id.0), "button": "right"}));
    d.frames(3);
    d.click("timeline.clipMenu.edit.label");
    let choice = d.ok("ui.elements", json!({"prefix": "timeline.clipMenu.label.Iris"}));
    assert_eq!(choice[0]["label"], json!("Interviews"), "{choice}");
    d.click("timeline.clipMenu.label.Iris");
    assert_eq!(d.v1(0).label, Label::Iris);
}

/// #1018: Scene Edit Detection… is in the clip's right-click menu and opens its dialog.
#[test]
fn scene_edit_detection_from_the_clip_menu_opens_the_dialog() {
    let mut d = Driver::new();
    let c = d.v1(0);
    d.exec("timeline.select", json!({"clips": [c.id.0]}));
    d.ok("ui.click", json!({"id": format!("timeline.clip.{}", c.id.0), "button": "right"}));
    d.frames(3);
    d.click("timeline.clipMenu.clip.sceneEditDetection");
    for id in ["sceneDetect.applyCuts", "sceneDetect.createSubclips", "sceneDetect.generateMarkers", "sceneDetect.ok"] {
        assert!(d.has(id), "{id}");
    }
    d.click("sceneDetect.cancel");
    assert!(!d.has("sceneDetect.ok"));
}

#[test]
fn quitting_with_unsaved_changes_asks_to_save() {
    let mut d = Driver::new();
    d.edit();
    // the close is cancelled and the prompt asks instead
    d.request_close();
    assert!(d.sent(&egui::ViewportCommand::CancelClose), "the window must stay open");
    d.frames(2);
    assert!(d.has("quit.save") && d.has("quit.dontSave") && d.has("quit.cancel"));
    // Cancel keeps the app and the changes
    d.click("quit.cancel");
    assert!(d.app().ui.clip_dialog.is_none());
    assert!(d.app().session.is_dirty() && !d.app().quit_confirmed);
    d.request_close();
    assert!(d.sent(&egui::ViewportCommand::CancelClose), "asks again");
    d.frames(2);
    // Don't Save discards the changes (and their recovery snapshot) and closes the window
    d.click("quit.dontSave");
    assert!(d.app().quit_confirmed && !d.app().session.is_dirty());
    assert!(d.app().ui.clip_dialog.is_none());
    d.request_close();
    assert!(!d.sent(&egui::ViewportCommand::CancelClose), "the confirmed quit goes through");
}

/// #1943: the macOS menu bar's Quit (Cmd+Q) sends `app.quit` to the command inbox; with unsaved
/// changes that closes the window through the prompt instead of quitting outright.
#[test]
fn native_menu_quit_with_unsaved_changes_asks_to_save() {
    let mut d = Driver::new();
    d.edit();
    let (tx, rx) = channel();
    d.app().command_inbox = Some(rx);
    tx.send("app.quit".to_string()).unwrap();
    d.frames(1);
    assert!(d.sent(&egui::ViewportCommand::Close), "Quit asks the window to close");
    assert!(!d.app().quit_confirmed, "nothing is confirmed yet");
    // the window's close request is cancelled and the prompt asks instead
    d.request_close();
    assert!(d.sent(&egui::ViewportCommand::CancelClose), "the window must stay open");
    d.frames(2);
    assert!(d.has("quit.save") && d.has("quit.dontSave") && d.has("quit.cancel"));
    assert!(d.app().session.is_dirty());
}

#[test]
fn quit_save_on_a_new_project_uses_the_save_dialog_and_scripted_quit_does_not_ask() {
    let path = std::env::temp_dir().join(format!("fc-quit-save-{}.fcproj", std::process::id()));
    let _ = std::fs::remove_file(&path);
    let mut d = Driver::new();
    d.app().session.path = None;
    d.edit();
    let picked = path.to_string_lossy().into_owned();
    let mut answers = vec![None, Some(picked)];
    d.app().hooks.pick_save = Some(Box::new(move |_| if answers.is_empty() { None } else { answers.remove(0) }));
    d.request_close();
    d.frames(2);
    // the Save dialog is cancelled: back to the prompt, nothing quits
    d.click("quit.save");
    assert!(d.app().ui.clip_dialog.is_some() && !d.app().quit_confirmed);
    // saved: the project is written and the window closes
    d.click("quit.save");
    assert!(d.app().quit_confirmed && !d.app().session.is_dirty());
    assert!(path.exists(), "saved to the chosen file");
    let _ = std::fs::remove_file(&path);
    d.request_close();
    assert!(!d.sent(&egui::ViewportCommand::CancelClose));
    // the control channel's app.quit quits without the prompt
    let mut d = Driver::new();
    d.edit();
    d.ok("app.quit", json!({}));
    assert!(d.app().quit_confirmed);
}

/// #580: Escape while dragging a value in a dialog puts the value back and keeps the dialog open;
/// Escape with no drag still closes it.
#[test]
fn escape_mid_drag_reverts_the_field_and_keeps_the_dialog() {
    let mut d = Driver::new();
    let c = d.v1(0);
    d.exec("timeline.select", json!({"clips": [c.id.0]}));
    d.menu("clip.speedDuration");
    let els = d.ok("ui.elements", json!({"prefix": "speedDuration.speed"}));
    let r: Vec<f32> = els.as_array().unwrap().iter().find(|e| e["id"] == "speedDuration.speed").expect("speed field")["rect"]
        .as_array()
        .unwrap()
        .iter()
        .map(|v| v.as_f64().unwrap() as f32)
        .collect();
    let from = egui::pos2(r[0] + r[2] / 2.0, r[1] + r[3] / 2.0);
    let speed = |d: &mut Driver| d.app().ui.clip_dialog.as_ref().map(|g| g.params["speed"].clone());
    let drag = |d: &mut Driver, escape: bool| {
        let mut send = |e: egui::Event| {
            d.harness.input_mut().events.push(e);
            d.frames(1);
        };
        let button = |pos, pressed| egui::Event::PointerButton { pos, button: egui::PointerButton::Primary, pressed, modifiers: Default::default() };
        send(egui::Event::PointerMoved(from));
        send(button(from, true));
        for k in 1..=10 {
            send(egui::Event::PointerMoved(from + egui::vec2(4.0 * k as f32, 0.0)));
            if escape && k == 5 {
                send(egui::Event::Key { key: egui::Key::Escape, physical_key: None, pressed: true, repeat: false, modifiers: Default::default() });
            }
        }
        send(button(from + egui::vec2(40.0, 0.0), false));
        d.frames(3);
    };
    drag(&mut d, true);
    assert_eq!(speed(&mut d), Some(json!(100.0)), "the speed it had before the drag, and the dialog is still open");
    drag(&mut d, false);
    assert_ne!(speed(&mut d), Some(json!(100.0)), "without Escape the same drag changes the speed");
    d.ok("ui.key", json!({"key": "Escape"}));
    d.frames(2);
    assert!(d.app().ui.clip_dialog.is_none(), "Escape with no drag closes the dialog");
    assert_eq!(d.v1(0).speed, 1.0, "and applies nothing");
}

#[test]
fn enter_confirms_and_submits_clip_dialog() {
    let mut d = Driver::new();
    let c = d.v1(0);
    d.exec("timeline.select", json!({"clips": [c.id.0]}));
    d.menu("clip.speedDuration");
    assert!(d.app().ui.clip_dialog.is_some(), "speedDuration dialog is open");
    d.click("speedDuration.speed");
    d.frames(2);
    d.ok("ui.key", json!({"key": "A", "ctrl": true}));
    d.ok("ui.type", json!({"text": "200"}));
    d.ok("ui.key", json!({"key": "Enter"}));
    d.frames(2);
    assert!(d.app().ui.clip_dialog.is_none(), "Enter closes the dialog and commits OK");
    assert_eq!(d.v1(0).speed, 2.0, "the speed change was applied");
}

#[test]
fn enter_on_focused_cancel_cancels_clip_dialog() {
    let mut d = Driver::new();
    let c = d.v1(0);
    d.exec("timeline.select", json!({"clips": [c.id.0]}));
    d.menu("clip.speedDuration");
    assert!(d.app().ui.clip_dialog.is_some(), "speedDuration dialog is open");
    d.click("speedDuration.speed");
    d.frames(2);
    d.ok("ui.key", json!({"key": "A", "ctrl": true}));
    d.ok("ui.type", json!({"text": "200"}));
    assert_eq!(d.app().ui.clip_dialog.as_ref().unwrap().params["speed"], json!(200.0));
    d.ok("ui.focus", json!({"id": "speedDuration.cancel"}));
    d.frames(2);
    d.ok("ui.key", json!({"key": "Enter"}));
    d.frames(2);
    assert!(d.app().ui.clip_dialog.is_none(), "Enter on focused Cancel closes the dialog");
    assert_eq!(d.v1(0).speed, 1.0, "Cancel leaves speed unchanged");
}

#[test]
fn shortcuts_suppressed_while_clip_dialog_open() {
    let mut d = Driver::new();
    let c = d.v1(0);
    d.exec("timeline.select", json!({"clips": [c.id.0]}));
    d.menu("clip.speedDuration");
    assert!(d.app().ui.clip_dialog.is_some(), "speedDuration dialog is open");

    // Timeline shortcut: Backspace (edit.clear) must not reach the timeline while the dialog is open
    d.ok("ui.key", json!({"key": "Backspace"}));
    d.frames(2);
    assert!(d.app().ui.clip_dialog.is_some(), "clip dialog remains open");
    let seq = d.app().session.active_sequence().unwrap();
    assert!(!seq.video_tracks[0].items.is_empty(), "selected clip is not deleted by Backspace shortcut");
    assert_eq!(seq.video_tracks[0].items[0].id, c.id, "clip remains on timeline");

    // Global shortcut: Space (playback.toggle) must not start playback while the dialog is open
    assert!(!d.app().playback.playing, "playback is stopped initially");
    d.ok("ui.key", json!({"key": "Space"}));
    d.frames(2);
    assert!(!d.app().playback.playing, "Space key does not trigger playback while clip dialog is open");
    assert!(d.app().ui.clip_dialog.is_some(), "clip dialog remains open");
}

#[test]
fn clip_context_menu_adds_a_marker_at_the_playhead_with_one_undo() {
    for audio in [false, true] {
        let mut d = Driver::new();
        d.exec("markers.clearAll", json!({}));
        d.exec("playhead.set", json!({"frame": 35}));
        let seq = d.app().session.active_sequence().unwrap();
        let clip = if audio { seq.audio_tracks[0].items[0].id } else { seq.video_tracks[0].items[0].id };
        let playhead = d.app().session.playhead();
        let before = d.app().session.project.clone();
        let history = d.app().session.history.undo.len();
        d.ok("ui.click", json!({"id": format!("timeline.clip.{}", clip.0), "button": "right", "fy": 0.3}));
        d.frames(3);
        assert!(d.has("timeline.clipMenu.markers.add"), "Add Marker is available on video and audio clips");
        d.click("timeline.clipMenu.markers.add");
        let markers = &d.app().session.active_sequence().unwrap().markers;
        assert_eq!(markers.len(), 1);
        assert_eq!(markers[0].start, playhead);
        assert_eq!(d.app().session.history.undo.len(), history + 1);
        let after = d.app().session.project.clone();
        d.exec("edit.undo", json!({}));
        assert_eq!(serde_json::to_value(&*d.app().session.project).unwrap(), serde_json::to_value(&*before).unwrap());
        d.exec("edit.redo", json!({}));
        assert_eq!(serde_json::to_value(&*d.app().session.project).unwrap(), serde_json::to_value(&*after).unwrap());
    }
}

/// #1885: Project panel ▸ right-click one of several selected clips ▸ Proxy ▸ Create Proxies…
/// makes proxies for all of them, as in Premiere; a clip right-clicked outside the selection gets
/// one for itself only.
#[test]
fn create_proxies_from_the_item_menu_takes_the_whole_selection() {
    let mut d = Driver::new();
    let wav: std::sync::Arc<[u8]> = filmcraft_engine::previews::write_wav_f32(&[0.0; 9_600], 48_000).into();
    let mut items = Vec::new();
    for name in ["/takes/a.wav", "/takes/b.wav", "/takes/c.wav"] {
        items.push(filmcraft_engine::commands::import_bytes(&mut d.app().session, name, wav.clone(), None).unwrap().0);
    }
    d.ok("ui.panel.show", json!({"panel": "Project"}));
    d.exec("project.view.set", json!({"view": "list"}));
    d.exec("project.select", json!({"items": [items[0], items[1]]}));
    d.frames(4);
    let create_from = |d: &mut Driver, item: u64| -> Vec<u64> {
        d.ok("ui.click", json!({"id": format!("project.item.{item}"), "button": "right"}));
        d.frames(3);
        d.click("project.itemMenu.proxy");
        d.click("project.itemMenu.proxy.create");
        let draft = d.app().ui.create_proxies.take().map(|p| p.items);
        draft.unwrap_or_else(|| panic!("Create Proxies… did not open its dialog"))
    };
    // right-click the second of the two selected clips: both get a proxy
    assert_eq!(create_from(&mut d, items[1]), vec![items[0], items[1]]);
    assert_eq!(d.app().session.state.project_selection.iter().map(|i| i.0).collect::<Vec<_>>(), vec![items[0], items[1]], "the selection stays");
    // right-click a clip that is not selected: only that one
    assert_eq!(create_from(&mut d, items[2]), vec![items[2]]);
}

/// File ▸ Close All Projects with unsaved changes refused ("save it first, or pass force"); like
/// Close Project, it asks Save / Don't Save / Cancel, and closes right away when nothing is unsaved.
#[test]
fn close_all_projects_asks_to_save() {
    let mut d = Driver::new();
    d.edit();
    assert_eq!(d.menu("file.closeAllProjects")["dialog"], "closeProject");
    assert!(d.has("closeProject.save") && d.has("closeProject.dontSave") && d.has("closeProject.cancel"));
    d.click("closeProject.cancel");
    assert!(!d.app().session.project.items.is_empty() && d.app().session.is_dirty(), "Cancel keeps the project");
    d.menu("file.closeAllProjects");
    d.click("closeProject.dontSave");
    assert!(d.app().session.project.items.is_empty(), "closed without saving");
    assert!(d.app().ui.clip_dialog.is_none());
    // nothing unsaved: no question
    d.exec("file.openDemoProject", json!({}));
    let path = std::env::temp_dir().join(format!("fc-close-all-{}.fcproj", std::process::id()));
    d.exec("file.saveAs", json!({"path": path.to_string_lossy()}));
    assert!(!d.app().session.is_dirty());
    d.menu("file.closeAllProjects");
    let _ = std::fs::remove_file(&path);
    assert!(d.app().ui.clip_dialog.is_none());
    assert!(d.app().session.project.items.is_empty(), "closed at once");
}
/// Clip ▸ Rename… asked for nothing and failed with "need `name`": it opens a Rename dialog with
/// the selected clip's name (its linked partner is renamed with it), or the selected Project item's
/// when the Project panel has focus, and says what to select when nothing is.
#[test]
fn clip_rename_menu_opens_a_rename_dialog() {
    let mut d = Driver::new();
    let (picture, sound) = {
        let q = d.app().session.active_sequence().unwrap();
        let v = q.video_tracks[0].items[0].clone();
        let a = q.audio_tracks.iter().flat_map(|t| &t.items).find(|a| a.link.is_some() && a.link == v.link).unwrap().clone();
        (v, a)
    };
    let name = |d: &mut Driver, c: filmcraft_engine::project::ClipId| d.app().session.active_sequence().unwrap().find_item(c).unwrap().1.name.clone();
    // nothing selected: an explanation, no dialog
    d.exec("edit.deselectAll", json!({}));
    d.ok("ui.set", json!({"focused": "Timeline"}));
    assert!(d.call("ui.menu.invoke", json!({"id": "clip.rename"}))["ok"] != json!(true));
    assert!(d.app().ui.clip_dialog.is_none());
    // a linked clip: both halves, with the current name filled in
    d.exec("timeline.select", json!({"clips": [picture.id.0]}));
    let undo = d.app().session.history.undo.len();
    d.menu("clip.rename");
    assert!(d.has("rename.name") && d.has("rename.ok"));
    assert_eq!(d.app().ui.clip_dialog.as_ref().unwrap().params["name"], json!(picture.name));
    d.ok("ui.set", json!({"clipDialog": {"name": "Opening shot"}}));
    d.click("rename.ok");
    assert!(d.app().ui.clip_dialog.is_none());
    assert_eq!(name(&mut d, picture.id), "Opening shot");
    assert_eq!(name(&mut d, sound.id), "Opening shot", "the linked sound is renamed with its picture");
    assert_eq!(d.app().session.history.undo.len(), undo + 1, "one undo step");
    // the Project panel: its selected item
    let item = d.app().session.project.items.values().find(|i| i.as_media().is_some()).map(|i| i.id).unwrap();
    d.exec("project.select", json!({"items": [item.0]}));
    d.ok("ui.set", json!({"focused": "Project"}));
    d.menu("clip.rename");
    d.ok("ui.set", json!({"clipDialog": {"name": "Renamed media"}}));
    d.click("rename.ok");
    assert_eq!(d.app().session.project.item(item).unwrap().name, "Renamed media");
}

/// #2191: Duration is editable, updates Speed, and applies through the undoable command.
#[test]
fn duration_text_edits_speed_and_rejects_invalid_input() {
    let mut d = Driver::new();
    let original = d.v1(0);
    let rate = d.app().session.active_sequence().unwrap().settings.frame_rate;
    let target = rate.tick_of(rate.frame_at(original.duration) / 2);
    d.exec("timeline.select", json!({"clips": [original.id.0]}));
    d.menu("clip.speedDuration");
    assert!(d.has("speedDuration.duration"), "Duration must be an editable automation field");
    /// Replace the duration through keyboard input and settle the dialog's draft state.
    fn type_duration(d: &mut Driver, text: &str) {
        d.click("speedDuration.duration");
        d.ok("ui.key", json!({"key": "Cmd+A"}));
        d.ok("ui.type", json!({"text": text}));
        d.frames(3);
    }
    for invalid in ["abc", "00:00:00:00", "-100"] {
        type_duration(&mut d, invalid);
        d.click("speedDuration.ok");
        assert!(d.app().ui.clip_dialog.is_some(), "invalid duration must not apply");
        assert_eq!(d.v1(0).duration, original.duration);
    }
    type_duration(&mut d, &filmcraft_time::format_timecode_frames(rate.frame_at(target), rate, false));
    let speed = d.app().ui.clip_dialog.as_ref().unwrap().params["speed"].as_f64().unwrap();
    assert!((speed - original.duration.0 as f64 * 100.0 / target.0 as f64).abs() < 1e-8);
    assert_eq!(d.v1(0).duration, original.duration, "typing only edits the draft");

    d.click("speedDuration.ok");
    assert!(d.app().ui.clip_dialog.is_none());
    assert!((d.v1(0).duration.0 - target.0).abs() <= 1);
    d.exec("edit.undo", json!({}));
    assert_eq!(d.v1(0).duration, original.duration);
    d.exec("edit.redo", json!({}));
    assert!((d.v1(0).duration.0 - target.0).abs() <= 1);

    d.menu("clip.speedDuration");
    type_duration(&mut d, "00:00:01:00");
    d.click("speedDuration.cancel");
    assert!((d.v1(0).duration.0 - target.0).abs() <= 1, "Cancel leaves the clip unchanged");
    d.menu("clip.speedDuration");
    d.app().ui.clip_dialog.as_mut().unwrap().params["duration"] = json!("00:00:01:00");
    d.frames(3);
    d.click("speedDuration.ok");
    assert!((d.v1(0).duration.0 - rate.tick_of(filmcraft_time::parse_timecode("00:00:01:00", rate, false, 0).unwrap()).0).abs() <= 1);
}

/// Speed edits retain the command's one-frame minimum without changing the requested speed.
#[test]
fn speed_edits_preserve_one_frame_minimum() {
    let mut d = Driver::new();
    let original = d.v1(0);
    let rate = d.app().session.active_sequence().unwrap().settings.frame_rate;
    d.exec("timeline.trim", json!({"clip": original.id.0, "edge": "out", "deltaFrames": 1 - rate.frame_at(original.duration)}));
    assert_eq!(d.v1(0).duration, rate.tick_of(1));
    d.exec("timeline.select", json!({"clips": [original.id.0]}));
    for percent in [200.0, 100_000.0] {
        d.menu("clip.speedDuration");
        d.app().ui.clip_dialog.as_mut().unwrap().params["speed"] = json!(percent);
        d.frames(3);
        assert_eq!(d.app().ui.clip_dialog.as_ref().unwrap().params["duration"], json!("00:00:00:01"));
        d.click("speedDuration.ok");
        assert!(d.app().ui.clip_dialog.is_none(), "a valid speed edit must apply");
        assert_eq!(d.v1(0).speed, percent / 100.0);
        assert_eq!(d.v1(0).duration, rate.tick_of(1));
        d.exec("edit.undo", json!({}));
        assert_eq!(d.v1(0).speed, 1.0);
    }
}

/// A file the OS dropped on the window, as the native integration hands it to egui.
#[derive(Debug)]
struct OsFile(std::path::PathBuf);

impl egui::DroppedFile for OsFile {
    fn path(&self) -> &std::path::Path {
        &self.0
    }

    fn bytes(&self) -> Result<Vec<u8>, String> {
        std::fs::read(&self.0).map_err(|e| e.to_string())
    }
}

impl Driver {
    /// Drop `paths` on the window: one frame carries them, as egui reports a drop once.
    fn drop_files(&mut self, paths: &[&std::path::Path]) {
        for p in paths {
            self.harness.input_mut().dropped_files.push(std::sync::Arc::new(OsFile(p.to_path_buf())));
        }
        self.frames(1);
        self.harness.input_mut().dropped_files.clear();
        self.frames(3);
    }

    fn path(&mut self) -> Option<String> {
        self.app().session.path.clone()
    }
}

/// #3843: a project file dropped on the window opens, as File ▸ Open does, instead of being
/// imported (which did nothing). With unsaved changes Save / Don't Save / Cancel is asked first,
/// as it now is for File ▸ Open and Open Recent too.
#[test]
fn a_dropped_project_file_opens_and_asks_about_unsaved_changes() {
    let tmp = tempfile::tempdir().unwrap();
    let dir = tmp.path();
    let (a, b) = (dir.join("A.fcproj"), dir.join("B.FCPROJ"));
    let mut d = Driver::new();
    d.exec("file.saveCopy", json!({"path": b.to_string_lossy()}));
    d.exec("file.saveAs", json!({"path": a.to_string_lossy()}));
    let a_path = d.path().expect("saved as A");
    let items = d.app().session.project.items.len();

    // Save on a never-saved project: Save As first, then the dropped project opens
    let saved = dir.join("saved-from-prompt.fcproj");
    let saved_path = saved.to_string_lossy().into_owned();
    let mut fresh = Driver::new();
    fresh.app().session.path = None;
    fresh.app().hooks.pick_save = Some(Box::new(move |_| Some(saved_path.clone())));
    fresh.edit();
    fresh.drop_files(&[&b]);
    fresh.click("openProject.save");
    assert!(saved.exists(), "saved to the chosen file");
    assert!(fresh.path().is_some_and(|p| p.ends_with("B.FCPROJ")), "opened {:?}", fresh.path());
    assert!(!fresh.app().session.is_dirty());

    // unsaved changes: the prompt, and Cancel keeps the project and its changes
    d.edit();
    d.drop_files(&[&b]);
    assert!(d.has("openProject.save") && d.has("openProject.dontSave") && d.has("openProject.cancel"));
    d.click("openProject.cancel");
    assert_eq!(d.path().as_deref(), Some(a_path.as_str()), "still the first project");
    assert!(d.app().session.is_dirty(), "its changes are kept");
    assert!(d.app().ui.clip_dialog.is_none());
    // Don't Save: the dropped project opens; nothing was imported into either project
    d.drop_files(&[&b]);
    d.click("openProject.dontSave");
    assert!(d.path().is_some_and(|p| p.ends_with("B.FCPROJ")), "opened {:?}", d.path());
    assert!(!d.app().session.is_dirty());
    assert_eq!(d.app().session.project.items.len(), items, "the project file was not imported as media");

    // nothing unsaved: the dropped project opens at once; media dropped with it is not imported
    let media = dir.join("note.txt");
    std::fs::write(&media, "not a project").unwrap();
    d.drop_files(&[&media, &a]);
    assert!(d.app().ui.clip_dialog.is_none(), "no prompt without unsaved changes");
    assert_eq!(d.path().as_deref(), Some(a_path.as_str()));
    assert_eq!(d.app().session.project.items.len(), items);

    // a project file that fails to open: Don't Save keeps the open project and its changes
    let broken = dir.join("broken.fcproj");
    std::fs::write(&broken, "not a project").unwrap();
    d.edit();
    d.drop_files(&[&broken]);
    d.click("openProject.dontSave");
    assert_eq!(d.path().as_deref(), Some(a_path.as_str()), "still the open project");
    assert!(d.app().session.is_dirty(), "its changes are kept");
    assert!(d.app().ui.status.contains("broken.fcproj"), "the reason is shown: {}", d.app().ui.status);

    // Open Recent and File ▸ Open ask the same question
    assert_eq!(d.menu("file.openRecent.1")["dialog"], "openProject");
    d.click("openProject.cancel");
    assert_eq!(d.path().as_deref(), Some(a_path.as_str()));
    let picked = b.to_string_lossy().into_owned();
    d.app().hooks.pick_open_project = Some(Box::new(move || Some(picked.clone())));
    d.menu("file.open");
    assert!(d.has("openProject.dontSave"), "File ▸ Open asks too");
    d.click("openProject.dontSave");
    assert!(d.path().is_some_and(|p| p.ends_with("B.FCPROJ")));
}