use super::*;
fn tmp_config() -> (tempfile::TempDir, std::path::PathBuf) {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("config.toml");
(dir, path)
}
#[test]
fn update_global_at_preserves_customized_and_persists_change() {
let (_dir, path) = tmp_config();
std::fs::write(
&path,
"max_ram_percent = 30\ncompression_level = \"standard\"\n",
)
.unwrap();
let returned = Config::update_global_at(&path, |c| c.proxy_enabled = Some(true))
.expect("update_global_at must succeed");
assert_eq!(returned.proxy_enabled, Some(true));
let reloaded: Config = toml::from_str(&std::fs::read_to_string(&path).unwrap()).unwrap();
assert_eq!(
reloaded.max_ram_percent, 30,
"customized value must survive"
);
assert_eq!(reloaded.compression_level, CompressionLevel::Standard);
assert_eq!(reloaded.proxy_enabled, Some(true));
}
#[test]
fn load_global_from_reads_only_the_given_file() {
let (_dir, path) = tmp_config();
std::fs::write(&path, "theme = \"global-theme\"\n").unwrap();
let cfg = Config::load_global_from(&path);
assert_eq!(cfg.theme, "global-theme");
}
#[test]
fn merged_load_then_save_leaks_local_override_root_cause_marker() {
let (_dir, path) = tmp_config();
std::fs::write(&path, "theme = \"global-theme\"\n").unwrap();
let mut cfg = Config::load_global_from(&path);
cfg.merge_local("theme = \"project-local\"\n", true);
cfg.save_to(&path).unwrap();
let reloaded: Config = toml::from_str(&std::fs::read_to_string(&path).unwrap()).unwrap();
assert_eq!(
reloaded.theme, "project-local",
"OLD load+merge_local+save leaks the project-local value into global (#443)"
);
}
#[test]
fn update_global_at_refuses_unparseable_and_leaves_file_untouched() {
let (_dir, path) = tmp_config();
let corrupt = "max_ram_percent = = =\n";
std::fs::write(&path, corrupt).unwrap();
let result = Config::update_global_at(&path, |c| c.proxy_enabled = Some(true));
assert!(
result.is_err(),
"must refuse to modify an unparseable config"
);
assert_eq!(
std::fs::read_to_string(&path).unwrap(),
corrupt,
"the corrupt file must be left exactly as-is"
);
}
#[test]
fn load_global_from_missing_or_empty_yields_defaults() {
let (_dir, path) = tmp_config();
let cfg = Config::load_global_from(&path);
assert_eq!(cfg.max_ram_percent, Config::default().max_ram_percent);
std::fs::write(&path, " \n").unwrap();
let cfg2 = Config::load_global_from(&path);
assert_eq!(cfg2.max_ram_percent, Config::default().max_ram_percent);
}