Files
imeai-aKode/akode-rs/tui/src/test_support.rs
andrea.difonzo 7c1c6dfa8d Rinomina: crate akode-*, cartelle akode-rs/ e akode-cli/, binario akode, ~/.akode, AKODE_* (A)
Riapplica la fase 5 di aKode (86e7c72e) sulla base ripulita:
- crate codex-* -> akode-*, codex-rs -> akode-rs, codex-cli -> akode-cli, binario e ausiliari akode;
- ~/.codex -> ~/.akode, CODEX_HOME -> AKODE_HOME, variabili CODEX_* -> AKODE_*, .codex/ -> .akode/ di progetto;
  nessun fallback a ~/.codex o a CODEX_*;
- testi per l'utente "Codex" -> "aKode"; testi verso il modello e istruzioni di sistema invariati;
- snapshot insta rinominati (non ancora rigenerati).
I conflitti con la base ripulita sono stati risolti riapplicando la rinomina ai file senza le
funzioni di imeAI (script di mappa dei token per file).

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-17 12:37:52 +01:00

165 lines
6.0 KiB
Rust

//! 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<Vec<ModelPreset>> = 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<ModelPreset> = 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>() -> T
where
T: DeserializeOwned,
{
from_app_server_wire(akode_app_server_protocol::SessionSource::Cli)
}
pub(crate) fn skill_scope_user<T>() -> T
where
T: DeserializeOwned,
{
from_app_server_wire(akode_app_server_protocol::SkillScope::User)
}
pub(crate) fn skill_scope_repo<T>() -> T
where
T: DeserializeOwned,
{
from_app_server_wire(akode_app_server_protocol::SkillScope::Repo)
}
fn from_app_server_wire<T>(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<std::path::Path>,
contents: impl AsRef<str>,
) -> 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<std::path::Path>) {
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}"));
}