"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const test_1 = require("@playwright/test");
const SM_8_9_1 = require("../../fixtures/SM-8.9");
const _setup_1 = require("../../test-setup.js");
const UtilitiesPage_1 = require("../../pages/SM-8.9/UtilitiesPage");
const sleep_1 = require("../../utils/sleep");
const mtTenant = process.env.IS_MT === 'true' ? '<default>' : '';
SM_8_9_1.test.describe.configure({ mode: 'parallel' });
SM_8_9_1.test.describe('Connectors User Flow Tests @tasklistV2', () => {
    SM_8_9_1.test.beforeEach(async ({ navigationPage }, testInfo) => {
        await navigationPage.goToModeler((testInfo.workerIndex + 1) * 1000);
    });
    SM_8_9_1.test.afterEach(async ({ page }, testInfo) => {
        await (0, _setup_1.captureScreenshot)(page, testInfo);
        await (0, _setup_1.captureFailureVideo)(page, testInfo);
    });
    (0, SM_8_9_1.test)('REST Connector No Auth User Flow', async ({ operateHomePage, modelerHomePage, operateProcessInstancePage, modelerCreatePage, connectorSettingsPage, operateProcessesPage, connectorMarketplacePage, navigationPage, }) => {
        SM_8_9_1.test.slow();
        const processName = 'REST_Connector_No_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 modelerHomePage.clickCrossComponentProjectFolder();
            await modelerHomePage.clickDiagramTypeDropdown();
            await modelerHomePage.clickBpmnTemplateOption();
        });
        await SM_8_9_1.test.step('Create BPMN Diagram with REST Connector and Start Process Instance', async () => {
            await (0, UtilitiesPage_1.modelRestConnector)(modelerCreatePage, connectorSettingsPage, connectorMarketplacePage, processName, 'https://camunda.proxy.beeceptor.com/pre-prod/no-auth-test', 'noAuth', 'body');
            await modelerCreatePage.runProcessInstance('', mtTenant);
            await (0, sleep_1.sleep)(20000);
        });
        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('status');
            await operateProcessInstancePage.assertProcessVariableContainsText('status', '"Awesome!"');
        });
    });
    (0, SM_8_9_1.test)('REST Connector Bearer Token Auth User Flow', async ({ operateHomePage, modelerHomePage, modelerCreatePage, connectorSettingsPage, navigationPage, operateProcessInstancePage, operateProcessesPage, connectorMarketplacePage, }) => {
        SM_8_9_1.test.slow();
        const processName = 'REST_Connector_Bearer_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 modelerHomePage.clickCrossComponentProjectFolder();
            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/bearer-auth-test', 'bearer', '{message:response.body.status}');
            await modelerCreatePage.runProcessInstance('', mtTenant);
            await (0, _setup_1.performBearerTokenAuthPostRequest)('https://camunda.proxy.beeceptor.com/pre-prod/bearer-auth-test', 'thisisabearertoken');
            await (0, sleep_1.sleep)(20000);
        });
        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', '"Awesome!"');
        });
    });
    (0, SM_8_9_1.test)('Message Start Event Webhook Connector No Auth User Flow', async ({ modelerHomePage, navigationPage, modelerCreatePage, request, operateHomePage, operateProcessInstancePage, operateProcessesPage, connectorMarketplacePage, connectorSettingsPage, }) => {
        SM_8_9_1.test.slow();
        const randomString = await (0, _setup_1.generateRandomStringAsync)(3);
        const processName = 'Start_Event_Webhook_Connector_No_Auth_Process' + randomString;
        await SM_8_9_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 SM_8_9_1.test.step('Create BPMN Diagram with REST Connector and Deploy Diagram', async () => {
            await (0, test_1.expect)(modelerCreatePage.generalPanel).toBeVisible({
                timeout: 60000,
            });
            await modelerCreatePage.enterDiagramName(processName);
            await (0, sleep_1.sleep)(1000);
            await modelerCreatePage.clickCanvas();
            await modelerCreatePage.clickGeneralPropertiesPanel();
            await modelerCreatePage.clickIdInput();
            await modelerCreatePage.fillIdInput(processName);
            await modelerCreatePage.assertNameInput(processName);
            await (0, sleep_1.sleep)(1000);
            await modelerCreatePage.clickStartEventElement();
            await modelerCreatePage.clickChangeTypeButton();
            try {
                await (0, test_1.expect)(modelerCreatePage.webhookMessageStartEventConnectorOption).toBeVisible({
                    timeout: 15000,
                });
            }
            catch (error) {
                await modelerCreatePage.clickMarketPlaceButton();
                await connectorMarketplacePage.clickSearchForConnectorTextbox();
                await connectorMarketplacePage.fillSearchForConnectorTextbox('Webhook Connector');
                await (0, sleep_1.sleep)(10000);
                await connectorMarketplacePage.downloadConnectorToProject();
            }
            await modelerCreatePage.clickWebhookMessageStartEventConnectorOption();
            await connectorSettingsPage.clickWebhookConfigurationTab();
            await modelerCreatePage.clickWebhookIdInput();
            await modelerCreatePage.clearWebhookIdInput();
            await modelerCreatePage.fillWebhookIdInput('test-webhook-id' + randomString);
            await (0, sleep_1.sleep)(10000);
            await modelerCreatePage.clickAppendElementButton();
            await modelerCreatePage.clickAppendEndEventButton();
            await (0, sleep_1.sleep)(10000);
            await modelerCreatePage.deployProcessInstance(mtTenant);
            await (0, sleep_1.sleep)(20000);
        });
        await SM_8_9_1.test.step('Make Authorization Request', async () => {
            const baseURL = process.env.PLAYWRIGHT_BASE_URL ||
                `http://gke-${process.env.BASE_URL}.ci.distro.ultrawombat.com`;
            const response = await request.post(`${baseURL}/connectors/inbound/test-webhook-id${randomString}`);
            await (0, test_1.expect)(response.status()).toBe(200);
        });
        await SM_8_9_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');
        });
    });
    (0, SM_8_9_1.test)('Connector Secrets User Flow', async ({ operateHomePage, modelerHomePage, navigationPage, modelerCreatePage, connectorSettingsPage, operateProcessInstancePage, operateProcessesPage, connectorMarketplacePage, }) => {
        SM_8_9_1.test.slow();
        const processName = 'REST_Connector_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 modelerHomePage.clickCrossComponentProjectFolder();
            await modelerHomePage.clickDiagramTypeDropdown();
            await modelerHomePage.clickBpmnTemplateOption();
        });
        await SM_8_9_1.test.step('Create BPMN Diagram with REST Connector using secrets 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: '{{secrets.username}}', password: '{{secrets.password}}' });
            await modelerCreatePage.runProcessInstance('', mtTenant);
            await (0, sleep_1.sleep)(20000);
        });
        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.assertProcessVariableContainsText('message', '"Message from Mock!"');
        });
    });
    (0, SM_8_9_1.test)('Intermediate Event Webhook Connector No Auth User Flow', async ({ modelerHomePage, modelerCreatePage, request, operateHomePage, operateProcessInstancePage, operateProcessesPage, navigationPage, connectorMarketplacePage, connectorSettingsPage, }) => {
        SM_8_9_1.test.slow();
        const randomString = await (0, _setup_1.generateRandomStringAsync)(3);
        const processName = 'Intermediate_Event_Webhook_Connector_No_Auth_Process' + randomString;
        await SM_8_9_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 SM_8_9_1.test.step('Create BPMN Diagram with Intermediate Event Webhook Connector and Run Diagram', async () => {
            await (0, test_1.expect)(modelerCreatePage.generalPanel).toBeVisible({
                timeout: 120000,
            });
            await modelerCreatePage.enterDiagramName(processName);
            await (0, sleep_1.sleep)(1000);
            await modelerCreatePage.clickCanvas();
            await modelerCreatePage.clickGeneralPropertiesPanel();
            await modelerCreatePage.clickIdInput();
            await modelerCreatePage.fillIdInput(processName);
            await modelerCreatePage.assertNameInput(processName);
            await (0, sleep_1.sleep)(1000);
            await modelerCreatePage.clickStartEventElement();
            await modelerCreatePage.clickAppendElementButton();
            await modelerCreatePage.clickIntermediateBoundaryEvent();
            await modelerCreatePage.clickChangeTypeButton();
            try {
                await (0, test_1.expect)(modelerCreatePage.webhookMessageStartEventConnectorOption).toBeVisible({
                    timeout: 15000,
                });
            }
            catch (error) {
                await modelerCreatePage.clickMarketPlaceButton();
                await connectorMarketplacePage.clickSearchForConnectorTextbox();
                await connectorMarketplacePage.fillSearchForConnectorTextbox('Webhook Connector');
                await (0, sleep_1.sleep)(10000);
                await connectorMarketplacePage.downloadConnectorToProject();
            }
            await modelerCreatePage.clickIntermediateWebhookConnectorOption();
            await connectorSettingsPage.clickCorrelationTab();
            await connectorSettingsPage.clickWebhookConfigurationTab();
            await modelerCreatePage.clickWebhookIdInput();
            await modelerCreatePage.clearWebhookIdInput();
            await modelerCreatePage.fillWebhookIdInput('test-webhook-intermediate' + randomString);
            await modelerCreatePage.clickCorrelationKeyProcessInput();
            await modelerCreatePage.fillCorrelationKeyProcessInput('"test"');
            await modelerCreatePage.clickCorrelationKeyPayloadInput();
            await modelerCreatePage.fillCorrelationKeyPayloadInput('"test"');
            await modelerCreatePage.clickAppendElementButton();
            await modelerCreatePage.clickAppendEndEventButton();
            await (0, sleep_1.sleep)(10000);
            await modelerCreatePage.runProcessInstance('', mtTenant);
            await (0, sleep_1.sleep)(20000);
        });
        await SM_8_9_1.test.step('Make Authorization Request', async () => {
            const baseURL = process.env.PLAYWRIGHT_BASE_URL ||
                `http://gke-${process.env.BASE_URL}.ci.distro.ultrawombat.com`;
            const url = `${baseURL}/connectors/inbound/test-webhook-intermediate${randomString}`;
            // Poll for inbound endpoint readiness — connector deployment is async after PI start
            const deadline = Date.now() + 180000;
            let response;
            let attempt = 0;
            while (Date.now() < deadline) {
                attempt++;
                response = await request.post(url, { data: { test: 'test' } });
                if (response.status() === 200)
                    break;
                console.error(`Webhook readiness attempt ${attempt}: ${url} -> ${response.status()}`);
                await (0, sleep_1.sleep)(10000);
            }
            if (!response || response.status() !== 200) {
                throw new Error(`Webhook request failed with status ${response?.status()} after ${attempt} attempts`);
            }
            await (0, sleep_1.sleep)(60000);
        });
        await SM_8_9_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_9_1.test.skip('Connectors Timer Event User Flow', async ({ page, modelerHomePage, modelerCreatePage, connectorMarketplacePage, connectorTemplatePage, navigationPage, operateProcessInstancePage, operateHomePage, operateProcessesPage, }) => {
        SM_8_9_1.test.slow();
        const processName = 'Connectors_Timer_Event_Process' + (await (0, _setup_1.generateRandomStringAsync)(3));
        await SM_8_9_1.test.step('Open Cross Component Test Project and Create a REST Connector Document Handling Flow', async () => {
            await (0, UtilitiesPage_1.modelAndRunConnectorsTimerEventDiagram)(modelerCreatePage, modelerHomePage, processName, page, connectorMarketplacePage, connectorTemplatePage, mtTenant);
        });
        await SM_8_9_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,
            });
        });
    });
});
