"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 sleep_1 = require("../../utils/sleep");
const users_1 = require("../../utils/users");
const randomName_1 = require("../../utils/randomName");
const CLUSTER_VAR_NAMES = {
    ORIGINAL_JSON: 'ORIGINAL_API_INFO',
    UPDATABLE_JSON: 'UPDATABLE_API_INFO',
    ORIGINAL_STRING: 'ORIGINAL_APP_VERSION',
    UPDATABLE_STRING: 'UPDATABLE_APP_VERSION',
};
function getConfigByType(type) {
    const configs = {
        JSON: {
            processName: 'Cluster Variable Original JSON',
            processNameUpdatable: 'Cluster Variable Updatable JSON',
            taskName: 'Preview JSON cluster variables',
            originalVariableName: CLUSTER_VAR_NAMES.ORIGINAL_JSON,
            updatableVariableName: CLUSTER_VAR_NAMES.UPDATABLE_JSON,
            originalValue: originalJsonClusterVariableValue,
            operateVariableName: 'URL',
            randomValuePrefix: 'https://updated.api.com',
        },
        String: {
            processName: 'Cluster Variable Original String',
            processNameUpdatable: 'Cluster Variable Updatable String',
            taskName: 'Preview String cluster variable',
            originalVariableName: CLUSTER_VAR_NAMES.ORIGINAL_STRING,
            updatableVariableName: CLUSTER_VAR_NAMES.UPDATABLE_STRING,
            originalValue: originalStringClusterVariableValue,
            operateVariableName: 'APP_VERSION_VALUE',
            randomValuePrefix: 'v3.0.0-updated',
        },
    };
    return configs[type];
}
const testUser = (0, users_1.getTestUser)('sixteenthUser');
let authToken;
let originalJsonClusterVariableValue;
let originalStringClusterVariableValue;
let jsonProcessKeyDisplay;
let jsonProcessKeyUpdate;
let stringProcessKeyDisplay;
let stringProcessKeyUpdate;
_8_9_1.test.describe.configure({ mode: 'parallel' });
_8_9_1.test.describe('Cluster Variables User Flows', () => {
    const clusterName = 'Test Cluster';
    _8_9_1.test.beforeAll(async () => {
        authToken = await (0, apiHelpers_1.authSaasAPI)();
        await Promise.all([
            (0, apiHelpers_1.createJsonClusterVariable)(authToken, 'saas', CLUSTER_VAR_NAMES.ORIGINAL_JSON, process.env.CLUSTER_VARIABLE_JSON_VALUE),
            (0, apiHelpers_1.createJsonClusterVariable)(authToken, 'saas', CLUSTER_VAR_NAMES.UPDATABLE_JSON, process.env.CLUSTER_VARIABLE_JSON_VALUE),
            (0, apiHelpers_1.createStringClusterVariable)(authToken, 'saas', CLUSTER_VAR_NAMES.ORIGINAL_STRING, process.env.CLUSTER_VARIABLE_STRING_VALUE),
            (0, apiHelpers_1.createStringClusterVariable)(authToken, 'saas', CLUSTER_VAR_NAMES.UPDATABLE_STRING, process.env.CLUSTER_VARIABLE_STRING_VALUE),
        ]);
        originalJsonClusterVariableValue = JSON.parse(process.env.CLUSTER_VARIABLE_JSON_VALUE);
        originalStringClusterVariableValue =
            process.env.CLUSTER_VARIABLE_STRING_VALUE;
        const deployments = [
            {
                path: './resources/cluster_variables/cluster_variable_json_readonly.bpmn',
                assignTo: 'jsonProcessKeyDisplay',
            },
            {
                path: './resources/cluster_variables/cluster_variable_json_mutable.bpmn',
                assignTo: 'jsonProcessKeyUpdate',
            },
            {
                path: './resources/cluster_variables/cluster_variable_json_form.form',
                assignTo: null,
            },
            {
                path: './resources/cluster_variables/cluster_variable_string_readonly.bpmn',
                assignTo: 'stringProcessKeyDisplay',
            },
            {
                path: './resources/cluster_variables/cluster_variable_string_mutable.bpmn',
                assignTo: 'stringProcessKeyUpdate',
            },
            {
                path: './resources/cluster_variables/cluster_variable_string_form.form',
                assignTo: null,
            },
        ];
        const deploymentResults = await Promise.all(deployments.map((d) => (0, apiHelpers_1.deployProcess)(d.path, authToken)));
        jsonProcessKeyDisplay = deploymentResults[0];
        jsonProcessKeyUpdate = deploymentResults[1];
        stringProcessKeyDisplay = deploymentResults[3];
        stringProcessKeyUpdate = deploymentResults[4];
    });
    _8_9_1.test.beforeEach(async ({ page, appsPage, loginPage }, testInfo) => {
        await (0, UtilitiesPage_1.loginWithRetry)(page, loginPage, testUser, (testInfo.workerIndex + 1) * 1000);
        await appsPage.clickCamundaApps();
        await appsPage.clickOperate(clusterName);
    });
    _8_9_1.test.afterEach(async ({ page }, testInfo) => {
        await (0, _setup_1.captureScreenshot)(page, testInfo);
        await (0, _setup_1.captureFailureVideo)(page, testInfo);
    });
    _8_9_1.test.describe('Global cluster variable', () => {
        const testConfigs = [
            {
                type: 'JSON',
                processKeyDisplay: () => jsonProcessKeyDisplay,
                processKeyUpdate: () => jsonProcessKeyUpdate,
            },
            {
                type: 'String',
                processKeyDisplay: () => stringProcessKeyDisplay,
                processKeyUpdate: () => stringProcessKeyUpdate,
            },
        ];
        for (const config of testConfigs) {
            (0, _8_9_1.test)(`User views ${config.type} cluster variable in Tasklist from readonly process and completes task`, async ({ appsPage, operateHomePage, operateProcessesPage, operateProcessInstancePage, taskPanelPage, taskDetailsPage, }) => {
                _8_9_1.test.slow();
                const derived = getConfigByType(config.type);
                const instanceKey = await (0, apiHelpers_1.createProcessInstance)(String(config.processKeyDisplay()), authToken);
                await _8_9_1.test.step('User can access the process on operate', async () => {
                    await operateHomePage.clickProcessesTab();
                    await operateProcessesPage.applyMoreFilters('Process Instance Key(s)', String(instanceKey));
                    await operateProcessesPage.clickProcessInstanceLink(derived.processName);
                    await operateProcessInstancePage.assertActiveTokenIsPresent();
                });
                await _8_9_1.test.step(`User can view the process with ${config.type} cluster variable on tasklist`, async () => {
                    await appsPage.clickCamundaApps();
                    await appsPage.clickTasklist(clusterName);
                    await (0, sleep_1.sleep)(5000);
                    await taskPanelPage.openTask(derived.taskName);
                    await taskDetailsPage.clickAssignToMeButton();
                });
                await _8_9_1.test.step(`User can view the value of ${config.type} cluster variable and complete the task`, async () => {
                    if (config.type === 'JSON') {
                        for (const value of Object.values(derived.originalValue)) {
                            await taskDetailsPage.assertTextIsPresent(String(value));
                        }
                    }
                    else {
                        await taskDetailsPage.assertTextIsPresent(String(derived.originalValue));
                    }
                    await taskDetailsPage.clickCompleteTaskButton();
                });
                await _8_9_1.test.step('User can assert the process is completed on Operate', async () => {
                    await appsPage.clickCamundaApps();
                    await appsPage.clickOperate(clusterName);
                    await operateHomePage.clickProcessesTab();
                    await operateProcessesPage.clickProcessCompletedCheckbox();
                    await operateProcessesPage.applyMoreFilters('Process Instance Key(s)', String(instanceKey));
                    await operateProcessesPage.clickProcessInstanceLink(derived.processName);
                    await operateProcessInstancePage.assertProcessCompleteStatusWithRetry();
                });
            });
        }
        for (const config of testConfigs) {
            (0, _8_9_1.test)(`User updates ${config.type} cluster variable in Admin UI and verifies changes in Operate`, async ({ appsPage, ocIdentityHomePage, ocIdentityClusterVariablesPage, operateHomePage, operateProcessesPage, operateProcessInstancePage, }) => {
                _8_9_1.test.slow();
                const derived = getConfigByType(config.type);
                const randomRawValue = await (0, randomName_1.randomNameAgregator)(derived.randomValuePrefix);
                const newValue = config.type === 'JSON'
                    ? JSON.stringify({
                        endpoint: randomRawValue,
                        timeout_ms: '5000',
                        array: ['updated_item1', 'updated_item2'],
                    })
                    : JSON.stringify(randomRawValue);
                await _8_9_1.test.step('User navigates to Admin UI', async () => {
                    await appsPage.clickCamundaApps();
                    await appsPage.clickAdmin(clusterName);
                });
                await _8_9_1.test.step('User navigates to Cluster Variables tab', async () => {
                    await ocIdentityHomePage.clickClusterVariablesTab();
                });
                await _8_9_1.test.step(`User verifies ${config.type} cluster variable exists`, async () => {
                    await ocIdentityClusterVariablesPage.assertVariableExists(derived.updatableVariableName);
                });
                await _8_9_1.test.step(`User updates the ${config.type} cluster variable value`, async () => {
                    await ocIdentityClusterVariablesPage.editVariable(derived.updatableVariableName, newValue);
                });
                await _8_9_1.test.step(`User creates a process instance with the updated ${config.type} cluster variable`, async () => {
                    const instanceKey = await (0, apiHelpers_1.createProcessInstance)(String(config.processKeyUpdate()), authToken);
                    await _8_9_1.test.step('User navigates to the process instance on Operate', async () => {
                        await appsPage.clickCamundaApps();
                        await appsPage.clickOperate(clusterName);
                        await operateHomePage.clickProcessesTab();
                        await operateProcessesPage.clickProcessCompletedCheckbox();
                        await operateProcessesPage.applyMoreFilters('Process Instance Key(s)', String(instanceKey));
                        await operateProcessesPage.clickProcessInstanceLink(derived.processNameUpdatable);
                        await operateProcessInstancePage.assertProcessCompleteStatusWithRetry();
                    });
                    await _8_9_1.test.step(`User verifies the updated ${config.type} cluster variable value is present in process variables`, async () => {
                        await operateProcessInstancePage.assertProcessVariableContainsText(derived.operateVariableName, randomRawValue);
                    });
                });
            });
        }
    });
});
