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

test("Unsent pull request comment survives leaving and returning", WEB, async ({ t3, app, screen, browser }) => {
  await t3.project("draft-proof", { ".git/config": '[remote "origin"]\nurl = https://github.com/pingdotgg/t3code.git\nfetch = +refs/heads/*:refs/remotes/origin/*\n' });
  await t3.open("/pull-requests?state=all&q=repo%3Apingdotgg%2Ft3code%20%2347941%20%2347774");
  const openPr = async (number: number) => {
    await app.open(`/pull-requests?state=all&repository=pingdotgg%2Ft3code&number=${number}&selectedUrl=${encodeURIComponent(`https://github.com/pingdotgg/t3code/pull/${number}`)}`);
    await screen.getByRole("button", /^(Comment on|Review) pull request$/).waitFor({ timeout: 60000 });
  };
  await openPr(47941);
  await screen.getByRole("button", "Comment on pull request", { exact: true }).click();
  const comment = browser.locator('textarea[aria-label="Comment on this pull request"]');
  await comment.fill("Unsent comment draft for repro 49137");
  await browser.locator('[role="status"][aria-label="Loading pull requests…"]').waitFor({ state: "hidden", timeout: 120000 });
  await app.screenshot("draft-before-leaving");
  await screen.getByRole("button", "Review", { exact: true }).click();
  await browser.locator('textarea[aria-label="Review summary"]').fill("Control review summary 49137");
  await screen.getByRole("button", "Close composer", { exact: true }).click();
  await openPr(47774);
  await openPr(47941);
  await screen.getByRole("button", "Review pull request", { exact: true }).click();
  await screen.getByRole("button", "Comment", { exact: true }).click();
  await browser.locator('[role="status"][aria-label="Loading pull requests…"]').waitFor({ state: "hidden", timeout: 120000 });
  await app.screenshot("comment-after-returning");
  await expect(comment).toHaveValue("Unsent comment draft for repro 49137");
});
