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

test("PR source and test file expansion survives a whitespace toggle", WEB, async ({ t3, app, screen, browser }) => {
  await browser.setViewport({ width: 1440, height: 1000 });
  const project = await t3.project("Review demo");
  execFileSync("git", ["remote", "add", "origin", "https://github.com/pingdotgg/t3code.git"], { cwd: project });
  const thread = await t3.thread({ project, title: "Review folds", messages: ["first"] });
  await t3.open(thread);
  await t3.open("/pull-requests?repository=pingdotgg%2Ft3code&number=44973&state=all&selectedUrl=https%3A%2F%2Fgithub.com%2Fpingdotgg%2Ft3code%2Fpull%2F44973");
  await screen.getByRole("button", "Maximize panel", { exact: true }).tap();
  await screen.getByRole("button", "Code", { exact: true }).tap();
  const source = "apps/web/src/components/files/VirtualTablePreview.tsx";
  const tests = "apps/web/src/components/files/AttachmentFilePreview.browser.test.tsx";
  const toggle = (path: string) => browser.locator("diffs-container").filter({ hasText: path }).getByRole("button", /^(Expand|Collapse) diff$/);
  await toggle(source).waitFor({ timeout: 60000 });
  await toggle(source).tap();
  await toggle(tests).tap();
  await browser.evaluate(async () => { await Promise.all(document.getAnimations().map(animation => animation.finished.catch(() => {}))); });
  await app.screenshot("files-expanded-before-toggle");
  await screen.getByRole("button", "Show whitespace changes", { exact: true }).tap();
  await screen.getByRole("button", "Hide whitespace changes", { exact: true }).waitFor();
  try {
    await expect(toggle(source)).toHaveAttribute("aria-expanded", "true");
    await expect(toggle(tests)).toHaveAttribute("aria-expanded", "true");
  } finally {
    await browser.evaluate(async () => { await Promise.all(document.getAnimations().map(animation => animation.finished.catch(() => {}))); });
    await app.screenshot("files-after-whitespace-toggle");
  }
});
