Both composers append the marker layer as the root's last child, so a root visibility="hidden" whose children set visible back drew the drawing without a marker (0 marker pixels in Chromium and Firefox, PDF and canvas). The reader refuses any root visibility but visible (root_visibility); cleaning, verdict, preview, PDF gate and both composers answer it.
161 lines
6.1 KiB
Rust
161 lines
6.1 KiB
Rust
//! The product's readers of one drawing, for the reader-rule suites (`svg_reserved_ids`,
|
|
//! `svg_root_visibility`): a two-view sheet whose main view carries one point marker, its
|
|
//! preview, canvas and verdict, and the assertion that every reader refuses a drawing.
|
|
#![allow(dead_code, clippy::unwrap_used, clippy::expect_used)]
|
|
|
|
use faunosteo_core::anatomy::svg::clean::{CleaningOutcome, clean_svg};
|
|
use faunosteo_core::anatomy::svg::{Element, SvgRejection, read_svg};
|
|
use faunosteo_core::domain::sheet::AnatomicalSheetDocument;
|
|
use faunosteo_core::domain::sheet::preview::{
|
|
PreviewDrawing, PreviewDrawingStatus, PreviewInput, PreviewSheet, PreviewStatus,
|
|
};
|
|
use faunosteo_core::domain::sheet::verdict::{
|
|
DrawingResult, VerdictBinding, VerdictDrawing, VerdictInput, codes, compute_verdict, sha256_hex,
|
|
};
|
|
use faunosteo_core::facade::call_with_preview;
|
|
use serde_json::{Value, json};
|
|
|
|
pub const MAIN_ASSET: &str = "11111111-2222-4333-8444-555555555555";
|
|
pub const DETAIL_ASSET: &str = "66666666-7777-4888-9999-aaaaaaaaaaaa";
|
|
pub const OPEN: &str = concat!(
|
|
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n",
|
|
r#"<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 10 10">"#
|
|
);
|
|
|
|
pub fn svg(body: &str) -> String {
|
|
format!("{OPEN}{body}</svg>")
|
|
}
|
|
|
|
pub fn name(text: &str) -> Value {
|
|
json!({ "fr": text, "en": text })
|
|
}
|
|
|
|
/// A sheet with a main view (one point marker) and a detail view.
|
|
pub fn document() -> String {
|
|
json!({
|
|
"schema_version": 2, "species_id": "canine", "name": name("Chien"),
|
|
"description": {}, "scope_disclaimer": { "fr": "Outil", "en": "Tool" },
|
|
"attribution": { "authors": ["A"], "license": null, "notes": null },
|
|
"vertebral_config": { "regions": [] }, "references": [],
|
|
"categories": [{ "id": "c", "name": name("C"), "color": null }],
|
|
"views": [
|
|
{ "id": "main", "kind": "main", "name": name("Vue"), "svg_asset_id": MAIN_ASSET, "view_box": null },
|
|
{ "id": "detail", "kind": "detail", "name": name("Tête"), "svg_asset_id": DETAIL_ASSET,
|
|
"view_box": null, "parent_view_id": "main" }
|
|
],
|
|
"annotations": [{
|
|
"id": "hock", "kind": "point", "view_id": "main", "category_id": "c",
|
|
"laterality": "unpaired", "geometry": { "type": "circle", "cx": 5, "cy": 5, "r": 1 },
|
|
"review_status": "reviewed", "name": name("Jarret"), "finding_zone_id": null,
|
|
"vertebral_region_code": null
|
|
}],
|
|
"finding_zones": []
|
|
})
|
|
.to_string()
|
|
}
|
|
|
|
pub fn sheet() -> AnatomicalSheetDocument {
|
|
serde_json::from_str(&document()).unwrap()
|
|
}
|
|
|
|
pub fn drawing(asset_id: &str, text: &str) -> PreviewDrawing {
|
|
PreviewDrawing {
|
|
asset_id: asset_id.to_owned(),
|
|
sha256: sha256_hex(text.as_bytes()),
|
|
bytes: text.as_bytes().to_vec(),
|
|
}
|
|
}
|
|
|
|
pub fn preview(detail: &str) -> PreviewSheet {
|
|
let input = PreviewInput {
|
|
status: PreviewStatus::Draft,
|
|
document: document(),
|
|
fingerprint: None,
|
|
};
|
|
let main = svg("<path d=\"M0 0h1\"/>");
|
|
let drawings = vec![drawing(MAIN_ASSET, &main), drawing(DETAIL_ASSET, detail)];
|
|
PreviewSheet::load(input, drawings).unwrap()
|
|
}
|
|
|
|
pub fn canvas_outcome(preview: &PreviewSheet, view: &str) -> Value {
|
|
let input = json!({ "viewId": view }).to_string();
|
|
serde_json::from_str(&call_with_preview(preview, "view.canvas", &input)).unwrap()
|
|
}
|
|
|
|
pub fn verdict_detail(detail: &str) -> (bool, DrawingResult, Option<String>) {
|
|
let main = svg("<path d=\"M0 0h1\"/>");
|
|
let document = document();
|
|
let row = |asset_id: &'static str, text: &str| VerdictDrawing {
|
|
asset_id,
|
|
sha256: Box::leak(sha256_hex(text.as_bytes()).into_boxed_str()),
|
|
byte_size: text.len() as u64,
|
|
bytes: Some(Box::leak(text.as_bytes().to_vec().into_boxed_slice())),
|
|
};
|
|
let verdict = compute_verdict(&VerdictInput {
|
|
binding: VerdictBinding {
|
|
sheet_id: "s".to_owned(),
|
|
version_id: "v".to_owned(),
|
|
revision: 1,
|
|
review_id: "r".to_owned(),
|
|
},
|
|
document: &document,
|
|
base_document: None,
|
|
drawings: vec![row(MAIN_ASSET, &main), row(DETAIL_ASSET, detail)],
|
|
});
|
|
let checked = verdict
|
|
.drawings
|
|
.iter()
|
|
.find(|d| d.asset_id == DETAIL_ASSET)
|
|
.unwrap();
|
|
(
|
|
verdict.failures.iter().any(|f| f == codes::SVG_REJECTED),
|
|
checked.result,
|
|
checked.detail.clone(),
|
|
)
|
|
}
|
|
|
|
/// Every element holding `id`, in document order.
|
|
pub fn holders<'a>(element: &'a Element, id: &str, out: &mut Vec<&'a Element>) {
|
|
if element.attr("id") == Some(id) {
|
|
out.push(element);
|
|
}
|
|
for child in element.element_children() {
|
|
holders(child, id, out);
|
|
}
|
|
}
|
|
|
|
/// The drawing is refused with `reason` by every reader of the product: the reader, the
|
|
/// cleaning (no stored form), the preview and its canvas, the verdict.
|
|
pub fn assert_refused_everywhere(text: &str, reason: &str) {
|
|
assert_eq!(
|
|
read_svg(text).err().map(SvgRejection::reason),
|
|
Some(reason),
|
|
"{text}"
|
|
);
|
|
let cleaning = clean_svg(text.as_bytes());
|
|
assert_eq!(cleaning.outcome, CleaningOutcome::Rejected, "{text}");
|
|
assert_eq!(cleaning.reason.as_deref(), Some(reason), "{text}");
|
|
assert!(cleaning.preview.is_none() && cleaning.stored.is_none() && cleaning.sha256.is_none());
|
|
|
|
let preview = preview(text);
|
|
let state = preview.state().unwrap();
|
|
let detail = state
|
|
.drawings
|
|
.iter()
|
|
.find(|d| d.asset_id == DETAIL_ASSET)
|
|
.unwrap();
|
|
assert_eq!(detail.state, PreviewDrawingStatus::Rejected, "{text}");
|
|
assert_eq!(detail.detail.as_deref(), Some(reason), "{text}");
|
|
let refused = canvas_outcome(&preview, "detail");
|
|
assert_eq!(
|
|
(refused["kind"].as_str(), refused["detail"].as_str()),
|
|
(Some("svg_rejected"), Some(reason)),
|
|
"{text}"
|
|
);
|
|
|
|
let (failed, result, detail) = verdict_detail(text);
|
|
assert!(failed, "{text}");
|
|
assert_eq!(result, DrawingResult::SvgRejected, "{text}");
|
|
assert_eq!(detail.as_deref(), Some(reason), "{text}");
|
|
}
|