"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const mcpMablTool_1 = require("./mcpMablTool");
const zod_1 = require("zod");
const mablApi_1 = require("@mablhq-internal/runtime/mablApi");
const run_cloud_1 = require("../../../tests/tests_cmds/run-cloud");
const common_1 = require("../utils/common");
const testUtils_1 = require("../utils/testUtils");
const constants_1 = require("../../../constants");
const runMablTestCloud = (0, mcpMablTool_1.defineTool)({
    schema: {
        name: 'run_mabl_test_cloud',
        title: 'Run a mabl test in the mabl cloud',
        description: 'Run a mabl test in the mabl cloud. Example: Run mabl test "Mabl Login" in application MyApplication in environment Development using the "Admin user" credential and the "Chrome" browser',
        inputSchema: zod_1.z.object({
            environmentId: zod_1.z
                .string()
                .describe('The id of the environment to run the test in, needs to be provided. If not provided, the user will be prompted to select from available environments.'),
            testId: zod_1.z.string().describe('The id of the test to run'),
            applicationId: zod_1.z
                .string()
                .describe('The id of the application to run the test in, needs to be provided. This parameter is required for cloud test execution. If not provided, the user will be prompted to select from available applications.'),
            browsers: zod_1.z
                .array(zod_1.z.enum(constants_1.BrowserTypeSelections))
                .describe('The browsers to run the test in, needs to be provided. If not provided, the user will be prompted to select from available browsers.'),
            credentialsId: zod_1.z
                .string()
                .describe('Optional. The id of the credentials to use for running the test. If not provided, the user will be prompted to select from available non-cloud credentials.')
                .optional(),
        }),
        type: 'destructive',
        outputSchema: zod_1.z.object({
            testRuns: zod_1.z.array(zod_1.z.object({
                id: zod_1.z.string().describe('The id of the test run'),
                browser: zod_1.z
                    .string()
                    .describe('The browser used to run the test')
                    .optional(),
                outputUrl: zod_1.z.string().describe('The output URL of the test run'),
            })),
        }),
    },
    handle: async (params, context) => {
        const { mablApiClient, workspaceId } = context.getCurrentContext();
        const test = await mablApiClient.getJourney(params.testId);
        if (test.test_type === mablApi_1.TestTypeEnum.Performance ||
            test.test_type === mablApi_1.TestTypeEnum.Mobile) {
            return (0, common_1.stringToToolResult)('Performance and mobile tests are not currently supported, please use mabl-cli to run these tests.', true);
        }
        if (!params.applicationId) {
            return (0, common_1.stringToToolResult)('applicationId is required when running a test in the cloud. Request the application explicitly.', true);
        }
        if (!params.environmentId) {
            return (0, common_1.stringToToolResult)('environmentId is required when running a test in the cloud. Request the environment explicitly.', true);
        }
        if (params.browsers?.length === 0 &&
            test.test_type === mablApi_1.TestTypeEnum.Browser) {
            return (0, common_1.stringToToolResult)(`At least one browser is required when running a browsertest in the cloud. The possible values are: ${constants_1.BrowserTypeSelections.join(', ')}`, true);
        }
        if (test.credentials_id && !params.credentialsId) {
            const error = await (0, testUtils_1.needCredentialsError)(mablApiClient, workspaceId);
            return (0, common_1.stringToToolResult)(error, true);
        }
        const testRun = await (0, run_cloud_1.executeRunCloudTest)(test, params.browsers, 'master', mablApiClient, undefined, undefined, undefined, params.credentialsId, params.applicationId, params.environmentId);
        if (!test.organization_id || !testRun[0]?.id) {
            return (0, common_1.stringToToolResult)('Failed to retrieve test run output URL due to missing organization_id or test run id.', true);
        }
        const testRuns = testRun.map((testRun) => ({
            id: testRun.id,
            browser: testRun.browser,
            outputUrl: (0, testUtils_1.getTestOutputUrl)(testRun.organization_id, testRun.id),
        }));
        return {
            content: [
                {
                    type: 'text',
                    text: JSON.stringify({ testRuns }, undefined, 2),
                },
            ],
            structuredContent: { testRuns },
        };
    },
});
exports.default = [runMablTestCloud];
