Skip to content

Commit fd74955

Browse files
authored
test: test coverage for schema_utils has been increased to 64%. (#6)
* chore: add test coverage alias * chore: improve test coverage * test: increase schema_utils test coverage up to 64%
1 parent 9da0787 commit fd74955

File tree

7 files changed

+176
-6
lines changed

7 files changed

+176
-6
lines changed

.cargo/config.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
[alias]
2+
3+
coverage = ["tarpaulin"]
24
run_clippy = [
35
"clippy",
46
"--features",

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
settings.json
44

55

6+
67
# test artifacts
78
/coverage
9+
.coverage
810
*.lcov
911
*.profraw

src/generated_schema/2024_11_05/schema_utils.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1223,5 +1223,9 @@ mod tests {
12231223
);
12241224
let result = detect_message_type(&json!(message));
12251225
assert!(matches!(result, MessageTypes::Error));
1226+
1227+
// default
1228+
let result = detect_message_type(&json!({}));
1229+
assert!(matches!(result, MessageTypes::Request));
12261230
}
12271231
}

tarpaulin.toml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
[version_2024_11_05]
2+
no-default-features = true
3+
features = "2024_11_05 schema_utils"
4+
# release = true
5+
6+
[version_draft]
7+
no-default-features = true
8+
features = "draft schema_utils"
9+
# release = true
10+
11+
[report]
12+
output-dir = "./.coverage"
13+
out = ["Html", "Xml"]

tests/common/sample_mcp_messages.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757
"req_tools_call_4": {"jsonrpc":"2.0","id":18,"method":"tools/call","params":{"_meta":{"progressToken":3},"name":"sampleLLM","arguments":{"a":15,"b":21,"prompt":"my prompt","maxTokens":5}}},
5858
/* ServerRequest::CreateMessageRequest */
5959
"req_sampling_create_message_2": {"method":"sampling/createMessage","params":{"messages":[{"role":"user","content":{"type":"text","text":"Resource sampleLLM context: my prompt"}}],"systemPrompt":"You are a helpful test server.","maxTokens":5,"temperature":0.7,"includeContext":"thisServer"},"jsonrpc":"2.0","id":1},
60-
/* ServerRequest::CreateMessageRequest */
60+
/* ClientResult::CreateMessageResult */
6161
"res_sampling_create_message_2": {"jsonrpc":"2.0","id":1,"result":{"model":"stub-model","stopReason":"endTurn","role":"assistant","content":{"type":"text","text":"This is a stub response."}}},
6262
/* ServerResult::CallToolResult */
6363
"res_tools_call_4": {"result":{"content":[{"type":"text","text":"LLM sampling result: This is a stub response."}]},"jsonrpc":"2.0","id":18},

tests/test_deserialize.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,8 @@
22
pub mod common;
33

44
mod test_deserialize {
5-
use rust_mcp_schema::{
6-
schema_utils::*, ClientNotification, ClientRequest, RequestId, ServerRequest, ServerResult, JSONRPC_VERSION,
7-
LATEST_PROTOCOL_VERSION,
8-
};
5+
use rust_mcp_schema::schema_utils::*;
6+
use rust_mcp_schema::*;
97

108
use super::common::get_message;
119

@@ -126,6 +124,16 @@ mod test_deserialize {
126124
));
127125
}
128126

127+
/* ---------------------- CLIENT RESPONSES ---------------------- */
128+
#[test]
129+
fn test_list_tools_result() {
130+
let message = get_message("res_sampling_create_message_2");
131+
assert!(matches!(message, ClientMessage::Response(client_message)
132+
if matches!(&client_message.result, ResultFromClient::ClientResult(client_result)
133+
if matches!( client_result, ClientResult::CreateMessageResult(_))
134+
)
135+
));
136+
}
129137
/* ---------------------- SERVER RESPONSES ---------------------- */
130138

131139
#[test]

tests/test_serialize.rs

Lines changed: 142 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,11 @@ mod test_serialize {
2727
protocol_version: LATEST_PROTOCOL_VERSION.to_string(),
2828
});
2929

30+
let client_request = ClientRequest::InitializeRequest(request);
31+
3032
let message: ClientMessage = ClientMessage::Request(ClientJsonrpcRequest::new(
3133
RequestId::Integer(15),
32-
RequestFromClient::ClientRequest(ClientRequest::InitializeRequest(request)),
34+
RequestFromClient::ClientRequest(client_request.clone()),
3335
));
3436

3537
let message: ClientMessage = re_serialize(message);
@@ -38,6 +40,17 @@ mod test_serialize {
3840
if matches!(&client_message.request, RequestFromClient::ClientRequest(client_request)
3941
if matches!(client_request, ClientRequest::InitializeRequest(_)))
4042
));
43+
44+
// test From<ClientRequest> for RequestFromClient
45+
let message: ClientMessage =
46+
ClientMessage::Request(ClientJsonrpcRequest::new(RequestId::Integer(15), client_request.into()));
47+
48+
let message: ClientMessage = re_serialize(message);
49+
50+
assert!(matches!(message, ClientMessage::Request(client_message)
51+
if matches!(&client_message.request, RequestFromClient::ClientRequest(client_request)
52+
if matches!(client_request, ClientRequest::InitializeRequest(_)))
53+
));
4154
}
4255

4356
#[test]
@@ -158,6 +171,67 @@ mod test_serialize {
158171
));
159172
}
160173

174+
#[test]
175+
fn test_client_custom_request() {
176+
let message: ClientMessage = ClientMessage::Request(ClientJsonrpcRequest::new(
177+
RequestId::Integer(15),
178+
RequestFromClient::CustomRequest(json!({"method":"my_custom_method"})),
179+
));
180+
181+
let message: ClientMessage = re_serialize(message);
182+
183+
assert!(matches!(message, ClientMessage::Request(client_message)
184+
if matches!(&client_message.request, RequestFromClient::CustomRequest(_)) && client_message.method == "my_custom_method"
185+
));
186+
187+
// test From<serde_json::Value> for RequestFromClient
188+
let message: ClientMessage = ClientMessage::Request(ClientJsonrpcRequest::new(
189+
RequestId::Integer(15),
190+
json!({"method":"my_custom_method"}).into(),
191+
));
192+
193+
let message: ClientMessage = re_serialize(message);
194+
195+
assert!(matches!(message, ClientMessage::Request(client_message)
196+
if matches!(&client_message.request, RequestFromClient::CustomRequest(_)) && client_message.method == "my_custom_method"
197+
));
198+
}
199+
200+
/* ---------------------- CLIENT RESPONSES ---------------------- */
201+
#[test]
202+
fn test_list_tools_result() {
203+
let client_result = ClientResult::CreateMessageResult(CreateMessageResult {
204+
content: CreateMessageResultContent::TextContent(TextContent::new(None, "This is a stub response.".to_string())),
205+
meta: None,
206+
model: "stub-model".to_string(),
207+
role: Role::Assistant,
208+
stop_reason: Some("endTurn".to_string()),
209+
});
210+
211+
let message: ClientMessage = ClientMessage::Response(ClientJsonrpcResponse::new(
212+
RequestId::Integer(15),
213+
ResultFromClient::ClientResult(client_result.clone()),
214+
));
215+
216+
let message: ClientMessage = re_serialize(message);
217+
218+
assert!(matches!(message, ClientMessage::Response(client_message)
219+
if matches!(&client_message.result, ResultFromClient::ClientResult(client_result)
220+
if matches!( client_result, ClientResult::CreateMessageResult(_))
221+
)
222+
));
223+
224+
// test From<ClientResult> for ResultFromClient
225+
let message: ClientMessage =
226+
ClientMessage::Response(ClientJsonrpcResponse::new(RequestId::Integer(15), client_result.into()));
227+
228+
assert!(matches!(message, ClientMessage::Response(client_message)
229+
if matches!(&client_message.result, ResultFromClient::ClientResult(client_result)
230+
if matches!( client_result, ClientResult::CreateMessageResult(_))
231+
)
232+
));
233+
}
234+
161235
/* ---------------------- SERVER RESPONSES ---------------------- */
162236

163237
#[test]
@@ -348,6 +422,52 @@ mod test_serialize {
348422
if matches!( client_notification, ClientNotification::CancelledNotification(notification) if notification.params.reason == Some("Request timed out".to_string())))
349423
));
350424
}
425+
426+
#[test]
427+
fn test_client_custom_notification() {
428+
let message: ClientMessage = ClientMessage::Notification(ClientJsonrpcNotification::new(
429+
NotificationFromClient::CustomNotification(json!({"method":"my_notification"})),
430+
));
431+
432+
let message: ClientMessage = re_serialize(message);
433+
434+
// test Display trait
435+
let str = message.to_string();
436+
assert_eq!(str, "{\"jsonrpc\":\"2.0\",\"method\":\"my_notification\",\"params\":{\"method\":\"my_notification\",\"params\":{\"method\":\"my_notification\"}}}");
437+
438+
assert!(matches!(message, ClientMessage::Notification(client_message)
439+
if matches!(&client_message.notification, NotificationFromClient::CustomNotification(_)) && client_message.method == "my_notification"
440+
));
441+
}
442+
443+
/* ---------------------- SERVER NOTIFICATIONS ---------------------- */
444+
#[test]
445+
fn test_server_cancel_notification() {
446+
let cancel_notification = CancelledNotification::new(CancelledNotificationParams {
447+
reason: Some("Request timed out".to_string()),
448+
request_id: RequestId::Integer(15),
449+
});
450+
let message: ServerMessage =
451+
ServerMessage::Notification(ServerJsonrpcNotification::new(NotificationFromServer::ServerNotification(
452+
ServerNotification::CancelledNotification(cancel_notification.clone()),
453+
)));
454+
455+
let message: ServerMessage = re_serialize(message);
456+
457+
assert!(matches!(message, ServerMessage::Notification(client_message)
458+
if matches!(&client_message.notification,NotificationFromServer::ServerNotification(client_notification)
459+
if matches!( client_notification, ServerNotification::CancelledNotification(_)))
460+
));
461+
462+
// test From<CancelledNotification> for NotificationFromServer
463+
let message: ServerMessage = ServerMessage::Notification(ServerJsonrpcNotification::new(cancel_notification.into()));
464+
465+
assert!(matches!(message, ServerMessage::Notification(client_message)
466+
if matches!(&client_message.notification,NotificationFromServer::ServerNotification(client_notification)
467+
if matches!( client_notification, ServerNotification::CancelledNotification(_)))
468+
));
469+
}
470+
351471
/* ---------------------- SERVER REQUESTS ---------------------- */
352472
#[test]
353473
fn test_server_requests() {
@@ -375,6 +495,27 @@ mod test_serialize {
375495
));
376496
}
377497

498+
#[test]
499+
fn test_client_custom_server_request() {
500+
let message: ServerMessage = ServerMessage::Request(ServerJsonrpcRequest::new(
501+
RequestId::Integer(15),
502+
RequestFromServer::CustomRequest(json!({"method":"my_custom_method"})),
503+
));
504+
505+
// test Display trait
506+
let str = message.to_string();
507+
assert_eq!(
508+
str,
509+
"{\"id\":15,\"jsonrpc\":\"2.0\",\"method\":\"my_custom_method\",\"params\":{\"method\":\"my_custom_method\"}}"
510+
);
511+
512+
let message: ServerMessage = re_serialize(message);
513+
514+
assert!(matches!(message, ServerMessage::Request(server_message)
515+
if matches!(&server_message.request, RequestFromServer::CustomRequest(_)) && server_message.method == "my_custom_method"
516+
));
517+
}
518+
378519
/* ---------------------- CLIENT & SERVER ERRORS ---------------------- */
379520

380521
#[test]

0 commit comments

Comments
 (0)