mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-12-02 02:48:02 -06:00
document.save, file.saveAs, file.saveCopy and file.saveAsTemplate wrote the file when given a key that no save format reads, beside the path or in options, or an unknown key inside a PDF option object such as marks.useDocumentBleed. document.exportForScreens did the same for an unknown top-level key, a key in a format row and a key in the settings of a format. These commands now refuse such a key and name its path (options.noSuchOption, formats[1].marks.useDocumentBleed). The svg object is checked in every save, export and format row, also when the file written is not SVG. A save still leaves out a key that only another save format reads, and warns as before. A format row and a format's settings still take includeArtboardFill and richBlack, which the encoders read. document.exportPresets.save checks its settings, with the rows and settings in it, the same way. In the app, file.save and file.exportForScreens sent as commands are checked as document.save and document.exportForScreens, and app.save is checked before anything is written; MCP's app.save, file.save and file.exportForScreens are checked the same way. The SVG Options dialog no longer passes its internal fields on to the save.
125 lines
6.1 KiB
Rust
125 lines
6.1 KiB
Rust
//! Export As through MCP: `options` carries `useArtboards` and the raster options to
|
|
//! `document.export`, so a range writes one file per artboard.
|
|
|
|
use serde_json::{Value, json};
|
|
|
|
use crate::{Backend, Headless, call_tool};
|
|
|
|
fn text(r: &crate::ToolResult) -> Value {
|
|
assert!(!r.is_error, "{r:?}");
|
|
serde_json::from_str(r.content[0]["text"].as_str().unwrap()).unwrap()
|
|
}
|
|
|
|
fn three_boards() -> Headless {
|
|
let mut h = Headless::new();
|
|
h.call("engine.execute", json!({"command": "file.new", "params": {"width": 50, "height": 30, "artboards": 3}})).unwrap();
|
|
h
|
|
}
|
|
|
|
/// The tool, `app.export` and `file.export` refuse a key no format takes, at the top level of
|
|
/// `options` and inside a PDF object.
|
|
#[test]
|
|
fn an_option_no_format_takes_is_an_error() {
|
|
let mut h = Headless::new();
|
|
h.call("engine.execute", json!({"command": "file.new", "params": {"width": 50, "height": 30}})).unwrap();
|
|
for (options, named) in [
|
|
(json!({"standard": "pdfX4", "noSuchOption": true}), "noSuchOption"),
|
|
(json!({"marks": {"useDocumentBleed": true}}), "marks.useDocumentBleed"),
|
|
] {
|
|
let r = call_tool(&mut h, "export", &json!({"format": "pdf", "options": options}));
|
|
assert!(r.is_error && r.content[0]["text"].as_str().unwrap().contains(named), "{r:?}");
|
|
let mut params = options.clone();
|
|
params["format"] = json!("pdf");
|
|
for method in ["app.export", "file.export"] {
|
|
let e = if method == "app.export" {
|
|
h.call(method, params.clone()).unwrap_err()
|
|
} else {
|
|
h.call("engine.execute", json!({"command": method, "params": params})).unwrap_err()
|
|
};
|
|
assert!(e.contains(named), "{method}: {e}");
|
|
}
|
|
}
|
|
// One object can still carry several formats' options.
|
|
let r = call_tool(&mut h, "export", &json!({"format": "pdf", "options": {"quality": 80, "styling": "style"}}));
|
|
assert!(!r.is_error, "{r:?}");
|
|
}
|
|
|
|
/// `app.save`, `file.save` and `file.exportForScreens` refuse a key no format takes before they
|
|
/// write anything.
|
|
#[test]
|
|
fn a_save_or_screens_option_no_format_takes_is_an_error() {
|
|
let dir = std::env::temp_dir().join(format!("vc-mcp-save-options-{}", std::process::id()));
|
|
let _ = std::fs::remove_dir_all(&dir);
|
|
std::fs::create_dir_all(&dir).unwrap();
|
|
let mut h = Headless::new();
|
|
h.call("engine.execute", json!({"command": "file.new", "params": {"width": 50, "height": 30}})).unwrap();
|
|
let pdf = dir.join("a.pdf").to_string_lossy().into_owned();
|
|
for (params, named) in [
|
|
(json!({"path": pdf, "noSuchOption": true}), "noSuchOption"),
|
|
(json!({"path": pdf, "options": {"marks": {"useDocumentBleed": true}}}), "options.marks.useDocumentBleed"),
|
|
] {
|
|
let e = h.call("app.save", params.clone()).unwrap_err();
|
|
assert!(e.contains(named), "app.save: {e}");
|
|
let e = h.call("engine.execute", json!({"command": "file.save", "params": params})).unwrap_err();
|
|
assert!(e.contains(named), "file.save: {e}");
|
|
}
|
|
let folder = dir.join("screens").to_string_lossy().into_owned();
|
|
for (params, named) in [
|
|
(json!({"folder": folder, "noSuchOption": true}), "noSuchOption"),
|
|
(json!({"folder": folder, "formats": [{"format": "pdf", "marks": {"useDocumentBleed": true}}]}), "formats[0].marks.useDocumentBleed"),
|
|
] {
|
|
let e = h.call("engine.execute", json!({"command": "file.exportForScreens", "params": params})).unwrap_err();
|
|
assert!(e.contains(named), "file.exportForScreens: {e}");
|
|
}
|
|
assert_eq!(std::fs::read_dir(&dir).unwrap().count(), 0, "nothing was written");
|
|
h.call("app.save", json!({"path": pdf})).unwrap();
|
|
let _ = std::fs::remove_dir_all(&dir);
|
|
}
|
|
|
|
#[test]
|
|
fn raster_export_uses_document_resolution_unless_overridden() {
|
|
let mut h = Headless::new();
|
|
h.call("engine.execute", json!({"command": "file.new", "params": {"width": 72, "height": 36, "rasterEffectsPpi": 300}})).unwrap();
|
|
for (args, size) in [
|
|
(json!({}), (300, 150)),
|
|
(json!({"scale": 1}), (72, 36)),
|
|
(json!({"options": {"scale": 2}}), (144, 72)),
|
|
(json!({"scale": 2, "options": {"ppi": 150}}), (150, 75)),
|
|
] {
|
|
let mut params = args.clone();
|
|
params["format"] = json!("png");
|
|
let r = text(&call_tool(&mut h, "export", ¶ms));
|
|
let bytes = vectorcraft_format::base64_decode(r["dataBase64"].as_str().unwrap()).unwrap();
|
|
assert_eq!(image::load_from_memory(&bytes).unwrap().to_rgba8().dimensions(), size, "{args}");
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn export_with_a_range_writes_one_file_per_artboard() {
|
|
let mut h = three_boards();
|
|
let dir = std::env::temp_dir().join(format!("vectorcraft-mcp-exportas-{}", std::process::id()));
|
|
std::fs::create_dir_all(&dir).unwrap();
|
|
let path = dir.join("icon.png").to_string_lossy().to_string();
|
|
let args = json!({"path": path, "range": "1, 3", "options": {"useArtboards": true, "ppi": 144, "background": "white"}});
|
|
let r = text(&call_tool(&mut h, "export", &args));
|
|
let files = r["files"].as_array().unwrap();
|
|
assert_eq!(files.len(), 2, "{r}");
|
|
for (f, board) in files.iter().zip(["Artboard-1", "Artboard-3"]) {
|
|
let p = f.as_str().unwrap();
|
|
assert!(p.ends_with(&format!("icon-{board}.png")), "{p}");
|
|
let img = image::load_from_memory(&std::fs::read(p).unwrap()).unwrap().to_rgba8();
|
|
assert_eq!(img.dimensions(), (100, 60), "144 ppi");
|
|
assert!(img.pixels().all(|px| px[3] == 255), "white background");
|
|
}
|
|
// Without a path the files come back as bytes.
|
|
let r = text(&call_tool(&mut h, "export", &json!({"format": "jpg", "options": {"useArtboards": true}})));
|
|
let names: Vec<&str> = r["files"].as_array().unwrap().iter().map(|f| f["name"].as_str().unwrap()).collect();
|
|
assert_eq!(names, ["Untitled-1-Artboard-1.jpg", "Untitled-1-Artboard-2.jpg", "Untitled-1-Artboard-3.jpg"]);
|
|
// SVG, SVGZ and EPS write one file per artboard of a range without useArtboards.
|
|
for format in ["svg", "svgz", "eps"] {
|
|
let r = text(&call_tool(&mut h, "export", &json!({"format": format, "range": "1, 3"})));
|
|
assert_eq!(r["files"].as_array().map(Vec::len), Some(2), "{format}: {r}");
|
|
}
|
|
let _ = std::fs::remove_dir_all(dir);
|
|
}
|