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

test("Tab then Enter saves a new citation comment", WEB, async ({ t3, app, screen, browser }) => {
  const project = await t3.project("citation-demo", { "src/math.ts": "export const sum = 1 + 1;\n" });
  const thread = await t3.thread({ project, title: "Citation keyboard flow", model: "showcase", messages: ["Show the math changes"] });
  await t3.open(thread);
  await screen.getByText("I added clamp next to sum and the tests pass.", { exact: false }).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);
    const selection = window.getSelection();
    selection?.removeAllRanges();
    selection?.addRange(range);
    document.dispatchEvent(new Event("selectionchange"));
    return null;
  });
  await screen.getByRole("button", "Cite selection in composer").tap();
  await browser.keyboard.type("agreed");
  await browser.keyboard.press("Tab");
  await app.screenshot("after-tab");
  await browser.keyboard.press("Enter");
  await app.screenshot("after-enter");
  await expect(screen.getByRole("link", "View cited assistant text: agreed")).toBeVisible();
  await expect(screen.getByRole("button", "Edit citation comment")).toBeVisible();
});
