//! The sheet document shape, ported from `packages/domain/tests/anatomical-sheet.schema.test.ts` //! with its exact accept / refuse cases. #![allow(clippy::disallowed_methods, clippy::unwrap_used, clippy::expect_used)] mod common; use faunosteo_core::domain::js::{is_stable_id, parse_json}; use faunosteo_core::domain::sheet::{ AnatomicalSheetDocument, Annotation, FindingNode, ReviewStatus, }; use serde_json::{Value, json}; fn slice_input() -> Value { let fixture = parse_json(&common::repo_file( "crates/core/tests/fixtures/horse-slice.json", )); fixture.unwrap()["input"].clone() } fn parses(doc: Value) -> bool { serde_json::from_value::(doc).is_ok() } /// JSON pointer of a path such as `["views", 0, "zoom"]`. fn pointer(path: &[Value]) -> String { path.iter() .map(|segment| match segment { Value::String(s) => format!("/{s}"), other => format!("/{other}"), }) .collect() } fn set(doc: &mut Value, path: &[Value], value: Value) { let (last, parent) = path.split_last().unwrap(); let target = doc.pointer_mut(&pointer(parent)).unwrap(); match (target, last) { (Value::Object(map), Value::String(key)) => { map.insert(key.clone(), value); } (Value::Array(items), Value::Number(n)) => { items[usize::try_from(n.as_u64().unwrap()).unwrap()] = value; } _ => panic!("bad path"), } } fn unset(doc: &mut Value, path: &[Value]) { let (last, parent) = path.split_last().unwrap(); let target = doc.pointer_mut(&pointer(parent)).unwrap(); target .as_object_mut() .unwrap() .remove(last.as_str().unwrap()); } #[test] fn accepts_the_horse_slice_and_its_own_output() { let doc: AnatomicalSheetDocument = serde_json::from_value(slice_input()).unwrap(); let again: AnatomicalSheetDocument = serde_json::from_value(serde_json::to_value(&doc).unwrap()).unwrap(); assert_eq!(again, doc); } /// One refused input: a label and the mutation of the lean fixture. type Rejection = (&'static str, Box); #[test] fn refuses_what_zod_refuses() { let cases: Vec = vec![ ( "an unknown top-level key", Box::new(|d| set(d, &[json!("extra")], json!(true))), ), ( "an unknown nested key", Box::new(|d| set(d, &[json!("views"), json!(0), json!("zoom")], json!(2))), ), ( "a stable id with spaces", Box::new(|d| { set( d, &[json!("annotations"), json!(0), json!("id")], json!("Hip Joint"), ) }), ), ( "a stable id with an accent", Box::new(|d| set(d, &[json!("categories"), json!(0), json!("id")], json!("É"))), ), ( "an empty stable id", Box::new(|d| { set( d, &[json!("finding_zones"), json!(0), json!("id")], json!(""), ) }), ), ( "schema_version 1", Box::new(|d| set(d, &[json!("schema_version")], json!(1))), ), ( "a non-uuid svg_asset_id", Box::new(|d| { set( d, &[json!("views"), json!(0), json!("svg_asset_id")], json!("horse"), ) }), ), ( "a polygon with 2 points", Box::new(|d| { set( d, &[ json!("annotations"), json!(1), json!("geometry"), json!("points"), ], json!([[0, 0], [1, 1]]), ) }), ), ( "a circle with r 0", Box::new(|d| { set( d, &[ json!("annotations"), json!(0), json!("geometry"), json!("r"), ], json!(0), ) }), ), ( "a region count of 0", Box::new(|d| { set( d, &[ json!("vertebral_config"), json!("regions"), json!(0), json!("count"), ], json!(0), ) }), ), ( "a missing en label", Box::new(|d| unset(d, &[json!("name"), json!("en")])), ), ( "a detail view without parent_view_id", Box::new(|d| unset(d, &[json!("views"), json!(1), json!("parent_view_id")])), ), ( "a detail_trigger with finding_zone_id", Box::new(|d| { set( d, &[json!("annotations"), json!(3), json!("finding_zone_id")], json!("hip"), ) }), ), ( "a javascript: reference url", Box::new(|d| { set( d, &[json!("references"), json!(0), json!("url")], json!("javascript:alert(1)"), ) }), ), ( "a reference type foo", Box::new(|d| { set( d, &[json!("references"), json!(0), json!("type")], json!("foo"), ) }), ), ( "a review_status foo", Box::new(|d| { set( d, &[json!("annotations"), json!(0), json!("review_status")], json!("foo"), ) }), ), ( "a count_range with min 0", Box::new(|d| { set( d, &[ json!("vertebral_config"), json!("regions"), json!(4), json!("count_range"), json!("min"), ], json!(0), ) }), ), ( "a finding category kind foo", Box::new(|d| { set( d, &[ json!("finding_zones"), json!(0), json!("categories"), json!(0), json!("kind"), ], json!("foo"), ) }), ), ]; for (label, mutate) in cases { let mut doc = slice_input(); mutate(&mut doc); assert!(!parses(doc), "accepted {label}"); } } #[test] fn stable_ids_follow_the_pattern() { for id in ["hip", "c7-t1", "view_main", "0"] { assert!(is_stable_id(id), "{id}"); } for id in ["Hip Joint", "É", "", "-hip", &"a".repeat(65)] { assert!(!is_stable_id(id), "{id}"); } } #[test] fn a_lean_node_gets_its_defaults_recursively() { let node: FindingNode = serde_json::from_value(json!({ "id": "articular", "name": { "fr": "Articulaire", "en": "Articular" }, "children": [{ "id": "flexion", "name": { "fr": "Flexion", "en": "Flexion" } }] })) .unwrap(); assert_eq!(serde_json::to_value(&node.description).unwrap(), json!({})); assert!(!node.requires_vet_referral); assert!(node.reference_ids.is_empty()); assert_eq!(node.review_status, ReviewStatus::Inherited); assert_eq!( serde_json::to_value(&node.children[0]).unwrap(), json!({ "id": "flexion", "name": { "fr": "Flexion", "en": "Flexion" }, "description": {}, "requires_vet_referral": false, "reference_ids": [], "review_status": "inherited", "children": [] }) ); } #[test] fn explicit_values_of_the_fixture_are_kept() { let doc: AnatomicalSheetDocument = serde_json::from_value(slice_input()).unwrap(); let lameness = &doc.finding_zones[0].categories[1].children[0]; assert!(lameness.requires_vet_referral); let trauma = &doc.finding_zones[0].categories[2].children[0]; assert_eq!(trauma.reference_ids, ["legacy-catalog"]); let regions = &doc.vertebral_config.regions; let range = regions[4].count_range.as_ref().unwrap(); assert_eq!((range.min.get(), range.max.get()), (15.0, 21.0)); assert!(regions[0].count_range.is_none()); let Annotation::Observable(first) = &doc.annotations[0] else { panic!("fixture") }; assert_eq!(first.review_status, ReviewStatus::Inherited); } #[test] fn the_lean_input_carries_no_default_key() { let input = slice_input(); let leaf = &input["finding_zones"][2]["categories"][0]["children"][0]; let keys: Vec<&String> = leaf.as_object().unwrap().keys().collect(); assert_eq!(keys, ["id", "name"]); }