//! SVG through the file commands: imported centred where it's pasted, with warnings for what it //! left out, opened with them, undone in one step; clipboard text recognised as SVG. use base64::Engine as _; use serde_json::json; use crate::Session; use crate::params::id_of; const SVG: &str = r##""##; fn data(svg: &str) -> String { base64::engine::general_purpose::STANDARD.encode(svg) } #[test] fn imported_svg_is_editable_centred_and_reports_what_it_left_out() { let mut s = Session::new(); let r = s.execute("file.import", &json!({"data": data(SVG), "name": "Pasted SVG.svg", "x": 100, "y": 50, "center": true})).unwrap(); let id = id_of(&r["id"]).unwrap(); assert_eq!(s.selection(), [id]); let doc = s.doc().unwrap(); let frame = doc.node(id).unwrap(); assert_eq!((frame.name.as_str(), frame.width, frame.height), ("Pasted SVG", 40.0, 20.0)); let o = doc.world_transform(id).translation(); assert_eq!((o.x, o.y), (80.0, 40.0), "centred on the point"); let card = doc.subtree(id).into_iter().filter_map(|i| doc.node(i)).find(|n| n.name == "Card").unwrap(); assert_eq!((card.kind.type_name(), card.corners.radii[0]), ("RECTANGLE", 4.0)); assert!(r["warnings"][0].as_str().is_some_and(|w| w.contains("noise")), "{r}"); // Top-left at the point without `center`; one undo takes it away. let r = s.execute("file.import", &json!({"data": data(SVG), "name": "x.svg", "x": 100, "y": 50})).unwrap(); let id2 = id_of(&r["id"]).unwrap(); assert_eq!(s.doc().unwrap().world_transform(id2).translation().x, 100.0); s.execute("edit.undo", &json!({})).unwrap(); assert!(s.doc().unwrap().node(id2).is_none()); // Not SVG: an error, nothing added. let before = s.doc().unwrap().node_count(); assert!(s.execute("file.import", &json!({"data": data("", " \n", "\u{feff}\n\n\n", ] { assert!(crate::looks_like_svg(yes), "{yes}"); } for no in ["", "svg", "", "", "