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

799 lines
31 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 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);
}
#[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; 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> {
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": "B", "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")
);
}
#[test]
fn the_document_cannot_hold_more_values_than_the_bound() {
let children: Vec<Value> = (0..70_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 op = json!({ "op": "add", "target": MOBILITY, "value": value });
let error = mini_apply(json!([op])).unwrap_err();
assert!(error.contains("more than 200000 values"), "{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}"
);
}
}
}