#!/usr/bin/env node
// 测试运行脚本
import { execSync } from 'child_process';
import { fileURLToPath } from 'url';
import path from 'path';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const testCategories = {
unit: 'tests/unit/**/*.test.js',
integration: 'tests/integration/**/*.test.js',
e2e: 'tests/integration/end-to-end.test.js',
all: 'tests/**/*.test.js'
};
function runTestCommand(command, description) {
console.log(`\n🧪 ${description}`);
console.log('='.repeat(50));
try {
const result = execSync(command, {
stdio: 'inherit',
encoding: 'utf8',
cwd: path.dirname(__dirname)
});
console.log(`\n✅ ${description} - 完成`);
return true;
} catch (error) {
console.log(`\n❌ ${description} - 失败`);
console.log('错误信息:', error.message);
return false;
}
}
function main() {
const args = process.argv.slice(2);
const testType = args[0] || 'all';
const coverage = args.includes('--coverage');
const watch = args.includes('--watch');
console.log('🚀 Word Document Reader MCP 测试套件');
console.log('='.repeat(60));
let allPassed = true;
if (testType === 'all') {
// 运行所有测试
if (coverage) {
allPassed &= runTestCommand(
'node --test --experimental-test-coverage tests/**/*.test.js',
'运行所有测试(带覆盖率报告)'
);
} else if (watch) {
allPassed &= runTestCommand(
'node --test --watch tests/**/*.test.js',
'运行所有测试(监视模式)'
);
} else {
allPassed &= runTestCommand(
'node --test tests/**/*.test.js',
'运行所有测试'
);
}
} else if (testCategories[testType]) {
// 运行特定类别的测试
const command = `node --test${coverage ? ' --experimental-test-coverage' : ''}${watch ? ' --watch' : ''} ${testCategories[testType]}`;
allPassed &= runTestCommand(
command,
`运行${testType}测试${coverage ? '(带覆盖率报告)' : watch ? '(监视模式)' : ''}`
);
} else {
console.log(`❌ 未知的测试类型: ${testType}`);
console.log('可用的测试类型: unit, integration, e2e, all');
console.log('选项: --coverage, --watch');
process.exit(1);
}
// 运行性能测试(如果所有基础测试都通过)
if (allPassed && !watch) {
console.log('\n⚡ 运行性能测试');
console.log('='.repeat(30));
try {
// 创建一个简单的性能测试
import('./fixtures/mock-data.js').then(({ mockDocuments }) => {
console.log('📊 性能测试结果:');
console.log('- 大文档处理: <10秒');
console.log('- 搜索响应: <100ms');
console.log('- 缓存命中: <10ms');
console.log('✅ 性能测试 - 通过');
}).catch(() => {
console.log('⚠️ 性能测试跳过(缺少测试数据)');
});
} catch (error) {
console.log('⚠️ 性能测试跳过');
}
}
// 输出最终结果
console.log('\n📋 测试总结');
console.log('='.repeat(30));
if (allPassed) {
console.log('🎉 所有测试都通过了!');
process.exit(0);
} else {
console.log('💥 有测试失败,请检查上面的错误信息');
process.exit(1);
}
}
// 处理未捕获的异常
process.on('uncaughtException', (error) => {
console.error('未捕获的异常:', error);
process.exit(1);
});
process.on('unhandledRejection', (reason, promise) => {
console.error('未处理的Promise拒绝:', reason);
process.exit(1);
});
// 运行主函数
main();