Files
codex-proxy-rs/backend/crates/gateway-admin/tests/use_case/settings.rs
T
ZyyandCodex 984b3e8e08 feat(settings): manage OpenAI connection pool at runtime
Persist the always-on WebSocket pool settings in runtime snapshots.
Apply per-turn timeouts and opening limits without replacing the shared
pool, while existing connections retain their original lifetime settings.

Default residency to us and initialize unset values during migration while
preserving explicit clearing. Expand setting help and move input units to
suffixes throughout settings forms.

BREAKING CHANGE: Deployment openai.ws_pool configuration is removed.
Settings API and SDK updates must include the complete connection pool
object. Existing custom pool values must be saved in platform settings.

Co-authored-by: Codex <noreply@openai.com>
2026-11-27 16:45:45 +08:00

489 lines
16 KiB
Rust

//! 验证设置服务调用、Provider 参数校验与价格管理行为
use std::{
collections::{BTreeMap, BTreeSet},
sync::{Arc, Mutex},
};
use async_trait::async_trait;
use gateway_admin::{
model::{
AdminErrorKind, MutationContext, Revision,
pricing::{PricingChange, PricingSyncPreview, StoredPricing, SyncPricing, UpdatePricing},
settings::{
AdminApiKey, AdminApiKeyMutation, ReplaceRuntimeSettings, RotationStrategy,
RuntimeSettings,
},
},
ports::store::{AdminStoreError, AdminStoreErrorKind, AdminStoreResult, SettingsStore},
};
struct UnusedSettingsStore;
#[derive(Default)]
struct PricingSettingsStore {
synced: Mutex<Option<gateway_admin::model::pricing::PricingSyncChanges>>,
updated: Mutex<Option<UpdatePricing>>,
}
#[tokio::test]
async fn explicit_service_calls_freeze_empty_plan_and_cancel_before_entering_terminal() {
use gateway_admin::public_service::{Origin, Plan, scope};
use std::sync::atomic::{AtomicUsize, Ordering};
let resolutions = Arc::new(AtomicUsize::new(0));
let source_calls = resolutions.clone();
let store = Arc::new(PricingSettingsStore::default());
let services = super::AdminHarness::new()
.settings(store.clone())
.service_middleware(Arc::new(move || {
source_calls.fetch_add(1, Ordering::SeqCst);
None
}))
.build()
.await;
let context = MutationContext {
actor: gateway_admin::model::MutationActor::System,
request_id: "empty-plan".into(),
};
let command = UpdatePricing {
provider: "xai".into(),
models: vec!["example-model".into()],
change: PricingChange::Reset,
};
for expected in 1..=2 {
services
.public_services()
.call(
"settings.update_pricing",
serde_json::to_value((&context, command.clone())).unwrap(),
)
.await
.unwrap();
// 每次显式调价只解析一次插件发布集合
assert_eq!(resolutions.load(Ordering::SeqCst), expected);
}
store.updated.lock().unwrap().take();
let cancellation = gateway_core::lifecycle::CancellationToken::new();
cancellation.cancel();
let error = scope(
Origin {
request_id: "cancelled-native".into(),
call_id: "entry".into(),
cancellation,
extensions: Default::default(),
plan: Plan::Frozen(None),
},
services.public_services().call(
"settings.update_pricing",
serde_json::to_value((context, command)).unwrap(),
),
)
.await
.unwrap_err();
assert_eq!(error.kind, "unavailable");
assert!(store.updated.lock().unwrap().is_none());
assert_eq!(resolutions.load(Ordering::SeqCst), 2);
}
#[tokio::test]
async fn client_profile_preview_rejects_unknown_provider_before_loading_settings() {
let services = super::AdminHarness::new()
.settings(Arc::new(UnusedSettingsStore))
.build()
.await;
let error = services
.settings()
.preview_client_profile("unknown", None)
.await
.expect_err("unknown provider cannot use global settings");
assert_eq!(error.kind(), AdminErrorKind::Invalid);
}
#[tokio::test]
async fn pricing_validates_the_bands_declared_by_the_registered_provider() {
for (provider, supported) in [
("text-provider", &["standard", "fast"][..]),
("image-provider", &["standard", "image"][..]),
] {
let store = Arc::new(PricingSettingsStore::default());
let services = super::AdminHarness::new()
.settings(store.clone())
.provider(Arc::new(super::UnavailableProvider {
pricing_bands: supported,
..super::UnavailableProvider::new(provider)
}))
.build()
.await;
assert_eq!(
services.settings().pricing().await.unwrap().supported_bands[provider],
supported
);
let context = MutationContext {
actor: gateway_admin::model::MutationActor::System,
request_id: "request-band-pricing".to_owned(),
};
for band in [
"standard",
"fast",
"ultrafast",
"flex",
"long_standard",
"long_fast",
"long_ultrafast",
"long_flex",
"image",
] {
let pricing = serde_json::from_value(serde_json::json!({
"multiplierBps": 10000,
"bands": {
"standard": {"input":"2", "output":"10", "cacheRead":"0.1", "cacheWrite":"2.5"},
(band): {"input":"12", "output":"60", "cacheRead":"0.6", "cacheWrite":"15"}
}
}))
.expect("valid token price override");
let update = UpdatePricing {
provider: provider.to_owned(),
models: vec!["example-model".to_owned()],
change: PricingChange::Replace(pricing),
};
let result = services
.settings()
.update_pricing(&context, update.clone())
.await;
if supported.contains(&band) {
result.expect("provider accepts its supported band");
assert_eq!(store.updated.lock().unwrap().take(), Some(update));
} else {
assert_eq!(
result
.expect_err("provider rejects unsupported band")
.kind(),
AdminErrorKind::Invalid
);
assert!(store.updated.lock().unwrap().is_none());
}
}
}
}
#[tokio::test]
async fn pricing_accepts_native_provider_and_rejects_unknown_sync_provider() {
let store = Arc::new(PricingSettingsStore::default());
let services = super::AdminHarness::new()
.settings(store.clone())
.provider(super::test_provider("xai"))
.build()
.await;
let context = MutationContext {
actor: gateway_admin::model::MutationActor::System,
request_id: "request-native-pricing".to_owned(),
};
let pricing = serde_json::from_value(serde_json::json!({
"multiplierBps": 10000,
"bands": {"standard": {
"input": "1", "output": "2", "cacheRead": "0", "cacheWrite": "0"
}}
}))
.expect("native pricing");
let update = UpdatePricing {
provider: "xai".to_owned(),
models: vec!["example-model".to_owned()],
change: PricingChange::Replace(pricing),
};
services
.settings()
.update_pricing(&context, update.clone())
.await
.expect("native provider pricing");
assert_eq!(
store.updated.lock().expect("updated pricing").as_ref(),
Some(&update)
);
let error = services
.settings()
.sync_pricing(
&context,
SyncPricing {
preview: PricingSyncPreview {
prices: BTreeMap::new(),
skipped: Vec::new(),
},
models: BTreeMap::from([(
"unregistered-provider".to_owned(),
BTreeSet::from(["example-model".to_owned()]),
)]),
},
)
.await
.expect_err("unknown provider cannot enter pricing sync");
assert_eq!(error.kind(), AdminErrorKind::Invalid);
}
#[async_trait]
impl SettingsStore for UnusedSettingsStore {
async fn load_pricing(&self) -> AdminStoreResult<gateway_admin::model::pricing::StoredPricing> {
Ok(Default::default())
}
async fn sync_pricing(
&self,
_: gateway_admin::model::pricing::PricingSyncChanges,
_: &MutationContext,
) -> AdminStoreResult<gateway_admin::model::Revision> {
panic!("unexpected pricing sync")
}
async fn update_pricing(
&self,
_: gateway_admin::model::pricing::UpdatePricing,
_: &MutationContext,
) -> AdminStoreResult<gateway_admin::model::Revision> {
panic!("unexpected pricing update")
}
async fn load_runtime_settings(&self) -> AdminStoreResult<RuntimeSettings> {
Err(unused())
}
async fn admin_api_key_exists(&self) -> AdminStoreResult<bool> {
Err(unused())
}
async fn replace_runtime_settings(
&self,
_: ReplaceRuntimeSettings,
_: &MutationContext,
) -> AdminStoreResult<RuntimeSettings> {
Err(unused())
}
async fn replace_admin_api_key(
&self,
_: AdminApiKey,
_: &MutationContext,
) -> AdminStoreResult<AdminApiKeyMutation> {
Err(unused())
}
async fn delete_admin_api_key(
&self,
_: &MutationContext,
) -> AdminStoreResult<AdminApiKeyMutation> {
Err(unused())
}
}
#[async_trait]
impl SettingsStore for PricingSettingsStore {
async fn load_pricing(&self) -> AdminStoreResult<StoredPricing> {
Ok(StoredPricing::default())
}
async fn sync_pricing(
&self,
command: gateway_admin::model::pricing::PricingSyncChanges,
_: &MutationContext,
) -> AdminStoreResult<Revision> {
*self.synced.lock().unwrap() = Some(command);
Ok(Revision::new(2).unwrap())
}
async fn update_pricing(
&self,
command: UpdatePricing,
_: &MutationContext,
) -> AdminStoreResult<Revision> {
*self.updated.lock().expect("updated pricing") = Some(command);
Ok(Revision::new(2).expect("revision"))
}
async fn load_runtime_settings(&self) -> AdminStoreResult<RuntimeSettings> {
Err(unused())
}
async fn admin_api_key_exists(&self) -> AdminStoreResult<bool> {
Err(unused())
}
async fn replace_runtime_settings(
&self,
_: ReplaceRuntimeSettings,
_: &MutationContext,
) -> AdminStoreResult<RuntimeSettings> {
Err(unused())
}
async fn replace_admin_api_key(
&self,
_: AdminApiKey,
_: &MutationContext,
) -> AdminStoreResult<AdminApiKeyMutation> {
Err(unused())
}
async fn delete_admin_api_key(
&self,
_: &MutationContext,
) -> AdminStoreResult<AdminApiKeyMutation> {
Err(unused())
}
}
#[tokio::test]
async fn settings_should_reject_zero_refresh_margin_before_store_call() {
let services = super::AdminHarness::new()
.settings(Arc::new(UnusedSettingsStore))
.build()
.await;
let error = services
.settings()
.replace(
&MutationContext {
actor: gateway_admin::model::MutationActor::System,
request_id: "request-settings".to_owned(),
},
ReplaceRuntimeSettings {
expected_revision: gateway_admin::model::Revision::new(1).unwrap(),
request_profile_updates: Default::default(),
model_mappings: Default::default(),
rotation_strategy: RotationStrategy::Smart,
values: gateway_admin::model::settings::RuntimeSettingsValues {
openai_websocket_pool: Default::default(),
openai_request_settings: Default::default(),
codex_privacy_policy: Default::default(),
request_location_enabled: false,
request_location: Default::default(),
refresh_margin_seconds: 0,
refresh_concurrency: 1,
max_concurrent_per_account: 1,
max_waiting_per_key: 0,
max_waiting_per_account: 0,
key_queue_timeout_seconds: 30,
account_queue_timeout_seconds: 30,
switch_account_on_queue_failure: false,
max_retry_after_seconds: 240,
openai_guardian_reserved_concurrency: 0,
account_affinity: gateway_core::account::AccountAffinity::Relaxed,
max_account_rotations: 3,
session_affinity_ttl_hours: 24,
responses_max_decompressed_body_bytes: 64 * 1024 * 1024,
smart_scheduling: gateway_core::account::SmartSchedulingConfig::default(),
quota_reset_window_weights: gateway_core::account::ResetWindowWeights::default(
),
min_codex_desktop_version: None,
min_codex_cli_version: None,
usage_retention_days: 31,
ops_event_retention_days: 30,
audit_retention_days: 30,
account_error_protection:
gateway_core::account::AccountErrorProtection::default(),
account_warmup_enabled: false,
account_warmup_cron: "0 8 * * *".to_owned(),
account_warmup_model: None,
reset_credit_lead_minutes: 10,
reset_credit_on_low_weekly_quota: false,
reset_credit_max_weekly_remaining_percent: None,
},
},
)
.await
.expect_err("invalid settings");
assert_eq!(error.kind(), AdminErrorKind::Invalid);
}
fn unused() -> AdminStoreError {
AdminStoreError::new(
AdminStoreErrorKind::Unavailable,
"settings",
"unused in this test",
)
}
struct ReferencePrices(Mutex<PricingSyncPreview>);
#[async_trait]
impl gateway_admin::ports::pricing::PricingSource for ReferencePrices {
async fn fetch(
&self,
_: &gateway_admin::ports::pricing::PricingSourceScope,
) -> Result<PricingSyncPreview, gateway_admin::model::AdminError> {
Ok(self.0.lock().unwrap().clone())
}
}
#[tokio::test]
async fn external_reference_prices_project_to_exact_provider_ids_in_preview_and_sync() {
let price: gateway_core::metering::ModelPriceOverride = serde_json::from_value(serde_json::json!({"multiplierBps":10000,"bands":{"standard":{"input":"1","output":"2","cacheRead":"0.1","cacheWrite":"1"}}})).unwrap();
let source = Arc::new(ReferencePrices(Mutex::new(PricingSyncPreview {
prices: BTreeMap::from([(
"google".into(),
BTreeMap::from([
("base-model".into(), price.clone()),
("unmapped".into(), price),
]),
)]),
skipped: vec!["google/missing".into(), "google/unrelated-image".into()],
})));
let mut provider = super::UnavailableProvider::new("antigravity");
provider.pricing_references = BTreeMap::from([
("ag-high".into(), ("google".into(), "base-model".into())),
("ag-low".into(), ("google".into(), "base-model".into())),
("ag-image".into(), ("google".into(), "missing".into())),
]);
let store = Arc::new(PricingSettingsStore::default());
let service = gateway_admin::initialize_settings(
store.clone(),
Arc::new(super::NoopSnapshot),
gateway_admin::ports::provider::ProviderAdminRegistry::new(vec![
Arc::new(provider) as Arc<dyn gateway_admin::ports::provider::ProviderAdmin>
])
.unwrap(),
source.clone(),
);
let preview = service.preview_pricing_sync().await.unwrap();
assert_eq!(
preview.prices.keys().cloned().collect::<Vec<_>>(),
["antigravity"]
);
assert_eq!(
preview.prices["antigravity"]
.keys()
.cloned()
.collect::<Vec<_>>(),
["ag-high", "ag-low"]
);
assert_eq!(preview.skipped, ["antigravity/ag-image"]);
// 未登记外部型号的变化不会污染同步确认,但实际保存仍重新读取并校验投影价目
source
.0
.lock()
.unwrap()
.prices
.get_mut("google")
.unwrap()
.remove("unmapped");
service
.sync_pricing(
&MutationContext {
actor: gateway_admin::model::MutationActor::System,
request_id: "reference-sync".into(),
},
SyncPricing {
preview,
models: BTreeMap::from([(
"antigravity".into(),
BTreeSet::from(["ag-high".into()]),
)]),
},
)
.await
.unwrap();
let saved = store.synced.lock().unwrap();
let saved = saved.as_ref().unwrap();
assert_eq!(
saved["antigravity"].keys().cloned().collect::<Vec<_>>(),
["ag-high"]
);
}