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

const svg = '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 64 64"><rect width="64" height="64" rx="14" fill="#dc2626"/><circle cx="32" cy="32" r="18" fill="#facc15"/></svg>';

test("Encoded HTML rel tokens display the declared project SVG", WEB, async ({ t3, app, browser, screen }) => {
  const harbor = await t3.project("harbor-launch", {
    "index.html": '<link rel="ic&#111;n" href="/brand/harbor.svg">',
    "public/brand/harbor.svg": svg,
  });
  const tide = await t3.project("tidewater", {
    "index.html": '<link rel="shortcut&Tab;icon" href="/brand/tide.svg">',
    "public/brand/tide.svg": svg,
  });
  await t3.project("control-plain", {
    "index.html": '<link rel="icon" href="/brand/control.svg">',
    "public/brand/control.svg": svg,
  });
  const harborThread = await t3.thread({ project: harbor, title: "Harbor favicon proof", messages: ["Hello"] });
  const tideThread = await t3.thread({ project: tide, title: "Tidewater favicon proof", messages: ["Hello"] });
  await t3.open(harborThread);
  await screen.getByRole("combobox", "Projects").tap();
  await browser.locator('[role="option"]:has-text("control-plain") img').waitFor();
  await app.screenshot("projects-picker");
  const picker = await browser.evaluate(() => ["harbor-launch", "tidewater"].map(name => {
    const option = [...document.querySelectorAll('[role="option"]')].find(el => el.textContent?.includes(name));
    const image = option?.querySelector("img");
    return !!image && image.complete && image.naturalWidth > 0 && image.src.startsWith("data:image/svg+xml");
  }));
  await browser.keyboard.press("Escape");
  await app.screenshot("harbor-chat-header");
  const harborHeader = await browser.evaluate(() => [...document.querySelectorAll("header img")].some(img => (img as HTMLImageElement).src.startsWith("data:image/svg+xml")));
  await t3.open(tideThread);
  await screen.getByRole("combobox", "Projects").waitFor();
  await app.screenshot("tidewater-chat-header");
  const tideHeader = await browser.evaluate(() => [...document.querySelectorAll("header img")].some(img => (img as HTMLImageElement).src.startsWith("data:image/svg+xml")));
  expect({ picker, harborHeader, tideHeader }).toEqual({ picker: [true, true], harborHeader: true, tideHeader: true });
});
