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

test("Minimap shows the cited passage and file label", WEB, async ({ t3, app, screen, browser }) => {
  await browser.setViewport({ width: 1440, height: 1000 });
  const project = await t3.project("demo", { "README.md": "# Demo\nThe clamp helper lives beside sum.\n", "src/math.ts": "export function sum(a: number, b: number) { return a + b; }\n" });
  const thread = await t3.thread({ project, title: "Minimap citation preview", messages: ["go"], model: "showcase" });
  await t3.open(thread);
  const paragraph = screen.getByText("I added clamp next to sum and the tests pass.", { exact: true });
  await paragraph.tap();
  await browser.evaluate(() => {
    const paragraph = [...document.querySelectorAll("p")].find(p => p.textContent === "I added clamp next to sum and the tests pass.");
    if (!paragraph) throw new Error("Assistant paragraph missing");
    const range = document.createRange();
    range.selectNodeContents(paragraph);
    window.getSelection()?.removeAllRanges();
    window.getSelection()?.addRange(range);
    paragraph.dispatchEvent(new MouseEvent("mouseup", { bubbles: true }));
    document.dispatchEvent(new Event("selectionchange"));
  });
  await screen.getByRole("button", "Cite selection in composer").tap();
  await screen.getByRole("button", "Save", { exact: true }).tap();
  const composer = screen.getByRole("textbox");
  await composer.press("End");
  await composer.press("Space");
  await browser.keyboard.type("Where does @READ");
  await screen.getByText("README.md", { exact: true }).tap();
  await browser.keyboard.type(" cover this?");
  await composer.press("Enter");
  await screen.getByRole("button", "Stop generation").waitFor({ state: "hidden" });
  const jump = screen.getByRole("button", /^Jump to message:/);
  await jump.hover();
  await jump.press("End");
  await jump.press("Enter");
  await jump.hover();
  await jump.press("End");
  await app.screenshot("minimap-follow-up");
  await expect(jump).toHaveAccessibleName(/Where does README\.md cover this\?/);
  await expect(jump).toHaveAccessibleName(/I added clamp next to sum and the tests pass\./);
  await expect(jump).not.toHaveAccessibleName(/t3-citation:|t3-context:|\[README\.md\]|assistant-quote-1/);
});
