#![cfg(test)]
use std::path::PathBuf;
use tempfile::TempDir;
use crate::backend::MemBackend;
use crate::engine::test_helpers::*;
use crate::engine::{Engine, EngineError, RenameEntityArgs};
use crate::ops::WarningHint;
use crate::storage::FilesystemBackend;
#[test]
fn rename_moves_entity_anchors_to_new_id() {
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();
let (actor, client) = cli_actor();
let mut args = empty_create_args("specs", "Old Anchored");
args.anchors = vec![crate::anchor::AnchorInput {
artifact: Some("src/lib.rs".into()),
grain: Some("file".into()),
class: Some("anchored".into()),
hash: Some("h1".into()),
hash_stability: Some("stable".into()),
..Default::default()
}];
let seeded = engine
.create_entity(args, actor, Some(&client), None)
.unwrap();
let old_id = seeded.id.clone();
let outcome = engine
.rename_entity(
RenameEntityArgs {
id: old_id.clone(),
expected_hash: Some(seeded.content_hash.clone()),
new_title: "New Anchored".to_string(),
},
actor,
Some(&client),
None,
)
.unwrap();
assert!(engine.entity_anchors(&old_id).is_empty());
assert_eq!(engine.entity_anchors(&outcome.new_id).len(), 1);
assert_eq!(
engine.anchors_referencing_artifact("src/lib.rs"),
vec![(
outcome.new_id.clone(),
engine.entity_anchors(&outcome.new_id)[0].clone()
)]
);
}
#[test]
fn rename_entity_renames_file_and_id_persists_across_restart() {
let tmp = TempDir::new().unwrap();
let mem_dir = tmp.path().to_path_buf();
let (old_id, new_id, new_file) = {
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();
let (actor, client) = cli_actor();
let seeded = engine
.create_entity(
empty_create_args("specs", "Old Name"),
actor,
Some(&client),
None,
)
.unwrap();
let outcome = engine
.rename_entity(
RenameEntityArgs {
id: seeded.id.clone(),
expected_hash: Some(seeded.content_hash.clone()),
new_title: "New Name".to_string(),
},
actor,
Some(&client),
None,
)
.unwrap();
assert_eq!(outcome.old_id.to_string(), "specs--old-name");
assert_eq!(outcome.new_id.to_string(), "specs--new-name");
assert_eq!(outcome.new_path, "new-name.md");
assert!(!mem_dir.join(&outcome.old_path).exists());
assert!(mem_dir.join(&outcome.new_path).exists());
(outcome.old_id, outcome.new_id, outcome.new_path)
};
let writer2 = FilesystemBackend::new(mem_dir.clone());
let engine2 = Engine::from_mounts(vec![(
folder_mount("specs", mem_dir),
Box::new(writer2) as Box<dyn MemBackend>,
)])
.unwrap();
assert!(engine2.get_entity(&old_id).is_none());
let new_entity = engine2.get_entity(&new_id).expect("new id must persist");
assert_eq!(new_entity.title, "New Name");
assert_eq!(new_entity.file_path, new_file);
}
#[test]
fn rename_entity_returns_typed_warning_on_slug_noop() {
let tmp = TempDir::new().unwrap();
let (mut engine, seeded) = engine_with_seed(&tmp, "Same Slug");
let (actor, client) = cli_actor();
let outcome = engine
.rename_entity(
RenameEntityArgs {
id: seeded.id.clone(),
expected_hash: Some(seeded.content_hash.clone()),
new_title: "Same Slug".to_string(), },
actor,
Some(&client),
None,
)
.unwrap();
assert_eq!(outcome.old_id, outcome.new_id);
assert_eq!(outcome.old_path, outcome.new_path);
assert!(outcome.write_id.is_empty());
assert_eq!(outcome.warnings.len(), 1);
assert!(matches!(
outcome.warnings[0],
WarningHint::TitleNormalizedToSlugNoop { .. }
));
}
#[test]
fn rename_entity_returns_write_id_on_real_rename() {
let tmp = TempDir::new().unwrap();
let (mut engine, seeded) = engine_with_seed(&tmp, "Old Name");
let (actor, client) = cli_actor();
let outcome = engine
.rename_entity(
RenameEntityArgs {
id: seeded.id.clone(),
expected_hash: Some(seeded.content_hash.clone()),
new_title: "Brand New Name".to_string(),
},
actor,
Some(&client),
None,
)
.unwrap();
assert_ne!(outcome.old_id, outcome.new_id);
assert!(
!outcome.write_id.is_empty(),
"write_id must be populated on a real rename"
);
assert!(outcome.warnings.is_empty());
}
#[test]
fn rename_entity_rewrites_self_references_in_body_and_relationships() {
use crate::entity::EntityId;
use indexmap::IndexMap;
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();
let (actor, client) = cli_actor();
let mut sections: IndexMap<String, String> = IndexMap::new();
sections.insert("identity".to_string(), "the seed identity".to_string());
sections.insert(
"purpose".to_string(),
"see also [[old-name]] for prior context".to_string(),
);
let seeded = engine
.create_entity(
crate::engine::CreateEntityArgs {
anchors: Vec::new(),
mem: "specs".to_string(),
title: "Old Name".to_string(),
entity_type: "spec".to_string(),
sections,
metadata: IndexMap::new(),
relations: Vec::new(),
dry_run: false,
},
actor,
Some(&client),
None,
)
.unwrap();
assert_eq!(seeded.id.to_string(), "specs--old-name");
let related = seeded.clone();
let outcome = engine
.rename_entity(
RenameEntityArgs {
id: seeded.id.clone(),
expected_hash: Some(related.content_hash.clone()),
new_title: "Brand New Name".to_string(),
},
actor,
Some(&client),
None,
)
.unwrap();
assert_eq!(outcome.new_id.to_string(), "specs--brand-new-name");
let new_bytes = std::fs::read_to_string(mem_dir.join(&outcome.new_path)).unwrap();
assert!(
new_bytes.contains("[[brand-new-name]]"),
"expected new slug in body, got:\n{new_bytes}"
);
assert!(
!new_bytes.contains("[[old-name]]"),
"old slug must not survive in the rewritten file, got:\n{new_bytes}"
);
let in_mem = engine.get_entity(&outcome.new_id).unwrap();
assert!(
in_mem
.sections
.get("purpose")
.map(|s| s.contains("[[brand-new-name]]"))
.unwrap_or(false),
"section body must be rewritten in-memory; got {:?}",
in_mem.sections.get("purpose")
);
let new_self_target = EntityId::new("specs", "brand-new-name");
assert!(
in_mem
.relationships
.iter()
.all(|r| r.target != seeded.id && r.target != new_self_target),
"a self-referential body link must produce no self-relation (F11), got: {:?}",
in_mem.relationships
);
}
#[test]
fn rename_preserves_referrer_last_modified_but_rewrites_link() {
let tmp = TempDir::new().unwrap();
let mem_dir = tmp.path().to_path_buf();
std::fs::write(
mem_dir.join("target.md"),
"---\ntype: spec\ncreated_date: 2020-01-01\nlast_modified: 2020-01-01\nlevel: M0\n---\n# Target\n\n## Identity\n\nT\n\n## Purpose\n\nP\n",
)
.unwrap();
std::fs::write(
mem_dir.join("referrer.md"),
"---\ntype: spec\ncreated_date: 2020-01-01\nlast_modified: 2020-01-01\nlevel: M0\n---\n# Referrer\n\n## Identity\n\nR\n\n## Purpose\n\nDepends on [[target]] for context.\n\n## Relationships\n\n- **REFERENCES**: [[target]]\n",
)
.unwrap();
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();
let (actor, client) = cli_actor();
let target_id = crate::entity::EntityId::new("specs", "target");
let target_hash = engine
.store()
.get(&target_id)
.expect("target loaded from disk")
.content_hash
.clone();
engine
.rename_entity(
RenameEntityArgs {
id: target_id,
expected_hash: Some(target_hash),
new_title: "Target Renamed".to_string(),
},
actor,
Some(&client),
None,
)
.expect("rename succeeds");
let referrer_md = std::fs::read_to_string(mem_dir.join("referrer.md")).unwrap();
assert!(
referrer_md.contains("last_modified: 2020-01-01"),
"referrer's last_modified must be preserved across a rename-driven slug rewrite; got:\n{referrer_md}"
);
assert!(
referrer_md.contains("[[target-renamed]]"),
"referrer's body wiki-link must be rewritten to the new slug; got:\n{referrer_md}"
);
assert!(
!referrer_md.contains("[[target]]"),
"old slug must not survive in the referrer body; got:\n{referrer_md}"
);
}
#[test]
fn rename_rewrites_prose_only_referrer_without_relationships_row() {
let tmp = TempDir::new().unwrap();
let mem_dir = tmp.path().to_path_buf();
std::fs::write(
mem_dir.join("target.md"),
"---\ntype: spec\ncreated_date: 2020-01-01\nlast_modified: 2020-01-01\nlevel: M0\n---\n# Target\n\n## Identity\n\nT\n\n## Purpose\n\nP\n",
)
.unwrap();
std::fs::write(
mem_dir.join("referrer.md"),
"---\ntype: spec\ncreated_date: 2020-01-01\nlast_modified: 2020-01-01\nlevel: M0\n---\n# Referrer\n\n## Identity\n\nR\n\n## Purpose\n\nDepends on [[target]] for context.\n",
)
.unwrap();
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();
let (actor, client) = cli_actor();
let target_id = crate::entity::EntityId::new("specs", "target");
let target_hash = engine
.store()
.get(&target_id)
.expect("target loaded from disk")
.content_hash
.clone();
engine
.rename_entity(
RenameEntityArgs {
id: target_id,
expected_hash: Some(target_hash),
new_title: "Target Renamed".to_string(),
},
actor,
Some(&client),
None,
)
.expect("rename succeeds");
let referrer_md = std::fs::read_to_string(mem_dir.join("referrer.md")).unwrap();
assert!(
referrer_md.contains("[[target-renamed]]"),
"the prose-only wiki-link must be rewritten to the new slug; got:\n{referrer_md}"
);
assert!(
!referrer_md.contains("[[target]]"),
"the old slug must not survive in the prose-only referrer; got:\n{referrer_md}"
);
}
#[test]
fn rename_entity_rewrites_same_mem_referrers_atomically() {
use crate::engine::CreateEntityArgs;
use crate::entity::EntityId;
use indexmap::IndexMap;
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();
let (actor, client) = cli_actor();
let target = engine
.create_entity(
empty_create_args("specs", "Target Spec"),
actor,
Some(&client),
None,
)
.unwrap();
assert_eq!(target.id.to_string(), "specs--target-spec");
let mut sections_a: IndexMap<String, String> = IndexMap::new();
sections_a.insert(
"identity".to_string(),
"referrer alpha identity".to_string(),
);
sections_a.insert(
"purpose".to_string(),
"rationale relies on [[target-spec]] for context".to_string(),
);
let referrer_a = engine
.create_entity(
CreateEntityArgs {
anchors: Vec::new(),
mem: "specs".to_string(),
title: "Referrer Alpha".to_string(),
entity_type: "spec".to_string(),
sections: sections_a,
metadata: IndexMap::new(),
relations: Vec::new(),
dry_run: false,
},
actor,
Some(&client),
None,
)
.unwrap();
let mut sections_b: IndexMap<String, String> = IndexMap::new();
sections_b.insert("identity".to_string(), "referrer beta identity".to_string());
sections_b.insert(
"purpose".to_string(),
"consult [[target-spec]] for the canonical phrasing".to_string(),
);
let referrer_b = engine
.create_entity(
CreateEntityArgs {
anchors: Vec::new(),
mem: "specs".to_string(),
title: "Referrer Bravo".to_string(),
entity_type: "spec".to_string(),
sections: sections_b,
metadata: IndexMap::new(),
relations: Vec::new(),
dry_run: false,
},
actor,
Some(&client),
None,
)
.unwrap();
let bystander = engine
.create_entity(
empty_create_args("specs", "Bystander"),
actor,
Some(&client),
None,
)
.unwrap();
let bystander_bytes_before =
std::fs::read_to_string(mem_dir.join(&bystander.file_path)).unwrap();
let renamed = engine
.rename_entity(
RenameEntityArgs {
id: target.id.clone(),
expected_hash: Some(target.content_hash.clone()),
new_title: "Renamed Spec".to_string(),
},
actor,
Some(&client),
None,
)
.unwrap();
assert_eq!(renamed.new_id.to_string(), "specs--renamed-spec");
for path in std::fs::read_dir(&mem_dir).unwrap().flatten() {
let p = path.path();
if p.extension().and_then(|s| s.to_str()) != Some("md") {
continue;
}
let body = std::fs::read_to_string(&p).unwrap();
assert!(
!body.contains("[[target-spec]]"),
"old slug must not survive in {}, got:\n{body}",
p.display()
);
}
let in_mem_a = engine.get_entity(&referrer_a.id).unwrap();
assert!(
in_mem_a
.relationships
.iter()
.any(|r| r.rel_type == "REFERENCES"
&& r.target == EntityId::new("specs", "renamed-spec")),
"expected referrer A's relation to point at renamed-spec, got {:?}",
in_mem_a.relationships
);
assert!(
in_mem_a
.sections
.get("purpose")
.map(|s| s.contains("[[renamed-spec]]"))
.unwrap_or(false),
"referrer A's body must be rewritten"
);
let in_mem_b = engine.get_entity(&referrer_b.id).unwrap();
assert!(
in_mem_b
.sections
.get("purpose")
.map(|s| s.contains("[[renamed-spec]]"))
.unwrap_or(false),
"referrer B's body must be rewritten"
);
let bystander_bytes_after =
std::fs::read_to_string(mem_dir.join(&bystander.file_path)).unwrap();
assert_eq!(
bystander_bytes_before, bystander_bytes_after,
"bystander must not be rewritten"
);
}
fn engine_with_two_mems_and_bidirectional_policy(specs_dir: PathBuf, memos_dir: PathBuf) -> Engine {
engine_with_two_named_mems_and_bidirectional_policy("specs", specs_dir, "memos", memos_dir)
}
fn engine_with_two_named_mems_and_bidirectional_policy(
first: &str,
first_dir: PathBuf,
second: &str,
second_dir: PathBuf,
) -> Engine {
use memstead_schema::workspace_config::CrossLinkValue;
let writer_first = FilesystemBackend::new(first_dir.clone());
let writer_second = FilesystemBackend::new(second_dir.clone());
let mut engine = Engine::from_mounts(vec![
(
folder_mount(first, first_dir),
Box::new(writer_first) as Box<dyn MemBackend>,
),
(
folder_mount(second, second_dir),
Box::new(writer_second) as Box<dyn MemBackend>,
),
])
.unwrap();
let mut settings = crate::workspace::WorkspaceSettings::default();
settings.cross_mem_links.insert(
second.to_string(),
CrossLinkValue::List(vec![first.to_string()]),
);
settings.cross_mem_links.insert(
first.to_string(),
CrossLinkValue::List(vec![second.to_string()]),
);
engine.set_settings(settings);
engine
}
#[test]
fn rename_entity_rewrites_cross_mem_referrer_in_hierarchical_mems() {
use crate::engine::CreateEntityArgs;
use crate::entity::EntityId;
use indexmap::IndexMap;
let tmp_a = TempDir::new().unwrap();
let tmp_b = TempDir::new().unwrap();
let a_dir = tmp_a.path().to_path_buf();
let b_dir = tmp_b.path().to_path_buf();
let mut engine = engine_with_two_named_mems_and_bidirectional_policy(
"library/a",
a_dir.clone(),
"library/b",
b_dir.clone(),
);
let (actor, client) = cli_actor();
let target = engine
.create_entity(
empty_create_args("library/a", "Target Spec"),
actor,
Some(&client),
None,
)
.unwrap();
assert_eq!(target.id.to_string(), "library/a--target-spec");
let mut sections: IndexMap<String, String> = IndexMap::new();
sections.insert("claim".to_string(), "the claim".to_string());
sections.insert(
"context".to_string(),
"discussion stems from [[library/a:target-spec]]".to_string(),
);
let referrer = engine
.create_entity(
CreateEntityArgs {
anchors: Vec::new(),
mem: "library/b".to_string(),
title: "Cross Note".to_string(),
entity_type: "memo".to_string(),
sections,
metadata: IndexMap::new(),
relations: Vec::new(),
dry_run: false,
},
actor,
Some(&client),
None,
)
.unwrap();
let renamed = engine
.rename_entity(
RenameEntityArgs {
id: target.id.clone(),
expected_hash: Some(target.content_hash.clone()),
new_title: "Renamed Spec".to_string(),
},
actor,
Some(&client),
None,
)
.unwrap();
assert_eq!(renamed.new_id.to_string(), "library/a--renamed-spec");
let referrer_bytes = std::fs::read_to_string(b_dir.join(&referrer.file_path)).unwrap();
assert!(
referrer_bytes.contains("[[library/a:renamed-spec]]"),
"expected colon-form rewrite in referrer body, got:\n{referrer_bytes}"
);
assert!(
!referrer_bytes.contains("target-spec"),
"old slug must not survive in referrer file, got:\n{referrer_bytes}"
);
let in_mem = engine.get_entity(&referrer.id).unwrap();
assert!(
in_mem
.relationships
.iter()
.any(|r| r.rel_type == "REFERENCES"
&& r.target == EntityId::new("library/a", "renamed-spec")),
"expected cross-mem relation to point at renamed-spec, got {:?}",
in_mem.relationships
);
}
#[test]
fn rename_entity_rewrites_cross_mem_write_referrer() {
use crate::engine::CreateEntityArgs;
use crate::entity::EntityId;
use indexmap::IndexMap;
let tmp_specs = TempDir::new().unwrap();
let tmp_memos = TempDir::new().unwrap();
let specs_dir = tmp_specs.path().to_path_buf();
let memos_dir = tmp_memos.path().to_path_buf();
let mut engine =
engine_with_two_mems_and_bidirectional_policy(specs_dir.clone(), memos_dir.clone());
let (actor, client) = cli_actor();
let target = engine
.create_entity(
empty_create_args("specs", "Target Spec"),
actor,
Some(&client),
None,
)
.unwrap();
let mut sections: IndexMap<String, String> = IndexMap::new();
sections.insert("claim".to_string(), "the claim".to_string());
sections.insert(
"context".to_string(),
"discussion stems from [[specs:target-spec]]".to_string(),
);
let referrer = engine
.create_entity(
CreateEntityArgs {
anchors: Vec::new(),
mem: "memos".to_string(),
title: "Cross Note".to_string(),
entity_type: "memo".to_string(),
sections,
metadata: IndexMap::new(),
relations: Vec::new(),
dry_run: false,
},
actor,
Some(&client),
None,
)
.unwrap();
let renamed = engine
.rename_entity(
RenameEntityArgs {
id: target.id.clone(),
expected_hash: Some(target.content_hash.clone()),
new_title: "Renamed Spec".to_string(),
},
actor,
Some(&client),
None,
)
.unwrap();
assert_eq!(renamed.new_id.to_string(), "specs--renamed-spec");
let referrer_path = memos_dir.join(&referrer.file_path);
let referrer_bytes = std::fs::read_to_string(&referrer_path).unwrap();
assert!(
referrer_bytes.contains("[[specs:renamed-spec]]"),
"expected colon-form rewrite in referrer body, got:\n{referrer_bytes}"
);
assert!(
!referrer_bytes.contains("target-spec"),
"old slug must not survive in referrer file, got:\n{referrer_bytes}"
);
let in_mem = engine.get_entity(&referrer.id).unwrap();
assert!(
in_mem
.relationships
.iter()
.any(|r| r.rel_type == "REFERENCES"
&& r.target == EntityId::new("specs", "renamed-spec")),
"expected cross-mem relation to point at renamed-spec, got {:?}",
in_mem.relationships
);
assert!(
in_mem.relationships.iter().all(|r| r.target != target.id),
"no relationship may still target the old id, got: {:?}",
in_mem.relationships
);
}
struct DriftingBackend {
inner: Box<dyn MemBackend>,
}
impl DriftingBackend {
fn new(inner: Box<dyn MemBackend>) -> Self {
Self { inner }
}
}
impl crate::backend::MemBackend for DriftingBackend {
fn list_entities(&self) -> Result<Vec<PathBuf>, crate::backend::BackendError> {
self.inner.list_entities()
}
fn read_entity(
&self,
rel: &std::path::Path,
) -> Result<Option<Vec<u8>>, crate::backend::BackendError> {
self.inner.read_entity(rel)
}
fn write_entity(
&self,
rel: &std::path::Path,
b: &[u8],
) -> Result<(), crate::backend::BackendError> {
self.inner.write_entity(rel, b)
}
fn delete_entity(&self, rel: &std::path::Path) -> Result<(), crate::backend::BackendError> {
self.inner.delete_entity(rel)
}
fn move_entity(
&self,
f: &std::path::Path,
t: &std::path::Path,
) -> Result<(), crate::backend::BackendError> {
self.inner.move_entity(f, t)
}
fn commit(
&self,
m: &str,
c: &crate::vcs::CommitContext<'_>,
) -> Result<crate::storage::CommitId, crate::backend::BackendError> {
self.inner.commit(m, c)
}
fn commit_with_expected_parent(
&self,
m: &str,
c: &crate::vcs::CommitContext<'_>,
expected_parent: Option<&str>,
) -> Result<crate::storage::CommitId, crate::backend::BackendError> {
if let Some(expected) = expected_parent {
Err(crate::backend::BackendError::ParentMismatch {
expected: expected.to_string(),
actual: "drifted-by-sibling-writer".to_string(),
})
} else {
self.inner.commit(m, c)
}
}
fn append_provenance(&self, r: &crate::Provenance) -> Result<(), crate::backend::BackendError> {
self.inner.append_provenance(r)
}
fn read_provenance(
&self,
c: Option<&str>,
) -> Result<Vec<crate::Provenance>, crate::backend::BackendError> {
self.inner.read_provenance(c)
}
fn current_head(&self) -> Result<Option<String>, crate::backend::BackendError> {
Ok(Some("snapshot-head-sha".to_string()))
}
}
#[test]
fn rename_entity_surfaces_partial_failure_when_peer_mem_drifts() {
use crate::engine::CreateEntityArgs;
use indexmap::IndexMap;
use memstead_schema::workspace_config::CrossLinkValue;
let tmp_specs = TempDir::new().unwrap();
let tmp_memos = TempDir::new().unwrap();
let specs_dir = tmp_specs.path().to_path_buf();
let memos_dir = tmp_memos.path().to_path_buf();
let writer_specs = FilesystemBackend::new(specs_dir.clone());
let writer_memos_inner: Box<dyn MemBackend> =
Box::new(FilesystemBackend::new(memos_dir.clone()));
let writer_memos = DriftingBackend::new(writer_memos_inner);
let mut engine = Engine::from_mounts(vec![
(
folder_mount("specs", specs_dir.clone()),
Box::new(writer_specs) as Box<dyn MemBackend>,
),
(
folder_mount("memos", memos_dir.clone()),
Box::new(writer_memos) as Box<dyn MemBackend>,
),
])
.unwrap();
let mut settings = crate::workspace::WorkspaceSettings::default();
settings.cross_mem_links.insert(
"memos".to_string(),
CrossLinkValue::List(vec!["specs".to_string()]),
);
settings.cross_mem_links.insert(
"specs".to_string(),
CrossLinkValue::List(vec!["memos".to_string()]),
);
engine.set_settings(settings);
let (actor, client) = cli_actor();
let target = engine
.create_entity(
empty_create_args("specs", "Target Spec"),
actor,
Some(&client),
None,
)
.unwrap();
let mut sections: IndexMap<String, String> = IndexMap::new();
sections.insert("claim".to_string(), "the claim".to_string());
sections.insert(
"context".to_string(),
"see [[specs:target-spec]]".to_string(),
);
let _referrer = engine
.create_entity(
CreateEntityArgs {
anchors: Vec::new(),
mem: "memos".to_string(),
title: "Cross Note".to_string(),
entity_type: "memo".to_string(),
sections,
metadata: IndexMap::new(),
relations: Vec::new(),
dry_run: false,
},
actor,
Some(&client),
None,
)
.unwrap();
let err = engine
.rename_entity(
RenameEntityArgs {
id: target.id.clone(),
expected_hash: Some(target.content_hash.clone()),
new_title: "Renamed Spec".to_string(),
},
actor,
Some(&client),
None,
)
.unwrap_err();
match err {
EngineError::RenamePartialFailure {
committed_mems,
failed_mem,
failure_cause,
} => {
assert_eq!(committed_mems, vec!["specs".to_string()]);
assert_eq!(failed_mem, "memos");
assert_eq!(failure_cause, "drift");
}
other => panic!("expected RenamePartialFailure, got {other:?}"),
}
assert!(specs_dir.join("renamed-spec.md").exists());
assert!(!specs_dir.join(&target.file_path).exists());
}
#[test]
fn rename_entity_tags_every_per_mem_commit_with_same_logical_operation_id() {
use crate::backend::MemBackend;
use crate::engine::CreateEntityArgs;
use indexmap::IndexMap;
let tmp_specs = TempDir::new().unwrap();
let tmp_memos = TempDir::new().unwrap();
let specs_dir = tmp_specs.path().to_path_buf();
let memos_dir = tmp_memos.path().to_path_buf();
let mut engine =
engine_with_two_mems_and_bidirectional_policy(specs_dir.clone(), memos_dir.clone());
let (actor, client) = cli_actor();
let target = engine
.create_entity(
empty_create_args("specs", "Target Spec"),
actor,
Some(&client),
None,
)
.unwrap();
let mut sections: IndexMap<String, String> = IndexMap::new();
sections.insert("claim".to_string(), "the claim".to_string());
sections.insert(
"context".to_string(),
"discussion stems from [[specs:target-spec]]".to_string(),
);
let _referrer = engine
.create_entity(
CreateEntityArgs {
anchors: Vec::new(),
mem: "memos".to_string(),
title: "Cross Note".to_string(),
entity_type: "memo".to_string(),
sections,
metadata: IndexMap::new(),
relations: Vec::new(),
dry_run: false,
},
actor,
Some(&client),
None,
)
.unwrap();
let _ = engine
.rename_entity(
RenameEntityArgs {
id: target.id.clone(),
expected_hash: Some(target.content_hash.clone()),
new_title: "Renamed Spec".to_string(),
},
actor,
Some(&client),
None,
)
.unwrap();
let specs_backend: Box<dyn MemBackend> = Box::new(FilesystemBackend::new(specs_dir.clone()));
let memos_backend: Box<dyn MemBackend> = Box::new(FilesystemBackend::new(memos_dir.clone()));
let specs_provenance = specs_backend.read_provenance(None).unwrap();
let memos_provenance = memos_backend.read_provenance(None).unwrap();
let specs_rename = specs_provenance
.iter()
.find(|p| matches!(p.kind, crate::provenance::ProvenanceKind::Rename))
.expect("specs mem must have a rename provenance entry");
let memos_rename = memos_provenance
.iter()
.find(|p| matches!(p.kind, crate::provenance::ProvenanceKind::Rename))
.expect("memos mem must have a rename provenance entry");
let specs_id = specs_rename
.logical_operation_id
.as_deref()
.expect("specs rename entry must carry a logical_operation_id");
let memos_id = memos_rename
.logical_operation_id
.as_deref()
.expect("memos rename entry must carry a logical_operation_id");
assert_eq!(
specs_id, memos_id,
"both per-mem rename commits must share the same logical_operation_id"
);
assert!(
specs_id.starts_with("logop-"),
"logical_operation_id must use the `logop-` prefix the engine mints; got {specs_id}"
);
}
#[test]
fn rename_entity_refuses_when_cross_mem_referrer_blocked_by_policy() {
use crate::engine::CreateEntityArgs;
use indexmap::IndexMap;
use memstead_schema::workspace_config::CrossLinkValue;
let tmp_specs = TempDir::new().unwrap();
let tmp_memos = TempDir::new().unwrap();
let specs_dir = tmp_specs.path().to_path_buf();
let memos_dir = tmp_memos.path().to_path_buf();
let mut engine =
engine_with_two_mems_and_bidirectional_policy(specs_dir.clone(), memos_dir.clone());
let (actor, client) = cli_actor();
let target = engine
.create_entity(
empty_create_args("specs", "Target Spec"),
actor,
Some(&client),
None,
)
.unwrap();
let mut sections: IndexMap<String, String> = IndexMap::new();
sections.insert("claim".to_string(), "the claim".to_string());
sections.insert(
"context".to_string(),
"see [[specs:target-spec]]".to_string(),
);
let _referrer = engine
.create_entity(
CreateEntityArgs {
anchors: Vec::new(),
mem: "memos".to_string(),
title: "Cross Note".to_string(),
entity_type: "memo".to_string(),
sections,
metadata: IndexMap::new(),
relations: Vec::new(),
dry_run: false,
},
actor,
Some(&client),
None,
)
.unwrap();
let mut settings = crate::workspace::WorkspaceSettings::default();
settings.cross_mem_links.insert(
"specs".to_string(),
CrossLinkValue::List(vec!["memos".to_string()]),
);
engine.set_settings(settings);
let err = engine
.rename_entity(
RenameEntityArgs {
id: target.id.clone(),
expected_hash: Some(target.content_hash.clone()),
new_title: "Renamed Spec".to_string(),
},
actor,
Some(&client),
None,
)
.unwrap_err();
match err {
EngineError::RenameBlockedByCrossMemPolicy {
from_mem,
blocked_referrers,
} => {
assert_eq!(from_mem, "specs");
assert_eq!(blocked_referrers.len(), 1);
assert_eq!(blocked_referrers[0].from_mem, "memos");
assert_eq!(blocked_referrers[0].to_mem, "specs");
assert_eq!(blocked_referrers[0].count, 1);
}
other => panic!("expected RenameBlockedByCrossMemPolicy, got {other:?}"),
}
assert!(specs_dir.join(&target.file_path).exists());
assert!(!specs_dir.join("renamed-spec.md").exists());
}
#[test]
fn rename_entity_demotes_to_stub_when_only_readonly_cross_mem_referrers_remain() {
use crate::engine::test_helpers::{archive_mount, build_archive};
use crate::workspace::{Mount, MountCapability, MountLifecycle, MountStorage};
let tmp = TempDir::new().unwrap();
let writable_dir = tmp.path().join("writable");
std::fs::create_dir_all(&writable_dir).unwrap();
let writer = FilesystemBackend::new(writable_dir.clone());
let archive_md = "---\ntype: spec\ncreated_date: 2026-01-15\nlast_modified: 2026-04-12\nlevel: M0\n---\n# Archived Source\n\n## Identity\n\nLinks to [[specs:target]].\n\n## Purpose\n\nFixture for rename residual-stub demotion.\n\n## Relationships\n\n- **REFERENCES**: [[specs:target]]\n";
let archive_path = build_archive(
tmp.path(),
"archive",
&[("archived-source.md", archive_md.as_bytes())],
);
let folder_mount = Mount {
mem: "specs".to_string(),
schema: Some(crate::engine::test_helpers::pin("default")),
storage: MountStorage::Folder {
path: writable_dir.clone(),
},
capability: MountCapability::Write,
lifecycle: MountLifecycle::Eager,
cross_linkable: true,
migration_target: None,
};
let archive_reader = crate::storage::ArchiveBackend::new(archive_path.clone());
let mut engine = Engine::from_mounts(vec![
(folder_mount, Box::new(writer) as Box<dyn MemBackend>),
(
archive_mount("archive", archive_path.clone()),
Box::new(archive_reader) as Box<dyn MemBackend>,
),
])
.unwrap();
let (actor, client) = cli_actor();
let target = engine
.create_entity(
empty_create_args("specs", "Target"),
actor,
Some(&client),
None,
)
.unwrap();
let archived_source_id = crate::EntityId::new("archive", "archived-source");
let incoming_pre: Vec<_> = engine
.store()
.incoming(&target.id)
.iter()
.map(|e| e.from.clone())
.collect();
assert!(
incoming_pre.contains(&archived_source_id),
"archive wiki-link must produce an incoming edge on target; got {incoming_pre:?}"
);
let outcome = engine
.rename_entity(
RenameEntityArgs {
id: target.id.clone(),
expected_hash: Some(target.content_hash.clone()),
new_title: "Renamed Target".to_string(),
},
actor,
Some(&client),
None,
)
.unwrap();
assert_eq!(outcome.new_id.to_string(), "specs--renamed-target");
assert!(writable_dir.join(&outcome.new_path).exists());
let demoted = engine
.get_entity(&target.id)
.expect("residual stub must remain at old id");
assert!(demoted.stub, "demoted entity must be flagged as stub");
assert!(demoted.entity_type.is_empty());
let incoming_old: Vec<_> = engine
.store()
.incoming(&target.id)
.iter()
.map(|e| e.from.clone())
.collect();
assert!(
incoming_old.contains(&archived_source_id),
"archive incoming edge must survive demotion at old id; got {incoming_old:?}"
);
let incoming_new: Vec<_> = engine
.store()
.incoming(&outcome.new_id)
.iter()
.map(|e| e.from.clone())
.collect();
assert!(
!incoming_new.contains(&archived_source_id),
"archive must not be wired to new id (markdown still references old slug); got {incoming_new:?}"
);
let referrers = outcome
.warnings
.iter()
.find_map(|w| match w {
WarningHint::ResidualStubForReadOnlyReferrers {
id: warn_id,
referrers,
} => {
assert_eq!(warn_id, &target.id);
Some(referrers.clone())
}
_ => None,
})
.expect("ResidualStubForReadOnlyReferrers warning must surface");
assert_eq!(referrers, vec![archived_source_id]);
}
#[test]
fn rename_entity_rewrites_full_id_form_body_link_on_same_mem_referrer() {
use crate::engine::CreateEntityArgs;
use indexmap::IndexMap;
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();
let (actor, client) = cli_actor();
let target = engine
.create_entity(
empty_create_args("specs", "Target Spec"),
actor,
Some(&client),
None,
)
.unwrap();
assert_eq!(target.id.to_string(), "specs--target-spec");
let mut sections: IndexMap<String, String> = IndexMap::new();
sections.insert("identity".to_string(), "referrer identity".to_string());
sections.insert(
"purpose".to_string(),
"see also [[specs--target-spec]] for context".to_string(),
);
let referrer = engine
.create_entity(
CreateEntityArgs {
anchors: Vec::new(),
mem: "specs".to_string(),
title: "Referrer Full".to_string(),
entity_type: "spec".to_string(),
sections,
metadata: IndexMap::new(),
relations: Vec::new(),
dry_run: false,
},
actor,
Some(&client),
None,
)
.unwrap();
let renamed = engine
.rename_entity(
RenameEntityArgs {
id: target.id.clone(),
expected_hash: Some(target.content_hash.clone()),
new_title: "Renamed Spec".to_string(),
},
actor,
Some(&client),
None,
)
.unwrap();
assert_eq!(renamed.new_id.to_string(), "specs--renamed-spec");
let referrer_path = mem_dir.join(&referrer.file_path);
let body = std::fs::read_to_string(&referrer_path).unwrap();
assert!(
body.contains("[[specs--renamed-spec]]"),
"expected full-id form retargeted to new slug, got:\n{body}"
);
assert!(
!body.contains("target-spec"),
"old slug must not survive in any form, got:\n{body}"
);
}
#[test]
fn rename_entity_rejects_collision_with_existing_id() {
let tmp = TempDir::new().unwrap();
let (mut engine, first) = engine_with_seed(&tmp, "First");
let (actor, client) = cli_actor();
let _ = engine
.create_entity(
empty_create_args("specs", "Second"),
actor,
Some(&client),
None,
)
.unwrap();
let err = engine
.rename_entity(
RenameEntityArgs {
id: first.id.clone(),
expected_hash: Some(first.content_hash.clone()),
new_title: "Second".to_string(),
},
actor,
Some(&client),
None,
)
.unwrap_err();
assert!(matches!(
err,
EngineError::AlreadyExists { ref id, ref existing_title, existing_is_stub: false }
if id == "specs--second" && existing_title == "Second"
));
}
#[test]
fn rename_propagation_gate_checks_actual_edge_direction() {
use crate::engine::error::BlockedReferrer;
use crate::engine::{CreateEntityArgs, RelateEntityArgs};
use indexmap::IndexMap;
use memstead_schema::workspace_config::CrossLinkValue;
let tmp_test = TempDir::new().unwrap();
let tmp_other = TempDir::new().unwrap();
let test_dir = tmp_test.path().to_path_buf();
let other_dir = tmp_other.path().to_path_buf();
let writer_test = FilesystemBackend::new(test_dir.clone());
let writer_other = FilesystemBackend::new(other_dir.clone());
let mut engine = Engine::from_mounts(vec![
(
folder_mount("test", test_dir.clone()),
Box::new(writer_test) as Box<dyn MemBackend>,
),
(
folder_mount("other", other_dir.clone()),
Box::new(writer_other) as Box<dyn MemBackend>,
),
])
.unwrap();
let (actor, client) = cli_actor();
let mut settings = crate::workspace::WorkspaceSettings::default();
settings.cross_mem_links.insert(
"test".to_string(),
CrossLinkValue::List(vec!["other".to_string()]),
);
engine.set_settings(settings);
let target = engine
.create_entity(
empty_create_args("other", "Target"),
actor,
Some(&client),
None,
)
.unwrap();
let mut src_sections: IndexMap<String, String> = IndexMap::new();
src_sections.insert("identity".to_string(), "source identity".to_string());
src_sections.insert("purpose".to_string(), "source purpose".to_string());
let src = engine
.create_entity(
CreateEntityArgs {
anchors: Vec::new(),
mem: "test".to_string(),
title: "Src".to_string(),
entity_type: "spec".to_string(),
sections: src_sections,
metadata: IndexMap::new(),
relations: Vec::new(),
dry_run: false,
},
actor,
Some(&client),
None,
)
.unwrap();
let src = engine
.relate_entity(
RelateEntityArgs {
source: src.id.clone(),
rel_type: "IMPLEMENTS".to_string(),
target: target.id.clone(),
expected_hash: Some(src.content_hash.clone()),
remove: false,
description: None,
dry_run: false,
},
actor,
Some(&client),
None,
)
.unwrap();
let _ = src;
engine.set_settings(crate::workspace::WorkspaceSettings::default());
let err = engine
.rename_entity(
RenameEntityArgs {
id: target.id.clone(),
expected_hash: Some(target.content_hash.clone()),
new_title: "Renamed".to_string(),
},
actor,
Some(&client),
None,
)
.unwrap_err();
match err {
EngineError::RenameBlockedByCrossMemPolicy {
from_mem,
blocked_referrers,
} => {
assert_eq!(from_mem, "other");
assert_eq!(
blocked_referrers,
vec![BlockedReferrer {
from_mem: "test".to_string(),
to_mem: "other".to_string(),
count: 1,
}],
"blocked_referrers must name the actual edge direction (test → other)",
);
}
other => panic!("expected RenameBlockedByCrossMemPolicy, got {other:?}"),
}
assert!(other_dir.join(&target.file_path).exists());
assert!(!other_dir.join("renamed.md").exists());
}
#[test]
fn rename_propagation_succeeds_when_actual_edge_direction_granted() {
use crate::engine::{CreateEntityArgs, RelateEntityArgs};
use indexmap::IndexMap;
use memstead_schema::workspace_config::CrossLinkValue;
let tmp_test = TempDir::new().unwrap();
let tmp_other = TempDir::new().unwrap();
let test_dir = tmp_test.path().to_path_buf();
let other_dir = tmp_other.path().to_path_buf();
let writer_test = FilesystemBackend::new(test_dir.clone());
let writer_other = FilesystemBackend::new(other_dir.clone());
let mut engine = Engine::from_mounts(vec![
(
folder_mount("test", test_dir),
Box::new(writer_test) as Box<dyn MemBackend>,
),
(
folder_mount("other", other_dir),
Box::new(writer_other) as Box<dyn MemBackend>,
),
])
.unwrap();
let (actor, client) = cli_actor();
let mut settings = crate::workspace::WorkspaceSettings::default();
settings.cross_mem_links.insert(
"test".to_string(),
CrossLinkValue::List(vec!["other".to_string()]),
);
engine.set_settings(settings);
let target = engine
.create_entity(
empty_create_args("other", "Target"),
actor,
Some(&client),
None,
)
.unwrap();
let mut src_sections: IndexMap<String, String> = IndexMap::new();
src_sections.insert("identity".to_string(), "source identity".to_string());
src_sections.insert("purpose".to_string(), "source purpose".to_string());
let src = engine
.create_entity(
CreateEntityArgs {
anchors: Vec::new(),
mem: "test".to_string(),
title: "Src".to_string(),
entity_type: "spec".to_string(),
sections: src_sections,
metadata: IndexMap::new(),
relations: Vec::new(),
dry_run: false,
},
actor,
Some(&client),
None,
)
.unwrap();
let _ = engine
.relate_entity(
RelateEntityArgs {
source: src.id.clone(),
rel_type: "IMPLEMENTS".to_string(),
target: target.id.clone(),
expected_hash: Some(src.content_hash.clone()),
remove: false,
description: None,
dry_run: false,
},
actor,
Some(&client),
None,
)
.unwrap();
let outcome = engine
.rename_entity(
RenameEntityArgs {
id: target.id.clone(),
expected_hash: Some(target.content_hash.clone()),
new_title: "Renamed".to_string(),
},
actor,
Some(&client),
None,
)
.unwrap();
assert_ne!(outcome.old_id, outcome.new_id);
let renamed = engine
.get_entity(&outcome.new_id)
.expect("renamed entity persists");
assert_eq!(renamed.title, "Renamed");
let updated_src = engine.get_entity(&src.id).expect("source persists");
assert!(
updated_src
.relationships
.iter()
.any(|r| r.rel_type == "IMPLEMENTS" && r.target == outcome.new_id),
"referrer's IMPLEMENTS edge must point at the new id after rewrite"
);
}
#[test]
fn rename_with_no_cross_mem_referrers_succeeds_regardless_of_policy() {
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"),
actor,
Some(&client),
None,
)
.unwrap();
let outcome = engine
.rename_entity(
RenameEntityArgs {
id: target.id.clone(),
expected_hash: Some(target.content_hash.clone()),
new_title: "Renamed Target".to_string(),
},
actor,
Some(&client),
None,
)
.unwrap();
assert_ne!(outcome.old_id, outcome.new_id);
}
#[test]
fn rename_carries_check_records_to_the_new_id() {
use crate::check::{CheckKind, CheckLedger, CheckState, Verdict, derive_state};
let tmp = TempDir::new().unwrap();
let mem_dir = tmp.path().join("specs");
std::fs::create_dir_all(&mem_dir).unwrap();
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(tmp.path().to_path_buf());
let (actor, client) = cli_actor();
let seeded = engine
.create_entity(
empty_create_args("specs", "Checked Before Rename"),
actor,
Some(&client),
None,
)
.unwrap();
let old_id = seeded.id.clone();
engine.set_identity(Some("checker".into()));
engine
.record_check(
"specs",
old_id.as_ref(),
Verdict::Ok,
CheckKind::Verification,
Some("read it"),
actor,
Some(&client),
)
.unwrap();
engine.set_identity(None);
let outcome = engine
.rename_entity(
RenameEntityArgs {
id: old_id.clone(),
expected_hash: Some(seeded.content_hash.clone()),
new_title: "Checked After Rename".to_string(),
},
actor,
Some(&client),
None,
)
.unwrap();
let ledger = CheckLedger::for_workspace(tmp.path());
let carried = ledger
.latest_for_kind(outcome.new_id.as_ref(), CheckKind::Verification)
.expect("the check followed the rename");
assert_eq!(carried.renamed_from.as_deref(), Some(old_id.as_ref()));
assert_eq!(carried.verdict, "ok");
assert_eq!(carried.identity.as_deref(), Some("checker"));
assert_eq!(carried.method.as_deref(), Some("read it"));
assert_eq!(carried.entity_hash, seeded.content_hash);
let original = ledger.latest_for(old_id.as_ref()).unwrap();
assert!(original.renamed_from.is_none());
let renamed = engine.get_entity(&outcome.new_id).unwrap();
assert_ne!(renamed.content_hash, seeded.content_hash);
assert_eq!(
derive_state(Some(&carried), &renamed.content_hash),
CheckState::CheckStale
);
let state = engine
.entity_check_state("specs", outcome.new_id.as_ref())
.unwrap();
assert_eq!(state.0, CheckState::CheckStale);
}