| 1 | use std::{ |
| 2 | fs, |
| 3 | path::{Path, PathBuf}, |
| 4 | process::Command, |
| 5 | }; |
| 6 | |
| 7 | fn run(root: &Path, config: Option<&str>, args: &[&str]) -> String { |
| 8 | let bin = std::env::var_os("CARGO_BIN_EXE_codewhale") |
| 9 | .filter(|value| !value.is_empty()) |
| 10 | .map(PathBuf::from) |
| 11 | .unwrap_or_else(|| PathBuf::from(env!("CARGO_BIN_EXE_codewhale"))); |
| 12 | let mut cmd = Command::new(bin); |
| 13 | cmd.current_dir(root) |
| 14 | .env_clear() |
| 15 | .env("HOME", root) |
| 16 | .env("USERPROFILE", root) |
| 17 | .env("CODEWHALE_HOME", root.join("state")) |
| 18 | .env("CODEWHALE_CONFIG_PATH", root.join("environment.toml")) |
| 19 | .env("DEEPSEEK_CONFIG_PATH", root.join("older.toml")) |
| 20 | .env("CODEWHALE_SECRET_BACKEND", "file") |
| 21 | .env("CODEWHALE_TELEMETRY", "off") |
| 22 | .env("NO_ANIMATIONS", "1"); |
| 23 | for name in ["PATH", "RUSTUP_HOME", "SystemRoot", "WINDIR"] { |
| 24 | if let Some(value) = std::env::var_os(name) { |
| 25 | cmd.env(name, value); |
| 26 | } |
| 27 | } |
| 28 | if let Some(path) = config { |
| 29 | cmd.args(["--config", path]); |
| 30 | } |
| 31 | let out = cmd.args(args).output().unwrap(); |
| 32 | assert!( |
| 33 | out.status.success(), |
| 34 | "{args:?}: {}", |
| 35 | String::from_utf8_lossy(&out.stderr) |
| 36 | ); |
| 37 | String::from_utf8(out.stdout).unwrap() |
| 38 | } |
| 39 | |
| 40 | #[test] |
| 41 | fn explicit_config_scopes_saved_preferences_and_the_tui_settings_reader() { |
| 42 | let temp = tempfile::tempdir().unwrap(); |
| 43 | let root = temp.path(); |
| 44 | fs::create_dir(root.join("state")).unwrap(); |
| 45 | let home_settings = root.join("state/settings.toml"); |
| 46 | let home_bytes = "default_mode = 'agent'\nlow_motion = true\n"; |
| 47 | fs::write(&home_settings, home_bytes).unwrap(); |
| 48 | for file in ["config.toml", "demo.toml", "environment.toml", "older.toml"] { |
| 49 | fs::write(root.join(file), "telemetry = false\n").unwrap(); |
| 50 | } |
| 51 | let canonical = Some("config.toml"); |
| 52 | let demo = Some("demo.toml"); |
| 53 | run(root, canonical, &["config", "set", "calm", "false"]); |
| 54 | run(root, None, &["config", "set", "calm", "true"]); |
| 55 | run(root, demo, &["config", "set", "low_motion", "false"]); |
| 56 | run(root, demo, &["config", "set", "default_mode", "plan"]); |
| 57 | assert!(run(root, demo, &["config", "list"]).contains("low_motion = false")); |
| 58 | let get = |key| { |
| 59 | let output = run(root, demo, &["config", "get", key, "--json"]); |
| 60 | serde_json::from_str::<serde_json::Value>(&output).unwrap() |
| 61 | }; |
| 62 | let saved = get("low_motion"); |
| 63 | assert_eq!(saved["configured_value"], "false"); |
| 64 | assert_eq!(saved["effective_value"], "true"); |
| 65 | assert_eq!(saved["source"], "env"); |
| 66 | let list: Vec<serde_json::Value> = |
| 67 | serde_json::from_str(&run(root, demo, &["config", "list", "--json"])).unwrap(); |
| 68 | assert!(list.contains(&saved)); |
| 69 | assert_eq!( |
| 70 | run(root, canonical, &["config", "get", "calm"]).trim(), |
| 71 | "false" |
| 72 | ); |
| 73 | assert_eq!(run(root, None, &["config", "get", "calm"]).trim(), "true"); |
| 74 | let doctor: serde_json::Value = |
| 75 | serde_json::from_str(&run(root, demo, &["doctor", "--json"])).unwrap(); |
| 76 | assert_eq!( |
| 77 | doctor["setup"]["runtime_posture"]["default_mode"]["value"], |
| 78 | "plan" |
| 79 | ); |
| 80 | run(root, demo, &["config", "unset", "motion"]); |
| 81 | assert!(get("low_motion")["configured_value"].is_null()); |
| 82 | assert_eq!(get("low_motion")["effective_value"], "true"); |
| 83 | run(root, demo, &["config", "unset", "default_mode"]); |
| 84 | assert_eq!(get("default_mode")["source"], "default"); |
| 85 | assert!( |
| 86 | !fs::read_to_string(root.join("demo.toml.settings.toml")) |
| 87 | .unwrap() |
| 88 | .contains("low_motion") |
| 89 | ); |
| 90 | assert_eq!(fs::read_to_string(&home_settings).unwrap(), home_bytes); |
| 91 | assert!(!root.join("older.toml.settings.toml").exists()); |
| 92 | } |
| 93 | |
| 94 | #[test] |
| 95 | fn invalid_config_override_preserves_doctor_json_and_redacted_api_key_handoff() { |
| 96 | const SENTINEL: &str = "sentinel-relative-config-path"; |
| 97 | let temp = tempfile::tempdir().unwrap(); |
| 98 | let root = temp.path(); |
| 99 | fs::create_dir(root.join("state")).unwrap(); |
| 100 | let bin = std::env::var_os("CARGO_BIN_EXE_codewhale") |
| 101 | .filter(|value| !value.is_empty()) |
| 102 | .map(PathBuf::from) |
| 103 | .unwrap_or_else(|| PathBuf::from(env!("CARGO_BIN_EXE_codewhale"))); |
| 104 | let build = || { |
| 105 | let mut cmd = Command::new(&bin); |
| 106 | cmd.current_dir(root) |
| 107 | .env_clear() |
| 108 | .env("HOME", root) |
| 109 | .env("USERPROFILE", root) |
| 110 | .env("CODEWHALE_HOME", root.join("state")) |
| 111 | .env("CODEWHALE_CONFIG_PATH", format!("{SENTINEL}.toml")) |
| 112 | .env("CODEWHALE_SECRET_BACKEND", "file") |
| 113 | .env("CODEWHALE_TELEMETRY", "off") |
| 114 | .env("NO_ANIMATIONS", "1"); |
| 115 | for name in ["PATH", "RUSTUP_HOME", "SystemRoot", "WINDIR"] { |
| 116 | if let Some(value) = std::env::var_os(name) { |
| 117 | cmd.env(name, value); |
| 118 | } |
| 119 | } |
| 120 | cmd |
| 121 | }; |
| 122 | |
| 123 | let handoff = build() |
| 124 | .args(["auth", "print-api-key", "--provider", "openrouter"]) |
| 125 | .output() |
| 126 | .unwrap(); |
| 127 | assert!(!handoff.status.success()); |
| 128 | assert!(handoff.stdout.is_empty()); |
| 129 | let stderr = String::from_utf8_lossy(&handoff.stderr); |
| 130 | assert_eq!(stderr, "error: unavailable credential\n"); |
| 131 | assert!(!stderr.contains(SENTINEL)); |
| 132 | |
| 133 | let doctor = build().args(["doctor", "--json"]).output().unwrap(); |
| 134 | assert!(!doctor.status.success()); |
| 135 | let doctor_stdout = String::from_utf8_lossy(&doctor.stdout); |
| 136 | let doctor_stderr = String::from_utf8_lossy(&doctor.stderr); |
| 137 | assert!(!doctor_stdout.contains(SENTINEL)); |
| 138 | assert!(!doctor_stderr.contains(SENTINEL)); |
| 139 | let parsed: serde_json::Value = serde_json::from_str(&doctor_stdout).unwrap(); |
| 140 | assert_eq!(parsed["status"], "error", "{parsed}"); |
| 141 | assert_eq!(parsed["error"]["kind"], "config_validation", "{parsed}"); |
| 142 | } |
| 143 |