import { execFileSync } from "node:child_process";
import { expect } from "e2e";
import { test, WEB, } from "./t3.ts";
import { APP_DIR } from "../e2e.config.ts";

test("Dismissing PR checkout preparation cancels the fix prompt", WEB, async ({t3, app, screen, browser}) => {
  const project = await t3.project("toast-cancellation");
  const git = (...args: string[]) => execFileSync("git", args, {cwd: project, encoding:"utf8"});
  git("remote", "add", "origin", "https://github.com/pingdotgg/t3code.git");
  // Reuse already-fetched public git objects to avoid another full repository download.
  git("fetch", APP_DIR, "d1a0de38f47bbed2fa8a8899cdeb562337c05b4b");
  git("reset", "--hard", "FETCH_HEAD");
  await t3.thread({project, title:"Cancel PR fix", messages:["Give me a short tour"], model:"showcase"});
  await t3.open();
  await browser.locator("[data-thread-item]").filter({hasText:"Cancel PR fix"}).tap();
  await browser.keyboard.press("Control+k");
  await screen.getByRole("dialog").getByRole("combobox").fill("Link pull request to thread");
  await screen.getByText("Link pull request to thread", {exact:true}).tap();
  await screen.getByPlaceholder("Pull request URL or #42").fill("https://github.com/pingdotgg/t3code/pull/34494");
  await screen.getByRole("button", "Link", {exact:true}).tap();
  await app.screenshot("linked-pr");
  await screen.getByRole("button", "Fix", {exact:true}).tap();
  await screen.getByText("Preparing the pull request checkout...", {exact:true}).tap();
  await screen.getByRole("button", "Dismiss notification").last().tap();
  // Observe the completed real checkout, including any delayed handoff to the draft.
  await new Promise(resolve => setTimeout(resolve, 20000));
  await app.screenshot("dismissed-checkout");
  await expect(screen.getByRole("textbox")).toHaveText("");
});
