GitCorner

Code

ossartcraftvectorcraft🌐
2bc41aeb8602
use std::io::{BufRead, BufReader, Write};

use serde_json::{Value, json};

use crate::{Backend, Headless, PROTOCOL_VERSION, Remote, Server, tool_definitions};

mod batchdraw;
mod concurrency;
mod conventions;
mod distortkeys;
mod drawopts;
mod exportas;
mod fileio;
mod freeform;
mod gradient;
mod limits;
mod links;
mod liquify;
mod livepaint;
mod move_offsets;
mod pen;
mod persp;
mod place;
mod protocol;
mod shapewidgets;
mod svg;

fn server() -> Server {
    Server::new(Box::new(Headless::with_document()))
}

fn rpc(s: &mut Server, id: u64, method: &str, params: Value) -> Value {
    let line = json!({"jsonrpc": "2.0", "id": id, "method": method, "params": params}).to_string();
    let reply = s.handle_line(&line).expect("reply");
    let v: Value = serde_json::from_str(&reply).expect("reply is JSON");
    assert_eq!(v["jsonrpc"], "2.0");
    assert_eq!(v["id"], id);
    v
}

fn call(s: &mut Server, id: u64, name: &str, args: Value) -> Value {
    let v = rpc(s, id, "tools/call", json!({"name": name, "arguments": args}));
    assert!(v.get("error").is_none(), "{v}");
    v["result"].clone()
}

fn text_of(result: &Value) -> String {
    result["content"].as_array().unwrap().iter().filter(|c| c["type"] == "text").map(|c| c["text"].as_str().unwrap().to_string()).collect()
}

/// A test's own folder under the temp dir, `vectorcraft-mcp-<tag>-<pid>`, made empty when created
/// and removed with everything in it when dropped, also when the test panics. A removal that fails
/// (a file still open) is ignored.
struct TempFolder(std::path::PathBuf);

impl TempFolder {
    fn new(tag: &str) -> Self {
        let dir = std::env::temp_dir().join(format!("vectorcraft-mcp-{tag}-{}", std::process::id()));
        let _ = std::fs::remove_dir_all(&dir);
        std::fs::create_dir_all(&dir).unwrap();
        Self(dir)
    }

    /// The path of `name` in the folder.
    fn file(&self, name: &str) -> std::path::PathBuf {
        self.0.join(name)
    }
}

impl std::ops::Deref for TempFolder {
    type Target = std::path::Path;
    fn deref(&self) -> &std::path::Path {
        &self.0
    }
}

impl Drop for TempFolder {
    fn drop(&mut self) {
        let _ = std::fs::remove_dir_all(&self.0);
    }
}

/// A `TempFolder` starts empty, and dropping it removes it with the files and folders in it, also
/// when the test that holds it panics.
#[test]
fn temp_folders_start_empty_and_are_removed_when_dropped() {
    let leftover = std::env::temp_dir().join(format!("vectorcraft-mcp-drop-check-{}", std::process::id()));
    std::fs::create_dir_all(&leftover).unwrap();
    std::fs::write(leftover.join("old.txt"), b"old").unwrap();
    let dir = TempFolder::new("drop-check");
    assert_eq!(std::fs::read_dir(&*dir).unwrap().count(), 0, "a leftover folder is emptied");
    std::fs::write(dir.file("a.txt"), b"a").unwrap();
    std::fs::create_dir(dir.file("sub")).unwrap();
    std::fs::write(dir.file("sub").join("b.txt"), b"b").unwrap();
    let path = dir.to_path_buf();
    drop(dir);
    assert!(!path.exists(), "{} is removed", path.display());

    let panicked = std::panic::catch_unwind(|| {
        let dir = TempFolder::new("panic-check");
        std::fs::write(dir.file("a.txt"), b"a").unwrap();
        panic!("the test fails with {} in use", dir.display());
    });
    assert!(panicked.is_err());
    let path = std::env::temp_dir().join(format!("vectorcraft-mcp-panic-check-{}", std::process::id()));
    assert!(!path.exists(), "{} is removed after a panic", path.display());
}

// ---------- framing ----------

#[test]
fn framing_basics() {
    let mut s = server();
    // Blank lines and notifications produce no output.
    assert_eq!(s.handle_line("   "), None);
    assert_eq!(s.handle_line(r#"{"jsonrpc":"2.0","method":"notifications/initialized"}"#), None);
    assert!(s.is_initialized());
    // Parse error → -32700 with null id.
    let v: Value = serde_json::from_str(&s.handle_line("{not json").unwrap()).unwrap();
    assert_eq!(v["error"]["code"], -32700);
    assert_eq!(v["id"], Value::Null);
    // Unknown method → -32601, id echoed (string ids too).
    let v: Value = serde_json::from_str(&s.handle_line(r#"{"jsonrpc":"2.0","id":"abc","method":"nope"}"#).unwrap()).unwrap();
    assert_eq!(v["error"]["code"], -32601);
    assert_eq!(v["id"], "abc");
    // Missing method → invalid request.
    let v: Value = serde_json::from_str(&s.handle_line(r#"{"jsonrpc":"2.0","id":3}"#).unwrap()).unwrap();
    assert_eq!(v["error"]["code"], -32600);
    // Responses from the client are ignored.
    assert_eq!(s.handle_line(r#"{"jsonrpc":"2.0","id":9,"result":{}}"#), None);
    // ping
    assert_eq!(rpc(&mut s, 4, "ping", json!({}))["result"], json!({}));
    // Batch (legacy clients).
    let v: Value = serde_json::from_str(
        &s.handle_line(r#"[{"jsonrpc":"2.0","id":1,"method":"ping"},{"jsonrpc":"2.0","method":"notifications/initialized"}]"#).unwrap(),
    )
    .unwrap();
    assert_eq!(v.as_array().unwrap().len(), 1);
}

#[test]
fn framing_rejects_invalid_versions_before_dispatch() {
    use std::cell::Cell;
    use std::rc::Rc;

    struct CountingBackend(Rc<Cell<usize>>);
    impl Backend for CountingBackend {
        fn call(&mut self, _: &str, _: Value) -> Result<Value, String> {
            self.0.set(self.0.get() + 1);
            Ok(json!({}))
        }
        fn has_ui(&self) -> bool {
            false
        }
        fn describe(&self) -> String {
            "test".into()
        }
    }

    let calls = Rc::new(Cell::new(0));
    let mut s = Server::new(Box::new(CountingBackend(calls.clone())));
    let mut request = json!({"id": 7, "method": "tools/call", "params": {"name": "run_command", "arguments": {"command": "edit.clear"}}});
    for version in [None, Some(json!("1.0")), Some(json!("3.0")), Some(json!(2.0)), Some(Value::Null)] {
        if let Some(version) = version {
            request["jsonrpc"] = version;
        }
        let reply: Value = serde_json::from_str(&s.handle_line(&request.to_string()).unwrap()).unwrap();
        assert_eq!(calls.get(), 0, "invalid envelope dispatched: {request}");
        assert_eq!(reply["error"]["code"], -32600, "{reply}");
        assert_eq!(reply["id"], 7);
    }
    request["jsonrpc"] = json!("2.0");
    let reply = s.handle(request).unwrap();
    assert_eq!(reply["result"]["isError"], false, "{reply}");
    assert_eq!(calls.get(), 1);
}

#[test]
fn framing_rejects_invalid_notification_versions_in_batches() {
    let mut s = server();
    for version in [None, Some(json!("1.0")), Some(json!(2.0)), Some(Value::Null)] {
        let mut notification = json!({"method": "notifications/initialized"});
        if let Some(version) = version {
            notification["jsonrpc"] = version;
        }
        let batch = json!([notification, {"jsonrpc": "2.0", "id": "ping", "method": "ping"}]);
        let replies: Value = serde_json::from_str(&s.handle_line(&batch.to_string()).unwrap()).unwrap();
        assert!(!s.is_initialized(), "invalid notification changed state: {batch}");
        assert_eq!(replies.as_array().unwrap().len(), 1, "{replies}");
        assert_eq!(replies[0]["id"], "ping");
        assert_eq!(replies[0]["result"], json!({}));
    }
    assert_eq!(s.handle(json!({"jsonrpc": "2.0", "method": "notifications/initialized"})), None);
    assert!(s.is_initialized());
}

#[test]
fn serve_loop_writes_one_line_per_request() {
    let mut s = server();
    let input = [
        json!({"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":PROTOCOL_VERSION,"capabilities":{},"clientInfo":{"name":"t","version":"0"}}}).to_string(),
        json!({"jsonrpc":"2.0","method":"notifications/initialized"}).to_string(),
        json!({"jsonrpc":"2.0","id":2,"method":"tools/list"}).to_string(),
    ]
    .join("\n");
    let mut out = Vec::new();
    s.serve(input.as_bytes(), &mut out).unwrap();
    let lines: Vec<Value> = String::from_utf8(out).unwrap().lines().map(|l| serde_json::from_str(l).unwrap()).collect();
    assert_eq!(lines.len(), 2);
    assert_eq!(lines[0]["result"]["protocolVersion"], PROTOCOL_VERSION);
    assert_eq!(lines[0]["result"]["serverInfo"]["name"], "vectorcraft");
    assert!(lines[0]["result"]["capabilities"]["tools"].is_object());
    assert!(lines[0]["result"]["capabilities"]["resources"].is_object());
    assert!(lines[1]["result"]["tools"].as_array().unwrap().len() >= 19);
}

#[test]
fn initialize_negotiates_version() {
    let mut s = server();
    let v = rpc(&mut s, 1, "initialize", json!({"protocolVersion": "2025-03-26"}));
    assert_eq!(v["result"]["protocolVersion"], "2025-03-26");
    let v = rpc(&mut s, 2, "initialize", json!({"protocolVersion": "1999-01-01"}));
    assert_eq!(v["result"]["protocolVersion"], PROTOCOL_VERSION);
}

// ---------- tools/list ----------

#[test]
fn tool_schemas_are_valid() {
    let tools = tool_definitions();
    let mut names = std::collections::HashSet::new();
    for t in &tools {
        let name = t["name"].as_str().expect("name");
        assert!(!name.is_empty() && name.len() <= 64 && name.chars().all(|c| c.is_ascii_alphanumeric() || c == '_' || c == '-'), "{name}");
        assert!(names.insert(name), "duplicate tool {name}");
        assert!(t["description"].as_str().is_some_and(|d| d.len() > 10), "{name} description");
        let schema = &t["inputSchema"];
        assert_eq!(schema["type"], "object", "{name}");
        let props = schema["properties"].as_object().unwrap_or_else(|| panic!("{name} properties"));
        for r in schema.get("required").and_then(Value::as_array).into_iter().flatten() {
            assert!(props.contains_key(r.as_str().unwrap()), "{name}: required {r} not in properties");
        }
        for (k, p) in props {
            assert!(p.is_object(), "{name}.{k}");
            assert!(p.get("type").is_some() || p.get("anyOf").is_some(), "{name}.{k} has no type");
        }
    }
    for want in [
        "list_commands",
        "run_command",
        "inspect_document",
        "inspect_ui",
        "select_tool",
        "pointer_gesture",
        "draw_path",
        "draw_shape",
        "set_paint",
        "press_key",
        "invoke_menu",
        "open_panel",
        "screenshot",
        "open_file",
        "save_file",
        "export",
        "undo",
        "redo",
        "command_list",
        "command_run",
        "command_batch",
        "doc_inspect",
        "render_preview",
        "ui_inspect",
        "ui_screenshot",
        "type_text",
        "answer_dialog",
        "add_text",
        "apply_effect",
        "pathfinder",
        "transform",
        "create_graph",
        "text_wrap",
    ] {
        assert!(names.contains(want), "missing tool {want}");
    }
    assert_eq!(tools.len(), 33, "parity docs and #6460 expect 33 MCP tools");
}

/// Headless `command_list` must expose engine commands that a stale release CLI used to omit (#6460).
#[test]
fn headless_command_list_includes_post_022_engine_commands() {
    let mut s = server();
    rpc(&mut s, 1, "initialize", json!({"protocolVersion": PROTOCOL_VERSION}));
    s.handle_line(r#"{"jsonrpc":"2.0","method":"notifications/initialized"}"#);
    let want = [
        "actionSet.read",
        "engine.apiReference",
        "script.run",
        "script.stop",
        "text.spelling.check",
        "document.move",
        "document.compare",
        "edit.newDocumentFromState",
    ];
    for (n, id) in want.iter().enumerate() {
        let r = call(&mut s, 10 + n as u64, "command_list", json!({"filter": id, "brief": true}));
        assert_eq!(r["isError"], false, "{id}: {r}");
        let list: Value = serde_json::from_str(&text_of(&r)).unwrap();
        assert!(list.as_array().unwrap().iter().any(|c| c["id"] == *id), "missing {id} in {list}");
    }
}

/// Headless `command_list` lists desktop UI commands with `ui: true` (#6460).
#[test]
fn headless_command_list_includes_ui_commands() {
    let mut s = server();
    rpc(&mut s, 1, "initialize", json!({"protocolVersion": PROTOCOL_VERSION}));
    s.handle_line(r#"{"jsonrpc":"2.0","method":"notifications/initialized"}"#);
    let want = [
        "addon.arrange.zoomSelection",
        "file.newDialog",
        "view.fitArtboard",
        "view.rotateReset",
        "app.quit",
        "window.arrange.tile",
        "align.horizontalLeft",
        "align.toKeyObject",
        "ui.action.play",
    ];
    for (n, id) in want.iter().enumerate() {
        let r = call(&mut s, 20 + n as u64, "command_list", json!({"filter": id, "brief": true}));
        assert_eq!(r["isError"], false, "{id}: {r}");
        let list: Value = serde_json::from_str(&text_of(&r)).unwrap();
        let entry = list.as_array().unwrap().iter().find(|c| c["id"] == *id).unwrap_or_else(|| panic!("missing {id} in {list}"));
        assert_eq!(entry["ui"], true, "{entry}");
    }
    let r = call(&mut s, 40, "command_run", json!({"id": "view.fitArtboard", "params": {}}));
    assert_eq!(r["isError"], true, "{r}");
    assert!(text_of(&r).contains("--connect"), "{r}");
}

// ---------- headless end-to-end ----------

#[test]
fn headless_end_to_end() {
    let dir = TempFolder::new("headless-end-to-end");
    let mut s = server();
    rpc(&mut s, 1, "initialize", json!({"protocolVersion": PROTOCOL_VERSION}));
    s.handle_line(r#"{"jsonrpc":"2.0","method":"notifications/initialized"}"#);

    let r = call(
        &mut s,
        2,
        "draw_shape",
        json!({"shape": "rectangle", "x": 100, "y": 100, "width": 200, "height": 120, "fill": "#ff0000", "stroke": "none"}),
    );
    assert_eq!(r["isError"], false, "{r}");
    let created: Value = serde_json::from_str(&text_of(&r)).unwrap();
    let id = created["id"].as_u64().expect("id");

    let r = call(
        &mut s,
        3,
        "draw_shape",
        json!({"shape": "star", "cx": 400, "cy": 400, "radius1": 80, "radius2": 40, "fill": [0, 0, 1], "strokeWidth": 3}),
    );
    assert_eq!(r["isError"], false, "{r}");

    let r = call(&mut s, 4, "inspect_document", json!({}));
    let doc: Value = serde_json::from_str(&text_of(&r)).unwrap();
    assert!(doc["objects"].as_u64().unwrap() >= 3, "{doc}");
    let layer = &doc["layers"][0];
    let rect = layer["children"].as_array().unwrap().iter().find(|c| c["id"] == id).expect("rect in layer");
    assert_eq!(rect["bounds"]["width"], 200.0);
    assert_eq!(rect["fill"], "#ff0000", "{rect}");
    assert_eq!(rect["stroke"], "None");

    // A depth-0 inspect is the skeleton: top layers with counts, nothing below them;
    // a bad slice is a tool error, not a full dump.
    let r = call(&mut s, 41, "inspect_document", json!({"depth": 0}));
    let skel: Value = serde_json::from_str(&text_of(&r)).unwrap();
    assert!(skel["layers"][0].get("children").is_none(), "{skel}");
    assert_eq!(skel["layers"][0]["childCount"], layer["children"].as_array().unwrap().len());
    assert_eq!(skel["artboards"], doc["artboards"]);
    assert_eq!(call(&mut s, 42, "inspect_document", json!({"depth": -1}))["isError"], true);

    // Scoping reaches the command: one node by id equals its part of the full tree;
    // an unknown id is a tool error.
    let r = call(&mut s, 43, "doc_inspect", json!({"ids": [id]}));
    let one: Value = serde_json::from_str(&text_of(&r)).unwrap();
    assert_eq!(one["nodes"], json!([rect]), "{one}");
    assert_eq!(call(&mut s, 44, "doc_inspect", json!({"ids": [999_999]}))["isError"], true);

    // Screenshot returns image content (base64 PNG) and text.
    let shot_path = dir.file("shot.png");
    let r = call(&mut s, 5, "screenshot", json!({"path": shot_path.to_str().unwrap()}));
    assert_eq!(r["isError"], false, "{r}");
    let img = r["content"].as_array().unwrap().iter().find(|c| c["type"] == "image").expect("image content");
    assert_eq!(img["mimeType"], "image/png");
    let png = vectorcraft_format::base64_decode(img["data"].as_str().unwrap()).unwrap();
    assert_eq!(&png[..8], b"\x89PNG\r\n\x1a\n");
    assert_eq!(std::fs::read(&shot_path).unwrap(), png);

    // Export SVG.
    let svg_path = dir.file("out.svg");
    let r = call(&mut s, 6, "export", json!({"path": svg_path.to_str().unwrap()}));
    assert_eq!(r["isError"], false, "{r}");
    let svg = std::fs::read_to_string(&svg_path).unwrap();
    assert!(svg.contains("<svg") && (svg.contains("<rect") || svg.contains("<path")), "{svg}");

    // Save native, open it back.
    let dc = dir.file("out.vectorcraft");
    assert_eq!(call(&mut s, 7, "save_file", json!({"path": dc.to_str().unwrap()}))["isError"], false);
    let r = call(&mut s, 8, "open_file", json!({"path": dc.to_str().unwrap()}));
    assert_eq!(r["isError"], false, "{r}");
    let r = call(&mut s, 9, "open_file", json!({"path": svg_path.to_str().unwrap()}));
    assert_eq!(r["isError"], false, "{r}");

    // PDF and PDF-compatible .ai open headless too.
    let pdf_path = dir.file("out.pdf");
    assert_eq!(call(&mut s, 20, "export", json!({"path": pdf_path.to_str().unwrap()}))["isError"], false);
    let ai_path = dir.file("out.ai");
    std::fs::copy(&pdf_path, &ai_path).unwrap();
    for (id, p) in [(21, &pdf_path), (22, &ai_path)] {
        let r = call(&mut s, id, "open_file", json!({"path": p.to_str().unwrap()}));
        assert_eq!(r["isError"], false, "{r}");
        let doc: Value = serde_json::from_str(&text_of(&call(&mut s, id + 10, "inspect_document", json!({})))).unwrap();
        assert!(doc["objects"].as_u64().unwrap() >= 2, "{doc}");
    }

    // Resources.
    let v = rpc(&mut s, 10, "resources/list", json!({}));
    assert_eq!(v["result"]["resources"].as_array().unwrap().len(), 4);
    let v = rpc(&mut s, 11, "resources/read", json!({"uri": "vectorcraft://document"}));
    let text = v["result"]["contents"][0]["text"].as_str().unwrap();
    assert!(serde_json::from_str::<Value>(text).unwrap()["layers"].is_array());
    let v = rpc(&mut s, 12, "resources/read", json!({"uri": "vectorcraft://document/json"}));
    assert!(v["result"]["contents"][0]["text"].as_str().unwrap().len() > 10);
    let v = rpc(&mut s, 13, "resources/read", json!({"uri": "vectorcraft://nope"}));
    assert_eq!(v["error"]["code"], -32002);
}

/// The trace-and-style prompt's steps, run in its order on an image drawn here: each one
/// succeeds, the expanded trace holds no image, and its paths keep the image's colors.
#[test]
fn trace_and_style_prompt_steps_run_in_order() {
    let dir = TempFolder::new("trace-and-style");
    let mut s = server();
    // 60 × 40 pixels: a red and a blue block on white.
    let png = dir.file("trace-and-style.png");
    image::RgbImage::from_fn(60, 40, |x, y| match (x, y) {
        (5..25, 5..35) => image::Rgb([220, 30, 30]),
        (35..55, 5..35) => image::Rgb([30, 60, 200]),
        _ => image::Rgb([255, 255, 255]),
    })
    .save(&png)
    .unwrap();
    let path = png.to_str().unwrap();
    let v = rpc(&mut s, 1, "prompts/get", json!({"name": "trace-and-style", "arguments": {"path": path}}));
    let prompt = v["result"]["messages"][0]["content"]["text"].as_str().unwrap().to_string();

    let ok = |s: &mut Server, id: u64, name: &str, args: Value| {
        let r = call(s, id, name, args);
        assert_eq!(r["isError"], false, "{name}: {r}");
        serde_json::from_str::<Value>(&text_of(&r)).unwrap_or(Value::Null)
    };
    let command = |s: &mut Server, id: u64, command: &str, params: Value| {
        assert!(prompt.contains(command), "the prompt names {command}: {prompt}");
        ok(s, id, "run_command", json!({"command": command, "params": params}))
    };
    ok(&mut s, 2, "open_file", json!({"path": path}));
    command(&mut s, 3, "select.all", json!({}));
    let traced = command(&mut s, 4, "imageTrace.make", json!({"preset": "6 Colors"}));
    assert!(traced["colors"].as_u64().unwrap() > 1, "{traced}");
    command(&mut s, 5, "imageTrace.expand", json!({}));
    ok(&mut s, 6, "apply_effect", json!({"effect": "stylize.scribble"}));
    let colors = command(&mut s, 7, "recolor.colors", json!({}));
    let from = colors["colors"].as_array().unwrap().iter().filter_map(|c| c["hex"].as_str()).find(|h| *h != "#ffffff").unwrap().to_string();
    command(&mut s, 8, "recolor.apply", json!({"map": {from.as_str(): "#00aa00"}}));
    let r = call(&mut s, 9, "render_preview", json!({}));
    assert_eq!(r["isError"], false, "{r}");

    // No image is left, and the paths carry more than one fill color, the new one among them.
    let doc = ok(&mut s, 10, "inspect_document", json!({}));
    let (mut kinds, mut fills) = (vec![], std::collections::BTreeSet::new());
    let mut stack: Vec<&Value> = doc["layers"].as_array().unwrap().iter().collect();
    while let Some(n) = stack.pop() {
        kinds.push(n["kind"].as_str().unwrap_or_default().to_string());
        if let Some(fill) = n["fill"].as_str().filter(|f| f.starts_with('#')) {
            fills.insert(fill.to_string());
        }
        stack.extend(n["children"].as_array().into_iter().flatten());
    }
    assert!(!kinds.iter().any(|k| k == "Image"), "{kinds:?}");
    assert!(fills.len() > 1 && fills.contains("#00aa00") && !fills.contains(&from), "{fills:?}");
}

/// A window capture and an artboard preview scaled down keep the color of opaque pixels beside
/// clear ones: the clear black beside them does not darken them.
#[test]
fn scaled_down_pictures_keep_the_color_beside_clear_pixels() {
    struct Striped;
    impl Backend for Striped {
        fn call(&mut self, method: &str, _: Value) -> Result<Value, String> {
            let mut data = Vec::with_capacity(64 * 4 * 4);
            for _ in 0..4 {
                for x in 0..64 {
                    data.extend_from_slice(&if x % 2 == 0 { [200, 30, 30, 255] } else { [0, 0, 0, 0] });
                }
            }
            let raw = craft_image_codecs::Image::from_raw(64, 4, craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, data)
                .unwrap();
            let png = craft_image_codecs::encode(&raw, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default()).unwrap();
            let png = vectorcraft_format::base64_encode(&png);
            Ok(match method {
                "document.inspect" => json!({"artboards": [{"width": 64, "height": 4}]}),
                _ => json!({"pngBase64": png, "width": 64, "height": 4}),
            })
        }
        fn has_ui(&self) -> bool {
            true
        }
        fn describe(&self) -> String {
            "test".into()
        }
    }

    let mut s = Server::new(Box::new(Striped));
    let shots = [("screenshot", json!({"window": true, "max_side": 16})), ("render_preview", json!({"max_side": 16}))];
    for (id, (tool, args)) in (1..).zip(shots) {
        let r = call(&mut s, id, tool, args);
        let data =
            r["content"].as_array().unwrap().iter().find(|c| c["type"] == "image").unwrap_or_else(|| panic!("{tool}: {r}"))["data"].as_str().unwrap();
        let decoded = craft_image_codecs::decode(&vectorcraft_format::base64_decode(data).unwrap()).unwrap();
        let img = image::RgbaImage::from_raw(decoded.width(), decoded.height(), decoded.to_rgba8()).unwrap();
        assert_eq!(img.dimensions(), (16, 1), "{tool}");
        assert!(img.pixels().all(|p| p.0 == [200, 30, 30, 128]), "{tool}: {:?}", img.get_pixel(0, 0));
    }
}

/// `screenshot` and `render_preview` with `traceViews: true` draw an Image Trace object in the
/// view set with imageTrace.setView, as the app's window does; without it they draw the tracing
/// result, as exports do. The image is a dark disc (r = 30 px) on blue, traced with Ignore White.
#[test]
fn screenshot_and_render_preview_draw_the_trace_view_with_trace_views() {
    let dir = TempFolder::new("trace-views");
    let mut s = server();
    let png = dir.file("trace-views.png");
    let mut rgb = Vec::with_capacity(100 * 100 * 3);
    for y in 0..100 {
        for x in 0..100 {
            let (dx, dy) = (f64::from(x) + 0.5 - 50.0, f64::from(y) + 0.5 - 50.0);
            rgb.extend_from_slice(&if dx * dx + dy * dy <= 900.0 { [0, 0, 0] } else { [170, 210, 255] });
        }
    }
    let raw = craft_image_codecs::Image::from_raw(100, 100, craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8, rgb).unwrap();
    std::fs::write(&png, craft_image_codecs::encode(&raw, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default()).unwrap())
        .unwrap();
    let ok = |s: &mut Server, id: u64, name: &str, args: Value| {
        let r = call(s, id, name, args);
        assert_eq!(r["isError"], false, "{name}: {r}");
        r
    };
    ok(&mut s, 1, "open_file", json!({"path": png.to_str().unwrap()}));
    ok(&mut s, 2, "run_command", json!({"command": "select.all"}));
    ok(&mut s, 3, "run_command", json!({"command": "imageTrace.make", "params": {"params": {"ignoreWhite": true}}}));
    let pixels = |r: &Value| {
        let data = r["content"].as_array().unwrap().iter().find(|c| c["type"] == "image").unwrap()["data"].as_str().unwrap();
        let decoded = craft_image_codecs::decode(&vectorcraft_format::base64_decode(data).unwrap()).unwrap();
        image::RgbaImage::from_raw(decoded.width(), decoded.height(), decoded.to_rgba8()).unwrap()
    };
    let dark = |p: &image::Rgba<u8>| p[0] < 60 && p[1] < 60 && p[2] < 60;
    let white = |p: &image::Rgba<u8>| p[0] > 245 && p[1] > 245 && p[2] > 245;
    let blue = |p: &image::Rgba<u8>| p[0] < 200 && p[2] > 245;
    let (middle, beside) = ((50, 50), (10, 10));
    let renders = [("screenshot", json!({})), ("render_preview", json!({"max_side": 100}))];
    for (id, (name, args)) in (10..).zip(renders) {
        let with = |mut a: Value| {
            a["traceViews"] = json!(true);
            a
        };
        ok(&mut s, id, "run_command", json!({"command": "imageTrace.setView", "params": {"view": "sourceImage"}}));
        let (view, result) = (pixels(&ok(&mut s, id, name, with(args.clone()))), pixels(&ok(&mut s, id, name, args.clone())));
        assert_eq!((view.width(), result.width()), (100, 100), "{name}");
        assert!(blue(view.get_pixel(beside.0, beside.1)), "{name}: the source image beside the disc {:?}", view.get_pixel(beside.0, beside.1));
        assert!(white(result.get_pixel(beside.0, beside.1)), "{name}: the tracing result {:?}", result.get_pixel(beside.0, beside.1));
        assert!(dark(view.get_pixel(middle.0, middle.1)) && dark(result.get_pixel(middle.0, middle.1)), "{name}: the disc's middle");
        // Outlines: a line at the disc's right side (x = 80) and no fill in its middle.
        ok(&mut s, id, "run_command", json!({"command": "imageTrace.setView", "params": {"view": "outlines"}}));
        let (view, result) = (pixels(&ok(&mut s, id, name, with(args.clone()))), pixels(&ok(&mut s, id, name, args.clone())));
        let row: Vec<u8> = (75..=85).map(|x| view.get_pixel(x, 50)[0]).collect();
        assert!(row.iter().any(|&r| r < 128), "{name}: the disc's outline {row:?}");
        assert!(white(view.get_pixel(middle.0, middle.1)), "{name}: no fill in outlines {:?}", view.get_pixel(middle.0, middle.1));
        assert!(dark(result.get_pixel(middle.0, middle.1)), "{name}: the tracing result's fill");
        assert_eq!(call(&mut s, id, name, json!({"traceViews": "yes"}))["isError"], true, "{name}: a non-boolean traceViews");
    }
}

#[test]
fn headless_path_gesture_undo() {
    let dir = TempFolder::new("path-gesture-undo");
    let mut s = server();
    let count = |s: &mut Server| -> u64 {
        let r = call(s, 90, "inspect_document", json!({}));
        serde_json::from_str::<Value>(&text_of(&r)).unwrap()["objects"].as_u64().unwrap()
    };
    let base = count(&mut s);
    let r = call(&mut s, 1, "draw_path", json!({"points": [[10, 10], [100, 10], [50, 90]], "closed": true, "fill": "#00ff00"}));
    assert_eq!(r["isError"], false, "{r}");
    let r = call(&mut s, 2, "draw_path", json!({"d": "M0 0 C 10 10 20 10 30 0", "stroke": "#000000", "strokeWidth": 2}));
    assert_eq!(r["isError"], false, "{r}");
    assert_eq!(count(&mut s), base + 2);

    // Drag out an ellipse with the ellipse tool.
    let r = call(
        &mut s,
        3,
        "pointer_gesture",
        json!({"tool": "ellipse", "events": [{"kind": "down", "x": 200, "y": 200}, {"kind": "drag", "x": 260, "y": 240}, {"kind": "up", "x": 300, "y": 280}]}),
    );
    assert_eq!(r["isError"], false, "{r}");
    assert_eq!(count(&mut s), base + 3);

    assert_eq!(call(&mut s, 4, "undo", json!({}))["isError"], false);
    assert_eq!(count(&mut s), base + 2);
    assert_eq!(call(&mut s, 5, "redo", json!({}))["isError"], false);
    assert_eq!(count(&mut s), base + 3);

    // Keyboard shortcut headless: Cmd+Z undoes.
    let r = call(&mut s, 6, "press_key", json!({"key": "Z", "mods": {"cmd": true}}));
    assert_eq!(r["isError"], false, "{r}");
    assert_eq!(count(&mut s), base + 2);

    // Long tail through run_command + list_commands filter.
    let r = call(&mut s, 7, "run_command", json!({"command": "select.all"}));
    assert_eq!(r["isError"], false, "{r}");
    let r = call(&mut s, 8, "run_command", json!({"command": "object.group"}));
    assert_eq!(r["isError"], false, "{r}");
    let r = call(&mut s, 9, "list_commands", json!({"filter": "group"}));
    let v: Value = serde_json::from_str(&text_of(&r)).unwrap();
    assert!(v["commands"].as_array().unwrap().iter().any(|c| c["id"] == "object.group"));
    assert!(
        v["commands"]
            .as_array()
            .unwrap()
            .iter()
            .all(|c| c["id"].as_str().unwrap().contains("group") || c["label"].as_str().unwrap().to_lowercase().contains("group"))
    );

    // set_paint on the selection.
    let r = call(&mut s, 10, "set_paint", json!({"fill": "#123456", "strokeWidth": 4}));
    assert_eq!(r["isError"], false, "{r}");
    let r = call(&mut s, 11, "select_tool", json!({"tool": "pen"}));
    assert_eq!(r["isError"], false, "{r}");
    let r = call(&mut s, 12, "export", json!({"path": dir.file("x.png").to_str().unwrap(), "scale": 0.5}));
    assert_eq!(r["isError"], false, "{r}");
    assert_eq!(&std::fs::read(dir.file("x.png")).unwrap()[..4], b"\x89PNG");
}

/// A hover's reply comes once the tool's highlight is ready, also when the tool finds it on another
/// thread (the Shape Builder's map of a large selection).
#[test]
fn headless_hover_waits_for_the_shape_builder_map() {
    let mut h = Headless::with_document();
    let run = |h: &mut Headless, command: &str, params: Value| {
        let r = crate::call_tool(h, "run_command", &json!({"command": command, "params": params}));
        assert!(!r.is_error, "{command}: {r:?}");
    };
    for k in 0..40 {
        run(&mut h, "shape.rectangle", json!({"x": 100 + k * 10, "y": 100, "width": 20, "height": 20}));
    }
    run(&mut h, "select.all", json!({}));
    run(&mut h, "tool.select", json!({"tool": "shapeBuilder"}));
    let r = crate::call_tool(&mut h, "pointer_gesture", &json!({"events": [{"kind": "move", "x": 115, "y": 110}]}));
    assert!(!r.is_error, "{r:?}");
    assert!(!h.session.tool_waiting());
    assert!(!h.session.overlays(h.view).is_empty(), "the region under the pointer is highlighted");
    // A click's reply comes once the merge the tool computes on another thread is applied, with
    // the selection after it.
    let click = json!({"events": [{"kind": "down", "x": 115, "y": 110}, {"kind": "up", "x": 115, "y": 110}]});
    let r = crate::call_tool(&mut h, "pointer_gesture", &click);
    assert!(!r.is_error, "{r:?}");
    assert!(!h.session.tool_waiting() && !h.session.tool_editing());
    let text: String = r.content.iter().filter_map(|c| c["text"].as_str()).collect();
    let reply: Value = serde_json::from_str(&text).unwrap();
    assert_eq!(reply["selection"].as_array().map(Vec::len), Some(41), "the overlap and the 40 squares, two of them cut: {reply}");
}

/// inspect_document reads back the paint type's characters show, as add_text and set_paint give it.
#[test]
fn inspect_document_reports_the_paint_of_type() {
    let mut s = server();
    let r = call(&mut s, 2, "add_text", json!({"text": "HI", "x": 50, "y": 60, "size": 24, "color": "#ff0000"}));
    assert_eq!(r["isError"], false, "{r}");
    let id = serde_json::from_str::<Value>(&text_of(&r)).unwrap()["id"].clone();
    let node = |s: &mut Server, n: u64| {
        let v: Value = serde_json::from_str(&text_of(&call(s, n, "inspect_document", json!({})))).unwrap();
        v["layers"][0]["children"].as_array().unwrap().iter().find(|c| c["id"] == id).cloned().unwrap()
    };
    let t = node(&mut s, 3);
    assert_eq!((t["fill"].as_str(), t["stroke"].as_str()), (Some("#ff0000"), Some("None")), "{t}");
    let r = call(&mut s, 4, "set_paint", json!({"fill": "#0000ff", "stroke": "#00ff00", "strokeWidth": 3, "ids": [id]}));
    assert_eq!(r["isError"], false, "{r}");
    let t = node(&mut s, 5);
    assert_eq!((t["fill"].as_str(), t["stroke"].as_str(), t["strokeWidth"].as_f64()), (Some("#0000ff"), Some("#00ff00"), Some(3.0)), "{t}");
}

/// set_paint says how many objects it painted, and says so when it only set the paint for new art.
#[test]
fn set_paint_reports_how_many_objects_it_painted() {
    let mut s = server();
    let r = call(&mut s, 2, "draw_shape", json!({"shape": "rectangle", "x": 0, "y": 0, "width": 10, "height": 10, "fill": "#ffffff"}));
    let id = serde_json::from_str::<Value>(&text_of(&r)).unwrap()["id"].clone();
    let fill = |s: &mut Server, n: u64| {
        let v: Value = serde_json::from_str(&text_of(&call(s, n, "inspect_document", json!({})))).unwrap();
        v["layers"][0]["children"].as_array().unwrap().iter().find(|c| c["id"] == id).unwrap()["fill"].clone()
    };
    let paint = |s: &mut Server, n: u64, args: Value| {
        let r = call(s, n, "set_paint", args);
        assert_eq!(r["isError"], false, "{r}");
        serde_json::from_str::<Value>(&text_of(&r)).unwrap()
    };

    // Nothing selected and no ids: only the paint for new art changes.
    let r = call(&mut s, 3, "run_command", json!({"command": "select.set", "params": {"ids": []}}));
    assert_eq!(r["isError"], false, "{r}");
    let v = paint(&mut s, 4, json!({"fill": "#ff0000"}));
    assert_eq!(v["applied"], json!({"fill": "#ff0000"}), "{v}");
    assert_eq!(v["objects"], 0, "{v}");
    assert!(v["note"].as_str().is_some_and(|n| n.contains("new art")), "{v}");
    assert_eq!(fill(&mut s, 5), "#ffffff");

    // The ids given, counted once each.
    let v = paint(&mut s, 6, json!({"fill": "#00ff00", "ids": [id, id]}));
    assert_eq!(v["objects"], 1, "{v}");
    assert!(v.get("note").is_none(), "{v}");
    assert_eq!(fill(&mut s, 7), "#00ff00");

    // The selection.
    let r = call(&mut s, 8, "run_command", json!({"command": "select.set", "params": {"ids": [id]}}));
    assert_eq!(r["isError"], false, "{r}");
    let v = paint(&mut s, 9, json!({"fill": "#0000ff"}));
    assert_eq!(v["objects"], 1, "{v}");
    assert!(v.get("note").is_none(), "{v}");
    assert_eq!(fill(&mut s, 10), "#0000ff");

    // Empty `ids` paint no object: only the paint for new art changes.
    let v = paint(&mut s, 11, json!({"strokeWidth": 3, "ids": []}));
    assert_eq!(v["objects"], 0, "{v}");
    assert!(v["note"].as_str().is_some_and(|n| n.contains("new art")), "{v}");

    // A selected group counts the objects inside it.
    let r = call(&mut s, 12, "draw_shape", json!({"shape": "ellipse", "x": 20, "y": 0, "width": 10, "height": 10}));
    assert_eq!(r["isError"], false, "{r}");
    for (n, command) in [(13, "select.all"), (14, "object.group")] {
        let r = call(&mut s, n, "run_command", json!({"command": command}));
        assert_eq!(r["isError"], false, "{r}");
    }
    let v = paint(&mut s, 15, json!({"stroke": "#000000"}));
    assert_eq!(v["objects"], 2, "{v}");
    let v = paint(&mut s, 16, json!({"strokeWidth": 2}));
    assert_eq!(v["objects"], 2, "{v}");
}

/// #1003: type made on or in a path takes add_text's `color` too, over the current fill.
#[test]
fn add_text_on_or_in_a_path_takes_its_color() {
    for mode in ["onPath", "area"] {
        let mut s = server();
        let r = call(&mut s, 2, "draw_shape", json!({"shape": "rectangle", "x": 40, "y": 40, "width": 180, "height": 60, "fill": "#0000ff"}));
        let path = serde_json::from_str::<Value>(&text_of(&r)).unwrap()["id"].clone();
        let r = call(&mut s, 3, "add_text", json!({"text": "Hello", "path": path, "mode": mode, "color": "#ff0000"}));
        assert_eq!(r["isError"], false, "{r}");
        let id = serde_json::from_str::<Value>(&text_of(&r)).unwrap()["id"].clone();
        let v: Value = serde_json::from_str(&text_of(&call(&mut s, 4, "inspect_document", json!({})))).unwrap();
        let t = v["layers"][0]["children"].as_array().unwrap().iter().find(|c| c["id"] == id).cloned().unwrap();
        assert_eq!(t["fill"].as_str(), Some("#ff0000"), "{mode}: {t}");
    }
}

/// #1748: add_text's `justify` centres point type on (x, y) or ends it there.
#[test]
fn add_text_justify_anchors_point_type_on_x() {
    let mut s = server();
    let mut ids = Vec::new();
    for (n, justify) in [(2, "left"), (3, "center"), (4, "right")] {
        let r = call(&mut s, n, "add_text", json!({"text": "Title", "x": 300, "y": 100, "size": 24, "justify": justify}));
        assert_eq!(r["isError"], false, "{r}");
        ids.push(serde_json::from_str::<Value>(&text_of(&r)).unwrap()["id"].clone());
    }
    let doc: Value = serde_json::from_str(&text_of(&call(&mut s, 5, "inspect_document", json!({})))).unwrap();
    let b = |id: &Value| {
        let t = doc["layers"][0]["children"].as_array().unwrap().iter().find(|c| &c["id"] == id).unwrap();
        (t["bounds"]["x"].as_f64().unwrap(), t["bounds"]["width"].as_f64().unwrap())
    };
    let ((l, lw), (c, cw), (r, rw)) = (b(&ids[0]), b(&ids[1]), b(&ids[2]));
    assert!(lw > 10.0 && (l - 300.0).abs() < 1.0, "{l} {lw}");
    assert!((c + cw / 2.0 - 300.0).abs() < 0.5, "{c} {cw}");
    assert!((r + rw - 300.0).abs() < 1.0, "{r} {rw}");
}

/// add_text forwards writing direction to both text-creation commands, keeping horizontal as the default.
#[test]
fn add_text_sets_writing_direction_for_every_kind() {
    let tools = tool_definitions();
    let definition = tools.iter().find(|t| t["name"] == "add_text").unwrap();
    assert_eq!(definition["inputSchema"]["properties"]["vertical"]["type"], "boolean");
    for mode in ["point", "area", "pathArea", "onPath"] {
        for vertical in [None, Some(false), Some(true)] {
            let mut s = server();
            let mut args = json!({"text": "AB", "size": 24});
            if mode == "point" || mode == "area" {
                args["x"] = json!(100);
                args["y"] = json!(100);
                if mode == "area" {
                    args["width"] = json!(180);
                    args["height"] = json!(160);
                }
            } else {
                let path = call(&mut s, 1, "draw_shape", json!({"shape": "rectangle", "x": 40, "y": 40, "width": 180, "height": 160}));
                assert_eq!(path["isError"], false, "{path}");
                args["path"] = serde_json::from_str::<Value>(&text_of(&path)).unwrap()["id"].clone();
                args["mode"] = json!(if mode == "pathArea" { "area" } else { "onPath" });
            }
            if let Some(value) = vertical {
                args["vertical"] = json!(value);
            }
            let made = call(&mut s, 2, "add_text", args);
            assert_eq!(made["isError"], false, "{mode} {vertical:?}: {made}");
            let id = serde_json::from_str::<Value>(&text_of(&made)).unwrap()["id"].clone();
            let node = call(&mut s, 3, "run_command", json!({"command": "document.node", "params": {"id": id}}));
            assert_eq!(node["isError"], false, "{node}");
            let node: Value = serde_json::from_str(&text_of(&node)).unwrap();
            assert_eq!(node["kind"]["type"], "text", "{node}");
            assert_eq!(node["kind"]["vertical"].as_bool().unwrap_or(false), vertical.unwrap_or(false), "{mode}: {node}");
        }
    }
}

/// save_file says it saves in the document's own format or the one the path's extension picks.
#[test]
fn save_file_describes_the_formats_it_writes() {
    let tools = tool_definitions();
    let d = tools.iter().find(|t| t["name"] == "save_file").and_then(|t| t["description"].as_str()).unwrap();
    for ext in [".vectorcraft", ".vctemplate", ".pdf", ".svg", ".svgz", ".ai"] {
        assert!(d.contains(ext), "{ext}: {d}");
    }
    assert!(d.contains("its own format"), "{d}");
}

#[test]
fn errors_are_tool_results_not_crashes() {
    let mut s = server();
    let cases = [
        ("no_such_tool", json!({})),
        ("draw_shape", json!({"shape": "hexagon"})),
        ("draw_shape", json!({"shape": "rectangle", "x": "left"})),
        ("draw_shape", json!({})),
        ("draw_path", json!({"points": [[1]]})),
        ("draw_path", json!({})),
        ("run_command", json!({"command": "does.not.exist"})),
        ("run_command", json!({"command": "object.move", "params": 5})),
        ("run_command", json!({})),
        ("pointer_gesture", json!({"events": [{"kind": "wiggle", "x": 0, "y": 0}]})),
        ("pointer_gesture", json!({"events": []})),
        ("select_tool", json!({"tool": "laser"})),
        ("set_paint", json!({})),
        ("set_paint", json!({"fill": "#zzzzzz"})),
        ("inspect_ui", json!({})),
        ("open_panel", json!({"panel": "Layers"})),
        ("type_text", json!({"text": "hi"})),
        ("screenshot", json!({"window": true})),
        ("open_file", json!({"path": "/definitely/not/here.svg"})),
        ("export", json!({"path": "/tmp/out.dwg"})),
        ("redo", json!({})),
        ("press_key", json!({"key": "F13"})),
    ];
    for (i, (name, args)) in cases.iter().enumerate() {
        let r = call(&mut s, i as u64, name, args.clone());
        assert_eq!(r["isError"], true, "{name} {args} → {r}");
        assert!(!text_of(&r).is_empty());
    }
    // Arguments that aren't an object.
    let v = rpc(&mut s, 100, "tools/call", json!({"name": "undo", "arguments": [1, 2]}));
    assert_eq!(v["result"]["isError"], true);
    // Missing name is a protocol error.
    let v = rpc(&mut s, 101, "tools/call", json!({}));
    assert_eq!(v["error"]["code"], -32602);
    // The server still works afterwards.
    let r = call(&mut s, 102, "inspect_document", json!({}));
    assert_eq!(r["isError"], false);
}

// ---------- remote ----------

/// A fake control server: answers `document.inspect`, echoes `engine.execute`, errors otherwise.
fn fake_app() -> (String, std::thread::JoinHandle<Vec<Value>>) {
    fake_app_refusing_confirm(None)
}

/// [`fake_app`], answering `ui.dialog.confirm` with the error `refusal` when one is given.
fn fake_app_refusing_confirm(refusal: Option<&'static str>) -> (String, std::thread::JoinHandle<Vec<Value>>) {
    fake_app_with(refusal, true)
}

/// [`fake_app_refusing_confirm`]. With `answers_in_set`, `ui.dialog.set {action}` also confirms or
/// cancels the dialog and replies `{dialog, result}`, as the app does; without it the set ignores
/// `action`, as an app that predates it does.
fn fake_app_with(refusal: Option<&'static str>, answers_in_set: bool) -> (String, std::thread::JoinHandle<Vec<Value>>) {
    let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
    let addr = listener.local_addr().unwrap().to_string();
    let h = std::thread::spawn(move || {
        let (stream, _) = listener.accept().unwrap();
        let mut out = stream.try_clone().unwrap();
        let mut seen = vec![];
        // A dialog is open until `ui.dialog.cancel` closes it; then `ui.dialog.set` is refused, as in the app.
        let mut dialog_open = true;
        for line in BufReader::new(stream).lines() {
            let msg: Value = serde_json::from_str(&line.unwrap()).unwrap();
            let reply = match msg["method"].as_str().unwrap() {
                "document.inspect" => json!({"ok": true, "result": {"title": "Remote", "layers": []}}),
                "engine.execute" => json!({"ok": true, "result": {"echo": msg["params"]}}),
                "ui.inspect" => json!({"ok": true, "result": {"tool": "selection"}}),
                "ui.dialog.set" if !dialog_open => json!({"ok": false, "error": "no dialog open"}),
                "ui.dialog.set" if answers_in_set && msg["params"].get("action").is_some() => {
                    let dialog = json!({"echo": {"fields": msg["params"]["fields"]}});
                    match msg["params"]["action"].as_str() {
                        Some("confirm") if refusal.is_some() => json!({"ok": false, "error": refusal}),
                        Some("confirm") => json!({"ok": true, "result": {"dialog": dialog, "result": {"echo": {}}}}),
                        _ => {
                            dialog_open = false;
                            json!({"ok": true, "result": {"dialog": dialog, "result": null}})
                        }
                    }
                }
                "ui.dialog.confirm" if refusal.is_some() => json!({"ok": false, "error": refusal}),
                m if m.starts_with("ui.dialog.") => {
                    dialog_open &= m != "ui.dialog.cancel";
                    json!({"ok": true, "result": {"echo": msg["params"]}})
                }
                _ => json!({"ok": false, "error": "nope"}),
            };
            let mut reply = reply;
            reply["id"] = msg["id"].clone();
            writeln!(out, "{reply}").unwrap();
            seen.push(msg);
        }
        seen
    });
    (addr, h)
}

#[test]
fn remote_forwards_methods() {
    let (addr, h) = fake_app();
    let mut s = Server::new(Box::new(Remote::connect(&addr).unwrap()));
    let r = call(&mut s, 1, "inspect_document", json!({}));
    assert!(text_of(&r).contains("Remote"));
    let r = call(&mut s, 2, "run_command", json!({"command": "object.group"}));
    assert!(text_of(&r).contains("object.group"), "{r}");
    let r = call(&mut s, 3, "inspect_ui", json!({}));
    assert_eq!(r["isError"], false, "{r}");
    let r = call(&mut s, 4, "open_panel", json!({"panel": "Layers"}));
    assert_eq!(r["isError"], true);
    assert!(text_of(&r).contains("nope"));
    drop(s);
    let seen = h.join().unwrap();
    let methods: Vec<&str> = seen.iter().map(|m| m["method"].as_str().unwrap()).collect();
    assert_eq!(methods, ["document.inspect", "engine.execute", "ui.inspect", "ui.set"]);
    assert_eq!(seen[1]["params"], json!({"command": "object.group", "params": {}}));
}

/// Fields and `action` go to the app in one `ui.dialog.set`, so the dialog the fields are set in is
/// the one confirmed or canceled.
#[test]
fn answer_dialog_sets_fields_in_order_and_confirms_in_one_request() {
    let (addr, h) = fake_app();
    let mut s = Server::new(Box::new(Remote::connect(&addr).unwrap()));
    let set = json!([{"field": "discard", "value": true}, {"field": "note"}]);
    let r = call(&mut s, 1, "answer_dialog", json!({"set": set, "action": "confirm"}));
    assert_eq!(r["isError"], false, "{r}");
    let reply: Value = serde_json::from_str(&text_of(&r)).unwrap();
    let fields = json!([{"field": "discard", "value": true}, {"field": "note", "value": null}]);
    assert_eq!(reply, json!({"dialog": {"echo": {"fields": fields}}, "result": {"echo": {}}}));
    let r = call(&mut s, 2, "answer_dialog", json!({"set": set}));
    assert_eq!(r["isError"], false, "{r}");
    let reply: Value = serde_json::from_str(&text_of(&r)).unwrap();
    assert_eq!(reply, json!({"dialog": {"echo": {"fields": fields}}, "result": null}));
    let r = call(&mut s, 3, "answer_dialog", json!({"action": "cancel"}));
    assert_eq!(r["isError"], false, "{r}");
    // Bad arguments are refused before anything reaches the app.
    for (i, args) in [
        json!({}),
        json!({"set": []}),
        json!({"set": {"field": "copies"}}),
        json!({"set": [{"value": 1}]}),
        json!({"set": [{"field": "copies", "value": 1}, 7]}),
        json!({"set": [{"field": "copies", "valu": 1}], "action": "confirm"}),
        json!({"set": vec![json!({"field": "copies"}); 257]}),
        json!({"action": "ok"}),
    ]
    .into_iter()
    .enumerate()
    {
        let r = call(&mut s, 10 + i as u64, "answer_dialog", args.clone());
        assert_eq!(r["isError"], true, "{args} → {r}");
    }
    drop(s);
    let seen = h.join().unwrap();
    let methods: Vec<&str> = seen.iter().map(|m| m["method"].as_str().unwrap()).collect();
    assert_eq!(methods, ["ui.dialog.set", "ui.dialog.set", "ui.dialog.cancel"]);
    assert_eq!(seen[0]["params"], json!({"fields": fields, "action": "confirm"}));
    assert_eq!(seen[1]["params"], json!({"fields": fields}));
}

/// An app whose `ui.dialog.set` predates `action` replies with the dialog alone; the dialog is
/// then confirmed with its own `ui.dialog.confirm`.
#[test]
fn answer_dialog_confirms_separately_when_the_app_set_did_not_answer() {
    let (addr, h) = fake_app_with(None, false);
    let mut s = Server::new(Box::new(Remote::connect(&addr).unwrap()));
    let set = json!([{"field": "copy", "value": true}]);
    let r = call(&mut s, 1, "answer_dialog", json!({"set": set, "action": "confirm"}));
    assert_eq!(r["isError"], false, "{r}");
    let reply: Value = serde_json::from_str(&text_of(&r)).unwrap();
    assert_eq!(reply, json!({"dialog": {"echo": {"fields": set, "action": "confirm"}}, "result": {"echo": {}}}));
    drop(s);
    let seen = h.join().unwrap();
    let methods: Vec<&str> = seen.iter().map(|m| m["method"].as_str().unwrap()).collect();
    assert_eq!(methods, ["ui.dialog.set", "ui.dialog.confirm"]);
}

/// When the app refuses the `ui.dialog.set`, the call is an error and nothing is confirmed.
#[test]
fn answer_dialog_does_not_confirm_when_the_app_refuses_the_set() {
    let (addr, h) = fake_app();
    let mut s = Server::new(Box::new(Remote::connect(&addr).unwrap()));
    let r = call(&mut s, 1, "answer_dialog", json!({"action": "cancel"}));
    assert_eq!(r["isError"], false, "{r}");
    let set = json!([{"field": "copy", "value": true}, {"field": "dy", "value": 2}]);
    let r = call(&mut s, 2, "answer_dialog", json!({"set": set, "action": "confirm"}));
    assert_eq!(r["isError"], true, "{r}");
    assert!(text_of(&r).contains("no dialog open"), "{r}");
    drop(s);
    let seen = h.join().unwrap();
    let methods: Vec<&str> = seen.iter().map(|m| m["method"].as_str().unwrap()).collect();
    assert_eq!(methods, ["ui.dialog.cancel", "ui.dialog.set"], "one set call, refused, and no confirm");
    assert_eq!(seen[1]["params"], json!({"fields": set, "action": "confirm"}));
}

/// The app refuses to confirm a plug-in's install approval for automation: the call is an error
/// with the app's reason, and nothing else is sent that could install the module.
#[test]
fn answer_dialog_reports_that_the_app_refuses_to_approve_a_plugin_install() {
    let refusal = "Only the person at the keyboard can approve installing a plug-in";
    let (addr, h) = fake_app_refusing_confirm(Some(refusal));
    let mut s = Server::new(Box::new(Remote::connect(&addr).unwrap()));
    let set = json!([{"field": "__params", "value": {"dataBase64": "AAAA"}}]);
    let r = call(&mut s, 1, "answer_dialog", json!({"set": set, "action": "confirm"}));
    assert_eq!(r["isError"], true, "{r}");
    assert!(text_of(&r).contains(refusal), "{r}");
    drop(s);
    let seen = h.join().unwrap();
    let methods: Vec<&str> = seen.iter().map(|m| m["method"].as_str().unwrap()).collect();
    assert_eq!(methods, ["ui.dialog.set"]);
}

#[test]
fn answer_dialog_needs_the_desktop_app() {
    let mut s = server();
    let r = call(&mut s, 1, "answer_dialog", json!({"action": "confirm"}));
    assert_eq!(r["isError"], true, "{r}");
    assert!(text_of(&r).contains("needs the desktop app"), "{r}");
}

/// How long a fake app waits for [`Remote`] to connect or to send, and how long a test waits for
/// a connection to end or a line to arrive. The wait only ends a test whose client or app never
/// does. A machine busy with other builds can hold a thread for seconds.
const WAIT: std::time::Duration = std::time::Duration::from_secs(20);

#[test]
fn remote_does_not_replay_when_edit_reply_is_lost() {
    let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
    listener.set_nonblocking(true).unwrap();
    let addr = listener.local_addr().unwrap().to_string();
    let h = std::thread::spawn(move || {
        let timeout = WAIT;
        let mut backend = Headless::with_document();
        let base = backend.call("document.inspect", json!({})).unwrap()["objects"].as_u64().unwrap();
        for connection in 0..2 {
            let deadline = std::time::Instant::now() + timeout;
            let stream = loop {
                match listener.accept() {
                    Ok((stream, _)) => break stream,
                    Err(e) if e.kind() == std::io::ErrorKind::WouldBlock && std::time::Instant::now() < deadline => std::thread::yield_now(),
                    Err(e) => panic!("accept failed: {e}"),
                }
            };
            stream.set_nonblocking(false).unwrap();
            stream.set_read_timeout(Some(timeout)).unwrap();
            stream.set_write_timeout(Some(timeout)).unwrap();
            let mut out = stream.try_clone().unwrap();
            let mut reader = BufReader::new(stream);
            for _ in 0..2 {
                let mut line = String::new();
                reader.read_line(&mut line).unwrap();
                let msg: Value = serde_json::from_str(&line).unwrap();
                let method = msg["method"].as_str().unwrap();
                let result = backend.call(method, msg["params"].clone()).unwrap();
                if connection == 0 {
                    assert_eq!(method, "engine.execute");
                    break;
                }
                let reply = json!({"id": msg["id"], "ok": true, "result": result});
                writeln!(out, "{reply}").unwrap();
                if method == "document.inspect" {
                    return result["objects"].as_u64().unwrap() - base;
                }
                assert_eq!(method, "engine.execute");
            }
        }
        panic!("document.inspect was not received");
    });

    let mut remote = Remote::connect(&addr).unwrap();
    let first = remote.call("engine.execute", json!({"command": "shape.rectangle", "params": {"x": 10, "y": 10, "width": 20, "height": 20}}));
    let next = remote.call("document.inspect", json!({}));
    let edits = h.join().unwrap();

    assert_eq!(edits, 1, "a lost reply must not duplicate the edit");
    let error = first.expect_err("the lost reply must report uncertainty");
    assert!(error.contains("operation may have completed"), "{error}");
    assert!(next.is_ok(), "the next call must reconnect: {next:?}");
}

#[test]
fn remote_file_tools_send_absolute_paths() {
    let (addr, h) = fake_app();
    let mut s = Server::new(Box::new(Remote::connect(&addr).unwrap()));
    let given = std::env::temp_dir().join("given.svg");
    let given = given.to_str().unwrap();
    call(&mut s, 1, "open_file", json!({"path": "rel/in.svg"}));
    call(&mut s, 2, "open_file", json!({"path": given}));
    call(&mut s, 3, "save_file", json!({"path": "rel/out.vectorcraft"}));
    call(&mut s, 4, "export", json!({"path": "rel/out.png", "scale": 2}));
    call(&mut s, 5, "export", json!({"format": "svg", "options": {"path": "rel/opt.svg"}}));
    call(&mut s, 6, "screenshot", json!({"path": "rel/shot.png"}));
    call(&mut s, 7, "run_command", json!({"command": "document.export", "params": {"path": "rel/cmd.svg"}}));
    drop(s);
    let seen = h.join().unwrap();
    let abs = |p: &str| std::path::absolute(p).unwrap().to_str().unwrap().to_string();
    let methods: Vec<&str> = seen.iter().map(|m| m["method"].as_str().unwrap()).collect();
    assert_eq!(methods, ["app.open", "app.open", "app.save", "engine.execute", "engine.execute", "ui.render", "engine.execute"]);
    assert_eq!(seen[0]["params"]["path"], abs("rel/in.svg"));
    assert_eq!(seen[1]["params"]["path"], given);
    assert_eq!(seen[2]["params"]["path"], abs("rel/out.vectorcraft"));
    assert_eq!(seen[3]["params"]["params"]["path"], abs("rel/out.png"));
    // The format still comes from the extension the agent gave: a raster export keeps `scale`.
    assert_eq!(seen[3]["params"]["params"]["scale"], 2);
    assert_eq!(seen[4]["params"]["params"]["path"], abs("rel/opt.svg"));
    assert_eq!(seen[5]["params"]["path"], abs("rel/shot.png"));
    // Command params go to the app as given.
    assert_eq!(seen[6]["params"]["params"]["path"], "rel/cmd.svg");
}

/// `p` written relative to this process's working directory, or None when the two are on
/// different drives.
fn relative_to_cwd(p: &std::path::Path) -> Option<std::path::PathBuf> {
    use std::path::Component;
    let cwd = std::env::current_dir().unwrap();
    if cwd.components().next() != p.components().next() {
        return None;
    }
    let mut rel = std::path::PathBuf::new();
    for _ in cwd.components().filter(|c| matches!(c, Component::Normal(_))) {
        rel.push("..");
    }
    for c in p.components().filter(|c| matches!(c, Component::Normal(_))) {
        rel.push(c);
    }
    Some(rel)
}

#[test]
fn headless_file_tools_take_relative_paths() {
    let dir = TempFolder::new("relative-paths");
    let mut s = server();
    let file = dir.file("relative.vectorcraft");
    let Some(rel) = relative_to_cwd(&file) else { return };
    let rel = rel.to_str().unwrap();
    assert!(std::path::Path::new(rel).is_relative());
    let r = call(&mut s, 1, "save_file", json!({"path": rel}));
    assert_eq!(r["isError"], false, "{r}");
    assert!(file.is_file());
    let r = call(&mut s, 2, "open_file", json!({"path": rel}));
    assert_eq!(r["isError"], false, "{r}");
}

#[test]
fn headless_file_tools_send_paths_as_given() {
    use std::cell::RefCell;
    use std::rc::Rc;

    struct Recording(Rc<RefCell<Vec<Value>>>);
    impl Backend for Recording {
        fn call(&mut self, _: &str, params: Value) -> Result<Value, String> {
            self.0.borrow_mut().push(params);
            Ok(json!({}))
        }
        fn has_ui(&self) -> bool {
            false
        }
        fn describe(&self) -> String {
            "test".into()
        }
    }

    let seen = Rc::new(RefCell::new(Vec::new()));
    let mut s = Server::new(Box::new(Recording(seen.clone())));
    call(&mut s, 1, "open_file", json!({"path": "rel/in.svg"}));
    call(&mut s, 2, "save_file", json!({"path": "rel/out.vectorcraft"}));
    call(&mut s, 3, "export", json!({"path": "rel/out.png"}));
    call(&mut s, 4, "export", json!({"format": "svg", "options": {"path": "rel/opt.svg"}}));
    call(&mut s, 5, "screenshot", json!({"path": "rel/shot.png"}));
    let seen = seen.borrow();
    assert_eq!(seen.len(), 5, "{seen:?}");
    assert_eq!(seen[0]["path"], "rel/in.svg");
    assert_eq!(seen[1]["path"], "rel/out.vectorcraft");
    assert_eq!(seen[2]["params"]["path"], "rel/out.png");
    assert_eq!(seen[3]["params"]["path"], "rel/opt.svg");
    assert_eq!(seen[4]["path"], "rel/shot.png");
}

#[test]
fn remote_rejects_uncorrelated_replies_without_replaying() {
    for reply in [
        json!({"ok": true, "result": {}}).to_string(),
        json!({"id": null, "ok": true, "result": {}}).to_string(),
        json!({"id": "1", "ok": true, "result": {}}).to_string(),
        json!({"id": 2, "ok": true, "result": {}}).to_string(),
        json!({"id": 2, "ok": false, "error": "unrelated error"}).to_string(),
        "not json".into(),
    ] {
        let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
        listener.set_nonblocking(true).unwrap();
        let addr = listener.local_addr().unwrap().to_string();
        let h = std::thread::spawn(move || {
            let timeout = WAIT;
            let mut seen = vec![];
            for connection in 0..2 {
                let deadline = std::time::Instant::now() + timeout;
                let stream = loop {
                    match listener.accept() {
                        Ok((stream, _)) => break stream,
                        Err(e) if e.kind() == std::io::ErrorKind::WouldBlock && std::time::Instant::now() < deadline => std::thread::yield_now(),
                        Err(e) => panic!("accept failed: {e}"),
                    }
                };
                stream.set_nonblocking(false).unwrap();
                stream.set_read_timeout(Some(timeout)).unwrap();
                stream.set_write_timeout(Some(timeout)).unwrap();
                let mut out = stream.try_clone().unwrap();
                let mut reader = BufReader::new(stream);
                let mut line = String::new();
                reader.read_line(&mut line).unwrap();
                let msg: Value = serde_json::from_str(&line).unwrap();
                seen.push(msg.clone());
                if connection == 0 {
                    writeln!(out, "{reply}").unwrap();
                    line.clear();
                    assert_eq!(reader.read_line(&mut line).unwrap(), 0, "invalid reply must close the connection without another request");
                } else {
                    writeln!(out, "{}", json!({"id": msg["id"], "ok": true, "result": {"objects": 1}})).unwrap();
                }
            }
            seen
        });
        let mut remote = Remote::connect(&addr).unwrap();
        let first = remote.call("engine.execute", json!({"command": "shape.rectangle", "params": {"width": 20, "height": 20}}));
        let next = remote.call("document.inspect", json!({}));
        drop(remote);
        let seen = h.join().unwrap();
        let error = first.expect_err("an uncorrelated reply cannot confirm the edit");
        assert!(error.contains("operation may have completed"), "{error}");
        assert_eq!(next.unwrap(), json!({"objects": 1}));
        assert_eq!(seen.len(), 2);
        assert_eq!(seen[0]["method"], "engine.execute");
        assert_eq!(seen[1]["method"], "document.inspect", "the edit must not be replayed");
        assert_eq!(seen[0]["id"], 1);
        assert_eq!(seen[1]["id"], 2);
    }
}

#[test]
fn remote_rejects_a_reply_with_a_mismatched_id_and_reconnects() {
    // A fake control server that answers the first request with another call's id, then serves
    // correctly: the client must reject the stray reply, drop the connection, and reconnect.
    let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
    let addr = listener.local_addr().unwrap().to_string();
    let h = std::thread::spawn(move || {
        let mut seen = vec![];
        for stream in listener.incoming().flatten().take(2) {
            stream.set_read_timeout(Some(WAIT)).unwrap();
            let mut out = stream.try_clone().unwrap();
            let mut reader = BufReader::new(stream);
            let mut line = String::new();
            if reader.read_line(&mut line).unwrap_or(0) == 0 {
                continue;
            }
            let msg: Value = serde_json::from_str(&line).unwrap();
            // First connection: somebody else's answer. Later ones: the right echo.
            let id = if seen.is_empty() { json!(msg["id"].as_u64().unwrap_or(0) + 1000) } else { msg["id"].clone() };
            let reply = json!({"ok": true, "result": {"echo": msg["params"]}, "id": id});
            writeln!(out, "{reply}").unwrap();
            seen.push(msg);
        }
        seen
    });
    let mut remote = Remote::connect(&addr).unwrap();
    let err = remote.call("engine.execute", json!({"command": "shape.rectangle", "params": {}})).expect_err("a stray reply must not be trusted");
    assert!(err.contains("mismatch"), "{err}");
    let good = remote.call("engine.execute", json!({"command": "shape.rectangle", "params": {}}));
    assert!(good.is_ok(), "the next call reconnects: {good:?}");
    drop(remote);
    let seen = h.join().unwrap();
    assert_eq!(seen.len(), 2, "one request per connection: {seen:?}");
    assert_ne!(seen[0]["id"], seen[1]["id"]);
}

/// The app's refusals carry no id (a 17th connection gets one and is closed). The call reports the
/// app's reason, not a mismatch, says nothing ran, and the next call starts a fresh connection.
#[test]
fn remote_reports_the_reason_of_a_refusal_without_an_id() {
    let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
    let addr = listener.local_addr().unwrap().to_string();
    let h = std::thread::spawn(move || {
        let (mut refused, _) = listener.accept().unwrap();
        // The request is read first, so the refusal answers it and the close is orderly.
        refused.set_read_timeout(Some(WAIT)).unwrap();
        let _ = BufReader::new(refused.try_clone().unwrap()).read_line(&mut String::new());
        writeln!(refused, "{}", json!({"ok": false, "error": "too many control connections"})).unwrap();
        drop(refused);
        let (stream, _) = listener.accept().unwrap();
        stream.set_read_timeout(Some(WAIT)).unwrap();
        let mut out = stream.try_clone().unwrap();
        let mut line = String::new();
        BufReader::new(stream).read_line(&mut line).unwrap();
        let msg: Value = serde_json::from_str(&line).unwrap();
        writeln!(out, "{}", json!({"ok": true, "result": 7, "id": msg["id"]})).unwrap();
    });
    let mut remote = Remote::connect(&addr).unwrap();
    let err = remote.call("document.inspect", json!({})).expect_err("a refusal is an error");
    assert!(err.contains("too many control connections") && err.contains("nothing was run"), "{err}");
    assert!(!err.contains("mismatch") && !err.contains("may have completed"), "{err}");
    assert_eq!(remote.call("document.inspect", json!({})), Ok(json!(7)), "the next call reconnects");
    h.join().unwrap();
}

/// How long a refused connect to the app may take: longer than the 800 ms connect timeout of
/// [`Remote`], and shorter than the 2 s a refused local connect takes on Windows without one.
const FAILED_CONNECT: std::time::Duration = std::time::Duration::from_millis(1500);

/// Runs `attempt` until one run takes less than `bound`, at most five times, and returns how long
/// each run took. A machine busy with other builds can hold the test's thread for seconds in one
/// run; a connect that waits longer than its own timeout is slow in every run.
fn runs_until_faster_than(bound: std::time::Duration, mut attempt: impl FnMut()) -> Vec<std::time::Duration> {
    let mut took = Vec::new();
    while took.len() < 5 {
        let start = std::time::Instant::now();
        attempt();
        took.push(start.elapsed());
        if took.last() < Some(&bound) {
            break;
        }
    }
    took
}

#[test]
fn remote_connect_fails_fast() {
    // A port this test holds, bound but not listening, so connecting to it is refused. (A port
    // bound and dropped could be taken by another process or test before the connect.)
    let held = socket2::Socket::new(socket2::Domain::IPV4, socket2::Type::STREAM, None).unwrap();
    held.bind(&std::net::SocketAddr::from(([127, 0, 0, 1], 0)).into()).unwrap();
    let port = held.local_addr().unwrap().as_socket().unwrap().port();
    let took = runs_until_faster_than(FAILED_CONNECT, || assert!(Remote::connect(&format!("127.0.0.1:{port}")).is_err()));
    assert!(took.last() < Some(&FAILED_CONNECT), "took {took:?}");
    // Nothing else could listen there meanwhile.
    assert!(std::net::TcpListener::bind(format!("127.0.0.1:{port}")).is_err());
    drop(held);
}

fn reusable_listener(addr: std::net::SocketAddr) -> std::net::TcpListener {
    let socket = socket2::Socket::new(socket2::Domain::IPV4, socket2::Type::STREAM, None).unwrap();
    socket.set_reuse_address(true).unwrap();
    socket.bind(&addr.into()).unwrap();
    socket.listen(1).unwrap();
    socket.into()
}

fn wait_for_remote_eof(remote: &Remote) {
    let probe = remote.connection_probe();
    probe.set_read_timeout(Some(WAIT)).unwrap();
    assert_eq!(probe.peek(&mut [0]).unwrap(), 0);
}

#[test]
fn remote_edit_succeeds_once_when_app_restarts() {
    let listener = reusable_listener(std::net::SocketAddr::from(([127, 0, 0, 1], 0)));
    let addr = listener.local_addr().unwrap();
    let mut remote = Remote::connect(&addr.to_string()).unwrap();
    let (old_connection, _) = listener.accept().unwrap();
    let old_handle = std::thread::spawn(move || {
        let timeout = WAIT;
        old_connection.set_read_timeout(Some(timeout)).unwrap();
        old_connection.set_write_timeout(Some(timeout)).unwrap();
        let mut reader = BufReader::new(old_connection.try_clone().unwrap());
        let mut writer = old_connection;
        let mut line = String::new();
        reader.read_line(&mut line).unwrap();
        let message: Value = serde_json::from_str(&line).unwrap();
        assert_eq!(message["method"], "document.inspect");
        let result = Headless::with_document().call("document.inspect", message["params"].clone()).unwrap();
        writeln!(writer, "{}", json!({"id": message["id"], "ok": true, "result": result})).unwrap();
        writer
    });
    let inspected = remote.call("document.inspect", json!({})).unwrap();
    assert!(inspected["objects"].as_u64().unwrap() > 0);
    drop(old_handle.join().unwrap());
    drop(listener);
    wait_for_remote_eof(&remote);

    let listener = reusable_listener(addr);
    listener.set_nonblocking(true).unwrap();
    let handle = std::thread::spawn(move || {
        let timeout = WAIT;
        let deadline = std::time::Instant::now() + timeout;
        let stream = loop {
            match listener.accept() {
                Ok((stream, _)) => break stream,
                Err(error) if error.kind() == std::io::ErrorKind::WouldBlock => {
                    if std::time::Instant::now() >= deadline {
                        return (0, 0);
                    }
                    std::thread::yield_now();
                }
                Err(error) => panic!("accept failed: {error}"),
            }
        };
        stream.set_nonblocking(false).unwrap();
        stream.set_read_timeout(Some(timeout)).unwrap();
        stream.set_write_timeout(Some(timeout)).unwrap();
        let mut writer = stream.try_clone().unwrap();
        let mut backend = Headless::with_document();
        let base = backend.call("document.inspect", json!({})).unwrap()["objects"].as_u64().unwrap();
        let mut requests = 0;
        for line in BufReader::new(stream).lines() {
            let message: Value = serde_json::from_str(&line.unwrap()).unwrap();
            let result = backend.call(message["method"].as_str().unwrap(), message["params"].clone()).unwrap();
            requests += 1;
            writeln!(writer, "{}", json!({"id": message["id"], "ok": true, "result": result})).unwrap();
        }
        let objects = backend.call("document.inspect", json!({})).unwrap()["objects"].as_u64().unwrap();
        (requests, objects - base)
    });

    let edit = remote.call("engine.execute", json!({"command": "shape.rectangle", "params": {"x": 10, "y": 10, "width": 20, "height": 20}}));
    drop(remote);
    let (requests, edits) = handle.join().unwrap();

    assert!(edit.is_ok(), "the first edit after restart must succeed: {edit:?}");
    assert_eq!((requests, edits), (1, 1), "the edit must be sent and applied exactly once");
}

/// A connection holding a line nobody asked for (an app that says why it closes an idle
/// connection) is not used: the call goes out once, on a fresh connection, and succeeds.
#[test]
fn remote_does_not_send_on_a_connection_with_a_stray_line() {
    let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
    let addr = listener.local_addr().unwrap().to_string();
    let mut remote = Remote::connect(&addr).unwrap();
    let (mut stale, _) = listener.accept().unwrap();
    stale.set_read_timeout(Some(WAIT)).unwrap();
    writeln!(stale, "{}", json!({"ok": false, "error": "idle"})).unwrap();
    let probe = remote.connection_probe();
    probe.set_read_timeout(Some(WAIT)).unwrap();
    assert_eq!(probe.peek(&mut [0]).unwrap(), 1, "the stray line arrived");
    drop(probe);
    let h = std::thread::spawn(move || {
        let (stream, _) = listener.accept().unwrap();
        stream.set_read_timeout(Some(WAIT)).unwrap();
        let mut out = stream.try_clone().unwrap();
        let mut line = String::new();
        BufReader::new(stream).read_line(&mut line).unwrap();
        let msg: Value = serde_json::from_str(&line).unwrap();
        writeln!(out, "{}", json!({"ok": true, "result": 7, "id": msg["id"]})).unwrap();
    });
    assert_eq!(remote.call("document.inspect", json!({})), Ok(json!(7)));
    h.join().unwrap();
    // Nothing was written to the stale connection: it ends (or is reset, its line unread) without a byte.
    let mut rest = String::new();
    assert_eq!(BufReader::new(stale).read_line(&mut rest).unwrap_or(0), 0, "{rest}");
}

#[test]
fn remote_call_reports_nothing_sent_when_reconnect_is_refused() {
    // A remote whose connection the app closed, and the app's port bound but not listening.
    let closed_remote = || {
        let listener = reusable_listener(std::net::SocketAddr::from(([127, 0, 0, 1], 0)));
        let addr = listener.local_addr().unwrap();
        let remote = Remote::connect(&addr.to_string()).unwrap();
        let (old_connection, _) = listener.accept().unwrap();
        drop(old_connection);
        drop(listener);
        wait_for_remote_eof(&remote);
        let held = socket2::Socket::new(socket2::Domain::IPV4, socket2::Type::STREAM, None).unwrap();
        held.set_reuse_address(true).unwrap();
        held.bind(&addr.into()).unwrap();
        (remote, held)
    };

    // Each run finds the old connection closed and connects again; only the call is timed.
    let mut errors = Vec::new();
    let mut took = Vec::new();
    while took.len() < 5 {
        let (mut remote, _held) = closed_remote();
        let start = std::time::Instant::now();
        errors.push(remote.call("engine.execute", json!({"command": "shape.rectangle", "params": {"width": 20, "height": 20}})).unwrap_err());
        took.push(start.elapsed());
        if took.last() < Some(&FAILED_CONNECT) {
            break;
        }
    }

    assert!(took.last() < Some(&FAILED_CONNECT), "took {took:?}");
    for error in errors {
        assert!(error.contains("not reachable"), "{error}");
        assert!(error.contains("nothing was sent"), "{error}");
        assert!(!error.contains("operation may have completed"), "{error}");
    }
}

#[test]
fn headless_backend_methods() {
    let mut h = Headless::new();
    assert!(h.call("document.inspect", json!({})).is_err(), "no document yet");
    h.call("engine.execute", json!({"command": "file.new", "params": {"width": 300, "height": 200}})).unwrap();
    let r = h.call("ui.render", json!({"scale": 2})).unwrap();
    assert_eq!((r["width"].as_u64(), r["height"].as_u64()), (Some(600), Some(400)));
    let cmds = h.call("engine.commands", json!({})).unwrap();
    assert!(cmds.as_array().unwrap().iter().any(|c| c["id"] == "file.export"));
    let listed = h.call("ui.tool.list", json!({})).unwrap();
    let groups = listed.as_array().unwrap();
    for tool in vectorcraft_tools::addons::GROUPS.iter().flat_map(|group| group.iter()) {
        let matching: Vec<_> = groups.iter().flat_map(|group| group.as_array().unwrap()).filter(|entry| entry["id"] == tool.id).collect();
        assert_eq!(matching.len(), 1, "{}", tool.id);
        assert_eq!(matching[0]["label"], tool.label);
        assert!(matching[0]["shortcut"].is_null());
        let selected = h.call("ui.tool.select", json!({"tool": tool.id})).unwrap();
        assert_eq!(selected["tool"], tool.id);
    }
    // The Trim at Crossings Tool is listed once, in the Scissors tool's group after the Knife.
    let trim = vectorcraft_tools::addons::trim::ID;
    let with_trim: Vec<_> =
        groups.iter().map(|group| group.as_array().unwrap()).filter(|group| group.iter().any(|entry| entry["id"] == trim)).collect();
    assert_eq!(with_trim.len(), 1, "{trim} is in one group");
    let ids: Vec<_> = with_trim[0].iter().map(|entry| entry["id"].clone()).collect();
    let knife = ids.iter().position(|id| *id == "knife").expect("the Knife in the same group");
    assert!(ids.contains(&json!("scissors")), "the Scissors tool's group: {ids:?}");
    assert_eq!(ids.get(knife + 1), Some(&json!(trim)));
    assert_eq!(ids.iter().filter(|id| **id == trim).count(), 1);
    let entry = with_trim[0].iter().find(|entry| entry["id"] == trim).unwrap();
    assert_eq!(entry["label"], "Trim at Crossings Tool");
    assert!(entry["shortcut"].is_null());
    assert_eq!(h.call("ui.tool.select", json!({"tool": trim})).unwrap()["tool"], trim);
    // The Enclosing Lasso Tool is listed once, in the Lasso tool's group after the Lasso.
    let enclosing = vectorcraft_tools::addons::enclosing_lasso::ID;
    let with_enclosing: Vec<_> =
        groups.iter().map(|group| group.as_array().unwrap()).filter(|group| group.iter().any(|entry| entry["id"] == enclosing)).collect();
    assert_eq!(with_enclosing.len(), 1, "{enclosing} is in one group");
    let ids: Vec<_> = with_enclosing[0].iter().map(|entry| entry["id"].clone()).collect();
    assert_eq!(ids, [json!("lasso"), json!(enclosing)]);
    assert_eq!(with_enclosing[0][1]["label"], "Enclosing Lasso Tool");
    assert!(with_enclosing[0][1]["shortcut"].is_null());
    assert_eq!(h.call("ui.tool.select", json!({"tool": enclosing})).unwrap()["tool"], enclosing);
    let polygonal = groups.iter().flat_map(|group| group.as_array().unwrap()).find(|entry| entry["id"] == "polygonalLasso");
    assert_eq!(polygonal.map(|entry| entry["label"].clone()), Some(json!("Polygonal Lasso Tool")));
    assert!(h.call("ui.resize", json!({})).is_err());
}

/// The headless key, pointer and screenshot calls refuse a value of the wrong type and change
/// nothing; omitting it or giving null keeps its default.
#[test]
fn headless_methods_refuse_params_of_the_wrong_type() {
    let mut h = Headless::new();
    h.call("engine.execute", json!({"command": "file.new", "params": {"width": 300, "height": 200}})).unwrap();
    h.call("ui.tool.select", json!({"tool": "rectangle"})).unwrap();
    let before = h.call("document.json", json!({})).unwrap();
    let drag = |mods: Value| json!({"events": [{"kind": "down", "x": 10, "y": 10}, {"kind": "drag", "x": 60, "y": 60, "mods": mods}, {"kind": "up", "x": 60, "y": 60}]});
    let after_rect = |last: Value| json!({"events": [{"kind": "down", "x": 10, "y": 10}, {"kind": "drag", "x": 60, "y": 60}, {"kind": "up", "x": 60, "y": 60}, last]});
    for (method, params, key) in [
        ("ui.render", json!({"artboard": "0"}), "artboard"),
        ("ui.render", json!({"scale": "2"}), "scale"),
        ("ui.render", json!({"traceViews": 1}), "traceViews"),
        ("ui.screenshot", json!({"data": "yes", "path": "x.png"}), "data"),
        ("ui.key", json!({"key": "Delete", "shift": "true"}), "shift"),
        ("ui.pointer", drag(json!({"shift": "yes"})), "mods"),
        ("ui.pointer", json!({"mods": 1, "events": []}), "mods"),
        ("ui.pointer", drag(json!([true])), "mods"),
        // A whole rectangle comes before the malformed event, and none of it is drawn.
        ("ui.pointer", after_rect(json!({"kind": "move", "x": "bad", "y": 0})), "x"),
        ("ui.pointer", after_rect(json!({"kind": "wiggle", "x": 0, "y": 0})), "wiggle"),
        ("ui.pointer", after_rect(json!({"kind": "move", "x": 1, "y": 1, "pressure": "bad"})), "pressure"),
        ("ui.pointer", after_rect(json!({"kind": "move", "x": 1, "y": 1, "holdMs": true})), "holdMs"),
        ("ui.pointer", after_rect(json!({"kind": "move", "x": 1, "y": 1, "tiltX": "5"})), "tiltX"),
        ("ui.pointer", after_rect(json!({"kind": "move", "x": 1, "y": 1, "rotation": [0]})), "rotation"),
        ("ui.pointer", after_rect(json!({"kind": "move", "x": 1, "y": 1, "space": "screeen"})), "space"),
    ] {
        let e = h.call(method, params.clone()).unwrap_err();
        assert!(e.contains(&format!("`{key}`")), "{method} {params}: {e}");
    }
    assert_eq!(h.call("document.json", json!({})).unwrap(), before, "a refused call changes nothing");
    let r = h.call("ui.render", json!({"artboard": null, "scale": null, "traceViews": null})).unwrap();
    assert_eq!((r["width"].as_u64(), r["height"].as_u64()), (Some(300), Some(200)));
    h.call("ui.pointer", drag(Value::Null)).unwrap();
    assert_ne!(h.call("document.json", json!({})).unwrap(), before, "null mods keep the packet's");
}

// ---------- workflow tools ----------

fn id_of(result: &Value) -> u64 {
    let v: Value = serde_json::from_str(&text_of(result)).unwrap();
    v["id"].as_u64().unwrap_or_else(|| panic!("no id in {v}"))
}

fn doc_json(s: &mut Server) -> Value {
    let r = call(s, 900, "run_command", json!({"command": "document.json"}));
    serde_json::from_str(&text_of(&r)).unwrap()
}

#[test]
fn transform_refused_by_a_later_operation_changes_nothing() {
    let mut s = server();
    let count = |s: &mut Server| -> u64 {
        let r = call(s, 90, "inspect_document", json!({}));
        serde_json::from_str::<Value>(&text_of(&r)).unwrap()["objects"].as_u64().unwrap()
    };
    let base = count(&mut s);
    let r = id_of(&call(&mut s, 1, "draw_shape", json!({"shape": "rectangle", "x": 100, "y": 100, "width": 100, "height": 50})));
    let before = doc_json(&mut s);
    for (id, args, says) in [
        (2, json!({"ids": [r], "scale": 50, "shear": 90}), "object.shear"),
        (3, json!({"ids": [r], "dx": 10, "rotate": 30, "shear": -270}), "object.shear"),
        (4, json!({"ids": [r], "scale": 50, "shear": 400}), "object.shear"),
        (5, json!({"ids": [r], "scale": 50, "reflect": "diagonal"}), "object.reflect"),
        (6, json!({"ids": [r], "dx": 10, "rotate": 30, "scaleX": 0}), "object.scale"),
    ] {
        let t = call(&mut s, id, "transform", args.clone());
        assert_eq!(t["isError"], true, "{args}: {t}");
        assert!(text_of(&t).contains(says), "{args}: {t}");
        assert_eq!(doc_json(&mut s), before, "{args}");
    }
    // No undo step was recorded: one undo removes the rectangle.
    assert_eq!(call(&mut s, 7, "undo", json!({}))["isError"], false);
    assert_eq!(count(&mut s), base, "one undo removes the rectangle");
}

#[test]
fn transform_rejects_a_mistyped_reflect_copy_or_origin_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);
    let cases = [
        ("object.reflect", json!({"dx": 10, "reflect": 42, "copy": true})),
        ("object.reflect", json!({"dx": 10, "reflect": true})),
        ("copy", json!({"dx": 10, "copy": "yes"})),
        ("origin", json!({"dx": 10, "rotate": 30, "origin": "center", "copy": true})),
        ("origin", json!({"dx": 10, "rotate": 30, "origin": [5]})),
        ("origin", json!({"dx": 10, "rotate": 30, "origin": [5, "a"]})),
    ];
    for (index, (says, 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.clone());
        assert_eq!(rejected["isError"], true, "{args}: {rejected}");
        assert!(text_of(&rejected).contains(says), "{args}: {rejected}");
        // The document, the selection and the undo history are as they were.
        assert_eq!(inspect(&mut s, request_id + 1), before, "{args}");
    }
    // A null `reflect`, `copy` or `origin` is left out.
    let applied = call(&mut s, 20, "transform", json!({"ids": [second], "dx": 10, "reflect": null, "copy": null, "origin": null}));
    assert_eq!(applied["isError"], false, "{applied}");
    let applied: Value = serde_json::from_str(&text_of(&applied)).unwrap();
    assert_eq!(applied["applied"], json!(["move"]));
    let applied = call(&mut s, 21, "transform", json!({"ids": [second], "rotate": 90, "origin": null}));
    assert_eq!(applied["isError"], false, "{applied}");
    // The schema allows null for every argument but `ids`.
    let tools = tool_definitions();
    let props = &tools.iter().find(|t| t["name"] == "transform").unwrap()["inputSchema"]["properties"];
    for key in ["dx", "dy", "rotate", "scale", "scaleX", "scaleY", "shear", "reflect", "copy", "origin"] {
        assert!(props[key]["anyOf"].as_array().is_some_and(|a| a.contains(&json!({"type": "null"}))), "{key}: {}", props[key]);
    }
}

#[test]
fn add_text_rejects_invalid_path_effects_before_editing() {
    let mut s = server();
    let path = id_of(&call(&mut s, 1, "draw_path", json!({"d": "M40 400 C150 300 250 500 360 400 Z"})));
    let before = call(&mut s, 2, "doc_inspect", json!({}));
    for (args, message) in [
        (json!({"text": "Type", "path": path, "pathEffect": "not-an-effect"}), "not-an-effect"),
        (json!({"text": "Type", "path": path, "pathEffect": 42}), "must be a string"),
        (json!({"text": "Type", "path": path, "mode": "area", "pathEffect": "skew"}), "requires type on a path"),
        (json!({"text": "Type", "x": 10, "y": 20, "pathEffect": "skew"}), "requires type on a path"),
    ] {
        let reply = call(&mut s, 3, "add_text", args);
        assert_eq!(reply["isError"], true, "{reply}");
        assert!(text_of(&reply).contains(message), "{reply}");
        assert_eq!(call(&mut s, 4, "doc_inspect", json!({})), before, "path, selection and history must stay intact");
    }
    let made = call(&mut s, 5, "add_text", json!({"text": "Type", "path": path, "pathEffect": "skew"}));
    assert_eq!(made["isError"], false, "{made}");
    assert!(doc_json(&mut s).to_string().contains("\"pathEffect\":\"skew\""));
}

#[test]
fn text_effect_pathfinder_transform_graph_and_wrap_tools() {
    let mut s = server();
    // Text: point, area and on a path with an effect.
    let t = id_of(&call(&mut s, 1, "add_text", json!({"text": "Hello", "x": 50, "y": 60, "size": 24})));
    assert!(t > 0);
    let area = id_of(&call(&mut s, 2, "add_text", json!({"text": "word ".repeat(80), "x": 40, "y": 100, "width": 300, "height": 200})));
    let p = call(&mut s, 3, "draw_path", json!({"d": "M40 400 C150 300 250 500 360 400"}));
    let pid = id_of(&p);
    let ot = id_of(&call(&mut s, 4, "add_text", json!({"text": "On a wave", "path": pid, "mode": "onPath", "pathEffect": "skew"})));
    let d = doc_json(&mut s);
    assert!(d.to_string().contains("\"pathEffect\":\"skew\""), "{ot}");
    // Effects: catalogue, then a drop shadow on the selection.
    let cat = call(&mut s, 5, "apply_effect", json!({}));
    assert!(text_of(&cat).contains("stylize.dropShadow"));
    let r1 = id_of(&call(&mut s, 6, "draw_shape", json!({"shape": "rectangle", "x": 400, "y": 50, "width": 100, "height": 100})));
    let r2 = id_of(&call(&mut s, 7, "draw_shape", json!({"shape": "rectangle", "x": 450, "y": 100, "width": 100, "height": 100})));
    let fx = call(&mut s, 8, "apply_effect", json!({"effect": "stylize.dropShadow", "params": {"x": 4}, "ids": [r2]}));
    assert!(fx.get("isError").is_none_or(|e| e == false), "{fx}");
    // Pathfinder unite over two ids.
    let u = call(&mut s, 9, "pathfinder", json!({"operation": "unite", "ids": [r1, r2]}));
    assert!(text_of(&u).contains("ids"), "{u}");
    // Transform: move + rotate on the selection.
    let tr = call(&mut s, 10, "transform", json!({"dx": 10, "rotate": 45}));
    assert!(text_of(&tr).contains("rotate"));
    let bad = call(&mut s, 11, "transform", json!({}));
    assert_eq!(bad["isError"], true);
    // Graph.
    let g = id_of(&call(&mut s, 12, "create_graph", json!({"type": "pie", "x": 50, "y": 450, "width": 200, "height": 150, "csv": ",A,B\nX,1,3"})));
    assert!(doc_json(&mut s).to_string().contains("\"kind\":\"pie\""), "{g}");
    // Text wrap: a circle over the area type.
    let c = id_of(&call(&mut s, 13, "draw_shape", json!({"shape": "ellipse", "x": 40, "y": 120, "width": 120, "height": 120})));
    let w = call(&mut s, 14, "text_wrap", json!({"ids": [c], "offset": 8}));
    assert!(w.get("isError").is_none_or(|e| e == false), "{w}");
    let d = doc_json(&mut s);
    let ds = d.to_string();
    // The wrap object carries the options and the area type resolved its shape.
    assert!(ds.contains("\"wrap\":{\"invert\":false,\"offset\":8.0}") && ds.contains("\"wrap\":[{\"invert\":false,\"offset\":8.0"), "{area}");
    // An offset outside -1000 to 1000 pt, an invert or a release that is not true or false is an
    // error, with release too, and the wrap and the selection stay as they were.
    let r = call(&mut s, 20, "run_command", json!({"command": "select.set", "params": {"ids": [g]}}));
    assert_eq!(r["isError"], false, "{r}");
    for (id, args) in [
        (21, json!({"ids": [c], "offset": 5000})),
        (22, json!({"ids": [c], "offset": "8"})),
        (23, json!({"ids": [c], "invert": "true"})),
        (24, json!({"ids": [c], "release": "true"})),
        (25, json!({"ids": [c], "release": true, "offset": -5000})),
    ] {
        let r = call(&mut s, id, "text_wrap", args.clone());
        assert_eq!(r["isError"], true, "{args}: {r}");
        assert!(doc_json(&mut s).to_string().contains("\"wrap\":{\"invert\":false,\"offset\":8.0}"), "{args}");
    }
    // The graph is still the selection: a wrap made without ids goes on it, not on the circle.
    let r = call(&mut s, 26, "text_wrap", json!({"offset": 9}));
    assert_eq!(r["isError"], false, "{r}");
    let made: Value = serde_json::from_str(&text_of(&r)).unwrap();
    assert_eq!(made["ids"], json!([g]), "{r}");
    call(&mut s, 27, "text_wrap", json!({"release": true}));
    call(&mut s, 15, "text_wrap", json!({"ids": [c], "release": true}));
    assert!(!doc_json(&mut s).to_string().contains("\"offset\":8.0"));
}

#[test]
fn oversized_screenshot_and_png_export_are_errors() {
    // 16383 pt at the maximum scale (16) is 262 128 px a side: an error result, not a crashed server.
    let dir = TempFolder::new("oversized-export");
    let mut s = server();
    let r = call(&mut s, 1, "run_command", json!({"command": "file.new", "params": {"width": 16383, "height": 16383}}));
    assert_eq!(r["isError"], false, "{r}");
    let r = call(&mut s, 2, "screenshot", json!({"scale": 16}));
    assert_eq!(r["isError"], true, "{r}");
    assert!(text_of(&r).contains("pixels"), "{r}");
    let path = dir.file("huge.png");
    let r = call(&mut s, 3, "export", json!({"path": path.to_str().unwrap(), "scale": 16}));
    assert_eq!(r["isError"], true, "{r}");
    assert!(!path.exists());
    // The session is still alive and a small screenshot works.
    let r = call(&mut s, 4, "screenshot", json!({"scale": 0.01}));
    assert_eq!(r["isError"], false, "{r}");
}

/// Preferences reach agents' gestures (#394): `prefs.set` Object Selection by Path Only, then a
/// click inside a filled square selects nothing; Command Click to Select Objects Behind, a
/// Cmd-click selects the square underneath.
#[test]
fn selection_preferences_apply_to_pointer_gestures() {
    let mut s = server();
    let square = |s: &mut Server, id: u64, x: u64| {
        let r = call(s, id, "draw_shape", json!({"shape": "rectangle", "x": x, "y": 100, "width": 100, "height": 100, "fill": "#ff0000"}));
        serde_json::from_str::<Value>(&text_of(&r)).unwrap()["id"].as_u64().unwrap()
    };
    let (back, front) = (square(&mut s, 1, 100), square(&mut s, 2, 150));
    let click = |s: &mut Server, id: u64, x: f64, mods: Value| {
        let events = json!([{"kind": "down", "x": x, "y": 125}, {"kind": "up", "x": x, "y": 125}]);
        let r = call(s, id, "pointer_gesture", json!({"tool": "selection", "events": events, "mods": mods}));
        assert_eq!(r["isError"], false, "{r}");
        serde_json::from_str::<Value>(&text_of(&r)).unwrap()["selection"].clone()
    };
    assert_eq!(click(&mut s, 3, 175.0, json!({})), json!([front]));
    assert_eq!(click(&mut s, 4, 175.0, json!({"cmd": true})), json!([back]), "Cmd-click selects behind");
    let r = call(&mut s, 5, "run_command", json!({"command": "prefs.set", "params": {"key": "objectSelectionByPathOnly", "value": true}}));
    assert_eq!(r["isError"], false, "{r}");
    assert_eq!(click(&mut s, 6, 225.0, json!({})), json!([]), "path only: the fill doesn't select");
    assert_eq!(click(&mut s, 7, 250.0, json!({})), json!([front]), "the path does");
}

#[test]
fn pointer_gesture_forwards_backquote_on_each_event() {
    let mut s = server();
    let made = call(&mut s, 1, "draw_shape", json!({"shape": "rectangle", "x": 100, "y": 100, "width": 50, "height": 50, "fill": "#ff0000"}));
    let id = serde_json::from_str::<Value>(&text_of(&made)).unwrap()["id"].as_u64().unwrap();
    let selected = call(&mut s, 2, "run_command", json!({"command": "select.set", "params": {"ids": [id]}}));
    assert_eq!(selected["isError"], false, "{selected}");
    let drag = call(
        &mut s,
        3,
        "pointer_gesture",
        json!({"tool": "selection", "backquote": true, "events": [
            {"kind": "down", "x": 125, "y": 125},
            {"kind": "drag", "x": 145, "y": 125},
            {"kind": "up", "x": 145, "y": 125}
        ]}),
    );
    assert_eq!(drag["isError"], false, "{drag}");
    let inspected = call(&mut s, 4, "inspect_document", json!({}));
    let doc = serde_json::from_str::<Value>(&text_of(&inspected)).unwrap();
    let rect = doc["layers"][0]["children"].as_array().unwrap().iter().find(|n| n["id"] == id).unwrap();
    assert_eq!(rect["bounds"]["x"], 100.0, "{doc}");
}

/// The documented Rotate View calls reach the desktop control methods, preserving document
/// coordinates and modifiers, and a successful null reset remains a successful MCP response.
#[test]
fn rotate_view_calls_forward_to_desktop_control() {
    use std::cell::RefCell;
    use std::rc::Rc;

    struct Recording(Rc<RefCell<Vec<(String, Value)>>>);
    impl Backend for Recording {
        fn call(&mut self, method: &str, params: Value) -> Result<Value, String> {
            self.0.borrow_mut().push((method.into(), params));
            Ok(Value::Null)
        }
        fn has_ui(&self) -> bool {
            true
        }
        fn describe(&self) -> String {
            "desktop control contract".into()
        }
    }

    let seen = Rc::new(RefCell::new(Vec::new()));
    let mut s = Server::new(Box::new(Recording(seen.clone())));
    let selected = call(&mut s, 1, "select_tool", json!({"tool": "rotateView"}));
    let turned = call(
        &mut s,
        2,
        "pointer_gesture",
        json!({"events": [
        {"kind": "down", "x": 300, "y": 150}, {"kind": "drag", "x": 200, "y": 250}, {"kind": "up", "x": 200, "y": 250}
    ], "mods": {"shift": true}}),
    );
    let reset = call(&mut s, 3, "command_run", json!({"id": "view.rotateReset", "params": {}}));
    for reply in [&selected, &turned, &reset] {
        assert_eq!(reply["isError"], false, "{reply}");
    }
    assert_eq!(text_of(&reset), "null");
    assert_eq!(
        *seen.borrow(),
        vec![
            ("ui.tool.select".into(), json!({"tool": "rotateView"})),
            (
                "ui.pointer".into(),
                json!({"events": [
            {"kind": "down", "x": 300, "y": 150, "space": "doc"},
            {"kind": "drag", "x": 200, "y": 250, "space": "doc"},
            {"kind": "up", "x": 200, "y": 250, "space": "doc"}
        ], "mods": {"shift": true}})
            ),
            ("engine.execute".into(), json!({"command": "view.rotateReset", "params": {}}))
        ]
    );
}

/// A `backquote` that is not a boolean is an error, and none of the gesture's events runs.
#[test]
fn pointer_gesture_rejects_a_backquote_that_is_not_a_boolean() {
    let mut s = server();
    let made = call(&mut s, 1, "draw_shape", json!({"shape": "rectangle", "x": 100, "y": 100, "width": 50, "height": 50, "fill": "#ff0000"}));
    let id = serde_json::from_str::<Value>(&text_of(&made)).unwrap()["id"].as_u64().unwrap();
    let drag = |top: Option<Value>, first: Option<Value>| {
        let mut down = json!({"kind": "down", "x": 300, "y": 300});
        if let Some(b) = first {
            down["backquote"] = b;
        }
        let mut a = json!({"tool": "rectangle", "events": [down, {"kind": "drag", "x": 350, "y": 350}, {"kind": "up", "x": 350, "y": 350}]});
        if let Some(b) = top {
            a["backquote"] = b;
        }
        a
    };
    for (n, args) in [drag(Some(json!("true")), None), drag(None, Some(json!(1)))].into_iter().enumerate() {
        let r = call(&mut s, 2 + n as u64, "pointer_gesture", args);
        assert_eq!(r["isError"], true, "{r}");
        assert!(text_of(&r).contains("`backquote` must be a boolean"), "{r}");
    }
    let inspected = call(&mut s, 4, "inspect_document", json!({}));
    let doc = serde_json::from_str::<Value>(&text_of(&inspected)).unwrap();
    let children = doc["layers"][0]["children"].as_array().unwrap();
    assert_eq!(children.iter().map(|n| n["id"].as_u64()).collect::<Vec<_>>(), vec![Some(id)], "{doc}");
}

/// A Shift-drag marquee reaches agents (#483): `pointer_gesture` with `mods.shift` toggles the
/// objects it reaches, so a selected one leaves the selection and an unselected one joins it.
#[test]
fn shift_marquee_gesture_toggles_the_selection() {
    let mut s = server();
    let square = |s: &mut Server, id: u64, x: u64| {
        let r = call(s, id, "draw_shape", json!({"shape": "rectangle", "x": x, "y": 100, "width": 50, "height": 50}));
        serde_json::from_str::<Value>(&text_of(&r)).unwrap()["id"].as_u64().unwrap()
    };
    let (a, b, c) = (square(&mut s, 1, 100), square(&mut s, 2, 200), square(&mut s, 3, 300));
    let r = call(&mut s, 4, "run_command", json!({"command": "select.set", "params": {"ids": [a, b]}}));
    assert_eq!(r["isError"], false, "{r}");
    let events = json!([{"kind": "down", "x": 180, "y": 80}, {"kind": "drag", "x": 300, "y": 200}, {"kind": "up", "x": 380, "y": 200}]);
    for tool in ["selection", "directSelection"] {
        let r = call(&mut s, 5, "pointer_gesture", json!({"tool": tool, "events": events, "mods": {"shift": true}}));
        assert_eq!(r["isError"], false, "{r}");
        let sel = &serde_json::from_str::<Value>(&text_of(&r)).unwrap()["selection"];
        // The Selection tool takes b out and adds c; Direct Selection's marquee then toggles their
        // anchors back: b's are selected again and c, whole, leaves.
        let want = if tool == "selection" { json!([a, c]) } else { json!([a, b]) };
        assert_eq!(*sel, want, "{tool}");
    }
}

/// `press_key {key: "e"}` while a Selection marquee is being dragged makes the marquee select only
/// the objects entirely inside it. With nothing dragged, E still chooses its tool.
#[test]
fn e_during_a_marquee_gesture_selects_only_the_objects_inside() {
    let mut s = server();
    let square = |s: &mut Server, id: u64, x: u64| {
        let r = call(s, id, "draw_shape", json!({"shape": "rectangle", "x": x, "y": 100, "width": 50, "height": 50}));
        serde_json::from_str::<Value>(&text_of(&r)).unwrap()["id"].as_u64().unwrap()
    };
    let (a, _b) = (square(&mut s, 1, 100), square(&mut s, 2, 200));
    let begin = json!([{"kind": "down", "x": 80, "y": 80}, {"kind": "drag", "x": 220, "y": 170}]);
    let r = call(&mut s, 3, "pointer_gesture", json!({"tool": "selection", "events": begin}));
    assert_eq!(r["isError"], false, "{r}");
    let r = call(&mut s, 4, "press_key", json!({"key": "e"}));
    assert_eq!(serde_json::from_str::<Value>(&text_of(&r)).unwrap()["handledBy"], "tool", "{r}");
    let end = json!([{"kind": "drag", "x": 230, "y": 170}, {"kind": "up", "x": 230, "y": 170}]);
    let r = call(&mut s, 5, "pointer_gesture", json!({"tool": "selection", "events": end}));
    assert_eq!(serde_json::from_str::<Value>(&text_of(&r)).unwrap()["selection"], json!([a]), "{r}");
    let r = call(&mut s, 6, "press_key", json!({"key": "e"}));
    assert_eq!(serde_json::from_str::<Value>(&text_of(&r)).unwrap()["handledBy"], "tool.select", "{r}");
}

/// `run_command tool.selectLastSelection` headless: the Selection tool before any selection tool
/// is chosen, then the selection tool chosen last; Magic Wand doesn't count. `list_commands`
/// lists it as a command the headless backend runs.
#[test]
fn select_last_selection_tool_runs_headless() {
    let mut s = server();
    let mut id = 0;
    let mut run = |s: &mut Server, command: &str, params: Value| {
        id += 1;
        let r = call(s, id, "run_command", json!({"command": command, "params": params}));
        assert_eq!(r["isError"], false, "{command}: {r}");
        serde_json::from_str::<Value>(&text_of(&r)).unwrap()
    };
    run(&mut s, "tool.select", json!({"tool": "pen"}));
    assert_eq!(run(&mut s, "tool.selectLastSelection", json!({})), json!({"tool": "selection"}));
    run(&mut s, "tool.select", json!({"tool": "groupSelection"}));
    run(&mut s, "tool.select", json!({"tool": "magicWand"}));
    assert_eq!(run(&mut s, "tool.selectLastSelection", json!({})), json!({"tool": "groupSelection"}));
    let r = call(&mut s, 99, "list_commands", json!({"filter": "selectLastSelection"}));
    let v: Value = serde_json::from_str(&text_of(&r)).unwrap();
    let listed = v["commands"].as_array().unwrap().iter().find(|c| c["id"] == "tool.selectLastSelection").cloned().unwrap_or_default();
    assert_eq!((listed["host"].clone(), listed["label"].clone()), (json!(true), json!("Select Last Selection Tool")), "{v}");
}

/// `press_key {key: "c"}` and `{key: "f"}` while an arc is being dragged go to the Arc tool, and
/// the release draws one arc. With nothing dragged, C is a shortcut again.
#[test]
fn c_and_f_during_an_arc_gesture_go_to_the_arc_tool() {
    let mut s = server();
    let count = |s: &mut Server, id: u64| {
        let r = call(s, id, "inspect_document", json!({}));
        serde_json::from_str::<Value>(&text_of(&r)).unwrap()["objects"].as_u64().unwrap()
    };
    let before = count(&mut s, 1);
    let begin = json!([{"kind": "down", "x": 100, "y": 100}, {"kind": "drag", "x": 200, "y": 180}]);
    let r = call(&mut s, 2, "pointer_gesture", json!({"tool": "arc", "events": begin}));
    assert_eq!(r["isError"], false, "{r}");
    for (id, key) in [(3, "c"), (4, "f")] {
        let r = call(&mut s, id, "press_key", json!({"key": key}));
        assert_eq!(serde_json::from_str::<Value>(&text_of(&r)).unwrap()["handledBy"], "tool", "{key}: {r}");
    }
    let r = call(&mut s, 5, "pointer_gesture", json!({"tool": "arc", "events": [{"kind": "up", "x": 200, "y": 180}]}));
    assert_eq!(r["isError"], false, "{r}");
    assert_eq!(count(&mut s, 6), before + 1);
    let r = call(&mut s, 7, "press_key", json!({"key": "c"}));
    assert_ne!(serde_json::from_str::<Value>(&text_of(&r)).unwrap()["handledBy"], "tool", "{r}");
}

/// `press_key` with `f`, `v`, `x` and `c` while a rectangular or polar grid is being dragged goes
/// to the grid tool, and the release draws one grid. With nothing dragged, V is a shortcut again.
#[test]
fn f_v_x_and_c_during_a_grid_gesture_go_to_the_grid_tool() {
    let count = |s: &mut Server, id: u64| {
        let r = call(s, id, "inspect_document", json!({"depth": 0}));
        serde_json::from_str::<Value>(&text_of(&r)).unwrap()["layers"][0]["childCount"].as_u64().unwrap()
    };
    for tool in ["rectangularGrid", "polarGrid"] {
        let mut s = server();
        let before = count(&mut s, 1);
        let begin = json!([{"kind": "down", "x": 100, "y": 100}, {"kind": "drag", "x": 200, "y": 180}]);
        let r = call(&mut s, 2, "pointer_gesture", json!({"tool": tool, "events": begin}));
        assert_eq!(r["isError"], false, "{r}");
        for (id, key) in [(3, "f"), (4, "v"), (5, "x"), (6, "c")] {
            let r = call(&mut s, id, "press_key", json!({"key": key}));
            assert_eq!(serde_json::from_str::<Value>(&text_of(&r)).unwrap()["handledBy"], "tool", "{tool} {key}: {r}");
        }
        let r = call(&mut s, 7, "pointer_gesture", json!({"tool": tool, "events": [{"kind": "up", "x": 200, "y": 180}]}));
        assert_eq!(r["isError"], false, "{r}");
        assert_eq!(count(&mut s, 8), before + 1, "{tool}");
        let r = call(&mut s, 9, "press_key", json!({"key": "v"}));
        assert_ne!(serde_json::from_str::<Value>(&text_of(&r)).unwrap()["handledBy"], "tool", "{tool}: {r}");
    }
}

/// Grid letters with Shift reach the grid tool during a drag; with Ctrl, Cmd or Alt they do not.
#[test]
fn shifted_grid_letters_during_a_drag_go_to_the_grid_tool() {
    let mut s = server();
    let r = call(
        &mut s,
        1,
        "pointer_gesture",
        json!({"tool": "polarGrid", "events": [{"kind": "down", "x": 100, "y": 100}, {"kind": "drag", "x": 200, "y": 180}]}),
    );
    assert_eq!(r["isError"], false, "{r}");
    for (id, key, mods, tool) in [
        (2, "v", json!({"shift": true}), true),
        (3, "f", json!({"ctrl": true}), false),
        (4, "x", json!({"cmd": true}), false),
        (5, "c", json!({"cmd": true}), false),
        (6, "v", json!({"alt": true}), false),
    ] {
        let r = call(&mut s, id, "press_key", json!({"key": key, "mods": mods}));
        let handled = serde_json::from_str::<Value>(&text_of(&r)).ok().is_some_and(|v| v["handledBy"] == "tool");
        assert_eq!(handled, tool, "{key} {mods}: {r}");
    }
}

/// A click on one of two selected objects makes it the key object: `pointer_gesture` replies
/// with it as `key` and `inspect_document` lists it, and a click on the key again lets it go.
#[test]
fn pointer_gestures_report_the_key_object() {
    let mut s = server();
    let square = |s: &mut Server, id: u64, x: u64| {
        let r = call(s, id, "draw_shape", json!({"shape": "rectangle", "x": x, "y": 100, "width": 50, "height": 50, "fill": "#ff0000"}));
        serde_json::from_str::<Value>(&text_of(&r)).unwrap()["id"].as_u64().unwrap()
    };
    let (a, b) = (square(&mut s, 1, 100), square(&mut s, 2, 200));
    let r = call(&mut s, 3, "run_command", json!({"command": "select.set", "params": {"ids": [a, b]}}));
    assert_eq!(r["isError"], false, "{r}");
    let click = |s: &mut Server, id: u64| {
        let events = json!([{"kind": "down", "x": 225, "y": 125}, {"kind": "up", "x": 225, "y": 125}]);
        let r = call(s, id, "pointer_gesture", json!({"tool": "selection", "events": events}));
        assert_eq!(r["isError"], false, "{r}");
        serde_json::from_str::<Value>(&text_of(&r)).unwrap().get("key").cloned()
    };
    assert_eq!(click(&mut s, 4), Some(json!(b)), "a click on a selected object makes it the key");
    let doc: Value = serde_json::from_str(&text_of(&call(&mut s, 5, "inspect_document", json!({})))).unwrap();
    assert_eq!(doc.get("key"), Some(&json!(b)));
    assert_eq!(click(&mut s, 6), Some(Value::Null), "a click on the key again lets it go");
}

/// Preferences › Type › Show Font Names in English (#394): agents set it with `prefs.set`;
/// `text.fontList` stays the English (canonical) family names used by `text.setStyle`, not the
/// native UI labels.
#[test]
fn show_font_names_in_english_does_not_change_font_list_for_agents() {
    let mut s = server();
    let r = call(&mut s, 1, "run_command", json!({"command": "prefs.set", "params": {"key": "fontNamesInEnglish", "value": false}}));
    assert_eq!(r["isError"], false, "{r}");
    let r = call(&mut s, 2, "run_command", json!({"command": "prefs.get", "params": {"key": "fontNamesInEnglish"}}));
    assert_eq!(r["isError"], false, "{r}");
    assert_eq!(serde_json::from_str::<Value>(&text_of(&r)).unwrap(), json!(false));
    let r = call(&mut s, 3, "run_command", json!({"command": "text.fontList", "params": {}}));
    assert_eq!(r["isError"], false, "{r}");
    let families = serde_json::from_str::<Value>(&text_of(&r)).unwrap()["families"].as_array().unwrap().clone();
    // Canonical English name for commands, whatever the UI labels show with the preference off.
    assert!(families.iter().any(|f| f.as_str() == Some("Source Sans 3")), "{families:?}");
}

/// Preferences › Type › Highlight Alternate Glyphs (#394): off by default so the canvas stays
/// clear until an agent (or the Preferences dialog) turns it on. Headless MCP has no canvas.
#[test]
fn highlight_alternate_glyphs_pref_round_trips_for_agents() {
    let mut s = server();
    let r = call(&mut s, 1, "run_command", json!({"command": "prefs.get", "params": {"key": "highlightAlternateGlyphs"}}));
    assert_eq!(r["isError"], false, "{r}");
    assert_eq!(serde_json::from_str::<Value>(&text_of(&r)).unwrap(), json!(false));
    let r = call(&mut s, 2, "run_command", json!({"command": "prefs.set", "params": {"key": "highlightAlternateGlyphs", "value": true}}));
    assert_eq!(r["isError"], false, "{r}");
    let r = call(&mut s, 3, "run_command", json!({"command": "prefs.get", "params": {"key": "highlightAlternateGlyphs"}}));
    assert_eq!(r["isError"], false, "{r}");
    assert_eq!(serde_json::from_str::<Value>(&text_of(&r)).unwrap(), json!(true));
}

/// Type preferences reach agents (#394): type the Type tool places starts with placeholder text,
/// selected; Alt+→ tracks it by Tracking and Cmd+Shift+. steps its size by Size/Leading.
#[test]
fn type_preferences_apply_to_agents() {
    let mut s = server();
    let events = json!([{"kind": "down", "x": 100, "y": 100}, {"kind": "up", "x": 100, "y": 100}]);
    let r = call(&mut s, 1, "pointer_gesture", json!({"tool": "type", "events": events}));
    assert_eq!(r["isError"], false, "{r}");
    let id = serde_json::from_str::<Value>(&text_of(&r)).unwrap()["selection"][0].as_u64().unwrap();
    let style = |s: &mut Server, n: u64| {
        let r = call(s, n, "run_command", json!({"command": "text.getRange", "params": {"id": id}}));
        let v = serde_json::from_str::<Value>(&text_of(&r)).unwrap();
        assert!(v["text"].as_str().unwrap().len() > 10, "placeholder text: {v}");
        let st = &v["runs"][0]["style"];
        (st["size"].as_f64().unwrap(), st["tracking"].as_f64().unwrap())
    };
    let (size, tracking) = style(&mut s, 2);
    let r = call(&mut s, 3, "run_command", json!({"command": "prefs.set", "params": {"values": {"typeSizeIncrement": 4, "trackingIncrement": 50}}}));
    assert_eq!(r["isError"], false, "{r}");
    let r = call(&mut s, 4, "press_key", json!({"key": "Right", "mods": {"alt": true}}));
    assert_eq!(r["isError"], false, "{r}");
    let r = call(&mut s, 5, "press_key", json!({"key": ".", "mods": {"cmd": true, "shift": true}}));
    assert_eq!(r["isError"], false, "{r}");
    assert_eq!(style(&mut s, 6), (size + 4.0, tracking + 50.0));
}

/// Cmd+Alt+Shift+. and , reach agents: the placeholder text the Type tool selects steps by five
/// times the Size/Leading increment (2 pt by default) and back.
#[test]
fn cmd_alt_shift_period_steps_the_font_size_by_five_for_agents() {
    let mut s = server();
    let events = json!([{"kind": "down", "x": 100, "y": 100}, {"kind": "up", "x": 100, "y": 100}]);
    let r = call(&mut s, 1, "pointer_gesture", json!({"tool": "type", "events": events}));
    assert_eq!(r["isError"], false, "{r}");
    let id = serde_json::from_str::<Value>(&text_of(&r)).unwrap()["selection"][0].as_u64().unwrap();
    let r = call(&mut s, 2, "run_command", json!({"command": "text.setRangeStyle", "params": {"id": id, "size": 12}}));
    assert_eq!(r["isError"], false, "{r}");
    let size = |s: &mut Server, n| {
        let r = call(s, n, "run_command", json!({"command": "text.getRange", "params": {"id": id, "effective": true}}));
        serde_json::from_str::<Value>(&text_of(&r)).unwrap()["runs"][0]["style"]["size"].clone()
    };
    let r = call(&mut s, 3, "press_key", json!({"key": ".", "mods": {"cmd": true, "alt": true, "shift": true}}));
    assert_eq!(r["isError"], false, "{r}");
    assert_eq!(serde_json::from_str::<Value>(&text_of(&r)).unwrap()["command"], "type.size.increaseByFive", "{r}");
    assert_eq!(size(&mut s, 4), json!(22.0));
    let r = call(&mut s, 5, "press_key", json!({"key": ",", "mods": {"cmd": true, "alt": true, "shift": true}}));
    assert_eq!(serde_json::from_str::<Value>(&text_of(&r)).unwrap()["command"], "type.size.decreaseByFive", "{r}");
    assert_eq!(size(&mut s, 6), json!(12.0));
}

/// Cmd+Alt+Q reaches agents: the placeholder text the Type tool selects goes back to tracking 0
/// and Auto kerning.
#[test]
fn cmd_alt_q_resets_tracking_and_kerning_for_agents() {
    let mut s = server();
    let events = json!([{"kind": "down", "x": 100, "y": 100}, {"kind": "up", "x": 100, "y": 100}]);
    let r = call(&mut s, 1, "pointer_gesture", json!({"tool": "type", "events": events}));
    assert_eq!(r["isError"], false, "{r}");
    let id = serde_json::from_str::<Value>(&text_of(&r)).unwrap()["selection"][0].as_u64().unwrap();
    let r = call(&mut s, 2, "run_command", json!({"command": "text.setRangeStyle", "params": {"id": id, "tracking": 200, "kerning": -40}}));
    assert_eq!(r["isError"], false, "{r}");
    let r = call(&mut s, 3, "press_key", json!({"key": "Q", "mods": {"cmd": true, "alt": true}}));
    assert_eq!(r["isError"], false, "{r}");
    assert_eq!(serde_json::from_str::<Value>(&text_of(&r)).unwrap()["command"], "type.resetTracking", "{r}");
    let r = call(&mut s, 4, "run_command", json!({"command": "text.getRange", "params": {"id": id}}));
    let runs = serde_json::from_str::<Value>(&text_of(&r)).unwrap()["runs"].clone();
    assert_eq!(runs.as_array().unwrap().len(), 1, "{runs}");
    assert_eq!((&runs[0]["style"]["tracking"], &runs[0]["style"]["kerning"]), (&json!(0.0), &Value::Null), "{runs}");
}

/// Cmd+Shift+X reaches agents: the placeholder text the Type tool selects goes back to a
/// horizontal scale of 100%.
#[test]
fn cmd_shift_x_resets_horizontal_scale_for_agents() {
    let mut s = server();
    let events = json!([{"kind": "down", "x": 100, "y": 100}, {"kind": "up", "x": 100, "y": 100}]);
    let r = call(&mut s, 1, "pointer_gesture", json!({"tool": "type", "events": events}));
    assert_eq!(r["isError"], false, "{r}");
    let id = serde_json::from_str::<Value>(&text_of(&r)).unwrap()["selection"][0].as_u64().unwrap();
    let r = call(&mut s, 2, "run_command", json!({"command": "text.setRangeStyle", "params": {"id": id, "hScale": 150}}));
    assert_eq!(r["isError"], false, "{r}");
    let r = call(&mut s, 3, "press_key", json!({"key": "X", "mods": {"cmd": true, "shift": true}}));
    assert_eq!(r["isError"], false, "{r}");
    assert_eq!(serde_json::from_str::<Value>(&text_of(&r)).unwrap()["command"], "type.resetHorizontalScale", "{r}");
    let r = call(&mut s, 4, "run_command", json!({"command": "text.getRange", "params": {"id": id}}));
    let runs = serde_json::from_str::<Value>(&text_of(&r)).unwrap()["runs"].clone();
    assert_eq!(runs.as_array().unwrap().len(), 1, "{runs}");
    assert_eq!(runs[0]["style"]["h_scale"], json!(100.0), "{runs}");
}

/// `press_key` runs Cmd+/ and Cmd+Alt+/ for agents: a new fill and then a new stroke go on top of
/// the selected rectangle's appearance stack.
#[test]
fn cmd_slash_and_cmd_alt_slash_add_a_fill_and_a_stroke_for_agents() {
    let mut s = server();
    let r = call(&mut s, 1, "run_command", json!({"command": "shape.rectangle", "params": {"x": 10, "y": 10, "width": 100, "height": 100}}));
    let id = serde_json::from_str::<Value>(&text_of(&r)).unwrap()["id"].as_u64().unwrap();
    let r = call(&mut s, 2, "run_command", json!({"command": "select.set", "params": {"ids": [id]}}));
    assert_eq!(r["isError"], false, "{r}");
    let kinds = |s: &mut Server, n: u64| {
        let r = call(s, n, "run_command", json!({"command": "effect.list", "params": {}}));
        let items = serde_json::from_str::<Value>(&text_of(&r)).unwrap()["applied"][0]["items"].clone();
        items.as_array().unwrap().iter().map(|it| it["kind"].as_str().unwrap().to_string()).collect::<Vec<_>>()
    };
    assert_eq!(kinds(&mut s, 3), ["fill", "stroke"]);
    let r = call(&mut s, 4, "press_key", json!({"key": "/", "mods": {"cmd": true}}));
    assert_eq!(r["isError"], false, "{r}");
    assert_eq!(serde_json::from_str::<Value>(&text_of(&r)).unwrap()["command"], "appearance.addFill", "{r}");
    assert_eq!(kinds(&mut s, 5), ["fill", "stroke", "fill"]);
    let r = call(&mut s, 6, "press_key", json!({"key": "/", "mods": {"cmd": true, "alt": true}}));
    assert_eq!(r["isError"], false, "{r}");
    assert_eq!(serde_json::from_str::<Value>(&text_of(&r)).unwrap()["command"], "appearance.addStroke", "{r}");
    assert_eq!(kinds(&mut s, 7), ["fill", "stroke", "fill", "stroke"]);
}

/// The drawing tools snap to Smart Guides over MCP as with the mouse (#506): a Rectangle drawn
/// from near another object's corner starts on it, and a Pen anchor placed beside that object's
/// centre lines up with it.
#[test]
fn drawing_gestures_snap_to_smart_guides() {
    let mut s = server();
    let r = call(&mut s, 1, "run_command", json!({"command": "file.new", "params": {"width": 800, "height": 600}}));
    assert_eq!(r["isError"], false, "{r}");
    let r = call(&mut s, 2, "draw_shape", json!({"shape": "rectangle", "x": 100, "y": 100, "width": 100, "height": 100}));
    assert_eq!(r["isError"], false, "{r}");
    let bounds = |s: &mut Server, n: u64, events: Value, tool: &str| {
        let r = call(s, n, "pointer_gesture", json!({"tool": tool, "events": events}));
        assert_eq!(r["isError"], false, "{r}");
        let id = serde_json::from_str::<Value>(&text_of(&r)).unwrap()["selection"][0].clone();
        let doc: Value = serde_json::from_str(&text_of(&call(s, n + 1, "inspect_document", json!({})))).unwrap();
        let made = doc["layers"][0]["children"].as_array().unwrap().iter().find(|c| c["id"] == id).expect("drawn").clone();
        let b = &made["bounds"];
        [&b["x"], &b["y"], &b["width"], &b["height"]].map(|v| v.as_f64().unwrap())
    };
    // The rectangle drawn first stays selected, and the press lands on its box's corner handle,
    // which would scale it instead of drawing.
    let r = call(&mut s, 30, "run_command", json!({"command": "select.none", "params": {}}));
    assert_eq!(r["isError"], false, "{r}");
    let events = json!([{"kind": "move", "x": 202, "y": 203}, {"kind": "down", "x": 202, "y": 203}, {"kind": "drag", "x": 330, "y": 341}, {"kind": "up", "x": 330, "y": 341}]);
    assert_eq!(bounds(&mut s, 3, events, "rectangle"), [200.0, 200.0, 130.0, 141.0]);
    let r = call(&mut s, 5, "run_command", json!({"command": "select.none", "params": {}}));
    assert_eq!(r["isError"], false, "{r}");
    let events = json!([{"kind": "down", "x": 300, "y": 420}, {"kind": "up", "x": 300, "y": 420}, {"kind": "move", "x": 151.5, "y": 431}, {"kind": "down", "x": 151.5, "y": 431}, {"kind": "up", "x": 151.5, "y": 431}]);
    assert_eq!(bounds(&mut s, 6, events, "pen"), [150.0, 420.0, 150.0, 11.0]);
}

/// #1471: add_text says when the text doesn't fit its frame, and takes area type's `fit`.
#[test]
fn add_text_reports_overflow_and_takes_a_fit() {
    let mut s = server();
    let made = |r: &Value| serde_json::from_str::<Value>(&text_of(r)).unwrap();
    let headline = json!({"text": "NEON DRIVE", "x": 0, "y": 20, "width": 400, "height": 60, "size": 60});
    let low = made(&call(&mut s, 1, "add_text", headline.clone()));
    assert_eq!(low["overflow"], true, "{low}");
    let mut auto = headline;
    auto["fit"] = json!("autoHeight");
    let auto = made(&call(&mut s, 2, "add_text", auto));
    assert!(auto["id"].is_u64() && auto.get("overflow").is_none(), "{auto}");
    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")]));
}