use crate::brain::agent::service::plan_mode_provider::{
ModeOverride, PlanModeSwap, normalized_override_for, override_for,
};
use crate::config::Config;
use crate::config::types::AgentConfig;
use crate::utils::plan_files::PlanModeState;
const EVERY_STATE: [PlanModeState; 4] = [
PlanModeState::NoPlan,
PlanModeState::PreInitEditing,
PlanModeState::PostInitEditing,
PlanModeState::Active,
];
fn planning(provider: Option<&str>, model: Option<&str>) -> AgentConfig {
AgentConfig {
plan_provider: provider.map(str::to_string),
plan_model: model.map(str::to_string),
..Default::default()
}
}
#[test]
fn an_unconfigured_install_never_overrides() {
let agent = AgentConfig::default();
for state in EVERY_STATE {
assert_eq!(
override_for(state, &agent),
None,
"unset config must not override in {state:?}"
);
}
}
#[test]
fn drafting_uses_the_plan_pair() {
let agent = planning(Some("anthropic"), Some("claude-opus-4-6"));
for state in [
PlanModeState::PreInitEditing,
PlanModeState::PostInitEditing,
] {
assert_eq!(
override_for(state, &agent),
Some(ModeOverride {
provider: Some("anthropic".to_string()),
model: Some("claude-opus-4-6".to_string()),
}),
"{state:?} must use the plan pair"
);
}
}
#[test]
fn a_session_with_no_plan_is_untouched() {
let agent = planning(Some("anthropic"), Some("claude-opus-4-6"));
assert_eq!(override_for(PlanModeState::NoPlan, &agent), None);
}
#[test]
fn a_provider_alone_means_that_providers_default_model() {
let agent = planning(Some("anthropic"), None);
assert_eq!(
override_for(PlanModeState::PostInitEditing, &agent),
Some(ModeOverride {
provider: Some("anthropic".to_string()),
model: None,
})
);
}
#[test]
fn a_model_alone_keeps_the_current_provider() {
let agent = planning(None, Some("claude-opus-4-6"));
assert_eq!(
override_for(PlanModeState::PostInitEditing, &agent),
Some(ModeOverride {
provider: None,
model: Some("claude-opus-4-6".to_string()),
})
);
}
#[test]
fn plan_keys_do_not_leak_into_execution() {
let agent = planning(Some("anthropic"), Some("claude-opus-4-6"));
assert_eq!(override_for(PlanModeState::Active, &agent), None);
}
fn executing(provider: Option<&str>, model: Option<&str>) -> AgentConfig {
AgentConfig {
execute_provider: provider.map(str::to_string),
execute_model: model.map(str::to_string),
..Default::default()
}
}
#[test]
fn executing_uses_the_execute_pair() {
let agent = executing(Some("openrouter"), Some("qwen3.8-max"));
assert_eq!(
override_for(PlanModeState::Active, &agent),
Some(ModeOverride {
provider: Some("openrouter".to_string()),
model: Some("qwen3.8-max".to_string()),
})
);
}
#[test]
fn execute_keys_do_not_leak_into_drafting() {
let agent = executing(Some("openrouter"), Some("qwen3.8-max"));
for state in [
PlanModeState::PreInitEditing,
PlanModeState::PostInitEditing,
] {
assert_eq!(
override_for(state, &agent),
None,
"{state:?} must not use the execute pair"
);
}
}
#[test]
fn drafting_and_executing_route_independently() {
let agent = AgentConfig {
plan_provider: Some("anthropic".to_string()),
plan_model: Some("claude-opus-4-6".to_string()),
execute_provider: Some("openrouter".to_string()),
execute_model: Some("qwen3.8-max".to_string()),
..Default::default()
};
assert_eq!(
override_for(PlanModeState::PostInitEditing, &agent)
.and_then(|o| o.provider)
.as_deref(),
Some("anthropic")
);
assert_eq!(
override_for(PlanModeState::Active, &agent)
.and_then(|o| o.provider)
.as_deref(),
Some("openrouter")
);
assert_eq!(override_for(PlanModeState::NoPlan, &agent), None);
}
#[test]
fn only_one_half_configured_leaves_the_other_alone() {
let plan_only = planning(Some("anthropic"), None);
assert_eq!(override_for(PlanModeState::Active, &plan_only), None);
let exec_only = executing(Some("openrouter"), None);
assert_eq!(
override_for(PlanModeState::PostInitEditing, &exec_only),
None
);
}
#[test]
fn an_untouched_override_is_recognised_as_still_applied() {
let swap = PlanModeSwap {
original_provider: "modelstudio".to_string(),
original_model: "qwen3.8-max".to_string(),
applied_provider: "anthropic".to_string(),
applied_model: "claude-opus-4-6".to_string(),
};
assert!(swap.still_applied("anthropic", "claude-opus-4-6"));
}
#[test]
fn a_user_switch_mid_plan_is_not_still_applied() {
let swap = PlanModeSwap {
original_provider: "modelstudio".to_string(),
original_model: "qwen3.8-max".to_string(),
applied_provider: "anthropic".to_string(),
applied_model: "claude-opus-4-6".to_string(),
};
assert!(!swap.still_applied("openrouter", "some-other-model"));
assert!(
!swap.still_applied("anthropic", "claude-sonnet-4-6"),
"same provider but a different model is still a deliberate change"
);
}
fn full_config(json: &str) -> Config {
serde_json::from_str(json).expect("fixture config")
}
#[test]
fn a_custom_prefixed_plan_provider_resolves_to_the_section_name() {
let cfg = full_config(
r#"{
"agent": { "plan_provider": "custom:myprovider", "plan_model": "some-model" },
"providers": {
"custom": { "myprovider": { "base_url": "http://localhost:1/v1", "api_key": "k" } }
}
}"#,
);
assert_eq!(
normalized_override_for(PlanModeState::PreInitEditing, &cfg),
Some(ModeOverride {
provider: Some("myprovider".into()),
model: Some("some-model".into()),
})
);
}
#[test]
fn an_execute_provider_slash_model_lands_the_model_in_the_override() {
let cfg = full_config(
r#"{
"agent": { "execute_provider": "zhipu/glm-5.3" },
"providers": { "zhipu": { "enabled": true, "api_key": "k" } }
}"#,
);
assert_eq!(
normalized_override_for(PlanModeState::Active, &cfg),
Some(ModeOverride {
provider: Some("zhipu".into()),
model: Some("glm-5.3".into()),
})
);
assert_eq!(
normalized_override_for(PlanModeState::PreInitEditing, &cfg),
None
);
}
#[test]
fn a_model_only_override_and_the_no_op_pass_through_unnormalised() {
let model_only = full_config(r#"{ "agent": { "plan_model": "some-model" }, "providers": {} }"#);
assert_eq!(
normalized_override_for(PlanModeState::PostInitEditing, &model_only),
Some(ModeOverride {
provider: None,
model: Some("some-model".into()),
})
);
let unset = full_config(r#"{ "agent": {}, "providers": {} }"#);
for state in EVERY_STATE {
assert_eq!(normalized_override_for(state, &unset), None, "{state:?}");
}
}