#!/usr/bin/env npx 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 index_1 = require("../../../src/agents/tools/ycmd/index");
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
async function comprehensiveTypeScriptTest() {
    console.log('\n=== Comprehensive TypeScript Test ===\n');
    const testFile = path.resolve('./test-typescript-comprehensive.ts');
    const testContent = `
// Test file for comprehensive TypeScript testing
import * as fs from 'fs';
import * as path from 'path';

interface User {
  id: number;
  name: string;
  email: string;
}

class UserManager {
  private users: User[] = [];

  addUser(user: User): void {
    this.users.push(user);
  }

  getUserById(id: number): User | undefined {
    return this.users.find(u => u.id === id);
  }

  getAllUsers(): User[] {
    return this.users;
  }
}

const manager = new UserManager();
manager.addUser({
  id: 1,
  name: 'John Doe',
  email: 'john@example.com'
});

// Test completions here
const user = manager.ge // Should complete to getUserById or getAllUsers
console.log(user);

// Test go-to-definition
const testPath = path.resolve('./test');
fs.readFileSync(testPath);
`.trim();
    try {
        fs.writeFileSync(testFile, testContent);
        console.log(`✅ Created test file: ${testFile}`);
        console.log('\n1. Starting ycmd server...');
        const server = await (0, index_1.ycmdStart)({});
        console.log(`✅ ycmd server started: ${JSON.stringify(server, null, 2)}`);
        console.log('\n2. Testing completions on method call (line 30, col 22)...');
        const completionResult = await (0, index_1.ycmdCompletion)({
            filepath: testFile,
            line: 30,
            column: 22,
            contents: testContent
        });
        console.log('Completion result:', JSON.stringify(completionResult, null, 2));
        if (completionResult.success && completionResult.completions && completionResult.completions.length > 0) {
            console.log('✅ Completions working! Found completions:');
            completionResult.completions.forEach((comp, idx) => {
                console.log(`  - ${comp.insertion_text || comp.text || comp.label}`);
            });
        }
        else if (completionResult.success) {
            console.log('⚠️  Completions working but no matches found (this may be expected)');
        }
        else {
            console.log('❌ Completions failed');
        }
        console.log('\n3. Testing go-to-definition on "path" import (line 3, col 15)...');
        const gotoResult = await (0, index_1.ycmdGoTo)({
            filepath: testFile,
            line: 3,
            column: 15,
            command: 'GoTo',
            contents: testContent
        });
        console.log('Go-to-definition result:', JSON.stringify(gotoResult, null, 2));
        if (gotoResult.success && gotoResult.locations && gotoResult.locations.length > 0) {
            console.log('✅ Go-to-definition working! Found locations:');
            gotoResult.locations.forEach((loc, idx) => {
                console.log(`  - ${loc.filepath}:${loc.line_num}:${loc.column_num}`);
            });
        }
        else if (gotoResult.success) {
            console.log('⚠️  Go-to-definition working but no locations found');
        }
        else {
            console.log('❌ Go-to-definition failed');
        }
        console.log('\n4. Testing completion on built-in fs module (line 33, col 3)...');
        const fsCompletionResult = await (0, index_1.ycmdCompletion)({
            filepath: testFile,
            line: 33,
            column: 3,
            contents: testContent
        });
        console.log('fs completion result:', JSON.stringify(fsCompletionResult, null, 2));
        if (fsCompletionResult.success && fsCompletionResult.completions && fsCompletionResult.completions.length > 0) {
            console.log('✅ fs module completions working! Found some completions.');
        }
        else if (fsCompletionResult.success) {
            console.log('⚠️  fs module completions working but no matches found');
        }
        else {
            console.log('❌ fs module completions failed');
        }
        console.log('\n=== Test Summary ===');
        const results = {
            serverStart: true,
            completions: completionResult.success,
            gotoDefinition: gotoResult.success,
            fsCompletions: fsCompletionResult.success
        };
        console.log('Results:', JSON.stringify(results, null, 2));
        const allPassed = Object.values(results).every(r => r === true);
        if (allPassed) {
            console.log('\n🎉 All tests passed! The "No Project" error has been successfully resolved.');
            console.log('✅ ycmd tools are now working properly with TypeScript files.');
        }
        else {
            console.log('\n⚠️  Some tests had issues, but the "No Project" error appears to be resolved.');
        }
    }
    catch (error) {
        console.error('❌ Test failed:', error);
        if (error instanceof Error && error.message.includes('No Project')) {
            console.log('\n❌ The "No Project" error is still occurring!');
        }
        else {
            console.log('\n✅ No "No Project" error detected, but other issues occurred.');
        }
    }
    finally {
        try {
            if (fs.existsSync(testFile)) {
                fs.unlinkSync(testFile);
                console.log(`\n🧹 Cleaned up test file: ${testFile}`);
            }
        }
        catch (err) {
            console.log('Could not clean up test file:', err);
        }
    }
}
process.on('SIGINT', () => {
    console.log('\nForce killing ycmd server process on parent exit');
    process.exit(0);
});
process.on('SIGTERM', () => {
    console.log('\nForce killing ycmd server process on parent exit');
    process.exit(0);
});
comprehensiveTypeScriptTest().catch(console.error);
//# sourceMappingURL=test_comprehensive_typescript.js.map