#!/usr/bin/env node /** * Quick Server Start Test * Tests that the server starts and responds to health checks */ import { spawn } from 'child_process'; import { request } from 'http'; const DATABASE_URL = process.env.DATABASE_URL || 'postgresql://memento:memento@localhost:5433/memento?schema=public'; const PORT = process.env.MCP_TEST_PORT || 3010; console.log('═══════════════════════════════════════════════════'); console.log(' Memento MCP Server - Start Test'); console.log('═══════════════════════════════════════════════════\n'); const env = { ...process.env, DATABASE_URL, PORT: PORT.toString(), MCP_REQUIRE_AUTH: 'false', MCP_LOG_LEVEL: 'error', }; console.log(`Starting server on port ${PORT}...`); const server = spawn('node', ['index-sse.js'], { cwd: new URL('..', import.meta.url).pathname, env, stdio: ['ignore', 'pipe', 'pipe'], }); let output = ''; server.stdout.on('data', (data) => { output += data.toString(); }); server.stderr.on('data', (data) => { output += data.toString(); }); server.on('error', (err) => { console.error('❌ Failed to start server:', err.message); process.exit(1); }); // Wait for server to start setTimeout(() => { console.log('Testing health endpoint...'); const options = { hostname: 'localhost', port: PORT, path: '/health', method: 'GET', }; const req = request(options, (res) => { let data = ''; res.on('data', (chunk) => { data += chunk; }); res.on('end', () => { if (res.statusCode === 200) { console.log('✅ Health check passed!'); console.log('Response:', data); // Test metrics endpoint request({ ...options, path: '/metrics' }, (mRes) => { if (mRes.statusCode === 200) { console.log('✅ Metrics endpoint available!'); } shutdown(); }).on('error', shutdown); } else { console.error(`❌ Health check failed: ${res.statusCode}`); console.error('Response:', data); shutdown(1); } }); }); req.on('error', (err) => { console.error('❌ Request failed:', err.message); console.error('Server output:'); console.error(output); shutdown(1); }); req.end(); }, 2000); function shutdown(code = 0) { server.kill('SIGTERM'); setTimeout(() => { server.kill('SIGKILL'); process.exit(code); }, 2000); }