fastmcp_rust/testing/mod.rs
1//! Test harness infrastructure for FastMCP.
2//!
3//! This module provides utilities for writing comprehensive tests without mocks:
4//!
5//! - [`TestContext`]: Wrapper around `Cx::for_testing()` with helper methods
6//! - [`TestServer`]: Builder for creating test servers with real handlers
7//! - [`TestClient`]: Client for in-process testing with MemoryTransport
8//! - Assertion helpers for validating JSON-RPC and MCP compliance
9//! - Timing utilities for performance measurements
10//! - [`fixtures`]: Test data generators for tools, resources, prompts, and messages
11//!
12//! # Example
13//!
14//! ```ignore
15//! use fastmcp_rust::testing::prelude::*;
16//!
17//! #[test]
18//! fn test_tool_call() {
19//! let ctx = TestContext::new();
20//!
21//! // Create a test server with real handlers
22//! let (router, client_transport, server_transport) = TestServer::builder()
23//! .build();
24//!
25//! // Create a test client
26//! let mut client = TestClient::new(client_transport);
27//! client.initialize().unwrap();
28//!
29//! // Call tool and verify
30//! let result = client.call_tool("my_tool", json!({"arg": "value"}));
31//! assert!(result.is_ok());
32//! }
33//! ```
34//!
35//! # Design Philosophy
36//!
37//! - **No mocks**: All tests use real implementations via MemoryTransport
38//! - **Deterministic**: Uses asupersync Lab runtime for reproducibility
39//! - **Comprehensive logging**: Built-in trace support for debugging
40//! - **Resource cleanup**: Automatic cleanup of spawned tasks
41
42mod assertions;
43mod client;
44mod context;
45pub mod fixtures;
46mod server;
47mod timing;
48mod trace;
49
50pub use assertions::*;
51pub use client::*;
52pub use context::*;
53pub use server::*;
54pub use timing::*;
55pub use trace::*;
56
57/// Prelude for convenient imports in tests.
58///
59/// ```ignore
60/// use fastmcp_rust::testing::prelude::*;
61/// ```
62pub mod prelude {
63 pub use super::{
64 // Timing
65 Stopwatch,
66 // Client
67 TestClient,
68 // Context
69 TestContext,
70 // Server
71 TestServer,
72 TestServerBuilder,
73 // Trace
74 TestTrace,
75 TestTraceBuilder,
76 TestTraceOutput,
77 Timer,
78 TimingStats,
79 TraceEntry,
80 TraceLevel,
81 TraceSummary,
82 // Assertions
83 assert_content_valid,
84 assert_is_notification,
85 assert_is_request,
86 assert_json_rpc_error,
87 assert_json_rpc_success,
88 assert_json_rpc_valid,
89 assert_mcp_compliant,
90 assert_prompt_valid,
91 assert_resource_valid,
92 assert_tool_valid,
93 is_trace_enabled,
94 measure_duration,
95 };
96
97 // Re-export commonly used types
98 pub use crate::{
99 Content, Cx, JsonRpcMessage, JsonRpcRequest, JsonRpcResponse, McpContext, McpError,
100 McpErrorCode, McpResult, Prompt, Resource, Tool,
101 };
102
103 pub use serde_json::json;
104}