"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const c8Run_8_10_1 = require("../../fixtures/c8Run-8.10");
const test_1 = require("@playwright/test");
const UtilitiesPage_1 = require("../../pages/c8Run-8.10/UtilitiesPage");
const _setup_1 = require("../../test-setup.js");
const apiHelpers_1 = require("../../utils/apiHelpers");
const keycloakHelpers_1 = require("../../utils/keycloakHelpers");
const zeebeClient_1 = require("../../utils/zeebeClient");
const sleep_1 = require("../../utils/sleep");
let bearerToken;
const authMethods = [
    {
        name: 'Basic Auth',
        processId: 'mcp_remote_client_basic_auth',
    },
    {
        name: 'Bearer Token',
        processId: 'mcp_remote_client_bearer_auth',
        getVariables: () => ({ bearerToken }),
    },
    {
        name: 'OAuth Client Credentials',
        processId: 'mcp_remote_client_oauth_auth',
    },
];
c8Run_8_10_1.test.beforeAll(async () => {
    if (process.env.MCP_SERVER_AVAILABLE !== 'true') {
        return;
    }
    await (0, apiHelpers_1.validateMcpServerHealth)('http://127.0.0.1:12001'); // No auth
    await (0, apiHelpers_1.validateMcpServerHealth)('http://127.0.0.1:12002'); // Basic auth
    await (0, apiHelpers_1.validateMcpServerHealth)('http://127.0.0.1:12004'); // OAuth
    await (0, apiHelpers_1.waitForConnectorsReady)();
    await (0, keycloakHelpers_1.validateKeycloakHealth)();
    bearerToken = await (0, keycloakHelpers_1.getAccessToken)();
    await (0, zeebeClient_1.deploy)([
        './resources/mcp_server/mcp_remote_client_basic_auth.bpmn',
        './resources/mcp_server/mcp_remote_client_bearer_auth.bpmn',
        './resources/mcp_server/mcp_remote_client_oauth_auth.bpmn',
    ]);
});
c8Run_8_10_1.test.describe('@tasklistV2 MCP Client connector authentication tests', () => {
    c8Run_8_10_1.test.skip(process.env.MCP_SERVER_AVAILABLE !== 'true', 'MCP authentication servers require Docker and are only available on Linux CI runners');
    c8Run_8_10_1.test.beforeEach(async ({ page, operateLoginPage, operateHomePage }) => {
        await (0, UtilitiesPage_1.navigateToApp)(page, 'operate');
        await operateLoginPage.login('demo', 'demo');
        await operateHomePage.operateBannerIsVisible();
    });
    c8Run_8_10_1.test.afterEach(async ({ page }, testInfo) => {
        await (0, _setup_1.captureScreenshot)(page, testInfo);
        await (0, _setup_1.captureFailureVideo)(page, testInfo);
    });
    for (const { name, processId, getVariables } of authMethods) {
        (0, c8Run_8_10_1.test)(`As a user I can authenticate using ${name} and list tools`, async ({ operateHomePage, operateProcessesPage, operateProcessInstancePage, }) => {
            await (0, zeebeClient_1.createInstances)(processId, 1, 1, getVariables?.());
            await (0, sleep_1.sleep)(2000);
            await c8Run_8_10_1.test.step('Navigate to completed process instance', async () => {
                await operateHomePage.clickProcessesTab();
                await operateProcessesPage.clickProcessCompletedCheckbox();
                await operateProcessesPage.clickProcessInstanceLink(processId);
                await operateProcessInstancePage.completedIconAssertion();
            });
            await c8Run_8_10_1.test.step(`Verify ${name} successfully listed tools`, async () => {
                await operateProcessInstancePage.verifyVariableJsonContent('listToolsResult', (value) => {
                    const data = value;
                    (0, test_1.expect)(data.toolDefinitions).toBeDefined();
                    (0, test_1.expect)(data.toolDefinitions.length).toBeGreaterThan(0);
                    (0, test_1.expect)(data.toolDefinitions.some((tool) => tool.name)).toBe(true);
                });
            });
        });
    }
});
