"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 userTaskHelpers_1 = require("../../../utils/userTaskHelpers");
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 - User Task 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_user_task_test_file.bpmn', authToken, 'saas', 'agentic_cluster'),
            (0, apiHelpers_1.deployProcess)('./resources/mcp_server_saas/mcp_user_task_tools.bpmn', authToken, 'saas', 'agentic_cluster'),
        ]);
        if (testFileProcessKey == null || toolsProcessKey == null) {
            throw new Error('Failed to deploy user task tools processes');
        }
        // Create test instance which will create a user task
        const testInstanceKey = await (0, apiHelpers_1.createProcessInstance)(String(testFileProcessKey), authToken, 'saas', 'agentic_cluster');
        // Get the user task key created by the test instance (with retry logic)
        const testUserTaskKey = await (0, userTaskHelpers_1.getUserTaskKeyByProcessInstanceKey)(testInstanceKey, authToken, 'saas', 'agentic_cluster');
        console.log('Creating tools instance with variables:', {
            testFileProcessKey,
            testUserTaskKey,
        });
        // Create tools instance with required variables
        toolsInstanceKey = await (0, apiHelpers_1.createProcessInstance)(String(toolsProcessKey), authToken, 'saas', 'agentic_cluster', { testFileProcessKey, testUserTaskKey });
    });
    _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 userTaskToolTests = [
        {
            name: 'searchUserTasks - search user tasks with filters such as processDefinitionKey',
            variable: 'mcpResultSearchUserTasks',
            assertions: [
                {
                    text: 'totalItems',
                    description: 'search response contains totalItems field',
                },
                {
                    text: 'userTaskKey',
                    description: 'search response contains userTaskKey field',
                },
                {
                    text: 'processDefinitionKey',
                    description: 'search response contains processDefinitionKey field',
                },
            ],
        },
        {
            name: 'getUserTask - retrieve a user task by key',
            variable: 'mcpResultGetUserTask',
            assertions: [
                {
                    text: 'userTaskKey',
                    description: 'response contains userTaskKey field',
                },
                {
                    text: 'processInstanceKey',
                    description: 'response contains processInstanceKey field',
                },
                {
                    text: 'assignee',
                    description: 'response contains assignee field',
                },
            ],
        },
        {
            name: 'assignUserTask - update the assignment of a user task',
            variable: 'mcpResultAssignUserTask',
            assertions: [
                {
                    text: 'mcpUser',
                    description: 'response reflects the assignee value mcpUser',
                },
            ],
        },
        {
            name: 'searchUserTaskVariables - search variables scoped to a specific user task',
            variable: 'mcpResultSearchUserTaskVariables',
            assertions: [
                {
                    text: 'totalItems',
                    description: 'search response contains totalItems field',
                },
                {
                    text: 'items',
                    description: 'search response contains items array',
                },
            ],
        },
    ];
    for (const testCase of userTaskToolTests) {
        (0, _8_9_1.test)(testCase.name, async ({ page, homePage, appsPage, operateHomePage, operateProcessesPage, operateProcessInstancePage, }) => {
            _8_9_1.test.slow();
            await _8_9_1.test.step('Navigate to completed process in Operate', async () => {
                await page.goto('/');
                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 User Task 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);
                });
            }
        });
    }
});
