/// <reference lib="dom" />
import { symlink } from "node:fs/promises";
import { join } from "node:path";
import { expect } from "e2e";
import { test, WEB } from "./t3.ts";

test("Ignore drops trailing space after an escaped backslash", WEB, async ({ t3, app, browser, screen }) => {
  await browser.setViewport({ width: 1280, height: 900 });
  const project = await t3.project("ignore-patterns", {
    "target.txt": "Synthetic ignore pattern fixture\n",
    ".ignore": "dir\\\\ \nkeep\\ \n",
  });
  await symlink("target.txt", join(project, "dir\\"));
  await symlink("target.txt", join(project, "keep "));
  await symlink("target.txt", join(project, "other"));
  const thread = await t3.thread({ project, title: "Ignore pattern file suggestions", messages: ["hello"] });
  await t3.open(thread);
  await screen.getByRole("textbox").tap();
  await browser.keyboard.type("@");
  await screen.getByRole("option", "other", { exact: false }).waitFor();
  await app.screenshot("all-file-suggestions");
  await browser.keyboard.type("dir");
  await screen.getByRole("option", "other", { exact: false }).waitFor({ state: "hidden" });
  await app.screenshot("dir-file-suggestions");
  const names = await browser.evaluate(() => Array.from(document.querySelectorAll('[data-composer-command-drawer] [role="option"]'), row => row.textContent ?? ""));
  expect(names.some(name => name.includes("dir\\"))).toBe(false);
});
