/**
 * @file test_mcp_message.cpp
 * @brief 测试MCP消息相关功能
 * 
 * 本文件包含对MCP消息模块的单元测试，基于规范2024-11-05。
 */

#include "mcp_message.h"
#include <gtest/gtest.h>
#include <string>

// 测试MCP异常
TEST(McpMessageTest, ExceptionTest) {
    // 创建一个MCP异常
    mcp::mcp_exception ex(mcp::error_code::invalid_params, "测试错误消息");
    
    // 验证错误代码和消息
    EXPECT_EQ(ex.code(), mcp::error_code::invalid_params);
    EXPECT_STREQ(ex.what(), "测试错误消息");
}

// 测试请求创建
TEST(McpMessageTest, RequestCreationTest) {
    // 创建一个请求
    mcp::json params = {{"key", "value"}, {"number", 42}};
    mcp::request req = mcp::request::create("test_method", params);
    
    // 验证请求属性
    EXPECT_EQ(req.jsonrpc, "2.0");
    EXPECT_FALSE(req.id.empty());
    EXPECT_EQ(req.method, "test_method");
    EXPECT_EQ(req.params["key"], "value");
    EXPECT_EQ(req.params["number"], 42);
    
    // 验证不是通知
    EXPECT_FALSE(req.is_notification());
}

// 测试通知创建
TEST(McpMessageTest, NotificationCreationTest) {
    // 创建一个通知
    mcp::json params = {{"event", "update"}, {"status", "completed"}};
    mcp::request notification = mcp::request::create_notification("event_notification", params);
    
    // 验证通知属性
    EXPECT_EQ(notification.jsonrpc, "2.0");
    EXPECT_TRUE(notification.id.empty());
    EXPECT_EQ(notification.method, "event_notification");
    EXPECT_EQ(notification.params["event"], "update");
    EXPECT_EQ(notification.params["status"], "completed");
    
    // 验证是通知
    EXPECT_TRUE(notification.is_notification());
}

// 测试请求转换为JSON
TEST(McpMessageTest, RequestToJsonTest) {
    // 创建一个请求
    mcp::json params = {{"param1", "value1"}};
    mcp::request req = mcp::request::create("test_method", params);
    
    // 转换为JSON
    mcp::json json_req = req.to_json();
    
    // 验证JSON结构
    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());
}

// 测试通知转换为JSON
TEST(McpMessageTest, NotificationToJsonTest) {
    // 创建一个通知
    mcp::request notification = mcp::request::create_notification("test_notification");
    
    // 转换为JSON
    mcp::json json_notification = notification.to_json();
    
    // 验证JSON结构
    EXPECT_EQ(json_notification["jsonrpc"], "2.0");
    EXPECT_EQ(json_notification["method"], "test_notification");
    EXPECT_FALSE(json_notification.contains("id"));
}

// 测试成功响应创建
TEST(McpMessageTest, SuccessResponseCreationTest) {
    // 创建一个成功响应
    mcp::json result = {{"result_key", "result_value"}};
    mcp::response res = mcp::response::create_success("request_id_123", result);
    
    // 验证响应属性
    EXPECT_EQ(res.jsonrpc, "2.0");
    EXPECT_EQ(res.id, "request_id_123");
    EXPECT_EQ(res.result["result_key"], "result_value");
    EXPECT_TRUE(res.error.empty());
    
    // 验证不是错误响应
    EXPECT_FALSE(res.is_error());
}

// 测试错误响应创建
TEST(McpMessageTest, ErrorResponseCreationTest) {
    // 创建一个错误响应
    mcp::json error_data = {{"detail", "详细错误信息"}};
    mcp::response res = mcp::response::create_error(
        "request_id_456", 
        mcp::error_code::method_not_found, 
        "方法未找到", 
        error_data
    );
    
    // 验证响应属性
    EXPECT_EQ(res.jsonrpc, "2.0");
    EXPECT_EQ(res.id, "request_id_456");
    EXPECT_TRUE(res.result.empty());
    EXPECT_EQ(res.error["code"], static_cast<int>(mcp::error_code::method_not_found));
    EXPECT_EQ(res.error["message"], "方法未找到");
    EXPECT_EQ(res.error["data"]["detail"], "详细错误信息");
    
    // 验证是错误响应
    EXPECT_TRUE(res.is_error());
}

// 测试响应转换为JSON
TEST(McpMessageTest, ResponseToJsonTest) {
    // 创建一个成功响应
    mcp::json result = {{"success", true}};
    mcp::response res = mcp::response::create_success("request_id_789", result);
    
    // 转换为JSON
    mcp::json json_res = res.to_json();
    
    // 验证JSON结构
    EXPECT_EQ(json_res["jsonrpc"], "2.0");
    EXPECT_EQ(json_res["id"], "request_id_789");
    EXPECT_EQ(json_res["result"]["success"], true);
    EXPECT_FALSE(json_res.contains("error"));
    
    // 创建一个错误响应
    mcp::response error_res = mcp::response::create_error(
        "request_id_999", 
        mcp::error_code::invalid_params, 
        "无效参数"
    );
    
    // 转换为JSON
    mcp::json json_error = error_res.to_json();
    
    // 验证JSON结构
    EXPECT_EQ(json_error["jsonrpc"], "2.0");
    EXPECT_EQ(json_error["id"], "request_id_999");
    EXPECT_EQ(json_error["error"]["code"], static_cast<int>(mcp::error_code::invalid_params));
    EXPECT_EQ(json_error["error"]["message"], "无效参数");
    EXPECT_FALSE(json_error.contains("result"));
} 