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

test("Automatic favicon survives quoted angle brackets before href", WEB, async ({ t3, app, screen, browser }) => {
  const svg = '<svg xmlns="http://www.w3.org/2000/svg" width="32" height="32" viewBox="0 0 32 32"><rect width="32" height="32" rx="5" fill="#dc2626"/><path d="M16 3L29 27H3Z" fill="#fde047"/><circle cx="16" cy="20" r="4" fill="#dc2626"/></svg>';
  for (const [title, media] of [["control-app", ""], ["beacon-app", ' media="(width > 600px)"'], ["less-app", " media='(width < 600px)'"]]) {
    await t3.project(title, { "brand/beacon.svg": svg, "index.html": `<html><head><link rel="icon"${media} href="./brand/beacon.svg"></head><body></body></html>` });
  }
  await browser.emulateMedia({ colorScheme: "dark" });
  await t3.open("/settings/projects");
  const rendered: Record<string, boolean> = {};
  let scope = "All projects";
  for (const title of ["control-app", "beacon-app", "less-app"]) {
    await screen.getByRole("button", `Project scope: ${scope}`).tap();
    await screen.getByRole("menuitemradio").filter({ hasText: title }).tap();
    await screen.getByText("Automatic", { exact: true }).waitFor();
    await screen.getByRole("button", "Choose a project icon", { exact: true }).waitFor();
    await app.screenshot(title);
    rendered[title] = await browser.evaluate(() => {
      const button = document.querySelector('button[aria-label="Choose a project icon"]');
      return !!button?.parentElement?.querySelector('img:not(.hidden)');
    });
    scope = title;
  }
  expect(rendered).toEqual({ "control-app": true, "beacon-app": true, "less-app": true });
});
