import { expect } from "e2e";
import { test, WEB } from "./t3.ts";

test("SCP remote tails do not link to an existing local file", WEB, async ({ t3, app, screen, browser }) => {
  const project = await t3.project("remote-links", { "acme/widgets.git": "synthetic local file\n" });
  await t3.thread({ project, title: "Terminal remote links", messages: ["Ready"] });
  await t3.open();
  await screen.getByRole("button", "Terminal remote links, Done, remote-links", { exact: true }).tap();
  await screen.getByRole("button", "Toggle terminal drawer", { exact: true }).tap();
  await browser.evaluate(() => new Promise<void>(resolve => setTimeout(resolve, 1500)));
  await browser.keyboard.type("export PS1='$ '; printf '\\033[2J\\033[H\\033[?25l'; printf '%s\\n' 'git clone git@github.com:acme/widgets.git' 'git clone ssh://git@github.com/acme/widgets.git' 'git clone git@github.com:missing/widgets.git'");
  await browser.keyboard.press("Enter");
  await browser.evaluate(() => new Promise<void>(resolve => setTimeout(resolve, 1500)));
  const geometry = await browser.evaluate(() => {
    const input = document.querySelector('textarea[aria-label="Terminal input"]')!;
    const canvas = input.parentElement!.querySelector('canvas')!;
    const bounds = canvas.getBoundingClientRect();
    const ctx = canvas.getContext('2d')!;
    const size = Number(ctx.font.match(/([\d.]+)px/)![1]);
    return { x: bounds.x + 4, y: bounds.y + 4, width: ctx.measureText('M').width, height: Math.round(size * 1.35) };
  });
  // Hover the equivalent URL and missing path first, then the real local-file collision.
  for (const [row, col] of [[1, 36], [2, 35], [0, 30]]) {
    await browser.mouse.move(geometry.x + (col + 0.5) * geometry.width, geometry.y + (row + 0.5) * geometry.height);
    await browser.evaluate(() => new Promise<void>(resolve => setTimeout(resolve, 1200)));
    await app.screenshot(["scp-existing", "ssh-url", "scp-missing"][row]);
  }
  const cursor = await browser.evaluate(() => {
    const input = document.querySelector('textarea[aria-label="Terminal input"]')!;
    return getComputedStyle(input.parentElement!.querySelector('canvas')!).cursor;
  });
  await expect(cursor).not.toBe("pointer");
});
