import { describe, it, expect } from "vitest";
import { parseZapAlerts } from "../../../scanners/dast/zap.js";
describe("zap scanner", () => {
    describe("parseZapAlerts", () => {
        it("parses ZAP alert to DASTFinding", () => {
            const alert = {
                sourceid: "1",
                other: "",
                method: "GET",
                evidence: "<script>alert(1)</script>",
                pluginId: "40012",
                cweid: "79",
                confidence: "High",
                wascid: "8",
                description: "Cross-site Scripting (XSS) is an attack technique...",
                messageId: "1",
                inputVector: "url",
                url: "https://example.com/search?q=test",
                tags: { "OWASP_2021_A03": "", "CWE-79": "" },
                reference: "https://owasp.org/www-community/attacks/xss/",
                solution: "Phase: Architecture and Design...",
                alert: "Cross Site Scripting (Reflected)",
                param: "q",
                attack: "<script>alert(1)</script>",
                name: "Cross Site Scripting (Reflected)",
                risk: "High",
                id: "1",
                alertRef: "40012-1",
            };
            const findings = parseZapAlerts([alert]);
            expect(findings).toHaveLength(1);
            expect(findings[0].scanner).toBe("zap");
            expect(findings[0].ruleId).toBe("zap-40012");
            expect(findings[0].name).toBe("Cross Site Scripting (Reflected)");
            expect(findings[0].severity).toBe("high");
            expect(findings[0].confidence).toBe(90);
            expect(findings[0].url).toBe("https://example.com/search?q=test");
            expect(findings[0].method).toBe("GET");
            expect(findings[0].parameter).toBe("q");
            expect(findings[0].evidence).toBe("<script>alert(1)</script>");
            expect(findings[0].attack).toBe("<script>alert(1)</script>");
            expect(findings[0].cweIds).toContain("CWE-79");
            expect(findings[0].solution).toContain("Phase:");
        });
        it("maps ZAP risk levels correctly", () => {
            const alerts = [
                { ...createMockAlert(), risk: "High", pluginId: "1" },
                { ...createMockAlert(), risk: "Medium", pluginId: "2" },
                { ...createMockAlert(), risk: "Low", pluginId: "3" },
                { ...createMockAlert(), risk: "Informational", pluginId: "4" },
            ];
            const findings = parseZapAlerts(alerts);
            expect(findings[0].severity).toBe("high");
            expect(findings[1].severity).toBe("medium");
            expect(findings[2].severity).toBe("low");
            expect(findings[3].severity).toBe("info");
        });
        it("maps ZAP confidence levels correctly", () => {
            const alerts = [
                { ...createMockAlert(), confidence: "High", pluginId: "1" },
                { ...createMockAlert(), confidence: "Medium", pluginId: "2" },
                { ...createMockAlert(), confidence: "Low", pluginId: "3" },
                { ...createMockAlert(), confidence: "Confirmed", pluginId: "4" },
            ];
            const findings = parseZapAlerts(alerts);
            expect(findings[0].confidence).toBe(90);
            expect(findings[1].confidence).toBe(70);
            expect(findings[2].confidence).toBe(50);
            expect(findings[3].confidence).toBe(100);
        });
        it("handles alerts without optional fields", () => {
            const alert = {
                sourceid: "1",
                other: "",
                method: "",
                evidence: "",
                pluginId: "10096",
                cweid: "",
                confidence: "Medium",
                wascid: "",
                description: "The response time was significantly different...",
                messageId: "1",
                inputVector: "",
                url: "https://example.com/api",
                tags: {},
                reference: "",
                solution: "",
                alert: "Timestamp Disclosure",
                param: "",
                attack: "",
                name: "Timestamp Disclosure",
                risk: "Low",
                id: "2",
                alertRef: "10096-1",
            };
            const findings = parseZapAlerts([alert]);
            expect(findings).toHaveLength(1);
            expect(findings[0].cweIds).toBeUndefined();
            expect(findings[0].references).toBeUndefined();
            expect(findings[0].tags).toEqual([]);
        });
        it("parses multiple references", () => {
            const alert = {
                ...createMockAlert(),
                reference: "https://owasp.org/xss\nhttps://cwe.mitre.org/data/definitions/79.html",
            };
            const findings = parseZapAlerts([alert]);
            expect(findings[0].references).toHaveLength(2);
            expect(findings[0].references).toContain("https://owasp.org/xss");
            expect(findings[0].references).toContain("https://cwe.mitre.org/data/definitions/79.html");
        });
        it("extracts tags from ZAP alert", () => {
            const alert = {
                ...createMockAlert(),
                tags: {
                    "OWASP_2021_A03": "Injection",
                    "CWE-79": "",
                    "WSTG-v42-INPV-01": "",
                },
            };
            const findings = parseZapAlerts([alert]);
            expect(findings[0].tags).toContain("OWASP_2021_A03");
            expect(findings[0].tags).toContain("CWE-79");
            expect(findings[0].tags).toContain("WSTG-v42-INPV-01");
        });
        it("handles empty alerts array", () => {
            const findings = parseZapAlerts([]);
            expect(findings).toHaveLength(0);
        });
    });
});
function createMockAlert() {
    return {
        sourceid: "1",
        other: "",
        method: "GET",
        evidence: "",
        pluginId: "99999",
        cweid: "0",
        confidence: "Medium",
        wascid: "0",
        description: "Test description",
        messageId: "1",
        inputVector: "",
        url: "https://example.com",
        tags: {},
        reference: "",
        solution: "",
        alert: "Test Alert",
        param: "",
        attack: "",
        name: "Test Alert",
        risk: "Medium",
        id: "1",
        alertRef: "99999-1",
    };
}
//# sourceMappingURL=zap.test.js.map