import { describe, it, expect } from "vitest";
import { scoreChange, scoreAllChanges, calculateDetectionScore, calculateReviewScore, calculateOverallScore, determineReviewPriority, groupByReviewPriority, calculateHallucinationScore, } from "../../../scanners/ai-code/confidence-scorer.js";
describe("confidence-scorer", () => {
    describe("scoreChange", () => {
        it("scores change with no indicators as low likelihood", () => {
            const result = scoreChange({
                file: "test.ts",
                startLine: 1,
                endLine: 10,
                changeType: "modified",
                indicators: [],
                hallucinations: [],
            });
            expect(result.aiLikelihood).toBe(0);
            expect(result.confidence).toBe("uncertain");
            expect(result.requiresReview).toBe(false);
        });
        it("scores change with high confidence indicators", () => {
            const indicators = [
                {
                    type: "comment",
                    location: { file: "test.ts", line: 1 },
                    description: "AI comment",
                    confidence: "high",
                    evidence: "// Generated by AI",
                },
                {
                    type: "metadata",
                    location: { file: "test.ts", line: 2 },
                    description: "AI metadata",
                    confidence: "high",
                    evidence: "@cursor-ai",
                },
            ];
            const result = scoreChange({
                file: "test.ts",
                startLine: 1,
                endLine: 10,
                changeType: "modified",
                indicators,
                hallucinations: [],
            });
            expect(result.aiLikelihood).toBeGreaterThan(50);
            expect(result.confidence).toBe("medium");
            expect(result.requiresReview).toBe(true);
        });
        it("requires review for hallucinations", () => {
            const hallucinations = [
                {
                    type: "nonexistent_import",
                    location: { file: "test.ts", line: 1 },
                    severity: "high",
                    description: "Import not found",
                    actual: "./nonexistent",
                },
            ];
            const result = scoreChange({
                file: "test.ts",
                startLine: 1,
                endLine: 10,
                changeType: "modified",
                indicators: [],
                hallucinations,
            });
            expect(result.requiresReview).toBe(true);
            expect(result.reviewReason).toContain("hallucination");
        });
        it("respects config auto approve threshold", () => {
            const indicators = [
                {
                    type: "pattern",
                    location: { file: "test.ts", line: 1 },
                    description: "Low pattern",
                    confidence: "low",
                    evidence: "test",
                },
            ];
            const result = scoreChange({
                file: "test.ts",
                startLine: 1,
                endLine: 10,
                changeType: "modified",
                indicators,
                hallucinations: [],
            }, {
                review: {
                    requireForConfidence: "medium",
                    requireForHallucinations: true,
                    autoApproveBelow: 10,
                },
            });
            expect(result.requiresReview).toBe(false);
        });
    });
    describe("scoreAllChanges", () => {
        it("scores multiple changes and summarizes", () => {
            const changes = [
                {
                    file: "a.ts",
                    startLine: 1,
                    endLine: 10,
                    changeType: "added",
                    indicators: [
                        {
                            type: "comment",
                            location: { file: "a.ts", line: 1 },
                            description: "AI",
                            confidence: "high",
                            evidence: "test",
                        },
                    ],
                    hallucinations: [],
                },
                {
                    file: "b.ts",
                    startLine: 1,
                    endLine: 20,
                    changeType: "modified",
                    indicators: [],
                    hallucinations: [
                        {
                            type: "nonexistent_import",
                            location: { file: "b.ts", line: 1 },
                            severity: "critical",
                            description: "Missing",
                            actual: "unknown",
                        },
                    ],
                },
                {
                    file: "c.ts",
                    startLine: 1,
                    endLine: 5,
                    changeType: "modified",
                    indicators: [],
                    hallucinations: [],
                },
            ];
            const result = scoreAllChanges(changes);
            expect(result.scoredChanges.length).toBe(3);
            expect(result.summary.totalChanges).toBe(3);
            expect(result.summary.aiLikelyChanges).toBeGreaterThan(0);
            expect(result.summary.requiresReview).toBeGreaterThan(0);
            expect(result.summary.hallucinationsFound).toBe(1);
        });
    });
    describe("calculateDetectionScore", () => {
        it("returns 100 for no changes", () => {
            expect(calculateDetectionScore([])).toBe(100);
        });
        it("returns higher score for lower AI likelihood", () => {
            const lowLikelihood = [
                {
                    file: "a.ts",
                    startLine: 1,
                    endLine: 10,
                    changeType: "modified",
                    aiLikelihood: 10,
                    confidence: "low",
                    indicators: [],
                    hallucinations: [],
                    requiresReview: false,
                },
            ];
            const highLikelihood = [
                {
                    file: "b.ts",
                    startLine: 1,
                    endLine: 10,
                    changeType: "modified",
                    aiLikelihood: 80,
                    confidence: "high",
                    indicators: [],
                    hallucinations: [],
                    requiresReview: true,
                },
            ];
            expect(calculateDetectionScore(lowLikelihood)).toBeGreaterThan(calculateDetectionScore(highLikelihood));
        });
    });
    describe("calculateHallucinationScore", () => {
        it("returns 100 for no hallucinations", () => {
            const changes = [
                {
                    file: "a.ts",
                    startLine: 1,
                    endLine: 10,
                    changeType: "modified",
                    aiLikelihood: 50,
                    confidence: "medium",
                    indicators: [],
                    hallucinations: [],
                    requiresReview: false,
                },
            ];
            expect(calculateHallucinationScore(changes)).toBe(100);
        });
        it("penalizes for hallucinations", () => {
            const changes = [
                {
                    file: "a.ts",
                    startLine: 1,
                    endLine: 10,
                    changeType: "modified",
                    aiLikelihood: 50,
                    confidence: "medium",
                    indicators: [],
                    hallucinations: [
                        {
                            type: "nonexistent_import",
                            location: { file: "a.ts", line: 1 },
                            severity: "high",
                            description: "Missing",
                            actual: "unknown",
                        },
                    ],
                    requiresReview: true,
                },
            ];
            expect(calculateHallucinationScore(changes)).toBeLessThan(100);
        });
    });
    describe("calculateReviewScore", () => {
        it("returns 100 when no review needed", () => {
            const changes = [
                {
                    file: "a.ts",
                    startLine: 1,
                    endLine: 10,
                    changeType: "modified",
                    aiLikelihood: 10,
                    confidence: "low",
                    indicators: [],
                    hallucinations: [],
                    requiresReview: false,
                },
            ];
            expect(calculateReviewScore(changes)).toBe(100);
        });
        it("returns lower score when review needed", () => {
            const changes = [
                {
                    file: "a.ts",
                    startLine: 1,
                    endLine: 10,
                    changeType: "modified",
                    aiLikelihood: 80,
                    confidence: "high",
                    indicators: [],
                    hallucinations: [],
                    requiresReview: true,
                },
                {
                    file: "b.ts",
                    startLine: 1,
                    endLine: 10,
                    changeType: "modified",
                    aiLikelihood: 10,
                    confidence: "low",
                    indicators: [],
                    hallucinations: [],
                    requiresReview: false,
                },
            ];
            expect(calculateReviewScore(changes)).toBe(50);
        });
    });
    describe("calculateOverallScore", () => {
        it("calculates weighted average", () => {
            const scores = {
                detectionScore: 80,
                hallucinationScore: 90,
                reviewScore: 70,
            };
            const overall = calculateOverallScore(scores);
            expect(overall).toBeGreaterThan(0);
            expect(overall).toBeLessThanOrEqual(100);
        });
        it("weights hallucination highest", () => {
            const lowHallucination = calculateOverallScore({
                detectionScore: 50,
                hallucinationScore: 100,
                reviewScore: 50,
            });
            const highHallucination = calculateOverallScore({
                detectionScore: 100,
                hallucinationScore: 0,
                reviewScore: 100,
            });
            expect(lowHallucination).toBeGreaterThan(highHallucination);
        });
    });
    describe("determineReviewPriority", () => {
        it("returns none for no review", () => {
            const change = {
                file: "a.ts",
                startLine: 1,
                endLine: 10,
                changeType: "modified",
                aiLikelihood: 10,
                confidence: "low",
                indicators: [],
                hallucinations: [],
                requiresReview: false,
            };
            expect(determineReviewPriority(change)).toBe("none");
        });
        it("returns critical for critical hallucinations", () => {
            const change = {
                file: "a.ts",
                startLine: 1,
                endLine: 10,
                changeType: "modified",
                aiLikelihood: 80,
                confidence: "high",
                indicators: [],
                hallucinations: [
                    {
                        type: "fabricated_package",
                        location: { file: "a.ts", line: 1 },
                        severity: "critical",
                        description: "Fake package",
                        actual: "fake-package",
                    },
                ],
                requiresReview: true,
            };
            expect(determineReviewPriority(change)).toBe("critical");
        });
    });
    describe("groupByReviewPriority", () => {
        it("groups changes by priority", () => {
            const changes = [
                {
                    file: "a.ts",
                    startLine: 1,
                    endLine: 10,
                    changeType: "modified",
                    aiLikelihood: 10,
                    confidence: "low",
                    indicators: [],
                    hallucinations: [],
                    requiresReview: false,
                },
                {
                    file: "b.ts",
                    startLine: 1,
                    endLine: 10,
                    changeType: "modified",
                    aiLikelihood: 80,
                    confidence: "high",
                    indicators: [],
                    hallucinations: [],
                    requiresReview: true,
                },
            ];
            const groups = groupByReviewPriority(changes);
            expect(groups.none.length).toBe(1);
            expect(groups.high.length).toBe(1);
        });
    });
});
//# sourceMappingURL=confidence-scorer.test.js.map