Files
Kigi-CLI/crates/codegen/kigi-shell/tests/test_config_update_isolation.rs
T
ZacharyZhang-NY a02b555e66 docs(comments): rewrite comments across all crates to the guidelines
Sweep every first-party crate source (1956 .rs files) to the project comment
guidelines: delete redundant restatements, decorative banners, change
narration, and end-of-line comments; keep and tighten the crucial ones
(invariants, bug rationale, SAFETY blocks, ported-source attribution).

No functional code changed. Every edit is proven comment-only against the
prior tree by a comment-stripping lexer (string/char/raw-string aware) plus a
separate doctest-fence check. Where removing a comment made rustfmt or clippy
want to re-lay-out adjacent code, the minimal triggering comment is restored so
code tokens stay byte-identical.

Gates green: cargo fmt --all --check (0 diffs), cargo check and cargo clippy
--workspace --all-targets (0 warnings).

Adds scripts/check_codegen_comment_guidelines.py — the enforcement gate for
these guidelines (flags banners, end-of-line comments, change narration, and
commented-out code).
2026-07-23 16:55:39 -04:00

163 lines
5.3 KiB
Rust

//! Regression test: `update_config` must not leak values from
//! `managed_config.toml` or `requirements.toml` into the user's `config.toml`.
//!
//! Bug: `update_config` used `load_effective_config()` (which merges all config
//! layers) to populate the `Config` struct, then `save_config` wrote that merged
//! result back to the user's `config.toml`. If `requirements.toml` contained
//! `auto_update = false`, any unrelated config write (theme change, model
//! preference, yolo toggle) would permanently poison the user's config.
use std::fs;
use std::path::PathBuf;
use std::sync::OnceLock;
use serial_test::serial;
/// Shared temp directory that lives for the entire test binary.
/// All tests share this as KIGI_SHARE_DIR (the `OnceLock` in kigi-config
/// only allows one value per process).
fn test_home() -> &'static PathBuf {
static HOME: OnceLock<PathBuf> = OnceLock::new();
HOME.get_or_init(|| {
let dir = tempfile::TempDir::new().unwrap();
// Keep so the directory survives the entire test process.
let path = dir.keep();
// SAFETY: called once at init before other threads touch this var.
unsafe { std::env::set_var("KIGI_SHARE_DIR", &path) };
path
})
}
/// Clean up config files between tests.
fn reset_config_files(home: &std::path::Path) {
let _ = fs::remove_file(home.join("config.toml"));
let _ = fs::remove_file(home.join("requirements.toml"));
let _ = fs::remove_file(home.join("managed_config.toml"));
}
#[tokio::test]
#[serial]
async fn update_config_does_not_leak_requirements_into_user_config() {
let home = test_home();
reset_config_files(home);
// User's config.toml: auto_update = true
fs::write(
home.join("config.toml"),
"[cli]\nauto_update = true\ninstaller = \"internal\"\n",
)
.unwrap();
// Enterprise requirements.toml overrides auto_update to false
fs::write(
home.join("requirements.toml"),
"[cli]\nauto_update = false\n",
)
.unwrap();
// Sanity-check: effective config should show auto_update = false
// (requirements wins over user config).
let effective = kigi_shell::config::load_effective_config().unwrap();
let effective_cfg = kigi_shell::util::config::load_config_from_toml(&effective);
assert_eq!(
effective_cfg.cli.auto_update,
Some(false),
"precondition: effective config should merge requirements (auto_update=false)"
);
// Simulate an unrelated config write (e.g. persisting a model preference).
kigi_shell::util::config::update_config(|cfg| {
cfg.models.default = Some("kigi-3".to_string());
})
.await
.expect("update_config should succeed");
// Read the user's config.toml back from disk (raw, no merge).
let raw = fs::read_to_string(home.join("config.toml")).unwrap();
let user_toml: toml::Value = toml::from_str(&raw).unwrap();
let user_cfg = kigi_shell::util::config::load_config_from_toml(&user_toml);
assert_eq!(
user_cfg.cli.auto_update,
Some(true),
"BUG REPRODUCED: auto_update in user config.toml was overwritten by \
requirements.toml value. The raw file contents:\n{raw}"
);
// Also verify the unrelated write succeeded.
assert_eq!(user_cfg.models.default.as_deref(), Some("kigi-3"));
}
#[tokio::test]
#[serial]
async fn update_config_preserves_none_when_only_requirements_sets_value() {
let home = test_home();
reset_config_files(home);
fs::write(
home.join("config.toml"),
"[cli]\ninstaller = \"internal\"\n",
)
.unwrap();
fs::write(
home.join("requirements.toml"),
"[cli]\nauto_update = false\n",
)
.unwrap();
kigi_shell::util::config::update_config(|cfg| {
cfg.ui.yolo = true;
})
.await
.expect("update_config should succeed");
let raw = fs::read_to_string(home.join("config.toml")).unwrap();
let user_toml: toml::Value = toml::from_str(&raw).unwrap();
let user_cfg = kigi_shell::util::config::load_config_from_toml(&user_toml);
assert_eq!(
user_cfg.cli.auto_update, None,
"auto_update should remain absent in user config — requirements.toml \
value must not leak. Raw file:\n{raw}"
);
}
#[tokio::test]
#[serial]
async fn update_config_does_not_leak_managed_config_values() {
let home = test_home();
reset_config_files(home);
fs::write(
home.join("config.toml"),
"[cli]\ninstaller = \"internal\"\n",
)
.unwrap();
fs::write(
home.join("managed_config.toml"),
"[cli]\nauto_update = false\nchannel = \"stable\"\n",
)
.unwrap();
kigi_shell::util::config::update_config(|cfg| {
cfg.models.default = Some("test-model".to_string());
})
.await
.expect("update_config should succeed");
let raw = fs::read_to_string(home.join("config.toml")).unwrap();
let user_toml: toml::Value = toml::from_str(&raw).unwrap();
let user_cfg = kigi_shell::util::config::load_config_from_toml(&user_toml);
assert_eq!(
user_cfg.cli.auto_update, None,
"auto_update from managed_config.toml leaked into user config. Raw:\n{raw}"
);
assert_eq!(
user_cfg.cli.channel, None,
"channel from managed_config.toml leaked into user config. Raw:\n{raw}"
);
}