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

test("PR number search keeps the synced title for a files-tab link", WEB, async ({ t3, app, browser, screen }) => {
  const project = await t3.project("Search proof");
  await runPromise(run("git", ["remote", "add", "origin", "https://github.com/pingdotgg/t3code.git"], { cwd: project }));
  await runPromise(run("git", ["remote", "add", "fork", "https://github.com/saphid/t3code.git"], { cwd: project }));
  await runPromise(run("git", ["fetch", "fork", "hunter/c-bd333e2db8"], { cwd: project }));
  await runPromise(run("git", ["checkout", "-b", "hunter/c-bd333e2db8", "--track", "fork/hunter/c-bd333e2db8"], { cwd: project }));
  const thread = await t3.thread({ project, title: "Unrelated notes", messages: ["Start"] });
  await browser.setViewport({ width: 1440, height: 1000 });
  await t3.open(thread);
  await screen.getByText("Start", { exact: true }).waitFor({ timeout: 30000 });
  const palette = screen.getByRole("dialog", "Command palette");
  await browser.keyboard.press("Control+k");
  await palette.getByRole("combobox").fill("#47471");
  await palette.getByText("Unrelated notes", { exact: true }).waitFor({ timeout: 30000 });
  await app.screenshot("branch-number-before-link");
  await palette.getByRole("combobox").fill("> Link pull request to thread");
  await palette.getByText("Link pull request to thread", { exact: true }).tap();
  const link = screen.getByRole("dialog", "Link pull request", { exact: true });
  await link.getByRole("textbox").fill("https://github.com/pingdotgg/t3code/pull/47471/files");
  await link.getByRole("button", "Link", { exact: true }).tap();
  await link.waitFor({ state: "hidden" });
  await browser.keyboard.press("Control+k");
  await palette.getByRole("combobox").fill("thread search keeps the PR title");
  await palette.getByText("Unrelated notes", { exact: true }).waitFor({ timeout: 60000 });
  await palette.getByRole("combobox").fill("https://github.com/pingdotgg/t3code/pull/47471");
  await palette.getByText("Unrelated notes", { exact: true }).waitFor();
  await app.screenshot("canonical-url-search");
  await palette.getByRole("combobox").fill("https://github.com/pingdotgg/t3code/pull/47471/files");
  await palette.getByText("Unrelated notes", { exact: true }).waitFor();
  await app.screenshot("files-url-search");
  await palette.getByRole("combobox").fill("#47471");
  await palette.getByText("Unrelated notes", { exact: true }).waitFor();
  await app.screenshot("number-search-with-linked-title");
  await expect(palette.getByRole("option").filter({ hasText: "Unrelated notes" })).toContainText("PR: #47471 fix(shared): thread search keeps the PR title");
});
