/// <reference lib="dom" />
import { writeFile, readFile } from "node:fs/promises";
import { T3_HOME } from "../e2e.config.ts";
import { expect } from "e2e";
import { test, WEB } from "./t3.ts";

test("Remote Files menu keeps chat actions and hides server shell actions", WEB, async ({ t3, app, screen, browser }) => {
  const project = await t3.project("file-menu-demo", { "example.txt": "A synthetic file for the remote menu proof.\n" });
  const thread = await t3.thread({ project, title: "File menu proof", messages: ["Ready"] });
  await writeFile(`${project}/example.txt`, "Updated synthetic contents.\n");
  await t3.open(thread);
  await screen.getByRole("button", "Toggle right panel").tap();
  await screen.getByRole("button", "Files F").tap();
  await screen.getByRole("treeitem", "example.txt").secondaryTap();
  await app.screenshot("localhost-control");
  await expect(screen.getByRole("menuitem", "Open with", { exact: true })).toBeVisible();
  await browser.keyboard.press("Escape");
  const localThreadUrl = await browser.url();
  const remote = new URL(app.baseUrl!);
  remote.hostname = process.env.T3_PROOF_HOST!;
  await app.open(`${remote.origin}${await readFile(`${T3_HOME}/pairing-path`, "utf8")}`);
  await expect(browser).not.toHaveURL(/\/pair(?:[?#]|$)/, { timeout: 60000 });
  await app.open(`${remote.origin}${new URL(localThreadUrl).pathname}`);
  await screen.getByRole("button", "Toggle right panel").tap();
  await screen.getByRole("button", "Files F").tap();
  await screen.getByRole("treeitem", "example.txt").secondaryTap();
  await app.screenshot("remote-file-menu");
  const remoteFileShellActions = await screen.getByRole("menuitem", "Open with", { exact: true }).count();
  await browser.keyboard.press("Escape");
  await screen.getByRole("button", "Add panel surface").tap();
  await screen.getByRole("menuitem", "Diff", { exact: false }).tap();
  await screen.getByRole("button", "Expand example.txt").tap();
  await expect.poll(() => browser.evaluate(() => {
    for (const host of document.querySelectorAll("*")) {
      const title = host.shadowRoot?.querySelector<HTMLElement>("[data-title]");
      if (!title || title.textContent?.trim() !== "example.txt") continue;
      const box = title.getBoundingClientRect();
      title.dispatchEvent(new MouseEvent("contextmenu", { bubbles: true, composed: true, clientX: box.x + 10, clientY: box.y + 10 }));
      return true;
    }
    return false;
  })).toBe(true);
  await app.screenshot("remote-diff-menu");
  await expect(screen.getByRole("menuitem", "Open with", { exact: true })).not.toBeVisible();
  expect(remoteFileShellActions).toBe(0);
  await browser.keyboard.press("Escape");
  await screen.getByRole("button", "Files", { exact: true }).tap();
  await screen.getByRole("treeitem", "example.txt").secondaryTap();
  await expect(screen.getByRole("menuitem", "Copy mention")).toBeVisible();
  await expect(screen.getByRole("menuitem", "Add to chat")).toBeVisible();
  await expect(screen.getByRole("menuitem", "Open", { exact: true })).not.toBeVisible();
  await expect(screen.getByRole("menuitem", "Reveal in Files", { exact: true })).not.toBeVisible();
  await expect(screen.getByRole("menuitem", "Open with", { exact: true })).not.toBeVisible();
});
