import { describe, it, expect, beforeEach, afterEach } from "vitest";
import { join } from "path";
import { mkdir, writeFile, rm } from "fs/promises";
import { tmpdir } from "os";
import { detectFramework, launchApp, stopApp, stopAllApps, getRunningApps, discoverFlows, runFlow, runAllFlows, generateSampleFlow, loadFlow, } from "../../../scanners/runtime/index.js";
describe("runtime module exports", () => {
    it("exports detectFramework", () => {
        expect(typeof detectFramework).toBe("function");
    });
    it("exports launchApp", () => {
        expect(typeof launchApp).toBe("function");
    });
    it("exports stopApp", () => {
        expect(typeof stopApp).toBe("function");
    });
    it("exports stopAllApps", () => {
        expect(typeof stopAllApps).toBe("function");
    });
    it("exports getRunningApps", () => {
        expect(typeof getRunningApps).toBe("function");
    });
    it("exports discoverFlows", () => {
        expect(typeof discoverFlows).toBe("function");
    });
    it("exports runFlow", () => {
        expect(typeof runFlow).toBe("function");
    });
    it("exports runAllFlows", () => {
        expect(typeof runAllFlows).toBe("function");
    });
    it("exports generateSampleFlow", () => {
        expect(typeof generateSampleFlow).toBe("function");
    });
    it("exports loadFlow", () => {
        expect(typeof loadFlow).toBe("function");
    });
});
describe("runtime integration", () => {
    let testDir;
    beforeEach(async () => {
        testDir = join(tmpdir(), `runtime-integration-${Date.now()}-${Math.random().toString(36).slice(2, 8)}`);
        await mkdir(testDir, { recursive: true });
    });
    afterEach(async () => {
        await stopAllApps();
        await rm(testDir, { recursive: true, force: true });
    });
    describe("flow discovery and execution", () => {
        it("discovers and runs all flows in project", async () => {
            const flowsDir = join(testDir, ".vaspera/flows");
            await mkdir(flowsDir, { recursive: true });
            const flow1 = `
name: "health"
priority: critical
steps:
  - action: wait
    timeout: 50
`;
            const flow2 = `
name: "smoke"
priority: high
steps:
  - action: wait
    timeout: 50
`;
            await writeFile(join(flowsDir, "health.yaml"), flow1);
            await writeFile(join(flowsDir, "smoke.yaml"), flow2);
            const results = await runAllFlows(testDir, "http://localhost:3000");
            expect(results).toHaveLength(2);
            const names = results.map((r) => r.flowName);
            expect(names).toContain("health");
            expect(names).toContain("smoke");
        });
        it("filters flows by priority", async () => {
            const flowsDir = join(testDir, ".vaspera/flows");
            await mkdir(flowsDir, { recursive: true });
            const critical = `name: "critical"\npriority: critical\nsteps:\n  - action: wait\n    timeout: 10`;
            const high = `name: "high"\npriority: high\nsteps:\n  - action: wait\n    timeout: 10`;
            const medium = `name: "medium"\npriority: medium\nsteps:\n  - action: wait\n    timeout: 10`;
            const low = `name: "low"\npriority: low\nsteps:\n  - action: wait\n    timeout: 10`;
            await writeFile(join(flowsDir, "critical.yaml"), critical);
            await writeFile(join(flowsDir, "high.yaml"), high);
            await writeFile(join(flowsDir, "medium.yaml"), medium);
            await writeFile(join(flowsDir, "low.yaml"), low);
            const highAndAbove = await runAllFlows(testDir, "http://localhost:3000", {
                priority: "high",
            });
            expect(highAndAbove).toHaveLength(2);
            const names = highAndAbove.map((r) => r.flowName);
            expect(names).toContain("critical");
            expect(names).toContain("high");
            expect(names).not.toContain("medium");
            expect(names).not.toContain("low");
        });
        it("sorts flows by priority (critical first)", async () => {
            const flowsDir = join(testDir, ".vaspera/flows");
            await mkdir(flowsDir, { recursive: true });
            const low = `name: "low"\npriority: low\nsteps:\n  - action: wait\n    timeout: 10`;
            const critical = `name: "critical"\npriority: critical\nsteps:\n  - action: wait\n    timeout: 10`;
            const medium = `name: "medium"\npriority: medium\nsteps:\n  - action: wait\n    timeout: 10`;
            await writeFile(join(flowsDir, "01-low.yaml"), low);
            await writeFile(join(flowsDir, "02-critical.yaml"), critical);
            await writeFile(join(flowsDir, "03-medium.yaml"), medium);
            const results = await runAllFlows(testDir, "http://localhost:3000");
            expect(results[0].flowName).toBe("critical");
            expect(results[1].flowName).toBe("medium");
            expect(results[2].flowName).toBe("low");
        });
    });
    describe("framework detection integration", () => {
        it("detects framework and suggests correct port", async () => {
            await writeFile(join(testDir, "vite.config.ts"), "export default {}");
            await writeFile(join(testDir, "package.json"), JSON.stringify({ devDependencies: { vite: "5.0.0" } }));
            const detection = await detectFramework(testDir);
            expect(detection.framework).toBe("vite");
            expect(detection.port).toBe(5173);
        });
    });
});
//# sourceMappingURL=index.test.js.map