#!/usr/bin/env node

const fs = require('fs');
const path = require('path');
const { removeConsoleWithOllama, checkOllamaHealth } = require('./ollama-console-remover.js');

// 测试用的压缩JS文件（包含console语句）
const testJsCode = `
function testFunction(){console.log("debug info");const data={name:"test",value:123};console.error("error occurred");return data;}console.warn("warning message");class TestClass{constructor(){console.debug("constructor called");this.id=Math.random();}getId(){console.info("getting id");return this.id;}}module.exports={TestClass,testFunction};
`;

async function runTest() {
    console.log('🧪 Testing Ollama console remover...');
    console.log('');

    // 1. 检查Ollama服务
    console.log('1️⃣ Checking Ollama service...');
    const isAvailable = await checkOllamaHealth();
    console.log(`   Status: ${isAvailable ? '✅ Available' : '❌ Not available'}`);
    
    if (!isAvailable) {
        console.log('   Please make sure Ollama is running with:');
        console.log('   - Ollama installed: https://ollama.com/');
        console.log('   - Ollama running: ollama serve');
        console.log('   - Model pulled: ollama pull deepseek-coder:6.7b');
        console.log('');
        console.log('   Using local fallback method for test...');
        
        // 使用本地备用方法
        const { removeConsoleFallback } = require('./ollama-console-remover.js');
        const cleanedCode = removeConsoleFallback(testJsCode);
        
        console.log('');
        console.log('📝 Original code:');
        console.log(testJsCode);
        console.log('');
        console.log('🧹 Cleaned code (local method):');
        console.log(cleanedCode);
        console.log('');
        console.log(`📊 Size reduction: ${testJsCode.length - cleanedCode.length} characters`);
        
        return;
    }

    // 2. 测试Ollama处理
    console.log('2️⃣ Testing with Ollama...');
    try {
        const cleanedCode = await removeConsoleWithOllama(testJsCode);
        
        console.log('📝 Original code:');
        console.log(testJsCode);
        console.log('');
        console.log('🧹 Cleaned code (Ollama):');
        console.log(cleanedCode);
        console.log('');
        console.log(`📊 Size reduction: ${testJsCode.length - cleanedCode.length} characters`);
        console.log('');
        console.log('✅ Test completed successfully!');
        
        // 保存测试结果
        const testResults = {
            original: testJsCode,
            cleaned: cleanedCode,
            reduction: testJsCode.length - cleanedCode.length,
            timestamp: new Date().toISOString()
        };
        
        fs.writeFileSync('test-results.json', JSON.stringify(testResults, null, 2));
        console.log('💾 Test results saved to test-results.json');
        
    } catch (error) {
        console.error('❌ Error testing Ollama:', error.message);
        console.log('');
        console.log('🔧 Troubleshooting tips:');
        console.log('1. Make sure Ollama is running: ollama serve');
        console.log('2. Make sure the model is pulled: ollama pull deepseek-coder:6.7b');
        console.log('3. Check if Ollama is accessible at http://localhost:11434');
        console.log('4. Try running: curl http://localhost:11434/api/tags');
    }
}

// 运行测试
runTest().catch(console.error);