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

test("A modified file whose patch GitHub withheld is not shown as an empty change", WEB, async ({ t3, app, browser, screen }) => {
  const project = await t3.project("distilled");
  execFileSync("git", ["remote", "add", "origin", "https://github.com/alchemy-run/distilled.git"], { cwd: project });
  execFileSync("git", ["fetch", "--depth=1", "origin", "main"], { cwd: project });
  execFileSync("git", ["reset", "--hard", "FETCH_HEAD"], { cwd: project });
  await browser.setViewport({ width: 1600, height: 1000 });
  await t3.open("/pull-requests?repository=alchemy-run%2Fdistilled&number=1223&state=all&selectedUrl=" + encodeURIComponent("https://github.com/alchemy-run/distilled/pull/1223"));
  await screen.getByRole("button", "Maximize panel", { exact: true }).tap();
  await screen.getByRole("button", "Code", { exact: true }).tap();
  await screen.getByRole("button", "Diff scope: All commits", { exact: true }).tap();
  await screen.getByRole("menuitemradio", /f1b5d44/).tap();
  await screen.getByText("packages/aws/src/services/socialmessaging.ts", { exact: true }).first().tap();
  await browser.evaluate(() => {
    const visit = (root: Document | ShadowRoot) => {
      for (const element of root.querySelectorAll("*")) {
        if (element.matches("[data-title]") && element.textContent?.trim() === "packages/aws/src/services/socialmessaging.ts") {
          element.scrollIntoView({ block: "center" });
          return true;
        }
        if (element.shadowRoot && visit(element.shadowRoot)) return true;
      }
      return false;
    };
    visit(document);
  });
  await app.screenshot("socialmessaging-missing-patch");
  expect(await browser.locator("diffs-container").filter({ hasText: "packages/aws/src/services/socialmessaging.ts" }).getByText("Diff too large to display.", { exact: true }).isVisible(), "A modified file with a withheld patch displays its unavailable-diff notice").toBe(true);
});
