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

test("The checked-out fork branch shows its own open pull request", WEB, async ({ t3, app, screen }) => {
  const project = await t3.project("DefinitelyTyped");
  const git = (...args: string[]) => execFileSync("git", args, { cwd: project, encoding: "utf8" });
  git("remote", "add", "origin", "https://github.com/fdx-wayne-yeap/DefinitelyTyped.git");
  git("config", "remote.origin.fetch", "+refs/heads/*:refs/remotes/origin/*");
  git("fetch", "--depth=1", "--filter=blob:none", "origin", "patch-1", "master");
  git("sparse-checkout", "set", "--no-cone", "README.md");
  git("checkout", "-B", "patch-1", "origin/patch-1");
  git("remote", "set-head", "origin", "master");
  git("remote", "add", "upstream", "https://github.com/DefinitelyTyped/DefinitelyTyped.git");
  git("fetch", "--depth=1", "--filter=blob:none", "upstream", "master");
  const thread = await t3.thread({ project, title: "Find the branch pull request", messages: ["hello"], model: "showcase" });
  await t3.open(thread);
  try {
    await expect(screen.getByText("#71729", { exact: false }).first()).toBeVisible({ timeout: 60000 });
  } finally {
    await app.screenshot("branch-pull-request");
  }
});
