"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.orchestrateTest = void 0;
const uuid_1 = require("uuid");
const predicateStateMessage_1 = require("../../utils/predicateStateMessage");
const orchestrateTestCorrect_1 = require("./orchestrateTestCorrect");
const orchestrateTestScenario_1 = require("./orchestrateTestScenario");
const orchestrateTestWrite_1 = require("./orchestrateTestWrite");
const orchestrateTest = (ctx) => (props) => __awaiter(void 0, void 0, void 0, function* () {
    var _a, _b, _c, _d, _e, _f;
    // PREDICATION
    const start = new Date();
    const predicate = (0, predicateStateMessage_1.predicateStateMessage)(ctx.state(), "test");
    if (predicate !== null)
        return ctx.assistantMessage({
            type: "assistantMessage",
            id: (0, uuid_1.v7)(),
            created_at: start.toISOString(),
            text: predicate,
            completed_at: new Date().toISOString(),
        });
    ctx.dispatch({
        type: "testStart",
        id: (0, uuid_1.v7)(),
        created_at: start.toISOString(),
        reason: props.instruction,
        step: (_b = (_a = ctx.state().analyze) === null || _a === void 0 ? void 0 : _a.step) !== null && _b !== void 0 ? _b : 0,
    });
    // CHECK OPERATIONS
    const operations = (_d = (_c = ctx.state().interface) === null || _c === void 0 ? void 0 : _c.document.operations) !== null && _d !== void 0 ? _d : [];
    if (operations.length === 0)
        return ctx.assistantMessage({
            id: (0, uuid_1.v7)(),
            type: "assistantMessage",
            created_at: start.toISOString(),
            completed_at: new Date().toISOString(),
            text: "Unable to write test code because there are no Operations, " +
                "please check if the Interface agent is called.",
        });
    // PLAN
    const scenarios = yield (0, orchestrateTestScenario_1.orchestrateTestScenario)(ctx, props.instruction);
    if (scenarios.length === 0)
        throw new Error("No scenarios generated. Please check the logs.");
    // TEST CODE
    const written = yield (0, orchestrateTestWrite_1.orchestrateTestWrite)(ctx, {
        instruction: props.instruction,
        scenarios,
    });
    if (written.length === 0)
        throw new Error("No test code written. Please check the logs.");
    const corrects = yield (0, orchestrateTestCorrect_1.orchestrateTestCorrect)(ctx, {
        instruction: props.instruction,
        functions: written.map((w) => {
            var _a;
            return ({
                scenario: w.scenario,
                artifacts: w.artifacts,
                location: w.event.location,
                script: (_a = w.event.final) !== null && _a !== void 0 ? _a : w.event.draft,
            });
        }),
    });
    // DO COMPILE
    const compiler = yield ctx.compiler();
    const compileResult = yield compiler.typescript.compile({
        files: Object.fromEntries([
            ...Object.entries(yield ctx.files({
                dbms: "sqlite",
            })).filter(([key]) => key.endsWith(".ts")),
            ...corrects.map((s) => [s.file.location, s.file.content]),
        ]),
    });
    return ctx.dispatch({
        type: "testComplete",
        id: (0, uuid_1.v7)(),
        created_at: new Date().toISOString(),
        files: corrects.map((s) => s.file),
        compiled: compileResult,
        step: (_f = (_e = ctx.state().interface) === null || _e === void 0 ? void 0 : _e.step) !== null && _f !== void 0 ? _f : 0,
        elapsed: new Date().getTime() - start.getTime(),
    });
});
exports.orchestrateTest = orchestrateTest;
//# sourceMappingURL=orchestrateTest.js.map