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

test("Escaped greater-than file links keep distinct folder labels", WEB, async ({ t3, app, screen, browser }) => {
  const project = await t3.project("repo", {
    "a/doc> final.ts": "export const one = 1;",
    "b/doc> final.ts": "export const two = 2;",
    "a/doc final.ts": "export const one = 1;",
    "b/doc final.ts": "export const two = 2;",
  });
  const control = await t3.thread({ project, title: "Control", messages: ["[one](<./a/doc final.ts>) [two](<./b/doc final.ts>)"] });
  const escaped = await t3.thread({ project, title: "Escaped greater-than links", messages: ["[one](<./a/doc\\> final.ts>) [two](<./b/doc\\> final.ts>)"] });
  const incomplete = await t3.thread({ project, title: "Incomplete angle destination", messages: ["[bad](<./x/unclosed text) [one](<./a/doc final.ts>) [two](<./b/doc final.ts>)"] });
  await t3.open(control);
  await screen.getByRole("button", /^Agent/).tap();
  await screen.getByRole("link", "doc final.ts · repo/a", { exact: true }).waitFor();
  await app.screenshot("control-folder-labels");
  await t3.open(escaped);
  await screen.getByRole("button", /^Agent/).tap();
  await screen.getByRole("link", /^doc> final.ts/).first().waitFor();
  await app.screenshot("escaped-folder-labels");
  const labels = await browser.evaluate(() => [...document.querySelectorAll("a")].map(a => a.textContent?.trim()).filter(t => t?.startsWith("doc> final.ts")));
  await t3.open(incomplete);
  await screen.getByRole("button", /^Agent/).tap();
  await screen.getByRole("link", /^doc final.ts/).first().waitFor();
  await app.screenshot("incomplete-angle-later-links");
  const laterLabels = await browser.evaluate(() => [...document.querySelectorAll("a")].map(a => a.textContent?.trim()).filter(t => t?.startsWith("doc final.ts")));
  expect(laterLabels).toEqual(["doc final.ts · repo/a", "doc final.ts · repo/b"]);
  expect(labels).toEqual(["doc> final.ts · repo/a", "doc> final.ts · repo/b"]);
});
