| 1 | // === Live /models limits: the ceilings a provider states itself ========== |
| 2 | // |
| 3 | // Every route that is not OrcaRouter, OpenRouter or Concentrate used to build |
| 4 | // its offerings with `limit: None`, so a provider publishing context and |
| 5 | // output ceilings in its own `/models` body lost them and the route fell back |
| 6 | // to the legacy 128K window. These tests pin the shapes that are read, the |
| 7 | // precedence among them, and the rule that one unreadable field costs only its |
| 8 | // own candidate. |
| 9 | // |
| 10 | // Synthetic ids only; nothing here touches the network. |
| 11 | |
| 12 | #[test] |
| 13 | fn limits_a_live_listing_states_context_and_output_ceilings() { |
| 14 | let limits = listed_limits_by_model_id( |
| 15 | r#"{"object":"list","data":[ |
| 16 | {"id":"row-openai","limit":{"context":1048000,"output":64000}}, |
| 17 | {"id":"row-flat","context_length":200000,"max_output_tokens":32000}, |
| 18 | {"id":"row-window","context_window":131072}, |
| 19 | {"id":"row-len","max_model_len":262144}, |
| 20 | {"id":"row-max","max_context_length":524288,"max_completion_tokens":16000} |
| 21 | ]}"#, |
| 22 | ); |
| 23 | let row = |id: &str| { |
| 24 | limits |
| 25 | .get(id) |
| 26 | .unwrap_or_else(|| panic!("no limit for {id}")) |
| 27 | }; |
| 28 | assert_eq!(row("row-openai").context, Some(1_048_000)); |
| 29 | assert_eq!(row("row-openai").output, Some(64_000)); |
| 30 | assert_eq!(row("row-flat").context, Some(200_000)); |
| 31 | assert_eq!(row("row-flat").output, Some(32_000)); |
| 32 | assert_eq!(row("row-window").context, Some(131_072)); |
| 33 | assert_eq!(row("row-len").context, Some(262_144)); |
| 34 | assert_eq!(row("row-max").context, Some(524_288)); |
| 35 | assert_eq!(row("row-max").output, Some(16_000)); |
| 36 | // `input` stays empty: no route is clamped on an inference. |
| 37 | assert!(limits.values().all(|limit| limit.input.is_none())); |
| 38 | } |
| 39 | |
| 40 | #[test] |
| 41 | fn limits_a_relay_metadata_block_states_capability_ahead_of_budget() { |
| 42 | let limits = listed_limits_by_model_id( |
| 43 | r#"{"data":[ |
| 44 | {"id":"row-both","metadata":{"context_limit":1000000,"context_budget":200000,"output_budget":32000}}, |
| 45 | {"id":"row-budget","metadata":{"context_budget":131072}} |
| 46 | ]}"#, |
| 47 | ); |
| 48 | // `context_limit` is the model capability; `context_budget` is the share |
| 49 | // one deployment accepts, so the capability wins when both are stated. |
| 50 | assert_eq!(limits["row-both"].context, Some(1_000_000)); |
| 51 | assert_eq!(limits["row-both"].output, Some(32_000)); |
| 52 | assert_eq!(limits["row-budget"].context, Some(131_072)); |
| 53 | } |
| 54 | |
| 55 | #[test] |
| 56 | fn limits_one_unreadable_field_costs_only_its_own_candidate() { |
| 57 | // Selecting a field by presence and parsing whatever was selected let an |
| 58 | // unreadable high-priority field hide every readable field below it, so |
| 59 | // the route fell back to 128K even though the listing stated a window. |
| 60 | let limits = listed_limits_by_model_id( |
| 61 | r#"{"data":[ |
| 62 | {"id":"row-mixed","context_length":200000,"metadata":{"context_limit":"1M"}}, |
| 63 | {"id":"row-block","context_length":300000,"limit":{"context":null}} |
| 64 | ]}"#, |
| 65 | ); |
| 66 | assert_eq!(limits["row-mixed"].context, Some(200_000)); |
| 67 | assert_eq!(limits["row-block"].context, Some(300_000)); |
| 68 | } |
| 69 | |
| 70 | #[test] |
| 71 | fn limits_a_row_stating_no_readable_ceiling_is_left_out() { |
| 72 | let limits = listed_limits_by_model_id( |
| 73 | r#"{"data":[ |
| 74 | {"id":"row-none"}, |
| 75 | {"id":"row-zero","context_length":0}, |
| 76 | {"id":"row-bad","context_length":"unbounded"} |
| 77 | ]}"#, |
| 78 | ); |
| 79 | assert!(limits.is_empty(), "{limits:?}"); |
| 80 | } |
| 81 | |
| 82 | #[test] |
| 83 | fn limits_a_body_without_a_data_array_yields_no_limits() { |
| 84 | assert!(listed_limits_by_model_id(r#"{"object":"list"}"#).is_empty()); |
| 85 | assert!(listed_limits_by_model_id("not json").is_empty()); |
| 86 | } |
| 87 | |
| 88 | #[tokio::test] |
| 89 | async fn an_unbound_official_go_client_cannot_send_after_an_account_refresh() { |
| 90 | let config: Config = toml::from_str( |
| 91 | r#" |
| 92 | provider = "opencode-go" |
| 93 | [providers.opencode_go] |
| 94 | base_url = "https://opencode.ai/inference/go/v1" |
| 95 | api_key = "test-opencode-catalog-credential" |
| 96 | "#, |
| 97 | ) |
| 98 | .expect("Go configuration"); |
| 99 | let client = CodewhaleClient::for_catalog_refresh(&config).expect("listing client"); |
| 100 | assert!(client.go_route.is_none()); |
| 101 | let request = client.http_client.post("http://127.0.0.1:1"); |
| 102 | let refused = client |
| 103 | .dispatch_attempt(request, crate::llm_client::RetryOperation::Inference) |
| 104 | .await |
| 105 | .expect_err("an inference cannot use a Go listing client"); |
| 106 | assert!(refused.to_string().contains("current account-specific route")); |
| 107 | } |
| 108 | |
| 109 | #[test] |
| 110 | fn generic_connection_listing_preserves_model_type_and_explicit_tool_denials() { |
| 111 | let delta = catalog_delta_from_models_body( |
| 112 | ProviderKind::Custom, |
| 113 | "named-gateway".into(), |
| 114 | "https://relay.example/v1", |
| 115 | r#"{"data":[ |
| 116 | {"id":"non-chat","type":"embedding","tool_call":true, |
| 117 | "capabilities":{"tools":{"supported":false}}}, |
| 118 | {"id":"assistant","type":"chat","capabilities":{"tools":{"supported":true}}, |
| 119 | "modalities":{"input":["text"],"output":["text"]}}, |
| 120 | {"id":"old-model","type":"chat","status":"disabled"}, |
| 121 | {"id":"anthropic-shape","type":"model","capabilities":{"tools":{"supported":true}}} |
| 122 | ]}"#, |
| 123 | &[], |
| 124 | ) |
| 125 | .expect("generic route delta"); |
| 126 | let row = |id| delta.offerings.iter().find(|row| row.wire_model_id == id).unwrap(); |
| 127 | assert_eq!(row("non-chat").model_type.as_deref(), Some("embedding")); |
| 128 | assert_eq!(row("non-chat").tool_call, Some(false)); |
| 129 | assert_eq!(row("assistant").tool_call, Some(true)); |
| 130 | assert!(row("assistant").modalities.is_some()); |
| 131 | assert_eq!(row("old-model").status.as_deref(), Some("disabled")); |
| 132 | assert_eq!(row("anthropic-shape").model_type, None); |
| 133 | assert_eq!(row("anthropic-shape").tool_call, Some(true)); |
| 134 | |
| 135 | let rejected = catalog_delta_from_models_body( |
| 136 | ProviderKind::Custom, |
| 137 | "named-gateway".into(), |
| 138 | "https://relay.example/v1", |
| 139 | r#"{"data":[{"id":"ordinary","type":"CW_TEST_ROUTE_TOKEN", |
| 140 | "modalities":{"output":["CW_TEST_ROUTE_TOKEN"]}}]}"#, |
| 141 | &["CW_TEST_ROUTE_TOKEN".into()], |
| 142 | ); |
| 143 | assert!(matches!(rejected, Err(CatalogRefreshError::InvalidResponse))); |
| 144 | } |
| 145 | |
| 146 | #[test] |
| 147 | fn limits_a_generic_route_keeps_the_limits_its_listing_states() { |
| 148 | let delta = catalog_delta_from_models_body( |
| 149 | ProviderKind::Openai, |
| 150 | "openai".to_string(), |
| 151 | "https://relay.example/v1", |
| 152 | r#"{"data":[{"id":"gpt-relay","context_length":1048000,"max_output_tokens":32000}]}"#, |
| 153 | &[], |
| 154 | ) |
| 155 | .expect("generic route delta"); |
| 156 | let row = delta |
| 157 | .offerings |
| 158 | .iter() |
| 159 | .find(|row| row.wire_model_id == "gpt-relay") |
| 160 | .expect("listed row"); |
| 161 | let limit = row.limit.as_ref().expect("listed limits are kept"); |
| 162 | assert_eq!(limit.context, Some(1_048_000)); |
| 163 | assert_eq!(limit.output, Some(32_000)); |
| 164 | assert_eq!(limit.input, None); |
| 165 | |
| 166 | let body = r#"{"data":[ |
| 167 | {"id":"deepseek-flash","context_window":1048576, |
| 168 | "effort":{"supported_levels":["low","high","max"],"default_level":"high"}}, |
| 169 | {"id":"new-model","effort":{"supported_levels":["max","future","low"],"default_level":"future"}}, |
| 170 | {"id":"unknown-model"} |
| 171 | ]}"#; |
| 172 | let direct = catalog_delta_from_models_body( |
| 173 | ProviderKind::Deepseek, |
| 174 | "deepseek".to_string(), |
| 175 | "https://api.deepseek.com/v1/", |
| 176 | body, |
| 177 | &[], |
| 178 | ) |
| 179 | .expect("direct DeepSeek catalog"); |
| 180 | let find = |id: &str| { |
| 181 | direct |
| 182 | .offerings |
| 183 | .iter() |
| 184 | .find(|row| row.wire_model_id == id) |
| 185 | .unwrap() |
| 186 | }; |
| 187 | assert_eq!( |
| 188 | find("deepseek-flash").limit.as_ref().unwrap().context, |
| 189 | Some(1_048_576) |
| 190 | ); |
| 191 | assert_eq!( |
| 192 | find("deepseek-flash").reasoning_options, |
| 193 | vec![ |
| 194 | json!({"type":"toggle"}), |
| 195 | json!({"type":"effort", "values":["low","high","max"], "default":"high"}) |
| 196 | ] |
| 197 | ); |
| 198 | let projected = |
| 199 | crate::reasoning_preference::ReasoningEffort::catalog_effort_values_for_offering(find( |
| 200 | "deepseek-flash", |
| 201 | )) |
| 202 | .unwrap() |
| 203 | .into_iter() |
| 204 | .filter(|effort| { |
| 205 | !matches!( |
| 206 | effort, |
| 207 | crate::reasoning_preference::ReasoningEffort::Off |
| 208 | | crate::reasoning_preference::ReasoningEffort::Auto |
| 209 | ) |
| 210 | }) |
| 211 | .map(|effort| effort.as_setting()) |
| 212 | .collect::<Vec<_>>(); |
| 213 | assert_eq!(projected, ["low", "high", "max"]); |
| 214 | assert_eq!( |
| 215 | find("new-model").reasoning_options, |
| 216 | vec![ |
| 217 | json!({"type":"toggle"}), |
| 218 | json!({"type":"effort", "values":["max","low"]}) |
| 219 | ] |
| 220 | ); |
| 221 | assert!(find("unknown-model").reasoning_options_present); |
| 222 | assert!(find("unknown-model").reasoning_options.is_empty()); |
| 223 | assert!( |
| 224 | crate::reasoning_preference::ReasoningEffort::catalog_effort_values_for_offering(find( |
| 225 | "unknown-model" |
| 226 | )) |
| 227 | .is_none() |
| 228 | ); |
| 229 | let mut unknown = find("unknown-model").clone(); |
| 230 | let mut old = unknown.clone(); |
| 231 | old.reasoning_options = vec![json!({"type":"effort", "values":["high"]})]; |
| 232 | old.reasoning_options_present = true; |
| 233 | unknown.complete_from(&old, Some("https://api.deepseek.com/v1")); |
| 234 | assert!(unknown.reasoning_options.is_empty()); |
| 235 | |
| 236 | for (provider, endpoint) in [ |
| 237 | (ProviderKind::Deepseek, "https://relay.example/v1"), |
| 238 | ( |
| 239 | ProviderKind::Deepseek, |
| 240 | "https://api.deepseek.com.example/v1", |
| 241 | ), |
| 242 | (ProviderKind::Custom, "https://api.deepseek.com/v1"), |
| 243 | ] { |
| 244 | let proxy = |
| 245 | catalog_delta_from_models_body(provider, "proxy".to_string(), endpoint, body, &[]) |
| 246 | .expect("compatible route catalog"); |
| 247 | assert!( |
| 248 | proxy |
| 249 | .offerings |
| 250 | .iter() |
| 251 | .all(|row| !row.reasoning_options_present) |
| 252 | ); |
| 253 | assert!( |
| 254 | proxy |
| 255 | .offerings |
| 256 | .iter() |
| 257 | .all(|row| row.reasoning_options.is_empty()) |
| 258 | ); |
| 259 | } |
| 260 | for invalid in [ |
| 261 | r#"{"data":[{"id":"a","effort":{"supported_levels":"high"}}]}"#, |
| 262 | r#"{"data":[{"id":"a","effort":{"supported_levels":["low"],"default_level":"high"}}]}"#, |
| 263 | r#"{"data":[{"id":"a","effort":{"supported_levels":["low","low"]}}]}"#, |
| 264 | r#"{"data":[{"id":"a"},{"id":"a"}]}"#, |
| 265 | r#"{"data":[{"id":"a","effort":{"supported_levels":["bad\nvalue"]}}]}"#, |
| 266 | ] { |
| 267 | assert!( |
| 268 | catalog_delta_from_models_body( |
| 269 | ProviderKind::Deepseek, |
| 270 | "deepseek".to_string(), |
| 271 | "https://api.deepseek.com", |
| 272 | invalid, |
| 273 | &[], |
| 274 | ) |
| 275 | .is_err() |
| 276 | ); |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | #[test] |
| 281 | fn siwc_refresh_rotates_atomically_and_rejects_a_different_account() { |
| 282 | codewhale_runtime::oauth::test_support::siwc_refresh_rotates_atomically_and_rejects_a_different_account(|config| CodewhaleClient::new(config).unwrap().chatgpt_reasoning_api); |
| 283 | } |
| 284 | |
| 285 | #[test] |
| 286 | fn unified_live_models_extract_extended_limits_and_capabilities() { |
| 287 | let limits = listed_limits_by_model_id( |
| 288 | r#"{"data":[ |
| 289 | {"id":"anthropic-row","max_input_tokens":200000,"max_tokens":64000}, |
| 290 | {"id":"gemini-row","inputTokenLimit":1048576,"outputTokenLimit":65536}, |
| 291 | {"id":"novita-row","context_size":131072}, |
| 292 | {"id":"plural-limits","limits":{"context":262144,"output":32768}}, |
| 293 | {"id":"top-provider-row","top_provider":{"context_length":500000,"max_completion_tokens":16384}} |
| 294 | ]}"#, |
| 295 | ); |
| 296 | assert_eq!(limits["anthropic-row"].context, Some(200_000)); |
| 297 | assert_eq!(limits["anthropic-row"].output, Some(64_000)); |
| 298 | assert_eq!(limits["gemini-row"].context, Some(1_048_576)); |
| 299 | assert_eq!(limits["gemini-row"].output, Some(65_536)); |
| 300 | assert_eq!(limits["novita-row"].context, Some(131_072)); |
| 301 | assert_eq!(limits["plural-limits"].context, Some(262_144)); |
| 302 | assert_eq!(limits["plural-limits"].output, Some(32_768)); |
| 303 | assert_eq!(limits["top-provider-row"].context, Some(500_000)); |
| 304 | assert_eq!(limits["top-provider-row"].output, Some(16_384)); |
| 305 | |
| 306 | let zenmux = catalog_delta_from_models_body( |
| 307 | ProviderKind::Zenmux, |
| 308 | "zenmux".to_string(), |
| 309 | "https://zenmux.ai/api/v1", |
| 310 | r#"{"data":[{ |
| 311 | "id":"anthropic/claude-opus-5", |
| 312 | "context_length":1000000, |
| 313 | "max_output_tokens":64000, |
| 314 | "supported_parameters":["reasoning","tools","structured_outputs"], |
| 315 | "supported_reasoning_levels":["low","medium","high","max"], |
| 316 | "default_reasoning_level":"high" |
| 317 | }]}"#, |
| 318 | &[], |
| 319 | ) |
| 320 | .expect("zenmux delta"); |
| 321 | let row = &zenmux.offerings[0]; |
| 322 | assert_eq!(row.limit.as_ref().and_then(|l| l.context), Some(1_000_000)); |
| 323 | assert_eq!(row.limit.as_ref().and_then(|l| l.output), Some(64_000)); |
| 324 | assert_eq!(row.reasoning, Some(true)); |
| 325 | assert_eq!(row.tool_call, Some(true)); |
| 326 | assert_eq!(row.structured_output, Some(true)); |
| 327 | assert!(row.reasoning_options_present); |
| 328 | assert_eq!( |
| 329 | row.reasoning_options, |
| 330 | vec![json!({ |
| 331 | "type": "effort", |
| 332 | "values": ["low", "medium", "high", "max"], |
| 333 | "default": "high" |
| 334 | })] |
| 335 | ); |
| 336 | |
| 337 | let anthropic = catalog_delta_from_models_body( |
| 338 | ProviderKind::Anthropic, |
| 339 | "anthropic".to_string(), |
| 340 | "https://api.anthropic.com", |
| 341 | r#"{"data":[{ |
| 342 | "id":"claude-next", |
| 343 | "max_input_tokens":1000000, |
| 344 | "max_tokens":128000, |
| 345 | "capabilities":{ |
| 346 | "thinking":{"supported":true}, |
| 347 | "effort":{"supported":true,"values":["low","medium","high","max"],"default":"high"}, |
| 348 | "function_calling":true |
| 349 | } |
| 350 | }]}"#, |
| 351 | &[], |
| 352 | ) |
| 353 | .expect("anthropic delta"); |
| 354 | let claude = &anthropic.offerings[0]; |
| 355 | assert_eq!(claude.limit.as_ref().and_then(|l| l.context), Some(1_000_000)); |
| 356 | assert_eq!(claude.limit.as_ref().and_then(|l| l.output), Some(128_000)); |
| 357 | assert_eq!(claude.reasoning, Some(true)); |
| 358 | assert_eq!(claude.tool_call, Some(true)); |
| 359 | assert_eq!( |
| 360 | claude.reasoning_options, |
| 361 | vec![json!({ |
| 362 | "type": "effort", |
| 363 | "values": ["low", "medium", "high", "max"], |
| 364 | "default": "high" |
| 365 | })] |
| 366 | ); |
| 367 | |
| 368 | let openrouter = parse_openrouter_models_response( |
| 369 | r#"{"data":[{ |
| 370 | "id":"anthropic/claude-next", |
| 371 | "context_length":200000, |
| 372 | "supported_parameters":["reasoning","tools","response_format"], |
| 373 | "supported_reasoning_levels":["low","medium","high"] |
| 374 | }]}"#, |
| 375 | ) |
| 376 | .expect("openrouter parses"); |
| 377 | let openrouter_row = |
| 378 | openrouter_to_catalog_offering(&openrouter[0], "openrouter", "fp", 1).expect("offering"); |
| 379 | assert_eq!(openrouter_row.structured_output, Some(true)); |
| 380 | assert!(openrouter_row.reasoning_options_present); |
| 381 | assert_eq!( |
| 382 | openrouter_row.reasoning_options, |
| 383 | vec![json!({ "type": "effort", "values": ["low", "medium", "high"] })] |
| 384 | ); |
| 385 | } |
| 386 |