import { describe, it, expect, beforeEach, afterEach } from "vitest";
import { join } from "path";
import { mkdir, writeFile, rm, readFile } from "fs/promises";
import { tmpdir } from "os";
import { loadProfile, discoverProfiles, generateSampleProfile, generateK6Script, } from "../../../scanners/scale/load-profiler.js";
describe("load-profiler", () => {
    let testDir;
    beforeEach(async () => {
        testDir = join(tmpdir(), `scale-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("loadProfile", () => {
        it("loads a valid YAML profile", async () => {
            const profileContent = `
name: "test-profile"
description: "Test load profile"
tool: k6
scenarios:
  - name: "Basic"
    duration: "1m"
    vus: 10
`;
            const profilePath = join(testDir, "test.yaml");
            await writeFile(profilePath, profileContent);
            const profile = await loadProfile(profilePath);
            expect(profile.name).toBe("test-profile");
            expect(profile.tool).toBe("k6");
            expect(profile.scenarios).toHaveLength(1);
        });
        it("throws on invalid YAML", async () => {
            const profilePath = join(testDir, "invalid.yaml");
            await writeFile(profilePath, "invalid: yaml: content:");
            await expect(loadProfile(profilePath)).rejects.toThrow();
        });
    });
    describe("discoverProfiles", () => {
        it("discovers all profiles in .vaspera/load/", async () => {
            const profilesDir = join(testDir, ".vaspera/load");
            await mkdir(profilesDir, { recursive: true });
            const profile1 = `name: "profile1"\nscenarios:\n  - name: "s1"\n    duration: "1m"\n    vus: 10`;
            const profile2 = `name: "profile2"\nscenarios:\n  - name: "s2"\n    duration: "2m"\n    vus: 20`;
            await writeFile(join(profilesDir, "profile1.yaml"), profile1);
            await writeFile(join(profilesDir, "profile2.yml"), profile2);
            const profiles = await discoverProfiles(testDir);
            expect(profiles).toHaveLength(2);
            expect(profiles.map((p) => p.profile.name).sort()).toEqual(["profile1", "profile2"]);
        });
        it("returns empty array when no profiles directory", async () => {
            const profiles = await discoverProfiles(testDir);
            expect(profiles).toEqual([]);
        });
        it("skips invalid profile files", async () => {
            const profilesDir = join(testDir, ".vaspera/load");
            await mkdir(profilesDir, { recursive: true });
            const valid = `name: "valid"\nscenarios:\n  - name: "s"\n    duration: "1m"\n    vus: 1`;
            const invalid = `name: 123\nscenarios: "not an array"`;
            await writeFile(join(profilesDir, "valid.yaml"), valid);
            await writeFile(join(profilesDir, "invalid.yaml"), invalid);
            const profiles = await discoverProfiles(testDir);
            expect(profiles).toHaveLength(1);
            expect(profiles[0].profile.name).toBe("valid");
        });
    });
    describe("generateSampleProfile", () => {
        it("creates a sample profile file", async () => {
            const profilePath = await generateSampleProfile(testDir, "staging");
            expect(profilePath).toBe(join(testDir, ".vaspera/load/staging.yaml"));
            const content = await readFile(profilePath, "utf-8");
            expect(content).toContain('name: "staging"');
            expect(content).toContain("scenarios:");
            expect(content).toContain("endpoints:");
        });
        it("uses default name when not specified", async () => {
            const profilePath = await generateSampleProfile(testDir);
            expect(profilePath).toContain("production.yaml");
        });
    });
    describe("generateK6Script", () => {
        it("generates valid k6 script", () => {
            const profile = {
                name: "test",
                tool: "k6",
                thresholds: {
                    p95: 500,
                    p99: 1000,
                    errorRate: 0.01,
                },
                endpoints: [
                    { path: "/", method: "GET", weight: 100 },
                ],
                scenarios: [
                    { name: "Ramp", type: "ramp", duration: "2m", vus: { start: 1, end: 50 } },
                ],
            };
            const script = generateK6Script(profile, "http://localhost:3000");
            expect(script).toContain("import http from 'k6/http'");
            expect(script).toContain("const BASE_URL = 'http://localhost:3000'");
            expect(script).toContain("http_req_duration");
            expect(script).toContain("p(95)<500");
            expect(script).toContain("Ramp:");
        });
        it("includes endpoints with correct methods", () => {
            const profile = {
                name: "test",
                tool: "k6",
                endpoints: [
                    { path: "/api/data", method: "POST", weight: 50, body: { key: "value" } },
                ],
                scenarios: [
                    { name: "Test", type: "ramp", duration: "1m", vus: 10 },
                ],
            };
            const script = generateK6Script(profile, "http://localhost:3000");
            expect(script).toContain("http.post");
            expect(script).toContain("/api/data");
        });
    });
});
//# sourceMappingURL=load-profiler.test.js.map