#!/usr/bin/env tsx
"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 });
const completion_1 = require("../../../src/agents/tools/ycmd/tools/completion");
const diagnostics_1 = require("../../../src/agents/tools/ycmd/tools/diagnostics");
const goto_1 = require("../../../src/agents/tools/ycmd/tools/goto");
const refactor_1 = require("../../../src/agents/tools/ycmd/tools/refactor");
const signature_1 = require("../../../src/agents/tools/ycmd/tools/signature");
const serverManager_1 = require("../../../src/agents/tools/ycmd/serverManager");
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
async function testYcmdAutoStart() {
    console.log('🧪 Testing YCMD Auto-Start Functionality');
    console.log('='.repeat(50));
    const testFile = path.join(process.cwd(), 'test_auto_start.py');
    const testContent = `import os
import sys

def hello_world(name: str) -> str:
    """Greets a person by name."""
    return f"Hello, {name}!"

def calculate_sum(a: int, b: int) -> int:
    """Calculate the sum of two numbers."""
    return a + b

# Test completion and diagnostics
result = hello_world("
`;
    console.log('📝 Creating test file:', testFile);
    await fs.promises.writeFile(testFile, testContent);
    try {
        console.log('🛑 Stopping any existing ycmd server...');
        await serverManager_1.ycmdServerManager.stop();
        await new Promise(resolve => setTimeout(resolve, 1000));
        console.log('🔍 Verifying server is not running...');
        const initiallyRunning = await serverManager_1.ycmdServerManager.isRunning();
        console.log(`Server initially running: ${initiallyRunning}`);
        console.log('\n🚀 Test 1: Testing completion with auto-start...');
        const completionResult = await (0, completion_1.ycmdCompletion)({
            filepath: testFile,
            line: 12,
            column: 21,
            contents: testContent
        });
        console.log('Completion result:', JSON.stringify(completionResult, null, 2));
        console.log('\n🔧 Test 2: Testing diagnostics...');
        const diagnosticsResult = await (0, diagnostics_1.ycmdDiagnostics)({
            filepath: testFile,
            fileContents: testContent
        });
        console.log('Diagnostics result:', JSON.stringify(diagnosticsResult, null, 2));
        console.log('\n🎯 Test 3: Testing goto definition...');
        const gotoResult = await (0, goto_1.ycmdGoTo)({
            filepath: testFile,
            line: 12,
            column: 10,
            command: 'GoTo',
            contents: testContent
        });
        console.log('Goto result:', JSON.stringify(gotoResult, null, 2));
        console.log('\n📋 Test 4: Testing signature help...');
        const signatureResult = await (0, signature_1.ycmdSignatureHelp)({
            filepath: testFile,
            line: 12,
            column: 10,
            contents: testContent
        });
        console.log('Signature result:', JSON.stringify(signatureResult, null, 2));
        console.log('\n🔄 Test 5: Testing refactor (organize imports)...');
        const refactorResult = await (0, refactor_1.ycmdRefactor)({
            filepath: testFile,
            line: 1,
            column: 1,
            command: 'organize_imports',
            contents: testContent
        });
        console.log('Refactor result:', JSON.stringify(refactorResult, null, 2));
        console.log('\n✅ Verifying server is running after tests...');
        const finallyRunning = await serverManager_1.ycmdServerManager.isRunning();
        console.log(`Server finally running: ${finallyRunning}`);
        const serverInfo = serverManager_1.ycmdServerManager.getServerInfo();
        if (serverInfo) {
            console.log(`Server info: ${serverInfo.host}:${serverInfo.port} (${serverInfo.status})`);
        }
        console.log('\n🎉 Auto-start functionality test completed!');
        console.log('All tools should have automatically started the ycmd server when needed.');
        process.exit(0);
    }
    catch (error) {
        console.error('❌ Test failed:', error);
    }
    finally {
        console.log('\n🧹 Cleaning up...');
        try {
            await fs.promises.unlink(testFile);
            console.log('Test file removed');
        }
        catch (error) {
            console.warn('Failed to remove test file:', error);
        }
        await serverManager_1.ycmdServerManager.stop();
        console.log('Server stopped');
        process.exit(1);
    }
}
//# sourceMappingURL=test_ycmd_auto_start.js.map