"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const test_1 = require("@playwright/test");
const _8_6_1 = require("../../fixtures/8.6");
const _setup_1 = require("../../test-setup.js");
const OperateProcessInstancePage_1 = require("../../pages/8.6/OperateProcessInstancePage");
const sleep_1 = require("../../utils/sleep");
const googleapi_1 = require("../../utils/googleapi");
const formatDate_1 = require("../../utils/formatDate");
const UtilitiesPage_1 = require("../../pages/8.6/UtilitiesPage");
const constants_1 = require("../../utils/constants");
const users_1 = require("../../utils/users");
const testUser = (0, users_1.getTestUser)('eighteenthUser');
_8_6_1.test.describe.configure({ mode: 'parallel' });
_8_6_1.test.describe('Console User Flow Tests', () => {
    const defaultClusterName = 'Test Cluster';
    const awsCluster = 'AWS Cluster';
    _8_6_1.test.beforeEach(async ({ page, loginPage }, testInfo) => {
        await (0, UtilitiesPage_1.loginWithRetry)(page, loginPage, testUser, (testInfo.workerIndex + 1) * 1000);
    });
    _8_6_1.test.afterEach(async ({ page }, testInfo) => {
        await (0, _setup_1.captureScreenshot)(page, testInfo);
        await (0, _setup_1.captureFailureVideo)(page, testInfo);
    });
    (0, _8_6_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_6_1.test.step('Add API Client to Cluster', async () => {
            await homePage.clickClusters();
            await clusterPage.clickClusterLink(defaultClusterName);
            await clusterDetailsPage.clickAPITab();
            await clusterDetailsPage.deleteAPIClientsIfExist();
            await clusterDetailsPage.createAPIClient(clientName);
            await clusterDetailsPage.clickCloseModalButton();
            await (0, test_1.expect)(clusterDetailsPage.clientsList.filter({ hasText: clientName })).toContainText('Zeebe, Tasklist, Operate, Optimize, and Secrets');
        });
        await _8_6_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_6_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_6_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(defaultClusterName);
        });
        await _8_6_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);
        });
    });
    //Waiting infra team to provide DNS configuration for email testing, skipping for now
    _8_6_1.test.skip('Alert Trigger Flow - Email Notification', async ({ page, homePage, modelerHomePage, appsPage, modelerCreatePage, clusterPage, clusterDetailsPage, connectorSettingsPage, }) => {
        _8_6_1.test.slow();
        const processName = 'Email_Alert_Process' + (await (0, _setup_1.generateRandomStringAsync)(3));
        const invalidURl = 'https://invalid';
        const senderEmail = 'no-reply@camunda.io';
        let lastTriggerTime = 0;
        await _8_6_1.test.step('Delete Alert Settings If Exist', async () => {
            await (0, test_1.expect)(homePage.clusterTab).toBeVisible({ timeout: 30000 });
            await homePage.clickClusters();
            await clusterPage.clickClusterLink(awsCluster);
            await clusterDetailsPage.clickAlertsTab();
            await clusterDetailsPage.deleteAlerts();
            (0, test_1.expect)(await clusterDetailsPage.alertsList.count()).toBe(0);
        });
        await _8_6_1.test.step('Create First Email Alert', async () => {
            await clusterDetailsPage.createAlert(true);
            (0, test_1.expect)(await clusterDetailsPage.alertsList.count()).toBeGreaterThan(1);
            await (0, test_1.expect)(clusterDetailsPage.alertsList.nth(0)).toContainText(`Email`);
            await (0, test_1.expect)(clusterDetailsPage.alertsList.nth(1)).toContainText(process.env.C8_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_6_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_6_1.test.step('Open Cross Component Test Project', async () => {
            await modelerHomePage.clickCrossComponentProjectFolder();
            await modelerHomePage.clickDiagramTypeDropdown();
            await modelerHomePage.clickBpmnTemplateOption();
        });
        await _8_6_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(awsCluster);
        });
        await _8_6_1.test.step('View Process Instance in Operate, and Assert It Fails', 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 page.reload();
            await (0, test_1.expect)(operateTabProcessInstancePage.incidentIcon).toBeVisible({
                timeout: 180000,
            });
        });
        await _8_6_1.test.step('Navigate to Clusters', async () => {
            await appsPage.clickCamundaApps();
            await appsPage.clickConsoleLink();
            await (0, test_1.expect)(homePage.clusterTab).toBeVisible({ timeout: 60000 });
            await homePage.clickClusters();
            await clusterPage.clickClusterLink(awsCluster);
        });
        await _8_6_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: 20000,
                });
                (0, test_1.expect)(await clusterDetailsPage.alertsList.count()).toBeGreaterThan(1);
                await (0, test_1.expect)(clusterDetailsPage.alertsList.nth(0)).toContainText(`Email`);
                await (0, test_1.expect)(clusterDetailsPage.alertsList.nth(1)).toContainText('Amount triggered1');
            }).toPass(constants_1.defaultAssertionOptions);
        });
        await _8_6_1.test.step('Verify Alerts Received via Email for Incident', async () => {
            await (0, sleep_1.sleep)(60000);
            const token = await (0, googleapi_1.getGoogleAccessToken)();
            const messages = await (0, googleapi_1.getGmailMessagesWithRetry)(token, senderEmail, processName);
            (0, test_1.expect)(messages.length).toBe(1);
            const emailDetails = await (0, googleapi_1.getLatestEmailDetails)(token, await messages[0]['id']);
            (0, test_1.expect)(emailDetails).toContain('New Incidents: 1');
            lastTriggerTime = Math.floor(Date.now() / 1000); // Current time in seconds (Unix timestamp)
            console.log('Captured Target Time:', lastTriggerTime);
        });
        await _8_6_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_6_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(awsCluster);
            await modelerCreatePage.runProcessInstance(awsCluster);
        });
        await _8_6_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(awsCluster);
        });
        await _8_6_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: 20000,
                });
                (0, test_1.expect)(await clusterDetailsPage.alertsList.count()).toBeGreaterThan(1);
                await (0, test_1.expect)(clusterDetailsPage.alertsList.nth(1)).toContainText('Amount triggered3');
            }).toPass(constants_1.defaultAssertionOptions);
        });
        await _8_6_1.test.step('Verify Alerts Received via Email for Incident', async () => {
            await (0, sleep_1.sleep)(60000);
            const token = await (0, googleapi_1.getGoogleAccessToken)();
            const messages = await (0, googleapi_1.getGmailMessagesWithRetry)(token, senderEmail, processName, lastTriggerTime);
            const emailDetails = await (0, googleapi_1.getLatestEmailDetails)(token, await messages[0]['id']);
            (0, test_1.expect)(emailDetails).toContain('New Incidents: 2'); //# of alerts triggered since the last one
        });
    });
});
