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        execution: None,
58        annotations: None,
59        #[cfg(feature = "mcp-icons")]
60        icons: None,
61        meta: None,
62    }
63}
64
65/// Create a valid prompt for testing
66pub fn test_prompt() -> Prompt {
67    Prompt {
68        name: "test_prompt".to_string(),
69        title: Some("Test Prompt".to_string()),
70        description: Some("A test prompt".to_string()),
71        arguments: None,
72        meta: None,
73    }
74}
75
76/// Create a prompt argument for testing
77pub fn test_prompt_argument(name: &str) -> PromptArgument {
78    PromptArgument {
79        name: name.to_string(),
80        title: Some(format!("Argument {name}")),
81        description: Some(format!("Description for {name}")),
82        required: Some(true),
83    }
84}
85
86/// Create a valid resource for testing
87pub fn test_resource() -> Resource {
88    Resource {
89        name: "test_resource".to_string(),
90        title: Some("Test Resource".to_string()),
91        uri: "file://test/resource.txt".to_string(),
92        description: Some("A test resource".to_string()),
93        mime_type: Some("text/plain".to_string()),
94        annotations: None,
95        size: Some(1024),
96        meta: None,
97    }
98}
99
100/// Create a valid initialize request for testing
101pub fn test_initialize_request() -> InitializeRequest {
102    InitializeRequest {
103        protocol_version: "2025-06-18".to_string(),
104        capabilities: ClientCapabilities::default(),
105        client_info: Implementation {
106            name: "test-client".to_string(),
107            title: Some("Test Client".to_string()),
108            version: "1.0.0".to_string(),
109            #[cfg(feature = "mcp-draft")]
110            description: None,
111            #[cfg(feature = "mcp-icons")]
112            icons: None,
113        },
114        _meta: None,
115    }
116}
117
118// ========== Validation Assertions ==========
119
120/// Assert that validation passed without warnings
121#[allow(dead_code)]
122pub fn assert_valid(result: &ValidationResult) {
123    assert!(
124        result.is_valid(),
125        "Expected validation to pass, but got errors: {:?}",
126        result.errors()
127    );
128}
129
130/// Assert that validation failed
131#[allow(dead_code)]
132pub fn assert_invalid(result: &ValidationResult) {
133    assert!(
134        result.is_invalid(),
135        "Expected validation to fail, but it passed"
136    );
137}