/// <reference lib="dom" />
import { expect } from "e2e";
import { test, WEB } from "./t3.ts";

test("PR description previews video after fence-like numbered prose", WEB, async ({t3, app, screen, browser}) => {
  await t3.project("video-preview", {".git/config": '[remote "origin"]\n  url = https://github.com/pingdotgg/t3code.git\n  fetch = +refs/heads/*:refs/remotes/origin/*\n'});
  await browser.setViewport({width:1440,height:1000});
  await t3.open("/pull-requests?q=45277&repository=pingdotgg%2Ft3code&number=45277&selectedUrl=" + encodeURIComponent("https://github.com/pingdotgg/t3code/pull/45277"));
  await screen.getByRole("button", "Edit description", {exact:true}).tap();
  await browser.locator('textarea[aria-label="Pull request description"]').fill("Intro\n2. ~~~\n   https://interactive-examples.mdn.mozilla.net/media/cc0-videos/flower.mp4");
  await screen.getByRole("button", "Preview", {exact:true}).tap();
  await screen.getByText("Intro", {exact:false}).waitFor();
  await screen.getByRole("status", /^Loading pull requests/).waitFor({state:"hidden", timeout:120_000});
  await browser.evaluate(async () => {
    const video = document.querySelector("video");
    if (video && video.readyState < 2 && !video.error) {
      await new Promise<void>((resolve) => {
        video.addEventListener("loadeddata", () => resolve(), {once:true});
        video.addEventListener("error", () => resolve(), {once:true});
        video.preload = "auto";
        video.load();
      });
    }
  });
  await app.screenshot("video-preview");
  await expect(browser.locator("video")).toBeVisible();
  await screen.getByRole("button", "Cancel", {exact:true}).tap();
});
