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

test("Opposite diff sides retain both review citations", WEB, async ({ t3, app, browser, screen }) => {
  await browser.setViewport({ width: 1600, height: 1000 });
  const project = await t3.project("Citation review");
  await runPromise(run("git", ["remote", "add", "origin", "https://github.com/pingdotgg/t3code.git"], { cwd: project }));
  const thread = await t3.thread({ project, title: "Review citations", messages: ["Review https://github.com/pingdotgg/t3code/pull/35300"] });
  await t3.open(thread);
  await screen.getByTestId("composer-editor").tap();
  await browser.keyboard.press("ControlOrMeta+k");
  await screen.getByRole("combobox").last().fill("Link pull request to thread");
  await screen.getByRole("option", "Link pull request to thread").tap();
  await browser.locator('input[placeholder="Pull request URL or #42"]').fill("https://github.com/pingdotgg/t3code/pull/35300");
  await screen.getByRole("button", "Link", { exact: true }).tap();
  await browser.keyboard.press("ControlOrMeta+k");
  await screen.getByRole("combobox").last().fill("Show linked pull requests");
  await screen.getByRole("option", "Show linked pull requests").tap();
  await screen.getByText("fix(web): preserve citations when adding review comments", { exact: false }).tap();
  await screen.getByRole("button", "Code", { exact: true }).tap();
  await screen.getByRole("button", "Expand diff", { exact: true }).first().tap();
  await app.screenshot("diff-ready");
  const select = async (side: string) => {
    const point = await browser.evaluate((side) => {
      for (const host of document.querySelectorAll("*")) {
        const line = host.shadowRoot?.querySelector<HTMLElement>(`[data-column-number="1345"][data-line-type="change-${side}"]`);
        if (line) {
          line.scrollIntoView({ block: "center" });
          const rect = line.getBoundingClientRect();
          return { x: rect.x + rect.width / 2, y: rect.y + rect.height / 2 };
        }
      }
      throw new Error("Missing diff gutter " + side);
    }, side);
    await browser.mouse.move(point.x, point.y);
    await browser.mouse.down();
    await browser.mouse.up();
  };
  await select("deletion");
  await browser.locator('textarea[placeholder="Add a comment…"]').fill("Q1 about the DELETED line 1345?");
  await screen.getByRole("button", "Add to agent", { exact: true }).tap();
  await screen.getByText("Added to the composer", { exact: true }).waitFor({ state: "hidden" });
  await app.screenshot("first-citation");
  await select("addition");
  await browser.locator('textarea[placeholder="Add a comment…"]').fill("Q2 about the ADDED line 1345?");
  await screen.getByRole("button", "Add to agent", { exact: true }).tap();
  await screen.getByText("Added to the composer", { exact: true }).waitFor({ state: "hidden" });
  await app.screenshot("both-questions");
  await expect(browser.locator('[data-slot="context-chip-label"]').filter({ hasText: "pullRequestDetail.logic.ts L1345" })).toHaveCount(2);
});
