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 email login from other sign-in options and return to chat", async ({
  page,
}) => {
  await page.goto("/splash");

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

  await page
    .getByRole("button", { name: "Sign up to unlock more features" })
    .click();
  await expect(page).toHaveURL(/\/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");

  const loginRequestPromise = page.waitForRequest("**/api/auth/login");
  await page.getByRole("button", { name: "Login" }).click();

  const loginRequest = await loginRequestPromise;
  expect(loginRequest.postDataJSON()).toMatchObject({
    email: "chanwillian0@gmail.com",
    password: "12345678",
    platform: "web",
    isTestAccount: true,
  });

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