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, getRunningApps, stopAllApps, } from "../../../scanners/runtime/app-launcher.js";
describe("app-launcher", () => {
    let testDir;
    beforeEach(async () => {
        testDir = join(tmpdir(), `runtime-test-${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("detectFramework", () => {
        it("detects Next.js from next.config.js", async () => {
            await writeFile(join(testDir, "next.config.js"), "module.exports = {}");
            await writeFile(join(testDir, "package.json"), JSON.stringify({ dependencies: { next: "14.0.0" } }));
            const result = await detectFramework(testDir);
            expect(result.framework).toBe("nextjs");
            expect(result.confidence).toBeGreaterThan(50);
            expect(result.devCommand).toBe("npm run dev");
            expect(result.port).toBe(3000);
        });
        it("detects Vite from vite.config.ts", async () => {
            await writeFile(join(testDir, "vite.config.ts"), "export default {}");
            await writeFile(join(testDir, "package.json"), JSON.stringify({ devDependencies: { vite: "5.0.0" } }));
            const result = await detectFramework(testDir);
            expect(result.framework).toBe("vite");
            expect(result.confidence).toBeGreaterThan(50);
            expect(result.port).toBe(5173);
        });
        it("detects Express from package.json", async () => {
            await writeFile(join(testDir, "server.js"), "const express = require('express')");
            await writeFile(join(testDir, "package.json"), JSON.stringify({ dependencies: { express: "4.18.0" } }));
            const result = await detectFramework(testDir);
            expect(result.framework).toBe("express");
            expect(result.healthEndpoint).toBe("/health");
        });
        it("detects FastAPI from requirements.txt", async () => {
            await writeFile(join(testDir, "main.py"), "from fastapi import FastAPI");
            await writeFile(join(testDir, "requirements.txt"), "fastapi\nuvicorn");
            const result = await detectFramework(testDir);
            expect(result.framework).toBe("fastapi");
            expect(result.port).toBe(8000);
            expect(result.devCommand).toBe("uvicorn main:app --reload");
        });
        it("detects Flask from requirements.txt", async () => {
            await writeFile(join(testDir, "app.py"), "from flask import Flask");
            await writeFile(join(testDir, "requirements.txt"), "flask\ngunicorn");
            const result = await detectFramework(testDir);
            expect(result.framework).toBe("flask");
            expect(result.port).toBe(5000);
        });
        it("detects Django from manage.py", async () => {
            await writeFile(join(testDir, "manage.py"), "#!/usr/bin/env python");
            await writeFile(join(testDir, "requirements.txt"), "django");
            const result = await detectFramework(testDir);
            expect(result.framework).toBe("django");
            expect(result.port).toBe(8000);
        });
        it("returns unknown for empty directory", async () => {
            const result = await detectFramework(testDir);
            expect(result.framework).toBe("unknown");
            expect(result.confidence).toBe(0);
        });
        it("includes indicators in detection result", async () => {
            await writeFile(join(testDir, "next.config.js"), "module.exports = {}");
            await writeFile(join(testDir, "package.json"), JSON.stringify({ dependencies: { next: "14.0.0" } }));
            const result = await detectFramework(testDir);
            expect(result.indicators).toContain("Found next.config.js");
            expect(result.indicators).toContain("Found next in package.json");
        });
        it("extracts version from package.json", async () => {
            await writeFile(join(testDir, "next.config.js"), "module.exports = {}");
            await writeFile(join(testDir, "package.json"), JSON.stringify({ dependencies: { next: "^14.2.3" } }));
            const result = await detectFramework(testDir);
            expect(result.version).toBe("14.2.3");
        });
        it("handles malformed package.json gracefully", async () => {
            await writeFile(join(testDir, "package.json"), "not json");
            const result = await detectFramework(testDir);
            expect(result.framework).toBe("unknown");
        });
    });
    describe("getRunningApps", () => {
        it("returns empty array when no apps running", () => {
            const apps = getRunningApps();
            expect(apps).toEqual([]);
        });
    });
});
//# sourceMappingURL=app-launcher.test.js.map