import { afterEach, beforeEach, describe, expect, it } from "vitest";
import { createControlPlane } from "../server.js";
describe("Control Plane Server", () => {
    let server;
    beforeEach(() => {
        // Use port 0 for random available port in tests
        server = createControlPlane({ host: "127.0.0.1", port: 0 });
    });
    afterEach(async () => {
        await server.stop();
    });
    it("should start server on port 17333", async () => {
        // Use a specific port to verify the config path works
        await server.stop(); // stop the port-0 server
        const specificServer = createControlPlane({
            host: "127.0.0.1",
            port: 17333,
        });
        try {
            const address = await specificServer.start();
            expect(address).toContain("127.0.0.1");
            expect(address).toContain("17333");
        }
        finally {
            await specificServer.stop();
        }
    });
    it("should respond to /healthz with ok status", async () => {
        const address = await server.start();
        const response = await fetch(`${address}/healthz`);
        const body = (await response.json());
        expect(response.status).toBe(200);
        expect(body.status).toBe("ok");
        expect(body.opencode).toBeDefined();
        expect(body.opencode.healthy).toBe(false);
        expect(body.opencode.lastHealthCheck).toBeNull();
    });
    it("should bind only to localhost", async () => {
        const address = await server.start();
        expect(address).toContain("127.0.0.1");
    });
    it("should shutdown gracefully", async () => {
        const address = await server.start();
        // Verify server is running
        const response = await fetch(`${address}/healthz`);
        expect(response.status).toBe(200);
        // Stop the server
        await server.stop();
        expect(server.address).toBeNull();
        // Verify server is no longer reachable
        await expect(fetch(`${address}/healthz`)).rejects.toThrow();
    });
    it("should return address after start", async () => {
        expect(server.address).toBeNull();
        const address = await server.start();
        expect(server.address).toBe(address);
    });
    it("should return 404 for unknown routes", async () => {
        const address = await server.start();
        const response = await fetch(`${address}/nonexistent`);
        expect(response.status).toBe(404);
    });
});
//# sourceMappingURL=server.test.js.map