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

test("Expanded provider list keeps Cancel and Next visible", WEB, async ({ t3, app, browser, screen }) => {
  const project = await t3.project("demo");
  await t3.thread({ project, title: "Provider setup", messages: ["Start"] });
  await browser.setViewport({ width: 1745, height: 800 });
  await t3.open("/settings/providers");
  await screen.getByRole("button", "Add provider", { exact: true }).first().tap();
  await screen.getByRole("radio", "OpenCode", { exact: true }).tap();
  await screen.getByRole("button", "Show more", { exact: true }).tap();
  await screen.getByText("Or choose from ACP Registry", { exact: true }).waitFor();
  await browser.evaluate(() => {
    for (const animation of document.getAnimations()) {
      if (Number.isFinite(animation.effect?.getComputedTiming().endTime)) animation.finish();
    }
    return null;
  });
  await app.screenshot("expanded-provider-list-1745x800");
  const visible = await browser.evaluate(() => {
    const popup = [...document.querySelectorAll('[data-slot="dialog-popup"]')].at(-1);
    if (!popup) return null;
    const dialog = popup.getBoundingClientRect();
    return [...popup.querySelectorAll("button")]
      .filter(button => ["Cancel", "Next"].includes(button.textContent?.trim() ?? ""))
      .map(button => {
        const box = button.getBoundingClientRect();
        const point = document.elementFromPoint(box.x + box.width / 2, box.y + box.height / 2);
        return { label: button.textContent?.trim(), visible: box.top >= dialog.top && box.bottom <= dialog.bottom && box.bottom <= innerHeight && !!point && button.contains(point) };
      });
  });
  expect(visible).toEqual([{ label: "Cancel", visible: true }, { label: "Next", visible: true }]);
});
