import { expect } from "e2e";
import { run } from "./shell.ts";
import { runPromise, test, WEB } from "./t3.ts";

test("A worktree starts from the exact remote branch ending in U+3000", WEB, async ({ t3, app, browser, screen }) => {
  const project = await t3.project("unicode-branch", { "README.md": "Synthetic branch-name reproduction\n" });
  const git = (...args: string[]) => runPromise(run("git", args, { cwd: project }));
  const remote = `${project}/../origin.git`;
  await git("init", "--bare", remote);
  await git("remote", "add", "origin", remote);
  await git("push", "origin", "HEAD:refs/heads/release/v2\u3000");
  await t3.open("/");
  await browser.locator('[data-composer-shortcut~="composer.workspace"]:visible').first().click();
  await screen.getByText("New worktree", { exact: true }).tap();
  await browser.locator('[data-slot="combobox-trigger"][aria-label^="Branch:"]:visible').first().click();
  await screen.getByRole("option", /origin\/release\/\s*v2/).tap();
  await screen.getByRole("textbox").fill("Show the worktree base");
  await screen.getByRole("textbox").press("Enter");
  try {
    await expect(screen.getByText("This turn is a replay.", { exact: false }).first()).toBeVisible({ timeout: 60000 });
    await screen.getByRole("button", "Stop generation").waitFor({ state: "hidden" });
  } finally {
    await app.screenshot("remote-branch-worktree-result");
  }
});
