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

test("Inline HTML before a fence keeps the sample inside code", WEB, async ({ t3, app, screen, browser }) => {
  const project = await t3.project("markdown-example", {
    "inline-html-fence.md": "<b>" + "x".repeat(4100) + "</b>\n```md\n\n# literal\n```\n",
    "short-control.md": "<b>" + "x".repeat(100) + "</b>\n```md\n\n# literal\n```\n",
  });
  const thread = await t3.thread({ project, title: "Markdown preview", messages: ["Start"] });
  await t3.open(thread);
  await screen.getByRole("button", "Toggle right panel").tap();
  await screen.getByRole("button", "Files F").tap();
  await screen.getByRole("treeitem", "inline-html-fence.md").tap();
  await screen.getByRole("button", "Show rendered markdown").tap();
  await screen.getByText("literal", { exact: false }).waitFor();
  await browser.evaluate(() => {
    const sample = [...document.querySelectorAll("h1, pre")].find(e => e.textContent?.includes("literal"));
    sample?.scrollIntoView({ block: "center" });
    return null;
  });
  await app.screenshot("inline-html-fence");
  await expect.poll(() => browser.evaluate(() => ({
    headings: [...document.querySelectorAll("h1")].filter(e => e.textContent === "literal").length,
    code: [...document.querySelectorAll("pre")].some(e => e.textContent?.includes("# literal")),
  }))).toEqual({ headings: 0, code: true });
});
