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

test("PR number search retains the synced title beside the branch PR", WEB, async ({t3, app, browser, screen}) => {
  const project = await t3.project("onboarding");
  const git = (args: string[]) => runPromise(run("git", args, {cwd: project}));
  await git(["remote", "add", "origin", "https://github.com/pingdotgg/t3code.git"]);
  await git(["checkout", "-b", "factory/issue-44420"]);
  await git(["config", "branch.factory/issue-44420.remote", "origin"]);
  await git(["config", "branch.factory/issue-44420.merge", "refs/heads/factory/issue-44420"]);
  await git(["update-ref", "refs/remotes/origin/factory/issue-44420", "HEAD"]);
  const thread = await t3.thread({project, title:"Tidy Onboarding Copy", messages:["Tidy up the onboarding copy"], model:"multi-turn"});
  await browser.setViewport({width:1440,height:900});
  await t3.open(thread);
  await screen.getByRole("textbox", "Message Tidy Onboarding Copy").waitFor();
  await browser.keyboard.press("Control+k");
  const palette = screen.getByRole("dialog", "Command palette");
  await palette.getByRole("combobox").fill("> Link pull request to thread");
  await palette.getByText("Link pull request to thread", {exact:true}).tap();
  await screen.getByPlaceholder("Pull request URL or #42").fill("https://github.com/pingdotgg/t3code/pull/44460");
  await screen.getByRole("button", "Link", {exact:true}).tap();
  await screen.getByRole("dialog", "Link pull request", {exact:true}).waitFor({state:"hidden"});
  await browser.keyboard.press("Control+k");
  await palette.getByRole("combobox").fill("worktree setup links");
  await palette.getByText(/PR: #44460 fix\(server\): worktree setup links/).waitFor();
  await palette.getByRole("combobox").fill("#44460");
  await palette.getByText("Tidy Onboarding Copy", {exact:true}).waitFor();
  await palette.getByText("Searching messages…", {exact:true}).waitFor({state:"hidden"});
  await app.screenshot("pr-number-search");
  await expect(palette).toContainText("PR: #44460 fix(server): worktree setup links");
});
