test-vision-pipeline.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /**
  2. * Vision Pipeline 端到端测试脚本
  3. *
  4. * 测试流程:
  5. * 1. LibreOffice 文档转换
  6. * 2. PDF 转图片
  7. * 3. Vision 模型分析
  8. * 4. 完整流程集成
  9. */
  10. import { NestFactory } from '@nestjs/core';
  11. import { AppModule } from './src/app.module';
  12. import { VisionPipelineService } from './src/vision-pipeline/vision-pipeline.service';
  13. import { LibreOfficeService } from './src/libreoffice/libreoffice.service';
  14. import { Pdf2ImageService } from './src/pdf2image/pdf2image.service';
  15. import { VisionService } from './src/vision/vision.service';
  16. import * as fs from 'fs/promises';
  17. import * as path from 'path';
  18. async function testVisionPipeline() {
  19. console.log('🚀 开始 Vision Pipeline 端到端测试\n');
  20. // 初始化 Nest 应用
  21. const app = await NestFactory.createApplicationContext(AppModule, {
  22. logger: ['error', 'warn', 'log'],
  23. });
  24. try {
  25. // 1. 测试 LibreOffice 服务
  26. console.log('=== 测试 1: LibreOffice 服务 ===');
  27. const libreOffice = app.get(LibreOfficeService);
  28. // 检查健康状态
  29. const isHealthy = await libreOffice.healthCheck();
  30. console.log(`LibreOffice 健康检查: ${isHealthy ? '✅ 通过' : '❌ 失败'}`);
  31. if (!isHealthy) {
  32. console.log('⚠️ LibreOffice 服务未运行,跳过后续测试');
  33. return;
  34. }
  35. // 2. 测试 PDF 转图片服务
  36. console.log('\n=== 测试 2: PDF 转图片服务 ===');
  37. const pdf2Image = app.get(Pdf2ImageService);
  38. const testPdf = '/home/fzxs/workspaces/demo/simple-kb/uploads/file-1766236004300-577549403.pdf';
  39. if (await fs.access(testPdf).then(() => true).catch(() => false)) {
  40. console.log(`测试 PDF: ${path.basename(testPdf)}`);
  41. const result = await pdf2Image.convertToImages(testPdf, {
  42. density: 150, // 降低密度以加快测试
  43. quality: 75,
  44. format: 'jpeg',
  45. });
  46. console.log(`✅ 转换成功: ${result.images.length}/${result.totalPages} 页`);
  47. console.log(` 成功: ${result.successCount}, 失败: ${result.failedCount}`);
  48. // 清理测试文件
  49. await pdf2Image.cleanupImages(result.images);
  50. console.log('✅ 临时文件已清理');
  51. } else {
  52. console.log('⚠️ 测试 PDF 文件不存在,跳过此测试');
  53. }
  54. // 3. 测试 Vision Pipeline 完整流程
  55. console.log('\n=== 测试 3: Vision Pipeline 完整流程 ===');
  56. const visionPipeline = app.get(VisionPipelineService);
  57. // 检查是否有支持的测试文件
  58. const testFiles = [
  59. '/home/fzxs/workspaces/demo/simple-kb/uploads/file-1766236004300-577549403.pdf',
  60. '/home/fzxs/workspaces/demo/simple-kb/uploads/file-1765705143480-947461268.pdf',
  61. ];
  62. let testFile: string | null = null;
  63. for (const file of testFiles) {
  64. if (await fs.access(file).then(() => true).catch(() => false)) {
  65. testFile = file;
  66. break;
  67. }
  68. }
  69. if (testFile) {
  70. console.log(`测试文件: ${path.basename(testFile)}`);
  71. // 模式推荐测试
  72. const recommendation = await visionPipeline.recommendMode(testFile);
  73. console.log(`模式推荐: ${recommendation.recommendedMode}`);
  74. console.log(`原因: ${recommendation.reason}`);
  75. if (recommendation.estimatedCost) {
  76. console.log(`预估成本: $${recommendation.estimatedCost.toFixed(2)}`);
  77. }
  78. if (recommendation.estimatedTime) {
  79. console.log(`预估时间: ${recommendation.estimatedTime.toFixed(1)}s`);
  80. }
  81. if (recommendation.warnings && recommendation.warnings.length > 0) {
  82. console.log(`警告: ${recommendation.warnings.join(', ')}`);
  83. }
  84. // 注意:完整流程测试需要配置 Vision 模型 API Key
  85. // 这里只测试流程结构
  86. console.log('\n✅ Vision Pipeline 模块已正确配置');
  87. console.log(' 注意: 完整流程测试需要配置有效的 Vision 模型 API Key');
  88. } else {
  89. console.log('⚠️ 未找到测试文件,跳过完整流程测试');
  90. }
  91. // 4. 检查环境配置
  92. console.log('\n=== 测试 4: 环境配置检查 ===');
  93. const configService = app.get(require('@nestjs/config').ConfigService);
  94. const requiredEnvVars = [
  95. 'LIBREOFFICE_URL',
  96. 'TEMP_DIR',
  97. 'ELASTICSEARCH_HOST',
  98. 'TIKA_HOST',
  99. ];
  100. for (const envVar of requiredEnvVars) {
  101. const value = configService.get(envVar);
  102. if (value) {
  103. console.log(`✅ ${envVar}: ${value}`);
  104. } else {
  105. console.log(`❌ ${envVar}: 未配置`);
  106. }
  107. }
  108. console.log('\n🎉 所有基础测试完成!');
  109. } catch (error) {
  110. console.error('❌ 测试失败:', error.message);
  111. console.error(error.stack);
  112. } finally {
  113. await app.close();
  114. }
  115. }
  116. // 运行测试
  117. if (require.main === module) {
  118. testVisionPipeline().catch(console.error);
  119. }
  120. export { testVisionPipeline };