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

test("Sidebar footer stays inside its minimum width at 20px", WEB, async ({ t3, app, browser, screen }) => {
  const project = await t3.project("footer-demo");
  const thread = await t3.thread({ project, title: "Footer layout", messages: ["Hello"], model: "multi-turn" });
  await t3.open(thread);
  await t3.open("/settings/appearance");
  await screen.getByRole("combobox", "Interface font size", { exact: true }).tap();
  await screen.getByRole("option", "20 px", { exact: true }).tap();
  await browser.keyboard.press("Tab");
  await browser.keyboard.press("Escape");
  await screen.getByRole("button", "Resize Sidebar", { exact: true }).waitFor();
  const rail = 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 app.screenshot("before-resize");
  for (const x of [200, 160]) {
    const pos = 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(pos.x, pos.y);
    await browser.mouse.down();
    await browser.mouse.move(x, rail.y);
    await browser.mouse.up();
  }
  await browser.mouse.move(700, 400);
  const result = await browser.evaluate(() => {
    const footer = document.querySelector<HTMLElement>('[data-slot="sidebar-footer"]')!;
    const sidebar = footer.closest('[data-slot="sidebar"]')!;
    const bounds = sidebar.getBoundingClientRect();
    const buttons = [...footer.querySelectorAll('button')].filter(b => b.getBoundingClientRect().width > 0);
    sidebar.setAttribute('style', `${sidebar.getAttribute('style') ?? ''}; border-right: 2px solid #2684ff`);
    buttons.forEach(b => { b.style.outline = '1px solid #ff4545'; });
    return { font: getComputedStyle(document.documentElement).fontSize, width: bounds.width, edge: bounds.right, buttons: buttons.map(b => ({ label: b.getAttribute('aria-label'), right: b.getBoundingClientRect().right })), overflow: Math.max(...buttons.map(b => b.getBoundingClientRect().right - bounds.right)) };
  });
  console.log(JSON.stringify(result));
  await app.screenshot("footer-at-20px-minimum");
  expect(result.overflow).toBeLessThanOrEqual(0);
});
