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

+4
View File
@@ -126,3 +126,7 @@ required-features = ["curation"]
[[test]]
name = "curation_review"
required-features = ["curation"]
[[test]]
name = "curation_markdown"
required-features = ["curation"]
+1 -1
View File
@@ -206,7 +206,7 @@ fn compare_file(
match compare_drawing(produced, committed, rule) {
Ok(_) => {
println!(
"{}: declared: {name} (trees equal under the R3 trigger-name rule)",
"{}: declared: {name} (byte-identical once the R3 trigger-name rule is applied)",
folder.name
);
Ok(())
+1 -1
View File
@@ -33,5 +33,5 @@ pub fn render_app_review_drawing(
let layer = render_layer(document, view_id, &HashSet::new())
.map_err(|_| fail(format!("unknown view \"{view_id}\"")))?;
root.children.push(Node::Element(layer));
Ok(drawing_text(&drawing.prolog, &root))
drawing_text(&drawing.prolog, &root)
}
+32 -5
View File
@@ -4,8 +4,8 @@
//! root element: the XML declaration and its line break), kept verbatim in front of every
//! drawing the pipeline writes, as xmldom's serialiser kept it.
use crate::anatomy::svg::serialize::serialize;
use crate::anatomy::svg::{Element, read_svg};
use crate::anatomy::svg::serialize::{is_xml_text, serialize};
use crate::anatomy::svg::{Element, Node, read_svg};
use crate::curation::PipelineError;
pub struct CleanedDrawing {
@@ -35,7 +35,34 @@ pub fn read_cleaned_drawing(bytes: &[u8]) -> Result<CleanedDrawing, PipelineErro
})
}
/// The file text of a drawing built on the cleaned root.
pub fn drawing_text(prolog: &str, root: &Element) -> String {
format!("{prolog}{}", serialize(root))
/// A value or text the drawing cannot carry as the TypeScript wrote it: a code point XML 1.0
/// refuses, or a carriage return (xmldom wrote it raw in text, a parser reads it back as a line
/// feed; the core writes `&#13;`, which a parser keeps). Walked with an explicit stack.
fn first_unwritable(root: &Element) -> Option<String> {
let bad = |text: &str| !is_xml_text(text) || text.contains('\r');
let mut pending = vec![root];
while let Some(element) = pending.pop() {
if let Some(attr) = element.attrs.iter().find(|a| bad(&a.value)) {
return Some(attr.value.clone());
}
for child in &element.children {
match child {
Node::Element(child) => pending.push(child),
Node::Text(text) if bad(text) => return Some(text.clone()),
Node::Text(_) => {}
}
}
}
None
}
/// The file text of a drawing built on the cleaned root; refused when a label, an id or a path
/// holds what the drawing cannot carry (`first_unwritable`).
pub fn drawing_text(prolog: &str, root: &Element) -> Result<String, PipelineError> {
if let Some(text) = first_unwritable(root) {
return Err(PipelineError::Guard(format!(
"a review drawing cannot carry {text:?}"
)));
}
Ok(format!("{prolog}{}", serialize(root)))
}
+23 -13
View File
@@ -3,6 +3,8 @@
//! as an SVG layer, and the baseline position of every moved or removed annotation beside it
//! (dashed). Pure data, deterministic, opens in any browser.
use std::collections::{HashMap, HashSet};
use serde_json::Value;
use super::drawing::{CleanedDrawing, drawing_text};
@@ -84,17 +86,28 @@ fn with_geometry<'a>(doc: &'a Value, view_id: &str) -> impl Iterator<Item = &'a
}
/// `new Map(entries)`: the first position of an id, its last value.
fn previous_geometries<'a>(before: &'a Value, view_id: &str) -> Vec<(String, &'a Value)> {
let mut map: Vec<(String, &Value)> = Vec::new();
struct Previous<'a> {
entries: Vec<(String, &'a Value)>,
index: HashMap<String, usize>,
}
fn previous_geometries<'a>(before: &'a Value, view_id: &str) -> Previous<'a> {
let mut previous = Previous {
entries: Vec::new(),
index: HashMap::new(),
};
for annotation in with_geometry(before, view_id) {
let id = js_string(annotation.get("id"));
let geometry = &annotation["geometry"];
match map.iter_mut().find(|(known, _)| *known == id) {
Some(entry) => entry.1 = geometry,
None => map.push((id, geometry)),
match previous.index.get(&id) {
Some(&at) => previous.entries[at].1 = geometry,
None => {
previous.index.insert(id.clone(), previous.entries.len());
previous.entries.push((id, geometry));
}
}
}
map
previous
}
/// `overlayMarkers`: curated annotations with their status, then the baseline shapes they left.
@@ -104,10 +117,7 @@ fn overlay_markers<'a>(before: &'a Value, after: &'a Value, view_id: &str) -> Ve
for annotation in with_geometry(after, view_id) {
let id = js_string(annotation.get("id"));
let geometry = &annotation["geometry"];
let old = previous
.iter()
.find(|(known, _)| *known == id)
.map(|(_, g)| *g);
let old = previous.index.get(&id).map(|&at| previous.entries[at].1);
let status = match old {
None => Status::Added,
Some(old) if js_deep_equal(old, geometry) => Status::Unchanged,
@@ -126,11 +136,11 @@ fn overlay_markers<'a>(before: &'a Value, after: &'a Value, view_id: &str) -> Ve
});
}
}
let kept: Vec<String> = annotations(after)
let kept: HashSet<String> = annotations(after)
.iter()
.map(|a| js_string(a.get("id")))
.collect();
for (id, geometry) in previous {
for (id, geometry) in previous.entries {
if !kept.contains(&id) {
markers.push(Marker {
id,
@@ -361,5 +371,5 @@ pub fn render_review_overlay(input: &OverlayInput<'_>) -> Result<String, Pipelin
}
legend(&mut layer, (at(0) + font_size, at(1)), font_size);
root.children.push(Node::Element(layer));
Ok(drawing_text(&input.drawing.prolog, &root))
drawing_text(&input.drawing.prolog, &root)
}
+78 -20
View File
@@ -5,7 +5,7 @@
//! alignment.
use std::cmp::Ordering;
use std::collections::HashSet;
use std::collections::{HashMap, HashSet};
use serde_json::Value;
@@ -44,13 +44,28 @@ fn codes(items: &[String]) -> String {
.join(", ")
}
fn subjects(input: &ReviewReportInput<'_>, change: &ChangeSummary) -> String {
let own = input
.records
.iter()
.filter(|record| record.change.id == change.id)
.map(record_subject);
codes(&distinct(own))
fn subjects(subjects: &HashMap<&str, Vec<String>>, change: &ChangeSummary) -> String {
codes(
subjects
.get(change.id.as_str())
.map_or(&[][..], Vec::as_slice),
)
}
/// The distinct subjects of each change's records, grouped in one pass.
fn subjects_by_change(records: &[ChangeRecord]) -> HashMap<&str, Vec<String>> {
let mut grouped: HashMap<&str, Vec<String>> = HashMap::new();
let mut seen: HashSet<(&str, String)> = HashSet::new();
for record in records {
let subject = record_subject(record);
if seen.insert((record.change.id.as_str(), subject.clone())) {
grouped
.entry(record.change.id.as_str())
.or_default()
.push(subject);
}
}
grouped
}
fn references(ids: &[String]) -> String {
@@ -61,7 +76,7 @@ fn references(ids: &[String]) -> String {
}
}
fn change_line(input: &ReviewReportInput<'_>, change: &ChangeSummary) -> String {
fn change_line(grouped: &HashMap<&str, Vec<String>>, change: &ChangeSummary) -> String {
let confirm = if change.level.needs_confirmation() {
"oui"
} else {
@@ -79,17 +94,18 @@ fn change_line(input: &ReviewReportInput<'_>, change: &ChangeSummary) -> String
level_letter(change.level),
kind_label(change.kind),
change.rationale,
subjects(input, change),
subjects(grouped, change),
references(&change.reference_ids),
)
}
fn change_list(input: &ReviewReportInput<'_>, keep: impl Fn(&ChangeSummary) -> bool) -> String {
let grouped = subjects_by_change(input.records);
let lines: Vec<String> = input
.changes
.iter()
.filter(|change| keep(change))
.map(|change| change_line(input, change))
.map(|change| change_line(&grouped, change))
.collect();
if lines.is_empty() {
NONE.to_owned()
@@ -220,17 +236,24 @@ fn clinical_vigilance(input: &ReviewReportInput<'_>) -> Result<String, PipelineE
.join("\n"))
}
fn citation_count(document: &Value, id: &str) -> Result<usize, PipelineError> {
let wanted = Value::from(id);
let mut count = 0;
/// Citing findings per reference id, counted in one walk: a finding citing an id twice
/// counts once, as `ids.includes(id)` did.
fn citation_counts(document: &Value) -> Result<HashMap<String, usize>, PipelineError> {
let mut counts: HashMap<String, usize> = HashMap::new();
for entry in all_findings(document)? {
let cited = at(document, &entry.path)?
let ids = at(document, &entry.path)?
.get("reference_ids")
.and_then(Value::as_array)
.is_some_and(|ids| ids.contains(&wanted));
count += usize::from(cited);
.and_then(Value::as_array);
let distinct: HashSet<&str> = ids
.into_iter()
.flatten()
.filter_map(Value::as_str)
.collect();
for id in distinct {
*counts.entry(id.to_owned()).or_default() += 1;
}
}
Ok(count)
Ok(counts)
}
/// `Boolean(value)` of a reference field.
@@ -247,6 +270,7 @@ fn bibliography(document: &Value) -> Result<String, PipelineError> {
.get("references")
.and_then(Value::as_array)
.map_or(&[][..], Vec::as_slice);
let counts = citation_counts(document)?;
let mut lines = Vec::new();
for (index, reference) in references.iter().enumerate() {
let links: Vec<String> = [
@@ -267,7 +291,7 @@ fn bibliography(document: &Value) -> Result<String, PipelineError> {
index + 1,
code(Some(&id)),
js_string(reference.get("citation")),
citation_count(document, &id)?
counts.get(&id).copied().unwrap_or(0)
));
}
Ok(if lines.is_empty() {
@@ -330,8 +354,42 @@ fn intro() -> String {
.join("\n")
}
/// A control character (a line break, a tab, a carriage return…) in an authored text the
/// report writes in a list item would break the item, and Prettier re-indents what follows:
/// refused (the TypeScript wrote it raw).
fn assert_single_line(text: &str) -> Result<(), PipelineError> {
if text.chars().any(char::is_control) {
return Err(PipelineError::UnsupportedMarkdown(text.to_owned()));
}
Ok(())
}
fn assert_list_items_single_line(input: &ReviewReportInput<'_>) -> Result<(), PipelineError> {
for change in input.changes {
assert_single_line(&change.title.fr)?;
assert_single_line(&change.rationale)?;
if let Some(question) = &change.question {
assert_single_line(question)?;
}
}
let references = input.document.get("references").and_then(Value::as_array);
for reference in references.into_iter().flatten() {
for key in ["citation", "url", "doi"] {
assert_single_line(&js_string(reference.get(key)))?;
}
}
for record in input.records {
if record.field.as_deref() == Some("requires_vet_referral") {
assert_single_line(&compact_value(&record.before)?)?;
assert_single_line(&compact_value(&record.after)?)?;
}
}
Ok(())
}
/// `renderReviewReport`: the raw text, before the table alignment.
pub fn render_review_report(input: &ReviewReportInput<'_>) -> Result<String, PipelineError> {
assert_list_items_single_line(input)?;
let is = |level: ChangeLevel| move |c: &ChangeSummary| c.level == level;
let sections: Vec<(&str, String)> = vec![
("1. Qualité des données initiales", initial_quality(input)),
+30 -8
View File
@@ -1,11 +1,13 @@
//! How a produced review drawing is compared with the committed xmldom file: byte for byte,
//! else as parsed trees after a declared rule. One rule exists: the R3 owner decision
//! else byte for byte after a declared rule applied to the committed file's tree (strict: the
//! committed file must rebuild to itself from its tree, so nothing outside the tree can hide). One rule exists: the R3 owner decision
//! (2026-10-15) that a trigger without a name of its own carries the opened view's name in
//! each language, where the TypeScript renderer wrote the French name twice. It is applied to
//! the committed tree (the frozen file stays as it is): on an element that opens a view, a
//! `data-anno-name-en` equal to the view's French name takes its English name, and a title
//! `FR / FR` under it reads `FR / EN`. Any other difference is reported.
use crate::anatomy::svg::serialize::serialize;
use crate::anatomy::svg::tree::parse_svg;
use crate::anatomy::svg::{Element, Node};
use crate::domain::sheet::AnatomicalSheetDocument;
@@ -101,7 +103,18 @@ fn first_difference(a: &Element, b: &Element, path: &str) -> Option<String> {
})
}
/// Compares a produced drawing with the committed one: bytes, else trees after `rule`.
/// The prolog (the bytes before the root element) and the text after it.
fn outside_root(text: &str) -> Result<(&str, &str), String> {
let document = roxmltree::Document::parse(text).map_err(|error| error.to_string())?;
let range = document.root_element().range();
Ok((&text[..range.start], &text[range.end..]))
}
/// Compares a produced drawing with the committed one: byte identity, else the committed file
/// rebuilt from its tree must be byte-identical to itself (so the tree carries all of it: no
/// comment, processing instruction, encoding or entity form the comparison could miss), the
/// rule must change that tree, and the rebuilt text after the rule must be byte-identical to
/// the produced one.
pub fn compare_drawing(
produced: &str,
committed: &str,
@@ -113,11 +126,20 @@ pub fn compare_drawing(
let Some(rule) = rule else {
return Err("bytes differ and no tree rule is declared".to_owned());
};
let produced = parse_svg(produced).map_err(|e| format!("produced: {}", e.reason()))?;
let mut committed = parse_svg(committed).map_err(|e| format!("committed: {}", e.reason()))?;
rule.apply(&mut committed);
match first_difference(&produced, &committed, "") {
None => Ok(Match::TreeAfterRule),
Some(difference) => Err(difference),
let (prolog, epilog) = outside_root(committed).map_err(|e| format!("committed: {e}"))?;
let tree = parse_svg(committed).map_err(|e| format!("committed: {}", e.reason()))?;
if format!("{prolog}{}{epilog}", serialize(&tree)) != committed {
return Err("the committed file holds what its tree does not carry".to_owned());
}
let mut ruled = tree.clone();
rule.apply(&mut ruled);
if ruled == tree {
return Err("bytes differ and the declared rule changes nothing".to_owned());
}
if format!("{prolog}{}{epilog}", serialize(&ruled)) == produced {
return Ok(Match::TreeAfterRule);
}
let produced_tree = parse_svg(produced).map_err(|e| format!("produced: {}", e.reason()))?;
Err(first_difference(&produced_tree, &ruled, "")
.unwrap_or_else(|| "the texts differ outside the trees".to_owned()))
}
+99 -1
View File
@@ -67,11 +67,105 @@ pub fn code(value: Option<&str>) -> String {
value.map_or_else(|| "-".to_owned(), |text| format!("`{text}`"))
}
/// The plain text of a raw line Prettier would rewrite, found in its parts outside code spans
/// (`oracles/r5/markdown-sweep.json`): `*` outside a `**strong**` span; an underscore not between two letters or digits; a
/// backslash before a space or at the end, or an authored one before the cell escape `\|`
/// (`\"` in a JSON value is kept as written); two spaces in a row, a tab, a carriage return.
/// The parts of plain text outside the `**strong**` spans Prettier keeps as written (the
/// opening and closing pairs bounded by a non-word character or the line's edge, the inside
/// non-empty, without `*` and without an outer space). `None` when a `*` is left outside
/// such a span.
fn outside_strong(plain: &str) -> Option<String> {
let mut out = String::new();
let mut rest = plain;
let edge = |c: Option<char>| c.is_none_or(|c| !c.is_alphanumeric());
while let Some(open) = rest.find("**") {
let inside = &rest[open + 2..];
let close = inside.find("**")?;
let content = &inside[..close];
let after = inside[close + 2..].chars().next();
let valid = !content.is_empty()
&& !content.contains('*')
&& !content.starts_with(' ')
&& !content.ends_with(' ')
&& edge(rest[..open].chars().next_back())
&& edge(after);
if !valid {
return None;
}
out.push_str(&rest[..open]);
out.push_str(content);
rest = &inside[close + 2..];
}
out.push_str(rest);
(!out.contains('*')).then_some(out)
}
fn plain_rewrite(plain: &str) -> bool {
let Some(plain) = outside_strong(plain) else {
return true;
};
let chars: Vec<char> = plain.chars().collect();
let word = |i: Option<usize>| {
i.and_then(|i| chars.get(i))
.is_some_and(|c| c.is_alphanumeric())
};
chars.iter().enumerate().any(|(i, &c)| match c {
'*' | '\t' | '\r' => true,
'_' => !(word(i.checked_sub(1)) && word(Some(i + 1))),
'\\' => match chars.get(i + 1) {
None | Some(' ') => true,
Some('|') => i.checked_sub(1).and_then(|p| chars.get(p)) == Some(&'\\'),
Some(_) => false,
},
' ' => chars.get(i + 1) == Some(&' '),
_ => false,
})
}
/// Refuses a raw line holding Markdown the port does not reproduce, cell by cell in a table
/// row: an odd number of backticks, an empty code span or one with an outer space, or plain
/// text Prettier would rewrite (`plain_rewrite`). The report is then not written at all.
fn guard_line(line: &str) -> Result<(), PipelineError> {
if line.starts_with('|') {
// A table row: each cell as GFM reads it (the padding around the pipes is layout).
for cell in split_row(line) {
guard_text(&cell).map_err(|_| PipelineError::UnsupportedMarkdown(line.to_owned()))?;
}
return Ok(());
}
guard_text(line)
}
fn guard_text(line: &str) -> Result<(), PipelineError> {
let parts: Vec<&str> = line.split('`').collect();
let refuse = || Err(PipelineError::UnsupportedMarkdown(line.to_owned()));
if parts.len().is_multiple_of(2) {
return refuse();
}
for (index, part) in parts.iter().enumerate() {
let code = index % 2 == 1;
let bad = if code {
part.is_empty() || part.starts_with(' ') || part.ends_with(' ')
} else {
plain_rewrite(part)
};
if bad {
return refuse();
}
}
Ok(())
}
/// What `formatMarkdown` does to a report written by `table()`: every table aligned (cells
/// padded to the widest of their column, three at least, separator dashes to the same
/// width), every other line kept.
/// width), every other line kept. A text with Markdown Prettier would rewrite otherwise is
/// refused (`guard_line`).
pub fn format_markdown(raw: &str) -> Result<String, PipelineError> {
let lines: Vec<&str> = raw.split('\n').collect();
for line in &lines {
guard_line(line)?;
}
let mut out: Vec<String> = Vec::with_capacity(lines.len());
let mut index = 0;
while index < lines.len() {
@@ -132,6 +226,10 @@ fn split_row(line: &str) -> Vec<String> {
fn align(lines: &[&str]) -> Result<Vec<String>, PipelineError> {
let rows: Vec<Vec<String>> = lines.iter().map(|line| split_row(line)).collect();
let columns = rows.first().map_or(0, Vec::len);
// A row with another cell count would lose or invent a cell where Prettier keeps it.
if let Some(index) = rows.iter().position(|row| row.len() != columns) {
return Err(PipelineError::UnsupportedMarkdown(lines[index].to_owned()));
}
let mut widths = vec![3; columns];
let content = rows.iter().enumerate().filter(|&(index, _)| index != 1);
for (_, row) in content {
+7
View File
@@ -28,6 +28,10 @@ pub enum PipelineError {
/// A text holds a character whose Prettier display width is not reproduced (emoji
/// sequences): the layout would differ, so nothing is written.
UnsupportedWidth(String),
/// A report text holds Markdown Prettier would rewrite (emphasis, escapes, whitespace
/// runs, line breaks in a list item…), which the port does not reproduce: nothing is
/// written rather than a report that differs silently.
UnsupportedMarkdown(String),
}
impl std::fmt::Display for PipelineError {
@@ -37,6 +41,9 @@ impl std::fmt::Display for PipelineError {
Self::UnsupportedWidth(text) => {
write!(f, "display width not reproduced for {text:?} (emoji)")
}
Self::UnsupportedMarkdown(text) => {
write!(f, "Markdown formatting not reproduced for {text:?}")
}
}
}
}
-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
@@ -0,0 +1,171 @@
/**
* R5.4 freeze (final gates, 2026-10-23): what Prettier's Markdown printer does to authored text
* in the two draft reports. The port reproduces only the table alignment and refuses the text
* Prettier would rewrite; this sweep, written by the TypeScript, holds it to that: every probe
* (a special character in six positions, whitespace patterns, Markdown constructs) is placed
* in a change title, rationale and question, a cited reference and a changed label, and the
* raw and formatted CHANGELOG and REVIEW-REPORT are frozen. `MAKE_R5_ORACLES=1` writes
* crates/core/tests/oracles/r5/markdown-sweep.json.
*/
import { mkdirSync, readFileSync, writeFileSync } from 'node:fs'
import { dirname, resolve } from 'node:path'
import { fileURLToPath } from 'node:url'
import { describe, expect, it } from 'vitest'
import {
applyChangeset,
buildReportMigrationMap,
ChangesetSchema,
documentStats,
renderChangelog,
renderReviewReport,
} from '../src'
import { changeSummary } from '../src/curation/schema'
import { formatMarkdown } from '../src/format'
import { MINI_REFERENCES, miniSheet } from './fixtures/mini-sheet'
const HERE = dirname(fileURLToPath(import.meta.url))
const OUT = resolve(HERE, '../../../crates/core/tests/oracles/r5')
const MAKE = process.env.MAKE_R5_ORACLES === '1'
const LEGACY_ID_MAP = {
views: { head: 'head' },
annotations: { 'legacy-hip': 'hip', 'legacy-neck': 'neck' },
categories: {},
finding_nodes: {},
assets: {},
}
const CHARACTERS = [...'*_~\\`<>&[]!#$|+=^{}"\'%@:;?/()-.,']
function positions(c: string): string[] {
return [c, `${c}x`, `x${c}`, `a ${c} b`, `a${c}b`, `${c}${c}`]
}
const PATTERNS = [
'a b',
'a\tb',
' a',
'a ',
'a\nb',
'a\rb',
'a\n\nb',
'*important*',
'**gras**',
'a **b c** d',
'a**b**c',
'__init__ et _x',
'to_confirm',
'`code`',
'``double``',
'a\\|b',
'a|b',
'<b>gras</b>',
'[lien](https://example.org/x)',
'&amp;',
'# titre',
'1. item',
'- item',
'> citation',
'Carmalt 2013 (≠ « palpation »), 3 %, 40 °C',
'a = b; c ? d : e',
'{"fr":"x"}',
'é ü ß ø Ω 中',
]
function probes(): string[] {
return [...CHARACTERS.flatMap(positions), ...PATTERNS]
}
function changesFile(probe: string) {
return {
changes: [
{
id: 'CHG-001',
level: 'C',
kind: 'correction',
title: { fr: `Titre ${probe}`, en: 'Title' },
rationale: `Raison ${probe} fin.`,
reference_ids: [],
question: `Question ${probe} ?`,
ops: [
{
op: 'add',
target: 'document',
field: 'references',
value: {
id: 'probe-ref',
type: 'web',
citation: `Citation ${probe}`,
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 ${probe}`,
},
],
},
],
}
}
async function run(probe: string) {
const file = changesFile(probe)
const changeset = ChangesetSchema.parse(file)
const baseline = miniSheet()
const result = applyChangeset(baseline, MINI_REFERENCES, changeset)
const before = documentStats(baseline)
const after = documentStats(result.document)
const migration = buildReportMigrationMap({
baseline: miniSheet() as never,
final: result.document as never,
legacyIdMap: LEGACY_ID_MAP,
records: result.records,
sheet: { sheetId: 's', versionId: null, documentSha256: '0'.repeat(64) },
mainViewId: 'main',
})
const changelogRaw = renderChangelog(result, before, after, migration.stats)
const reviewRaw = renderReviewReport({
changes: changeset.changes.map(changeSummary),
records: result.records,
document: result.document,
statsBefore: before,
legacyAnomalies: [],
})
return {
probe,
changes_file: file,
changelog_raw: changelogRaw,
changelog: await formatMarkdown(changelogRaw),
review_raw: reviewRaw,
review: await formatMarkdown(reviewRaw),
}
}
describe('R5.4 Markdown sweep oracle', () => {
it('markdown-sweep.json', async () => {
const rows = []
for (const probe of probes()) rows.push(await run(probe))
const oracle = {
baseline: miniSheet(),
references: MINI_REFERENCES,
legacy_id_map: LEGACY_ID_MAP,
cases: rows,
}
const text = `${JSON.stringify(oracle)}\n`
const file = resolve(OUT, 'markdown-sweep.json')
if (MAKE) {
mkdirSync(OUT, { recursive: true })
writeFileSync(file, text)
}
expect(text).toBe(readFileSync(file, 'utf8'))
})
})