use crate::config::Config;
use std::ffi::OsString;
use std::path::{Path, PathBuf};
use crate::production_storage::{account_home, is_within};
#[cfg(test)]
use crate::production_storage::{canonicalized_with, comparison_key};
pub fn live_storage_roots() -> Vec<PathBuf> {
let home = account_home().expect("test storage fence must resolve the account profile");
#[cfg(windows)]
let local_data = Some(
crate::production_storage::known_local_app_data()
.unwrap_or_else(|_| home.join("AppData/Local")),
);
#[cfg(not(windows))]
let local_data: Option<PathBuf> = None;
let mut temporary = vec![std::env::temp_dir()];
if let Some(gate_home) = std::env::var_os("AFT_GATE_HERMETIC_HOME_ROOT") {
temporary.push(PathBuf::from(gate_home));
}
let current = live_storage_root_from(&home, local_data.as_deref(), &temporary, &|name| {
std::env::var_os(name)
});
let account = live_storage_root_from(&home, local_data.as_deref(), &[], &|_| None);
let mut roots = crate::production_storage::account_storage_roots()
.expect("test storage fence must resolve a protected root");
roots.extend([current, account]);
roots
}
pub fn live_storage_root_from(
home: &Path,
local_data: Option<&Path>,
temporary: &[PathBuf],
lookup: &impl Fn(&str) -> Option<OsString>,
) -> PathBuf {
crate::storage_path::storage_dir_from_test_environment(&|name| {
let non_temporary = || {
lookup(name).filter(|value| {
!temporary
.iter()
.any(|root| is_within(Path::new(value), root))
})
};
match name {
"AFT_STORAGE_DIR" | "AFT_CACHE_DIR" => None,
"HOME" | "USERPROFILE" => non_temporary().or_else(|| Some(home.as_os_str().into())),
"LOCALAPPDATA" => {
non_temporary().or_else(|| local_data.map(|path| path.as_os_str().into()))
}
_ => non_temporary(),
}
})
}
pub fn assert_context(config: &Config) {
let configured = config
.storage_dir
.as_deref()
.filter(|p| !p.as_os_str().is_empty())
.expect(
"test context must use an explicit isolated storage_dir, never the process default",
);
assert_root(configured);
assert_root(&crate::storage_path::storage_dir(Some(configured)));
}
pub fn assert_root(root: &Path) {
assert!(
!live_storage_roots()
.iter()
.any(|forbidden| is_within(root, forbidden)),
"test context cannot use the live default AFT storage root: {}",
root.display()
);
}
pub fn assert_database(path: &Path) {
if let Some(parent) = path.parent() {
assert_root(parent);
}
}
#[cfg(any(test, feature = "test-support"))]
#[cfg_attr(not(test), allow(dead_code, unused_imports))]
pub mod tests {
use super::*;
#[test]
fn windows_storage_comparison_ignores_verbatim_prefix_case_and_separators() {
let parent = comparison_key(
Path::new(r"C:\Users\RunnerAdmin\AppData\Local\cortexkit\aft"),
true,
);
for root in [
r"\\?\c:\users\runneradmin\appdata\LOCAL\CORTEXKIT\AFT\aft.db",
"c:/Users/RunnerAdmin/AppData/Local/cortexkit/aft/aft.db",
] {
assert!(
comparison_key(Path::new(root), true).starts_with(&parent),
"{root}"
);
}
assert_eq!(
comparison_key(Path::new(r"\\?\UNC\SERVER\Share\aft"), true),
comparison_key(Path::new(r"\\server\share\AFT"), true)
);
assert!(!comparison_key(
Path::new(r"C:\Users\RunnerAdmin\AppData\Local\cortexkit\aft-other"),
true
)
.starts_with(&parent));
}
#[test]
fn storage_comparison_expands_short_name_ancestors_before_appending_missing_tail() {
let short = Path::new("C:/Users/RUNNER~1/AppData/Local/cortexkit/aft/aft.db");
let canonical = canonicalized_with(short, &|path| {
(path == Path::new("C:/Users/RUNNER~1")).then(|| PathBuf::from("C:/Users/RunnerAdmin"))
});
assert_eq!(
comparison_key(&canonical, true),
comparison_key(
Path::new(r"\\?\c:\users\RunnerAdmin\AppData\Local\cortexkit\aft\aft.db"),
true
)
);
}
#[test]
fn temporary_home_and_xdg_do_not_redefine_the_live_storage_root() {
let home = tempfile::tempdir().unwrap();
let fixture = tempfile::tempdir().unwrap();
let live_data = home.path().join("data");
let temporary = [fixture.path().to_path_buf()];
let live = live_storage_root_from(home.path(), None, &temporary, &|name| {
(name == "XDG_DATA_HOME").then(|| live_data.as_os_str().into())
});
assert_eq!(live, live_data.join("cortexkit").join("aft"));
let with_override = |name: &str| match name {
"HOME" | "USERPROFILE" => Some(fixture.path().as_os_str().into()),
"XDG_DATA_HOME" | "LOCALAPPDATA" => Some(fixture.path().join("data").into_os_string()),
"AFT_STORAGE_DIR" | "AFT_CACHE_DIR" => {
Some(fixture.path().join("cache").into_os_string())
}
_ => None,
};
let sandbox_default =
crate::storage_path::storage_dir_from_test_environment(&with_override);
let forbidden = live_storage_root_from(home.path(), None, &temporary, &with_override);
assert!(!is_within(&sandbox_default, &forbidden));
let expected = if cfg!(windows) {
home.path().join("AppData").join("Local")
} else {
home.path().join(".local").join("share")
};
assert_eq!(forbidden, expected.join("cortexkit").join("aft"));
}
pub fn with_temporary_xdg(test: impl FnOnce(&Path)) {
let _env = crate::test_env::process_env_lock();
let fixture = tempfile::tempdir().unwrap();
struct Restore(Option<OsString>);
impl Drop for Restore {
fn drop(&mut self) {
match &self.0 {
Some(value) => std::env::set_var("XDG_DATA_HOME", value),
None => std::env::remove_var("XDG_DATA_HOME"),
}
}
}
let _restore = Restore(std::env::var_os("XDG_DATA_HOME"));
std::env::set_var("XDG_DATA_HOME", fixture.path());
test(fixture.path());
}
#[test]
#[should_panic(expected = "test context cannot use the live default AFT storage root")]
fn test_root_rejects_account_default_even_with_a_temporary_xdg_home() {
#[cfg(windows)]
let data = account_home().unwrap().join("AppData/Local");
#[cfg(not(windows))]
let data = account_home().unwrap().join(".local").join("share");
with_temporary_xdg(|_| assert_root(&data.join("cortexkit").join("aft")));
}
}