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

test("Escape cancels a sidebar drag while keeping Pull Requests open", WEB, async ({ t3, app, browser }) => {
  const project = await t3.project("demo");
  const input = { project, title: "Release checklist", messages: ["Start"] };
  const thread = await t3.thread(input).catch(async (error) => {
    if (!String(error).includes("Advertised models: default.")) throw error;
    return t3.thread({ ...input, model: "default" });
  });
  await browser.setViewport({ width: 1440, height: 900 });
  await t3.open(thread);
  await t3.open("/pull-requests");
  const row = browser.locator(`[data-sidebar-row$=":${thread.threadId}"]`);
  await row.waitFor();
  const start = await browser.evaluate((id) => {
    const row = document.querySelector(`[data-sidebar-row$=":${id}"]`);
    if (!row) throw new Error("Release checklist sidebar row missing");
    const rect = row.getBoundingClientRect();
    return { x: rect.left + rect.width / 2, y: rect.top + rect.height / 2 };
  }, thread.threadId);
  await browser.mouse.move(start.x, start.y);
  await browser.mouse.down();
  try {
    await browser.mouse.move(start.x + 150, start.y);
    await browser.mouse.move(start.x + 450, start.y);
    await app.screenshot("active-sidebar-drag-over-pull-requests");
    await browser.keyboard.press("Escape");
    await browser.mouse.up();
    await app.screenshot("pull-requests-after-escape");
    expect(await browser.evaluate(() => location.pathname)).toBe("/pull-requests");
  } finally {
    await browser.mouse.up();
  }
});
