"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const _8_9_1 = require("../../../fixtures/8.9");
const UtilitiesPage_1 = require("../../../pages/8.9/UtilitiesPage");
const _setup_1 = require("../../../test-setup.js");
const apiHelpers_1 = require("../../../utils/apiHelpers");
const users_1 = require("../../../utils/users");
const test_1 = require("@playwright/test");
const testUser = (0, users_1.getTestUser)('fourteenthUser');
let authToken;
let toolsInstanceKey;
_8_9_1.test.describe.configure({ mode: 'parallel' });
_8_9_1.test.describe('Orchestration Cluster MCP Server - Process Instance Tools @tasklistV2', () => {
    const clusterName = 'Agentic Orchestration Cluster';
    _8_9_1.test.beforeAll(async () => {
        authToken = await (0, apiHelpers_1.authSaasAPI)(undefined, 'agentic_cluster');
        const [testFileProcessKey, toolsProcessKey] = await Promise.all([
            (0, apiHelpers_1.deployProcess)('./resources/mcp_server_saas/mcp_process_instance_test_file.bpmn', authToken, 'saas', 'agentic_cluster'),
            (0, apiHelpers_1.deployProcess)('./resources/mcp_server_saas/mcp_process_instance_tools.bpmn', authToken, 'saas', 'agentic_cluster'),
        ]);
        if (testFileProcessKey == null || toolsProcessKey == null) {
            throw new Error('Failed to deploy process instance tools processes');
        }
        const testInstanceKey = await (0, apiHelpers_1.createProcessInstance)(String(testFileProcessKey), authToken, 'saas', 'agentic_cluster');
        // Wait for the test instance to be exported to secondary storage before the
        // tools instance queries it via the getProcessInstance MCP tool. That tool
        // wraps GET /v2/process-instances/{key}, which is eventually consistent and
        // returns 404 until the Camunda Exporter syncs — a fixed sleep is not enough.
        await (0, apiHelpers_1.waitForProcessInstanceVisible)(String(testInstanceKey), authToken, 'saas', 'agentic_cluster');
        toolsInstanceKey = await (0, apiHelpers_1.createProcessInstance)(String(toolsProcessKey), authToken, 'saas', 'agentic_cluster', { testFileProcessKey, testInstanceKey });
    });
    _8_9_1.test.beforeEach(async ({ page, loginPage }, testInfo) => {
        await (0, UtilitiesPage_1.loginWithRetry)(page, loginPage, testUser, (testInfo.workerIndex + 1) * 1000);
    });
    _8_9_1.test.afterEach(async ({ page }, testInfo) => {
        await (0, _setup_1.captureScreenshot)(page, testInfo);
        await (0, _setup_1.captureFailureVideo)(page, testInfo);
    });
    const processInstanceToolTests = [
        {
            name: 'searchProcessInstances - search process instances with filters such as processDefinitionKey',
            variable: 'mcpResultSearchProcessInstances',
            assertions: [
                {
                    text: 'totalItems',
                    description: 'search response contains totalItems field',
                },
                {
                    text: 'processInstanceKey',
                    description: 'search response contains processInstanceKey field',
                },
                {
                    text: 'processDefinitionKey',
                    description: 'search response contains processDefinitionKey field',
                },
            ],
        },
        {
            name: 'getProcessInstance - retrieve a process instance by key',
            variable: 'mcpResultGetProcessInstance',
            assertions: [
                {
                    text: 'processInstanceKey',
                    description: 'process instance response contains processInstanceKey',
                },
                {
                    text: 'mcp_process_instance_test_file',
                    description: 'process instance response contains correct process name',
                },
                {
                    text: 'state',
                    description: 'process instance response contains state field',
                },
            ],
        },
        {
            name: 'createProcessInstance - create a new process instance with variables',
            variable: 'mcpResultCreateProcessInstance',
            assertions: [
                {
                    text: 'processInstanceKey',
                    description: 'create response contains processInstanceKey',
                },
                {
                    text: 'processDefinitionKey',
                    description: 'create response contains processDefinitionKey',
                },
            ],
        },
    ];
    for (const testCase of processInstanceToolTests) {
        (0, _8_9_1.test)(testCase.name, async ({ homePage, appsPage, operateHomePage, operateProcessesPage, operateProcessInstancePage, }) => {
            _8_9_1.test.slow();
            await _8_9_1.test.step('Navigate to completed process in Operate', async () => {
                await homePage.clickClusters();
                await appsPage.clickOperate(clusterName);
                await (0, test_1.expect)(operateHomePage.operateBanner).toBeVisible({
                    timeout: 60000,
                });
                await operateHomePage.clickProcessesTab();
                await operateProcessesPage.clickProcessCompletedCheckbox();
                await operateProcessesPage.applyMoreFilters('Process Instance Key(s)', String(toolsInstanceKey));
                await operateProcessesPage.clickProcessInstanceLink('MCP Process Instance Tools');
                await operateProcessInstancePage.assertProcessCompleteStatusWithRetry();
            });
            for (const assertion of testCase.assertions) {
                await _8_9_1.test.step(assertion.description, async () => {
                    await operateProcessInstancePage.assertProcessVariableContainsText(testCase.variable, assertion.text);
                });
            }
        });
    }
});
