mirror of
https://github.com/fawney19/Aether.git
synced 2026-09-01 17:00:21 +08:00
refactor: 简化 outcome clippy lint 并移动 usage 测试模块至文件末尾
This commit is contained in:
@@ -149,7 +149,7 @@ pub(crate) async fn record_failed_usage_for_exhausted_request(
|
|||||||
Value::String("application/json".to_string()),
|
Value::String("application/json".to_string()),
|
||||||
)]);
|
)]);
|
||||||
if let Some(reason) = diagnostic
|
if let Some(reason) = diagnostic
|
||||||
.and_then(|value| Some(value.reason.trim()))
|
.map(|value| value.reason.trim())
|
||||||
.filter(|value| !value.is_empty())
|
.filter(|value| !value.is_empty())
|
||||||
{
|
{
|
||||||
client_headers.insert(
|
client_headers.insert(
|
||||||
|
|||||||
@@ -235,80 +235,6 @@ pub fn admin_usage_provider_key_name(
|
|||||||
.or_else(|| admin_usage_request_metadata_string(item, "key_name"))
|
.or_else(|| admin_usage_request_metadata_string(item, "key_name"))
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
mod tests {
|
|
||||||
use super::{admin_usage_is_failed, admin_usage_matches_status};
|
|
||||||
use aether_data_contracts::repository::usage::StoredRequestUsageAudit;
|
|
||||||
|
|
||||||
fn sample_usage(
|
|
||||||
status: &str,
|
|
||||||
status_code: Option<i32>,
|
|
||||||
error_message: Option<&str>,
|
|
||||||
) -> StoredRequestUsageAudit {
|
|
||||||
StoredRequestUsageAudit::new(
|
|
||||||
"usage-1".to_string(),
|
|
||||||
"req-1".to_string(),
|
|
||||||
Some("user-1".to_string()),
|
|
||||||
Some("api-key-1".to_string()),
|
|
||||||
Some("alice".to_string()),
|
|
||||||
Some("default".to_string()),
|
|
||||||
"OpenAI".to_string(),
|
|
||||||
"gpt-5".to_string(),
|
|
||||||
None,
|
|
||||||
Some("provider-1".to_string()),
|
|
||||||
Some("endpoint-1".to_string()),
|
|
||||||
Some("provider-key-1".to_string()),
|
|
||||||
Some("chat".to_string()),
|
|
||||||
Some("openai:chat".to_string()),
|
|
||||||
Some("openai".to_string()),
|
|
||||||
Some("chat".to_string()),
|
|
||||||
Some("openai:chat".to_string()),
|
|
||||||
Some("openai".to_string()),
|
|
||||||
Some("chat".to_string()),
|
|
||||||
false,
|
|
||||||
false,
|
|
||||||
10,
|
|
||||||
20,
|
|
||||||
30,
|
|
||||||
0.0,
|
|
||||||
0.0,
|
|
||||||
status_code,
|
|
||||||
error_message.map(ToOwned::to_owned),
|
|
||||||
None,
|
|
||||||
Some(120),
|
|
||||||
None,
|
|
||||||
status.to_string(),
|
|
||||||
"settled".to_string(),
|
|
||||||
100,
|
|
||||||
101,
|
|
||||||
Some(102),
|
|
||||||
)
|
|
||||||
.expect("usage should build")
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn explicit_completed_status_wins_over_legacy_failure_fields() {
|
|
||||||
let item = sample_usage(
|
|
||||||
"completed",
|
|
||||||
Some(429),
|
|
||||||
Some("rate limited on first attempt"),
|
|
||||||
);
|
|
||||||
assert!(!admin_usage_is_failed(&item));
|
|
||||||
assert!(!admin_usage_matches_status(&item, Some("failed")));
|
|
||||||
assert!(admin_usage_matches_status(&item, Some("completed")));
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn legacy_failure_signals_still_work_when_status_is_missing() {
|
|
||||||
let item = StoredRequestUsageAudit {
|
|
||||||
status: String::new(),
|
|
||||||
..sample_usage("completed", Some(429), Some("rate limited"))
|
|
||||||
};
|
|
||||||
assert!(admin_usage_is_failed(&item));
|
|
||||||
assert!(admin_usage_matches_status(&item, Some("failed")));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn admin_usage_record_json(
|
pub fn admin_usage_record_json(
|
||||||
item: &StoredRequestUsageAudit,
|
item: &StoredRequestUsageAudit,
|
||||||
users_by_id: &BTreeMap<String, StoredUserSummary>,
|
users_by_id: &BTreeMap<String, StoredUserSummary>,
|
||||||
@@ -1372,3 +1298,77 @@ pub fn build_admin_usage_replay_plan_response(
|
|||||||
}))
|
}))
|
||||||
.into_response()
|
.into_response()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::{admin_usage_is_failed, admin_usage_matches_status};
|
||||||
|
use aether_data_contracts::repository::usage::StoredRequestUsageAudit;
|
||||||
|
|
||||||
|
fn sample_usage(
|
||||||
|
status: &str,
|
||||||
|
status_code: Option<i32>,
|
||||||
|
error_message: Option<&str>,
|
||||||
|
) -> StoredRequestUsageAudit {
|
||||||
|
StoredRequestUsageAudit::new(
|
||||||
|
"usage-1".to_string(),
|
||||||
|
"req-1".to_string(),
|
||||||
|
Some("user-1".to_string()),
|
||||||
|
Some("api-key-1".to_string()),
|
||||||
|
Some("alice".to_string()),
|
||||||
|
Some("default".to_string()),
|
||||||
|
"OpenAI".to_string(),
|
||||||
|
"gpt-5".to_string(),
|
||||||
|
None,
|
||||||
|
Some("provider-1".to_string()),
|
||||||
|
Some("endpoint-1".to_string()),
|
||||||
|
Some("provider-key-1".to_string()),
|
||||||
|
Some("chat".to_string()),
|
||||||
|
Some("openai:chat".to_string()),
|
||||||
|
Some("openai".to_string()),
|
||||||
|
Some("chat".to_string()),
|
||||||
|
Some("openai:chat".to_string()),
|
||||||
|
Some("openai".to_string()),
|
||||||
|
Some("chat".to_string()),
|
||||||
|
false,
|
||||||
|
false,
|
||||||
|
10,
|
||||||
|
20,
|
||||||
|
30,
|
||||||
|
0.0,
|
||||||
|
0.0,
|
||||||
|
status_code,
|
||||||
|
error_message.map(ToOwned::to_owned),
|
||||||
|
None,
|
||||||
|
Some(120),
|
||||||
|
None,
|
||||||
|
status.to_string(),
|
||||||
|
"settled".to_string(),
|
||||||
|
100,
|
||||||
|
101,
|
||||||
|
Some(102),
|
||||||
|
)
|
||||||
|
.expect("usage should build")
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn explicit_completed_status_wins_over_legacy_failure_fields() {
|
||||||
|
let item = sample_usage(
|
||||||
|
"completed",
|
||||||
|
Some(429),
|
||||||
|
Some("rate limited on first attempt"),
|
||||||
|
);
|
||||||
|
assert!(!admin_usage_is_failed(&item));
|
||||||
|
assert!(!admin_usage_matches_status(&item, Some("failed")));
|
||||||
|
assert!(admin_usage_matches_status(&item, Some("completed")));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn legacy_failure_signals_still_work_when_status_is_missing() {
|
||||||
|
let item = StoredRequestUsageAudit {
|
||||||
|
status: String::new(),
|
||||||
|
..sample_usage("completed", Some(429), Some("rate limited"))
|
||||||
|
};
|
||||||
|
assert!(admin_usage_is_failed(&item));
|
||||||
|
assert!(admin_usage_matches_status(&item, Some("failed")));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user