import { afterAll, describe, expect, test, vi } from "vitest";
import { handleRequest } from "./index";

describe("server request routing", () => {
  const consoleInfo = vi.spyOn(console, "info").mockImplementation(() => undefined);

  afterAll(() => {
    consoleInfo.mockRestore();
  });

  test("routes dashboard tile batch requests", async () => {
    const response = await handleRequest(
      new Request("https://example.test/api/dashboard/tiles", {
        method: "POST",
        body: JSON.stringify({
          tiles: [
            {
              kind: "status",
              stripId: "footer-status",
              id: "auto-refresh",
            },
          ],
        }),
      }),
    );

    expect(response.status).toBe(200);
    await expect(response.json()).resolves.toMatchObject({
      state: "ready",
      tiles: [
        {
          state: "ready",
          tile: {
            kind: "status",
            stripId: "footer-status",
            id: "auto-refresh",
          },
        },
      ],
    });
  });

  test("rejects non-post dashboard tile batch requests", async () => {
    const response = await handleRequest(
      new Request("https://example.test/api/dashboard/tiles", {
        method: "GET",
      }),
    );

    expect(response.status).toBe(405);
    expect(response.headers.get("allow")).toBe("POST");
  });
});
