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

test("Code indented with spaces and a tab has no footnotes", WEB, async ({ t3, app, browser, screen }) => {
  const filler = "Filler " + "x".repeat(4100) + "\n\n";
  const note = (indent: string, long = true) => (long ? filler : "") + "Paragraph\n\n" + indent + "code[^a]\n\n[^a]: note\n";
  const files = {
    "note1.md": note(" \t"),
    "note2.md": note("  \t"),
    "note3.md": note("   \t"),
    "control-short.md": note(" \t", false),
    "control-4spaces.md": note("    "),
  };
  const project = await t3.project("footnote-preview", files);
  const input = { project, title: "Markdown footnote proof", messages: ["Start"] };
  const thread = await t3.thread({ ...input, model: "default" }).catch((error: unknown) => {
    if (!String(error).includes("Model default is not advertised by provider devReplay.")) throw error;
    return t3.thread(input);
  });
  await t3.open(thread);
  await screen.getByRole("button", "Toggle right panel").tap();
  await screen.getByRole("button", "Files F").tap();
  const counts: number[] = [];
  for (const name of Object.keys(files)) {
    await screen.getByRole("treeitem", name, { exact: true }).tap();
    if (name === "note1.md") {
      await screen.getByRole("button", /^Maximize panel/).tap();
      await screen.getByRole("button", "Show rendered markdown").tap();
    }
    await browser.locator("pre code").filter({ hasText: "code[^a]" }).waitFor();
    await browser.evaluate(() => {
      Array.from(document.querySelectorAll("pre code")).find((node) => node.textContent?.includes("code[^a]"))?.scrollIntoView({ block: "center" });
    });
    await app.screenshot(name.replace(".md", ""));
    counts.push(await browser.locator("section[data-footnotes]").count());
  }
  expect(counts).toEqual([0, 0, 0, 0, 0]);
});
