Files
codex/codex-rs/models-manager/src/api_key_discovery_tests.rs
T
faizan-oai e3a52b87b2 Expose available access programs in model discovery (#44893)
## What changed

Carry optional `available_access_programs` metadata through model information, presets, caches, and the TUI, and expose it as `availableAccessPrograms` in app-server `model/list` responses. Update the generated JSON schemas, TypeScript types, and Python models.

Preserve the distinction between missing metadata and an empty `cyber` list. Ignore unknown cyber program names when reading the catalog so new server programs do not prevent older clients from loading it. Discovery metadata does not grant access; inference still enforces authorization.

## Testing

Add coverage for absent, null, empty, and populated metadata, unknown program names, and app-server serialization. Extend cache tests and verify that online refreshes persist changed access metadata even when the catalog ETag stays unchanged.

GitOrigin-RevId: b3ef5805c1c61b8d64b9b896c9c0a79120143667
2026-09-11 19:13:04 +00:00

95 lines
3.2 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,
uses_codex_backend: false,
responses: Mutex::new(vec![models.clone()].into()),
etag: None,
fetch_count: AtomicUsize::new(0),
observed_proxy_policy: Mutex::new(None),
});
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);
}