cpp-mcp/test/test_mcp_message.cpp

279 lines
9.2 KiB
C++
Raw Normal View History

2025-03-08 23:44:34 +08:00
/**
* @file test_mcp_message.cpp
2025-03-09 15:45:09 +08:00
* @brief Test MCP message related functionality
2025-03-08 23:44:34 +08:00
*
2025-03-09 15:45:09 +08:00
* This file contains unit tests for the MCP message module, based on specification 2024-11-05.
2025-03-08 23:44:34 +08:00
*/
#include "mcp_message.h"
#include <gtest/gtest.h>
#include <string>
2025-03-09 15:45:09 +08:00
// Test MCP exception
2025-03-08 23:44:34 +08:00
TEST(McpMessageTest, ExceptionTest) {
2025-03-09 15:45:09 +08:00
// Create an MCP exception
mcp::mcp_exception ex(mcp::error_code::invalid_params, "Test error message");
2025-03-08 23:44:34 +08:00
2025-03-09 15:45:09 +08:00
// Verify error code and message
2025-03-08 23:44:34 +08:00
EXPECT_EQ(ex.code(), mcp::error_code::invalid_params);
2025-03-09 15:45:09 +08:00
EXPECT_STREQ(ex.what(), "Test error message");
2025-03-08 23:44:34 +08:00
}
2025-03-09 15:45:09 +08:00
// Test request creation
2025-03-08 23:44:34 +08:00
TEST(McpMessageTest, RequestCreationTest) {
2025-03-09 15:45:09 +08:00
// Create a request
2025-03-08 23:44:34 +08:00
mcp::json params = {{"key", "value"}, {"number", 42}};
mcp::request req = mcp::request::create("test_method", params);
2025-03-09 15:45:09 +08:00
// Verify request properties
2025-03-08 23:44:34 +08:00
EXPECT_EQ(req.jsonrpc, "2.0");
2025-03-09 15:45:09 +08:00
EXPECT_FALSE(req.id.is_null());
EXPECT_TRUE(req.id.is_number());
2025-03-08 23:44:34 +08:00
EXPECT_EQ(req.method, "test_method");
EXPECT_EQ(req.params["key"], "value");
EXPECT_EQ(req.params["number"], 42);
2025-03-09 15:45:09 +08:00
// Verify it's not a notification
2025-03-08 23:44:34 +08:00
EXPECT_FALSE(req.is_notification());
}
2025-03-09 15:45:09 +08:00
// Test notification creation
2025-03-08 23:44:34 +08:00
TEST(McpMessageTest, NotificationCreationTest) {
2025-03-09 15:45:09 +08:00
// Create a notification
2025-03-08 23:44:34 +08:00
mcp::json params = {{"event", "update"}, {"status", "completed"}};
mcp::request notification = mcp::request::create_notification("event_notification", params);
2025-03-09 15:45:09 +08:00
// Verify notification properties
2025-03-08 23:44:34 +08:00
EXPECT_EQ(notification.jsonrpc, "2.0");
2025-03-09 15:45:09 +08:00
EXPECT_TRUE(notification.id.is_null());
2025-03-08 23:44:34 +08:00
EXPECT_EQ(notification.method, "event_notification");
EXPECT_EQ(notification.params["event"], "update");
EXPECT_EQ(notification.params["status"], "completed");
2025-03-09 15:45:09 +08:00
// Verify it is a notification
2025-03-08 23:44:34 +08:00
EXPECT_TRUE(notification.is_notification());
}
2025-03-09 15:45:09 +08:00
// Test request conversion to JSON
2025-03-08 23:44:34 +08:00
TEST(McpMessageTest, RequestToJsonTest) {
2025-03-09 15:45:09 +08:00
// Create a request
2025-03-08 23:44:34 +08:00
mcp::json params = {{"param1", "value1"}};
mcp::request req = mcp::request::create("test_method", params);
2025-03-09 15:45:09 +08:00
// Convert to JSON
2025-03-08 23:44:34 +08:00
mcp::json json_req = req.to_json();
2025-03-09 15:45:09 +08:00
// Verify JSON structure
2025-03-08 23:44:34 +08:00
EXPECT_EQ(json_req["jsonrpc"], "2.0");
EXPECT_EQ(json_req["method"], "test_method");
EXPECT_EQ(json_req["params"]["param1"], "value1");
EXPECT_FALSE(json_req["id"].empty());
}
2025-03-09 15:45:09 +08:00
// Test notification conversion to JSON
2025-03-08 23:44:34 +08:00
TEST(McpMessageTest, NotificationToJsonTest) {
2025-03-09 15:45:09 +08:00
// Create a notification
2025-03-08 23:44:34 +08:00
mcp::request notification = mcp::request::create_notification("test_notification");
2025-03-09 15:45:09 +08:00
// Convert to JSON
2025-03-08 23:44:34 +08:00
mcp::json json_notification = notification.to_json();
2025-03-09 15:45:09 +08:00
// Verify JSON structure
2025-03-08 23:44:34 +08:00
EXPECT_EQ(json_notification["jsonrpc"], "2.0");
EXPECT_EQ(json_notification["method"], "test_notification");
EXPECT_FALSE(json_notification.contains("id"));
}
2025-03-09 15:45:09 +08:00
// Test success response creation
2025-03-08 23:44:34 +08:00
TEST(McpMessageTest, SuccessResponseCreationTest) {
2025-03-09 15:45:09 +08:00
// Create a success response
2025-03-08 23:44:34 +08:00
mcp::json result = {{"result_key", "result_value"}};
2025-03-09 15:45:09 +08:00
std::string req_id = "request_id_123";
mcp::response res = mcp::response::create_success(req_id, result);
2025-03-08 23:44:34 +08:00
2025-03-09 15:45:09 +08:00
// Verify response properties
2025-03-08 23:44:34 +08:00
EXPECT_EQ(res.jsonrpc, "2.0");
2025-03-09 15:45:09 +08:00
EXPECT_EQ(res.id, req_id);
2025-03-08 23:44:34 +08:00
EXPECT_EQ(res.result["result_key"], "result_value");
EXPECT_TRUE(res.error.empty());
2025-03-09 15:45:09 +08:00
// Verify it's not an error response
2025-03-08 23:44:34 +08:00
EXPECT_FALSE(res.is_error());
}
2025-03-09 15:45:09 +08:00
// Test error response creation
2025-03-08 23:44:34 +08:00
TEST(McpMessageTest, ErrorResponseCreationTest) {
2025-03-09 15:45:09 +08:00
// Create an error response
mcp::json error_data = {{"detail", "Detailed error information"}};
std::string req_id = "request_id_456";
2025-03-08 23:44:34 +08:00
mcp::response res = mcp::response::create_error(
2025-03-09 15:45:09 +08:00
req_id,
2025-03-08 23:44:34 +08:00
mcp::error_code::method_not_found,
2025-03-09 15:45:09 +08:00
"Method not found",
2025-03-08 23:44:34 +08:00
error_data
);
2025-03-09 15:45:09 +08:00
// Verify response properties
2025-03-08 23:44:34 +08:00
EXPECT_EQ(res.jsonrpc, "2.0");
2025-03-09 15:45:09 +08:00
EXPECT_EQ(res.id, req_id);
2025-03-08 23:44:34 +08:00
EXPECT_TRUE(res.result.empty());
EXPECT_EQ(res.error["code"], static_cast<int>(mcp::error_code::method_not_found));
2025-03-09 15:45:09 +08:00
EXPECT_EQ(res.error["message"], "Method not found");
EXPECT_EQ(res.error["data"]["detail"], "Detailed error information");
2025-03-08 23:44:34 +08:00
2025-03-09 15:45:09 +08:00
// Verify it is an error response
2025-03-08 23:44:34 +08:00
EXPECT_TRUE(res.is_error());
}
2025-03-09 15:45:09 +08:00
// Test response conversion to JSON
2025-03-08 23:44:34 +08:00
TEST(McpMessageTest, ResponseToJsonTest) {
2025-03-09 15:45:09 +08:00
// Create a success response
2025-03-08 23:44:34 +08:00
mcp::json result = {{"success", true}};
2025-03-09 15:45:09 +08:00
std::string req_id = "request_id_789";
mcp::response res = mcp::response::create_success(req_id, result);
2025-03-08 23:44:34 +08:00
2025-03-09 15:45:09 +08:00
// Convert to JSON
2025-03-08 23:44:34 +08:00
mcp::json json_res = res.to_json();
2025-03-09 15:45:09 +08:00
// Verify JSON structure
2025-03-08 23:44:34 +08:00
EXPECT_EQ(json_res["jsonrpc"], "2.0");
2025-03-09 15:45:09 +08:00
EXPECT_EQ(json_res["id"], req_id);
2025-03-08 23:44:34 +08:00
EXPECT_EQ(json_res["result"]["success"], true);
EXPECT_FALSE(json_res.contains("error"));
2025-03-09 15:45:09 +08:00
// Create an error response
std::string error_req_id = "request_id_999";
2025-03-08 23:44:34 +08:00
mcp::response error_res = mcp::response::create_error(
2025-03-09 15:45:09 +08:00
error_req_id,
2025-03-08 23:44:34 +08:00
mcp::error_code::invalid_params,
2025-03-09 15:45:09 +08:00
"Invalid parameters"
2025-03-08 23:44:34 +08:00
);
2025-03-09 15:45:09 +08:00
// Convert to JSON
2025-03-08 23:44:34 +08:00
mcp::json json_error = error_res.to_json();
2025-03-09 15:45:09 +08:00
// Verify JSON structure
2025-03-08 23:44:34 +08:00
EXPECT_EQ(json_error["jsonrpc"], "2.0");
2025-03-09 15:45:09 +08:00
EXPECT_EQ(json_error["id"], error_req_id);
2025-03-08 23:44:34 +08:00
EXPECT_EQ(json_error["error"]["code"], static_cast<int>(mcp::error_code::invalid_params));
2025-03-09 15:45:09 +08:00
EXPECT_EQ(json_error["error"]["message"], "Invalid parameters");
2025-03-08 23:44:34 +08:00
EXPECT_FALSE(json_error.contains("result"));
2025-03-09 15:45:09 +08:00
}
// Test request creation with numeric ID
TEST(McpMessageTest, RequestWithNumericIdTest) {
// Create request with numeric ID
int numeric_id = 42;
mcp::json params = {{"key", "value"}};
mcp::request req = mcp::request::create_with_id(numeric_id, "test_method", params);
// Verify request properties
EXPECT_EQ(req.jsonrpc, "2.0");
EXPECT_EQ(req.id, numeric_id);
EXPECT_EQ(req.method, "test_method");
EXPECT_EQ(req.params["key"], "value");
// Verify it's not a notification
EXPECT_FALSE(req.is_notification());
// Convert to JSON and verify
mcp::json json_req = req.to_json();
EXPECT_EQ(json_req["id"], numeric_id);
}
// Test request creation with string ID
TEST(McpMessageTest, RequestWithStringIdTest) {
// Create request with string ID
std::string string_id = "custom-id-123";
mcp::json params = {{"key", "value"}};
mcp::request req = mcp::request::create_with_id(string_id, "test_method", params);
// Verify request properties
EXPECT_EQ(req.jsonrpc, "2.0");
EXPECT_EQ(req.id, string_id);
EXPECT_EQ(req.method, "test_method");
EXPECT_EQ(req.params["key"], "value");
// Verify it's not a notification
EXPECT_FALSE(req.is_notification());
// Convert to JSON and verify
mcp::json json_req = req.to_json();
EXPECT_EQ(json_req["id"], string_id);
}
// Test response creation with numeric ID
TEST(McpMessageTest, ResponseWithNumericIdTest) {
// Create success response with numeric ID
int numeric_id = 123;
mcp::json result = {{"result_key", "result_value"}};
mcp::response res = mcp::response::create_success(numeric_id, result);
// Verify response properties
EXPECT_EQ(res.jsonrpc, "2.0");
EXPECT_EQ(res.id, numeric_id);
EXPECT_EQ(res.result["result_key"], "result_value");
EXPECT_TRUE(res.error.empty());
// Verify it's not an error response
EXPECT_FALSE(res.is_error());
// Convert to JSON and verify
mcp::json json_res = res.to_json();
EXPECT_EQ(json_res["id"], numeric_id);
// Create error response with numeric ID
mcp::response error_res = mcp::response::create_error(
numeric_id,
mcp::error_code::invalid_params,
"Invalid parameters"
);
// Verify error response properties
EXPECT_EQ(error_res.jsonrpc, "2.0");
EXPECT_EQ(error_res.id, numeric_id);
EXPECT_TRUE(error_res.result.empty());
EXPECT_EQ(error_res.error["code"], static_cast<int>(mcp::error_code::invalid_params));
// Convert to JSON and verify
mcp::json json_error = error_res.to_json();
EXPECT_EQ(json_error["id"], numeric_id);
}
// Test response creation with string ID
TEST(McpMessageTest, ResponseWithStringIdTest) {
// Create success response with string ID
std::string string_id = "response-id-456";
mcp::json result = {{"result_key", "result_value"}};
mcp::response res = mcp::response::create_success(string_id, result);
// Verify response properties
EXPECT_EQ(res.jsonrpc, "2.0");
EXPECT_EQ(res.id, string_id);
EXPECT_EQ(res.result["result_key"], "result_value");
EXPECT_TRUE(res.error.empty());
// Verify it's not an error response
EXPECT_FALSE(res.is_error());
// Convert to JSON and verify
mcp::json json_res = res.to_json();
EXPECT_EQ(json_res["id"], string_id);
// Create error response with string ID
mcp::response error_res = mcp::response::create_error(
string_id,
mcp::error_code::invalid_params,
"Invalid parameters"
);
// Verify error response properties
EXPECT_EQ(error_res.jsonrpc, "2.0");
EXPECT_EQ(error_res.id, string_id);
EXPECT_TRUE(error_res.result.empty());
EXPECT_EQ(error_res.error["code"], static_cast<int>(mcp::error_code::invalid_params));
// Convert to JSON and verify
mcp::json json_error = error_res.to_json();
EXPECT_EQ(json_error["id"], string_id);
2025-03-08 23:44:34 +08:00
}