From 2ecb214510b7648711eb652edbeb9480a8f8160c Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=CE=A7=CE=B3=CF=86=CF=84=20Kompanion?=
 <chigammaphitau@chelydra.at>
Date: Sat, 18 Oct 2025 09:37:32 +0200
Subject: [PATCH] Add full integration tests and more debug outout

---
 src/mcp/KompanionQtServer.cpp |  6 +++++-
 tests/CMakeLists.txt          |  6 ++++++
 tests/e2e_mcp_test.sh         | 14 ++++++++++----
 3 files changed, 21 insertions(+), 5 deletions(-)

diff --git a/src/mcp/KompanionQtServer.cpp b/src/mcp/KompanionQtServer.cpp
index af8785b..44eeea1 100644
--- a/src/mcp/KompanionQtServer.cpp
+++ b/src/mcp/KompanionQtServer.cpp
@@ -69,7 +69,8 @@ KompanionQtServer::KompanionQtServer(const QString &backend, KomMcpServer *logic
         return result;
     });
 
-    addRequestHandler([this](const QUuid &, const QMcpCallToolRequest &request, QMcpJSONRPCErrorError *error) {
+    addRequestHandler([this](const QUuid &correlationId, const QMcpCallToolRequest &request, QMcpJSONRPCErrorError *error) {
+        qDebug() << "KompanionQtServer: Received tool call request:" << correlationId;
         QMcpCallToolResult result;
 
         if (!m_logic) {
@@ -138,7 +139,9 @@ KompanionQtServer::KompanionQtServer(const QString &backend, KomMcpServer *logic
         }
 
         const QByteArray payload = QJsonDocument(args).toJson(QJsonDocument::Compact);
+        qDebug() << "KompanionQtServer: Dispatching payload:" << payload;
         const std::string responseStr = m_logic->dispatch(toolKey, payload.toStdString());
+        qDebug() << "KompanionQtServer: Received response:" << responseStr.c_str();
         const QByteArray jsonBytes = QByteArray::fromStdString(responseStr);
 
         QJsonParseError parseError{};
@@ -158,6 +161,7 @@ KompanionQtServer::KompanionQtServer(const QString &backend, KomMcpServer *logic
 
 QList<QMcpTool> KompanionQtServer::loadToolsFromSchema() const
 {
+    qDebug() << "KompanionQtServer: Loading tools from schema...";
     QList<QMcpTool> tools;
     QFile kSchemaResource(":/kompanion/ToolSchemas.json");
     if (!kSchemaResource.open(QIODevice::ReadOnly)) {
diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt
index 132e50c..888fd6a 100644
--- a/tests/CMakeLists.txt
+++ b/tests/CMakeLists.txt
@@ -24,3 +24,9 @@ target_link_libraries(test_memory_exchange PRIVATE kom_dal)
 target_compile_options(test_memory_exchange PRIVATE -fexceptions)
 
 add_test(NAME mcp_memory_exchange COMMAND test_memory_exchange)
+
+add_test(
+  NAME e2e_mcp_test
+  COMMAND /bin/bash ${CMAKE_CURRENT_SOURCE_DIR}/e2e_mcp_test.sh
+  WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
+)
diff --git a/tests/e2e_mcp_test.sh b/tests/e2e_mcp_test.sh
index 3f83e40..6c95743 100755
--- a/tests/e2e_mcp_test.sh
+++ b/tests/e2e_mcp_test.sh
@@ -13,6 +13,9 @@ MCP_SERVER_URL="http://${MCP_SERVER_HOST}:${MCP_SERVER_PORT}"
 # --- Cleanup Function ---
 cleanup() {
     echo "--- Cleaning up ---"
+    if [ -n "$mcp_server_pid" ]; then
+        kill "$mcp_server_pid" || true
+    fi
     pkill -f kom_mcp || true
     sleep 1 # Give the OS time to release the port
     netstat -tuln | grep ":${MCP_SERVER_PORT}" || true # Check if port is still in use
@@ -31,10 +34,13 @@ export DB_URL="dbname=${TEST_DB_NAME} user=kompanion host=/var/run/postgresql" E
 python3 "${PROJECT_ROOT_DIR}/tools/ingest_dir.py" "${PROJECT_ROOT_DIR}/tests/test_data" "dev_knowledge"
 
 echo ">> Starting MCP server..."
-$MCP_SERVER_EXECUTABLE --backend sse --address "${MCP_SERVER_HOST}:${MCP_SERVER_PORT}" &
+sleep 2
+timeout 10 $MCP_SERVER_EXECUTABLE --backend sse --address "${MCP_SERVER_HOST}:${MCP_SERVER_PORT}" < /dev/null > /dev/null 2>&1 &
+mcp_server_pid=$!
 mcp_server_pid=$!
 
-sleep 3
+sleep 5
+ps -ef | grep kom_mcp
 
 # --- API Test Functions ---
 
@@ -47,9 +53,9 @@ send_request() {
 # --- Running API tests ---
 
 echo "--> Establishing SSE connection..."
-SSE_RESPONSE=$(curl -s -N -H "Accept:text/event-stream" "${MCP_SERVER_URL}/")
+SSE_RESPONSE=$(curl -s -N -H "Accept:text/event-stream" "${MCP_SERVER_URL}/sse")
 sleep 1 # Give the server time to send the response
-SESSION_ID=$(echo "$SSE_RESPONSE" | grep -oE 'data: /messages/\?session_id=([a-f0-9-]+)' | cut -d '=' -f 2)
+SESSION_ID=$(echo "$SSE_RESPONSE" | grep -m 1 -oE 'data: /messages/\?session_id=([a-f0-9-]+)' | cut -d '=' -f 2)
 
 if [ -z "$SESSION_ID" ]; then
     echo "Failed to get session ID"
