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

test("Sidebar resize cursor stays consistent through a drag", WEB, async ({ t3, app, screen, browser }) => {
  await browser.setViewport({ width: 1600, height: 1000 });
  const project = await t3.project("Cursor demo");
  const thread = await t3.thread({ project, title: "Sidebar cursor", messages: ["Hello"] });
  await t3.open(thread);
  await screen.getByText("Syncing messages...", { exact: true }).waitFor({ state: "hidden" });
  await screen.getByRole("button", "Resize Sidebar").hover();
  const samples: Array<{ phase: string; rail: string; body: string; x: number }> = [];
  const sample = async (phase: string) => {
    const state = await browser.evaluate(() => {
      const rail = document.querySelector('[data-slot="sidebar-rail"]')!;
      const rect = rail.getBoundingClientRect();
      return { rail: getComputedStyle(rail).cursor, body: document.body.style.cursor || "(default)", x: Math.round(rect.x) };
    });
    samples.push({ phase, ...state });
    await browser.evaluate((rows) => {
      let panel = document.getElementById("cursor-observations");
      if (!panel) {
        panel = document.createElement("pre"); panel.id = "cursor-observations";
        panel.style.cssText = "position:fixed;right:24px;top:100px;z-index:99999;background:#111827;color:#f9fafb;padding:20px;border:2px solid #94a3b8;font:16px/1.7 monospace;pointer-events:none";
        document.body.append(panel);
      }
      panel.textContent = "LIVE COMPUTED CURSORS (CSS pixels)\n" + rows.map(r => `${r.phase}: rail=${r.rail} body=${r.body} x=${r.x}`).join("\n") + (panel.dataset.right ? `\nRight panel: ${panel.dataset.right}` : "");
    }, samples);
    return state;
  };
  await sample("Hover");
  await app.screenshot("sidebar-hover");
  const point = await browser.evaluate(() => {
    const r = document.querySelector('[data-slot="sidebar-rail"]')!.getBoundingClientRect();
    return { x: r.x + r.width / 2, y: r.y + r.height / 2 };
  });
  await browser.mouse.move(point.x, point.y);
  await browser.mouse.down();
  for (let dx = 4; dx <= 40; dx += 4) await browser.mouse.move(point.x + dx, point.y);
  await sample("Drag +40px");
  await app.screenshot("sidebar-drag");
  await browser.mouse.up();
  await sample("Release");
  await app.screenshot("sidebar-release");
  await screen.getByRole("button", "Toggle right panel", { exact: true }).tap();
  await screen.getByRole("separator").first().hover();
  const right = await browser.evaluate(() => Array.from(document.querySelectorAll('[role="separator"]')).filter(e => e.getBoundingClientRect().height > 100).map(e => getComputedStyle(e).cursor));
  await browser.evaluate((cursors) => { const panel = document.getElementById("cursor-observations")!; panel.dataset.right = cursors.join(", "); panel.textContent += `\nRight panel: ${cursors.join(", ")}`; }, right);
  await app.screenshot("right-panel-cursor");
  await browser.keyboard.press("Control+b");
  await sample("Collapsed toggle");
  await app.screenshot("sidebar-collapsed");
  await browser.keyboard.press("Control+b");
  await screen.getByRole("button", "Resize Sidebar").hover();
  await sample("Reopened");
  await app.screenshot("sidebar-reopened");
  expect({ expanded: samples.filter(s => s.phase !== "Collapsed toggle").map(s => s.rail), dragBody: samples[1].body, collapsed: samples[3].rail, right }).toEqual({ expanded: ["col-resize", "col-resize", "col-resize", "col-resize"], dragBody: "col-resize", collapsed: "e-resize", right: ["col-resize"] });
});
