imo-publications-mcp-server
Version:
MCP server for IMO (International Maritime Organization) publications - Node.js TypeScript version
158 lines (132 loc) • 4.36 kB
JavaScript
import { spawn } from 'child_process';
import path from 'path';
import { fileURLToPath } from 'url';
import os from 'os';
import fs from 'fs';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
// Test log file
const testLogFile = path.join(os.tmpdir(), 'imo-publications-test.log');
function log(message) {
const timestamp = new Date().toISOString();
const logMessage = `${timestamp} - ${message}\n`;
console.log(message);
try {
fs.appendFileSync(testLogFile, logMessage);
} catch (err) {
// Ignore file write errors
}
}
log('Starting IMO Publications MCP Server test...');
log(`Test log file: ${testLogFile}`);
// Test configuration
const testConfig = {
mongoUri: process.env.MONGODB_URI || 'mongodb://localhost:27017',
dbName: process.env.MONGODB_DB_NAME || 'imo_publications_test',
typesenseHost: process.env.TYPESENSE_HOST || 'localhost',
typesensePort: process.env.TYPESENSE_PORT || '8108',
typesenseProtocol: process.env.TYPESENSE_PROTOCOL || 'http',
typesenseApiKey: process.env.TYPESENSE_API_KEY || 'test-key'
};
log('Test configuration:');
log(` MongoDB URI: ${testConfig.mongoUri.substring(0, 20)}...`);
log(` DB Name: ${testConfig.dbName}`);
log(` Typesense Host: ${testConfig.typesenseHost}`);
log(` Typesense Port: ${testConfig.typesensePort}`);
log(` Typesense Protocol: ${testConfig.typesenseProtocol}`);
log(` Typesense API Key: ${testConfig.typesenseApiKey ? 'Set' : 'Not set'}`);
// Check if built files exist
const distPath = path.join(__dirname, 'dist', 'index.js');
log(`Checking for built server at: ${distPath}`);
if (!fs.existsSync(distPath)) {
log('ERROR: Built server not found. Run "npm run build" first.');
process.exit(1);
}
// Test scenarios
const testScenarios = [
{
name: 'Help Command',
args: ['--help']
},
{
name: 'Version Check (build test)',
args: [
'--mongo-uri', testConfig.mongoUri,
'--db-name', testConfig.dbName,
'--typesense-host', testConfig.typesenseHost,
'--typesense-port', testConfig.typesensePort,
'--typesense-protocol', testConfig.typesenseProtocol,
'--typesense-api-key', testConfig.typesenseApiKey,
'--non-interactive'
],
timeout: 5000
}
];
let currentScenario = 0;
function runNextTest() {
if (currentScenario >= testScenarios.length) {
log('All tests completed successfully!');
log(`Full test log available at: ${testLogFile}`);
return;
}
const scenario = testScenarios[currentScenario];
log(`\nRunning test: ${scenario.name}`);
log(`Command: node ${distPath} ${scenario.args.join(' ')}`);
const child = spawn('node', [distPath, ...scenario.args], {
stdio: 'pipe',
env: {
...process.env,
MONGODB_URI: testConfig.mongoUri,
MONGODB_DB_NAME: testConfig.dbName,
TYPESENSE_HOST: testConfig.typesenseHost,
TYPESENSE_PORT: testConfig.typesensePort,
TYPESENSE_PROTOCOL: testConfig.typesenseProtocol,
TYPESENSE_API_KEY: testConfig.typesenseApiKey
}
});
let output = '';
let errorOutput = '';
child.stdout.on('data', (data) => {
output += data.toString();
});
child.stderr.on('data', (data) => {
errorOutput += data.toString();
});
// Set timeout for scenarios that specify it
let timeout;
if (scenario.timeout) {
timeout = setTimeout(() => {
child.kill('SIGTERM');
log(`Test "${scenario.name}" timed out after ${scenario.timeout}ms - this is expected for server startup tests`);
currentScenario++;
runNextTest();
}, scenario.timeout);
}
child.on('exit', (code) => {
if (timeout) clearTimeout(timeout);
log(`Test "${scenario.name}" completed with exit code: ${code}`);
if (output) {
log('STDOUT:');
log(output);
}
if (errorOutput) {
log('STDERR:');
log(errorOutput);
}
// For help command, expect exit code 0
if (scenario.name === 'Help Command' && code !== 0) {
log(`ERROR: Help command failed with exit code ${code}`);
process.exit(1);
}
currentScenario++;
runNextTest();
});
child.on('error', (error) => {
if (timeout) clearTimeout(timeout);
log(`Test "${scenario.name}" error: ${error.message}`);
process.exit(1);
});
}
// Start testing
runNextTest();