Address CodeRabbit review: doc.replaceSmartObject restores the active document (#8150)

`index` targets one call (#7025): replace_smart_object now runs through
on_doc like place and the data-set methods, so the previously active
document is active again on success, on error and on the trusted-local path.
The index test covers a successful and a failing indexed replace.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
David MannandClaude Opus 5.5 committed 2026-10-23 00:24:13 +02:00
1 parent 1efa06b715
commit 4604cfc4bb
2 files changed
+19 -18

No files matched your search

+15 -17
View File
@@ -156,24 +156,22 @@ impl Headless {
/// `layer.smartObjects.replaceContents`, which takes an ambient `path` and is refused in
/// automation (#8061). `params` is the command's own (`layer`).
pub fn replace_smart_object(&mut self, index: Option<usize>, path: &str, params: &Value) -> Result<Value, AutomationError> {
let i = self.doc_index(index)?;
if self.session.active_index() != Some(i) && !self.session.set_active(i) {
return Err(AutomationError::BadRequest(format!("no document at index {i}")));
}
let mut opts = params.as_object().cloned().unwrap_or_default();
opts.insert("path".into(), json!(path));
if matches!(&self.filesystem, Filesystem::TrustedLocal) {
return self.command_run("layer.smartObjects.replaceContents", Value::Object(opts));
}
let bytes = match &self.filesystem {
Filesystem::Workspace(workspace) => workspace.read(path)?,
_ => {
return Err(AutomationError::BadRequest("automation filesystem access is not granted: read authority is absent".into()));
self.on_doc(index, |this| {
let mut opts = params.as_object().cloned().unwrap_or_default();
opts.insert("path".into(), json!(path));
if matches!(&this.filesystem, Filesystem::TrustedLocal) {
return this.command_run("layer.smartObjects.replaceContents", Value::Object(opts));
}
};
let name = Path::new(path).file_name().and_then(|name| name.to_str()).unwrap_or(path);
self.sync_jobs();
Ok(photocraft_engine::smart_cmds::replace_contents_from_bytes(&mut self.session, &Value::Object(opts), name, bytes)?)
let bytes = match &this.filesystem {
Filesystem::Workspace(workspace) => workspace.read(path)?,
_ => {
return Err(AutomationError::BadRequest("automation filesystem access is not granted: read authority is absent".into()));
}
};
let name = Path::new(path).file_name().and_then(|name| name.to_str()).unwrap_or(path);
this.sync_jobs();
Ok(photocraft_engine::smart_cmds::replace_contents_from_bytes(&mut this.session, &Value::Object(opts), name, bytes)?)
})
}
/// Export each exported layer of a document as its own file beneath the write root: the
+4 -1
View File
@@ -880,7 +880,7 @@ mod tests {
}
#[test]
fn indexed_place_and_data_set_methods_keep_the_active_document() {
fn indexed_place_replace_and_data_set_methods_keep_the_active_document() {
let base = std::env::temp_dir().join(format!("pc-doc-index-active-{}", std::process::id()));
let root = base.join("workspace");
std::fs::create_dir_all(&root).unwrap();
@@ -897,6 +897,9 @@ mod tests {
let steps = [
("doc.place", json!({"path": "logo.png", "index": 1}), true),
// Document 1's active layer is now the placed Smart Object.
("doc.replaceSmartObject", json!({"path": "logo.png", "index": 1}), true),
("doc.replaceSmartObject", json!({"path": "missing.png", "index": 1}), false),
("doc.importDataSets", json!({"path": "sets.csv", "index": 1}), true),
("doc.exportLayers", json!({"dir": "layers", "index": 1}), true),
("doc.exportDataSets", json!({"dir": "sets", "index": 1}), true),