"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const SM_8_9_1 = require("../../fixtures/SM-8.9");
const test_1 = require("@playwright/test");
const apiHelpers_1 = require("../../utils/apiHelpers");
const randomName_1 = require("../../utils/randomName");
SM_8_9_1.test.describe.configure({ mode: 'parallel' });
SM_8_9_1.test.describe('API optimize SM Tests', () => {
    const baseUrl = process.env.CAMUNDA_OPTIMIZE_BASE_URL;
    (0, SM_8_9_1.test)('Dashboard Scenarios Flow Tests', async ({ request, browser }) => {
        const page = await browser.newPage();
        const optimizeCookie = await (0, apiHelpers_1.getOptimizeCookieSm)(page);
        await page.close();
        const optimizeBearerToken = await (0, apiHelpers_1.authSmAPI)();
        const collectionIdValue = await (0, apiHelpers_1.createCollection)(request, {
            name: await (0, randomName_1.randomNameAgregator)('Test Collection'),
            optimizeCookie,
        });
        await (0, apiHelpers_1.updateCollectionScope)(request, {
            optimizeCookie,
            collectionId: collectionIdValue,
            data: [
                {
                    definitionKey: 'customer_onboarding_en',
                    definitionType: 'process',
                    tenants: ['<default>'],
                },
            ],
        });
        const dashboardIdValue = await (0, apiHelpers_1.createDashboard)(request, {
            name: await (0, randomName_1.randomNameAgregator)('Test Dashboard'),
            optimizeCookie,
            collectionId: collectionIdValue,
        });
        await SM_8_9_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 SM_8_9_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 SM_8_9_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 SM_8_9_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 SM_8_9_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 SM_8_9_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 SM_8_9_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 SM_8_9_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 SM_8_9_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 SM_8_9_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 SM_8_9_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 SM_8_9_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 SM_8_9_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 SM_8_9_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, SM_8_9_1.test)('Reports Scenarios Flow Tests', async ({ request, browser }) => {
        const page = await browser.newPage();
        const optimizeCookie = await (0, apiHelpers_1.getOptimizeCookieSm)(page);
        await page.close();
        const optimizeBearerToken = await (0, apiHelpers_1.authSmAPI)();
        const collectionIdValue = await (0, apiHelpers_1.createCollection)(request, {
            name: await (0, randomName_1.randomNameAgregator)('Test Collection'),
            optimizeCookie,
        });
        await (0, apiHelpers_1.updateCollectionScope)(request, {
            optimizeCookie,
            collectionId: collectionIdValue,
            data: [
                {
                    definitionKey: 'customer_onboarding_en',
                    definitionType: 'process',
                    tenants: ['<default>'],
                },
            ],
        });
        const 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'] },
                },
            ],
        });
        await SM_8_9_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 SM_8_9_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 SM_8_9_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 SM_8_9_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 SM_8_9_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 SM_8_9_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 SM_8_9_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 SM_8_9_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, SM_8_9_1.test)('Import entities successfully (200)', async ({ request, browser }) => {
        const page = await browser.newPage();
        const optimizeCookie = await (0, apiHelpers_1.getOptimizeCookieSm)(page);
        await page.close();
        const optimizeBearerToken = await (0, apiHelpers_1.authSmAPI)();
        const collectionIdValue = await (0, apiHelpers_1.createCollection)(request, {
            name: await (0, randomName_1.randomNameAgregator)('Test Collection'),
            optimizeCookie,
        });
        await (0, apiHelpers_1.updateCollectionScope)(request, {
            optimizeCookie,
            collectionId: collectionIdValue,
            data: [
                {
                    definitionKey: 'customer_onboarding_en',
                    definitionType: 'process',
                    tenants: ['<default>'],
                },
            ],
        });
        const dashboardIdValue = await (0, apiHelpers_1.createDashboard)(request, {
            name: await (0, randomName_1.randomNameAgregator)('Test Dashboard'),
            optimizeCookie,
            collectionId: collectionIdValue,
        });
        const exportedEntities = await (async () => {
            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);
            return 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, SM_8_9_1.test)('Import without token or invalid token (401)', async ({ request, browser, }) => {
        const page = await browser.newPage();
        const optimizeCookie = await (0, apiHelpers_1.getOptimizeCookieSm)(page);
        await page.close();
        const optimizeBearerToken = await (0, apiHelpers_1.authSmAPI)();
        const collectionIdValue = await (0, apiHelpers_1.createCollection)(request, {
            name: await (0, randomName_1.randomNameAgregator)('Test Collection'),
            optimizeCookie,
        });
        await (0, apiHelpers_1.updateCollectionScope)(request, {
            optimizeCookie,
            collectionId: collectionIdValue,
            data: [
                {
                    definitionKey: 'customer_onboarding_en',
                    definitionType: 'process',
                    tenants: ['<default>'],
                },
            ],
        });
        const dashboardIdValue = await (0, apiHelpers_1.createDashboard)(request, {
            name: await (0, randomName_1.randomNameAgregator)('Test Dashboard'),
            optimizeCookie,
            collectionId: collectionIdValue,
        });
        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);
        const exportedEntities = await exportResponse.json();
        const response = await request.post(`${baseUrl}/api/public/import?collectionId=${collectionIdValue}`, {
            headers: {
                Authorization: 'Bearer invalid_token',
                'Content-Type': 'application/json',
            },
            data: exportedEntities,
        });
        (0, test_1.expect)(response.status()).toBe(401);
    });
    (0, SM_8_9_1.test)('Import to non-existent collection (404)', async ({ request, browser, }) => {
        const page = await browser.newPage();
        const optimizeCookie = await (0, apiHelpers_1.getOptimizeCookieSm)(page);
        await page.close();
        const optimizeBearerToken = await (0, apiHelpers_1.authSmAPI)();
        const collectionIdValue = await (0, apiHelpers_1.createCollection)(request, {
            name: await (0, randomName_1.randomNameAgregator)('Test Collection'),
            optimizeCookie,
        });
        await (0, apiHelpers_1.updateCollectionScope)(request, {
            optimizeCookie,
            collectionId: collectionIdValue,
            data: [
                {
                    definitionKey: 'customer_onboarding_en',
                    definitionType: 'process',
                    tenants: ['<default>'],
                },
            ],
        });
        const dashboardIdValue = await (0, apiHelpers_1.createDashboard)(request, {
            name: await (0, randomName_1.randomNameAgregator)('Test Dashboard'),
            optimizeCookie,
            collectionId: collectionIdValue,
        });
        const exportedEntities = await (async () => {
            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);
            return await exportResponse.json();
        })();
        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,
        });
        (0, test_1.expect)(response.status()).toBe(404);
    });
    (0, SM_8_9_1.test)('Authorization Edge Cases Tests (401)', async ({ request, browser }) => {
        const page = await browser.newPage();
        const optimizeCookie = await (0, apiHelpers_1.getOptimizeCookieSm)(page);
        await page.close();
        const collectionIdValue = await (0, apiHelpers_1.createCollection)(request, {
            name: await (0, randomName_1.randomNameAgregator)('Test Collection'),
            optimizeCookie,
        });
        const expiredToken = 'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJ0ZXN0IiwiZXhwIjoxNjA5NDU5MjAwfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c';
        const malformedToken = 'Bearer eyJhbGciOiJIUzI1NiJ9.';
        const noPrefixToken = 'raw-token-string-without-bearer-prefix';
        await SM_8_9_1.test.step('Expired JWT token on GET dashboard returns 401', async () => {
            const response = await request.get(`${baseUrl}/api/public/dashboard?collectionId=${collectionIdValue}`, { headers: { Authorization: expiredToken } });
            (0, test_1.expect)(response.status()).toBe(401);
        });
        await SM_8_9_1.test.step('Malformed JWT (truncated) on GET dashboard returns 401', async () => {
            const response = await request.get(`${baseUrl}/api/public/dashboard?collectionId=${collectionIdValue}`, { headers: { Authorization: malformedToken } });
            (0, test_1.expect)(response.status()).toBe(401);
        });
        await SM_8_9_1.test.step('Token without Bearer prefix on GET dashboard returns 401', async () => {
            const response = await request.get(`${baseUrl}/api/public/dashboard?collectionId=${collectionIdValue}`, { headers: { Authorization: noPrefixToken } });
            (0, test_1.expect)(response.status()).toBe(401);
        });
        await SM_8_9_1.test.step('Empty Authorization header on GET dashboard returns 401', async () => {
            const response = await request.get(`${baseUrl}/api/public/dashboard?collectionId=${collectionIdValue}`, { headers: { Authorization: '' } });
            (0, test_1.expect)(response.status()).toBe(401);
        });
        await SM_8_9_1.test.step('Missing token on GET reports returns 401', async () => {
            const response = await request.get(`${baseUrl}/api/public/report?collectionId=${collectionIdValue}`);
            (0, test_1.expect)(response.status()).toBe(401);
        });
        await SM_8_9_1.test.step('Missing token on DELETE dashboard returns 401', async () => {
            const response = await request.delete(`${baseUrl}/api/public/dashboard/nonexistent-id`);
            (0, test_1.expect)(response.status()).toBe(401);
        });
        await SM_8_9_1.test.step('Missing token on DELETE report returns 401', async () => {
            const response = await request.delete(`${baseUrl}/api/public/report/nonexistent-id`);
            (0, test_1.expect)(response.status()).toBe(401);
        });
        await SM_8_9_1.test.step('Missing token on import returns 401', async () => {
            const response = await request.post(`${baseUrl}/api/public/import?collectionId=${collectionIdValue}`, {
                headers: { 'Content-Type': 'application/json' },
                data: [],
            });
            (0, test_1.expect)(response.status()).toBe(401);
        });
        await SM_8_9_1.test.step('Expired JWT on export dashboard returns 401', async () => {
            const response = await request.post(`${baseUrl}/api/public/export/dashboard/definition/json`, {
                headers: {
                    Authorization: expiredToken,
                    'Content-Type': 'application/json',
                },
                data: ['some-dashboard-id'],
            });
            (0, test_1.expect)(response.status()).toBe(401);
        });
        await SM_8_9_1.test.step('Expired JWT on export report returns 401', async () => {
            const response = await request.post(`${baseUrl}/api/public/export/report/definition/json`, {
                headers: {
                    Authorization: expiredToken,
                    'Content-Type': 'application/json',
                },
                data: ['some-report-id'],
            });
            (0, test_1.expect)(response.status()).toBe(401);
        });
        await SM_8_9_1.test.step('Malformed JWT on export dashboard returns 401', async () => {
            const response = await request.post(`${baseUrl}/api/public/export/dashboard/definition/json`, {
                headers: {
                    Authorization: malformedToken,
                    'Content-Type': 'application/json',
                },
                data: ['some-dashboard-id'],
            });
            (0, test_1.expect)(response.status()).toBe(401);
        });
        await SM_8_9_1.test.step('Malformed JWT on import returns 401', async () => {
            const response = await request.post(`${baseUrl}/api/public/import?collectionId=${collectionIdValue}`, {
                headers: {
                    Authorization: malformedToken,
                    'Content-Type': 'application/json',
                },
                data: [],
            });
            (0, test_1.expect)(response.status()).toBe(401);
        });
    });
});
