use crate::memory::COLLECTION_MEMORY;
use crate::memory::db::Store;
use crate::memory::index::{archive_memory_files, index_file_sync_keyed};
#[test]
fn archive_walk_keys_files_with_prefix_and_ignores_non_md() {
let home = tempfile::tempdir().unwrap();
let memory = home.path().join("memory");
std::fs::create_dir_all(memory.join("archive")).unwrap();
std::fs::write(memory.join("2026-09-20.md"), "# daily").unwrap();
std::fs::write(memory.join("archive/2026-09.md"), "# september").unwrap();
std::fs::write(memory.join("archive/2026-08.md"), "# august").unwrap();
std::fs::write(memory.join("archive/notes.txt"), "not markdown").unwrap();
let files = archive_memory_files(&memory);
let keys: Vec<&str> = files.iter().map(|(k, _)| k.as_str()).collect();
assert_eq!(keys, vec!["archive/2026-08.md", "archive/2026-09.md"]);
for (_, path) in &files {
assert!(path.starts_with(memory.join("archive")));
}
}
#[test]
fn archive_walk_is_empty_without_archive_dir() {
let home = tempfile::tempdir().unwrap();
let memory = home.path().join("memory");
std::fs::create_dir_all(&memory).unwrap();
std::fs::write(memory.join("2026-09-20.md"), "# daily").unwrap();
assert!(archive_memory_files(&memory).is_empty());
}
#[test]
fn archived_file_indexes_under_prefixed_key_and_stays_active() {
let home = tempfile::tempdir().unwrap();
let memory = home.path().join("memory");
std::fs::create_dir_all(memory.join("archive")).unwrap();
let body = "# Archived MEMORY.md sections 2026-09\n\n\
## Old staging layout\n\n\
The staging box lived on Hetzner until 2025.\n";
std::fs::write(memory.join("archive/2026-09.md"), body).unwrap();
let store = Store::open(&home.path().join("test.db")).unwrap();
for (key, path) in archive_memory_files(&memory) {
let content = std::fs::read_to_string(&path).unwrap();
let indexed = index_file_sync_keyed(&store, COLLECTION_MEMORY, &key, &content).unwrap();
assert!(indexed, "first index must write the document");
let again = index_file_sync_keyed(&store, COLLECTION_MEMORY, &key, &content).unwrap();
assert!(
!again,
"unchanged content must hash-skip under the same key"
);
}
let paths = store.get_active_document_paths(COLLECTION_MEMORY).unwrap();
assert!(
paths.iter().any(|p| p == "archive/2026-09.md"),
"archived month must be active in the memory collection so memory_search can see it: {paths:?}"
);
let found = store
.find_active_document(COLLECTION_MEMORY, "archive/2026-09.md")
.unwrap();
assert!(found.is_some(), "archive doc must be findable by its key");
}