use tempfile::tempdir;
use wm_core::Galaxy;
use wm_memory::MemoryStore;
#[test]
fn store_opens_with_default_map_size() {
let tmp = tempdir().unwrap();
let store = MemoryStore::open_default(tmp.path()).unwrap();
let _env = store.env();
}
#[test]
fn store_creates_all_14_galaxy_databases() {
let tmp = tempdir().unwrap();
let store = MemoryStore::open_default(tmp.path()).unwrap();
for galaxy in Galaxy::all() {
let db = store.galaxy_db(galaxy);
assert!(db.is_ok(), "Failed to open galaxy DB for {galaxy:?}");
}
}
#[test]
fn store_galaxy_db_names_match_enum() {
let tmp = tempdir().unwrap();
let store = MemoryStore::open_default(tmp.path()).unwrap();
for galaxy in Galaxy::all() {
let db = store.galaxy_db(galaxy).unwrap();
let _ = db;
}
}
#[test]
fn store_can_reopen_existing_environment() {
let tmp = tempdir().unwrap();
{
let _store = MemoryStore::open_default(tmp.path()).unwrap();
}
{
let store = MemoryStore::open_default(tmp.path()).unwrap();
for galaxy in Galaxy::all() {
assert!(
store.galaxy_db(galaxy).is_ok(),
"Galaxy {galaxy:?} should be openable on reopen"
);
}
}
}
#[test]
fn store_custom_map_size_works() {
let tmp = tempdir().unwrap();
let store = MemoryStore::open(tmp.path(), 10 * 1024 * 1024).unwrap();
let _ = store.galaxy_db(Galaxy::Citta).unwrap();
}
#[test]
fn store_all_galaxy_db_names_are_unique_lmdb_names() {
let tmp = tempdir().unwrap();
let store = MemoryStore::open_default(tmp.path()).unwrap();
let mut names = std::collections::HashSet::new();
for galaxy in Galaxy::all() {
let db_name = galaxy.db_name();
assert!(
names.insert(db_name.to_string()),
"Duplicate LMDB db name: {db_name}"
);
store.galaxy_db(galaxy).unwrap();
}
assert_eq!(names.len(), Galaxy::COUNT);
}
#[test]
fn test_valkyrie_sanctuary_memory_storage() {
use wm_memory::Memory;
let tmp = tempdir().unwrap();
let store = MemoryStore::open_default(tmp.path()).unwrap();
let memory = Memory::new(
Galaxy::Valkyrie,
"WhiteMagic sanctuary memory: kept whole, never deleted.".to_string(),
);
let id = memory.metadata.id;
store.put(Galaxy::Valkyrie, &memory).unwrap();
let retrieved = store.get(Galaxy::Valkyrie, id).unwrap();
assert!(retrieved.is_some());
let r = retrieved.unwrap();
assert_eq!(
r.content,
"WhiteMagic sanctuary memory: kept whole, never deleted."
);
assert_eq!(store.count(Galaxy::Valkyrie).unwrap(), 1);
}