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

test("Route favicon resolves an escaped apostrophe", WEB, async ({ t3, app, browser, screen }) => {
  const svg = '<svg xmlns="http://www.w3.org/2000/svg" width="64" height="64" viewBox="0 0 64 64"><rect width="64" height="64" rx="12" fill="#e52b36"/><circle cx="32" cy="32" r="18" fill="#ffd43b"/></svg>';
  const escaped = await t3.project("escaped", {
    "public/brand/it's.svg": svg,
    "src/routes/__root.tsx": String.raw`const links = [{ 'rel': 'icon', 'href': '/brand/it\'s.svg' }];`,
  });
  await t3.project("control", {
    "public/brand/it's.svg": svg,
    "src/routes/__root.tsx": `const links = [{ 'rel': 'icon', 'href': "/brand/it's.svg" }];`,
  });
  const thread = await t3.thread({ project: escaped, title: "Favicon comparison", messages: ["Start"] });
  await t3.open(thread);
  await screen.getByRole("combobox", "Projects", { exact: true }).tap();
  await screen.getByRole("combobox", "Search projects").waitFor();
  await browser.locator('[role="option"]:has-text("control") img').waitFor();
  try {
    await expect.poll(() => browser.evaluate(() => {
      const row = [...document.querySelectorAll('[role="option"]')].find(el => el.textContent?.includes("escaped"));
      const image = row?.querySelector("img");
      return Boolean(image?.complete && image.naturalWidth > 0);
    }), { message: "The escaped project displays its inferred SVG favicon" }).toBe(true);
  } finally {
    await app.screenshot("projects-route-favicon");
  }
});
