| 1 | //! Offline coverage of shipped defaults, not arbitrary live/account model rosters. |
| 2 | //! Rate authority stays in the production audit; the fixture stores only exact, |
| 3 | //! source-reviewed omissions. Never regenerate it from observed audit outcomes. |
| 4 | |
| 5 | use std::collections::{BTreeMap, BTreeSet}; |
| 6 | |
| 7 | use codewhale_config::ProviderKind; |
| 8 | use codewhale_config::descriptors::{DescriptorWire, bundled_provider_descriptors}; |
| 9 | use codewhale_config::provider::all_providers; |
| 10 | use codewhale_config::route::{LogicalModelRef, RouteRequest, RouteResolver}; |
| 11 | |
| 12 | use super::*; |
| 13 | use crate::config::{Config, ProviderConfig}; |
| 14 | use crate::cost_status::EffectiveRouteEnvelope; |
| 15 | use crate::test_support::EnvVarGuard; |
| 16 | |
| 17 | // Identity, exact wire ID, exact endpoint, protocol, captured billing mode. |
| 18 | // Keep literal model/endpoint values in reviewed exemptions: referring to an |
| 19 | // expanding default/roster constant would automatically bless a new omission. |
| 20 | type RouteKey = [String; 5]; |
| 21 | |
| 22 | #[derive(serde::Deserialize)] |
| 23 | #[serde(deny_unknown_fields)] |
| 24 | struct ReviewedOmissions { |
| 25 | reason: String, |
| 26 | review: String, |
| 27 | routes: Vec<RouteKey>, |
| 28 | } |
| 29 | |
| 30 | fn reviewed_omissions() -> BTreeMap<RouteKey, UnpricedReason> { |
| 31 | let groups: Vec<ReviewedOmissions> = |
| 32 | serde_json::from_str(include_str!("default_coverage_unpriced.json")) |
| 33 | .expect("reviewed default coverage fixture"); |
| 34 | let mut reviewed = BTreeMap::new(); |
| 35 | for group in groups { |
| 36 | let reason = UnpricedReason::from_label(&group.reason); |
| 37 | assert_eq!(reason.label(), group.reason, "unknown review reason"); |
| 38 | assert!(!group.review.trim().is_empty(), "missing source review"); |
| 39 | assert!(!group.routes.is_empty(), "empty review group"); |
| 40 | for key in group.routes { |
| 41 | assert!( |
| 42 | reviewed.insert(key.clone(), reason).is_none(), |
| 43 | "duplicate default coverage exemption: {key:?}" |
| 44 | ); |
| 45 | } |
| 46 | } |
| 47 | reviewed |
| 48 | } |
| 49 | |
| 50 | #[test] |
| 51 | fn shipped_default_routes_have_reviewed_pricing_coverage() { |
| 52 | let _live = crate::provider_lake::lock_live_snapshot(); |
| 53 | // Explicit table endpoints below bypass endpoint overrides. These remaining |
| 54 | // variables can still change billing products or legacy Ollama migration. |
| 55 | let _env: Vec<_> = [ |
| 56 | "MINIMAX_API_KEY", |
| 57 | "XIAOMI_MIMO_MODE", |
| 58 | "XIAOMI_MIMO_BASE_URL", |
| 59 | "XIAOMI_MIMO_TOKEN_PLAN_API_KEY", |
| 60 | "MIMO_TOKEN_PLAN_API_KEY", |
| 61 | "XIAOMI_MIMO_API_KEY", |
| 62 | "XIAOMI_API_KEY", |
| 63 | "MIMO_API_KEY", |
| 64 | "OLLAMA_BASE_URL", |
| 65 | ] |
| 66 | .into_iter() |
| 67 | .map(EnvVarGuard::remove) |
| 68 | .collect(); |
| 69 | let _cloud = EnvVarGuard::set("CODEWHALE_DISABLE_CLOUD_FACTS", "1"); |
| 70 | crate::provider_lake::clear_live_snapshot(); |
| 71 | crate::provider_catalog_live::reset_cache_for_test(); |
| 72 | |
| 73 | let resolver = RouteResolver::new(); |
| 74 | let mut defaults = Vec::new(); |
| 75 | for entry in all_providers() { |
| 76 | if entry.kind() == ProviderKind::Antigravity { |
| 77 | continue; // Retired tombstone, not a shipped runnable choice. |
| 78 | } |
| 79 | let candidate = resolver |
| 80 | .resolve(&RouteRequest { |
| 81 | explicit_provider: Some(entry.kind()), |
| 82 | ..Default::default() |
| 83 | }) |
| 84 | .unwrap_or_else(|error| panic!("{} default cannot resolve: {error}", entry.id())); |
| 85 | let provider = ApiProvider::from_kind(entry.kind()); |
| 86 | let mut config = Config { |
| 87 | provider: Some(entry.id().to_string()), |
| 88 | ..Default::default() |
| 89 | }; |
| 90 | config.provider_config_for_mut(provider).base_url = |
| 91 | Some(candidate.endpoint().base_url.clone()); |
| 92 | defaults.push((config, provider, candidate)); |
| 93 | } |
| 94 | let built_in_count = defaults.len(); |
| 95 | for descriptor in bundled_provider_descriptors() { |
| 96 | assert_eq!(descriptor.wire, DescriptorWire::OpenaiCompatible); |
| 97 | let mut config = Config { |
| 98 | provider: Some(descriptor.id.clone()), |
| 99 | ..Default::default() |
| 100 | }; |
| 101 | *config.provider_config_for_mut(ApiProvider::Custom) = ProviderConfig { |
| 102 | kind: Some("openai-compatible".to_string()), |
| 103 | base_url: Some(descriptor.base_url.clone()), |
| 104 | model: Some(descriptor.default_model.clone()), |
| 105 | ..Default::default() |
| 106 | }; |
| 107 | config |
| 108 | .resolve_provider_identity(&descriptor.id) |
| 109 | .expect("named compatible default must retain a valid identity"); |
| 110 | let candidate = resolver |
| 111 | .resolve(&RouteRequest { |
| 112 | explicit_provider: Some(ProviderKind::Custom), |
| 113 | model_selector: Some(LogicalModelRef::from(descriptor.default_model.clone())), |
| 114 | base_url_override: Some(config.base_url_for_route(ApiProvider::Custom)), |
| 115 | ..Default::default() |
| 116 | }) |
| 117 | .expect("named compatible bootstrap route must resolve"); |
| 118 | defaults.push((config, ApiProvider::Custom, candidate)); |
| 119 | } |
| 120 | |
| 121 | // A deliberate breadth receipt, including the generic Custom placeholder. |
| 122 | // Inventory still comes from production owners, not these expected counts. |
| 123 | assert_eq!(built_in_count, 51, "review changed shipped-default breadth"); |
| 124 | assert_eq!( |
| 125 | defaults.len() - built_in_count, |
| 126 | 6, |
| 127 | "review compatible-default breadth" |
| 128 | ); |
| 129 | let recorded_at = Utc.with_ymd_and_hms(2026, 9, 8, 12, 0, 0).unwrap(); |
| 130 | // Both ordinary token classes are actually used. Cache and long-context |
| 131 | // tiers have separate existing tests; this guard makes no all-tier claim. |
| 132 | let usage = Usage { |
| 133 | input_tokens: 1_000, |
| 134 | output_tokens: 100, |
| 135 | ..Default::default() |
| 136 | }; |
| 137 | let mut seen = BTreeSet::new(); |
| 138 | let mut unpriced = BTreeMap::new(); |
| 139 | for (config, provider, candidate) in defaults { |
| 140 | let base_url = config.base_url_for_route(provider); |
| 141 | // Config normalizes the optional trailing separator before dispatch. |
| 142 | // Retain the exact host/path comparison and audit that runtime form. |
| 143 | assert_eq!( |
| 144 | base_url, |
| 145 | candidate.endpoint().base_url.trim_end_matches('/') |
| 146 | ); |
| 147 | let route = EffectiveRouteEnvelope::capture( |
| 148 | Some(&config), |
| 149 | provider, |
| 150 | config.provider_identity_for(provider), |
| 151 | candidate.wire_model_id().as_str(), |
| 152 | Some(&base_url), |
| 153 | recorded_at, |
| 154 | ); |
| 155 | assert!( |
| 156 | route.provider_live_pricing.is_none(), |
| 157 | "offline default captured live pricing" |
| 158 | ); |
| 159 | let key = [ |
| 160 | route.provider_identity.clone(), |
| 161 | route.model.clone(), |
| 162 | base_url, |
| 163 | serde_json::to_value(candidate.protocol()) |
| 164 | .unwrap() |
| 165 | .as_str() |
| 166 | .unwrap() |
| 167 | .to_string(), |
| 168 | serde_json::to_value(route.billing_mode) |
| 169 | .unwrap() |
| 170 | .as_str() |
| 171 | .unwrap() |
| 172 | .to_string(), |
| 173 | ]; |
| 174 | assert!(seen.insert(key.clone()), "duplicate default route: {key:?}"); |
| 175 | let audit = route.audit(&usage); |
| 176 | if let Some(reason) = audit.unpriced_reason { |
| 177 | assert!( |
| 178 | audit.estimate.is_none(), |
| 179 | "unpriced route has an estimate: {key:?}" |
| 180 | ); |
| 181 | unpriced.insert(key, reason); |
| 182 | } else { |
| 183 | assert!( |
| 184 | audit.usd_priced || audit.cny_priced, |
| 185 | "no authoritative currency: {key:?}" |
| 186 | ); |
| 187 | assert!( |
| 188 | audit |
| 189 | .provenance |
| 190 | .is_some_and(|source| source != PricingProvenance::Unknown) |
| 191 | ); |
| 192 | assert!( |
| 193 | audit.unpriced_classes.is_empty(), |
| 194 | "used token class missing: {key:?}" |
| 195 | ); |
| 196 | assert!( |
| 197 | audit |
| 198 | .estimate |
| 199 | .is_some_and(CostEstimate::is_finite_nonnegative), |
| 200 | "invalid price: {key:?}" |
| 201 | ); |
| 202 | } |
| 203 | } |
| 204 | assert_eq!( |
| 205 | unpriced, |
| 206 | reviewed_omissions(), |
| 207 | "New or changed omissions need exact source review; newly priced/removed routes must retire stale exemptions" |
| 208 | ); |
| 209 | } |
| 210 |