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

test("Citation jump refreshes Scroll to end", WEB, async ({ t3, app, browser, screen }) => {
  const project = await t3.project("citation-proof");
  const thread = await t3.thread({ project, title: "Citation jump proof", model: "showcase", messages: Array.from({ length: 7 }, (_, i) => `Turn ${i + 1}: continue`) });
  await t3.open(thread);
  const viewport = browser.locator(".messages-timeline-scroll");
  await viewport.waitFor();
  await viewport.click();
  await browser.keyboard.press("Home");
  await screen.getByRole("button", "Scroll to end", { exact: true }).waitFor();
  await screen.getByRole("button", "Reply to this response", { exact: true }).first().click();
  await screen.getByRole("button", "Scroll to end", { exact: true }).click();
  await screen.getByRole("button", "Scroll to end", { exact: true }).waitFor({ state: "hidden" });
  const chip = screen.getByRole("button", /^Done I added clamp/);
  await chip.click();
  try {
    await expect(screen.getByRole("button", "Scroll to end", { exact: true })).toBeVisible({ timeout: 15000 });
  } finally {
    await app.screenshot("earlier-citation-scroll-to-end");
  }
  await screen.getByRole("button", "Scroll to end", { exact: true }).click();
  await expect(screen.getByRole("button", "Scroll to end", { exact: true })).toBeHidden();
  await app.screenshot("returned-to-end");
});
