Skip to content

Commit 415f03e

Browse files
committed
test: add handler transport and tool context tests
1 parent 01670df commit 415f03e

1 file changed

Lines changed: 145 additions & 0 deletions

File tree

mcp-server/src/handler.rs

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1420,4 +1420,149 @@ mod tests {
14201420
let error = HandlerError::Backend("Test backend error".to_string());
14211421
assert_eq!(error.to_string(), "Backend error: Test backend error");
14221422
}
1423+
1424+
// ============================================================================
1425+
// Transport and ToolContext Tests
1426+
// ============================================================================
1427+
1428+
/// Mock transport for testing set_transport and make_tool_context
1429+
struct MockTestTransport {
1430+
supports_bidir: bool,
1431+
}
1432+
1433+
#[async_trait::async_trait]
1434+
impl pulseengine_mcp_transport::Transport for MockTestTransport {
1435+
async fn start(
1436+
&mut self,
1437+
_handler: pulseengine_mcp_transport::RequestHandler,
1438+
) -> std::result::Result<(), pulseengine_mcp_transport::TransportError> {
1439+
Ok(())
1440+
}
1441+
1442+
async fn stop(
1443+
&mut self,
1444+
) -> std::result::Result<(), pulseengine_mcp_transport::TransportError> {
1445+
Ok(())
1446+
}
1447+
1448+
async fn health_check(
1449+
&self,
1450+
) -> std::result::Result<(), pulseengine_mcp_transport::TransportError> {
1451+
Ok(())
1452+
}
1453+
1454+
fn supports_bidirectional(&self) -> bool {
1455+
self.supports_bidir
1456+
}
1457+
1458+
async fn send_notification(
1459+
&self,
1460+
_session_id: Option<&str>,
1461+
_method: &str,
1462+
_params: serde_json::Value,
1463+
) -> std::result::Result<(), pulseengine_mcp_transport::TransportError> {
1464+
Ok(())
1465+
}
1466+
1467+
async fn send_request(
1468+
&self,
1469+
_session_id: Option<&str>,
1470+
_method: &str,
1471+
_params: serde_json::Value,
1472+
_timeout: std::time::Duration,
1473+
) -> std::result::Result<serde_json::Value, pulseengine_mcp_transport::TransportError>
1474+
{
1475+
Ok(serde_json::json!({}))
1476+
}
1477+
}
1478+
1479+
#[tokio::test]
1480+
async fn test_set_transport() {
1481+
let handler = create_test_handler().await;
1482+
1483+
// Initially no transport
1484+
{
1485+
let guard = handler.transport.read().await;
1486+
assert!(guard.is_none());
1487+
}
1488+
1489+
// Set transport
1490+
let transport = Arc::new(MockTestTransport {
1491+
supports_bidir: true,
1492+
}) as Arc<dyn pulseengine_mcp_transport::Transport>;
1493+
handler.set_transport(transport);
1494+
1495+
// Now transport should be set
1496+
{
1497+
let guard = handler.transport.read().await;
1498+
assert!(guard.is_some());
1499+
}
1500+
}
1501+
1502+
#[tokio::test]
1503+
async fn test_make_tool_context_no_transport() {
1504+
let handler = create_test_handler().await;
1505+
1506+
// Without transport, should return NoOp context
1507+
let ctx = handler
1508+
.make_tool_context(
1509+
"req-1".to_string(),
1510+
"test-tool".to_string(),
1511+
None,
1512+
Some("session-1".to_string()),
1513+
)
1514+
.await;
1515+
1516+
assert_eq!(ctx.request_id(), "req-1");
1517+
assert_eq!(ctx.tool_name(), "test-tool");
1518+
}
1519+
1520+
#[tokio::test]
1521+
async fn test_make_tool_context_with_bidirectional_transport() {
1522+
let handler = create_test_handler().await;
1523+
1524+
// Set bidirectional transport
1525+
let transport = Arc::new(MockTestTransport {
1526+
supports_bidir: true,
1527+
}) as Arc<dyn pulseengine_mcp_transport::Transport>;
1528+
handler.set_transport(transport);
1529+
1530+
let ctx = handler
1531+
.make_tool_context(
1532+
"req-2".to_string(),
1533+
"bidir-tool".to_string(),
1534+
Some("progress-token".to_string()),
1535+
Some("session-2".to_string()),
1536+
)
1537+
.await;
1538+
1539+
assert_eq!(ctx.request_id(), "req-2");
1540+
assert_eq!(ctx.tool_name(), "bidir-tool");
1541+
assert_eq!(ctx.progress_token(), Some("progress-token"));
1542+
assert_eq!(ctx.session_id(), Some("session-2"));
1543+
}
1544+
1545+
#[tokio::test]
1546+
async fn test_make_tool_context_with_non_bidirectional_transport() {
1547+
let handler = create_test_handler().await;
1548+
1549+
// Set non-bidirectional transport
1550+
let transport = Arc::new(MockTestTransport {
1551+
supports_bidir: false,
1552+
}) as Arc<dyn pulseengine_mcp_transport::Transport>;
1553+
handler.set_transport(transport);
1554+
1555+
// Should return NoOp context since transport doesn't support bidirectional
1556+
let ctx = handler
1557+
.make_tool_context(
1558+
"req-3".to_string(),
1559+
"non-bidir-tool".to_string(),
1560+
None,
1561+
None,
1562+
)
1563+
.await;
1564+
1565+
assert_eq!(ctx.request_id(), "req-3");
1566+
assert_eq!(ctx.tool_name(), "non-bidir-tool");
1567+
}
14231568
}

0 commit comments

Comments
 (0)