import { describe, it, expect } from "vitest";
import { groupFindings } from "../../autofix/pr-generator.js";
describe("pr-generator", () => {
    const mockFindings = [
        {
            id: "sec-001",
            severity: "critical",
            category: "sql-injection",
            file: "src/db.ts",
            line: 10,
            description: "SQL injection vulnerability",
            evidence: "test",
            confidence: 100,
            verifications: [],
            created_at: new Date().toISOString(),
        },
        {
            id: "sec-002",
            severity: "critical",
            category: "xss",
            file: "src/render.ts",
            line: 20,
            description: "XSS vulnerability",
            evidence: "test",
            confidence: 100,
            verifications: [],
            created_at: new Date().toISOString(),
        },
        {
            id: "sec-003",
            severity: "high",
            category: "hardcoded-secret",
            file: "src/config.ts",
            line: 5,
            description: "Hardcoded secret",
            evidence: "test",
            confidence: 100,
            verifications: [],
            created_at: new Date().toISOString(),
        },
        {
            id: "perf-001",
            severity: "medium",
            category: "performance",
            file: "src/db.ts",
            line: 30,
            description: "Performance issue",
            evidence: "test",
            confidence: 100,
            verifications: [],
            created_at: new Date().toISOString(),
        },
    ];
    describe("groupFindings", () => {
        describe("by severity", () => {
            it("groups findings by severity level", () => {
                const groups = groupFindings(mockFindings, "severity", "vaspera/autofix");
                expect(groups.length).toBe(3); // critical, high, medium
                const criticalGroup = groups.find((g) => g.severity === "critical");
                expect(criticalGroup?.findings).toHaveLength(2);
                const highGroup = groups.find((g) => g.severity === "high");
                expect(highGroup?.findings).toHaveLength(1);
                const mediumGroup = groups.find((g) => g.severity === "medium");
                expect(mediumGroup?.findings).toHaveLength(1);
            });
            it("generates branch names with severity", () => {
                const groups = groupFindings(mockFindings, "severity", "fix");
                for (const group of groups) {
                    expect(group.branchName).toMatch(/^fix\//);
                    if (group.severity) {
                        expect(group.branchName).toContain(group.severity);
                    }
                }
            });
            it("assigns groupId with severity prefix", () => {
                const groups = groupFindings(mockFindings, "severity", "fix");
                for (const group of groups) {
                    expect(group.groupId).toMatch(/^severity-/);
                }
            });
        });
        describe("by file", () => {
            it("groups findings by file path", () => {
                const groups = groupFindings(mockFindings, "file", "vaspera/autofix");
                expect(groups.length).toBe(3); // src/db.ts, src/render.ts, src/config.ts
                const dbGroup = groups.find((g) => g.file === "src/db.ts");
                expect(dbGroup?.findings).toHaveLength(2);
            });
            it("uses short filename in branch name", () => {
                const groups = groupFindings(mockFindings, "file", "fix");
                const dbGroup = groups.find((g) => g.file === "src/db.ts");
                // Branch names are sanitized: db.ts -> db-ts
                expect(dbGroup?.branchName).toMatch(/db-ts/);
            });
            it("assigns file property to groups", () => {
                const groups = groupFindings(mockFindings, "file", "fix");
                for (const group of groups) {
                    expect(group.file).toBeDefined();
                }
            });
        });
        describe("by pattern", () => {
            it("groups findings by category/pattern", () => {
                const groups = groupFindings(mockFindings, "pattern", "vaspera/autofix");
                expect(groups.length).toBe(4); // sql-injection, xss, hardcoded-secret, performance
            });
            it("assigns patternId to groups", () => {
                const groups = groupFindings(mockFindings, "pattern", "fix");
                const sqlGroup = groups.find((g) => g.patternId === "sql-injection");
                expect(sqlGroup).toBeDefined();
                expect(sqlGroup?.findings).toHaveLength(1);
            });
        });
        describe("single", () => {
            it("creates one group per finding", () => {
                const groups = groupFindings(mockFindings, "single", "vaspera/autofix");
                expect(groups.length).toBe(4);
                for (const group of groups) {
                    expect(group.findings).toHaveLength(1);
                }
            });
            it("preserves severity from finding", () => {
                const groups = groupFindings(mockFindings, "single", "fix");
                const criticalGroups = groups.filter((g) => g.severity === "critical");
                expect(criticalGroups.length).toBe(2);
            });
        });
        describe("edge cases", () => {
            it("handles empty findings array", () => {
                const groups = groupFindings([], "severity", "fix");
                expect(groups).toHaveLength(0);
            });
            it("handles findings without file", () => {
                const findingsWithoutFile = [
                    {
                        id: "sec-001",
                        severity: "high",
                        category: "test",
                        description: "Test",
                        evidence: "test",
                        confidence: 100,
                        verifications: [],
                        created_at: new Date().toISOString(),
                    },
                ];
                const groups = groupFindings(findingsWithoutFile, "file", "fix");
                expect(groups).toHaveLength(0);
            });
        });
    });
});
//# sourceMappingURL=pr-generator.test.js.map