import { expect } from "e2e";
import { test, WEB } from "./t3.ts";

test("Thread details remain visible after returning from Snooze", WEB, async ({ t3, app, browser }) => {
  const project = await t3.project("hover-demo");
  const thread = await t3.thread({ project, title: "Fix flaky websocket reconnect", messages: ["go"] });
  await t3.open(thread);
  const row = browser.locator('[role="button"]').filter({ hasText: "Fix flaky websocket reconnect" });
  await row.hover();
  const pause = (ms: number) => browser.evaluate((ms) => new Promise<void>(resolve => setTimeout(resolve, ms)), ms);
  await pause(1200);
  const points = await browser.evaluate(() => {
    const row = [...document.querySelectorAll('[role="button"]')].find(el => el.textContent?.includes("Fix flaky websocket reconnect"));
    const title = [...(row?.querySelectorAll('span') ?? [])].find(el => el.textContent === "Fix flaky websocket reconnect");
    const snooze = row?.querySelector('[aria-label="Snooze thread"]');
    if (!row || !title || !snooze) throw new Error("Thread title or Snooze not mounted");
    const a = title.getBoundingClientRect(); const b = snooze.getBoundingClientRect();
    return { title: { x: a.x + 50, y: a.y + a.height / 2 }, snooze: { x: b.x + b.width / 2, y: b.y + b.height / 2 } };
  });
  await browser.mouse.move(points.title.x, points.title.y);
  await pause(1200);
  await app.screenshot("initial-thread-details");
  for (let i = 1; i <= 8; i++) await browser.mouse.move(points.title.x + (points.snooze.x - points.title.x) * i / 8, points.title.y + (points.snooze.y - points.title.y) * i / 8);
  await pause(1200);
  await app.screenshot("snooze-tooltip");
  for (let i = 1; i <= 8; i++) await browser.mouse.move(points.snooze.x + (points.title.x - points.snooze.x) * i / 8, points.snooze.y + (points.title.y - points.snooze.y) * i / 8);
  await pause(3000);
  await app.screenshot("returned-thread-details");
  await expect(browser.locator('[data-slot="tooltip-viewport"]')).toContainText("Fix flaky websocket reconnect", { timeout: 1000 });
});
