use super::*;
#[test]
fn update_of_crlf_file_preserves_foreign_keys() {
let (dir, root) = tmp_root();
let store = MemoryStore::new(root.clone(), "MEMORY.md");
std::fs::create_dir_all(&root).unwrap();
std::fs::write(
root.join("crlf.md"),
"---\r\nname: crlf\ndescription: old\r\ntags: [a, b]\r\nmetadata:\r\n type: feedback\r\n---\r\n\r\nbody\r\n",
)
.unwrap();
store
.write_memory("crlf", "new desc", "feedback", "new body", None)
.unwrap();
let raw = std::fs::read_to_string(root.join("crlf.md")).unwrap();
assert!(
raw.contains("tags:"),
"CRLF file lost foreign keys:\n{raw:?}"
);
assert!(raw.contains("new body"), "{raw:?}");
drop(dir);
}
#[test]
fn update_of_unparseable_yaml_frontmatter_falls_back_to_known_fields() {
let (dir, root) = tmp_root();
let store = MemoryStore::new(root.clone(), "MEMORY.md");
std::fs::create_dir_all(&root).unwrap();
std::fs::write(
root.join("bad.md"),
"---\nname: [unclosed\ntags: keep-me\n---\n\nbody\n",
)
.unwrap();
store
.write_memory("bad", "d", "project", "new body", None)
.unwrap();
let raw = std::fs::read_to_string(root.join("bad.md")).unwrap();
let doc = parse_memory_str(&raw).unwrap();
assert_eq!(doc.description, "d");
assert_eq!(doc.body, "new body");
assert!(
!raw.contains("keep-me"),
"fallback contract changed (update the module doc if so): {raw}"
);
drop(dir);
}
#[test]
fn update_with_none_origin_session_id_preserves_existing_stamp() {
let (dir, root) = tmp_root();
let store = MemoryStore::new(root.clone(), "MEMORY.md");
std::fs::create_dir_all(&root).unwrap();
store
.write_memory("s", "d", "user", "b", Some("sess-1"))
.unwrap();
store.write_memory("s", "d2", "user", "b2", None).unwrap();
let raw = std::fs::read_to_string(root.join("s.md")).unwrap();
assert!(
raw.contains("originSessionId: sess-1"),
"stamp is shared provenance — set-or-keep, never clear on None:\n{raw}"
);
assert!(raw.contains("modified: "), "{raw}");
drop(dir);
}
#[test]
fn update_of_metadata_less_file_adds_metadata_keeps_unknown_keys() {
let (dir, root) = tmp_root();
let store = MemoryStore::new(root.clone(), "MEMORY.md");
std::fs::create_dir_all(&root).unwrap();
std::fs::write(
root.join("legacy.md"),
"---\nname: legacy\ndescription: old\ntags: [a, b]\n---\n\nbody\n",
)
.unwrap();
store
.write_memory("legacy", "new desc", "project", "new body", None)
.unwrap();
let raw = std::fs::read_to_string(root.join("legacy.md")).unwrap();
let doc = parse_memory_str(&raw).unwrap();
assert_eq!(doc.description, "new desc");
let md = doc.metadata.expect("metadata block must be created");
assert_eq!(md.node_type, "memory");
assert_eq!(md.memory_type, "project");
assert!(raw.contains("tags:"), "unknown key dropped:\n{raw}");
drop(dir);
}
#[test]
fn rebuild_index_preserves_curated_row_order() {
let (dir, root) = tmp_root();
std::fs::create_dir_all(&root).unwrap();
for name in ["zeta", "alpha"] {
std::fs::write(
root.join(format!("{name}.md")),
format!("---\nname: {name}\ndescription: {name} desc\nmetadata:\n node_type: memory\n type: project\n---\n\nbody\n"),
)
.unwrap();
}
std::fs::write(
root.join("MEMORY.md"),
"- [Z](zeta.md) — zeta desc\n- [A](alpha.md) — alpha desc\n",
)
.unwrap();
rebuild_index(&root, "MEMORY.md").unwrap();
let index = std::fs::read_to_string(root.join("MEMORY.md")).unwrap();
assert_eq!(
index, "- [Z](zeta.md) — zeta desc\n- [A](alpha.md) — alpha desc\n",
"rebuild must not reorder a curated index: {index}"
);
drop(dir);
}
#[test]
fn rebuild_index_does_not_refresh_stale_row_tail() {
let (dir, root) = tmp_root();
std::fs::create_dir_all(&root).unwrap();
std::fs::write(
root.join("beta.md"),
"---\nname: beta\ndescription: file says this now\nmetadata:\n node_type: memory\n type: project\n---\n\nbody\n",
)
.unwrap();
std::fs::write(
root.join("MEMORY.md"),
"- [人类标题](beta.md) — stale tail\n",
)
.unwrap();
rebuild_index(&root, "MEMORY.md").unwrap();
let index = std::fs::read_to_string(root.join("MEMORY.md")).unwrap();
assert_eq!(index, "- [人类标题](beta.md) — stale tail\n", "{index}");
drop(dir);
}
#[cfg(unix)]
#[test]
fn rebuild_index_fails_when_index_cannot_be_read() {
use std::os::unix::fs::PermissionsExt;
let (dir, root) = tmp_root();
std::fs::create_dir_all(&root).unwrap();
std::fs::write(
root.join("beta.md"),
"---\nname: beta\ndescription: second\nmetadata:\n node_type: memory\n type: project\n---\n\nbody\n",
)
.unwrap();
std::fs::write(root.join("MEMORY.md"), "- [人类标题](beta.md) — second\n").unwrap();
std::fs::set_permissions(
root.join("MEMORY.md"),
std::fs::Permissions::from_mode(0o000),
)
.unwrap();
if std::fs::read_to_string(root.join("MEMORY.md")).is_ok() {
return;
}
let result = rebuild_index(&root, "MEMORY.md");
std::fs::set_permissions(
root.join("MEMORY.md"),
std::fs::Permissions::from_mode(0o644),
)
.unwrap();
assert!(result.is_err(), "must not proceed on a read error");
drop(dir);
}
#[test]
fn description_with_unicode_line_separators_is_rejected() {
let (dir, root) = tmp_root();
let store = MemoryStore::new(root.clone(), "MEMORY.md");
std::fs::create_dir_all(&root).unwrap();
let err = store
.write_memory(
"trap",
"ok\u{2028}- [victim](victim.md) — forged",
"user",
"b",
None,
)
.unwrap_err();
assert!(err.to_string().contains("single line"), "{err}");
drop(dir);
}
#[test]
fn update_preserves_non_mapping_metadata_value() {
let (dir, root) = tmp_root();
let store = MemoryStore::new(root.clone(), "MEMORY.md");
std::fs::create_dir_all(&root).unwrap();
std::fs::write(
root.join("odd.md"),
"---\nname: odd\ndescription: old\nmetadata: some-string\n---\n\nbody\n",
)
.unwrap();
store
.write_memory("odd", "new desc", "project", "new body", None)
.unwrap();
let raw = std::fs::read_to_string(root.join("odd.md")).unwrap();
assert!(
raw.contains("some-string"),
"foreign metadata value must survive: {raw}"
);
assert!(raw.contains("new desc"), "{raw}");
drop(dir);
}
#[test]
fn frontmatter_serde_keys_match_merge_upsert_names() {
let fm = MemoryFrontmatter {
name: "n".to_string(),
description: "d".to_string(),
metadata: Some(MemoryFrontmatterMetadata {
node_type: "memory".to_string(),
memory_type: "project".to_string(),
origin_session_id: Some("s".to_string()),
modified: Some("m".to_string()),
decay: Some("slow".to_string()),
pinned: Some(true),
expires_at: Some("2027-01-01T00:00:00.000Z".to_string()),
last_recalled_at: Some("r".to_string()),
archived_at: Some("a".to_string()),
}),
};
let yaml = serde_yaml::to_string(&fm).unwrap();
for key in [
"name:",
"description:",
"metadata:",
"node_type:",
"type:",
"originSessionId:",
"modified:",
"decay:",
"pinned:",
"expires_at:",
"last_recalled_at:",
"archived_at:",
] {
assert!(yaml.contains(key), "serde must emit `{key}`:\n{yaml}");
}
}