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

test("Project monograms reject three graphemes and save two", WEB, async ({ t3, app, screen, browser }) => {
  // Redact generated workspace paths from public captures; controls and validation are unchanged.
  await browser.addInitScript(() => {
    const redact = () => {
      if (!document.body) return;
      const nodes = document.createTreeWalker(document.body, NodeFilter.SHOW_TEXT);
      while (nodes.nextNode()) {
        const node = nodes.currentNode;
        if (node.textContent?.includes("/.t3/scenarios/workspaces/")) {
          node.textContent = node.textContent.replace(/\/[^\s]*\/\.t3\/scenarios\/workspaces\/[^/]+\//g, "/demo/");
        }
      }
    };
    new MutationObserver(redact).observe(document, { childList: true, subtree: true, characterData: true });
  });
  await t3.project("Monogram demo");
  await t3.open("/settings/projects");
  await screen.getByRole("button", "Project scope: All projects").tap();
  await screen.getByRole("menuitemradio", "Monogram demo", { exact: true }).tap();
  await screen.getByRole("button", "Choose a project icon", { exact: true }).tap();
  await screen.getByRole("button", "Monogram", { exact: true }).tap();
  await screen.getByRole("button", "Blue", { exact: true }).tap();
  const letters = screen.getByRole("textbox", "Letters");
  const save = screen.getByRole("button", "Save icon", { exact: true });
  await letters.fill("ABC");
  await app.screenshot("invalid-three-graphemes");
  await expect(save).toBeDisabled();
  await expect(letters).toHaveAttribute("aria-invalid", "true");
  await letters.fill("T3");
  await app.screenshot("valid-two-graphemes");
  await expect(save).toBeEnabled();
  await save.tap();
  await expect(screen.getByText("T3 · blue", { exact: true })).toBeVisible();
  await screen.getByRole("button", "Choose a project icon", { exact: true }).tap();
  await app.screenshot("saved-two-graphemes");
  await expect(letters).toHaveValue("T3");
});
