| 1 | #![cfg(feature = "schema")] |
| 2 | |
| 3 | use codewhale_app_server_protocol::runtime::{ |
| 4 | DynamicToolCallResult, DynamicToolSpec, RuntimeEventEnvelope, RuntimeImageInput, |
| 5 | }; |
| 6 | use std::collections::BTreeMap; |
| 7 | |
| 8 | #[test] |
| 9 | fn runtime_schema_matches_real_nullable_and_flattened_wire_fields() { |
| 10 | let schema = codewhale_app_server_protocol::export::runtime_schema(); |
| 11 | let definitions = schema["$defs"].as_object().unwrap(); |
| 12 | let event = &definitions["RuntimeEventEnvelope"]; |
| 13 | let turn = &event["properties"]["turn_id"]; |
| 14 | let nullable = turn["type"] |
| 15 | .as_array() |
| 16 | .is_some_and(|types| types.iter().any(|ty| ty == "null")) |
| 17 | || turn["anyOf"] |
| 18 | .as_array() |
| 19 | .is_some_and(|types| types.iter().any(|ty| ty["type"] == "null")); |
| 20 | assert!(nullable); |
| 21 | let required = event["required"].as_array().unwrap(); |
| 22 | for field in ["schema_version", "turn_id", "item_id", "payload"] { |
| 23 | assert!(required.iter().any(|name| name == field)); |
| 24 | } |
| 25 | assert!(!required.iter().any(|name| name == "created_at")); |
| 26 | assert_eq!(event["properties"]["created_at"]["type"], "string"); |
| 27 | for name in ["RuntimeCapabilities", "RuntimeExperimentalCapabilities"] { |
| 28 | let record = &definitions[name]; |
| 29 | let required = record["required"].as_array().unwrap(); |
| 30 | for field in record["properties"].as_object().unwrap().keys() { |
| 31 | assert!( |
| 32 | required |
| 33 | .iter() |
| 34 | .any(|name| name.as_str() == Some(field.as_str())) |
| 35 | ); |
| 36 | } |
| 37 | } |
| 38 | let spec = &definitions["DynamicToolSpec"]; |
| 39 | assert!( |
| 40 | !spec["required"] |
| 41 | .as_array() |
| 42 | .unwrap() |
| 43 | .iter() |
| 44 | .any(|name| name == "defer_loading") |
| 45 | ); |
| 46 | let published_spec = &definitions["DynamicToolSpecOutput"]; |
| 47 | assert!( |
| 48 | published_spec["required"] |
| 49 | .as_array() |
| 50 | .unwrap() |
| 51 | .iter() |
| 52 | .any(|name| name == "defer_loading") |
| 53 | ); |
| 54 | assert_eq!(published_spec["properties"]["namespace"]["type"], "string"); |
| 55 | assert!( |
| 56 | !definitions["DynamicToolCallResult"]["required"] |
| 57 | .as_array() |
| 58 | .unwrap() |
| 59 | .iter() |
| 60 | .any(|name| name == "content") |
| 61 | ); |
| 62 | let input: DynamicToolSpec = serde_json::from_value( |
| 63 | serde_json::json!({"name":"tool","description":"fixture","input_schema":{}}), |
| 64 | ) |
| 65 | .unwrap(); |
| 66 | assert!(!input.defer_loading); |
| 67 | let result: DynamicToolCallResult = |
| 68 | serde_json::from_value(serde_json::json!({"success":false})).unwrap(); |
| 69 | assert!(result.content.is_empty()); |
| 70 | assert!( |
| 71 | serde_json::from_value::<RuntimeImageInput>( |
| 72 | serde_json::json!({"mime":"image/png","dataBase64":"","extra":true}) |
| 73 | ) |
| 74 | .is_err() |
| 75 | ); |
| 76 | assert!(event["properties"].get("extra").is_none()); |
| 77 | assert_eq!( |
| 78 | definitions["RuntimeImageInput"]["additionalProperties"], |
| 79 | false |
| 80 | ); |
| 81 | let value = serde_json::to_value(RuntimeEventEnvelope { |
| 82 | schema_version: 1, |
| 83 | seq: 4, |
| 84 | event: "future.name".into(), |
| 85 | kind: "future.name".into(), |
| 86 | thread_id: "thread".into(), |
| 87 | turn_id: None, |
| 88 | item_id: None, |
| 89 | timestamp: "now".into(), |
| 90 | created_at: None, |
| 91 | payload: serde_json::json!({"receipt": null}), |
| 92 | extra: BTreeMap::new(), |
| 93 | }) |
| 94 | .unwrap(); |
| 95 | assert!(value["turn_id"].is_null()); |
| 96 | assert!(value.get("created_at").is_none()); |
| 97 | let typescript = codewhale_app_server_protocol::export::runtime_typescript(&schema); |
| 98 | assert!(typescript.contains("turn_id: (string | null)")); |
| 99 | assert!(!typescript.contains("turn_id?:")); |
| 100 | assert!(typescript.contains("created_at?: string")); |
| 101 | assert!(typescript.contains("export type RuntimeImageInput =")); |
| 102 | assert!(typescript.contains("Json[] | { [key: string]: Json | undefined }")); |
| 103 | assert!(typescript.contains("type: 'input_text'")); |
| 104 | assert!(typescript.contains("dataBase64: string")); |
| 105 | assert!(typescript.contains("[key: string]: Json | undefined")); |
| 106 | } |
| 107 | |
| 108 | #[test] |
| 109 | fn committed_runtime_schema_and_sdk_exports_match_one_rust_contract() { |
| 110 | let package = std::path::Path::new(env!("CARGO_MANIFEST_DIR")); |
| 111 | let repository = package.parent().and_then(std::path::Path::parent).unwrap(); |
| 112 | let schema = codewhale_app_server_protocol::export::runtime_schema(); |
| 113 | let json = serde_json::to_string_pretty(&schema).unwrap() + "\n"; |
| 114 | let typescript = codewhale_app_server_protocol::export::runtime_typescript(&schema); |
| 115 | assert_eq!( |
| 116 | std::fs::read_to_string(package.join("schema/json/runtime.schema.json")).unwrap(), |
| 117 | json |
| 118 | ); |
| 119 | assert_eq!( |
| 120 | std::fs::read_to_string(package.join("schema/typescript/runtime.d.ts")).unwrap(), |
| 121 | typescript |
| 122 | ); |
| 123 | assert_eq!( |
| 124 | std::fs::read_to_string(repository.join("npm/runtime-sdk/generated/runtime.d.ts")).unwrap(), |
| 125 | typescript |
| 126 | ); |
| 127 | } |
| 128 |