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

test("First commit shows net changes for a staged then edited file", WEB, async ({ t3, app, screen }) => {
  const project = await t3.project("First commit demo");
  const git = (...args: string[]) => execFileSync("git", args, { cwd: project });
  git("rm", "-f", "README.md");
  git("update-ref", "-d", "HEAD");
  writeFileSync(join(project, "new.txt"), "staged\n");
  git("add", "new.txt");
  writeFileSync(join(project, "new.txt"), "actual\n");
  const thread = await t3.thread({ project, title: "Prepare first commit", messages: [] });
  await t3.open(thread);
  await screen.getByRole("button", "Git action options").tap();
  await screen.getByRole("menuitem", "Commit", { exact: true }).tap();
  const file = screen.getByRole("button", "Open new.txt in editor");
  await file.waitFor({ state: "visible" });
  await new Promise((resolve) => setTimeout(resolve, 750));
  await app.screenshot("first-commit-net-changes");
  await expect(file).toContainText("+1 / -0");
  await expect(screen.getByRole("dialog")).not.toContainText("+2 / -1");
});
