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

const icon = '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 32 32"><rect width="32" height="32" rx="6" fill="#e22"/><circle cx="16" cy="16" r="10" fill="#ffe000"/></svg>';

test("A percent-encoded favicon href displays its local SVG", WEB, async ({ t3, app, screen, browser }) => {
  const project = await t3.project("Team site", {
    "index.html": '<!doctype html><link rel="icon" href="/team%20mark.svg">',
    "public/team mark.svg": icon,
  });
  const control = await t3.project("Control site", {
    "index.html": '<!doctype html><link rel="icon" href="/team-mark.svg">',
    "public/team-mark.svg": icon,
  });
  const thread = await t3.thread({ project, title: "Inspect Team site favicon", messages: ["Start"] });
  const controlThread = await t3.thread({ project: control, title: "Inspect control favicon", messages: ["Start"] });
  await browser.setViewport({ width: 1280, height: 800 });
  await t3.open(controlThread);
  await screen.getByText("fixture turn complete", { exact: false }).waitFor();
  await t3.open(thread);
  await screen.getByText("fixture turn complete", { exact: false }).waitFor();
  try {
    await expect.poll(() => browser.evaluate(() => Array.from(document.querySelectorAll<HTMLImageElement>('nav[aria-label="Thread breadcrumb"] img')).some(img => img.complete && img.naturalWidth > 0 && img.getBoundingClientRect().width > 0))).toBe(true);
  } finally {
    await app.screenshot("team-site-favicon");
  }
});
