/**
 * Tests for evaluation report generator
 */
import { describe, it, expect } from "vitest";
import { generateEvaluationReport, formatReportAsMarkdown, generateCompactSummary, generateJsonReport, generateReadmeBadges, } from "./report.js";
describe("report", () => {
    const mockMetrics = {
        totalFixtures: 10,
        successfulFixtures: 9,
        failedFixtures: 1,
        totalExpected: 20,
        totalActual: 18,
        totalTruePositives: 15,
        totalFalsePositives: 3,
        totalFalseNegatives: 5,
        precision: 0.833,
        recall: 0.75,
        f1Score: 0.789,
        byCategory: {
            "sql-injection": {
                expected: 5,
                truePositives: 4,
                falsePositives: 1,
                falseNegatives: 1,
                precision: 0.8,
                recall: 0.8,
                f1Score: 0.8,
            },
        },
        byScanner: {
            semgrep: {
                findings: 10,
                truePositives: 8,
                falsePositives: 2,
                precision: 0.8,
            },
            gitleaks: {
                findings: 5,
                truePositives: 5,
                falsePositives: 0,
                precision: 1.0,
            },
        },
        totalDuration: 5000,
        timestamp: "2024-01-15T12:00:00Z",
    };
    const mockConfig = {
        scanners: ["semgrep", "gitleaks", "tsc"],
        stabilityRuns: 1,
        timeout: 60000,
        parallel: true,
    };
    const mockResults = [
        {
            fixtureId: "sql-001",
            success: true,
            actualFindings: [],
            expectedFindings: [],
            truePositives: [],
            falsePositives: [],
            falseNegatives: [],
            duration: 1000,
        },
    ];
    describe("generateEvaluationReport", () => {
        it("creates a complete evaluation report", () => {
            const report = generateEvaluationReport(mockMetrics, mockResults, mockConfig, undefined, "abc123");
            expect(report.version).toBe("1.0.0");
            expect(report.timestamp).toBeDefined();
            expect(report.commitSha).toBe("abc123");
            expect(report.metrics).toBe(mockMetrics);
            expect(report.fixtureResults).toBe(mockResults);
            expect(report.config).toBe(mockConfig);
        });
        it("includes stability metrics when provided", () => {
            const stability = {
                runs: 5,
                consistentFindings: 15,
                inconsistentFindings: 3,
                stabilityPercent: 83.3,
                byFixture: { "sql-001": 1.0 },
            };
            const report = generateEvaluationReport(mockMetrics, mockResults, mockConfig, stability);
            expect(report.stability).toBe(stability);
        });
    });
    describe("formatReportAsMarkdown", () => {
        it("generates markdown with title", () => {
            const report = generateEvaluationReport(mockMetrics, mockResults, mockConfig);
            const markdown = formatReportAsMarkdown(report);
            expect(markdown).toContain("# Vaspera Hardening Evaluation Report");
        });
        it("includes overall metrics table", () => {
            const report = generateEvaluationReport(mockMetrics, mockResults, mockConfig);
            const markdown = formatReportAsMarkdown(report);
            expect(markdown).toContain("## Overall Metrics");
            expect(markdown).toContain("| **Precision**");
            expect(markdown).toContain("| **Recall**");
            expect(markdown).toContain("| **F1 Score**");
        });
        it("includes target comparison", () => {
            const report = generateEvaluationReport(mockMetrics, mockResults, mockConfig);
            const markdown = formatReportAsMarkdown(report);
            expect(markdown).toContain("## Target Comparison");
            expect(markdown).toContain("Target");
            expect(markdown).toContain("Actual");
            expect(markdown).toContain("Status");
        });
        it("includes category breakdown", () => {
            const report = generateEvaluationReport(mockMetrics, mockResults, mockConfig);
            const markdown = formatReportAsMarkdown(report);
            expect(markdown).toContain("## Performance by Category");
            expect(markdown).toContain("Sql Injection");
        });
        it("includes scanner performance", () => {
            const report = generateEvaluationReport(mockMetrics, mockResults, mockConfig);
            const markdown = formatReportAsMarkdown(report);
            expect(markdown).toContain("## Performance by Scanner");
            expect(markdown).toContain("semgrep");
            expect(markdown).toContain("gitleaks");
        });
        it("includes stability section when available", () => {
            const stability = {
                runs: 5,
                consistentFindings: 15,
                inconsistentFindings: 3,
                stabilityPercent: 83.3,
                byFixture: {},
            };
            const report = generateEvaluationReport(mockMetrics, mockResults, mockConfig, stability);
            const markdown = formatReportAsMarkdown(report);
            expect(markdown).toContain("## Stability Analysis");
            expect(markdown).toContain("**Runs:** 5");
            expect(markdown).toContain("83.3%");
        });
        it("includes fixture summary", () => {
            const report = generateEvaluationReport(mockMetrics, mockResults, mockConfig);
            const markdown = formatReportAsMarkdown(report);
            expect(markdown).toContain("## Fixture Summary");
            expect(markdown).toContain("sql-001");
        });
        it("includes error analysis section", () => {
            const report = generateEvaluationReport(mockMetrics, mockResults, mockConfig);
            const markdown = formatReportAsMarkdown(report);
            expect(markdown).toContain("## Error Analysis");
            expect(markdown).toContain("### False Positives");
            expect(markdown).toContain("### False Negatives");
        });
        it("includes footer with duration", () => {
            const report = generateEvaluationReport(mockMetrics, mockResults, mockConfig);
            const markdown = formatReportAsMarkdown(report);
            expect(markdown).toContain("Vaspera Hardening");
            expect(markdown).toContain("5.0s");
        });
        it("shows passing status when targets are met", () => {
            const passingMetrics = {
                ...mockMetrics,
                precision: 0.95,
                recall: 0.90,
                f1Score: 0.92,
            };
            const report = generateEvaluationReport(passingMetrics, mockResults, mockConfig);
            const markdown = formatReportAsMarkdown(report);
            expect(markdown).toContain("PASSING");
            expect(markdown).toContain("✅");
        });
        it("shows improvement needed when targets not met", () => {
            const report = generateEvaluationReport(mockMetrics, mockResults, mockConfig);
            const markdown = formatReportAsMarkdown(report);
            expect(markdown).toContain("NEEDS IMPROVEMENT");
            expect(markdown).toContain("⚠️");
        });
        it("includes commit SHA when provided", () => {
            const report = generateEvaluationReport(mockMetrics, mockResults, mockConfig, undefined, "abc123def456");
            const markdown = formatReportAsMarkdown(report);
            expect(markdown).toContain("**Commit:** `abc123d`");
        });
    });
    describe("generateCompactSummary", () => {
        it("generates single-line summary", () => {
            const summary = generateCompactSummary(mockMetrics);
            expect(summary).toContain("Vaspera Eval:");
            expect(summary).toContain("Precision:");
            expect(summary).toContain("Recall:");
            expect(summary).toContain("F1:");
        });
        it("shows PASS when targets met", () => {
            const passingMetrics = {
                ...mockMetrics,
                precision: 0.95,
                recall: 0.90,
                f1Score: 0.92,
            };
            const summary = generateCompactSummary(passingMetrics);
            expect(summary).toContain("PASS");
        });
        it("shows FAIL when targets not met", () => {
            const summary = generateCompactSummary(mockMetrics);
            expect(summary).toContain("FAIL");
        });
        it("includes counts", () => {
            const summary = generateCompactSummary(mockMetrics);
            expect(summary).toContain("TP: 15");
            expect(summary).toContain("FP: 3");
            expect(summary).toContain("FN: 5");
        });
    });
    describe("generateJsonReport", () => {
        it("returns valid JSON", () => {
            const report = generateEvaluationReport(mockMetrics, mockResults, mockConfig);
            const json = generateJsonReport(report);
            expect(() => JSON.parse(json)).not.toThrow();
        });
        it("contains all report fields", () => {
            const report = generateEvaluationReport(mockMetrics, mockResults, mockConfig);
            const json = generateJsonReport(report);
            const parsed = JSON.parse(json);
            expect(parsed.version).toBeDefined();
            expect(parsed.metrics).toBeDefined();
            expect(parsed.fixtureResults).toBeDefined();
            expect(parsed.config).toBeDefined();
        });
        it("is pretty-printed", () => {
            const report = generateEvaluationReport(mockMetrics, mockResults, mockConfig);
            const json = generateJsonReport(report);
            // Pretty-printed JSON should have newlines
            expect(json).toContain("\n");
        });
    });
    describe("generateReadmeBadges", () => {
        it("generates shield.io badge URLs", () => {
            const badges = generateReadmeBadges(mockMetrics);
            expect(badges).toContain("img.shields.io/badge");
            expect(badges).toContain("Precision");
            expect(badges).toContain("Recall");
            expect(badges).toContain("F1_Score");
        });
        it("uses green color for high scores", () => {
            const highMetrics = {
                ...mockMetrics,
                precision: 0.95,
                recall: 0.90,
                f1Score: 0.92,
            };
            const badges = generateReadmeBadges(highMetrics);
            expect(badges).toContain("brightgreen");
        });
        it("uses yellow color for medium scores", () => {
            const mediumMetrics = {
                ...mockMetrics,
                precision: 0.75,
                recall: 0.75,
                f1Score: 0.75,
            };
            const badges = generateReadmeBadges(mediumMetrics);
            expect(badges).toContain("yellow");
        });
        it("uses red color for low scores", () => {
            const lowMetrics = {
                ...mockMetrics,
                precision: 0.5,
                recall: 0.5,
                f1Score: 0.5,
            };
            const badges = generateReadmeBadges(lowMetrics);
            expect(badges).toContain("red");
        });
        it("URL-encodes percentage values", () => {
            const badges = generateReadmeBadges(mockMetrics);
            // % should be encoded as %25
            expect(badges).toContain("%25");
        });
    });
});
//# sourceMappingURL=report.test.js.map