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

test("PR reference links survive an embedded recording", WEB, async ({ t3, app, screen, browser }) => {
  await browser.setViewport({ width: 1280, height: 1200 });
  await t3.project("Markdown reference proof", { ".git/config": '[remote "origin"]\n  url = https://github.com/pingdotgg/t3code.git\n  fetch = +refs/heads/*:refs/remotes/origin/*\n' });
  await t3.open("/pull-requests?repository=pingdotgg%2Ft3code&number=36660&host=github.com&selectedUrl=https%3A%2F%2Fgithub.com%2Fpingdotgg%2Ft3code%2Fpull%2F36660");
  await screen.getByRole("button", "Maximize panel", { exact: true }).tap();
  await screen.getByRole("button", "Edit description").tap();
  const recording = "https://gh-file-drop-api-prod-mi5fy3sowv63ufte.pinglabs.workers.dev/f/4ac38f99d29807fc/recording.mp4";
  const cases = [
    ["use-recording-definition", `See [the docs][guide].\n\n${recording}\n\n[guide]: https://example.com/docs`],
    ["definition-recording-use", `[guide]: https://example.com/docs\n\n${recording}\n\nSee [the docs][guide].`],
    ["control-recording-outside", `See [the docs][guide].\n\n[guide]: https://example.com/docs\n\n${recording}`],
  ];
  const links: string[][] = [];
  for (const [label, text] of cases) {
    await screen.getByRole("combobox", "Pull request description").fill(text!);
    await screen.getByRole("button", "Preview", { exact: true }).tap();
    await browser.evaluate(async () => {
      const video = document.querySelector("video")!;
      const ended = new Promise<void>((resolve, reject) => {
        video.addEventListener("ended", () => resolve(), { once: true });
        video.addEventListener("error", () => reject(new Error("Fixture recording failed to load")), { once: true });
      });
      await video.play();
      await ended;
    });
    await app.screenshot(label!);
    links.push(await browser.evaluate(() => Array.from(document.querySelectorAll('a')).filter(a => a.textContent === "the docs").map(a => a.getAttribute("href")!)));
    await screen.getByRole("button", "Write", { exact: true }).tap();
  }
  await screen.getByRole("button", "Cancel", { exact: true }).tap();
  expect(links).toEqual([["https://example.com/docs"], ["https://example.com/docs"], ["https://example.com/docs"]]);
});
