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

test("Enabling neighboring hidden models keeps their rows and scroll position", WEB, async ({ t3, app, browser, screen }) => {
  await t3.project("demo");
  await browser.setViewport({ width: 1000, height: 800 });
  await t3.open("/settings/providers?instanceId=claudeAgent");
  await screen.getByRole("button", "Disable all", { exact: true }).tap();
  await screen.getByRole("button", "Enable all", { exact: true }).waitFor({ state: "visible" });
  const slugs = await browser.evaluate(() => Array.from(document.querySelectorAll<HTMLElement>("[data-model-slug]"), row => row.dataset.modelSlug!));
  const selected = slugs.slice(-5, -2);
  await browser.evaluate((slug) => {
    const row = document.querySelector<HTMLElement>(`[data-model-slug="${slug}"]`)!;
    row.scrollIntoView({ block: "center" });
  }, selected[0]);
  const snapshot = () => browser.evaluate(() => {
    const rows = Array.from(document.querySelectorAll<HTMLElement>("[data-model-slug]"));
    const list = rows[0]!.parentElement!.parentElement!;
    return { order: rows.map(row => row.dataset.modelSlug), scrollTop: list.scrollTop };
  });
  await browser.evaluate(async () => { await Promise.all(document.getAnimations().map(animation => animation.finished.catch(() => {}))); });
  const before = await snapshot();
  await app.screenshot("before-enabling");
  for (const slug of selected) {
    await browser.locator(`[data-model-slug="${slug}"] [role="switch"]`).click();
    await browser.locator(`[data-model-slug="${slug}"] [role="switch"][aria-checked="true"]`).waitFor({ state: "attached" });
  }
  await browser.evaluate(async () => { await Promise.all(document.getAnimations().map(animation => animation.finished.catch(() => {}))); });
  await app.screenshot("after-enabling-neighbors");
  const after = await snapshot();
  expect(after.order).toEqual(before.order);
  expect(after.scrollTop).toBe(before.scrollTop);
});
