Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 54 additions & 22 deletions crates/rmcp/tests/test_message_schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,71 @@ mod tests {
use rmcp::model::{ClientJsonRpcMessage, ServerJsonRpcMessage};
use schemars::schema_for;

fn compare_schemas(name: &str, actual: &str, expected_file: &str) {
let expected = match std::fs::read_to_string(expected_file) {
Ok(content) => content,
Err(e) => {
panic!(
"Failed to read expected schema file {}: {}",
expected_file, e
);
}
};

let actual_json: serde_json::Value =
serde_json::from_str(actual).expect("Failed to parse actual schema as JSON");
let expected_json: serde_json::Value =
serde_json::from_str(&expected).expect("Failed to parse expected schema as JSON");

if actual_json == expected_json {
println!("{} schema matches expected", name);
return;
}

// Write current schema to file for comparison
let current_file = expected_file.replace(".json", "_current.json");
std::fs::write(&current_file, actual).expect("Failed to write current schema");

println!("{} schema differs from expected", name);
println!("Expected: {}", expected_file);
println!("Current: {}", current_file);
println!(
"Run 'diff {} {}' to see differences",
expected_file, current_file
);

// UPDATE_SCHEMA=1 cargo test -p rmcp --test test_message_schema --features="server client schemars"
if std::env::var("UPDATE_SCHEMA").is_ok() {
println!("UPDATE_SCHEMA is set, updating expected file");
std::fs::write(expected_file, actual).expect("Failed to update expected schema file");
println!("Updated {}", expected_file);
} else {
println!("Set UPDATE_SCHEMA=1 to auto-update expected schemas");
panic!("Schema validation failed");
}
}

#[test]
fn test_client_json_rpc_message_schema() {
let schema = schema_for!(ClientJsonRpcMessage);
let schema_str = serde_json::to_string_pretty(&schema).unwrap();
let expected = std::fs::read_to_string(
let schema_str = serde_json::to_string_pretty(&schema).expect("Failed to serialize schema");

compare_schemas(
"ClientJsonRpcMessage",
&schema_str,
"tests/test_message_schema/client_json_rpc_message_schema.json",
)
.unwrap();

// Parse both strings to JSON values for more robust comparison
let schema_json: serde_json::Value = serde_json::from_str(&schema_str).unwrap();
let expected_json: serde_json::Value = serde_json::from_str(&expected).unwrap();
assert_eq!(
schema_json, expected_json,
"Schema generation for ClientJsonRpcMessage should match expected output"
);
}

#[test]
fn test_server_json_rpc_message_schema() {
let schema = schema_for!(ServerJsonRpcMessage);
let schema_str = serde_json::to_string_pretty(&schema).unwrap();
let expected = std::fs::read_to_string(
let schema_str = serde_json::to_string_pretty(&schema).expect("Failed to serialize schema");

compare_schemas(
"ServerJsonRpcMessage",
&schema_str,
"tests/test_message_schema/server_json_rpc_message_schema.json",
)
.unwrap();

// Parse both strings to JSON values for more robust comparison
let schema_json: serde_json::Value = serde_json::from_str(&schema_str).unwrap();
let expected_json: serde_json::Value = serde_json::from_str(&expected).unwrap();
assert_eq!(
schema_json, expected_json,
"Schema generation for ServerJsonRpcMessage should match expected output"
);
}
}
Loading