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

test("Linked submitted reviews highlight and focus their verdict rows", WEB, async ({ t3, app, browser, screen }) => {
  const project = await t3.project("review-links");
  execFileSync("git", ["remote", "add", "origin", "https://github.com/pingdotgg/t3code.git"], { cwd: project });
  const thread = await t3.thread({ project, title: "Review links", messages: ["[Approval review](https://github.com/pingdotgg/t3code/pull/45356#pullrequestreview-6101952051) and [Dismissed review](https://github.com/pingdotgg/t3code/pull/45356#pullrequestreview-6101898631)"] });
  await t3.open(thread);
  await screen.getByRole("button", /^Agent \[Approval review\]/).tap();
  await screen.getByRole("link", "Approval review", { exact: true }).tap();
  await screen.getByText(/^approved$/i).first().waitFor();
  await app.screenshot("linked-approval-verdict");
  const approval = await browser.evaluate(() => {
    const row = document.querySelector('[data-pr-link-target="true"]');
    return { marked: !!row, focused: row === document.activeElement };
  });
  await screen.getByRole("link", "Dismissed review", { exact: true }).tap();
  await screen.getByText(/^review dismissed$/i).first().waitFor();
  await app.screenshot("linked-dismissed-verdict");
  const dismissed = await browser.evaluate(() => {
    const row = document.querySelector('[data-pr-link-target="true"]');
    return { marked: !!row, focused: row === document.activeElement };
  });
  expect({ approval, dismissed }).toEqual({ approval: { marked: true, focused: true }, dismissed: { marked: true, focused: true } });
});
