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

import { mockCoreApis } from "../../fixtures/api-mocks";

test.beforeEach(async ({ context, page }) => {
  await context.clearCookies();
  await mockCoreApis(page);
});

test("user can log out from the sidebar after email login", async ({
  page,
}) => {
  await page.goto("/auth");

  await page.getByRole("button", { name: /Other Sign In Options/i }).click();
  await expect(
    page.getByRole("dialog", { name: /Other sign-in options/i }),
  ).toBeVisible();

  await page.getByRole("button", { name: "Email Sign In" }).click();
  await expect(page.getByPlaceholder("Email")).toHaveValue(
    "chanwillian0@gmail.com",
  );
  await expect(page.getByPlaceholder("Password")).toHaveValue("12345678");

  await page.getByRole("button", { name: "Login" }).click();
  await expect(page).toHaveURL(/\/chat$/);
  await expect(page.getByRole("button", { name: "Menu" })).toBeVisible();

  await page.getByRole("button", { name: "Menu" }).click();
  await expect(page).toHaveURL(/\/sidebar$/);

  const logoutRequestPromise = page.waitForRequest("**/api/auth/logout");
  await page.getByRole("button", { name: /Log out/i }).click();

  const logoutRequest = await logoutRequestPromise;
  expect(logoutRequest.method()).toBe("POST");

  await expect(page).toHaveURL(/\/splash$/);
  await expect(page.getByRole("button", { name: "Skip" })).toBeVisible();
  await expect(
    page.getByRole("button", { name: "Login with Facebook" }),
  ).toBeVisible();
});
