test-full-integration.shโข8.49 kB
#!/bin/bash
# Full Integration Test Script for MCP Self-Learning Server
# Tests all components: CLI, API, clients, integrations, and systemd service
set -e # Exit on any error
echo "๐งช MCP Self-Learning Server - Full Integration Test Suite"
echo "========================================================"
echo
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Test counters
TOTAL_TESTS=0
PASSED_TESTS=0
FAILED_TESTS=0
# Function to run a test
run_test() {
local test_name="$1"
local test_command="$2"
local expected_output="$3"
TOTAL_TESTS=$((TOTAL_TESTS + 1))
echo -n "[$TOTAL_TESTS] Testing $test_name... "
if output=$(eval "$test_command" 2>&1); then
if [[ -z "$expected_output" ]] || echo "$output" | grep -q "$expected_output"; then
echo -e "${GREEN}โ
PASS${NC}"
PASSED_TESTS=$((PASSED_TESTS + 1))
return 0
else
echo -e "${RED}โ FAIL${NC} (Expected: $expected_output)"
echo " Output: $output"
FAILED_TESTS=$((FAILED_TESTS + 1))
return 1
fi
else
echo -e "${RED}โ FAIL${NC}"
echo " Error: $output"
FAILED_TESTS=$((FAILED_TESTS + 1))
return 1
fi
}
# Function to check if service is running
check_service() {
curl -s http://localhost:8765/health > /dev/null 2>&1
}
# Function to wait for service
wait_for_service() {
local max_attempts=30
local attempt=1
echo -n "Waiting for service to start"
while [ $attempt -le $max_attempts ]; do
if check_service; then
echo -e " ${GREEN}โ
${NC}"
return 0
fi
echo -n "."
sleep 1
attempt=$((attempt + 1))
done
echo -e " ${RED}โ Timeout${NC}"
return 1
}
echo "๐ง Phase 1: Global Installation Tests"
echo "------------------------------------"
run_test "Global CLI Installation" "which mcp-learn" "mcp-learn"
run_test "Version Command" "mcp-learn --version" "1.0.0"
run_test "Help Command" "mcp-learn --help" "MCP Self-Learning Server CLI"
run_test "Health Check Command" "mcp-learn health" "health check"
echo
echo "๐ฅ Phase 2: Service Management Tests"
echo "-----------------------------------"
# Stop any existing API server
pkill -f "mcp-learn api" 2>/dev/null || true
sleep 2
# Test systemd service
run_test "Service Status Check" "systemctl --user is-active mcp-self-learning.service || echo 'inactive'" "active"
if ! check_service; then
echo "Service not running, starting it..."
systemctl --user start mcp-self-learning.service
wait_for_service
fi
run_test "Service Health Check" "curl -s http://localhost:8765/health | jq -r .status" "healthy"
run_test "Service Status Check" "curl -s http://localhost:8765/status | jq -r .running" "true"
echo
echo "๐ Phase 3: REST API Tests"
echo "-------------------------"
run_test "Health Endpoint" "curl -s http://localhost:8765/health | jq -r .status" "healthy"
run_test "Status Endpoint" "curl -s http://localhost:8765/status | jq -r .running" "true"
# Test analyze endpoint
ANALYZE_DATA='{"interaction":{"type":"integration_test","input":"test input","output":"test output","success":true}}'
run_test "Analyze Endpoint" "curl -s -X POST http://localhost:8765/analyze -H 'Content-Type: application/json' -d '$ANALYZE_DATA' | jq -r .success" "true"
run_test "Insights Endpoint" "curl -s http://localhost:8765/insights | jq -r .success" "true"
run_test "Metrics Endpoint" "curl -s http://localhost:8765/metrics | jq -r .success" "true"
echo
echo "๐ฑ Phase 4: CLI Commands with Service Running"
echo "--------------------------------------------"
run_test "Status Command" "mcp-learn status | head -1" "Checking status"
run_test "Analyze Command" "mcp-learn analyze --type 'test' --input 'CLI test' --output 'CLI result' --success | head -1" "Analyzing pattern"
run_test "Insights Command" "mcp-learn insights | head -1" "Fetching insights"
echo
echo "๐ข Phase 5: Node.js Client Library Test"
echo "--------------------------------------"
# Create temporary Node.js test
cat > test-temp-client.js << 'EOF'
import SelfLearningClient from './lib/self-learning-client.js';
const client = new SelfLearningClient({ port: 8765 });
try {
const connected = await client.connect();
if (connected) {
const health = await client.getHealth();
console.log(health.status);
} else {
console.log('failed');
}
} catch (error) {
console.log('error');
}
EOF
run_test "Node.js Client Connection" "timeout 10 node test-temp-client.js" "healthy"
rm -f test-temp-client.js
echo
echo "๐ Phase 6: Python Client Library Test"
echo "--------------------------------------"
# Create temporary Python test
cat > test-temp-python.py << 'EOF'
import sys
import asyncio
sys.path.insert(0, './lib')
async def test():
try:
from self_learning_client import SelfLearningClient
client = SelfLearningClient(base_url="http://localhost:8765")
connected = await client.connect()
if connected:
health = await client.get_health()
print(health['status'])
await client.disconnect()
else:
print('failed')
except Exception as e:
print('error')
asyncio.run(test())
EOF
# Use virtual environment if it exists
if [ -d "test-env" ]; then
run_test "Python Client Connection" "source test-env/bin/activate && timeout 10 python3 test-temp-python.py" "healthy"
else
echo "[$((TOTAL_TESTS + 1))] Testing Python Client Connection... ${YELLOW}โญ๏ธ SKIP${NC} (Virtual env not available)"
TOTAL_TESTS=$((TOTAL_TESTS + 1))
fi
rm -f test-temp-python.py
echo
echo "๐ค Phase 7: Claudio Integration Test"
echo "-----------------------------------"
# Create temporary Claudio test
cat > test-temp-claudio.js << 'EOF'
import { ClaudioMCPLearningTools } from './integrations/claudio-mcp-tools.js';
try {
const tools = new ClaudioMCPLearningTools();
await tools.initialize();
const definitions = tools.getToolDefinitions();
console.log(definitions.length);
await tools.cleanup();
} catch (error) {
console.log('error');
}
EOF
run_test "Claudio Integration" "timeout 15 node test-temp-claudio.js" "6"
rm -f test-temp-claudio.js
echo
echo "๐พ Phase 8: Data Persistence Test"
echo "--------------------------------"
# Test data persistence by adding data, restarting service, and checking if data persists
BEFORE_RESTART=$(curl -s http://localhost:8765/insights | jq -r '.insights.knowledgeItems')
run_test "Data Before Restart" "echo '$BEFORE_RESTART' | grep -E '^[0-9]+$'" ""
# Restart service
echo "Restarting service to test persistence..."
systemctl --user restart mcp-self-learning.service
wait_for_service
AFTER_RESTART=$(curl -s http://localhost:8765/insights | jq -r '.insights.knowledgeItems')
run_test "Data After Restart" "echo '$AFTER_RESTART' | grep -E '^[0-9]+$'" ""
echo
echo "๐ Phase 9: Load Test (Basic)"
echo "----------------------------"
echo "Running concurrent requests..."
for i in {1..5}; do
curl -s http://localhost:8765/health > /dev/null &
done
wait
run_test "Service Still Responsive" "curl -s http://localhost:8765/health | jq -r .status" "healthy"
echo
echo "๐งน Phase 10: Cleanup Test"
echo "------------------------"
run_test "Stop Service" "systemctl --user stop mcp-self-learning.service; sleep 2; echo 'stopped'" "stopped"
run_test "Service Stopped" "systemctl --user is-active mcp-self-learning.service || echo 'inactive'" "inactive"
# Restart for any follow-up usage
systemctl --user start mcp-self-learning.service
wait_for_service
echo
echo "๐ Test Results Summary"
echo "======================="
echo -e "Total Tests: ${BLUE}$TOTAL_TESTS${NC}"
echo -e "Passed: ${GREEN}$PASSED_TESTS${NC}"
echo -e "Failed: ${RED}$FAILED_TESTS${NC}"
if [ $FAILED_TESTS -eq 0 ]; then
echo -e "\n${GREEN}๐ ALL TESTS PASSED!${NC}"
echo -e "${GREEN}โ
MCP Self-Learning Server is fully functional${NC}"
echo
echo "๐ System Ready for Production Use!"
echo " โข Global CLI: mcp-learn"
echo " โข REST API: http://localhost:8765"
echo " โข Systemd Service: mcp-self-learning.service"
echo " โข Claudio Integration: Ready"
echo " โข Claudia Integration: Ready"
exit 0
else
echo -e "\n${RED}โ Some tests failed${NC}"
echo "Please check the output above for details"
exit 1
fi