defmodule AiDebug.MCPConnectivityTest do use ExUnit.Case, async: true alias AiDebug.MCPHttpClient describe "MCP connectivity" do test "MCPHttpClient can connect to HTTP bridge" do # Start the MCP HTTP bridge if not running ensure_mcp_http_bridge_running() # Test basic connectivity case HTTPoison.get("http://localhost:3010/health") do {:ok, %{status_code: 200, body: body}} -> # Bridge is running, test tool call assert {:ok, _result} = MCPHttpClient.call_tool("get_subscription_info", %{}) {:error, %{reason: :econnrefused}} -> # Bridge not running, this is the expected failure case we need to fix assert false, "MCP HTTP bridge not running on port 3010" other -> assert false, "Unexpected response from MCP health check: #{inspect(other)}" end end test "QA Gates can successfully call MCP tools" do ensure_mcp_http_bridge_running() # Test the specific tools that were failing in QA Gates performance_result = AiDebug.QAGates.check_performance("http://localhost:4000") refute performance_result.error == "mcp_server_not_running" accessibility_result = AiDebug.QAGates.check_accessibility("http://localhost:4000") refute accessibility_result.error == "mcp_server_not_running" console_result = AiDebug.QAGates.check_console_errors("http://localhost:4000") refute console_result.error == "mcp_server_not_running" end end defp ensure_mcp_http_bridge_running do case HTTPoison.get("http://localhost:3010/health") do {:ok, %{status_code: 200}} -> :ok {:error, %{reason: :econnrefused}} -> # Start the MCP HTTP bridge start_mcp_http_bridge() # Wait for startup :timer.sleep(2000) # Verify it's running case HTTPoison.get("http://localhost:3010/health") do {:ok, %{status_code: 200}} -> :ok other -> assert false, "Failed to start MCP HTTP bridge: #{inspect(other)}" end end end defp start_mcp_http_bridge do # Path to the HTTP bridge script bridge_script = Path.expand("../../../scripts/start-mcp-http.cjs", __DIR__) if File.exists?(bridge_script) do # Start as background process spawn(fn -> System.cmd("node", [bridge_script], stderr_to_stdout: true) end) else assert false, "MCP HTTP bridge script not found at #{bridge_script}" end end end