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

test("References after indented headings resolve across preview chunks", WEB, async ({ t3, app, screen, browser }) => {
  const markdown = `[Release notes][release]\n\n${"Background for this release. ".repeat(180)}\n\n## Appendix\n\n  ### References\n[release]: https://example.com/releases\n`;
  const project = await t3.project("release-preview", { "release.md": markdown });
  const thread = await t3.thread({ project, title: "Review release notes", messages: ["Start"] });
  await t3.open(thread);
  await screen.getByText("fixture turn complete", { exact: false }).waitFor();
  await screen.getByRole("button", "Toggle right panel").tap();
  await screen.getByText("Files", { exact: true }).tap();
  const openFile = () => browser.evaluate(() => {
    for (const host of document.querySelectorAll("*")) {
      const row = host.shadowRoot?.querySelector<HTMLElement>('[data-item-path="release.md"]');
      if (row) { row.click(); return true; }
    }
    return false;
  });
  // Wait for the asynchronously loaded file tree, then select the same file as the report.
  await expect.poll(openFile).toBe(true);
  await screen.getByRole("button", "Show rendered markdown").tap();
  await screen.getByText("Background for this release.", { exact: false }).waitFor({ state: "visible" });
  await app.screenshot("release-reference");
  await expect(screen.getByRole("link", "Release notes", { exact: true })).toHaveAttribute("href", "https://example.com/releases");
});
