import { describe, expect, it } from "bun:test";
import { router } from "./router";

describe("Web Router", () => {
    it("should return home page on /", async () => {
        const req = new Request("http://localhost/");
        const res = await router(req);
        expect(res.status).toBe(200);
        expect(res.headers.get("Content-Type")).toBe("text/html");
        const text = await res.text();
        expect(text).toContain("Aurora Web");
        expect(text).toContain("Uptime:");
        expect(text).toContain('id="uptime-display"');
    });

    it("should return dashboard page on /dashboard", async () => {
        const req = new Request("http://localhost/dashboard");
        const res = await router(req);
        expect(res.status).toBe(200);
        expect(await res.text()).toContain("Live Activity");
    });

    it("should return health check on /health", async () => {
        const req = new Request("http://localhost/health");
        const res = await router(req);
        expect(res.status).toBe(200);
        expect(res.headers.get("Content-Type")).toBe("application/json");
        const data = await res.json();
        expect(data).toHaveProperty("status", "ok");
    });

    it("should block path traversal", async () => {
        // Attempts to go up two directories to reach the project root or src
        const req = new Request("http://localhost/public/../../package.json");
        const res = await router(req);
        // Should be 403 Forbidden or 404 Not Found (our logical change makes it 403)
        expect([403, 404]).toContain(res.status);
    });

    it("should serve existing static file", async () => {
        // We know style.css exists in src/web/public
        const req = new Request("http://localhost/public/style.css");
        const res = await router(req);
        expect(res.status).toBe(200);
        if (res.status === 200) {
            const text = await res.text();
            expect(text).toContain("body");
        }
    });

    it("should not serve static files on non-GET methods", async () => {
        const req = new Request("http://localhost/public/style.css", { method: "POST" });
        const res = await router(req);
        expect(res.status).toBe(404);
    });

    it("should return 404 for unknown routes", async () => {
        const req = new Request("http://localhost/unknown");
        const res = await router(req);
        expect(res.status).toBe(404);
    });
});
