/// <reference lib="dom" />
import { expect } from "e2e";
import { test, WEB } from "./t3.ts";

test("Font family commits trim padding and whitespace defaults", WEB, async ({ t3, app, screen, browser }) => {
  const project = await t3.project("Typography proof");
  await t3.thread({ project, title: "Font settings", messages: ["Hello"] });
  await browser.emulateMedia({ colorScheme: "dark" });
  await t3.open("/settings/appearance");
  const field = screen.getByRole("textbox", "Interface font family", { exact: true });
  await field.waitFor();
  const center = () => browser.evaluate(() => { document.querySelector('[aria-label="Interface font family"]')?.scrollIntoView({ block: "center" }); });
  await center();
  const value = () => browser.evaluate(() => (document.querySelector('[aria-label="Interface font family"]') as HTMLInputElement).value);
  await field.fill("   ");
  await field.press("Enter");
  await field.press("Tab");
  await center();
  await app.screenshot("whitespace-only-committed");
  const whitespace = await value();
  const resetShown = await browser.evaluate(() => !!document.querySelector('[aria-label="Reset interface font to default"]'));
  await t3.open("/settings/general");
  await screen.getByRole("button", "Restore device defaults", { exact: true }).waitFor();
  const restoreDisabled = await screen.getByRole("button", "Restore device defaults", { exact: true }).getAttribute("disabled");
  await app.screenshot("restore-defaults-after-whitespace");
  await t3.open("/settings/appearance");
  await center();
  await field.fill("  monospace  ");
  await field.press("Enter");
  await field.press("Tab");
  await center();
  await app.screenshot("padded-family-enter");
  const padded = await value();
  await field.fill("monospace ");
  await browser.evaluate(() => new Promise(resolve => setTimeout(resolve, 800)));
  const draft = await value();
  await center();
  await app.screenshot("debounced-draft-before-blur");
  await field.press("Tab");
  await center();
  await app.screenshot("debounced-family-after-blur");
  const blurred = await value();
  expect({ whitespace, resetShown, restoreDisabled: restoreDisabled !== null, padded, draft, blurred }).toEqual({ whitespace: "", resetShown: false, restoreDisabled: true, padded: "monospace", draft: "monospace ", blurred: "monospace" });
});
