Files
codex/codex-rs/models-manager/src/api_key_discovery_tests.rs
T
andrewgu-oai 458f7046a5 Treat explicit provider model catalogs as authoritative (#49135)
## Why

Providers with `model_catalog_url` could advertise bundled models absent from their catalog or reuse stale models after a failed refresh. Model metadata could also match by prefix or namespaced suffix instead of the provider's exact model ID.

## What changed

- Suppress bundled model fallback and merging for explicit provider catalogs, including when discovery is disabled.
- Require unique, non-empty catalog slugs and match metadata by exact model ID.
- Serialize explicit catalog refreshes and invalidate in-memory and persisted catalog entries after fetch failures, preventing stale cache reuse.
- Report a clear configuration error when startup has no available model, while allowing an explicitly configured `model`.

## Testing

Add regression coverage for exact model matching, hidden catalog entries, cache invalidation across restart and recovery, invalid slugs, and empty-catalog startup with and without an explicit model. Update app-server model-list expectations and verify the TUI configuration error.

GitOrigin-RevId: ac323a94178e3c48df8d4a159a86fdbac20b03a2
2026-09-29 02:33:55 +00:00

92 lines
3.1 KiB
Rust

//! API-key discovery rollout must gate network requests and cached catalog authority.
use super::*;
use pretty_assertions::assert_eq;
#[tokio::test]
async fn api_key_discovery_disabled_preserves_command_auth_discovery_and_merging() {
let models = vec![remote_model(
"command-auth",
"Command Auth",
/*priority*/ 0,
)];
let endpoint = Arc::new(TestModelsEndpoint {
has_command_auth: true,
responses: Mutex::new(vec![Ok(models.clone())].into()),
..TestModelsEndpoint::default()
});
let manager = OpenAiModelsManager::new_without_cache(
endpoint.clone(),
Some(AuthManager::from_auth_for_testing(CodexAuth::from_api_key(
"test-key",
))),
);
let mut merged = load_remote_models_from_file().unwrap();
merged.extend(models.clone());
assert_eq!(
manager
.raw_model_catalog(RefreshStrategy::Online, DEFAULT_HTTP_CLIENT_FACTORY)
.await
.models,
merged
);
assert_eq!(endpoint.fetch_count(), 1);
}
#[tokio::test]
async fn api_key_discovery_startup_flag_controls_fetches_and_cached_catalogs() {
let home = tempdir().unwrap();
let models = vec![remote_model("dynamic", "Dynamic", /*priority*/ 0)];
let endpoint = TestModelsEndpoint::without_refresh(vec![models.clone()]);
let auth = AuthManager::from_auth_for_testing(CodexAuth::from_api_key("test-key"));
let manager =
OpenAiModelsManager::new(home.path().into(), endpoint.clone(), Some(auth.clone()));
let bundled = load_remote_models_from_file().unwrap();
for strategy in [
RefreshStrategy::Online,
RefreshStrategy::OnlineIfUncached,
RefreshStrategy::Offline,
] {
assert_eq!(
manager
.raw_model_catalog(strategy, DEFAULT_HTTP_CLIENT_FACTORY)
.await
.models,
bundled
);
}
assert_eq!(endpoint.fetch_count(), 0);
let manager =
OpenAiModelsManager::new(home.path().into(), endpoint.clone(), Some(auth.clone()));
manager.set_api_key_model_discovery_enabled(/*enabled*/ true);
assert_eq!(
manager
.raw_model_catalog(RefreshStrategy::Online, DEFAULT_HTTP_CLIENT_FACTORY)
.await
.models,
models
);
assert_eq!(endpoint.fetch_count(), 1);
// A new session must honor its startup flag even when a matching disk cache exists.
for enabled in [false, true] {
let restarted =
OpenAiModelsManager::new(home.path().into(), endpoint.clone(), Some(auth.clone()));
restarted.set_api_key_model_discovery_enabled(enabled);
let expected = if enabled { &models } else { &bundled };
for strategy in [RefreshStrategy::Offline, RefreshStrategy::OnlineIfUncached] {
assert_eq!(
&restarted
.raw_model_catalog(strategy, DEFAULT_HTTP_CLIENT_FACTORY)
.await
.models,
expected
);
}
assert_eq!(&restarted.try_get_remote_models().unwrap(), expected);
}
assert_eq!(endpoint.fetch_count(), 1);
}