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

test("Shift arrows extend PR comment selection while mention suggestions are open", WEB, async ({ t3, app, screen, browser }) => {
  const project = await t3.project("Selection demo");
  await runPromise(run("git", ["remote", "add", "origin", "https://github.com/pingdotgg/t3code.git"], { cwd: project }));
  await t3.open("/pull-requests?" + new URLSearchParams({ selectedUrl: "https://github.com/pingdotgg/t3code/pull/31247" }));
  await screen.getByRole("button", /^Open #31247 /).tap();
  await screen.getByRole("button", "Timeline", { exact: true }).tap();
  await screen.getByRole("button", "Comment on pull request", { exact: true }).tap();
  const input = screen.getByRole("combobox", "Comment on this pull request", { exact: true });
  await input.fill("previous line\n@ju");
  await screen.getByRole("option").first().waitFor();
  await input.press("Shift+ArrowUp");
  const up = await browser.evaluate(() => { const t = document.querySelector('textarea[aria-label="Comment on this pull request"]') as HTMLTextAreaElement; return [t.selectionStart, t.selectionEnd]; });
  await app.screenshot("shift-arrow-up");
  await input.press("ArrowRight");
  await screen.getByRole("option").first().waitFor();
  await input.press("Escape");
  await input.fill("previous line\n@ju\nnext line");
  await input.press("ArrowUp");
  await input.press("End");
  await screen.getByRole("option").first().waitFor();
  await input.press("Shift+ArrowDown");
  const down = await browser.evaluate(() => { const t = document.querySelector('textarea[aria-label="Comment on this pull request"]') as HTMLTextAreaElement; return [t.selectionStart, t.selectionEnd]; });
  await app.screenshot("shift-arrow-down");
  await input.fill("previous line\n@ju");
  await screen.getByRole("option").first().waitFor();
  const initial = await browser.evaluate(() => document.querySelector('[role="option"][aria-selected="true"]')?.textContent);
  await input.press("ArrowDown");
  const plain = await browser.evaluate(() => document.querySelector('[role="option"][aria-selected="true"]')?.textContent);
  await app.screenshot("plain-arrow-down");
  await input.fill("");
  expect(up[0] < up[1], "Shift+ArrowUp extends the native selection").toBe(true);
  expect(down[0] < down[1], "Shift+ArrowDown extends the native selection").toBe(true);
  expect(plain, "Unmodified ArrowDown still cycles suggestions").not.toBe(initial);
});
