import { test, expect } from "@playwright/test";

test("邀请页可展示房间信息并完成加入", async ({ page }) => {
  const joinBodies: Array<{ userId?: string }> = [];

  await page.route("**/api/room/ROOM01", async (route) => {
    if (route.request().method() !== "GET") {
      await route.fallback();
      return;
    }

    await route.fulfill({
      status: 200,
      contentType: "application/json",
      body: JSON.stringify({ userCount: 2, scene: "eat" }),
    });
  });

  await page.route("**/api/room/ROOM01/join", async (route) => {
    const body = route.request().postDataJSON() as { userId?: string } | null;
    joinBodies.push(body ?? {});
    await route.fulfill({ status: 200, contentType: "application/json", body: "{}" });
  });

  await page.goto("/invite/ROOM01");

  await expect(page.getByText("ROOM01")).toBeVisible();
  await expect(page.getByText("已有 2 人在房间")).toBeVisible();

  await page.getByRole("button", { name: "加入房间" }).click();
  await expect(page).toHaveURL(/\/room\/ROOM01$/);

  expect(joinBodies.length).toBeGreaterThan(0);
  expect(joinBodies[0]?.userId).toBeTruthy();
});
