| 1 | use codewhale_config::ProviderKind; |
| 2 | use codewhale_engine::route_preferences::persist_provider_selection; |
| 3 | |
| 4 | use codewhale_runtime::config_persistence::*; |
| 5 | |
| 6 | use codewhale_runtime::config_persistence::test_support::*; |
| 7 | use std::fs; |
| 8 | |
| 9 | #[test] |
| 10 | fn route_migration_is_atomic_preserves_conflicts_and_ignores_scoped_settings() { |
| 11 | use crate::test_support::{EnvVarGuard, lock_test_env}; |
| 12 | let _lock = lock_test_env(); |
| 13 | let home = tempfile::tempdir().unwrap(); |
| 14 | let project = tempfile::tempdir().unwrap(); |
| 15 | let _home = EnvVarGuard::set("CODEWHALE_HOME", home.path()); |
| 16 | let _scope = EnvVarGuard::set("CODEWHALE_CONFIG_PATH", project.path().join("config.toml")); |
| 17 | let path = home.path().join("config.toml"); |
| 18 | let source = "# keep this comment\nprovider = 'deepseek'\n[providers.zai]\nmodel = 'GLM-5.2'\n[providers.TeamA]\nkind = 'openai-compatible'\nbase_url = 'https://upper.example.test/v1'\nmodel = 'Old-Upper'\n[providers.teama]\nkind = 'openai-compatible'\nbase_url = 'https://lower.example.test/v1'\nmodel = 'Old-Lower'\n"; |
| 19 | let legacy = "default_provider = 'zai'\n[provider_models]\nzai = 'GLM-5.3'\nTeamA = 'New-Upper'\nteama = 'New-Lower'\n"; |
| 20 | fs::write(&path, source).unwrap(); |
| 21 | fs::write(home.path().join("settings.toml"), legacy).unwrap(); |
| 22 | fs::write( |
| 23 | project.path().join("settings.toml"), |
| 24 | "default_provider = 'openai'\n[provider_models]\nzai = 'wrong-scoped-model'\n", |
| 25 | ) |
| 26 | .unwrap(); |
| 27 | |
| 28 | let admitted_source = format!( |
| 29 | "{source}\n[providers.Missing]\nkind = 'openai-compatible'\nbase_url = 'https://missing.example.test/v1'\n" |
| 30 | ); |
| 31 | let captured_missing = codewhale_runtime::config::parse_config_base(&admitted_source) |
| 32 | .unwrap() |
| 33 | .resolve_provider_pin_identity("Missing") |
| 34 | .unwrap(); |
| 35 | let error = persist_provider_selection(Some(&path), &captured_missing, Some("new-model")); |
| 36 | assert!(error.is_err()); |
| 37 | assert_eq!( |
| 38 | fs::read_to_string(&path).unwrap(), |
| 39 | source, |
| 40 | "failed save must not commit migration separately" |
| 41 | ); |
| 42 | |
| 43 | persist_provider_selection( |
| 44 | Some(&path), |
| 45 | &selection_identity(&path, "deepseek"), |
| 46 | Some("deepseek-v4-pro"), |
| 47 | ) |
| 48 | .unwrap(); |
| 49 | let body = fs::read_to_string(&path).unwrap(); |
| 50 | let doc: toml::Value = toml::from_str(&body).unwrap(); |
| 51 | assert!(body.contains("# keep this comment")); |
| 52 | assert_eq!(doc["route_preferences_version"].as_integer(), Some(1)); |
| 53 | assert_eq!(doc["provider"].as_str(), Some("deepseek")); |
| 54 | assert_eq!( |
| 55 | doc["providers"]["deepseek"]["model"].as_str(), |
| 56 | Some("deepseek-v4-pro") |
| 57 | ); |
| 58 | assert_eq!(doc["providers"]["zai"]["model"].as_str(), Some("GLM-5.3")); |
| 59 | assert_eq!( |
| 60 | doc["providers"]["TeamA"]["model"].as_str(), |
| 61 | Some("New-Upper") |
| 62 | ); |
| 63 | assert_eq!( |
| 64 | doc["providers"]["teama"]["model"].as_str(), |
| 65 | Some("New-Lower") |
| 66 | ); |
| 67 | assert_eq!( |
| 68 | doc["providers"]["TeamA"]["base_url"].as_str(), |
| 69 | Some("https://upper.example.test/v1") |
| 70 | ); |
| 71 | assert_eq!( |
| 72 | doc["route_preferences_migration"]["previous_models"]["zai"].as_str(), |
| 73 | Some("GLM-5.2") |
| 74 | ); |
| 75 | assert_eq!( |
| 76 | fs::read_to_string(home.path().join("settings.toml")).unwrap(), |
| 77 | legacy |
| 78 | ); |
| 79 | |
| 80 | persist_provider_model_key(Some(&path), &selection_identity(&path, "zai"), "GLM-5.1").unwrap(); |
| 81 | let doc: toml::Value = toml::from_str(&fs::read_to_string(&path).unwrap()).unwrap(); |
| 82 | assert_eq!(doc["providers"]["zai"]["model"].as_str(), Some("GLM-5.1")); |
| 83 | assert_eq!( |
| 84 | doc["route_preferences_migration"]["previous_models"]["zai"].as_str(), |
| 85 | Some("GLM-5.2") |
| 86 | ); |
| 87 | } |
| 88 | |
| 89 | #[test] |
| 90 | fn moving_root_default_off_the_root_does_not_resurrect_a_shadowed_legacy_model() { |
| 91 | use crate::test_support::{EnvVarGuard, lock_test_env}; |
| 92 | let _lock = lock_test_env(); |
| 93 | let home = tempfile::tempdir().unwrap(); |
| 94 | let _home = EnvVarGuard::set("CODEWHALE_HOME", home.path()); |
| 95 | let _model_guard = ModelEnvGuard::new(); |
| 96 | let path = home.path().join("config.toml"); |
| 97 | // `default_text_model` shadows the legacy root `model` on every route. |
| 98 | // Moving the alias onto openai's leaf must not un-shadow the legacy |
| 99 | // value for the pass-through incoming route. |
| 100 | fs::write( |
| 101 | &path, |
| 102 | "route_preferences_version = 1\nprovider = 'openai'\nmodel = 'deepseek-v4-flash'\ndefault_text_model = 'fixture-model'\n", |
| 103 | ) |
| 104 | .unwrap(); |
| 105 | persist_provider_selection(Some(&path), &selection_identity(&path, "openrouter"), None) |
| 106 | .unwrap(); |
| 107 | let doc: toml::Value = toml::from_str(&fs::read_to_string(&path).unwrap()).unwrap(); |
| 108 | assert!(doc.get("default_text_model").is_none(), "{doc:?}"); |
| 109 | assert!(doc.get("model").is_none(), "{doc:?}"); |
| 110 | assert_eq!( |
| 111 | doc["providers"]["openai"]["model"].as_str(), |
| 112 | Some("fixture-model") |
| 113 | ); |
| 114 | let switched = codewhale_plugins::providers::load_config(Some(path.clone()), None).unwrap(); |
| 115 | assert_eq!( |
| 116 | switched.active_provider_identity().unwrap().provider, |
| 117 | ProviderKind::Openrouter |
| 118 | ); |
| 119 | // OpenRouter normalizes the stale value to `deepseek/deepseek-v4-flash`. |
| 120 | assert!( |
| 121 | !switched.default_model().contains("deepseek-v4-flash"), |
| 122 | "{}", |
| 123 | switched.default_model() |
| 124 | ); |
| 125 | assert_ne!(switched.default_model(), "fixture-model"); |
| 126 | |
| 127 | persist_provider_selection(Some(&path), &selection_identity(&path, "openai"), None).unwrap(); |
| 128 | let returned = codewhale_plugins::providers::load_config(Some(path.clone()), None).unwrap(); |
| 129 | assert_eq!( |
| 130 | returned.active_provider_identity().unwrap().provider, |
| 131 | ProviderKind::Openai |
| 132 | ); |
| 133 | assert_eq!(returned.default_model(), "fixture-model"); |
| 134 | } |
| 135 | |
| 136 | #[test] |
| 137 | fn switching_provider_away_and_back_preserves_every_route_selection() { |
| 138 | use crate::test_support::{EnvVarGuard, lock_test_env}; |
| 139 | let _lock = lock_test_env(); |
| 140 | let home = tempfile::tempdir().unwrap(); |
| 141 | let _home = EnvVarGuard::set("CODEWHALE_HOME", home.path()); |
| 142 | let _model_guard = ModelEnvGuard::new(); |
| 143 | let path = home.path().join("config.toml"); |
| 144 | for root_key in ["default_text_model", "model"] { |
| 145 | for existing in [None, Some("GLM-4.5-Air")] { |
| 146 | let mut source = format!( |
| 147 | "route_preferences_version = 1\nprovider = 'zai'\n{root_key} = 'GLM-4.6'\n" |
| 148 | ); |
| 149 | if let Some(model) = existing { |
| 150 | source.push_str(&format!("[providers.zai]\nmodel = '{model}'\n")); |
| 151 | } |
| 152 | fs::write(&path, source).unwrap(); |
| 153 | persist_provider_selection( |
| 154 | Some(&path), |
| 155 | &selection_identity(&path, "deepseek"), |
| 156 | Some("deepseek-v4-pro"), |
| 157 | ) |
| 158 | .unwrap(); |
| 159 | let doc: toml::Value = toml::from_str(&fs::read_to_string(&path).unwrap()).unwrap(); |
| 160 | // The outgoing route was resolving the root alias only when it |
| 161 | // had no leaf; then the choice moves onto that leaf. Otherwise |
| 162 | // the alias is inert saved state and stays. Neither case may |
| 163 | // delete it. |
| 164 | if existing.is_none() { |
| 165 | assert!(doc.get(root_key).is_none(), "{doc:?}"); |
| 166 | assert_eq!( |
| 167 | doc["providers"]["zai"]["model"].as_str(), |
| 168 | Some("GLM-4.6"), |
| 169 | "the outgoing route's choice must follow it onto its leaf" |
| 170 | ); |
| 171 | } else { |
| 172 | assert_eq!( |
| 173 | doc[root_key].as_str(), |
| 174 | Some("GLM-4.6"), |
| 175 | "an inert root fallback must survive the switch" |
| 176 | ); |
| 177 | } |
| 178 | assert_eq!( |
| 179 | doc["providers"]["deepseek"]["model"].as_str(), |
| 180 | Some("deepseek-v4-pro") |
| 181 | ); |
| 182 | let restored = codewhale_plugins::providers::load_config(Some(path.clone()), None) |
| 183 | .expect("saved provider switch must remain loadable"); |
| 184 | assert_eq!( |
| 185 | restored.active_provider_identity().unwrap().provider, |
| 186 | ProviderKind::Deepseek |
| 187 | ); |
| 188 | assert_eq!(restored.default_model(), "deepseek-v4-pro"); |
| 189 | |
| 190 | // Switching back must find the choice this route had, whether |
| 191 | // it was stored on its own leaf or in the root fallback. |
| 192 | persist_provider_selection(Some(&path), &selection_identity(&path, "zai"), None) |
| 193 | .unwrap(); |
| 194 | let returned = codewhale_plugins::providers::load_config(Some(path.clone()), None) |
| 195 | .expect("switching back must remain loadable"); |
| 196 | assert_eq!( |
| 197 | returned.active_provider_identity().unwrap().provider, |
| 198 | ProviderKind::Zai |
| 199 | ); |
| 200 | assert_eq!( |
| 201 | returned.default_model(), |
| 202 | existing.unwrap_or("GLM-4.6"), |
| 203 | "switching away and back must restore the outgoing selection" |
| 204 | ); |
| 205 | } |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | #[test] |
| 210 | fn a_bare_switch_relocates_a_root_alias_the_incoming_route_cannot_serve() { |
| 211 | use crate::test_support::{EnvVarGuard, lock_test_env}; |
| 212 | let _lock = lock_test_env(); |
| 213 | let home = tempfile::tempdir().unwrap(); |
| 214 | let _home = EnvVarGuard::set("CODEWHALE_HOME", home.path()); |
| 215 | let _model_guard = ModelEnvGuard::new(); |
| 216 | let path = home.path().join("config.toml"); |
| 217 | // No model argument, and the outgoing route keeps its only model in the |
| 218 | // root fallback. Official DeepSeek cannot serve that id, so leaving it |
| 219 | // behind would commit a switch whose config no longer loads. |
| 220 | fs::write( |
| 221 | &path, |
| 222 | "route_preferences_version = 1\nprovider = 'volcengine'\ndefault_text_model = 'ark-private-id'\n", |
| 223 | ) |
| 224 | .unwrap(); |
| 225 | persist_provider_selection(Some(&path), &selection_identity(&path, "deepseek"), None).unwrap(); |
| 226 | |
| 227 | let doc: toml::Value = toml::from_str(&fs::read_to_string(&path).unwrap()).unwrap(); |
| 228 | assert!( |
| 229 | doc.get("default_text_model").is_none(), |
| 230 | "an alias the incoming route cannot serve must not be left behind" |
| 231 | ); |
| 232 | assert_eq!( |
| 233 | doc["providers"]["volcengine"]["model"].as_str(), |
| 234 | Some("ark-private-id"), |
| 235 | "the displaced choice moves onto its own route's leaf, it is not dropped" |
| 236 | ); |
| 237 | codewhale_plugins::providers::load_config(Some(path.clone()), None) |
| 238 | .expect("a bare provider switch must remain loadable"); |
| 239 | |
| 240 | persist_provider_selection(Some(&path), &selection_identity(&path, "volcengine"), None) |
| 241 | .unwrap(); |
| 242 | let returned = codewhale_plugins::providers::load_config(Some(path.clone()), None) |
| 243 | .expect("switching back must remain loadable"); |
| 244 | assert_eq!( |
| 245 | returned.active_provider_identity().unwrap().provider, |
| 246 | ProviderKind::Volcengine |
| 247 | ); |
| 248 | assert_eq!(returned.default_model(), "ark-private-id"); |
| 249 | } |
| 250 | |
| 251 | #[test] |
| 252 | fn bare_switch_from_legacy_custom_commits_a_loadable_config() { |
| 253 | use crate::test_support::{EnvVarGuard, lock_test_env}; |
| 254 | let _lock = lock_test_env(); |
| 255 | let home = tempfile::tempdir().unwrap(); |
| 256 | let _home = EnvVarGuard::set("CODEWHALE_HOME", home.path()); |
| 257 | let _model_guard = ModelEnvGuard::new(); |
| 258 | let path = home.path().join("config.toml"); |
| 259 | let original = "route_preferences_version = 1\nprovider = 'custom'\nbase_url = 'https://proxy.example.test/v1'\ndefault_text_model = 'proxy-wire-id'\n[providers.deepseek]\nbase_url = 'https://api.deepseek.com/beta'\n"; |
| 260 | fs::write(&path, original).unwrap(); |
| 261 | codewhale_plugins::providers::load_config(Some(path.clone()), None).unwrap(); |
| 262 | // The write moves the top-level route into `[providers.custom]`, |
| 263 | // model included (#6394), so the custom model is no longer the |
| 264 | // switch's only copy and a bare switch commits a loadable config. |
| 265 | persist_provider_selection(Some(&path), &selection_identity(&path, "deepseek"), None).unwrap(); |
| 266 | let switched = codewhale_plugins::providers::load_config(Some(path.clone()), None) |
| 267 | .expect("a bare provider switch must remain loadable"); |
| 268 | assert_eq!( |
| 269 | switched.active_provider_identity().unwrap().provider, |
| 270 | ProviderKind::Deepseek |
| 271 | ); |
| 272 | let doc: toml::Value = toml::from_str(&fs::read_to_string(&path).unwrap()).unwrap(); |
| 273 | assert_eq!( |
| 274 | doc["providers"]["custom"]["model"].as_str(), |
| 275 | Some("proxy-wire-id") |
| 276 | ); |
| 277 | assert_eq!( |
| 278 | doc["providers"]["custom"]["base_url"].as_str(), |
| 279 | Some("https://proxy.example.test/v1") |
| 280 | ); |
| 281 | } |
| 282 | |
| 283 | #[test] |
| 284 | fn an_unnamed_custom_route_keeps_its_root_model_across_a_switch() { |
| 285 | use crate::test_support::{EnvVarGuard, lock_test_env}; |
| 286 | let _lock = lock_test_env(); |
| 287 | let home = tempfile::tempdir().unwrap(); |
| 288 | let _home = EnvVarGuard::set("CODEWHALE_HOME", home.path()); |
| 289 | let _model_guard = ModelEnvGuard::new(); |
| 290 | let path = home.path().join("config.toml"); |
| 291 | // The one-way root migration copies the model to the exact custom |
| 292 | // leaf. A switch with its own model retains the historical root alias |
| 293 | // too; both choices survive without granting missing-id table authority. |
| 294 | fs::write( |
| 295 | &path, |
| 296 | "route_preferences_version = 1\nprovider = 'custom'\nbase_url = 'https://proxy.example.test/v1'\ndefault_text_model = 'proxy-wire-id'\n", |
| 297 | ) |
| 298 | .unwrap(); |
| 299 | persist_provider_selection( |
| 300 | Some(&path), |
| 301 | &selection_identity(&path, "deepseek"), |
| 302 | Some("deepseek-v4-pro"), |
| 303 | ) |
| 304 | .unwrap(); |
| 305 | let doc: toml::Value = toml::from_str(&fs::read_to_string(&path).unwrap()).unwrap(); |
| 306 | assert_eq!( |
| 307 | doc["default_text_model"].as_str(), |
| 308 | Some("proxy-wire-id"), |
| 309 | "the custom route's only saved model must survive the switch" |
| 310 | ); |
| 311 | let restored = codewhale_plugins::providers::load_config(Some(path.clone()), None) |
| 312 | .expect("the switched config must remain loadable"); |
| 313 | assert_eq!(restored.default_model(), "deepseek-v4-pro"); |
| 314 | |
| 315 | // Nothing can re-select an unnamed custom route by identity once the |
| 316 | // selector names another provider, so restoring it is a `provider` |
| 317 | // edit. The model has to still be there when it happens. |
| 318 | let returning = fs::read_to_string(&path) |
| 319 | .unwrap() |
| 320 | .replace("provider = 'deepseek'", "provider = 'custom'") |
| 321 | .replace("provider = \"deepseek\"", "provider = 'custom'"); |
| 322 | fs::write(&path, returning).unwrap(); |
| 323 | let returned = codewhale_plugins::providers::load_config(Some(path.clone()), None) |
| 324 | .expect("returning to the custom route must remain loadable"); |
| 325 | assert_eq!(returned.default_model(), "proxy-wire-id"); |
| 326 | } |
| 327 | |
| 328 | #[test] |
| 329 | fn canonical_model_writer_targets_the_literal_custom_table_and_exact_named_ids() { |
| 330 | use crate::test_support::{EnvVarGuard, lock_test_env}; |
| 331 | let _lock = lock_test_env(); |
| 332 | let home = tempfile::tempdir().unwrap(); |
| 333 | let _home = EnvVarGuard::set("CODEWHALE_HOME", home.path()); |
| 334 | let path = home.path().join("config.toml"); |
| 335 | fs::write(&path, "route_preferences_version = 1\nprovider = 'custom'\nbase_url = 'https://legacy.example.test/v1'\ndefault_text_model = 'old-wire-id'\n").unwrap(); |
| 336 | persist_provider_selection( |
| 337 | Some(&path), |
| 338 | &selection_identity(&path, "custom"), |
| 339 | Some("Exact-New-ID"), |
| 340 | ) |
| 341 | .unwrap(); |
| 342 | // The literal route is the `[providers.custom]` table since #6394. |
| 343 | let doc: toml::Value = toml::from_str(&fs::read_to_string(&path).unwrap()).unwrap(); |
| 344 | assert_eq!( |
| 345 | doc["providers"]["custom"]["model"].as_str(), |
| 346 | Some("Exact-New-ID") |
| 347 | ); |
| 348 | assert!(doc.get("base_url").is_none()); |
| 349 | |
| 350 | fs::write(&path, "route_preferences_version = 1\nprovider = 'Team.A'\n[providers.'Team.A']\nkind = 'openai-compatible'\nbase_url = 'https://named.example.test/v1'\nmodel = 'old'\n").unwrap(); |
| 351 | persist_provider_selection( |
| 352 | Some(&path), |
| 353 | &selection_identity(&path, "Team.A"), |
| 354 | Some("Exact-Named-ID"), |
| 355 | ) |
| 356 | .unwrap(); |
| 357 | let doc: toml::Value = toml::from_str(&fs::read_to_string(&path).unwrap()).unwrap(); |
| 358 | assert_eq!( |
| 359 | doc["providers"]["Team.A"]["model"].as_str(), |
| 360 | Some("Exact-Named-ID") |
| 361 | ); |
| 362 | let admitted_lower = codewhale_runtime::config::parse_config_base( |
| 363 | "provider = 'team.a'\n[providers.'team.a']\nkind = 'openai-compatible'\nbase_url = 'https://lower.example.test/v1'\n" |
| 364 | ).unwrap().resolve_provider_pin_identity("team.a").unwrap(); |
| 365 | let before_wrong_case = fs::read_to_string(&path).unwrap(); |
| 366 | assert!(persist_provider_selection(Some(&path), &admitted_lower, Some("wrong")).is_err()); |
| 367 | assert_eq!(fs::read_to_string(&path).unwrap(), before_wrong_case); |
| 368 | persist_provider_model_key( |
| 369 | Some(&path), |
| 370 | &selection_identity(&path, "deepseek-cn"), |
| 371 | "deepseek-v4-pro", |
| 372 | ) |
| 373 | .unwrap(); |
| 374 | let doc: toml::Value = toml::from_str(&fs::read_to_string(&path).unwrap()).unwrap(); |
| 375 | assert_eq!( |
| 376 | doc["providers"]["deepseek_cn"]["model"].as_str(), |
| 377 | Some("deepseek-v4-pro") |
| 378 | ); |
| 379 | } |
| 380 | |
| 381 | #[test] |
| 382 | fn malformed_legacy_preferences_do_not_partially_commit_a_route_save() { |
| 383 | use crate::test_support::{EnvVarGuard, lock_test_env}; |
| 384 | let _lock = lock_test_env(); |
| 385 | let home = tempfile::tempdir().unwrap(); |
| 386 | let _home = EnvVarGuard::set("CODEWHALE_HOME", home.path()); |
| 387 | let path = home.path().join("config.toml"); |
| 388 | let source = "provider = 'zai'\n[providers.zai]\nmodel = 'GLM-5.2'\n"; |
| 389 | fs::write(&path, source).unwrap(); |
| 390 | let settings_path = home.path().join("settings.toml"); |
| 391 | let malformed = "default_provider = [\n"; |
| 392 | fs::write(&settings_path, malformed).unwrap(); |
| 393 | let error = persist_provider_selection( |
| 394 | Some(&path), |
| 395 | &selection_identity(&path, "zai"), |
| 396 | Some("GLM-5.3"), |
| 397 | ) |
| 398 | .expect_err("unreadable legacy preferences must block the entire save"); |
| 399 | assert!(error.to_string().contains("configuration was not changed")); |
| 400 | assert_eq!(fs::read_to_string(&path).unwrap(), source); |
| 401 | assert_eq!(fs::read_to_string(&settings_path).unwrap(), malformed); |
| 402 | } |
| 403 | #[test] |
| 404 | fn fresh_locked_root_migration_admits_only_the_captured_table_and_keeps_undo_cas() { |
| 405 | let _lock = crate::test_support::lock_test_env(); |
| 406 | let home = tempfile::tempdir().unwrap(); |
| 407 | let _home = crate::test_support::EnvVarGuard::set("CODEWHALE_HOME", home.path()); |
| 408 | let path = home.path().join("config.toml"); |
| 409 | let original = "route_preferences_version = 1\nprovider = 'custom'\nbase_url = 'http://127.0.0.1:18180/v1'\ndefault_text_model = 'old-model'\n"; |
| 410 | fs::write(&path, original).unwrap(); |
| 411 | let captured = codewhale_runtime::config::parse_config_base(original) |
| 412 | .unwrap() |
| 413 | .active_provider_identity() |
| 414 | .unwrap(); |
| 415 | assert!(captured.persisted_id().is_none()); |
| 416 | let (_, undo) = |
| 417 | persist_provider_selection(Some(&path), &captured, Some("selected-model")).unwrap(); |
| 418 | let saved = fs::read_to_string(&path).unwrap(); |
| 419 | let parsed = codewhale_runtime::config::parse_config_base(&saved).unwrap(); |
| 420 | assert_eq!(parsed.default_model(), "selected-model"); |
| 421 | assert_eq!( |
| 422 | parsed |
| 423 | .provider_config_for(&parsed.active_provider_identity().unwrap()) |
| 424 | .unwrap() |
| 425 | .model |
| 426 | .as_deref(), |
| 427 | Some("selected-model") |
| 428 | ); |
| 429 | let newer = saved.replace("selected-model", "newer-model"); |
| 430 | fs::write(&path, &newer).unwrap(); |
| 431 | assert!(!undo.undo().unwrap()); |
| 432 | assert_eq!(fs::read_to_string(&path).unwrap(), newer); |
| 433 | } |
| 434 | |
| 435 | #[test] |
| 436 | fn locked_root_write_refuses_table_only_profile_conflict_and_changed_generation() { |
| 437 | let _lock = crate::test_support::lock_test_env(); |
| 438 | let home = tempfile::tempdir().unwrap(); |
| 439 | let _home = crate::test_support::EnvVarGuard::set("CODEWHALE_HOME", home.path()); |
| 440 | let path = home.path().join("config.toml"); |
| 441 | let root = "route_preferences_version = 1\nprovider = 'custom'\nbase_url = 'http://127.0.0.1:18180/v1'\ndefault_text_model = 'old-model'\n"; |
| 442 | let captured = codewhale_runtime::config::parse_config_base(root) |
| 443 | .unwrap() |
| 444 | .active_provider_identity() |
| 445 | .unwrap(); |
| 446 | for source in [ |
| 447 | "route_preferences_version = 1\nprovider = 'custom'\n[providers.custom]\nbase_url = 'http://127.0.0.1:18180/v1'\nmodel = 'old-model'\n", |
| 448 | "route_preferences_version = 1\nprovider = 'deepseek'\n[profiles.dev]\nprovider = 'custom'\nbase_url = 'http://127.0.0.1:18180/v1'\ndefault_text_model = 'old-model'\n", |
| 449 | "route_preferences_version = 1\nprovider = 'custom'\nbase_url = 'http://127.0.0.1:18180/v1'\ndefault_text_model = 'old-model'\n[providers.custom]\nbase_url = 'http://127.0.0.1:19191/v1'\n", |
| 450 | "route_preferences_version = 1\nprovider = 'custom'\nbase_url = 'http://127.0.0.1:18180/v1'\ndefault_text_model = 'changed-model'\n", |
| 451 | "route_preferences_version = 1\nprovider = 'custom'\nbase_url = 'http://127.0.0.1:18180/v1'\ndefault_text_model = 'old-model'\napi_key = 'changed-key'\n", |
| 452 | ] { |
| 453 | fs::write(&path, source).unwrap(); |
| 454 | assert!(persist_provider_selection(Some(&path), &captured, Some("must-not-save")).is_err()); |
| 455 | assert_eq!(fs::read_to_string(&path).unwrap(), source); |
| 456 | assert!(persist_provider_model_key(Some(&path), &captured, "must-not-save").is_err()); |
| 457 | assert_eq!(fs::read_to_string(&path).unwrap(), source); |
| 458 | assert!( |
| 459 | persist_route_base_url(Some(&path), &captured, "http://127.0.0.1:22222/v1").is_err() |
| 460 | ); |
| 461 | assert_eq!(fs::read_to_string(&path).unwrap(), source); |
| 462 | } |
| 463 | } |
| 464 |