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

test("Untracked files remain committable when Git configuration hides them", 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");
  git("config", "status.showUntrackedFiles", "no");
  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);
  await screen.getByText("first fixture turn complete", { exact: true }).waitFor({ state: "visible" });
  await screen.getByRole("button", /^Changes/).tap();
  await screen.getByText("notes.md", { exact: false }).first().waitFor({ state: "visible" });
  await app.screenshot("diff-includes-untracked-notes");
  await screen.getByRole("button", "Toggle right panel", { exact: true }).tap();
  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");
});
