//! Test-only helpers shared across the TUI crate. use std::sync::LazyLock; use akode_models_manager::bundled_models_response; use akode_protocol::openai_models::ModelPreset; pub(crate) use akode_utils_absolute_path::test_support::PathBufExt; pub(crate) use akode_utils_absolute_path::test_support::test_path_buf; use serde::Serialize; use serde::de::DeserializeOwned; pub(crate) static TEST_MODEL_PRESETS: LazyLock> = LazyLock::new(|| { let mut response = bundled_models_response() .unwrap_or_else(|err| panic!("bundled models.json should parse: {err}")); response.models.sort_by_key(|model| model.priority); let mut presets: Vec = response.models.into_iter().map(Into::into).collect(); ModelPreset::mark_default_by_picker_visibility(&mut presets); presets }); pub(crate) fn test_path_display(path: &str) -> String { test_path_buf(path).display().to_string() } pub(crate) fn session_source_cli() -> T where T: DeserializeOwned, { from_app_server_wire(akode_app_server_protocol::SessionSource::Cli) } pub(crate) fn skill_scope_user() -> T where T: DeserializeOwned, { from_app_server_wire(akode_app_server_protocol::SkillScope::User) } pub(crate) fn skill_scope_repo() -> T where T: DeserializeOwned, { from_app_server_wire(akode_app_server_protocol::SkillScope::Repo) } fn from_app_server_wire(value: impl Serialize) -> T where T: DeserializeOwned, { serde_json::to_value(value) .and_then(serde_json::from_value) .unwrap_or_else(|err| { panic!("app-server wire value should map to legacy helper type: {err}") }) } pub(crate) use crate::boxed_future; /// Provider id used by test configurations; aKode has no built-in provider. pub(crate) const TEST_MODEL_PROVIDER_ID: &str = core_test_support::test_codex::TEST_MODEL_PROVIDER_ID; const TEST_MODEL_PROVIDER_BASE_URL: &str = "http://127.0.0.1:9/v1"; /// Select the test provider and the bundled catalog in an in-memory configuration. pub(crate) fn apply_test_model_provider(config: &mut crate::legacy_core::config::Config) { let provider = core_test_support::test_codex::test_model_provider_info(Some( TEST_MODEL_PROVIDER_BASE_URL.to_string(), )); config.model_provider_id = TEST_MODEL_PROVIDER_ID.to_string(); config .model_providers .insert(TEST_MODEL_PROVIDER_ID.to_string(), provider.clone()); config.model_provider = provider; config.model_catalog = bundled_models_response().ok(); } /// Settings that make an embedded app server use the test provider and the bundled catalog. /// /// Writes the catalog into `codex_home` and returns `contents` with the provider selection /// added. Root keys go first so they never land inside a table from `contents`; keys that /// `contents` already sets are left alone. pub(crate) fn with_test_model_provider( codex_home: impl AsRef, contents: impl AsRef, ) -> String { let contents = contents.as_ref(); let catalog_path = codex_home.as_ref().join("test-models.json"); let catalog = bundled_models_response() .and_then(|catalog| serde_json::to_string(&catalog)) .unwrap_or_else(|err| panic!("bundled models.json should serialize: {err}")); std::fs::write(&catalog_path, catalog) .unwrap_or_else(|err| panic!("write test model catalog: {err}")); let sets_root_key = |key: &str| { contents.lines().any(|line| { line.strip_prefix(key) .is_some_and(|rest| rest.trim_start().starts_with('=')) }) }; let mut root = String::new(); if !sets_root_key("model_provider") { root.push_str(&format!("model_provider = {TEST_MODEL_PROVIDER_ID:?}\n")); } if !sets_root_key("model_catalog_json") { root.push_str(&format!( "model_catalog_json = {:?}\n", catalog_path.display().to_string() )); } // Tests that pointed the removed built-in provider at a mock server keep that endpoint. let base_url = contents .lines() .find_map(|line| { let value = line .strip_prefix("openai_base_url")? .trim_start() .strip_prefix('=')?; Some( value .trim() .trim_matches(|c| c == '"' || c == '\'') .to_string(), ) }) .unwrap_or_else(|| TEST_MODEL_PROVIDER_BASE_URL.to_string()); let provider = if contents.contains(&format!("[model_providers.{TEST_MODEL_PROVIDER_ID}]")) { String::new() } else { format!( concat!( "\n[model_providers.{id}]\n", "name = {id:?}\n", "base_url = {url:?}\n", "wire_api = \"responses\"\n", "requires_openai_auth = true\n", "supports_websockets = true\n", "supports_standalone_web_search = true\n", "merge_system_messages = false\n", "capabilities = {{ remote_compaction = \"v2\" }}\n", ), id = TEST_MODEL_PROVIDER_ID, url = base_url, ) }; format!("{root}{contents}\n{provider}") } /// Write `config.toml` in `codex_home` with `contents` plus the test provider selection. pub(crate) fn write_test_config_toml(codex_home: &std::path::Path, contents: &str) { std::fs::write( codex_home.join("config.toml"), with_test_model_provider(codex_home, contents), ) .unwrap_or_else(|err| panic!("write test config.toml: {err}")); } /// Add the test provider selection to `codex_home/config.toml`, keeping existing settings. pub(crate) fn ensure_test_config_toml(codex_home: impl AsRef) { let codex_home = codex_home.as_ref(); let path = codex_home.join("config.toml"); let existing = std::fs::read_to_string(&path).unwrap_or_default(); std::fs::write(&path, with_test_model_provider(codex_home, existing)) .unwrap_or_else(|err| panic!("write test config.toml: {err}")); }