Files
faunosteo/crates/core/tests/curation_markdown.rs

178 lines
7.0 KiB
Rust

//! 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::ids::sha256_hex;
use faunosteo_core::curation::report_guard::format_report;
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,
) -> (Result<String, PipelineError>, 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);
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; the
// inline constructs (links, wiki links, autolinks, character references) are probed apart;
// the list-item fields refuse the wiki and liquid brackets whole (none in the equine ones).
let tag = probe.match_indices('<').any(|(at, _)| {
probe[at + 1..].starts_with(|c: char| c.is_ascii_alphabetic() || "/!?".contains(c))
});
let construct = tag
|| ["](", "][", "![", "[[", "]]", "{%", "%}", "&", "`"]
.iter()
.any(|m| probe.contains(m));
!construct
&& !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);
let digest = |text: &str| sha256_hex(text.as_bytes());
if let Ok(changelog) = &changelog {
assert_eq!(
digest(changelog),
case["changelog_raw_sha256"],
"{probe:?} raw"
);
}
if let Ok(review) = &review {
assert_eq!(digest(review), case["review_raw_sha256"], "{probe:?} raw");
}
let outcomes = [
(
changelog.and_then(|text| format_report(&text)),
&case["changelog_sha256"],
),
(
review.and_then(|text| format_report(&text)),
&case["review_sha256"],
),
];
let mut case_refused = false;
for (outcome, expected) in outcomes {
match outcome {
Ok(text) if digest(&text) == *expected => {}
Ok(_) => silent.push(probe.to_owned()),
Err(PipelineError::UnsupportedMarkdown(_) | PipelineError::UnsupportedWidth(_)) => {
case_refused = true;
}
Err(other) => panic!("{probe:?}: {other}"),
}
}
let closer = case["reference_id"] == "probe_ref_";
assert!(
!(case_refused && equine_safe(probe) && !closer),
"{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, 798);
println!("{same} identical, {refused} refused");
}
/// One long authored cell pads every row of its level: the CHANGELOG is refused before its
/// table is joined, through the pipeline (security gate R5.4 round 7), while the records stay
/// within their own bound.
#[test]
fn a_changelog_whose_alignment_would_explode_is_refused_before_it_is_built() {
let frozen = oracle();
let mobility = "finding:hip/hip-dysfunctions/mobility";
let sets: Vec<Value> = (0..20)
.map(|i| json!({ "op": "set", "target": mobility, "field": "name.en", "value": format!("v{i}") }))
.collect();
let change = |id: &str, rationale: String, ops: Vec<Value>| {
json!({ "id": id, "level": "A", "kind": "technical",
"title": { "fr": "Essai", "en": "Test" }, "rationale": rationale,
"reference_ids": [], "ops": ops })
};
let long = change("CHG-001", "a".repeat(2_000_000), vec![sets[0].clone()]);
let rows = change("CHG-002", "Essai".to_owned(), sets);
let case = json!({ "changes_file": { "changes": [long, rows] } });
let (changelog, _) = raw_reports(&frozen, &case);
let error = changelog.unwrap_err();
assert!(
matches!(&error, PipelineError::Guard(message) if message.contains("exceeds the")),
"{error}"
);
}