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

test("Push offers View for the branch's own MR behind a newer fork MR", { ...IOS, video: "off" }, async ({ t3, app, screen }) => {
  const project = await t3.project("Original branch");
  const git = (...args: string[]) => execFileSync("git", args, { cwd: project, encoding: "utf8" });
  git("remote", "add", "origin", process.env.MR_FIXTURE_URL!);
  git("fetch", "origin");
  git("checkout", "-B", "feature", "origin/feature");
  git("branch", "--set-upstream-to=origin/feature", "feature");
  git("-c", "user.name=Scenario", "-c", "user.email=scenario@example.invalid", "commit", "--allow-empty", "-m", "Push proof");
  await t3.thread({ project, title: "Original branch push", messages: ["Verify branch push"] });
  await t3.open();
  await screen.getByText("Original branch push", { exact: false }).first().tap();
  await app.startRecording();
  try {
  await screen.getByRole("button", "Git controls").tap();
  await app.screenshot("before-push");
  await screen.getByText("Push", { exact: true }).tap();
  await screen.getByText("Pushed to origin/feature", { exact: false }).waitFor({ state: "visible", timeout: 60000 });
  await app.screenshot("push-result");
  await expect(screen.getByRole("button", "View")).toBeVisible();
  await screen.getByRole("button", "View").tap();
  await screen.getByText("Original branch MR", { exact: true }).waitFor({ state: "visible", timeout: 60000 });
  await app.screenshot("original-mr");
  } finally {
    await app.stopRecording();
  }
});
