turbomcp_protocol/
test_helpers.rs

1//! Test utilities for turbomcp-protocol
2//!
3//! This module provides shared test helpers used throughout the crate's tests.
4//! Following the pattern from axum and tokio, these utilities are public to
5//! allow downstream crates to use them in their tests.
6//!
7//! ## Organization
8//!
9//! All test fixtures and helpers are in this single module for simplicity.
10//! As the test suite grows, this can be split into submodules if needed.
11//!
12//! ## Usage
13//!
14//! ```rust
15//! #[cfg(test)]
16//! mod tests {
17//!     use super::*;
18//!     use crate::test_helpers::*;
19//!
20//!     #[test]
21//!     fn my_test() {
22//!         let request = test_request();
23//!         assert_valid(&result);
24//!     }
25//! }
26//! ```
27
28use crate::jsonrpc::*;
29use crate::types::*;
30use crate::validation::*;
31
32// ========== JSON-RPC Request Fixtures ==========
33
34/// Create a standard test JSON-RPC request
35pub fn test_request() -> JsonRpcRequest {
36    JsonRpcRequest {
37        jsonrpc: JsonRpcVersion,
38        method: "tools/list".to_string(),
39        params: None,
40        id: RequestId::String("test-123".to_string()),
41    }
42}
43
44/// Create a valid tool for testing
45pub fn test_tool() -> Tool {
46    Tool {
47        name: "test_tool".to_string(),
48        title: Some("Test Tool".to_string()),
49        description: Some("A test tool for validation".to_string()),
50        input_schema: ToolInputSchema {
51            schema_type: "object".to_string(),
52            properties: None,
53            required: None,
54            additional_properties: None,
55        },
56        output_schema: None,
57        annotations: None,
58        #[cfg(feature = "mcp-icons")]
59        icons: None,
60        meta: None,
61    }
62}
63
64/// Create a valid prompt for testing
65pub fn test_prompt() -> Prompt {
66    Prompt {
67        name: "test_prompt".to_string(),
68        title: Some("Test Prompt".to_string()),
69        description: Some("A test prompt".to_string()),
70        arguments: None,
71        meta: None,
72    }
73}
74
75/// Create a prompt argument for testing
76pub fn test_prompt_argument(name: &str) -> PromptArgument {
77    PromptArgument {
78        name: name.to_string(),
79        title: Some(format!("Argument {name}")),
80        description: Some(format!("Description for {name}")),
81        required: Some(true),
82    }
83}
84
85/// Create a valid resource for testing
86pub fn test_resource() -> Resource {
87    Resource {
88        name: "test_resource".to_string(),
89        title: Some("Test Resource".to_string()),
90        uri: "file://test/resource.txt".to_string(),
91        description: Some("A test resource".to_string()),
92        mime_type: Some("text/plain".to_string()),
93        annotations: None,
94        size: Some(1024),
95        meta: None,
96    }
97}
98
99/// Create a valid initialize request for testing
100pub fn test_initialize_request() -> InitializeRequest {
101    InitializeRequest {
102        protocol_version: "2025-06-18".to_string(),
103        capabilities: ClientCapabilities::default(),
104        client_info: Implementation {
105            name: "test-client".to_string(),
106            title: Some("Test Client".to_string()),
107            version: "1.0.0".to_string(),
108            #[cfg(feature = "mcp-draft")]
109            description: None,
110            #[cfg(feature = "mcp-icons")]
111            icons: None,
112        },
113        _meta: None,
114    }
115}
116
117// ========== Validation Assertions ==========
118
119/// Assert that validation passed without warnings
120#[allow(dead_code)]
121pub fn assert_valid(result: &ValidationResult) {
122    assert!(
123        result.is_valid(),
124        "Expected validation to pass, but got errors: {:?}",
125        result.errors()
126    );
127}
128
129/// Assert that validation failed
130#[allow(dead_code)]
131pub fn assert_invalid(result: &ValidationResult) {
132    assert!(
133        result.is_invalid(),
134        "Expected validation to fail, but it passed"
135    );
136}