"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 sleep_1 = require("../../utils/sleep");
_8_10_1.test.describe.configure({ mode: 'serial' });
let managementToken;
let clusterUuid;
let originalFilters = null;
let dataFiltersAvailable = false;
const readFilters = async () => {
    const response = await (0, apiHelpers_1.getClusterDataFilters)(managementToken, clusterUuid);
    const status = response.status();
    let body = null;
    try {
        body = (await response.json());
    }
    catch {
        body = null;
    }
    return { status, body };
};
const skipIfUnavailable = () => {
    _8_10_1.test.skip(!dataFiltersAvailable, 'Console Data Filters API not reachable on this cluster (endpoint returned non-2xx or is not yet enabled).');
};
_8_10_1.test.beforeAll(async () => {
    try {
        managementToken = await (0, apiHelpers_1.authConsoleManagementAPI)();
        clusterUuid = (0, apiHelpers_1.getTestClusterUuid)();
        const { status, body } = await readFilters();
        dataFiltersAvailable = status >= 200 && status < 300;
        if (dataFiltersAvailable) {
            originalFilters = body;
        }
    }
    catch (error) {
        dataFiltersAvailable = false;
        console.warn(`Data Filters API precheck failed: ${error instanceof Error ? error.message : String(error)}`);
    }
});
_8_10_1.test.afterAll(async () => {
    if (dataFiltersAvailable && originalFilters) {
        try {
            await (0, apiHelpers_1.updateClusterDataFilters)(managementToken, clusterUuid, originalFilters);
        }
        catch (error) {
            console.warn(`Failed to restore original data filters: ${error instanceof Error ? error.message : String(error)}`);
        }
    }
});
_8_10_1.test.describe('Optimize Data Filters API @tasklistV2', () => {
    _8_10_1.test.slow();
    (0, _8_10_1.test)('T11728029 business_* variable filter enabled by default', async () => {
        skipIfUnavailable();
        await _8_10_1.test.step('Read cluster data filters config', async () => {
            const { status, body } = await readFilters();
            (0, test_1.expect)(status).toBe(200);
            (0, test_1.expect)(body).not.toBeNull();
        });
        await _8_10_1.test.step('Assert business_* variable include filter is present', async () => {
            const { body } = await readFilters();
            const includeNames = body?.variableFilter?.includeVariableNames ?? [];
            const hasBusinessDefault = includeNames.some((name) => name.startsWith('business_'));
            (0, test_1.expect)(hasBusinessDefault).toBe(true);
        });
    });
    (0, _8_10_1.test)('T11728030 default filter configured before deploying processes', async () => {
        skipIfUnavailable();
        await _8_10_1.test.step('Assert variable filter config exists independent of deployed processes', async () => {
            const { status, body } = await readFilters();
            (0, test_1.expect)(status).toBe(200);
            (0, test_1.expect)(body?.variableFilter).toBeDefined();
        });
    });
    (0, _8_10_1.test)('T11728020 enable and persist a variable filter pattern', async () => {
        skipIfUnavailable();
        const pattern = 'e2e_business_*';
        await _8_10_1.test.step('Enable and set variable include filter', async () => {
            const response = await (0, apiHelpers_1.updateClusterDataFilters)(managementToken, clusterUuid, {
                variableFilter: {
                    enabled: true,
                    includeVariableNames: [pattern],
                },
            });
            (0, test_1.expect)([200, 201, 204]).toContain(response.status());
        });
        await _8_10_1.test.step('Assert the pattern persists on re-read', async () => {
            const { body } = await readFilters();
            const includeNames = body?.variableFilter?.includeVariableNames ?? [];
            (0, test_1.expect)(includeNames).toContain(pattern);
            (0, test_1.expect)(body?.variableFilter?.enabled).toBe(true);
        });
    });
    (0, _8_10_1.test)('T11728034 filter change applies without a manual restart', async () => {
        skipIfUnavailable();
        const pattern = 'e2e_no_restart_*';
        await _8_10_1.test.step('Update the variable filter', async () => {
            const response = await (0, apiHelpers_1.updateClusterDataFilters)(managementToken, clusterUuid, {
                variableFilter: {
                    enabled: true,
                    includeVariableNames: [pattern],
                },
            });
            (0, test_1.expect)([200, 201, 204]).toContain(response.status());
        });
        await _8_10_1.test.step('Assert change is reflected without any restart call', async () => {
            let reflected = false;
            for (let attempt = 0; attempt < 5 && !reflected; attempt++) {
                const { body } = await readFilters();
                const includeNames = body?.variableFilter?.includeVariableNames ?? [];
                reflected = includeNames.includes(pattern);
                if (!reflected) {
                    await (0, sleep_1.sleep)(3000);
                }
            }
            (0, test_1.expect)(reflected).toBe(true);
        });
    });
    (0, _8_10_1.test)('T11735087 exclude variable names take precedence over include', async () => {
        skipIfUnavailable();
        const shared = 'e2e_precedence_var';
        await _8_10_1.test.step('Set the same variable in include and exclude', async () => {
            const response = await (0, apiHelpers_1.updateClusterDataFilters)(managementToken, clusterUuid, {
                variableFilter: {
                    enabled: true,
                    includeVariableNames: [shared],
                    excludeVariableNames: [shared],
                },
            });
            (0, test_1.expect)([200, 201, 204]).toContain(response.status());
        });
        await _8_10_1.test.step('Assert exclude wins on read-back', async () => {
            const { body } = await readFilters();
            const excludeNames = body?.variableFilter?.excludeVariableNames ?? [];
            (0, test_1.expect)(excludeNames).toContain(shared);
        });
    });
    (0, _8_10_1.test)('T11735088 set both include and exclude process definitions', async () => {
        skipIfUnavailable();
        const includeKey = 'e2e_include_proc';
        const excludeKey = 'e2e_exclude_proc';
        await _8_10_1.test.step('Set include and exclude process definition lists', async () => {
            const response = await (0, apiHelpers_1.updateClusterDataFilters)(managementToken, clusterUuid, {
                processDefinitionFilter: {
                    includeProcessDefinitionKeys: [includeKey],
                    excludeProcessDefinitionKeys: [excludeKey],
                },
            });
            (0, test_1.expect)([200, 201, 204]).toContain(response.status());
        });
        await _8_10_1.test.step('Assert both lists persist', async () => {
            const { body } = await readFilters();
            const include = body?.processDefinitionFilter?.includeProcessDefinitionKeys ?? [];
            const exclude = body?.processDefinitionFilter?.excludeProcessDefinitionKeys ?? [];
            (0, test_1.expect)(include).toContain(includeKey);
            (0, test_1.expect)(exclude).toContain(excludeKey);
        });
    });
    (0, _8_10_1.test)('T11735089 exclude process definitions take precedence over include', async () => {
        skipIfUnavailable();
        const shared = 'e2e_precedence_proc';
        await _8_10_1.test.step('Set the same process definition in include and exclude', async () => {
            const response = await (0, apiHelpers_1.updateClusterDataFilters)(managementToken, clusterUuid, {
                processDefinitionFilter: {
                    includeProcessDefinitionKeys: [shared],
                    excludeProcessDefinitionKeys: [shared],
                },
            });
            (0, test_1.expect)([200, 201, 204]).toContain(response.status());
        });
        await _8_10_1.test.step('Assert exclude wins on read-back', async () => {
            const { body } = await readFilters();
            const exclude = body?.processDefinitionFilter?.excludeProcessDefinitionKeys ?? [];
            (0, test_1.expect)(exclude).toContain(shared);
        });
    });
    (0, _8_10_1.test)('T11728019 unauthenticated request cannot read data filters', async () => {
        skipIfUnavailable();
        await _8_10_1.test.step('Send request without a valid token', async () => {
            const response = await (0, apiHelpers_1.getClusterDataFilters)('Bearer invalid-token', clusterUuid);
            (0, test_1.expect)([401, 403]).toContain(response.status());
        });
    });
});
