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

test("Markdown previews keep the first next-line reference destination", WEB, async ({ t3, app, screen, browser }) => {
  const markdown = "[Release notes][release]\n\n[release]:\n  https://example.com/current\n\n" +
    "Ordinary background for this release. ".repeat(133) +
    "\n\n## Appendix\n\n[release]: https://example.com/previous\n";
  const project = await t3.project("release-documentation", { "release.md": markdown });
  const thread = await t3.thread({ project, title: "Release preview", messages: ['See :codex-file-citation{path="release.md"}'] });
  await t3.open(thread);
  await screen.getByRole("button", /^Agent See/).tap();
  await screen.getByRole("link", /^release\.md/).tap();
  await screen.getByRole("button", "Show rendered markdown").tap();
  const link = screen.getByRole("link", "Release notes", { exact: true });
  await link.hover();
  await browser.locator('[data-slot="tooltip-viewport"]').waitFor();
  await browser.evaluate(async () => {
    const popup = document.querySelector('[data-slot="tooltip-popup"]');
    if (popup) await Promise.all(popup.getAnimations({ subtree: true }).map(animation => animation.finished));
    return null;
  });
  await app.screenshot("release-reference-destination");
  await expect(link).toHaveAttribute("href", "https://example.com/current");
});
