/**
 * Unit tests for pr-comment module
 */
import { describe, it, expect } from "vitest";
import { generatePrComment, formatCommentBody } from "./pr-comment.js";
const mockContext = {
    owner: "test-owner",
    repo: "test-repo",
    sha: "abc123",
    ref: "refs/heads/main",
    eventName: "pull_request",
    pullRequest: {
        number: 42,
        base: { sha: "def456", ref: "main" },
        head: { sha: "abc123", ref: "feature-branch" },
    },
    workflow: "CI",
    runId: 12345,
    runNumber: 1,
    actor: "test-user",
};
const createMockScanResult = (bySeverity = {}) => ({
    timestamp: "2024-01-01T00:00:00.000Z",
    projectPath: "/test/project",
    scanners: [
        {
            scanner: "semgrep",
            findings: [],
            duration: 1000,
            success: true,
        },
        {
            scanner: "gitleaks",
            findings: [],
            duration: 500,
            success: true,
        },
    ],
    totalFindings: Object.values(bySeverity).reduce((a, b) => a + (b || 0), 0),
    bySeverity: {
        critical: bySeverity.critical || 0,
        high: bySeverity.high || 0,
        medium: bySeverity.medium || 0,
        low: bySeverity.low || 0,
        info: bySeverity.info || 0,
    },
    byScanner: {
        semgrep: 0,
        "npm-audit": 0,
        gitleaks: 0,
        tsc: 0,
        eslint: 0,
        bandit: 0,
        gosec: 0,
        brakeman: 0,
        trivy: 0,
        "binary-analysis": 0,
        "memory-safety": 0,
        "race-condition": 0,
        healthcare: 0,
        plugin: 0,
        logic: 0,
        dast: 0,
        zap: 0,
        nuclei: 0,
        terraform: 0,
        tfsec: 0,
        checkov: 0,
        openapi: 0,
        spectral: 0,
        rust: 0,
        "cargo-audit": 0,
        clippy: 0,
        detection: 0,
    },
    totalDuration: 1500,
    allSucceeded: true,
    failedScanners: [],
});
describe("generatePrComment", () => {
    it("generates comment content structure", () => {
        const scanResult = createMockScanResult();
        const content = generatePrComment(scanResult, mockContext);
        expect(content).toHaveProperty("title");
        expect(content).toHaveProperty("summary");
        expect(content).toHaveProperty("findingsTable");
        expect(content).toHaveProperty("scannerStatus");
        expect(content).toHaveProperty("footer");
    });
    it("shows critical status for critical findings", () => {
        const scanResult = createMockScanResult({ critical: 1 });
        const content = generatePrComment(scanResult, mockContext);
        expect(content.title).toContain("🔴");
        expect(content.title).toContain("Critical Issues Found");
    });
    it("shows high status for high findings", () => {
        const scanResult = createMockScanResult({ high: 2 });
        const content = generatePrComment(scanResult, mockContext);
        expect(content.title).toContain("🟠");
        expect(content.title).toContain("High Severity Issues Found");
    });
    it("shows medium status for medium findings", () => {
        const scanResult = createMockScanResult({ medium: 3 });
        const content = generatePrComment(scanResult, mockContext);
        expect(content.title).toContain("🟡");
        expect(content.title).toContain("Medium Severity Issues Found");
    });
    it("shows clean status for no findings", () => {
        const scanResult = createMockScanResult();
        const content = generatePrComment(scanResult, mockContext);
        expect(content.title).toContain("✅");
        expect(content.title).toContain("No Issues Found");
    });
    it("shows certification status when provided", () => {
        const scanResult = createMockScanResult();
        const content = generatePrComment(scanResult, mockContext, "certified");
        expect(content.title).toContain("✅");
        expect(content.title).toContain("Certified");
        expect(content.certificationBadge).toBeDefined();
        expect(content.certificationBadge).toContain("Certified");
    });
    it("includes severity counts in summary", () => {
        const scanResult = createMockScanResult({ critical: 1, high: 2, medium: 3 });
        const content = generatePrComment(scanResult, mockContext);
        expect(content.summary).toContain("Critical");
        expect(content.summary).toContain("1");
        expect(content.summary).toContain("High");
        expect(content.summary).toContain("2");
        expect(content.summary).toContain("Medium");
        expect(content.summary).toContain("3");
    });
    it("includes scanner status table", () => {
        const scanResult = createMockScanResult();
        const content = generatePrComment(scanResult, mockContext);
        expect(content.scannerStatus).toContain("| Scanner |");
        expect(content.scannerStatus).toContain("semgrep");
        expect(content.scannerStatus).toContain("gitleaks");
        expect(content.scannerStatus).toContain("✅");
    });
    it("shows no findings message when empty", () => {
        const scanResult = createMockScanResult();
        const content = generatePrComment(scanResult, mockContext);
        expect(content.findingsTable).toContain("No findings to display");
    });
});
describe("formatCommentBody", () => {
    it("formats complete comment body", () => {
        const scanResult = createMockScanResult();
        const content = generatePrComment(scanResult, mockContext);
        const body = formatCommentBody(content);
        expect(body).toContain("<!-- vaspera-hardening-comment -->");
        expect(body).toContain("Vaspera Hardening");
        expect(body).toContain("## Scanner Results");
        expect(body).toContain("## Top Findings");
        expect(body).toContain("Generated by");
    });
    it("includes certification badge when present", () => {
        const scanResult = createMockScanResult();
        const content = generatePrComment(scanResult, mockContext, "approved");
        const body = formatCommentBody(content);
        expect(body).toContain("### Certification Status");
        expect(body).toContain("Approved");
    });
    it("does not include certification section when not present", () => {
        const scanResult = createMockScanResult();
        const content = generatePrComment(scanResult, mockContext);
        const body = formatCommentBody(content);
        expect(body).not.toContain("### Certification Status");
    });
    it("includes marker for comment identification", () => {
        const scanResult = createMockScanResult();
        const content = generatePrComment(scanResult, mockContext);
        const body = formatCommentBody(content);
        expect(body.startsWith("<!-- vaspera-hardening-comment -->")).toBe(true);
    });
});
describe("certification badges", () => {
    it("generates certified badge", () => {
        const scanResult = createMockScanResult();
        const content = generatePrComment(scanResult, mockContext, "certified");
        expect(content.certificationBadge).toContain("Certified-brightgreen");
        expect(content.certificationBadge).toContain("passed all security checks");
    });
    it("generates approved badge", () => {
        const scanResult = createMockScanResult();
        const content = generatePrComment(scanResult, mockContext, "approved");
        expect(content.certificationBadge).toContain("Approved-green");
        expect(content.certificationBadge).toContain("minor issues");
    });
    it("generates review_required badge", () => {
        const scanResult = createMockScanResult();
        const content = generatePrComment(scanResult, mockContext, "review_required");
        expect(content.certificationBadge).toContain("Review_Required-yellow");
        expect(content.certificationBadge).toContain("manual review");
    });
    it("generates blocked badge", () => {
        const scanResult = createMockScanResult();
        const content = generatePrComment(scanResult, mockContext, "blocked");
        expect(content.certificationBadge).toContain("Blocked-red");
        expect(content.certificationBadge).toContain("critical issues");
    });
});
//# sourceMappingURL=pr-comment.test.js.map