| 1 | //! Process-level regression coverage for read-only diagnostic commands. |
| 2 | |
| 3 | use std::fs; |
| 4 | use std::path::PathBuf; |
| 5 | use std::process::{Command, Output}; |
| 6 | use std::sync::{Arc, Mutex, mpsc}; |
| 7 | use std::thread; |
| 8 | use std::time::Duration; |
| 9 | |
| 10 | use axum::body::Bytes; |
| 11 | use axum::http::HeaderMap; |
| 12 | use axum::routing::post; |
| 13 | use axum::{Json, Router}; |
| 14 | use codewhale_secrets::{FileKeyringStore, KeyringStore}; |
| 15 | use tempfile::TempDir; |
| 16 | |
| 17 | #[test] |
| 18 | fn doctor_text_leaves_a_sealed_home_untouched() { |
| 19 | let output = run_sealed_diagnostic(["doctor"]); |
| 20 | let stdout = String::from_utf8_lossy(&output.stdout); |
| 21 | assert!(stdout.contains("codewhale Doctor"), "stdout:\n{stdout}"); |
| 22 | } |
| 23 | |
| 24 | #[test] |
| 25 | fn doctor_json_leaves_a_sealed_home_untouched() { |
| 26 | let output = run_sealed_diagnostic(["doctor", "--json"]); |
| 27 | let report: serde_json::Value = |
| 28 | serde_json::from_slice(&output.stdout).unwrap_or_else(|error| { |
| 29 | panic!( |
| 30 | "doctor --json must remain machine-readable: {error}\nstdout:\n{}\nstderr:\n{}", |
| 31 | String::from_utf8_lossy(&output.stdout), |
| 32 | String::from_utf8_lossy(&output.stderr) |
| 33 | ) |
| 34 | }); |
| 35 | assert_eq!(report["api_connectivity"]["checked"], false); |
| 36 | } |
| 37 | |
| 38 | #[test] |
| 39 | fn doctor_json_rejects_kimi_code_claude_alias_with_machine_readable_guidance() { |
| 40 | let fixture = TempDir::new().expect("fixture root"); |
| 41 | let workspace = fixture.path().join("workspace"); |
| 42 | let home = fixture.path().join("home"); |
| 43 | let codewhale_home = fixture.path().join("isolated-codewhale-home"); |
| 44 | fs::create_dir_all(&workspace).expect("workspace"); |
| 45 | let config = workspace.join("kimi-invalid.toml"); |
| 46 | let config_bytes = br#"provider = "moonshot" |
| 47 | |
| 48 | [providers.moonshot] |
| 49 | api_key = "doctor-json-kimi-secret" |
| 50 | base_url = "https://api.kimi.com/coding/v1" |
| 51 | model = "k3[1m]" |
| 52 | "#; |
| 53 | fs::write(&config, config_bytes).expect("write invalid Kimi config"); |
| 54 | |
| 55 | let mut command = diagnostic_command(&workspace, &home); |
| 56 | command |
| 57 | .args([ |
| 58 | "--config", |
| 59 | config.to_str().expect("config path"), |
| 60 | "doctor", |
| 61 | "--json", |
| 62 | ]) |
| 63 | .env("CODEWHALE_HOME", &codewhale_home); |
| 64 | let output = command.output().expect("run invalid Kimi doctor json"); |
| 65 | |
| 66 | assert!( |
| 67 | !output.status.success(), |
| 68 | "invalid configuration must return nonzero\nstdout:\n{}\nstderr:\n{}", |
| 69 | String::from_utf8_lossy(&output.stdout), |
| 70 | String::from_utf8_lossy(&output.stderr) |
| 71 | ); |
| 72 | let report: serde_json::Value = serde_json::from_slice(&output.stdout) |
| 73 | .expect("invalid doctor --json output must remain machine-readable"); |
| 74 | assert_eq!(report["status"], "error"); |
| 75 | assert_eq!(report["error"]["kind"], "config_validation"); |
| 76 | let message = report["error"]["message"] |
| 77 | .as_str() |
| 78 | .expect("config error message"); |
| 79 | assert!(message.contains("model = \"k3\""), "{message}"); |
| 80 | assert!(message.contains("context_window = 1048576"), "{message}"); |
| 81 | assert!(message.contains("plan includes 1M context"), "{message}"); |
| 82 | |
| 83 | let stderr = String::from_utf8_lossy(&output.stderr); |
| 84 | assert!( |
| 85 | stderr.contains("doctor configuration validation failed; see JSON output"), |
| 86 | "stderr must point only to the JSON envelope: {stderr}" |
| 87 | ); |
| 88 | for stdout_only_detail in ["k3[1m]", "context_window", "plan includes 1M context"] { |
| 89 | assert!( |
| 90 | !stderr.contains(stdout_only_detail), |
| 91 | "actionable validation details belong only in redacted stdout JSON: {stderr}" |
| 92 | ); |
| 93 | } |
| 94 | |
| 95 | let all_output = format!( |
| 96 | "{}\n{}", |
| 97 | String::from_utf8_lossy(&output.stdout), |
| 98 | String::from_utf8_lossy(&output.stderr) |
| 99 | ); |
| 100 | assert!(!all_output.contains("doctor-json-kimi-secret")); |
| 101 | assert_eq!( |
| 102 | fs::read(&config).expect("read config after doctor"), |
| 103 | config_bytes, |
| 104 | "doctor must not rewrite an invalid config" |
| 105 | ); |
| 106 | assert!(!home.exists(), "doctor must not create HOME state"); |
| 107 | assert!( |
| 108 | !codewhale_home.exists(), |
| 109 | "doctor must not create CODEWHALE_HOME state" |
| 110 | ); |
| 111 | } |
| 112 | |
| 113 | #[test] |
| 114 | fn doctor_json_parse_failure_never_enters_provider_dispatch() { |
| 115 | let fixture = TempDir::new().expect("fixture root"); |
| 116 | let workspace = fixture.path().join("workspace"); |
| 117 | let home = fixture.path().join("home"); |
| 118 | let codewhale_home = fixture.path().join("isolated-codewhale-home"); |
| 119 | fs::create_dir_all(&workspace).expect("workspace"); |
| 120 | let listener = std::net::TcpListener::bind("127.0.0.1:0").expect("owned provider listener"); |
| 121 | listener |
| 122 | .set_nonblocking(true) |
| 123 | .expect("nonblocking listener"); |
| 124 | let endpoint = listener.local_addr().expect("provider address"); |
| 125 | let config = workspace.join("malformed.toml"); |
| 126 | let config_bytes = format!( |
| 127 | "provider = \"openai\"\n[providers.openai]\nbase_url = \"http://{endpoint}/v1\"\napi_key = \"doctor-malformed-secret\"\nmodel =\n" |
| 128 | ); |
| 129 | fs::write(&config, &config_bytes).expect("write malformed source"); |
| 130 | let output = diagnostic_command(&workspace, &home) |
| 131 | .args([ |
| 132 | "--config", |
| 133 | config.to_str().expect("config path"), |
| 134 | "doctor", |
| 135 | "--json", |
| 136 | ]) |
| 137 | .env("CODEWHALE_HOME", &codewhale_home) |
| 138 | .output() |
| 139 | .expect("run malformed doctor json"); |
| 140 | |
| 141 | assert!(!output.status.success()); |
| 142 | let report: serde_json::Value = |
| 143 | serde_json::from_slice(&output.stdout).expect("machine-readable parse error"); |
| 144 | assert_eq!(report["error"]["kind"], "config_validation"); |
| 145 | let all_output = format!( |
| 146 | "{}\n{}", |
| 147 | String::from_utf8_lossy(&output.stdout), |
| 148 | String::from_utf8_lossy(&output.stderr) |
| 149 | ); |
| 150 | assert!(!all_output.contains("doctor-malformed-secret")); |
| 151 | assert!( |
| 152 | matches!(listener.accept(), Err(error) if error.kind() == std::io::ErrorKind::WouldBlock), |
| 153 | "a rejected diagnostic source must not contact its provider" |
| 154 | ); |
| 155 | assert_eq!( |
| 156 | fs::read(&config).expect("source after report"), |
| 157 | config_bytes.as_bytes() |
| 158 | ); |
| 159 | assert!(!home.exists(), "doctor must not create HOME state"); |
| 160 | assert!( |
| 161 | !codewhale_home.exists(), |
| 162 | "doctor must not create Codewhale state" |
| 163 | ); |
| 164 | } |
| 165 | |
| 166 | #[test] |
| 167 | fn doctor_json_omits_untrusted_config_validation_details() { |
| 168 | let fixture = TempDir::new().expect("fixture root"); |
| 169 | let workspace = fixture.path().join("workspace"); |
| 170 | let home = fixture.path().join("home"); |
| 171 | let codewhale_home = fixture.path().join("isolated-codewhale-home"); |
| 172 | fs::create_dir_all(&workspace).expect("workspace"); |
| 173 | let config = workspace.join("untrusted-invalid.toml"); |
| 174 | let config_bytes = br#"provider = "doctor-untrusted-provider-secret" |
| 175 | api_key = "doctor-json-arbitrary-secret" |
| 176 | "#; |
| 177 | fs::write(&config, config_bytes).expect("write invalid config"); |
| 178 | |
| 179 | let mut command = diagnostic_command(&workspace, &home); |
| 180 | command |
| 181 | .args([ |
| 182 | "--config", |
| 183 | config.to_str().expect("config path"), |
| 184 | "doctor", |
| 185 | "--json", |
| 186 | ]) |
| 187 | .env("CODEWHALE_HOME", &codewhale_home); |
| 188 | let output = command.output().expect("run invalid doctor json"); |
| 189 | |
| 190 | assert!(!output.status.success()); |
| 191 | let report: serde_json::Value = |
| 192 | serde_json::from_slice(&output.stdout).expect("machine-readable doctor error"); |
| 193 | assert_eq!(report["error"]["kind"], "config_validation"); |
| 194 | // A plain value error names the key and the valid values, never the |
| 195 | // rejected value itself (it may be a pasted secret). |
| 196 | let message = report["error"]["message"].as_str().expect("message"); |
| 197 | assert!( |
| 198 | message.starts_with("Invalid provider (value not shown): expected deepseek"), |
| 199 | "{message}" |
| 200 | ); |
| 201 | let fix = report["error"]["fix"].as_str().expect("fix"); |
| 202 | assert!( |
| 203 | fix.starts_with("codewhale config set provider deepseek (if CODEWHALE_PROVIDER"), |
| 204 | "{fix}" |
| 205 | ); |
| 206 | let all_output = format!( |
| 207 | "{}\n{}", |
| 208 | String::from_utf8_lossy(&output.stdout), |
| 209 | String::from_utf8_lossy(&output.stderr) |
| 210 | ); |
| 211 | assert!(!all_output.contains("doctor-untrusted-provider-secret")); |
| 212 | assert!(!all_output.contains("doctor-json-arbitrary-secret")); |
| 213 | assert_eq!( |
| 214 | fs::read(&config).expect("read config after doctor"), |
| 215 | config_bytes |
| 216 | ); |
| 217 | assert!(!home.exists()); |
| 218 | assert!(!codewhale_home.exists()); |
| 219 | } |
| 220 | |
| 221 | #[test] |
| 222 | fn doctor_json_reports_valid_kimi_code_k3_context_override_from_runtime_route() { |
| 223 | let fixture = TempDir::new().expect("fixture root"); |
| 224 | let workspace = fixture.path().join("workspace"); |
| 225 | let home = fixture.path().join("home"); |
| 226 | let codewhale_home = fixture.path().join("isolated-codewhale-home"); |
| 227 | fs::create_dir_all(&workspace).expect("workspace"); |
| 228 | let config = workspace.join("kimi-valid.toml"); |
| 229 | let config_bytes = br#"provider = "moonshot" |
| 230 | |
| 231 | [providers.moonshot] |
| 232 | api_key = "doctor-json-valid-kimi-secret" |
| 233 | base_url = "https://api.kimi.com/coding/v1" |
| 234 | model = "k3" |
| 235 | context_window = 1048576 |
| 236 | "#; |
| 237 | fs::write(&config, config_bytes).expect("write valid Kimi config"); |
| 238 | |
| 239 | let mut command = diagnostic_command(&workspace, &home); |
| 240 | command |
| 241 | .args([ |
| 242 | "--config", |
| 243 | config.to_str().expect("config path"), |
| 244 | "doctor", |
| 245 | "--json", |
| 246 | ]) |
| 247 | .env("CODEWHALE_HOME", &codewhale_home); |
| 248 | let output = command.output().expect("run valid Kimi doctor json"); |
| 249 | |
| 250 | assert!( |
| 251 | output.status.success(), |
| 252 | "valid Kimi doctor --json failed\nstdout:\n{}\nstderr:\n{}", |
| 253 | String::from_utf8_lossy(&output.stdout), |
| 254 | String::from_utf8_lossy(&output.stderr) |
| 255 | ); |
| 256 | let report: serde_json::Value = |
| 257 | serde_json::from_slice(&output.stdout).expect("machine-readable doctor report"); |
| 258 | assert_eq!(report["route"]["model"], "k3"); |
| 259 | assert_eq!(report["route"]["context_window"]["tokens"], 1_048_576); |
| 260 | assert_eq!(report["route"]["context_window"]["source"], "configured"); |
| 261 | assert!(report["route"]["route_error"].is_null()); |
| 262 | assert_eq!(report["capability"]["resolved_model"], "k3"); |
| 263 | assert_eq!(report["capability"]["context_window"], 1_048_576); |
| 264 | assert_eq!(report["capability"]["context_window_source"], "configured"); |
| 265 | assert!(report["capability"]["route_error"].is_null()); |
| 266 | |
| 267 | let all_output = format!( |
| 268 | "{}\n{}", |
| 269 | String::from_utf8_lossy(&output.stdout), |
| 270 | String::from_utf8_lossy(&output.stderr) |
| 271 | ); |
| 272 | assert!(!all_output.contains("doctor-json-valid-kimi-secret")); |
| 273 | assert_eq!( |
| 274 | fs::read(&config).expect("read config after doctor"), |
| 275 | config_bytes, |
| 276 | "doctor must not rewrite a valid config" |
| 277 | ); |
| 278 | assert!(!home.exists(), "doctor must not create HOME state"); |
| 279 | assert!( |
| 280 | !codewhale_home.exists(), |
| 281 | "doctor must not create CODEWHALE_HOME state" |
| 282 | ); |
| 283 | } |
| 284 | |
| 285 | #[test] |
| 286 | fn doctor_context_json_leaves_a_sealed_home_untouched() { |
| 287 | let output = run_sealed_diagnostic(["doctor", "--context-json"]); |
| 288 | let report: serde_json::Value = |
| 289 | serde_json::from_slice(&output.stdout).unwrap_or_else(|error| { |
| 290 | panic!( |
| 291 | "doctor --context-json must remain machine-readable: {error}\nstdout:\n{}\nstderr:\n{}", |
| 292 | String::from_utf8_lossy(&output.stdout), |
| 293 | String::from_utf8_lossy(&output.stderr) |
| 294 | ) |
| 295 | }); |
| 296 | assert!( |
| 297 | report["entries"].is_array(), |
| 298 | "doctor --context-json must emit a source map\nstdout:\n{}", |
| 299 | String::from_utf8_lossy(&output.stdout) |
| 300 | ); |
| 301 | } |
| 302 | |
| 303 | #[test] |
| 304 | fn setup_status_leaves_a_sealed_home_untouched() { |
| 305 | let output = run_sealed_diagnostic(["setup", "--status"]); |
| 306 | let stdout = String::from_utf8_lossy(&output.stdout); |
| 307 | assert!(stdout.contains("Codewhale Status"), "stdout:\n{stdout}"); |
| 308 | } |
| 309 | |
| 310 | #[test] |
| 311 | fn diagnostics_read_home_legacy_settings_without_migrating_them() { |
| 312 | for args in [ |
| 313 | &["doctor"][..], |
| 314 | &["doctor", "--full"][..], |
| 315 | &["doctor", "--json"][..], |
| 316 | &["setup", "--status"][..], |
| 317 | ] { |
| 318 | let fixture = TempDir::new().expect("fixture root"); |
| 319 | let workspace = fixture.path().join("workspace"); |
| 320 | let home = fixture.path().join("home"); |
| 321 | let legacy = home.join(".deepseek").join("settings.toml"); |
| 322 | let primary_home = home.join(".codewhale"); |
| 323 | let legacy_bytes = b"default_mode = \"plan\"\nprefer_external_pdftotext = true\n"; |
| 324 | fs::create_dir_all(&workspace).expect("workspace"); |
| 325 | fs::create_dir_all(legacy.parent().expect("legacy parent")).expect("legacy directory"); |
| 326 | fs::write(&legacy, legacy_bytes).expect("legacy settings"); |
| 327 | |
| 328 | let output = diagnostic_command(&workspace, &home) |
| 329 | .args(args) |
| 330 | .output() |
| 331 | .expect("run diagnostic against legacy settings"); |
| 332 | assert!( |
| 333 | output.status.success(), |
| 334 | "diagnostic {args:?} failed\nstdout:\n{}\nstderr:\n{}", |
| 335 | String::from_utf8_lossy(&output.stdout), |
| 336 | String::from_utf8_lossy(&output.stderr) |
| 337 | ); |
| 338 | |
| 339 | match args { |
| 340 | ["doctor"] => { |
| 341 | let stdout = String::from_utf8_lossy(&output.stdout); |
| 342 | for part in [ |
| 343 | "Works:", |
| 344 | "Needs attention:", |
| 345 | "Next step:", |
| 346 | "`codewhale doctor --full` prints every check.", |
| 347 | ] { |
| 348 | assert!( |
| 349 | stdout.contains(part), |
| 350 | "the short report must print {part:?}\nstdout:\n{stdout}" |
| 351 | ); |
| 352 | } |
| 353 | assert!( |
| 354 | !stdout.contains("default_mode=plan (settings)"), |
| 355 | "the short report must leave the posture detail to --full\nstdout:\n{stdout}" |
| 356 | ); |
| 357 | } |
| 358 | ["doctor", "--full"] => { |
| 359 | let stdout = String::from_utf8_lossy(&output.stdout); |
| 360 | assert!( |
| 361 | stdout.contains("default_mode=plan (settings)"), |
| 362 | "doctor must report the legacy default mode\nstdout:\n{stdout}" |
| 363 | ); |
| 364 | assert!( |
| 365 | !stdout.contains("prefer_external_pdftotext"), |
| 366 | "doctor must not advertise the removed PDF preference\nstdout:\n{stdout}" |
| 367 | ); |
| 368 | } |
| 369 | ["doctor", "--json"] => { |
| 370 | let report: serde_json::Value = |
| 371 | serde_json::from_slice(&output.stdout).expect("machine-readable doctor report"); |
| 372 | assert_eq!( |
| 373 | report["setup"]["runtime_posture"]["default_mode"]["value"], |
| 374 | "plan" |
| 375 | ); |
| 376 | assert_eq!( |
| 377 | report["setup"]["runtime_posture"]["default_mode"]["source"], |
| 378 | "settings" |
| 379 | ); |
| 380 | } |
| 381 | ["setup", "--status"] => { |
| 382 | let stdout = String::from_utf8_lossy(&output.stdout); |
| 383 | assert!( |
| 384 | stdout.contains("default_mode: plan (settings)"), |
| 385 | "setup status must report the legacy default mode\nstdout:\n{stdout}" |
| 386 | ); |
| 387 | } |
| 388 | _ => unreachable!("fixed diagnostic command list"), |
| 389 | } |
| 390 | |
| 391 | assert_eq!( |
| 392 | fs::read(&legacy).expect("legacy settings after diagnostic"), |
| 393 | legacy_bytes, |
| 394 | "diagnostic {args:?} must not rewrite legacy settings" |
| 395 | ); |
| 396 | assert!( |
| 397 | !primary_home.exists(), |
| 398 | "diagnostic {args:?} must not create a primary Codewhale home" |
| 399 | ); |
| 400 | } |
| 401 | } |
| 402 | |
| 403 | #[test] |
| 404 | fn doctor_json_does_not_inherit_an_ambient_legacy_secret_from_an_explicit_home() { |
| 405 | let fixture = TempDir::new().expect("fixture root"); |
| 406 | let workspace = fixture.path().join("workspace"); |
| 407 | let home = fixture.path().join("home"); |
| 408 | let codewhale_home = fixture.path().join("isolated-codewhale-home"); |
| 409 | fs::create_dir_all(&workspace).expect("workspace"); |
| 410 | let legacy = home.join(".deepseek").join("secrets").join("secrets.json"); |
| 411 | FileKeyringStore::new(&legacy) |
| 412 | .set("deepseek", "synthetic-ambient-legacy-value") |
| 413 | .expect("seed ambient legacy secret"); |
| 414 | let legacy_before = fs::read(&legacy).expect("read legacy secret before doctor"); |
| 415 | |
| 416 | let mut command = Command::new(crate::binary::codewhale()); |
| 417 | command |
| 418 | .current_dir(&workspace) |
| 419 | .args(["doctor", "--json"]) |
| 420 | .env_clear() |
| 421 | .env("PATH", std::env::var_os("PATH").expect("PATH")) |
| 422 | .env("HOME", &home) |
| 423 | .env("USERPROFILE", &home) |
| 424 | .env("CODEWHALE_HOME", &codewhale_home) |
| 425 | .env("CODEWHALE_SECRET_BACKEND", "file") |
| 426 | .env( |
| 427 | "CODEWHALE_RELEASE_BASE_URL", |
| 428 | "https://example.invalid/releases", |
| 429 | ) |
| 430 | .env("DEEPSEEK_TUI_VERSION", env!("CARGO_PKG_VERSION")); |
| 431 | preserve_host_rustup_home(&mut command); |
| 432 | preserve_host_platform_runtime(&mut command); |
| 433 | |
| 434 | let output = command.output().expect("run isolated doctor --json"); |
| 435 | assert!( |
| 436 | output.status.success(), |
| 437 | "isolated doctor --json failed\nstdout:\n{}\nstderr:\n{}", |
| 438 | String::from_utf8_lossy(&output.stdout), |
| 439 | String::from_utf8_lossy(&output.stderr) |
| 440 | ); |
| 441 | let report: serde_json::Value = |
| 442 | serde_json::from_slice(&output.stdout).expect("machine-readable doctor report"); |
| 443 | assert_eq!( |
| 444 | report["api_key"]["source"], "secret_store_unprobed", |
| 445 | "doctor must report only structural eligibility, not an ambient legacy secret from outside an explicit home" |
| 446 | ); |
| 447 | assert_eq!(report["api_key"]["availability"], "not_probed"); |
| 448 | assert_eq!( |
| 449 | fs::read(&legacy).expect("read legacy secret after doctor"), |
| 450 | legacy_before, |
| 451 | "doctor must not rewrite the ambient legacy secret" |
| 452 | ); |
| 453 | assert!( |
| 454 | !codewhale_home.exists(), |
| 455 | "doctor must not create an isolated Codewhale home or secret store" |
| 456 | ); |
| 457 | } |
| 458 | |
| 459 | #[test] |
| 460 | fn doctor_text_probe_uses_a_legacy_key_without_migrating_it() { |
| 461 | let fixture = TempDir::new().expect("fixture root"); |
| 462 | let workspace = fixture.path().join("workspace"); |
| 463 | let home = fixture.path().join("home"); |
| 464 | fs::create_dir_all(&workspace).expect("workspace"); |
| 465 | let legacy = home.join(".deepseek").join("secrets").join("secrets.json"); |
| 466 | let primary = home.join(".codewhale").join("secrets").join("secrets.json"); |
| 467 | FileKeyringStore::new(&legacy) |
| 468 | .set("deepseek", "diagnostic-legacy-key") |
| 469 | .expect("seed legacy secret"); |
| 470 | let legacy_before = fs::read(&legacy).expect("read legacy secret before doctor"); |
| 471 | let server = CompletionServer::start(); |
| 472 | let base_url = server.base_url(); |
| 473 | let config = workspace.join("doctor.toml"); |
| 474 | fs::write( |
| 475 | &config, |
| 476 | format!( |
| 477 | "provider = \"deepseek\"\n[providers.deepseek]\nbase_url = \"{base_url}\"\nmodel = \"deepseek-chat\"\nauth_mode = \"api_key\"\n" |
| 478 | ), |
| 479 | ) |
| 480 | .expect("write doctor config"); |
| 481 | |
| 482 | let output = diagnostic_command(&workspace, &home) |
| 483 | .args([ |
| 484 | "--config", |
| 485 | config.to_str().expect("config path"), |
| 486 | "doctor", |
| 487 | "--probe-local", |
| 488 | "--full", |
| 489 | ]) |
| 490 | .output() |
| 491 | .expect("run doctor probe"); |
| 492 | assert!( |
| 493 | output.status.success(), |
| 494 | "doctor probe failed\nstdout:\n{}\nstderr:\n{}", |
| 495 | String::from_utf8_lossy(&output.stdout), |
| 496 | String::from_utf8_lossy(&output.stderr) |
| 497 | ); |
| 498 | let stdout = String::from_utf8_lossy(&output.stdout); |
| 499 | assert!( |
| 500 | stdout.contains("API connection successful"), |
| 501 | "stdout:\n{stdout}" |
| 502 | ); |
| 503 | // The probe runs before the verdict, and the verdict reports its result. |
| 504 | let verdict = stdout |
| 505 | .find("Ready: the live deepseek API check passed") |
| 506 | .unwrap_or_else(|| panic!("verdict must use the probe result\nstdout:\n{stdout}")); |
| 507 | let connectivity = stdout |
| 508 | .find("API Connectivity") |
| 509 | .expect("connectivity section"); |
| 510 | assert!(verdict < connectivity, "stdout:\n{stdout}"); |
| 511 | let requests = server.received_requests(); |
| 512 | assert_eq!( |
| 513 | requests.len(), |
| 514 | 1, |
| 515 | "doctor must make one local probe request" |
| 516 | ); |
| 517 | let authorization = requests[0] |
| 518 | .get("authorization") |
| 519 | .and_then(|value| value.to_str().ok()); |
| 520 | assert_eq!( |
| 521 | authorization, |
| 522 | Some("Bearer diagnostic-legacy-key"), |
| 523 | "doctor probe must use the legacy credential without printing it" |
| 524 | ); |
| 525 | assert!( |
| 526 | !primary.exists(), |
| 527 | "doctor's text connectivity probe must not create a migrated primary secret store" |
| 528 | ); |
| 529 | assert_eq!( |
| 530 | fs::read(&legacy).expect("read legacy secret after doctor"), |
| 531 | legacy_before, |
| 532 | "doctor must not rewrite the legacy secret store" |
| 533 | ); |
| 534 | |
| 535 | let output = diagnostic_command(&workspace, &home) |
| 536 | .args([ |
| 537 | "--config", |
| 538 | config.to_str().expect("config path"), |
| 539 | "doctor", |
| 540 | "--probe-local", |
| 541 | ]) |
| 542 | .output() |
| 543 | .expect("run short doctor probe"); |
| 544 | let stdout = String::from_utf8_lossy(&output.stdout); |
| 545 | assert!( |
| 546 | output.status.success(), |
| 547 | "short doctor probe failed\nstdout:\n{stdout}" |
| 548 | ); |
| 549 | assert!( |
| 550 | stdout.contains("(live check passed)"), |
| 551 | "the short report must say the live check passed\nstdout:\n{stdout}" |
| 552 | ); |
| 553 | assert!( |
| 554 | !stdout.contains("API Connectivity"), |
| 555 | "the short report has no connectivity section\nstdout:\n{stdout}" |
| 556 | ); |
| 557 | assert_eq!(server.received_requests().len(), 2); |
| 558 | |
| 559 | fs::write(&config, "").expect("write unconfigured route"); |
| 560 | let output = diagnostic_command(&workspace, &home) |
| 561 | .args([ |
| 562 | "--config", |
| 563 | config.to_str().expect("config path"), |
| 564 | "doctor", |
| 565 | "--probe-local", |
| 566 | ]) |
| 567 | .env("CODEWHALE_DISABLE_LOCAL_OLLAMA_PROBE", "1") |
| 568 | .env("CODEWHALE_DISABLE_MODELS_DEV_FETCH", "1") |
| 569 | .env("CODEWHALE_DISABLE_CLOUD_FACTS", "1") |
| 570 | .env("CODEWHALE_NO_UPDATE_CHECK", "1") |
| 571 | .output() |
| 572 | .expect("run doctor with unconfigured route"); |
| 573 | assert!(output.status.success(), "unconfigured doctor probe failed"); |
| 574 | assert!( |
| 575 | !primary.exists(), |
| 576 | "credential inspection must not migrate the store" |
| 577 | ); |
| 578 | assert_eq!(fs::read(&legacy).expect("legacy preserved"), legacy_before); |
| 579 | } |
| 580 | |
| 581 | #[test] |
| 582 | fn doctor_json_reports_a_legacy_store_without_reading_or_migrating_it() { |
| 583 | let fixture = TempDir::new().expect("fixture root"); |
| 584 | let workspace = fixture.path().join("workspace"); |
| 585 | let home = fixture.path().join("home"); |
| 586 | fs::create_dir_all(&workspace).expect("workspace"); |
| 587 | let legacy = home.join(".deepseek").join("secrets").join("secrets.json"); |
| 588 | let primary = home.join(".codewhale").join("secrets").join("secrets.json"); |
| 589 | FileKeyringStore::new(&legacy) |
| 590 | .set("xiaomi-mimo", "tp-diagnostic-legacy-key") |
| 591 | .expect("seed legacy Xiaomi secret"); |
| 592 | let legacy_before = fs::read(&legacy).expect("read legacy secret before doctor"); |
| 593 | let config = workspace.join("doctor.toml"); |
| 594 | fs::write( |
| 595 | &config, |
| 596 | "provider = \"xiaomi-mimo\"\n[providers.xiaomi_mimo]\nmode = \"standard\"\n", |
| 597 | ) |
| 598 | .expect("write doctor config"); |
| 599 | |
| 600 | let output = diagnostic_command(&workspace, &home) |
| 601 | .args([ |
| 602 | "--config", |
| 603 | config.to_str().expect("config path"), |
| 604 | "doctor", |
| 605 | "--json", |
| 606 | ]) |
| 607 | .output() |
| 608 | .expect("run doctor json"); |
| 609 | assert!( |
| 610 | output.status.success(), |
| 611 | "doctor --json failed\nstdout:\n{}\nstderr:\n{}", |
| 612 | String::from_utf8_lossy(&output.stdout), |
| 613 | String::from_utf8_lossy(&output.stderr) |
| 614 | ); |
| 615 | let report: serde_json::Value = |
| 616 | serde_json::from_slice(&output.stdout).expect("machine-readable doctor report"); |
| 617 | assert_eq!(report["api_key"]["source"], "secret_store_unprobed"); |
| 618 | assert_eq!(report["api_key"]["availability"], "not_probed"); |
| 619 | assert_eq!( |
| 620 | report["route"]["auth"]["scheme"], "unknown", |
| 621 | "ordinary JSON doctor must not read the legacy key prefix to refine the Xiaomi scheme" |
| 622 | ); |
| 623 | assert_eq!(report["route"]["auth"]["source"], "secret_store_unprobed"); |
| 624 | assert_eq!(report["route"]["auth"]["availability"], "not_probed"); |
| 625 | assert!( |
| 626 | !primary.exists(), |
| 627 | "doctor --json must not migrate a legacy secret while classifying auth" |
| 628 | ); |
| 629 | assert_eq!( |
| 630 | fs::read(&legacy).expect("read legacy secret after doctor"), |
| 631 | legacy_before, |
| 632 | "doctor --json must not rewrite the legacy secret store" |
| 633 | ); |
| 634 | } |
| 635 | |
| 636 | #[test] |
| 637 | fn setup_status_reports_a_legacy_store_without_reading_or_migrating_it() { |
| 638 | let fixture = TempDir::new().expect("fixture root"); |
| 639 | let workspace = fixture.path().join("workspace"); |
| 640 | let home = fixture.path().join("home"); |
| 641 | fs::create_dir_all(&workspace).expect("workspace"); |
| 642 | let legacy = home.join(".deepseek").join("secrets").join("secrets.json"); |
| 643 | let primary = home.join(".codewhale").join("secrets").join("secrets.json"); |
| 644 | FileKeyringStore::new(&legacy) |
| 645 | .set("deepseek", "setup-status-legacy-key") |
| 646 | .expect("seed legacy secret"); |
| 647 | let legacy_before = fs::read(&legacy).expect("read legacy secret before setup"); |
| 648 | |
| 649 | let output = diagnostic_command(&workspace, &home) |
| 650 | .args(["setup", "--status"]) |
| 651 | .output() |
| 652 | .expect("run setup status"); |
| 653 | assert!( |
| 654 | output.status.success(), |
| 655 | "setup --status failed\nstdout:\n{}\nstderr:\n{}", |
| 656 | String::from_utf8_lossy(&output.stdout), |
| 657 | String::from_utf8_lossy(&output.stderr) |
| 658 | ); |
| 659 | let stdout = String::from_utf8_lossy(&output.stdout); |
| 660 | assert!( |
| 661 | stdout.contains("api_key: secret store eligible (store not probed)"), |
| 662 | "stdout:\n{stdout}" |
| 663 | ); |
| 664 | assert!( |
| 665 | stdout.contains("credential availability: not_probed"), |
| 666 | "stdout:\n{stdout}" |
| 667 | ); |
| 668 | assert!( |
| 669 | !primary.exists(), |
| 670 | "setup --status must not create a migrated primary secret store" |
| 671 | ); |
| 672 | assert_eq!( |
| 673 | fs::read(&legacy).expect("read legacy secret after setup"), |
| 674 | legacy_before, |
| 675 | "setup --status must not rewrite the legacy secret store" |
| 676 | ); |
| 677 | } |
| 678 | |
| 679 | #[test] |
| 680 | fn doctor_json_stash_honors_an_explicit_codewhale_home() { |
| 681 | let fixture = TempDir::new().expect("fixture root"); |
| 682 | let workspace = fixture.path().join("workspace"); |
| 683 | let home = fixture.path().join("home"); |
| 684 | let codewhale_home = fixture.path().join("isolated-codewhale-home"); |
| 685 | fs::create_dir_all(&workspace).expect("workspace"); |
| 686 | let ambient_stash = home.join(".codewhale").join("composer_stash.jsonl"); |
| 687 | fs::create_dir_all(ambient_stash.parent().expect("ambient stash parent")) |
| 688 | .expect("ambient stash parent"); |
| 689 | fs::write( |
| 690 | &ambient_stash, |
| 691 | r#"{"text":"ambient draft must not be inspected"}"#, |
| 692 | ) |
| 693 | .expect("ambient stash"); |
| 694 | let ambient_before = fs::read(&ambient_stash).expect("read ambient stash before doctor"); |
| 695 | |
| 696 | let mut command = diagnostic_command(&workspace, &home); |
| 697 | command |
| 698 | .args(["doctor", "--json"]) |
| 699 | .env("CODEWHALE_HOME", &codewhale_home); |
| 700 | let output = command.output().expect("run isolated doctor json"); |
| 701 | assert!( |
| 702 | output.status.success(), |
| 703 | "isolated doctor --json failed\nstdout:\n{}\nstderr:\n{}", |
| 704 | String::from_utf8_lossy(&output.stdout), |
| 705 | String::from_utf8_lossy(&output.stderr) |
| 706 | ); |
| 707 | let report: serde_json::Value = |
| 708 | serde_json::from_slice(&output.stdout).expect("machine-readable doctor report"); |
| 709 | assert_eq!( |
| 710 | report["storage"]["stash"]["path"], |
| 711 | codewhale_home |
| 712 | .join("composer_stash.jsonl") |
| 713 | .display() |
| 714 | .to_string() |
| 715 | ); |
| 716 | assert_eq!(report["storage"]["stash"]["present"], false); |
| 717 | assert_eq!(report["storage"]["stash"]["count"], 0); |
| 718 | assert!(report["storage"]["stash"]["error"].is_null()); |
| 719 | assert!( |
| 720 | !String::from_utf8_lossy(&output.stdout).contains("ambient draft must not be inspected"), |
| 721 | "doctor must not inspect an ambient stash outside explicit CODEWHALE_HOME" |
| 722 | ); |
| 723 | assert_eq!( |
| 724 | fs::read(&ambient_stash).expect("read ambient stash after doctor"), |
| 725 | ambient_before, |
| 726 | "doctor must not rewrite the ambient stash" |
| 727 | ); |
| 728 | assert!( |
| 729 | !codewhale_home.exists(), |
| 730 | "a diagnostic must not create an explicit stash home" |
| 731 | ); |
| 732 | } |
| 733 | |
| 734 | #[test] |
| 735 | fn doctor_reports_journal_archives_read_only() { |
| 736 | let fixture = TempDir::new().expect("fixture root"); |
| 737 | let workspace = fixture.path().join("workspace"); |
| 738 | let home = fixture.path().join("home"); |
| 739 | let codewhale_home = fixture.path().join("isolated-codewhale-home"); |
| 740 | fs::create_dir_all(&workspace).expect("workspace"); |
| 741 | fs::create_dir_all(&home).expect("home"); |
| 742 | |
| 743 | let mut empty_cmd = diagnostic_command(&workspace, &home); |
| 744 | empty_cmd |
| 745 | .args(["doctor", "--json"]) |
| 746 | .env("CODEWHALE_HOME", &codewhale_home); |
| 747 | let empty_out = empty_cmd.output().expect("run empty doctor json"); |
| 748 | assert!(empty_out.status.success()); |
| 749 | let empty_report: serde_json::Value = |
| 750 | serde_json::from_slice(&empty_out.stdout).expect("empty doctor json"); |
| 751 | assert_eq!( |
| 752 | empty_report["storage"]["journal_archives"]["present"], |
| 753 | false |
| 754 | ); |
| 755 | assert_eq!(empty_report["storage"]["journal_archives"]["sessions"], 0); |
| 756 | assert!( |
| 757 | !codewhale_home.exists(), |
| 758 | "doctor must not create CODEWHALE_HOME or .journal-archive" |
| 759 | ); |
| 760 | |
| 761 | let archive_dir = codewhale_home.join("sessions").join(".journal-archive"); |
| 762 | fs::create_dir_all(&archive_dir).expect("archive dir"); |
| 763 | let orphan_file = archive_dir.join("orphan-doc-test.jsonl"); |
| 764 | let payload = b"{\"id\":\"e1\"}\n"; |
| 765 | fs::write(&orphan_file, payload).expect("seed orphan archive"); |
| 766 | |
| 767 | let mut json_cmd = diagnostic_command(&workspace, &home); |
| 768 | json_cmd |
| 769 | .args(["doctor", "--json"]) |
| 770 | .env("CODEWHALE_HOME", &codewhale_home); |
| 771 | let json_out = json_cmd.output().expect("run doctor json with archive"); |
| 772 | assert!(json_out.status.success()); |
| 773 | let report: serde_json::Value = |
| 774 | serde_json::from_slice(&json_out.stdout).expect("doctor json with archive"); |
| 775 | assert_eq!(report["storage"]["journal_archives"]["present"], true); |
| 776 | assert_eq!(report["storage"]["journal_archives"]["sessions"], 1); |
| 777 | assert_eq!( |
| 778 | report["storage"]["journal_archives"]["orphaned_sessions"], |
| 779 | 1 |
| 780 | ); |
| 781 | assert_eq!( |
| 782 | report["storage"]["journal_archives"]["total_bytes"], |
| 783 | payload.len() as u64 |
| 784 | ); |
| 785 | assert!(report["storage"]["journal_archives"]["error"].is_null()); |
| 786 | |
| 787 | let mut full_cmd = diagnostic_command(&workspace, &home); |
| 788 | full_cmd |
| 789 | .args(["doctor", "--full"]) |
| 790 | .env("CODEWHALE_HOME", &codewhale_home); |
| 791 | let full_out = full_cmd.output().expect("run doctor full with archive"); |
| 792 | assert!(full_out.status.success()); |
| 793 | let stdout = String::from_utf8_lossy(&full_out.stdout); |
| 794 | assert!( |
| 795 | stdout.contains("journal archives: 1 session(s)"), |
| 796 | "{stdout}" |
| 797 | ); |
| 798 | assert!(stdout.contains("1 orphaned"), "{stdout}"); |
| 799 | assert!( |
| 800 | stdout.contains("codewhale sessions archive prune --orphans-only --apply"), |
| 801 | "{stdout}" |
| 802 | ); |
| 803 | assert_eq!( |
| 804 | fs::read(&orphan_file).expect("orphan file intact"), |
| 805 | payload, |
| 806 | "doctor must never mutate or delete journal archives" |
| 807 | ); |
| 808 | } |
| 809 | |
| 810 | #[test] |
| 811 | fn doctor_json_reports_memory_enabled_from_config() { |
| 812 | let fixture = TempDir::new().expect("fixture root"); |
| 813 | let workspace = fixture.path().join("workspace"); |
| 814 | let home = fixture.path().join("home"); |
| 815 | let codewhale_home = fixture.path().join("isolated-codewhale-home"); |
| 816 | fs::create_dir_all(&workspace).expect("workspace"); |
| 817 | let config = workspace.join("memory-on.toml"); |
| 818 | fs::write(&config, "[memory]\nenabled = true\n").expect("write memory config"); |
| 819 | |
| 820 | let mut command = diagnostic_command(&workspace, &home); |
| 821 | command |
| 822 | .args([ |
| 823 | "--config", |
| 824 | config.to_str().expect("config path"), |
| 825 | "doctor", |
| 826 | "--json", |
| 827 | ]) |
| 828 | .env("CODEWHALE_HOME", &codewhale_home); |
| 829 | let output = command.output().expect("run memory doctor json"); |
| 830 | assert!( |
| 831 | output.status.success(), |
| 832 | "memory doctor --json failed\nstdout:\n{}\nstderr:\n{}", |
| 833 | String::from_utf8_lossy(&output.stdout), |
| 834 | String::from_utf8_lossy(&output.stderr) |
| 835 | ); |
| 836 | let report: serde_json::Value = |
| 837 | serde_json::from_slice(&output.stdout).expect("machine-readable doctor report"); |
| 838 | assert_eq!(report["memory"]["enabled"], true); |
| 839 | assert!(report["plugins"]["bundles"].is_array(), "{report}"); |
| 840 | } |
| 841 | |
| 842 | fn run_sealed_diagnostic<const N: usize>(args: [&str; N]) -> Output { |
| 843 | let fixture = TempDir::new().expect("fixture root"); |
| 844 | let workspace = fixture.path().join("workspace"); |
| 845 | let sealed_home = fixture.path().join("sealed-home"); |
| 846 | let codewhale_home = fixture.path().join("sealed-codewhale-home"); |
| 847 | std::fs::create_dir_all(&workspace).expect("workspace"); |
| 848 | |
| 849 | let mut command = Command::new(crate::binary::codewhale()); |
| 850 | command |
| 851 | .current_dir(&workspace) |
| 852 | .args(args) |
| 853 | .env_clear() |
| 854 | .env("PATH", std::env::var_os("PATH").expect("PATH")) |
| 855 | .env("HOME", &sealed_home) |
| 856 | .env("USERPROFILE", &sealed_home) |
| 857 | .env("CODEWHALE_HOME", &codewhale_home) |
| 858 | .env("CODEWHALE_SECRET_BACKEND", "file") |
| 859 | // Keep the text doctor command offline: the release crate treats this |
| 860 | // as a pinned mirror version and does not issue a metadata request. |
| 861 | .env( |
| 862 | "CODEWHALE_RELEASE_BASE_URL", |
| 863 | "https://example.invalid/releases", |
| 864 | ) |
| 865 | .env("DEEPSEEK_TUI_VERSION", env!("CARGO_PKG_VERSION")); |
| 866 | preserve_host_rustup_home(&mut command); |
| 867 | preserve_host_platform_runtime(&mut command); |
| 868 | |
| 869 | let output = command.output().expect("run sealed diagnostic"); |
| 870 | assert!( |
| 871 | output.status.success(), |
| 872 | "diagnostic {args:?} failed\nstdout:\n{}\nstderr:\n{}", |
| 873 | String::from_utf8_lossy(&output.stdout), |
| 874 | String::from_utf8_lossy(&output.stderr) |
| 875 | ); |
| 876 | assert!( |
| 877 | !sealed_home.exists(), |
| 878 | "diagnostic {args:?} must not create a HOME tree at {}", |
| 879 | sealed_home.display() |
| 880 | ); |
| 881 | assert!( |
| 882 | !codewhale_home.exists(), |
| 883 | "diagnostic {args:?} must not create CODEWHALE_HOME or a secrets store at {}", |
| 884 | codewhale_home.display() |
| 885 | ); |
| 886 | output |
| 887 | } |
| 888 | |
| 889 | fn diagnostic_command(workspace: &std::path::Path, home: &std::path::Path) -> Command { |
| 890 | let mut command = Command::new(crate::binary::codewhale()); |
| 891 | command |
| 892 | .current_dir(workspace) |
| 893 | .env_clear() |
| 894 | .env("PATH", std::env::var_os("PATH").expect("PATH")) |
| 895 | .env("HOME", home) |
| 896 | .env("USERPROFILE", home) |
| 897 | .env("CODEWHALE_SECRET_BACKEND", "file") |
| 898 | .env( |
| 899 | "CODEWHALE_RELEASE_BASE_URL", |
| 900 | "https://example.invalid/releases", |
| 901 | ) |
| 902 | .env("DEEPSEEK_TUI_VERSION", env!("CARGO_PKG_VERSION")); |
| 903 | preserve_host_rustup_home(&mut command); |
| 904 | preserve_host_platform_runtime(&mut command); |
| 905 | command |
| 906 | } |
| 907 | |
| 908 | struct CompletionServer { |
| 909 | base_url: String, |
| 910 | requests: Arc<Mutex<Vec<HeaderMap>>>, |
| 911 | shutdown: Option<tokio::sync::oneshot::Sender<()>>, |
| 912 | owner: Option<thread::JoinHandle<()>>, |
| 913 | } |
| 914 | |
| 915 | impl CompletionServer { |
| 916 | fn start() -> Self { |
| 917 | let (ready_sender, ready_receiver) = mpsc::sync_channel(1); |
| 918 | let (shutdown, shutdown_receiver) = tokio::sync::oneshot::channel(); |
| 919 | let requests = Arc::new(Mutex::new(Vec::new())); |
| 920 | let server_requests = Arc::clone(&requests); |
| 921 | let owner = thread::spawn(move || { |
| 922 | let runtime = tokio::runtime::Builder::new_multi_thread() |
| 923 | .worker_threads(2) |
| 924 | .enable_all() |
| 925 | .build() |
| 926 | .expect("local probe runtime"); |
| 927 | runtime.block_on(async move { |
| 928 | let app = Router::new().route( |
| 929 | "/v1/chat/completions", |
| 930 | post(move |headers: HeaderMap, body: Bytes| { |
| 931 | let requests = Arc::clone(&server_requests); |
| 932 | async move { |
| 933 | // Extracting Bytes makes Axum drain the complete request body |
| 934 | // before replying. Preserve only headers for the credential |
| 935 | // assertion; the request payload itself is intentionally dropped. |
| 936 | drop(body); |
| 937 | requests |
| 938 | .lock() |
| 939 | .expect("local probe request lock") |
| 940 | .push(headers); |
| 941 | Json(serde_json::json!({ |
| 942 | "id": "doctor", |
| 943 | "object": "chat.completion", |
| 944 | "created": 0, |
| 945 | "model": "deepseek-chat", |
| 946 | "choices": [{ |
| 947 | "index": 0, |
| 948 | "message": {"role": "assistant", "content": "ok"}, |
| 949 | "finish_reason": "stop" |
| 950 | }], |
| 951 | "usage": { |
| 952 | "prompt_tokens": 1, |
| 953 | "completion_tokens": 1, |
| 954 | "total_tokens": 2 |
| 955 | } |
| 956 | })) |
| 957 | } |
| 958 | }), |
| 959 | ); |
| 960 | let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await; |
| 961 | let listener = listener.expect("bind local probe server"); |
| 962 | let address = listener.local_addr().expect("local probe address"); |
| 963 | ready_sender |
| 964 | .send(format!("http://{address}/v1")) |
| 965 | .expect("publish local probe address"); |
| 966 | axum::serve(listener, app) |
| 967 | .with_graceful_shutdown(async { |
| 968 | let _ = shutdown_receiver.await; |
| 969 | }) |
| 970 | .await |
| 971 | .expect("serve local probe request"); |
| 972 | }); |
| 973 | }); |
| 974 | let base_url = ready_receiver |
| 975 | .recv_timeout(Duration::from_secs(10)) |
| 976 | .expect("local probe server must start"); |
| 977 | Self { |
| 978 | base_url, |
| 979 | requests, |
| 980 | shutdown: Some(shutdown), |
| 981 | owner: Some(owner), |
| 982 | } |
| 983 | } |
| 984 | |
| 985 | fn base_url(&self) -> String { |
| 986 | self.base_url.clone() |
| 987 | } |
| 988 | |
| 989 | fn received_requests(&self) -> Vec<HeaderMap> { |
| 990 | self.requests |
| 991 | .lock() |
| 992 | .expect("local probe request lock") |
| 993 | .clone() |
| 994 | } |
| 995 | } |
| 996 | |
| 997 | impl Drop for CompletionServer { |
| 998 | fn drop(&mut self) { |
| 999 | if let Some(shutdown) = self.shutdown.take() { |
| 1000 | let _ = shutdown.send(()); |
| 1001 | } |
| 1002 | if let Some(owner) = self.owner.take() { |
| 1003 | let result = owner.join(); |
| 1004 | if !thread::panicking() { |
| 1005 | result.expect("stop local probe server"); |
| 1006 | } |
| 1007 | } |
| 1008 | } |
| 1009 | } |
| 1010 | |
| 1011 | /// A rustup shim may initialize its own toolchain state below `$HOME` when |
| 1012 | /// `doctor` asks `rustc --version`. Preserve an already-configured toolchain |
| 1013 | /// root so this test isolates Codewhale's own state contract. |
| 1014 | fn preserve_host_rustup_home(command: &mut Command) { |
| 1015 | let rustup_home = std::env::var_os("RUSTUP_HOME") |
| 1016 | .map(PathBuf::from) |
| 1017 | .or_else(|| { |
| 1018 | std::env::var_os("HOME") |
| 1019 | .map(PathBuf::from) |
| 1020 | .map(|home| home.join(".rustup")) |
| 1021 | .filter(|path| path.is_dir()) |
| 1022 | }); |
| 1023 | if let Some(rustup_home) = rustup_home { |
| 1024 | command.env("RUSTUP_HOME", rustup_home); |
| 1025 | } |
| 1026 | } |
| 1027 | |
| 1028 | /// `env_clear` is part of these tests' credential-isolation boundary, but a |
| 1029 | /// Windows child still needs the non-secret OS root variables used to locate |
| 1030 | /// platform networking components. Without them a reqwest client can fail its |
| 1031 | /// loopback connection before the local fixture ever receives a request. |
| 1032 | fn preserve_host_platform_runtime(_command: &mut Command) { |
| 1033 | #[cfg(windows)] |
| 1034 | for name in ["SystemRoot", "WINDIR"] { |
| 1035 | if let Some(value) = std::env::var_os(name) { |
| 1036 | _command.env(name, value); |
| 1037 | } |
| 1038 | } |
| 1039 | } |
| 1040 |