//! The Rust review files against the TypeScript (R5.4): CHANGELOG.md and REVIEW-REPORT.md of the //! committed draft byte for byte, their raw texts against the ledger's pre-Prettier digests, the //! four review drawings byte for byte or, for the application's main drawing, as trees under //! the declared R3 rule; the synthetic cases of `oracles/r5/review-synthetic.json`; two runs //! byte-equal; an undeclared difference refused. #![allow(clippy::disallowed_methods, clippy::unwrap_used, clippy::expect_used)] mod common; use faunosteo_core::curation::PipelineError; use faunosteo_core::curation::draft::app_review::render_app_review_drawing; use faunosteo_core::curation::draft::changelog::render_changelog; use faunosteo_core::curation::draft::drawing::read_cleaned_drawing; use faunosteo_core::curation::draft::migration_map::{ MigrationMapInput, SheetRef, build_report_migration_map, }; use faunosteo_core::curation::draft::overlay::{OverlayInput, render_review_overlay}; use faunosteo_core::curation::draft::review_report::{ReviewReportInput, render_review_report}; use faunosteo_core::curation::draft::stats::document_stats; use faunosteo_core::curation::draft::svg_compare::{Match, TriggerNames, compare_drawing}; use faunosteo_core::curation::draft::{ DraftInput, DraftResult, apply_changeset, build_equine_draft, parse_changeset, parse_references, parse_view_box, render_draft_artifacts, }; use faunosteo_core::curation::ids::sha256_hex; use faunosteo_core::curation::legacy::types::Anomaly; use faunosteo_core::curation::legacy::{ EquineLegacyInput, HEAD_VIEW_ID, ImportResult, SvgSource, import_equine_legacy, }; use faunosteo_core::curation::prettier_json::format_json; use faunosteo_core::curation::report_guard::format_report; use faunosteo_core::domain::sheet::AnatomicalSheetDocument; use faunosteo_core::json::to_js_compact; use serde_json::{Map, Value}; const SOURCES: &str = "supabase/seed/anatomical-sheets/equine/legacy-sources"; const CURATION: &str = "supabase/seed/anatomical-sheets/equine/curation"; const DRAFT: &str = "supabase/seed/anatomical-sheets/equine/draft"; fn bytes(relative: &str) -> Vec { std::fs::read(common::repo_path(relative)).unwrap() } fn equine() -> (ImportResult, DraftResult) { let svg = |name: &str| SvgSource { file_name: name.to_owned(), bytes: bytes(&format!("{SOURCES}/{name}")), }; let result = import_equine_legacy(&EquineLegacyInput { main_svg: svg("Horse.svg"), head_svg: svg("Horse_head.svg"), catalog: bytes(&format!("{SOURCES}/pathologies.json")), vertebrae: bytes(&format!("{SOURCES}/vertebrae.ts")), }) .unwrap(); let references = parse_references(&common::repo_file(&format!("{CURATION}/references.json"))).unwrap(); let changeset = parse_changeset(&common::repo_file(&format!("{CURATION}/changes.json"))).unwrap(); let head = result.view_boxes.get(HEAD_VIEW_ID).and_then(Value::as_str); let draft = build_equine_draft(&DraftInput { import_result: &result, cleaned_svg: &bytes(&format!("{DRAFT}/assets/horse.svg")), head_view_box: parse_view_box(head).unwrap(), references: &references, changes: &changeset.changes, }) .unwrap(); (result, draft) } fn oracle(name: &str) -> Value { serde_json::from_str(&common::repo_file(&format!( "crates/core/tests/oracles/r5/{name}" ))) .unwrap() } fn typed(document: &Value) -> AnatomicalSheetDocument { serde_json::from_value(document.clone()).unwrap() } const REVIEW_FILES: [&str; 6] = [ "review/annotations-main.svg", "review/annotations-head.svg", "review/app-main.svg", "review/app-head.svg", "CHANGELOG.md", "REVIEW-REPORT.md", ]; #[test] fn the_review_files_of_the_committed_draft_are_reproduced() { let (result, draft) = equine(); let files = render_draft_artifacts(&draft, &result.anomalies).unwrap(); let rule = TriggerNames::of(&typed(&draft.document)); for name in REVIEW_FILES { let produced = &files.iter().find(|(file, _)| file == name).unwrap().1; let committed = common::repo_file(&format!("{DRAFT}/{name}")); let outcome = compare_drawing(produced, &committed, Some(&rule)); let expected = if name == "review/app-main.svg" { Match::TreeAfterRule } else { Match::Bytes }; assert_eq!(outcome, Ok(expected), "{name}"); } } /// The one declared difference of the drawings, measured: `app-main.svg` differs from the /// xmldom file in the trigger's English name and its title only, byte for byte elsewhere. #[test] fn app_main_differs_only_by_the_r3_trigger_names() { let (result, draft) = equine(); let files = render_draft_artifacts(&draft, &result.anomalies).unwrap(); let produced = &files .iter() .find(|(file, _)| file == "review/app-main.svg") .unwrap() .1; let committed = common::repo_file(&format!("{DRAFT}/review/app-main.svg")); let fr = "Tête (vue latérale gauche)"; let en = "Head (left lateral view)"; let expected = committed .replace( &format!("data-anno-name-en=\"{fr}\""), &format!("data-anno-name-en=\"{en}\""), ) .replace( &format!("{fr} / {fr}"), &format!("{fr} / {en}"), ); assert_ne!(expected, committed); assert_eq!(*produced, expected); } #[test] fn the_raw_reports_are_the_texts_prettier_received() { let (result, draft) = equine(); let document_json = format_json(&draft.document).unwrap(); let reference = SheetRef { sheet_id: draft.sheet_id.clone(), version_id: None, document_sha256: sha256_hex(document_json.as_bytes()), }; let (_, stats) = faunosteo_core::curation::draft::pipeline::draft_migration_map(&draft, &reference).unwrap(); let changelog = render_changelog( &draft.records, &draft.stats_before, &draft.stats_after, &stats, ) .unwrap(); let review = render_review_report(&ReviewReportInput { changes: &draft.changes, records: &draft.records, document: &draft.document, stats_before: &draft.stats_before, legacy_anomalies: &result.anomalies, }) .unwrap(); let ledger = oracle("pipeline.json")["artifacts"] .as_array() .unwrap() .clone(); for (file, raw) in [("CHANGELOG.md", changelog), ("REVIEW-REPORT.md", review)] { let row = ledger .iter() .find(|row| row["folder"] == DRAFT && row["file"] == file) .unwrap(); assert_eq!( sha256_hex(raw.as_bytes()), row["before_prettier_sha256"], "{file} raw" ); let formatted = format_report(&raw).unwrap(); assert_eq!( sha256_hex(formatted.as_bytes()), row["sha256"], "{file} formatted" ); } } #[test] fn two_runs_emit_the_same_review_files() { let (result, draft) = equine(); let first = render_draft_artifacts(&draft, &result.anomalies).unwrap(); let second = render_draft_artifacts(&draft, &result.anomalies).unwrap(); assert_eq!(first, second); } #[test] fn an_undeclared_drawing_difference_is_refused() { let committed = common::repo_file(&format!("{DRAFT}/review/annotations-main.svg")); let changed = committed.replacen("#1d4ed8", "#1d4ed9", 1); let (_, draft) = equine(); let rule = TriggerNames::of(&typed(&draft.document)); assert!(compare_drawing(&changed, &committed, None).is_err()); assert!(compare_drawing(&changed, &committed, Some(&rule)).is_err()); let app = common::repo_file(&format!("{DRAFT}/review/app-head.svg")); let moved = app.replacen("cx=\"", "cx=\"1", 1); assert!(compare_drawing(&moved, &app, Some(&rule)).is_err()); } /// The tree fallback hides nothing (final gate R5.4): a stylesheet processing instruction, a /// comment or another declared encoding in the committed application drawing is refused, /// although the trees would still be equal under the rule. #[test] fn the_rule_fallback_refuses_what_the_tree_does_not_carry() { let (result, draft) = equine(); let files = render_draft_artifacts(&draft, &result.anomalies).unwrap(); let produced = &files .iter() .find(|(file, _)| file == "review/app-main.svg") .unwrap() .1; let committed = common::repo_file(&format!("{DRAFT}/review/app-main.svg")); let rule = TriggerNames::of(&typed(&draft.document)); assert_eq!( compare_drawing(produced, &committed, Some(&rule)), Ok(Match::TreeAfterRule) ); let stylesheet = committed.replacen( "?>\n", "?>\n\n", 1, ); let comment = committed.replacen("", "", 1); let trailing = format!("{committed}"); let encoding = committed.replacen("encoding=\"UTF-8\"", "encoding=\"ISO-8859-1\"", 1); for variant in [stylesheet, comment, trailing, encoding] { assert_ne!(variant, committed); assert!(compare_drawing(produced, &variant, Some(&rule)).is_err()); } // A difference the rule does not touch, on a file the rule could apply to. let head = common::repo_file(&format!("{DRAFT}/review/app-head.svg")); let commented = head.replacen("", "", 1); assert!(compare_drawing(&head, &commented, Some(&rule)).is_err()); } // --------------------------------------------------------------------------------------------- // Synthetic cases written by the TypeScript (`oracles/r5/review-synthetic.json`). /// Cases the port refuses where the TypeScript wrote a file (declared in RUST.md §11.10 and §11.14). const DECLARED_REFUSALS: [&str; 1] = ["declared-emoji-in-a-cell"]; struct Rendered { changelog_raw: String, changelog: Result, review_raw: String, review: Result, drawings: Map, } fn render_case(frozen: &Value, case: &Value) -> Rendered { let baseline = &frozen["baseline"]; let references = parse_references( &to_js_compact(&serde_json::json!({ "references": frozen["references"] })).unwrap(), ) .unwrap(); let changeset = parse_changeset(&to_js_compact(&case["changes_file"]).unwrap()).unwrap(); let result = apply_changeset(baseline, &references, &changeset.changes).unwrap(); let before = document_stats(baseline).unwrap(); let after = document_stats(&result.document).unwrap(); let sheet = SheetRef { sheet_id: "11111111-1111-5111-8111-111111111111".to_owned(), version_id: None, document_sha256: "0".repeat(64), }; let (_, stats) = build_report_migration_map(&MigrationMapInput { baseline, final_document: &result.document, legacy_id_map: frozen["legacy_id_map"].as_object().unwrap(), records: &result.records, sheet: &sheet, main_view_id: "main", }) .unwrap(); let anomalies: Vec = frozen["anomalies"] .as_array() .unwrap() .iter() .map(|a| { let text = |key: &str| a[key].as_str().unwrap().to_owned(); let level = if a["level"] == "a_valider" { "a_valider" } else { "informatif" }; Anomaly { code: text("code"), level, subject: text("subject"), message: text("message"), } }) .collect(); let changes: Vec<_> = changeset.changes.iter().map(|c| c.summary()).collect(); let changelog_raw = render_changelog(&result.records, &before, &after, &stats).unwrap(); let review_raw = render_review_report(&ReviewReportInput { changes: &changes, records: &result.records, document: &result.document, stats_before: &before, legacy_anomalies: &anomalies, }) .unwrap(); let drawing = read_cleaned_drawing(frozen["drawing"].as_str().unwrap().as_bytes()).unwrap(); let document = typed(&result.document); let mut drawings = Map::new(); for view in &document.views { let id = view.id(); let overlay = render_review_overlay(&OverlayInput { drawing: &drawing, before: baseline, after: &result.document, view_id: id, }) .unwrap(); drawings.insert(format!("annotations-{id}"), Value::from(overlay)); } for view in &document.views { let app = render_app_review_drawing(&drawing, &document, view.id()).unwrap(); drawings.insert(format!("app-{}", view.id()), Value::from(app)); } Rendered { changelog: format_report(&changelog_raw), review: format_report(&review_raw), changelog_raw, review_raw, drawings, } } #[test] fn the_synthetic_review_cases_match_the_typescript_or_are_refused_as_declared() { let frozen = oracle("review-synthetic.json"); let cases = frozen["cases"].as_array().unwrap(); assert_eq!(cases.len(), 4); for case in cases { let name = case["name"].as_str().unwrap(); let rendered = render_case(&frozen, case); assert_eq!( rendered.changelog_raw, case["changelog_raw"].as_str().unwrap(), "{name}" ); assert_eq!( rendered.review_raw, case["review_raw"].as_str().unwrap(), "{name}" ); if DECLARED_REFUSALS.contains(&name) { let refused = rendered.changelog.unwrap_err(); assert!( matches!( refused, PipelineError::UnsupportedWidth(_) | PipelineError::UnsupportedMarkdown(_) ), "{name}" ); } else { assert_eq!( rendered.changelog.unwrap(), case["changelog"].as_str().unwrap(), "{name}" ); } // The review report and the drawings of a declared case are held too: the same text // or a declared refusal, never another text. match rendered.review { Ok(review) => assert_eq!(review, case["review"].as_str().unwrap(), "{name}"), Err(PipelineError::UnsupportedWidth(_) | PipelineError::UnsupportedMarkdown(_)) if DECLARED_REFUSALS.contains(&name) => {} Err(other) => panic!("{name}: {other}"), } let views = typed(&case_document(&frozen, case)).views.len(); assert_eq!(rendered.drawings.len(), 2 * views, "{name}"); let rule = TriggerNames::of(&typed(&case_document(&frozen, case))); for (key, produced) in &rendered.drawings { let committed = case["drawings"][key].as_str().unwrap(); let outcome = compare_drawing(produced.as_str().unwrap(), committed, Some(&rule)); assert!(outcome.is_ok(), "{name} {key}: {outcome:?}"); if key.starts_with("annotations-") { assert_eq!(outcome, Ok(Match::Bytes), "{name} {key}"); } } } } fn case_document(frozen: &Value, case: &Value) -> Value { let references = parse_references( &to_js_compact(&serde_json::json!({ "references": frozen["references"] })).unwrap(), ) .unwrap(); let changeset = parse_changeset(&to_js_compact(&case["changes_file"]).unwrap()).unwrap(); apply_changeset(&frozen["baseline"], &references, &changeset.changes) .unwrap() .document } /// A label XML cannot carry, or a carriage return xmldom wrote raw and the core escapes, never /// reaches a review drawing (final gate R5.4): the drawing is refused. #[test] fn a_label_the_drawing_cannot_carry_is_refused() { let frozen = oracle("review-synthetic.json"); let references = parse_references( &to_js_compact(&serde_json::json!({ "references": frozen["references"] })).unwrap(), ) .unwrap(); let drawing = read_cleaned_drawing(frozen["drawing"].as_str().unwrap().as_bytes()).unwrap(); for label in ["Hanche\u{1}", "Han\rche"] { let file = serde_json::json!({ "changes": [{ "id": "CHG-001", "level": "A", "kind": "technical", "title": { "fr": "Essai", "en": "Test" }, "rationale": "Essai", "reference_ids": [], "ops": [{ "op": "set", "target": "annotation:hip", "field": "name.fr", "value": label }], }]}); let changeset = parse_changeset(&to_js_compact(&file).unwrap()).unwrap(); let result = apply_changeset(&frozen["baseline"], &references, &changeset.changes).unwrap(); let error = render_app_review_drawing(&drawing, &typed(&result.document), "main").unwrap_err(); assert!( error.to_string().contains("cannot carry"), "{label:?}: {error}" ); } } /// An id an op writes is checked at the op (security gate R5.4 round 2): a line break or a /// backtick in an added, nested or renamed id, or in a field segment, would otherwise reach the /// reports' code spans through the records (forged sections) even when a later op removes the /// item. The committed drawing is read by the core's reader first (its depth cap holds). #[test] fn ids_and_field_segments_that_could_forge_report_structure_are_refused() { let frozen = oracle("review-synthetic.json"); let references = parse_references( &to_js_compact(&serde_json::json!({ "references": frozen["references"] })).unwrap(), ) .unwrap(); let forged = "x`\n## 11. Section forgée\n`y"; let annotation = serde_json::json!({ "id": forged, "kind": "point", "view_id": "main", "category_id": "bones", "laterality": "unpaired", "geometry": null, "name": { "fr": "a", "en": "a" }, "finding_zone_id": "neck", "vertebral_region_code": null, }); let node = serde_json::json!({ "id": "n1", "name": { "fr": "a", "en": "a" }, "children": [{ "id": forged, "name": { "fr": "a", "en": "a" } }] }); let cases = [ serde_json::json!([ { "op": "add", "target": "document", "field": "annotations", "value": annotation }, { "op": "remove", "target": format!("annotation:{forged}") }, ]), serde_json::json!([{ "op": "rename", "target": "annotation:hip", "id": forged }]), serde_json::json!([{ "op": "add", "target": "finding:hip/hip-dysfunctions/mobility", "value": node }]), serde_json::json!([{ "op": "set", "target": "finding:hip/hip-dysfunctions/mobility", "field": "name.x\ny", "value": "a" }]), serde_json::json!([{ "op": "set", "target": "finding:hip/hip-dysfunctions/mobility", "field": "name.x`y", "value": "a" }]), ]; for ops in cases { let file = serde_json::json!({ "changes": [{ "id": "CHG-001", "level": "A", "kind": "technical", "title": { "fr": "Essai", "en": "Test" }, "rationale": "Essai", "reference_ids": [], "ops": ops, }]}); let changeset = parse_changeset(&to_js_compact(&file).unwrap()).unwrap(); let error = apply_changeset(&frozen["baseline"], &references, &changeset.changes).unwrap_err(); assert!(error.to_string().starts_with("CHG-001 op 0:"), "{error}"); } // A segment outside the paths' alphabet: a `]` in a code span closed an author's `[` // (security gate R5.4 round 5). for field in ["name.x]", "name.a b", "name.x*", "name.é"] { let file = serde_json::json!({ "changes": [{ "id": "CHG-001", "level": "A", "kind": "technical", "title": { "fr": "Essai [", "en": "Test" }, "rationale": "Essai", "reference_ids": [], "ops": [{ "op": "set", "target": "finding:hip/hip-dysfunctions/mobility", "field": field, "value": "a" }], }]}); let changeset = parse_changeset(&to_js_compact(&file).unwrap()).unwrap(); let error = apply_changeset(&frozen["baseline"], &references, &changeset.changes).unwrap_err(); assert!( error.to_string().contains("segment outside [A-Za-z0-9_-]"), "{field}: {error}" ); } let deep = format!( "{}{}", "".repeat(100_000), "".repeat(100_000) ); let (_, draft) = equine(); let rule = TriggerNames::of(&typed(&draft.document)); assert!(compare_drawing("", &deep, Some(&rule)).is_err()); } /// Every review drawing repeats the whole cleaned drawing, two per view: the bytes are /// projected and bounded before any drawing is rendered (security gate R5.4 round 8). #[test] fn review_drawings_past_their_bound_are_refused_before_rendering() { let (result, mut draft) = equine(); let views = draft.document["views"].as_array_mut().unwrap(); let main = views[0].clone(); for index in 0..300 { let mut view = main.clone(); view["id"] = Value::from(format!("extra-{index}")); views.push(view); } let error = render_draft_artifacts(&draft, &result.anomalies).unwrap_err(); assert!( error.to_string().contains("past the 67108864 bound"), "{error}" ); } /// An unnamed trigger writes the opened view's names in its markup: many of them opening a /// long-named view are refused before rendering too (security gate R5.4 round 10: a 485 KB /// changeset wrote a 131 MB drawing). The equine draft stays far below the bound. #[test] fn unnamed_triggers_repeating_a_long_view_name_are_refused_before_rendering() { use faunosteo_core::curation::draft::pipeline::{ MAX_REVIEW_DRAWINGS_BYTES, review_drawings_bytes, }; let (result, mut draft) = equine(); let equine = review_drawings_bytes(&draft).unwrap(); println!("equine review drawings projected at {equine} bytes"); assert!(equine * 4 < MAX_REVIEW_DRAWINGS_BYTES, "{equine}"); draft.document["views"][1]["name"]["fr"] = Value::from("t".repeat(32_768)); let annotations = draft.document["annotations"].as_array_mut().unwrap(); let trigger = annotations .iter() .find(|a| a["kind"] == "detail_trigger") .unwrap() .clone(); for index in 0..1_000 { let mut copy = trigger.clone(); copy["id"] = Value::from(format!("trigger-{index}")); annotations.push(copy); } let error = render_draft_artifacts(&draft, &result.anomalies).unwrap_err(); assert!( error.to_string().contains("past the 67108864 bound"), "{error}" ); } /// The drawings projection is an upper bound of what is written: on the equine draft, and on /// a draft whose annotations are all gone, where only the drawings' fixed parts remain /// (verifier R5.4 round 11: 477 bytes over before the per-drawing allowance). #[test] fn the_drawings_projection_bounds_what_is_written() { use faunosteo_core::curation::draft::pipeline::review_drawings_bytes; // The drawings alone (`render_draft_artifacts` writes exactly these): a draft with no // annotation is no valid sheet, so since R5.6 it has no fingerprint and no full render. let written = |draft: &DraftResult, _anomalies: &[Anomaly]| -> usize { faunosteo_core::curation::draft::pipeline::review_drawings(draft) .unwrap() .iter() .map(|(_, text)| text.len()) .sum() }; let (result, mut draft) = equine(); assert!(review_drawings_bytes(&draft).unwrap() >= written(&draft, &result.anomalies)); draft.document["annotations"] = Value::Array(Vec::new()); draft.baseline["annotations"] = Value::Array(Vec::new()); assert!(review_drawings_bytes(&draft).unwrap() >= written(&draft, &result.anomalies)); } /// `curation.overlay.test.ts` "reports an annotation dropped by the changeset" (R5.7): an /// annotation of the baseline that the curated document no longer holds is drawn as removed. #[test] fn an_annotation_dropped_by_the_changeset_is_drawn_as_removed() { let frozen = oracle("review-synthetic.json"); let before = &frozen["baseline"]; let mut after = before.clone(); after["annotations"] .as_array_mut() .unwrap() .retain(|a| a["id"] != "hip"); let drawing = read_cleaned_drawing(frozen["drawing"].as_str().unwrap().as_bytes()).unwrap(); let overlay = render_review_overlay(&OverlayInput { drawing: &drawing, before, after: &after, view_id: "main", }) .unwrap(); let tree = roxmltree::Document::parse(&overlay).unwrap(); let status = |id: &str| { tree.descendants() .find(|n| n.attribute("data-review-id") == Some(id)) .and_then(|n| n.attribute("data-review-status")) }; assert_eq!(status("hip"), Some("removed")); assert_ne!(status("neck"), Some("removed")); }