Files
faunosteo/crates/core/tests/anatomy_compose.rs
bchanot 0da7d3ab9f test(domain): R5.7 port the contracts only the TS suites checked
612 leaves and 26 legacy ids (curation_leaves), region inference, R5.1 ledger rows,
generated folder lists, migration-map oracle entry by entry, overlay removal, halo
filter, CHG-000 and dollar-tag refusals; report-v2 fixtures moved to crates/core/tests
(sha256 unchanged).
2026-10-25 03:59:49 +01:00

181 lines
7.3 KiB
Rust

//! R3 O1 + O3: the Rust compositions against the TypeScript trees frozen before the switch.
//! O3 (`oracles/r3/highlight.json`, `packages/pdf/tests/r3.highlight-oracles.test.ts`): the
//! engine's highlighted view for every observation and view of the three reference reports
//! and an edge sheet. O1 (`apps/app/tests/fixtures/goldens/r3/canvas.*.json`,
//! `apps/app/tests/unit/canvas.trees.test.ts`): the editor canvas of each view with no state,
//! and its layer for every state set of the reference reports. Trees compared as structure,
//! text nodes included; the Rust tree is the composition serialised and read again.
#![allow(clippy::disallowed_methods, clippy::unwrap_used, clippy::expect_used)]
mod common;
use std::collections::HashSet;
use common::svg_tree::{reread, tree_value, view_names, with_trigger_names_per_language};
use faunosteo_core::anatomy::compose::{asset_id_of_view, compose_canvas, compose_highlight};
use faunosteo_core::anatomy::layer::render_layer;
use faunosteo_core::anatomy::svg::{Element, read_svg};
use faunosteo_core::domain::sheet::AnatomicalSheetDocument;
use serde::Deserialize;
use serde_json::{Value, json};
const HORSE: &str = "supabase/seed/anatomical-sheets/equine/draft/assets/horse.svg";
fn draft() -> AnatomicalSheetDocument {
let (_, draft) = common::oracle_file("sheet.draft.json");
AnatomicalSheetDocument::deserialize(&draft["output"]).unwrap()
}
fn sanitized(text: &str) -> Element {
read_svg(text).unwrap().root
}
/// The frozen case's root rebuilt: root attributes, children before the asset, the asset's
/// children (the shared sanitised tree, or the case's own), the layer.
fn expected(case: &Value, oracle: &Value) -> Value {
let mut children = case["before"].as_array().unwrap().clone();
match &case["asset"] {
Value::String(reference) => {
let id = reference.strip_prefix("asset:").unwrap();
children.extend(
oracle["sanitized"][id]["children"]
.as_array()
.unwrap()
.iter()
.cloned(),
);
}
own => children.extend(own.as_array().unwrap().iter().cloned()),
}
children.push(case["layer"].clone());
let root = &case["root"];
json!({ "ns": root["ns"], "name": root["name"], "attrs": root["attrs"], "children": children })
}
#[test]
fn every_highlighted_view_equals_the_engine_tree() {
let (_, oracle) = common::oracle_file("r3/highlight.json");
let horse = sanitized(&common::repo_file(HORSE));
let draft = draft();
let edge = AnatomicalSheetDocument::deserialize(&oracle["edgeDocument"]).unwrap();
let edge_assets = &oracle["edgeAssets"];
let edge_main = sanitized(edge_assets["main"].as_str().unwrap());
let edge_own = sanitized(edge_assets["own"].as_str().unwrap());
let cases = oracle["cases"].as_array().unwrap();
assert_eq!(cases.len(), 36);
for case in cases {
let view = case["viewId"].as_str().unwrap();
let is_edge = case["sheet"] == "edge";
let document = if is_edge { &edge } else { &draft };
let asset_id = asset_id_of_view(document, view).unwrap();
let asset = if !is_edge {
horse.clone()
} else if asset_id == edge_assets["ownAssetId"] {
edge_own.clone()
} else {
edge_main.clone()
};
let ids: HashSet<&str> = case["observation"]["zones"]
.as_array()
.unwrap()
.iter()
.filter(|z| z["viewId"] == view)
.map(|z| z["annotationId"].as_str().unwrap())
.collect();
let main = document
.views
.iter()
.find(|v| v.id() == view)
.unwrap()
.is_main();
let composed = compose_highlight(document, view, asset, &ids, main).unwrap();
let label = format!("{} {} {view}", case["sheet"], case["observation"]["id"]);
let mut frozen = expected(case, &oracle);
with_trigger_names_per_language(&mut frozen, &view_names(document));
assert_eq!(reread(&composed), frozen, "{label}");
}
}
/// A frozen canvas golden with the declared trigger-name change applied (draft views).
fn canvas_golden(name: &str) -> Value {
let text = common::repo_file(&format!("apps/app/tests/fixtures/goldens/r3/{name}"));
let mut frozen: Value = serde_json::from_str(&text).unwrap();
with_trigger_names_per_language(&mut frozen, &view_names(&draft()));
frozen
}
#[test]
fn every_canvas_equals_the_frozen_tree_and_every_state_set_its_layer() {
let horse = sanitized(&common::repo_file(HORSE));
let draft = draft();
for view in ["main", "head"] {
let canvas = compose_canvas(&draft, view, &horse).unwrap();
assert_eq!(
reread(&canvas),
canvas_golden(&format!("canvas.{view}.json")),
"{view}"
);
let layers: Value = serde_json::from_str(&common::repo_file(&format!(
"apps/app/tests/fixtures/goldens/r3/canvas.{view}.layers.json"
)))
.unwrap();
for (name, frozen) in layers.as_object().unwrap() {
let mut frozen = frozen.clone();
with_trigger_names_per_language(&mut frozen, &view_names(&draft));
let frozen = &frozen;
let states = frozen_states(frozen);
let ids: HashSet<&str> = states.iter().map(String::as_str).collect();
let layer = render_layer(&draft, view, &ids).unwrap();
assert_eq!(tree_value(&layer), *frozen, "{view} {name}");
}
}
}
/// The annotation ids a frozen layer marks `data-state="current"`.
fn frozen_states(layer: &Value) -> Vec<String> {
let mut ids = Vec::new();
for child in layer["children"].as_array().unwrap() {
let attrs = &child["attrs"];
if attrs["data-state"] == "current" {
ids.push(attrs["data-anno-id"].as_str().unwrap().to_owned());
}
}
ids
}
#[test]
fn a_label_xml_cannot_carry_is_refused_never_replaced() {
let mut draft = draft();
let horse = sanitized(&common::repo_file(HORSE));
if let Some(faunosteo_core::domain::sheet::Annotation::Observable(a)) =
draft.annotations.first_mut()
{
a.name.en = "bad\u{1}name".to_owned();
}
assert!(compose_canvas(&draft, "main", &horse).is_err());
}
fn count(element: &Element, matches: &dyn Fn(&Element) -> bool) -> usize {
usize::from(matches(element))
+ element
.element_children()
.map(|c| count(c, matches))
.sum::<usize>()
}
/// `render.compose.test.ts` "never duplicates the filter on a root that already holds it"
/// (R5.7): composing a drawing that already carries the halo filter adds no second filter and no
/// second `<defs>` (the TypeScript composed twice on one DOM root; the core takes the root it
/// returned).
#[test]
fn the_halo_filter_is_never_duplicated_on_a_root_that_holds_it() {
let draft = draft();
let ids: HashSet<&str> = ["hip"].into_iter().collect();
let horse = sanitized(&common::repo_file(HORSE));
let once = compose_highlight(&draft, "main", horse, &ids, true).unwrap();
let twice = compose_highlight(&draft, "main", once, &ids, true).unwrap();
let halo = |e: &Element| e.qname == "filter" && e.attr("id") == Some("osteo-halo");
assert_eq!(count(&twice, &halo), 1);
assert_eq!(count(&twice, &|e: &Element| e.qname == "defs"), 1);
}