Merge pull request #6509 from yoriswind/codex/6488-validate-transform-numeric-steps

Reject nonnumeric MCP transform steps before editing
This commit is contained in:
BFlat
2026-11-18 13:00:13 +07:00
committed by GitHub
3 changed files with 129 additions and 1 deletions
+53
View File
@@ -17,6 +17,7 @@ mod limits;
mod links;
mod liquify;
mod livepaint;
mod move_offsets;
mod pen;
mod persp;
mod place;
@@ -2210,3 +2211,55 @@ fn add_text_reports_overflow_and_takes_a_fit() {
let point = made(&call(&mut s, 3, "add_text", json!({"text": "NEON DRIVE", "x": 0, "y": 300, "size": 60})));
assert!(point.get("overflow").is_none(), "{point}");
}
#[test]
fn transform_rejects_nonnumeric_steps_before_editing() {
let inspect = |s: &mut Server, id| serde_json::from_str::<Value>(&text_of(&call(s, id, "doc_inspect", json!({})))).unwrap();
let mut s = server();
let first = id_of(&call(&mut s, 1, "draw_path", json!({"d": "M10 10 L30 10 L30 25 Z"})));
let second = id_of(&call(&mut s, 2, "draw_path", json!({"d": "M40 40 L60 40 L60 55 Z"})));
let before = inspect(&mut s, 3);
assert_eq!(before["selection"], json!([second]));
let cases = [
("rotate", json!({"dx": 10, "rotate": "bad"})),
("scale", json!({"dx": 10, "scale": "bad"})),
("scaleX", json!({"dx": 10, "scaleX": "bad", "scaleY": 150})),
("scaleY", json!({"dx": 10, "scaleX": 150, "scaleY": "bad"})),
("shear", json!({"dx": 10, "shear": "bad"})),
];
for (index, (field, mut args)) in cases.into_iter().enumerate() {
args["ids"] = json!([first]);
let request_id = 4 + index as u64 * 2;
let rejected = call(&mut s, request_id, "transform", args);
assert_eq!(rejected["isError"], true, "{field}: {rejected}");
assert!(text_of(&rejected).contains(field), "{field}: {rejected}");
assert_eq!(inspect(&mut s, request_id + 1), before, "{field}");
}
let applied = call(&mut s, 14, "transform", json!({"ids": [first], "dx": 10, "rotate": null, "scale": 150, "scaleX": 125}));
assert_eq!(applied["isError"], false, "{applied}");
let applied: Value = serde_json::from_str(&text_of(&applied)).unwrap();
assert_eq!(applied["applied"], json!(["move", "scale"]));
let after = inspect(&mut s, 15);
assert_ne!(after, before);
assert_eq!(after["objects"], before["objects"]);
assert_eq!(after["selection"], json!([first]));
let bounds = &after["selectionBounds"];
assert!((bounds["width"].as_f64().unwrap() - 25.0).abs() < 1e-6, "{bounds}");
assert!((bounds["height"].as_f64().unwrap() - 22.5).abs() < 1e-6, "{bounds}");
assert!(after["history"].as_array().unwrap().contains(&json!("Move")));
assert!(after["history"].as_array().unwrap().contains(&json!("Scale")));
let applied = call(&mut s, 16, "transform", json!({"ids": [second], "dx": 10, "rotate": 90, "origin": [0, 0]}));
assert_eq!(applied["isError"], false, "{applied}");
let applied: Value = serde_json::from_str(&text_of(&applied)).unwrap();
assert_eq!(applied["applied"], json!(["move", "rotate"]));
let after = inspect(&mut s, 17);
assert_eq!(after["selection"], json!([second]));
let bounds = &after["selectionBounds"];
for (field, expected) in [("x", 40.0), ("y", -70.0), ("width", 15.0), ("height", 20.0)] {
assert!((bounds[field].as_f64().unwrap() - expected).abs() < 1e-6, "{field}: {bounds}");
}
assert!(after["history"].as_array().unwrap().ends_with(&[json!("Move"), json!("Rotate")]));
}
+67
View File
@@ -0,0 +1,67 @@
use serde_json::{Value, json};
use super::{Server, call, id_of, server, text_of};
fn inspect(s: &mut Server, id: u64) -> Value {
serde_json::from_str(&text_of(&call(s, id, "doc_inspect", json!({})))).unwrap()
}
fn path(s: &mut Server, id: u64, d: &str) -> u64 {
id_of(&call(s, id, "draw_path", json!({"d": d})))
}
fn child(doc: &Value, id: u64) -> Value {
doc["layers"][0]["children"].as_array().unwrap().iter().find(|node| node["id"] == id).unwrap().clone()
}
#[test]
fn transform_rejects_malformed_move_offsets_before_selection_or_copy() {
let mut s = server();
let first = path(&mut s, 1, "M40 40 L60 40 L60 60 L40 60 Z");
let second = path(&mut s, 2, "M100 100 L130 100 L130 140 L100 140 Z");
let before = inspect(&mut s, 3);
assert_eq!(before["selection"], json!([second]));
let cases = [("dx", json!({"ids": [first], "dx": "bad", "copy": true})), ("dy", json!({"ids": [first], "dx": 10, "dy": "bad", "copy": true}))];
for (index, (field, args)) in cases.into_iter().enumerate() {
let request_id = 4 + index as u64 * 2;
let result = call(&mut s, request_id, "transform", args);
assert_eq!(result["isError"], true, "{field}: {result}");
assert!(text_of(&result).contains(field), "{field}: {result}");
assert_eq!(inspect(&mut s, request_id + 1), before, "{field}");
}
// The recorded request omits dx and must still move a copy down by 10 points.
let result = call(&mut s, 8, "transform", json!({"ids": [first], "dy": 10, "copy": true}));
assert_eq!(result["isError"], false, "{result}");
let reply: Value = serde_json::from_str(&text_of(&result)).unwrap();
assert_eq!(reply["applied"], json!(["move"]));
let after = inspect(&mut s, 9);
let copied = after["selection"][0].as_u64().unwrap();
assert_ne!(copied, first);
assert_ne!(copied, second);
let original_before = child(&before, first);
let original_after = child(&after, first);
let second_after = child(&after, second);
let copy_after = child(&after, copied);
assert_eq!(original_after["bounds"], original_before["bounds"]);
assert_eq!(second_after["bounds"], child(&before, second)["bounds"]);
assert_eq!(copy_after["bounds"]["x"], original_before["bounds"]["x"]);
assert_eq!(copy_after["bounds"]["y"], original_before["bounds"]["y"].as_f64().unwrap() + 10.0);
assert_eq!(copy_after["bounds"]["width"], original_before["bounds"]["width"]);
assert_eq!(copy_after["bounds"]["height"], original_before["bounds"]["height"]);
let before_history = before["history"].as_array().unwrap();
let after_history = after["history"].as_array().unwrap();
assert_eq!(after_history.len(), before_history.len() + 1);
assert_eq!(after_history.last(), Some(&json!("Move")));
// Null keeps the engine's optional-number default while a negative numeric offset is applied.
let result = call(&mut s, 10, "transform", json!({"ids": [second], "dx": null, "dy": -7}));
assert_eq!(result["isError"], false, "{result}");
let after_null = inspect(&mut s, 11);
let moved = child(&after_null, second);
let second_before = child(&before, second);
assert_eq!(moved["bounds"]["x"], second_before["bounds"]["x"]);
assert_eq!(moved["bounds"]["y"], second_before["bounds"]["y"].as_f64().unwrap() - 7.0);
assert_eq!(after_null["history"].as_array().unwrap().last(), Some(&json!("Move")));
}
+9 -1
View File
@@ -275,7 +275,15 @@ fn dispatch(b: &mut dyn Backend, name: &str, a: &Args) -> Result<ToolResult, Str
select_ids(b, a)?;
j(exec(b, &format!("object.pathfinder.{}", req_str(a, "operation")?), json!({}))?)
}
"transform" => j(transform(b, a)?),
"transform" => {
// Validate all numeric steps before the handler changes selection or runs a step.
for field in ["dx", "dy", "rotate", "scale", "scaleX", "scaleY", "shear"] {
if a.get(field).is_some_and(|value| !value.is_null() && value.as_f64().is_none()) {
return Err(format!("`{field}` must be a number"));
}
}
j(transform(b, a)?)
}
"create_graph" => {
let mut p = Value::Object(a.clone());
if p.get("type").is_none() {