import { expect } from "e2e";
import { execFileSync } from "node:child_process";
import { test, WEB } from "./t3.ts";

const repository = "julius54/t3-media-proof-23501-2";
const api = "projects/88871160/merge_requests/1";
const description = "Synthetic private upload verification\n\n![proof](/uploads/eb14f0dd9d1e29025086e5db6e9b161d/proof.png)\n\n![proof](/uploads/ed3affc9ea5f6d85f23c13dc36665fe3/proof.mp4)\n\n- [ ] Verify private uploads\n";
const readDescription = () => JSON.parse(execFileSync("glab", ["api", api], { encoding: "utf8" })).description as string;

test("Private GitLab merge request uploads render", WEB, async ({ t3, app, screen, browser }) => {
  execFileSync("glab", ["api", api, "-X", "PUT", "-f", "description=" + description]);
  const project = await t3.project("Private media proof", { ".git/config": `[remote "origin"]\nurl = https://gitlab.com/${repository}.git\nfetch = +refs/heads/*:refs/remotes/origin/*\n` });
  await t3.thread({ project, title: "Media verification", messages: ["Prepare media verification"] });
  await t3.open();
  await screen.getByText("Private media proof", { exact: true }).first().waitFor({ state: "visible" });
  const path = "/pull-requests?repository=" + encodeURIComponent(repository) + "&number=1&host=gitlab.com&selectedUrl=" + encodeURIComponent(`https://gitlab.com/${repository}/-/merge_requests/1`);
  await app.open(path);
  await screen.getByRole("heading", "Private media verification").waitFor({ state: "visible" });
  await screen.getByRole("button", "Maximize panel").tap();
  try {
    await expect.poll(() => browser.evaluate(() => Array.from(document.querySelectorAll("img")).some(i => i.alt === "proof" && i.complete && i.naturalWidth === 480))).toBe(true);
    await expect.poll(() => browser.evaluate(() => Array.from(document.querySelectorAll("video")).some(v => v.readyState >= 2 && v.duration >= 4))).toBe(true);
  } catch (error) {
    await app.screenshot("private-upload-description");
    await screen.getByRole("button", "Show full comment").tap();
    await browser.evaluate(() => document.querySelectorAll("article")[0]?.scrollIntoView({ block: "start" }));
    await browser.locator("article [role=alert]").first().waitFor({ state: "visible" });
    await app.screenshot("comment-uploads");
    throw error;
  }
  await app.screenshot("private-upload-description");
  await browser.evaluate(async () => { const v = document.querySelector("video")!; v.muted = true; await v.play(); });
  await expect.poll(() => browser.evaluate(() => document.querySelector("video")!.currentTime)).toBeGreaterThan(1.5);
  await browser.evaluate(() => { const v = document.querySelector("video")!; v.pause(); v.currentTime = 3; });
  await expect.poll(() => browser.evaluate(() => { const v = document.querySelector("video")!; return !v.seeking && v.currentTime >= 3; })).toBe(true);
  await app.screenshot("video-seeked");
  await screen.getByRole("button", "Show full comment").tap();
  await browser.evaluate(() => document.querySelectorAll("article")[0]?.scrollIntoView({ block: "start" }));
  await expect.poll(() => browser.evaluate(() => Array.from(document.querySelectorAll("article img")).some(i => (i as HTMLImageElement).naturalWidth === 480))).toBe(true);
  await expect.poll(() => browser.evaluate(() => Array.from(document.querySelectorAll("article video")).some(v => (v as HTMLVideoElement).readyState >= 2))).toBe(true);
  await browser.evaluate(async () => { const v = document.querySelector("article video") as HTMLVideoElement; v.muted = true; await v.play(); });
  await expect.poll(() => browser.evaluate(() => (document.querySelector("article video") as HTMLVideoElement).currentTime)).toBeGreaterThan(1.5);
  await browser.evaluate(() => { const v = document.querySelector("article video") as HTMLVideoElement; v.pause(); v.currentTime = 3; });
  await expect.poll(() => browser.evaluate(() => { const v = document.querySelector("article video") as HTMLVideoElement; return !v.seeking && v.currentTime >= 3; })).toBe(true);
  await app.screenshot("comment-uploads");
  await screen.getByRole("checkbox", "Toggle task").tap();
  await expect.poll(readDescription).toBe(description.replace("- [ ]", "- [x]").trimEnd());
  await app.open(path);
  await expect(screen.getByRole("checkbox", "Toggle task")).toBeChecked();
  await expect.poll(() => browser.evaluate(() => Array.from(document.querySelectorAll("video")).some(v => v.readyState >= 2))).toBe(true);
  await app.screenshot("checkbox-persisted");
});
