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

import { mockCoreApis } from "@e2e/fixtures/api-mocks";
import {
  clearBrowserState,
  defaultCharacterChatUrl,
  enterChatFromSplash,
  submitEmailLogin,
  switchToEmailSignIn,
} from "@e2e/fixtures/test-helpers";

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

test("user can email login from other sign-in options and return to chat", async ({
  page,
}) => {
  await enterChatFromSplash(page);

  await page
    .getByRole("button", { name: "Sign up to unlock more features" })
    .click();
  await expect(page).toHaveURL(/\/auth(?:\?.*)?$/);

  await switchToEmailSignIn(page);
  const loginRequest = await submitEmailLogin(page, {
    expectedUrl: defaultCharacterChatUrl,
  });
  expect(["desktop", "android"]).toContain(loginRequest?.postDataJSON().platform);

  await expect(page.getByRole("textbox", { name: "Message" })).toBeVisible();
  await expect(page.getByRole("button", { name: "Save CozSweet" })).toBeVisible();
});
