"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.testVisionPipeline = testVisionPipeline;
const core_1 = require("@nestjs/core");
const app_module_1 = require("./src/app.module");
const vision_pipeline_service_1 = require("./src/vision-pipeline/vision-pipeline.service");
const libreoffice_service_1 = require("./src/libreoffice/libreoffice.service");
const pdf2image_service_1 = require("./src/pdf2image/pdf2image.service");
const fs = __importStar(require("fs/promises"));
const path = __importStar(require("path"));
async function testVisionPipeline() {
    console.log('🚀 Starting Vision Pipeline end-to-end test\n');
    const app = await core_1.NestFactory.createApplicationContext(app_module_1.AppModule, {
        logger: ['error', 'warn', 'log'],
    });
    try {
        console.log('=== Test 1: LibreOffice service ===');
        const libreOffice = app.get(libreoffice_service_1.LibreOfficeService);
        const isHealthy = await libreOffice.healthCheck();
        console.log(`LibreOffice health check: ${isHealthy ? '✅ Passed' : '❌ Failed'}`);
        if (!isHealthy) {
            console.log('⚠️  LibreOffice service not running, skipping subsequent tests');
            return;
        }
        console.log('\n=== Test 2: PDF to Image service ===');
        const pdf2Image = app.get(pdf2image_service_1.Pdf2ImageService);
        const testPdf = '/home/fzxs/workspaces/demo/simple-kb/uploads/file-1766236004300-577549403.pdf';
        if (await fs.access(testPdf).then(() => true).catch(() => false)) {
            console.log(`Test PDF: ${path.basename(testPdf)}`);
            const result = await pdf2Image.convertToImages(testPdf, {
                density: 150,
                quality: 75,
                format: 'jpeg',
            });
            console.log(`✅ Conversion successful: ${result.images.length}/${result.totalPages} pages`);
            console.log(`   Success: ${result.successCount}, Failed: ${result.failedCount}`);
            await pdf2Image.cleanupImages(result.images);
            console.log('✅ Temporary files cleaned up');
        }
        else {
            console.log('⚠️  Test PDF file does not exist, skipping this test');
        }
        console.log('\n=== Test 3: Vision Pipeline complete flow ===');
        const visionPipeline = app.get(vision_pipeline_service_1.VisionPipelineService);
        const testFiles = [
            '/home/fzxs/workspaces/demo/simple-kb/uploads/file-1766236004300-577549403.pdf',
            '/home/fzxs/workspaces/demo/simple-kb/uploads/file-1765705143480-947461268.pdf',
        ];
        let testFile = null;
        for (const file of testFiles) {
            if (await fs.access(file).then(() => true).catch(() => false)) {
                testFile = file;
                break;
            }
        }
        if (testFile) {
            console.log(`Test file: ${path.basename(testFile)}`);
            const recommendation = await visionPipeline.recommendMode(testFile);
            console.log(`Recommended mode: ${recommendation.recommendedMode}`);
            console.log(`Reason: ${recommendation.reason}`);
            if (recommendation.estimatedCost) {
                console.log(`Estimated cost: $${recommendation.estimatedCost.toFixed(2)}`);
            }
            if (recommendation.estimatedTime) {
                console.log(`Estimated time: ${recommendation.estimatedTime.toFixed(1)}s`);
            }
            if (recommendation.warnings && recommendation.warnings.length > 0) {
                console.log(`Warnings: ${recommendation.warnings.join(', ')}`);
            }
            console.log('\n✅ Vision Pipeline module correctly configured');
            console.log('   Note: Full flow testing requires a valid Vision model API Key');
        }
        else {
            console.log('⚠️  Test files not found, skipping complete flow test');
        }
        console.log('\n=== Test 4: Environment configuration check ===');
        const configService = app.get(require('@nestjs/config').ConfigService);
        const requiredEnvVars = [
            'LIBREOFFICE_URL',
            'TEMP_DIR',
            'ELASTICSEARCH_HOST',
            'TIKA_HOST',
        ];
        for (const envVar of requiredEnvVars) {
            const value = configService.get(envVar);
            if (value) {
                console.log(`✅ ${envVar}: ${value}`);
            }
            else {
                console.log(`❌ ${envVar}: Not configured`);
            }
        }
        console.log('\n🎉 All basic tests completed!');
    }
    catch (error) {
        console.error('❌ Test failed:', error.message);
        console.error(error.stack);
    }
    finally {
        await app.close();
    }
}
if (require.main === module) {
    testVisionPipeline().catch(console.error);
}
//# sourceMappingURL=test-vision-pipeline.js.map