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

test("Global Git configuration cannot hide committable untracked files", WEB, async ({ t3, app, screen }) => {
  const project = await t3.project("demo", { "README.md": "Demo repository\n" });
  const git = (...args: string[]) => execFileSync("git", args, { cwd: project });
  git("clone", "--bare", project, `${project}-origin.git`);
  git("remote", "add", "origin", `${project}-origin.git`);
  git("push", "-u", "origin", "main");
  // The runner inherits an isolated GIT_CONFIG_GLOBAL with status.showUntrackedFiles=no.
  if (!process.env.GIT_CONFIG_GLOBAL) throw new Error("Isolated global Git config required");
  writeFileSync(`${project}/notes.md`, "New notes for the demo\n");
  const thread = await t3.thread({ project, title: "Untracked notes", messages: ["Check the demo repository"] });
  await t3.open(thread);
  const action = screen.getByRole("button", /^(Commit|Commit & push)$/);
  await action.waitFor({ state: "visible" });
  await app.screenshot("untracked-commit-action");
  await expect(action).toBeEnabled();
  await screen.getByRole("button", "Git action options").tap();
  await screen.getByRole("menuitem", "Commit", { exact: true }).tap();
  await expect(screen.getByText("notes.md", { exact: true })).toBeVisible();
  await app.screenshot("commit-file-list");
});
