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

test("Stashed previews omit a cluster cut before its combining mark", WEB, async ({ t3, app, browser, screen }) => {
  const project = await t3.project("demo");
  const thread = await t3.thread({ project, title: "Combining mark preview", messages: ["Start"] });
  await t3.open(thread);
  const composer = screen.getByRole("textbox");
  for (const suffix of ["कि यह फ़ाइल ठीक करें", "e\u0301 and more text here"]) {
    await composer.tap();
    await browser.evaluate((text) => {
      const editor = document.activeElement;
      if (!(editor instanceof HTMLElement) || !editor.isContentEditable) throw new Error("Composer not focused");
      const clipboardData = new DataTransfer();
      clipboardData.setData("text/plain", text);
      editor.dispatchEvent(new ClipboardEvent("paste", { clipboardData, bubbles: true, cancelable: true }));
      return null;
    }, "a".repeat(89) + suffix);
    await browser.keyboard.press("Control+s");
  }
  await screen.getByRole("button", "Stashed prompts: 2. Open stash.").tap();
  await screen.getByRole("list", "Stashed prompts").waitFor();
  await app.screenshot("stash-preview-default");
  // Reveal the complete 90-unit snippets without changing their text or accessible names.
  await browser.evaluate(() => {
    for (const row of document.querySelectorAll<HTMLElement>("[data-stash-restore]")) row.style.fontSize = "9px";
    return null;
  });
  await app.screenshot("stash-preview-complete-text");
  const rows = await browser.evaluate(() => Array.from(document.querySelectorAll("[data-stash-restore]"), (row) => ({text: row.textContent, label: row.getAttribute("aria-label")})));
  expect(rows).toEqual([0, 1].map(() => ({text: "a".repeat(89) + "…", label: "Restore stashed prompt: " + "a".repeat(89) + "…"})));
});
