"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const test_1 = require("@playwright/test");
const SM_8_10_1 = require("../../fixtures/SM-8.10");
const _setup_1 = require("../../test-setup.js");
const sleep_1 = require("../../utils/sleep");
const UtilitiesPage_1 = require("../../pages/SM-8.10/UtilitiesPage");
SM_8_10_1.test.describe.configure({ mode: 'parallel' });
SM_8_10_1.test.describe('Deploy and run a process in Play', () => {
    SM_8_10_1.test.beforeEach(async ({ navigationPage }, testInfo) => {
        await navigationPage.goToModeler((testInfo.workerIndex + 1) * 1000);
    });
    SM_8_10_1.test.afterEach(async ({ page }, testInfo) => {
        await (0, _setup_1.captureScreenshot)(page, testInfo);
        await (0, _setup_1.captureFailureVideo)(page, testInfo);
    });
    (0, SM_8_10_1.test)('User Tasks and Service Task', async ({ page, modelerHomePage, modelerCreatePage, playPage, }) => {
        SM_8_10_1.test.slow();
        const randomString = await (0, _setup_1.generateRandomStringAsync)(3);
        const processName = 'Play_Test_Process' + randomString;
        await SM_8_10_1.test.step('Open Cross Component Test Project', async () => {
            await modelerHomePage.clickCrossComponentProjectFolder();
        });
        await SM_8_10_1.test.step('Create a BPMN Diagram with user tasks and service task', async () => {
            await (0, UtilitiesPage_1.modelDiagramFromFile)(page, modelerHomePage, modelerCreatePage, processName, 'Play_Test_User_Service_Task');
        });
        await SM_8_10_1.test.step('Open Play', async () => {
            await modelerCreatePage.switchToPlay();
            await modelerCreatePage.completePlayConfiguration();
        });
        await SM_8_10_1.test.step('Start and complete the process instance in Play', async () => {
            await playPage.dismissStartModal();
            await playPage.clickStartInstanceButton();
            await playPage.waitForInstanceDetailsToBeLoaded();
            await playPage.waitForNextElementToBeActive('play-user-task');
            await playPage.waitForCompleteJobButtonToBeAvailable();
            await playPage.clickCompleteJobButton();
            await playPage.waitForNextElementToBeActive('play-service-task');
            await playPage.waitForCompleteJobButtonToBeAvailable();
            await playPage.clickCompleteJobButton();
            await playPage.waitForProcessToBeCompleted();
        });
    });
    (0, SM_8_10_1.test)('Create,update and delete test scenarios in play', async ({ modelerHomePage, modelerCreatePage, playPage, }) => {
        SM_8_10_1.test.slow();
        const randomString = await (0, _setup_1.generateRandomStringAsync)(3);
        const processName = 'Play_Test_Process' + randomString;
        const scenarioName = 'My first scenario' + randomString;
        const updatedScenarioName = 'My first scenario-updated' + randomString;
        await SM_8_10_1.test.step('Open Cross Component Test Project', async () => {
            await modelerHomePage.clickCrossComponentProjectFolder();
        });
        await SM_8_10_1.test.step('Add A BPMN Template To The Project', async () => {
            await modelerHomePage.clickDiagramTypeDropdown();
            await modelerHomePage.clickBpmnTemplateOption();
        });
        await SM_8_10_1.test.step('Create a BPMN Diagram with simple start and end event', async () => {
            await (0, test_1.expect)(modelerCreatePage.generalPanel).toBeVisible({
                timeout: 120000,
            });
            await modelerCreatePage.enterDiagramName(processName);
            await modelerCreatePage.addParallelUserTasks(1, 'zeebe-user-task' + randomString);
            await modelerCreatePage.clickSecondPlacedGateway();
            await modelerCreatePage.clickAppendElementButton();
            await modelerCreatePage.clickAppendEndEventButton('parallelGateway');
            await (0, test_1.expect)(modelerCreatePage.generalPanel).toBeVisible({
                timeout: 30000,
            });
            // Wait for modeler background auto-save to complete
            await (0, sleep_1.sleep)(2000);
        });
        await SM_8_10_1.test.step('Open Play', async () => {
            await modelerCreatePage.switchToPlay();
            await modelerCreatePage.completePlayConfiguration();
        });
        await SM_8_10_1.test.step('Start and complete the process instance in Play and save scenario', async () => {
            await playPage.dismissStartModal();
            await playPage.clickStartInstanceButton();
            await playPage.waitForInstanceDetailsToBeLoaded();
            await playPage.waitForNextElementToBeActive('zeebe-user-task' + randomString + '1');
            await playPage.waitForCompleteJobButtonToBeAvailable();
            await playPage.clickCompleteJobButton();
            await playPage.waitForProcessToBeCompleted();
            await playPage.waitForScenarioRecordPanelReady();
            await playPage.enterScenarioName(scenarioName);
            await playPage.confirmSaveScenario();
        });
        await SM_8_10_1.test.step('Update the saved scenario', async () => {
            await modelerCreatePage.switchToImplementTab();
            await (0, test_1.expect)(modelerCreatePage.generalPanel).toBeVisible({
                timeout: 30000,
            });
            await modelerCreatePage.clickFirstPlacedGateway();
            // On slow clusters clickFirstPlacedGateway() may trigger page.reload();
            // the canvas needs settling before the context-pad JavaScript handler
            // attaches. Wait up to 20 s for the context pad, then re-click the
            // gateway directly (canvas is settled by then) if it still isn't open.
            if (!(await modelerCreatePage.appendElementButton
                .isVisible({ timeout: 20000 })
                .catch(() => false))) {
                await modelerCreatePage.firstPlacedGateway.click({ timeout: 10000 });
            }
            await (0, test_1.expect)(modelerCreatePage.appendElementButton).toBeVisible({
                timeout: 15000,
            });
            await modelerCreatePage.clickAppendElementButton();
            await modelerCreatePage.clickAppendTaskButton();
            await modelerCreatePage.clickConnectToOtherElementButton();
            await modelerCreatePage.clickSecondPlacedGateway();
            await (0, test_1.expect)(modelerCreatePage.generalPanel).toBeVisible({
                timeout: 30000,
            });
            // Wait for modeler background auto-save to complete
            await (0, sleep_1.sleep)(2000);
            await modelerCreatePage.switchToPlay();
            // After diagram modification, Play shows the setup panel again (new flow)
            // or a Continue button (legacy flow), or loads directly.
            await modelerCreatePage.completePlayConfiguration();
            await playPage.dismissStartModal();
            await playPage.clickStartInstanceButton();
            await playPage.waitForNextElementToBeActive('zeebe-user-task' + randomString + '1');
            await playPage.waitForCompleteJobButtonToBeAvailable();
            await playPage.clickCompleteJobButton();
            await playPage.waitForProcessToBeCompleted();
            await playPage.waitForScenarioRecordPanelReady();
            await playPage.enterScenarioName(updatedScenarioName);
            await playPage.confirmSaveScenario();
            await playPage.assertSavedScenarioNameInPanel(updatedScenarioName);
        });
        await SM_8_10_1.test.step('Run all scenarios', async () => {
            await playPage.clickViewAllScenariosButton();
            await playPage.clickRunAllScenariosButton();
            await playPage.assertScenarioCompleted(scenarioName);
            await playPage.assertScenarioCompleted(updatedScenarioName);
        });
        await SM_8_10_1.test.step('Delete test scenario', async () => {
            await playPage.deleteScenario(scenarioName);
            await playPage.confirmDeleteScenario();
            await (0, test_1.expect)(playPage.getScenarioRow(scenarioName)).toHaveCount(0, {
                timeout: 10000,
            });
        });
    });
});
