"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const test_1 = require("@playwright/test");
const _8_10_1 = require("../../fixtures/8.10");
const apiHelpers_1 = require("../../utils/apiHelpers");
const randomName_1 = require("../../utils/randomName");
_8_10_1.test.describe.configure({ mode: 'parallel' });
_8_10_1.test.describe('API Optimize Collections Lifecycle Tests @optimize-collections-api @8.10', () => {
    let optimizeCookie;
    let optimizeBearerToken;
    let baseUrl;
    _8_10_1.test.beforeAll(async ({ browser }) => {
        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);
        baseUrl = process.env.CAMUNDA_OPTIMIZE_BASE_URL;
    });
    (0, _8_10_1.test)('Create a collection and retrieve it by ID (200)', async ({ request }) => {
        const collectionName = await (0, randomName_1.randomNameAgregator)('Collection Create');
        const collectionId = await (0, apiHelpers_1.createCollection)(request, {
            name: collectionName,
            optimizeCookie,
        });
        await _8_10_1.test.step('GET /api/collection/{id} returns the created collection (200)', async () => {
            const response = await request.get(`${baseUrl}/api/collection/${collectionId}`, { headers: { Cookie: optimizeCookie } });
            await (0, apiHelpers_1.assertResponseStatus)(response, 200);
            const body = await response.json();
            (0, test_1.expect)(body).toHaveProperty('id', collectionId);
            (0, test_1.expect)(body).toHaveProperty('name', collectionName);
        });
        await request.delete(`${baseUrl}/api/public/collection/${collectionId}`, {
            headers: { Authorization: optimizeBearerToken },
        });
    });
    (0, _8_10_1.test)('Rename a collection updates its name', async ({ request }) => {
        const originalName = await (0, randomName_1.randomNameAgregator)('Collection Original');
        const collectionId = await (0, apiHelpers_1.createCollection)(request, {
            name: originalName,
            optimizeCookie,
        });
        const renamedName = await (0, randomName_1.randomNameAgregator)('Collection Renamed');
        await _8_10_1.test.step('PUT /api/collection/{id} updates the collection name', async () => {
            const response = await request.put(`${baseUrl}/api/collection/${collectionId}`, {
                headers: {
                    Cookie: optimizeCookie,
                    'Content-Type': 'application/json',
                },
                data: { name: renamedName },
            });
            (0, test_1.expect)([200, 204]).toContain(response.status());
        });
        await _8_10_1.test.step('GET /api/collection/{id} reflects the new name', async () => {
            const response = await request.get(`${baseUrl}/api/collection/${collectionId}`, { headers: { Cookie: optimizeCookie } });
            await (0, apiHelpers_1.assertResponseStatus)(response, 200);
            const body = await response.json();
            (0, test_1.expect)(body).toHaveProperty('name', renamedName);
        });
        await request.delete(`${baseUrl}/api/public/collection/${collectionId}`, {
            headers: { Authorization: optimizeBearerToken },
        });
    });
    (0, _8_10_1.test)('Update collection scope adds a process definition (200)', async ({ request, }) => {
        const collectionName = await (0, randomName_1.randomNameAgregator)('Collection Scope');
        const collectionId = await (0, apiHelpers_1.createCollection)(request, {
            name: collectionName,
            optimizeCookie,
        });
        const definitionKey = 'customer_onboarding_en';
        await _8_10_1.test.step('PUT /api/collection/{id}/scope adds a process definition', async () => {
            await (0, apiHelpers_1.updateCollectionScope)(request, {
                optimizeCookie,
                collectionId,
                data: [
                    {
                        definitionKey,
                        definitionType: 'process',
                        tenants: ['<default>'],
                    },
                ],
            });
        });
        await _8_10_1.test.step('GET /api/collection/{id} exposes the added scope entry', async () => {
            const response = await request.get(`${baseUrl}/api/collection/${collectionId}`, { headers: { Cookie: optimizeCookie } });
            await (0, apiHelpers_1.assertResponseStatus)(response, 200);
            const body = await response.json();
            const scope = body?.data?.scope;
            (0, test_1.expect)(Array.isArray(scope)).toBeTruthy();
            (0, test_1.expect)(scope.some((entry) => entry.definitionKey === definitionKey)).toBeTruthy();
        });
        await request.delete(`${baseUrl}/api/public/collection/${collectionId}`, {
            headers: { Authorization: optimizeBearerToken },
        });
    });
    (0, _8_10_1.test)('Delete a collection and verify it is no longer retrievable', async ({ request, }) => {
        const collectionName = await (0, randomName_1.randomNameAgregator)('Collection Delete');
        const collectionId = await (0, apiHelpers_1.createCollection)(request, {
            name: collectionName,
            optimizeCookie,
        });
        let deleteSucceeded = false;
        await _8_10_1.test.step('DELETE /api/public/collection/{id} is accepted (200; SaaS may respond 500)', async () => {
            const response = await request.delete(`${baseUrl}/api/public/collection/${collectionId}`, { headers: { Authorization: optimizeBearerToken } });
            (0, test_1.expect)([200, 500]).toContain(response.status());
            deleteSucceeded = response.status() === 200;
        });
        await _8_10_1.test.step('Deleted collection is no longer retrievable when delete succeeded', async () => {
            const response = await request.get(`${baseUrl}/api/collection/${collectionId}`, { headers: { Cookie: optimizeCookie } });
            const acceptableStatuses = deleteSucceeded
                ? [401, 404, 500]
                : [200, 401, 404, 500];
            (0, test_1.expect)(acceptableStatuses).toContain(response.status());
        });
    });
    (0, _8_10_1.test)('Create collection without auth cookie returns 401', async ({ request, }) => {
        await _8_10_1.test.step('POST /api/collection without cookie (401)', async () => {
            const response = await request.post(`${baseUrl}/api/collection`, {
                headers: { 'Content-Type': 'application/json' },
                data: { name: await (0, randomName_1.randomNameAgregator)('Unauthorized Collection') },
            });
            await (0, apiHelpers_1.assertUnauthorizedResponseBody)(response);
        });
    });
    (0, _8_10_1.test)('Get collection without auth cookie returns 401', async ({ request }) => {
        const collectionName = await (0, randomName_1.randomNameAgregator)('Collection Auth Get');
        const collectionId = await (0, apiHelpers_1.createCollection)(request, {
            name: collectionName,
            optimizeCookie,
        });
        await _8_10_1.test.step('GET /api/collection/{id} without cookie (401)', async () => {
            const response = await request.get(`${baseUrl}/api/collection/${collectionId}`);
            await (0, apiHelpers_1.assertUnauthorizedResponseBody)(response);
        });
        await request.delete(`${baseUrl}/api/public/collection/${collectionId}`, {
            headers: { Authorization: optimizeBearerToken },
        });
    });
    (0, _8_10_1.test)('Delete collection without token returns 401', async ({ request }) => {
        const collectionName = await (0, randomName_1.randomNameAgregator)('Collection Auth Delete');
        const collectionId = await (0, apiHelpers_1.createCollection)(request, {
            name: collectionName,
            optimizeCookie,
        });
        await _8_10_1.test.step('DELETE /api/public/collection/{id} without token (401)', async () => {
            const response = await request.delete(`${baseUrl}/api/public/collection/${collectionId}`);
            await (0, apiHelpers_1.assertUnauthorizedResponseBody)(response);
        });
        await request.delete(`${baseUrl}/api/public/collection/${collectionId}`, {
            headers: { Authorization: optimizeBearerToken },
        });
    });
    (0, _8_10_1.test)('Delete collection with invalid token returns 401', async ({ request, }) => {
        const collectionName = await (0, randomName_1.randomNameAgregator)('Collection Invalid Token');
        const collectionId = await (0, apiHelpers_1.createCollection)(request, {
            name: collectionName,
            optimizeCookie,
        });
        await _8_10_1.test.step('DELETE /api/public/collection/{id} with invalid token (401)', async () => {
            const response = await request.delete(`${baseUrl}/api/public/collection/${collectionId}`, { headers: { Authorization: 'Bearer invalid_token' } });
            await (0, apiHelpers_1.assertUnauthorizedResponseBody)(response);
        });
        await request.delete(`${baseUrl}/api/public/collection/${collectionId}`, {
            headers: { Authorization: optimizeBearerToken },
        });
    });
    (0, _8_10_1.test)('Delete non-existent collection is rejected (404; SaaS may respond 500)', async ({ request, }) => {
        await _8_10_1.test.step('DELETE /api/public/collection/{id} for missing collection', async () => {
            const response = await request.delete(`${baseUrl}/api/public/collection/nonexistent-collection-id`, { headers: { Authorization: optimizeBearerToken } });
            (0, test_1.expect)([404, 500]).toContain(response.status());
        });
    });
});
