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

test("Reopening branch picker drops an externally deleted ref", WEB, async ({t3, app, browser, screen}) => {
  const project = await t3.project("repro");
  const thread = await t3.thread({project, title: "Branch picker reproduction", messages: ["Inspect this repository."]});
  const git = (...args: string[]) => runPromise(run("git", args, {cwd: project}));
  await browser.setViewport({width: 1280, height: 800});
  await t3.open(thread);
  await screen.getByRole("button", "New thread in repro", {exact: true}).click();
  const picker = browser.locator('[data-slot="combobox-trigger"][aria-label^="Branch:"]:visible').first();
  await picker.click();
  await screen.getByRole("option", /^main\b/).waitFor();
  await runPromise(Effect.sleep("6 seconds"));
  await browser.locator('input[role="combobox"]:visible').last().fill("feat");
  await screen.getByRole("option", /Create new ref/).click();
  await picker.filter({hasText: "feat"}).waitFor();
  await runPromise(Effect.sleep("6 seconds"));
  await git("checkout", "main");
  await git("branch", "-D", "feat");
  await picker.click();
  await screen.getByRole("option", /^main\b/).waitFor();
  await browser.keyboard.press("Escape");
  await runPromise(Effect.sleep("21 seconds"));
  await picker.click();
  await screen.getByRole("option", /^main\b/).waitFor();
  await app.screenshot("branch-picker-after-external-delete");
  await expect(screen.getByRole("option", /^feat\b/)).not.toBeVisible();
  await expect(screen.getByRole("option", /^main\b/)).toContainText("current");
});
