fix(anatomy): R5.4 Markdown guard from a TypeScript sweep, strict drawing comparison, unwritable characters refused, indexed renderers

This commit is contained in:
bchanot committed 2026-10-23 11:51:41 +02:00
1 parent 0a79c52e62
commit 94f8b35105
14 files changed
+647 -50

No files matched your search

-1
View File
@@ -36,7 +36,6 @@ 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";
/// The files R5.3 produces; the review drawings and the Markdown reports are R5.4.
/// The text files of the draft, in the TypeScript order; the review files (drawings, the two
/// reports) are checked by `tests/curation_review.rs` (R5.4).
const DRAFT_FILES: [&str; 11] = [
+137
View File
@@ -0,0 +1,137 @@
//! The Markdown guard against the TypeScript (R5.4 final gates): every probe of
//! `oracles/r5/markdown-sweep.json` gives the raw reports the TypeScript wrote, then either the
//! TypeScript's formatted reports byte for byte or a refusal (`UnsupportedMarkdown`): the port
//! never writes a report that differs silently. Probes made of what the equine reports use are
//! never refused.
#![allow(clippy::disallowed_methods, clippy::unwrap_used, clippy::expect_used)]
mod common;
use faunosteo_core::curation::PipelineError;
use faunosteo_core::curation::draft::changelog::render_changelog;
use faunosteo_core::curation::draft::migration_map::{
MigrationMapInput, SheetRef, build_report_migration_map,
};
use faunosteo_core::curation::draft::review_report::{ReviewReportInput, render_review_report};
use faunosteo_core::curation::draft::stats::document_stats;
use faunosteo_core::curation::draft::{apply_changeset, parse_changeset, parse_references};
use faunosteo_core::curation::markdown::format_markdown;
use faunosteo_core::json::to_js_compact;
use serde_json::{Value, json};
fn oracle() -> Value {
serde_json::from_str(&common::repo_file(
"crates/core/tests/oracles/r5/markdown-sweep.json",
))
.unwrap()
}
/// The raw CHANGELOG and REVIEW-REPORT of a probe, as the port writes them.
fn raw_reports(frozen: &Value, case: &Value) -> (String, Result<String, PipelineError>) {
let baseline = &frozen["baseline"];
let references =
parse_references(&to_js_compact(&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: "s".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 changes: Vec<_> = changeset.changes.iter().map(|c| c.summary()).collect();
let changelog = render_changelog(&result.records, &before, &after, &stats).unwrap();
let review = render_review_report(&ReviewReportInput {
changes: &changes,
records: &result.records,
document: &result.document,
stats_before: &before,
legacy_anomalies: &[],
});
(changelog, review)
}
/// Characters the equine reports use outside code spans: a probe made only of these (with
/// single spaces) is never refused.
const EQUINE_SAFE: &str = "[]&#{}=<>\"':;()/%?-.,«»…≠°—+";
fn equine_safe(probe: &str) -> bool {
// A leading or trailing space becomes two spaces once the probe sits in its sentence.
!probe.contains(" ")
&& !probe.starts_with(' ')
&& !probe.ends_with(' ')
&& probe
.chars()
.all(|c| c.is_alphanumeric() || c == ' ' || EQUINE_SAFE.contains(c))
}
#[test]
fn every_probe_is_formatted_as_the_typescript_or_refused() {
let frozen = oracle();
let (mut same, mut refused, mut silent) = (0, 0, Vec::new());
for case in frozen["cases"].as_array().unwrap() {
let probe = case["probe"].as_str().unwrap();
let (changelog, review) = raw_reports(&frozen, case);
assert_eq!(
changelog,
case["changelog_raw"].as_str().unwrap(),
"{probe:?} raw"
);
if let Ok(review) = &review {
assert_eq!(
review,
case["review_raw"].as_str().unwrap(),
"{probe:?} raw"
);
}
let outcomes = [
(
format_markdown(&changelog),
case["changelog"].as_str().unwrap(),
),
(
review.and_then(|text| format_markdown(&text)),
case["review"].as_str().unwrap(),
),
];
let mut case_refused = false;
for (outcome, expected) in outcomes {
match outcome {
Ok(text) if text == expected => {}
Ok(_) => silent.push(probe.to_owned()),
Err(PipelineError::UnsupportedMarkdown(_) | PipelineError::UnsupportedWidth(_)) => {
case_refused = true;
}
Err(other) => panic!("{probe:?}: {other}"),
}
}
assert!(
!(case_refused && equine_safe(probe)),
"{probe:?}: refused, yet made of what the equine reports use"
);
if case_refused {
refused += 1
} else {
same += 1
}
}
silent.dedup();
assert!(
silent.is_empty(),
"silent differences for {} probes: {silent:?}",
silent.len()
);
assert_eq!(same + refused, 220);
println!("{same} identical, {refused} refused");
}
+63
View File
@@ -204,6 +204,42 @@ fn an_undeclared_drawing_difference_is_refused() {
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<?xml-stylesheet type=\"text/css\" href=\"https://attacker.invalid/x.css\"?>\n",
1,
);
let comment = committed.replacen("</svg>", "<!-- hidden --></svg>", 1);
let trailing = format!("{committed}<!-- after -->");
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("</svg>", "<!-- x --></svg>", 1);
assert!(compare_drawing(&head, &commented, Some(&rule)).is_err());
}
// ---------------------------------------------------------------------------------------------
// Synthetic cases written by the TypeScript (`oracles/r5/review-synthetic.json`).
@@ -358,3 +394,30 @@ fn case_document(frozen: &Value, case: &Value) -> Value {
.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}"
);
}
}
@@ -0,0 +1 @@
{"baseline":{"schema_version":2,"species_id":"equine","name":{"fr":"Mini","en":"Mini"},"description":{},"scope_disclaimer":{},"attribution":{"authors":[],"license":null,"notes":null},"vertebral_config":{"regions":[{"code":"C","name":{"fr":"Cervicales","en":"Cervical"},"count":7},{"code":"T","name":{"fr":"Thoraciques","en":"Thoracic"},"count":18}]},"references":[],"categories":[{"id":"joints","name":{"fr":"Articulations","en":"Joints"},"color":null},{"id":"bones","name":{"fr":"Os","en":"Bones"},"color":null}],"views":[{"id":"main","kind":"main","name":{"fr":"Vue","en":"View"},"svg_asset_id":"3f6c1b52-8a4e-4d17-9b0c-5e2a7d91c4f8","view_box":null},{"id":"head","kind":"detail","parent_view_id":"main","name":{"fr":"Tête","en":"Head"},"svg_asset_id":null,"view_box":{"min_x":0,"min_y":0,"width":10,"height":10}}],"annotations":[{"id":"hip","kind":"point","view_id":"main","category_id":"joints","laterality":"bilateral","geometry":{"type":"circle","cx":1,"cy":2,"r":3},"name":{"fr":"Hanche","en":"Hip"},"finding_zone_id":"hip","vertebral_region_code":null},{"id":"neck","kind":"zone","view_id":"main","category_id":"bones","laterality":"unpaired","geometry":null,"name":{"fr":"Cou","en":"Neck"},"finding_zone_id":"neck","vertebral_region_code":"C"},{"id":"head-trigger","kind":"detail_trigger","view_id":"main","category_id":"bones","laterality":"unpaired","geometry":null,"name":null,"opens_view_id":"head"}],"finding_zones":[{"id":"hip","name":{"fr":"Hanche","en":"Hip"},"categories":[{"id":"hip-dysfunctions","kind":"dysfunction","name":{"fr":"Dysfonctions","en":"Dysfunctions"},"children":[{"id":"mobility","name":{"fr":"mobility fr","en":"mobility en"},"children":[{"id":"flexion","name":{"fr":"flexion fr","en":"flexion en"}},{"id":"extension","name":{"fr":"extension fr","en":"extension en"}}]},{"id":"pain","name":{"fr":"pain fr","en":"pain en"}}]},{"id":"anamnesis","kind":"anamnesis","name":{"fr":"Anamnèse","en":"Anamnesis"},"children":[{"id":"trauma","name":{"fr":"trauma fr","en":"trauma en"}}]}]},{"id":"neck","name":{"fr":"Cou","en":"Neck"},"categories":[{"id":"neck-dysfunctions","kind":"dysfunction","name":{"fr":"Dysfonctions","en":"Dysfunctions"},"children":[{"id":"stiffness","name":{"fr":"stiffness fr","en":"stiffness en"}},{"id":"vet","name":{"fr":"vet fr","en":"vet en"},"requires_vet_referral":true}]}]}]},"references":[{"id":"atlas","type":"book","citation":"Atlas of anatomy","url":null,"doi":null},{"id":"paper","type":"journal","citation":"A paper","url":null,"doi":"10.1/x"}],"legacy_id_map":{"views":{"head":"head"},"annotations":{"legacy-hip":"hip","legacy-neck":"neck"},"categories":{},"finding_nodes":{},"assets":{}},"cases":[{"probe":"*","changes_file":{"changes":[{"id":"CHG-001","level":"C","kind":"correction","title":{"fr":"Titre *","en":"Title"},"rationale":"Raison * fin.","reference_ids":[],"question":"Question * ?","ops":[{"op":"add","target":"document","field":"references","value":{"id":"probe-ref","type":"web","citation":"Citation *","url":null,"doi":null}},{"op":"cite","target":"finding:hip/hip-dysfunctions/mobility","reference_ids":["probe-ref"]},{"op":"set","target":"finding:hip/hip-dysfunctions/mobility","field":"name.en","value":"Label *"}]}]},"changelog_raw":"# Journal des changements : révision de travail\n\nGénéré par `pnpm import:equine` : import historique (`legacy-import/`) puis changements rédigés (`curation/changes.json`). Ne pas modifier à la main : relancer la commande.\n\nNiveaux : A = erreur démontrée, B = amélioration étayée, C = point incertain. Les niveaux B et C demandent une confirmation professionnelle.\n\n## Comparaison quantitative\n\n| Indicateur | Avant | Après |\n| --- | --- | --- |\n| Vues | 2 | 2 |\n| Dessins SVG distincts | 1 | 1 |\n| Annotations | 3 | 3 |\n| Annotations (point) | 1 | 1 |\n| Annotations (zone) | 1 | 1 |\n| Annotations (detail_trigger) | 1 | 1 |\n| Annotations, revue (inherited) | 3 | 3 |\n| Annotations, revue (reviewed) | 0 | 0 |\n| Annotations, revue (to_confirm) | 0 | 0 |\n| Catégories d'annotation | 2 | 2 |\n| Zones de constats | 2 | 2 |\n| Catégories de constats | 3 | 3 |\n| Nœuds (catégories incluses) | 10 | 10 |\n| Feuilles | 6 | 6 |\n| Renvois vétérinaires | 1 | 1 |\n| Nœuds, revue (inherited) | 10 | 9 |\n| Nœuds, revue (reviewed) | 0 | 0 |\n| Nœuds, revue (to_confirm) | 0 | 1 |\n| Descriptions | 0 | 0 |\n| Descriptions en français seulement | 0 | 0 |\n| Références bibliographiques | 0 | 3 |\n| Nœuds cités | 0 | 1 |\n| Régions vertébrales avec plage documentée | 0 | 0 |\n\n## Migration des bilans v2\n\n| Nœuds | Nombre |\n| --- | --- |\n| Nœuds du catalogue historique | 10 |\n| Chemin identique | 10 |\n| Chemin renommé ou déplacé (suivi des renommages) | 0 |\n| Relogé sous une autre catégorie (correspondance unique) | 0 |\n| Retiré : le bilan garde le constat avec ses libellés figés | 0 |\n\n## Niveau A : erreurs démontrées\n\nAucun changement de ce niveau.\n\n## Niveau B : améliorations étayées (confirmatioLine truncated