"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 zeebeClient_1 = require("../../utils/zeebeClient");
const sleep_1 = require("../../utils/sleep");
c8Run_8_10_1.test.beforeAll(async () => {
    if (process.env.MCP_SERVER_AVAILABLE !== 'true') {
        return;
    }
    await (0, apiHelpers_1.validateMcpServerHealth)();
    await (0, apiHelpers_1.waitForConnectorsReady)();
    await (0, zeebeClient_1.deploy)(['./resources/mcp_server/mcp_remote_client_operations.bpmn']);
    await (0, zeebeClient_1.createInstances)('mcp_remote_client', 1, 1);
    await (0, sleep_1.sleep)(2000);
});
c8Run_8_10_1.test.describe('@tasklistV2 MCP Client connector tests', () => {
    c8Run_8_10_1.test.skip(process.env.MCP_SERVER_AVAILABLE !== 'true', 'MCP server is only available on Linux and macOS 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);
    });
    (0, c8Run_8_10_1.test)('As an user I can invoke the MCP Client connector and use all its operations', async ({ operateHomePage, operateProcessesPage, operateProcessInstancePage, }) => {
        c8Run_8_10_1.test.slow();
        await c8Run_8_10_1.test.step('Navigate to completed process instance', async () => {
            await operateHomePage.clickProcessesTab();
            await operateProcessesPage.clickProcessCompletedCheckbox();
            await operateProcessesPage.clickProcessInstanceLink('mcp_remote_client');
            await operateProcessInstancePage.completedIconAssertion();
        });
        await c8Run_8_10_1.test.step('Verify listToolsResult variable contains 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);
                const greetTool = data.toolDefinitions.find((tool) => tool.name === 'greet');
                (0, test_1.expect)(greetTool).toBeDefined();
                (0, test_1.expect)(greetTool?.description).toBe('Return a nice greeting message');
            });
        });
        await c8Run_8_10_1.test.step('Verify callToolResult variable from greet tool call', async () => {
            await operateProcessInstancePage.verifyVariableJsonContent('callToolResult', (value) => {
                const data = value;
                (0, test_1.expect)(data.name).toBe('greet');
                (0, test_1.expect)(data.isError).toBe(false);
                (0, test_1.expect)(data.content).toBeDefined();
                (0, test_1.expect)(data.content[0].type).toBe('text');
                (0, test_1.expect)(data.content[0].text).toContain('Hello, John');
            });
        });
        await c8Run_8_10_1.test.step('Verify listResourcesResult variable contains resources', async () => {
            await operateProcessInstancePage.verifyVariableJsonContent('listResourcesResult', (value) => {
                const data = value;
                (0, test_1.expect)(data.resources).toBeDefined();
                (0, test_1.expect)(data.resources.length).toBeGreaterThan(0);
                const jokesResource = data.resources.find((resource) => resource.uri === 'file://jokes-guide.md');
                (0, test_1.expect)(jokesResource).toBeDefined();
                (0, test_1.expect)(jokesResource?.name).toBe('Jokes Guideline');
                (0, test_1.expect)(jokesResource?.mimeType).toBe('text/markdown');
            });
        });
        await c8Run_8_10_1.test.step('Verify listResourcesTemplateResult variable contains resource templates', async () => {
            await operateProcessInstancePage.verifyVariableJsonContent('listResourcesTemplateResult', (value) => {
                const data = value;
                (0, test_1.expect)(data.resourceTemplates).toBeDefined();
                (0, test_1.expect)(data.resourceTemplates).toEqual([]);
            });
        });
        await c8Run_8_10_1.test.step('Verify readResourceResult variable from jokes-guide.md', async () => {
            await operateProcessInstancePage.verifyVariableJsonContent('readResourceResult', (value) => {
                const data = value;
                (0, test_1.expect)(data.contents).toBeDefined();
                (0, test_1.expect)(data.contents.length).toBeGreaterThan(0);
                (0, test_1.expect)(data.contents[0].uri).toBe('file://jokes-guide.md');
                (0, test_1.expect)(data.contents[0].mimeType).toBe('text/markdown');
                (0, test_1.expect)(data.contents[0].text).toContain('How to Write Jokes');
            });
        });
        await c8Run_8_10_1.test.step('Verify listPromptsResult variable contains prompts', async () => {
            await operateProcessInstancePage.verifyVariableJsonContent('listPromptsResult', (value) => {
                const data = value;
                (0, test_1.expect)(data.promptDescriptions).toBeDefined();
                (0, test_1.expect)(data.promptDescriptions.length).toBeGreaterThan(0);
                const greetingPrompt = data.promptDescriptions.find((prompt) => prompt.name === 'get-greeting');
                (0, test_1.expect)(greetingPrompt).toBeDefined();
                (0, test_1.expect)(greetingPrompt?.description).toContain('personalized greeting');
            });
        });
        await c8Run_8_10_1.test.step('Verify getPromptResult variable from get-greeting prompt', async () => {
            await operateProcessInstancePage.verifyVariableJsonContent('getPromptResult', (value) => {
                const data = value;
                (0, test_1.expect)(data.description).toBe('Greeting Prompt');
                (0, test_1.expect)(data.messages).toBeDefined();
                (0, test_1.expect)(data.messages.length).toBeGreaterThan(0);
                (0, test_1.expect)(data.messages[0].role).toBe('user');
                (0, test_1.expect)(data.messages[0].content.text).toContain('John');
                (0, test_1.expect)(data.messages[0].content.text).toContain('greet tool');
            });
        });
    });
});
