#[cfg_attr(not(all(target_arch = "wasm32", feature = "opfs")), allow(dead_code))]
pub(super) fn normalize_root(root: &str) -> String {
let trimmed = root.trim_matches('/');
if trimmed.is_empty() {
String::new()
} else {
format!("/{trimmed}")
}
}
#[cfg_attr(not(all(target_arch = "wasm32", feature = "opfs")), allow(dead_code))]
pub(super) fn join_root(root: &str, path: &str) -> String {
if root.is_empty() {
path.to_string()
} else {
format!("{}/{}", root, path.trim_start_matches('/'))
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn normalize_trims_surrounding_slashes() {
assert_eq!(normalize_root("db"), "/db");
assert_eq!(normalize_root("/db"), "/db");
assert_eq!(normalize_root("/db/"), "/db");
assert_eq!(normalize_root("a/b"), "/a/b");
}
#[test]
fn normalize_empty_or_slash_only_means_no_root() {
assert_eq!(normalize_root(""), "");
assert_eq!(normalize_root("/"), "");
assert_eq!(normalize_root("///"), "");
}
#[test]
fn join_uses_single_separator_regardless_of_leading_slash() {
assert_eq!(join_root("/db", "/main.db"), "/db/main.db");
assert_eq!(join_root("/db", "main.db"), "/db/main.db");
assert_eq!(join_root("/db", "/segments/seg-1"), "/db/segments/seg-1");
}
#[test]
fn empty_root_passes_path_through_unchanged() {
assert_eq!(join_root("", "/main.db"), "/main.db");
assert_eq!(join_root("", "main.db"), "main.db");
}
#[test]
fn distinct_roots_never_collide_on_the_fixed_main_db_path() {
let a = join_root(&normalize_root("alpha"), "/main.db");
let b = join_root(&normalize_root("beta"), "/main.db");
assert_eq!(a, "/alpha/main.db");
assert_eq!(b, "/beta/main.db");
assert_ne!(a, b);
}
}