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

test("Tracking a repository returns keyboard focus to Add folder", WEB, async ({ t3, app, browser, screen }) => {
  const project = await t3.project("Focus demo");
  const extra = await t3.project("extra");
  await t3.thread({ project, title: "Focus check", messages: ["Start"], model: "default" });
  // Hide machine-local paths in public evidence without changing controls or focus.
  await browser.addInitScript(() => {
    const redact = () => {
      for (const element of document.querySelectorAll("p, code, span, input")) {
        if (element instanceof HTMLInputElement && element.id) {
          if (element.value.startsWith("/")) element.style.color = "transparent";
        } else if (element.children.length === 0 && element.textContent?.includes("/home/")) {
          (element as HTMLElement).style.visibility = "hidden";
        }
      }
    };
    new MutationObserver(redact).observe(document, { childList: true, subtree: true });
    document.addEventListener("input", redact);
  });
  await t3.open("/settings/projects");
  await screen.getByRole("button", /^Project scope:/).click();
  await screen.getByRole("menuitemradio", "Focus demo").click();
  const add = screen.getByRole("button", "Add folder", { exact: true });
  await add.waitFor();
  await add.focus();
  await browser.keyboard.press("Enter");
  const dialog = screen.getByRole("dialog", "Add folder");
  await dialog.waitFor();
  await dialog.getByRole("textbox", "Folder path").fill(extra);
  await browser.keyboard.press("Enter");
  await dialog.waitFor({ state: "hidden" });
  await screen.getByRole("button", "Remove extra from the project").waitFor();
  const returnedFocus = await browser.evaluate(() => {
    const active = document.activeElement;
    return active instanceof HTMLButtonElement && active.textContent?.trim() === "Add folder";
  });
  const showFocus = async () => browser.evaluate(() => {
    let badge = document.getElementById("focus-evidence");
    if (!badge) {
      badge = document.createElement("div");
      badge.id = "focus-evidence";
      badge.style.cssText = "position:fixed;bottom:10px;left:300px;z-index:2147483647;background:#111827;color:white;padding:10px 16px;border-radius:6px;font:14px monospace;pointer-events:none";
      document.body.append(badge);
    }
    const active = document.activeElement;
    badge.textContent = "Observed keyboard focus: " + (active?.tagName === "BODY" ? "BODY" : active?.getAttribute("aria-label") ?? active?.textContent?.trim() ?? "none");
  });
  await showFocus();
  await app.screenshot("after-successful-add");
  await browser.keyboard.press("Tab");
  await showFocus();
  await app.screenshot("after-tab");
  const tabFocus = await browser.evaluate(() => document.activeElement?.getAttribute("aria-label") === "Remove extra from the project");
  await add.focus();
  await browser.keyboard.press("Enter");
  await dialog.waitFor();
  await browser.keyboard.press("Escape");
  await dialog.waitFor({ state: "hidden" });
  await showFocus();
  await app.screenshot("escape-cancel-control");
  await screen.getByRole("button", "Remove extra from the project").click();
  await screen.getByRole("button", "Remove extra from the project").waitFor({ state: "hidden" });
  await browser.locator("button:not(:disabled)").filter({ hasText: "Add folder" }).waitFor();
  await add.focus();
  await browser.keyboard.press("Enter");
  await dialog.waitFor();
  await dialog.getByRole("textbox", "Folder path").fill(extra);
  await browser.keyboard.press("Enter");
  await dialog.waitFor({ state: "hidden" });
  await screen.getByRole("button", "Remove extra from the project").waitFor();
  await showFocus();
  await app.screenshot("second-successful-add");
  expect(returnedFocus).toBe(true);
  expect(tabFocus).toBe(true);
  await expect(add).toBeFocused();
});
