interplane-mv
INTERPLANE: open interoperability protocol between AI models and agent runtimes. Models express intent. Runtimes retain authority.
Brought by drakestapleton
interplane-mv / rust / crates / interplane-probe / src / tests.rs
| 1 | use super::*; |
| 2 | use std::collections::HashMap; |
| 3 | |
| 4 | fn chat(content: &str) -> Value { |
| 5 | json!({"choices": [{"message": {"role": "assistant", "content": content}}]}) |
| 6 | } |
| 7 | fn calls(names: &[&str]) -> Value { |
| 8 | let tc: Vec<Value> = names |
| 9 | .iter() |
| 10 | .enumerate() |
| 11 | .map(|(i, n)| json!({"id": format!("c{i}"), "type": "function", "function": {"name": n, "arguments": "{\"city\":\"Paris\"}"}})) |
| 12 | .collect(); |
| 13 | json!({"choices": [{"message": {"role": "assistant", "content": null, "tool_calls": tc}}]}) |
| 14 | } |
| 15 | fn pr(name: &str, v: Verdict) -> ProbeResult { |
| 16 | ProbeResult { |
| 17 | name: name.into(), |
| 18 | verdict: v, |
| 19 | detail: None, |
| 20 | duration_ms: None, |
| 21 | attempts: None, |
| 22 | request_digest: None, |
| 23 | response_digest: None, |
| 24 | extensions: Map::new(), |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | #[test] |
| 29 | fn chat_and_streaming_verdicts() { |
| 30 | assert_eq!(eval_chat_basic(&chat("hello")).0, Verdict::Pass); |
| 31 | assert_eq!(eval_chat_basic(&chat(" ")).0, Verdict::Fail); |
| 32 | assert_eq!(eval_chat_basic(&json!({})).0, Verdict::Fail); |
| 33 | let sse = "data: {\"choices\":[{\"delta\":{\"content\":\"Hel\"}}]}\n\ndata: {\"choices\":[{\"delta\":{\"content\":\"lo\"}}]}\n\ndata: [DONE]\n"; |
| 34 | assert_eq!(assemble_sse(sse), (2, "Hello".to_string())); |
| 35 | assert_eq!(eval_streaming(sse).0, Verdict::Pass); |
| 36 | assert_eq!(eval_streaming("data: [DONE]").0, Verdict::Fail); |
| 37 | assert_eq!( |
| 38 | eval_streaming("data: {\"choices\":[{\"delta\":{}}]}").0, |
| 39 | Verdict::Fail |
| 40 | ); |
| 41 | } |
| 42 | #[test] |
| 43 | fn tool_verdicts() { |
| 44 | assert_eq!( |
| 45 | eval_tools_native(&calls(&["get_weather"]), "get_weather").0, |
| 46 | Verdict::Pass |
| 47 | ); |
| 48 | assert_eq!( |
| 49 | eval_tools_native(&calls(&["other"]), "get_weather").0, |
| 50 | Verdict::Fail |
| 51 | ); |
| 52 | assert_eq!( |
| 53 | eval_tools_native(&chat("no call"), "get_weather").0, |
| 54 | Verdict::Fail |
| 55 | ); |
| 56 | let bad_args = json!({"choices": [{"message": {"tool_calls": [{"function": {"name": "get_weather", "arguments": "[1]"}}]}}]}); |
| 57 | assert_eq!(eval_tools_native(&bad_args, "get_weather").0, Verdict::Fail); |
| 58 | assert_eq!( |
| 59 | eval_parallel(&calls(&["read_file", "read_file"])).0, |
| 60 | Verdict::Pass |
| 61 | ); |
| 62 | assert_eq!(eval_parallel(&calls(&["get_weather"])).0, Verdict::Degraded); |
| 63 | assert_eq!(eval_parallel(&chat("x")).0, Verdict::Fail); |
| 64 | assert_eq!( |
| 65 | eval_unknown_refusal(&chat("I cannot"), &["get_weather"]).0, |
| 66 | Verdict::Pass |
| 67 | ); |
| 68 | assert_eq!( |
| 69 | eval_unknown_refusal(&calls(&["get_weather"]), &["get_weather"]).0, |
| 70 | Verdict::Degraded |
| 71 | ); |
| 72 | assert_eq!( |
| 73 | eval_unknown_refusal(&calls(&["send_email"]), &["get_weather"]).0, |
| 74 | Verdict::Fail |
| 75 | ); |
| 76 | } |
| 77 | #[test] |
| 78 | fn qwen35_text_probe_uses_the_lenshift_parser() { |
| 79 | let ok = chat("<tool_call>\n<function=get_weather>\n<parameter=city>\nParis\n</parameter>\n</function>\n</tool_call>"); |
| 80 | assert_eq!(eval_text_qwen35(&ok, "get_weather").0, Verdict::Pass); |
| 81 | assert_eq!( |
| 82 | eval_text_qwen35( |
| 83 | &chat("<tool_call>\n<function=get_weather>\n<parameter=city>\nPar"), |
| 84 | "get_weather" |
| 85 | ) |
| 86 | .0, |
| 87 | Verdict::Fail |
| 88 | ); |
| 89 | assert_eq!( |
| 90 | eval_text_qwen35(&chat("It is sunny."), "get_weather").0, |
| 91 | Verdict::Unsupported |
| 92 | ); |
| 93 | assert_eq!( |
| 94 | eval_text_qwen35(&chat(""), "get_weather").0, |
| 95 | Verdict::Unsupported |
| 96 | ); |
| 97 | } |
| 98 | #[test] |
| 99 | fn replay_reasoning_json_context_models() { |
| 100 | assert_eq!( |
| 101 | eval_result_replay(&chat("It is 4217 degrees"), "4217").0, |
| 102 | Verdict::Pass |
| 103 | ); |
| 104 | assert_eq!( |
| 105 | eval_result_replay(&chat("I do not know"), "4217").0, |
| 106 | Verdict::Degraded |
| 107 | ); |
| 108 | assert_eq!(eval_result_replay(&chat(""), "4217").0, Verdict::Fail); |
| 109 | assert_eq!( |
| 110 | eval_reasoning_channel( |
| 111 | &json!({"choices": [{"message": {"content": "x", "reasoning_content": "hmm"}}]}) |
| 112 | ) |
| 113 | .0, |
| 114 | Verdict::Pass |
| 115 | ); |
| 116 | assert_eq!( |
| 117 | eval_reasoning_channel( |
| 118 | &json!({"choices": [{"message": {"content": "x", "reasoning": "hmm"}}]}) |
| 119 | ) |
| 120 | .0, |
| 121 | Verdict::Pass |
| 122 | ); |
| 123 | assert_eq!( |
| 124 | eval_reasoning_channel(&chat("<think>a</think>b")).0, |
| 125 | Verdict::Pass |
| 126 | ); |
| 127 | assert_eq!( |
| 128 | eval_reasoning_channel(&chat("plain")).0, |
| 129 | Verdict::Unsupported |
| 130 | ); |
| 131 | assert_eq!( |
| 132 | eval_json_structured(&chat("{\"ok\": true}")).0, |
| 133 | Verdict::Pass |
| 134 | ); |
| 135 | assert_eq!(eval_json_structured(&chat("[1]")).0, Verdict::Fail); |
| 136 | assert_eq!(eval_json_structured(&chat("sure!")).0, Verdict::Fail); |
| 137 | assert_eq!(eval_context(&chat("7104"), "7104").0, Verdict::Pass); |
| 138 | assert_eq!(eval_context(&chat("9999"), "7104").0, Verdict::Degraded); |
| 139 | assert_eq!(eval_context(&chat(""), "7104").0, Verdict::Fail); |
| 140 | assert_eq!( |
| 141 | eval_models_list(&json!({"data": [{"id": "m"}]}), "m").0, |
| 142 | Verdict::Pass |
| 143 | ); |
| 144 | assert_eq!( |
| 145 | eval_models_list(&json!({"data": [{"id": "x"}]}), "m").0, |
| 146 | Verdict::Fail |
| 147 | ); |
| 148 | assert_eq!(eval_models_list(&json!({}), "m").0, Verdict::Fail); |
| 149 | } |
| 150 | #[test] |
| 151 | fn http_status_classification() { |
| 152 | let ok = |s: u16, b: &str| { |
| 153 | classify( |
| 154 | &Ok(HttpResponse { |
| 155 | status: s, |
| 156 | body: b.into(), |
| 157 | }), |
| 158 | true, |
| 159 | true, |
| 160 | ) |
| 161 | }; |
| 162 | assert!(matches!(ok(200, "{}"), Exchange::Body(_))); |
| 163 | assert!(matches!( |
| 164 | ok(400, "{}"), |
| 165 | Exchange::Verdict((Verdict::Unsupported, _)) |
| 166 | )); |
| 167 | assert!(matches!( |
| 168 | ok(401, "{}"), |
| 169 | Exchange::Verdict((Verdict::Fail, _)) |
| 170 | )); |
| 171 | assert!(matches!( |
| 172 | ok(500, "{}"), |
| 173 | Exchange::Verdict((Verdict::Fail, _)) |
| 174 | )); |
| 175 | assert!(matches!( |
| 176 | ok(200, "<html>"), |
| 177 | Exchange::Verdict((Verdict::Fail, _)) |
| 178 | )); |
| 179 | assert!(matches!( |
| 180 | classify(&Err("boom".into()), true, true), |
| 181 | Exchange::Verdict((Verdict::Fail, _)) |
| 182 | )); |
| 183 | // a basic chat probe is not a "feature": a 4xx is a failure, not "unsupported" |
| 184 | assert!(matches!( |
| 185 | classify( |
| 186 | &Ok(HttpResponse { |
| 187 | status: 404, |
| 188 | body: String::new() |
| 189 | }), |
| 190 | false, |
| 191 | true |
| 192 | ), |
| 193 | Exchange::Verdict((Verdict::Fail, _)) |
| 194 | )); |
| 195 | } |
| 196 | #[test] |
| 197 | fn profiles_follow_required_probes() { |
| 198 | let all_pass = [ |
| 199 | "chat.basic", |
| 200 | "models.list", |
| 201 | "tools.native", |
| 202 | "tools.result_replay", |
| 203 | "tools.text_qwen35", |
| 204 | "chat.streaming", |
| 205 | ]; |
| 206 | let probes: Vec<_> = all_pass.iter().map(|n| pr(n, Verdict::Pass)).collect(); |
| 207 | assert!(compute_profiles(&probes) |
| 208 | .iter() |
| 209 | .all(|p| p.status == ProfileStatus::Compatible)); |
| 210 | |
| 211 | let mut p = probes.clone(); |
| 212 | p.iter_mut() |
| 213 | .find(|x| x.name == "tools.native") |
| 214 | .unwrap() |
| 215 | .verdict = Verdict::Fail; |
| 216 | let prof = compute_profiles(&p); |
| 217 | let st = |n: &str| prof.iter().find(|x| x.name == n).unwrap().status.clone(); |
| 218 | assert_eq!(st("lenshift.openai.1"), ProfileStatus::Incompatible); |
| 219 | assert_eq!(st("lenshift.qwen35.1"), ProfileStatus::Compatible); |
| 220 | |
| 221 | let mut p = probes.clone(); |
| 222 | p.iter_mut() |
| 223 | .find(|x| x.name == "chat.streaming") |
| 224 | .unwrap() |
| 225 | .verdict = Verdict::Skipped; |
| 226 | assert_eq!( |
| 227 | compute_profiles(&p) |
| 228 | .iter() |
| 229 | .find(|x| x.name == "relayline.tool_replay.1") |
| 230 | .unwrap() |
| 231 | .status, |
| 232 | ProfileStatus::Untested |
| 233 | ); |
| 234 | p.iter_mut() |
| 235 | .find(|x| x.name == "chat.basic") |
| 236 | .unwrap() |
| 237 | .verdict = Verdict::Undetermined; |
| 238 | assert_eq!( |
| 239 | compute_profiles(&p)[0].status, |
| 240 | ProfileStatus::Untested, |
| 241 | "UNKNOWN is never treated as a pass" |
| 242 | ); |
| 243 | assert_eq!(compute_profiles(&[])[0].status, ProfileStatus::Untested); |
| 244 | } |
| 245 | #[test] |
| 246 | fn urls_and_time() { |
| 247 | assert_eq!(base_url("http://h:8000"), "http://h:8000/v1"); |
| 248 | assert_eq!(base_url("http://h:8000/v1/"), "http://h:8000/v1"); |
| 249 | assert_eq!( |
| 250 | redact_endpoint("https://user:secret@host:8000/v1?key=abc#f"), |
| 251 | "https://host:8000/v1" |
| 252 | ); |
| 253 | assert_eq!(redact_endpoint("http://h"), "http://h"); |
| 254 | assert_eq!(rfc3339(0), "1970-01-01T00:00:00Z"); |
| 255 | assert_eq!(rfc3339(1_767_225_600), "2026-01-01T00:00:00Z"); |
| 256 | assert_eq!(rfc3339(951_782_400), "2000-02-29T00:00:00Z"); |
| 257 | assert!(needle_prompt(8, "7104").contains("The secret code word is 7104.")); |
| 258 | // lengths equal the Python probe's prompts for the same sizes |
| 259 | assert_eq!(needle_prompt(8, "PELICAN-4410").len(), 25_010); |
| 260 | assert_eq!(needle_prompt(32, "PELICAN-4410").len(), 98_654); |
| 261 | } |
| 262 | |
| 263 | /// A canned server: answers by path and request shape. No network. |
| 264 | struct Canned { |
| 265 | routes: HashMap<&'static str, (u16, String)>, |
| 266 | } |
| 267 | impl Transport for Canned { |
| 268 | fn get(&self, path: &str) -> Result<HttpResponse, String> { |
| 269 | let (s, b) = self.routes.get(path).cloned().unwrap_or((404, "{}".into())); |
| 270 | Ok(HttpResponse { status: s, body: b }) |
| 271 | } |
| 272 | fn post(&self, path: &str, body: &Value) -> Result<HttpResponse, String> { |
| 273 | let key = if body.get("stream").is_some() { |
| 274 | "stream" |
| 275 | } else if body.get("tools").is_some() { |
| 276 | "tools" |
| 277 | } else { |
| 278 | path |
| 279 | }; |
| 280 | let (s, b) = self.routes.get(key).cloned().unwrap_or((500, "{}".into())); |
| 281 | Ok(HttpResponse { status: s, body: b }) |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | #[test] |
| 286 | fn full_run_with_canned_endpoint_and_schema_valid_report() { |
| 287 | let routes = HashMap::from([ |
| 288 | ( |
| 289 | "/chat/completions", |
| 290 | (200, chat("hello 7104 {\"ok\": true}").to_string()), |
| 291 | ), |
| 292 | ( |
| 293 | "stream", |
| 294 | ( |
| 295 | 200, |
| 296 | "data: {\"choices\":[{\"delta\":{\"content\":\"hi\"}}]}\n\ndata: [DONE]\n" |
| 297 | .to_string(), |
| 298 | ), |
| 299 | ), |
| 300 | ( |
| 301 | "tools", |
| 302 | (200, calls(&["read_file", "read_file"]).to_string()), |
| 303 | ), |
| 304 | ("/models", (200, json!({"data": [{"id": "m"}]}).to_string())), |
| 305 | ]); |
| 306 | let cfg = ProbeConfig { |
| 307 | endpoint: "http://user:pw@127.0.0.1:1/v1?token=abc".into(), |
| 308 | model: "m".into(), |
| 309 | backend: None, |
| 310 | skip: vec!["context".into()], |
| 311 | environment: detect_environment(), |
| 312 | }; |
| 313 | let report = run_probes(&Canned { routes }, &cfg); |
| 314 | let by = |n: &str| report.probes.iter().find(|p| p.name == n).unwrap(); |
| 315 | assert_eq!(by("chat.basic").verdict, Verdict::Pass); |
| 316 | assert_eq!(by("chat.streaming").verdict, Verdict::Pass); |
| 317 | assert_eq!(by("tools.native").verdict, Verdict::Pass); |
| 318 | assert_eq!(by("tools.parallel").verdict, Verdict::Pass); |
| 319 | assert_eq!(by("models.list").verdict, Verdict::Pass); |
| 320 | assert_eq!(by("context.8k").verdict, Verdict::Skipped); |
| 321 | assert_eq!(by("context.32k").verdict, Verdict::Skipped); |
| 322 | assert_eq!(by("reasoning.channel").verdict, Verdict::Unsupported); |
| 323 | assert_eq!(by("reasoning.disable").verdict, Verdict::Undetermined); |
| 324 | assert!( |
| 325 | by("chat.basic").request_digest.is_some() && by("chat.basic").response_digest.is_some() |
| 326 | ); |
| 327 | assert_eq!(report.endpoint, "http://127.0.0.1:1/v1"); |
| 328 | assert!(!report_text(&report).contains("pw") && !report_text(&report).contains("abc")); |
| 329 | assert_eq!(report.profiles[0].status, ProfileStatus::Compatible); |
| 330 | |
| 331 | // the report validates against spec/schemas/probe.schema.json |
| 332 | let dir = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../../../spec/schemas"); |
| 333 | let mut resources = vec![]; |
| 334 | for e in std::fs::read_dir(&dir).unwrap() { |
| 335 | let v: Value = |
| 336 | serde_json::from_str(&std::fs::read_to_string(e.unwrap().path()).unwrap()).unwrap(); |
| 337 | resources.push(( |
| 338 | v["$id"].as_str().unwrap().to_string(), |
| 339 | jsonschema::Resource::from_contents(v).unwrap(), |
| 340 | )); |
| 341 | } |
| 342 | let main: Value = |
| 343 | serde_json::from_str(&std::fs::read_to_string(dir.join("probe.schema.json")).unwrap()) |
| 344 | .unwrap(); |
| 345 | let v = jsonschema::options() |
| 346 | .with_resources(resources.into_iter()) |
| 347 | .build(&main) |
| 348 | .unwrap(); |
| 349 | let inst = serde_json::to_value(&report).unwrap(); |
| 350 | let errs: Vec<String> = v.iter_errors(&inst).map(|e| e.to_string()).collect(); |
| 351 | assert!(errs.is_empty(), "{errs:?}"); |
| 352 | } |
| 353 | |
| 354 | #[test] |
| 355 | fn dead_endpoint_fails_closed_never_passes() { |
| 356 | struct Dead; |
| 357 | impl Transport for Dead { |
| 358 | fn get(&self, _: &str) -> Result<HttpResponse, String> { |
| 359 | Err("connection refused".into()) |
| 360 | } |
| 361 | fn post(&self, _: &str, _: &Value) -> Result<HttpResponse, String> { |
| 362 | Err("connection refused".into()) |
| 363 | } |
| 364 | } |
| 365 | let cfg = ProbeConfig { |
| 366 | endpoint: "http://x".into(), |
| 367 | model: "m".into(), |
| 368 | skip: vec!["context".into()], |
| 369 | ..Default::default() |
| 370 | }; |
| 371 | let report = run_probes(&Dead, &cfg); |
| 372 | assert!(report.probes.iter().all(|p| matches!( |
| 373 | p.verdict, |
| 374 | Verdict::Fail | Verdict::Skipped | Verdict::Unsupported | Verdict::Undetermined |
| 375 | ))); |
| 376 | assert!(report |
| 377 | .profiles |
| 378 | .iter() |
| 379 | .all(|p| p.status != ProfileStatus::Compatible)); |
| 380 | } |
| 381 | |
| 382 | /// Records every POST body so tests can pin what the probe actually sends. |
| 383 | struct Recorder(std::sync::Mutex<Vec<Value>>); |
| 384 | impl Transport for Recorder { |
| 385 | fn get(&self, _: &str) -> Result<HttpResponse, String> { |
| 386 | Ok(HttpResponse { |
| 387 | status: 404, |
| 388 | body: "{}".into(), |
| 389 | }) |
| 390 | } |
| 391 | fn post(&self, _: &str, body: &Value) -> Result<HttpResponse, String> { |
| 392 | self.0.lock().unwrap().push(body.clone()); |
| 393 | Ok(HttpResponse { |
| 394 | status: 500, |
| 395 | body: "{}".into(), |
| 396 | }) |
| 397 | } |
| 398 | } |
| 399 | |
| 400 | /// The request bodies are pinned per probe id (spec/PROBE.md). The retired Python probe had the same |
| 401 | /// test, `test_chat_basic_request_body_is_pinned`. |
| 402 | #[test] |
| 403 | fn chat_basic_request_body_is_pinned() { |
| 404 | let rec = Recorder(Default::default()); |
| 405 | let cfg = ProbeConfig { |
| 406 | endpoint: "http://127.0.0.1:1/v1".into(), |
| 407 | model: "m".into(), |
| 408 | backend: None, |
| 409 | skip: vec![], |
| 410 | environment: detect_environment(), |
| 411 | }; |
| 412 | let _ = run_probes(&rec, &cfg); |
| 413 | let bodies = rec.0.lock().unwrap(); |
| 414 | assert_eq!( |
| 415 | bodies[0], |
| 416 | json!({"model": "m", "messages": [{"role": "user", "content": "Reply with the single word: ready"}], |
| 417 | "max_tokens": 2048, "temperature": 0, "seed": 42}) |
| 418 | ); |
| 419 | // JCS digest pinned equal to the Python probe's request digest for the same body. |
| 420 | assert_eq!( |
| 421 | digest(&bodies[0]), |
| 422 | "sha256:ed2eb0d61ca98c888bf63c7a884f2f568ee4bc54943f98acbe49db86ecdde923" |
| 423 | ); |
| 424 | } |