Files
imeai-aKode/akode-rs/app-server/tests/suite/v2/model_provider_config_tests.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

191 lines
7.3 KiB
Rust

//! Exercises provider management through the public app-server requests.
#![cfg(test)]
use anyhow::Result;
use app_test_support::TestAppServer;
use akode_app_server_protocol::ClientRequest;
use akode_app_server_protocol::RequestId;
use pretty_assertions::assert_eq;
use serde_json::Value;
use serde_json::json;
use tempfile::TempDir;
const CLEARED_AUTH_ENV: &[(&str, Option<&str>)] = &[
("OPENAI_API_KEY", None),
("AKODE_API_KEY", None),
("AKODE_ACCESS_TOKEN", None),
("OPENAI_FEDERATION_RULE_ID", None),
("OPENAI_IDENTITY_TOKEN_FILE", None),
("OPENAI_WORKLOAD_IDENTITY_CONTEXT", None),
];
#[tokio::test]
async fn create_and_patch_gateway_providers_without_changing_the_default() -> Result<()> {
let home = TempDir::new()?;
let mut server = TestAppServer::builder()
.with_codex_home(home.path())
.with_env_overrides(CLEARED_AUTH_ENV)
.build_initialized()
.await?;
let settings = json!({"type":"customGateway", "name":"Team Gateway", "baseUrl":"https://gateway.example.test/v1", "envKey":"TEAM_KEY"});
// Pipeline creation requests: allocation must run against the preceding committed write.
let first = server
.send_request(
"modelProvider/config/write",
Some(json!({"settings":settings})),
)
.await?;
let second = server
.send_request(
"modelProvider/config/write",
Some(json!({"settings":settings})),
)
.await?;
let saved: Value = server.read_response(first).await?;
let duplicate: Value = server.read_response(second).await?;
assert_eq!(duplicate["provider"]["id"], "team-gateway-2");
let mut expected = json!({"id":"team-gateway", "name":"Team Gateway", "canEdit":true,
"baseUrl":"https://gateway.example.test/v1",
"settings":{"type":"customGateway", "auth":{"mode":"environmentVariable", "envKey":"TEAM_KEY"}}});
assert_eq!(saved, json!({"provider":expected}));
// Disjoint stale-client patches preserve siblings; writing a field again wins.
for (mut patch, field, value) in [
(json!({"name":"Renamed"}), "name", json!("Renamed")),
(
json!({"baseUrl":"https://new.example.test"}),
"baseUrl",
json!("https://new.example.test"),
),
(json!({"name":"Latest"}), "name", json!("Latest")),
(
json!({"envKey":null}),
"settings",
json!({"type":"customGateway", "auth":{"mode":"none"}}),
),
(json!({"baseUrl":null}), "baseUrl", Value::Null),
] {
patch["type"] = json!("customGateway");
patch["providerId"] = json!("team-gateway");
let params = serde_json::from_value(json!({"settings":patch}))?;
let updated: Value = server
.request(|request_id| ClientRequest::ModelProviderConfigWrite { request_id, params })
.await?;
expected[field] = value;
assert_eq!(updated, json!({"provider":expected}));
}
let path = home.path().join("config.toml");
let before = std::fs::read_to_string(&path)?;
let clear = server.send_request("modelProvider/config/write", Some(json!({"settings":{"type":"customGateway","providerId":"team-gateway","name":null, "baseUrl":"https://must-not-save.example.test"}}))).await?;
let error = server
.read_stream_until_error_message(RequestId::Integer(clear))
.await?;
assert_eq!(error.error.code, -32600);
assert_eq!(std::fs::read_to_string(&path)?, before);
let list = server
.send_request("modelProvider/list", Some(json!({})))
.await?;
let after: Value = server.read_response(list).await?;
assert_eq!(
after["data"]
.as_array()
.unwrap()
.iter()
.find(|p| p["id"] == "team-gateway"),
Some(&expected)
);
// aKode: there is no default (`openai`) provider when none is configured.
assert_eq!(after["defaultProviderId"], "");
let persisted: Value = toml::from_str(&before)?;
assert_eq!(
persisted["model_providers"]["team-gateway"],
json!({"name":"Latest", "wire_api":"responses"})
);
Ok(())
}
#[tokio::test]
async fn custom_provider_edits_preserve_other_fields_and_respect_policy() -> Result<()> {
let home = TempDir::new()?;
std::fs::write(
home.path().join("requirements.toml"),
"model_provider = 'corp.gateway'\n[model_providers.\"corp.gateway\"]\nname = 'Managed gateway'\nbase_url = 'https://managed.example.test'\n",
)?;
let config_path = home.path().join("config.toml");
let mut expected = json!({
"model_providers": {
"basic": {"name":"Basic", "base_url":"https://user:password@basic.example.test/v1"},
"external": {
"name": "External",
"base_url": "https://external.example.test",
"http_headers": {"Authorization": "Bearer test-only"},
"query_params": {"api-version": "v1"},
}
}
});
std::fs::write(&config_path, toml::to_string(&expected)?)?;
let mut server = TestAppServer::builder()
.with_codex_home(home.path())
.with_env_overrides(CLEARED_AUTH_ENV)
.build_initialized()
.await?;
let edit = server
.send_request(
"modelProvider/config/write",
Some(json!({"settings": {"type":"customGateway", "providerId":"external", "name":"Renamed"}})),
)
.await?;
let saved: Value = server.read_response(edit).await?;
assert_eq!(saved["provider"]["id"], "external");
expected["model_providers"]["external"]["name"] = json!("Renamed");
let actual: Value = toml::from_str(&std::fs::read_to_string(&config_path)?)?;
assert_eq!(actual, expected);
for (id, mut patch, error_code) in [
(
"corp.gateway",
json!({"name": "Changed"}),
"configLayerReadonly",
),
// aKode: `openai` is no longer a built-in provider, so it is an unknown id here.
("openai", json!({"name": "Changed"}), "configPathNotFound"),
("external", json!({"envKey": null}), "configValidationError"),
(
"basic",
json!({"envKey": "NEW_KEY"}),
"configValidationError",
),
("basic", json!({"envKey": null}), "configValidationError"),
(
"ollama-chat",
json!({"name": "Renamed"}),
"configValidationError",
),
("ollama", json!({}), "configValidationError"),
] {
patch["type"] = json!("customGateway");
patch["providerId"] = json!(id);
let edit = server
.send_request(
"modelProvider/config/write",
Some(json!({"settings": patch})),
)
.await?;
let error = server
.read_stream_until_error_message(RequestId::Integer(edit))
.await?;
if id == "ollama-chat" {
insta::assert_snapshot!(
error.error.message,
@"A custom provider or gateway cannot use a built-in provider ID."
);
}
assert_eq!(
error.error.data,
Some(json!({"config_write_error_code": error_code}))
);
}
let persisted: Value = toml::from_str(&std::fs::read_to_string(&config_path)?)?;
assert_eq!(persisted, expected);
Ok(())
}