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

test("Mixed space and tab footnote continuation survives chunking", WEB, async ({ t3, app, browser, screen }) => {
  const note = (indent: string, long = true) => `${long ? `Filler ${"x".repeat(4100)}\n\n` : ""}Real[^b]\n\n[^b]: First paragraph\n\n${indent}Second paragraph\n`;
  const files = {
    "note-1sp-tab.md": note(" \t"),
    "note-2sp-tab.md": note("  \t"),
    "note-3sp-tab.md": note("   \t"),
    "note-control-4sp.md": note("    "),
    "short-1sp-tab.md": note(" \t", false),
  };
  const project = await t3.project("Footnote proof", files);
  const thread = await t3.thread({ project, title: "Footnote paragraphs", messages: ["Start"], model: "default" });
  await t3.open(thread);
  await screen.getByRole("button", "Toggle right panel", { exact: true }).tap();
  await screen.getByRole("button", "Files F", { exact: true }).tap();
  const results: Record<string, boolean> = {};
  let first = true;
  for (const name of Object.keys(files)) {
    await screen.getByRole("treeitem", name, { exact: true }).tap();
    if (first) {
      await screen.getByRole("button", "Maximize panel", { exact: true }).tap();
      first = false;
    }
    await screen.getByRole("button", "Show rendered markdown").tap();
    await browser.evaluate(() => { for (const el of document.querySelectorAll<HTMLElement>(".scrollbar-gutter-both")) { if (el.querySelector("[data-footnotes]")) el.scrollTop = el.scrollHeight; } return null; });
    await screen.getByRole("link", "1", { exact: true }).tap();
    await screen.getByText("First paragraph", { exact: false }).waitFor();
    results[name] = await browser.evaluate(() => [...document.querySelectorAll('[data-footnotes]')].some(el => el.textContent?.includes("Second paragraph")));
    await app.screenshot(name.replace(".md", ""));
  }
  expect(results).toEqual(Object.fromEntries(Object.keys(files).map(name => [name, true])));
});
