|
|
@@ -14,7 +14,7 @@ import * as fs from 'fs/promises';
|
|
|
import * as path from 'path';
|
|
|
|
|
|
async function testErrorHandling() {
|
|
|
- console.log('🧪 开始错误处理和降级机制测试\n');
|
|
|
+ console.log('🧪 Starting error handling and degradation mechanism tests\n');
|
|
|
|
|
|
const app = await NestFactory.createApplicationContext(AppModule, {
|
|
|
logger: ['error', 'warn', 'log'],
|
|
|
@@ -22,7 +22,7 @@ async function testErrorHandling() {
|
|
|
|
|
|
try {
|
|
|
// 测试 1: LibreOffice 服务不可用
|
|
|
- console.log('=== 测试 1: LibreOffice 服务不可用 ===');
|
|
|
+ console.log('=== Test 1: LibreOffice service unavailable ===');
|
|
|
const libreOffice = app.get(LibreOfficeService);
|
|
|
|
|
|
try {
|
|
|
@@ -37,71 +37,71 @@ async function testErrorHandling() {
|
|
|
if (await fs.access(testWord).then(() => true).catch(() => false)) {
|
|
|
try {
|
|
|
await libreOffice.convertToPDF(testWord);
|
|
|
- console.log('❌ 应该失败但成功了');
|
|
|
+ console.log('❌ Should have failed but succeeded');
|
|
|
} catch (error) {
|
|
|
- console.log(`✅ 正确捕获错误: ${error.message}`);
|
|
|
+ console.log(`✅ Correctly caught error: ${error.message}`);
|
|
|
}
|
|
|
} else {
|
|
|
- console.log('⚠️ 测试 Word 文件不存在,跳过此部分');
|
|
|
+ console.log('⚠️ Test Word file does not exist, skipping this part');
|
|
|
}
|
|
|
|
|
|
// 恢复配置
|
|
|
process.env.LIBREOFFICE_URL = originalUrl;
|
|
|
} catch (error) {
|
|
|
- console.log('✅ LibreOffice 错误处理测试完成');
|
|
|
+ console.log('✅ LibreOffice error handling test complete');
|
|
|
}
|
|
|
|
|
|
// 测试 2: PDF 转图片失败
|
|
|
- console.log('\n=== 测试 2: PDF 转图片失败 ===');
|
|
|
+ console.log('\n=== Test 2: PDF to Image conversion failed ===');
|
|
|
const pdf2Image = app.get(Pdf2ImageService);
|
|
|
|
|
|
try {
|
|
|
// 测试不存在的 PDF
|
|
|
await pdf2Image.convertToImages('/nonexistent/file.pdf');
|
|
|
- console.log('❌ 应该失败但成功了');
|
|
|
+ console.log('❌ Should have failed but succeeded');
|
|
|
} catch (error) {
|
|
|
- console.log(`✅ 正确捕获错误: ${error.message}`);
|
|
|
+ console.log(`✅ Correctly caught error: ${error.message}`);
|
|
|
}
|
|
|
|
|
|
// 测试 3: Vision Pipeline 完整流程 - 降级测试
|
|
|
- console.log('\n=== 测试 3: Vision Pipeline 降级机制 ===');
|
|
|
+ console.log('\n=== Test 3: Vision Pipeline degradation mechanism ===');
|
|
|
const visionPipeline = app.get(VisionPipelineService);
|
|
|
|
|
|
// 检查是否有测试文件
|
|
|
const testPdf = '/home/fzxs/workspaces/demo/simple-kb/uploads/file-1766236004300-577549403.pdf';
|
|
|
if (await fs.access(testPdf).then(() => true).catch(() => false)) {
|
|
|
- console.log(`测试文件: ${path.basename(testPdf)}`);
|
|
|
+ console.log(`Test file: ${path.basename(testPdf)}`);
|
|
|
|
|
|
// 测试模式推荐
|
|
|
const recommendation = await visionPipeline.recommendMode(testPdf);
|
|
|
- console.log(`模式推荐: ${recommendation.recommendedMode}`);
|
|
|
- console.log(`原因: ${recommendation.reason}`);
|
|
|
+ console.log(`Recommended mode: ${recommendation.recommendedMode}`);
|
|
|
+ console.log(`Reason: ${recommendation.reason}`);
|
|
|
|
|
|
// 如果推荐精准模式,测试流程
|
|
|
if (recommendation.recommendedMode === 'precise') {
|
|
|
- console.log('\n⚠️ 注意: 完整流程测试需要:');
|
|
|
- console.log(' 1. LibreOffice 服务运行');
|
|
|
- console.log(' 2. ImageMagick 安装');
|
|
|
- console.log(' 3. Vision 模型 API Key 配置');
|
|
|
- console.log('\n如需完整测试,请手动配置以上环境');
|
|
|
+ console.log('\n⚠️ Note: Full pipeline testing requires:');
|
|
|
+ console.log(' 1. LibreOffice service running');
|
|
|
+ console.log(' 2. ImageMagick installed');
|
|
|
+ console.log(' 3. Vision model API Key configured');
|
|
|
+ console.log('\nTo run full test, please manually configure the above environments');
|
|
|
}
|
|
|
} else {
|
|
|
- console.log('⚠️ 未找到测试文件');
|
|
|
+ console.log('⚠️ Test files not found');
|
|
|
}
|
|
|
|
|
|
// 测试 4: KnowledgeBase 降级逻辑
|
|
|
- console.log('\n=== 测试 4: KnowledgeBase 降级逻辑 ===');
|
|
|
+ console.log('\n=== Test 4: KnowledgeBase degradation logic ===');
|
|
|
const kbService = app.get(KnowledgeBaseService);
|
|
|
|
|
|
- console.log('降级逻辑检查:');
|
|
|
- console.log('✅ 支持的格式: PDF, DOC, DOCX, PPT, PPTX');
|
|
|
- console.log('✅ 检查 Vision 模型配置');
|
|
|
- console.log('✅ 自动降级到快速模式');
|
|
|
- console.log('✅ 错误日志记录');
|
|
|
- console.log('✅ 临时文件清理');
|
|
|
+ console.log('Degradation logic check:');
|
|
|
+ console.log('✅ Supported formats: PDF, DOC, DOCX, PPT, PPTX');
|
|
|
+ console.log('✅ Check Vision model configuration');
|
|
|
+ console.log('✅ Auto-degrade to fast mode');
|
|
|
+ console.log('✅ Error logging');
|
|
|
+ console.log('✅ Temporary file cleanup');
|
|
|
|
|
|
// 测试 5: 环境配置验证
|
|
|
- console.log('\n=== 测试 5: 环境配置验证 ===');
|
|
|
+ console.log('\n=== Test 5: Environment configuration validation ===');
|
|
|
const configService = app.get(require('@nestjs/config').ConfigService);
|
|
|
|
|
|
const checks = [
|
|
|
@@ -117,55 +117,55 @@ async function testErrorHandling() {
|
|
|
const value = configService.get(check.name);
|
|
|
const passed = check.required ? !!value : true;
|
|
|
const status = passed ? '✅' : '❌';
|
|
|
- console.log(`${status} ${check.name}: ${value || '未配置'}`);
|
|
|
+ console.log(`${status} ${check.name}: ${value || 'Not configured'}`);
|
|
|
if (!passed) allPassed = false;
|
|
|
}
|
|
|
|
|
|
if (allPassed) {
|
|
|
- console.log('\n🎉 所有配置检查通过!');
|
|
|
+ console.log('\n🎉 All configuration checks passed!');
|
|
|
} else {
|
|
|
- console.log('\n⚠️ 请检查缺失的配置项');
|
|
|
+ console.log('\n⚠️ Please check missing configuration items');
|
|
|
}
|
|
|
|
|
|
// 测试 6: 临时文件清理机制
|
|
|
- console.log('\n=== 测试 6: 临时文件清理机制 ===');
|
|
|
+ console.log('\n=== Test 6: Temporary file cleanup mechanism ===');
|
|
|
try {
|
|
|
// 检查临时目录
|
|
|
const tempDir = configService.get('TEMP_DIR', './temp');
|
|
|
const tempExists = await fs.access(tempDir).then(() => true).catch(() => false);
|
|
|
|
|
|
if (tempExists) {
|
|
|
- console.log(`✅ 临时目录存在: ${tempDir}`);
|
|
|
+ console.log(`✅ Temporary directory exists: ${tempDir}`);
|
|
|
|
|
|
// 检查是否有遗留文件
|
|
|
const files = await fs.readdir(tempDir);
|
|
|
if (files.length > 0) {
|
|
|
- console.log(`⚠️ 发现 ${files.length} 个临时文件,建议清理`);
|
|
|
+ console.log(`⚠️ Found ${files.length} temporary files, cleanup recommended`);
|
|
|
} else {
|
|
|
- console.log('✅ 临时目录为空');
|
|
|
+ console.log('✅ Temporary directory is empty');
|
|
|
}
|
|
|
} else {
|
|
|
- console.log('⚠️ 临时目录不存在,将在首次运行时创建');
|
|
|
+ console.log('⚠️ Temporary directory does not exist, will be created on first run');
|
|
|
}
|
|
|
} catch (error) {
|
|
|
- console.log(`❌ 临时目录检查失败: ${error.message}`);
|
|
|
+ console.log(`❌ Temporary directory check failed: ${error.message}`);
|
|
|
}
|
|
|
|
|
|
- console.log('\n=== 错误处理测试总结 ===');
|
|
|
- console.log('✅ LibreOffice 连接错误处理');
|
|
|
- console.log('✅ PDF 转图片失败处理');
|
|
|
- console.log('✅ Vision 模型错误处理');
|
|
|
- console.log('✅ 自动降级到快速模式');
|
|
|
- console.log('✅ 临时文件清理');
|
|
|
- console.log('✅ 环境配置验证');
|
|
|
- console.log('\n💡 建议:');
|
|
|
- console.log(' 1. 在生产环境中添加更多监控');
|
|
|
- console.log(' 2. 实现用户配额限制');
|
|
|
- console.log(' 3. 添加处理超时机制');
|
|
|
- console.log(' 4. 定期清理临时文件');
|
|
|
+ console.log('\n=== Error Handling Test Summary ===');
|
|
|
+ console.log('✅ LibreOffice connection error handling');
|
|
|
+ console.log('✅ PDF to Image conversion failure handling');
|
|
|
+ console.log('✅ Vision model error handling');
|
|
|
+ console.log('✅ Auto-degrade to fast mode');
|
|
|
+ console.log('✅ Temporary file cleanup');
|
|
|
+ console.log('✅ Environment configuration validation');
|
|
|
+ console.log('\n💡 Suggestions:');
|
|
|
+ console.log(' 1. Add more monitoring in production environment');
|
|
|
+ console.log(' 2. Implement user quota limits');
|
|
|
+ console.log(' 3. Add processing timeout mechanism');
|
|
|
+ console.log(' 4. Regularly clean up temporary files');
|
|
|
|
|
|
} catch (error) {
|
|
|
- console.error('❌ 测试失败:', error.message);
|
|
|
+ console.error('❌ Test failed:', error.message);
|
|
|
console.error(error.stack);
|
|
|
} finally {
|
|
|
await app.close();
|