| 1 | //! Behavior tests for the offering pricing projection (#3085). |
| 2 | |
| 3 | use super::*; |
| 4 | use crate::catalog::{CatalogOffering, CatalogSource, bundled_offerings_from_models_dev}; |
| 5 | use crate::models_dev::{ModelsDevCatalog, ModelsDevCost}; |
| 6 | use crate::route::PricingSku; |
| 7 | |
| 8 | /// A DeepSeek-shaped priced offering (input/output/cache-read known, |
| 9 | /// cache-write deliberately unknown) tagged with the given provenance source. |
| 10 | fn priced(source: CatalogSource) -> CatalogOffering { |
| 11 | CatalogOffering { |
| 12 | provider: "deepseek".into(), |
| 13 | wire_model_id: "deepseek-v4-pro".into(), |
| 14 | canonical_model: Some("deepseek-v4-pro".into()), |
| 15 | endpoint_key: "chat".into(), |
| 16 | cost: Some(ModelsDevCost { |
| 17 | input: Some(0.28), |
| 18 | output: Some(0.42), |
| 19 | cache_read: Some(0.028), |
| 20 | cache_write: None, |
| 21 | ..Default::default() |
| 22 | }), |
| 23 | source, |
| 24 | ..Default::default() |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | #[test] |
| 29 | fn maps_models_dev_cost_with_bundled_provenance_in_usd() { |
| 30 | let p = |
| 31 | OfferingPricing::from_catalog_offering(&priced(CatalogSource::Bundled)).expect("priced"); |
| 32 | assert_eq!(p.currency, Currency::Usd); |
| 33 | assert_eq!(p.input_per_million, Some(0.28)); |
| 34 | assert_eq!(p.output_per_million, Some(0.42)); |
| 35 | assert_eq!(p.cache_read_per_million, Some(0.028)); |
| 36 | assert_eq!(p.cache_write_per_million, None); |
| 37 | assert_eq!(p.provenance, PricingProvenance::ModelsDevBundled); |
| 38 | assert_eq!(p.effective_at, None); |
| 39 | assert!(p.has_any_price()); |
| 40 | } |
| 41 | |
| 42 | #[test] |
| 43 | fn malformed_catalog_prices_fail_closed_at_every_projection() { |
| 44 | for invalid in [f64::NAN, f64::INFINITY, f64::NEG_INFINITY, -0.01] { |
| 45 | for field in 0..7 { |
| 46 | let mut offering = priced(CatalogSource::Bundled); |
| 47 | offering.provider = "openrouter".to_string(); |
| 48 | offering.wire_model_id = "openai/gpt-5.5".to_string(); |
| 49 | let cost = offering.cost.as_mut().expect("cost fixture"); |
| 50 | match field { |
| 51 | 0 => cost.input = Some(invalid), |
| 52 | 1 => cost.output = Some(invalid), |
| 53 | 2 => cost.cache_read = Some(invalid), |
| 54 | 3 => cost.cache_write = Some(invalid), |
| 55 | 4 => cost.reasoning = Some(invalid), |
| 56 | 5 => cost.input_audio = Some(invalid), |
| 57 | 6 => cost.output_audio = Some(invalid), |
| 58 | _ => unreachable!(), |
| 59 | } |
| 60 | assert!( |
| 61 | OfferingPricing::from_catalog_offering(&offering).is_none(), |
| 62 | "field {field} accepted {invalid:?}" |
| 63 | ); |
| 64 | assert_eq!( |
| 65 | route_pricing_sku(&offering), |
| 66 | PricingSku::UnknownOrStale, |
| 67 | "route projection accepted field {field} = {invalid:?}" |
| 68 | ); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | let zero = priced(CatalogSource::Bundled); |
| 73 | let mut zero = zero; |
| 74 | let cost = zero.cost.as_mut().expect("cost fixture"); |
| 75 | cost.input = Some(0.0); |
| 76 | cost.output = Some(0.0); |
| 77 | cost.cache_read = Some(0.0); |
| 78 | cost.cache_write = Some(0.0); |
| 79 | assert!(OfferingPricing::from_catalog_offering(&zero).is_some()); |
| 80 | } |
| 81 | |
| 82 | /// A price of an impossible magnitude is a unit error, not an expensive model. |
| 83 | /// |
| 84 | /// The two ways this happens in the wild are a per-token price parsed as |
| 85 | /// per-million (10^6 too large) and a minor-unit integer read as a major unit |
| 86 | /// (10^2 too large). Both would bill the user orders of magnitude over, so the |
| 87 | /// row is rejected at the same boundary that rejects NaN and negatives — and |
| 88 | /// rejected *whole*, so the surviving fields cannot produce a quietly |
| 89 | /// under-counted estimate instead. |
| 90 | #[test] |
| 91 | fn absurd_catalog_prices_fail_closed_at_every_projection() { |
| 92 | let absurd = [ |
| 93 | // $3/token, mistakenly published as a per-million rate. |
| 94 | 3_000_000.0, |
| 95 | // The declared bound itself is out, one ulp above is far out. |
| 96 | MAX_PLAUSIBLE_PRICE_PER_MILLION + 1.0, |
| 97 | f64::MAX, |
| 98 | ]; |
| 99 | for price in absurd { |
| 100 | for field in 0..7 { |
| 101 | let mut offering = priced(CatalogSource::Bundled); |
| 102 | let cost = offering.cost.as_mut().expect("cost fixture"); |
| 103 | match field { |
| 104 | 0 => cost.input = Some(price), |
| 105 | 1 => cost.output = Some(price), |
| 106 | 2 => cost.cache_read = Some(price), |
| 107 | 3 => cost.cache_write = Some(price), |
| 108 | 4 => cost.reasoning = Some(price), |
| 109 | 5 => cost.input_audio = Some(price), |
| 110 | 6 => cost.output_audio = Some(price), |
| 111 | _ => unreachable!(), |
| 112 | } |
| 113 | assert!( |
| 114 | !catalog_cost_is_valid(cost), |
| 115 | "field {field} accepted absurd price {price}" |
| 116 | ); |
| 117 | assert!( |
| 118 | OfferingPricing::from_catalog_offering(&offering).is_none(), |
| 119 | "field {field} produced pricing from absurd price {price}" |
| 120 | ); |
| 121 | assert_eq!( |
| 122 | route_pricing_sku(&offering), |
| 123 | PricingSku::UnknownOrStale, |
| 124 | "route projection accepted field {field} = {price}" |
| 125 | ); |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | // The bound is generous on purpose: a genuinely expensive published rate |
| 130 | // stays priced. Rejecting a real price would be its own kind of lie. |
| 131 | let mut expensive = priced(CatalogSource::Bundled); |
| 132 | let cost = expensive.cost.as_mut().expect("cost fixture"); |
| 133 | cost.input = Some(600.0); |
| 134 | cost.output = Some(2_400.0); |
| 135 | assert!( |
| 136 | OfferingPricing::from_catalog_offering(&expensive).is_some(), |
| 137 | "a plausible frontier rate must survive the magnitude bound" |
| 138 | ); |
| 139 | |
| 140 | // Exactly at the bound is still accepted; only values above it are not. |
| 141 | let mut boundary = priced(CatalogSource::Bundled); |
| 142 | let cost = boundary.cost.as_mut().expect("cost fixture"); |
| 143 | cost.input = Some(MAX_PLAUSIBLE_PRICE_PER_MILLION); |
| 144 | assert!(OfferingPricing::from_catalog_offering(&boundary).is_some()); |
| 145 | } |
| 146 | |
| 147 | #[test] |
| 148 | fn live_source_carries_provider_live_provenance_and_fetch_time() { |
| 149 | let src = CatalogSource::Live { |
| 150 | base_url_fingerprint: "fp".into(), |
| 151 | fetched_at: 1_700, |
| 152 | }; |
| 153 | let p = OfferingPricing::from_catalog_offering(&priced(src)).expect("priced"); |
| 154 | assert_eq!(p.provenance, PricingProvenance::ProviderLive); |
| 155 | assert_eq!(p.effective_at, None); |
| 156 | assert_eq!(p.fetched_at, Some(1_700)); |
| 157 | assert_eq!(p.endpoint_fingerprint.as_deref(), Some("fp")); |
| 158 | } |
| 159 | |
| 160 | /// A live row is only authoritative while it is *both* fresh and fetched from |
| 161 | /// the endpoint the turn was served on. Every other combination is a defect the |
| 162 | /// caller must fail closed on rather than billing against. |
| 163 | #[test] |
| 164 | fn live_pricing_defect_gates_stale_and_mismatched_rows() { |
| 165 | let live = OfferingPricing::from_catalog_offering(&priced(CatalogSource::Live { |
| 166 | base_url_fingerprint: "route-fp".into(), |
| 167 | fetched_at: 1_000, |
| 168 | })) |
| 169 | .expect("priced"); |
| 170 | |
| 171 | // Fresh + fingerprint-matched: authoritative. |
| 172 | assert_eq!( |
| 173 | live.live_pricing_defect(Some("route-fp"), Some(1_500), 1_000), |
| 174 | None |
| 175 | ); |
| 176 | |
| 177 | // Same age, at the window boundary: stale is inclusive, matching |
| 178 | // `is_stale`, so a row exactly at the TTL is not authoritative. |
| 179 | assert_eq!( |
| 180 | live.live_pricing_defect(Some("route-fp"), Some(2_000), 1_000), |
| 181 | Some(LivePricingDefect::Stale { |
| 182 | age_secs: 1_000, |
| 183 | max_age_secs: 1_000, |
| 184 | }) |
| 185 | ); |
| 186 | |
| 187 | // A row fetched from a different endpoint prices a different billing |
| 188 | // surface; it is never a "fresher price" for this route. |
| 189 | assert_eq!( |
| 190 | live.live_pricing_defect(Some("other-fp"), Some(1_500), 1_000), |
| 191 | Some(LivePricingDefect::EndpointMismatch { |
| 192 | row_fingerprint: "route-fp".into(), |
| 193 | route_fingerprint: "other-fp".into(), |
| 194 | }) |
| 195 | ); |
| 196 | |
| 197 | // An unknown route endpoint cannot confirm any live row. |
| 198 | assert_eq!( |
| 199 | live.live_pricing_defect(None, Some(1_500), 1_000), |
| 200 | Some(LivePricingDefect::UnknownRouteEndpoint) |
| 201 | ); |
| 202 | |
| 203 | // No clock means the age is unknowable, so the row stays unproven rather |
| 204 | // than being assumed fresh. |
| 205 | assert_eq!( |
| 206 | live.live_pricing_defect(Some("route-fp"), None, 1_000), |
| 207 | Some(LivePricingDefect::MissingTimestamp) |
| 208 | ); |
| 209 | |
| 210 | // Non-live provenances carry no fetch clock and are not age-gated here. |
| 211 | for source in [CatalogSource::Bundled, CatalogSource::UserOverride] { |
| 212 | let row = OfferingPricing::from_catalog_offering(&priced(source)).expect("priced"); |
| 213 | assert_eq!(row.live_pricing_defect(None, Some(u64::MAX), 1), None); |
| 214 | assert!(row.provenance.is_authoritative_without_freshness_check()); |
| 215 | } |
| 216 | assert!(!PricingProvenance::ProviderLive.is_authoritative_without_freshness_check()); |
| 217 | } |
| 218 | |
| 219 | /// A live row that claims live provenance but lost its fingerprint (hand-built |
| 220 | /// or migrated from an older schema) cannot be matched to a route. |
| 221 | #[test] |
| 222 | fn live_row_without_a_fingerprint_is_never_authoritative() { |
| 223 | let mut live = OfferingPricing::from_catalog_offering(&priced(CatalogSource::Live { |
| 224 | base_url_fingerprint: "fp".into(), |
| 225 | fetched_at: 1_000, |
| 226 | })) |
| 227 | .expect("priced"); |
| 228 | live.endpoint_fingerprint = None; |
| 229 | assert_eq!( |
| 230 | live.live_pricing_defect(Some("fp"), Some(1_001), 1_000), |
| 231 | Some(LivePricingDefect::MissingEndpointFingerprint) |
| 232 | ); |
| 233 | |
| 234 | // Defect labels are stable, non-localized, and carry no URL — only the |
| 235 | // non-secret FNV digests the catalog already scopes caches on. |
| 236 | let mismatch = LivePricingDefect::EndpointMismatch { |
| 237 | row_fingerprint: "a".into(), |
| 238 | route_fingerprint: "b".into(), |
| 239 | }; |
| 240 | assert_eq!(mismatch.label(), "live_pricing_endpoint_mismatch"); |
| 241 | let json = serde_json::to_string(&mismatch).expect("serialize defect"); |
| 242 | assert!(!json.contains("http"), "{json}"); |
| 243 | } |
| 244 | |
| 245 | #[test] |
| 246 | fn no_cost_or_empty_cost_object_is_unknown() { |
| 247 | let mut offering = priced(CatalogSource::Bundled); |
| 248 | offering.cost = None; |
| 249 | assert!( |
| 250 | OfferingPricing::from_catalog_offering(&offering).is_none(), |
| 251 | "absent cost is unknown, not free" |
| 252 | ); |
| 253 | |
| 254 | // A cost object present but with no concrete price is still unknown. |
| 255 | offering.cost = Some(ModelsDevCost::default()); |
| 256 | let withheld = |
| 257 | OfferingPricing::from_catalog_offering(&offering).expect("explicit withheld rate"); |
| 258 | assert!(withheld.rate_withheld); |
| 259 | assert_eq!(withheld.estimate_cost(&TokenUsage::default()), None); |
| 260 | } |
| 261 | |
| 262 | #[test] |
| 263 | fn estimate_cost_sums_priced_classes() { |
| 264 | let p = OfferingPricing::from_catalog_offering(&priced(CatalogSource::Bundled)).unwrap(); |
| 265 | // 1M input @0.28 + 0.5M output @0.42 + 2M cache_read @0.028 = 0.546 |
| 266 | let usage = TokenUsage { |
| 267 | input: 1_000_000, |
| 268 | output: 500_000, |
| 269 | cache_read: 2_000_000, |
| 270 | cache_write: 0, |
| 271 | |
| 272 | reasoning: None, |
| 273 | input_audio: None, |
| 274 | output_audio: None, |
| 275 | |
| 276 | request_usages: Vec::new(), |
| 277 | }; |
| 278 | let cost = p.estimate_cost(&usage).expect("priced classes estimate"); |
| 279 | assert!((cost - 0.546).abs() < 1e-9, "got {cost}"); |
| 280 | } |
| 281 | |
| 282 | #[test] |
| 283 | fn estimate_cost_is_none_when_a_used_class_is_unpriced() { |
| 284 | // cache_write price is unknown; charging cache-write tokens cannot be |
| 285 | // estimated honestly, so the whole estimate is None rather than under-reported. |
| 286 | let p = OfferingPricing::from_catalog_offering(&priced(CatalogSource::Bundled)).unwrap(); |
| 287 | let usage = TokenUsage { |
| 288 | input: 100, |
| 289 | output: 0, |
| 290 | cache_read: 0, |
| 291 | cache_write: 10, |
| 292 | |
| 293 | reasoning: None, |
| 294 | input_audio: None, |
| 295 | output_audio: None, |
| 296 | |
| 297 | request_usages: Vec::new(), |
| 298 | }; |
| 299 | assert!(p.estimate_cost(&usage).is_none()); |
| 300 | } |
| 301 | |
| 302 | #[test] |
| 303 | fn estimate_cost_with_zero_usage_is_zero() { |
| 304 | let p = OfferingPricing::from_catalog_offering(&priced(CatalogSource::Bundled)).unwrap(); |
| 305 | assert_eq!(p.estimate_cost(&TokenUsage::default()), Some(0.0)); |
| 306 | } |
| 307 | |
| 308 | #[test] |
| 309 | fn finite_rates_that_overflow_the_computed_total_fail_closed() { |
| 310 | // Constructed directly rather than through `from_catalog_offering`: the |
| 311 | // magnitude bound now rejects a rate this large at the catalog boundary, so |
| 312 | // the only way to reach the estimator with one is to bypass that boundary. |
| 313 | // The estimator keeps its own overflow guard regardless — it is the last |
| 314 | // check before a number becomes money, and it must not depend on an earlier |
| 315 | // layer having run. |
| 316 | let pricing = OfferingPricing { |
| 317 | provider: "deepseek".to_string(), |
| 318 | wire_model_id: "deepseek-v4-pro".to_string(), |
| 319 | canonical_model: Some("deepseek-v4-pro".to_string()), |
| 320 | currency: Currency::Usd, |
| 321 | input_per_million: Some(f64::MAX), |
| 322 | output_per_million: None, |
| 323 | cache_read_per_million: None, |
| 324 | cache_write_per_million: None, |
| 325 | provenance: PricingProvenance::ModelsDevBundled, |
| 326 | effective_at: None, |
| 327 | endpoint_fingerprint: None, |
| 328 | |
| 329 | ..Default::default() |
| 330 | }; |
| 331 | let usage = TokenUsage { |
| 332 | input: u64::MAX, |
| 333 | ..TokenUsage::default() |
| 334 | }; |
| 335 | assert_eq!(pricing.estimate_cost(&usage), None); |
| 336 | |
| 337 | // And the boundary itself refuses to hand such a row over in the first |
| 338 | // place, so the guard above is defence in depth, not the only defence. |
| 339 | let mut offering = priced(CatalogSource::Bundled); |
| 340 | offering.cost.as_mut().expect("cost fixture").input = Some(f64::MAX); |
| 341 | assert!(OfferingPricing::from_catalog_offering(&offering).is_none()); |
| 342 | } |
| 343 | |
| 344 | #[test] |
| 345 | fn route_pricing_sku_is_token_when_priced_and_unknown_otherwise() { |
| 346 | match route_pricing_sku(&priced(CatalogSource::Bundled)) { |
| 347 | PricingSku::Token { |
| 348 | input_per_mtok, |
| 349 | output_per_mtok, |
| 350 | } => { |
| 351 | assert_eq!(input_per_mtok, Some(0.28)); |
| 352 | assert_eq!(output_per_mtok, Some(0.42)); |
| 353 | } |
| 354 | other => panic!("expected Token, got {other:?}"), |
| 355 | } |
| 356 | |
| 357 | // No cost → honest UnknownOrStale, never a fabricated zero price. |
| 358 | let mut unpriced = priced(CatalogSource::Bundled); |
| 359 | unpriced.cost = None; |
| 360 | assert!(matches!( |
| 361 | route_pricing_sku(&unpriced), |
| 362 | PricingSku::UnknownOrStale |
| 363 | )); |
| 364 | } |
| 365 | |
| 366 | #[test] |
| 367 | fn currency_round_trips_including_other() { |
| 368 | for currency in [Currency::Usd, Currency::Cny, Currency::Other("eur".into())] { |
| 369 | let json = serde_json::to_string(¤cy).expect("serialize"); |
| 370 | let back: Currency = serde_json::from_str(&json).expect("deserialize"); |
| 371 | assert_eq!(currency, back); |
| 372 | } |
| 373 | } |
| 374 | |
| 375 | #[test] |
| 376 | fn user_override_pricing_round_trips_and_carries_no_secrets() { |
| 377 | let pricing = OfferingPricing { |
| 378 | provider: "custom".into(), |
| 379 | wire_model_id: "house-model".into(), |
| 380 | canonical_model: None, |
| 381 | currency: Currency::Cny, |
| 382 | input_per_million: Some(8.0), |
| 383 | output_per_million: Some(16.0), |
| 384 | cache_read_per_million: None, |
| 385 | cache_write_per_million: None, |
| 386 | provenance: PricingProvenance::UserOverride, |
| 387 | effective_at: None, |
| 388 | endpoint_fingerprint: None, |
| 389 | |
| 390 | ..Default::default() |
| 391 | }; |
| 392 | let json = serde_json::to_string_pretty(&pricing).expect("serialize"); |
| 393 | let back: OfferingPricing = serde_json::from_str(&json).expect("round-trip"); |
| 394 | assert_eq!(pricing, back); |
| 395 | |
| 396 | let lower = json.to_lowercase(); |
| 397 | for needle in [ |
| 398 | "api_key", |
| 399 | "apikey", |
| 400 | "authorization", |
| 401 | "secret", |
| 402 | "password", |
| 403 | "bearer", |
| 404 | "access_token", |
| 405 | ] { |
| 406 | assert!(!lower.contains(needle), "pricing JSON contains `{needle}`"); |
| 407 | } |
| 408 | } |
| 409 | |
| 410 | #[test] |
| 411 | fn staleness_applies_to_live_rows_only() { |
| 412 | let live = CatalogSource::Live { |
| 413 | base_url_fingerprint: "fp".into(), |
| 414 | fetched_at: 1_000, |
| 415 | }; |
| 416 | let live_price = OfferingPricing::from_catalog_offering(&priced(live)).unwrap(); |
| 417 | assert!(!live_price.is_stale(1_500, 3_600), "within TTL"); |
| 418 | assert!(live_price.is_stale(5_000, 3_600), "past TTL"); |
| 419 | |
| 420 | // A bundled price has no fetch clock and is not age-stale. |
| 421 | let bundled = OfferingPricing::from_catalog_offering(&priced(CatalogSource::Bundled)).unwrap(); |
| 422 | assert!(!bundled.is_stale(u64::MAX, 1)); |
| 423 | } |
| 424 | |
| 425 | #[test] |
| 426 | fn pricing_flows_from_the_models_dev_parser() { |
| 427 | let raw = r#"{ |
| 428 | "providers": { |
| 429 | "zai": { |
| 430 | "models": { |
| 431 | "glm-5.2": { |
| 432 | "id": "glm-5.2", |
| 433 | "modalities": { "input": ["text"], "output": ["text"] }, |
| 434 | "cost": { "input": 1.4, "output": 4.4, "cache_read": 0.26 } |
| 435 | } |
| 436 | } |
| 437 | } |
| 438 | } |
| 439 | }"#; |
| 440 | let catalog = ModelsDevCatalog::parse_json(raw).expect("fixture parses"); |
| 441 | let rows = bundled_offerings_from_models_dev(&catalog); |
| 442 | let pricing = OfferingPricing::from_catalog_offering(&rows[0]).expect("zai glm-5.2 is priced"); |
| 443 | |
| 444 | assert_eq!(pricing.provider, "zai"); |
| 445 | assert_eq!(pricing.wire_model_id, "glm-5.2"); |
| 446 | assert_eq!(pricing.input_per_million, Some(1.4)); |
| 447 | assert_eq!(pricing.output_per_million, Some(4.4)); |
| 448 | assert_eq!(pricing.cache_read_per_million, Some(0.26)); |
| 449 | assert_eq!(pricing.provenance, PricingProvenance::ModelsDevBundled); |
| 450 | } |
| 451 | |
| 452 | #[test] |
| 453 | fn cache_only_offering_is_unknown_at_the_route_layer() { |
| 454 | // Priced only on cache classes (no input/output): the route Token badge |
| 455 | // would have no visible rates, so route_pricing_sku degrades to |
| 456 | // UnknownOrStale — yet the cache rate is still usable for estimate_cost. |
| 457 | let mut offering = priced(CatalogSource::Bundled); |
| 458 | offering.cost = Some(ModelsDevCost { |
| 459 | input: None, |
| 460 | output: None, |
| 461 | cache_read: Some(0.028), |
| 462 | cache_write: None, |
| 463 | ..Default::default() |
| 464 | }); |
| 465 | |
| 466 | assert!(matches!( |
| 467 | route_pricing_sku(&offering), |
| 468 | PricingSku::UnknownOrStale |
| 469 | )); |
| 470 | |
| 471 | let pricing = |
| 472 | OfferingPricing::from_catalog_offering(&offering).expect("cache-only row is still priced"); |
| 473 | assert!(pricing.has_any_price()); |
| 474 | let usage = TokenUsage { |
| 475 | cache_read: 1_000_000, |
| 476 | ..Default::default() |
| 477 | }; |
| 478 | assert_eq!(pricing.estimate_cost(&usage), Some(0.028)); |
| 479 | } |
| 480 | |
| 481 | #[test] |
| 482 | fn user_override_source_maps_through_from_catalog_offering() { |
| 483 | // Exercises provenance_from_source / effective_at_from_source for the |
| 484 | // override arm via the hydration path (not direct construction). |
| 485 | let pricing = OfferingPricing::from_catalog_offering(&priced(CatalogSource::UserOverride)) |
| 486 | .expect("priced"); |
| 487 | assert_eq!(pricing.provenance, PricingProvenance::UserOverride); |
| 488 | assert_eq!(pricing.effective_at, None); |
| 489 | } |
| 490 | |
| 491 | #[test] |
| 492 | fn staleness_is_inclusive_at_the_ttl_boundary() { |
| 493 | let live = CatalogSource::Live { |
| 494 | base_url_fingerprint: "fp".into(), |
| 495 | fetched_at: 1_000, |
| 496 | }; |
| 497 | let p = OfferingPricing::from_catalog_offering(&priced(live)).unwrap(); |
| 498 | // age == max_age_secs counts as stale (`>=` semantics)... |
| 499 | assert!(p.is_stale(1_100, 100)); |
| 500 | // ...one second younger is still fresh. |
| 501 | assert!(!p.is_stale(1_099, 100)); |
| 502 | } |
| 503 | |
| 504 | #[test] |
| 505 | fn unpriced_used_classes_names_exactly_what_makes_an_estimate_fail_closed() { |
| 506 | // The DeepSeek-shaped row publishes input/output/cache-read but no |
| 507 | // cache-write rate. |
| 508 | let pricing = OfferingPricing::from_catalog_offering(&priced(CatalogSource::Bundled)) |
| 509 | .expect("priced row"); |
| 510 | |
| 511 | // A turn that never wrote to cache is fully priced. |
| 512 | let no_write = TokenUsage { |
| 513 | input: 1_000_000, |
| 514 | output: 1_000_000, |
| 515 | cache_read: 1_000_000, |
| 516 | cache_write: 0, |
| 517 | |
| 518 | reasoning: None, |
| 519 | input_audio: None, |
| 520 | output_audio: None, |
| 521 | |
| 522 | request_usages: Vec::new(), |
| 523 | }; |
| 524 | assert!(pricing.unpriced_used_classes(&no_write).is_empty()); |
| 525 | assert_eq!(pricing.estimate_cost(&no_write), Some(0.28 + 0.42 + 0.028)); |
| 526 | |
| 527 | // The moment cache-write tokens appear, the estimate fails closed and the |
| 528 | // audit names the single class responsible. |
| 529 | let with_write = TokenUsage { |
| 530 | cache_write: 1, |
| 531 | ..no_write |
| 532 | }; |
| 533 | assert_eq!( |
| 534 | pricing.unpriced_used_classes(&with_write), |
| 535 | vec![TokenClass::CacheWrite] |
| 536 | ); |
| 537 | assert_eq!(pricing.estimate_cost(&with_write), None); |
| 538 | |
| 539 | // Zero-token classes never count as unpriced. |
| 540 | let empty = TokenUsage::default(); |
| 541 | assert!(pricing.unpriced_used_classes(&empty).is_empty()); |
| 542 | assert_eq!(pricing.estimate_cost(&empty), Some(0.0)); |
| 543 | } |
| 544 | |
| 545 | #[test] |
| 546 | fn token_class_labels_and_counts_stay_aligned_with_token_usage() { |
| 547 | let usage = TokenUsage { |
| 548 | input: 1, |
| 549 | output: 2, |
| 550 | cache_read: 3, |
| 551 | cache_write: 4, |
| 552 | |
| 553 | reasoning: None, |
| 554 | input_audio: None, |
| 555 | output_audio: None, |
| 556 | |
| 557 | request_usages: Vec::new(), |
| 558 | }; |
| 559 | let seen: Vec<(&str, u64)> = TokenClass::ALL |
| 560 | .into_iter() |
| 561 | .map(|class| (class.label(), class.tokens(&usage))) |
| 562 | .collect(); |
| 563 | assert_eq!( |
| 564 | seen, |
| 565 | vec![ |
| 566 | ("input", 1), |
| 567 | ("output", 2), |
| 568 | ("cache_read", 3), |
| 569 | ("cache_write", 4), |
| 570 | ("reasoning", 0), |
| 571 | ("input_audio", 0), |
| 572 | ("output_audio", 0), |
| 573 | ] |
| 574 | ); |
| 575 | } |
| 576 | |
| 577 | #[test] |
| 578 | fn text_usage_prices_multimodal_and_equal_rate_reasoning_while_distinct_reasoning_fails_closed() { |
| 579 | let gemini_carbon = OfferingPricing { |
| 580 | provider: "google".into(), |
| 581 | wire_model_id: "gemini-4-carbon".into(), |
| 582 | currency: Currency::Usd, |
| 583 | input_per_million: Some(2.0), |
| 584 | output_per_million: Some(10.0), |
| 585 | cache_read_per_million: Some(0.1), |
| 586 | input_audio_per_million: Some(2.0), |
| 587 | provenance: PricingProvenance::ModelsDevBundled, |
| 588 | ..Default::default() |
| 589 | }; |
| 590 | let text_turn = TokenUsage { |
| 591 | input: 4_963, |
| 592 | output: 46, |
| 593 | cache_read: 131_324, |
| 594 | ..Default::default() |
| 595 | }; |
| 596 | let carbon_cost = gemini_carbon |
| 597 | .estimate_cost(&text_turn) |
| 598 | .expect("equal-rate input_audio must not block text turns"); |
| 599 | let expected_carbon = 4_963.0 * 2e-6 + 131_324.0 * 0.1e-6 + 46.0 * 10e-6; |
| 600 | assert!( |
| 601 | (carbon_cost - expected_carbon).abs() < 1e-12, |
| 602 | "{carbon_cost} != {expected_carbon}" |
| 603 | ); |
| 604 | |
| 605 | let flash_lite = OfferingPricing { |
| 606 | provider: "google".into(), |
| 607 | wire_model_id: "gemini-3.1-flash-lite".into(), |
| 608 | currency: Currency::Usd, |
| 609 | input_per_million: Some(0.25), |
| 610 | output_per_million: Some(1.5), |
| 611 | cache_read_per_million: Some(0.025), |
| 612 | input_audio_per_million: Some(0.5), |
| 613 | provenance: PricingProvenance::ModelsDevBundled, |
| 614 | ..Default::default() |
| 615 | }; |
| 616 | let plain_text = TokenUsage { |
| 617 | input: 1_000_000, |
| 618 | output: 100_000, |
| 619 | ..Default::default() |
| 620 | }; |
| 621 | assert!( |
| 622 | (flash_lite |
| 623 | .estimate_cost(&plain_text) |
| 624 | .expect("text turn on audio-capable model prices") |
| 625 | - 0.4) |
| 626 | .abs() |
| 627 | < 1e-12 |
| 628 | ); |
| 629 | let audio_turn = TokenUsage { |
| 630 | input: 1_000_000, |
| 631 | output: 0, |
| 632 | input_audio: Some(400_000), |
| 633 | ..Default::default() |
| 634 | }; |
| 635 | assert!( |
| 636 | (flash_lite |
| 637 | .estimate_cost(&audio_turn) |
| 638 | .expect("explicit audio tokens bill at audio rate") |
| 639 | - (0.6 * 0.25 + 0.4 * 0.5)) |
| 640 | .abs() |
| 641 | < 1e-12 |
| 642 | ); |
| 643 | let audio_with_cache = TokenUsage { |
| 644 | input: 1_000_000, |
| 645 | cache_read: 100_000, |
| 646 | input_audio: Some(400_000), |
| 647 | ..Default::default() |
| 648 | }; |
| 649 | assert_eq!( |
| 650 | flash_lite.quote_cost(&audio_with_cache), |
| 651 | Err(PricingQuoteError::MissingUsage(TokenClass::InputAudio)) |
| 652 | ); |
| 653 | |
| 654 | let equal_reasoning = OfferingPricing { |
| 655 | provider: "openrouter".into(), |
| 656 | wire_model_id: "google/gemini-4-carbon".into(), |
| 657 | currency: Currency::Usd, |
| 658 | input_per_million: Some(2.0), |
| 659 | output_per_million: Some(10.0), |
| 660 | reasoning_per_million: Some(10.0), |
| 661 | input_audio_per_million: Some(2.0), |
| 662 | provenance: PricingProvenance::ModelsDevLive, |
| 663 | ..Default::default() |
| 664 | }; |
| 665 | assert!( |
| 666 | (equal_reasoning |
| 667 | .estimate_cost(&plain_text) |
| 668 | .expect("equal reasoning/output rate prices without reasoning breakdown") |
| 669 | - 3.0) |
| 670 | .abs() |
| 671 | < 1e-12 |
| 672 | ); |
| 673 | |
| 674 | let distinct_reasoning = OfferingPricing { |
| 675 | provider: "openrouter".into(), |
| 676 | wire_model_id: "perplexity/sonar-deep-research".into(), |
| 677 | currency: Currency::Usd, |
| 678 | input_per_million: Some(2.0), |
| 679 | output_per_million: Some(8.0), |
| 680 | reasoning_per_million: Some(3.0), |
| 681 | provenance: PricingProvenance::ModelsDevLive, |
| 682 | ..Default::default() |
| 683 | }; |
| 684 | assert_eq!( |
| 685 | distinct_reasoning.quote_cost(&plain_text), |
| 686 | Err(PricingQuoteError::MissingUsage(TokenClass::Reasoning)) |
| 687 | ); |
| 688 | let with_reasoning = TokenUsage { |
| 689 | input: 0, |
| 690 | output: 1_000_000, |
| 691 | reasoning: Some(600_000), |
| 692 | ..Default::default() |
| 693 | }; |
| 694 | assert!( |
| 695 | (distinct_reasoning |
| 696 | .estimate_cost(&with_reasoning) |
| 697 | .expect("reported reasoning breakdown prices at distinct rate") |
| 698 | - (0.4 * 8.0 + 0.6 * 3.0)) |
| 699 | .abs() |
| 700 | < 1e-12 |
| 701 | ); |
| 702 | } |
| 703 |