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

test("Non-UTF-8 content search keeps highlights and whole-word regex matches", WEB, async ({t3, app, browser, screen}) => {
  const project = await t3.project("proj", {
    "src/latin1.ts": new Uint8Array([99,97,102,233,32,110,101,101,100,108,101,32,110,101,101,100,108,101,13,10]),
    "src/plain.ts": 'export const ok = "needle";\n',
  });
  const thread = await t3.thread({project, title:"Search demonstration", messages:["Start"]});
  await t3.open(thread);
  // Let the real background content index finish warming, as in the report.
  await new Promise(resolve => setTimeout(resolve, 65000));
  await browser.keyboard.press("Control+Shift+f");
  await screen.getByPlaceholder("Search in proj").fill("needle");
  await screen.getByText("2 results in 2 files", {exact:true}).waitFor({timeout:60000});
  await app.screenshot("literal-highlights");
  const literal = await browser.evaluate(() => Array.from(document.querySelectorAll('[data-testid="project-content-search"] section')).filter(s => s.textContent?.includes("latin1.ts")).flatMap(s => Array.from(s.querySelectorAll("mark"), m => m.textContent)).join(""));
  await screen.getByRole("button", "Match whole word", {exact:true}).tap();
  await screen.getByRole("button", "Use regular expression", {exact:true}).tap();
  await new Promise(resolve => setTimeout(resolve, 1500));
  await app.screenshot("whole-word-regex");
  const regex = await browser.evaluate(() => Array.from(document.querySelectorAll('[data-testid="project-content-search"] section')).filter(s => s.textContent?.includes("latin1.ts")).flatMap(s => Array.from(s.querySelectorAll("mark"), m => m.textContent)).join(""));
  await screen.getByRole("button", "Use regular expression", {exact:true}).tap();
  await screen.getByText("2 results in 2 files", {exact:true}).waitFor();
  await app.screenshot("whole-word-literal-control");
  expect({literal, regex}).toEqual({literal:"needleneedle", regex:"needleneedle"});
});
