From 1656961cc577bc1d2136d4f4278682bf46344f87 Mon Sep 17 00:00:00 2001 From: shmuelbiller1 Date: Thu, 8 Oct 2026 18:51:28 -0400 Subject: [PATCH] Fix/1350 preserve active on save (#1416) * fix(automation): preserve active document after indexed native save (#1350) * test(automation): cover indexed save preserving active document (#1350) * rustfmt inactive_save.rs Co-Authored-By: Claude Opus 5.5 --------- Co-authored-by: Brandon Thomas Co-authored-by: Claude Opus 5.5 --- crates/automation/src/headless.rs | 5 ++++ crates/automation/tests/inactive_save.rs | 36 ++++++++++++++++++++++++ 2 files changed, 41 insertions(+) create mode 100644 crates/automation/tests/inactive_save.rs diff --git a/crates/automation/src/headless.rs b/crates/automation/src/headless.rs index e3bd8c74b8d..684cb316bd2 100644 --- a/crates/automation/src/headless.rs +++ b/crates/automation/src/headless.rs @@ -146,11 +146,16 @@ impl Headless { .map(|f| f.trim_start_matches('.').eq_ignore_ascii_case("pcraft")) .unwrap_or_else(|| target.extension().is_some_and(|e| e.eq_ignore_ascii_case("pcraft"))); if is_native { + // Saving by index must not retarget the next automation command. + let previously_active = self.session.active_index(); self.session.set_active(i); if let Some(st) = self.session.active_mut() { st.saved_revision = st.revision; st.path = Some(target.to_string_lossy().into_owned()); } + if let Some(active) = previously_active { + self.session.set_active(active); + } } Ok(json!({ "path": target.to_string_lossy(), "warnings": warnings })) } diff --git a/crates/automation/tests/inactive_save.rs b/crates/automation/tests/inactive_save.rs new file mode 100644 index 00000000000..7f7aaefc822 --- /dev/null +++ b/crates/automation/tests/inactive_save.rs @@ -0,0 +1,36 @@ +//! Regression for #1350: saving an inactive .pcraft file must not select it. + +use photocraft_automation::Headless; +use serde_json::json; + +#[test] +fn indexed_native_save_preserves_active_document_and_future_command_target() { + let dir = std::env::temp_dir().join(format!( + "pc-inactive-save-{}-{}", + std::process::id(), + std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap().as_nanos() + )); + std::fs::create_dir_all(&dir).unwrap(); + let path = dir.join("first.pcraft"); + let mut headless = Headless::trusted_local(); + + headless.handle("doc.new", json!({"width": 2, "height": 2, "background": "white", "name": "first"})).unwrap(); + headless.handle("doc.new", json!({"width": 2, "height": 2, "background": "white", "name": "second"})).unwrap(); + assert_eq!(headless.session.active_index(), Some(1)); + let first_revision = headless.session.documents()[0].revision; + let second_revision = headless.session.documents()[1].revision; + + headless.handle("doc.save", json!({"index": 0, "path": path.to_str().unwrap()})).unwrap(); + assert!(path.is_file(), "inactive document was saved"); + assert_eq!(headless.session.active_index(), Some(1), "save must not select document 0"); + let first = &headless.session.documents()[0]; + assert_eq!(first.path.as_deref(), path.to_str()); + assert_eq!(first.revision, first.saved_revision, "saved document is clean"); + + headless.handle("engine.execute", json!({"command": "image.adjustments.invert", "params": {}})).unwrap(); + assert_eq!(headless.session.active_index(), Some(1)); + assert_eq!(headless.session.documents()[0].revision, first_revision); + assert!(headless.session.documents()[1].revision > second_revision); + drop(headless); + std::fs::remove_dir_all(dir).unwrap(); +}