Files
faunosteo/crates/core/tests/curation_draft.rs
T
bchanot e0f149b28a feat(anatomy): R5.3 curated draft in Rust (changeset applier, migration map, stats, seed SQL) and pnpm r5:import-equine
crates/core/src/curation/draft behind the curation feature: strict changeset and references parsing as JSON.parse + Zod, the six ops, rename cascades, the B/C review rule, the R2 shape and integrity gate, migration map, document stats, seed SQL. import-equine writes both folders; --check reproduces legacy-import/ and the R5.3 draft files byte for byte (12 files, 0 differences), --out claims an absent or empty, non-symlink destination and never deletes. Parity: 825 records, 46 changes, stats, 612 leaves 442/141/29, 58 synthetic TypeScript cases (oracle r5/curation-synthetic.json).
2026-10-23 00:10:55 +02:00

415 lines
15 KiB
Rust

//! The Rust draft pipeline against the TypeScript (R5.3): the R5.3 files of the committed
//! `draft/` folder byte for byte, the draft section frozen in `oracles/r5/pipeline.json` (46
//! changes, 825 records, stats, migration stats), two runs byte-equal, the 612 legacy leaves
//! migrated through the core's reader on the produced map, and the synthetic cases of
//! `oracles/r5/curation-synthetic.json` (the generic contract, refusals included). Every
//! expectation was written by the TypeScript pipeline.
#![allow(clippy::disallowed_methods, clippy::unwrap_used, clippy::expect_used)]
mod archive_support;
mod common;
use faunosteo_core::curation::draft::migration_map::{
MigrationMapInput, SheetRef, build_report_migration_map,
};
use faunosteo_core::curation::draft::records::ChangeRecord;
use faunosteo_core::curation::draft::schema::Changeset;
use faunosteo_core::curation::draft::seed::{DraftSeedParams, render_draft_seed_sql};
use faunosteo_core::curation::draft::stats::document_stats;
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::AssetRecord;
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::domain::report::ReportMigrationMap;
use faunosteo_core::domain::report::read::read_report_document;
use faunosteo_core::domain::sheet::AnatomicalSheetDocument;
use faunosteo_core::json::to_js_compact;
use serde_json::{Map, Value, json};
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.
const R5_3_FILES: [&str; 5] = [
"document.json",
"assets.json",
"sheet-ref.json",
"report-migration-map.json",
"equine_draft.sql",
];
fn bytes(relative: &str) -> Vec<u8> {
std::fs::read(common::repo_path(relative)).unwrap()
}
fn legacy_result() -> ImportResult {
let svg = |name: &str| SvgSource {
file_name: name.to_owned(),
bytes: bytes(&format!("{SOURCES}/{name}")),
};
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()
}
fn equine_draft() -> DraftResult {
let import_result = legacy_result();
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 = import_result
.view_boxes
.get(HEAD_VIEW_ID)
.and_then(Value::as_str);
build_equine_draft(&DraftInput {
import_result: &import_result,
cleaned_svg: &bytes(&format!("{DRAFT}/assets/horse.svg")),
head_view_box: parse_view_box(head).unwrap(),
references: &references,
changes: &changeset.changes,
})
.unwrap()
}
fn oracle(name: &str) -> Value {
serde_json::from_str(&common::repo_file(&format!(
"crates/core/tests/oracles/r5/{name}"
)))
.unwrap()
}
fn assert_same(actual: &Value, expected: &Value, what: &str) {
assert_eq!(
to_js_compact(actual).unwrap(),
to_js_compact(expected).unwrap(),
"{what}"
);
}
fn value<T: serde::Serialize + ?Sized>(value: &T) -> Value {
serde_json::to_value(value).unwrap()
}
#[test]
fn the_r5_3_files_of_the_committed_draft_folder_are_reproduced_byte_for_byte() {
let draft = equine_draft();
let files = render_draft_artifacts(&draft).unwrap();
let names: Vec<&str> = files.iter().map(|(name, _)| name.as_str()).collect();
assert_eq!(names, R5_3_FILES);
for (name, text) in &files {
assert!(
*text == common::repo_file(&format!("{DRAFT}/{name}")),
"{name} differs"
);
}
let asset = &draft.assets[0];
let frozen = bytes(&format!("{DRAFT}/assets/{}", asset.file));
assert_eq!(sha256_hex(&frozen), asset.sha256);
assert_eq!(frozen.len(), asset.byte_size);
}
#[test]
fn the_document_fingerprint_is_the_historical_value() {
let files = render_draft_artifacts(&equine_draft()).unwrap();
let document = &files[0].1;
let reference: Value = serde_json::from_str(&files[2].1).unwrap();
assert_eq!(
reference["document_sha256"],
sha256_hex(document.as_bytes())
);
assert_eq!(
reference["document_sha256"],
"27c7383c445c73dc2001ebf4794a3ea63d416910bbbaa0d7848dd720de7c2660"
);
}
#[test]
fn the_draft_section_of_the_frozen_run_is_reproduced() {
let draft = equine_draft();
let frozen = oracle("pipeline.json");
let expected = frozen["draft"].as_object().unwrap();
assert_eq!(draft.sheet_id, expected["sheetId"].as_str().unwrap());
assert_eq!(draft.changes.len(), 46);
assert_same(&value(&draft.changes), &expected["changes"], "changes");
assert_same(&value(&draft.assets), &expected["assets"], "assets");
assert_same(
&value(&draft.stats_before),
&expected["statsBefore"],
"statsBefore",
);
assert_same(
&value(&draft.stats_after),
&expected["statsAfter"],
"statsAfter",
);
assert_same(&value(&draft.issues), &expected["issues"], "issues");
assert_eq!(draft.records.len(), 825);
let records = value(&draft.records);
for (index, record) in expected["records"].as_array().unwrap().iter().enumerate() {
assert_same(&records[index], record, &format!("record {index}"));
}
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();
assert_same(
&value(&stats),
&expected["migrationStats"],
"migrationStats",
);
}
#[test]
fn two_runs_emit_the_same_bytes() {
let first = render_draft_artifacts(&equine_draft()).unwrap();
let second = render_draft_artifacts(&equine_draft()).unwrap();
assert_eq!(first, second);
}
fn node_is_leaf(
document: &AnatomicalSheetDocument,
zone_id: &str,
path: &[String],
) -> Option<bool> {
let zone = document.finding_zones.iter().find(|z| z.id == zone_id)?;
let category = zone.categories.iter().find(|c| c.id == path[0])?;
let mut children = &category.children;
let mut leaf = children.is_empty();
for segment in &path[1..] {
let node = children.iter().find(|n| n.id == *segment)?;
children = &node.children;
leaf = children.is_empty();
}
Some(leaf)
}
#[test]
fn the_612_legacy_leaves_migrate_through_the_produced_map_as_the_typescript_measured() {
let draft = equine_draft();
let files = render_draft_artifacts(&draft).unwrap();
let map: ReportMigrationMap = serde_json::from_str(&files[3].1).unwrap();
let document: AnatomicalSheetDocument = serde_json::from_value(draft.document.clone()).unwrap();
let leaves = archive_support::legacy_leaves();
assert_eq!(leaves.len(), 612);
let outcome =
read_report_document(&archive_support::leaves_as_v2_report(&leaves), &map).unwrap();
let findings: Vec<_> = outcome
.report
.observations
.iter()
.flat_map(|o| o.findings.iter())
.collect();
assert_eq!(findings.len(), 612);
let (mut identical, mut moved, mut removed) = (0, 0, 0);
let mut flagged = Vec::new();
for (leaf, finding) in leaves.iter().zip(&findings) {
let same = finding.finding_zone_id == leaf.zone_id && finding.path == leaf.path;
if finding.unresolved.is_some() {
removed += 1;
flagged.push(format!(
"{}/{}",
finding.finding_zone_id,
finding.path.join("/")
));
assert!(node_is_leaf(&document, &finding.finding_zone_id, &finding.path).is_none());
} else {
if same {
identical += 1
} else {
moved += 1
}
assert_eq!(
node_is_leaf(&document, &finding.finding_zone_id, &finding.path),
Some(true),
"{}/{}",
finding.finding_zone_id,
finding.path.join("/")
);
}
assert_eq!(finding.label_fr_snapshot, leaf.fr);
}
assert_eq!((identical, moved, removed), (442, 141, 29));
let nulls: Vec<String> = leaves
.iter()
.map(|leaf| format!("{}/{}", leaf.zone_id, leaf.path.join("/")))
.filter(|key| map.finding_paths.get(key).is_some_and(Option::is_none))
.collect();
assert_eq!(flagged, nulls);
}
// ---------------------------------------------------------------------------------------------
// Synthetic cases written by the TypeScript (`oracles/r5/curation-synthetic.json`).
fn synthetic_records(records: &[ChangeRecord]) -> Value {
value(records)
}
fn synthetic_map(
baseline: &Value,
result_document: &Value,
records: &[ChangeRecord],
) -> (Value, Value) {
let legacy_id_map: Map<String, Value> = json!({
"views": { "head": "head" },
"annotations": { "legacy-hip": "hip", "legacy-neck": "neck" },
"categories": {},
"finding_nodes": {},
"assets": {},
})
.as_object()
.unwrap()
.clone();
let sheet = SheetRef {
sheet_id: "11111111-1111-5111-8111-111111111111".to_owned(),
version_id: None,
document_sha256: "0".repeat(64),
};
let (map, stats) = build_report_migration_map(&MigrationMapInput {
baseline,
final_document: result_document,
legacy_id_map: &legacy_id_map,
records,
sheet: &sheet,
main_view_id: "main",
})
.unwrap();
(map, value(&stats))
}
fn synthetic_seed(document_json: &str) -> String {
let assets = [AssetRecord {
file: "mini.svg".to_owned(),
sha256: "ab".repeat(32),
byte_size: 42,
svg_asset_id: "3f6c1b52-8a4e-4d17-9b0c-5e2a7d91c4f8".to_owned(),
}];
render_draft_seed_sql(&DraftSeedParams {
sheet_id: "11111111-1111-5111-8111-111111111111",
assets: &assets,
document_json,
change_note: "Essai 'quoted'",
})
.unwrap()
}
/// Cases the TypeScript accepted and the port refuses, declared in RUST.md §11.12.
const DECLARED_REFUSALS: [&str; 1] = ["declared-set-without-value-drops-key"];
#[test]
fn the_synthetic_cases_match_the_typescript_or_are_refused_alike() {
let frozen = oracle("curation-synthetic.json");
let baseline = &frozen["baseline"];
let references = parse_references(&to_js_compact(&frozen["references_file"]).unwrap()).unwrap();
let (mut compared, mut refused) = (0, 0);
for case in frozen["cases"].as_array().unwrap() {
let name = case["name"].as_str().unwrap();
let changeset: Result<Changeset, _> =
parse_changeset(&to_js_compact(&case["changes_file"]).unwrap());
let outcome =
changeset.and_then(|set| apply_changeset(baseline, &references, &set.changes));
if !case["ok"].as_bool().unwrap() || DECLARED_REFUSALS.contains(&name) {
assert!(
outcome.is_err(),
"{name}: the TypeScript refused, the port accepted"
);
refused += 1;
continue;
}
compared += 1;
let result = outcome.unwrap_or_else(|error| panic!("{name}: {error}"));
let document_json = format_json(&result.document).unwrap();
assert_eq!(
document_json,
case["document"].as_str().unwrap(),
"{name} document"
);
assert_same(
&synthetic_records(&result.records),
&case["records"],
&format!("{name} records"),
);
assert_same(
&value(&document_stats(&result.document).unwrap()),
&case["stats"],
&format!("{name} stats"),
);
let (map, stats) = synthetic_map(baseline, &result.document, &result.records);
assert_eq!(
format_json(&map).unwrap(),
case["migration_map"].as_str().unwrap(),
"{name} map"
);
assert_same(
&stats,
&case["migration_stats"],
&format!("{name} migration stats"),
);
assert_eq!(
synthetic_seed(&document_json),
case["seed_sql"].as_str().unwrap(),
"{name} seed"
);
}
assert_eq!((compared, refused), (9, 49));
let reference_cases = frozen["reference_cases"].as_array().unwrap();
assert_eq!(reference_cases.len(), 5);
for case in reference_cases {
let name = case["name"].as_str().unwrap();
let outcome = parse_references(&to_js_compact(&case["file"]).unwrap());
assert!(
outcome.is_err(),
"{name}: the TypeScript refused, the port accepted"
);
}
}
// ---------------------------------------------------------------------------------------------
// Parse semantics the synthetic oracle cannot carry (a JSON value holds no repeated key).
const ONE_CHANGE: &str = r#"{"changes":[{"id":"CHG-001","level":"A","kind":"technical",
"title":{"fr":"Essai","en":"Test"},"rationale":"first","reference_ids":[],
"ops":[{"op":"remove","target":"finding:hip/hip-dysfunctions/mobility/flexion"}]}]}"#;
#[test]
fn a_repeated_key_keeps_its_last_value_as_json_parse_does() {
let repeated = ONE_CHANGE.replace(
r#""rationale":"first","#,
r#""rationale":"first","rationale":"last","#,
);
let expected = ONE_CHANGE.replace(r#""rationale":"first","#, r#""rationale":"last","#);
assert_eq!(
parse_changeset(&repeated).unwrap(),
parse_changeset(&expected).unwrap()
);
let references = r#"{"references":[{"id":"atlas","type":"book","citation":"a",
"citation":"b","url":null,"doi":null}]}"#;
assert_eq!(parse_references(references).unwrap()[0].citation, "b");
}
#[test]
fn a_reserved_touch_mark_key_in_an_authored_value_is_refused() {
let text = ONE_CHANGE.replace(
r#"{"op":"remove","target":"finding:hip/hip-dysfunctions/mobility/flexion"}"#,
r#"{"op":"set","target":"finding:hip/hip-dysfunctions/mobility","field":"name",
"value":{"fr":"a","en":"a","\u0000touched":0}}"#,
);
let error = parse_changeset(&text).unwrap_err();
assert!(error.to_string().contains("reserved key"), "{error}");
}