fix(domain): V1-2.2 refuse a removal while another element names it
sheet.edit remove answers has_dependents, the document unchanged, while dependents lists anything but descendants (owner decision 2026-10-29); the property test checks no removal adds a referential issue.
This commit is contained in:
1 parent
e823655f09
commit
bb5efa0f00
3 files changed
+161
-34
No files matched your search
@@ -1,7 +1,8 @@
|
||||
//! V1-2.2: the editor's commands (contract §6.6, §8.1). The status rules, case by case; the
|
||||
//! refusals; `dependents`; the facade; and a property test over random command sequences:
|
||||
//! every output reads back strictly, existing keys survive unless removed, an edited or new
|
||||
//! element is `to_confirm`, nothing ever becomes `inherited`.
|
||||
//! element is `to_confirm`, nothing ever becomes `inherited`, a removal leaves no new dangling
|
||||
//! reference and is refused only while something names its element.
|
||||
#![allow(clippy::disallowed_methods, clippy::unwrap_used, clippy::expect_used)]
|
||||
|
||||
mod common;
|
||||
@@ -351,27 +352,88 @@ fn markings_are_set_one_element_category_or_zone_at_a_time() {
|
||||
);
|
||||
}
|
||||
|
||||
/// Owner decision 2026-10-29: a removal never leaves an orphan or a dangling reference. It
|
||||
/// takes the element's descendants and is refused, the document unchanged, while anything else
|
||||
/// names it.
|
||||
#[test]
|
||||
fn a_removal_takes_the_descendants_and_leaves_references_to_the_panel() {
|
||||
fn a_removal_takes_its_descendants_and_is_refused_while_named() {
|
||||
let doc = small();
|
||||
let next = edit(
|
||||
&doc,
|
||||
json!({ "op": "remove", "key": finding(&["cat", "n1"]) }),
|
||||
);
|
||||
assert!(next.finding_zones[0].categories[0].children.is_empty());
|
||||
let next = edit(
|
||||
&doc,
|
||||
for key in [
|
||||
json!({ "kind": "view", "id": "head" }),
|
||||
json!({ "kind": "view", "id": "main" }),
|
||||
json!({ "kind": "zone", "id": "z1" }),
|
||||
json!({ "kind": "category", "id": "c" }),
|
||||
json!({ "kind": "region", "code": "C" }),
|
||||
json!({ "kind": "reference", "id": "r1" }),
|
||||
] {
|
||||
let command = json!({ "op": "remove", "key": key });
|
||||
assert_eq!(
|
||||
refused(&doc, command.clone()),
|
||||
EditError::HasDependents,
|
||||
"{key}"
|
||||
);
|
||||
let input = json!({ "document": doc, "command": command }).to_string();
|
||||
let out: Value = serde_json::from_str(&call("sheet.edit", &input)).unwrap();
|
||||
assert_eq!(out["kind"], json!("has_dependents"), "{key}");
|
||||
}
|
||||
// Once nothing names them, the view and the zone go (the zone with its categories).
|
||||
let mut next = edit(&doc, json!({ "op": "remove", "key": annotation("a2") }));
|
||||
next = edit(&next, json!({ "op": "remove", "key": annotation("t1") }));
|
||||
next = edit(
|
||||
&next,
|
||||
json!({ "op": "remove", "key": { "kind": "view", "id": "head" } }),
|
||||
);
|
||||
assert_eq!(next.views.len(), 1);
|
||||
assert_eq!(next.annotations.len(), 3, "a2 and t1 stay, dangling");
|
||||
let issues = faunosteo_core::domain::sheet::validate_sheet_document(&next);
|
||||
assert!(issues.iter().any(|i| i.code == "annotation.view.missing"));
|
||||
let next = edit(
|
||||
&doc,
|
||||
let mut unlinked = serde_json::to_value(&next.annotations[0]).unwrap();
|
||||
unlinked["finding_zone_id"] = Value::Null;
|
||||
next = edit(
|
||||
&next,
|
||||
json!({ "op": "update", "key": annotation("a1"), "element": { "kind": "annotation", "value": unlinked } }),
|
||||
);
|
||||
next = edit(
|
||||
&next,
|
||||
json!({ "op": "remove", "key": { "kind": "zone", "id": "z1" } }),
|
||||
);
|
||||
assert!(next.finding_zones.is_empty());
|
||||
// With no citing finding left, the reference goes too; nothing dangles.
|
||||
next = edit(
|
||||
&next,
|
||||
json!({ "op": "remove", "key": { "kind": "reference", "id": "r1" } }),
|
||||
);
|
||||
let issues = faunosteo_core::domain::sheet::validate_sheet_document(&next);
|
||||
assert!(
|
||||
issues
|
||||
.iter()
|
||||
.all(|i| !REFERENTIAL.contains(&i.code.as_str())),
|
||||
"{issues:?}"
|
||||
);
|
||||
}
|
||||
|
||||
/// The integrity rules that name an element that does not exist.
|
||||
const REFERENTIAL: [&str; 7] = [
|
||||
"annotation.view.missing",
|
||||
"annotation.category.missing",
|
||||
"annotation.finding_zone.missing",
|
||||
"annotation.region.missing",
|
||||
"annotation.opens_view.invalid",
|
||||
"finding.reference.missing",
|
||||
"view.parent.missing",
|
||||
];
|
||||
|
||||
/// How many issues of each referential rule a document has (paths shift with removals).
|
||||
fn referential_issues(doc: &AnatomicalSheetDocument) -> HashMap<String, usize> {
|
||||
let mut counts = HashMap::new();
|
||||
for issue in faunosteo_core::domain::sheet::validate_sheet_document(doc) {
|
||||
if REFERENTIAL.contains(&issue.code.as_str()) {
|
||||
*counts.entry(issue.code).or_insert(0) += 1;
|
||||
}
|
||||
}
|
||||
counts
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -715,7 +777,7 @@ fn random_command_sequences_keep_every_invariant() {
|
||||
let mut rng = Rng(seed);
|
||||
let mut counter = 0;
|
||||
let mut doc = start;
|
||||
let (mut applied, mut refusals) = (0, 0);
|
||||
let (mut applied, mut refusals, mut named) = (0, 0, 0);
|
||||
for _ in 0..400 {
|
||||
let mut raw = random_command(&doc, &mut rng, &mut counter);
|
||||
let changes = raw.as_object_mut().unwrap().remove("changes");
|
||||
@@ -728,9 +790,28 @@ fn random_command_sequences_keep_every_invariant() {
|
||||
.filter(|s| **s == ReviewStatus::Inherited)
|
||||
.count();
|
||||
match apply_edit(&doc, command) {
|
||||
Err(_) => refusals += 1,
|
||||
Err(error) => {
|
||||
refusals += 1;
|
||||
if error == EditError::HasDependents {
|
||||
let key: ElementKey = serde_json::from_value(raw["key"].clone()).unwrap();
|
||||
assert!(
|
||||
dependents(&doc, &key)
|
||||
.iter()
|
||||
.any(|d| d.relation != Relation::Descendant),
|
||||
"refused with nothing naming it: {raw}"
|
||||
);
|
||||
named += 1;
|
||||
}
|
||||
}
|
||||
Ok(next) => {
|
||||
applied += 1;
|
||||
if raw["op"] == "remove" {
|
||||
let (old, new) = (referential_issues(&doc), referential_issues(&next));
|
||||
let grew = new
|
||||
.iter()
|
||||
.any(|(code, n)| old.get(code).is_none_or(|o| n > o));
|
||||
assert!(!grew, "orphan left: {raw}");
|
||||
}
|
||||
let text = serde_json::to_string(&next).unwrap();
|
||||
let reread: AnatomicalSheetDocument =
|
||||
serde_json::from_value(parse_json(&text).unwrap()).unwrap();
|
||||
@@ -750,8 +831,8 @@ fn random_command_sequences_keep_every_invariant() {
|
||||
}
|
||||
}
|
||||
assert!(
|
||||
applied > 200 && refusals > 0,
|
||||
"{applied} applied, {refusals} refused"
|
||||
applied > 200 && refusals > 0 && named > 0,
|
||||
"{applied} applied, {refusals} refused, {named} named elsewhere"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user