import { expect } from "e2e";

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

test(
  "Timeline minimap preserves angle brackets in user messages",
  WEB,
  async ({ t3, app, screen, browser }) => {
    const project = await t3.project("demo");
    const thread = await t3.thread({
      project,
      title: "Angle bracket minimap",
      model: "default",
      messages: [
        "First turn: hello there",
        "Fix <Button> focus and Promise<string> handling in <t3-thread://v2/env-1/thread-1>",
        "Third turn: thanks",
      ],
    });
    await browser.setViewport({ width: 1440, height: 1000 });
    await t3.open(thread);

    const userMessage = screen.getByText(
      "Fix <Button> focus and Promise<string> handling in <t3-thread://v2/env-1/thread-1>",
      { exact: true },
    );
    await userMessage.waitFor({ timeout: 30_000 });

    const jumpButtons = screen.getByRole("button", /^Jump to message:/);
    await jumpButtons.last().focus();
    await jumpButtons.last().press("ArrowUp");
    const label = await browser.evaluate(() => ({
      ariaLabels: Array.from(document.querySelectorAll<HTMLButtonElement>('button[aria-label^="Jump to message:"]')).map(
        (button) => button.getAttribute("aria-label") ?? "",
      ),
    }));
    await app.screenshot("minimap-angle-brackets");
    expect(label.ariaLabels.join("\n")).toContain("<Button>");
    expect(label.ariaLabels.join("\n")).toContain("Promise<string>");
    expect(label.ariaLabels.join("\n")).toContain("a thread");
  },
);
