import { expect } from "e2e";
import { unlink } from "node:fs/promises";
import { setTimeout as delay } from "node:timers/promises";
import { run } from "./shell.ts";
import { runPromise, test, WEB } from "./t3.ts";

test("Background status retains the configured upstream of an unborn branch", WEB, async ({ t3, app, screen, browser }) => {
  const project = await t3.project("unborn-upstream");
  const git = (...args: string[]) => runPromise(run("git", args, { cwd: project }));
  await git("-c", "user.name=Scenario", "-c", "user.email=scenario@example.invalid", "commit", "--allow-empty", "-m", "Second commit");
  await git("clone", "--bare", ".", "../origin.git");
  await git("remote", "add", "origin", "../origin.git");
  await git("fetch", "origin");
  await git("config", "branch.main.remote", "origin");
  await git("config", "branch.main.merge", "refs/heads/main");
  await git("update-ref", "-d", "HEAD");
  await git("rm", "-r", "--cached", "--quiet", ".");
  await unlink(`${project}/README.md`);
  const thread = await t3.thread({ project, title: "Unborn branch upstream", messages: ["Hello"], model: "default" });
  await t3.open(thread);
  const action = browser.locator('[aria-label="Git actions for unborn-upstream"] button').first();
  await action.hover();
  await delay(1000);
  await app.screenshot("full-status-after-hover");
  await delay(43000);
  await app.screenshot("background-status-after-poll");
  await expect(action).toHaveText("Commit");
});
