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

143 lines
5.3 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::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; the
// inline constructs (links, wiki links, autolinks, character references) are probed apart.
let construct = ["](", "][", "[[", "&", "<h", "<a@"]
.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);
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, 247);
println!("{same} identical, {refused} refused");
}