Skip to content

Commit 6588978

Browse files
Brandon BennettBrandon Bennett
authored andcommitted
test(rmcp): add missing edge case tests from code review
Add tests identified during code review: - description stripping for primitive types - composition types (Option<String> with anyOf/oneOf/null) - cache correctness (Arc::ptr_eq for repeated calls) - schema_for_input rejecting array types (not just primitives) - schema_for_output accepting unit type ()
1 parent 025d3c6 commit 6588978

4 files changed

Lines changed: 45 additions & 9 deletions

File tree

crates/rmcp/src/handler/server/common.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,42 @@ mod tests {
319319
assert_eq!(schema.get("type"), Some(&serde_json::json!("integer")));
320320
}
321321

322+
#[test]
323+
fn test_schema_for_output_strips_description_for_primitive() {
324+
let schema = schema_for_output::<i32>();
325+
assert!(!schema.contains_key("description"));
326+
}
327+
328+
#[test]
329+
fn test_schema_for_output_accepts_composition() {
330+
let schema = schema_for_output::<Option<String>>();
331+
let schema_str = serde_json::to_string(&schema).unwrap();
332+
assert!(
333+
schema_str.contains("anyOf")
334+
|| schema_str.contains("oneOf")
335+
|| schema_str.contains("null"),
336+
"Expected composition schema for Option<String>, got: {schema_str}"
337+
);
338+
}
339+
340+
#[test]
341+
fn test_schema_for_output_caches_result() {
342+
let schema1 = schema_for_output::<i32>();
343+
let schema2 = schema_for_output::<i32>();
344+
assert!(Arc::ptr_eq(&schema1, &schema2));
345+
}
346+
347+
#[test]
348+
fn test_schema_for_input_rejects_array() {
349+
let result = schema_for_input::<Vec<i32>>();
350+
assert!(result.is_err());
351+
}
352+
353+
#[test]
354+
fn test_schema_for_output_accepts_unit() {
355+
let _schema = schema_for_output::<()>();
356+
}
357+
322358
#[test]
323359
fn test_schema_for_output_accepts_object() {
324360
let schema = schema_for_output::<TestObject>();

crates/rmcp/tests/test_json_schema_detection.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -174,10 +174,7 @@ async fn test_json_string_type_generates_schema() {
174174
let server = TestServer::new();
175175
let tools = server.tool_router.list_all();
176176

177-
let string_tool = tools
178-
.iter()
179-
.find(|t| t.name == "with-json-string")
180-
.unwrap();
177+
let string_tool = tools.iter().find(|t| t.name == "with-json-string").unwrap();
181178
assert!(
182179
string_tool.output_schema.is_some(),
183180
"Json<String> return type should generate output schema"

crates/rmcp/tests/test_structured_output.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,10 @@ impl TestServer {
9595
}
9696

9797
/// Tool that returns a list of calculation results
98-
#[tool(name = "calculate-list", description = "Return a list of calculation results")]
98+
#[tool(
99+
name = "calculate-list",
100+
description = "Return a list of calculation results"
101+
)]
99102
pub async fn calculate_list(
100103
&self,
101104
params: Parameters<CalculationRequest>,

crates/rmcp/tests/test_tool_builder_methods.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,8 @@ fn test_with_output_schema_primitive() {
7676

7777
#[test]
7878
fn test_with_output_schema_array() {
79-
let tool = Tool::new("test", "Test tool", JsonObject::new())
80-
.with_output_schema::<Vec<String>>();
79+
let tool =
80+
Tool::new("test", "Test tool", JsonObject::new()).with_output_schema::<Vec<String>>();
8181

8282
assert!(tool.output_schema.is_some());
8383

@@ -90,8 +90,8 @@ fn test_with_output_schema_array() {
9090

9191
#[test]
9292
fn test_with_output_schema_option() {
93-
let tool = Tool::new("test", "Test tool", JsonObject::new())
94-
.with_output_schema::<Option<String>>();
93+
let tool =
94+
Tool::new("test", "Test tool", JsonObject::new()).with_output_schema::<Option<String>>();
9595

9696
assert!(tool.output_schema.is_some());
9797

0 commit comments

Comments
 (0)