import { describe, it, expect, beforeEach, afterEach } from "vitest";
import { join } from "path";
import { mkdir, writeFile, rm } from "fs/promises";
import { tmpdir } from "os";
import { detectProvider, isDeploymentEnvironment, getCurrentEnvironment, } from "../../../scanners/deploy/provider-detector.js";
describe("provider-detector", () => {
    let testDir;
    beforeEach(async () => {
        testDir = join(tmpdir(), `deploy-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("detectProvider", () => {
        it("detects Vercel from vercel.json", async () => {
            await writeFile(join(testDir, "vercel.json"), "{}");
            const result = await detectProvider(testDir);
            expect(result.provider).toBe("vercel");
            expect(result.confidence).toBeGreaterThan(30);
            expect(result.indicators).toContain("Found vercel.json");
        });
        it("detects Vercel from .vercel/project.json", async () => {
            const vercelDir = join(testDir, ".vercel");
            await mkdir(vercelDir, { recursive: true });
            await writeFile(join(vercelDir, "project.json"), JSON.stringify({ projectId: "prj_123", orgId: "org_456" }));
            const result = await detectProvider(testDir);
            expect(result.provider).toBe("vercel");
            expect(result.projectId).toBe("prj_123");
        });
        it("detects AWS from serverless.yml", async () => {
            await writeFile(join(testDir, "serverless.yml"), "service: my-service");
            const result = await detectProvider(testDir);
            expect(result.provider).toBe("aws");
            expect(result.indicators).toContain("Found serverless.yml");
        });
        it("detects Railway from railway.json", async () => {
            await writeFile(join(testDir, "railway.json"), "{}");
            const result = await detectProvider(testDir);
            expect(result.provider).toBe("railway");
        });
        it("detects Render from render.yaml", async () => {
            await writeFile(join(testDir, "render.yaml"), "services: []");
            const result = await detectProvider(testDir);
            expect(result.provider).toBe("render");
        });
        it("detects Fly from fly.toml", async () => {
            await writeFile(join(testDir, "fly.toml"), 'app = "my-app"');
            const result = await detectProvider(testDir);
            expect(result.provider).toBe("fly");
        });
        it("detects GCP from app.yaml", async () => {
            await writeFile(join(testDir, "app.yaml"), "runtime: nodejs18");
            const result = await detectProvider(testDir);
            expect(result.provider).toBe("gcp");
        });
        it("returns unknown for empty directory", async () => {
            const result = await detectProvider(testDir);
            expect(result.provider).toBe("unknown");
            expect(result.confidence).toBe(0);
        });
        it("detects from package.json dependencies", async () => {
            await writeFile(join(testDir, "package.json"), JSON.stringify({
                dependencies: { "@vercel/node": "^2.0.0" },
            }));
            const result = await detectProvider(testDir);
            expect(result.provider).toBe("vercel");
            expect(result.indicators.some((i) => i.includes("@vercel/node"))).toBe(true);
        });
    });
    describe("isDeploymentEnvironment", () => {
        it("returns false when no deployment env vars", () => {
            const original = { ...process.env };
            delete process.env.VERCEL;
            delete process.env.AWS_LAMBDA_FUNCTION_NAME;
            delete process.env.GOOGLE_CLOUD_PROJECT;
            expect(isDeploymentEnvironment()).toBe(false);
            Object.assign(process.env, original);
        });
    });
    describe("getCurrentEnvironment", () => {
        it("returns development by default", () => {
            const result = getCurrentEnvironment("unknown");
            expect(["development", "test"]).toContain(result);
        });
    });
});
//# sourceMappingURL=provider-detector.test.js.map