| 1 | //! `model resolve` must report the route the runtime would actually take. |
| 2 | //! |
| 3 | //! Regression coverage for #4832, where a Z.ai config reported |
| 4 | //! `provider: deepseek` because the subcommand read only the CLI flags and |
| 5 | //! never consulted the resolved runtime. A diagnostic that confidently |
| 6 | //! reports the wrong provider is worse than one that reports nothing, so |
| 7 | //! every provider is asserted here rather than DeepSeek alone. |
| 8 | |
| 9 | use std::collections::BTreeMap; |
| 10 | use std::fs; |
| 11 | use std::path::PathBuf; |
| 12 | use std::process::Command; |
| 13 | |
| 14 | use tempfile::TempDir; |
| 15 | |
| 16 | /// Run `model resolve` against a sealed HOME containing `config`. |
| 17 | /// |
| 18 | /// `env_clear` plus a temporary HOME keeps this off the real |
| 19 | /// `~/.codewhale/config.toml`; the suite has written to real user state before |
| 20 | /// (#4831) and this test must never be the one that does it again. |
| 21 | fn resolve_with_config(config: &str, args: &[&str]) -> BTreeMap<String, String> { |
| 22 | let fixture = TempDir::new().expect("fixture root"); |
| 23 | let home = fixture.path().join("sealed-home"); |
| 24 | fs::create_dir_all(home.join(".codewhale")).expect("sealed config dir"); |
| 25 | fs::write(home.join(".codewhale").join("config.toml"), config).expect("seed config"); |
| 26 | |
| 27 | let mut command = Command::new(codewhale_binary()); |
| 28 | command.arg("model").arg("resolve").args(args); |
| 29 | let output = command |
| 30 | .env_clear() |
| 31 | .env("HOME", &home) |
| 32 | .env("USERPROFILE", &home) |
| 33 | .env("CODEWHALE_HOME", home.join(".codewhale")) |
| 34 | .env("CODEWHALE_SECRET_BACKEND", "file") |
| 35 | .output() |
| 36 | .expect("run model resolve"); |
| 37 | |
| 38 | assert!( |
| 39 | output.status.success(), |
| 40 | "model resolve {args:?} failed\nstdout:\n{}\nstderr:\n{}", |
| 41 | String::from_utf8_lossy(&output.stdout), |
| 42 | String::from_utf8_lossy(&output.stderr) |
| 43 | ); |
| 44 | |
| 45 | String::from_utf8_lossy(&output.stdout) |
| 46 | .lines() |
| 47 | .filter_map(|line| line.split_once(": ")) |
| 48 | .map(|(key, value)| (key.trim().to_string(), value.trim().to_string())) |
| 49 | .collect() |
| 50 | } |
| 51 | |
| 52 | #[test] |
| 53 | fn resolve_reports_the_configured_provider_not_a_deepseek_fallback() { |
| 54 | let report = resolve_with_config( |
| 55 | "provider = \"zai\"\n\n[providers.zai]\napi_key = \"k\"\n", |
| 56 | &[], |
| 57 | ); |
| 58 | |
| 59 | assert_eq!( |
| 60 | report.get("provider").map(String::as_str), |
| 61 | Some("zai"), |
| 62 | "configured provider must survive to the diagnostic: {report:?}" |
| 63 | ); |
| 64 | assert_eq!( |
| 65 | report.get("provider_source").map(String::as_str), |
| 66 | Some("config"), |
| 67 | "provenance must name the config file: {report:?}" |
| 68 | ); |
| 69 | } |
| 70 | |
| 71 | #[test] |
| 72 | fn resolve_reports_a_provider_scoped_model_as_explicitly_configured() { |
| 73 | let report = resolve_with_config( |
| 74 | "provider = \"moonshot\"\n\n[providers.moonshot]\napi_key = \"k\"\nmodel = \"kimi-k3-turbo\"\n", |
| 75 | &[], |
| 76 | ); |
| 77 | |
| 78 | assert_eq!(report.get("provider").map(String::as_str), Some("moonshot")); |
| 79 | assert_eq!( |
| 80 | report.get("requested").map(String::as_str), |
| 81 | Some("kimi-k3-turbo"), |
| 82 | "a configured model is a request, not a fallback: {report:?}" |
| 83 | ); |
| 84 | assert_eq!( |
| 85 | report.get("used_fallback").map(String::as_str), |
| 86 | Some("false"), |
| 87 | "{report:?}" |
| 88 | ); |
| 89 | assert_eq!( |
| 90 | report.get("model_source").map(String::as_str), |
| 91 | Some("config [providers.*].model"), |
| 92 | "{report:?}" |
| 93 | ); |
| 94 | let base = codewhale_config::ProviderKind::Zai |
| 95 | .provider() |
| 96 | .default_base_url(); |
| 97 | let configured = format!( |
| 98 | "provider = 'zai'\n[providers.zai]\napi_key = 'fixture-only'\nmodel = 'GLM-5.2'\n[[custom_models]]\nprovider = 'zai'\nbase_url = '{base}'\nid = 'operator-only-fixture'\nlimit = {{ context = 96000, output = 8000 }}\n" |
| 99 | ); |
| 100 | let report = resolve_with_config(&configured, &["operator-only-fixture"]); |
| 101 | assert_eq!( |
| 102 | report.get("resolved").map(String::as_str), |
| 103 | Some("operator-only-fixture") |
| 104 | ); |
| 105 | assert_eq!(report.get("provider").map(String::as_str), Some("zai")); |
| 106 | assert_eq!( |
| 107 | report.get("used_fallback").map(String::as_str), |
| 108 | Some("false") |
| 109 | ); |
| 110 | } |
| 111 | |
| 112 | #[test] |
| 113 | fn resolve_admits_when_nothing_was_configured() { |
| 114 | // The honest answer to "what did the user ask for" is "nothing". The |
| 115 | // built-in default may still be shown, but it must be labelled as ours. |
| 116 | let report = resolve_with_config("", &[]); |
| 117 | |
| 118 | assert_eq!( |
| 119 | report.get("requested").map(String::as_str), |
| 120 | Some(""), |
| 121 | "an unconfigured model must not be presented as a request: {report:?}" |
| 122 | ); |
| 123 | assert_eq!( |
| 124 | report.get("used_fallback").map(String::as_str), |
| 125 | Some("true"), |
| 126 | "{report:?}" |
| 127 | ); |
| 128 | assert_eq!( |
| 129 | report.get("model_source").map(String::as_str), |
| 130 | Some("provider default"), |
| 131 | "{report:?}" |
| 132 | ); |
| 133 | } |
| 134 | |
| 135 | #[test] |
| 136 | fn a_foreign_model_argument_cannot_switch_the_configured_provider() { |
| 137 | let fixture = TempDir::new().expect("fixture root"); |
| 138 | let home = fixture.path().join("sealed-home"); |
| 139 | fs::create_dir_all(home.join(".codewhale")).expect("sealed config dir"); |
| 140 | fs::write( |
| 141 | home.join(".codewhale").join("config.toml"), |
| 142 | "provider = \"zai\"\n\n[providers.zai]\napi_key = \"k\"\n", |
| 143 | ) |
| 144 | .expect("seed config"); |
| 145 | |
| 146 | let output = Command::new(codewhale_binary()) |
| 147 | .arg("model") |
| 148 | .arg("resolve") |
| 149 | .arg("deepseek-v4-flash") |
| 150 | .env_clear() |
| 151 | .env("HOME", &home) |
| 152 | .env("USERPROFILE", &home) |
| 153 | .env("CODEWHALE_HOME", home.join(".codewhale")) |
| 154 | .env("CODEWHALE_SECRET_BACKEND", "file") |
| 155 | .output() |
| 156 | .expect("run failing model resolve"); |
| 157 | |
| 158 | assert!(!output.status.success()); |
| 159 | let combined = format!( |
| 160 | "{}\n{}", |
| 161 | String::from_utf8_lossy(&output.stdout), |
| 162 | String::from_utf8_lossy(&output.stderr) |
| 163 | ) |
| 164 | .to_ascii_lowercase(); |
| 165 | assert!( |
| 166 | combined.contains("not served by direct provider zai"), |
| 167 | "{combined}" |
| 168 | ); |
| 169 | assert!(!combined.contains("provider: deepseek"), "{combined}"); |
| 170 | } |
| 171 | |
| 172 | #[test] |
| 173 | fn an_explicit_matching_provider_can_resolve_its_model() { |
| 174 | let report = resolve_with_config( |
| 175 | "provider = \"zai\"\n\n[providers.zai]\napi_key = \"k\"\n", |
| 176 | &["deepseek-v4-flash", "--provider", "deepseek"], |
| 177 | ); |
| 178 | |
| 179 | assert_eq!(report.get("provider").map(String::as_str), Some("deepseek")); |
| 180 | assert_eq!( |
| 181 | report.get("requested").map(String::as_str), |
| 182 | Some("deepseek-v4-flash") |
| 183 | ); |
| 184 | assert_eq!( |
| 185 | report.get("used_fallback").map(String::as_str), |
| 186 | Some("false") |
| 187 | ); |
| 188 | assert_eq!( |
| 189 | report.get("provider_source").map(String::as_str), |
| 190 | Some("--provider") |
| 191 | ); |
| 192 | } |
| 193 | |
| 194 | #[test] |
| 195 | fn unknown_providerless_model_stays_on_configured_provider() { |
| 196 | let report = resolve_with_config( |
| 197 | "provider = \"zai\"\n\n[providers.zai]\napi_key = \"k\"\n", |
| 198 | &["totally-unknown-model"], |
| 199 | ); |
| 200 | |
| 201 | assert_eq!(report.get("provider").map(String::as_str), Some("zai")); |
| 202 | assert_eq!( |
| 203 | report.get("resolved").map(String::as_str), |
| 204 | Some("totally-unknown-model") |
| 205 | ); |
| 206 | assert_eq!( |
| 207 | report.get("used_fallback").map(String::as_str), |
| 208 | Some("false") |
| 209 | ); |
| 210 | } |
| 211 | |
| 212 | #[test] |
| 213 | fn an_explicit_provider_flag_is_reported_as_the_source() { |
| 214 | let report = resolve_with_config( |
| 215 | "provider = \"zai\"\n\n[providers.zai]\napi_key = \"k\"\n", |
| 216 | &["--provider", "moonshot"], |
| 217 | ); |
| 218 | |
| 219 | assert_eq!(report.get("provider").map(String::as_str), Some("moonshot")); |
| 220 | assert_eq!( |
| 221 | report.get("provider_source").map(String::as_str), |
| 222 | Some("--provider"), |
| 223 | "{report:?}" |
| 224 | ); |
| 225 | } |
| 226 | |
| 227 | #[test] |
| 228 | fn explicit_openai_without_a_model_reports_its_own_documented_default() { |
| 229 | let report = resolve_with_config( |
| 230 | "provider = \"zai\"\n\n[providers.zai]\napi_key = \"k\"\nmodel = \"GLM-5.2\"\n", |
| 231 | &["--provider", "openai"], |
| 232 | ); |
| 233 | |
| 234 | assert_eq!(report.get("provider").map(String::as_str), Some("openai")); |
| 235 | assert_eq!( |
| 236 | report.get("resolved").map(String::as_str), |
| 237 | Some("gpt-5.6"), |
| 238 | "an OpenAI query must use OpenAI's documented default, not the first catalog row or the configured Z.ai model: {report:?}" |
| 239 | ); |
| 240 | assert_eq!( |
| 241 | report.get("used_fallback").map(String::as_str), |
| 242 | Some("true"), |
| 243 | "{report:?}" |
| 244 | ); |
| 245 | assert_eq!( |
| 246 | report.get("provider_source").map(String::as_str), |
| 247 | Some("--provider"), |
| 248 | "{report:?}" |
| 249 | ); |
| 250 | assert_eq!( |
| 251 | report.get("model_source").map(String::as_str), |
| 252 | Some("provider default"), |
| 253 | "the overridden Z.ai model provenance must not leak into the OpenAI hypothetical: {report:?}" |
| 254 | ); |
| 255 | } |
| 256 | |
| 257 | /// Run `model resolve` with global flags placed before the subcommand, which |
| 258 | /// is where `--provider` / `--model` actually go. |
| 259 | fn resolve_with_global_flags( |
| 260 | config: &str, |
| 261 | global: &[&str], |
| 262 | args: &[&str], |
| 263 | ) -> BTreeMap<String, String> { |
| 264 | let fixture = TempDir::new().expect("fixture root"); |
| 265 | let home = fixture.path().join("sealed-home"); |
| 266 | fs::create_dir_all(home.join(".codewhale")).expect("sealed config dir"); |
| 267 | fs::write(home.join(".codewhale").join("config.toml"), config).expect("seed config"); |
| 268 | |
| 269 | let mut command = Command::new(codewhale_binary()); |
| 270 | command.args(global).arg("model").arg("resolve").args(args); |
| 271 | let output = command |
| 272 | .env_clear() |
| 273 | .env("HOME", &home) |
| 274 | .env("USERPROFILE", &home) |
| 275 | .env("CODEWHALE_HOME", home.join(".codewhale")) |
| 276 | .env("CODEWHALE_SECRET_BACKEND", "file") |
| 277 | .output() |
| 278 | .expect("run model resolve"); |
| 279 | |
| 280 | assert!( |
| 281 | output.status.success(), |
| 282 | "model resolve {global:?} {args:?} failed\nstdout:\n{}\nstderr:\n{}", |
| 283 | String::from_utf8_lossy(&output.stdout), |
| 284 | String::from_utf8_lossy(&output.stderr) |
| 285 | ); |
| 286 | |
| 287 | String::from_utf8_lossy(&output.stdout) |
| 288 | .lines() |
| 289 | .filter_map(|line| line.split_once(": ")) |
| 290 | .map(|(key, value)| (key.trim().to_string(), value.trim().to_string())) |
| 291 | .collect() |
| 292 | } |
| 293 | |
| 294 | /// v0.9.1 kimi-k3 dogfood report: `codewhale --provider moonshot --model kimi-k3 model resolve` |
| 295 | /// reported `kimi-k2.7-code`. The top-level flags are the route this process |
| 296 | /// is on, not a hypothetical, so the diagnostic has to answer with the runtime |
| 297 | /// resolution instead of re-deriving a registry default and ignoring `--model`. |
| 298 | #[test] |
| 299 | fn top_level_provider_and_model_flags_report_the_runtime_route() { |
| 300 | let report = resolve_with_global_flags( |
| 301 | "provider = \"zai\"\n\n[providers.zai]\napi_key = \"k\"\n", |
| 302 | &["--provider", "moonshot", "--model", "kimi-k3"], |
| 303 | &[], |
| 304 | ); |
| 305 | |
| 306 | assert_eq!(report.get("provider").map(String::as_str), Some("moonshot")); |
| 307 | assert_eq!( |
| 308 | report.get("resolved").map(String::as_str), |
| 309 | Some("kimi-k3"), |
| 310 | "the diagnostic must not contradict the model the run will use: {report:?}" |
| 311 | ); |
| 312 | assert_eq!( |
| 313 | report.get("requested").map(String::as_str), |
| 314 | Some("kimi-k3"), |
| 315 | "{report:?}" |
| 316 | ); |
| 317 | assert_eq!( |
| 318 | report.get("used_fallback").map(String::as_str), |
| 319 | Some("false"), |
| 320 | "{report:?}" |
| 321 | ); |
| 322 | assert_eq!( |
| 323 | report.get("model_source").map(String::as_str), |
| 324 | Some("--model"), |
| 325 | "{report:?}" |
| 326 | ); |
| 327 | } |
| 328 | |
| 329 | /// Moonshot ships `kimi-k3` on the direct platform API and `k3` on the Kimi |
| 330 | /// Code coding-plan API. Both must resolve, and neither may be answered by |
| 331 | /// another provider's identically named model (OpenCode Go also serves a |
| 332 | /// `kimi-k3`). |
| 333 | #[test] |
| 334 | fn moonshot_k3_products_resolve_without_crossing_providers() { |
| 335 | for (model, base_url) in [ |
| 336 | ("kimi-k3", "https://api.moonshot.ai/v1"), |
| 337 | ("k3", "https://api.kimi.com/coding/v1"), |
| 338 | ] { |
| 339 | let config = format!( |
| 340 | "provider = \"moonshot\"\n\n[providers.moonshot]\napi_key = \"k\"\nbase_url = \"{base_url}\"\n" |
| 341 | ); |
| 342 | let report = resolve_with_global_flags(&config, &[], &[model, "--provider", "moonshot"]); |
| 343 | |
| 344 | assert_eq!( |
| 345 | report.get("provider").map(String::as_str), |
| 346 | Some("moonshot"), |
| 347 | "a Moonshot question must not be answered by another provider: {report:?}" |
| 348 | ); |
| 349 | assert_eq!( |
| 350 | report.get("resolved").map(String::as_str), |
| 351 | Some(model), |
| 352 | "{report:?}" |
| 353 | ); |
| 354 | assert_eq!( |
| 355 | report.get("used_fallback").map(String::as_str), |
| 356 | Some("false"), |
| 357 | "{report:?}" |
| 358 | ); |
| 359 | } |
| 360 | } |
| 361 | |
| 362 | #[test] |
| 363 | fn unlisted_model_on_selected_provider_passes_through_without_fallback() { |
| 364 | let report = resolve_with_config( |
| 365 | "provider = \"moonshot\"\n\n[providers.moonshot]\napi_key = \"k\"\n", |
| 366 | &["glm-5.2", "--provider", "moonshot"], |
| 367 | ); |
| 368 | |
| 369 | assert_eq!(report.get("provider").map(String::as_str), Some("moonshot")); |
| 370 | assert_eq!(report.get("resolved").map(String::as_str), Some("glm-5.2")); |
| 371 | assert_eq!( |
| 372 | report.get("used_fallback").map(String::as_str), |
| 373 | Some("false") |
| 374 | ); |
| 375 | } |
| 376 | |
| 377 | #[test] |
| 378 | fn a_new_glm_sibling_resolves_on_zai_and_passes_through_on_moonshot() { |
| 379 | let served = resolve_with_global_flags( |
| 380 | "provider = \"zai\"\n\n[providers.zai]\napi_key = \"k\"\n", |
| 381 | &[], |
| 382 | &["glm-5.3", "--provider", "zai"], |
| 383 | ); |
| 384 | |
| 385 | assert_eq!(served.get("provider").map(String::as_str), Some("zai")); |
| 386 | assert_eq!( |
| 387 | served.get("resolved").map(String::as_str), |
| 388 | Some("GLM-5.3"), |
| 389 | "a catalogued model must resolve to itself, not to the provider default: {served:?}" |
| 390 | ); |
| 391 | assert_eq!( |
| 392 | served.get("used_fallback").map(String::as_str), |
| 393 | Some("false"), |
| 394 | "a model the provider serves must not be reported as a fallback: {served:?}" |
| 395 | ); |
| 396 | |
| 397 | let passthrough = resolve_with_global_flags( |
| 398 | "provider = \"moonshot\"\n\n[providers.moonshot]\napi_key = \"k\"\n", |
| 399 | &[], |
| 400 | &["glm-5.3", "--provider", "moonshot"], |
| 401 | ); |
| 402 | |
| 403 | assert_eq!( |
| 404 | passthrough.get("provider").map(String::as_str), |
| 405 | Some("moonshot") |
| 406 | ); |
| 407 | assert_eq!( |
| 408 | passthrough.get("resolved").map(String::as_str), |
| 409 | Some("glm-5.3") |
| 410 | ); |
| 411 | assert_eq!( |
| 412 | passthrough.get("used_fallback").map(String::as_str), |
| 413 | Some("false") |
| 414 | ); |
| 415 | } |
| 416 | |
| 417 | /// The OpenRouter sibling carries a different wire id (`z-ai/glm-5.3`) than the |
| 418 | /// direct Z.ai row (`GLM-5.3`), so the bare family alias has to be rewritten |
| 419 | /// per provider rather than passed through. This pins the OpenRouter half of |
| 420 | /// that rewrite, which the Z.ai case above cannot observe, and pins that adding |
| 421 | /// the sibling left the OpenRouter default alone. |
| 422 | #[test] |
| 423 | fn the_openrouter_glm_sibling_resolves_to_its_own_gateway_wire_id() { |
| 424 | let served = resolve_with_global_flags( |
| 425 | "provider = \"openrouter\"\n\n[providers.openrouter]\napi_key = \"k\"\n", |
| 426 | &[], |
| 427 | &["glm-5.3", "--provider", "openrouter"], |
| 428 | ); |
| 429 | |
| 430 | assert_eq!( |
| 431 | served.get("provider").map(String::as_str), |
| 432 | Some("openrouter") |
| 433 | ); |
| 434 | assert_eq!( |
| 435 | served.get("resolved").map(String::as_str), |
| 436 | Some("z-ai/glm-5.3"), |
| 437 | "the bare alias must be rewritten to the OpenRouter wire id, not passed through: {served:?}" |
| 438 | ); |
| 439 | assert_eq!( |
| 440 | served.get("used_fallback").map(String::as_str), |
| 441 | Some("false"), |
| 442 | "a gateway row the provider serves must not be reported as a fallback: {served:?}" |
| 443 | ); |
| 444 | |
| 445 | let default_route = resolve_with_config( |
| 446 | "provider = \"openrouter\"\n\n[providers.openrouter]\napi_key = \"k\"\n", |
| 447 | &[], |
| 448 | ); |
| 449 | let resolved = default_route |
| 450 | .get("resolved") |
| 451 | .map(String::as_str) |
| 452 | .unwrap_or_default(); |
| 453 | assert!( |
| 454 | !resolved.to_ascii_lowercase().contains("glm"), |
| 455 | "adding a GLM sibling must not make GLM the OpenRouter default: {default_route:?}" |
| 456 | ); |
| 457 | } |
| 458 | |
| 459 | /// A Z.ai config that names no model lands on the deliberate default, |
| 460 | /// `GLM-5.3`, with `provider default` provenance. This is the surface where a |
| 461 | /// default move would otherwise change silently under a user. |
| 462 | #[test] |
| 463 | fn zai_default_route_resolves_to_glm_5_3_with_provider_default_provenance() { |
| 464 | let report = resolve_with_config( |
| 465 | "provider = \"zai\"\n\n[providers.zai]\napi_key = \"k\"\n", |
| 466 | &[], |
| 467 | ); |
| 468 | |
| 469 | assert_eq!( |
| 470 | report.get("resolved").map(String::as_str), |
| 471 | Some("GLM-5.3"), |
| 472 | "the Z.ai default is GLM-5.3: {report:?}" |
| 473 | ); |
| 474 | assert_eq!( |
| 475 | report.get("model_source").map(String::as_str), |
| 476 | Some("provider default"), |
| 477 | "{report:?}" |
| 478 | ); |
| 479 | } |
| 480 | |
| 481 | /// An explicit `GLM-5.2` selection keeps its own id after the default moved |
| 482 | /// to `GLM-5.3`: only the default changed, never a user's saved route. |
| 483 | #[test] |
| 484 | fn explicit_glm_5_2_selection_survives_the_default_move() { |
| 485 | let report = resolve_with_config( |
| 486 | "provider = \"zai\"\n\n[providers.zai]\napi_key = \"k\"\nmodel = \"GLM-5.2\"\n", |
| 487 | &[], |
| 488 | ); |
| 489 | |
| 490 | assert_eq!( |
| 491 | report.get("resolved").map(String::as_str), |
| 492 | Some("GLM-5.2"), |
| 493 | "an explicit GLM-5.2 route must not be upgraded: {report:?}" |
| 494 | ); |
| 495 | assert_ne!( |
| 496 | report.get("model_source").map(String::as_str), |
| 497 | Some("provider default"), |
| 498 | "{report:?}" |
| 499 | ); |
| 500 | } |
| 501 | |
| 502 | fn codewhale_binary() -> PathBuf { |
| 503 | if let Some(path) = std::env::var_os("CARGO_BIN_EXE_codewhale").filter(|v| !v.is_empty()) { |
| 504 | return PathBuf::from(path); |
| 505 | } |
| 506 | if let Some(path) = option_env!("CARGO_BIN_EXE_codewhale") { |
| 507 | return PathBuf::from(path); |
| 508 | } |
| 509 | |
| 510 | let mut path = std::env::current_exe().expect("current test executable path"); |
| 511 | path.pop(); |
| 512 | if path.ends_with("deps") { |
| 513 | path.pop(); |
| 514 | } |
| 515 | path.push(format!("codewhale{}", std::env::consts::EXE_SUFFIX)); |
| 516 | path |
| 517 | } |
| 518 |