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

test("A primary remote named upstream pins and badges trunk as default", WEB, async ({ t3, app, browser, screen }) => {
  const project = await t3.project("Branch default demo");
  const git = (...args: string[]) => runPromise(run("git", args, { cwd: project }));
  await git("branch", "-m", "trunk");
  await git("init", "--bare", "--quiet", "../remote.git");
  await git("remote", "add", "upstream", "../remote.git");
  await git("push", "upstream", "trunk");
  await git("symbolic-ref", "refs/remotes/upstream/HEAD", "refs/remotes/upstream/trunk");
  await git("checkout", "-b", "other");
  await runPromise(run("git", ["-c", "user.name=Scenario", "-c", "user.email=scenario@example.invalid", "commit", "--allow-empty", "-m", "Newer branch"], { cwd: project, env: { ...process.env, GIT_AUTHOR_DATE: "2030-01-01T00:00:00Z", GIT_COMMITTER_DATE: "2030-01-01T00:00:00Z" } }));
  await git("checkout", "-b", "feature", "trunk");
  const thread = await t3.thread({ project, title: "Inspect upstream default branch", messages: ["Hello" ] });
  await browser.setViewport({ width: 1280, height: 800 });
  await t3.open(thread);
  await browser.locator('text="Syncing messages..."').waitFor({ state: "hidden" });
  await browser.locator('[data-slot="combobox-trigger"][aria-label^="Branch:"]:visible').first().click();
  const trunk = screen.getByRole("option", /^trunk\b/);
  await trunk.waitFor();
  try {
    await expect(trunk).toContainText("default");
    await expect(screen.getByRole("option").nth(1)).toContainText("trunk");
  } finally {
    await browser.locator('text="Syncing messages..."').waitFor({ state: "hidden" });
    await app.screenshot("upstream-default-branch-picker");
  }
});
