use super::*;
#[test]
fn update_entity_pointer_schema_auto_synthesises_references_from_body_link() {
use crate::EntityId;
use crate::engine::UpdateEntityArgs;
use indexmap::IndexMap;
use tempfile::TempDir;
let tmp = TempDir::new().unwrap();
let mem_dir = tmp.path().to_path_buf();
let writer = FilesystemBackend::new(mem_dir.clone());
let mut engine = Engine::from_mounts(vec![(
folder_mount("specs", mem_dir.clone()),
Box::new(writer) as Box<dyn MemBackend>,
)])
.unwrap();
engine.set_workspace_root(mem_dir.clone());
let (actor, client) = cli_actor();
let target = engine
.create_entity(
empty_create_args("specs", "Target"),
actor,
Some(&client),
None,
)
.unwrap();
let source = engine
.create_entity(
empty_create_args("specs", "Source"),
actor,
Some(&client),
None,
)
.unwrap();
let mut sections: IndexMap<String, String> = IndexMap::new();
sections.insert(
"purpose".to_string(),
"see [[target]] for context".to_string(),
);
let outcome = engine
.update_entity(
UpdateEntityArgs {
anchors: Vec::new(),
id: source.id.clone(),
expected_hash: crate::ExpectedHash::Current(source.content_hash.clone()),
sections,
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("auto-synthesis must satisfy the alias-existence invariant");
assert!(
outcome
.modified_sections
.replaced
.iter()
.any(|s| s == "purpose"),
);
let in_mem = engine.get_entity(&source.id).unwrap();
assert_eq!(
in_mem
.sections
.get("purpose")
.map(String::as_str)
.unwrap_or(""),
"see [[target]] for context",
);
assert!(
in_mem
.relationships
.iter()
.any(|r| r.rel_type == "REFERENCES" && r.target == target.id),
"synthesis must emit REFERENCES → target; relationships: {:?}",
in_mem.relationships,
);
let _ = EntityId::new("specs", "x");
}
#[test]
fn update_entity_declare_relations_passes_strict_validator_in_one_call() {
use crate::engine::UpdateEntityArgs;
use crate::ops::RelateArg;
use indexmap::IndexMap;
use tempfile::TempDir;
let tmp = TempDir::new().unwrap();
let mem_dir = tmp.path().to_path_buf();
let writer = FilesystemBackend::new(mem_dir.clone());
let mut engine = Engine::from_mounts(vec![(
folder_mount("specs", mem_dir.clone()),
Box::new(writer) as Box<dyn MemBackend>,
)])
.unwrap();
engine.set_workspace_root(mem_dir.clone());
let (actor, client) = cli_actor();
let target = engine
.create_entity(
empty_create_args("specs", "Target"),
actor,
Some(&client),
None,
)
.unwrap();
let source = engine
.create_entity(
empty_create_args("specs", "Source"),
actor,
Some(&client),
None,
)
.unwrap();
let mut sections: IndexMap<String, String> = IndexMap::new();
sections.insert(
"purpose".to_string(),
"see [[target]] for context".to_string(),
);
let outcome = engine
.update_entity(
UpdateEntityArgs {
anchors: Vec::new(),
relations_unset: Vec::new(),
anchors_unset: Vec::new(),
id: source.id.clone(),
expected_hash: crate::ExpectedHash::Current(source.content_hash.clone()),
sections,
append_sections: IndexMap::new(),
patch_sections: IndexMap::new(),
sections_unset: Vec::new(),
metadata: IndexMap::new(),
metadata_unset: Vec::new(),
dry_run: false,
declare_relations: vec![RelateArg {
rel_type: "USES".to_string(),
target: target.id.clone(),
description: None,
}],
},
actor,
Some(&client),
None,
)
.expect("declare_relations + body update must succeed in one call");
assert_eq!(outcome.relations_declared.len(), 1);
assert_eq!(outcome.relations_declared[0].rel_type, "USES");
assert_eq!(outcome.relations_declared[0].target, target.id);
assert!(
!outcome.relations_declared[0].target_was_stubbed,
"target was already present in store; target_was_stubbed must be false"
);
let in_mem = engine.get_entity(&source.id).unwrap();
assert!(
in_mem.relationships.iter().any(|r| r.target == target.id),
"declared relation must land in entity.relationships; got {:?}",
in_mem.relationships
);
}
#[test]
fn update_entity_declare_relations_auto_stubs_absent_target() {
use crate::EntityId;
use crate::engine::UpdateEntityArgs;
use crate::ops::RelateArg;
use indexmap::IndexMap;
let tmp = TempDir::new().unwrap();
let (mut engine, source) = engine_with_seed(&tmp, "Source");
let (actor, client) = cli_actor();
let absent_target = EntityId::new("specs", "not-yet-existing");
assert!(!engine.store().contains(&absent_target));
let outcome = engine
.update_entity(
UpdateEntityArgs {
anchors: Vec::new(),
relations_unset: Vec::new(),
anchors_unset: Vec::new(),
id: source.id.clone(),
expected_hash: crate::ExpectedHash::Current(source.content_hash.clone()),
sections: IndexMap::new(),
append_sections: IndexMap::new(),
patch_sections: IndexMap::new(),
sections_unset: Vec::new(),
metadata: IndexMap::new(),
metadata_unset: Vec::new(),
dry_run: false,
declare_relations: vec![RelateArg {
rel_type: "USES".to_string(),
target: absent_target.clone(),
description: None,
}],
},
actor,
Some(&client),
None,
)
.unwrap();
assert_eq!(outcome.relations_declared.len(), 1);
assert!(
outcome.relations_declared[0].target_was_stubbed,
"absent target must be auto-stubbed; got target_was_stubbed=false"
);
assert!(engine.store().contains(&absent_target));
let stub = engine.get_entity(&absent_target).unwrap();
assert!(stub.stub);
}
#[test]
fn update_entity_alias_synthesis_runs_unconditionally_for_pointer_schemas() {
use crate::engine::UpdateEntityArgs;
use indexmap::IndexMap;
use tempfile::TempDir;
let tmp = TempDir::new().unwrap();
let mem_dir = tmp.path().to_path_buf();
let writer = FilesystemBackend::new(mem_dir.clone());
let mut engine = Engine::from_mounts(vec![(
folder_mount("specs", mem_dir.clone()),
Box::new(writer) as Box<dyn MemBackend>,
)])
.unwrap();
engine.set_workspace_root(mem_dir.clone());
let (actor, client) = cli_actor();
let target = engine
.create_entity(
empty_create_args("specs", "Target"),
actor,
Some(&client),
None,
)
.unwrap();
let source = engine
.create_entity(
empty_create_args("specs", "Source"),
actor,
Some(&client),
None,
)
.unwrap();
let mut sections: IndexMap<String, String> = IndexMap::new();
sections.insert(
"purpose".to_string(),
"see [[target]] for context".to_string(),
);
engine
.update_entity(
UpdateEntityArgs {
anchors: Vec::new(),
id: source.id.clone(),
expected_hash: crate::ExpectedHash::Current(source.content_hash.clone()),
sections,
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("synthesis must back the wiki-link and let the body land");
let in_mem = engine.get_entity(&source.id).unwrap();
assert!(
in_mem
.relationships
.iter()
.any(|r| r.rel_type == "REFERENCES" && r.target == target.id),
"synthesis must emit REFERENCES → target; relationships: {:?}",
in_mem.relationships,
);
}
#[test]
fn update_entity_dry_run_returns_prospective_hash_without_writing() {
let tmp = TempDir::new().unwrap();
let (mut engine, seeded) = engine_with_seed(&tmp, "Preview Subject");
let (actor, client) = cli_actor();
let original_hash = seeded.content_hash.clone();
let mut sections = IndexMap::new();
sections.insert("identity".to_string(), "preview body".to_string());
let outcome = engine
.update_entity(
UpdateEntityArgs {
anchors: Vec::new(),
id: seeded.id.clone(),
expected_hash: crate::ExpectedHash::Current("wrong-hash".to_string()),
sections,
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: true,
relations_unset: Vec::new(),
anchors_unset: Vec::new(),
},
actor,
Some(&client),
None,
)
.unwrap();
assert_eq!(outcome.content_hash, original_hash);
let prospective = outcome
.prospective_hash
.expect("prospective_hash populated on dry_run");
assert_ne!(prospective, original_hash);
assert!(outcome.write_id.is_empty());
let store_entity = engine.get_entity(&seeded.id).unwrap();
assert_eq!(store_entity.content_hash, original_hash);
}
#[test]
fn references_edges_round_trip_across_full_crud_cycle() {
let tmp = TempDir::new().unwrap();
let mem_dir = tmp.path().to_path_buf();
let writer = FilesystemBackend::new(mem_dir.clone());
let mut engine = Engine::from_mounts(vec![(
folder_mount("specs", mem_dir),
Box::new(writer) as Box<dyn MemBackend>,
)])
.unwrap();
let (actor, client) = cli_actor();
let foo = engine
.create_entity(
empty_create_args("specs", "Foo"),
actor,
Some(&client),
None,
)
.unwrap();
let bar = engine
.create_entity(
empty_create_args("specs", "Bar"),
actor,
Some(&client),
None,
)
.unwrap();
let count_references = |engine: &Engine| -> usize {
engine
.store()
.all_ids()
.flat_map(|id| engine.store().outgoing(id))
.filter(|e| e.rel_type == "REFERENCES")
.count()
};
let baseline_edges = engine.store().edge_count();
let baseline_refs = count_references(&engine);
let mut sections = IndexMap::new();
sections.insert(
"identity".to_string(),
"See [[foo]] and [[bar]] inline.".to_string(),
);
sections.insert("purpose".to_string(), "probe purpose".to_string());
let probe = engine
.create_entity(
CreateEntityArgs {
anchors: Vec::new(),
mem: "specs".to_string(),
title: "Probe".to_string(),
entity_type: "spec".to_string(),
sections,
metadata: IndexMap::new(),
relations: Vec::new(),
dry_run: false,
},
actor,
Some(&client),
None,
)
.unwrap();
assert_eq!(count_references(&engine), baseline_refs + 2);
let relate1 = engine
.relate_entity(
RelateEntityArgs {
source: probe.id.clone(),
expected_hash: Some(probe.content_hash.clone()),
rel_type: "INFORMED_BY".to_string(),
target: foo.id.clone(),
remove: false,
description: None,
dry_run: false,
},
actor,
Some(&client),
None,
)
.unwrap();
assert_eq!(
count_references(&engine),
baseline_refs + 2,
"set-membership aliasing — adding INFORMED_BY does not \
absorb the REFERENCES relation"
);
let mut sections = IndexMap::new();
sections.insert("identity".to_string(), "See [[foo]] inline.".to_string());
let updated = engine
.update_entity(
UpdateEntityArgs {
anchors: Vec::new(),
id: probe.id.clone(),
expected_hash: crate::ExpectedHash::Current(relate1.content_hash.clone()),
sections,
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();
assert_eq!(
count_references(&engine),
baseline_refs + 1,
"REFERENCES → bar must be auto-GC'd when its body link drops"
);
let renamed = engine
.rename_entity(
crate::engine::RenameEntityArgs {
id: probe.id.clone(),
expected_hash: crate::ExpectedHash::Current(updated.content_hash.clone()),
new_title: "Probe Renamed".to_string(),
},
actor,
Some(&client),
None,
)
.unwrap();
assert_eq!(count_references(&engine), baseline_refs + 1);
engine
.delete_entity(
crate::engine::DeleteEntityArgs {
id: renamed.new_id.clone(),
expected_hash: crate::ExpectedHash::Current(renamed.content_hash.clone()),
},
actor,
Some(&client),
None,
)
.unwrap();
assert_eq!(
engine.store().edge_count(),
baseline_edges,
"total edges must round-trip to baseline"
);
assert_eq!(
count_references(&engine),
baseline_refs,
"REFERENCES counter must round-trip to baseline"
);
engine.reload_one_mem("specs").unwrap();
assert_eq!(
engine.store().edge_count(),
baseline_edges,
"total edges must match disk after reload"
);
assert_eq!(
count_references(&engine),
baseline_refs,
"REFERENCES must match disk after reload"
);
assert!(engine.store().contains(&foo.id));
assert!(engine.store().contains(&bar.id));
}
#[test]
fn update_entity_returns_write_id_title_modified_date_warnings_shape() {
let tmp = TempDir::new().unwrap();
let (mut engine, seeded) = engine_with_seed(&tmp, "Subject");
let (actor, client) = cli_actor();
let mut sections = IndexMap::new();
sections.insert("identity".to_string(), "edited body".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,
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();
assert!(
!outcome.write_id.is_empty(),
"write_id must be populated on a real update"
);
assert_eq!(outcome.title, "Subject");
assert!(
!outcome.modified_date.is_empty(),
"modified_date must be auto-stamped on update for the default spec schema",
);
assert!(outcome.warnings.is_empty());
assert_eq!(
outcome.modified_sections.replaced,
vec!["identity".to_string()]
);
}
#[test]
fn update_entity_noop_resetting_section_to_current_value_preserves_last_modified() {
let tmp = TempDir::new().unwrap();
let (mut engine, seeded) = engine_with_seed(&tmp, "Section Resetter");
let (actor, client) = cli_actor();
let pre_last_modified = engine
.get_entity(&seeded.id)
.and_then(|e| e.metadata.get("last_modified"))
.map(|v| v.to_frontmatter_string())
.expect("seeded entity has last_modified");
let mut sections = IndexMap::new();
sections.insert("identity".to_string(), "fixture identity body".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,
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();
assert_eq!(outcome.write_id, "", "no-op must not commit");
assert_eq!(
outcome.content_hash, seeded.content_hash,
"no-op must not advance content_hash",
);
assert!(
outcome.warnings.iter().any(|w| w.code() == "UPDATE_NOOP"),
"UPDATE_NOOP must fire on bytes-identical re-set",
);
assert_eq!(
outcome.modified_date, pre_last_modified,
"no-op must preserve last_modified at the pre-call value",
);
assert!(
outcome.modified_sections.replaced.is_empty()
&& outcome.modified_sections.appended.is_empty()
&& outcome.modified_sections.patched.is_empty(),
"no-op must report an empty section delta, got {:?}",
outcome.modified_sections,
);
let post_last_modified = engine
.get_entity(&seeded.id)
.and_then(|e| e.metadata.get("last_modified"))
.map(|v| v.to_frontmatter_string())
.expect("entity still in store");
assert_eq!(post_last_modified, pre_last_modified);
}
#[test]
fn update_entity_empty_payload_refuses_with_typed_code() {
let tmp = TempDir::new().unwrap();
let (mut engine, seeded) = engine_with_seed(&tmp, "Empty Payload");
let (actor, client) = cli_actor();
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: 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::EmptyUpdate { id } => {
assert_eq!(id, seeded.id.to_string());
}
other => panic!("expected EMPTY_UPDATE, got {other:?}"),
}
let log_path = tmp.path().join(".memstead/changes.jsonl");
if let Ok(log) = std::fs::read_to_string(&log_path) {
let updates = log.matches("\"kind\":\"update\"").count();
assert_eq!(updates, 0, "EMPTY_UPDATE refusal must not log an update");
}
}
#[test]
fn update_entity_noop_same_content_surfaces_warning() {
let tmp = TempDir::new().unwrap();
let (mut engine, seeded) = engine_with_seed(&tmp, "Same Content Noop");
let (actor, client) = cli_actor();
let mut sections = IndexMap::new();
sections.insert("identity".to_string(), "fixture identity body".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,
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();
assert_eq!(outcome.write_id, "");
assert_eq!(outcome.content_hash, seeded.content_hash);
let codes: Vec<&str> = outcome.warnings.iter().map(|w| w.code()).collect();
assert!(
codes.contains(&"UPDATE_NOOP"),
"same-content update must surface UPDATE_NOOP; got {codes:?}",
);
}
#[test]
fn update_entity_noop_metadata_unset_on_absent_key() {
let tmp = TempDir::new().unwrap();
let (mut engine, seeded) = engine_with_seed(&tmp, "Absent Key Noop");
let (actor, client) = cli_actor();
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: IndexMap::new(),
patch_sections: IndexMap::new(),
sections_unset: Vec::new(),
metadata: IndexMap::new(),
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();
assert_eq!(outcome.write_id, "");
assert_eq!(outcome.content_hash, seeded.content_hash);
assert!(
outcome.warnings.iter().any(|w| w.code() == "UPDATE_NOOP"),
"absent-key metadata_unset must surface UPDATE_NOOP",
);
assert!(
outcome.modified_metadata.set.is_empty() && outcome.modified_metadata.unset.is_empty(),
"no-op must report an empty metadata delta, got {:?}",
outcome.modified_metadata,
);
let mut sections = IndexMap::new();
sections.insert("identity".to_string(), "real change".to_string());
let real = engine
.update_entity(
UpdateEntityArgs {
anchors: Vec::new(),
id: seeded.id.clone(),
expected_hash: crate::ExpectedHash::Current(seeded.content_hash.clone()),
sections,
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();
assert!(!real.write_id.is_empty());
assert_ne!(real.content_hash, seeded.content_hash);
}
#[test]
fn update_entity_noop_setting_metadata_to_current_value_reports_empty_delta() {
let tmp = TempDir::new().unwrap();
let (mut engine, seeded) = engine_with_seed(&tmp, "Stability Resetter");
let (actor, client) = cli_actor();
let mut metadata = IndexMap::new();
metadata.insert("level".to_string(), "M0".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: IndexMap::new(),
patch_sections: IndexMap::new(),
sections_unset: Vec::new(),
metadata,
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.write_id, "", "no-op must not commit");
assert_eq!(
outcome.content_hash, seeded.content_hash,
"no-op must not advance hash"
);
assert!(
outcome.warnings.iter().any(|w| w.code() == "UPDATE_NOOP"),
"re-set to current value must surface UPDATE_NOOP",
);
assert!(
outcome.modified_metadata.set.is_empty() && outcome.modified_metadata.unset.is_empty(),
"no-op must not claim `level` was set — applied delta is empty, got {:?}",
outcome.modified_metadata,
);
}
#[test]
fn update_entity_noop_declare_already_related_edge() {
use crate::ops::RelateArg;
let tmp = TempDir::new().unwrap();
let mem_dir = tmp.path().to_path_buf();
let writer = FilesystemBackend::new(mem_dir.clone());
let mut engine = Engine::from_mounts(vec![(
folder_mount("specs", mem_dir),
Box::new(writer) as Box<dyn MemBackend>,
)])
.unwrap();
let (actor, client) = cli_actor();
let target = engine
.create_entity(
empty_create_args("specs", "Target Already Related"),
actor,
Some(&client),
None,
)
.unwrap();
let source = engine
.create_entity(
empty_create_args("specs", "Source Already Related"),
actor,
Some(&client),
None,
)
.unwrap();
let after_relate = engine
.relate_entity(
RelateEntityArgs {
source: source.id.clone(),
expected_hash: Some(source.content_hash.clone()),
rel_type: "USES".to_string(),
target: target.id.clone(),
remove: false,
description: None,
dry_run: false,
},
actor,
Some(&client),
None,
)
.unwrap();
let outcome = engine
.update_entity(
UpdateEntityArgs {
anchors: Vec::new(),
relations_unset: Vec::new(),
anchors_unset: Vec::new(),
id: source.id.clone(),
expected_hash: crate::ExpectedHash::Current(after_relate.content_hash.clone()),
sections: IndexMap::new(),
append_sections: IndexMap::new(),
patch_sections: IndexMap::new(),
sections_unset: Vec::new(),
metadata: IndexMap::new(),
metadata_unset: Vec::new(),
declare_relations: vec![RelateArg {
rel_type: "USES".to_string(),
target: target.id.clone(),
description: None,
}],
dry_run: false,
},
actor,
Some(&client),
None,
)
.unwrap();
assert_eq!(outcome.write_id, "");
assert_eq!(outcome.content_hash, after_relate.content_hash);
assert!(
outcome.warnings.iter().any(|w| w.code() == "UPDATE_NOOP"),
"duplicate declare must surface UPDATE_NOOP",
);
assert_eq!(outcome.relations_declared.len(), 1);
assert_eq!(outcome.relations_declared[0].rel_type, "USES");
assert_eq!(outcome.relations_declared[0].target, target.id);
assert!(!outcome.relations_declared[0].target_was_stubbed);
}
#[test]
fn update_entity_real_change_still_commits_and_advances_hash() {
let tmp = TempDir::new().unwrap();
let (mut engine, seeded) = engine_with_seed(&tmp, "Real Change Subject");
let (actor, client) = cli_actor();
let mut sections = IndexMap::new();
sections.insert("identity".to_string(), "definitely new body".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,
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();
assert!(!outcome.write_id.is_empty(), "real change must commit");
assert_ne!(
outcome.content_hash, seeded.content_hash,
"real change must advance content_hash",
);
assert!(
!outcome.warnings.iter().any(|w| w.code() == "UPDATE_NOOP"),
"real change must not surface UPDATE_NOOP",
);
}
#[test]
fn update_entity_noop_preserves_expected_hash_across_chain() {
let tmp = TempDir::new().unwrap();
let (mut engine, seeded) = engine_with_seed(&tmp, "Chained Noops Subject");
let (actor, client) = cli_actor();
let mut noop_sections = IndexMap::new();
noop_sections.insert("identity".to_string(), "fixture identity body".to_string());
for _ in 0..2 {
let outcome = engine
.update_entity(
UpdateEntityArgs {
anchors: Vec::new(),
id: seeded.id.clone(),
expected_hash: crate::ExpectedHash::Current(seeded.content_hash.clone()),
sections: noop_sections.clone(),
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();
assert_eq!(outcome.write_id, "");
assert_eq!(outcome.content_hash, seeded.content_hash);
}
let mut sections = IndexMap::new();
sections.insert(
"identity".to_string(),
"third call: real change".to_string(),
);
let real = engine
.update_entity(
UpdateEntityArgs {
anchors: Vec::new(),
id: seeded.id.clone(),
expected_hash: crate::ExpectedHash::Current(seeded.content_hash.clone()),
sections,
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();
assert!(!real.write_id.is_empty());
assert_ne!(real.content_hash, seeded.content_hash);
}
#[test]
fn a_refused_update_leaves_no_stub_for_its_declared_target() {
let tmp = TempDir::new().unwrap();
let (mut engine, seeded) = engine_with_seed(&tmp, "Stub Source");
let (actor, client) = cli_actor();
let ghost = EntityId("specs--ghost".to_string());
assert!(engine.get_entity(&ghost).is_none());
let mut patches = IndexMap::new();
patches.insert(
"identity".to_string(),
vec![crate::ops::PatchArg {
old: "text that is not there".to_string(),
new: "anything".to_string(),
all: false,
}],
);
let err = engine
.update_entity(
UpdateEntityArgs {
anchors: Vec::new(),
relations_unset: Vec::new(),
anchors_unset: 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(),
dry_run: false,
declare_relations: vec![crate::ops::RelateArg {
rel_type: "USES".to_string(),
target: ghost.clone(),
description: None,
}],
},
actor,
Some(&client),
None,
)
.unwrap_err();
assert_eq!(err.code(), "PATCH_OLD_NOT_FOUND", "{err:?}");
assert!(
engine.get_entity(&ghost).is_none(),
"the refused update left a stub for its declared target"
);
}