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