"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const test_1 = require("@playwright/test");
const SM_8_7_1 = require("../../fixtures/SM-8.7");
const _setup_1 = require("../../test-setup.js");
const UtlitiesPage_1 = require("../../pages/SM-8.7/UtlitiesPage");
const UtlitiesPage_2 = require("../../pages/SM-8.7/UtlitiesPage");
const KeycloakUtils_1 = require("../../pages/SM-8.7/KeycloakUtils");
const optimizeReportUtils_1 = require("../../pages/SM-8.7/optimizeReportUtils");
const env_1 = require("../../utils/env");
const loggingUtils_1 = require("../../utils/loggingUtils");
if (env_1.isOpenSearch) {
    SM_8_7_1.test.skip();
}
SM_8_7_1.test.describe.parallel('Optimize User Flow Tests', () => {
    SM_8_7_1.test.beforeEach(async ({ navigationPage, identityPage, identityTenantPage, keycloakLoginPage, keycloakAdminPage, }, testInfo) => {
        (0, loggingUtils_1.setupTestLogging)(testInfo);
        await (0, KeycloakUtils_1.setupKeycloakUser)(navigationPage, identityPage, keycloakLoginPage, keycloakAdminPage, identityTenantPage);
    });
    SM_8_7_1.test.afterEach(async ({ page }, testInfo) => {
        await (0, _setup_1.captureScreenshot)(page, testInfo);
        await (0, _setup_1.captureFailureVideo)(page, testInfo);
        (0, loggingUtils_1.cleanupTestLogging)();
    });
    (0, SM_8_7_1.test)('Process Import Flow', async ({ page, context, operateHomePage, modelerHomePage, navigationPage, modelerCreatePage, operateProcessesPage, operateProcessInstancePage, optimizeHomePage, optimizeDashboardPage, }) => {
        const processName = 'Optimize_Job_Worker_User_Task_Flow' +
            (await (0, _setup_1.generateRandomStringAsync)(3));
        await SM_8_7_1.test.step('Open Cross Component Test Project and Create a BPMN Diagram Template', async () => {
            await context.clearCookies();
            await navigationPage.goToModeler(1000);
            await modelerHomePage.clickCreateNewProjectButton();
            await modelerHomePage.createCrossComponentProjectFolder();
            await modelerHomePage.clickDiagramTypeDropdown();
            await modelerHomePage.clickBpmnTemplateOption();
        });
        await SM_8_7_1.test.step('Create BPMN Diagram with User Task and Start Process Instance', async () => {
            await modelerCreatePage.modelJobWorkerDiagram(processName);
            await modelerCreatePage.runProcessInstance();
        });
        await SM_8_7_1.test.step('View Process Instance in Operate and Assert Process has been successfully imported in Optimize', async () => {
            await navigationPage.goToOperate();
            await operateHomePage.clickProcessesTab();
            await page.reload();
            await operateProcessesPage.clickProcessInstanceLink(processName);
            await operateProcessInstancePage.activeIconAssertion();
            await navigationPage.goToOptimize();
            await optimizeHomePage.clickDashboardLink();
            await page.reload();
            await optimizeDashboardPage.clickFilterTable();
            await optimizeDashboardPage.fillFilterTable(processName);
            await optimizeDashboardPage.processAssertion(processName);
        });
    });
    (0, SM_8_7_1.test)('Job Worker User Task User Flow', async ({ page, context, modelerHomePage, navigationPage, modelerCreatePage, operateHomePage, operateProcessesPage, operateProcessInstancePage, optimizeHomePage, optimizeCollectionsPage, optimizeReportPage, }) => {
        const reportName = await (0, _setup_1.generateRandomStringAsync)(5);
        const processName = 'Optimize_Job_Worker_User_Task_Diagram' + reportName;
        await SM_8_7_1.test.step('Open Cross Component Test Project and Create a BPMN Diagram Template', async () => {
            await context.clearCookies();
            await navigationPage.goToModeler(1000);
            await modelerHomePage.clickCreateNewProjectButton();
            await modelerHomePage.createCrossComponentProjectFolder();
            await modelerHomePage.clickDiagramTypeDropdown();
            await modelerHomePage.clickBpmnTemplateOption();
        });
        await SM_8_7_1.test.step('Create BPMN Diagram with User Task and Start Process Instance', async () => {
            await modelerCreatePage.modelCamundaUserTaskDiagram(processName);
            await modelerCreatePage.runProcessInstance();
        });
        await SM_8_7_1.test.step('View Process Instance in Operate, Create User Task Report in Optimize, Start Another Process Instance in Modeler & Assert the Report Updates', async () => {
            await navigationPage.goToOperate();
            await page.reload();
            await operateHomePage.clickProcessesTab();
            await operateProcessesPage.clickProcessInstanceLink(processName);
            let result = await operateProcessInstancePage.assertEitherIncidentOrActiveIconVisible();
            (0, test_1.expect)(result).toBe('active');
            await navigationPage.goToOptimize();
            await page.reload();
            await optimizeHomePage.clickCollectionsLink();
            await (0, optimizeReportUtils_1.createReportForProcess)(optimizeCollectionsPage, optimizeReportPage, processName);
            await optimizeReportPage.clickUserTaskProcess(processName);
            await (0, test_1.expect)(optimizeReportPage.versionSelection).toBeVisible({
                timeout: 30000,
            });
            // TODO There is a race condition here. The report is static so if it is
            // TODO generated too fast, then we don't get to see the instance count
            // TODO as 2. Looping allows use to remove the need to sleep
            //await optimizeReportPage.clickVersionSelection();
            //await optimizeReportPage.clickAlwaysDisplayLatestSelection();
            await optimizeReportPage.clickBlankReportButton();
            await optimizeReportPage.clickCreateReportLink();
            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, UtlitiesPage_2.assertPageTextWithRetry)(page, reportName, false);
            await optimizeCollectionsPage.clickMostRecentProcessReport(reportName);
            await (0, UtlitiesPage_1.assertLocatorVisibleWithRetry)(page, optimizeReportPage.oneUserTaskInstance, 'One user task count');
            await navigationPage.goToModeler();
            await modelerHomePage.clickCrossComponentProjectFolder();
            await modelerHomePage.clickProcessDiagram(processName);
            await modelerCreatePage.runProcessInstance();
            await navigationPage.goToOperate();
            await operateHomePage.clickProcessesTab();
            await operateProcessesPage.clickProcessInstanceLink(processName);
            result =
                await operateProcessInstancePage.assertEitherIncidentOrActiveIconVisible();
            (0, test_1.expect)(result).toBe('active');
            await navigationPage.goToOptimize();
            await (0, optimizeReportUtils_1.assertReportWithRefreshes)(page, optimizeHomePage, optimizeCollectionsPage, optimizeReportPage, reportName, optimizeReportPage.twoUserTaskInstance, 'Two user tasks count');
            await (0, test_1.expect)(optimizeReportPage.oneUserTaskInstance).not.toBeVisible({
                timeout: 1000,
            });
        });
    });
    (0, SM_8_7_1.test)('New Instances Updated Flow', async ({ page, context, modelerHomePage, navigationPage, modelerCreatePage, operateHomePage, operateProcessesPage, operateProcessInstancePage, optimizeHomePage, optimizeCollectionsPage, optimizeReportPage, }) => {
        const reportName = await (0, _setup_1.generateRandomStringAsync)(5);
        const processName = 'Optimize_Job_Worker_User_Task_Flow_Updated' +
            (await (0, _setup_1.generateRandomStringAsync)(3));
        await SM_8_7_1.test.step('Open Cross Component Test Project and Create a BPMN Diagram Template', async () => {
            await context.clearCookies();
            await navigationPage.goToModeler(1000);
            await modelerHomePage.clickCreateNewProjectButton();
            await modelerHomePage.createCrossComponentProjectFolder();
            await modelerHomePage.clickDiagramTypeDropdown();
            await modelerHomePage.clickBpmnTemplateOption();
        });
        await SM_8_7_1.test.step('Create BPMN Diagram with User Task and Start Process Instance', async () => {
            await modelerCreatePage.modelJobWorkerDiagram(processName);
            await modelerCreatePage.deployProcessInstance();
        });
        await SM_8_7_1.test.step('Create Process Instance Count Report in Optimize, Start A Process Instance in Modeler & Assert the Report Updates', async () => {
            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).toBeVisible({
                timeout: 30000,
            });
            await optimizeReportPage.clickVersionSelection();
            await optimizeReportPage.clickAlwaysDisplayLatestSelection();
            await optimizeReportPage.clickBlankReportButton();
            await optimizeReportPage.clickCreateReportLink();
            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, UtlitiesPage_2.assertPageTextWithRetry)(page, reportName, false, 30000);
            await optimizeCollectionsPage.clickMostRecentProcessReport(reportName);
            await (0, UtlitiesPage_2.assertPageTextWithRetry)(page, 'Displaying data from 0 instances.', false);
            await navigationPage.goToModeler();
            await modelerHomePage.clickCrossComponentProjectFolder();
            await modelerHomePage.clickProcessDiagram(processName);
            await modelerCreatePage.runProcessInstance();
            await navigationPage.goToOperate();
            await operateHomePage.clickProcessesTab();
            await operateProcessesPage.clickProcessInstanceLink(processName);
            const result = await operateProcessInstancePage.assertEitherIncidentOrActiveIconVisible();
            (0, test_1.expect)(result).toBe('active');
            await navigationPage.goToOptimize();
            await optimizeHomePage.clickCollectionsLink();
            await (0, UtlitiesPage_2.assertPageTextWithRetry)(page, reportName, false);
            await optimizeCollectionsPage.clickMostRecentProcessReport(reportName);
            await (0, UtlitiesPage_2.assertPageTextWithRetry)(page, 'Displaying data from 1 instance.', false);
        });
    });
});
