"use strict";
/*
 * Copyright Camunda Services GmbH and/or licensed to Camunda Services GmbH under
 * one or more contributor license agreements. See the NOTICE file distributed
 * with this work for additional information regarding copyright ownership.
 * Licensed under the Camunda License 1.0. You may not use this file
 * except in compliance with the Camunda License 1.0.
 */
Object.defineProperty(exports, "__esModule", { value: true });
const c8Run_8_7_1 = require("../../fixtures/c8Run-8.7");
const zeebeClient_1 = require("../../utils/zeebeClient");
const test_1 = require("@playwright/test");
const UtilitiesPage_1 = require("../../pages/c8Run-8.7/UtilitiesPage");
const sleep_1 = require("../../utils/sleep");
const constants_1 = require("../../utils/constants");
const _setup_1 = require("../../test-setup.js");
const tasklistBaseURL = (0, UtilitiesPage_1.getBaseAppUrl)('tasklist');
if (process.env.RUN_AS_DOCKER_COMPOSE == 'true') {
    c8Run_8_7_1.test.beforeAll(async () => {
        await Promise.all([
            (0, zeebeClient_1.deploy)('./resources/User_Task_Process_With_Form_API.bpmn'),
            (0, zeebeClient_1.deploy)('./resources/New Form.form'),
        ]);
        await Promise.all([(0, zeebeClient_1.createInstances)('Form_User_Task_API', 1, 3)]);
        await (0, sleep_1.sleep)(5000);
    });
    let tasklistToken = '';
    c8Run_8_7_1.test.describe('Tasklist API tests', () => {
        c8Run_8_7_1.test.use({
            baseURL: tasklistBaseURL,
        });
        c8Run_8_7_1.test.beforeEach(async ({ page, identityLoginPage, identityHomePage, request }) => {
            await (0, UtilitiesPage_1.navigateToApp)(page, 'identity');
            await identityLoginPage.login('demo', 'demo');
            await identityHomePage.assignPermissionToTasklistAPI();
            tasklistToken = await (0, UtilitiesPage_1.getAccessTokenFor)(request, 'tasklist');
        });
        c8Run_8_7_1.test.afterEach(async ({ page }, testInfo) => {
            await (0, _setup_1.captureScreenshot)(page, testInfo);
            await (0, _setup_1.captureFailureVideo)(page, testInfo);
        });
        (0, c8Run_8_7_1.test)('Search for tasks', async ({ request }) => {
            await (0, test_1.expect)(async () => {
                const taskList = await request.post(tasklistBaseURL + '/v1/tasks/search', {
                    headers: {
                        Authorization: `Bearer ${tasklistToken}`,
                        'Content-Type': 'application/json',
                    },
                });
                (0, test_1.expect)(taskList.status()).toBe(200);
            }).toPass(constants_1.defaultAssertionOptions);
        });
        (0, c8Run_8_7_1.test)('Get a task via ID', async ({ request }) => {
            let taskData = [];
            await (0, test_1.expect)(async () => {
                const response = await request.post(tasklistBaseURL + '/v1/tasks/search', {
                    headers: {
                        Authorization: `Bearer ${tasklistToken}`,
                        'Content-Type': 'application/json',
                    },
                });
                (0, test_1.expect)(response.status()).toBe(200);
                taskData = await response.json();
                (0, test_1.expect)(taskData.length).toBeGreaterThan(0);
            }).toPass(constants_1.defaultAssertionOptions);
            await (0, test_1.expect)(async () => {
                const response = await request.get(tasklistBaseURL + `/v1/tasks/${taskData[0].id}`, {
                    headers: {
                        Authorization: `Bearer ${tasklistToken}`,
                        'Content-Type': 'application/json',
                    },
                });
                (0, test_1.expect)(response.status()).toBe(200);
            }).toPass(constants_1.defaultAssertionOptions);
        });
    });
}
