"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const test_1 = require("@playwright/test");
const _setup_1 = require("../../test-setup.js");
const SM_8_9_1 = require("../../fixtures/SM-8.9");
const UtilitiesPage_1 = require("../../pages/SM-8.9/UtilitiesPage");
const optimizeReportUtils_1 = require("../../pages/SM-8.9/optimizeReportUtils");
const loggingUtils_1 = require("../../utils/loggingUtils");
const resetSession_1 = require("../../utils/resetSession");
const KeycloakUtils_1 = require("../../pages/SM-8.9/KeycloakUtils");
const sleep_1 = require("../../utils/sleep");
const env_1 = require("../../utils/env");
SM_8_9_1.test.describe.parallel('Smoke Tests', () => {
    SM_8_9_1.test.beforeEach(async ({ navigationPage, managementIdentityPage, managementIdentityTenantPage, keycloakLoginPage, keycloakAdminPage, page, browser, loginPage, ocIdentityHomePage, ocIdentityRolesPage, }, testInfo) => {
        if (process.env.IS_MIGRATION === 'true') {
            if (process.env.IS_MT === 'true') {
                const demoUserEmail = process.env.DEMO_USER_EMAIL || 'demo@example.org';
                await navigationPage.goToManagementIdentity();
                await managementIdentityTenantPage.assignUserToTenant('Default', 'demo', `Demo User${demoUserEmail}`);
                await navigationPage.goToOCAdmin((testInfo.workerIndex + 1) * 1000);
            }
            else {
                await navigationPage.goToOCIdentity((testInfo.workerIndex + 1) * 1000);
            }
        }
        else {
            console.log(`BeforeEach start: timeout = ${testInfo.timeout}`);
            (0, loggingUtils_1.setupTestLogging)(testInfo);
            const { testUsername, testPassword } = await (0, KeycloakUtils_1.setupKeycloakUser)(navigationPage, managementIdentityPage, keycloakLoginPage, keycloakAdminPage, ocIdentityHomePage, ocIdentityRolesPage, process.env.IS_MT === 'true', managementIdentityTenantPage);
            console.log(`BeforeEach end: timeout = ${testInfo.timeout}`);
            await (0, resetSession_1.resetSession)(browser, page);
            await loginPage.login(testUsername, testPassword);
        }
    });
    SM_8_9_1.test.afterEach(async ({ page }, testInfo) => {
        console.log(`AfterEach start: timeout = ${testInfo.timeout}`);
        await (0, _setup_1.captureScreenshot)(page, testInfo);
        await (0, _setup_1.captureFailureVideo)(page, testInfo);
        if (process.env.IS_MIGRATION !== 'true') {
            (0, loggingUtils_1.cleanupTestLogging)();
            console.log(`AfterEach end: timeout = ${testInfo.timeout}`);
        }
    });
    (0, SM_8_9_1.test)('Basic Login', async ({ context, navigationPage }, testInfo) => {
        console.log(`[${testInfo.title}] Test start: timeout = ${testInfo.timeout}`);
        await context.clearCookies();
        await navigationPage.goToManagementIdentity(1000);
        console.log(`[${testInfo.title}] Test end: timeout = ${testInfo.timeout}`);
    });
    (0, SM_8_9_1.test)('Basic Navigation', async ({ context, navigationPage }, testInfo) => {
        console.log(`[${testInfo.title}] Test start: timeout = ${testInfo.timeout}`);
        await SM_8_9_1.test.step('Assert Web Modeler Navigation', async () => {
            await context.clearCookies();
            await navigationPage.goToModeler();
        });
        await SM_8_9_1.test.step('Assert Tasklist Navigation', async () => {
            await context.clearCookies();
            await navigationPage.goToTasklist();
        });
        await SM_8_9_1.test.step('Assert Operate Navigation', async () => {
            await context.clearCookies();
            await navigationPage.goToOperate();
        });
        await SM_8_9_1.test.step('Assert OC Identity Navigation', async () => {
            await context.clearCookies();
            await navigationPage.goToOCIdentity();
        });
        if (process.env.IS_OPTIMIZE !== 'false') {
            await SM_8_9_1.test.step('Assert Optimize Navigation', async () => {
                await context.clearCookies();
                await navigationPage.goToOptimize();
            });
        }
        await SM_8_9_1.test.step('Assert Console Navigation', async () => {
            await context.clearCookies();
            await navigationPage.goToConsole();
        });
        console.log(`[${testInfo.title}] Test end: timeout = ${testInfo.timeout}`);
    });
    (0, SM_8_9_1.test)('Most Common Flow User Flow With All Apps', async ({ page, modelerHomePage, navigationPage, modelerCreatePage, operateHomePage, operateProcessesPage, operateProcessInstancePage, optimizeHomePage, optimizeCollectionsPage, optimizeReportPage, taskDetailsPage, taskPanelPage, }, testInfo) => {
        console.log(`[${testInfo.title}] Test start: timeout = ${testInfo.timeout}`);
        const randomName = await (0, _setup_1.generateRandomStringAsync)(3);
        const processName = 'Zeebe_User_Task_Process' + randomName;
        const reportName = await (0, _setup_1.generateRandomStringAsync)(5);
        await SM_8_9_1.test.step('Open Cross Component Test Project and Create a BPMN Diagram Template', async () => {
            await navigationPage.goToModeler();
            await modelerHomePage.createCrossComponentProjectFolder();
            await modelerHomePage.clickDiagramTypeDropdown();
            await modelerHomePage.clickBpmnTemplateOption();
        });
        await SM_8_9_1.test.step('Create BPMN Diagram with Zeebe User Task and Start Process Instance', async () => {
            await modelerCreatePage.modelCamundaUserTaskDiagram(processName, processName);
            await modelerCreatePage.runProcessInstance();
        });
        await SM_8_9_1.test.step('View Process Instance in Operate, complete User Task in Tasklist & assert process complete in Operate and Assert Process has been successfully imported in Optimize', async () => {
            await navigationPage.goToOperate();
            await operateHomePage.clickProcessesTab();
            await operateProcessesPage.clickProcessInstanceLink(processName, 'active');
            let result = await operateProcessInstancePage.assertEitherIncidentOrActiveIconVisible();
            (0, test_1.expect)(result).toBe('active');
            await navigationPage.goToTasklist();
            await (0, UtilitiesPage_1.completeTaskWithRetry)(page, taskPanelPage, taskDetailsPage, processName, 'medium');
            await navigationPage.goToOperate();
            await (0, test_1.expect)(operateHomePage.processesTab).toBeVisible({ timeout: 120000 });
            await operateHomePage.clickProcessesTab();
            await operateProcessesPage.clickProcessInstanceLink(processName, 'completed');
            result =
                await operateProcessInstancePage.assertEitherIncidentOrCompletedIconVisible();
            (0, test_1.expect)(result).toBe('completed');
            if (process.env.IS_OPTIMIZE !== 'false') {
                await navigationPage.goToOptimize();
                await optimizeHomePage.clickCollectionsLink();
                await (0, optimizeReportUtils_1.createReportForProcess)(optimizeCollectionsPage, optimizeReportPage, processName);
                await optimizeReportPage.clickUserTaskProcess(processName);
                await (0, test_1.expect)(optimizeReportPage.versionSelection).toBeEnabled({
                    timeout: 30000,
                });
                await optimizeReportPage.clickVersionSelection();
                await optimizeReportPage.clickAlwaysDisplayLatestSelection();
                await optimizeReportPage.clickBlankReportButton();
                await optimizeReportPage.clickCreateReportLink();
                await (0, test_1.expect)(optimizeReportPage.selectDropdown).toBeVisible({
                    timeout: 60000,
                });
                await optimizeReportPage.clickSelectDropdown();
                await optimizeReportPage.clickUserTaskOption();
                await optimizeReportPage.clickHeatMapButton();
                await (0, test_1.expect)(optimizeReportPage.tableOption).toBeVisible({
                    timeout: 30000,
                });
                await optimizeReportPage.clickTableOption();
                await optimizeReportPage.clickReportName();
                await optimizeReportPage.clearReportName();
                await optimizeReportPage.fillReportName(reportName);
                await optimizeReportPage.clickSaveButton();
                await optimizeHomePage.clickCollectionsLink();
                await (0, optimizeReportUtils_1.assertReportWithRefreshes)(page, optimizeHomePage, optimizeCollectionsPage, optimizeReportPage, reportName, optimizeReportPage.oneUserTaskInstance, 'One user task count');
            }
        });
        console.log(`[${testInfo.title}] Test end: timeout = ${testInfo.timeout}`);
    });
    (0, SM_8_9_1.test)('Most Common REST Connector User Flow @tasklistV2', async ({ context, operateHomePage, modelerHomePage, modelerCreatePage, connectorSettingsPage, navigationPage, operateProcessesPage, operateProcessInstancePage, connectorMarketplacePage, }, testInfo) => {
        SM_8_9_1.test.setTimeout(env_1.isOpenSearch ? 18 * 60 * 1000 : 12 * 60 * 1000);
        console.log(`[${testInfo.title}] Test start: timeout = ${testInfo.timeout}`);
        const processName = 'REST_Connector_Basic_Auth_Process' +
            (await (0, _setup_1.generateRandomStringAsync)(3));
        await SM_8_9_1.test.step('Open Cross Component Test Project and Create a BPMN Diagram Template', async () => {
            await context.clearCookies();
            await navigationPage.goToModeler();
            await modelerHomePage.createCrossComponentProjectFolder();
            await modelerHomePage.clickDiagramTypeDropdown();
            await modelerHomePage.clickBpmnTemplateOption();
        });
        await SM_8_9_1.test.step('Create BPMN Diagram with REST Connector with Basic Auth and Start Process Instance', async () => {
            await (0, UtilitiesPage_1.modelRestConnector)(modelerCreatePage, connectorSettingsPage, connectorMarketplacePage, processName, 'https://camunda.proxy.beeceptor.com/pre-prod/basic-auth-test', 'basic', '{message:response.body.message}', 'result', { username: 'username', password: 'password' });
            await modelerCreatePage.runProcessInstance();
            await (0, sleep_1.sleep)(3000);
        });
        await SM_8_9_1.test.step('View Process Instance in Operate, assert it completes and assert result expression', async () => {
            await navigationPage.goToOperate();
            await operateHomePage.clickProcessesTab();
            await operateProcessesPage.clickProcessInstanceLink(processName, 'completed');
            const result = await operateProcessInstancePage.assertEitherIncidentOrCompletedIconVisible();
            (0, test_1.expect)(result).toBe('completed');
            await operateProcessInstancePage.assertVariablesListVisible();
            await operateProcessInstancePage.connectorResultVariableName('message');
            await operateProcessInstancePage.assertProcessVariableContainsText('message', '"Message from Mock!"');
        });
        console.log(`[${testInfo.title}] Test end: timeout = ${testInfo.timeout}`);
    });
    (0, SM_8_9_1.test)('Assign Test User Roles @tasklistV2', async ({ ocIdentityHomePage, ocIdentityRolesPage, }) => {
        if (process.env.IS_MIGRATION !== 'true') {
            SM_8_9_1.test.skip(true, 'Skipping test because test not required in this test environment');
        }
        await ocIdentityHomePage.clickRolesTab();
        await ocIdentityRolesPage.clickRole('admin');
        await ocIdentityRolesPage.assignUserToRole('bart');
        await ocIdentityRolesPage.assignUserToRole('lisa');
    });
    (0, SM_8_9_1.test)('Assign Test Client Admin Role @tasklistV2', async ({ ocIdentityHomePage, ocIdentityRolesPage, }) => {
        if (process.env.IS_MIGRATION !== 'true') {
            SM_8_9_1.test.skip(true, 'Skipping test because test not required in this test environment');
        }
        await ocIdentityHomePage.clickRolesTab();
        await ocIdentityRolesPage.clickRole('admin');
        await ocIdentityRolesPage.addClientToRole('test');
    });
});
