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:
bchanot committed 2026-10-29 16:24:17 +01:00
1 parent e823655f09
commit bb5efa0f00
3 files changed
+161 -34

No files matched your search

+49 -16
View File
@@ -1,6 +1,7 @@
//! V1-2.2: completeness, review groups, digests, removal groups and the change list
//! (contract §6.3, §6.6), on the equine draft (the counts of contract §1 and §14.1) and on
//! derived versions built by the edit commands.
//! derived versions built by the edit commands (a zone removed after its annotations are
//! unlinked, since a removal is refused while anything names it).
#![allow(clippy::disallowed_methods, clippy::unwrap_used, clippy::expect_used)]
mod common;
@@ -26,6 +27,34 @@ fn edit(document: &AnatomicalSheetDocument, command: Value) -> AnatomicalSheetDo
apply_edit(document, command).unwrap()
}
/// The document without the zone, as an editor gets there: the annotations linked to it are
/// unlinked first (a removal is refused while they name it), then the zone goes. Answers the
/// ids of the annotations unlinked.
fn without_zone(
document: &AnatomicalSheetDocument,
zone_id: &str,
) -> (AnatomicalSheetDocument, Vec<String>) {
let mut next = document.clone();
let mut unlinked = Vec::new();
for annotation in &document.annotations {
let mut value = serde_json::to_value(annotation).unwrap();
if value["finding_zone_id"] == json!(zone_id) {
value["finding_zone_id"] = Value::Null;
let key = json!({ "kind": "annotation", "id": annotation.id() });
next = edit(
&next,
json!({ "op": "update", "key": key, "element": { "kind": "annotation", "value": value } }),
);
unlinked.push(annotation.id().to_owned());
}
}
let next = edit(
&next,
json!({ "op": "remove", "key": { "kind": "zone", "id": zone_id } }),
);
(next, unlinked)
}
fn groups(
document: &AnatomicalSheetDocument,
base: Option<&AnatomicalSheetDocument>,
@@ -146,10 +175,8 @@ fn a_group_is_coverable_once_every_element_is_reviewed() {
fn a_derived_version_that_deletes_a_zone_has_its_removal_group() {
let base = equine();
let zone = base.finding_zones[5].id.clone();
let derived = edit(
&base,
json!({ "op": "remove", "key": { "kind": "zone", "id": zone } }),
);
let (derived, unlinked) = without_zone(&base, &zone);
assert!(!unlinked.is_empty(), "every equine zone is linked");
let all = groups(&derived, Some(&base));
assert_eq!(
all.len(),
@@ -166,10 +193,18 @@ fn a_derived_version_that_deletes_a_zone_has_its_removal_group() {
// A second derivation removing the same zone gives the same removal digest (carry).
let again = groups(&derived.clone(), Some(&base));
assert_eq!(again.last().unwrap().digest, removal.digest);
// Unchanged groups keep the base's digest; without a base nothing is a removal.
// Unchanged groups keep the base's digest (the views of the unlinked annotations changed);
// without a base nothing is a removal.
let base_groups = groups(&base, None);
let redrawn: Vec<String> = base
.annotations
.iter()
.filter(|a| unlinked.iter().any(|id| id == a.id()))
.map(|a| format!("view:{}", a.view_id()))
.collect();
for g in all.iter().filter(|g| !g.removed) {
assert_eq!(g.digest, group(&base_groups, &g.key).digest, "{}", g.key);
let same = g.digest == group(&base_groups, &g.key).digest;
assert_eq!(same, !redrawn.contains(&g.key), "{}", g.key);
}
assert!(groups(&derived, None).iter().all(|g| !g.removed));
// The change list names the zone and everything that went with it.
@@ -186,7 +221,11 @@ fn a_derived_version_that_deletes_a_zone_has_its_removal_group() {
);
let zone_size = 1 + count_findings(&base, &zone);
assert_eq!(removed.len(), zone_size);
assert_eq!(changes.len(), zone_size, "nothing else changed");
assert_eq!(
changes.len(),
zone_size + unlinked.len(),
"nothing else changed"
);
}
fn count_findings(document: &AnatomicalSheetDocument, zone_id: &str) -> usize {
@@ -306,14 +345,8 @@ fn a_removal_digest_is_bound_to_its_group_key() {
let base = equine();
let first = base.finding_zones[0].id.clone();
let second = base.finding_zones[1].id.clone();
let without_first = edit(
&base,
json!({ "op": "remove", "key": { "kind": "zone", "id": first } }),
);
let derived = edit(
&without_first,
json!({ "op": "remove", "key": { "kind": "zone", "id": second } }),
);
let (without_first, _) = without_zone(&base, &first);
let (derived, _) = without_zone(&without_first, &second);
let removals: Vec<ReviewGroup> = groups(&derived, Some(&base))
.into_iter()
.filter(|g| g.removed)