"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const test_1 = require("@playwright/test");
const _8_8_1 = require("../../fixtures/8.8");
const _setup_1 = require("../../test-setup.js");
const OperateProcessInstancePage_1 = require("../../pages/8.8/OperateProcessInstancePage");
const formatDate_1 = require("../../utils/formatDate");
const UtilitiesPage_1 = require("../../pages/8.8/UtilitiesPage");
const sleep_1 = require("../../utils/sleep");
const users_1 = require("../../utils/users");
const mailSlurpClient_1 = require("../../utils/mailSlurpClient");
const testUser = (0, users_1.getTestUser)('eighteenthUser');
_8_8_1.test.describe.configure({ mode: 'parallel' });
_8_8_1.test.describe('Console User Flow Tests @tasklistV2', () => {
    const clusterName = 'Test Cluster';
    let inbox = '';
    _8_8_1.test.beforeEach(async ({ page, loginPage }, testInfo) => {
        await (0, UtilitiesPage_1.loginWithRetry)(page, loginPage, testUser, (testInfo.workerIndex + 1) * 1000);
    });
    _8_8_1.test.afterEach(async ({ page }, testInfo) => {
        await (0, _setup_1.captureScreenshot)(page, testInfo);
        await (0, _setup_1.captureFailureVideo)(page, testInfo);
        if (inbox.length > 0) {
            await (0, mailSlurpClient_1.deleteInbox)(inbox);
        }
    });
    (0, _8_8_1.test)('Console Most Common Flow', async ({ page, homePage, modelerHomePage, appsPage, modelerCreatePage, clusterPage, clusterDetailsPage, }) => {
        const clientName = await (0, _setup_1.generateRandomStringAsync)(6);
        const processName = 'Console_Test_Process' + (await (0, _setup_1.generateRandomStringAsync)(3));
        await _8_8_1.test.step('Add API Client to Cluster', async () => {
            await homePage.clickClusters();
            await clusterPage.clickClusterLink(clusterName);
            await clusterDetailsPage.clickAPITab();
            await clusterDetailsPage.createAPIClient(clientName);
            await clusterDetailsPage.clickCloseModalButton();
        });
        await _8_8_1.test.step('Navigate to Web Modeler', async () => {
            await appsPage.clickCamundaApps();
            await appsPage.clickModeler();
            await (0, test_1.expect)(modelerHomePage.modelerPageBanner).toBeVisible({
                timeout: 120000,
            });
        });
        await _8_8_1.test.step('Open Cross Component Test Project and Create a BPMN Diagram Template', async () => {
            await modelerHomePage.clickCrossComponentProjectFolder();
            await modelerHomePage.clickDiagramTypeDropdown();
            await modelerHomePage.clickBpmnTemplateOption();
        });
        await _8_8_1.test.step('Create BPMN Diagram with User Task and Start Process Instance', async () => {
            await (0, test_1.expect)(modelerCreatePage.generalPanel).toBeVisible({
                timeout: 120000,
            });
            await modelerCreatePage.enterDiagramName(processName);
            await (0, sleep_1.sleep)(10000);
            await modelerCreatePage.clickAppendElementButton();
            await modelerCreatePage.clickAppendTaskButton();
            await modelerCreatePage.clickAppendElementButton();
            await modelerCreatePage.clickAppendEndEventButton();
            await modelerCreatePage.runProcessInstance(clusterName);
        });
        await _8_8_1.test.step('View Process Instance in Operate, and assert it completes', async () => {
            await (0, test_1.expect)(modelerCreatePage.viewProcessInstanceLink).toBeVisible({
                timeout: 120000,
            });
            await modelerCreatePage.clickViewProcessInstanceLink();
            const operateTabPromise = page.waitForEvent('popup', { timeout: 60000 });
            const operateTab = await operateTabPromise;
            const operateTabProcessInstancePage = new OperateProcessInstancePage_1.OperateProcessInstancePage(operateTab);
            await (0, UtilitiesPage_1.assertLocatorVisibleWithRetry)(operateTabProcessInstancePage, operateTabProcessInstancePage.completedIcon, 'completed icon in Operate', 60000);
        });
    });
    (0, _8_8_1.test)('Alert Trigger Flow - Email Notification', async ({ page, homePage, modelerHomePage, appsPage, modelerCreatePage, clusterPage, clusterDetailsPage, connectorSettingsPage, operateHomePage, operateProcessesPage, operateProcessInstancePage, }) => {
        _8_8_1.test.slow();
        const processName = 'Email_Alert_Process' + (await (0, _setup_1.generateRandomStringAsync)(3));
        const invalidURl = 'https://invalid';
        let lastTriggerTime = 0;
        const alertFlowClusterName = 'Alert Flow Cluster';
        const { id } = await (0, mailSlurpClient_1.createInbox)(testUser.username);
        inbox = id;
        await _8_8_1.test.step('Create Cluster', async () => {
            await homePage.clickClusters();
            await clusterPage.createClusterIfNotExists(alertFlowClusterName);
        });
        await _8_8_1.test.step('Delete Alert Settings If Exist', async () => {
            await homePage.clickClusters();
            await clusterPage.clickClusterLink(alertFlowClusterName);
            await clusterDetailsPage.clickAlertsTab();
            await clusterDetailsPage.deleteAlerts();
            await test_1.expect
                .poll(async () => clusterDetailsPage.alertsList.count(), {
                timeout: 30000,
            })
                .toBe(0);
        });
        await _8_8_1.test.step('Create First Email Alert', async () => {
            await clusterDetailsPage.createAlert(true);
            await test_1.expect
                .poll(async () => clusterDetailsPage.alertsList.count(), {
                timeout: 30000,
            })
                .toBeGreaterThan(1);
            await (0, test_1.expect)(clusterDetailsPage.alertsList.nth(0)).toContainText(`Email`);
            await (0, test_1.expect)(clusterDetailsPage.alertsList.nth(1)).toContainText(testUser.username);
            await (0, test_1.expect)(clusterDetailsPage.alertsList.nth(1)).toContainText((0, formatDate_1.formatDate)());
            await (0, test_1.expect)(clusterDetailsPage.alertsList.nth(1)).toContainText('Amount triggered0');
        });
        await _8_8_1.test.step('Navigate to Web Modeler', async () => {
            await appsPage.clickCamundaApps();
            await appsPage.clickModeler();
            await (0, test_1.expect)(modelerHomePage.modelerPageBanner).toBeVisible({
                timeout: 120000,
            });
        });
        await _8_8_1.test.step('Open Cross Component Test Project', async () => {
            await modelerHomePage.clickCrossComponentProjectFolder();
            await modelerHomePage.clickDiagramTypeDropdown();
            await modelerHomePage.clickBpmnTemplateOption();
        });
        await _8_8_1.test.step('Create BPMN Diagram with REST Connector and Start Process Instance', async () => {
            await (0, UtilitiesPage_1.modelRestConnector)(modelerCreatePage, connectorSettingsPage, processName, invalidURl, 'noAuth', 'body');
            await (0, sleep_1.sleep)(20000);
            await modelerCreatePage.runProcessInstance(alertFlowClusterName);
            await (0, test_1.expect)(modelerCreatePage.viewProcessInstanceLink).toBeVisible({
                timeout: 120000,
            });
        });
        await _8_8_1.test.step('View Process Instance in Operate, and Assert It Fails', async () => {
            await appsPage.clickCamundaApps();
            await appsPage.clickOperate(alertFlowClusterName);
            await (0, test_1.expect)(operateHomePage.operateBanner).toBeVisible({
                timeout: 60000,
            });
            await operateHomePage.clickProcessesTab();
            await operateProcessesPage.clickProcessInstanceLink(processName);
            await (0, UtilitiesPage_1.assertLocatorVisibleWithRetry)(operateProcessInstancePage, operateProcessInstancePage.incidentIcon, 'incident icon in Operate');
        });
        await _8_8_1.test.step('Navigate to Clusters', async () => {
            await appsPage.clickCamundaApps();
            await appsPage.clickConsoleLink();
            await (0, UtilitiesPage_1.assertLocatorVisibleWithRetry)(page, homePage.clusterTab, 'Cluster tab');
            await homePage.clickClusters();
            await clusterPage.clickClusterLink(alertFlowClusterName);
        });
        await _8_8_1.test.step('Verify Alert Count Increases to 1 After Trigger', async () => {
            await clusterDetailsPage.clickAlertsTab();
            await (0, test_1.expect)(async () => {
                await page.reload();
                await (0, test_1.expect)(clusterDetailsPage.alertsList.first()).toBeVisible({
                    timeout: 30000,
                });
                (0, test_1.expect)(await clusterDetailsPage.alertsList.count()).toBeGreaterThan(1);
                await (0, test_1.expect)(clusterDetailsPage.alertsList.nth(0)).toContainText(`Email`);
                await clusterDetailsPage.assertAlertText('Amount triggered1');
            }).toPass({ timeout: 90000 });
        });
        await _8_8_1.test.step('Verify Alerts Received via Email for Incident', async () => {
            await (0, sleep_1.sleep)(60000);
            await (0, UtilitiesPage_1.assertLatestAlertEmail)(id, mailSlurpClient_1.mailSlurp, processName, 'New Incidents: 1');
            lastTriggerTime = Math.floor(Date.now() / 1000); // Current time in seconds (Unix timestamp)
            console.log('Captured Target Time:', lastTriggerTime);
        });
        await _8_8_1.test.step('Navigate to Web Modeler', async () => {
            await appsPage.clickCamundaApps();
            await appsPage.clickModeler();
            await (0, test_1.expect)(modelerHomePage.modelerPageBanner).toBeVisible({
                timeout: 120000,
            });
        });
        await _8_8_1.test.step('Open Cross Component Test Project and Run The Process Instance Two Times', async () => {
            await modelerHomePage.clickCrossComponentProjectFolder();
            await modelerHomePage.clickProcessDiagram(processName);
            await modelerCreatePage.runProcessInstance(alertFlowClusterName);
            await (0, sleep_1.sleep)(10000);
            await modelerCreatePage.runProcessInstance(alertFlowClusterName);
        });
        await _8_8_1.test.step('Navigate to Cluster Details', async () => {
            await appsPage.clickCamundaApps();
            await appsPage.clickConsoleLink();
            await (0, test_1.expect)(homePage.clusterTab).toBeVisible({ timeout: 60000 });
            await homePage.clickClusters();
            await clusterPage.clickClusterLink(alertFlowClusterName);
        });
        await _8_8_1.test.step('Verify Alert Count Increases to 3 After Trigger', async () => {
            await clusterDetailsPage.clickAlertsTab();
            await (0, test_1.expect)(async () => {
                await page.reload();
                await (0, test_1.expect)(clusterDetailsPage.alertsList.first()).toBeVisible({
                    timeout: 30000,
                });
                (0, test_1.expect)(await clusterDetailsPage.alertsList.count()).toBeGreaterThan(1);
                await clusterDetailsPage.assertAlertText('Amount triggered3');
            }).toPass({ timeout: 90000 });
        });
        await _8_8_1.test.step('Verify Alerts Received via Email for Incident', async () => {
            await (0, sleep_1.sleep)(60000);
            await (0, UtilitiesPage_1.assertLatestAlertEmail)(id, mailSlurpClient_1.mailSlurp, processName, 'New Incidents: 2');
        });
    });
});
