Files
faunosteo/crates/core/tests/curation_draft.rs
T
bchanot 0da7d3ab9f test(domain): R5.7 port the contracts only the TS suites checked
612 leaves and 26 legacy ids (curation_leaves), region inference, R5.1 ledger rows,
generated folder lists, migration-map oracle entry by entry, overlay removal, halo
filter, CHG-000 and dollar-tag refusals; report-v2 fixtures moved to crates/core/tests
(sha256 unchanged).
2026-10-25 03:59:49 +01:00

1167 lines
46 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::domain::sheet::fingerprint::{
FINGERPRINT_CONVENTION, TRUSTED_ALIASES, sheet_fingerprint,
};
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 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] = [
"document.json",
"assets.json",
"sheet-ref.json",
"report-migration-map.json",
"review/annotations-main.svg",
"review/annotations-head.svg",
"review/app-main.svg",
"review/app-head.svg",
"CHANGELOG.md",
"REVIEW-REPORT.md",
"equine_draft.sql",
];
/// The files R5.3 produced, compared byte for byte here.
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, DRAFT_FILES);
for (name, text) in files
.iter()
.filter(|(name, _)| R5_3_FILES.contains(&name.as_str()))
{
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);
}
/// The declared difference of the R5.6 switch, `R5.6-fingerprint-v1` (owner 2026-10-24): from the
/// pre-switch file the TypeScript wrote, only the fingerprint moves from the registered alias to
/// its canonical value, and `sheet-ref.json` gains the descriptive block. Applied to a parsed
/// value, then printed by the generator's own formatter: anything else that differs fails.
fn r5_6_rule(name: &str, mut value: Value) -> Value {
let pair = &TRUSTED_ALIASES[0];
let stamp = match name {
"sheet-ref.json" => &mut value["document_sha256"],
"report-migration-map.json" => &mut value["sheet"]["documentSha256"],
other => panic!("{other}: no declared rule"),
};
assert_eq!(
*stamp, pair.alias,
"{name}: the pre-switch value is the alias"
);
*stamp = Value::from(pair.canonical);
if name == "sheet-ref.json" {
value.as_object_mut().unwrap().insert(
"fingerprint".into(),
serde_json::json!({ "convention": FINGERPRINT_CONVENTION, "aliases": [pair.alias] }),
);
}
value
}
fn pre_switch(name: &str) -> String {
common::repo_file(&format!(
"crates/core/tests/fixtures/r5-6/pre-switch/{name}"
))
}
#[test]
fn the_pre_switch_copies_are_the_files_the_typescript_wrote() {
let artifacts = oracle("pipeline.json")["artifacts"].clone();
for name in ["sheet-ref.json", "report-migration-map.json"] {
let row = artifacts
.as_array()
.unwrap()
.iter()
.find(|a| a["file"] == name && a["folder"].as_str().unwrap().ends_with("draft"))
.unwrap()
.clone();
let text = pre_switch(name);
assert_eq!(row["sha256"], sha256_hex(text.as_bytes()), "{name}");
assert_eq!(row["bytes"], text.len(), "{name}");
}
}
#[test]
fn the_switch_changes_only_the_fingerprint_of_the_two_files() {
for name in ["sheet-ref.json", "report-migration-map.json"] {
let before: Value = serde_json::from_str(&pre_switch(name)).unwrap();
let expected = format_json(&r5_6_rule(name, before)).unwrap();
assert!(
expected == common::repo_file(&format!("{DRAFT}/{name}")),
"{name}: the committed file is not the pre-switch one under R5.6-fingerprint-v1"
);
}
}
#[test]
fn the_document_fingerprint_is_v1_and_its_bytes_keep_the_historical_alias() {
let files = render_draft_artifacts(&equine_draft(), &[]).unwrap();
let document = &files[0].1;
let reference: Value = serde_json::from_str(&files[2].1).unwrap();
let pair = &TRUSTED_ALIASES[0];
assert_eq!(sha256_hex(document.as_bytes()), pair.alias);
assert_eq!(
reference["document_sha256"],
sheet_fingerprint(document).unwrap()
);
assert_eq!(reference["document_sha256"], pair.canonical);
assert_eq!(
reference["fingerprint"]["convention"],
FINGERPRINT_CONVENTION
);
assert_eq!(
reference["fingerprint"]["aliases"],
serde_json::json!([pair.alias])
);
}
#[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; 4] = [
"declared-proto-segment-overwritten",
"declared-set-without-value-drops-key",
"declared-children-string-renamed-then-removed",
"declared-children-object-then-added-to",
];
/// Cases the TypeScript refused and the port accepts, declared in RUST.md §11.12 (checked by
/// `an_inherited_member_name_is_a_plain_key_and_bypasses_nothing`).
const DECLARED_ACCEPTS: [&str; 1] = ["declared-inherited-member-overwritten"];
fn has_set_without_value(changes_file: &Value) -> bool {
changes_file["changes"]
.as_array()
.into_iter()
.flatten()
.filter_map(|change| change["ops"].as_array())
.flatten()
.any(|op| op["op"] == "set" && op.get("value").is_none())
}
/// The refusal reason, compared by stage: a schema refusal (Zod's issue list) is a parse
/// refusal of `changes.json`; an op or change refusal carries the same message, change id and
/// op index included; a gate refusal names the same gate (the details are Zod's or serde's).
/// Declared family: an inherited `Object.prototype` member read at the end of a field path
/// made `structuredClone` throw at the op; the port writes a plain key the shape gate refuses.
fn assert_same_refusal(name: &str, typescript: &str, rust: &str) {
// Declared family: a non-object child of an added item made the TypeScript throw a
// TypeError where it walked it; the port refuses it at the add.
if typescript.contains("Cannot read properties of")
|| typescript.contains("Cannot create property")
{
assert!(rust.contains("that is not an object"), "{name}: {rust}");
return;
}
if typescript.ends_with("could not be cloned.") {
assert!(
rust.starts_with("curated document is not shape-valid"),
"{name}: {rust}"
);
return;
}
if typescript.starts_with('[') {
assert!(rust.starts_with("changes.json: "), "{name}: {rust}");
return;
}
for gate in [
"curated document is not shape-valid",
"curated document has integrity errors",
] {
if typescript.starts_with(gate) {
assert!(rust.starts_with(gate), "{name}: {rust}");
return;
}
}
assert_eq!(rust, typescript, "{name}");
}
#[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 DECLARED_ACCEPTS.contains(&name) {
assert!(outcome.is_ok(), "{name}: declared as accepted by the port");
continue;
}
if !case["ok"].as_bool().unwrap() || DECLARED_REFUSALS.contains(&name) {
let error = outcome
.err()
.unwrap_or_else(|| panic!("{name}: the TypeScript refused, the port accepted"));
if name == "declared-proto-segment-overwritten" {
assert!(
error.to_string().contains("reserved key \"__proto__\""),
"{name}"
);
} else if name == "declared-children-object-then-added-to" {
assert!(error.to_string().contains("that is not a list"), "{name}");
} else if name == "declared-children-string-renamed-then-removed" {
assert!(
error.to_string().contains("that is not an object"),
"{name}"
);
} else if has_set_without_value(&case["changes_file"]) {
// The declared family: refused at parse, whatever the TypeScript made of it.
assert!(
error.to_string().contains("missing field `value`"),
"{name}"
);
} else {
let expected = case["error"].as_str().unwrap();
assert_same_refusal(name, expected, &error.to_string());
}
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), (14, 67));
let reference_cases = frozen["reference_cases"].as_array().unwrap();
assert_eq!(reference_cases.len(), 7);
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}");
}
// ---------------------------------------------------------------------------------------------
// Declared and kept behaviours, characterised (owner 2026-10-23).
fn mini_apply(
ops: Value,
) -> Result<faunosteo_core::curation::draft::records::CurationResult, String> {
mini_apply_at("B", ops)
}
/// One change of the given level on the mini sheet (`changesetOf` of the TypeScript fixtures
/// used level A: no review mark).
fn mini_apply_at(
level: &str,
ops: Value,
) -> Result<faunosteo_core::curation::draft::records::CurationResult, String> {
let frozen = oracle("curation-synthetic.json");
let references = parse_references(&to_js_compact(&frozen["references_file"]).unwrap()).unwrap();
let file = json!({ "changes": [{
"id": "CHG-001", "level": level, "kind": "technical",
"title": { "fr": "Essai", "en": "Test" }, "rationale": "Essai",
"reference_ids": [], "ops": ops,
}]});
let changeset = parse_changeset(&to_js_compact(&file).unwrap()).map_err(|e| e.to_string())?;
apply_changeset(&frozen["baseline"], &references, &changeset.changes).map_err(|e| e.to_string())
}
const MOBILITY: &str = "finding:hip/hip-dysfunctions/mobility";
fn mobility(document: &Value) -> &Value {
&document["finding_zones"][0]["categories"][0]["children"][0]
}
/// An inherited `Object.prototype` member name (`constructor`, `toString`, `valueOf`…) in a
/// field path is an ordinary key in the port: it is never resolved through a prototype, the
/// settable table still checks the first segment, and every object of the sheet contract is
/// strict, so the key cannot reach the document. The only accepted form is a key a later op
/// replaces: the document is then the one the later op alone gives; one record traces the
/// overwritten write (or removed with its item). The TypeScript refused that form
/// (structuredClone of the function read). `__proto__` is not one of these names: it is
/// refused at the op (`a_proto_segment_is_refused_at_the_op`).
#[test]
fn an_inherited_member_name_is_a_plain_key_and_bypasses_nothing() {
let renamed = json!({ "fr": "Mobilité", "en": "Mobility" });
let set_name = json!({ "op": "set", "target": MOBILITY, "field": "name", "value": renamed });
for member in ["constructor", "toString", "hasOwnProperty", "valueOf"] {
let field = format!("name.{member}");
let write = json!({ "op": "set", "target": MOBILITY, "field": field, "value": "x" });
let kept = mini_apply(json!([write])).unwrap_err();
assert!(
kept.starts_with("curated document is not shape-valid"),
"{member}: {kept}"
);
let as_field = json!({ "op": "set", "target": MOBILITY, "field": member, "value": "x" });
let refused = mini_apply(json!([as_field])).unwrap_err();
assert!(
refused.contains(&format!("unknown field \"{member}\"")),
"{member}: {refused}"
);
let deep = json!({ "op": "set", "target": MOBILITY, "field": format!("{member}.fr"), "value": "x" });
assert!(
mini_apply(json!([deep]))
.unwrap_err()
.contains("unknown field"),
"{member}"
);
let overwritten = mini_apply(json!([write, set_name])).unwrap();
let alone = mini_apply(json!([set_name])).unwrap();
assert_eq!(
format_json(&overwritten.document).unwrap(),
format_json(&alone.document).unwrap(),
"{member}: the document is the later op's alone"
);
assert_eq!(mobility(&overwritten.document)["name"], renamed);
let traced: Vec<_> = overwritten
.records
.iter()
.filter(|r| r.field.as_deref() == Some(field.as_str()))
.collect();
assert_eq!(traced.len(), 1, "{member}");
assert_eq!(
traced[0].before,
Value::Null,
"{member}: never read through the prototype"
);
assert_eq!(
overwritten.records.len(),
alone.records.len() + 1,
"{member}"
);
}
}
/// Technical debt kept during R5 (owner 2026-10-23, RUST.md §11.12): the migration map replays
/// a record by its op, whatever its target, as `migration-map.ts` does. A cascade record of a
/// view, zone or region rename on an annotation is replayed as a rename of that annotation,
/// and a reference rename's cascade on a cited finding as a rename of the finding's path. The
/// values below are the TypeScript's (frozen in `curation-synthetic.json`); a fix would change
/// them on both sides and is the owner's decision.
#[test]
fn the_migration_map_replays_cascade_records_by_their_op_as_the_typescript_does() {
let frozen = oracle("curation-synthetic.json");
let case = |name: &str| {
let case = frozen["cases"]
.as_array()
.unwrap()
.iter()
.find(|c| c["name"] == name)
.unwrap();
(
serde_json::from_str::<Value>(case["migration_map"].as_str().unwrap()).unwrap(),
case["migration_stats"].clone(),
)
};
let (map, _) = case("quirk-view-rename-retargets-annotation");
assert_eq!(
map["annotations"]["legacy-hip"]["id"], "principal",
"should stay hip"
);
assert_eq!(
map["annotations"]["legacy-neck"]["id"], "principal",
"should stay neck"
);
assert_eq!(map["views"]["head"], "principal", "should stay head");
let (map, stats) = case("quirk-reference-rename-renames-finding-path");
let path = "hip/hip-dysfunctions/mobility";
assert_eq!(
map["finding_paths"][path], path,
"restored by re-homing only"
);
assert_eq!(
stats["rehomed"], 3,
"should be 0: the three paths are identical"
);
let (map, _) = case("renames-cascade");
assert_eq!(
map["annotations"]["legacy-hip"]["id"], "hip-joint",
"should be coxofemoral"
);
assert_eq!(
map["finding_paths"][path],
Value::Null,
"should be hip-joint/hip-dysfunctions/motion"
);
}
// ---------------------------------------------------------------------------------------------
// Resource bounds and reserved names reached through a field path (final R5.3 gates).
#[test]
fn a_reserved_or_unbounded_field_path_is_refused_without_writing() {
for field in [
"name.\u{0}touched",
"description.\u{0}touched",
"name.\u{0}other",
] {
let op = json!({ "op": "set", "target": MOBILITY, "field": field, "value": "author text" });
let error = mini_apply(json!([op])).unwrap_err();
assert!(
error.contains("CHG-001 op 0: reserved key"),
"{field}: {error}"
);
}
// 20,000 segments overflowed the stack before the bound (security gate, MEDIUM-1).
let deep = format!("name{}", ".a".repeat(20_000));
let op = json!({ "op": "set", "target": MOBILITY, "field": deep, "value": "x" });
let error = mini_apply(json!([op])).unwrap_err();
assert!(error.contains("more than 8 segments"), "{error}");
}
#[test]
fn the_document_cannot_be_nested_past_the_bound_by_repeated_adds() {
let mut ops = Vec::new();
let mut parent = MOBILITY.to_owned();
for level in 0..40 {
let id = format!("n{level}");
ops.push(json!({ "op": "add", "target": parent, "value": { "id": id, "name": { "fr": "a", "en": "a" } } }));
parent = format!("{parent}/{id}");
}
let error = mini_apply(Value::Array(ops)).unwrap_err();
assert!(error.contains("nests more than 64 levels"), "{error}");
}
fn import_equine(args: &[&str]) -> (i32, String) {
let output = std::process::Command::new(env!("CARGO_BIN_EXE_import-equine"))
.args(args)
.env_remove("INIT_CWD")
.output()
.unwrap();
let text = String::from_utf8_lossy(&output.stderr).into_owned();
(output.status.code().unwrap_or(-1), text)
}
/// Fixed fixtures under the test target's temporary folder: created once, never deleted, and
/// never written by the refusals checked here. The seed refusal is tested on the pure check
/// (`inside`, unit test of the binary): a regression must never write into the real seed.
fn out_fixtures() -> std::path::PathBuf {
let base = std::path::Path::new(env!("CARGO_TARGET_TMPDIR")).join("import-equine-out");
for folder in ["empty", "full"] {
std::fs::create_dir_all(base.join(folder)).unwrap();
}
std::fs::write(base.join("full/keep.txt"), b"keep").unwrap();
std::fs::write(base.join("file"), b"file").unwrap();
let link = base.join("link");
if std::fs::symlink_metadata(&link).is_err() {
std::os::unix::fs::symlink(base.join("empty"), &link).unwrap();
}
base
}
#[test]
fn out_refuses_every_unsafe_destination_and_writes_nothing() {
let base = out_fixtures();
let at = |rest: &str| format!("{}/{rest}", base.display());
let cases = [
(at("link"), "a symbolic link"),
(at("link/"), "a symbolic link"),
(at("link/."), "a symbolic link"),
(at("file"), "not a folder"),
(at("full"), "not empty"),
(at("empty/../new"), "`..` is not accepted"),
(at("absent/new"), "the parent folder does not exist"),
];
for (path, why) in &cases {
let (code, stderr) = import_equine(&["--out", path]);
assert_eq!(code, 2, "{path}: {stderr}");
assert!(stderr.contains(why), "{path}: {stderr}");
}
assert_eq!(std::fs::read_dir(base.join("empty")).unwrap().count(), 0);
assert_eq!(std::fs::read_dir(base.join("full")).unwrap().count(), 1);
assert!(std::fs::symlink_metadata(base.join("absent")).is_err());
}
/// `JSON.parse` keeps no integer beyond 2^53: the record holds the double the document holds.
#[test]
fn an_integer_beyond_2_53_is_read_as_the_double_json_parse_gives() {
let text = |count: &str| {
format!(
r#"{{"changes":[{{"id":"CHG-001","level":"A","kind":"technical",
"title":{{"fr":"Essai","en":"Test"}},"rationale":"Essai","reference_ids":[],
"ops":[{{"op":"set","target":"region:C","field":"count","value":{count}}}]}}]}}"#
)
};
let exact = parse_changeset(&text("9007199254740993")).unwrap();
let rounded = parse_changeset(&text("9007199254740992")).unwrap();
let print = |set: &Changeset| to_js_compact(&serde_json::to_value(set).unwrap()).unwrap();
assert_eq!(print(&exact), print(&rounded));
let frozen = oracle("curation-synthetic.json");
let references = parse_references(&to_js_compact(&frozen["references_file"]).unwrap()).unwrap();
let result = apply_changeset(&frozen["baseline"], &references, &exact.changes).unwrap();
assert_eq!(
to_js_compact(&result.records[0].after).unwrap(),
"9007199254740992"
);
assert!(
format_json(&result.document)
.unwrap()
.contains("9007199254740992")
);
}
/// The records bound now stops a growing changeset first (every added value is recorded); the
/// document bound still holds a baseline near it: one small add past 200,000 values is refused.
#[test]
fn the_document_cannot_hold_more_values_than_the_bound() {
let frozen = oracle("curation-synthetic.json");
let references = parse_references(&to_js_compact(&frozen["references_file"]).unwrap()).unwrap();
let mut baseline = frozen["baseline"].clone();
baseline["padding"] = Value::from(vec![Value::Null; 199_950]);
let value = json!({ "id": "small", "name": { "fr": "a", "en": "a" },
"children": (0..20).map(|i| json!({ "id": format!("n{i}"), "name": { "fr": "a", "en": "a" } }))
.collect::<Vec<_>>() });
let file = json!({ "changes": [{
"id": "CHG-001", "level": "A", "kind": "technical",
"title": { "fr": "Essai", "en": "Test" }, "rationale": "Essai",
"reference_ids": [], "ops": [{ "op": "add", "target": MOBILITY, "value": value }],
}]});
let changeset = parse_changeset(&to_js_compact(&file).unwrap()).unwrap();
let error = apply_changeset(&baseline, &references, &changeset.changes)
.unwrap_err()
.to_string();
assert!(error.contains("more than 200000 values"), "{error}");
}
/// A rename or a remove records the whole item, before and after, with the change's texts:
/// the records are bounded in total (security gate R5.4 round 7: 400 renames of a
/// 5,000-child item asked for 4.3 GB). The equine changeset stays far below the bound.
#[test]
fn the_records_cannot_outgrow_their_bound() {
use faunosteo_core::curation::draft::records::MAX_RECORDED_WEIGHT;
use faunosteo_core::curation::draft::records::record_weight;
let equine: usize = equine_draft().records.iter().map(record_weight).sum();
println!("equine records weigh {equine} units");
assert!(equine * 8 < MAX_RECORDED_WEIGHT, "{equine}");
let children: Vec<Value> = (0..5_000)
.map(|i| json!({ "id": format!("n{i}"), "name": { "fr": "a", "en": "a" } }))
.collect();
let value = json!({ "id": "many", "name": { "fr": "a", "en": "a" }, "children": children });
let mut ops = vec![json!({ "op": "add", "target": MOBILITY, "value": value })];
for round in 0..200 {
let (from, to) = if round % 2 == 0 {
("many", "other")
} else {
("other", "many")
};
ops.push(json!({ "op": "rename", "target": format!("{MOBILITY}/{from}"), "id": to }));
}
let error = mini_apply(Value::from(ops)).unwrap_err();
assert!(error.contains("the records weigh more than"), "{error}");
}
/// One op, or the B/C marking, records once per item it reaches: the bound is charged at each
/// record, before the change and the op are cloned into it (security gate R5.4 round 8: a level
/// B add of 10,000 children with a 128 KiB rationale reached 1.37 GB inside one marking).
#[test]
fn the_records_bound_holds_inside_one_op_and_the_marking() {
let children: Vec<Value> = (0..5_000)
.map(|i| json!({ "id": format!("n{i}"), "name": { "fr": "a", "en": "a" } }))
.collect();
let value = json!({ "id": "many", "name": { "fr": "a", "en": "a" }, "children": children });
let add = json!({ "op": "add", "target": MOBILITY, "value": value });
let frozen = oracle("curation-synthetic.json");
let references = parse_references(&to_js_compact(&frozen["references_file"]).unwrap()).unwrap();
let description = json!({ "fr": "d".repeat(4_096), "en": "d" });
let subtree = json!({ "op": "set", "target": format!("{MOBILITY}/many"), "field": "description",
"value": description, "scope": "subtree" });
let cases = [
("B", "r".repeat(8_192), json!([add])),
("A", "Essai".to_owned(), json!([add, subtree])),
];
// A run of one-character reference ids weighs its headers too (security gate R5.4 round 9:
// 200,000 ids cloned into each record reached 948 MB at one unit per byte).
let mut cases = cases.to_vec();
let sets: Vec<Value> = (0..100)
.map(|i| json!({ "op": "set", "target": MOBILITY, "field": "name.en", "value": format!("v{i}") }))
.collect();
cases.push(("ids", "Essai".to_owned(), Value::from(sets)));
for (level, rationale, ops) in cases {
let ids = if level == "ids" {
vec!["atlas"; 200_000]
} else {
Vec::new()
};
let level = if level == "ids" { "A" } else { level };
let file = json!({ "changes": [{
"id": "CHG-001", "level": level, "kind": "technical",
"title": { "fr": "Essai", "en": "Test" }, "rationale": rationale,
"reference_ids": ids, "ops": ops,
}]});
let changeset = parse_changeset(&to_js_compact(&file).unwrap()).unwrap();
let error = apply_changeset(&frozen["baseline"], &references, &changeset.changes)
.unwrap_err()
.to_string();
assert!(
error.contains("the records weigh more than"),
"{level}: {error}"
);
}
}
/// A `__proto__` segment had no faithful reading in the TypeScript (a prototype set, a
/// polluted `Object.prototype`, or nothing written): the port refuses it at the op.
#[test]
fn a_proto_segment_is_refused_at_the_op() {
let renamed = json!({ "fr": "a", "en": "b" });
let set_name = json!({ "op": "set", "target": MOBILITY, "field": "name", "value": renamed });
for field in [
"name.__proto__",
"name.__proto__.x",
"description.__proto__.fr",
] {
let write = json!({ "op": "set", "target": MOBILITY, "field": field, "value": "x" });
for ops in [json!([write]), json!([write, set_name])] {
let error = mini_apply(ops).unwrap_err();
assert!(
error.contains("CHG-001 op 0: reserved key \"__proto__\""),
"{field}: {error}"
);
}
}
}
// ---------------------------------------------------------------------------------------------
// R5.7: cases of the deleted TypeScript suites that no oracle carries, ported with their exact
// expectations (`crates/core/tests/curation/LEDGER.tsv`).
/// `curation.apply.test.ts` "adds a zone, an annotation and a reference at document level": a
/// `cite` of a reference an earlier op of the same change added is accepted and recorded.
#[test]
fn a_reference_added_at_document_level_can_be_cited_by_the_same_change() {
let ops = json!([
{ "op": "add", "target": "document", "field": "finding_zones",
"value": { "id": "back", "name": { "fr": "Dos", "en": "Back" }, "categories": [] } },
{ "op": "add", "target": "document", "field": "annotations", "position": { "after": "neck" },
"value": { "id": "back", "kind": "zone", "view_id": "main", "category_id": "bones",
"laterality": "unpaired", "geometry": null, "name": { "fr": "Dos", "en": "Back" },
"finding_zone_id": "back", "vertebral_region_code": "T" } },
{ "op": "add", "target": "document", "field": "references",
"value": { "id": "book", "type": "book", "citation": "Book", "url": null, "doi": null } },
{ "op": "cite", "target": "finding:hip/anamnesis", "reference_ids": ["book"] },
]);
let result = mini_apply_at("A", ops).unwrap();
let ids: Vec<&str> = result.document["annotations"]
.as_array()
.unwrap()
.iter()
.map(|a| a["id"].as_str().unwrap())
.collect();
assert_eq!(ids, ["hip", "neck", "back", "head-trigger"]);
let targets: Vec<&str> = result.records.iter().map(|r| r.target.as_str()).collect();
assert_eq!(
targets,
[
"zone:back",
"annotation:back",
"reference:book",
"finding:hip/anamnesis"
]
);
}
/// `curation.assets.test.ts` "reads a viewBox into a document view_box".
#[test]
fn a_view_box_of_four_numbers_is_read_and_three_are_refused() {
let view_box = parse_view_box(Some("60 100 500 380")).unwrap();
assert_eq!(
(
view_box.min_x,
view_box.min_y,
view_box.width,
view_box.height
),
(60.0, 100.0, 500.0, 380.0)
);
let error = parse_view_box(Some("1 2 3")).unwrap_err().to_string();
assert!(error.contains("invalid viewBox"), "{error}");
}
/// `curation.migration-map.test.ts` "follows a rename, a move and a removal, and a renamed
/// annotation": a move addressed through a path renamed by an earlier op of the same change.
#[test]
fn the_map_follows_a_rename_then_a_move_through_the_renamed_path() {
let result = mini_apply_at("A", json!([
{ "op": "rename", "target": "finding:hip/hip-dysfunctions/mobility", "id": "motion" },
{ "op": "move", "target": "finding:hip/hip-dysfunctions/motion/flexion", "to": "finding:hip/anamnesis" },
{ "op": "remove", "target": "finding:neck/neck-dysfunctions/vet" },
{ "op": "rename", "target": "annotation:hip", "id": "coxofemoral" },
]))
.unwrap();
let baseline = oracle("curation-synthetic.json")["baseline"].clone();
let (map, stats) = synthetic_map(&baseline, &result.document, &result.records);
let paths = &map["finding_paths"];
assert_eq!(
paths["hip/hip-dysfunctions/mobility/extension"],
"hip/hip-dysfunctions/motion/extension"
);
assert_eq!(
paths["hip/hip-dysfunctions/mobility/flexion"],
"hip/anamnesis/flexion"
);
assert_eq!(paths["neck/neck-dysfunctions/vet"], Value::Null);
assert_eq!(
paths["neck/neck-dysfunctions/stiffness"],
"neck/neck-dysfunctions/stiffness"
);
assert_eq!(map["annotations"]["legacy-hip"]["id"], "coxofemoral");
assert_eq!(stats["removed"], 1);
}
/// `curation.pipeline.test.ts` "refuses a changeset that reuses the reserved CHG-000".
#[test]
fn a_changeset_reusing_the_reserved_asset_change_id_is_refused() {
let import_result = legacy_result();
let reserved = ONE_CHANGE.replace("CHG-001", "CHG-000");
let changeset = parse_changeset(&reserved).unwrap();
let error = build_equine_draft(&DraftInput {
import_result: &import_result,
cleaned_svg: &bytes(&format!("{DRAFT}/assets/horse.svg")),
head_view_box: parse_view_box(Some("60 100 500 380")).unwrap(),
references: &[],
changes: &changeset.changes,
})
.err()
.unwrap()
.to_string();
assert!(error.contains("CHG-000 is reserved"), "{error}");
}
/// `importer.guards.test.ts` "refuses a document that contains the dollar-quote tag".
#[test]
fn a_document_holding_the_dollar_quote_tag_is_refused_by_the_seed() {
let document_json = to_js_compact(&json!({ "note": "$equine_doc$" })).unwrap();
let error = render_draft_seed_sql(&DraftSeedParams {
sheet_id: "11111111-1111-5111-8111-111111111111",
assets: &[],
document_json: &document_json,
change_note: "x",
})
.unwrap_err()
.to_string();
assert!(error.contains("$equine_doc$"), "{error}");
}
/// Every file under `dir`, relative to it, sorted.
fn files_under(dir: &str) -> Vec<String> {
fn walk(root: &std::path::Path, at: &std::path::Path, out: &mut Vec<String>) {
for entry in std::fs::read_dir(at).unwrap() {
let path = entry.unwrap().path();
if path.is_dir() {
walk(root, &path, out);
} else {
out.push(
path.strip_prefix(root)
.unwrap()
.to_string_lossy()
.into_owned(),
);
}
}
}
let root = std::path::PathBuf::from(common::repo_path(dir));
let mut out = Vec::new();
walk(&root, &root, &mut out);
out.sort();
out
}
/// `r5.pipeline-oracles.test.ts` "the ledger digests are the committed files": each of the 16
/// text artifacts the TypeScript recorded at R5.1 is the committed file, byte count included;
/// the two fingerprint-bearing draft files are the pre-switch copies (the switch itself is
/// `the_switch_changes_only_the_fingerprint_of_the_two_files`).
#[test]
fn every_ledger_row_of_the_r5_1_run_is_the_committed_file() {
let artifacts = oracle("pipeline.json")["artifacts"].clone();
let rows = artifacts.as_array().unwrap();
assert_eq!(rows.len(), 16);
for row in rows {
let (folder, file) = (
row["folder"].as_str().unwrap(),
row["file"].as_str().unwrap(),
);
let switched = folder.ends_with("/draft")
&& ["sheet-ref.json", "report-migration-map.json"].contains(&file);
let path = if switched {
format!("crates/core/tests/fixtures/r5-6/pre-switch/{file}")
} else {
format!("{folder}/{file}")
};
let committed = bytes(&path);
assert_eq!(row["sha256"], sha256_hex(&committed), "{folder}/{file}");
assert_eq!(row["bytes"], committed.len(), "{folder}/{file}");
}
}
/// `seed.golden.test.ts` "holds no file the run does not produce" and the legacy folder's "no
/// SQL": the two generated folders hold exactly the ledger's files plus the byte copies.
#[test]
fn the_generated_folders_hold_exactly_the_files_the_pipeline_writes() {
let artifacts = oracle("pipeline.json")["artifacts"].clone();
let listed = |suffix: &str| -> Vec<String> {
artifacts
.as_array()
.unwrap()
.iter()
.filter(|row| row["folder"].as_str().unwrap().ends_with(suffix))
.map(|row| row["file"].as_str().unwrap().to_owned())
.collect()
};
let mut legacy = listed("/legacy-import");
legacy.extend([
"assets/Horse.svg".to_owned(),
"assets/Horse_head.svg".to_owned(),
]);
legacy.sort();
let legacy_dir = "supabase/seed/anatomical-sheets/equine/legacy-import";
assert_eq!(files_under(legacy_dir), legacy);
assert!(legacy.iter().all(|file| !file.ends_with(".sql")));
let mut draft = listed("/draft");
draft.sort();
assert_eq!(draft.len(), 12);
assert_eq!(files_under(DRAFT), draft);
}