import { describe, expect, it, afterEach } from "vitest";
describe("oauth-server", () => {
    let server = null;
    afterEach(() => {
        if (server) {
            server.close();
            server = null;
        }
    });
    async function importModule() {
        return await import("../oauth-server.js");
    }
    it("should start server on random port", async () => {
        const { startOAuthServer } = await importModule();
        server = await startOAuthServer();
        expect(server.port).toBeGreaterThan(0);
        expect(server.callbackUrl).toBe(`http://127.0.0.1:${server.port}/auth/callback`);
    });
    it("should extract code from callback URL", async () => {
        const { startOAuthServer } = await importModule();
        server = await startOAuthServer();
        const callbackPromise = server.waitForCallback();
        // Simulate OAuth redirect with code
        const response = await fetch(`http://127.0.0.1:${server.port}/auth/callback?code=test_auth_code_123`);
        expect(response.ok).toBe(true);
        const result = await callbackPromise;
        expect(result.code).toBe("test_auth_code_123");
        server = null; // Server closes itself after callback
    });
    it("should return success page", async () => {
        const { startOAuthServer } = await importModule();
        server = await startOAuthServer();
        const callbackPromise = server.waitForCallback();
        const response = await fetch(`http://127.0.0.1:${server.port}/auth/callback?code=test_code`);
        expect(response.ok).toBe(true);
        const html = await response.text();
        expect(html).toContain("Authentication successful!");
        expect(html).toContain("You can close this window.");
        await callbackPromise;
        server = null; // Server closes itself
    });
    it("should close server after callback", async () => {
        const { startOAuthServer } = await importModule();
        server = await startOAuthServer();
        const callbackPromise = server.waitForCallback();
        await fetch(`http://127.0.0.1:${server.port}/auth/callback?code=test_code`);
        await callbackPromise;
        // Server should be closed - next request should fail
        await expect(fetch(`http://127.0.0.1:${server.port}/auth/callback?code=another`)).rejects.toThrow();
        server = null; // Already closed
    });
    it("should return error page when error parameter is present", async () => {
        const { startOAuthServer } = await importModule();
        server = await startOAuthServer();
        const callbackPromise = server.waitForCallback();
        // Prevent unhandled rejection warning — rejection is asserted below
        callbackPromise.catch(() => { });
        const response = await fetch(`http://127.0.0.1:${server.port}/auth/callback?error=access_denied`);
        expect(response.status).toBe(400);
        const html = await response.text();
        expect(html).toContain("Login Failed");
        expect(html).toContain("You can close this window.");
        await expect(callbackPromise).rejects.toThrow("access_denied");
        server = null; // Server rejection handled
    });
    it("should reject with server_error when OAuth fails", async () => {
        const { startOAuthServer } = await importModule();
        server = await startOAuthServer();
        const callbackPromise = server.waitForCallback();
        // Prevent unhandled rejection warning — rejection is asserted below
        callbackPromise.catch(() => { });
        await fetch(`http://127.0.0.1:${server.port}/auth/callback?error=server_error`);
        await expect(callbackPromise).rejects.toThrow("server_error");
        server = null;
    });
    it("should return HTML for implicit flow when code parameter is missing", async () => {
        // When no code is provided, the server serves HTML that extracts tokens from URL fragment (implicit flow)
        const { startOAuthServer } = await importModule();
        server = await startOAuthServer();
        const response = await fetch(`http://127.0.0.1:${server.port}/auth/callback`);
        expect(response.status).toBe(200);
        const text = await response.text();
        expect(text).toContain("Completing login");
        expect(text).toContain("access_token");
    });
    it("should return 404 for non-callback paths", async () => {
        const { startOAuthServer } = await importModule();
        server = await startOAuthServer();
        const response = await fetch(`http://127.0.0.1:${server.port}/other`);
        expect(response.status).toBe(404);
    });
    it("should timeout after 5 minutes", async () => {
        // We can't actually wait 5 minutes, but we can verify the timeout
        // mechanism works by testing the close/cleanup behavior
        const { startOAuthServer } = await importModule();
        server = await startOAuthServer();
        // Manually close simulates what happens on timeout
        server.close();
        // The server should no longer accept connections
        await expect(fetch(`http://127.0.0.1:${server.port}/auth/callback?code=test`)).rejects.toThrow();
        server = null; // Already closed
    });
});
//# sourceMappingURL=oauth-server.test.js.map