Files
vectorcraft/crates/engine/src/cmd/addons/document/tests.rs
T
dotCooCoo 1e31f9b94c Document Tools: Flood Fill Artboards refuses infinite colors and keeps the fill on Cancel
- A color component too large for f32 (c: 1e308, [1e39, 0, 0], gray or
  Lab out of range) read as infinite and reached the rectangles; the
  command now refuses it with "bad color".
- The dialog's Color option opened on the current fill in display sRGB
  and sent it as working RGB, so a document whose working RGB space is
  not sRGB got a shifted color. The dialog now opens on the fill's
  display color and converts the picked color back to working RGB.
- With New Art Has Basic Appearance off and nothing selected, a
  previewed color gave the new rectangles' fill to the new-art paints,
  and Cancel or a replaced preview kept it. Both now put back the paints
  the interaction began with.

Docs: docs/mcp.md.
2026-11-29 10:45:42 -08:00

617 lines
32 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
//! Document Tools' commands: the timestamped copy's name and place with the document left as it
//! was, symbols made of layers, a folder of images traced, and bad params refused.
use std::path::{Path, PathBuf};
use serde_json::{Value, json};
use vectorcraft_doc::{NodeId, NodeKind};
use crate::Session;
/// 2026-10-05 14:03:00 UTC.
const TIME: i64 = 1_791_208_980;
/// A new, empty folder of its own for test `name`.
fn folder(name: &str) -> PathBuf {
let dir = std::env::temp_dir().join(format!("vc-addon-document-{name}-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&dir);
std::fs::create_dir_all(&dir).unwrap();
dir
}
fn session() -> Session {
let mut s = Session::new();
s.execute("file.new", &json!({"width": 400, "height": 300})).unwrap();
s.execute("shape.rectangle", &json!({"x": 10, "y": 10, "width": 100, "height": 50})).unwrap();
s
}
fn stamp(s: &mut Session, p: Value) -> crate::Result<Value> {
s.execute("addon.document.saveTimestampedCopy", &p)
}
#[test]
fn a_timestamped_copy_goes_next_to_the_document_which_stays_as_it_was() {
let dir = folder("next-to");
let mut s = session();
let own = dir.join("poster.vectorcraft");
s.execute("document.save", &json!({"path": own})).unwrap();
s.execute("shape.ellipse", &json!({"x": 0, "y": 0, "width": 10, "height": 10})).unwrap();
let (steps, title) = (s.doc().unwrap().history.undo.len(), s.doc().unwrap().doc.title.clone());
let r = stamp(&mut s, json!({"time": TIME})).unwrap();
let copy = dir.join("poster_2026-10-05_14-03.vectorcraft");
assert_eq!(Path::new(r["path"].as_str().unwrap()), copy);
assert!(copy.is_file());
let st = s.doc().unwrap();
assert_eq!(st.path.as_deref().map(Path::new), Some(own.as_path()), "the document keeps its path");
assert_eq!((st.history.undo.len(), &st.doc.title, st.is_dirty()), (steps, &title, true), "no step; still modified");
// The copy holds the document as it is now: the ellipse too.
let mut other = Session::new();
other.execute("document.open", &json!({"path": copy})).unwrap();
assert_eq!(other.doc().unwrap().doc.layers[0].children().unwrap().len(), 2);
// The same minute again: numbered.
let r = stamp(&mut s, json!({"time": TIME})).unwrap();
assert!(r["path"].as_str().unwrap().ends_with("poster_2026-10-05_14-03-2.vectorcraft"), "{r}");
let _ = std::fs::remove_dir_all(&dir);
}
#[test]
fn an_unsaved_document_needs_a_folder_and_other_formats_take_their_extension() {
let dir = folder("unsaved");
let mut s = session();
let before = s.doc().unwrap().doc.clone();
assert!(stamp(&mut s, json!({"time": TIME})).is_err(), "no folder to put it in");
let r = stamp(&mut s, json!({"time": TIME, "folder": dir, "format": "svg"})).unwrap();
let name = Path::new(r["path"].as_str().unwrap()).file_name().unwrap().to_string_lossy().into_owned();
assert!(name.ends_with("_2026-10-05_14-03.svg") && dir.join(&name).is_file(), "{name}");
let st = s.doc().unwrap();
assert!(st.path.is_none() && *st.doc == *before, "the document is unchanged");
let _ = std::fs::remove_dir_all(&dir);
}
#[test]
fn bad_params_are_refused_and_writes_stay_inside_the_automation_roots() {
let dir = folder("bad");
let mut s = session();
for p in [
json!({"folder": dir, "format": "png"}),
json!({"folder": dir, "format": "dxf"}),
json!({"folder": dir, "time": "now"}),
json!({"folder": dir, "time": 1.5}),
json!({"folder": dir.join("missing").join("deeper"), "time": TIME}),
] {
assert!(stamp(&mut s, p.clone()).is_err(), "{p}");
}
assert_eq!(std::fs::read_dir(&dir).unwrap().count(), 0, "nothing written");
let allowed = dir.join("allowed");
std::fs::create_dir_all(&allowed).unwrap();
let roots = crate::file_access::AutomationRoots::new(&[&dir], &[&allowed]).unwrap();
let outside = crate::file_access::confine(roots.as_ref(), || stamp(&mut s, json!({"folder": dir, "time": TIME})));
assert!(outside.is_err(), "outside the write root");
let inside = crate::file_access::confine(roots.as_ref(), || stamp(&mut s, json!({"folder": allowed, "time": TIME})));
assert!(inside.is_ok(), "{inside:?}");
assert!(!enabled_without_document(), "off with no document");
let _ = std::fs::remove_dir_all(&dir);
}
fn enabled_without_document() -> bool {
Session::new().commands().into_iter().find(|c| c.id == "addon.document.saveTimestampedCopy").unwrap().enabled
}
fn layer_ids(s: &Session) -> Vec<u64> {
s.doc().unwrap().doc.layers.iter().map(|l| l.id.0).collect()
}
fn symbol_names(s: &Session) -> Vec<String> {
s.doc().unwrap().doc.symbols.iter().map(|x| x.name.clone()).collect()
}
/// A document of three layers: `Layer 1` with two rectangles, an empty one, and `Icons` with one
/// ellipse.
fn layered() -> Session {
let mut s = session();
s.execute("shape.rectangle", &json!({"x": 200, "y": 10, "width": 50, "height": 50})).unwrap();
s.execute("layer.new", &json!({})).unwrap();
s.execute("layer.new", &json!({"name": "Icons"})).unwrap();
s.execute("shape.ellipse", &json!({"x": 0, "y": 100, "width": 40, "height": 20})).unwrap();
s
}
#[test]
fn a_symbol_is_made_of_each_layer_and_empty_layers_are_reported() {
let mut s = layered();
let before = symbol_names(&s).len();
let n = s.doc().unwrap().history.undo.len();
let r = s.execute("addon.document.symbolsFromLayers", &json!({})).unwrap();
assert_eq!(s.doc().unwrap().history.undo.len(), n + 1, "one undo step");
let names: Vec<String> = serde_json::from_value(r["symbols"].clone()).unwrap();
assert_eq!(names.len(), 2, "{r}");
assert!(names.contains(&"Icons".to_string()));
assert_eq!(symbol_names(&s).len(), before + 2);
assert_eq!(r["skipped"].as_array().unwrap().len(), 1, "the empty layer: {r}");
assert_eq!(r["skipped"][0]["reason"], json!("empty"));
assert_eq!(r["ids"], json!([]), "without replace the art stays");
assert_eq!(s.doc().unwrap().doc.layers[0].children().unwrap().len(), 2);
// Again: the names are taken, so they get numbers.
let r = s.execute("addon.document.symbolsFromLayers", &json!({})).unwrap();
assert!(r["symbols"].as_array().unwrap().contains(&json!("Icons 2")), "{r}");
s.execute("edit.undo", &json!({})).unwrap();
s.execute("edit.undo", &json!({})).unwrap();
assert_eq!(symbol_names(&s).len(), before);
}
#[test]
fn replace_puts_an_instance_where_the_layers_art_was() {
let mut s = layered();
let first = layer_ids(&s)[0];
let bounds = |s: &Session, id: NodeId| s.doc().unwrap().doc.bounds_of(&[id], false).unwrap();
let layer = NodeId(first);
let art = bounds(&s, layer);
let r = s.execute("addon.document.symbolsFromLayers", &json!({"layers": [first], "replace": true})).unwrap();
let ids = r["ids"].as_array().unwrap();
assert_eq!(ids.len(), 1);
let inst = NodeId(ids[0].as_u64().unwrap());
let st = s.doc().unwrap();
let kids = st.doc.layers[0].children().unwrap();
assert_eq!(kids.len(), 1);
assert!(matches!(&kids[0].kind, NodeKind::SymbolInstance { .. }));
assert_eq!(st.selection.objects, [inst]);
let now = bounds(&s, layer);
assert!((now.x0 - art.x0).abs() < 1e-6 && (now.y1 - art.y1).abs() < 1e-6 && (now.width() - art.width()).abs() < 1e-6, "{art:?} {now:?}");
assert_eq!(s.doc().unwrap().doc.layers.len(), 3, "the other layers untouched");
}
#[test]
fn locked_layers_keep_their_art_and_bad_layers_are_refused() {
let mut s = layered();
let ids = layer_ids(&s);
s.execute("layer.setProps", &json!({"id": ids[2], "locked": true})).unwrap();
let r = s.execute("addon.document.symbolsFromLayers", &json!({"replace": true})).unwrap();
let skipped: Vec<&str> = r["skipped"].as_array().unwrap().iter().map(|k| k["reason"].as_str().unwrap()).collect();
assert_eq!(skipped, ["empty", "locked"]);
let before = (s.doc().unwrap().doc.clone(), s.doc().unwrap().history.undo.len());
let rect = s.doc().unwrap().doc.layers[0].children().unwrap()[0].id.0;
for p in [json!({"layers": [rect]}), json!({"layers": [u64::MAX]}), json!({"layers": "all"}), json!({"layers": [-1]}), json!({"replace": "yes"})]
{
assert!(s.execute("addon.document.symbolsFromLayers", &p).is_err(), "{p}");
}
assert!(*s.doc().unwrap().doc == *before.0 && s.doc().unwrap().history.undo.len() == before.1);
// Only an empty layer: nothing made, no step.
let r = s.execute("addon.document.symbolsFromLayers", &json!({"layers": [ids[1]]})).unwrap();
assert_eq!((r["symbols"].clone(), s.doc().unwrap().history.undo.len()), (json!([]), before.1));
}
#[test]
fn the_layers_highlighted_in_the_layers_panel_are_the_default() {
let mut s = layered();
let ids = layer_ids(&s);
// One highlighted row is just the current layer: every layer.
s.execute("layer.setCurrent", &json!({"id": ids[0]})).unwrap();
let r = s.execute("addon.document.symbolsFromLayers", &json!({})).unwrap();
assert_eq!(r["symbols"].as_array().unwrap().len(), 2);
s.execute("edit.undo", &json!({})).unwrap();
s.execute("layer.highlight", &json!({"ids": [ids[1], ids[2]]})).unwrap();
let r = s.execute("addon.document.symbolsFromLayers", &json!({})).unwrap();
assert_eq!((r["symbols"].clone(), r["skipped"].as_array().unwrap().len()), (json!(["Icons"]), 1));
}
const FLOOD: &str = "addon.document.floodFillArtboards";
/// A document of three 100 × 80 artboards with one rectangle on `Layer 1`, and a red fill.
fn boards() -> Session {
let mut s = Session::new();
s.execute("file.new", &json!({"width": 100, "height": 80, "artboards": 3})).unwrap();
s.execute("shape.rectangle", &json!({"x": 10, "y": 10, "width": 20, "height": 20})).unwrap();
s.execute("paint.setFill", &json!({"color": "#ff0000"})).unwrap();
s
}
/// The children of the layer `id`.
fn kids(s: &Session, id: u64) -> Vec<std::sync::Arc<vectorcraft_doc::Node>> {
s.doc().unwrap().doc.node(NodeId(id)).unwrap().children().unwrap().to_vec()
}
#[test]
fn flood_fill_covers_the_active_artboard_on_a_new_layer_at_the_bottom() {
let mut s = boards();
let (current, steps) = (s.doc().unwrap().active_layer, s.doc().unwrap().history.undo.len());
let before = s.doc().unwrap().doc.clone();
let second = before.artboards[1].clone();
s.doc_mut().unwrap().active_artboard = Some(second.id);
let r = s.execute(FLOOD, &json!({})).unwrap();
assert_eq!(r["created"], json!(true), "{r}");
let st = s.doc().unwrap();
let layer = &st.doc.layers[0];
assert_eq!((layer.id.0, layer.name.as_deref()), (r["layer"].as_u64().unwrap(), Some("Flood Fill")), "a new layer below the others");
assert_eq!(st.doc.layers.len(), 2);
let rects = kids(&s, layer.id.0);
assert_eq!(rects.len(), 1);
let rect = &rects[0];
assert_eq!(r["ids"], json!([rect.id.0]));
assert_eq!(rect.geometric_bounds(), Some(second.rect), "exactly the active artboard");
assert!(matches!(&rect.kind, NodeKind::Path { live: Some(live), .. } if matches!(**live, vectorcraft_doc::LiveShape::Rectangle { .. })));
assert_eq!(rect.appearance.fill_paint(), s.paint.fill, "the current fill");
assert_eq!(rect.appearance.stroke_paint(), vectorcraft_color::Paint::None, "no stroke");
let st = s.doc().unwrap();
assert_eq!((st.selection.objects.clone(), st.active_layer), (vec![rect.id], current), "the rectangle is selected; the current layer stays");
assert_eq!(st.history.undo.len(), steps + 1, "one undo step");
assert_eq!(st.history.undo.last().unwrap().label, "Flood Fill Artboards");
s.execute("edit.undo", &json!({})).unwrap();
assert_eq!(*s.doc().unwrap().doc, *before, "undo removes the layer and the rectangle");
}
#[test]
fn flood_fill_reuses_its_layer_and_keeps_its_lock_and_visibility() {
let mut s = boards();
let layer = s.execute(FLOOD, &json!({})).unwrap()["layer"].as_u64().unwrap();
let first = s.doc().unwrap().doc.layers.iter().find(|l| l.name.as_deref() == Some("Layer 1")).unwrap().id.0;
let art = kids(&s, first)[0].id.0;
s.execute("select.set", &json!({"ids": [art]})).unwrap();
s.execute("layer.setProps", &json!({"id": layer, "locked": true})).unwrap();
let selected = s.doc().unwrap().selection.objects.clone();
assert_eq!(selected.len(), 1);
let r = s.execute(FLOOD, &json!({"all": true, "color": "#00ff00"})).unwrap();
assert_eq!((r["created"].clone(), r["layer"].as_u64()), (json!(false), Some(layer)), "{r}");
let st = s.doc().unwrap();
let node = st.doc.node(NodeId(layer)).unwrap();
assert!(node.locked && node.visible, "the layer stays locked");
assert_eq!(st.doc.layers.len(), 2, "no second layer");
assert_eq!(st.selection.objects, selected, "art on a locked layer is not selected");
let rects = kids(&s, layer);
let added: Vec<_> = rects[1..].iter().map(|n| n.geometric_bounds().unwrap()).collect();
let artboards: Vec<_> = st.doc.artboards.iter().map(|a| a.rect).collect();
assert_eq!(added, artboards, "one rectangle per artboard, on top of the layer's art");
let green = vectorcraft_color::Paint::solid(vectorcraft_color::Color::from_hex("#00ff00").unwrap());
assert!(rects[1..].iter().all(|n| n.appearance.fill_paint() == green));
// Hidden: the layer stays hidden, and listed artboards are filled once each, in order.
s.execute("layer.setProps", &json!({"id": layer, "locked": false, "visible": false})).unwrap();
let r = s.execute(FLOOD, &json!({"artboards": [2, 0, 2]})).unwrap();
let bounds = |id: &Value| s.doc().unwrap().doc.node(NodeId(id.as_u64().unwrap())).unwrap().geometric_bounds().unwrap();
let new: Vec<_> = r["ids"].as_array().unwrap().iter().map(bounds).collect();
assert_eq!(new, [artboards[0], artboards[2]]);
assert!(!s.doc().unwrap().doc.node(NodeId(layer)).unwrap().visible);
assert_eq!(s.doc().unwrap().selection.objects, selected, "art on a hidden layer is not selected");
// A layer named by `layer`, found or made.
let r = s.execute(FLOOD, &json!({"layer": "Layer 1", "color": {"c": 0, "m": 0, "y": 0, "k": 50}})).unwrap();
assert_eq!((r["created"].clone(), r["layer"].as_u64()), (json!(false), Some(first)));
let r = s.execute(FLOOD, &json!({"layer": " Paper "})).unwrap();
assert_eq!((r["created"].clone(), s.doc().unwrap().doc.layers[0].name.as_deref()), (json!(true), Some("Paper")));
}
#[test]
fn flood_fill_refuses_bad_params_and_a_none_fill() {
let mut s = boards();
let long = "x".repeat(256);
for p in [
json!({"color": "#ff000080"}),
json!({"color": "red-ish"}),
json!({"color": null}),
json!({"artboards": [3]}),
json!({"artboards": []}),
json!({"artboards": null}),
json!({"artboards": [0.5]}),
json!({"all": null}),
json!({"all": "yes"}),
json!({"all": true, "artboards": [0]}),
json!({"layer": ""}),
json!({"layer": " "}),
json!({"layer": 5}),
json!({"layer": null}),
json!({"layer": long}),
] {
let before = (s.doc().unwrap().doc.clone(), s.doc().unwrap().history.undo.len());
assert!(s.execute(FLOOD, &p).is_err(), "{p}");
assert!(*s.doc().unwrap().doc == *before.0 && s.doc().unwrap().history.undo.len() == before.1, "{p} changed nothing");
}
// Components too large for f32 become infinite once read.
for color in [json!({"c": 1e308, "m": 0, "y": 0, "k": 0}), json!([1e39, 0, 0]), json!({"gray": 1e41}), json!({"l": 1e39, "a": 0, "b": 0})] {
let p = json!({ "color": color });
let before = (s.doc().unwrap().doc.clone(), s.doc().unwrap().history.undo.len());
let e = s.execute(FLOOD, &p).unwrap_err().to_string();
assert!(e.contains("bad color"), "{p}: {e}");
assert!(*s.doc().unwrap().doc == *before.0 && s.doc().unwrap().history.undo.len() == before.1, "{p} changed nothing");
}
s.execute("paint.setFill", &json!({"none": true})).unwrap();
let e = s.execute(FLOOD, &json!({})).unwrap_err().to_string();
assert!(e.contains("the current fill is None"), "{e}");
assert!(s.execute(FLOOD, &json!({"color": "#336699"})).is_ok(), "a given color needs no current fill");
}
/// With New Art Has Basic Appearance off, a preview in a color selects rectangles whose color the
/// new-art fill takes; a later preview without `color` still uses the fill the interaction began with.
#[test]
fn flood_fill_previews_without_color_use_the_fill_the_interaction_began_with() {
let mut s = boards();
s.prefs.new_art_basic = false;
let red = s.paint.fill.clone();
s.begin_interaction("Flood Fill Artboards").unwrap();
s.preview(FLOOD, &json!({"color": "#0000ff"})).unwrap();
assert_ne!(s.paint.fill, red, "the previewed rectangles gave their color to the new-art fill");
let r = s.preview(FLOOD, &json!({})).unwrap();
s.commit_interaction().unwrap();
let id = NodeId(r["ids"][0].as_u64().unwrap());
let fill = s.doc().unwrap().doc.node(id).unwrap().appearance.fill_paint();
assert_eq!(fill, red, "the fill the interaction began with, not the previewed color");
}
/// With New Art Has Basic Appearance off and nothing selected, a canceled preview in a color puts
/// back the new-art fill that its selected rectangles had given their color to.
#[test]
fn flood_fill_cancel_from_an_empty_selection_keeps_the_new_art_fill() {
let mut s = boards();
s.prefs.new_art_basic = false;
s.execute("select.none", &json!({})).unwrap();
assert!(s.doc().unwrap().selection.is_empty());
let red = s.paint.fill.clone();
s.begin_interaction("Flood Fill Artboards").unwrap();
s.preview(FLOOD, &json!({"color": "#0000ff"})).unwrap();
assert_ne!(s.paint.fill, red, "the previewed rectangles gave their color to the new-art fill");
s.cancel_interaction().unwrap();
assert_eq!(s.paint.fill, red, "the cancel puts the new-art fill back");
assert!(s.doc().unwrap().selection.is_empty());
}
#[test]
fn flood_fill_cancel_after_a_rejected_preview_keeps_the_new_art_fill() {
let mut s = boards();
s.prefs.new_art_basic = false;
s.execute("select.none", &json!({})).unwrap();
assert!(s.doc().unwrap().selection.is_empty());
let red = s.paint.fill.clone();
s.begin_interaction("Flood Fill Artboards").unwrap();
s.preview(FLOOD, &json!({"color": "#0000ff"})).unwrap();
assert_ne!(s.paint.fill, red);
assert!(s.preview(FLOOD, &json!({"color": [1e39, 0, 0]})).is_err());
assert_eq!(s.paint.fill, red);
s.cancel_interaction().unwrap();
assert_eq!(s.paint.fill, red);
assert!(s.doc().unwrap().selection.is_empty());
}
/// #4920: Export All Open Documents writes an SVG of each open document; a folder that can't be
/// written lists every document in `failed`, and either way the active document stays active.
#[test]
fn export_all_open_documents_writes_each_and_reports_failures() {
let dir = std::env::temp_dir().join(format!("vc-export-all-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&dir);
std::fs::create_dir_all(&dir).unwrap();
let mut s = Session::new();
s.execute("file.new", &json!({"width": 100, "height": 100, "name": "one"})).unwrap();
s.execute("file.new", &json!({"width": 100, "height": 100, "name": "two"})).unwrap();
s.execute("document.activate", &json!({"index": 0})).unwrap();
let r = s.execute("addon.document.exportAllOpen", &json!({"folder": dir, "format": "svg"})).unwrap();
assert_eq!(r["files"].as_array().unwrap().len(), 2, "{r}");
assert!(dir.join("one.svg").exists() && dir.join("two.svg").exists());
assert_eq!(s.active_index(), Some(0));
assert!(s.documents().iter().all(|d| d.path.is_none()), "the documents stay unsaved");
let missing = dir.join("missing").join("deeper");
let r = s.execute("addon.document.exportAllOpen", &json!({"folder": missing, "format": "svg"})).unwrap();
assert_eq!((r["files"].as_array().unwrap().len(), r["failed"].as_array().unwrap().len()), (0, 2), "{r}");
assert_eq!(s.active_index(), Some(0));
let _ = std::fs::remove_dir_all(&dir);
}
/// An SVG document last saved with linked images exports as one SVG with its images embedded: no
/// image file is written beside the copies (#4920 review).
#[test]
fn export_all_open_documents_writes_one_file_each() {
let dir = std::env::temp_dir().join(format!("vc-export-all-one-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&dir);
std::fs::create_dir_all(dir.join("out")).unwrap();
let mut s = Session::new();
s.execute("file.new", &json!({"width": 100, "height": 100, "name": "pic"})).unwrap();
let mut png = vec![];
{
let __dyn: image::DynamicImage = (image::RgbaImage::from_pixel(4, 4, image::Rgba([1, 2, 3, 255]))).into();
let (__w, __h) = (__dyn.width(), __dyn.height());
let (__layout, __sample, __raw) = match __dyn {
image::DynamicImage::ImageLuma8(b) => (craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8, b.into_raw()),
image::DynamicImage::ImageLumaA8(b) => (craft_image_codecs::ChannelLayout::GrayA, craft_image_codecs::SampleType::U8, b.into_raw()),
image::DynamicImage::ImageRgb8(b) => (craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8, b.into_raw()),
image::DynamicImage::ImageRgba8(b) => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, b.into_raw()),
image::DynamicImage::ImageLuma16(b) => (
craft_image_codecs::ChannelLayout::Gray,
craft_image_codecs::SampleType::U16,
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
),
image::DynamicImage::ImageLumaA16(b) => (
craft_image_codecs::ChannelLayout::GrayA,
craft_image_codecs::SampleType::U16,
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
),
image::DynamicImage::ImageRgb16(b) => (
craft_image_codecs::ChannelLayout::Rgb,
craft_image_codecs::SampleType::U16,
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
),
image::DynamicImage::ImageRgba16(b) => (
craft_image_codecs::ChannelLayout::Rgba,
craft_image_codecs::SampleType::U16,
b.into_raw().into_iter().flat_map(|s| s.to_ne_bytes()).collect(),
),
other => (craft_image_codecs::ChannelLayout::Rgba, craft_image_codecs::SampleType::U8, other.into_rgba8().into_raw()),
};
let __cimg = craft_image_codecs::Image::from_raw(__w, __h, __layout, __sample, __raw).unwrap();
craft_image_codecs::encode(&__cimg, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default())
.map(|__b| png.extend_from_slice(&__b))
}
.unwrap();
s.execute("file.place", &json!({"name": "a.png", "dataBase64": vectorcraft_format::base64_encode(&png)})).unwrap();
s.execute("document.save", &json!({"path": dir.join("pic.svg"), "format": "svg", "options": {"images": "link"}})).unwrap();
let saved: Vec<_> = std::fs::read_dir(&dir).unwrap().filter_map(|e| e.ok()).filter(|e| e.path().is_file()).collect();
assert!(saved.len() > 1, "the linked save wrote its image beside it: {saved:?}");
let out = dir.join("out");
let r = s.execute("addon.document.exportAllOpen", &json!({"folder": out, "format": "svg"})).unwrap();
let names: Vec<String> = std::fs::read_dir(&out).unwrap().map(|e| e.unwrap().file_name().to_string_lossy().into_owned()).collect();
assert_eq!(names, ["pic.svg"], "{r}");
assert!(std::fs::read_to_string(out.join("pic.svg")).unwrap().contains("data:image/png;base64,"), "the image is embedded");
let _ = std::fs::remove_dir_all(&dir);
}
/// A PDF document last saved with a page range exports with every page, and an SVG document with
/// two artboards exports as one SVG holding the art of both (#4920 review).
#[test]
fn export_all_open_documents_keeps_every_page_and_artboard() {
let dir = std::env::temp_dir().join(format!("vc-export-all-pages-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&dir);
std::fs::create_dir_all(dir.join("out")).unwrap();
let mut s = Session::new();
s.execute("file.new", &json!({"width": 100, "height": 100, "artboards": 2, "name": "pages"})).unwrap();
s.execute("document.save", &json!({"path": dir.join("pages.pdf"), "format": "pdf", "options": {"range": "1"}})).unwrap();
let pages = |b: &[u8]| {
String::from_utf8_lossy(b).matches("/Type /Page\n").count()
+ String::from_utf8_lossy(b).matches("/Type/Page/").count()
+ String::from_utf8_lossy(b).matches("/Type /Page/").count()
+ String::from_utf8_lossy(b).matches("/Type /Page ").count()
};
let first = pages(&std::fs::read(dir.join("pages.pdf")).unwrap());
let out = dir.join("out");
s.execute("addon.document.exportAllOpen", &json!({"folder": out, "format": "pdf"})).unwrap();
let copy = pages(&std::fs::read(out.join("pages.pdf")).unwrap());
assert!(copy > first, "the copy has every page: {first} saved, {copy} exported");
// SVG: one file with the art on both artboards.
let a = s.doc().unwrap().doc.artboards[1].rect;
s.execute("shape.rectangle", &json!({"x": a.x0 + 10.0, "y": a.y0 + 10.0, "width": 20, "height": 20})).unwrap();
s.execute("addon.document.exportAllOpen", &json!({"folder": out, "format": "svg"})).unwrap();
let svgs: Vec<String> =
std::fs::read_dir(&out).unwrap().map(|e| e.unwrap().file_name().to_string_lossy().into_owned()).filter(|n| n.ends_with(".svg")).collect();
assert_eq!(svgs, ["pages.svg"]);
let svg = std::fs::read_to_string(out.join("pages.svg")).unwrap();
// Not the first artboard alone (its 100 × 100 box at the origin): the art's own bounds.
let view = svg.split("viewBox=\"").nth(1).and_then(|v| v.split('"').next()).unwrap();
let w: f64 = view.split_whitespace().nth(2).unwrap().parse().unwrap();
assert!(w > 19.0 && w < 30.0, "the SVG shows the art (the square on the second artboard), not the first artboard: viewBox {view}");
let _ = std::fs::remove_dir_all(&dir);
}
/// A 24×24 white PNG with a dark (or red) square in it.
#[cfg(not(target_arch = "wasm32"))]
fn png(path: &Path, dark: bool) {
let mut img = image::RgbaImage::from_pixel(24, 24, image::Rgba([255, 255, 255, 255]));
for y in 6..18 {
for x in 6..18 {
img.put_pixel(x, y, if dark { image::Rgba([0, 0, 0, 255]) } else { image::Rgba([200, 30, 30, 255]) });
}
}
img.save(path).unwrap();
}
/// #4921: each image of the folder is traced and saved under its own name; a file that isn't an
/// image is left alone, one that can't be read is reported, and every document the batch opened
/// is closed again, the active one staying active.
#[cfg(not(target_arch = "wasm32"))]
#[test]
fn batch_trace_folder_traces_and_saves_each_image() {
const C: &str = "addon.document.batchTraceFolder";
let dir = folder("batch-trace");
let (src, out) = (dir.join("in"), dir.join("out"));
std::fs::create_dir_all(&src).unwrap();
png(&src.join("logo.png"), true);
png(&src.join("mark.png"), false);
std::fs::write(src.join("notes.txt"), "not an image").unwrap();
std::fs::write(src.join("broken.png"), b"not a png").unwrap();
let mut s = Session::new();
s.execute("file.new", &json!({"width": 100, "height": 100})).unwrap();
let docs = s.documents().len();
let r = s.execute(C, &json!({"folder": src, "destinationFolder": out, "format": "svg", "expand": true})).unwrap();
assert_eq!(r["traced"].as_array().unwrap().len(), 2, "{r}");
assert_eq!(r["failed"].as_array().unwrap().len(), 1, "{r}");
assert!(r["failed"][0]["path"].as_str().unwrap().ends_with("broken.png"));
let mut names: Vec<String> = std::fs::read_dir(&out).unwrap().map(|e| e.unwrap().file_name().to_string_lossy().into_owned()).collect();
names.sort();
assert_eq!(names, ["logo.svg", "mark.svg"]);
let svg = std::fs::read_to_string(out.join("logo.svg")).unwrap();
assert!(svg.contains("<path") && !svg.contains("<image"), "traced and expanded, not the image");
assert_eq!(s.documents().len(), docs, "the opened images are closed");
assert_eq!(s.active_index(), Some(0));
// Again: the taken names are numbered, nothing is replaced.
s.execute(C, &json!({"folder": src, "destinationFolder": out, "format": "svg"})).unwrap();
assert!(out.join("logo-2.svg").exists() && out.join("mark-2.svg").exists());
// Refused: no folders, an unknown preset or format, a folder without images.
for bad in [
json!({"destinationFolder": out}),
json!({"folder": src}),
json!({"folder": src, "destinationFolder": out, "preset": "No Such Preset"}),
json!({"folder": src, "destinationFolder": out, "format": "docx"}),
json!({"folder": out.join("empty"), "destinationFolder": out}),
] {
assert!(s.execute(C, &bad).is_err(), "{bad}");
}
let _ = std::fs::remove_dir_all(&dir);
}
/// A layered PSD is traced as its merged image: a white layer under a black square gives the
/// square's shape, not a trace of the white layer alone.
#[cfg(not(target_arch = "wasm32"))]
#[test]
fn batch_trace_folder_traces_a_layered_psd_as_its_merged_image() {
const C: &str = "addon.document.batchTraceFolder";
let dir = folder("batch-trace-psd");
let (src, out) = (dir.join("in"), dir.join("out"));
std::fs::create_dir_all(&src).unwrap();
let mut s = Session::new();
s.execute("file.new", &json!({"width": 24, "height": 24})).unwrap();
s.execute("paint.setStroke", &json!({"none": true})).unwrap();
s.execute("paint.setFill", &json!({"color": "#ffffff"})).unwrap();
s.execute("shape.rectangle", &json!({"x": 0, "y": 0, "width": 24, "height": 24})).unwrap();
s.execute("layer.new", &json!({"name": "Square"})).unwrap();
s.execute("select.none", &json!({})).unwrap();
s.execute("paint.setFill", &json!({"color": "#000000"})).unwrap();
s.execute("shape.rectangle", &json!({"x": 6, "y": 6, "width": 12, "height": 12})).unwrap();
let psd = s.execute("document.export", &json!({"format": "psd"})).unwrap();
std::fs::write(src.join("two.psd"), vectorcraft_format::base64_decode(psd["dataBase64"].as_str().unwrap()).unwrap()).unwrap();
let r =
s.execute(C, &json!({"folder": src, "destinationFolder": out, "format": "svg", "preset": "Black and White Logo", "expand": true})).unwrap();
assert_eq!(r["traced"].as_array().unwrap().len(), 1, "{r}");
let svg = std::fs::read_to_string(out.join("two.svg")).unwrap();
assert!(svg.contains("fill=\"#000000\""), "the black square is traced: {svg}");
let _ = std::fs::remove_dir_all(&dir);
}
/// Batch Trace Folder rejects optional values with the wrong type before opening images.
#[cfg(not(target_arch = "wasm32"))]
#[test]
fn batch_trace_folder_rejects_wrong_typed_options() {
const C: &str = "addon.document.batchTraceFolder";
let dir = folder("batch-trace-bad-options");
let src = dir.join("in");
let out = dir.join("out");
std::fs::create_dir_all(&src).unwrap();
let mut s = Session::new();
for (key, value) in [
("format", json!(42)),
("format", Value::Null),
("preset", json!(false)),
("preset", Value::Null),
("expand", json!("true")),
("expand", Value::Null),
("includeSubdirs", json!(1)),
("includeSubdirs", Value::Null),
] {
let mut params = json!({"folder": src, "destinationFolder": out});
params[key] = value;
let error = s.execute(C, &params).unwrap_err();
assert!(error.to_string().contains(&format!("`{key}`")), "{key}: {error}");
assert!(!out.exists(), "invalid options must not create a destination folder");
}
let _ = std::fs::remove_dir_all(&dir);
}
/// A folder too big to list in full is refused rather than traced in part.
#[cfg(not(target_arch = "wasm32"))]
#[test]
fn batch_trace_folder_refuses_a_folder_it_cannot_list_in_full() {
let dir = folder("batch-trace-big");
let src = dir.join("in");
std::fs::create_dir_all(&src).unwrap();
for i in 0..=super::batch_trace::MAX_FILES * 2 {
std::fs::write(src.join(format!("note-{i}.txt")), "").unwrap();
}
png(&src.join("logo.png"), true);
let mut s = Session::new();
let e = s.execute("addon.document.batchTraceFolder", &json!({"folder": src, "destinationFolder": dir.join("out")})).unwrap_err();
assert!(e.to_string().contains("more than"), "{e}");
assert!(!dir.join("out").exists(), "nothing traced");
let _ = std::fs::remove_dir_all(&dir);
}