"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 KeycloakUtils_1 = require("../../pages/SM-8.7/KeycloakUtils");
const sleep_1 = require("../../utils/sleep");
const env_1 = require("../../utils/env");
const loggingUtils_1 = require("../../utils/loggingUtils");
SM_8_7_1.test.describe.parallel('Connectors 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) => {
        (0, loggingUtils_1.cleanupTestLogging)();
        await (0, _setup_1.captureScreenshot)(page, testInfo);
        await (0, _setup_1.captureFailureVideo)(page, testInfo);
    });
    // Skipped due to 320: https://github.com/camunda/marketplace-api/issues/320
    SM_8_7_1.test.skip('REST Connector No Auth User Flow', async ({ page, operateHomePage, modelerHomePage, operateProcessInstancePage, modelerCreatePage, connectorSettingsPage, operateProcessesPage, connectorMarketplacePage, navigationPage, context, }) => {
        const processName = 'REST_Connector_No_Auth_Process' + (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();
            await modelerHomePage.clickCreateNewProjectButton();
            await modelerHomePage.createCrossComponentProjectFolder();
            await modelerHomePage.clickDiagramTypeDropdown();
            await modelerHomePage.clickBpmnTemplateOption();
        });
        await SM_8_7_1.test.step('Create BPMN Diagram with REST Connector and Start Process Instance', async () => {
            await (0, UtlitiesPage_1.modelRestConnector)(modelerCreatePage, connectorSettingsPage, connectorMarketplacePage, processName, 'http://camunda.proxy.beeceptor.com/pre-prod/no-auth-test', 'noAuth', 'body');
            await modelerCreatePage.runProcessInstance();
        });
        await SM_8_7_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 page.reload();
            await (0, test_1.expect)(page.getByTestId('variables-list')).toBeVisible({
                timeout: 30000,
            });
            (0, test_1.expect)((await operateProcessInstancePage.connectorResultVariableName('status')).isVisible()).toBeTruthy();
            await (0, test_1.expect)(page.getByText('"Awesome!"')).toBeVisible({
                timeout: 60000,
            });
        });
    });
    // Skipped due to 320: https://github.com/camunda/marketplace-api/issues/320
    SM_8_7_1.test.skip('REST Connector Bearer Token Auth User Flow', async ({ page, operateHomePage, modelerHomePage, modelerCreatePage, connectorSettingsPage, navigationPage, operateProcessInstancePage, operateProcessesPage, connectorMarketplacePage, context, }) => {
        const processName = 'REST_Connector_Bearer_Auth_Process' +
            (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();
            await modelerHomePage.clickCreateNewProjectButton();
            await modelerHomePage.createCrossComponentProjectFolder();
            await modelerHomePage.clickDiagramTypeDropdown();
            await modelerHomePage.clickBpmnTemplateOption();
        });
        await SM_8_7_1.test.step('Create BPMN Diagram with REST Connector with Basic Auth and Start Process Instance', async () => {
            await (0, UtlitiesPage_1.modelRestConnector)(modelerCreatePage, connectorSettingsPage, connectorMarketplacePage, processName, 'https://camunda.proxy.beeceptor.com/pre-prod/bearer-auth-test', 'bearer', '{message:response.body.status}');
            await modelerCreatePage.runProcessInstance();
            await (0, _setup_1.performBearerTokenAuthPostRequest)('https://camunda.proxy.beeceptor.com/pre-prod/bearer-auth-test', 'thisisabearertoken');
        });
        await SM_8_7_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 (0, test_1.expect)(page.getByTestId('variables-list')).toBeVisible({
                timeout: 90000,
            });
            (0, test_1.expect)((await operateProcessInstancePage.connectorResultVariableName('message')).isVisible()).toBeTruthy();
            await (0, test_1.expect)(page.getByText('"Awesome!"')).toBeVisible({
                timeout: 60000,
            });
        });
    });
    (0, SM_8_7_1.test)('Message Start Event Webhook Connector No Auth User Flow', async ({ modelerHomePage, navigationPage, modelerCreatePage, request, operateHomePage, operateProcessInstancePage, operateProcessesPage, connectorMarketplacePage, context, }) => {
        SM_8_7_1.test.slow();
        const randomString = await (0, _setup_1.generateRandomStringAsync)(3);
        const processName = 'Start_Event_Webhook_Connector_No_Auth_Process' + randomString;
        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();
            await modelerHomePage.clickCreateNewProjectButton();
            await modelerHomePage.createCrossComponentProjectFolder();
            await modelerHomePage.clickDiagramTypeDropdown();
            await modelerHomePage.clickBpmnTemplateOption();
        });
        await SM_8_7_1.test.step('Create BPMN Diagram with REST Connector and Deploy Diagram', async () => {
            await (0, UtlitiesPage_1.modelWebhookConnector)(modelerCreatePage, connectorMarketplacePage, processName, 'test-webhook-id' + randomString);
            await modelerCreatePage.deployProcessInstance();
        });
        await SM_8_7_1.test.step('Make Authorization Request', async () => {
            const baseURL = process.env.PLAYWRIGHT_BASE_URL ||
                `http://gke-${process.env.BASE_URL}.ci.distro.ultrawombat.com`;
            // OpenSearch registers inbound webhooks more slowly than ES.
            const timeout = env_1.isOpenSearch ? 180000 : 60000;
            const interval = 5000;
            const startTime = Date.now();
            let response;
            while (Date.now() - startTime < timeout) {
                response = await request.post(`${baseURL}/connectors/inbound/test-webhook-id${randomString}`);
                if (response.status() === 200) {
                    break;
                }
                await (0, sleep_1.sleep)(interval);
            }
            (0, test_1.expect)(response?.status()).toBe(200);
        });
        await SM_8_7_1.test.step('Assert Diagram Has Successfully Completed in Operate', async () => {
            await navigationPage.goToOperate();
            await operateHomePage.clickProcessesTab();
            await operateProcessesPage.clickProcessInstanceLink(processName, 'completed');
            const result = await operateProcessInstancePage.assertEitherIncidentOrCompletedIconVisible();
            (0, test_1.expect)(result).toBe('completed');
        });
    });
    // Skipped due to 320: https://github.com/camunda/marketplace-api/issues/320
    SM_8_7_1.test.skip('Connector Secrets User Flow', async ({ page, operateHomePage, modelerHomePage, navigationPage, modelerCreatePage, connectorSettingsPage, operateProcessInstancePage, operateProcessesPage, connectorMarketplacePage, context, }) => {
        const processName = 'REST_Connector_Process' + (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();
            await modelerHomePage.clickCreateNewProjectButton();
            await modelerHomePage.createCrossComponentProjectFolder();
            await modelerHomePage.clickDiagramTypeDropdown();
            await modelerHomePage.clickBpmnTemplateOption();
        });
        await SM_8_7_1.test.step('Create BPMN Diagram with REST Connector using secrets and Start Process Instance', async () => {
            await (0, UtlitiesPage_1.modelRestConnector)(modelerCreatePage, connectorSettingsPage, connectorMarketplacePage, processName, 'https://camunda.proxy.beeceptor.com/pre-prod/basic-auth-test', 'basic', '{message:response.body.message}', 'result', { username: '{{secrets.username}}', password: '{{secrets.password}}' });
            await modelerCreatePage.runProcessInstance();
        });
        await SM_8_7_1.test.step('View Process Instance in Operate, assert it completes and assert result expression', async () => {
            await navigationPage.goToOperate();
            await page.reload();
            await operateHomePage.clickProcessesTab();
            await operateProcessesPage.clickProcessInstanceLink(processName, 'completed');
            const result = await operateProcessInstancePage.assertEitherIncidentOrCompletedIconVisible();
            (0, test_1.expect)(result).toBe('completed');
            await (0, sleep_1.sleep)(1000); // moving too fast here means the text is not displayed.. clicking the row would fix this but there is no testId on the row so that would be flaky
            await page.reload();
            await (0, test_1.expect)(page.getByTestId('variables-list')).toBeVisible({
                timeout: 90000,
            });
            await (0, test_1.expect)(page.getByTestId('variable-message').getByText('"Message from Mock!"')).toBeVisible({ timeout: 180000 });
        });
    });
    (0, SM_8_7_1.test)('Intermediate Event Webhook Connector No Auth User Flow', async ({ modelerHomePage, modelerCreatePage, request, operateHomePage, operateProcessInstancePage, operateProcessesPage, navigationPage, connectorMarketplacePage, context, }) => {
        const randomString = await (0, _setup_1.generateRandomStringAsync)(3);
        const processName = 'Intermediate_Event_Webhook_Connector_No_Auth_Process' + randomString;
        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();
            await modelerHomePage.clickCreateNewProjectButton();
            await modelerHomePage.createCrossComponentProjectFolder();
            await modelerHomePage.clickDiagramTypeDropdown();
            await modelerHomePage.clickBpmnTemplateOption();
        });
        await SM_8_7_1.test.step('Create BPMN Diagram with Intermediate Event Webhook Connector and Run Diagram', async () => {
            await (0, UtlitiesPage_1.modelIntermediateWebhookConnector)(modelerCreatePage, connectorMarketplacePage, processName, 'test-webhook-intermediate' + randomString);
            await modelerCreatePage.runProcessInstance();
        });
        await SM_8_7_1.test.step('Make Authorization Request', async () => {
            const baseURL = process.env.PLAYWRIGHT_BASE_URL ||
                `http://gke-${process.env.BASE_URL}.ci.distro.ultrawombat.com`;
            // It takes about 6 seconds for the process to appear in the connectors endpoint
            // So we need to wait for it to appear before making the request.
            // OpenSearch is significantly slower at registering inbound webhooks.
            const timeout = env_1.isOpenSearch ? 180000 : 60000;
            const interval = 5000;
            const startTime = Date.now();
            let response;
            while (Date.now() - startTime < timeout) {
                response = await request.post(`${baseURL}/connectors/inbound/test-webhook-intermediate${randomString}`, {
                    data: {
                        test: 'test',
                    },
                });
                if (response.status() === 200) {
                    break;
                }
                await (0, sleep_1.sleep)(interval);
            }
            (0, test_1.expect)(response?.status()).toBe(200);
        });
        await SM_8_7_1.test.step('Assert Diagram Has Successfully Completed in Operate', async () => {
            await navigationPage.goToOperate();
            await operateHomePage.clickProcessesTab();
            await operateProcessesPage.clickProcessInstanceLink(processName, 'completed');
            const result = await operateProcessInstancePage.assertEitherIncidentOrCompletedIconVisible();
            (0, test_1.expect)(result).toBe('completed');
        });
    });
    //Skipped due to bug 1252: https://github.com/camunda/c8-cross-component-e2e-tests/issues/1252
    SM_8_7_1.test.skip('Connectors Timer Event User Flow', async ({ page, modelerHomePage, modelerCreatePage, connectorMarketplacePage, connectorTemplatePage, navigationPage, operateProcessInstancePage, operateHomePage, operateProcessesPage, context, }) => {
        const processName = 'Connectors_Timer_Event_Process' + (await (0, _setup_1.generateRandomStringAsync)(3));
        await SM_8_7_1.test.step('Open Cross Component Test Project and Create a REST Connector Document Handling Flow', async () => {
            await context.clearCookies();
            await navigationPage.goToModeler();
            await (0, UtlitiesPage_1.modelAndRunConnectorsTimerEventDiagram)(modelerCreatePage, modelerHomePage, processName, page, connectorMarketplacePage, connectorTemplatePage);
        });
        await SM_8_7_1.test.step('View Process Instance in Operate, assert it completes and assert variable values are correct', async () => {
            await navigationPage.goToOperate();
            await operateHomePage.clickProcessesTab();
            await operateProcessesPage.clickProcessInstanceLink(processName);
            await operateProcessInstancePage.activeIconAssertion();
            await operateProcessInstancePage.completedIconAssertion();
            await (0, test_1.expect)(page.getByText('["20A","20B"]')).toBeVisible({
                timeout: 60000,
            });
            await (0, test_1.expect)(page.getByText('seatNumbers')).toBeVisible({
                timeout: 60000,
            });
            await (0, test_1.expect)(page.getByText('paymentRequestId')).toBeVisible({
                timeout: 60000,
            });
        });
    });
});
