"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const test_1 = require("@playwright/test");
const _8_10_1 = require("../../fixtures/8.10");
const _setup_1 = require("../../test-setup.js");
const UtilitiesPage_1 = require("../../pages/8.10/UtilitiesPage");
const users_1 = require("../../utils/users");
const agenticDashboardHelpers_1 = require("../../utils/agenticDashboardHelpers");
const testUser = (0, users_1.getTestUser)('twentyFirstUser');
const clusterName = 'Test Cluster';
_8_10_1.test.describe.configure({ mode: 'serial' });
_8_10_1.test.describe('Agentic Control Plane Dashboard API Tests @agentic-dashboard-api @8.10', () => {
    _8_10_1.test.beforeEach(async ({ page, loginPage }, testInfo) => {
        await (0, UtilitiesPage_1.loginWithRetry)(page, loginPage, testUser, (testInfo.workerIndex + 1) * 1000);
    });
    _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)('Dashboard API responses load without server errors', async ({ page, appsPage, optimizeHomePage, }) => {
        const responses = await (0, agenticDashboardHelpers_1.captureDashboardResponses)(page, async () => {
            await (0, agenticDashboardHelpers_1.openAgenticControlPlane)(page, appsPage, optimizeHomePage, clusterName);
        });
        const dashboardResponses = responses.filter(agenticDashboardHelpers_1.isLikelyDashboardApiResponse);
        (0, test_1.expect)(dashboardResponses.length).toBeGreaterThan(0);
        (0, test_1.expect)(dashboardResponses.some(agenticDashboardHelpers_1.isAgenticDashboardDefinitionResponse)).toBe(true);
        for (const response of dashboardResponses) {
            (0, test_1.expect)(response.status).toBe(200);
        }
    });
    (0, _8_10_1.test)('Dashboard payload exposes execution count and comparison data', async ({ page, appsPage, optimizeHomePage, }) => {
        const responses = await (0, agenticDashboardHelpers_1.captureDashboardResponses)(page, async () => {
            await (0, agenticDashboardHelpers_1.openAgenticControlPlane)(page, appsPage, optimizeHomePage, clusterName);
        });
        const definitionBodies = (0, agenticDashboardHelpers_1.parseJsonResponseBodies)(responses.filter(agenticDashboardHelpers_1.isAgenticDashboardDefinitionResponse));
        (0, test_1.expect)(definitionBodies.length).toBeGreaterThan(0);
        (0, test_1.expect)(definitionBodies.some(agenticDashboardHelpers_1.dashboardHasComparisonTile)).toBe(true);
        const reportBodies = (0, agenticDashboardHelpers_1.parseJsonResponseBodies)(responses.filter(agenticDashboardHelpers_1.isReportEvaluateResponse));
        (0, test_1.expect)(reportBodies.length).toBeGreaterThan(0);
        const executionReport = (0, agenticDashboardHelpers_1.findReportByName)(reportBodies, agenticDashboardHelpers_1.EXECUTION_REPORT_NAME);
        (0, test_1.expect)(executionReport).toBeDefined();
        const executionCount = (0, agenticDashboardHelpers_1.getReportNumericResult)(executionReport);
        (0, test_1.expect)(typeof executionCount).toBe('number');
        (0, test_1.expect)(Number.isFinite(executionCount)).toBe(true);
    });
    (0, _8_10_1.test)('Dashboard API uses current terminology and not legacy wording', async ({ page, appsPage, optimizeHomePage, }) => {
        const responses = await (0, agenticDashboardHelpers_1.captureDashboardResponses)(page, async () => {
            await (0, agenticDashboardHelpers_1.openAgenticControlPlane)(page, appsPage, optimizeHomePage, clusterName);
        });
        const dashboardResponses = responses.filter(agenticDashboardHelpers_1.isLikelyDashboardApiResponse);
        (0, test_1.expect)(dashboardResponses.length).toBeGreaterThan(0);
        const joined = dashboardResponses
            .map((r) => r.bodyText)
            .join('\n')
            .toLowerCase();
        (0, test_1.expect)(joined.includes(agenticDashboardHelpers_1.CURRENT_TERMINOLOGY)).toBeTruthy();
        (0, test_1.expect)(joined.includes(agenticDashboardHelpers_1.LEGACY_TERMINOLOGY)).toBeFalsy();
    });
    (0, _8_10_1.test)('Changing the date range triggers refreshed report API calls', async ({ page, appsPage, optimizeHomePage, }) => {
        await (0, agenticDashboardHelpers_1.openAgenticControlPlane)(page, appsPage, optimizeHomePage, clusterName);
        let dateRangeChanged = false;
        const responses = await (0, agenticDashboardHelpers_1.captureDashboardResponses)(page, async () => {
            dateRangeChanged = await (0, agenticDashboardHelpers_1.changeDashboardDateRange)(page);
        });
        (0, test_1.expect)(dateRangeChanged).toBe(true);
        const reportResponses = responses.filter(agenticDashboardHelpers_1.isReportEvaluateResponse);
        (0, test_1.expect)(reportResponses.length).toBeGreaterThan(0);
        for (const response of reportResponses) {
            (0, test_1.expect)(response.status).toBe(200);
        }
    });
    (0, _8_10_1.test)('Report payloads do not expose Phase 1 out-of-scope details', async ({ page, appsPage, optimizeHomePage, }) => {
        const responses = await (0, agenticDashboardHelpers_1.captureDashboardResponses)(page, async () => {
            await (0, agenticDashboardHelpers_1.openAgenticControlPlane)(page, appsPage, optimizeHomePage, clusterName);
        });
        const dashboardResponses = responses.filter(agenticDashboardHelpers_1.isLikelyDashboardApiResponse);
        (0, test_1.expect)(dashboardResponses.length).toBeGreaterThan(0);
        const joined = dashboardResponses
            .map((r) => r.bodyText)
            .join('\n')
            .toLowerCase();
        (0, test_1.expect)(joined.includes('reasoning token')).toBeFalsy();
        (0, test_1.expect)(joined.includes('tool_name')).toBeFalsy();
        (0, test_1.expect)(joined.includes('per-tool')).toBeFalsy();
    });
});
