| 1 | use codewhale_app_server_protocol::rpc::{ |
| 2 | RequestId, RpcCall, RpcErrorDetails, RpcErrorResponse, RpcMessage, RpcResponse, |
| 3 | }; |
| 4 | use serde_json::{Value, json}; |
| 5 | |
| 6 | #[test] |
| 7 | fn correlated_rpc_envelopes_match_pinned_stable_schema() { |
| 8 | let request_schema: Value = |
| 9 | serde_json::from_str(include_str!("pinned/JSONRPCRequest.json")).unwrap(); |
| 10 | let response_schema: Value = |
| 11 | serde_json::from_str(include_str!("pinned/JSONRPCResponse.json")).unwrap(); |
| 12 | let error_schema: Value = |
| 13 | serde_json::from_str(include_str!("pinned/JSONRPCError.json")).unwrap(); |
| 14 | let message_schema: Value = |
| 15 | serde_json::from_str(include_str!("pinned/JSONRPCMessage.json")).unwrap(); |
| 16 | let notification_schema: Value = |
| 17 | serde_json::from_str(include_str!("pinned/JSONRPCNotification.json")).unwrap(); |
| 18 | let message_validator = jsonschema::validator_for(&message_schema).unwrap(); |
| 19 | let notification_validator = jsonschema::validator_for(¬ification_schema).unwrap(); |
| 20 | let request_validator = jsonschema::validator_for(&request_schema).unwrap(); |
| 21 | let response_validator = jsonschema::validator_for(&response_schema).unwrap(); |
| 22 | let error_validator = jsonschema::validator_for(&error_schema).unwrap(); |
| 23 | for id in [ |
| 24 | RequestId::String(String::new()), |
| 25 | RequestId::String("request/string".into()), |
| 26 | RequestId::Integer(i64::MIN), |
| 27 | RequestId::Integer(i64::MAX), |
| 28 | ] { |
| 29 | let call: RpcCall = serde_json::from_value(json!({"id":id,"method":"initialize","params":{"clientInfo":{"name":"fixture","version":"1"}}})).unwrap(); |
| 30 | let request = serde_json::to_value(call).unwrap(); |
| 31 | assert!(request.get("jsonrpc").is_none()); |
| 32 | assert!(request_validator.is_valid(&request)); |
| 33 | assert!(message_validator.is_valid(&request)); |
| 34 | assert!(matches!( |
| 35 | serde_json::from_value::<RpcMessage>(request).unwrap(), |
| 36 | RpcMessage::Request(_) |
| 37 | )); |
| 38 | let response = serde_json::to_value(RpcResponse { |
| 39 | id: id.clone(), |
| 40 | result: json!({}), |
| 41 | }) |
| 42 | .unwrap(); |
| 43 | assert!(response_validator.is_valid(&response)); |
| 44 | assert!(message_validator.is_valid(&response)); |
| 45 | assert!(matches!( |
| 46 | serde_json::from_value::<RpcMessage>(response).unwrap(), |
| 47 | RpcMessage::Response(_) |
| 48 | )); |
| 49 | let error = serde_json::to_value(RpcErrorResponse { |
| 50 | id, |
| 51 | error: RpcErrorDetails { |
| 52 | code: -32600, |
| 53 | data: None, |
| 54 | message: "Not initialized".into(), |
| 55 | }, |
| 56 | }) |
| 57 | .unwrap(); |
| 58 | assert!(error_validator.is_valid(&error)); |
| 59 | assert!(error["error"].get("data").is_none()); |
| 60 | assert!(message_validator.is_valid(&error)); |
| 61 | assert!(matches!( |
| 62 | serde_json::from_value::<RpcMessage>(error).unwrap(), |
| 63 | RpcMessage::Error(_) |
| 64 | )); |
| 65 | } |
| 66 | let notification: RpcMessage = serde_json::from_value(json!({"method":"initialized"})).unwrap(); |
| 67 | let value = serde_json::to_value(notification).unwrap(); |
| 68 | assert!(notification_validator.is_valid(&value)); |
| 69 | assert!(message_validator.is_valid(&value)); |
| 70 | assert!(value.get("id").is_none()); |
| 71 | } |
| 72 | |
| 73 | #[test] |
| 74 | fn rpc_call_preserves_notification_absence_and_refuses_non_ids() { |
| 75 | let notification: RpcCall = |
| 76 | serde_json::from_value(json!({"jsonrpc":"2.0","method":"initialized"})).unwrap(); |
| 77 | let value = serde_json::to_value(notification).unwrap(); |
| 78 | assert!(value.get("id").is_none()); |
| 79 | assert!(value.get("jsonrpc").is_none()); |
| 80 | for id in [ |
| 81 | json!(null), |
| 82 | json!(true), |
| 83 | json!(1.0), |
| 84 | json!(u64::MAX), |
| 85 | json!({}), |
| 86 | json!([]), |
| 87 | ] { |
| 88 | assert!(serde_json::from_value::<RequestId>(id.clone()).is_err()); |
| 89 | let call: RpcCall = serde_json::from_value(json!({"id":id,"method":"shutdown"})).unwrap(); |
| 90 | assert!(call.id.is_none()); |
| 91 | } |
| 92 | for version in [json!(null), json!(true), json!("1.0")] { |
| 93 | let call: RpcCall = |
| 94 | serde_json::from_value(json!({"jsonrpc":version,"id":1,"method":"initialize"})) |
| 95 | .unwrap(); |
| 96 | assert_eq!(call.id, Some(RequestId::Integer(1))); |
| 97 | assert!(serde_json::to_value(call).unwrap().get("jsonrpc").is_none()); |
| 98 | } |
| 99 | assert!(serde_json::from_value::<RpcCall>(json!({"id":1,"method":7})).is_err()); |
| 100 | assert!(serde_json::from_value::<RpcMessage>(json!({"id":null,"result":{}})).is_err()); |
| 101 | assert!( |
| 102 | serde_json::from_value::<RpcMessage>( |
| 103 | json!({"id":null,"error":{"code":-32600,"message":"invalid"}}) |
| 104 | ) |
| 105 | .is_err() |
| 106 | ); |
| 107 | assert!(serde_json::from_value::<RpcCall>(json!({"id":1,"result":{}})).is_err()); |
| 108 | for params in [json!({}), json!(null), json!(["value"])] { |
| 109 | let message: RpcMessage = |
| 110 | serde_json::from_value(json!({"id":1,"method":"fixture","params":params})).unwrap(); |
| 111 | let encoded = serde_json::to_value(message).unwrap(); |
| 112 | if params.is_null() { |
| 113 | assert!(encoded.get("params").is_none()); |
| 114 | } else { |
| 115 | assert_eq!(encoded["params"], params); |
| 116 | } |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | #[test] |
| 121 | fn initialization_contract_matches_pinned_stable_schema() { |
| 122 | use codewhale_app_server_protocol::lifecycle::{InitializeParams, InitializeResponse}; |
| 123 | let params_schema: Value = |
| 124 | serde_json::from_str(include_str!("pinned/InitializeParams.json")).unwrap(); |
| 125 | let result_schema: Value = |
| 126 | serde_json::from_str(include_str!("pinned/InitializeResponse.json")).unwrap(); |
| 127 | let params: InitializeParams = serde_json::from_value(json!({"clientInfo":{"name":"fixture","version":"1"},"capabilities":{"experimentalApi":false,"optOutNotificationMethods":null}})).unwrap(); |
| 128 | assert!( |
| 129 | jsonschema::validator_for(¶ms_schema) |
| 130 | .unwrap() |
| 131 | .is_valid(&serde_json::to_value(params).unwrap()) |
| 132 | ); |
| 133 | let result = InitializeResponse { |
| 134 | user_agent: "Codewhale/fixture".into(), |
| 135 | codex_home: std::path::PathBuf::from("/fixture/codewhale"), |
| 136 | platform_family: "unix".into(), |
| 137 | platform_os: "macos".into(), |
| 138 | }; |
| 139 | let value = serde_json::to_value(result).unwrap(); |
| 140 | assert!( |
| 141 | jsonschema::validator_for(&result_schema) |
| 142 | .unwrap() |
| 143 | .is_valid(&value) |
| 144 | ); |
| 145 | assert_eq!(value["codexHome"], "/fixture/codewhale"); |
| 146 | } |
| 147 |