返回 CodeWhale
test_cases_08.rs
根目录 / crates / engine / src / client / test_cases_08.rs
1 // === OrcaRouter: live /models discovery + capability filtering ==========
2 //
3 // OrcaRouter serves the extended OpenRouter-shaped catalog and adds
4 // `supported_endpoint_types` to every row. These tests use synthetic ids
5 // only; they never touch the network.
6
7 pub(super) fn orcarouter_client_for(server: &MockServer) -> CodewhaleClient {
8 let _ = rustls::crypto::ring::default_provider().install_default();
9 CodewhaleClient::new(&{
10 let __cw_config_init_0_0 = Some("orcarouter".to_string());
11 let __cw_config_init_0_1 = Some(ProvidersConfig {
12 orcarouter: ProviderConfig {
13 api_key: Some("test-key".to_string()),
14 base_url: Some(server.uri()),
15 ..ProviderConfig::default()
16 },
17 ..ProvidersConfig::default()
18 });
19 let mut __cw_config_init_0_value: Config = Config::default();
20 __cw_config_init_0_value.provider = __cw_config_init_0_0;
21 __cw_config_init_0_value.providers = __cw_config_init_0_1;
22 __cw_config_init_0_value
23 })
24 .expect("orcarouter client")
25 }
26
27 /// Every OrcaRouter row carries `supported_endpoint_types`; only rows that
28 /// advertise a chat dialect reach the roster, and the image-input fact is
29 /// read from `architecture.input_modalities` verbatim.
30 #[tokio::test]
31 async fn orcarouter_catalog_keeps_chat_rows_and_their_image_input_fact() {
32 let server = MockServer::start().await;
33 mount_models_json(
34 &server,
35 200,
36 json!({"data": [
37 {
38 "id": "synthetic/chat-text",
39 "supported_endpoint_types": ["openai", "openai-response"],
40 "context_length": 1048576,
41 "architecture": {"input_modalities": ["text"], "output_modalities": ["text"]},
42 "top_provider": {"context_length": 1048576, "max_completion_tokens": 384000},
43 "pricing": {"prompt": "0.00000022", "completion": "0.00000066"}
44 },
45 {
46 "id": "synthetic/chat-vision",
47 "supported_endpoint_types": ["openai", "anthropic"],
48 "architecture": {"input_modalities": ["text", "image"], "output_modalities": ["text"]}
49 },
50 {
51 "id": "synthetic/router-auto",
52 "supported_endpoint_types": ["openai", "openai-response", "anthropic", "gemini"],
53 "context_length": 1000000
54 },
55 {
56 "id": "synthetic/image-gen",
57 "supported_endpoint_types": ["image-generation"]
58 },
59 {
60 "id": "synthetic/video-gen",
61 "supported_endpoint_types": ["openai-video"]
62 },
63 {
64 "id": "synthetic/reranker",
65 "supported_endpoint_types": ["jina-rerank"]
66 },
67 {
68 "id": "synthetic/embedder",
69 "supported_endpoint_types": ["embeddings"]
70 },
71 {
72 "id": "synthetic/no-dialect"
73 }
74 ]}),
75 )
76 .await;
77
78 let delta = orcarouter_client_for(&server)
79 .fetch_catalog_delta()
80 .await
81 .expect("delta");
82 assert_eq!(delta.provider, "orcarouter");
83
84 let ids: Vec<&str> = delta
85 .offerings
86 .iter()
87 .map(|offering| offering.wire_model_id.as_str())
88 .collect();
89 assert_eq!(
90 ids,
91 vec![
92 "synthetic/chat-text",
93 "synthetic/chat-vision",
94 "synthetic/router-auto"
95 ],
96 "chat-only roster; non-chat dialects and dialect-less rows are excluded: {ids:?}"
97 );
98
99 let vision = delta
100 .offerings
101 .iter()
102 .find(|offering| offering.wire_model_id == "synthetic/chat-vision")
103 .expect("vision row");
104 assert_eq!(
105 vision.modalities.as_ref().expect("stated modalities").input,
106 vec!["text", "image"],
107 "a stated input-modality fact is preserved verbatim"
108 );
109
110 // OrcaRouter does not publish `supported_parameters`; reasoning and
111 // tool support stay unclaimed rather than becoming a factual refusal.
112 for offering in &delta.offerings {
113 assert!(offering.reasoning.is_none(), "{offering:?}");
114 assert!(offering.tool_call.is_none(), "{offering:?}");
115 }
116
117 // A row that names no architecture keeps modalities unclaimed instead
118 // of inheriting a text-only default.
119 let auto = delta
120 .offerings
121 .iter()
122 .find(|offering| offering.wire_model_id == "synthetic/router-auto")
123 .expect("router row");
124 assert_eq!(auto.modalities, None);
125 }
126
127 /// The chat filter is a hard gate: a roster made only of non-chat dialects
128 /// fails closed rather than producing an empty-but-successful catalog.
129 #[tokio::test]
130 async fn orcarouter_catalog_fails_closed_when_no_row_is_chat() {
131 let server = MockServer::start().await;
132 mount_models_json(
133 &server,
134 200,
135 json!({"data": [
136 {"id": "synthetic/image-gen", "supported_endpoint_types": ["image-generation"]},
137 {"id": "synthetic/video-gen", "supported_endpoint_types": ["openai-video"]}
138 ]}),
139 )
140 .await;
141 assert_eq!(
142 orcarouter_client_for(&server)
143 .fetch_catalog_delta()
144 .await
145 .expect_err("no chat rows"),
146 CatalogRefreshError::InvalidResponse
147 );
148 }
149
150 /// One malformed row costs only its own row, and an unauthorized refresh
151 /// maps to the typed auth failure with the body unread.
152 #[tokio::test]
153 async fn orcarouter_catalog_skips_malformed_rows_and_types_auth_failure() {
154 let server = MockServer::start().await;
155 mount_models_json(
156 &server,
157 200,
158 json!({"data": [
159 {"id": "synthetic/good", "supported_endpoint_types": ["openai"]},
160 {"id": "synthetic/bad id", "supported_endpoint_types": ["openai"]},
161 {"id": "~synthetic/alias", "supported_endpoint_types": ["openai"]}
162 ]}),
163 )
164 .await;
165 let delta = orcarouter_client_for(&server)
166 .fetch_catalog_delta()
167 .await
168 .expect("delta");
169 assert_eq!(
170 delta
171 .offerings
172 .iter()
173 .map(|offering| offering.wire_model_id.as_str())
174 .collect::<Vec<_>>(),
175 vec!["synthetic/good"]
176 );
177
178 server.reset().await;
179 mount_models_json(&server, 401, json!({"error": "denied"})).await;
180 assert_eq!(
181 orcarouter_client_for(&server)
182 .fetch_catalog_delta()
183 .await
184 .expect_err("unauthorized"),
185 CatalogRefreshError::Unauthorized
186 );
187 }
188
189 // === OrcaRouter: live end to end through the implemented provider ========
190 //
191 // Opt-in: needs the environment's ORCAROUTER_API_KEY and real egress. Both
192 // halves go through the client this PR wired — the `/v1/models` refresh and
193 // a `/v1/chat/completions` request — never a side channel.
194
195 fn live_orcarouter_client() -> Option<CodewhaleClient> {
196 let key = std::env::var("ORCAROUTER_API_KEY").ok()?;
197 if key.trim().is_empty() || std::env::var_os("CODEWHALE_SKIP_LIVE").is_some() {
198 return None;
199 }
200 let _ = rustls::crypto::ring::default_provider().install_default();
201 CodewhaleClient::new(&{
202 let __cw_config_init_1_0 = Some("orcarouter".to_string());
203 let __cw_config_init_1_1 = Some(ProvidersConfig {
204 orcarouter: ProviderConfig {
205 api_key: Some(key),
206 ..ProviderConfig::default()
207 },
208 ..ProvidersConfig::default()
209 });
210 let mut __cw_config_init_1_value: Config = Config::default();
211 __cw_config_init_1_value.provider = __cw_config_init_1_0;
212 __cw_config_init_1_value.providers = __cw_config_init_1_1;
213 __cw_config_init_1_value
214 })
215 .ok()
216 }
217
218 #[tokio::test]
219 #[ignore = "opt-in live: calls api.orcarouter.ai with ORCAROUTER_API_KEY"]
220 async fn orcarouter_live_catalog_and_chat_through_the_provider_path() {
221 let Some(client) = live_orcarouter_client() else {
222 return;
223 };
224 let delta = client
225 .fetch_catalog_delta()
226 .await
227 .expect("live OrcaRouter /v1/models");
228 assert!(
229 !delta.offerings.is_empty(),
230 "the live chat roster must not be empty"
231 );
232 for offering in &delta.offerings {
233 assert_eq!(offering.provider, "orcarouter");
234 assert!(
235 offering.wire_model_id.contains('/'),
236 "vendor/model namespace is kept verbatim: {}",
237 offering.wire_model_id
238 );
239 }
240
241 // A real inference call through the same client, on a model the live
242 // roster just advertised — not a hand-written example id. OrcaRouter
243 // API keys are per-key model-scoped, so the test walks the advertised
244 // roster until one model answers; a key that can call none fails with
245 // the provider's own error.
246 let mut last_error = None;
247 let mut answered = false;
248 for offering in &delta.offerings {
249 let model = offering.wire_model_id.clone();
250 assert!(
251 !model.is_empty(),
252 "the live chat roster must advertise model ids"
253 );
254 let request = translation_message_request(
255 "Reply with the single word: ready",
256 model.clone(),
257 "English",
258 16,
259 );
260 match client
261 .create_message_without_response_cache(
262 request,
263 crate::llm_client::RequestRetryObservation::isolated(),
264 )
265 .await
266 {
267 Ok(response) => {
268 assert!(
269 !response.content.is_empty(),
270 "a live completion for {model} must carry content"
271 );
272 answered = true;
273 break;
274 }
275 // This key is not scoped for this model; the roster is wider
276 // than the key. Try the next advertised model.
277 Err(error) => last_error = Some((model, error.to_string())),
278 }
279 }
280 if let Some((model, error)) = last_error {
281 assert!(
282 answered,
283 "no advertised model answered for this key; last {model}: {error}"
284 );
285 }
286 }
287
287 lines RUST