import { expect } from "e2e";
import { test, WEB } from "./t3.ts";

test("Markdown reference resolves after an unclosed list continuation fence", WEB, async ({ t3, app, browser, screen }) => {
  const prefix = "[Release notes][release]\n\n" + "Background for this release. ".repeat(180) + "\n\n## Appendix\n\n1. Example\n";
  const definition = "[release]: https://example.com/releases\n";
  const project = await t3.project("documentation", {
    "control.md": prefix + "\n" + definition,
    "release.md": prefix + "   ```text\n   sample\n" + definition,
  });
  const thread = await t3.thread({ project, title: "Release documentation", model: "default", messages: ["Review release documentation"] });
  await t3.open(thread);
  await screen.getByRole("textbox", "Message Release documentation").waitFor();
  const open = async (name: string) => {
    await browser.keyboard.press("Control+p");
    await screen.getByPlaceholder("Search files…").fill(name);
    await screen.getByRole("option", `${name} ${name}`, { exact: true }).waitFor();
    await screen.getByRole("option").filter({ hasText: name === "control.md" ? "release.md" : "control.md" }).waitFor({ state: "hidden" });
    await screen.getByRole("option", `${name} ${name}`, { exact: true }).tap();
    await screen.getByPlaceholder("Search files…").waitFor({ state: "hidden" });
    await browser.evaluate(async () => { await Promise.all(document.getAnimations().filter(a => a.effect?.getTiming().iterations !== Infinity).map(a => a.finished.catch(() => {}))); return null; });
  };
  await open("control.md");
  await screen.getByRole("button", "Show rendered markdown").tap();
  await screen.getByRole("link", "Release notes", { exact: true }).waitFor();
  await app.screenshot("control-reference");
  await open("release.md");
  await screen.getByText("Background for this release.", { exact: false }).waitFor();
  await app.screenshot("list-fence-reference");
  await expect(screen.getByRole("link", "Release notes", { exact: true })).toHaveAttribute("href", "https://example.com/releases");
});
