mirror of
https://github.com/fawney19/Aether.git
synced 2026-09-02 17:30:23 +08:00
feat: 全栈功能增强 - 扩展 provider/pool 管理、完善调度与数据层、重构前端 Pool 页面
后端: - 扩展 pool_admin payloads 和 provider query models,增强 endpoint key 管理 - 完善 scheduler-core 候选排序与请求候选逻辑 - 增强 usage-runtime 写入、provider-transport 网络层与 OAuth 刷新 - 改进 AI pipeline 响应转换与流式处理 - 扩展 global_models/provider_catalog 数据层查询能力 - 增强 video-tasks-core 多 provider 支持 - 新增大量集成测试覆盖 pool/keys/provider_query/frontdoor 前端: - 重构 PoolManagement 页面,拆分状态管理/对话框逻辑到独立模块 - 新增 poolAdvancedDialog/poolSchedulingDialog/poolManagementState/poolMobilePresentation 工具函数及测试 - 改进 Dialog 组件与 provider tabs 显示 部署: - 更新 Rust CI workflow 和 Dockerfile 构建配置 Closes #275 Co-authored-by: AAEE86 <ppk0227@hotmail.com>
This commit is contained in:
@@ -1091,7 +1091,7 @@ fn admin_provider_write_uses_specific_local_owners() {
|
||||
] {
|
||||
assert!(
|
||||
endpoint_keys_mutations.contains(pattern),
|
||||
"handlers/admin/provider/endpoint_keys/mutations/mod.rs should expose explicit mutation owner {pattern}"
|
||||
"handlers/admin/provider/endpoint_keys/mutations/mod.rs should expose explicit mutation owner {pattern}"
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -33,7 +33,8 @@ fn usage_runtime_paths_depend_on_shared_crates_not_app_runtime_shims() {
|
||||
);
|
||||
}
|
||||
|
||||
for path in ["apps/aether-gateway/src/async_task/runtime.rs"] {
|
||||
{
|
||||
let path = "apps/aether-gateway/src/async_task/runtime.rs";
|
||||
let source = read_workspace_file(path);
|
||||
assert!(
|
||||
source.contains("aether_billing"),
|
||||
|
||||
@@ -116,6 +116,90 @@ async fn gateway_handles_admin_provider_keys_locally_with_trusted_admin_principa
|
||||
upstream_handle.abort();
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn gateway_admin_provider_keys_prefers_upstream_plan_type_over_auth_config() {
|
||||
let upstream_hits = Arc::new(Mutex::new(0usize));
|
||||
let upstream_hits_clone = Arc::clone(&upstream_hits);
|
||||
let upstream = Router::new().route(
|
||||
"/api/admin/endpoints/providers/provider-codex/keys",
|
||||
any(move |_request: Request| {
|
||||
let upstream_hits_inner = Arc::clone(&upstream_hits_clone);
|
||||
async move {
|
||||
*upstream_hits_inner.lock().expect("mutex should lock") += 1;
|
||||
(StatusCode::OK, Body::from("unexpected upstream hit"))
|
||||
}
|
||||
}),
|
||||
);
|
||||
|
||||
let mut provider = sample_provider("provider-codex", "codex", 10);
|
||||
provider.provider_type = "codex".to_string();
|
||||
let mut key = sample_key(
|
||||
"key-codex-oauth",
|
||||
"provider-codex",
|
||||
"openai:cli",
|
||||
"oauth-placeholder",
|
||||
);
|
||||
key.auth_type = "oauth".to_string();
|
||||
key.encrypted_auth_config = Some(
|
||||
encrypt_python_fernet_plaintext(
|
||||
DEVELOPMENT_ENCRYPTION_KEY,
|
||||
&json!({
|
||||
"plan_type": "free",
|
||||
"account_id": "acct-codex-legacy"
|
||||
})
|
||||
.to_string(),
|
||||
)
|
||||
.expect("auth config should encrypt"),
|
||||
);
|
||||
key.upstream_metadata = Some(json!({
|
||||
"codex": {
|
||||
"plan_type": "plus",
|
||||
"updated_at": 1_775_553_285u64
|
||||
}
|
||||
}));
|
||||
|
||||
let provider_catalog_repository = Arc::new(InMemoryProviderCatalogReadRepository::seed(
|
||||
vec![provider],
|
||||
vec![],
|
||||
vec![key],
|
||||
));
|
||||
|
||||
let (_upstream_url, upstream_handle) = start_server(upstream).await;
|
||||
let gateway = build_router_with_state(
|
||||
AppState::new()
|
||||
.expect("gateway should build")
|
||||
.with_data_state_for_tests(
|
||||
GatewayDataState::with_provider_catalog_reader_for_tests(
|
||||
provider_catalog_repository,
|
||||
)
|
||||
.with_encryption_key_for_tests(DEVELOPMENT_ENCRYPTION_KEY),
|
||||
),
|
||||
);
|
||||
let (gateway_url, gateway_handle) = start_server(gateway).await;
|
||||
|
||||
let response = reqwest::Client::new()
|
||||
.get(format!(
|
||||
"{gateway_url}/api/admin/endpoints/providers/provider-codex/keys?skip=0&limit=50"
|
||||
))
|
||||
.header(crate::constants::GATEWAY_HEADER, "rust-phase3b")
|
||||
.header(TRUSTED_ADMIN_USER_ID_HEADER, "admin-user-123")
|
||||
.header(TRUSTED_ADMIN_USER_ROLE_HEADER, "admin")
|
||||
.header(TRUSTED_ADMIN_SESSION_ID_HEADER, "session-123")
|
||||
.send()
|
||||
.await
|
||||
.expect("request should succeed");
|
||||
|
||||
assert_eq!(response.status(), StatusCode::OK);
|
||||
let payload: serde_json::Value = response.json().await.expect("json body should parse");
|
||||
let items = payload.as_array().expect("payload should be an array");
|
||||
assert_eq!(items.len(), 1);
|
||||
assert_eq!(items[0]["oauth_plan_type"], "plus");
|
||||
assert_eq!(*upstream_hits.lock().expect("mutex should lock"), 0);
|
||||
|
||||
gateway_handle.abort();
|
||||
upstream_handle.abort();
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn gateway_creates_admin_provider_key_locally_with_trusted_admin_principal() {
|
||||
let upstream_hits = Arc::new(Mutex::new(0usize));
|
||||
|
||||
@@ -92,6 +92,9 @@ async fn gateway_handles_admin_global_models_locally_with_trusted_admin_principa
|
||||
payload["models"].as_array().expect("models array")[0]["name"],
|
||||
"gpt-4.1"
|
||||
);
|
||||
assert_eq!(payload["models"][0]["provider_count"], 1);
|
||||
assert_eq!(payload["models"][0]["active_provider_count"], 1);
|
||||
assert_eq!(payload["models"][0]["usage_count"], 0);
|
||||
assert_eq!(*upstream_hits.lock().expect("mutex should lock"), 0);
|
||||
|
||||
gateway_handle.abort();
|
||||
@@ -311,6 +314,9 @@ async fn gateway_handles_admin_global_model_detail_locally_with_trusted_admin_pr
|
||||
assert_eq!(response.status(), StatusCode::OK);
|
||||
let payload: serde_json::Value = response.json().await.expect("json body should parse");
|
||||
assert_eq!(payload["id"], "global-gpt-5");
|
||||
assert_eq!(payload["provider_count"], 1);
|
||||
assert_eq!(payload["active_provider_count"], 1);
|
||||
assert_eq!(payload["usage_count"], 0);
|
||||
assert_eq!(payload["total_models"], 1);
|
||||
assert_eq!(payload["total_providers"], 1);
|
||||
assert_eq!(*upstream_hits.lock().expect("mutex should lock"), 0);
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
use std::sync::{Arc, Mutex};
|
||||
|
||||
use aether_crypto::DEVELOPMENT_ENCRYPTION_KEY;
|
||||
use aether_crypto::{encrypt_python_fernet_plaintext, DEVELOPMENT_ENCRYPTION_KEY};
|
||||
use aether_data::repository::provider_catalog::InMemoryProviderCatalogReadRepository;
|
||||
use aether_data_contracts::repository::provider_catalog::ProviderCatalogReadRepository;
|
||||
use axum::body::{Body, Bytes};
|
||||
use axum::body::{to_bytes, Body, Bytes};
|
||||
use axum::routing::{any, get, post};
|
||||
use axum::{extract::Request, Router};
|
||||
use http::{HeaderMap, HeaderValue, StatusCode};
|
||||
@@ -405,6 +405,68 @@ async fn gateway_handles_admin_pool_trailing_slash_routes_locally_with_trusted_a
|
||||
upstream_handle.abort();
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn gateway_pool_list_includes_usage_totals_and_nullable_lru_score() {
|
||||
let provider = sample_provider("provider-openai", "openai", 10).with_transport_fields(
|
||||
true,
|
||||
false,
|
||||
true,
|
||||
None,
|
||||
None,
|
||||
None,
|
||||
None,
|
||||
None,
|
||||
Some(json!({
|
||||
"pool_advanced": {
|
||||
"enabled": true
|
||||
}
|
||||
})),
|
||||
);
|
||||
let mut key = sample_key(
|
||||
"key-openai-usage",
|
||||
"provider-openai",
|
||||
"openai:chat",
|
||||
"sk-usage",
|
||||
);
|
||||
key.name = "usage key".to_string();
|
||||
key.request_count = Some(1566);
|
||||
key.total_tokens = 187_327_321;
|
||||
key.total_cost_usd = 93.1319297;
|
||||
|
||||
let provider_catalog_repository = Arc::new(InMemoryProviderCatalogReadRepository::seed(
|
||||
vec![provider],
|
||||
Vec::new(),
|
||||
vec![key],
|
||||
));
|
||||
let state = AppState::new()
|
||||
.expect("gateway should build")
|
||||
.with_data_state_for_tests(GatewayDataState::with_provider_catalog_reader_for_tests(
|
||||
provider_catalog_repository,
|
||||
));
|
||||
|
||||
let response = local_admin_pool_response(
|
||||
&state,
|
||||
http::Method::GET,
|
||||
"/api/admin/pool/provider-openai/keys?page=1&page_size=50&status=all",
|
||||
None,
|
||||
)
|
||||
.await;
|
||||
|
||||
assert_eq!(response.status(), StatusCode::OK);
|
||||
let payload: serde_json::Value = serde_json::from_slice(
|
||||
&to_bytes(response.into_body(), usize::MAX)
|
||||
.await
|
||||
.expect("body should read"),
|
||||
)
|
||||
.expect("json body should parse");
|
||||
let keys = payload["keys"].as_array().expect("keys should be array");
|
||||
assert_eq!(keys.len(), 1);
|
||||
assert_eq!(keys[0]["request_count"], json!(1566));
|
||||
assert_eq!(keys[0]["total_tokens"], json!(187_327_321u64));
|
||||
assert_eq!(keys[0]["total_cost_usd"], json!("93.13192970"));
|
||||
assert!(keys[0]["lru_score"].is_null());
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn gateway_handles_admin_pool_list_keys_locally_with_trusted_admin_principal() {
|
||||
let upstream_hits = Arc::new(Mutex::new(0usize));
|
||||
@@ -563,6 +625,495 @@ async fn gateway_handles_admin_pool_list_keys_locally_with_trusted_admin_princip
|
||||
upstream_handle.abort();
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn gateway_handles_admin_pool_list_keys_with_quota_compatibility_fields() {
|
||||
let upstream_hits = Arc::new(Mutex::new(0usize));
|
||||
let upstream_hits_clone = Arc::clone(&upstream_hits);
|
||||
let upstream = Router::new().route(
|
||||
"/api/admin/pool/provider-antigravity/keys",
|
||||
any(move |_request: Request| {
|
||||
let upstream_hits_inner = Arc::clone(&upstream_hits_clone);
|
||||
async move {
|
||||
*upstream_hits_inner.lock().expect("mutex should lock") += 1;
|
||||
(StatusCode::OK, Body::from("unexpected upstream hit"))
|
||||
}
|
||||
}),
|
||||
);
|
||||
|
||||
let mut provider = sample_provider("provider-antigravity", "antigravity", 10)
|
||||
.with_transport_fields(
|
||||
true,
|
||||
false,
|
||||
true,
|
||||
None,
|
||||
None,
|
||||
None,
|
||||
None,
|
||||
None,
|
||||
Some(json!({
|
||||
"pool_advanced": {
|
||||
"enabled": true
|
||||
}
|
||||
})),
|
||||
);
|
||||
provider.provider_type = "antigravity".to_string();
|
||||
|
||||
let mut key = sample_key(
|
||||
"key-antigravity-a",
|
||||
"provider-antigravity",
|
||||
"gemini:chat",
|
||||
"sk-antigravity",
|
||||
);
|
||||
key.name = "quota-key".to_string();
|
||||
key.auth_type = "oauth".to_string();
|
||||
key.expires_at_unix_secs = Some(1_775_556_730);
|
||||
key.encrypted_auth_config = Some(
|
||||
encrypt_python_fernet_plaintext(
|
||||
DEVELOPMENT_ENCRYPTION_KEY,
|
||||
r#"{"plan_type":"pro","account_id":"acct-antigravity-1","account_name":"quota-user","account_user_id":"quota-user-1","organizations":[{"id":"org-1","name":"Org One"}]}"#,
|
||||
)
|
||||
.expect("auth config ciphertext should build"),
|
||||
);
|
||||
key.status_snapshot = Some(json!({
|
||||
"oauth": {
|
||||
"code": "expired",
|
||||
"label": "已过期",
|
||||
"reason": "Token 已过期,请重新授权",
|
||||
"expires_at": 1775556730u64,
|
||||
"invalid_at": null,
|
||||
"source": "expires_at",
|
||||
"requires_reauth": true,
|
||||
"expiring_soon": false
|
||||
},
|
||||
"account": {
|
||||
"code": "ok",
|
||||
"label": null,
|
||||
"reason": null,
|
||||
"blocked": false,
|
||||
"source": null,
|
||||
"recoverable": false
|
||||
},
|
||||
"quota": {
|
||||
"code": "ok",
|
||||
"label": null,
|
||||
"reason": null,
|
||||
"exhausted": false,
|
||||
"usage_ratio": 0.0,
|
||||
"updated_at": 1775553285u64,
|
||||
"reset_seconds": null,
|
||||
"plan_type": null
|
||||
}
|
||||
}));
|
||||
key.upstream_metadata = Some(json!({
|
||||
"antigravity": {
|
||||
"updated_at": 1775553285u64,
|
||||
"quota_by_model": {
|
||||
"gemini-2.5-flash": { "used_percent": 0.0 },
|
||||
"gemini-2.5-pro": { "used_percent": 0.0 }
|
||||
}
|
||||
}
|
||||
}));
|
||||
|
||||
let provider_catalog_repository = Arc::new(InMemoryProviderCatalogReadRepository::seed(
|
||||
vec![provider],
|
||||
Vec::new(),
|
||||
vec![key],
|
||||
));
|
||||
|
||||
let (_upstream_url, upstream_handle) = start_server(upstream).await;
|
||||
let gateway = build_router_with_state(
|
||||
AppState::new()
|
||||
.expect("gateway should build")
|
||||
.with_data_state_for_tests(GatewayDataState::with_provider_catalog_reader_for_tests(
|
||||
provider_catalog_repository,
|
||||
)),
|
||||
);
|
||||
let (gateway_url, gateway_handle) = start_server(gateway).await;
|
||||
|
||||
let response = reqwest::Client::new()
|
||||
.get(format!(
|
||||
"{gateway_url}/api/admin/pool/provider-antigravity/keys?page=1&page_size=10&status=all"
|
||||
))
|
||||
.header(crate::constants::GATEWAY_HEADER, "rust-phase3b")
|
||||
.header(TRUSTED_ADMIN_USER_ID_HEADER, "admin-user-123")
|
||||
.header(TRUSTED_ADMIN_USER_ROLE_HEADER, "admin")
|
||||
.header(TRUSTED_ADMIN_SESSION_ID_HEADER, "session-123")
|
||||
.send()
|
||||
.await
|
||||
.expect("request should succeed");
|
||||
|
||||
assert_eq!(response.status(), StatusCode::OK);
|
||||
let payload: serde_json::Value = response.json().await.expect("json body should parse");
|
||||
let keys = payload["keys"].as_array().expect("keys should be array");
|
||||
assert_eq!(keys.len(), 1);
|
||||
assert_eq!(keys[0]["account_quota"], json!("最低剩余 100.0% (2 模型)"));
|
||||
assert_eq!(keys[0]["quota_updated_at"], json!(1775553285u64));
|
||||
assert_eq!(keys[0]["oauth_expires_at"], json!(1775556730u64));
|
||||
assert_eq!(keys[0]["oauth_plan_type"], json!("pro"));
|
||||
assert_eq!(keys[0]["oauth_account_id"], json!("acct-antigravity-1"));
|
||||
assert_eq!(keys[0]["oauth_account_name"], json!("quota-user"));
|
||||
assert_eq!(keys[0]["oauth_account_user_id"], json!("quota-user-1"));
|
||||
assert_eq!(keys[0]["oauth_organizations"][0]["id"], json!("org-1"));
|
||||
assert_eq!(keys[0]["account_status_code"], json!("ok"));
|
||||
assert_eq!(keys[0]["account_status_blocked"], json!(false));
|
||||
assert_eq!(*upstream_hits.lock().expect("mutex should lock"), 0);
|
||||
|
||||
gateway_handle.abort();
|
||||
upstream_handle.abort();
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn gateway_includes_pool_quota_and_compat_fields_in_list_keys_response() {
|
||||
let mut provider = sample_provider("provider-antigravity", "antigravity", 10)
|
||||
.with_transport_fields(
|
||||
true,
|
||||
false,
|
||||
true,
|
||||
None,
|
||||
None,
|
||||
None,
|
||||
None,
|
||||
None,
|
||||
Some(json!({
|
||||
"pool_advanced": {
|
||||
"enabled": true
|
||||
}
|
||||
})),
|
||||
);
|
||||
provider.provider_type = "antigravity".to_string();
|
||||
let mut key = sample_key(
|
||||
"key-antigravity-oauth",
|
||||
"provider-antigravity",
|
||||
"gemini:chat",
|
||||
"oauth-placeholder",
|
||||
);
|
||||
key.name = "quota key".to_string();
|
||||
key.auth_type = "oauth".to_string();
|
||||
key.expires_at_unix_secs = Some(1_775_556_730);
|
||||
key.encrypted_auth_config = Some(
|
||||
encrypt_python_fernet_plaintext(
|
||||
DEVELOPMENT_ENCRYPTION_KEY,
|
||||
&json!({
|
||||
"plan_type": "pro",
|
||||
"account_id": "acct-demo-001",
|
||||
"account_name": "Demo Account",
|
||||
"account_user_id": "user-demo-001",
|
||||
"organizations": [],
|
||||
})
|
||||
.to_string(),
|
||||
)
|
||||
.expect("auth config should encrypt"),
|
||||
);
|
||||
key.upstream_metadata = Some(json!({
|
||||
"antigravity": {
|
||||
"updated_at": 1_775_553_285u64,
|
||||
"quota_by_model": {
|
||||
"gemini-2.5-pro": { "used_percent": 0 },
|
||||
"gemini-2.5-flash": { "used_percent": 0 }
|
||||
}
|
||||
}
|
||||
}));
|
||||
key.status_snapshot = Some(json!({
|
||||
"oauth": {
|
||||
"code": "expired",
|
||||
"label": "已过期",
|
||||
"reason": "Token 已过期,请重新授权",
|
||||
"expires_at": 1_775_556_730u64,
|
||||
"invalid_at": serde_json::Value::Null,
|
||||
"source": "expires_at",
|
||||
"requires_reauth": true,
|
||||
"expiring_soon": false
|
||||
},
|
||||
"account": {
|
||||
"code": "ok",
|
||||
"label": serde_json::Value::Null,
|
||||
"reason": serde_json::Value::Null,
|
||||
"blocked": false,
|
||||
"source": serde_json::Value::Null,
|
||||
"recoverable": false
|
||||
},
|
||||
"quota": {
|
||||
"code": "ok",
|
||||
"label": serde_json::Value::Null,
|
||||
"reason": serde_json::Value::Null,
|
||||
"exhausted": false,
|
||||
"usage_ratio": 0.0,
|
||||
"updated_at": 1_775_553_285u64,
|
||||
"reset_seconds": serde_json::Value::Null,
|
||||
"plan_type": serde_json::Value::Null
|
||||
}
|
||||
}));
|
||||
|
||||
let provider_catalog_repository = Arc::new(InMemoryProviderCatalogReadRepository::seed(
|
||||
vec![provider],
|
||||
Vec::new(),
|
||||
vec![key],
|
||||
));
|
||||
let state = AppState::new()
|
||||
.expect("gateway should build")
|
||||
.with_data_state_for_tests(
|
||||
GatewayDataState::with_provider_catalog_reader_for_tests(provider_catalog_repository)
|
||||
.with_encryption_key_for_tests(DEVELOPMENT_ENCRYPTION_KEY),
|
||||
);
|
||||
|
||||
let response = local_admin_pool_response(
|
||||
&state,
|
||||
http::Method::GET,
|
||||
"/api/admin/pool/provider-antigravity/keys?page=1&page_size=50&status=all",
|
||||
None,
|
||||
)
|
||||
.await;
|
||||
|
||||
assert_eq!(response.status(), StatusCode::OK);
|
||||
let payload: serde_json::Value = serde_json::from_slice(
|
||||
&to_bytes(response.into_body(), usize::MAX)
|
||||
.await
|
||||
.expect("body should read"),
|
||||
)
|
||||
.expect("json body should parse");
|
||||
let keys = payload["keys"].as_array().expect("keys should be array");
|
||||
assert_eq!(keys.len(), 1);
|
||||
assert_eq!(keys[0]["account_quota"], "最低剩余 100.0% (2 模型)");
|
||||
assert_eq!(keys[0]["quota_updated_at"], json!(1_775_553_285u64));
|
||||
assert_eq!(keys[0]["oauth_expires_at"], json!(1_775_556_730u64));
|
||||
assert_eq!(keys[0]["oauth_plan_type"], "pro");
|
||||
assert_eq!(keys[0]["oauth_account_id"], "acct-demo-001");
|
||||
assert_eq!(keys[0]["oauth_account_name"], "Demo Account");
|
||||
assert_eq!(keys[0]["oauth_account_user_id"], "user-demo-001");
|
||||
assert_eq!(keys[0]["oauth_organizations"], json!([]));
|
||||
assert_eq!(keys[0]["account_status_code"], "ok");
|
||||
assert_eq!(keys[0]["account_status_blocked"], json!(false));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn gateway_formats_codex_quota_countdown_from_reset_after_seconds() {
|
||||
let mut provider = sample_provider("provider-codex", "codex", 10).with_transport_fields(
|
||||
true,
|
||||
false,
|
||||
true,
|
||||
None,
|
||||
None,
|
||||
None,
|
||||
None,
|
||||
None,
|
||||
Some(json!({
|
||||
"pool_advanced": {
|
||||
"enabled": true
|
||||
}
|
||||
})),
|
||||
);
|
||||
provider.provider_type = "codex".to_string();
|
||||
|
||||
let mut key = sample_key(
|
||||
"key-codex-oauth",
|
||||
"provider-codex",
|
||||
"openai:cli",
|
||||
"oauth-placeholder",
|
||||
);
|
||||
key.name = "codex quota key".to_string();
|
||||
key.auth_type = "oauth".to_string();
|
||||
key.upstream_metadata = Some(json!({
|
||||
"codex": {
|
||||
"plan_type": "plus",
|
||||
"updated_at": 1_775_553_285u64,
|
||||
"primary_used_percent": 10.0,
|
||||
"primary_reset_after_seconds": 266_400,
|
||||
"secondary_used_percent": 33.0,
|
||||
"secondary_reset_after_seconds": 13_800
|
||||
}
|
||||
}));
|
||||
|
||||
let provider_catalog_repository = Arc::new(InMemoryProviderCatalogReadRepository::seed(
|
||||
vec![provider],
|
||||
Vec::new(),
|
||||
vec![key],
|
||||
));
|
||||
let state = AppState::new()
|
||||
.expect("gateway should build")
|
||||
.with_data_state_for_tests(GatewayDataState::with_provider_catalog_reader_for_tests(
|
||||
provider_catalog_repository,
|
||||
));
|
||||
|
||||
let response = local_admin_pool_response(
|
||||
&state,
|
||||
http::Method::GET,
|
||||
"/api/admin/pool/provider-codex/keys?page=1&page_size=50&status=all",
|
||||
None,
|
||||
)
|
||||
.await;
|
||||
|
||||
assert_eq!(response.status(), StatusCode::OK);
|
||||
let payload: serde_json::Value = serde_json::from_slice(
|
||||
&to_bytes(response.into_body(), usize::MAX)
|
||||
.await
|
||||
.expect("body should read"),
|
||||
)
|
||||
.expect("json body should parse");
|
||||
let keys = payload["keys"].as_array().expect("keys should be array");
|
||||
assert_eq!(keys.len(), 1);
|
||||
assert_eq!(
|
||||
keys[0]["account_quota"],
|
||||
"周剩余 90.0% (3天2小时后重置) | 5H剩余 67.0% (3小时50分钟后重置)"
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn gateway_pool_prefers_upstream_plan_type_over_auth_config() {
|
||||
let mut provider = sample_provider("provider-codex", "codex", 10).with_transport_fields(
|
||||
true,
|
||||
false,
|
||||
true,
|
||||
None,
|
||||
None,
|
||||
None,
|
||||
None,
|
||||
None,
|
||||
Some(json!({
|
||||
"pool_advanced": {
|
||||
"enabled": true
|
||||
}
|
||||
})),
|
||||
);
|
||||
provider.provider_type = "codex".to_string();
|
||||
let mut key = sample_key(
|
||||
"key-codex-precedence",
|
||||
"provider-codex",
|
||||
"openai:cli",
|
||||
"oauth-placeholder",
|
||||
);
|
||||
key.auth_type = "oauth".to_string();
|
||||
key.encrypted_auth_config = Some(
|
||||
encrypt_python_fernet_plaintext(
|
||||
DEVELOPMENT_ENCRYPTION_KEY,
|
||||
&json!({
|
||||
"plan_type": "free",
|
||||
"account_id": "acct-codex-legacy"
|
||||
})
|
||||
.to_string(),
|
||||
)
|
||||
.expect("auth config should encrypt"),
|
||||
);
|
||||
key.upstream_metadata = Some(json!({
|
||||
"codex": {
|
||||
"plan_type": "plus",
|
||||
"updated_at": 1_775_553_285u64
|
||||
}
|
||||
}));
|
||||
|
||||
let provider_catalog_repository = Arc::new(InMemoryProviderCatalogReadRepository::seed(
|
||||
vec![provider],
|
||||
Vec::new(),
|
||||
vec![key],
|
||||
));
|
||||
let state = AppState::new()
|
||||
.expect("gateway should build")
|
||||
.with_data_state_for_tests(
|
||||
GatewayDataState::with_provider_catalog_reader_for_tests(provider_catalog_repository)
|
||||
.with_encryption_key_for_tests(DEVELOPMENT_ENCRYPTION_KEY),
|
||||
);
|
||||
|
||||
let response = local_admin_pool_response(
|
||||
&state,
|
||||
http::Method::GET,
|
||||
"/api/admin/pool/provider-codex/keys?page=1&page_size=50&status=all",
|
||||
None,
|
||||
)
|
||||
.await;
|
||||
|
||||
assert_eq!(response.status(), StatusCode::OK);
|
||||
let payload: serde_json::Value = serde_json::from_slice(
|
||||
&to_bytes(response.into_body(), usize::MAX)
|
||||
.await
|
||||
.expect("body should read"),
|
||||
)
|
||||
.expect("json body should parse");
|
||||
let keys = payload["keys"].as_array().expect("keys should be array");
|
||||
assert_eq!(keys.len(), 1);
|
||||
assert_eq!(keys[0]["oauth_plan_type"], "plus");
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn gateway_pool_plan_free_selector_prefers_upstream_plan_type() {
|
||||
let mut provider = sample_provider("provider-codex", "codex", 10).with_transport_fields(
|
||||
true,
|
||||
false,
|
||||
true,
|
||||
None,
|
||||
None,
|
||||
None,
|
||||
None,
|
||||
None,
|
||||
Some(json!({
|
||||
"pool_advanced": {
|
||||
"enabled": true
|
||||
}
|
||||
})),
|
||||
);
|
||||
provider.provider_type = "codex".to_string();
|
||||
let mut key = sample_key(
|
||||
"key-codex-selector",
|
||||
"provider-codex",
|
||||
"openai:cli",
|
||||
"oauth-placeholder",
|
||||
);
|
||||
key.auth_type = "oauth".to_string();
|
||||
key.encrypted_auth_config = Some(
|
||||
encrypt_python_fernet_plaintext(
|
||||
DEVELOPMENT_ENCRYPTION_KEY,
|
||||
&json!({
|
||||
"plan_type": "free",
|
||||
"account_id": "acct-codex-legacy"
|
||||
})
|
||||
.to_string(),
|
||||
)
|
||||
.expect("auth config should encrypt"),
|
||||
);
|
||||
key.upstream_metadata = Some(json!({
|
||||
"codex": {
|
||||
"plan_type": "plus",
|
||||
"updated_at": 1_775_553_285u64
|
||||
}
|
||||
}));
|
||||
|
||||
let provider_catalog_repository = Arc::new(InMemoryProviderCatalogReadRepository::seed(
|
||||
vec![provider],
|
||||
Vec::new(),
|
||||
vec![key],
|
||||
));
|
||||
let state = AppState::new()
|
||||
.expect("gateway should build")
|
||||
.with_data_state_for_tests(
|
||||
GatewayDataState::with_provider_catalog_reader_for_tests(provider_catalog_repository)
|
||||
.with_encryption_key_for_tests(DEVELOPMENT_ENCRYPTION_KEY),
|
||||
);
|
||||
|
||||
let response = local_admin_pool_response(
|
||||
&state,
|
||||
http::Method::POST,
|
||||
"/api/admin/pool/provider-codex/keys/resolve-selection",
|
||||
Some(json!({
|
||||
"quick_selectors": ["plan_free"]
|
||||
})),
|
||||
)
|
||||
.await;
|
||||
|
||||
assert_eq!(response.status(), StatusCode::OK);
|
||||
let payload: serde_json::Value = serde_json::from_slice(
|
||||
&to_bytes(response.into_body(), usize::MAX)
|
||||
.await
|
||||
.expect("body should read"),
|
||||
)
|
||||
.expect("json body should parse");
|
||||
assert_eq!(payload["total"], json!(0));
|
||||
assert_eq!(
|
||||
payload["items"]
|
||||
.as_array()
|
||||
.expect("items should be array")
|
||||
.len(),
|
||||
0
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn gateway_handles_admin_pool_resolve_selection_locally_with_trusted_admin_principal() {
|
||||
let upstream_hits = Arc::new(Mutex::new(0usize));
|
||||
|
||||
@@ -1,16 +1,17 @@
|
||||
use std::sync::{Arc, Mutex};
|
||||
|
||||
use aether_data::repository::global_models::InMemoryGlobalModelReadRepository;
|
||||
use aether_contracts::ExecutionPlan;
|
||||
use aether_crypto::DEVELOPMENT_ENCRYPTION_KEY;
|
||||
use aether_data::repository::provider_catalog::InMemoryProviderCatalogReadRepository;
|
||||
use aether_data_contracts::repository::provider_catalog::StoredProviderCatalogEndpoint;
|
||||
use axum::body::Body;
|
||||
use axum::routing::any;
|
||||
use axum::{extract::Request, Router};
|
||||
use axum::{extract::Request, Json, Router};
|
||||
use http::StatusCode;
|
||||
use serde_json::json;
|
||||
|
||||
use super::super::{
|
||||
build_router_with_state, sample_admin_global_model, sample_admin_provider_model, sample_key,
|
||||
build_router_with_state, build_state_with_execution_runtime_override, sample_key,
|
||||
sample_provider, start_server, AppState,
|
||||
};
|
||||
use crate::constants::{
|
||||
@@ -63,22 +64,48 @@ async fn assert_admin_provider_query_route(
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn gateway_handles_admin_provider_query_models_locally_with_trusted_admin_principal() {
|
||||
let upstream_hits = Arc::new(Mutex::new(0usize));
|
||||
let upstream_hits_clone = Arc::clone(&upstream_hits);
|
||||
let upstream = Router::new().route(
|
||||
"/api/admin/provider-query/models",
|
||||
any(move |_request: Request| {
|
||||
let upstream_hits_inner = Arc::clone(&upstream_hits_clone);
|
||||
async fn gateway_handles_admin_provider_query_models_fetches_upstream_for_selected_key() {
|
||||
let execution_runtime_hits = Arc::new(Mutex::new(0usize));
|
||||
let execution_runtime_hits_clone = Arc::clone(&execution_runtime_hits);
|
||||
let execution_runtime = Router::new().route(
|
||||
"/v1/execute/sync",
|
||||
any(move |Json(plan): Json<ExecutionPlan>| {
|
||||
let execution_runtime_hits_inner = Arc::clone(&execution_runtime_hits_clone);
|
||||
async move {
|
||||
*upstream_hits_inner.lock().expect("mutex should lock") += 1;
|
||||
(StatusCode::OK, Body::from("unexpected upstream hit"))
|
||||
*execution_runtime_hits_inner
|
||||
.lock()
|
||||
.expect("mutex should lock") += 1;
|
||||
assert_eq!(plan.url, "https://api.openai.example/v1/models");
|
||||
assert_eq!(
|
||||
plan.headers.get("authorization").map(String::as_str),
|
||||
Some("Bearer sk-test")
|
||||
);
|
||||
Json(json!({
|
||||
"request_id": "req-provider-query-selected",
|
||||
"status_code": 200,
|
||||
"headers": {
|
||||
"content-type": "application/json"
|
||||
},
|
||||
"body": {
|
||||
"json_body": {
|
||||
"data": [{
|
||||
"id": "LLM-Research/Llama-4-Maverick-17B-128E-Instruct",
|
||||
"object": "",
|
||||
"owned_by": "system",
|
||||
"created": 1732517497u64
|
||||
}]
|
||||
}
|
||||
}
|
||||
}))
|
||||
}
|
||||
}),
|
||||
);
|
||||
|
||||
let (execution_runtime_url, execution_runtime_handle) = start_server(execution_runtime).await;
|
||||
let mut provider = sample_provider("provider-openai", "OpenAI", 10);
|
||||
provider.provider_type = "openai".to_string();
|
||||
let provider_catalog_repository = Arc::new(InMemoryProviderCatalogReadRepository::seed(
|
||||
vec![sample_provider("provider-openai", "OpenAI", 10)],
|
||||
vec![provider],
|
||||
vec![StoredProviderCatalogEndpoint::new(
|
||||
"endpoint-openai-chat".to_string(),
|
||||
"provider-openai".to_string(),
|
||||
@@ -89,7 +116,7 @@ async fn gateway_handles_admin_provider_query_models_locally_with_trusted_admin_
|
||||
)
|
||||
.expect("endpoint should build")
|
||||
.with_transport_fields(
|
||||
"https://api.openai.com/v1".to_string(),
|
||||
"https://api.openai.example".to_string(),
|
||||
None,
|
||||
None,
|
||||
None,
|
||||
@@ -99,67 +126,20 @@ async fn gateway_handles_admin_provider_query_models_locally_with_trusted_admin_
|
||||
None,
|
||||
)
|
||||
.expect("endpoint transport should build")],
|
||||
vec![
|
||||
{
|
||||
let mut key = sample_key(
|
||||
"key-openai-allowed",
|
||||
"provider-openai",
|
||||
"openai:chat",
|
||||
"sk-test",
|
||||
);
|
||||
key.allowed_models = Some(json!(["gpt-5"]));
|
||||
key
|
||||
},
|
||||
sample_key(
|
||||
"key-openai-all",
|
||||
"provider-openai",
|
||||
"openai:chat",
|
||||
"sk-test-2",
|
||||
),
|
||||
],
|
||||
vec![sample_key(
|
||||
"key-openai-selected",
|
||||
"provider-openai",
|
||||
"openai:chat",
|
||||
"sk-test",
|
||||
)],
|
||||
));
|
||||
let global_model_repository = Arc::new(
|
||||
InMemoryGlobalModelReadRepository::seed(Vec::new())
|
||||
.with_admin_global_models(vec![
|
||||
sample_admin_global_model("global-gpt-5", "gpt-5", "GPT 5"),
|
||||
sample_admin_global_model("global-gpt-4.1", "gpt-4.1", "GPT 4.1"),
|
||||
])
|
||||
.with_admin_provider_models(vec![
|
||||
{
|
||||
let mut model = sample_admin_provider_model(
|
||||
"provider-model-gpt-5",
|
||||
"provider-openai",
|
||||
"global-gpt-5",
|
||||
"gpt-5",
|
||||
);
|
||||
model.global_model_name = Some("gpt-5".to_string());
|
||||
model.global_model_display_name = Some("GPT 5".to_string());
|
||||
model
|
||||
},
|
||||
{
|
||||
let mut model = sample_admin_provider_model(
|
||||
"provider-model-gpt-4.1",
|
||||
"provider-openai",
|
||||
"global-gpt-4.1",
|
||||
"gpt-4.1",
|
||||
);
|
||||
model.global_model_name = Some("gpt-4.1".to_string());
|
||||
model.global_model_display_name = Some("GPT 4.1".to_string());
|
||||
model
|
||||
},
|
||||
]),
|
||||
);
|
||||
|
||||
let (upstream_url, upstream_handle) = start_server(upstream).await;
|
||||
let gateway = build_router_with_state(
|
||||
AppState::new()
|
||||
.expect("gateway should build")
|
||||
.with_data_state_for_tests(
|
||||
GatewayDataState::with_provider_catalog_reader_for_tests(
|
||||
provider_catalog_repository,
|
||||
)
|
||||
.with_global_model_repository_for_tests(global_model_repository),
|
||||
),
|
||||
build_state_with_execution_runtime_override(execution_runtime_url)
|
||||
.with_data_state_for_tests(GatewayDataState::with_provider_transport_reader_for_tests(
|
||||
provider_catalog_repository,
|
||||
DEVELOPMENT_ENCRYPTION_KEY.to_string(),
|
||||
)),
|
||||
);
|
||||
let (gateway_url, gateway_handle) = start_server(gateway).await;
|
||||
|
||||
@@ -171,7 +151,7 @@ async fn gateway_handles_admin_provider_query_models_locally_with_trusted_admin_
|
||||
.header(TRUSTED_ADMIN_SESSION_ID_HEADER, "session-123")
|
||||
.json(&json!({
|
||||
"provider_id": "provider-openai",
|
||||
"api_key_id": "key-openai-allowed"
|
||||
"api_key_id": "key-openai-selected"
|
||||
}))
|
||||
.send()
|
||||
.await
|
||||
@@ -184,33 +164,167 @@ async fn gateway_handles_admin_provider_query_models_locally_with_trusted_admin_
|
||||
assert_eq!(payload["provider"]["name"], "OpenAI");
|
||||
assert_eq!(payload["provider"]["display_name"], "OpenAI");
|
||||
assert_eq!(payload["data"]["error"], serde_json::Value::Null);
|
||||
assert_eq!(payload["data"]["from_cache"], json!(true));
|
||||
assert_eq!(payload["data"]["from_cache"], json!(false));
|
||||
assert_eq!(payload["data"]["keys_total"], serde_json::Value::Null);
|
||||
let models = payload["data"]["models"]
|
||||
.as_array()
|
||||
.expect("models should be an array");
|
||||
assert_eq!(models.len(), 1);
|
||||
assert_eq!(
|
||||
models[0]["id"],
|
||||
json!("LLM-Research/Llama-4-Maverick-17B-128E-Instruct")
|
||||
);
|
||||
assert_eq!(models[0]["owned_by"], json!("system"));
|
||||
assert_eq!(models[0]["api_formats"], json!(["openai:chat"]));
|
||||
assert_eq!(
|
||||
*execution_runtime_hits.lock().expect("mutex should lock"),
|
||||
1
|
||||
);
|
||||
|
||||
gateway_handle.abort();
|
||||
execution_runtime_handle.abort();
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn gateway_handles_admin_provider_query_models_aggregating_active_keys() {
|
||||
let execution_runtime_hits = Arc::new(Mutex::new(0usize));
|
||||
let execution_runtime_hits_clone = Arc::clone(&execution_runtime_hits);
|
||||
let execution_runtime = Router::new().route(
|
||||
"/v1/execute/sync",
|
||||
any(move |Json(plan): Json<ExecutionPlan>| {
|
||||
let execution_runtime_hits_inner = Arc::clone(&execution_runtime_hits_clone);
|
||||
async move {
|
||||
*execution_runtime_hits_inner
|
||||
.lock()
|
||||
.expect("mutex should lock") += 1;
|
||||
assert_eq!(plan.url, "https://api.openai.example/v1/models");
|
||||
let auth = plan
|
||||
.headers
|
||||
.get("authorization")
|
||||
.map(String::as_str)
|
||||
.unwrap_or_default()
|
||||
.to_string();
|
||||
let body = if auth == "Bearer sk-test-1" {
|
||||
json!({
|
||||
"data": [{
|
||||
"id": "gpt-5",
|
||||
"api_formats": ["openai:chat"],
|
||||
"object": "model",
|
||||
"owned_by": "system",
|
||||
"created": 1732517497u64
|
||||
}]
|
||||
})
|
||||
} else {
|
||||
json!({
|
||||
"data": [{
|
||||
"id": "gpt-4.1",
|
||||
"api_formats": ["openai:chat"],
|
||||
"object": "model",
|
||||
"owned_by": "system",
|
||||
"created": 1732517498u64
|
||||
}]
|
||||
})
|
||||
};
|
||||
Json(json!({
|
||||
"request_id": format!("req-provider-query-{auth}"),
|
||||
"status_code": 200,
|
||||
"headers": {
|
||||
"content-type": "application/json"
|
||||
},
|
||||
"body": {
|
||||
"json_body": body
|
||||
}
|
||||
}))
|
||||
}
|
||||
}),
|
||||
);
|
||||
|
||||
let (execution_runtime_url, execution_runtime_handle) = start_server(execution_runtime).await;
|
||||
let mut provider = sample_provider("provider-openai", "OpenAI", 10);
|
||||
provider.provider_type = "openai".to_string();
|
||||
let provider_catalog_repository = Arc::new(InMemoryProviderCatalogReadRepository::seed(
|
||||
vec![provider],
|
||||
vec![StoredProviderCatalogEndpoint::new(
|
||||
"endpoint-openai-chat".to_string(),
|
||||
"provider-openai".to_string(),
|
||||
"openai:chat".to_string(),
|
||||
Some("chat".to_string()),
|
||||
Some("primary".to_string()),
|
||||
true,
|
||||
)
|
||||
.expect("endpoint should build")
|
||||
.with_transport_fields(
|
||||
"https://api.openai.example".to_string(),
|
||||
None,
|
||||
None,
|
||||
None,
|
||||
None,
|
||||
None,
|
||||
None,
|
||||
None,
|
||||
)
|
||||
.expect("endpoint transport should build")],
|
||||
vec![
|
||||
sample_key(
|
||||
"key-openai-1",
|
||||
"provider-openai",
|
||||
"openai:chat",
|
||||
"sk-test-1",
|
||||
),
|
||||
sample_key(
|
||||
"key-openai-2",
|
||||
"provider-openai",
|
||||
"openai:chat",
|
||||
"sk-test-2",
|
||||
),
|
||||
],
|
||||
));
|
||||
|
||||
let gateway = build_router_with_state(
|
||||
build_state_with_execution_runtime_override(execution_runtime_url)
|
||||
.with_data_state_for_tests(GatewayDataState::with_provider_transport_reader_for_tests(
|
||||
provider_catalog_repository,
|
||||
DEVELOPMENT_ENCRYPTION_KEY.to_string(),
|
||||
)),
|
||||
);
|
||||
let (gateway_url, gateway_handle) = start_server(gateway).await;
|
||||
|
||||
let response = reqwest::Client::new()
|
||||
.post(format!("{gateway_url}/api/admin/provider-query/models"))
|
||||
.header(crate::constants::GATEWAY_HEADER, "rust-phase3b")
|
||||
.header(TRUSTED_ADMIN_USER_ID_HEADER, "admin-user-123")
|
||||
.header(TRUSTED_ADMIN_USER_ROLE_HEADER, "admin")
|
||||
.header(TRUSTED_ADMIN_SESSION_ID_HEADER, "session-123")
|
||||
.json(&json!({
|
||||
"provider_id": "provider-openai"
|
||||
}))
|
||||
.send()
|
||||
.await
|
||||
.expect("request should succeed");
|
||||
|
||||
assert_eq!(response.status(), StatusCode::OK);
|
||||
let payload: serde_json::Value = response.json().await.expect("json body should parse");
|
||||
assert_eq!(payload["success"], json!(true));
|
||||
assert_eq!(payload["data"]["from_cache"], json!(false));
|
||||
assert_eq!(payload["data"]["keys_total"], json!(2));
|
||||
assert_eq!(payload["data"]["keys_cached"], json!(0));
|
||||
assert_eq!(payload["data"]["keys_fetched"], json!(2));
|
||||
let models = payload["data"]["models"]
|
||||
.as_array()
|
||||
.expect("models should be an array");
|
||||
assert_eq!(models.len(), 2);
|
||||
let model_ids: Vec<_> = models
|
||||
let model_ids = models
|
||||
.iter()
|
||||
.map(|model| {
|
||||
(
|
||||
model["id"].as_str().expect("id should be present"),
|
||||
model["display_name"]
|
||||
.as_str()
|
||||
.expect("display_name should be present"),
|
||||
)
|
||||
})
|
||||
.collect();
|
||||
assert_eq!(model_ids, vec![("gpt-4.1", "GPT 4.1"), ("gpt-5", "GPT 5")]);
|
||||
for model in models {
|
||||
assert_eq!(model["owned_by"], "OpenAI");
|
||||
assert_eq!(model["api_format"], "openai:chat");
|
||||
assert_eq!(model["api_formats"], json!(["openai:chat"]));
|
||||
}
|
||||
assert_eq!(*upstream_hits.lock().expect("mutex should lock"), 0);
|
||||
.map(|model| model["id"].as_str().expect("id should exist"))
|
||||
.collect::<Vec<_>>();
|
||||
assert_eq!(model_ids, vec!["gpt-5", "gpt-4.1"]);
|
||||
assert_eq!(
|
||||
*execution_runtime_hits.lock().expect("mutex should lock"),
|
||||
2
|
||||
);
|
||||
|
||||
gateway_handle.abort();
|
||||
upstream_handle.abort();
|
||||
execution_runtime_handle.abort();
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
|
||||
@@ -351,6 +351,9 @@ pub(super) fn sample_admin_global_model(
|
||||
})),
|
||||
Some(json!(["streaming", "vision"])),
|
||||
Some(json!({"streaming": true, "vision": false, "billing": {"currency": "USD"}})),
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
Some(1_711_000_000),
|
||||
Some(1_711_000_100),
|
||||
)
|
||||
|
||||
@@ -174,6 +174,85 @@ async fn gateway_handles_public_openai_models_without_hitting_fallback_probe() {
|
||||
fallback_probe_handle.abort();
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn gateway_handles_public_openai_models_with_cross_format_candidates_without_hitting_fallback_probe(
|
||||
) {
|
||||
let fallback_probe_hits = Arc::new(Mutex::new(0usize));
|
||||
let fallback_probe_hits_clone = Arc::clone(&fallback_probe_hits);
|
||||
let fallback_probe = Router::new().route(
|
||||
"/{*path}",
|
||||
any(move |_request: Request| {
|
||||
let fallback_probe_hits_inner = Arc::clone(&fallback_probe_hits_clone);
|
||||
async move {
|
||||
*fallback_probe_hits_inner.lock().expect("mutex should lock") += 1;
|
||||
(StatusCode::OK, Body::from("proxied"))
|
||||
}
|
||||
}),
|
||||
);
|
||||
|
||||
let auth_repository = Arc::new(InMemoryAuthApiKeySnapshotRepository::seed(vec![(
|
||||
Some(hash_api_key("sk-openai-models-cross-format")),
|
||||
unrestricted_models_snapshot("key-1", "user-1"),
|
||||
)]));
|
||||
let candidate_repository =
|
||||
Arc::new(InMemoryMinimalCandidateSelectionReadRepository::seed(vec![
|
||||
sample_models_candidate_row(
|
||||
"provider-claude",
|
||||
"claude",
|
||||
"claude:chat",
|
||||
"claude-3-7-sonnet",
|
||||
10,
|
||||
),
|
||||
]));
|
||||
|
||||
let (_unused_fallback_probe_url, fallback_probe_handle) = start_server(fallback_probe).await;
|
||||
let gateway = build_router_with_state(
|
||||
AppState::new()
|
||||
.expect("gateway should build")
|
||||
.with_data_state_for_tests(
|
||||
crate::data::GatewayDataState::with_minimal_candidate_selection_and_auth_for_tests(
|
||||
candidate_repository,
|
||||
auth_repository,
|
||||
),
|
||||
),
|
||||
);
|
||||
let (gateway_url, gateway_handle) = start_server(gateway).await;
|
||||
|
||||
let client = reqwest::Client::new();
|
||||
let list_response = client
|
||||
.get(format!("{gateway_url}/v1/models"))
|
||||
.header("authorization", "Bearer sk-openai-models-cross-format")
|
||||
.send()
|
||||
.await
|
||||
.expect("request should succeed");
|
||||
|
||||
assert_eq!(list_response.status(), StatusCode::OK);
|
||||
let list_payload: serde_json::Value =
|
||||
list_response.json().await.expect("json body should parse");
|
||||
assert_eq!(list_payload["object"], "list");
|
||||
assert_eq!(list_payload["data"][0]["id"], "claude-3-7-sonnet");
|
||||
assert_eq!(list_payload["data"][0]["owned_by"], "claude");
|
||||
|
||||
let detail_response = client
|
||||
.get(format!("{gateway_url}/v1/models/claude-3-7-sonnet"))
|
||||
.header("authorization", "Bearer sk-openai-models-cross-format")
|
||||
.send()
|
||||
.await
|
||||
.expect("request should succeed");
|
||||
assert_eq!(detail_response.status(), StatusCode::OK);
|
||||
let detail_payload: serde_json::Value = detail_response
|
||||
.json()
|
||||
.await
|
||||
.expect("json body should parse");
|
||||
assert_eq!(detail_payload["id"], "claude-3-7-sonnet");
|
||||
assert_eq!(detail_payload["owned_by"], "claude");
|
||||
|
||||
assert_eq!(*fallback_probe_hits.lock().expect("mutex should lock"), 0);
|
||||
|
||||
gateway_handle.abort();
|
||||
fallback_probe_handle.abort();
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn gateway_handles_public_claude_models_without_hitting_fallback_probe() {
|
||||
let fallback_probe_hits = Arc::new(Mutex::new(0usize));
|
||||
|
||||
@@ -1,5 +1,9 @@
|
||||
use std::fs;
|
||||
use std::time::{SystemTime, UNIX_EPOCH};
|
||||
|
||||
use crate::tests::{
|
||||
any, build_router, start_server, Arc, Body, Mutex, Request, Router, StatusCode, READYZ_PATH,
|
||||
any, attach_static_frontend, build_router, start_server, Arc, Body, Mutex, Request, Router,
|
||||
StatusCode, READYZ_PATH,
|
||||
};
|
||||
|
||||
#[tokio::test]
|
||||
@@ -112,64 +116,69 @@ async fn gateway_handles_public_service_health_without_proxying_upstream() {
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn gateway_handles_public_root_without_proxying_upstream() {
|
||||
let upstream_hits = Arc::new(Mutex::new(0usize));
|
||||
let upstream_hits_clone = Arc::clone(&upstream_hits);
|
||||
let upstream = Router::new().route(
|
||||
"/{*path}",
|
||||
any(move |_request: Request| {
|
||||
let upstream_hits_inner = Arc::clone(&upstream_hits_clone);
|
||||
async move {
|
||||
*upstream_hits_inner.lock().expect("mutex should lock") += 1;
|
||||
(StatusCode::OK, Body::from("proxied"))
|
||||
}
|
||||
}),
|
||||
);
|
||||
async fn gateway_serves_frontend_routes_and_assets_without_shadowing_public_api() {
|
||||
let unique_suffix = SystemTime::now()
|
||||
.duration_since(UNIX_EPOCH)
|
||||
.expect("system clock should be monotonic enough for tests")
|
||||
.as_nanos();
|
||||
let static_dir =
|
||||
std::env::temp_dir().join(format!("aether-gateway-static-test-{unique_suffix}"));
|
||||
let assets_dir = static_dir.join("assets");
|
||||
fs::create_dir_all(&assets_dir).expect("static assets dir should be created");
|
||||
fs::write(
|
||||
static_dir.join("index.html"),
|
||||
"<!doctype html><html><body>Aether Frontend</body></html>",
|
||||
)
|
||||
.expect("index.html should be written");
|
||||
fs::write(assets_dir.join("app.js"), "console.log('frontend asset');")
|
||||
.expect("asset file should be written");
|
||||
|
||||
let (upstream_url, upstream_handle) = start_server(upstream).await;
|
||||
let gateway = build_router().expect("gateway should build");
|
||||
let gateway =
|
||||
attach_static_frontend(build_router().expect("gateway should build"), &static_dir);
|
||||
let (gateway_url, gateway_handle) = start_server(gateway).await;
|
||||
let client = reqwest::Client::new();
|
||||
|
||||
let response = reqwest::Client::new()
|
||||
let response = client
|
||||
.get(format!("{gateway_url}/"))
|
||||
.send()
|
||||
.await
|
||||
.expect("request should succeed");
|
||||
|
||||
assert_eq!(response.status(), StatusCode::OK);
|
||||
let payload: serde_json::Value = response.json().await.expect("json body should parse");
|
||||
assert_eq!(payload["status"], "running");
|
||||
let content_type = response
|
||||
.headers()
|
||||
.get("content-type")
|
||||
.and_then(|value| value.to_str().ok())
|
||||
.unwrap_or_default()
|
||||
.to_string();
|
||||
let body = response.text().await.expect("html body should be readable");
|
||||
assert!(content_type.starts_with("text/html"));
|
||||
assert!(body.contains("Aether Frontend"));
|
||||
|
||||
let response = client
|
||||
.get(format!("{gateway_url}/guide"))
|
||||
.send()
|
||||
.await
|
||||
.expect("spa request should succeed");
|
||||
assert_eq!(response.status(), StatusCode::OK);
|
||||
let body = response.text().await.expect("spa body should be readable");
|
||||
assert!(body.contains("Aether Frontend"));
|
||||
|
||||
let response = client
|
||||
.get(format!("{gateway_url}/assets/app.js"))
|
||||
.send()
|
||||
.await
|
||||
.expect("asset request should succeed");
|
||||
assert_eq!(response.status(), StatusCode::OK);
|
||||
assert_eq!(
|
||||
payload["message"],
|
||||
"AI Proxy with Modular Architecture v4.0.0"
|
||||
);
|
||||
assert_eq!(payload["endpoints"]["health"], "/v1/health");
|
||||
assert_eq!(*upstream_hits.lock().expect("mutex should lock"), 0);
|
||||
|
||||
gateway_handle.abort();
|
||||
upstream_handle.abort();
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn gateway_handles_public_site_info_without_proxying_upstream() {
|
||||
let upstream_hits = Arc::new(Mutex::new(0usize));
|
||||
let upstream_hits_clone = Arc::clone(&upstream_hits);
|
||||
let upstream = Router::new().route(
|
||||
"/{*path}",
|
||||
any(move |_request: Request| {
|
||||
let upstream_hits_inner = Arc::clone(&upstream_hits_clone);
|
||||
async move {
|
||||
*upstream_hits_inner.lock().expect("mutex should lock") += 1;
|
||||
(StatusCode::OK, Body::from("proxied"))
|
||||
}
|
||||
}),
|
||||
response
|
||||
.text()
|
||||
.await
|
||||
.expect("asset body should be readable"),
|
||||
"console.log('frontend asset');"
|
||||
);
|
||||
|
||||
let (upstream_url, upstream_handle) = start_server(upstream).await;
|
||||
let gateway = build_router().expect("gateway should build");
|
||||
let (gateway_url, gateway_handle) = start_server(gateway).await;
|
||||
|
||||
let response = reqwest::Client::new()
|
||||
let response = client
|
||||
.get(format!("{gateway_url}/api/public/site-info"))
|
||||
.send()
|
||||
.await
|
||||
@@ -179,8 +188,7 @@ async fn gateway_handles_public_site_info_without_proxying_upstream() {
|
||||
let payload: serde_json::Value = response.json().await.expect("json body should parse");
|
||||
assert_eq!(payload["site_name"], "Aether");
|
||||
assert_eq!(payload["site_subtitle"], "AI Gateway");
|
||||
assert_eq!(*upstream_hits.lock().expect("mutex should lock"), 0);
|
||||
|
||||
gateway_handle.abort();
|
||||
upstream_handle.abort();
|
||||
let _ = fs::remove_dir_all(&static_dir);
|
||||
}
|
||||
|
||||
@@ -3871,15 +3871,15 @@ async fn gateway_handles_wallet_balance_locally_without_proxying_upstream() {
|
||||
|
||||
#[tokio::test]
|
||||
async fn gateway_handles_wallet_today_cost_locally_without_proxying_upstream() {
|
||||
let now = Utc::now();
|
||||
let auth_now = Utc::now();
|
||||
let usage_now = chrono::DateTime::<chrono::Utc>::from_naive_utc_and_offset(
|
||||
Utc::now()
|
||||
auth_now
|
||||
.date_naive()
|
||||
.and_hms_opt(12, 0, 0)
|
||||
.expect("midday should be valid"),
|
||||
chrono::Utc,
|
||||
);
|
||||
let user = sample_auth_user(now);
|
||||
let user = sample_auth_user(auth_now);
|
||||
let access_token = build_test_auth_token(
|
||||
"access",
|
||||
serde_json::Map::from_iter([
|
||||
@@ -3891,7 +3891,7 @@ async fn gateway_handles_wallet_today_cost_locally_without_proxying_upstream() {
|
||||
),
|
||||
("session_id".to_string(), json!("session-wallet-today-1")),
|
||||
]),
|
||||
now + chrono::Duration::hours(1),
|
||||
auth_now + chrono::Duration::hours(1),
|
||||
);
|
||||
let usage_repository = Arc::new(InMemoryUsageReadRepository::seed(vec![
|
||||
sample_user_usage_audit(
|
||||
@@ -3916,13 +3916,13 @@ async fn gateway_handles_wallet_today_cost_locally_without_proxying_upstream() {
|
||||
let (gateway_url, upstream_hits, gateway_handle, upstream_handle) =
|
||||
start_auth_gateway_with_usage_state(
|
||||
user,
|
||||
sample_auth_wallet("user-auth-1", now),
|
||||
sample_auth_wallet("user-auth-1", auth_now),
|
||||
[sample_auth_session(
|
||||
"user-auth-1",
|
||||
"session-wallet-today-1",
|
||||
"device-wallet-today-1",
|
||||
"refresh-token-placeholder",
|
||||
now,
|
||||
auth_now,
|
||||
)],
|
||||
usage_repository,
|
||||
)
|
||||
|
||||
@@ -25,7 +25,7 @@ pub(super) use super::async_task::VideoTaskTruthSourceMode;
|
||||
pub(super) use super::constants::*;
|
||||
pub(super) use super::fallback_metrics::{GatewayFallbackMetricKind, GatewayFallbackReason};
|
||||
pub(super) use super::rate_limit::FrontdoorUserRpmConfig;
|
||||
pub(super) use super::router::{build_router, build_router_with_state};
|
||||
pub(super) use super::router::{attach_static_frontend, build_router, build_router_with_state};
|
||||
pub(super) use super::state::{AppState, FrontdoorCorsConfig};
|
||||
pub(super) use super::usage::UsageRuntimeConfig;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user