"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
/* eslint-disable @typescript-eslint/no-unused-vars */
/* eslint-disable prefer-const */
/* eslint-disable @typescript-eslint/no-explicit-any */
const _8_8_1 = require("../../fixtures/8.8");
const test_1 = require("@playwright/test");
const apiHelpers_1 = require("../../utils/apiHelpers");
const randomName_1 = require("../../utils/randomName");
_8_8_1.test.describe.configure({ mode: 'parallel' });
_8_8_1.test.describe('API optimize SaaS Tests', () => {
    let optimizeCookie;
    let optimizeBearerToken;
    let collectionIdValue;
    let dashboardIdValue;
    let collectionScopeResponse;
    let reportId;
    let baseUrl;
    let exportedEntities;
    _8_8_1.test.beforeAll(async ({ browser, request }) => {
        const page = await browser.newPage();
        optimizeCookie = await (0, apiHelpers_1.getOptimizeCoockie)(page);
        await page.close();
        optimizeBearerToken = await (0, apiHelpers_1.authSaasAPI)(process.env.OPTIMIZE_API_TOKEN_AUDIENCE);
        collectionIdValue = await (0, apiHelpers_1.createCollection)(request, {
            name: await (0, randomName_1.randomNameAgregator)('Test Collection'),
            optimizeCookie,
        });
        collectionScopeResponse = await (0, apiHelpers_1.updateCollectionScope)(request, {
            optimizeCookie,
            collectionId: collectionIdValue,
            data: [
                {
                    definitionKey: 'customer_onboarding_en',
                    definitionType: 'process',
                    tenants: ['<default>'],
                },
            ],
        });
        dashboardIdValue = await (0, apiHelpers_1.createDashboard)(request, {
            name: await (0, randomName_1.randomNameAgregator)('Test Dashboard'),
            optimizeCookie,
            collectionId: collectionIdValue,
        });
        reportId = await (0, apiHelpers_1.createSingleProcessReport)(request, {
            optimizeCookie,
            collectionId: collectionIdValue,
            name: 'Blank report',
            definitions: [
                {
                    key: 'aProcess',
                    filter: [],
                    groupBy: { type: 'flowNodes', value: null },
                    distributedBy: { type: 'none', value: null },
                    view: { entity: 'flowNode', properties: ['duration'] },
                },
            ],
        });
        baseUrl = process.env.CAMUNDA_OPTIMIZE_BASE_URL;
    });
    (0, _8_8_1.test)('Dashboard Scenarios Flow Tests', async ({ request }) => {
        await _8_8_1.test.step('Get dashboards successfully (200)', async () => {
            const response = await request.get(`${baseUrl}/api/public/dashboard?collectionId=${collectionIdValue}`, { headers: { Authorization: optimizeBearerToken } });
            await (0, apiHelpers_1.assertResponseStatus)(response, 200);
            const body = await response.json();
            (0, test_1.expect)(Array.isArray(body)).toBeTruthy();
            (0, test_1.expect)(body.length).toBeGreaterThan(0);
            const dashboard = body[0];
            (0, test_1.expect)(dashboard).toHaveProperty('id');
        });
        await _8_8_1.test.step('Get dashboards without token (401)', async () => {
            const response = await request.get(`${baseUrl}/api/public/dashboard?collectionId=${collectionIdValue}`);
            (0, test_1.expect)(response.status()).toBe(401);
        });
        await _8_8_1.test.step('Get dashboards with invalid token (401)', async () => {
            const response = await request.get(`${baseUrl}/api/public/dashboard?collectionId=${collectionIdValue}`, { headers: { Authorization: 'Bearer invalid_token' } });
            (0, test_1.expect)(response.status()).toBe(401);
        });
        await _8_8_1.test.step('Force internal server error (500)', async () => {
            const response = await request.get(`${baseUrl}/api/public/dashboard/force-internal-error`, { headers: { Authorization: optimizeBearerToken } });
            (0, test_1.expect)(response.status()).toBe(500);
        });
        await _8_8_1.test.step('Export dashboard definitions successfully (200)', async () => {
            const response = await request.post(`${baseUrl}/api/public/export/dashboard/definition/json`, {
                headers: {
                    Authorization: optimizeBearerToken,
                    'Content-Type': 'application/json',
                },
                data: [dashboardIdValue],
            });
            await (0, apiHelpers_1.assertResponseStatus)(response, 200);
            const body = await response.json();
            (0, test_1.expect)(Array.isArray(body)).toBeTruthy();
        });
        await _8_8_1.test.step('Export dashboards without token (401)', async () => {
            const response = await request.post(`${baseUrl}/api/public/export/dashboard/definition/json`, {
                headers: { 'Content-Type': 'application/json' },
                data: [dashboardIdValue],
            });
            (0, test_1.expect)(response.status()).toBe(401);
        });
        await _8_8_1.test.step('Export dashboards with invalid token (401)', async () => {
            const response = await request.post(`${baseUrl}/api/public/export/dashboard/definition/json`, {
                headers: {
                    Authorization: 'Bearer invalid_token',
                    'Content-Type': 'application/json',
                },
                data: [dashboardIdValue],
            });
            (0, test_1.expect)(response.status()).toBe(401);
        });
        await _8_8_1.test.step('Export dashboard with invalid body (400)', async () => {
            const response = await request.post(`${baseUrl}/api/public/export/dashboard/definition/json`, {
                headers: {
                    Authorization: optimizeBearerToken,
                    'Content-Type': 'application/json',
                },
                data: [{}],
            });
            (0, test_1.expect)(response.status()).toBe(400);
        });
        await _8_8_1.test.step('Export non-existent dashboard (404)', async () => {
            const fakeDashboardId = 'nonexistent-dashboard-id';
            const response = await request.post(`${baseUrl}/api/public/export/dashboard/definition/json`, {
                headers: {
                    Authorization: optimizeBearerToken,
                    'Content-Type': 'application/json',
                },
                data: [fakeDashboardId],
            });
            (0, test_1.expect)([401, 404]).toContain(response.status());
        });
        await _8_8_1.test.step('Export existing dashboard successfully (200)', async () => {
            const response = await request.post(`${baseUrl}/api/public/export/dashboard/definition/json`, {
                headers: {
                    Authorization: optimizeBearerToken,
                    'Content-Type': 'application/json',
                },
                data: [dashboardIdValue],
            });
            await (0, apiHelpers_1.assertResponseStatus)(response, 200);
        });
        await _8_8_1.test.step('Delete dashboard successfully (200)', async () => {
            const response = await request.delete(`${baseUrl}/api/public/dashboard/${dashboardIdValue}`, { headers: { Authorization: optimizeBearerToken } });
            await (0, apiHelpers_1.assertResponseStatus)(response, 200);
        });
        await _8_8_1.test.step('Verify dashboard not found after deletion (404/410/500)', async () => {
            const response = await request.get(`${baseUrl}/api/public/dashboard/${dashboardIdValue}`, { headers: { Authorization: optimizeBearerToken } });
            (0, test_1.expect)([404, 410, 500]).toContain(response.status());
        });
        await _8_8_1.test.step('Delete dashboard with invalid token (401)', async () => {
            const response = await request.delete(`${baseUrl}/api/public/dashboard/${dashboardIdValue}`, { headers: { Authorization: 'Bearer invalid_token' } });
            (0, test_1.expect)(response.status()).toBe(401);
        });
        await _8_8_1.test.step('Delete non-existent dashboard (404)', async () => {
            const fakeDashboardId = 'nonexistent-dashboard-id';
            const response = await request.delete(`${baseUrl}/api/public/dashboard/${fakeDashboardId}`, { headers: { Authorization: optimizeBearerToken } });
            (0, test_1.expect)(response.status()).toBe(404);
        });
    });
    (0, _8_8_1.test)('Reports Scenarios Flow Tests', async ({ request }) => {
        await _8_8_1.test.step('Get reports successfully (200)', async () => {
            const response = await request.get(`${baseUrl}/api/public/report?collectionId=${collectionIdValue}`, { headers: { Authorization: optimizeBearerToken } });
            await (0, apiHelpers_1.assertResponseStatus)(response, 200);
        });
        await _8_8_1.test.step('Get reports with invalid/missing token (401)', async () => {
            const response = await request.get(`${baseUrl}/api/public/report?collectionId=${collectionIdValue}`, { headers: { Authorization: 'Bearer invalid_token' } });
            (0, test_1.expect)(response.status()).toBe(401);
        });
        await _8_8_1.test.step('Export reports successfully (200)', async () => {
            const response = await request.post(`${baseUrl}/api/public/export/report/definition/json`, {
                headers: {
                    Authorization: optimizeBearerToken,
                    'Content-Type': 'application/json',
                },
                data: [reportId],
            });
            await (0, apiHelpers_1.assertResponseStatus)(response, 200);
            const body = await response.json();
            (0, test_1.expect)(Array.isArray(body)).toBeTruthy();
            (0, test_1.expect)(body.length).toBeGreaterThan(0);
            (0, test_1.expect)(body[0]).toHaveProperty('id');
            (0, test_1.expect)(body[0]).toHaveProperty('exportEntityType');
            (0, test_1.expect)(body[0]).toHaveProperty('name');
            (0, test_1.expect)(body[0]).toHaveProperty('collectionId');
        });
        await _8_8_1.test.step('Export reports with invalid token (401)', async () => {
            const response = await request.post(`${baseUrl}/api/public/export/report/definition/json`, {
                headers: {
                    Authorization: 'Bearer invalid_token',
                    'Content-Type': 'application/json',
                },
                data: [reportId],
            });
            (0, test_1.expect)(response.status()).toBe(401);
        });
        await _8_8_1.test.step('Export non-existent report (404)', async () => {
            const invalidReportId = '11111111-1111-1111-1111-111111111111';
            const response = await request.post(`${baseUrl}/api/public/export/report/definition/json`, {
                headers: {
                    Authorization: optimizeBearerToken,
                    'Content-Type': 'application/json',
                },
                data: [invalidReportId],
            });
            await (0, apiHelpers_1.assertResponseStatus)(response, 404);
        });
        await _8_8_1.test.step('Delete report successfully (200)', async () => {
            const response = await request.delete(`${baseUrl}/api/public/report/${reportId}`, { headers: { Authorization: optimizeBearerToken } });
            await (0, apiHelpers_1.assertResponseStatus)(response, 200);
        });
        await _8_8_1.test.step('Verify deleted report returns 404 or 500', async () => {
            const response = await request.get(`${baseUrl}/api/public/report/${reportId}`, { headers: { Authorization: optimizeBearerToken } });
            (0, test_1.expect)([404, 500]).toContain(response.status());
        });
        await _8_8_1.test.step('Attempt delete report with invalid token (401)', async () => {
            const response = await request.delete(`${baseUrl}/api/public/report/${reportId}`, { headers: { Authorization: 'Bearer invalid_token' } });
            (0, test_1.expect)(response.status()).toBe(401);
        });
    });
    (0, _8_8_1.test)('Import entities successfully (200)', async ({ request }) => {
        const exportResponse = await request.post(`${baseUrl}/api/public/export/dashboard/definition/json`, {
            headers: {
                Authorization: optimizeBearerToken,
                'Content-Type': 'application/json',
            },
            data: [dashboardIdValue],
        });
        await (0, apiHelpers_1.assertResponseStatus)(exportResponse, 200);
        exportedEntities = await exportResponse.json();
        const response = await request.post(`${baseUrl}/api/public/import?collectionId=${collectionIdValue}`, {
            headers: {
                Authorization: optimizeBearerToken,
                'Content-Type': 'application/json',
            },
            data: exportedEntities,
        });
        await (0, apiHelpers_1.assertResponseStatus)(response, 200);
    });
    (0, _8_8_1.test)('Import without token or invalid token (401)', async ({ request }) => {
        const response = await request.post(`${baseUrl}/api/public/import?collectionId=${collectionIdValue}`, {
            headers: {
                Authorization: 'Bearer invalid_token',
                'Content-Type': 'application/json',
            },
            data: exportedEntities,
        });
        const status = response.status();
        (0, test_1.expect)(status).toBe(401);
    });
    // Skipped due to bug 40497: https://github.com/camunda/camunda/issues/40497
    _8_8_1.test.skip('Import to non-existent collection (404)', async ({ request }) => {
        const fakeCollectionId = 'nonexistent-collection-id';
        const response = await request.post(`${baseUrl}/api/public/import?collectionId=${fakeCollectionId}`, {
            headers: {
                Authorization: optimizeBearerToken,
                'Content-Type': 'application/json',
            },
            data: exportedEntities,
        });
        const status = response.status();
        (0, test_1.expect)(status).toBe(404);
    });
});
