"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const _8_10_1 = require("../../../fixtures/8.10");
const UtilitiesPage_1 = require("../../../pages/8.10/UtilitiesPage");
const _setup_1 = require("../../../test-setup.js");
const apiHelpers_1 = require("../../../utils/apiHelpers");
const incidentHelpers_1 = require("../../../utils/incidentHelpers");
const users_1 = require("../../../utils/users");
const test_1 = require("@playwright/test");
const testUser = (0, users_1.getTestUser)('fifteenthUser');
let authToken;
let toolsInstanceKey;
_8_10_1.test.describe.configure({ mode: 'parallel' });
_8_10_1.test.describe('Orchestration Cluster MCP Server - Incident Tools @tasklistV2', () => {
    const clusterName = 'Agentic Orchestration Cluster';
    _8_10_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_incident_test_file.bpmn', authToken, 'saas', 'agentic_cluster'),
            (0, apiHelpers_1.deployProcess)('./resources/mcp_server_saas/mcp_incident_tools.bpmn', authToken, 'saas', 'agentic_cluster'),
        ]);
        if (testFileProcessKey == null || toolsProcessKey == null) {
            throw new Error('Failed to deploy incident tools processes');
        }
        // Create test instance — the call activity in the BPMN references a
        // non-existent process, so Zeebe raises a CALLED_ELEMENT_ERROR incident
        // immediately without needing a worker.
        const testInstanceKey = await (0, apiHelpers_1.createProcessInstance)(String(testFileProcessKey), authToken, 'saas', 'agentic_cluster');
        // Get the incident key created by the test instance (with retry logic)
        const testIncidentKey = await (0, incidentHelpers_1.getIncidentKeyByProcessInstanceKey)(testInstanceKey, authToken, 'saas', 'agentic_cluster');
        console.log('Creating tools instance with variables:', {
            testFileProcessKey,
            testIncidentKey,
        });
        // Create tools instance with required variables
        toolsInstanceKey = await (0, apiHelpers_1.createProcessInstance)(String(toolsProcessKey), authToken, 'saas', 'agentic_cluster', { testFileProcessKey, testIncidentKey });
    });
    _8_10_1.test.beforeEach(async ({ page, loginPage }, testInfo) => {
        await (0, UtilitiesPage_1.loginWithRetry)(page, loginPage, testUser, (testInfo.workerIndex + 1) * 1000);
    });
    _8_10_1.test.afterEach(async ({ page }, testInfo) => {
        await (0, _setup_1.captureScreenshot)(page, testInfo);
        await (0, _setup_1.captureFailureVideo)(page, testInfo);
    });
    const incidentToolTests = [
        {
            name: 'searchIncidents - search incidents with filters such as processDefinitionKey',
            variable: 'mcpResultSearchIncidents',
            assertions: [
                {
                    text: 'totalItems',
                    description: 'search response contains totalItems field',
                },
                {
                    text: 'incidentKey',
                    description: 'search response contains incidentKey field',
                },
                {
                    text: 'processDefinitionKey',
                    description: 'search response contains processDefinitionKey field',
                },
                {
                    text: 'state',
                    description: 'search response contains state field',
                },
            ],
        },
        {
            name: 'getIncident - retrieve an incident by key',
            variable: 'mcpResultGetIncident',
            assertions: [
                {
                    text: 'incidentKey',
                    description: 'response contains incidentKey field',
                },
                {
                    text: 'processInstanceKey',
                    description: 'response contains processInstanceKey field',
                },
                {
                    text: 'errorType',
                    description: 'response contains errorType field',
                },
                {
                    text: 'state',
                    description: 'response contains state field',
                },
            ],
        },
        {
            name: 'resolveIncident - resolve an incident by key',
            variable: 'mcpResultResolveIncident',
            assertions: [
                {
                    text: 'resolved',
                    description: 'response confirms incident was resolved',
                },
            ],
        },
    ];
    for (const testCase of incidentToolTests) {
        (0, _8_10_1.test)(testCase.name, async ({ homePage, appsPage, operateHomePage, operateProcessesPage, operateProcessInstancePage, }) => {
            _8_10_1.test.slow();
            await _8_10_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 Incident Tools');
                await operateProcessInstancePage.assertProcessCompleteStatusWithRetry();
            });
            for (const assertion of testCase.assertions) {
                await _8_10_1.test.step(assertion.description, async () => {
                    await operateProcessInstancePage.assertProcessVariableContainsText(testCase.variable, assertion.text);
                });
            }
        });
    }
});
