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

test("Terminal tabs activate from right padding and top strip", WEB, async ({ t3, app, screen, browser }) => {
  await browser.emulateMedia({ colorScheme: "dark" });
  const project = await t3.project("tab-demo");
  const thread = await t3.thread({ project, title: "Tab activation", messages: ["Hello"] });
  await t3.open(thread);
  await screen.getByRole("button", "Toggle terminal drawer").tap();
  await screen.getByRole("button", /^New Terminal/).tap();
  await screen.getByRole("button", "Terminal 2", { exact: true }).waitFor();
  const results: boolean[] = [];
  for (const edge of ["right padding", "top strip"]) {
    await screen.getByRole("button", "Terminal 2", { exact: true }).tap();
    const point = await browser.evaluate((edge) => {
      const button = [...document.querySelectorAll("button")].find(b => b.textContent?.trim() === "Terminal 1");
      if (!button) throw new Error("Terminal 1 missing");
      const rect = button.parentElement!.getBoundingClientRect();
      return { x: edge === "right padding" ? rect.right - 2 : rect.x + rect.width * 0.6, y: edge === "top strip" ? rect.top + 1 : rect.top + rect.height / 2 };
    }, edge);
    await browser.mouse.move(point.x, point.y);
    await browser.mouse.down();
    await browser.mouse.up();
    results.push(await browser.evaluate(() => {
      const button = [...document.querySelectorAll("button")].find(b => b.textContent?.trim() === "Terminal 1");
      return button?.parentElement?.classList.contains("bg-accent") ?? false;
    }));
    await app.screenshot(edge.replace(" ", "-"));
  }
  expect(results).toEqual([true, true]);
});
