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

test("Fix handoffs preserve the visible non-line review instruction", WEB, async ({t3, app, browser, screen}) => {
  await browser.setViewport({width: 1440, height: 1000});
  const project = await t3.project("total-repro");
  const git = (args: string[]) => runPromise(run("git", args, {cwd: project}));
  await git(["remote", "add", "origin", "https://gitlab.com/julius54/total-proof-44088-1795772627.git"]);
  await git(["fetch", "origin", "main", "fix-total"]);
  await git(["reset", "--hard", "origin/main"]);
  const thread = await t3.thread({project, title:"Review total", messages:["Ready"]});
  await t3.open(thread);
  const reviewPath = "/pull-requests?selectedUrl=" + encodeURIComponent("https://gitlab.com/julius54/total-proof-44088-1795772627/-/merge_requests/1");
  await t3.open(reviewPath);
  await screen.getByRole("button", /^Open #1 Fix wrong total/).tap();
  await screen.getByRole("button", "Fix in a thread", {exact:true}).tap();
  const composer = browser.locator('[contenteditable="true"][role="textbox"]');
  await composer.filter({hasText:"Fix the review remark quoted below."}).waitFor();
  await composer.tap();
  await composer.press("Control+End");
  await app.screenshot("individual-fix-prompt");
  const individual = await composer.textContent();
  await t3.open(reviewPath);
  await screen.getByRole("button", /^Open #1 Fix wrong total/).tap();
  await screen.getByRole("button", "More merge request actions", {exact:true}).tap();
  await screen.getByRole("menuitem", "Fix findings in a thread", {exact:true}).tap();
  await composer.filter({hasText:"Review remarks with no line to attach them to:"}).waitFor();
  await composer.tap();
  await composer.press("Control+End");
  await app.screenshot("bulk-fix-prompt");
  const bulk = await composer.textContent();
  expect({individualVisible: individual?.includes("julius54: Fix wrong total"), individualHidden: individual?.includes("<!--"), bulkVisible: bulk?.includes("julius54: Fix wrong total"), bulkHidden: bulk?.includes("<!--")}).toEqual({individualVisible:true, individualHidden:false, bulkVisible:true, bulkHidden:false});
});
