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

test("Heading footnotes survive an unmatched paragraph backtick", WEB, async ({ t3, app, browser, screen }) => {
  const project = await t3.project("footnotes", {
    "notes.md": `Filler ${"x".repeat(4100)}\n\n\`open\n# Heading[^a]\`\n\nReal[^b]\n\n[^a]: Heading note\n[^b]: Body note\n`,
  });
  const thread = await t3.thread({ project, title: "Read notes", messages: ["Start"], model: "default" });
  await t3.open(thread);
  await screen.getByRole("button", "Toggle right panel").tap();
  await screen.getByRole("button", "Files F").tap();
  await screen.getByRole("treeitem", "notes.md").tap();
  await screen.getByRole("button", "Show rendered markdown").tap();
  await screen.getByRole("button", "Hide file explorer").tap();
  await screen.getByRole("button", "Maximize panel", { exact: false }).tap();
  await browser.evaluate(() => {
    const scroller = [...document.querySelectorAll<HTMLElement>(".scrollbar-gutter-both")].find(el => el.querySelector("h1"));
    scroller?.scrollTo(0, scroller.scrollHeight);
  });
  await screen.getByText("Body note", { exact: false }).waitFor();
  await app.screenshot("heading-and-body-footnotes");
  await expect.poll(() => browser.evaluate(() => [...document.querySelectorAll('section[data-footnotes] li')].map(el => el.textContent?.trim()))).toEqual(["Heading note ↩", "Body note ↩"]);
});
