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

test("Relative Markdown parent link opens a normalized file path", WEB, async ({ t3, app, browser, screen }) => {
  const project = await t3.project("proj", {
    "docs/internals/01_architecture.md": "# Architecture\n\nSee the [development runbook](../operations/00_development.md).\n",
    "docs/operations/00_development.md": "# Development runbook\n\nDevelopment setup and operations.\n",
  });
  const thread = await t3.thread({ project, title: "Relative file link", messages: ["Start"] });
  await t3.open(thread);
  await screen.getByRole("button", "Toggle right panel").tap();
  await screen.getByText("Files", { exact: true }).tap();
  await screen.getByRole("button", /^Maximize panel/).tap();
  await screen.getByRole("button", "Expand all folders").tap();
  await browser.evaluate(() => {
    for (const host of document.querySelectorAll("*")) {
      const row = host.shadowRoot?.querySelector<HTMLElement>('[data-item-path="docs/internals/01_architecture.md"]');
      if (row) { row.click(); return true; }
    }
    return false;
  });
  await screen.getByRole("button", "Show rendered markdown").tap();
  await screen.getByRole("link", /^00_development\.md/).tap();
  await screen.getByRole("heading", "Development runbook", { exact: true }).waitFor();
  await app.screenshot("relative-link-opened");
  const crumbs = await browser.evaluate(() => [...document.querySelectorAll("[data-file-crumb]")].map(crumb => crumb.textContent?.trim()));
  expect(crumbs).not.toContain("..");
  await expect.poll(() => browser.evaluate(() => {
    for (const host of document.querySelectorAll("*")) {
      const row = host.shadowRoot?.querySelector('[data-item-path="docs/operations/00_development.md"]');
      if (row) return row.getAttribute("aria-selected");
    }
    return null;
  })).toBe("true");
});
