Keep nested Batch outcomes in failed action results

This commit is contained in:
yoriswind committed 2026-10-31 23:45:42 -07:00
1 parent 822e72d856
commit fe1d1c5d70
4 files changed
+88 -3

No files matched your search

+11 -2
View File
@@ -448,7 +448,10 @@ fn run_recorded(s: &mut Session, action: &Action, from: usize) -> (u64, Option<V
match playback_params(s, id, params).and_then(|p| if saves_smart_contents(s, id) { s.execute(SAVE_CONTENTS, json!({})) } else { s.execute(id, p) }) {
Ok(value) => {
if let Some(error) = nested_failure(id, &value) {
failed = Some(json!({"step": i, "id": id, "error": error}));
// Like Scripts, retain the child result so partial Batch outputs remain visible.
let mut failure = json!({"step": i, "id": id, "error": error});
failure["result"] = value;
failed = Some(failure);
break;
}
ran += 1;
@@ -495,9 +498,15 @@ pub fn restore_recording(s: &mut Session, idx: usize, action: &str, from: usize)
/// Nested playback and Batch return structured failures even though the command succeeded.
/// Promote it to the caller's failed step so no later parent steps run after a child fails.
/// Batch text summarizes counts and the first failure; its full result retains every file outcome.
pub fn nested_failure(id: &str, value: &Value) -> Option<String> {
if id == "file.automate.batch" {
return value.get("errors")?.as_array()?.first()?.get("error")?.as_str().map(str::to_string);
let errors = value.get("errors")?.as_array()?;
let first = errors.first()?;
let error = first.get("error")?.as_str()?;
let file = first.get("file").and_then(Value::as_str).unwrap_or("unknown input");
let saved = value.get("files").and_then(Value::as_array).map_or(0, Vec::len);
return Some(format!("Batch: {} file(s) failed, {saved} saved; first failure `{file}`: {error}", errors.len()));
}
(id == "actions.play" || id == "actions.conditional").then(|| value.get("failed")).flatten().map(|failure| {
format!(
+38
View File
@@ -1401,6 +1401,7 @@ fn named_batch_allows_nested_work_and_preserves_authorization() {
}
}
/// Parent failures retain per-file outcomes and stop, while a successful Batch continues.
#[test]
fn nested_batch_errors_stop_actions_and_scripts_without_changing_top_level_results() {
use crate::actions_cmds::Action;
@@ -1424,6 +1425,43 @@ fn nested_batch_errors_stop_actions_and_scripts_without_changing_top_level_resul
assert!(!std::path::Path::new(&output).exists());
assert_eq!(crate::actions_cmds::nested_failure("file.automate.batch", &json!({"files": ["ok.png"], "errors": []})), None);
assert_eq!(crate::actions_cmds::nested_failure("file.automate.batch", &json!({"errors": "invalid"})), None);
let corrupt = join(&dir, "corrupt.png");
std::fs::write(&corrupt, b"not a PNG").unwrap();
for caller in ["batch", "action", "script"] {
let output = join(&dir, caller);
let batch = json!({"input": [input, corrupt], "output": output, "steps": [], "format": "png"});
let steps = vec![("file.automate.batch".into(), batch.clone()), ("file.new".into(), json!({"width": 2, "height": 2}))];
let result = match caller {
"batch" => s.execute("file.automate.batch", batch).unwrap(),
"action" => {
s.actions.list.push(Action::new("Mixed", steps));
let result = s.execute("actions.play", json!({"action": "Mixed"})).unwrap();
assert_eq!(result["ran"], 0, "{result}");
let error = result["failed"]["error"].as_str().unwrap();
assert!(error.contains("1 file(s) failed") && error.contains("1 saved") && error.contains(&corrupt), "{error}");
result["failed"]["result"].clone()
}
_ => {
let result = s.execute("file.scripts.browse", json!({"steps": steps})).unwrap();
assert_eq!(result["ok"], false, "{result}");
assert_eq!(result["results"].as_array().unwrap().len(), 1);
result["results"][0]["result"].clone()
}
};
let saved = join(&output, "red.png");
assert_eq!(result["files"], json!([saved]), "{caller}: {result}");
assert_eq!(result["errors"].as_array().unwrap().len(), 1);
assert_eq!(result["errors"][0]["file"], corrupt);
assert!(std::path::Path::new(&saved).exists(), "{caller}: partial success remains saved");
assert!(s.documents().is_empty(), "{caller}: neither parent reaches file.new");
}
let batch = json!({"input": [input], "output": join(&dir, "success"), "steps": [], "format": "png"});
s.actions.list.push(Action::new("Success", vec![("file.automate.batch".into(), batch), ("file.new".into(), json!({"width": 2, "height": 2}))]));
let result = s.execute("actions.play", json!({"action": "Success"})).unwrap();
assert!(result.get("failed").is_none(), "{result}");
assert_eq!(result["ran"], 2);
assert_eq!(s.documents().len(), 1, "a successful Batch reaches file.new");
}
#[test]
+4 -1
View File
@@ -183,7 +183,10 @@ fn play_owned(app: &mut PhotocraftApp, params: &Value, suspended: Option<Value>)
match result {
Ok(value) => {
if let Some(error) = actions_cmds::nested_failure(id, &value).or_else(|| actions_cmds::save_target_closed(&app.session, id, target)) {
failed = Some(json!({"step": i, "id": id, "error": error}));
// Keep the same child-result detail as headless Actions and Scripts.
let mut failure = json!({"step": i, "id": id, "error": error});
failure["result"] = value;
failed = Some(failure);
break;
}
if let Some(inner) = value.get("paused").filter(|p| p.is_object()) {
@@ -303,6 +303,7 @@ fn drag_steps_and_action_headers_reorders_and_keeps_selection() {
assert_eq!(h.state().ui.actions.selected, Some(0));
}
/// Shell playback retains nested failures and Batch file outcomes without running later steps.
#[test]
fn nested_shell_actions_include_view_steps_and_propagate_failures() {
let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default());
@@ -328,6 +329,40 @@ fn nested_shell_actions_include_view_steps_and_propagate_failures() {
assert!(r["failed"]["error"].as_str().unwrap().contains("denied nested view"));
assert!(app.session.actions.playback_stack.is_empty());
assert_eq!(app.session.actions.playing, 0);
app.session.authorize = None;
let dir = tempfile::tempdir().unwrap();
let input = dir.path().join("valid.png");
let corrupt = dir.path().join("corrupt.png");
let bytes = photocraft_io::export(&app.session.active().unwrap().doc, "png", &Default::default()).unwrap().bytes;
std::fs::write(&input, bytes).unwrap();
std::fs::write(&corrupt, b"not a PNG").unwrap();
let before = app.session.documents().len();
for mixed in [true, false] {
let output = dir.path().join(if mixed { "mixed" } else { "success" });
let inputs = if mixed { vec![&input, &corrupt] } else { vec![&input] };
let batch = json!({"input": inputs, "output": output, "steps": [], "format": "png"});
let index = app.session.actions.list.len();
let name = if mixed { "Mixed Batch" } else { "Successful Batch" };
app.session.actions.list.push(Action::new(name, vec![("file.automate.batch".into(), batch), ("file.new".into(), json!({"width": 2, "height": 2}))]));
let result = app.run("actions.play", json!({"action": index})).unwrap();
if mixed {
assert_eq!(result["ran"], 0, "{result}");
let batch = &result["failed"]["result"];
assert_eq!(batch["files"], json!([output.join("valid.png")]));
assert_eq!(batch["errors"].as_array().unwrap().len(), 1);
assert_eq!(batch["errors"][0]["file"], json!(corrupt));
report_play(&mut app, &result);
assert!(app.ui.status_error);
assert!(app.ui.status.contains("1 file(s) failed") && app.ui.status.contains("1 saved") && app.ui.status.contains("corrupt.png"));
assert_eq!(app.session.documents().len(), before, "a failed Batch stops before file.new");
} else {
assert!(result.get("failed").is_none(), "{result}");
assert_eq!(result["ran"], 2);
assert_eq!(app.session.documents().len(), before + 1, "a successful Batch continues");
}
assert!(output.join("valid.png").exists(), "successful files remain available");
}
}
#[test]