#!/usr/bin/env node
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const events_1 = require("events");
const mockMessage = { role: "user", content: "test" };
const mockContext = {
    Tools: {
        getTools: () => [],
        callTool: async (name, params) => ({ name, content: "mock result" })
    },
    Events: { registerAgent: () => { } },
    messageProcessor: { process: async () => "processed" }
};
class MockBaseAgent extends events_1.EventEmitter {
    name = "MockAgent";
    agentEvents = new events_1.EventEmitter();
    eventTypes = { done: "done" };
    constructor(context) {
        super();
    }
    async call(userInput) {
        console.log(`🤖 Agent processing: "${userInput}"`);
        await new Promise(resolve => setTimeout(resolve, 50));
        const result = "Task completed successfully";
        setTimeout(() => {
            console.log("📡 Emitting done event...");
            this.agentEvents.emit(this.eventTypes.done, result);
        }, 10);
        return result;
    }
}
async function runTests() {
    console.log("🧪 Starting Agent Event Handler Reliability Tests\n");
    let passedTests = 0;
    let totalTests = 0;
    try {
        totalTests++;
        console.log("Test 1: Normal event handler flow");
        const agent = new MockBaseAgent(mockContext);
        let eventFired = false;
        let eventMessage = "";
        const eventPromise = new Promise((resolve, reject) => {
            const timeout = setTimeout(() => {
                reject(new Error("Event handler timeout"));
            }, 1000);
            agent.agentEvents.once(agent.eventTypes.done, (message) => {
                clearTimeout(timeout);
                console.log("✅ Agent has finished.");
                eventFired = true;
                eventMessage = message;
                resolve();
            });
        });
        const result = await agent.call("Complete normal test");
        await eventPromise;
        if (eventFired && eventMessage === result) {
            console.log("✅ Test 1 PASSED - Event handler fired correctly\n");
            passedTests++;
        }
        else {
            console.log("❌ Test 1 FAILED - Event handler didn't fire properly\n");
        }
    }
    catch (error) {
        console.log(`❌ Test 1 FAILED - ${error.message}\n`);
    }
    try {
        totalTests++;
        console.log("Test 2: Race condition - late event handler");
        const agent = new MockBaseAgent(mockContext);
        const agentPromise = agent.call("Race condition test");
        await new Promise(resolve => setTimeout(resolve, 100));
        let eventFired = false;
        const lateEventPromise = new Promise((resolve, reject) => {
            const timeout = setTimeout(() => {
                reject(new Error("Late event handler timeout - demonstrates the bug"));
            }, 500);
            agent.agentEvents.once(agent.eventTypes.done, (message) => {
                clearTimeout(timeout);
                console.log("✅ Agent has finished (late handler).");
                eventFired = true;
                resolve();
            });
        });
        const result = await agentPromise;
        try {
            await lateEventPromise;
            console.log("✅ Test 2 PASSED - Late event handler worked (no race condition)\n");
            passedTests++;
        }
        catch (error) {
            console.log("❌ Test 2 DEMONSTRATED BUG - Race condition detected!");
            console.log("   This shows the event handler was registered too late\n");
        }
    }
    catch (error) {
        console.log(`❌ Test 2 ERROR - ${error.message}\n`);
    }
    try {
        totalTests++;
        console.log("Test 3: Multiple event handlers");
        const agent = new MockBaseAgent(mockContext);
        let handler1Fired = false;
        let handler2Fired = false;
        const promise1 = new Promise((resolve) => {
            agent.agentEvents.once(agent.eventTypes.done, () => {
                console.log("✅ Handler 1: Agent has finished.");
                handler1Fired = true;
                resolve();
            });
        });
        const promise2 = new Promise((resolve) => {
            agent.agentEvents.once(agent.eventTypes.done, () => {
                console.log("✅ Handler 2: Agent has completed the task.");
                handler2Fired = true;
                resolve();
            });
        });
        const result = await agent.call("Multiple handlers test");
        await Promise.race([
            Promise.all([promise1, promise2]),
            new Promise((_, reject) => setTimeout(() => reject(new Error("Multiple handlers timeout")), 1000))
        ]);
        if (handler1Fired && handler2Fired) {
            console.log("✅ Test 3 PASSED - All event handlers fired\n");
            passedTests++;
        }
        else {
            console.log("❌ Test 3 FAILED - Not all handlers fired\n");
        }
    }
    catch (error) {
        console.log(`❌ Test 3 FAILED - ${error.message}\n`);
    }
    console.log("📊 Test Results:");
    console.log(`   Passed: ${passedTests}/${totalTests} tests`);
    if (passedTests < totalTests) {
        console.log("\n🔍 Event Handler Issues Detected:");
        console.log("   - Race conditions when handlers are registered late");
        console.log("   - Timing issues in event emission");
        console.log("   - This demonstrates the reliability issues in AgentModule.ts");
    }
    else {
        console.log("\n✅ All tests passed - Event handlers are working reliably");
    }
}
runTests().catch(console.error);
//# sourceMappingURL=run-test.js.map