import { expect } from "e2e";
import { readdir, writeFile } from "node:fs/promises";
import { execFileSync } from "node:child_process";
import { join } from "node:path";
import { T3_HOME } from "../e2e.config.ts";
import { test, WEB } from "./t3.ts";

test("Generated folder and commit branch names preserve German and Vietnamese words", WEB, async ({ t3, app, screen, browser }) => {
  const project = await t3.project("Naming demo");
  const thread = await t3.thread({ project, title: "Generated branch names", messages: ["Ready to verify names"] });
  await t3.open(thread);
  await screen.getByRole("button", "Add project", { exact: true }).tap();
  await screen.getByText("New project", { exact: true }).tap();
  await screen.getByRole("combobox").fill("Straßenbahn Planer");
  const preview = await screen.getByText(/^Creates /).textContent();
  await app.screenshot("folder-name-preview");
  await expect.soft(preview).toContain("strassenbahn-planer");
  await screen.getByRole("button", /^Create \(/).tap();
  // Wait for the palette to close before inspecting the real folder it created.
  await screen.getByRole("combobox", "Project name", { exact: true }).waitFor({ state: "hidden" });
  await app.screenshot("created-folder");
  const folders = await readdir(join(T3_HOME, "projects"));
  await expect.soft(folders).toContain("strassenbahn-planer");
  await t3.open(thread);
  execFileSync("git", ["config", "user.name", "Scenario"], { cwd: project });
  execFileSync("git", ["config", "user.email", "scenario@example.invalid"], { cwd: project });
  for (const [file, message, expected, label] of [
    ["notes.txt", "Größe der Straße korrigieren", "feature/grosse-der-strasse-korrigieren", "german-branch"],
    ["second.txt", "Đặt tên dự án", "feature/dat-ten-du-an", "vietnamese-branch"],
  ]) {
    await writeFile(join(project, file!), message!);
    await screen.getByRole("button", file === "notes.txt" ? /^Changes .*\+1 -0$/ : /^Changes .*\+2 -0$/).waitFor({ state: "visible" });
    await screen.getByRole("button", "Git action options", { exact: true }).tap();
    await screen.getByRole("menuitem", "Commit", { exact: true }).press("Enter");
    await screen.getByPlaceholder("Leave empty to auto-generate").fill(message!);
    await app.screenshot(`${label}-commit-message`);
    await screen.getByRole("button", "Commit on new branch", { exact: true }).tap();
    await screen.getByPlaceholder("Leave empty to auto-generate").waitFor({ state: "hidden" });
    // The branch control updates after the actual git operation completes.
    await screen.getByText(file === "notes.txt" ? /feature\/gro/ : /feature\/(?:dat|at)-ten/).first().waitFor({ state: "visible" });
    const branch = execFileSync("git", ["branch", "--show-current"], { cwd: project, encoding: "utf8" }).trim();
    await screen.getByText(branch, { exact: true }).first().waitFor({ state: "visible" });
    await screen.getByRole("combobox", `Branch: ${branch}`, { exact: true }).waitFor({ state: "visible" });
    await screen.getByRole("combobox", `Branch: ${branch}`, { exact: true }).tap();
    await app.screenshot(label!);
    await browser.keyboard.press("Escape");
    await expect.soft(branch).toBe(expected);
  }
  await writeFile(join(project, "proof.txt"), "Inspect the generated branch without changing it.\n");
  await t3.open(thread);
  await screen.getByRole("button", "Git action options", { exact: true }).tap();
  await screen.getByRole("menuitem", "Commit", { exact: true }).press("Enter");
  await screen.getByPlaceholder("Leave empty to auto-generate").waitFor({ state: "visible" });
  await app.screenshot("vietnamese-branch-details");
  await screen.getByRole("button", "Cancel", { exact: true }).tap();
});
