use hakodb::config::{DurabilityMode, HakoConfig};
use hakodb::document::hako_doc::HakoDoc;
use hakodb::document::value::Value;
use hakodb::engine::Hako;
fn temp_dir(tag: &str) -> std::path::PathBuf {
let dir = std::env::temp_dir().join(format!(
"hakodb-migtest-{}-{}",
std::process::id(),
tag
));
let _ = std::fs::remove_dir_all(&dir);
std::fs::create_dir_all(&dir).unwrap();
dir
}
fn put_simple(db: &Hako, col: &str, id: &str, n: i64) {
let mut doc = HakoDoc::default();
doc.insert("v", Value::Int(n));
db.put(col, id, &doc).unwrap();
}
fn open_manual(dir: &std::path::Path) -> Hako {
let mut cfg = HakoConfig::default();
cfg.durability_mode = DurabilityMode::Manual;
Hako::open(dir, cfg).unwrap()
}
#[test]
fn legacy_dirs_migrate_with_data_intact() {
let dir = temp_dir("basic");
{
let db = open_manual(&dir);
put_simple(&db, "__firelite_system", "probe", 1);
put_simple(&db, "__firelite_rooms", "room1", 2);
put_simple(&db, "__firelite_security", "policy", 3);
put_simple(&db, "__users", "admin", 4);
put_simple(&db, "users", "alice", 5);
}
{
let db = open_manual(&dir);
for old in ["__firelite_system", "__firelite_rooms", "__firelite_security"] {
assert!(
!dir.join(old).exists(),
"legacy dir must be gone: {old}"
);
}
for new in ["__hako_system", "__hako_rooms", "__hako_security"] {
assert!(dir.join(new).is_dir(), "canonical dir missing: {new}");
}
let get = |col: &str, id: &str| {
db.get(col, id)
.unwrap()
.expect("migrated doc missing")
.get("v")
.cloned()
};
assert_eq!(get("__hako_system", "probe"), Some(Value::Int(1)));
assert_eq!(get("__hako_rooms", "room1"), Some(Value::Int(2)));
assert_eq!(get("__hako_security", "policy"), Some(Value::Int(3)));
assert_eq!(get("__users", "admin"), Some(Value::Int(4)));
assert_eq!(get("users", "alice"), Some(Value::Int(5)));
for col in [
"__hako_system",
"__hako_rooms",
"__firelite_system",
"__firelite_rooms",
"__users",
"__groups",
] {
assert!(
db.is_sync_excluded_effective(col),
"{col} must be sync-excluded"
);
}
assert!(!db.is_sync_excluded_effective("users"));
assert!(!db.is_sync_excluded_effective("__hako_security"));
let cols = db.sync_collections().unwrap();
assert!(cols.contains(&"users".to_string()), "{cols:?}");
for hidden in ["__hako_system", "__hako_rooms", "__users"] {
assert!(!cols.contains(&hidden.to_string()), "{cols:?}");
}
assert!(cols.contains(&"__hako_security".to_string()), "{cols:?}");
}
let _ = std::fs::remove_dir_all(&dir);
}
#[test]
fn both_present_keeps_canonical_and_orphan() {
let dir = temp_dir("both");
{
let db = open_manual(&dir);
put_simple(&db, "__hako_system", "keep", 10);
put_simple(&db, "users", "u", 11);
std::fs::create_dir_all(dir.join("__firelite_system")).unwrap();
put_simple(&db, "__firelite_system", "orphan", 99);
}
{
let db = open_manual(&dir);
let keep = db
.get("__hako_system", "keep")
.unwrap()
.expect("canonical doc missing");
assert_eq!(keep.get("v").cloned(), Some(Value::Int(10)));
assert!(dir.join("__firelite_system").is_dir());
assert!(db.is_sync_excluded_effective("__firelite_system"));
assert!(!db.sync_collections().unwrap().contains(&"__firelite_system".to_string()));
}
let _ = std::fs::remove_dir_all(&dir);
}