import { expect } from "e2e";

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

test(
  "Inline HTML attribute footnote look-alikes are ignored in chunked Markdown previews",
  WEB,
  async ({ t3, app, browser, screen }) => {
    const filler = "lead ".repeat(900);
    const markdown = `before <span title="x > [^fake]">text</span>\n\n${filler}\n\n# Real[^real]\n\n[^fake]: Fake note\n[^real]: Real note\n`;
    const project = await t3.project("footnotes", { "footnotes.md": markdown });
    const thread = await t3.thread({
      project,
      title: "Footnote preview",
      messages: ['See :codex-file-citation{path="footnotes.md"}'],
    });
    await t3.open(thread);
    await screen.getByRole("button", /^Agent See/).tap();
    await screen.getByRole("link", /^footnotes\.md/).tap();
    await screen.getByRole("button", "Show rendered markdown").tap();
    await screen.getByText("Real note", { exact: false }).waitFor();
    await browser.evaluate(() => {
      const scroller = [...document.querySelectorAll<HTMLElement>(".scrollbar-gutter-both")].find(
        (element) => element.querySelector("section[data-footnotes]"),
      );
      if (!scroller) throw new Error("Rendered Markdown scroller not found");
      scroller.scrollTop = scroller.scrollHeight;
    });
    await app.screenshot("inline-html-footnote-lookalike");

    const footnotes = browser.locator("section[data-footnotes]");
    await expect(footnotes).toHaveCount(1);
    await expect(footnotes).toHaveText(/Real note/);
    await expect(footnotes).not.toHaveText(/Fake note/);
    await expect(browser.locator('a[data-footnote-ref][href="#user-content-fn-1"]')).toHaveCount(1);
  },
);
