"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const _8_8_1 = require("../../fixtures/8.8");
const UtilitiesPage_1 = require("../../pages/8.8/UtilitiesPage");
const _setup_1 = require("../../test-setup.js");
const apiHelpers_1 = require("../../utils/apiHelpers");
const sleep_1 = require("../../utils/sleep");
const users_1 = require("../../utils/users");
const testUser = (0, users_1.getTestUser)('fourteenthUser');
let authToken;
const aiAgentTestCases = [
    {
        provider: 'openAI',
        bpmnPath: './resources/agentic_ai/ai-agent-chat-with-tools_open_ai.bpmn',
    },
    {
        provider: 'AWS',
        bpmnPath: './resources/agentic_ai/ai-agent-chat-with-tools_aws.bpmn',
    },
    {
        provider: 'Azure',
        bpmnPath: './resources/agentic_ai/ai-agent-chat-with-tools_azure.bpmn',
    },
];
const dbVectorTestCases = [
    //Skipped due to bug 1208: https://github.com/camunda/c8-cross-component-e2e-tests/issues/1208
    /* {
      embedding_provider: 'AzureOpenAI',
      embedding_store: 'Azure AI Search',
      bpmnPath:
        './resources/connectors/db_vector_connector/db-vector-plain-text_azure_open_ai.bpmn',
    },*/
    {
        embedding_provider: 'OpenAI',
        embedding_store: 'Azure Cosmos DB',
        bpmnPath: './resources/connectors/db_vector_connector/db-vector-plain-text_open_ai.bpmn',
    },
];
const regexTestCases = [
    {
        regex_ai_agent_time: /The current date and time in ([A-Za-zÀ-ÿ .,'-]+(?:, [A-Za-zÀ-ÿ .,'-]+)*), is approximately ((?:0?[1-9]|1[0-2]):[0-5][0-9] (?:AM|PM)) on ([A-Z][a-z]+ (?:[1-9]|[12][0-9]|3[01]), \d{4})\./,
        regex_db_vector: /Color and Varieties:\s*While orange is the most common color today, carrots can also be found in purple, red, yellow, and white varieties\. The orange color comes from beta-carotene, a pigment that the body converts into vitamin A\. Different colors may contain other antioxidants:\s*Purple carrots are rich in anthocyanins\s*Red carrots contain lycopene\s*Yellow carrots have lutein/,
    },
];
_8_8_1.test.describe.configure({ mode: 'parallel' });
_8_8_1.test.describe('Agentic Orchestation User Flows', () => {
    const clusterName = 'Test Cluster';
    _8_8_1.test.beforeAll(async () => {
        authToken = await (0, apiHelpers_1.authSaasAPI)();
        await Promise.all([
            (0, apiHelpers_1.deployProcess)('./resources/agentic_ai/ai-agent-chat-user-feedback.form', authToken),
            (0, apiHelpers_1.deployProcess)('./resources/connectors/db_vector_connector/db-vector-retrieve-data.form', authToken),
        ]);
        await (0, sleep_1.sleep)(5000);
    });
    _8_8_1.test.beforeEach(async ({ page, appsPage, loginPage }, testInfo) => {
        await (0, UtilitiesPage_1.loginWithRetry)(page, loginPage, testUser, (testInfo.workerIndex + 1) * 1000);
        await appsPage.clickCamundaApps();
        await appsPage.clickOperate(clusterName);
    });
    _8_8_1.test.afterEach(async ({ page }, testInfo) => {
        await (0, _setup_1.captureScreenshot)(page, testInfo);
        await (0, _setup_1.captureFailureVideo)(page, testInfo);
    });
    _8_8_1.test.describe('AI Agent connector - provider user tests flow', () => {
        for (const params of aiAgentTestCases) {
            (0, _8_8_1.test)(`AI agent with tools provides feedback to user - ${params.provider}`, async ({ appsPage, operateHomePage, operateProcessInstancePage, operateProcessesPage, taskPanelPage, taskDetailsPage, }) => {
                _8_8_1.test.slow();
                const processKey = await (0, apiHelpers_1.deployProcess)(params.bpmnPath, authToken);
                if (processKey == null) {
                    throw new Error('Failed to deploy process or missing processDefinitionKey');
                }
                const instanceKey = await (0, apiHelpers_1.createProcessInstance)(String(processKey), authToken);
                await _8_8_1.test.step('User can access the process on operate', async () => {
                    await operateHomePage.clickProcessesTab();
                    await operateProcessesPage.applyMoreFilters('Process Instance Key(s)', String(instanceKey));
                    await operateProcessesPage.clickProcessInstanceLink(`AI Agent Chat With Tools ${params.provider}`);
                    await operateProcessInstancePage.assertActiveTokenIsPresent();
                });
                await _8_8_1.test.step('User can view the agent feedback on tasklist', async () => {
                    await appsPage.clickCamundaApps();
                    await appsPage.clickTasklist(clusterName);
                    await (0, sleep_1.sleep)(5000);
                    await taskPanelPage.openTask(`AI Agent Chat With Tools ${params.provider}`);
                    await taskDetailsPage.clickAssignToMeButton();
                });
                await _8_8_1.test.step('User can view the agent response and complete the task', async () => {
                    await taskDetailsPage.assertAIResponse(regexTestCases[0].regex_ai_agent_time);
                    await taskDetailsPage.checkChecklistBox('Are you satisfied with the result?');
                    await taskDetailsPage.clickCompleteTaskButton();
                });
                await _8_8_1.test.step('User can assert the proccess is completed on Operate', async () => {
                    await appsPage.clickCamundaApps();
                    await appsPage.clickOperate(clusterName);
                    await operateHomePage.clickProcessesTab();
                    await operateProcessesPage.clickProcessCompletedCheckbox();
                    await operateProcessesPage.applyMoreFilters('Process Instance Key(s)', String(instanceKey));
                    await operateProcessesPage.clickProcessInstanceLink(`AI Agent Chat With Tools ${params.provider}`);
                    await operateProcessInstancePage.assertProcessCompleteStatusWithRetry();
                });
            });
        }
    });
    _8_8_1.test.describe('DB Vector Connector User Flow Tests', () => {
        for (const params of dbVectorTestCases) {
            (0, _8_8_1.test)(`DB Vector connector embed via ${params.embedding_provider} and stored on ${params.embedding_store} with source as plain text`, async ({ appsPage, operateHomePage, operateProcessInstancePage, operateProcessesPage, taskPanelPage, taskDetailsPage, }) => {
                _8_8_1.test.slow();
                const processKey = await (0, apiHelpers_1.deployProcess)(params.bpmnPath, authToken);
                if (processKey == null) {
                    throw new Error('Failed to deploy process or missing processDefinitionKey');
                }
                const instanceKey = await (0, apiHelpers_1.createProcessInstance)(String(processKey), authToken);
                await _8_8_1.test.step('User can access the process on operate', async () => {
                    await operateHomePage.clickProcessesTab();
                    await operateProcessesPage.applyMoreFilters('Process Instance Key(s)', String(instanceKey));
                    await operateProcessesPage.clickProcessInstanceLink(`db_vector_process_${params.embedding_provider}`);
                    await operateProcessInstancePage.assertActiveTokenIsPresent();
                });
                await _8_8_1.test.step('User can view the agent feedback on tasklist', async () => {
                    await appsPage.clickCamundaApps();
                    await appsPage.clickTasklist(clusterName);
                    await (0, sleep_1.sleep)(30000);
                    await taskPanelPage.openTask(`db_vector_process_${params.embedding_provider}`);
                    await taskDetailsPage.clickAssignToMeButton();
                    await taskDetailsPage.assertAIResponse(regexTestCases[0].regex_db_vector);
                    await taskDetailsPage.clickCompleteTaskButton();
                });
                await _8_8_1.test.step('User can assert the process is completed on Operate', async () => {
                    await appsPage.clickCamundaApps();
                    await appsPage.clickOperate(clusterName);
                    await operateHomePage.clickProcessesTab();
                    await operateProcessesPage.clickProcessCompletedCheckbox();
                    await operateProcessesPage.applyMoreFilters('Process Instance Key(s)', String(instanceKey));
                    await operateProcessesPage.clickProcessInstanceLink(`db_vector_process_${params.embedding_provider}`);
                    await operateProcessInstancePage.assertProcessCompleteStatusWithRetry();
                });
            });
        }
    });
});
