import { execFileSync } from "node:child_process";
import { expect } from "e2e";
import { test, WEB } from "./t3.ts";

test("A review added to chat keeps its whole summary and line-comment status", WEB, async ({ t3, app, screen, browser }) => {
  const project = await t3.project("yMusic");
  const git = (args: string[]) => execFileSync("git", args, { cwd: project, encoding: "utf8" });
  git(["remote", "add", "origin", "https://github.com/TonybynMp4/yMusic.git"]);
  git(["fetch", "--depth=1", "origin", "+refs/pull/137/head:refs/remotes/origin/feat/autostart-setting"]);
  git(["checkout", "-B", "feat/autostart-setting", "--track", "origin/feat/autostart-setting"]);
  const thread = await t3.thread({ project, title: "Review handoff", messages: ["Ready to discuss the pull request review."] });
  await t3.open(thread);
  await app.screenshot("thread-ready");
  await screen.getByRole("button", "https://github.com/TonybynMp4/yMusic/pull/137", { exact: true }).tap();
  await screen.getByRole("button", "Filter comments").tap();
  await screen.getByRole("menuitemcheckbox", "Bot comments").tap();
  await browser.keyboard.press("Escape");
  const review = browser.locator("article").filter({ hasText: "One misplaced doc comment inline" });
  await review.getByRole("button", "Add to chat").tap();
  await screen.getByRole("button", "Close #137", { exact: true }).tap();
  const chip = screen.getByRole("button", "Review comment, #137 @pullfrog comment. Show details", { exact: true });
  const showTail = async () => {
    await browser.evaluate(async () => {
      await document.fonts.ready;
      await Promise.all(document.getAnimations().filter((animation) => animation.effect?.getTiming().iterations !== Infinity).map((animation) => animation.finished.catch(() => {})));
      const popup = document.querySelector('[data-slot="popover-popup"]');
      if (!popup) throw new Error("Review details did not open");
      await new Promise<void>((resolve) => {
        let timer: ReturnType<typeof setTimeout>;
        const finish = () => { observer.disconnect(); resolve(); };
        const observer = new ResizeObserver(() => { clearTimeout(timer); timer = setTimeout(finish, 500); });
        observer.observe(popup);
        timer = setTimeout(finish, 500);
      });
      const viewport = document.querySelector('[data-slot="popover-viewport"]');
      if (!viewport) throw new Error("Review details viewport is missing");
      viewport.scrollTop = viewport.scrollHeight;
    });
  };
  await chip.tap();
  await showTail();
  await app.screenshot("draft-review-tail");
  await browser.keyboard.press("Escape");
  await screen.getByRole("button", "Submit message", { exact: true }).tap();
  await screen.getByText("second fixture turn complete", { exact: false }).textContent();
  await chip.tap();
  await showTail();
  await app.screenshot("sent-review-tail");
  const details = browser.locator('[data-slot="popover-popup"]');
  await expect(details.getByText("This review posted no line comments.", { exact: false })).toBeVisible();
  await expect(details.getByText("instead of re-reading.", { exact: false })).toBeVisible();
  await expect(browser.locator('[data-slot="popover-popup"] a[href="https://github.com/TonybynMp4/yMusic/pull/137#pullrequestreview-5937777275"]')).toBeVisible();
});
