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

test("Escape clears ignored file search without discarding clone setup", WEB, async ({ t3, app, browser, screen }) => {
  const files: Record<string, string> = {
    ".git/config": '[remote "origin"]\nurl = https://github.com/example-org/demo-repo.git\n',
    ".gitignore": "*.txt\n.env\n",
    ".env": "DEMO_SETTING=synthetic\n",
    "build/KEEP.md": "Tracked so Git lists individual ignored files.\n",
  };
  for (let i = 1; i <= 250; i++) files[`build/build-${i}.txt`] = `Synthetic build ${i}\n`;
  const project = await t3.project("demo-repo", files);
  const thread = await t3.thread({ project, title: "Clone setup", messages: ["Ready"] });
  const second = startSecondServer(app.baseUrl ?? "");
  try {
    const { host, token } = await second.ready;
    await browser.setViewport({ width: 1440, height: 1000 });
    await t3.open("/settings/connections");
    await screen.getByRole("button", "Add environment").tap();
    const pairing = screen.getByRole("dialog");
    await pairing.getByPlaceholder("backend.example.com").fill(host);
    await pairing.getByPlaceholder("PAIRCODE").fill(token);
    await pairing.getByRole("button", "Add environment").tap();
    await screen.getByRole("button", "List", { exact: true }).tap();
    await screen.getByRole("button", "More actions for Scenario").waitFor();
    await t3.open(thread);
    await app.open("/");
    const picker = browser.locator("[data-draft-project-trigger]");
    await picker.tap();
    await screen.getByRole("option", "demo-repo", { exact: true }).tap();
    await screen.getByRole("combobox", "Run on", { exact: true }).tap();
    await screen.getByRole("option", "Add to another machine", { exact: true }).tap();
    // Hide only the synthetic workspace's host path in public captures.
    await browser.evaluate(() => {
      const style = document.createElement("style");
      style.textContent = '[role="dialog"] span[title^="/home/"] { visibility: hidden; }';
      document.head.append(style);
      return null;
    });
    const dialog = screen.getByRole("dialog", "Add to another machine");
    await dialog.getByText("Showing the first 200 files. Search to find the rest.", { exact: true }).waitFor();
    await dialog.getByRole("button", "SSH", { exact: true }).tap();
    await dialog.getByText("build/build-1.txt", { exact: true }).tap();
    const search = dialog.getByRole("searchbox", "Search ignored files");
    await search.fill("build-21");
    await dialog.getByText("build/build-210.txt", { exact: true }).waitFor();
    await dialog.getByText("build/build-1.txt", { exact: true }).waitFor({ state: "hidden" });
    await app.screenshot("before-escape-search-and-clone-setup");
    await browser.keyboard.press("Escape");
    try {
      await expect(dialog).toBeVisible();
      await dialog.getByText("build/build-1.txt", { exact: true }).waitFor();
    } finally {
      await app.screenshot("after-escape-search-and-clone-setup");
    }
    await expect(search).toHaveValue("");
    expect(await browser.evaluate(() => document.activeElement?.getAttribute("aria-label"))).toBe("Search ignored files");
    await expect(dialog.getByText("git@github.com:example-org/demo-repo.git", { exact: true })).toBeVisible();
    await expect(dialog.getByText("+ 2 files", { exact: true })).toBeVisible();
    await expect(dialog.getByRole("checkbox", "build/build-1.txt 18 B", { exact: true })).toBeChecked();
    await browser.keyboard.press("Escape");
    await expect(dialog).toBeHidden();
    await app.screenshot("subsequent-escape-closes-dialog");
  } finally { await second.stop(); }
});
