import { AgentOutputType, ChatMessageBuilder } from '../agent-core/index.js';
import { AgentFunctionBase } from './utils/index.js';
const runTest = async (context) => {
    try {
        const result = await context.workspace.exec('poetry run pytest -v', undefined, 10000);
        return result;
    }
    catch (e) {
        return {
            exitCode: 1,
            stdout: `Error thrown from tests: ${e.message}`,
            stderr: '',
        };
    }
};
const extractErrors = (errorsMessage) => {
    const pattern = /(?<=^={3,} (FAILURES|ERRORS) ={3,}\n)[\s\S]*?(?=\n^={3,} short test summary info)/gm;
    const match = errorsMessage.match(pattern);
    if (match) {
        return `Errors found in testing: \n ${match[0]}`;
    }
    return 'Could not find errors in error message';
};
export class RunPytest extends AgentFunctionBase {
    constructor() {
        super();
    }
    get name() {
        return 'runPytest';
    }
    get description() {
        return 'Run python test and returns error';
    }
    get parameters() {
        return {
            type: 'object',
            properties: {
                filename: {},
            },
            additionalProperties: false,
        };
    }
    buildExecutor({ context }) {
        return async (params, rawParams) => {
            const response = await runTest(context);
            if (response.exitCode === 0) {
                return {
                    outputs: [
                        {
                            type: AgentOutputType.Success,
                            title: 'Succesfully ran test',
                        },
                    ],
                    messages: [
                        ChatMessageBuilder.functionCall(this.name, params),
                        ChatMessageBuilder.functionCallResult(this.name, 'Succesfully ran test'),
                    ],
                };
            }
            if (response.stdout === 'Timeout achieved') {
                return {
                    outputs: [
                        {
                            type: AgentOutputType.Error,
                            title: 'Error running tests',
                        },
                    ],
                    messages: [
                        ChatMessageBuilder.functionCall(this.name, params),
                        ChatMessageBuilder.functionCallResult(this.name, "Test have reached timeout. Maybe there's an infinite loop in the code. Please review and fix"),
                    ],
                };
            }
            const errorMessages = extractErrors(response.stdout);
            return {
                outputs: [
                    {
                        type: AgentOutputType.Error,
                        title: 'Error running tests',
                    },
                ],
                messages: [ChatMessageBuilder.functionCall(this.name, params), ChatMessageBuilder.functionCallResult(this.name, errorMessages)],
            };
        };
    }
}
