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

test("Settings stays open when a backdrop press is released inside", WEB, async ({ t3, app, browser, screen }) => {
  const project = await t3.project("demo");
  const thread = await t3.thread({ project, title: "Backdrop gesture", messages: ["hello"] });
  await t3.open(thread);
  await t3.open("/settings/general");
  await screen.getByRole("dialog", "Settings").waitFor({ state: "visible" });
  const points = await browser.evaluate(() => {
    const dialog = document.querySelector('[role="dialog"][aria-label="Settings"]')!;
    const input = Array.from(dialog.querySelectorAll("input")).find((el) => el.getBoundingClientRect().width > 0)!;
    const box = input.getBoundingClientRect();
    const modal = dialog.getBoundingClientRect();
    return { from: { x: modal.left / 2, y: innerHeight / 2 }, to: { x: box.left + box.width / 2, y: box.top + box.height / 2 } };
  });
  await browser.evaluate(async () => { await Promise.all(document.getAnimations().map((animation) => animation.finished.catch(() => {}))); return null; });
  await app.screenshot("settings-before-gesture");
  await browser.mouse.move(points.from.x, points.from.y);
  await browser.mouse.down();
  for (let step = 1; step <= 12; step += 1) {
    await browser.mouse.move(points.from.x + (points.to.x - points.from.x) * step / 12, points.from.y + (points.to.y - points.from.y) * step / 12);
  }
  await browser.mouse.up();
  await browser.evaluate(async () => { await Promise.all(document.getAnimations().map((animation) => animation.finished.catch(() => {}))); return null; });
  await app.screenshot("settings-after-backdrop-release-inside");
  await expect(screen.getByRole("dialog", "Settings")).toBeVisible();
});
