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

test("File link folder labels ignore Markdown inside code", WEB, async ({ t3, app, browser, screen }) => {
  const project = await t3.project("demo-docs", {
    "a/README.md": "# Setup A\n",
    "b/README.md": "# Setup B\n",
  });
  const cases = [
    { label: "fenced-code", text: "Here is the snippet:\n\n```md\n- [Setup](a/README.md)\n```\n\nThen open [README.md](b/README.md).", count: 1 },
    { label: "inline-code", text: "Write `[Setup](a/README.md)` in the docs. Then open [README.md](b/README.md).", count: 1 },
    { label: "single-link-control", text: "Then open [README.md](b/README.md).", count: 1 },
    { label: "two-links-control", text: "Open [README.md](a/README.md) and [README.md](b/README.md).", count: 2 },
  ];
  const threads = [];
  for (const entry of cases) {
    threads.push(await t3.thread({ project, title: entry.label, messages: [entry.text] }));
  }
  const actual = [];
  for (const [index, entry] of cases.entries()) {
    await t3.open(threads[index]!);
    await screen.getByRole("button", /^Agent /).tap();
    await browser.locator("[data-user-message-body]").getByRole("link").first().waitFor();
    if (entry.label === "fenced-code") {
      await browser.locator("[data-user-message-body] pre").getByText("- [Setup](a/README.md)", { exact: true }).waitFor();
    }
    await app.screenshot(entry.label);
    actual.push(await browser.evaluate(() => [...document.querySelectorAll('[data-user-message-body] a')].map(link => link.textContent?.trim())));
  }
  expect(actual).toEqual([
    ["README.md"],
    ["README.md"],
    ["README.md"],
    ["README.md · demo-docs/a", "README.md · demo-docs/b"],
  ]);
});
