feat(anatomy): view.canvas op on the loaded sheet; drawings read once at load
This commit is contained in:
1 parent
1ec9316a73
commit
842797b440
5 files changed
+168
No files matched your search
@@ -0,0 +1,83 @@
|
||||
//! R3 facade: `view.canvas` on the loaded sheet. The drawing is read once at load; a view
|
||||
//! answers its composed canvas, a rejected or unknown asset a closed kind for that view only.
|
||||
#![allow(clippy::disallowed_methods, clippy::unwrap_used, clippy::expect_used)]
|
||||
|
||||
mod common;
|
||||
|
||||
use common::svg_tree::tree_value;
|
||||
use faunosteo_core::anatomy::svg::tree::parse_svg;
|
||||
use faunosteo_core::domain::js::parse_json;
|
||||
use faunosteo_core::domain::sheet::{BundledSheetData, LoadedSheet};
|
||||
use faunosteo_core::facade::call_with_sheet;
|
||||
use serde_json::{Value, json};
|
||||
|
||||
const SEED: &str = "supabase/seed/anatomical-sheets/equine/draft";
|
||||
const HORSE_ID: &str = "7f767f74-3b36-51a7-8465-52dd7e2ead4a";
|
||||
|
||||
fn sheet_with(horse: &str) -> LoadedSheet {
|
||||
let payload = json!({
|
||||
"status": "draft",
|
||||
"ref": {
|
||||
"sheet_id": "s", "version_id": null, "document_sha256": "0".repeat(64),
|
||||
"schema_version": 2, "species_id": "equine"
|
||||
},
|
||||
"document": parse_json(&common::repo_file(&format!("{SEED}/document.json"))).unwrap(),
|
||||
"migrationMap": parse_json(&common::repo_file(&format!("{SEED}/report-migration-map.json"))).unwrap(),
|
||||
"assets": { HORSE_ID: horse }, "assetHashes": { HORSE_ID: "0".repeat(64) }
|
||||
});
|
||||
let data: BundledSheetData = serde_json::from_value(payload).unwrap();
|
||||
LoadedSheet::from_bundle(data).unwrap()
|
||||
}
|
||||
|
||||
fn call(sheet: &LoadedSheet, input: Value) -> Value {
|
||||
serde_json::from_str(&call_with_sheet(sheet, "view.canvas", &input.to_string())).unwrap()
|
||||
}
|
||||
|
||||
fn golden(name: &str) -> Value {
|
||||
serde_json::from_str(&common::repo_file(&format!(
|
||||
"apps/app/tests/fixtures/goldens/r3/{name}"
|
||||
)))
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn each_view_answers_the_frozen_canvas() {
|
||||
let sheet = sheet_with(&common::repo_file(&format!("{SEED}/assets/horse.svg")));
|
||||
for view in ["main", "head"] {
|
||||
let out = call(&sheet, json!({ "viewId": view }));
|
||||
assert_eq!(out["ok"], true, "{out}");
|
||||
let svg = out["value"]["svg"].as_str().unwrap();
|
||||
assert_eq!(
|
||||
tree_value(&parse_svg(svg).unwrap()),
|
||||
golden(&format!("canvas.{view}.json")),
|
||||
"{view}"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn closed_kinds_for_an_unknown_view_a_rejected_or_altered_drawing_and_bad_input() {
|
||||
let horse = common::repo_file(&format!("{SEED}/assets/horse.svg"));
|
||||
let sheet = sheet_with(&horse);
|
||||
assert_eq!(
|
||||
call(&sheet, json!({ "viewId": "nope" }))["kind"],
|
||||
"unknown_view"
|
||||
);
|
||||
assert_eq!(
|
||||
call(&sheet, json!({ "view": "main" }))["kind"],
|
||||
"invalid_shape"
|
||||
);
|
||||
let deep = format!(
|
||||
"<svg xmlns=\"http://www.w3.org/2000/svg\">{}</svg>",
|
||||
"<g>".repeat(80) + &"</g>".repeat(80)
|
||||
);
|
||||
let rejected = call(&sheet_with(&deep), json!({ "viewId": "main" }));
|
||||
assert_eq!(
|
||||
(rejected["kind"].as_str(), rejected["detail"].as_str()),
|
||||
(Some("svg_rejected"), Some("too_deep"))
|
||||
);
|
||||
let scripted = horse.replacen("<path", "<script>alert(1)</script><path onclick=\"x\"", 1);
|
||||
let altered = call(&sheet_with(&scripted), json!({ "viewId": "head" }));
|
||||
assert_eq!(altered["kind"], "svg_rejected");
|
||||
assert_eq!(altered["detail"], "removed script, path@onclick");
|
||||
}
|
||||
Reference in new issue
Block a user