@@ -2,39 +2,71 @@ mod tests {
2
2
use rmcp:: model:: { ClientJsonRpcMessage , ServerJsonRpcMessage } ;
3
3
use schemars:: schema_for;
4
4
5
+ fn compare_schemas ( name : & str , actual : & str , expected_file : & str ) {
6
+ let expected = match std:: fs:: read_to_string ( expected_file) {
7
+ Ok ( content) => content,
8
+ Err ( e) => {
9
+ panic ! (
10
+ "Failed to read expected schema file {}: {}" ,
11
+ expected_file, e
12
+ ) ;
13
+ }
14
+ } ;
15
+
16
+ let actual_json: serde_json:: Value =
17
+ serde_json:: from_str ( actual) . expect ( "Failed to parse actual schema as JSON" ) ;
18
+ let expected_json: serde_json:: Value =
19
+ serde_json:: from_str ( & expected) . expect ( "Failed to parse expected schema as JSON" ) ;
20
+
21
+ if actual_json == expected_json {
22
+ println ! ( "{} schema matches expected" , name) ;
23
+ return ;
24
+ }
25
+
26
+ // Write current schema to file for comparison
27
+ let current_file = expected_file. replace ( ".json" , "_current.json" ) ;
28
+ std:: fs:: write ( & current_file, actual) . expect ( "Failed to write current schema" ) ;
29
+
30
+ println ! ( "{} schema differs from expected" , name) ;
31
+ println ! ( "Expected: {}" , expected_file) ;
32
+ println ! ( "Current: {}" , current_file) ;
33
+ println ! (
34
+ "Run 'diff {} {}' to see differences" ,
35
+ expected_file, current_file
36
+ ) ;
37
+
38
+ // UPDATE_SCHEMA=1 cargo test -p rmcp --test test_message_schema --features="server client schemars"
39
+ if std:: env:: var ( "UPDATE_SCHEMA" ) . is_ok ( ) {
40
+ println ! ( "UPDATE_SCHEMA is set, updating expected file" ) ;
41
+ std:: fs:: write ( expected_file, actual) . expect ( "Failed to update expected schema file" ) ;
42
+ println ! ( "Updated {}" , expected_file) ;
43
+ } else {
44
+ println ! ( "Set UPDATE_SCHEMA=1 to auto-update expected schemas" ) ;
45
+ panic ! ( "Schema validation failed" ) ;
46
+ }
47
+ }
48
+
5
49
#[ test]
6
50
fn test_client_json_rpc_message_schema ( ) {
7
51
let schema = schema_for ! ( ClientJsonRpcMessage ) ;
8
- let schema_str = serde_json:: to_string_pretty ( & schema) . unwrap ( ) ;
9
- let expected = std:: fs:: read_to_string (
52
+ let schema_str = serde_json:: to_string_pretty ( & schema) . expect ( "Failed to serialize schema" ) ;
53
+
54
+ compare_schemas (
55
+ "ClientJsonRpcMessage" ,
56
+ & schema_str,
10
57
"tests/test_message_schema/client_json_rpc_message_schema.json" ,
11
- )
12
- . unwrap ( ) ;
13
-
14
- // Parse both strings to JSON values for more robust comparison
15
- let schema_json: serde_json:: Value = serde_json:: from_str ( & schema_str) . unwrap ( ) ;
16
- let expected_json: serde_json:: Value = serde_json:: from_str ( & expected) . unwrap ( ) ;
17
- assert_eq ! (
18
- schema_json, expected_json,
19
- "Schema generation for ClientJsonRpcMessage should match expected output"
20
58
) ;
21
59
}
22
60
23
61
#[ test]
24
62
fn test_server_json_rpc_message_schema ( ) {
25
63
let schema = schema_for ! ( ServerJsonRpcMessage ) ;
26
- let schema_str = serde_json:: to_string_pretty ( & schema) . unwrap ( ) ;
27
- let expected = std:: fs:: read_to_string (
64
+ let schema_str = serde_json:: to_string_pretty ( & schema) . expect ( "Failed to serialize schema" ) ;
65
+
66
+ compare_schemas (
67
+ "ServerJsonRpcMessage" ,
68
+ & schema_str,
28
69
"tests/test_message_schema/server_json_rpc_message_schema.json" ,
29
- )
30
- . unwrap ( ) ;
31
-
32
- // Parse both strings to JSON values for more robust comparison
33
- let schema_json: serde_json:: Value = serde_json:: from_str ( & schema_str) . unwrap ( ) ;
34
- let expected_json: serde_json:: Value = serde_json:: from_str ( & expected) . unwrap ( ) ;
35
- assert_eq ! (
36
- schema_json, expected_json,
37
- "Schema generation for ServerJsonRpcMessage should match expected output"
38
70
) ;
39
71
}
40
72
}
0 commit comments