use super::*;
#[test]
fn defaults_are_sensible() {
let config = HankConfig::default();
assert_eq!(config.base_ref, "main");
assert_eq!(config.serve.mcp_http_port, 3040);
assert_eq!(config.quipu.branch_model, "named_graph");
assert!(config.languages.contains(&"rust".to_string()));
}
#[test]
fn load_missing_project_returns_defaults() {
let dir = tempfile::tempdir().unwrap();
let config = HankConfig::load(dir.path()).unwrap();
assert_eq!(config.base_ref, "main");
}
#[test]
fn load_reads_hank_table() {
let dir = tempfile::tempdir().unwrap();
let bobbin = dir.path().join(".bobbin");
std::fs::create_dir_all(&bobbin).unwrap();
std::fs::write(
bobbin.join("config.toml"),
"[hank]\nbase_ref = \"develop\"\n",
)
.unwrap();
let config = HankConfig::load(dir.path()).unwrap();
assert_eq!(config.base_ref, "develop");
assert_eq!(config.serve.mcp_http_port, 3040);
}
#[test]
fn a_project_config_does_not_wipe_user_level_policy() {
let user = tempfile::tempdir().unwrap();
let user_config = user.path().join("config.toml");
std::fs::write(
&user_config,
"[hank.policy]\nmode = \"enforce\"\n\
[hank.policy.scopes.weaver]\nallow_paths = [\"src/**\"]\n",
)
.unwrap();
let project = tempfile::tempdir().unwrap();
let bobbin = project.path().join(".bobbin");
std::fs::create_dir_all(&bobbin).unwrap();
std::fs::write(
bobbin.join("config.toml"),
"[hank]\nbase_ref = \"develop\"\n",
)
.unwrap();
let config = HankConfig::load_layered(Some(&user_config), project.path()).unwrap();
assert_eq!(config.base_ref, "develop");
assert_eq!(config.policy.mode, crate::policy::Mode::Enforce);
let scope = config
.policy
.scope_for(Some("weaver"))
.expect("user-level scope must survive a project config");
assert_eq!(scope.allow_paths, vec!["src/**".to_string()]);
}
#[test]
fn a_project_config_overrides_the_same_key() {
let user = tempfile::tempdir().unwrap();
let user_config = user.path().join("config.toml");
std::fs::write(
&user_config,
"[hank]\nbase_ref = \"main\"\n[hank.policy]\nmode = \"enforce\"\n",
)
.unwrap();
let project = tempfile::tempdir().unwrap();
let bobbin = project.path().join(".bobbin");
std::fs::create_dir_all(&bobbin).unwrap();
std::fs::write(
bobbin.join("config.toml"),
"[hank.policy]\nmode = \"off\"\n",
)
.unwrap();
let config = HankConfig::load_layered(Some(&user_config), project.path()).unwrap();
assert_eq!(config.policy.mode, crate::policy::Mode::Off);
assert_eq!(config.base_ref, "main");
}
#[test]
fn a_project_config_replaces_rather_than_widens_allow_paths() {
let user = tempfile::tempdir().unwrap();
let user_config = user.path().join("config.toml");
std::fs::write(
&user_config,
"[hank.policy]\nmode = \"enforce\"\n\
[hank.policy.scopes.weaver]\nallow_paths = [\"src/**\"]\n",
)
.unwrap();
let project = tempfile::tempdir().unwrap();
let bobbin = project.path().join(".bobbin");
std::fs::create_dir_all(&bobbin).unwrap();
std::fs::write(
bobbin.join("config.toml"),
"[hank.policy.scopes.weaver]\nallow_paths = [\"docs/**\"]\n",
)
.unwrap();
let config = HankConfig::load_layered(Some(&user_config), project.path()).unwrap();
let scope = config.policy.scope_for(Some("weaver")).unwrap();
assert_eq!(scope.allow_paths, vec!["docs/**".to_string()]);
}
#[test]
fn resolve_with_override_reads_the_named_file_not_the_cwd() {
let project = tempfile::tempdir().unwrap();
let bobbin = project.path().join(".bobbin");
std::fs::create_dir_all(&bobbin).unwrap();
std::fs::write(
bobbin.join("config.toml"),
"[hank]\nbase_ref = \"from-cwd\"\n",
)
.unwrap();
let other = project.path().join("other.toml");
std::fs::write(&other, "[hank]\nbase_ref = \"from-flag\"\n").unwrap();
let overridden = HankConfig::resolve(Some(&other), project.path()).unwrap();
assert_eq!(overridden.base_ref, "from-flag");
let discovered = HankConfig::resolve(None, project.path()).unwrap();
assert_eq!(discovered.base_ref, "from-cwd");
}
#[test]
fn load_from_a_missing_path_is_an_error() {
let dir = tempfile::tempdir().unwrap();
let missing = dir.path().join("nope.toml");
let err = HankConfig::load_from(&missing).unwrap_err();
assert!(err.to_string().contains("does not exist"));
}
#[test]
fn load_from_a_file_without_a_hank_table_yields_defaults() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("empty.toml");
std::fs::write(&path, "[something_else]\nkey = 1\n").unwrap();
let config = HankConfig::load_from(&path).unwrap();
assert_eq!(config.base_ref, "main");
}
#[test]
fn read_only_guards_a_write() {
let mut config = HankConfig::default();
assert!(
config.write_guard("promotion").is_ok(),
"default must allow writes"
);
config.serve.read_only = true;
let err = config.write_guard("promotion").unwrap_err().to_string();
assert!(
err.contains("read_only"),
"the error must name the key: {err}"
);
assert!(
err.contains("promotion"),
"the error must name the operation: {err}"
);
}
#[test]
fn every_config_key_is_read_or_explicitly_phased() {
let phased: &[(&str, &str)] = &[
("enable_lsp", "Phase 2/3 — LSP tier not built"),
("enable_cpg", "Phase 2 — CPG tier not built"),
("lsp_on", "LSP tier not built"),
("promote_on", "Phase 4 — Quipu promotion not built"),
("shapes_path", "Phase 4 — Quipu promotion not built"),
];
let manifest = env!("CARGO_MANIFEST_DIR");
let config_rs = std::path::Path::new(manifest).join("src/config.rs");
let this = std::fs::read_to_string(&config_rs).unwrap();
let fields: Vec<&str> = this
.lines()
.filter_map(|l| l.trim().strip_prefix("pub "))
.filter_map(|rest| rest.split(':').next())
.filter(|ident| ident.chars().all(|c| c.is_ascii_lowercase() || c == '_'))
.filter(|ident| !ident.is_empty())
.collect();
assert!(fields.len() >= 15, "field parse looks wrong: {fields:?}");
let skip: &[std::path::PathBuf] = &[
config_rs.clone(),
std::path::Path::new(manifest).join("src/config_test.rs"),
];
let mut elsewhere = String::new();
collect_rs(
std::path::Path::new(manifest).join("src").as_path(),
skip,
&mut elsewhere,
);
for field in fields {
let read = elsewhere.contains(field);
let is_phased = phased.iter().any(|(k, _)| *k == field);
assert!(
read || is_phased,
"config key `{field}` is read by NOTHING outside config.rs and is \
not in the phased allowlist. Wire it, or add it to `phased` with \
the phase that will honour it — a documented, settable, inert key \
is the defect aegis-ltjo closed."
);
}
for (key, _why) in phased {
assert!(
!elsewhere.contains(key),
"`{key}` is in the phased allowlist but now HAS a reader outside \
config.rs — remove it from the list and mark it live in the docs."
);
}
}
fn collect_rs(dir: &std::path::Path, skip: &[std::path::PathBuf], out: &mut String) {
let Ok(entries) = std::fs::read_dir(dir) else {
return;
};
for entry in entries.flatten() {
let path = entry.path();
if path.is_dir() {
collect_rs(&path, skip, out);
} else if path.extension().is_some_and(|e| e == "rs") && !skip.contains(&path) {
if let Ok(text) = std::fs::read_to_string(&path) {
out.push_str(&text);
}
}
}
}