"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 variableHelpers_1 = require("../../../utils/variableHelpers");
const users_1 = require("../../../utils/users");
const test_1 = require("@playwright/test");
const testUser = (0, users_1.getTestUser)('sixteenthUser');
let authToken;
let toolsInstanceKey;
_8_9_1.test.describe.configure({ mode: 'parallel' });
_8_9_1.test.describe('Orchestration Cluster MCP Server - Variable 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_variable_test_file.bpmn', authToken, 'saas', 'agentic_cluster'),
            (0, apiHelpers_1.deployProcess)('./resources/mcp_server_saas/mcp_variable_tools.bpmn', authToken, 'saas', 'agentic_cluster'),
        ]);
        if (testFileProcessKey == null || toolsProcessKey == null) {
            throw new Error('Failed to deploy variable tools processes');
        }
        // Create test instance which will create variables
        const testInstanceKey = await (0, apiHelpers_1.createProcessInstance)(String(testFileProcessKey), authToken, 'saas', 'agentic_cluster', { testVariable1: 'test_value_1', testVariable2: 'test_value_2' });
        // Get a variable key created by the test instance (with retry logic)
        const testVariableKey = await (0, variableHelpers_1.getVariableKeyByProcessInstanceKey)(testInstanceKey, 'testVariable1', authToken, 'saas', 'agentic_cluster');
        console.log('Creating tools instance with variables:', {
            testFileProcessKey,
            testVariableKey,
        });
        // Create tools instance with required variables
        toolsInstanceKey = await (0, apiHelpers_1.createProcessInstance)(String(toolsProcessKey), authToken, 'saas', 'agentic_cluster', { testFileProcessKey, testVariableKey });
    });
    _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 variableToolTests = [
        {
            name: 'searchVariables - search variables with filters',
            variable: 'mcpResultSearchVariables',
            assertions: [
                {
                    text: 'items',
                    description: 'search response contains items field',
                },
                {
                    text: 'variableKey',
                    description: 'search response contains variableKey field',
                },
                {
                    text: 'name',
                    description: 'search response contains name field',
                },
                {
                    text: 'value',
                    description: 'search response contains value field',
                },
                {
                    text: 'processInstanceKey',
                    description: 'search response contains processInstanceKey field',
                },
            ],
        },
        {
            name: 'getVariable - retrieve a variable by key',
            variable: 'mcpResultGetVariable',
            assertions: [
                {
                    text: 'variableKey',
                    description: 'response contains variableKey field',
                },
                {
                    text: 'name',
                    description: 'response contains name field',
                },
                {
                    text: 'value',
                    description: 'response contains value field',
                },
                {
                    text: 'processInstanceKey',
                    description: 'response contains processInstanceKey field',
                },
                {
                    text: 'scopeKey',
                    description: 'response contains scopeKey field',
                },
            ],
        },
    ];
    for (const testCase of variableToolTests) {
        (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 Variable 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);
                });
            }
        });
    }
});
