import { writeFile } from "node:fs/promises";
import { execFileSync } from "node:child_process";
import { expect } from "e2e";
import { test, WEB } from "./t3.ts";

test("Changing the selected PR clears a real local checkout failure", WEB, async ({ t3, app, browser, screen }) => {
  const project = await t3.project("checkout-demo", { "README.md": "Original tracked contents\n" });
  execFileSync("git", ["remote", "add", "origin", "https://github.com/pingdotgg/t3code.git"], { cwd: project });
  await writeFile(`${project}/README.md`, "Uncommitted edit\n");
  await browser.setViewport({ width: 1280, height: 800 });
  await t3.open("/");
  await browser.locator('[data-slot="combobox-trigger"][aria-label^="Branch:"]:visible').first().click();
  await screen.getByPlaceholder("Search refs...").fill("#39554");
  const checkoutLabel = /Checkout (?:pull|change) request/i;
  await screen.getByRole("option", checkoutLabel).tap();
  const dialog = screen.getByRole("dialog", checkoutLabel);
  await dialog.getByText("fix(web): scope checkout errors to the selected pull request", { exact: true }).waitFor();
  await dialog.getByRole("button", "Local", { exact: true }).tap();
  const previousError = dialog.getByText(/before checking out pull request #39554 here/);
  await previousError.waitFor();
  await app.screenshot("first-checkout-failure");
  await dialog.getByRole("textbox").fill("#39557");
  await dialog.getByText("fix(web): fold accents in thread link picker search", { exact: true }).waitFor();
  await app.screenshot("second-pull-request");
  expect(await previousError.isVisible(), "The second pull request does not display the first checkout error").toBe(false);
});
