"use strict";
/**
 * Test Reporter
 *
 * Generates and formats test execution reports.
 * Reports are saved in .mycontext/test-reports/
 */
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.TestReporter = void 0;
const fs = __importStar(require("fs-extra"));
const path = __importStar(require("path"));
const chalk_1 = __importDefault(require("chalk"));
class TestReporter {
    constructor(projectPath) {
        this.projectPath = projectPath;
        this.reportsDir = path.join(projectPath, ".mycontext", "test-reports");
        fs.ensureDirSync(this.reportsDir);
    }
    /**
     * Generate a report from test execution
     */
    async generateReport(execution, mission) {
        const report = {
            reportId: execution.executionId,
            mission: mission || {}, // Will be filled by caller
            execution,
            generatedAt: new Date().toISOString(),
            summary: {
                totalSteps: execution.steps.length,
                successfulSteps: execution.steps.filter((s) => s.success).length,
                failedSteps: execution.steps.filter((s) => !s.success).length,
                totalValidations: execution.validationResults.length,
                passedValidations: execution.validationResults.filter((v) => v.passed)
                    .length,
                failedValidations: execution.validationResults.filter((v) => !v.passed)
                    .length,
                overallStatus: execution.status,
            },
            insights: this.generateInsights(execution),
            recommendations: this.generateRecommendations(execution),
            driftReport: execution.driftAnalysis ? {
                overallScore: execution.driftAnalysis.narrativeCompliance,
                criticalAlerts: execution.driftAnalysis.alerts.filter(a => a.severity === 'high').length
            } : undefined
        };
        // Save report to disk
        await this.saveReport(report);
        return report;
    }
    /**
     * Save report to disk
     */
    async saveReport(report) {
        const filename = `${report.reportId}.json`;
        const filepath = path.join(this.reportsDir, filename);
        await fs.writeJson(filepath, report, { spaces: 2 });
    }
    /**
     * Get a report by execution ID
     */
    async getReport(executionId) {
        const filepath = path.join(this.reportsDir, `${executionId}.json`);
        if (await fs.pathExists(filepath)) {
            return await fs.readJson(filepath);
        }
        return null;
    }
    /**
     * Format report as text
     */
    async formatReport(report, options = {}) {
        const { includeScreenshots = true } = options;
        const { execution, summary } = report;
        const lines = [];
        // Header
        lines.push(`# Test Report: ${report.mission.name || "Unknown Mission"}`);
        lines.push("");
        lines.push(`**Execution ID**: ${execution.executionId}`);
        lines.push(`**Status**: ${execution.status.toUpperCase()}`);
        lines.push(`**Date**: ${new Date(execution.startedAt).toLocaleString()}`);
        lines.push(`**Duration**: ${execution.duration ? `${execution.duration}ms` : "N/A"}`);
        lines.push("");
        // Summary
        lines.push(`## Summary`);
        lines.push("");
        lines.push(`- **Total Steps**: ${summary.totalSteps}`);
        lines.push(`- **Successful Steps**: ${summary.successfulSteps}`);
        lines.push(`- **Failed Steps**: ${summary.failedSteps}`);
        lines.push(`- **Total Validations**: ${summary.totalValidations}`);
        lines.push(`- **Passed Validations**: ${summary.passedValidations}`);
        lines.push(`- **Failed Validations**: ${summary.failedValidations}`);
        lines.push("");
        // Steps
        if (execution.steps.length > 0) {
            lines.push(`## Execution Steps (${execution.steps.length})`);
            lines.push("");
            execution.steps.forEach((step) => {
                const statusIcon = step.success ? "✅" : "❌";
                lines.push(`### ${statusIcon} Step ${step.order + 1}: ${step.action}`);
                if (step.intent) {
                    lines.push(`**Intent**: ${step.intent}`);
                }
                if (!step.success && step.error) {
                    lines.push(`**Error**: ${step.error}`);
                }
                if (includeScreenshots && step.screenshot) {
                    lines.push(`**Screenshot**: ${step.screenshot}`);
                }
                if (step.metadata) {
                    lines.push(`**Metadata**: ${JSON.stringify(step.metadata, null, 2)}`);
                }
                lines.push("");
            });
        }
        // Validations
        if (execution.validationResults.length > 0) {
            lines.push(`## Validation Results (${execution.validationResults.length})`);
            lines.push("");
            execution.validationResults.forEach((validation, index) => {
                const statusIcon = validation.passed ? "✅" : "❌";
                lines.push(`### ${statusIcon} Validation ${index + 1}`);
                lines.push(`**Type**: ${validation.rule.type}`);
                lines.push(`**Description**: ${validation.rule.description}`);
                lines.push(`**Result**: ${validation.message}`);
                if (validation.actualValue) {
                    lines.push(`**Actual Value**: ${validation.actualValue}`);
                }
                lines.push("");
            });
        }
        // Error
        if (execution.error) {
            lines.push(`## Error`);
            lines.push("");
            lines.push("```");
            lines.push(execution.error.message);
            if (execution.error.stack) {
                lines.push("");
                lines.push(execution.error.stack);
            }
            lines.push("```");
            lines.push("");
        }
        // Final State
        lines.push(`## Final State`);
        lines.push("");
        lines.push(`**URL**: ${execution.finalState.url || "N/A"}`);
        if (includeScreenshots && execution.finalState.screenshot) {
            lines.push(`**Screenshot**: ${execution.finalState.screenshot}`);
        }
        lines.push("");
        // AI Notes
        if (execution.aiNotes) {
            lines.push(`## AI Notes`);
            lines.push("");
            lines.push(execution.aiNotes);
            lines.push("");
        }
        // Insights
        if (report.insights && report.insights.length > 0) {
            lines.push(`## Insights`);
            lines.push("");
            report.insights.forEach((insight) => lines.push(`- ${insight}`));
            lines.push("");
        }
        // Recommendations
        if (report.recommendations && report.recommendations.length > 0) {
            lines.push(`## Recommendations`);
            lines.push("");
            report.recommendations.forEach((rec) => lines.push(`- ${rec}`));
            lines.push("");
        }
        // Drift Analysis
        if (report.driftReport) {
            lines.push("## Drift Analysis (Antigravity Grounding)");
            lines.push("");
            lines.push(`- **Narrative Compliance**: ${(report.driftReport.overallScore * 100).toFixed(1)}%`);
            lines.push(`- **Critical Deviations**: ${report.driftReport.criticalAlerts}`);
            lines.push("");
            if (report.execution.driftAnalysis && report.execution.driftAnalysis.alerts.length > 0) {
                lines.push("### Drift Alerts");
                lines.push("");
                report.execution.driftAnalysis.alerts.forEach(alert => {
                    const icon = alert.severity === 'high' ? '🚨' : alert.severity === 'medium' ? '⚠️' : 'ℹ️';
                    lines.push(`${icon} **[${alert.type.toUpperCase()}]** ${alert.message}`);
                    lines.push(`  - **Expected**: ${alert.expected}`);
                    lines.push(`  - **Actual**: ${alert.actual}`);
                    lines.push("");
                });
            }
        }
        return lines.join("\n");
    }
    /**
     * Generate insights from execution
     */
    generateInsights(execution) {
        const insights = [];
        // Analyze step success rate
        const successRate = execution.steps.length > 0
            ? (execution.steps.filter((s) => s.success).length /
                execution.steps.length) *
                100
            : 0;
        if (successRate === 100 && execution.status === "passed") {
            insights.push("All steps executed successfully");
        }
        else if (successRate < 50) {
            insights.push(`Low success rate (${Math.round(successRate)}%) - test may need adjustment`);
        }
        // Analyze execution time
        if (execution.duration) {
            if (execution.duration > 30000) {
                insights.push(`Test took ${Math.round(execution.duration / 1000)}s - consider optimization`);
            }
            else if (execution.duration < 5000) {
                insights.push("Fast execution time - excellent!");
            }
        }
        // Analyze validation results
        if (execution.validationResults.length > 0) {
            const passRate = (execution.validationResults.filter((v) => v.passed).length /
                execution.validationResults.length) *
                100;
            if (passRate < 100) {
                insights.push(`${Math.round(100 - passRate)}% of validations failed - review expected outcomes`);
            }
        }
        // Check for errors
        if (execution.error) {
            insights.push(`Test encountered an error: ${execution.error.message}`);
        }
        return insights;
    }
    /**
     * Generate recommendations
     */
    generateRecommendations(execution) {
        const recommendations = [];
        // Failed steps
        const failedSteps = execution.steps.filter((s) => !s.success);
        if (failedSteps.length > 0) {
            recommendations.push(`Review failed steps: ${failedSteps.map((s) => s.action).join(", ")}`);
        }
        // Failed validations
        const failedValidations = execution.validationResults.filter((v) => !v.passed);
        if (failedValidations.length > 0) {
            recommendations.push(`Update validation rules or fix issues causing failures`);
        }
        // No validations
        if (execution.validationResults.length === 0) {
            recommendations.push(`Add validation rules to ensure test reliability`);
        }
        // Long execution
        if (execution.duration && execution.duration > 30000) {
            recommendations.push(`Consider adding explicit waits or optimizing selectors`);
        }
        // Error
        if (execution.error) {
            recommendations.push(`Fix the error before proceeding: ${execution.error.message}`);
        }
        return recommendations;
    }
    /**
     * Generate summary report for multiple executions
     */
    async generateSummaryReport(executionIds) {
        const reports = [];
        for (const id of executionIds) {
            const report = await this.getReport(id);
            if (report) {
                reports.push(report);
            }
        }
        const passed = reports.filter((r) => r.execution.status === "passed").length;
        const failed = reports.filter((r) => r.execution.status === "failed").length;
        const errors = reports.filter((r) => r.execution.status === "error").length;
        const durations = reports
            .filter((r) => r.execution.duration)
            .map((r) => r.execution.duration);
        const averageDuration = durations.length > 0
            ? durations.reduce((sum, d) => sum + d, 0) / durations.length
            : 0;
        return {
            totalTests: reports.length,
            passed,
            failed,
            errors,
            averageDuration: Math.round(averageDuration),
            reports,
        };
    }
    /**
     * Console log a report with colors
     */
    printReport(report) {
        console.log(chalk_1.default.bold.blue(`\n📊 Test Report: ${report.mission.name}`));
        console.log(chalk_1.default.gray(`═══════════════════════════════════════`));
        const statusColor = report.execution.status === "passed"
            ? chalk_1.default.green
            : report.execution.status === "failed"
                ? chalk_1.default.red
                : chalk_1.default.yellow;
        console.log(`Status: ${statusColor(report.execution.status.toUpperCase())}`);
        console.log(`Duration: ${report.execution.duration ? `${report.execution.duration}ms` : "N/A"}`);
        console.log("");
        console.log(chalk_1.default.bold("Summary:"));
        console.log(`  Steps: ${report.summary.successfulSteps}/${report.summary.totalSteps} passed`);
        console.log(`  Validations: ${report.summary.passedValidations}/${report.summary.totalValidations} passed`);
        if (report.insights && report.insights.length > 0) {
            console.log(chalk_1.default.bold("\nInsights:"));
            report.insights.forEach((insight) => console.log(chalk_1.default.cyan(`  • ${insight}`)));
        }
        if (report.recommendations && report.recommendations.length > 0) {
            console.log(chalk_1.default.bold("\nRecommendations:"));
            report.recommendations.forEach((rec) => console.log(chalk_1.default.yellow(`  • ${rec}`)));
        }
        console.log(chalk_1.default.gray(`\nFull report: ${report.reportId}\n`));
    }
}
exports.TestReporter = TestReporter;
//# sourceMappingURL=test-reporter.js.map