import { writeFile, chmod } from "node:fs/promises";
import { expect } from "e2e";
import { run } from "./shell.ts";
import { runPromise, test, WEB } from "./t3.ts";

test("A slashed remote immediately shows the local branch name during checkout", WEB, async ({ t3, app, browser, screen }) => {
  const project = await t3.project("remote-label-demo");
  const git = (...args: string[]) => runPromise(run("git", args, { cwd: project }));
  await git("branch", "feature");
  await git("clone", "--bare", ".", "../remote.git");
  await git("branch", "-D", "feature");
  await git("remote", "add", "team/origin", "../remote.git");
  await git("fetch", "team/origin");
  // Keep the real checkout pending long enough to inspect its optimistic toolbar label.
  await writeFile(`${project}/.git/hooks/post-checkout`, "#!/bin/sh\nsleep 30\n");
  await chmod(`${project}/.git/hooks/post-checkout`, 0o755);
  const thread = await t3.thread({ project, title: "Remote branch checkout", messages: ["Hello"] });
  await browser.setViewport({ width: 1280, height: 800 });
  await t3.open(thread);
  const picker = browser.locator('[data-slot="combobox-trigger"][aria-label^="Branch:"]:visible').first();
  await picker.click();
  await screen.getByRole("option", /^team\/origin\/\s*feature\b/).tap();
  await browser.locator('[data-slot="combobox-trigger"][aria-label="Branch: feature"], [data-slot="combobox-trigger"][aria-label="Branch: origin/feature"]').first().waitFor({ state: "visible" });
  await app.screenshot("pending-remote-checkout-label");
  await expect(picker).toHaveText("feature");
});
