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

test("File globs include matching symlinks and respect exclusions", WEB, async ({ t3, app, screen, browser }) => {
  const project = await t3.project("glob-search-demo", { "src/index.ts": "export const value = 1;\n" });
  await symlink("src/index.ts", `${project}/index-link.ts`);
  await symlink("src/index.ts", `${project}/index-link.test.ts`);
  const thread = await t3.thread({ project, title: "Symlink file search", messages: ["Start"] });
  await t3.open(thread);
  await screen.getByText("first fixture turn complete", { exact: true }).waitFor();
  await browser.keyboard.press("Control+p");
  const search = screen.getByPlaceholder("Search files…");
  await search.waitFor();
  await screen.getByRole("option", "index-link.ts", { exact: false }).waitFor();
  await app.screenshot("unfiltered-symlinks");
  const failures: unknown[] = [];
  for (const [label, query] of [["star-ts", "*.ts"], ["recursive-ts", "**/*.ts"], ["exclude-test-ts", "*.ts !*.test.ts"]]) {
    await search.fill(query);
    await screen.getByRole("option", "README.md", { exact: false }).waitFor({ state: "hidden" });
    await screen.getByRole("option", "index.ts", { exact: false }).waitFor();
    try {
      await expect(screen.getByRole("option", "index-link.ts", { exact: false })).toBeVisible();
      if (label === "exclude-test-ts") {
        await expect(screen.getByRole("option", "index-link.test.ts", { exact: false })).not.toBeVisible();
      } else {
        await expect(screen.getByRole("option", "index-link.test.ts", { exact: false })).toBeVisible();
      }
    } catch (error) { failures.push(error); }
    await app.screenshot(label);
  }
  if (failures.length) throw failures[0];
});
