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

test("Escape cancels the voice alias before closing Settings", WEB, async ({ t3, app, screen, browser }) => {
  await browser.emulateMedia({ colorScheme: "dark" });
  const project = await t3.project("voice-demo");
  const thread = await t3.thread({ project, title: "Voice dictionary demo", messages: ["Start"] });
  await t3.open(thread);
  await t3.open("/settings/voice");
  await screen.getByRole("textbox", "Add a word or phrase to the dictionary", { exact: true }).fill("Kubernetes");
  await browser.keyboard.press("Enter");
  await screen.getByRole("button", "Add an alias for Kubernetes", { exact: true }).tap();
  await screen.getByRole("textbox", "Alias for Kubernetes", { exact: true }).fill("cooper");
  await app.screenshot("alias-draft-before-escape");
  await browser.keyboard.press("Escape");
  await browser.evaluate(async () => {
    await Promise.all(document.getAnimations().filter((animation) => Number.isFinite(animation.effect?.getComputedTiming().endTime)).map((animation) => animation.finished.catch(() => {})));
    return null;
  });
  await app.screenshot("after-first-escape");
  await expect(screen.getByRole("button", "Add an alias for Kubernetes", { exact: true })).toBeVisible();
  await expect(screen.getByRole("textbox", "Alias for Kubernetes", { exact: true })).not.toBeVisible();
  await expect(screen.getByText("cooper", { exact: true })).not.toBeVisible();
  await browser.keyboard.press("Escape");
  await expect(screen.getByRole("dialog")).not.toBeVisible();
  await app.screenshot("after-second-escape");
});
