import { describe, it, expect } from "vitest";
import { analyzeFilePath, shouldFilter, } from "../../scanners/fp-filter.js";
describe("fp-filter", () => {
    describe("analyzeFilePath", () => {
        it("identifies test files", () => {
            const testPaths = [
                "src/__tests__/foo.test.ts",
                "src/foo.spec.js",
                "tests/unit/bar.ts",
                "cypress/e2e/login.cy.ts",
                "src/components/Button.stories.tsx",
            ];
            for (const path of testPaths) {
                const result = analyzeFilePath(path);
                expect(result.isTestFile, `${path} should be identified as test file`).toBe(true);
            }
        });
        it("identifies generated code", () => {
            const generatedPaths = [
                "src/api.generated.ts",
                "generated/types.ts",
                "__generated__/schema.ts",
                "src/types.d.ts",
                "src/graphql.ts",
            ];
            for (const path of generatedPaths) {
                const result = analyzeFilePath(path);
                expect(result.isGeneratedCode, `${path} should be identified as generated`).toBe(true);
            }
        });
        it("identifies vendor/third-party code", () => {
            const vendorPaths = [
                "node_modules/lodash/index.js",
                "vendor/jquery.min.js",
                "third-party/analytics.js",
                "lib/vendor/moment.js",
            ];
            for (const path of vendorPaths) {
                const result = analyzeFilePath(path);
                expect(result.isThirdPartyVendored, `${path} should be identified as vendor`).toBe(true);
            }
        });
        it("identifies fixture/example files", () => {
            const fixturePaths = [
                "fixtures/sample-data.json",
                "examples/basic/app.ts",
                "demo/showcase.tsx",
                "seeds/users.ts",
            ];
            for (const path of fixturePaths) {
                const result = analyzeFilePath(path);
                expect(result.isFixtureFile || result.isExampleFile, `${path} should be identified as fixture/example`).toBe(true);
            }
        });
        it("identifies regular source files", () => {
            const srcPaths = [
                "src/index.ts",
                "src/components/Button.tsx",
                "lib/utils.js",
            ];
            for (const path of srcPaths) {
                const result = analyzeFilePath(path);
                expect(result.isTestFile).toBe(false);
                expect(result.isGeneratedCode).toBe(false);
                expect(result.isThirdPartyVendored).toBe(false);
                expect(result.isFixtureFile).toBe(false);
            }
        });
        it("identifies mock files", () => {
            const mockPaths = [
                "src/api.mock.ts",
                "mocks/handlers.ts",
                "__mocks__/axios.js",
            ];
            for (const path of mockPaths) {
                const result = analyzeFilePath(path);
                expect(result.isMockFile || result.isTestFile, `${path} should be mock/test`).toBe(true);
            }
        });
    });
    describe("shouldFilter", () => {
        const createFinding = (overrides = {}) => ({
            scanner: "semgrep",
            ruleId: "security.sql-injection",
            file: "src/db.ts",
            line: 10,
            message: "SQL injection vulnerability",
            severity: "high",
            confidence: 100,
            category: "sql-injection",
            ...overrides,
        });
        const createContext = (overrides = {}) => ({
            codeContext: {
                isTestFile: false,
                isGeneratedCode: false,
                isThirdPartyVendored: false,
                isMockFile: false,
                isFixtureFile: false,
                isExampleFile: false,
            },
            semanticContext: {
                hasValidation: false,
                hasEncoding: false,
                isSanitized: false,
                isConstant: false,
                isEnvironmentVariable: false,
            },
            historicalContext: {
                previousFPs: [],
                ruleAccuracy: 1.0,
                suppressions: [],
            },
            ...overrides,
        });
        it("filters vendor/third-party code with high confidence", () => {
            const finding = createFinding();
            const context = createContext({
                codeContext: {
                    isTestFile: false,
                    isGeneratedCode: false,
                    isThirdPartyVendored: true,
                    isMockFile: false,
                    isFixtureFile: false,
                    isExampleFile: false,
                },
            });
            const result = shouldFilter(finding, context);
            expect(result.filter).toBe(true);
            expect(result.confidence).toBeGreaterThanOrEqual(90);
            expect(result.reason).toContain("Third-party");
        });
        it("filters SQL injection in test files", () => {
            const finding = createFinding({ category: "sql-injection" });
            const context = createContext({
                codeContext: {
                    isTestFile: true,
                    isGeneratedCode: false,
                    isThirdPartyVendored: false,
                    isMockFile: false,
                    isFixtureFile: false,
                    isExampleFile: false,
                },
            });
            const result = shouldFilter(finding, context);
            expect(result.filter).toBe(true);
            expect(result.reason).toContain("Test file");
        });
        it("filters hardcoded secrets that are env vars", () => {
            const finding = createFinding({ category: "hardcoded-secret" });
            const context = createContext({
                semanticContext: {
                    hasValidation: false,
                    hasEncoding: false,
                    isSanitized: false,
                    isConstant: false,
                    isEnvironmentVariable: true,
                },
            });
            const result = shouldFilter(finding, context);
            expect(result.filter).toBe(true);
            expect(result.reason).toContain("Environment variable");
        });
        it("filters constant values for injection categories", () => {
            const finding = createFinding({ category: "sql-injection" });
            const context = createContext({
                semanticContext: {
                    hasValidation: false,
                    hasEncoding: false,
                    isSanitized: false,
                    isConstant: true,
                    isEnvironmentVariable: false,
                },
            });
            const result = shouldFilter(finding, context);
            expect(result.filter).toBe(true);
            expect(result.reason).toContain("Constant value");
        });
        it("does not filter real findings in source code", () => {
            const finding = createFinding();
            const context = createContext();
            const result = shouldFilter(finding, context);
            expect(result.filter).toBe(false);
            expect(result.confidence).toBe(100);
            expect(result.suggestion).toBe("confirm");
        });
        it("suggests review when sanitization is present", () => {
            const finding = createFinding();
            const context = createContext({
                semanticContext: {
                    hasValidation: true,
                    hasEncoding: false,
                    isSanitized: false,
                    isConstant: false,
                    isEnvironmentVariable: false,
                },
            });
            const result = shouldFilter(finding, context);
            expect(result.filter).toBe(false);
            expect(result.suggestion).toBe("review");
        });
        it("filters fixture files with lower confidence", () => {
            const finding = createFinding();
            const context = createContext({
                codeContext: {
                    isTestFile: false,
                    isGeneratedCode: false,
                    isThirdPartyVendored: false,
                    isMockFile: false,
                    isFixtureFile: true,
                    isExampleFile: false,
                },
            });
            const result = shouldFilter(finding, context);
            expect(result.filter).toBe(true);
            expect(result.confidence).toBeLessThan(80);
            expect(result.reason).toContain("Fixture");
        });
        it("considers historical suppressions", () => {
            const finding = createFinding();
            const context = createContext({
                historicalContext: {
                    previousFPs: [],
                    ruleAccuracy: 1.0,
                    suppressions: ["security.sql-injection"],
                },
            });
            const result = shouldFilter(finding, context);
            expect(result.filter).toBe(true);
            expect(result.reason).toContain("suppressed");
        });
    });
});
//# sourceMappingURL=fp-filter.test.js.map