import { expect } from "e2e";
import { writeFile } from "node:fs/promises";
import { execFileSync } from "node:child_process";
import { test, WEB } from "./t3.ts";

test("Workspace searches honor every Git ignore source", WEB, async ({ t3, app, screen, browser }) => {
  const project = await t3.project("ignore-sources", { ".gitignore": "gitignore-target.txt\n", "visible-target.txt": "workspace needle\n" });
  await writeFile(`${project}/.git/info/exclude`, "info-exclude-target.txt\n");
  await writeFile(`${project}/.git/global-ignore`, "global-exclude-target.txt\n");
  execFileSync("git", ["config", "core.excludesFile", `${project}/.git/global-ignore`], { cwd: project });
  for (const name of ["gitignore-target.txt", "info-exclude-target.txt", "global-exclude-target.txt"]) {
    await writeFile(`${project}/${name}`, "workspace needle\n");
  }
  execFileSync("git", ["check-ignore", "gitignore-target.txt", "info-exclude-target.txt", "global-exclude-target.txt"], { cwd: project });
  await t3.open();
  await screen.getByTestId("composer-editor").waitFor();
  await browser.keyboard.press("Control+p");
  await screen.getByText("visible-target.txt").first().waitFor();
  await app.screenshot("flat-file-list");
  for (const name of ["info-exclude-target.txt", "global-exclude-target.txt", "gitignore-target.txt"]) {
    await expect.soft(screen.getByText(name).first()).toBeHidden();
  }
  await screen.getByPlaceholder("Search files…").fill("target");
  await screen.getByText("visible-target.txt").first().waitFor();
  await app.screenshot("path-search");
  for (const name of ["info-exclude-target.txt", "global-exclude-target.txt", "gitignore-target.txt"]) {
    await expect.soft(screen.getByText(name).first()).toBeHidden();
  }
  await browser.keyboard.press("Escape");
  await browser.keyboard.press("Control+Shift+f");
  await screen.getByPlaceholder("Search in ignore-sources").fill("workspace needle");
  await screen.getByText("visible-target.txt").first().waitFor();
  await app.screenshot("content-search");
  for (const name of ["info-exclude-target.txt", "global-exclude-target.txt", "gitignore-target.txt"]) {
    await expect.soft(screen.getByText(name).first()).toBeHidden();
  }
});
