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

test("The local environment exposes and copies its machine-local MCP URL", WEB, async ({ t3, app, screen, browser }) => {
  await t3.project("demo");
  await t3.open("/settings/access");
  await screen.getByRole("button", "More actions for this machine").waitFor();
  await app.screenshot("local-environment-mcp-url");
  const guidance = screen.getByText(/^Give this URL to an agent running on this machine:/);
  await expect(guidance).toBeVisible();
  await screen.getByRole("button", "Copy", { exact: true }).tap();
  await expect(screen.getByText("MCP URL copied", { exact: true })).toBeVisible();
  await browser.evaluate(() => {
    const probe = document.createElement("textarea");
    probe.setAttribute("aria-label", "Clipboard verification");
    document.body.append(probe);
    probe.focus();
    return null;
  });
  await browser.keyboard.press("ControlOrMeta+V");
  const copied = await browser.evaluate(() => document.querySelector<HTMLTextAreaElement>('textarea[aria-label="Clipboard verification"]')?.value);
  const expected = await browser.evaluate(() => document.body.innerText.match(/Give this URL to an agent running on this machine: (http[^\s]+\/mcp)/)?.[1]);
  expect(copied).toBe(expected);
  await browser.evaluate(() => { document.querySelector('textarea[aria-label="Clipboard verification"]')?.remove(); return null; });
  await app.screenshot("mcp-url-copied");
});
