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

test("Escape clears the color filter and preserves the unsaved theme", WEB, async ({ t3, app, screen, browser }) => {
  const project = await t3.project("theme-demo");
  await t3.thread({ project, title: "Theme preview", messages: ["Hello"] });
  await t3.open("/settings/appearance");
  await screen.getByRole("button", "Create theme", { exact: true }).tap();
  const name = screen.getByPlaceholder("e.g. Aurora");
  await name.fill("Aurora draft");
  const advanced = screen.getByRole("switch", "Use advanced theme colors");
  await advanced.tap();
  const filter = screen.getByRole("textbox", "Filter colors");
  await filter.fill("accent");
  await app.screenshot("populated-color-filter");
  await browser.keyboard.down("Escape");
  await app.screenshot("first-escape-preserves-draft");
  try {
    await expect(name).toHaveValue("Aurora draft");
    await expect(filter).toHaveValue("");
    await expect(advanced).toHaveAttribute("aria-checked", "true");
    await browser.keyboard.down("Escape");
    await browser.keyboard.down("Escape");
    await app.screenshot("held-escape-preserves-draft");
    await expect(name).toHaveValue("Aurora draft");
  } finally {
    await browser.keyboard.up("Escape");
  }
  await browser.keyboard.press("Escape");
  await app.screenshot("distinct-escape-closes-only-editor");
  await expect(filter).not.toBeVisible();
  await expect(screen.getByRole("button", "Create theme", { exact: true })).toBeVisible();
});
