"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
/**
 * AWS Connectors User Flows
 *
 * Happy-path E2E tests for AWS Bedrock connector integrations running on the
 * Test Cluster (SaaS). Each test deploys a BPMN process, starts an instance,
 * and asserts that the connector completed successfully via Operate.
 *
 * ## AWS IAM Requirements
 *
 * The IAM user configured via `BEDROCK_CI_AWS_ACCESS_KEY` /
 * `BEDROCK_CI_AWS_SECRET_KEY` must have the following policies. Missing
 * actions surface as connector incidents with `AccessDeniedException` in the
 * Operate variables panel.
 *
 * ### AgentCore Code Interpreter (`io.camunda:aws-bedrock-codeinterpreter:1`)
 * ```json
 * {
 *   "Action": [
 *     "bedrock-agentcore:StartCodeInterpreterSession",
 *     "bedrock-agentcore:InvokeCodeInterpreter",
 *     "bedrock-agentcore:StopCodeInterpreterSession"
 *   ],
 *   "Resource": "*"
 * }
 * ```
 * NOTE: `CreateCodeInterpreterSession` and `ExecuteCode` are NOT valid IAM
 * actions — AWS will reject the policy if they are included.
 *
 * ### AgentCore Runtime (`io.camunda:aws-bedrock-agentcore-runtime:1`)
 * Requires IAM access to the specific agent runtime ARN set via
 * `AGENTCORE_AGENT_RUNTIME_ARN`.
 *
 * ### AgentCore Long-Term Memory (`io.camunda:aws-bedrock-agentcore-lt-memory:1`)
 * Requires IAM access to the memory resource ARN set via
 * `AGENTCORE_LTM_MEMORY_ID`.
 *
 * ### Bedrock Knowledge Base (`io.camunda:aws-bedrock-knowledgebase:1`)
 * Requires `bedrock:Retrieve` on the knowledge base ARN set via
 * `BEDROCK_KB_KNOWLEDGE_BASE_ID`.
 *
 */
const test_1 = require("@playwright/test");
const _8_10_1 = require("../../fixtures/8.10");
const UtilitiesPage_1 = require("../../pages/8.10/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");
// fourteenthUser drives agentic AI (AWS Bedrock) tests on Test Cluster — reuse
const testUser = (0, users_1.getTestUser)('fourteenthUser');
let authToken;
_8_10_1.test.describe.configure({ mode: 'parallel' });
_8_10_1.test.describe('AWS Connectors User Flows', () => {
    const clusterName = 'Test Cluster';
    _8_10_1.test.beforeAll(async () => {
        authToken = await (0, apiHelpers_1.authSaasAPI)();
    });
    _8_10_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_10_1.test.afterEach(async ({ page }, testInfo) => {
        await (0, _setup_1.captureScreenshot)(page, testInfo);
        await (0, _setup_1.captureFailureVideo)(page, testInfo);
    });
    (0, _8_10_1.test)('AWS Bedrock AgentCore LTM - Retrieve memory records', async ({ operateHomePage, operateProcessInstancePage, operateProcessesPage, }) => {
        _8_10_1.test.slow();
        const processKey = await (0, apiHelpers_1.deployProcess)('./resources/connectors/aws/bedrock-agentcore-ltm.bpmn', authToken);
        if (processKey == null) {
            throw new Error('Failed to deploy AgentCore LTM process');
        }
        const instanceKey = await (0, apiHelpers_1.createProcessInstance)(String(processKey), authToken);
        await _8_10_1.test.step('Process completes and connector returns a well-formed AgentCore LTM response', async () => {
            await (0, sleep_1.sleep)(15000);
            await operateHomePage.clickProcessesTab();
            await operateProcessesPage.clickProcessCompletedCheckbox();
            await operateProcessesPage.applyMoreFilters('Process Instance Key(s)', String(instanceKey));
            await operateProcessesPage.clickProcessInstanceLink('AWS Bedrock AgentCore LTM');
            await operateProcessInstancePage.assertProcessCompleteStatusWithRetry();
            // Assert the mapped memoryRecords variable is present (proves result
            // expression was evaluated, even when the memory namespace is empty)
            await (0, test_1.expect)(operateProcessInstancePage.connectorResultVariableName('memoryRecords')).toBeVisible({ timeout: 30000 });
            // Assert the raw agentcoreResult contains the expected response envelope
            // fields (resultCount, nextToken) — these are always present regardless
            // of whether the memory namespace has any records seeded
            await operateProcessInstancePage.assertProcessVariableContainsText('agentcoreResult', 'resultCount');
            await operateProcessInstancePage.assertProcessVariableContainsText('agentcoreResult', 'nextToken');
        });
    });
    (0, _8_10_1.test)('AWS Bedrock Knowledge Base - Retrieve relevant chunks from knowledge base', async ({ operateHomePage, operateProcessInstancePage, operateProcessesPage, }) => {
        _8_10_1.test.slow();
        const processKey = await (0, apiHelpers_1.deployProcess)('./resources/connectors/aws/bedrock-knowledge-base.bpmn', authToken);
        if (processKey == null) {
            throw new Error('Failed to deploy Bedrock KB process');
        }
        const instanceKey = await (0, apiHelpers_1.createProcessInstance)(String(processKey), authToken);
        await _8_10_1.test.step('Process completes and retrievedChunks output variable is present', async () => {
            await (0, sleep_1.sleep)(15000);
            await operateHomePage.clickProcessesTab();
            await operateProcessesPage.clickProcessCompletedCheckbox();
            await operateProcessesPage.applyMoreFilters('Process Instance Key(s)', String(instanceKey));
            await operateProcessesPage.clickProcessInstanceLink('AWS Bedrock Knowledge Base');
            await operateProcessInstancePage.assertProcessCompleteStatusWithRetry();
            // Assert retrievedChunks variable is present and contains at least one
            // entry with a sourceUri (list with at least one entry containing a source URI)
            await (0, test_1.expect)(operateProcessInstancePage.connectorResultVariableName('retrievedChunks')).toBeVisible({ timeout: 30000 });
            await operateProcessInstancePage.assertProcessVariableContainsText('retrievedChunks', 'sourceUri');
        });
    });
    (0, _8_10_1.test)('AWS AgentCore Runtime - Invoke deployed agent and receive response', async ({ operateHomePage, operateProcessInstancePage, operateProcessesPage, }) => {
        _8_10_1.test.slow();
        const processKey = await (0, apiHelpers_1.deployProcess)('./resources/connectors/aws/agentcore-runtime.bpmn', authToken);
        if (processKey == null) {
            throw new Error('Failed to deploy AgentCore Runtime process');
        }
        const instanceKey = await (0, apiHelpers_1.createProcessInstance)(String(processKey), authToken);
        await _8_10_1.test.step('Process completes and agentResponse output variable is present', async () => {
            await (0, sleep_1.sleep)(15000);
            await operateHomePage.clickProcessesTab();
            await operateProcessesPage.clickProcessCompletedCheckbox();
            await operateProcessesPage.applyMoreFilters('Process Instance Key(s)', String(instanceKey));
            await operateProcessesPage.clickProcessInstanceLink('AWS AgentCore Runtime');
            await operateProcessInstancePage.assertProcessCompleteStatusWithRetry();
            await (0, test_1.expect)(operateProcessInstancePage.connectorResultVariableName('agentResponse')).toBeVisible({ timeout: 30000 });
        });
    });
    // IAM: bedrock-agentcore:StartCodeInterpreterSession,
    //      bedrock-agentcore:InvokeCodeInterpreter,
    //      bedrock-agentcore:StopCodeInterpreterSession on Resource: *
    (0, _8_10_1.test)('AWS AgentCore Code Interpreter - Execute Python code and receive stdout', async ({ operateHomePage, operateProcessInstancePage, operateProcessesPage, }) => {
        _8_10_1.test.slow();
        const processKey = await (0, apiHelpers_1.deployProcess)('./resources/connectors/aws/agentcore-code-interpreter.bpmn', authToken);
        if (processKey == null) {
            throw new Error('Failed to deploy AgentCore Code Interpreter process');
        }
        const instanceKey = await (0, apiHelpers_1.createProcessInstance)(String(processKey), authToken);
        await _8_10_1.test.step('Process completes and stdout output variable is present', async () => {
            await (0, sleep_1.sleep)(15000);
            await operateHomePage.clickProcessesTab();
            await operateProcessesPage.clickProcessCompletedCheckbox();
            await operateProcessesPage.applyMoreFilters('Process Instance Key(s)', String(instanceKey));
            await operateProcessesPage.clickProcessInstanceLink('AWS AgentCore Code Interpreter');
            await operateProcessInstancePage.assertProcessCompleteStatusWithRetry();
            await (0, test_1.expect)(operateProcessInstancePage.connectorResultVariableName('stdout')).toBeVisible({ timeout: 30000 });
            // Confirms the Python code executed end-to-end inside AWS AgentCore and
            // the result expression `={stdout: stdout, exitCode: exitCode}` was
            // evaluated — stdout is the key observable for this connector.
            await operateProcessInstancePage.assertProcessVariableContainsText('stdout', 'Hello from AgentCore Code Interpreter!');
        });
    });
});
