#!/usr/bin/env bash set -euo pipefail set -x # --- Test Configuration --- TEST_DB_NAME="kompanion_autotest" MCP_SERVER_EXECUTABLE="./bin/kom_mcp" PROJECT_ROOT_DIR=$(git rev-parse --show-toplevel) MCP_SERVER_HOST="127.0.0.1" MCP_SERVER_PORT="8081" MCP_SERVER_URL="http://${MCP_SERVER_HOST}:${MCP_SERVER_PORT}" # --- Cleanup Function --- cleanup() { echo "--- Cleaning up ---" 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 psql -v ON_ERROR_STOP=1 -c "DROP DATABASE IF EXISTS \"$TEST_DB_NAME\";" >/dev/null 2>&1 } trap cleanup EXIT echo "--- Setting up test environment ---" echo ">> Initializing test database..." "${PROJECT_ROOT_DIR}/db/scripts/create-test-db.sh" "$TEST_DB_NAME" echo ">> Harvesting embeddings..." export DB_URL="dbname=${TEST_DB_NAME} user=kompanion host=/var/run/postgresql" EMBED_NAMESPACE="dev_knowledge" 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}" & mcp_server_pid=$! sleep 3 # --- API Test Functions --- send_request() { local session_id=$1 local payload=$2 curl -s -X POST -H "Content-Type: application/json" -d "$payload" "${MCP_SERVER_URL}/messages?session_id=${session_id}" } # --- Running API tests --- echo "--> Establishing SSE connection..." SSE_RESPONSE=$(curl -s -N -H "Accept:text/event-stream" "${MCP_SERVER_URL}/") 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) if [ -z "$SESSION_ID" ]; then echo "Failed to get session ID" exit 1 fi echo "Session ID: $SESSION_ID" # Test upsert_memory echo "--> Testing upsert_memory..." UPSERT_PAYLOAD='{ "jsonrpc": "2.0", "method": "tool", "params": { "name": "kom.memory.v1.upsert_memory", "arguments": { "auth_token": "dev_knowledge:test-secret", "namespace": "dev_knowledge", "items": [ { "id": "test-item-1", "text": "This is a test item for upsert_memory.", "tags": ["test", "upsert"] } ] } } }' response=$(send_request "$SESSION_ID" "$UPSERT_PAYLOAD") echo "$response" | grep '"status":"ok"' > /dev/null || (echo "upsert_memory test failed" && exit 1) echo "upsert_memory test passed." # Test search_memory echo "--> Testing search_memory..." SEARCH_PAYLOAD='{ "jsonrpc": "2.0", "method": "tool", "params": { "name": "kom.memory.v1.search_memory", "arguments": { "auth_token": "dev_knowledge:test-secret", "namespace": "dev_knowledge", "query": { "text": "upsert" } } } }' response=$(send_request "$SESSION_ID" "$SEARCH_PAYLOAD") echo "$response" | grep '"id":"test-item-1"' > /dev/null || (echo "search_memory test failed" && exit 1) echo "search_memory test passed." # Test save_context echo "--> Testing save_context..." SAVE_CONTEXT_PAYLOAD='{ "jsonrpc": "2.0", "method": "tool", "params": { "name": "kom.memory.v1.save_context", "arguments": { "auth_token": "dev_knowledge:test-secret", "namespace": "dev_knowledge", "key": "test-context-1", "content": { "message": "This is a test context." }, "tags": ["test", "context"] } } }' response=$(send_request "$SESSION_ID" "$SAVE_CONTEXT_PAYLOAD") echo "$response" | grep '"id":' > /dev/null || (echo "save_context test failed" && exit 1) echo "save_context test passed." # Test recall_context echo "--> Testing recall_context..." RECALL_CONTEXT_PAYLOAD='{ "jsonrpc": "2.0", "method": "tool", "params": { "name": "kom.memory.v1.recall_context", "arguments": { "auth_token": "dev_knowledge:test-secret", "namespace": "dev_knowledge", "key": "test-context-1" } } }' response=$(send_request "$SESSION_ID" "$RECALL_CONTEXT_PAYLOAD") echo "$response" | grep '"key":"test-context-1"' > /dev/null || (echo "recall_context test failed" && exit 1) echo "recall_context test passed."