test(anatomy): R5.3 characterisation: Object.prototype member as a plain key, migration-map cascade debt, refusal reasons compared by stage

This commit is contained in:
bchanot committed 2026-10-23 00:50:22 +02:00
1 parent 8010661de6
commit 9901106a62
3 files changed
+252 -6

No files matched your search

+201 -5
View File
@@ -311,6 +311,49 @@ fn synthetic_seed(document_json: &str) -> String {
/// 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"];
/// 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) {
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");
@@ -323,11 +366,24 @@ fn the_synthetic_cases_match_the_typescript_or_are_refused_alike() {
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) {
assert!(
outcome.is_err(),
"{name}: the TypeScript refused, the port accepted"
);
let error = outcome
.err()
.unwrap_or_else(|| panic!("{name}: the TypeScript refused, the port accepted"));
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;
}
@@ -366,7 +422,7 @@ fn the_synthetic_cases_match_the_typescript_or_are_refused_alike() {
"{name} seed"
);
}
assert_eq!((compared, refused), (9, 49));
assert_eq!((compared, refused), (11, 52));
let reference_cases = frozen["reference_cases"].as_array().unwrap();
assert_eq!(reference_cases.len(), 5);
for case in reference_cases {
@@ -412,3 +468,143 @@ fn a_reserved_touch_mark_key_in_an_authored_value_is_refused() {
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`, `__proto__`…) 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. The TypeScript refused that form (structuredClone of the function read).
#[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",
"__proto__",
"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"
);
}