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

test("Automatic project monograms preserve complete Khmer consonant clusters", WEB, async ({ t3, app, browser, screen }) => {
  await browser.setViewport({ width: 1440, height: 1000 });
  await browser.emulateMedia({ colorScheme: "dark" });
  const names = ["ខ្មែរ", "ស្រុក", "ថ្មី", "server"];
  const threads = [];
  for (const name of names) {
    const project = await t3.project(name);
    threads.push(await t3.thread({ project, title: `Automatic icon: ${name}`, messages: ["Start"], model: "default" }));
  }
  const icons: string[][] = [];
  for (let i = 0; i < names.length; i++) {
    await t3.open(threads[i]!);
    await browser.locator('[data-slot="project-monogram"]').first().waitFor();
    await screen.getByText("Syncing messages...", { exact: true }).waitFor({ state: "hidden" });
    await app.screenshot(`breadcrumb-${i + 1}`);
    icons.push(await browser.evaluate(() => Array.from(document.querySelectorAll('[data-slot="project-monogram"]')).map(el => el.textContent ?? "")));
  }
  await screen.getByRole("combobox", "Projects", { exact: true }).tap();
  await screen.getByRole("option", /ខ្មែរ/).waitFor();
  await app.screenshot("projects-picker");
  const pickerIcons = await browser.evaluate(() => Array.from(document.querySelectorAll('[data-slot="project-monogram"]')).map(el => el.textContent ?? ""));
  expect(icons[0]).toContain("ខ្មែរ");
  expect(icons[1]).toContain("ស្រុក");
  expect(icons[2]).toContain("ថ្មី");
  expect(icons[3]).toContain("SR");
  for (const expected of ["ខ្មែរ", "ស្រុក", "ថ្មី", "SR"]) expect(pickerIcons).toContain(expected);
});
