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

test("pdb function stack frames are active terminal links", WEB, async ({ t3, app, browser, screen }) => {
  const project = await t3.project("pyproj", { "ok.py": "def f():\n    x = 1\n    return x\n\n\nprint(f())\n" });
  const thread = await t3.thread({ project, title: "Python debugger", messages: ["hello"] });
  await t3.open(thread);
  await screen.getByRole("button", "Toggle terminal drawer").tap();
  const input = screen.getByLabel("Terminal input");
  await expect(input).toHaveAttribute("data-workspace-focus-default", "");
  await input.focus();
  await browser.keyboard.type("mkdir -p /tmp/factory-pr-44703-evidence-1/pyproj; cp ok.py /tmp/factory-pr-44703-evidence-1/pyproj/ok.py; python3 -m pdb -c 'b f' -c c /tmp/factory-pr-44703-evidence-1/pyproj/ok.py\n");
  // Wait for the real debugger by selecting the terminal's rendered text.
  const text = async () => {
    await browser.evaluate(() => {
      document.addEventListener("copy", event => { document.body.dataset.copied = event.clipboardData?.getData("text/plain") ?? ""; }, {once:true});
    });
    await input.focus();
    await browser.keyboard.press("Control+Shift+A");
    await browser.keyboard.press("Control+Shift+C");
    return browser.evaluate(() => document.body.dataset.copied ?? "");
  };
  await expect.poll(text, {timeout:30000}).toContain("-> x = 1");
  await browser.keyboard.press("Escape");
  await browser.keyboard.type("!print('\\033[2J\\033[H', end='')\nwhere\n");
  await expect.poll(text).toContain("<module>()");
  await browser.keyboard.press("Escape");
  const box = await browser.evaluate(() => {
    const canvas = document.querySelector('[aria-label="Terminal input"]')?.parentElement?.querySelector("canvas");
    const b = canvas!.getBoundingClientRect();
    return {x:b.x,y:b.y,width:b.width,height:b.height, cell:canvas!.getContext("2d")!.measureText("M").width};
  });
  // Clear the copy selection, then hover the function frame in the real pdb stack.
  await browser.mouse.move(box.x + box.width - 30, box.y + box.height - 20);
  await browser.mouse.down();
  await browser.mouse.up();
  const settle = () => browser.evaluate(() => new Promise(resolve => setTimeout(resolve, 1000)));
  const cursor = () => browser.evaluate(() => document.querySelector('[aria-label="Terminal input"]')?.parentElement?.querySelector("canvas")?.style.cursor);
  await settle();
  await browser.mouse.move(box.x + 30, box.y + 85);
  await settle();
  await app.screenshot("pdb-module-control-hover");
  await browser.mouse.move(box.x + 30, box.y + 121);
  await settle();
  const pathCursor = await cursor();
  await app.screenshot("pdb-function-frame-hover");
  await browser.mouse.move(box.x + 4 + 378, box.y + 121);
  await settle();
  const lineCursor = await cursor();
  await app.screenshot("pdb-line-number-hover");
  await expect({ pathCursor, lineCursor }).toEqual({ pathCursor: "pointer", lineCursor: "pointer" });
});
