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

test("Project search warmup keeps complete Unicode characters", WEB, async ({ t3, app, screen, browser }) => {
  const files: Record<string, string> = { "long.txt": "needle" + "a".repeat(505) + "😀" };
  for (let i = 0; i < 30000; i++) files[`filler/file-${i}.txt`] = `filler ${i}\n`;
  const project = await t3.project("unicode-search", files);
  const thread = await t3.thread({ project, title: "Search preview", messages: ["Start"] });
  await t3.open(thread);
  await screen.getByRole("textbox", "Message Search preview").waitFor();
  await browser.keyboard.press("Control+Shift+f");
  await screen.getByRole("combobox", "Search in unicode-search").fill("needle");
  await browser.locator('[data-testid="project-content-search"] section mark').waitFor();
  const result = await browser.evaluate(() => {
    const mark = document.querySelector('[data-testid="project-content-search"] section mark');
    const preview = mark?.parentElement?.textContent ?? "";
    return { preview, units: preview.length, wellFormed: preview.isWellFormed(), highlight: mark?.textContent,
      ending: Array.from({ length: Math.min(3, preview.length) }, (_, i) => preview.charCodeAt(preview.length - 3 + i).toString(16)) };
  });
  console.log("Rendered preview:", JSON.stringify(result));
  await app.screenshot("warmup-search-preview");
  expect(result).toEqual({ preview: "needle" + "a".repeat(505), units: 511, wellFormed: true, highlight: "needle", ending: ["61", "61", "61"] });
});
