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-core / tests / core.rs
| 1 | use interplane_core::*; |
| 2 | use serde_json::{json, Value}; |
| 3 | |
| 4 | fn tool_request_payload() -> Value { |
| 5 | json!({ |
| 6 | "kind": "tool_request", "request_id": "r1", |
| 7 | "tool": {"namespace": "filesystem", "name": "read"}, |
| 8 | "arguments": {"path": "/a"}, |
| 9 | "provenance": {"dialect": "openai", "parser_version": "1.0.0", "x_prov": 1}, |
| 10 | "x_payload": [1, 2] |
| 11 | }) |
| 12 | } |
| 13 | fn envelope_value() -> Value { |
| 14 | json!({ |
| 15 | "interplane_version": "0.1", "message_id": "m1", "trace_id": "t1", |
| 16 | "timestamp": "2026-01-01T00:00:00Z", |
| 17 | "source": {"kind": "model", "id": "x", "x_party": true}, |
| 18 | "destination": {"kind": "runtime", "id": "mock"}, |
| 19 | "payload": tool_request_payload(), |
| 20 | "x_top": {"a": 1} |
| 21 | }) |
| 22 | } |
| 23 | fn decision(kind: &str, approval: Option<&str>) -> Decision { |
| 24 | let mut v = json!({"kind":"decision","request_id":"r1","decision":kind, |
| 25 | "authority":{"runtime":"mock","policy_engine":"mock.policy"}}); |
| 26 | if let Some(a) = approval { |
| 27 | v["approval"] = json!({"approval_id": a}); |
| 28 | } |
| 29 | serde_json::from_value(v).unwrap() |
| 30 | } |
| 31 | |
| 32 | // ---- envelope ---- |
| 33 | #[test] |
| 34 | fn envelope_positive_roundtrip_and_unknown_fields() { |
| 35 | let v = envelope_value(); |
| 36 | let e = validate_envelope_value(&v).unwrap(); |
| 37 | assert_eq!(e.extensions["x_top"], json!({"a": 1})); |
| 38 | assert_eq!(e.source.extensions["x_party"], json!(true)); |
| 39 | let back = serde_json::to_value(&e).unwrap(); |
| 40 | assert_eq!(back, v, "unknown fields survive the round trip"); |
| 41 | let e2: Envelope = serde_json::from_value(back).unwrap(); |
| 42 | assert_eq!(e, e2); |
| 43 | match e.parse_payload().unwrap() { |
| 44 | Payload::ToolRequest(t) => { |
| 45 | assert_eq!(t.extensions["x_payload"], json!([1, 2])); |
| 46 | assert_eq!(t.provenance.extensions["x_prov"], json!(1)); |
| 47 | } |
| 48 | _ => panic!("wrong payload"), |
| 49 | } |
| 50 | } |
| 51 | #[test] |
| 52 | fn envelope_version_major_mismatch() { |
| 53 | let mut v = envelope_value(); |
| 54 | v["interplane_version"] = json!("2.0"); |
| 55 | assert_eq!( |
| 56 | validate_envelope_value(&v).unwrap_err(), |
| 57 | ErrorCode::UnsupportedVersion |
| 58 | ); |
| 59 | // checked first, even when the rest is junk |
| 60 | assert_eq!( |
| 61 | validate_envelope_value(&json!({"interplane_version": "1.0"})).unwrap_err(), |
| 62 | ErrorCode::UnsupportedVersion |
| 63 | ); |
| 64 | v["interplane_version"] = json!("0.7"); |
| 65 | assert!( |
| 66 | validate_envelope_value(&v).is_ok(), |
| 67 | "minor differences are fine" |
| 68 | ); |
| 69 | } |
| 70 | #[test] |
| 71 | fn envelope_negative_cases() { |
| 72 | let cases: Vec<(&str, Value, ErrorCode)> = vec![ |
| 73 | ( |
| 74 | "bad message id", |
| 75 | { |
| 76 | let mut v = envelope_value(); |
| 77 | v["message_id"] = json!("has space"); |
| 78 | v |
| 79 | }, |
| 80 | ErrorCode::MalformedEnvelope, |
| 81 | ), |
| 82 | ( |
| 83 | "too long id", |
| 84 | { |
| 85 | let mut v = envelope_value(); |
| 86 | v["trace_id"] = json!("a".repeat(129)); |
| 87 | v |
| 88 | }, |
| 89 | ErrorCode::MalformedEnvelope, |
| 90 | ), |
| 91 | ( |
| 92 | "bad timestamp", |
| 93 | { |
| 94 | let mut v = envelope_value(); |
| 95 | v["timestamp"] = json!("yesterday"); |
| 96 | v |
| 97 | }, |
| 98 | ErrorCode::MalformedEnvelope, |
| 99 | ), |
| 100 | ( |
| 101 | "bad party kind", |
| 102 | { |
| 103 | let mut v = envelope_value(); |
| 104 | v["source"]["kind"] = json!("alien"); |
| 105 | v |
| 106 | }, |
| 107 | ErrorCode::MalformedEnvelope, |
| 108 | ), |
| 109 | ( |
| 110 | "unknown payload kind", |
| 111 | { |
| 112 | let mut v = envelope_value(); |
| 113 | v["payload"]["kind"] = json!("telepathy"); |
| 114 | v |
| 115 | }, |
| 116 | ErrorCode::MalformedEnvelope, |
| 117 | ), |
| 118 | ( |
| 119 | "arguments not object", |
| 120 | { |
| 121 | let mut v = envelope_value(); |
| 122 | v["payload"]["arguments"] = json!([1]); |
| 123 | v |
| 124 | }, |
| 125 | ErrorCode::MalformedToolCall, |
| 126 | ), |
| 127 | ( |
| 128 | "missing tool", |
| 129 | { |
| 130 | let mut v = envelope_value(); |
| 131 | v["payload"].as_object_mut().unwrap().remove("tool"); |
| 132 | v |
| 133 | }, |
| 134 | ErrorCode::MalformedEnvelope, |
| 135 | ), |
| 136 | ( |
| 137 | "bad request id", |
| 138 | { |
| 139 | let mut v = envelope_value(); |
| 140 | v["payload"]["request_id"] = json!("a b"); |
| 141 | v |
| 142 | }, |
| 143 | ErrorCode::MalformedEnvelope, |
| 144 | ), |
| 145 | ]; |
| 146 | for (name, v, code) in cases { |
| 147 | assert_eq!(validate_envelope_value(&v).unwrap_err(), code, "{name}"); |
| 148 | } |
| 149 | } |
| 150 | #[test] |
| 151 | fn envelope_malformed_input() { |
| 152 | for v in [ |
| 153 | json!(null), |
| 154 | json!([]), |
| 155 | json!("x"), |
| 156 | json!({}), |
| 157 | json!({"interplane_version": 1}), |
| 158 | ] { |
| 159 | assert_eq!( |
| 160 | validate_envelope_value(&v).unwrap_err(), |
| 161 | ErrorCode::MalformedEnvelope |
| 162 | ); |
| 163 | } |
| 164 | let mut v = envelope_value(); |
| 165 | v.as_object_mut().unwrap().remove("payload"); |
| 166 | assert_eq!( |
| 167 | validate_envelope_value(&v).unwrap_err(), |
| 168 | ErrorCode::MalformedEnvelope |
| 169 | ); |
| 170 | } |
| 171 | |
| 172 | // ---- tool_request ---- |
| 173 | #[test] |
| 174 | fn tool_request_roundtrip_const_kind() { |
| 175 | let t: ToolRequest = serde_json::from_value(tool_request_payload()).unwrap(); |
| 176 | let again: ToolRequest = serde_json::from_value(serde_json::to_value(&t).unwrap()).unwrap(); |
| 177 | assert_eq!(t, again); |
| 178 | let mut bad = tool_request_payload(); |
| 179 | bad["kind"] = json!("decision"); |
| 180 | assert!(serde_json::from_value::<ToolRequest>(bad).is_err()); |
| 181 | let mut noargs = tool_request_payload(); |
| 182 | noargs.as_object_mut().unwrap().remove("arguments"); |
| 183 | assert!(serde_json::from_value::<ToolRequest>(noargs).is_err()); |
| 184 | } |
| 185 | |
| 186 | // ---- error codes ---- |
| 187 | #[test] |
| 188 | fn error_code_unknown_is_preserved() { |
| 189 | let c: ErrorCode = serde_json::from_value(json!("from_the_future")).unwrap(); |
| 190 | assert_eq!(c, ErrorCode::Unknown("from_the_future".into())); |
| 191 | assert_eq!(serde_json::to_value(&c).unwrap(), json!("from_the_future")); |
| 192 | assert_eq!( |
| 193 | serde_json::to_value(ErrorCode::PolicyDenied).unwrap(), |
| 194 | json!("policy_denied") |
| 195 | ); |
| 196 | assert!(serde_json::from_value::<ErrorCode>(json!(5)).is_err()); |
| 197 | } |
| 198 | |
| 199 | // ---- decision ---- |
| 200 | #[test] |
| 201 | fn decision_roundtrip_and_unknown_value_is_never_authorized() { |
| 202 | let mut v = json!({"kind":"decision","request_id":"r1","decision":"authorized", |
| 203 | "authority":{"runtime":"mock","policy_engine":"p","x":1},"constraints":["c"],"x_top":true}); |
| 204 | let d: Decision = serde_json::from_value(v.clone()).unwrap(); |
| 205 | assert!(d.is_authorized()); |
| 206 | let ser = serde_json::to_value(&d).unwrap(); |
| 207 | assert_eq!(ser["x_top"], json!(true)); |
| 208 | assert_eq!(ser["authority"]["x"], json!(1)); |
| 209 | assert_eq!( |
| 210 | ser["reason"], |
| 211 | Value::Null, |
| 212 | "pinned shape: nulls are emitted" |
| 213 | ); |
| 214 | assert_eq!(serde_json::from_value::<Decision>(ser).unwrap(), d); |
| 215 | v["decision"] = json!("super_authorized"); |
| 216 | let d: Decision = serde_json::from_value(v).unwrap(); |
| 217 | assert_eq!(d.decision, DecisionKind::Unknown("super_authorized".into())); |
| 218 | assert!(!d.is_authorized()); |
| 219 | assert!(serde_json::from_value::<Decision>( |
| 220 | json!({"kind":"decision","request_id":"r","decision":"authorized"}) |
| 221 | ) |
| 222 | .is_err()); |
| 223 | } |
| 224 | |
| 225 | // ---- result ---- |
| 226 | #[test] |
| 227 | fn result_roundtrip_digest_ignores_duration() { |
| 228 | let mut r = ToolResult::ok("r1", json!({"a": 1})); |
| 229 | r.provenance.as_mut().unwrap().duration_ms = Some(5); |
| 230 | let d1 = r.result_digest(); |
| 231 | r.provenance.as_mut().unwrap().duration_ms = Some(900); |
| 232 | assert_eq!(d1, r.result_digest()); |
| 233 | let back: ToolResult = serde_json::from_value(serde_json::to_value(&r).unwrap()).unwrap(); |
| 234 | assert_eq!(back, r); |
| 235 | assert!(d1.starts_with("sha256:") && d1.len() == 71); |
| 236 | let f = ToolResult::failed( |
| 237 | None, |
| 238 | ResultStatus::Rejected, |
| 239 | ErrorCode::MalformedEnvelope, |
| 240 | "x", |
| 241 | ); |
| 242 | assert_eq!(f.request_id, None); |
| 243 | assert_eq!(serde_json::to_value(&f).unwrap()["request_id"], Value::Null); |
| 244 | } |
| 245 | |
| 246 | // ---- other payloads: round trip + unknown field ---- |
| 247 | #[test] |
| 248 | fn other_payloads_roundtrip() { |
| 249 | let docs = vec![ |
| 250 | json!({"kind":"capability_request","request_id":"r","runtime":"mock","capability":"read_file","arguments":{}, |
| 251 | "tool":{"namespace":null,"name":"read_file"},"mapping":{"table_version":"1","rule_id":"passthrough:read_file","passthrough":true,"catalog_digest":"sha256:".to_string()+&"0".repeat(64),"coerced":["n"],"y":2},"z":1}), |
| 252 | json!({"kind":"event","event":"decided","seq":3,"request_id":"r","turn":0,"payload":{"a":1},"z":1}), |
| 253 | json!({"kind":"catalog","runtime":"mock","catalog_version":"1","capabilities":[ |
| 254 | {"name":"read_file","description":"d","parameters":{"type":"object"},"domains":["filesystem"],"z":1}],"z":1}), |
| 255 | json!({"kind":"selection","runtime":"mock","catalog_digest":"sha256:".to_string()+&"0".repeat(64), |
| 256 | "selector":{"name":"domain_match","version":"1","max_capabilities":3},"requested_domains":["a"], |
| 257 | "selected":[{"name":"x","rule_id":"domain_match"}],"excluded":[{"name":"y","reason":"domain_mismatch"}], |
| 258 | "measure":{"full_count":2,"selected_count":1,"q":1},"z":1}), |
| 259 | json!({"kind":"probe_report","probe_version":"0.1.0","endpoint":"http://h","model":"m","started_at":"2026-01-01T00:00:00Z", |
| 260 | "probes":[{"name":"chat.basic","verdict":"PASS","z":1}], |
| 261 | "profiles":[{"name":"interplane.core.0.1","status":"untested","required_probes":["chat.basic"]}],"z":1}), |
| 262 | ]; |
| 263 | for d in docs { |
| 264 | let p = Payload::from_value(&d).unwrap(); |
| 265 | let ser = match &p { |
| 266 | Payload::CapabilityRequest(x) => serde_json::to_value(x), |
| 267 | Payload::Event(x) => serde_json::to_value(x), |
| 268 | Payload::Catalog(x) => serde_json::to_value(x), |
| 269 | Payload::Selection(x) => serde_json::to_value(x), |
| 270 | Payload::ProbeReport(x) => serde_json::to_value(x), |
| 271 | _ => unreachable!(), |
| 272 | } |
| 273 | .unwrap(); |
| 274 | assert_eq!(ser, d); |
| 275 | assert_eq!(Payload::from_value(&ser).unwrap(), p); |
| 276 | } |
| 277 | assert_eq!( |
| 278 | Payload::from_value(&json!({"kind": "nope"})).unwrap_err(), |
| 279 | ErrorCode::MalformedEnvelope |
| 280 | ); |
| 281 | assert_eq!( |
| 282 | Payload::from_value(&json!({"kind": "event"})).unwrap_err(), |
| 283 | ErrorCode::MalformedEnvelope |
| 284 | ); |
| 285 | } |
| 286 | |
| 287 | #[test] |
| 288 | fn catalog_digest_is_order_independent() { |
| 289 | let a = json!({"kind":"catalog","runtime":"m","catalog_version":"1","capabilities":[ |
| 290 | {"name":"a","description":"","parameters":{}},{"name":"b","description":"","parameters":{"type":"object"}}]}); |
| 291 | let mut b = a.clone(); |
| 292 | b["capabilities"].as_array_mut().unwrap().reverse(); |
| 293 | let ca: Catalog = serde_json::from_value(a).unwrap(); |
| 294 | let cb: Catalog = serde_json::from_value(b).unwrap(); |
| 295 | assert_eq!(ca.compute_digest(), cb.compute_digest()); |
| 296 | } |
| 297 | |
| 298 | // ---- lifecycle ---- |
| 299 | #[test] |
| 300 | fn lifecycle_happy_path() { |
| 301 | let mut l = Lifecycle::new("r1"); |
| 302 | l.map().unwrap(); |
| 303 | assert_eq!( |
| 304 | l.apply_decision(&decision("authorized", None)).unwrap(), |
| 305 | State::Authorized |
| 306 | ); |
| 307 | assert_eq!( |
| 308 | l.begin_execution(&decision("authorized", None)).unwrap(), |
| 309 | State::Executing |
| 310 | ); |
| 311 | assert_eq!(l.finish(State::Succeeded).unwrap(), State::Succeeded); |
| 312 | assert!(l.state().is_terminal()); |
| 313 | assert!(l.finish(State::Failed).is_err()); |
| 314 | } |
| 315 | #[test] |
| 316 | fn lifecycle_denied_is_terminal_and_unknown_is_denied() { |
| 317 | let mut l = Lifecycle::new("r1"); |
| 318 | l.map().unwrap(); |
| 319 | assert_eq!( |
| 320 | l.apply_decision(&decision("denied", None)).unwrap(), |
| 321 | State::Denied |
| 322 | ); |
| 323 | assert!(l.apply_decision(&decision("authorized", None)).is_err()); |
| 324 | assert!(l |
| 325 | .apply_decision(&decision("authorized", Some("a"))) |
| 326 | .is_err()); |
| 327 | assert!(l.begin_execution(&decision("authorized", None)).is_err()); |
| 328 | assert_eq!(l.state(), State::Denied); |
| 329 | let mut l = Lifecycle::new("r1"); |
| 330 | l.map().unwrap(); |
| 331 | assert_eq!( |
| 332 | l.apply_decision(&decision("weird", None)).unwrap(), |
| 333 | State::Denied |
| 334 | ); |
| 335 | } |
| 336 | #[test] |
| 337 | fn lifecycle_cannot_skip_the_decision() { |
| 338 | let mut l = Lifecycle::new("r1"); |
| 339 | assert!(l.begin_execution(&decision("authorized", None)).is_err()); |
| 340 | assert!( |
| 341 | l.apply_decision(&decision("authorized", None)).is_err(), |
| 342 | "not mapped yet" |
| 343 | ); |
| 344 | l.map().unwrap(); |
| 345 | assert!( |
| 346 | l.begin_execution(&decision("authorized", None)).is_err(), |
| 347 | "mapped is not authorized" |
| 348 | ); |
| 349 | assert!(l.finish(State::Succeeded).is_err()); |
| 350 | // a non-authorized decision can never drive execution |
| 351 | l.apply_decision(&decision("authorized", None)).unwrap(); |
| 352 | assert_eq!( |
| 353 | l.begin_execution(&decision("denied", None)), |
| 354 | Err(LifecycleError::NotAuthorized) |
| 355 | ); |
| 356 | assert_eq!( |
| 357 | l.begin_execution(&decision("weird", None)), |
| 358 | Err(LifecycleError::NotAuthorized) |
| 359 | ); |
| 360 | } |
| 361 | #[test] |
| 362 | fn lifecycle_approval_needs_new_decision_with_approval_id() { |
| 363 | let mut l = Lifecycle::new("r1"); |
| 364 | l.map().unwrap(); |
| 365 | assert_eq!( |
| 366 | l.apply_decision(&decision("requires_approval", Some("ap1"))) |
| 367 | .unwrap(), |
| 368 | State::RequiresApproval |
| 369 | ); |
| 370 | assert_eq!( |
| 371 | l.apply_decision(&decision("authorized", None)), |
| 372 | Err(LifecycleError::MissingApproval) |
| 373 | ); |
| 374 | assert_eq!(l.state(), State::RequiresApproval); |
| 375 | assert_eq!( |
| 376 | l.apply_decision(&decision("authorized", Some("ap1"))) |
| 377 | .unwrap(), |
| 378 | State::Authorized |
| 379 | ); |
| 380 | let mut l = Lifecycle::new("r1"); |
| 381 | l.map().unwrap(); |
| 382 | l.apply_decision(&decision("requires_approval", Some("ap1"))) |
| 383 | .unwrap(); |
| 384 | assert_eq!( |
| 385 | l.apply_decision(&decision("denied", Some("ap1"))).unwrap(), |
| 386 | State::Denied |
| 387 | ); |
| 388 | } |
| 389 | #[test] |
| 390 | fn lifecycle_continuation_must_cite_the_minted_approval_id() { |
| 391 | // Ported from the retired Python test test_requires_approval_needs_new_decision_citing_id. |
| 392 | let mut l = Lifecycle::new("r1"); |
| 393 | l.map().unwrap(); |
| 394 | l.apply_decision(&decision("requires_approval", Some("A1"))) |
| 395 | .unwrap(); |
| 396 | for bad in [Some("forged"), Some(""), Some("mock-approval-r2"), None] { |
| 397 | assert_eq!( |
| 398 | l.apply_decision(&decision("authorized", bad)), |
| 399 | Err(LifecycleError::MissingApproval), |
| 400 | "{bad:?} must be refused" |
| 401 | ); |
| 402 | assert_eq!(l.state(), State::RequiresApproval); |
| 403 | } |
| 404 | assert_eq!( |
| 405 | l.apply_decision(&decision("authorized", Some("A1"))) |
| 406 | .unwrap(), |
| 407 | State::Authorized |
| 408 | ); |
| 409 | // A request whose requires_approval decision minted no id can never be continued. |
| 410 | let mut l = Lifecycle::new("r1"); |
| 411 | l.map().unwrap(); |
| 412 | l.apply_decision(&decision("requires_approval", None)) |
| 413 | .unwrap(); |
| 414 | assert_eq!( |
| 415 | l.apply_decision(&decision("authorized", Some("A1"))), |
| 416 | Err(LifecycleError::MissingApproval) |
| 417 | ); |
| 418 | // With the minted id, every value but authorized is DENIED. |
| 419 | for kind in ["requires_approval", "not_found", "invalid", "weird"] { |
| 420 | let mut l = Lifecycle::new("r1"); |
| 421 | l.map().unwrap(); |
| 422 | l.apply_decision(&decision("requires_approval", Some("A1"))) |
| 423 | .unwrap(); |
| 424 | assert_eq!( |
| 425 | l.apply_decision(&decision(kind, Some("A1"))).unwrap(), |
| 426 | State::Denied, |
| 427 | "{kind}" |
| 428 | ); |
| 429 | } |
| 430 | } |
| 431 | #[test] |
| 432 | fn lifecycle_rejections_and_wrong_request() { |
| 433 | let mut l = Lifecycle::new("r1"); |
| 434 | assert_eq!(l.reject().unwrap(), State::Rejected); |
| 435 | assert!(l.map().is_err()); |
| 436 | let mut l = Lifecycle::new("r1"); |
| 437 | l.map().unwrap(); |
| 438 | assert_eq!( |
| 439 | l.apply_decision(&decision("not_found", None)).unwrap(), |
| 440 | State::Rejected |
| 441 | ); |
| 442 | let mut l = Lifecycle::new("r1"); |
| 443 | l.map().unwrap(); |
| 444 | assert_eq!( |
| 445 | l.apply_decision(&decision("invalid", None)).unwrap(), |
| 446 | State::Rejected |
| 447 | ); |
| 448 | let mut l = Lifecycle::new("other"); |
| 449 | l.map().unwrap(); |
| 450 | assert_eq!( |
| 451 | l.apply_decision(&decision("authorized", None)), |
| 452 | Err(LifecycleError::WrongRequest) |
| 453 | ); |
| 454 | } |
| 455 | |
| 456 | // ---- property tests ---- |
| 457 | mod props { |
| 458 | use super::*; |
| 459 | use proptest::prelude::*; |
| 460 | |
| 461 | fn arb_json() -> impl Strategy<Value = Value> { |
| 462 | let leaf = prop_oneof![ |
| 463 | Just(Value::Null), |
| 464 | any::<bool>().prop_map(Value::Bool), |
| 465 | any::<i64>().prop_map(|i| json!(i)), |
| 466 | "[ -~é\u{1f600}\n\t]{0,12}".prop_map(Value::String), |
| 467 | ]; |
| 468 | leaf.prop_recursive(3, 24, 4, |inner| { |
| 469 | prop_oneof![ |
| 470 | prop::collection::vec(inner.clone(), 0..4).prop_map(Value::Array), |
| 471 | prop::collection::hash_map("[a-z]{0,4}", inner, 0..4) |
| 472 | .prop_map(|m| Value::Object(m.into_iter().collect())), |
| 473 | ] |
| 474 | }) |
| 475 | } |
| 476 | proptest! { |
| 477 | #[test] |
| 478 | fn canonical_parses_back_equal_and_is_stable(v in arb_json()) { |
| 479 | let c = canonicalize(&v); |
| 480 | let back: Value = serde_json::from_str(&c).unwrap(); |
| 481 | prop_assert_eq!(&back, &v); |
| 482 | prop_assert_eq!(canonicalize(&back), c); |
| 483 | } |
| 484 | #[test] |
| 485 | fn id_pattern_matches_reference(s in "[ -~]{0,10}") { |
| 486 | let expect = !s.is_empty() && s.chars().all(|c| c.is_ascii_alphanumeric() || "._:-".contains(c)); |
| 487 | prop_assert_eq!(is_valid_id(&s), expect); |
| 488 | } |
| 489 | } |
| 490 | } |
| 491 | |
| 492 | #[test] |
| 493 | fn result_shape_is_pinned() { |
| 494 | let r = ToolResult::failed( |
| 495 | Some("r1"), |
| 496 | ResultStatus::Denied, |
| 497 | ErrorCode::RuntimeUnavailable, |
| 498 | "m", |
| 499 | ); |
| 500 | let v = serde_json::to_value(&r).unwrap(); |
| 501 | let keys: Vec<&String> = v.as_object().unwrap().keys().collect(); |
| 502 | assert_eq!( |
| 503 | keys, |
| 504 | [ |
| 505 | "data", |
| 506 | "decision", |
| 507 | "error", |
| 508 | "kind", |
| 509 | "provenance", |
| 510 | "request_id", |
| 511 | "status" |
| 512 | ] |
| 513 | ); |
| 514 | let pk: Vec<&String> = v["provenance"].as_object().unwrap().keys().collect(); |
| 515 | assert_eq!( |
| 516 | pk, |
| 517 | [ |
| 518 | "capability", |
| 519 | "content_kind", |
| 520 | "duration_ms", |
| 521 | "runtime", |
| 522 | "trust", |
| 523 | "trusted" |
| 524 | ] |
| 525 | ); |
| 526 | assert_eq!( |
| 527 | v["error"], |
| 528 | json!({"code": "runtime_unavailable", "message": "m", "retryable": true}) |
| 529 | ); |
| 530 | let t = ToolResult::failed( |
| 531 | Some("r1"), |
| 532 | ResultStatus::Error, |
| 533 | ErrorCode::ExecutionError, |
| 534 | "m", |
| 535 | ); |
| 536 | assert_eq!( |
| 537 | serde_json::to_value(&t).unwrap()["error"]["retryable"], |
| 538 | json!(false) |
| 539 | ); |
| 540 | } |
| 541 | |
| 542 | #[test] |
| 543 | fn trust_levels_and_unknown_values() { |
| 544 | assert_eq!( |
| 545 | TrustLevel::parse("trusted_runtime").trusted_flag(), |
| 546 | Some(true) |
| 547 | ); |
| 548 | assert_eq!( |
| 549 | TrustLevel::parse("external_untrusted").trusted_flag(), |
| 550 | Some(false) |
| 551 | ); |
| 552 | assert_eq!(TrustLevel::parse("unknown").trusted_flag(), None); |
| 553 | assert_eq!(TrustLevel::parse("user_supplied").trusted_flag(), None); |
| 554 | assert_eq!( |
| 555 | TrustLevel::parse("godmode").trusted_flag(), |
| 556 | Some(false), |
| 557 | "never trusted by accident" |
| 558 | ); |
| 559 | assert_eq!(ContentKind::parse("web_content").as_str(), "web_content"); |
| 560 | } |
| 561 | |
| 562 | #[test] |
| 563 | fn input_record_examples_roundtrip_and_null_parent_is_explicit() { |
| 564 | let dir = std::path::Path::new(env!("CARGO_MANIFEST_DIR")) |
| 565 | .join("../../../conformance/fixtures/input"); |
| 566 | let mut n = 0; |
| 567 | for e in std::fs::read_dir(dir).unwrap() { |
| 568 | let v: Value = |
| 569 | serde_json::from_str(&std::fs::read_to_string(e.unwrap().path()).unwrap()).unwrap(); |
| 570 | let rec: InputRecord = serde_json::from_value(v.clone()).unwrap(); |
| 571 | assert_eq!(serde_json::to_value(&rec).unwrap(), v); |
| 572 | n += 1; |
| 573 | } |
| 574 | assert_eq!(n, 9); |
| 575 | let mut v = json!({ |
| 576 | "input_id": "i", "content_kind": "memory", "trust": "made_up", "source": {"kind": "runtime", "id": "r"}, |
| 577 | "origin": "m", "content_digest": "sha256:00", "trace_id": "t", "x": 1 |
| 578 | }); |
| 579 | let rec: InputRecord = serde_json::from_value(v.clone()).unwrap(); |
| 580 | assert_eq!(rec.parent_id, None); |
| 581 | assert!(rec.derived_from.is_empty()); |
| 582 | v["parent_id"] = Value::Null; |
| 583 | v["derived_from"] = json!([]); |
| 584 | assert_eq!(serde_json::to_value(&rec).unwrap(), v); |
| 585 | } |
| 586 | |
| 587 | #[test] |
| 588 | fn exposure_roundtrips_inside_provenance_extensions() { |
| 589 | let mut p = tool_request_payload(); |
| 590 | p["provenance"]["exposure"] = json!({"inputs": ["a"], "floor": "unknown"}); |
| 591 | let tr: ToolRequest = serde_json::from_value(p.clone()).unwrap(); |
| 592 | assert_eq!(serde_json::to_value(&tr).unwrap(), p); |
| 593 | let ex: Exposure = serde_json::from_value(p["provenance"]["exposure"].clone()).unwrap(); |
| 594 | assert_eq!(ex.floor, TrustLevel::Undetermined); |
| 595 | } |
| 596 | |
| 597 | #[test] |
| 598 | fn cancel_only_leaves_requires_approval_and_only_to_denied() { |
| 599 | let mut l = Lifecycle::new("r"); |
| 600 | assert!(l.cancel().is_err()); |
| 601 | l.map().unwrap(); |
| 602 | assert!(l.cancel().is_err()); |
| 603 | let d: Decision = serde_json::from_value(serde_json::json!({ |
| 604 | "kind": "decision", "request_id": "r", "decision": "requires_approval", |
| 605 | "authority": {"runtime": "m", "policy_engine": "p"}, |
| 606 | "approval": {"approval_id": "a1"} |
| 607 | })) |
| 608 | .unwrap(); |
| 609 | l.apply_decision(&d).unwrap(); |
| 610 | assert_eq!(l.cancel(), Ok(State::Denied)); |
| 611 | assert!(l.cancel().is_err()); |
| 612 | assert_eq!(l.state(), State::Denied); |
| 613 | } |