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

test("Project content search preserves newline filenames during warmup", WEB, async ({ t3, app, screen, browser }) => {
  const files: Record<string, string> = {
    "src/one\ntwo.txt": "unique_lf_needle\n",
    "src/readme.txt": "A synthetic search fixture.\n",
  };
  for (let directory = 0; directory < 300; directory++) {
    for (let file = 0; file < 200; file++) {
      files[`filler/d${directory}/f${file}.txt`] = "filler text for content-index warmup\n";
    }
  }
  const observed = [];
  for (const mode of ["literal", "regex"]) {
    const project = await t3.project(`newline-${mode}`, files);
    const thread = await t3.thread({ project, title: "Filename search proof", messages: [] });
    await t3.open(thread);
    await screen.getByRole("textbox", "Message Filename search proof", { exact: true }).waitFor();
    await browser.keyboard.press("Control+Shift+f");
    if (mode === "regex") await screen.getByRole("button", "Use regular expression", { exact: true }).tap();
    await screen.getByPlaceholder(`Search in newline-${mode}`).fill(mode === "literal" ? "unique_lf_needle" : "unique_lf_n.+");
    await screen.getByRole("button", "1 unique_lf_needle", { exact: true }).waitFor();
    observed.push(await browser.evaluate(`(() => {
      const section = document.querySelector('[data-testid="project-content-search"] section');
      return { name: section?.querySelector('.font-medium')?.textContent,
        text: section?.textContent,
        highlight: Array.from(section?.querySelectorAll('mark') ?? []).map(x => x.textContent).join('') };
    })()`));
    await app.screenshot(`${mode}-newline-filename`);
    await browser.keyboard.press("Escape");
  }
  expect(observed).toEqual([
    { name: "one\ntwo.txt", text: "one\ntwo.txtsrc11unique_lf_needle", highlight: "unique_lf_needle" },
    { name: "one\ntwo.txt", text: "one\ntwo.txtsrc11unique_lf_needle", highlight: "unique_lf_needle" },
  ]);
});
