yams/tests/unit/mcp/websocket_transport_catch2_test.cpp
2025-12-13 16:27:07 -05:00

125 lines
4.4 KiB
C++

// MCP WebSocket Transport tests
// Catch2 migration from GTest (yams-3s4 / yams-84g)
// Note: Most tests disabled in original due to WebSocketTransport implementation status
#include <catch2/catch_test_macros.hpp>
#include <nlohmann/json.hpp>
#include <atomic>
#include <chrono>
#include <thread>
#include <yams/mcp/error_handling.h>
#include <yams/mcp/mcp_server.h>
using namespace yams::mcp;
using json = nlohmann::json;
// WebSocket transport tests are disabled in the original GTest file
// because WebSocketTransport may not be fully implemented.
// These tests are kept as placeholders for when it becomes available.
#if 0 // Disabled: WebSocketTransport not fully implemented
namespace {
class WebSocketTransportFixture {
public:
WebSocketTransport::Config config;
WebSocketTransportFixture() {
// Default configuration for testing
config.host = "localhost";
config.port = 8080;
config.path = "/mcp";
config.useSSL = false;
config.connectTimeout = std::chrono::milliseconds{1000};
config.receiveTimeout = std::chrono::milliseconds{5000};
config.maxMessageSize = 1024 * 1024;
config.enablePingPong = true;
config.pingInterval = std::chrono::seconds{30};
}
};
} // namespace
TEST_CASE_METHOD(WebSocketTransportFixture, "WebSocketTransport - Configuration",
"[mcp][transport][websocket][catch2]") {
WebSocketTransport transport(config);
// Test that transport can be created with valid configuration
CHECK_FALSE(transport.isConnected());
// Configuration should be preserved
REQUIRE_NOTHROW(transport.close());
}
TEST_CASE_METHOD(WebSocketTransportFixture, "WebSocketTransport - Configuration validation",
"[mcp][transport][websocket][catch2]") {
// Valid configuration
WebSocketTransport::Config validConfig = config;
REQUIRE_NOTHROW(WebSocketTransport transport(validConfig));
// Test with SSL enabled
WebSocketTransport::Config sslConfig = config;
sslConfig.useSSL = true;
sslConfig.port = 443;
REQUIRE_NOTHROW(WebSocketTransport transport(sslConfig));
// Test with custom timeouts
WebSocketTransport::Config timeoutConfig = config;
timeoutConfig.connectTimeout = std::chrono::milliseconds{10000};
timeoutConfig.receiveTimeout = std::chrono::milliseconds{30000};
REQUIRE_NOTHROW(WebSocketTransport transport(timeoutConfig));
}
TEST_CASE_METHOD(WebSocketTransportFixture, "WebSocketTransport - Connection failure",
"[mcp][transport][websocket][catch2]") {
// Test connection to non-existent server
WebSocketTransport transport(config);
// Should not be connected initially
CHECK_FALSE(transport.isConnected());
// Connection should fail (no server running on localhost:8080)
bool connected = transport.connect();
CHECK_FALSE(connected);
CHECK_FALSE(transport.isConnected());
}
TEST_CASE_METHOD(WebSocketTransportFixture, "WebSocketTransport - Connection timeout",
"[mcp][transport][websocket][catch2]") {
// Test connection timeout with very short timeout
WebSocketTransport::Config timeoutConfig = config;
timeoutConfig.connectTimeout = std::chrono::milliseconds{100}; // Very short timeout
WebSocketTransport transport(timeoutConfig);
auto start = std::chrono::steady_clock::now();
bool connected = transport.connect();
auto end = std::chrono::steady_clock::now();
CHECK_FALSE(connected);
// Should timeout within reasonable time (allowing some margin)
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
CHECK(duration.count() < 1000); // Should be much less than 1 second
}
TEST_CASE_METHOD(WebSocketTransportFixture, "WebSocketTransport - Send without connection",
"[mcp][transport][websocket][catch2]") {
WebSocketTransport transport(config);
json testMessage = {{"jsonrpc", "2.0"}, {"method", "test"}, {"id", 1}};
// Should throw exception when trying to send without connection
REQUIRE_THROWS_AS(transport.send(testMessage), std::runtime_error);
}
#endif // Disabled WebSocket tests
// Placeholder test to ensure the test file compiles
TEST_CASE("WebSocketTransport - Placeholder", "[mcp][transport][websocket][catch2][placeholder]") {
// WebSocket transport tests are disabled pending full implementation
SUCCEED("WebSocket transport tests are disabled - placeholder test passes");
}