import { describe, it, expect, beforeEach, afterEach } from "vitest";
import { join } from "path";
import { mkdir, writeFile, rm } from "fs/promises";
import { tmpdir } from "os";
import { parseImports, checkRelativeImport, checkPackageImport, checkDeprecatedAPIs, checkMixedFrameworks, checkFileForHallucinations, scanDirectoryForHallucinations, } from "../../../scanners/ai-code/hallucination-checker.js";
describe("hallucination-checker", () => {
    let testDir;
    beforeEach(async () => {
        testDir = join(tmpdir(), `hallucination-test-${Date.now()}-${Math.random().toString(36).slice(2, 8)}`);
        await mkdir(testDir, { recursive: true });
    });
    afterEach(async () => {
        await rm(testDir, { recursive: true, force: true });
    });
    describe("parseImports", () => {
        it("parses ES module imports", () => {
            const content = `
import { foo, bar } from "./utils";
import React from "react";
import * as path from "path";
`;
            const imports = parseImports(content, "test.ts");
            expect(imports.length).toBe(3);
            expect(imports[0].source).toBe("./utils");
            expect(imports[0].isRelative).toBe(true);
            expect(imports[0].specifiers).toContain("foo");
            expect(imports[0].specifiers).toContain("bar");
            expect(imports[1].source).toBe("react");
            expect(imports[1].isPackage).toBe(true);
            expect(imports[2].source).toBe("path");
            expect(imports[2].isPackage).toBe(true);
        });
        it("parses CommonJS requires", () => {
            const content = `
const { readFile } = require("fs");
const path = require("path");
`;
            const imports = parseImports(content, "test.js");
            expect(imports.length).toBe(2);
            expect(imports[0].source).toBe("fs");
            expect(imports[0].specifiers).toContain("readFile");
            expect(imports[1].source).toBe("path");
        });
    });
    describe("checkRelativeImport", () => {
        it("returns null for existing file", async () => {
            await writeFile(join(testDir, "utils.ts"), "export const x = 1;");
            await writeFile(join(testDir, "main.ts"), 'import { x } from "./utils";');
            const importInfo = {
                source: "./utils",
                specifiers: ["x"],
                line: 1,
                isRelative: true,
                isPackage: false,
            };
            const result = await checkRelativeImport(importInfo, join(testDir, "main.ts"), testDir);
            expect(result).toBeNull();
        });
        it("detects non-existent relative import", async () => {
            await writeFile(join(testDir, "main.ts"), 'import { x } from "./nonexistent";');
            const importInfo = {
                source: "./nonexistent",
                specifiers: ["x"],
                line: 1,
                isRelative: true,
                isPackage: false,
            };
            const result = await checkRelativeImport(importInfo, join(testDir, "main.ts"), testDir);
            expect(result).not.toBeNull();
            expect(result?.type).toBe("nonexistent_import");
            expect(result?.severity).toBe("high");
        });
        it("resolves index files", async () => {
            await mkdir(join(testDir, "utils"), { recursive: true });
            await writeFile(join(testDir, "utils", "index.ts"), "export const y = 2;");
            const importInfo = {
                source: "./utils",
                specifiers: ["y"],
                line: 1,
                isRelative: true,
                isPackage: false,
            };
            const result = await checkRelativeImport(importInfo, join(testDir, "main.ts"), testDir);
            expect(result).toBeNull();
        });
    });
    describe("checkPackageImport", () => {
        it("returns null for listed dependency", async () => {
            await writeFile(join(testDir, "package.json"), JSON.stringify({
                dependencies: { lodash: "^4.0.0" },
            }));
            const importInfo = {
                source: "lodash",
                specifiers: [],
                line: 1,
                isRelative: false,
                isPackage: true,
            };
            const result = await checkPackageImport(importInfo, testDir);
            expect(result).toBeNull();
        });
        it("returns null for Node.js builtins", async () => {
            await writeFile(join(testDir, "package.json"), JSON.stringify({ dependencies: {} }));
            const importInfo = {
                source: "fs",
                specifiers: ["readFile"],
                line: 1,
                isRelative: false,
                isPackage: true,
            };
            const result = await checkPackageImport(importInfo, testDir);
            expect(result).toBeNull();
        });
        it("detects unlisted package", async () => {
            await writeFile(join(testDir, "package.json"), JSON.stringify({ dependencies: {} }));
            const importInfo = {
                source: "some-unknown-package",
                specifiers: [],
                line: 1,
                isRelative: false,
                isPackage: true,
            };
            const result = await checkPackageImport(importInfo, testDir);
            expect(result).not.toBeNull();
            expect(result?.type).toBe("nonexistent_import");
        });
        it("detects known fabricated packages", async () => {
            await writeFile(join(testDir, "package.json"), JSON.stringify({ dependencies: {} }));
            const importInfo = {
                source: "react-native-awesome-button",
                specifiers: [],
                line: 1,
                isRelative: false,
                isPackage: true,
            };
            const result = await checkPackageImport(importInfo, testDir);
            expect(result).not.toBeNull();
            expect(result?.type).toBe("fabricated_package");
            expect(result?.severity).toBe("critical");
        });
    });
    describe("checkDeprecatedAPIs", () => {
        it("detects deprecated React lifecycle methods", () => {
            const content = `
class MyComponent extends React.Component {
  componentWillMount() {
    console.log("mounting");
  }
}
`;
            const findings = checkDeprecatedAPIs(content, "Component.tsx");
            expect(findings.length).toBe(1);
            expect(findings[0].type).toBe("deprecated_api");
            expect(findings[0].actual).toBe("componentWillMount");
        });
        it("detects deprecated string methods", () => {
            const content = `
const str = "hello".substr(0, 2);
`;
            const findings = checkDeprecatedAPIs(content, "test.ts");
            expect(findings.length).toBe(1);
            expect(findings[0].actual).toBe("substr");
        });
        it("returns empty for clean code", () => {
            const content = `
const str = "hello".substring(0, 2);
`;
            const findings = checkDeprecatedAPIs(content, "test.ts");
            expect(findings.length).toBe(0);
        });
    });
    describe("checkMixedFrameworks", () => {
        it("detects React + Vue mixing", () => {
            const content = `
import React from "react";
import { ref } from "vue";

export default function App() {
  return <div />;
}
`;
            const findings = checkMixedFrameworks(content, "app.tsx");
            expect(findings.length).toBe(1);
            expect(findings[0].type).toBe("mixed_frameworks");
            expect(findings[0].severity).toBe("high");
        });
        it("returns empty for single framework", () => {
            const content = `
import React from "react";
import { useState } from "react";

export default function App() {
  const [count, setCount] = useState(0);
  return <div>{count}</div>;
}
`;
            const findings = checkMixedFrameworks(content, "app.tsx");
            expect(findings.length).toBe(0);
        });
    });
    describe("checkFileForHallucinations", () => {
        it("combines all checks", async () => {
            await writeFile(join(testDir, "package.json"), JSON.stringify({ dependencies: {} }));
            await writeFile(join(testDir, "test.ts"), `
import { x } from "./nonexistent";
import unknown from "some-fake-package";
const str = "hi".substr(0, 1);
`);
            const findings = await checkFileForHallucinations(join(testDir, "test.ts"), testDir);
            expect(findings.length).toBeGreaterThanOrEqual(2);
        });
    });
    describe("scanDirectoryForHallucinations", () => {
        it("scans multiple files", async () => {
            await writeFile(join(testDir, "package.json"), JSON.stringify({ dependencies: { react: "^18.0.0" } }));
            await writeFile(join(testDir, "a.ts"), 'import { x } from "./nonexistent";');
            await writeFile(join(testDir, "b.ts"), 'import React from "react";');
            const result = await scanDirectoryForHallucinations(testDir);
            expect(result.files).toBe(2);
            expect(result.findings.length).toBeGreaterThan(0);
            expect(result.summary.byType).toBeDefined();
            expect(result.summary.bySeverity).toBeDefined();
        });
    });
});
//# sourceMappingURL=hallucination-checker.test.js.map