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

test("Automatic project icons preserve Indic conjuncts, including after rename", WEB, async ({t3, app, browser, screen}) => {
  const names = ["ক্ষমা", "ਪ੍ਰੀਤ", "ક્ષમા", "ಕ್ಷಮೆ", "क्षमा"];
  for (const name of names) await t3.project(name);
  const project = await t3.project("rename-me");
  await t3.thread({project, title: "Monogram check", messages: ["Start"], model: "default"});
  await browser.addInitScript(() => {
    const redact = () => {
      for (const el of document.querySelectorAll("*")) {
        if (el.childNodes.length === 1 && el.firstChild?.nodeType === Node.TEXT_NODE && el.textContent?.startsWith("/home/")) el.textContent = "[synthetic workspace]";
      }
    };
    new MutationObserver(redact).observe(document, {childList: true, subtree: true});
  });
  await t3.open("/settings/projects");
  await screen.getByRole("button", /^Project scope:/).tap();
  await app.screenshot("automatic-project-scope-icons");
  const actual = await browser.evaluate(() => [...document.querySelectorAll('[role="menuitemradio"]')].map(el => ({title: el.querySelector('.block.truncate')?.textContent, icon: el.querySelector('svg text')?.textContent})).filter(el => el.icon));
  await screen.getByRole("menuitemradio", "rename-me", {exact: true}).tap();
  await screen.getByRole("textbox", "Project name").fill("ক্ষমা");
  await browser.keyboard.press("Enter");
  await screen.getByRole("button", "Project scope: ক্ষমা", {exact: true}).waitFor();
  await app.screenshot("automatic-icon-after-rename");
  const renamed = await browser.evaluate(() => document.querySelector('[aria-label="Choose a project icon"]')?.parentElement?.querySelector('svg text')?.textContent);
  expect(actual.filter(el => names.includes(el.title ?? "")).map(el => el.icon).sort()).toEqual([...names].sort());
  expect(renamed).toBe("ক্ষমা");
});
