mirror of
https://github.com/zyycn/codex-proxy-rs.git
synced 2026-12-04 16:01:04 +00:00
Add Chinese responsibility summaries to every backend Rust source, test, and build script. Remove sentence-ending periods from comments while preserving meaningful dots in URLs, versions, identifiers, and examples. Keep generated SDK documentation in sync with its contract generator, group middleware module declarations and exports, and record the comment conventions in the development skill. Validated with Rustfmt, strict Clippy, backend tests, documentation tests, and skill validation. Database-backed integration cases were skipped because dedicated PostgreSQL and Redis test services were not configured. Co-authored-by: Codex <noreply@openai.com>
128 lines
3.8 KiB
Rust
128 lines
3.8 KiB
Rust
//! 计量测试入口,以及金额精度、用量与费用来源合同测试
|
|
|
|
use std::str::FromStr;
|
|
|
|
mod pricing;
|
|
|
|
use gateway_core::metering::{
|
|
CalculatedCost, CalculatedCostAmounts, CalculatedCostBreakdown, CalculatedCostRates,
|
|
CostEstimate, CostEstimateStatus, CostSource, CurrencyCode, Decimal, Money,
|
|
ProviderReportedCost, Usage,
|
|
};
|
|
|
|
#[test]
|
|
fn decimal_should_render_with_database_scale() {
|
|
let decimal = Decimal::from_str("12.34").expect("valid decimal");
|
|
assert_eq!(decimal.to_string(), "12.3400000000");
|
|
}
|
|
|
|
#[test]
|
|
fn decimal_canonical_should_strip_trailing_zeroes() {
|
|
assert_eq!(Decimal::from_str("12.34").unwrap().canonical(), "12.34");
|
|
assert_eq!(Decimal::from_str("12").unwrap().canonical(), "12");
|
|
assert_eq!(Decimal::from_str("0").unwrap().canonical(), "0");
|
|
assert_eq!(Decimal::from_str("1.0000000000").unwrap().canonical(), "1");
|
|
}
|
|
|
|
#[test]
|
|
fn decimal_div_u64_should_keep_up_to_ten_fraction_digits() {
|
|
let decimal = Decimal::from_str("10").unwrap();
|
|
let third = decimal.checked_div_u64(3).unwrap();
|
|
assert_eq!(third.canonical(), "3.3333333333");
|
|
assert_eq!(decimal.checked_div_u64(0), None);
|
|
}
|
|
|
|
#[test]
|
|
fn provider_usd_ticks_should_map_exactly_to_decimal_scale() {
|
|
let estimate = ProviderReportedCost::from_usd_ticks(12_345_678_901)
|
|
.expect("valid provider ticks")
|
|
.into_estimate();
|
|
assert_eq!(estimate.status(), CostEstimateStatus::Known);
|
|
assert_eq!(estimate.source(), CostSource::ProviderReported);
|
|
let total = estimate.total().expect("provider total");
|
|
assert_eq!(total.amount().scaled(), 12_345_678_901);
|
|
assert_eq!(total.currency().as_str(), "USD");
|
|
}
|
|
|
|
#[test]
|
|
fn calculated_usd_ticks_should_be_marked_as_calculated() {
|
|
let estimate = CalculatedCost::from_usd_ticks(12_345)
|
|
.expect("valid calculated cost")
|
|
.into_estimate();
|
|
|
|
assert_eq!(estimate.source(), CostSource::Calculated);
|
|
assert_eq!(
|
|
estimate.total().map(|total| total.amount().scaled()),
|
|
Some(12_345)
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn calculated_breakdown_should_preserve_runtime_components_without_changing_total_contract() {
|
|
let usd = CurrencyCode::new("USD").expect("USD currency");
|
|
let money = |ticks| Money::new(Decimal::from_scaled(ticks).expect("valid ticks"), usd);
|
|
let breakdown = CalculatedCostBreakdown::new(
|
|
CalculatedCostAmounts::new(
|
|
money(100),
|
|
money(200),
|
|
money(30),
|
|
money(0),
|
|
money(330),
|
|
money(330),
|
|
),
|
|
CalculatedCostRates::new(
|
|
money(10_000_000_000),
|
|
money(20_000_000_000),
|
|
money(3_000_000_000),
|
|
money(0),
|
|
),
|
|
Some("default".to_owned()),
|
|
100,
|
|
);
|
|
|
|
assert_eq!(breakdown.input_amount().amount().scaled(), 100);
|
|
assert_eq!(breakdown.standard_amount(), breakdown.total_amount());
|
|
assert_eq!(breakdown.service_tier(), Some("default"));
|
|
assert_eq!(breakdown.multiplier_percent(), 100);
|
|
assert_eq!(breakdown.calculated_cost().total().amount().scaled(), 330);
|
|
}
|
|
|
|
#[test]
|
|
fn decimal_should_reject_more_than_ten_fraction_digits() {
|
|
assert!(Decimal::from_str("1.00000000001").is_err());
|
|
}
|
|
|
|
#[test]
|
|
fn unknown_cost_should_not_fabricate_zero() {
|
|
assert_eq!(CostEstimate::unavailable().total(), None);
|
|
}
|
|
|
|
#[test]
|
|
fn usage_total_should_remain_independent() {
|
|
let usage = Usage {
|
|
input_tokens: Some(10),
|
|
output_tokens: Some(5),
|
|
total_tokens: Some(99),
|
|
..Usage::new()
|
|
};
|
|
|
|
assert_eq!(usage.total_tokens, Some(99));
|
|
}
|
|
|
|
#[test]
|
|
fn usage_merge_should_only_replace_observed_fields() {
|
|
let mut usage = Usage {
|
|
input_tokens: Some(10),
|
|
..Usage::new()
|
|
};
|
|
usage.merge(&Usage {
|
|
output_tokens: Some(5),
|
|
..Usage::new()
|
|
});
|
|
|
|
assert_eq!(
|
|
(usage.input_tokens, usage.output_tokens),
|
|
(Some(10), Some(5))
|
|
);
|
|
}
|