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

test("Linked PR comment keeps all four highlight edges", WEB, async ({ t3, app, browser, screen }) => {
  await t3.project("t3code", { ".git/config": '[remote "origin"]\nurl = https://github.com/pingdotgg/t3code.git\nfetch = +refs/heads/*:refs/remotes/origin/*\n' });
  const selectedUrl = "https://github.com/pingdotgg/t3code/pull/37881#issuecomment-6936440314";
  await t3.open(`/pull-requests?repository=pingdotgg%2Ft3code&number=37881&selectedUrl=${encodeURIComponent(selectedUrl)}`);
  await browser.locator('article[data-pr-link-target="true"]').waitFor({ state: "visible", timeout: 120000 });
  await screen.getByText("Loading pull requests...", { exact: true }).waitFor({ state: "hidden", timeout: 120000 });
  await browser.evaluate(async () => {
    (document.activeElement as HTMLElement | null)?.blur();
    await document.fonts.ready;
  });
  const first = await app.screenshot("linked-comment-outline");
  await new Promise((resolve) => setTimeout(resolve, 500));
  const second = await app.screenshot("linked-comment-outline-stable");
  if (!readFileSync(first).equals(readFileSync(second))) throw new Error("Capture was not stable");
  const outlineIsUnclipped = await browser.evaluate(() => {
    const article = document.querySelector('article[data-pr-link-target="true"]')!;
    const shadow = getComputedStyle(article).boxShadow;
    const rect = article.getBoundingClientRect();
    if (shadow.includes("inset")) return true;
    for (let parent = article.parentElement; parent; parent = parent.parentElement) {
      if (["hidden", "clip"].includes(getComputedStyle(parent).overflowX)) {
        const clip = parent.getBoundingClientRect();
        if (rect.left - 1 < clip.left || rect.right + 1 > clip.right) return false;
      }
    }
    return true;
  });
  expect(outlineIsUnclipped).toBe(true);
});
