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

test("Push on main explains why feature branch checkout is unavailable", WEB, async ({ t3, app, screen, browser }) => {
  await browser.addInitScript(() => {
    document.addEventListener("DOMContentLoaded", () => {
      const style = document.createElement("style");
      style.textContent = "*, *::before, *::after { animation-duration: 0s !important; transition-duration: 0s !important; }";
      document.head.append(style);
    });
  });
  const project = await t3.project("Push demo", { "README.md": "Initial line\n" });
  const git = (...args: string[]) => execFileSync("git", args, { cwd: project, encoding: "utf8" });
  git("init", "--bare", "--initial-branch=main", `${project}/../remote.git`);
  git("remote", "add", "origin", `${project}/../remote.git`);
  git("push", "-u", "origin", "main");
  git("--git-dir", `${project}/../remote.git`, "symbolic-ref", "HEAD", "refs/heads/main");
  git("remote", "set-head", "origin", "main");
  writeFileSync(`${project}/README.md`, "Initial line\nUnpushed line\n");
  git("add", "README.md");
  git("-c", "user.name=Scenario", "-c", "user.email=scenario@example.invalid", "commit", "-m", "Unpushed change");
  writeFileSync(`${project}/README.md`, "Initial line\nUnpushed line\nLocal edit\n");
  const thread = await t3.thread({ project, title: "Push on main", messages: ["first"] });
  await t3.open(thread);
  await screen.getByRole("button", "Changes vs origin/main +2 -0", { exact: true }).waitFor({ state: "visible" });
  await screen.getByRole("button", "Git action options").tap();
  await browser.locator('[role="menuitem"]:not([aria-disabled="true"]):not([data-disabled])').filter({ hasText: /^Push$/ }).waitFor({ state: "visible" });
  await app.screenshot("git-actions");
  await screen.getByRole("menuitem", "Push", { exact: true }).tap();
  await screen.getByRole("button", "Abort", { exact: true }).waitFor({ state: "visible" });
  await app.screenshot("push-default-dialog");
  const enabled = await browser.evaluate(() => [...document.querySelectorAll("button")].some(b => b.textContent?.includes("Check out feature branch & continue") && !b.disabled));
  if (enabled) {
    await screen.getByRole("button", "Check out feature branch & continue").tap();
    await screen.getByText("Feature-branch checkout is only supported for commit actions.", { exact: false }).waitFor({ state: "visible" });
    await app.screenshot("rejected-feature-checkout");
  } else {
    await screen.getByRole("button", "Abort", { exact: true }).tap();
  }
  await screen.getByRole("button", "Commit & push", { exact: true }).tap();
  await app.screenshot("commit-push-control");
  await screen.getByRole("button", "Abort", { exact: true }).tap();
  await screen.getByRole("button", "Git action options").tap();
  await screen.getByRole("menuitem", "Push", { exact: true }).tap();
  await expect(screen.getByRole("button", "Check out feature branch & continue")).toBeDisabled();
  await expect(screen.getByText("This action doesn't commit your changes", { exact: false })).toBeVisible();
  await expect(screen.getByRole("button", "Push to main", { exact: true })).toBeEnabled();
});
