use super::{add_to_allowlist, try_migrate_legacy, AllowlistConfig, AllowlistEntry};
use std::path::PathBuf;
use tempfile::TempDir;
fn entry(path: &std::path::Path) -> AllowlistEntry {
AllowlistEntry {
path: path.to_path_buf(),
name: None,
exclude: vec![],
extensions: vec![],
skip_kg: false,
}
}
const DAEMON_TOML: &str = "[[index]]\nid = \"p\"\nroot_path = \"/srv/project\"\n";
const REAL_ALLOWLIST_TOML: &str = "[[index]]\npath = \"/srv/my-project\"\n";
#[test]
fn allowlist_path_does_not_collide_with_daemon_registry() {
let filename = AllowlistConfig::default_path()
.file_name()
.unwrap_or_default()
.to_string_lossy()
.into_owned();
assert_eq!(filename, "allowlist.toml", "got: {filename}");
assert_ne!(
filename, "indexes.toml",
"allowlist must not use the daemon registry filename"
);
}
#[test]
fn daemon_registry_does_not_parse_as_allowlist() {
let parsed = toml::from_str::<AllowlistConfig>(DAEMON_TOML);
match parsed {
Ok(cfg) => {
assert!(
cfg.entries.is_empty(),
"daemon-registry entries must not become allowlist approvals; \
got {} entries",
cfg.entries.len()
);
}
Err(_) => {
}
}
}
#[test]
fn allowlist_load_from_daemon_registry_yields_no_entries() {
let dir = TempDir::new().unwrap();
let daemon_registry = dir.path().join("indexes.toml");
std::fs::write(&daemon_registry, DAEMON_TOML).unwrap();
let cfg = AllowlistConfig::load_from(&daemon_registry);
match cfg {
Ok(c) => {
assert!(
c.entries.is_empty(),
"daemon-registry entries must not become allowlist approvals; \
got {} entries",
c.entries.len()
);
}
Err(_) => {
}
}
}
#[test]
fn add_to_allowlist_succeeds_when_daemon_registry_colocated() {
let dir = TempDir::new().unwrap();
let daemon_registry = dir.path().join("indexes.toml");
std::fs::write(&daemon_registry, DAEMON_TOML).unwrap();
let allowlist = dir.path().join("allowlist.toml");
let safe = PathBuf::from("/srv/my-project");
add_to_allowlist(entry(&safe), Some(&allowlist)).unwrap();
let cfg = AllowlistConfig::load_from(&allowlist).unwrap();
assert_eq!(cfg.entries.len(), 1);
assert_eq!(cfg.entries[0].path, safe);
assert!(
std::fs::read_to_string(&daemon_registry)
.unwrap()
.contains("/srv/project"),
"daemon registry must be untouched"
);
}
#[test]
fn migration_real_allowlist_is_migrated() {
let dir = TempDir::new().unwrap();
let new_path = dir.path().join("allowlist.toml");
let legacy_path = dir.path().join("indexes.toml");
std::fs::write(&legacy_path, REAL_ALLOWLIST_TOML).unwrap();
try_migrate_legacy(&new_path, &legacy_path);
assert!(
new_path.exists(),
"allowlist.toml must be created by migration"
);
let cfg = AllowlistConfig::load_from(&new_path).unwrap();
assert_eq!(
cfg.entries.len(),
1,
"migrated allowlist must contain the original entry"
);
assert_eq!(cfg.entries[0].path, PathBuf::from("/srv/my-project"));
}
#[test]
fn migration_daemon_registry_is_not_migrated() {
let dir = TempDir::new().unwrap();
let new_path = dir.path().join("allowlist.toml");
let legacy_path = dir.path().join("indexes.toml");
std::fs::write(&legacy_path, DAEMON_TOML).unwrap();
try_migrate_legacy(&new_path, &legacy_path);
assert!(
!new_path.exists(),
"allowlist.toml must NOT be created when legacy file is a daemon registry"
);
}
#[test]
fn migration_skipped_when_new_path_exists() {
let dir = TempDir::new().unwrap();
let new_path = dir.path().join("allowlist.toml");
let legacy_path = dir.path().join("indexes.toml");
let existing = AllowlistConfig {
entries: vec![AllowlistEntry {
path: PathBuf::from("/srv/existing"),
name: None,
exclude: vec![],
extensions: vec![],
skip_kg: false,
}],
};
existing.save_to(&new_path).unwrap();
std::fs::write(&legacy_path, REAL_ALLOWLIST_TOML).unwrap();
try_migrate_legacy(&new_path, &legacy_path);
let cfg = AllowlistConfig::load_from(&new_path).unwrap();
assert_eq!(cfg.entries.len(), 1);
assert_eq!(cfg.entries[0].path, PathBuf::from("/srv/existing"));
}
#[test]
fn migration_skipped_when_legacy_absent() {
let dir = TempDir::new().unwrap();
let new_path = dir.path().join("allowlist.toml");
let legacy_path = dir.path().join("indexes.toml");
try_migrate_legacy(&new_path, &legacy_path);
assert!(
!new_path.exists(),
"no allowlist.toml should be created when legacy file is absent"
);
}