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

test("Embedded PR videos use src and ignore data-src", WEB, async ({t3, app, screen, browser}) => {
  const project = await t3.project("video-attributes");
  await runPromise(run("git", ["remote", "add", "origin", "https://dev.azure.com/t3code-factory-test/t3-factory-test/_git/t3-factory-test"], {cwd:project}));
  const thread = await t3.thread({project, title:"Video attribute evidence", messages:["Hello"]});
  await browser.emulateMedia({colorScheme:"dark"});
  await t3.open(thread);
  await t3.open("/pull-requests?q=Video%20src%20attribute%20proof%2044620");
  await screen.getByText("Video src attribute proof 44620", {exact:true}).tap();
  await screen.getByText("Case A: src first", {exact:true}).waitFor({timeout:120000});
  await browser.evaluate(async () => {
    for (const video of document.querySelectorAll('video[aria-label="Pull request video"]')) {
      const v = video as HTMLVideoElement;
      v.muted = true;
      await v.play();
      v.pause();
    }
    return null;
  });
  for (const label of ["Case A: src first", "Case B: data-src first", "Case C: data-src only"]) {
    await browser.evaluate((label) => {
      const p = [...document.querySelectorAll('p')].find(p => p.textContent === label);
      p?.scrollIntoView({block:'start'});
      for (let el = p?.parentElement; el; el = el.parentElement) {
        if (el.scrollHeight > el.clientHeight && /auto|scroll/.test(getComputedStyle(el).overflowY)) {
          el.scrollTop = Math.max(0, el.scrollTop - 70);
          break;
        }
      }
      return null;
    }, label);
    await app.screenshot(label.replaceAll(':',''));
  }
  const sources = await browser.evaluate(() => [...document.querySelectorAll<HTMLVideoElement>('video[aria-label="Pull request video"]')].map(v => v.currentSrc));
  expect(sources, "Only src attributes create videos, independent of attribute order").toEqual([
    "https://gh-file-drop-api-prod-mi5fy3sowv63ufte.pinglabs.workers.dev/f/ef3d533e99a10d31/right.mp4",
    "https://gh-file-drop-api-prod-mi5fy3sowv63ufte.pinglabs.workers.dev/f/ef3d533e99a10d31/right.mp4"
  ]);
});
