test-vision-pipeline.ts 4.8 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('🚀 Starting Vision Pipeline end-to-end test\n');
  20. // 初始化 Nest 应用
  21. const app = await NestFactory.createApplicationContext(AppModule, {
  22. logger: ['error', 'warn', 'log'],
  23. });
  24. try {
  25. // 1. 测试 LibreOffice 服务
  26. console.log('=== Test 1: LibreOffice service ===');
  27. const libreOffice = app.get(LibreOfficeService);
  28. // 检查健康状态
  29. const isHealthy = await libreOffice.healthCheck();
  30. console.log(`LibreOffice health check: ${isHealthy ? '✅ Passed' : '❌ Failed'}`);
  31. if (!isHealthy) {
  32. console.log('⚠️ LibreOffice service not running, skipping subsequent tests');
  33. return;
  34. }
  35. // 2. 测试 PDF 转图片服务
  36. console.log('\n=== Test 2: PDF to Image service ===');
  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(`Test PDF: ${path.basename(testPdf)}`);
  41. const result = await pdf2Image.convertToImages(testPdf, {
  42. density: 150, // 降低密度以加快测试
  43. quality: 75,
  44. format: 'jpeg',
  45. });
  46. console.log(`✅ Conversion successful: ${result.images.length}/${result.totalPages} pages`);
  47. console.log(` Success: ${result.successCount}, Failed: ${result.failedCount}`);
  48. // 清理测试文件
  49. await pdf2Image.cleanupImages(result.images);
  50. console.log('✅ Temporary files cleaned up');
  51. } else {
  52. console.log('⚠️ Test PDF file does not exist, skipping this test');
  53. }
  54. // 3. 测试 Vision Pipeline 完整流程
  55. console.log('\n=== Test 3: Vision Pipeline complete flow ===');
  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(`Test file: ${path.basename(testFile)}`);
  71. // 模式推荐测试
  72. const recommendation = await visionPipeline.recommendMode(testFile);
  73. console.log(`Recommended mode: ${recommendation.recommendedMode}`);
  74. console.log(`Reason: ${recommendation.reason}`);
  75. if (recommendation.estimatedCost) {
  76. console.log(`Estimated cost: $${recommendation.estimatedCost.toFixed(2)}`);
  77. }
  78. if (recommendation.estimatedTime) {
  79. console.log(`Estimated time: ${recommendation.estimatedTime.toFixed(1)}s`);
  80. }
  81. if (recommendation.warnings && recommendation.warnings.length > 0) {
  82. console.log(`Warnings: ${recommendation.warnings.join(', ')}`);
  83. }
  84. // 注意:完整流程测试需要配置 Vision 模型 API Key
  85. // 这里只测试流程结构
  86. console.log('\n✅ Vision Pipeline module correctly configured');
  87. console.log(' Note: Full flow testing requires a valid Vision model API Key');
  88. } else {
  89. console.log('⚠️ Test files not found, skipping complete flow test');
  90. }
  91. // 4. 检查环境配置
  92. console.log('\n=== Test 4: Environment configuration check ===');
  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}: Not configured`);
  106. }
  107. }
  108. console.log('\n🎉 All basic tests completed!');
  109. } catch (error) {
  110. console.error('❌ Test failed:', 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 };