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

test("Copy remains clickable beside the minimap with thread details open", WEB, async ({t3, app, browser, screen}) => {
  const project = await t3.project("Minimap demo");
  const thread = await t3.thread({project, title: "Copy beside minimap", model: "showcase", messages: ["turn 1 go", "turn 2 go", "turn 3 go", "turn 4 go", "turn 5 go"]});
  await browser.setViewport({width:1280,height:800});
  await t3.open(thread);
  await screen.getByRole("button", "Toggle thread details panel").waitFor();
  await browser.evaluate(async () => {
    const scroller = document.querySelector<HTMLElement>(".messages-timeline-scroll")!;
    const buttons = Array.from(document.querySelectorAll<HTMLElement>('[data-message-role="assistant"] button[aria-label="Copy message"]'));
    const button = buttons[buttons.length - 2] ?? buttons[0];
    if (!button) throw new Error("No assistant copy button rendered");
    button.dataset.proofCopy = "true";
    scroller.scrollTop += button.getBoundingClientRect().top - 388;
    await new Promise<void>(resolve => requestAnimationFrame(() => requestAnimationFrame(() => resolve())));
    (window as any).__copied = [];
    const original = navigator.clipboard.writeText.bind(navigator.clipboard);
    navigator.clipboard.writeText = async text => { (window as any).__copied.push(text); await original(text); };
    return null;
  });
  const point = await browser.evaluate(() => {
    const b = document.querySelector<HTMLElement>('[data-proof-copy]')!.getBoundingClientRect();
    return {x:b.x+b.width/2,y:b.y+b.height/2};
  });
  await browser.mouse.move(point.x+100,point.y-35);
  await app.screenshot("message-actions-visible");
  await browser.mouse.move(point.x,point.y);
  await app.screenshot("pointer-on-copy");
  await browser.mouse.down();
  await browser.mouse.up();
  await app.screenshot("after-copy-click");
  expect(await browser.evaluate(() => (window as any).__copied.length)).toBe(1);
});
