Files
imeai-aKode/codex-rs/app-server/src/config_manager_provider_tests.rs
andrea.difonzo 6089fbde91 aKode fase 2b-2: rimosso login ChatGPT e tutti i sottosistemi solo-ChatGPT
- Login: resta solo chiave API (+ header, Bedrock). Rimossi OAuth/browser,
  device code, refresh/revoke su auth.openai.com, PAT, agent/workload identity,
  runtime agent instance, backend-client e openapi-models, workspace routing.
- Rimossi: generazione immagini (ext + skill imagegen), history notes, plugin
  remoti (catalogo, sync, share), cyber access program, daybreak, cloud config
  e requirements (config_server ora fail-closed), attestation, frozen cloud
  skills, EMA MCP auth, Codex Apps/connettori, ricerca ospitata, header/zstd/
  routing/cookie del backend Codex, fallback OPENAI_API_KEY del realtime.
- Messaggi d'errore senza link a chatgpt.com.
- Chiavi di config legacy accettate e ignorate; feature rimosse = Stage::Removed.
- Test: cancellati solo quelli del codice rimosso; test di sicurezza
  (sandbox estensioni, permessi, redazione, auth storage, requirements)
  adattati. Restano solo fallimenti d'ambiente (code-mode-host/V8, ~/.agents).
- Istruzioni di sistema invariate.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-10 16:20:15 +01:00

245 lines
8.5 KiB
Rust

//! Checks retained providers against managed policy and the current provider selection.
use super::*;
use anyhow::Result;
use codex_config::ThreadConfigContext;
use codex_config::ThreadConfigLoadError;
use codex_config::ThreadConfigLoadErrorCode;
use codex_config::ThreadConfigLoaderFuture;
use codex_config::ThreadConfigSource;
use codex_config::test_support::ManagedConfigFixture;
use pretty_assertions::assert_eq;
use tempfile::tempdir;
use test_case::test_case;
#[test_case(AMAZON_BEDROCK_PROVIDER_ID, "aws.region = 'us-west-2'", "us-west-2", "us-east-1", None; "bedrock")]
#[test_case(AMAZON_BEDROCK_RUNTIME_PROVIDER_ID, "aws.region = 'us-west-2'", "us-west-2", "us-east-1", None; "bedrock_runtime")]
#[test_case(GCP_PROVIDER_ID, "project_id = 'example-project'\nlocation = 'us-central1'", "example-project", "other-project", None; "gcp_project")]
#[test_case(GCP_PROVIDER_ID, "project_id = 'example-project'\nlocation = 'us-central1'", "us-central1", "us-east4", None; "gcp_location")]
#[test_case(GCP_PROVIDER_ID, "project_id = 'example-project'\nauth.command = 'print-token'", "print-token", "other-token", None; "gcp_auth")]
#[tokio::test]
async fn provider_requirements_resolve_builtin_overrides(
provider_id: &str,
provider_config: &str,
original: &str,
replacement: &str,
openai_base_url: Option<&str>,
) -> Result<()> {
let home = tempdir()?;
let workspace = tempdir()?;
if let Some(base_url) = openai_base_url {
std::fs::write(
home.path().join("config.toml"),
format!("openai_base_url = '{base_url}'"),
)?;
}
let requirements = format!(
"model_provider = '{provider_id}'\n[model_providers.{provider_id}]\n{provider_config}"
);
let manager = ConfigManager::new_for_tests(home.path().to_path_buf(),
Vec::new(),
(ManagedConfigFixture::requirements(&requirements))
.loader_overrides_for_tests(home.path())?,
);
let current = manager
.load_latest_config(Some(workspace.path().to_path_buf()))
.await?;
if provider_id == "openai" {
assert_eq!(current.model_provider.base_url.as_deref(), openai_base_url);
// Retained threads keep their inference route even if user defaults later change.
std::fs::write(
home.path().join("config.toml"),
"openai_base_url = 'https://eu.api.openai.com/v1'",
)?;
}
manager.check_thread_model_provider(&current).await?;
let changed = ConfigManager::new_for_tests(home.path().to_path_buf(),
Vec::new(),
(ManagedConfigFixture::requirements(
requirements.replace(original, replacement),
))
.loader_overrides_for_tests(home.path())?,
);
assert_eq!(
changed
.check_thread_model_provider(&current)
.await
.unwrap_err()
.kind(),
std::io::ErrorKind::PermissionDenied,
);
Ok(())
}
struct UnavailableThreadConfig;
impl ThreadConfigLoader for UnavailableThreadConfig {
fn load(
&self,
_context: ThreadConfigContext,
) -> ThreadConfigLoaderFuture<'_, Vec<ThreadConfigSource>> {
Box::pin(async {
Err(ThreadConfigLoadError::new(
ThreadConfigLoadErrorCode::RequestFailed,
/*status_code*/ None,
"thread config service unavailable",
))
})
}
}
#[tokio::test]
async fn provider_requirements_do_not_reload_thread_config() -> Result<()> {
let home = tempdir()?;
let mut manager = ConfigManager::without_managed_config_for_tests(home.path().to_path_buf());
manager.thread_config_loader = Arc::new(codex_config::StaticThreadConfigLoader::new(vec![
ThreadConfigSource::Session(codex_config::SessionThreadConfig {
model_provider: Some("retained".into()),
model_providers: toml::from_str("[retained]\nname = 'Retained'")?,
..Default::default()
}),
]));
let current = manager.load_latest_config(/*fallback_cwd*/ None).await?;
manager.thread_config_loader = Arc::new(UnavailableThreadConfig);
assert!(
manager
.load_latest_config(/*fallback_cwd*/ None)
.await
.is_err()
);
manager.check_thread_model_provider(&current).await?;
manager.loader_overrides = ManagedConfigFixture::requirements("model_provider = 'retained'")
.loader_overrides_for_tests(home.path())?;
manager.check_thread_model_provider(&current).await?;
manager.loader_overrides = ManagedConfigFixture::requirements("model_provider = 'other'")
.loader_overrides_for_tests(home.path())?;
assert_eq!(
manager
.check_thread_model_provider(&current)
.await
.unwrap_err()
.kind(),
std::io::ErrorKind::PermissionDenied,
);
Ok(())
}
#[tokio::test]
async fn provider_requirements_ignore_system_defaults_but_reject_requirement_changes() -> Result<()>
{
let home = tempdir()?;
let system_config_path = home.path().join("system-config.toml");
let requirements_path = home.path().join("requirements.toml");
let managed_config_path = home.path().join("managed-config.toml");
let mut overrides = LoaderOverrides::without_managed_config_for_tests();
overrides.system_config_path = Some(system_config_path.clone());
overrides.system_requirements_path = Some(requirements_path.clone());
overrides.managed_config_path = Some(managed_config_path.clone());
let manager = ConfigManager::new_for_tests(
home.path().to_path_buf(),
Vec::new(),
overrides,
);
let current = manager.load_latest_config(/*fallback_cwd*/ None).await?;
std::fs::write(&system_config_path, "invalid toml !!!")?;
manager.check_thread_model_provider(&current).await?;
std::fs::write(&requirements_path, "model_provider = 'other'")?;
assert_eq!(
manager
.check_thread_model_provider(&current)
.await
.unwrap_err()
.kind(),
std::io::ErrorKind::PermissionDenied,
);
std::fs::write(&requirements_path, "invalid toml !!!")?;
assert_eq!(
manager
.check_thread_model_provider(&current)
.await
.unwrap_err()
.kind(),
std::io::ErrorKind::InvalidData,
);
std::fs::write(
&requirements_path,
"[model_providers.gateway]\nbase_url = 'https://example.test'",
)?;
assert_eq!(
manager
.check_thread_model_provider(&current)
.await
.unwrap_err()
.kind(),
std::io::ErrorKind::InvalidData,
);
std::fs::remove_file(requirements_path)?;
std::fs::write(managed_config_path, "invalid toml !!!")?;
assert_eq!(
manager
.check_thread_model_provider(&current)
.await
.unwrap_err()
.kind(),
std::io::ErrorKind::InvalidData,
);
Ok(())
}
#[tokio::test]
async fn provider_requirement_load_errors_reject_input() -> Result<()> {
let home = tempdir()?;
let previous = ConfigManager::without_managed_config_for_tests(home.path().to_path_buf())
.load_latest_config(/*fallback_cwd*/ None)
.await?;
{
let manager = ConfigManager::new_for_tests(
home.path().to_path_buf(),
Vec::new(),
ManagedConfigFixture::requirements("invalid toml !!!")
.loader_overrides_for_tests(home.path())?,
);
assert!(
manager
.check_thread_model_provider(&previous)
.await
.is_err()
);
}
Ok(())
}
/// A managed startup selection must disappear when its policy is removed.
#[tokio::test]
async fn catalog_reload_uses_latest_model_provider() -> Result<()> {
let home = tempdir()?;
let manager = ConfigManager::new_for_tests(home.path().to_path_buf(),
Vec::new(),
(ManagedConfigFixture::requirements(
"model_provider = 'gateway'\n[model_providers.gateway]\nname = 'Gateway'\nbase_url = 'https://example.test'",
))
.loader_overrides_for_tests(home.path())?,
);
let startup = manager.load_non_project_config().await?;
assert_eq!(startup.model_provider_id, "gateway");
// Removing the managed requirements file removes the managed selection.
std::fs::remove_file(
home.path()
.join("managed-config-fixture")
.join("requirements.toml"),
)?;
let refreshed = manager
.load_startup_config(/*fallback_cwd*/ None)
.await?
.config;
assert_eq!(refreshed.model_provider_id, "openai");
Ok(())
}