import { describe, it, expect } from "vitest";
import { generateCommitMessage, generateCommitBody, generatePRTitle, generatePRBody, groupFixesBySeverity, groupFixesByFile, groupFixesByPattern, } from "../../autofix/commit-generator.js";
describe("commit-generator", () => {
    const mockFixResults = [
        {
            findingId: "sec-001",
            file: "src/auth.ts",
            applied: true,
            diff: { before: "old", after: "new", lineNumber: 10 },
        },
        {
            findingId: "sec-002",
            file: "src/api.ts",
            applied: true,
            diff: { before: "old2", after: "new2", lineNumber: 25 },
        },
        {
            findingId: "perf-001",
            file: "src/auth.ts",
            applied: true,
            diff: { before: "old3", after: "new3", lineNumber: 50 },
        },
    ];
    describe("generateCommitMessage", () => {
        it("generates message for single fix", () => {
            const result = generateCommitMessage([mockFixResults[0]], "high");
            expect(result).toContain("fix");
            expect(result).toContain("sec-001");
        });
        it("generates message for multiple fixes", () => {
            const result = generateCommitMessage(mockFixResults, "high");
            expect(result).toContain("3 automated fixes");
        });
        it("includes severity when requested", () => {
            const result = generateCommitMessage(mockFixResults, "critical", { includeSeverity: true });
            expect(result).toContain("[critical]");
        });
        it("omits severity when requested", () => {
            const result = generateCommitMessage(mockFixResults, "critical", { includeSeverity: false });
            expect(result).not.toContain("[critical]");
        });
        it("returns fallback for empty fixes", () => {
            const result = generateCommitMessage([], undefined);
            expect(result).toContain("no changes applied");
        });
        it("truncates long messages", () => {
            const result = generateCommitMessage(mockFixResults, "critical", { maxLength: 50 });
            expect(result.length).toBeLessThanOrEqual(50);
        });
    });
    describe("generateCommitBody", () => {
        it("includes file grouping", () => {
            const result = generateCommitBody(mockFixResults, "high");
            expect(result).toContain("src/auth.ts");
            expect(result).toContain("src/api.ts");
        });
        it("includes severity", () => {
            const result = generateCommitBody(mockFixResults, "critical");
            expect(result).toContain("CRITICAL");
        });
        it("returns empty for no fixes", () => {
            const result = generateCommitBody([], undefined);
            expect(result).toBe("");
        });
        it("includes Vaspera attribution", () => {
            const result = generateCommitBody(mockFixResults, "high");
            expect(result).toContain("Vaspera Hardening");
        });
    });
    describe("generatePRTitle", () => {
        it("generates title with severity", () => {
            const result = generatePRTitle(mockFixResults, "high");
            expect(result).toContain("high");
            expect(result).toContain("3");
        });
        it("handles singular fix correctly", () => {
            const result = generatePRTitle([mockFixResults[0]], "critical");
            expect(result).toContain("1 critical-severity autofix");
            expect(result).not.toContain("autofixes");
        });
        it("uses template when provided", () => {
            const result = generatePRTitle(mockFixResults, "high", "Security: {{count}} fixes for {{severity}} issues");
            expect(result).toBe("Security: 3 fixes for high issues");
        });
    });
    describe("generatePRBody", () => {
        it("includes summary section", () => {
            const result = generatePRBody(mockFixResults, "high");
            expect(result).toContain("## Summary");
            expect(result).toContain("automated security fixes");
        });
        it("includes review checklist", () => {
            const result = generatePRBody(mockFixResults, "high");
            expect(result).toContain("## Review Checklist");
            expect(result).toContain("semantically correct");
        });
        it("includes certification ID when provided", () => {
            const result = generatePRBody(mockFixResults, "high", { certificationId: "cert-123" });
            expect(result).toContain("cert-123");
        });
        it("includes before/after when requested", () => {
            const result = generatePRBody(mockFixResults, "high", { includeBeforeAfter: true });
            expect(result).toContain("Before:");
            expect(result).toContain("After:");
        });
    });
    describe("groupFixesBySeverity", () => {
        it("groups fixes by their severity", () => {
            const severities = new Map([
                ["sec-001", "high"],
                ["sec-002", "high"],
                ["perf-001", "medium"],
            ]);
            const result = groupFixesBySeverity(mockFixResults, severities);
            expect(result.get("high")).toHaveLength(2);
            expect(result.get("medium")).toHaveLength(1);
        });
        it("defaults to medium for unknown findings", () => {
            const severities = new Map();
            const result = groupFixesBySeverity(mockFixResults, severities);
            expect(result.get("medium")).toHaveLength(3);
        });
    });
    describe("groupFixesByFile", () => {
        it("groups fixes by file path", () => {
            const result = groupFixesByFile(mockFixResults);
            expect(result.get("src/auth.ts")).toHaveLength(2);
            expect(result.get("src/api.ts")).toHaveLength(1);
        });
    });
    describe("groupFixesByPattern", () => {
        it("groups fixes by pattern ID", () => {
            const result = groupFixesByPattern(mockFixResults);
            expect(result.has("sec")).toBeTruthy();
            expect(result.has("perf")).toBeTruthy();
        });
        it("extracts pattern from finding ID", () => {
            const fixes = [
                { findingId: "sec-hardcoded-secret-001", file: "a.ts", applied: true },
                { findingId: "sec-hardcoded-secret-002", file: "b.ts", applied: true },
            ];
            const result = groupFixesByPattern(fixes);
            expect(result.get("sec-hardcoded-secret")).toHaveLength(2);
        });
    });
});
//# sourceMappingURL=commit-generator.test.js.map