const { spawn } = require('child_process');
const axios = require('axios');
// Basic integration tests for MCP WooCommerce Server
describe('MCP WooCommerce Server Tests', () => {
let serverProcess;
const PORT = process.env.TEST_PORT || 3001;
beforeAll(async () => {
// Set test environment variables
process.env.PORT = PORT;
process.env.WOOCOMMERCE_SITE_URL = 'https://demo.woocommerce.com';
process.env.WOOCOMMERCE_CONSUMER_KEY = 'test_key';
process.env.WOOCOMMERCE_CONSUMER_SECRET = 'test_secret';
process.env.LOG_LEVEL = 'error';
// Start the server for testing
serverProcess = spawn('node', ['dist/index.js'], {
env: process.env,
stdio: 'pipe'
});
// Wait for server to start
await new Promise(resolve => setTimeout(resolve, 3000));
});
afterAll(() => {
if (serverProcess) {
serverProcess.kill();
}
});
test('Health endpoint should be accessible', async () => {
try {
const response = await axios.get(`http://localhost:${PORT}/health`, {
timeout: 5000
});
expect(response.status).toBe(200);
expect(response.data).toHaveProperty('status');
console.log('✅ Health check passed');
} catch (error) {
if (error.code === 'ECONNREFUSED') {
console.log('⚠️ Server not running - skipping health check');
} else {
console.log('❌ Health check failed:', error.message);
}
}
});
test('Info endpoint should return server information', async () => {
try {
const response = await axios.get(`http://localhost:${PORT}/info`, {
timeout: 5000
});
expect(response.status).toBe(200);
expect(response.data).toHaveProperty('server_info');
console.log('✅ Info endpoint passed');
} catch (error) {
if (error.code === 'ECONNREFUSED') {
console.log('⚠️ Server not running - skipping info check');
} else {
console.log('❌ Info check failed:', error.message);
}
}
});
test('N8n webhook endpoint should accept POST requests', async () => {
try {
const response = await axios.post(`http://localhost:${PORT}/webhook/n8n`, {
test: 'data',
event: 'test_event'
}, {
timeout: 5000
});
expect(response.status).toBe(200);
expect(response.data).toHaveProperty('success', true);
console.log('✅ N8n webhook passed');
} catch (error) {
if (error.code === 'ECONNREFUSED') {
console.log('⚠️ Server not running - skipping webhook check');
} else {
console.log('❌ Webhook check failed:', error.message);
}
}
});
});
// Run manual tests if this file is executed directly
if (require.main === module) {
console.log('🚀 Starting MCP WooCommerce Server Manual Tests...\n');
const runTest = async (name, testFn) => {
try {
console.log(`Running: ${name}`);
await testFn();
} catch (error) {
console.log(`❌ ${name} failed:`, error.message);
}
};
const PORT = process.env.TEST_PORT || 3001;
// Manual test functions
const tests = [
['Health Check', async () => {
const response = await axios.get(`http://localhost:${PORT}/health`);
console.log('Health Status:', response.data.status);
}],
['Info Check', async () => {
const response = await axios.get(`http://localhost:${PORT}/info`);
console.log('Server Name:', response.data.server_info?.name);
console.log('WooCommerce URL:', response.data.store_url);
}],
['Webhook Test', async () => {
const response = await axios.post(`http://localhost:${PORT}/webhook/n8n`, {
test_event: 'manual_test',
timestamp: new Date().toISOString()
});
console.log('Webhook Response:', response.data.success ? 'OK' : 'Failed');
}]
];
// Run all tests sequentially
(async () => {
for (const [name, testFn] of tests) {
await runTest(name, testFn);
console.log('');
}
console.log('✨ Manual tests completed!\n');
console.log('To run with Jest: npm test');
console.log('To start server: npm start');
console.log('To build: npm run build');
})().catch(error => {
console.error('Test execution failed:', error.message);
process.exit(1);
});
}