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

test("Relative colon citation opens its workspace file", WEB, async ({ t3, app, browser, screen }) => {
  await browser.emulateMedia({ colorScheme: "dark" });
  const project = await t3.project("citation-demo", {
    "notes:final.txt": "first line\nsecond line of the colon file\nthird line\n",
    "notes.txt": "first line\nsecond line of the plain file\nthird line\n",
    "file:notes.txt": "first line\nsecond line of the file-colon file\nthird line\n",
  });
  // CLI user messages use the same citation Markdown renderer as assistant replies.
  const thread = await t3.thread({ project, title: "Colon filename citations", messages: [
    'Citations\n\n:codex-file-citation{path="notes:final.txt" line_range_start="2"}\n\n:codex-file-citation{path="notes.txt" line_range_start="2"}\n\n:codex-file-citation{path="file:notes.txt"}\n\n[notes:final.txt](<./notes:final.txt>)',
  ] });
  await t3.open(thread);
  await screen.getByRole("button", /^Agent Citations/).tap();
  await screen.getByRole("link", /^notes\.txt/).first().waitFor();
  await screen.getByText(/Syncing messages/).waitFor({ state: "hidden" });
  await app.screenshot("rendered-citations");
  // Click even the inert base anchor so both recordings show the same attempted interaction.
  await browser.evaluate(() => {
    const citation = [...document.querySelectorAll("a")].find(a => a.textContent?.startsWith("notes:final.txt"));
    citation?.click();
    return null;
  });
  try {
    await expect.poll(() => browser.evaluate(() => {
      for (const host of document.querySelectorAll("*")) {
        const content = host.shadowRoot?.querySelector("[data-content]");
        if (content) return content.textContent ?? "";
      }
      return null;
    })).toContain("second line of the colon file");
  } finally {
    await app.screenshot("after-clicking-colon-citation");
  }
});
