import { expect } from "e2e";
import { test, WEB } from "./t3.ts";

test("Escape closes Clerk sign-in while Settings stays open", WEB, async ({ t3, app, screen, browser }) => {
  await t3.project("Escape demo");
  await t3.open("/settings/general");
  await screen.getByRole("button", "Sign in to T3 Connect").tap();
  await browser.locator('input[name="identifier"]').tap();
  await app.screenshot("clerk-open");
  await browser.keyboard.press("Escape");
  await browser.locator(".cl-modalContent").waitFor({ state: "hidden" });
  await browser.evaluate(async () => {
    await Promise.all(document.getAnimations().map((animation) => animation.finished.catch(() => {})));
  });
  await app.screenshot("after-clerk-escape");
  await expect(screen.getByRole("dialog", "Settings", { exact: true })).toBeVisible();
  await screen.getByRole("dialog", "Settings", { exact: true }).tap();
  await browser.keyboard.press("Escape");
  await expect(screen.getByRole("dialog", "Settings", { exact: true })).toBeHidden();
  await browser.evaluate(async () => {
    await Promise.all(document.getAnimations().map((animation) => animation.finished.catch(() => {})));
  });
  await app.screenshot("after-settings-escape");
});
