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

test("Uploaded GitLab author avatar loads in the merge request list", WEB, async ({ t3, app, screen, browser }) => {
  await t3.project("Avatar demonstration", {
    ".git/config": '[core]\nrepositoryformatversion = 0\nbare = false\n[remote "origin"]\nurl = https://gitlab.com/julius54/avatar-proof-39973.git\nfetch = +refs/heads/*:refs/remotes/origin/*\n',
  });
  await t3.open("/pull-requests");
  await screen.getByText("Custom avatar demonstration", { exact: true }).waitFor({ state: "visible", timeout: 90000 });
  await browser.evaluate(async () => {
    const images = Array.from(document.images).filter(image => image.src.includes("/uploads/-/system/user/avatar/"));
    await Promise.all(images.map(image => image.complete ? Promise.resolve() : new Promise(resolve => { image.onload = resolve; image.onerror = resolve; })));
  });
  await app.screenshot("gitlab-custom-avatar");
  const loaded = await browser.evaluate(() => Array.from(document.images).some(image => image.src.startsWith("https://gitlab.com/uploads/-/system/user/avatar/") && image.complete && image.naturalWidth > 0));
  expect(loaded).toBe(true);
});
