diff --git a/crates/engine-core/src/cmd/automate_cmds.rs b/crates/engine-core/src/cmd/automate_cmds.rs index 7b73550356..da8240f493 100644 --- a/crates/engine-core/src/cmd/automate_cmds.rs +++ b/crates/engine-core/src/cmd/automate_cmds.rs @@ -13,6 +13,7 @@ //! Close, Print and Export itself; the app shell fires Start, Open and Save. use crate::actions_cmds::ActionsExt; +use crate::preset_store::PresetStoreExt; use photocraft_doc::{Layer, LayerContent}; use serde_json::{Value, json}; @@ -345,7 +346,31 @@ fn create_droplet(s: &mut Session, p: &Value) -> Result { let name = p.get("name").and_then(Value::as_str).map(str::to_string).unwrap_or_else(|| stem(&path)); let steps_json: Vec = steps.iter().map(|(id, pr)| json!([id, pr])).collect(); let mut options = serde_json::Map::new(); + // The Create Droplet Destination (#13711): the form's choice rides with the droplet and + // `run_droplet` acts on it. A call that names no destination stores no key, so droplets + // written by earlier builds, and by API calls without the choice, keep running as Folder. + let dest_choice = match p.get("destination").filter(|v| !v.is_null()) { + None => None, + Some(Value::String(value)) if matches!(value.as_str(), "none" | "folder" | "saveAndClose") => { + options.insert("destination".into(), json!(value.as_str())); + Some(value.as_str()) + } + Some(_) => return Err(bad(cmd, "\"destination\" must be \"none\", \"folder\" or \"saveAndClose\"")), + }; + // `overrideSaveAs` is the form's Override Action "Save As" Commands tick, stored as the issue + // asks. The hosted form answers the untouched box with false, so only a ticked box is stored: + // an off box must mean exactly today's run, which stored no such key. `run_droplet` does not + // act on either state (not measured for droplet runs, see #13711). + if p.get("overrideSaveAs").and_then(Value::as_bool) == Some(true) { + options.insert("overrideSaveAs".into(), json!(true)); + } for k in ["output", "format", "quality", "suffix"] { + // "output" rides only with a folder droplet (#13711 review): neither a none nor a + // saveAndClose run ever reads a folder, and a stored destination of none or saveAndClose + // only exists in droplets written by this code, so leaving the key out keeps behaviour. + if k == "output" && matches!(dest_choice, Some("none" | "saveAndClose")) { + continue; + } if let Some(v) = p.get(k) { options.insert(k.into(), v.clone()); } @@ -366,7 +391,10 @@ fn create_droplet(s: &mut Session, p: &Value) -> Result { } let text = serde_json::to_string_pretty(&droplet).unwrap_or_default(); write_file(&path, text.as_bytes())?; - let shim = p.get("shim").and_then(Value::as_bool).unwrap_or(cfg!(unix)); + // A none droplet writes no shim (#13711 review): its shim calls `photocraft-cli droplet`, and + // a headless run of a none droplet is always refused - a launcher built from it could only + // ever print that refusal. + let shim = dest_choice != Some("none") && p.get("shim").and_then(Value::as_bool).unwrap_or(cfg!(unix)); let shim_path = if shim { Some(write_shim(&path)?) } else { None }; Ok(json!({"path": path, "shim": shim_path, "steps": steps.len()})) } @@ -422,6 +450,14 @@ fn run_droplet(s: &mut Session, p: &Value) -> Result { } let steps = v.get("action").and_then(|a| a.get("steps")).cloned().ok_or_else(|| bad(cmd, "droplet has no action"))?; let opts = v.get("options").cloned().unwrap_or(json!({})); + // Where the run writes (the Create Droplet Destination dropdown, #13711), stored with the + // droplet. A droplet written before the change names no destination: it runs as Folder, which + // is exactly what it does today. + let destination = match opts.get("destination") { + None | Some(Value::Null) => "folder", + Some(Value::String(value)) if matches!(value.as_str(), "none" | "folder" | "saveAndClose") => value.as_str(), + Some(_) => return Err(bad(cmd, "the droplet's \"destination\" must be \"none\", \"folder\" or \"saveAndClose\"")), + }; // A droplet saved before Include All Subfolders existed has no key and lists a folder's own images. let include_subfolders = match opts.get("includeSubfolders") { None | Some(Value::Null) => false, @@ -460,14 +496,32 @@ fn run_droplet(s: &mut Session, p: &Value) -> Result { if inputs.is_empty() { return Err(bad(cmd, "no input images")); } - let output = output.unwrap_or_else(|| default_output(&inputs[0])); - let mut bp = json!({"steps": steps, "input": inputs, "output": output, "format": opts.get("format").cloned().unwrap_or(json!("same"))}); + // Destination None never reaches Batch (#13711): it keeps the opened documents here and writes + // nothing, and a headless run of it is refused - see `run_droplet_open`. + if destination == "none" { + return run_droplet_open(s, cmd, path, &steps, &inputs, v.get("calledActions")); + } + let mut bp = json!({"steps": steps, "input": inputs, "format": opts.get("format").cloned().unwrap_or(json!("same"))}); + let output = if destination == "saveAndClose" { + // Batch's Save and Close (#13659) writes each source over itself in its own format and + // takes no output folder: neither the stored `output` nor a call-site one is passed on. + bp["destination"] = json!("saveAndClose"); + Value::Null + } else { + // The folder the call or the droplet chose, else the one the Include All Subfolders walk + // settled on above, else next to the first input. + let output = output.unwrap_or_else(|| default_output(&inputs[0])); + bp["output"] = json!(&output); + json!(output) + }; if let Some(name) = v.get("action").and_then(|action| action.get("name")).or_else(|| v.get("name")) { bp["action"] = name.clone(); } - // The Play options are saved with the droplet: Override Open makes a recorded Open read each - // dropped input, and the two Suppress options open the inputs as Batch does with them. - // Include All Subfolders was applied above, where the dropped folders became files. + // Override Open is saved with the droplet: a recorded Open then reads each dropped input, and the + // two Suppress options open the inputs as Batch does with them; Include All Subfolders was applied + // above, where the dropped folders became files. The stored Override "Save As" tick is not passed + // on: its effect during a droplet run is not measured (#13711), so the run keeps today's Batch + // behaviour exactly. for key in ["quality", "overrideOpen", "suppressOpenOptions", "suppressWarnings"] { if let Some(value) = opts.get(key) { bp[key] = value.clone(); @@ -480,6 +534,96 @@ fn run_droplet(s: &mut Session, p: &Value) -> Result { Ok(json!({"droplet": path, "output": output, "files": r["files"], "errors": r["errors"]})) } +/// Destination None of the Create Droplet dialog (#13711), run in the app: each input opens as a +/// document of this session, the action's steps play on it, and nothing is written - the tab stays +/// open and changed, its last History state the action's step. Only the running app can keep those +/// tabs: the desktop and web shells attach a preset store to their session once it loads +/// (`crate::preset_store`), the headless sessions (`photocraft-cli droplet`, MCP, tests) never do, +/// and there the run is refused with an error naming the app instead of opening documents nobody +/// will see. (The shells attach the store a few frames into startup; a droplet drop or a menu run +/// is a user action after that, and an early refusal says so rather than guessing.) +fn run_droplet_open(s: &mut Session, cmd: &str, path: &str, steps: &Value, inputs: &[String], called: Option<&Value>) -> Result { + if s.preset_store().is_none() { + return Err(EngineError::Other(format!( + "{cmd}: this droplet's Destination is None, which leaves the opened documents in the app: run it in the app, or give the droplet a Folder or Save and Close" + ))); + } + // The steps are parsed and filtered first, before any of the caller's state changes: a stored + // droplet whose steps do not parse is refused here, and the run has installed nothing and + // raised nothing yet (#13711 review, automate_cmds.rs:573). The steps are exactly the ones + // Batch's folder run keeps (its retained-steps rule, quickExport included): view steps go, + // recorded saves are skipped - they mark a destination None has none of, write nothing, and + // leave the document changed, as Batch with no destination does. + let steps: Vec<(String, Value)> = + parse_steps(steps, cmd)?.into_iter().filter(|(id, _)| crate::file_cmds::batch_keeps_step(id) && id != "file.export.quickExport").collect(); + // The droplet plays from the actions it captured (#2794's rule): install them for this run, as + // the folder path forwards `calledActions` to Batch, so a Play Action step cannot fall back to + // - or play a wrong same-named action of - the app's live list. + let embedded = match called { + None | Some(Value::Null) => None, + Some(v) => { + let actions: Vec = serde_json::from_value(v.clone()).map_err(|e| bad(cmd, format!("\"calledActions\": {e}")))?; + if actions.len() > 4096 { + return Err(bad(cmd, "\"calledActions\" holds more than 4096 actions")); + } + // In the droplet's order, so its calls by index keep their targets (Batch's own rule). + Some(crate::actions_cmds::ActionList::from_file(crate::actions_cmds::ActionsFile::new(actions, Vec::new()))) + } + }; + let previous_called = std::mem::replace(&mut s.actions_mut().called, embedded); + // The folder run nests through Batch's own scratch sessions, each raised `batch_depth`; this + // run plays in the caller's session, so it raises the depth itself and hands it back - a + // droplet that runs droplets stops at `MAX_BATCH_DEPTH` with the shared error text instead of + // opening one document per level until the stack or memory runs out (#13711 review HIGH-1). + let previous_depth = s.recording.batch_depth; + s.recording.batch_depth = previous_depth.saturating_add(1); + let mut errors = Vec::new(); + for input in inputs { + let ran = (|| -> Result<()> { + // The shared open core, as File > Open and Batch's scratch run use it: import options, + // Raw Defaults resolution, the Color Settings policy and the read-only source mark - + // not a bare import (#13711 review MEDIUM-3). + crate::file_cmds::open_bytes_as(s, &file_name(input), &read_file(input)?, None, Some(input.clone()))?; + for (id, params) in &steps { + if crate::file_cmds::batch_save_point(id) { + continue; + } + // A droplet run inside a script charges each step to the script's total, as Batch does. + if s.recording.script_depth > 0 { + if s.recording.script_steps >= MAX_SCRIPT_STEPS { + return Err(EngineError::Other(format!("the script ran more than {MAX_SCRIPT_STEPS} steps in all (scripts calling scripts)"))); + } + s.recording.script_steps += 1; + } + let params = crate::actions_cmds::playback_params(s, id, params)?; + let response = s.execute(id, params)?; + // A nested batch, droplet or played action that hit the nesting or shared playback + // limit fails this input too, instead of letting it pass as processed - Batch's own + // step-loop promotion, mirrored so the stop reaches the outermost answer. + if let Some(limit) = crate::actions_cmds::limit_stop(&response) { + return Err(EngineError::BatchLimit(limit)); + } + if let Some(error) = crate::actions_cmds::nested_failure(id, &response) { + return Err(EngineError::Other(error)); + } + } + Ok(()) + })(); + if let Err(error) = ran { + let mut entry = json!({"file": input, "error": error.to_string()}); + // The mark Batch's FileFailure puts on a limit stop, so the next enclosing run (and + // its parent through `limit_stop`) promotes it instead of swallowing it. + if matches!(error, EngineError::BatchLimit(_)) { + entry["limit"] = json!(true); + } + errors.push(entry); + } + } + s.recording.batch_depth = previous_depth; + s.actions_mut().called = previous_called; + Ok(json!({"droplet": path, "output": null, "files": [], "errors": errors})) +} + /// The images a dropped folder contributes: its own, sorted by name, or with Include All /// Subfolders also every subfolder's, depth first as Batch walks them, without going into the /// subfolder `skip` (the output). Results land flat in the output folder, so a later image @@ -679,7 +823,7 @@ pub fn specs() -> Vec { "file.automate.createDroplet", "Create Droplet…", &["File", "Automate"], - r##"{"path":str (.pcdroplet),"steps":[[id,params]…]?,"action":name|index|[steps]? (used only when steps is absent; named actions exclude disabled steps),"set":str? (with a named action: that set's action of the name),"name":str?,"output":folder?,"format":"same|png|jpg|…"?,"quality":0..12?,"overrideOpen":bool?,"includeSubfolders":bool?,"suppressOpenOptions":bool?,"suppressWarnings":bool? (the Play options, saved in the droplet and used by each run),"shim":bool=true on Unix (writes .command calling `photocraft-cli droplet`)} → {path, shim} (the actions its Play Action and Conditional steps call are saved in the droplet)"##, + r##"{"path":str (.pcdroplet),"steps":[[id,params]…]?,"action":name|index|[steps]? (used only when steps is absent; named actions exclude disabled steps),"set":str? (with a named action: that set's action of the name),"name":str?,"destination":"none|folder|saveAndClose"? (#13711, stored with the droplet; an omitted destination stores no key and the droplet runs as folder, as before),"output":folder? (stored only with a folder destination),"format":"same|png|jpg|…"?,"quality":0..12?,"overrideOpen":bool?,"includeSubfolders":bool?,"suppressOpenOptions":bool?,"suppressWarnings":bool? (the Play options, saved in the droplet and used by each run),"overrideSaveAs":bool? (the form's Override Action "Save As" Commands tick, stored; a run does not act on it yet),"shim":bool=true on Unix (writes .command calling `photocraft-cli droplet`; a none droplet gets no shim - its headless run is refused)} → {path, shim} (the actions its Play Action and Conditional steps call are saved in the droplet)"##, native, create_droplet ), @@ -687,7 +831,7 @@ pub fn specs() -> Vec { "file.automate.runDroplet", "Run Droplet", &[], - r##"{"droplet":path,"input":[files or folders],"output":folder? (default: droplet's, else /droplet-output)} → {files, errors} (a folder gives its own images, or with the droplet's includeSubfolders also its subfolders' depth first; results are written flat into output, a later one replacing an earlier one of the same name)"##, + r##"{"droplet":path,"input":[files or folders],"output":folder? (default: droplet's, else /droplet-output)} → {files, errors} (an input whose output name was already written in the run goes to errors; the droplet's stored "destination" (#13711): folder and a stored or absent output write copies there as before, saveAndClose overwrites each input in its own format and takes no output, none opens each input in the running app, keeps it changed and writes nothing - refused in a headless session, where no app stays open; a folder gives its own images, or with the droplet's includeSubfolders also its subfolders' depth first, written flat into output, a later one replacing an earlier one of the same name)"##, native, run_droplet ), @@ -718,6 +862,9 @@ pub fn specs() -> Vec { ] } +#[cfg(test)] +#[path = "droplet_destination_tests.rs"] +mod droplet_destination_tests; #[cfg(test)] #[path = "droplet_play_options_tests.rs"] mod droplet_play_options_tests; diff --git a/crates/engine-core/src/cmd/droplet_destination_tests.rs b/crates/engine-core/src/cmd/droplet_destination_tests.rs new file mode 100644 index 0000000000..8cd260d5e1 --- /dev/null +++ b/crates/engine-core/src/cmd/droplet_destination_tests.rs @@ -0,0 +1,520 @@ +//! The Create Droplet Destination (#13711): the droplet stores `destination` (none, folder or +//! saveAndClose), and `file.automate.runDroplet` runs it: Save and Close overwrites each input in +//! its own format, Folder writes its copy as before, None keeps each document open and changed in +//! the app and writes nothing (a headless session refuses it). A droplet written before the change +//! (no `destination` stored, an `output` folder) still runs as Folder. + +use super::*; + +use crate::actions_cmds::ActionsExt; +use crate::preset_store::PresetStoreExt; + +/// The fixture folder for one test, like `droplet_play_options_tests.rs`: a `tempfile` folder that +/// is taken away when the returned `TempDir` goes out of scope at the end of the test, even if the +/// test panics. The caller keeps the pair: the `TempDir` only to hold, the `String` to name paths. +fn tmp(name: &str) -> (tempfile::TempDir, String) { + let root = tempfile::tempdir().expect("a fixture folder"); + let d = root.path().join(name); + std::fs::create_dir_all(&d).expect("the fixture folder"); + (root, d.to_string_lossy().into_owned()) +} + +/// A 4x2 gray PNG at `path` (or a JPEG of it, by extension); the run's steps rotate it, so the +/// written shape tells what ran. +fn input_picture(path: &str) { + let mut s = crate::session(); + s.execute("file.new", json!({"width": 4, "height": 2, "background": "#808080"})).unwrap(); + crate::file_cmds::save_doc(&s.active().unwrap().doc, path, None).unwrap(); +} + +/// `dir/Rotate.pcdroplet` for one rotation step, with `extra` merged into the command params. +fn droplet(dir: &str, extra: Value) -> String { + let mut p = json!({"path": format!("{dir}/Rotate"), "steps": [["image.imageRotation.90cw", {}]], "shim": false}); + if let Value::Object(map) = extra { + p.as_object_mut().unwrap().extend(map); + } + let r = crate::session().execute("file.automate.createDroplet", p).unwrap(); + r["path"].as_str().unwrap().to_string() +} + +/// A session with an attached preset store: the desktop and web apps attach one once loaded +/// (`crate::preset_store`); headless CLI/MCP sessions and plain tests never do. +fn app_session() -> Session { + let mut s = crate::session(); + s.attach_preset_store(crate::preset_store::open(Box::new(crate::preset_store::MemBackend::default()))); + s +} + +/// #13711: createDroplet stores the Destination the form chose, and an Override Action "Save As" +/// Commands tick, in the droplet; a call that names neither keeps storing no such keys (as before). +#[test] +fn create_droplet_stores_destination_and_the_override_tick() { + let (_tmp, dir) = tmp("store"); + let dp = droplet(&dir, json!({"destination": "saveAndClose", "overrideSaveAs": true, "output": format!("{dir}/out")})); + let v: Value = serde_json::from_slice(&std::fs::read(&dp).unwrap()).unwrap(); + assert_eq!(v["options"]["destination"], json!("saveAndClose"), "the destination rides in the droplet"); + assert_eq!(v["options"]["overrideSaveAs"], json!(true), "the tick rides in the droplet"); + assert!(v["options"].get("output").is_none(), "a saveAndClose droplet stores no output folder"); + + let none_dp = droplet(&dir, json!({"path": format!("{dir}/NoneDest"), "destination": "none", "output": format!("{dir}/out")})); + let v: Value = serde_json::from_slice(&std::fs::read(&none_dp).unwrap()).unwrap(); + assert_eq!(v["options"]["destination"], json!("none")); + assert!(v["options"].get("output").is_none(), "a none droplet stores no output folder"); + + let plain = droplet(&dir, json!({})); + let v: Value = serde_json::from_slice(&std::fs::read(&plain).unwrap()).unwrap(); + assert!(v["options"].get("destination").is_none(), "an omitted destination stores no key, as before"); + assert!(v["options"].get("overrideSaveAs").is_none(), "an omitted box stores no key, as before"); + + let e = crate::session() + .execute( + "file.automate.createDroplet", + json!({"path": format!("{dir}/Bad"), "steps": [["image.adjustments.invert", {}]], "destination": "banana", "shim": false}), + ) + .unwrap_err(); + assert!(e.to_string().contains("destination"), "{e}"); +} + +/// #13711 Done-when: droplets written before this change (no destination stored, an `output` +/// folder) still run as Folder. The JSON is the old on-disk shape kept alive by main's own +/// pre-`calledActions` pin (`old.pcdroplet` inside +/// droplets_carry_the_actions_their_play_steps_call, crates/engine/src/cmd/automate_cmds/tests.rs, +/// as it stands at the base c337d6e4e). Guard test: passes before and after. +#[test] +fn a_droplet_without_a_destination_key_still_runs_as_folder() { + let (_tmp, dir) = tmp("legacy"); + std::fs::create_dir_all(format!("{dir}/in")).unwrap(); + let input = format!("{dir}/in/a.png"); + input_picture(&input); + let before = std::fs::read(&input).unwrap(); + let dp = format!("{dir}/old.pcdroplet"); + std::fs::write(&dp, json!({"photocraftDroplet": 1, "action": {"steps": [["image.imageRotation.90cw", {}]]}, "options": {"output": format!("{dir}/out-old"), "format": "png"}}).to_string()).unwrap(); + let r = crate::session().execute("file.automate.runDroplet", json!({"droplet": &dp, "input": [format!("{dir}/in")]})).unwrap(); + assert_eq!(r["errors"], json!([]), "{r}"); + assert_eq!(r["output"], json!(format!("{dir}/out-old")), "{r}"); + assert_eq!(std::fs::read(&input).unwrap(), before, "the input keeps its bytes: the copy went to the output folder"); + let out = photocraft_codecs::decode(&std::fs::read(format!("{dir}/out-old/a.png")).unwrap()).unwrap(); + assert_eq!(out.dimensions(), (2, 4), "the rotation ran and wrote into the stored output folder"); +} + +/// #13711: Destination Folder with the key stored behaves exactly like the key-less droplet: the +/// copy is written to the stored output folder, the input untouched. +#[test] +fn a_droplet_with_destination_folder_writes_its_copy_as_before() { + let (_tmp, dir) = tmp("folder"); + let input = format!("{dir}/a.png"); + input_picture(&input); + let before = std::fs::read(&input).unwrap(); + let dp = droplet(&dir, json!({"destination": "folder", "output": format!("{dir}/out"), "format": "png"})); + let r = crate::session().execute("file.automate.runDroplet", json!({"droplet": &dp, "input": [&input]})).unwrap(); + assert_eq!(r["errors"], json!([]), "{r}"); + assert_eq!(r["output"], json!(format!("{dir}/out")), "{r}"); + assert_eq!(std::fs::read(&input).unwrap(), before, "the input is not touched"); + let out = photocraft_codecs::decode(&std::fs::read(format!("{dir}/out/a.png")).unwrap()).unwrap(); + assert_eq!(out.dimensions(), (2, 4)); +} + +/// #13711: Destination Save and Close overwrites each input in its own format (as Batch's Save and +/// Close, #13659): each input file itself becomes the rotated picture, and no output folder appears. +#[test] +fn destination_save_and_close_overwrites_each_input_in_its_own_format() { + let (_tmp, dir) = tmp("save-close"); + let (a, b) = (format!("{dir}/a.png"), format!("{dir}/b.jpg")); + input_picture(&a); + input_picture(&b); + let dp = droplet(&dir, json!({"destination": "saveAndClose"})); + let r = crate::session().execute("file.automate.runDroplet", json!({"droplet": &dp, "input": [&a, &b]})).unwrap(); + assert_eq!(r["errors"], json!([]), "{r}"); + for (path, name) in [(&a, "a.png"), (&b, "b.jpg")] { + let out = photocraft_codecs::decode(&std::fs::read(path).unwrap()).unwrap(); + assert_eq!(out.dimensions(), (2, 4), "{name}: the input itself holds the rotated picture"); + } + assert!(!std::path::Path::new(&format!("{dir}/out")).exists(), "Save and Close takes no output folder"); +} + +/// #13711: a saveAndClose droplet whose stored format is not the source's own fails on Batch's own +/// Save and Close rule, before any file is written (the rule measured for #13659, passed on). +#[test] +fn destination_save_and_close_refuses_a_different_stored_format_without_writing() { + let (_tmp, dir) = tmp("save-close-format"); + let input = format!("{dir}/a.png"); + input_picture(&input); + let before = std::fs::read(&input).unwrap(); + let dp = droplet(&dir, json!({"destination": "saveAndClose", "format": "jpg"})); + let e = crate::session().execute("file.automate.runDroplet", json!({"droplet": &dp, "input": [&input]})).unwrap_err(); + assert!(e.to_string().contains("format"), "{e}"); + assert_eq!(std::fs::read(&input).unwrap(), before, "refused before writing anything"); +} + +/// #13711: Destination None, run in the app: each input opens as a document that stays open and +/// changed, its last History state is the action's step, and nothing is written anywhere. +#[test] +fn destination_none_keeps_each_opened_document_changed_and_writes_nothing() { + let (_tmp, dir) = tmp("none-app"); + let input = format!("{dir}/a.png"); + input_picture(&input); + let before = std::fs::read(&input).unwrap(); + let dp = droplet(&dir, json!({"destination": "none"})); + let mut s = app_session(); + let r = s.execute("file.automate.runDroplet", json!({"droplet": &dp, "input": [&input]})).unwrap(); + assert_eq!(r["errors"], json!([]), "{r}"); + assert_eq!(r["files"], json!([]), "nothing is written: {r}"); + assert_eq!(r["output"], Value::Null, "the run names no output folder: {r}"); + assert_eq!(std::fs::read(&input).unwrap(), before, "the input file keeps its bytes"); + assert_eq!(s.documents().len(), 1, "the document stays open"); + let st = s.active().unwrap(); + assert!(st.is_dirty(), "the tab shows the changed mark"); + assert_eq!(st.history.undo_label(), Some("Rotate Canvas"), "the last History state is the action's step"); + assert!( + !std::path::Path::new(&format!("{dir}/out")).exists() && !std::path::Path::new(&format!("{dir}/droplet-output")).exists(), + "None writes no folder at all" + ); +} + +/// #13711: None with recorded Save steps still writes nothing: with no destination they mark +/// nothing, so they are skipped and the document stays changed (as Batch with no destination). +#[test] +fn destination_none_skips_the_recorded_save_and_still_writes_nothing() { + let (_tmp, dir) = tmp("none-save"); + let input = format!("{dir}/a.png"); + input_picture(&input); + let before = std::fs::read(&input).unwrap(); + let listing = |dir: &str| -> Vec { + let mut files: Vec<_> = std::fs::read_dir(dir).unwrap().filter_map(|e| e.ok()).map(|e| e.path()).collect(); + files.sort(); + files + }; + let dp = droplet(&dir, json!({"destination": "none", "steps": [["image.imageRotation.90cw", {}], ["file.save", {}], ["image.adjustments.invert", {}]]})); + let files_before = listing(&dir); + let mut s = app_session(); + let r = s.execute("file.automate.runDroplet", json!({"droplet": &dp, "input": [&input]})).unwrap(); + assert_eq!(r["errors"], json!([]), "{r}"); + assert_eq!(r["files"], json!([])); + // The step recorded after the Save ran too: a run that merely stopped at the save point would + // end with the rotation as its last state. + assert_eq!(s.active().unwrap().history.undo_label(), Some("Invert"), "the steps after the recorded Save ran"); + assert_eq!(std::fs::read(&input).unwrap(), before, "the recorded Save wrote nothing over the input"); + // No new file of ANY kind appeared in the test folder (a written copy would be one). + let fresh: Vec<_> = listing(&dir).into_iter().filter(|p| !files_before.contains(p)).collect(); + assert!(fresh.is_empty(), "the run created no new file anywhere in the folder, got {fresh:?}"); + assert!(s.active().unwrap().is_dirty(), "the document stays changed"); +} + +/// #13711: a run that names an output override still writes nothing when the droplet's Destination +/// is None: the stored destination decides, the override cannot reintroduce a write. +#[test] +fn destination_none_ignores_a_call_output_override_and_writes_nothing() { + let (_tmp, dir) = tmp("none-override"); + let input = format!("{dir}/a.png"); + input_picture(&input); + let dp = droplet(&dir, json!({"destination": "none"})); + let mut s = app_session(); + let r = s.execute("file.automate.runDroplet", json!({"droplet": &dp, "input": [&input], "output": format!("{dir}/forced")})).unwrap(); + assert_eq!(r["files"], json!([]), "{r}"); + assert!(!std::path::Path::new(&format!("{dir}/forced")).exists(), "the override folder was not even created"); +} + +/// #13711: headless (the `photocraft-cli droplet` kind: a bare session, no preset store attached) +/// the run returns an error saying None needs the app, instead of writing anything. +#[test] +fn destination_none_is_refused_when_no_app_is_involved() { + let (_tmp, dir) = tmp("none-headless"); + let input = format!("{dir}/a.png"); + input_picture(&input); + let before = std::fs::read(&input).unwrap(); + let dp = droplet(&dir, json!({"destination": "none"})); + let e = crate::session().execute("file.automate.runDroplet", json!({"droplet": &dp, "input": [&input]})).unwrap_err(); + assert!(e.to_string().contains("app"), "the error names the app: {e}"); + assert_eq!(std::fs::read(&input).unwrap(), before, "nothing was written"); + let droplets_and_images = + std::fs::read_dir(&dir).unwrap().filter_map(|e| e.ok()).filter(|e| e.path().extension().is_some_and(|x| x == "pcdroplet" || x == "png")).count(); + assert_eq!(droplets_and_images, 2, "only the droplet and its one input sit in the folder"); +} + +/// #13711: a droplet file carrying an unknown destination is refused instead of guessed (fail +/// closed), and nothing is written. +#[test] +fn an_unknown_stored_destination_is_refused_without_writing() { + let (_tmp, dir) = tmp("bad-dest"); + let input = format!("{dir}/a.png"); + input_picture(&input); + let before = std::fs::read(&input).unwrap(); + let dp = format!("{dir}/bad.pcdroplet"); + std::fs::write(&dp, json!({"photocraftDroplet": 1, "action": {"steps": [["image.imageRotation.90cw", {}]]}, "options": {"destination": "banana", "output": format!("{dir}/out"), "format": "png"}}).to_string()).unwrap(); + let e = crate::session().execute("file.automate.runDroplet", json!({"droplet": &dp, "input": [&input]})).unwrap_err(); + assert!(e.to_string().contains("destination"), "{e}"); + assert_eq!(std::fs::read(&input).unwrap(), before, "refused before writing"); + assert!(!std::path::Path::new(&format!("{dir}/out")).exists()); +} + +/// #13711: an Override Action "Save As" Commands tick stored in the droplet does not change the +/// run yet (not measured for droplets; for Batch see #13659): the folder run writes its copy as +/// today, exactly like the same droplet without the tick. +#[test] +fn a_stored_override_tick_does_not_change_the_run() { + let (_tmp, dir) = tmp("override-run"); + let (ticked, plain) = (format!("{dir}/a.png"), format!("{dir}/b.png")); + input_picture(&ticked); + input_picture(&plain); + let with = droplet( + &dir, + json!({"path": format!("{dir}/With"), "destination": "folder", "output": format!("{dir}/out-with"), "format": "png", "overrideSaveAs": true}), + ); + let without = droplet(&dir, json!({"path": format!("{dir}/Without"), "destination": "folder", "output": format!("{dir}/out-without"), "format": "png"})); + let mut s = crate::session(); + let a = s.execute("file.automate.runDroplet", json!({"droplet": &with, "input": [&ticked]})).unwrap(); + assert_eq!(a["errors"], json!([]), "{a}"); + let b = s.execute("file.automate.runDroplet", json!({"droplet": &without, "input": [&plain]})).unwrap(); + assert_eq!(b["errors"], json!([]), "{b}"); + let x = photocraft_codecs::decode(&std::fs::read(format!("{dir}/out-with/a.png")).unwrap()).unwrap(); + let y = photocraft_codecs::decode(&std::fs::read(format!("{dir}/out-without/b.png")).unwrap()).unwrap(); + assert_eq!((x.dimensions(), y.dimensions()), ((2, 4), (2, 4)), "both write the folder copy as before"); +} + +/// #13711 review HIGH-1: the None run plays steps in the caller's session, so it must raise and +/// hand back `actions.batch_depth` like Batch raises it for its scratch run. A droplet whose steps +/// run the same droplet then stops at the nesting limit with the shared error text, and the stop +/// reaches the outermost answer marked like Batch marks a child's stop - instead of opening one +/// document per level until the stack or memory runs out. None twin of +/// `a_droplet_that_runs_itself_stops_at_the_nesting_limit` (file_cmds::tests, the folder path). +#[test] +fn a_none_droplet_that_runs_itself_stops_at_the_nesting_limit() { + let (_tmp, dir) = tmp("none-recursion"); + let input = format!("{dir}/a.png"); + input_picture(&input); + let before = std::fs::read(&input).unwrap(); + let dp = format!("{dir}/self.pcdroplet"); + let step = json!(["file.automate.runDroplet", {"droplet": dp, "input": [input]}]); + std::fs::write(&dp, json!({"photocraftDroplet": 1, "name": "self", "action": {"steps": [step]}, "options": {"destination": "none"}}).to_string()).unwrap(); + let mut s = app_session(); + let r = s.execute("file.automate.runDroplet", json!({"droplet": &dp, "input": [&input]})).unwrap(); + assert!(r["errors"][0]["error"].as_str().unwrap().contains("nested more than"), "{r}"); + assert_eq!(r["errors"][0]["limit"], json!(true), "the stop is marked, as Batch marks its children's stops"); + assert_eq!(r["files"], json!([]), "{r}"); + assert_eq!(s.recording.batch_depth, 0, "the None run hands the caller's nesting depth back"); + assert_eq!(std::fs::read(&input).unwrap(), before, "nothing was written at any level"); +} + +/// #13711 review HIGH-2: the None run plays from the actions the droplet captured (its +/// `calledActions`, #2794), installing them for its steps as the folder path forwards them to +/// Batch. The app's live list here holds an unrelated action of the same name - it must not play. +#[test] +fn a_none_droplet_plays_the_action_it_captured_not_the_live_list() { + use crate::actions_cmds::{Action, ActionStep}; + + let (_tmp, dir) = tmp("captured"); + let input = format!("{dir}/a.png"); + input_picture(&input); + let mut maker = crate::session(); + maker.actions_mut().list.push(Action::new("Inner", vec![ActionStep::new("image.imageRotation.90cw", json!({}))])).unwrap(); + maker.actions_mut().list.push(Action::new("Top", vec![ActionStep::new("actions.play", json!({"action": "Inner"}))])).unwrap(); + let r = maker.execute("file.automate.createDroplet", json!({"path": format!("{dir}/Top"), "action": "Top", "destination": "none", "shim": false})).unwrap(); + let dp = r["path"].as_str().unwrap().to_string(); + let v: Value = serde_json::from_slice(&std::fs::read(&dp).unwrap()).unwrap(); + assert!(v["calledActions"].as_array().is_some_and(|a| a.iter().any(|ac| ac["name"] == "Inner")), "the droplet captured Inner: {v}"); + + let mut s = app_session(); + s.actions_mut().list.push(Action::new("Inner", vec![ActionStep::new("edit.fill", json!({"color": "#000000"}))])).unwrap(); + let r = s.execute("file.automate.runDroplet", json!({"droplet": &dp, "input": [&input]})).unwrap(); + assert_eq!(r["errors"], json!([]), "{r}"); + let st = s.active().unwrap(); + assert_eq!((st.doc.size.width, st.doc.size.height), (2, 4), "the captured Inner rotated; the live same-named fill did not play"); +} + +/// #13711 review minor (automate_cmds.rs:573): a stored droplet whose steps do not parse is +/// refused BEFORE the run touches the caller's action state. The None run installs the droplet's +/// captured actions (`actions.called`) and raises `actions.batch_depth` for its own steps; a step +/// list that fails to parse must hand both back, or the next action the app plays resolves in the +/// droplet's private namespace and its first nested Batch starts one level too deep. +#[test] +fn a_none_droplet_with_unparsable_steps_leaves_the_caller_s_action_state_untouched() { + use crate::actions_cmds::{Action, ActionStep}; + + let (_tmp, dir) = tmp("none-unparsable-steps"); + let input = format!("{dir}/a.png"); + input_picture(&input); + let dp = format!("{dir}/Broken.pcdroplet"); + // `[[]]` is the shape `parse_steps` refuses: an array step with no id at all. The droplet also + // carries a captured action, which is what the run would install before parsing. + std::fs::write( + &dp, + json!({"photocraftDroplet": 1, "name": "Broken", "action": {"steps": [[]]}, "options": {"destination": "none"}, "calledActions": [{"name": "Captured", "steps": []}]}).to_string(), + ) + .unwrap(); + let mut s = app_session(); + s.actions_mut().list.push(Action::new("Live", vec![ActionStep::new("image.adjustments.invert", json!({}))])).unwrap(); + let e = s.execute("file.automate.runDroplet", json!({"droplet": &dp, "input": [&input]})).unwrap_err(); + assert!(e.to_string().contains("step"), "the parse error names the steps: {e}"); + assert_eq!(s.documents().len(), 0, "a refused run opens nothing"); + assert!(s.actions().called.is_none(), "the refused run installs no captured action list"); + assert_eq!(s.recording.batch_depth, 0, "and leaves the caller's nesting depth alone"); + // The live list is still the one playback finds, and a nested run starts at depth 1. + s.execute("file.new", json!({"width": 2, "height": 1, "background": "#ff0000"})).unwrap(); + let r = s.execute("actions.play", json!({"action": "Live"})).unwrap(); + assert!(r.get("failed").is_none(), "the caller's own action still plays: {r}"); + assert_eq!(s.actions().playable().actions().len(), 1, "playback still resolves in the caller's list"); + let st = s.active().unwrap(); + assert_eq!((st.doc.size.width, st.doc.size.height), (2, 1), "the caller's action ran on its own document"); +} + +/// #13711 review (automate_cmds.rs:593), the finding's own input. Guard test, passes before and +/// after: a captured action's recorded `file.save` step cannot write during a None run, because the +/// engine has no `file.save` to run - Save is the desktop shell's command +/// (crates/engine/src/shell_commands.rs:29, replayed by the shell's runner), and engine playback +/// refuses it. The input becomes an error entry, the source keeps its bytes, no file appears. +#[test] +fn a_none_droplet_refuses_a_nested_recorded_save_and_writes_no_file() { + use crate::actions_cmds::{Action, ActionStep}; + + let (_tmp, dir) = tmp("none-nested-shell-save"); + let input = format!("{dir}/a.png"); + input_picture(&input); + let before = std::fs::read(&input).unwrap(); + let files_in = |dir: &str| -> Vec { + let mut v: Vec<_> = std::fs::read_dir(dir).unwrap().filter_map(|e| e.ok()).map(|e| e.path()).collect(); + v.sort(); + v + }; + let mut maker = crate::session(); + maker + .actions_mut() + .list + .push(Action::new("Inner", vec![ActionStep::new("image.imageRotation.90cw", json!({})), ActionStep::new("file.save", json!({}))])) + .unwrap(); + maker.actions_mut().list.push(Action::new("Top", vec![ActionStep::new("actions.play", json!({"action": "Inner"}))])).unwrap(); + let dp = maker + .execute("file.automate.createDroplet", json!({"path": format!("{dir}/Top"), "action": "Top", "destination": "none", "shim": false})) + .unwrap()["path"] + .as_str() + .unwrap() + .to_string(); + let listing = files_in(&dir); + + let mut s = app_session(); + let r = s.execute("file.automate.runDroplet", json!({"droplet": &dp, "input": [&input]})).unwrap(); + let error = r["errors"][0]["error"].as_str().unwrap_or_default(); + assert!(error.contains("`file.save`") && error.contains("unknown command"), "the nested Save step is refused as the shell's command: {r}"); + assert_eq!(std::fs::read(&input).unwrap(), before, "the source keeps its bytes"); + assert_eq!(files_in(&dir), listing, "and no file appears anywhere in the folder"); +} + +/// Guard test, passes before and after: a nested engine-level save step keeps its own behaviour in +/// a None run, exactly as it does in Batch's runs today. `file.saveACopy` names its own path, so it +/// writes that copy in all three runs - the droplet None run, Batch's folder run, and Batch's +/// Opened-Files run with no output folder (the closest existing "writes nothing" neighbour). None +/// of the three overwrites the source. Suppressing the nested step instead would be a new contract +/// for the whole family, not a fix to one path (the same question sits open on #13659 / PR #15440). +#[test] +fn a_none_droplet_treats_a_nested_engine_save_exactly_as_batch_does() { + use crate::actions_cmds::{Action, ActionStep}; + + let inner = |dir: &str| { + Action::new( + "Inner", + vec![ActionStep::new("image.imageRotation.90cw", json!({})), ActionStep::new("file.saveACopy", json!({"path": format!("{dir}/copy.png")}))], + ) + }; + + // 1. The droplet's None run. + let (_tmp, dir) = tmp("nested-copy-droplet"); + let input = format!("{dir}/a.png"); + input_picture(&input); + let before = std::fs::read(&input).unwrap(); + let mut maker = crate::session(); + maker.actions_mut().list.push(inner(&dir)).unwrap(); + maker.actions_mut().list.push(Action::new("Top", vec![ActionStep::new("actions.play", json!({"action": "Inner"}))])).unwrap(); + let dp = maker + .execute("file.automate.createDroplet", json!({"path": format!("{dir}/Top"), "action": "Top", "destination": "none", "shim": false})) + .unwrap()["path"] + .as_str() + .unwrap() + .to_string(); + let mut s = app_session(); + let r = s.execute("file.automate.runDroplet", json!({"droplet": &dp, "input": [&input]})).unwrap(); + assert_eq!(r["errors"], json!([]), "{r}"); + let droplet_copy = std::path::Path::new(&format!("{dir}/copy.png")).exists(); + + // 2. Batch with an output folder, and 3. Batch over open documents with no output folder. + let (_tmp2, dir2) = tmp("nested-copy-batch"); + std::fs::create_dir_all(format!("{dir2}/in")).unwrap(); + let input2 = format!("{dir2}/in/a.png"); + input_picture(&input2); + let mut b = crate::session(); + b.actions_mut().list.push(inner(&dir2)).unwrap(); + let folder = b + .execute( + "file.automate.batch", + json!({"steps": [["actions.play", {"action": "Inner"}]], "input": [&input2], "output": format!("{dir2}/out"), "format": "png"}), + ) + .unwrap(); + assert_eq!(folder["errors"], json!([]), "{folder}"); + let batch_copy = std::path::Path::new(&format!("{dir2}/copy.png")).exists(); + + let (_tmp3, dir3) = tmp("nested-copy-batch-open"); + let input3 = format!("{dir3}/a.png"); + input_picture(&input3); + let before3 = std::fs::read(&input3).unwrap(); + let mut o = crate::session(); + crate::file_cmds::open_bytes_as(&mut o, "a.png", &std::fs::read(&input3).unwrap(), None, Some(input3.clone())).unwrap(); + o.actions_mut().list.push(inner(&dir3)).unwrap(); + let open = o.execute("file.automate.batch", json!({"source": "open", "steps": [["actions.play", {"action": "Inner"}]]})).unwrap(); + assert_eq!(open["errors"], json!([]), "{open}"); + assert_eq!(open["files"], json!([]), "the run itself writes nothing: {open}"); + let open_copy = std::path::Path::new(&format!("{dir3}/copy.png")).exists(); + + assert_eq!( + (droplet_copy, batch_copy, open_copy), + (true, true, true), + "a nested engine-level save writes its own copy in all three runs, so the None droplet adds no new write rule" + ); + assert_eq!(std::fs::read(&input).unwrap(), before, "the droplet None run leaves the source alone"); + assert_eq!(std::fs::read(&input3).unwrap(), before3, "and so does the no-output Batch run"); +} + +/// #13711 review MEDIUM-3: the None run opens each input through the shared open core (as File > +/// Open and Batch's scratch run do), not a bare import: a developed raw takes Preferences > +/// Raw Defaults > Resolution. (Chosen cheap honest neighbour: the raw ppi; the profiled-JPEG +/// colour policy rides in the same call - `open_bytes_as` runs the Color Settings policy through +/// `Session::open_document` - but its pin would need an ICC fixture, so it is not re-pinned here.) +#[test] +fn a_none_droplet_opens_a_raw_at_the_raw_defaults_resolution() { + use photocraft_raw::testgen::{DngSpec, mosaic, scene}; + + let (_tmp, dir) = tmp("raw-ppi"); + let input = format!("{dir}/IMG.dng"); + std::fs::write(&input, DngSpec::cfa(24, 16, mosaic(&scene(24, 16), 24, [0, 1, 1, 2], 0, 65535)).build()).unwrap(); + let dp = droplet(&dir, json!({"path": format!("{dir}/Invert"), "destination": "none", "steps": [["image.adjustments.invert", {}]]})); + let mut s = app_session(); + s.execute("prefs.set", json!({"path": "rawDefaults.resolution", "value": 240})).unwrap(); + let r = s.execute("file.automate.runDroplet", json!({"droplet": &dp, "input": [&input]})).unwrap(); + assert_eq!(r["errors"], json!([]), "{r}"); + assert_eq!(r["files"], json!([])); + let st = s.active().unwrap(); + assert_eq!(st.doc.resolution_dpi, 240.0, "the raw opened at the Raw Defaults ppi, as File > Open does"); + assert!(st.is_dirty(), "the opened raw stays open and changed"); +} + +/// #13711 review MEDIUM-4: a none droplet gets no Unix `.command` shim, even one asked for: the +/// shim runs `photocraft-cli droplet`, and a headless run of a none droplet is always refused, so +/// the launcher could only ever fail. A folder droplet still gets the shim as today. +#[test] +fn a_none_droplet_gets_no_shim_even_when_one_is_asked_for() { + let (_tmp, dir) = tmp("shim-none"); + let r = crate::session() + .execute( + "file.automate.createDroplet", + json!({"path": format!("{dir}/NoShim"), "steps": [["image.adjustments.invert", {}]], "destination": "none", "shim": true}), + ) + .unwrap(); + assert_eq!(r["shim"], Value::Null, "no launcher for a run that can only be refused: {r}"); + assert!(!std::path::Path::new(&format!("{dir}/NoShim.command")).exists()); + + let r = crate::session() + .execute( + "file.automate.createDroplet", + json!({"path": format!("{dir}/WithShim"), "steps": [["image.adjustments.invert", {}]], "destination": "folder", "output": format!("{dir}/out"), "shim": true}), + ) + .unwrap(); + assert!(r["shim"].is_string(), "a folder droplet still gets the shim: {r}"); + assert!(std::path::Path::new(r["shim"].as_str().unwrap()).exists(), "the shim file is written"); +} diff --git a/crates/ui-egui/src/dialogs/file_forms.rs b/crates/ui-egui/src/dialogs/file_forms.rs index 241d109b0a..ccc717c02f 100644 --- a/crates/ui-egui/src/dialogs/file_forms.rs +++ b/crates/ui-egui/src/dialogs/file_forms.rs @@ -121,7 +121,28 @@ pub(crate) fn open_create_droplet(app: &mut PhotocraftApp) -> Result(id).unwrap(); assert_eq!(hosted.fields().text("set"), Some("B")); @@ -64,6 +66,8 @@ fn create_droplet_uses_the_action_selected_in_the_form() { assert!(app.framework.dialogs.set_json_field(&ctx, id, "action", json!("Missing")).is_err(), "the action choice offers the listed actions"); set(&mut app, &ctx, id, "action", json!("Rotate")); set(&mut app, &ctx, id, "path", json!(path)); + // #13711: the form opens on Destination None; this flow runs a droplet, so it picks Folder. + set(&mut app, &ctx, id, "destination", json!("folder")); set(&mut app, &ctx, id, "output", json!(output)); crate::dialogs::confirm(&mut app, &ctx, id).unwrap(); let saved: Value = serde_json::from_slice(&std::fs::read(&path).unwrap()).unwrap(); @@ -79,3 +83,95 @@ fn create_droplet_uses_the_action_selected_in_the_form() { assert!(app.framework.dialogs.cancel_by_id(&ctx, id)); assert!(!cancelled.exists()); } + +/// An app with one saved action, selected in the Actions panel: what Create Droplet asks for. +fn droplet_app() -> Box { + let mut app = app(); + app.session.actions_mut().list.push(Action::new("Invert", vec![ActionStep::new("image.adjustments.invert", json!({}))])).unwrap(); + app.ui.actions.panel.selected = Some(ActionSelection::Action(0)); + app +} + +/// #13711: Create Droplet carries the reference application's Destination dropdown: the values +/// `none`, `saveAndClose` and `folder` are offered (and nothing else), the form opens on None, and +/// the Override Action "Save As" Commands box starts off. +#[test] +fn create_droplet_opens_on_destination_none_offering_the_three_reference_values() { + let mut app = droplet_app(); + let ctx = app.ctx().clone(); + let id = open_create_droplet(&mut app).unwrap(); + let hosted = app.framework.dialogs.get_by_id::(id).unwrap(); + assert_eq!(hosted.fields().text("destination"), Some("none"), "the dropdown opens on None"); + assert_eq!(hosted.fields().bool("overrideSaveAs"), Some(false), "the Override box starts off"); + set(&mut app, &ctx, id, "destination", json!("folder")); + set(&mut app, &ctx, id, "destination", json!("saveAndClose")); + assert!(app.framework.dialogs.set_json_field(&ctx, id, "destination", json!("banana")).is_err(), "only the three reference values are offered"); + assert_eq!(app.framework.dialogs.get_by_id::(id).unwrap().fields().text("destination"), Some("saveAndClose"), "the choice took"); +} + +/// #13711: OK stores the Destination choice in the droplet (all three), and a ticked Override +/// Action "Save As" Commands rides with it; a call that leaves the box off stores no tick. +#[test] +fn create_droplet_ok_stores_the_destination_in_the_droplet() { + let mut app = droplet_app(); + let ctx = app.ctx().clone(); + let dir = tempfile::tempdir().unwrap(); + let saved_at = |app: &mut PhotocraftApp, name: &str, setup: &dyn Fn(&mut PhotocraftApp, &egui::Context, DialogId)| -> Value { + let path = dir.path().join(name).to_string_lossy().into_owned(); + let id = open_create_droplet(app).unwrap(); + set(app, &ctx, id, "path", json!(path)); + setup(app, &ctx, id); + crate::dialogs::confirm(app, &ctx, id).unwrap(); + serde_json::from_slice(&std::fs::read(&path).unwrap()).unwrap() + }; + let v = saved_at(&mut app, "default.pcdroplet", &|_, _, _| {}); + assert_eq!(v["options"]["destination"], json!("none"), "the form's default None is stored"); + assert!(v["options"].get("overrideSaveAs").is_none(), "an untouched box stores no tick"); + assert!(v["options"].get("output").is_none(), "a none droplet stores no output folder (#13711 review)"); + let v = saved_at(&mut app, "close.pcdroplet", &|app, ctx, id| set(app, ctx, id, "destination", json!("saveAndClose"))); + assert_eq!(v["options"]["destination"], json!("saveAndClose")); + assert!(v["options"].get("output").is_none(), "a saveAndClose droplet stores no output folder either"); + let v = saved_at(&mut app, "folder.pcdroplet", &|app, ctx, id| { + set(app, ctx, id, "destination", json!("folder")); + set(app, ctx, id, "overrideSaveAs", json!(true)); + }); + assert_eq!(v["options"]["destination"], json!("folder")); + assert!(v["options"]["output"].is_string(), "the folder run keeps its output path in the droplet"); + assert_eq!(v["options"]["overrideSaveAs"], json!(true), "the tick rides in the droplet"); +} + +/// #13711: with Destination None (and, as the unmeasured middle choice, Save and Close) the Output +/// row's Choose... and the Override Action "Save As" Commands box are greyed; with Folder both go +/// live again. +#[test] +fn create_droplet_greys_choose_and_override_until_folder_is_picked() { + use egui_kittest::Harness; + use egui_kittest::kittest::{NodeT, Queryable}; + + let app = droplet_app(); + let mut h = Harness::builder() + .with_size(egui::vec2(1000.0, 800.0)) + .with_max_steps(64) + .with_context(app.ctx().clone()) + .build_ui_state(|ui, app: &mut Box| crate::dialogs::run(app, ui), app); + PhotocraftApp::setup_context(&h.ctx, Default::default()); + open_create_droplet(h.state_mut()).unwrap(); + h.run_steps(4); + let greyed = + |h: &Harness<'_, Box>, label: &str| h.get_by_role_and_label(egui::accesskit::Role::CheckBox, label).accesskit_node().is_disabled(); + let browses = |h: &Harness<'_, Box>| h.query_all_by_label("Browse…").map(|node| node.accesskit_node().is_disabled()).collect::>(); + assert!(greyed(&h, "Override Action \"Save As\" Commands"), "the box is greyed with Destination None"); + assert_eq!(browses(&h), vec![true], "the Output row's Choose... is greyed with Destination None"); + let pick = |h: &mut Harness<'_, Box>, caption: &str| { + h.get_by_role_and_label(egui::accesskit::Role::ComboBox, "Destination").click(); + h.run_steps(2); + h.get_by_label(caption).click(); + h.run_steps(2); + }; + pick(&mut h, "Save and Close"); + assert!(greyed(&h, "Override Action \"Save As\" Commands"), "Save and Close keeps both off too (not measured to enable them)"); + assert_eq!(browses(&h), vec![true], "and its Choose... greyed"); + pick(&mut h, "Folder"); + assert!(!greyed(&h, "Override Action \"Save As\" Commands"), "the box is live with Folder"); + assert_eq!(browses(&h), vec![false], "and Choose... opens the folder picker with Folder"); +}