use super::*;
fn unset_args(id: EntityId, hash: String, unset: &[&str]) -> UpdateEntityArgs {
UpdateEntityArgs {
anchors: Vec::new(),
id,
expected_hash: crate::ExpectedHash::Current(hash),
sections: IndexMap::new(),
append_sections: IndexMap::new(),
patch_sections: IndexMap::new(),
sections_unset: unset.iter().map(|s| s.to_string()).collect(),
metadata: IndexMap::new(),
metadata_unset: Vec::new(),
declare_relations: Vec::new(),
dry_run: false,
relations_unset: Vec::new(),
anchors_unset: Vec::new(),
}
}
#[test]
fn update_entity_sections_unset_removes_optional_section() {
let tmp = TempDir::new().unwrap();
let (mut engine, seeded) = engine_with_seed(&tmp, "Unset Subject");
let (actor, client) = cli_actor();
let mut sections = IndexMap::new();
sections.insert("specifies".to_string(), "temporary content".to_string());
let with_specifies = engine
.update_entity(
UpdateEntityArgs {
sections,
..unset_args(seeded.id.clone(), seeded.content_hash.clone(), &[])
},
actor,
Some(&client),
None,
)
.unwrap();
let outcome = engine
.update_entity(
unset_args(
seeded.id.clone(),
with_specifies.content_hash.clone(),
&["specifies", "not-present"],
),
actor,
Some(&client),
None,
)
.unwrap();
assert_eq!(outcome.modified_sections.unset, vec!["specifies"]);
let entity = engine.store().get(&seeded.id).unwrap();
assert!(
!entity.sections.contains_key("specifies"),
"section removed: {:?}",
entity.sections.keys().collect::<Vec<_>>()
);
}
#[test]
fn update_entity_sections_unset_refuses_required_section() {
let tmp = TempDir::new().unwrap();
let (mut engine, seeded) = engine_with_seed(&tmp, "Unset Required");
let (actor, client) = cli_actor();
let err = engine
.update_entity(
unset_args(
seeded.id.clone(),
seeded.content_hash.clone(),
&["identity"],
),
actor,
Some(&client),
None,
)
.unwrap_err();
match err {
EngineError::MissingRequiredSection {
entity_type,
sections,
..
} => {
assert_eq!(entity_type, "spec");
assert_eq!(sections.len(), 1);
assert_eq!(sections[0].key, "identity");
}
other => panic!("expected MissingRequiredSection, got {other:?}"),
}
}
#[test]
fn update_entity_sections_unset_conflicts_and_relationships_refuse() {
let tmp = TempDir::new().unwrap();
let (mut engine, seeded) = engine_with_seed(&tmp, "Unset Conflict");
let (actor, client) = cli_actor();
let mut sections = IndexMap::new();
sections.insert("specifies".to_string(), "body".to_string());
let err = engine
.update_entity(
UpdateEntityArgs {
sections,
..unset_args(
seeded.id.clone(),
seeded.content_hash.clone(),
&["specifies"],
)
},
actor,
Some(&client),
None,
)
.unwrap_err();
match err {
EngineError::ConflictingSectionModes { section, modes } => {
assert_eq!(section, "specifies");
assert!(modes.contains(&"sections_unset".to_string()), "{modes:?}");
assert!(modes.contains(&"sections".to_string()), "{modes:?}");
}
other => panic!("expected ConflictingSectionModes, got {other:?}"),
}
let err = engine
.update_entity(
unset_args(
seeded.id.clone(),
seeded.content_hash.clone(),
&["relationships"],
),
actor,
Some(&client),
None,
)
.unwrap_err();
assert_eq!(err.code(), "SECTION_NOT_UPDATABLE", "{err:?}");
}
#[test]
fn update_entity_applies_multiple_patches_per_section_in_order() {
let tmp = TempDir::new().unwrap();
let (mut engine, seeded) = engine_with_seed(&tmp, "Multi Patch");
let (actor, client) = cli_actor();
let mut patches = IndexMap::new();
patches.insert(
"identity".to_string(),
vec![
crate::ops::PatchArg {
old: "fixture".to_string(),
new: "FIRST".to_string(),
all: false,
},
crate::ops::PatchArg {
old: "FIRST identity".to_string(),
new: "SECOND".to_string(),
all: false,
},
],
);
let outcome = engine
.update_entity(
UpdateEntityArgs {
patch_sections: patches,
..unset_args(seeded.id.clone(), seeded.content_hash.clone(), &[])
},
actor,
Some(&client),
None,
)
.unwrap();
assert_eq!(outcome.modified_sections.patched, vec!["identity"]);
let entity = engine.store().get(&seeded.id).unwrap();
assert_eq!(entity.sections["identity"], "SECOND body");
}
#[test]
fn update_entity_patch_names_the_sections_that_do_contain_old() {
let tmp = TempDir::new().unwrap();
let (mut engine, seeded) = engine_with_seed(&tmp, "Patch Wrong Section");
let (actor, client) = cli_actor();
let mut patches = IndexMap::new();
patches.insert(
"identity".to_string(),
vec![crate::ops::PatchArg {
old: "fixture purpose body".to_string(),
new: "nope".to_string(),
all: false,
}],
);
let err = engine
.update_entity(
UpdateEntityArgs {
anchors: Vec::new(),
id: seeded.id.clone(),
expected_hash: crate::ExpectedHash::Current(seeded.content_hash.clone()),
sections: IndexMap::new(),
append_sections: IndexMap::new(),
patch_sections: patches,
sections_unset: Vec::new(),
metadata: IndexMap::new(),
metadata_unset: Vec::new(),
declare_relations: Vec::new(),
dry_run: false,
relations_unset: Vec::new(),
anchors_unset: Vec::new(),
},
actor,
Some(&client),
None,
)
.unwrap_err();
match err {
EngineError::PatchOldNotFound {
section,
found_in_sections,
..
} => {
assert_eq!(section, "identity");
assert_eq!(found_in_sections, vec!["purpose".to_string()]);
}
other => panic!("expected PatchOldNotFound, got {other:?}"),
}
}
#[test]
fn update_entity_appends_to_existing_section_with_newline_separator() {
let tmp = TempDir::new().unwrap();
let (mut engine, seeded) = engine_with_seed(&tmp, "Append Subject");
let (actor, client) = cli_actor();
let mut appends = IndexMap::new();
appends.insert("identity".to_string(), "appended tail.".to_string());
let outcome = engine
.update_entity(
UpdateEntityArgs {
anchors: Vec::new(),
id: seeded.id.clone(),
expected_hash: crate::ExpectedHash::Current(seeded.content_hash.clone()),
sections: IndexMap::new(),
append_sections: appends,
patch_sections: IndexMap::new(),
sections_unset: Vec::new(),
metadata: IndexMap::new(),
metadata_unset: Vec::new(),
declare_relations: Vec::new(),
dry_run: false,
relations_unset: Vec::new(),
anchors_unset: Vec::new(),
},
actor,
Some(&client),
None,
)
.unwrap();
assert_eq!(outcome.modified_sections.appended, vec!["identity"]);
assert!(outcome.modified_sections.replaced.is_empty());
let updated = engine.get_entity(&seeded.id).unwrap();
let body = updated.sections.get("identity").expect("identity section");
assert!(
body.contains("appended tail."),
"appended body missing: {body:?}"
);
}
fn engine_with_open_fence(tmp: &TempDir) -> (Engine, crate::EntityId) {
let (_engine, seeded) = engine_with_seed(tmp, "Fenced");
let id = seeded.id.clone();
let path = tmp.path().join(&seeded.file_path);
let raw = std::fs::read_to_string(&path).expect("seeded file");
let doctored = raw.replace("fixture identity body", "intro\n\n```rust\nfn main() {}");
assert_ne!(doctored, raw, "the seeded body must be there to doctor");
std::fs::write(&path, doctored).unwrap();
let mem_dir = tmp.path().to_path_buf();
let writer = FilesystemBackend::new(mem_dir.clone());
let engine = Engine::from_mounts(vec![(
folder_mount("specs", mem_dir),
Box::new(writer) as Box<dyn MemBackend>,
)])
.unwrap();
drop(seeded);
(engine, id)
}
#[test]
fn a_write_that_does_not_resolve_an_open_fence_is_refused() {
let tmp = TempDir::new().unwrap();
let (mut engine, id) = engine_with_open_fence(&tmp);
let (actor, client) = cli_actor();
let stored = engine.get_entity(&id).expect("entity loads");
assert!(
stored
.sections
.get("purpose")
.is_none_or(|v| v.trim().is_empty()),
"purpose should read as absent or empty: {:?}",
stored.sections.get("purpose")
);
assert!(
stored.sections["identity"].contains("## Purpose"),
"its content is inside identity: {:?}",
stored.sections.get("identity")
);
let hash = stored.content_hash.clone();
let err = engine
.update_entity(
UpdateEntityArgs {
anchors: Vec::new(),
id: id.clone(),
expected_hash: crate::ExpectedHash::Current(hash),
sections: IndexMap::from_iter([(
"purpose".to_string(),
"a new purpose".to_string(),
)]),
append_sections: IndexMap::new(),
patch_sections: IndexMap::new(),
sections_unset: Vec::new(),
metadata: IndexMap::new(),
metadata_unset: Vec::new(),
declare_relations: Vec::new(),
dry_run: false,
relations_unset: Vec::new(),
anchors_unset: Vec::new(),
},
actor,
Some(&client),
None,
)
.unwrap_err();
match err {
EngineError::UnterminatedFenceInStoredBody {
ref section,
ref fence,
ref swallowed,
..
} => {
assert_eq!(section, "identity");
assert_eq!(fence, "```");
assert_eq!(swallowed, &vec!["Purpose".to_string()]);
}
other => panic!("expected UnterminatedFenceInStoredBody, got {other:?}"),
}
assert_eq!(err.code(), "UNTERMINATED_FENCE_IN_STORED_BODY");
}
#[test]
fn replacing_the_absorbing_section_is_the_way_out() {
let tmp = TempDir::new().unwrap();
let (mut engine, id) = engine_with_open_fence(&tmp);
let (actor, client) = cli_actor();
let hash = engine.get_entity(&id).unwrap().content_hash.clone();
let outcome = engine
.update_entity(
UpdateEntityArgs {
anchors: Vec::new(),
id: id.clone(),
expected_hash: crate::ExpectedHash::Current(hash),
sections: IndexMap::from_iter([
(
"identity".to_string(),
"intro\n\n```rust\nfn main() {}\n```".to_string(),
),
("purpose".to_string(), "the recovered purpose".to_string()),
]),
append_sections: IndexMap::new(),
patch_sections: IndexMap::new(),
sections_unset: Vec::new(),
metadata: IndexMap::new(),
metadata_unset: Vec::new(),
declare_relations: Vec::new(),
dry_run: false,
relations_unset: Vec::new(),
anchors_unset: Vec::new(),
},
actor,
Some(&client),
None,
)
.expect("a corrected body for the absorbing section is admitted");
assert!(
outcome
.modified_sections
.replaced
.contains(&"identity".to_string())
);
let fixed = engine.get_entity(&id).unwrap();
assert_eq!(
fixed.sections.get("purpose").map(String::as_str),
Some("the recovered purpose"),
"the swallowed section is a section again"
);
assert!(
crate::markdown::closing_fence_if_unterminated(fixed.sections.get("identity").unwrap())
.is_none()
);
}
#[test]
fn every_verb_that_regenerates_the_file_is_gated_not_only_update() {
let tmp = TempDir::new().unwrap();
let (mut engine, id) = engine_with_open_fence(&tmp);
let (actor, client) = cli_actor();
let before =
std::fs::read_to_string(tmp.path().join(&engine.get_entity(&id).unwrap().file_path))
.unwrap();
let hash = engine.get_entity(&id).unwrap().content_hash.clone();
let err = engine
.relate_entity(
RelateEntityArgs {
source: id.clone(),
expected_hash: Some(hash),
rel_type: "USES".to_string(),
target: crate::EntityId::new("specs", "some-target"),
remove: false,
description: None,
dry_run: false,
},
actor,
Some(&client),
None,
)
.expect_err("relate must not be able to freeze the absorption");
assert_eq!(err.code(), "UNTERMINATED_FENCE_IN_STORED_BODY");
let err = engine
.rename_entity(
crate::engine::RenameEntityArgs {
id: id.clone(),
new_title: "Renamed Fenced".to_string(),
expected_hash: crate::ExpectedHash::Current(
engine.get_entity(&id).unwrap().content_hash.clone(),
),
},
actor,
Some(&client),
None,
)
.expect_err("rename must not be able to freeze the absorption either");
assert_eq!(err.code(), "UNTERMINATED_FENCE_IN_STORED_BODY");
let after =
std::fs::read_to_string(tmp.path().join(&engine.get_entity(&id).unwrap().file_path))
.unwrap();
assert_eq!(before, after, "a refused write must not touch the file");
}
#[test]
fn an_entity_with_no_open_fence_updates_exactly_as_before() {
let tmp = TempDir::new().unwrap();
let (mut engine, seeded) = engine_with_seed(&tmp, "Ordinary");
let (actor, client) = cli_actor();
engine
.update_entity(
UpdateEntityArgs {
anchors: Vec::new(),
id: seeded.id.clone(),
expected_hash: crate::ExpectedHash::Current(seeded.content_hash.clone()),
sections: IndexMap::from_iter([(
"purpose".to_string(),
"a new purpose".to_string(),
)]),
append_sections: IndexMap::new(),
patch_sections: IndexMap::new(),
sections_unset: Vec::new(),
metadata: IndexMap::new(),
metadata_unset: Vec::new(),
declare_relations: Vec::new(),
dry_run: false,
relations_unset: Vec::new(),
anchors_unset: Vec::new(),
},
actor,
Some(&client),
None,
)
.expect("an ordinary update is untouched by the fence gate");
}
#[test]
fn update_entity_against_stub_surfaces_typed_stub_not_updatable() {
let tmp = TempDir::new().unwrap();
let (mut engine, source) = engine_with_seed(&tmp, "Source");
let (actor, client) = cli_actor();
let stub_id = crate::EntityId::new("specs", "stub-update-target");
engine
.relate_entity(
RelateEntityArgs {
source: source.id.clone(),
expected_hash: Some(source.content_hash.clone()),
rel_type: "USES".to_string(),
target: stub_id.clone(),
remove: false,
description: None,
dry_run: false,
},
actor,
Some(&client),
None,
)
.unwrap();
let err = engine
.update_entity(
UpdateEntityArgs {
anchors: Vec::new(),
id: stub_id.clone(),
expected_hash: crate::ExpectedHash::Current(String::new()),
sections: IndexMap::from_iter([("identity".to_string(), "body".to_string())]),
append_sections: IndexMap::new(),
patch_sections: IndexMap::new(),
sections_unset: Vec::new(),
metadata: IndexMap::new(),
metadata_unset: Vec::new(),
declare_relations: Vec::new(),
dry_run: false,
relations_unset: Vec::new(),
anchors_unset: Vec::new(),
},
actor,
Some(&client),
None,
)
.unwrap_err();
match err {
EngineError::StubNotUpdatable { id } => assert_eq!(id, stub_id.to_string()),
other => panic!("expected StubNotUpdatable, got {other:?}"),
}
}
#[test]
fn update_entity_rejects_conflicting_section_modes() {
let tmp = TempDir::new().unwrap();
let (mut engine, seeded) = engine_with_seed(&tmp, "Conflict");
let (actor, client) = cli_actor();
let mut sections = IndexMap::new();
sections.insert("identity".to_string(), "replace".to_string());
let mut appends = IndexMap::new();
appends.insert("identity".to_string(), "append".to_string());
let err = engine
.update_entity(
UpdateEntityArgs {
anchors: Vec::new(),
id: seeded.id.clone(),
expected_hash: crate::ExpectedHash::Current(seeded.content_hash.clone()),
sections,
append_sections: appends,
patch_sections: IndexMap::new(),
sections_unset: Vec::new(),
metadata: IndexMap::new(),
metadata_unset: Vec::new(),
declare_relations: Vec::new(),
dry_run: false,
relations_unset: Vec::new(),
anchors_unset: Vec::new(),
},
actor,
Some(&client),
None,
)
.unwrap_err();
match err {
EngineError::ConflictingSectionModes { section, modes } => {
assert_eq!(section, "identity");
assert_eq!(modes, vec!["sections", "append_sections"]);
}
other => panic!("expected ConflictingSectionModes, got {other:?}"),
}
}
#[test]
fn update_entity_rejects_overlapping_metadata_and_metadata_unset_keys() {
let tmp = TempDir::new().unwrap();
let (mut engine, seeded) = engine_with_seed(&tmp, "Overlap Subject");
let (actor, client) = cli_actor();
let mut metadata = IndexMap::new();
metadata.insert("tags".to_string(), "foo".to_string());
let err = engine
.update_entity(
UpdateEntityArgs {
anchors: Vec::new(),
id: seeded.id.clone(),
expected_hash: crate::ExpectedHash::Current(seeded.content_hash.clone()),
sections: IndexMap::new(),
append_sections: IndexMap::new(),
patch_sections: IndexMap::new(),
sections_unset: Vec::new(),
metadata,
metadata_unset: vec!["tags".to_string()],
declare_relations: Vec::new(),
dry_run: false,
relations_unset: Vec::new(),
anchors_unset: Vec::new(),
},
actor,
Some(&client),
None,
)
.unwrap_err();
match err {
EngineError::SetAndUnsetConflict { keys } => {
assert_eq!(keys, vec!["tags".to_string()]);
}
other => panic!("expected SetAndUnsetConflict, got {other:?}"),
}
}
#[test]
fn a_patch_that_assembles_a_heading_is_refused() {
let tmp = TempDir::new().unwrap();
let (mut engine, seeded) = engine_with_seed(&tmp, "Heading Patch");
let (actor, client) = cli_actor();
let staged = engine
.update_entity(
UpdateEntityArgs {
sections: IndexMap::from_iter([(
"identity".to_string(),
"intro\n ## Purpose\nkept".to_string(),
)]),
..unset_args(seeded.id.clone(), seeded.content_hash.clone(), &[])
},
actor,
Some(&client),
None,
)
.unwrap();
let purpose_before = engine.store().get(&seeded.id).unwrap().sections["purpose"].clone();
let mut patches = IndexMap::new();
patches.insert(
"identity".to_string(),
vec![crate::ops::PatchArg {
old: " ## ".to_string(),
new: "## ".to_string(),
all: false,
}],
);
let err = engine
.update_entity(
UpdateEntityArgs {
patch_sections: patches,
..unset_args(seeded.id.clone(), staged.content_hash.clone(), &[])
},
actor,
Some(&client),
None,
)
.unwrap_err();
assert_eq!(err.code(), "SECTION_CONTENT_INVALID", "{err:?}");
let after = engine.store().get(&seeded.id).unwrap();
assert_eq!(after.content_hash, staged.content_hash);
assert_eq!(after.sections["purpose"], purpose_before);
}