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

test("Localdomain links never disclose hosts to the favicon service", WEB, async ({ t3, app, screen, browser }) => {
  const requests: string[] = [];
  await browser.route("https://www.google.com/s2/favicons?**", async (route) => {
    requests.push(route.request.url);
    await route.continue();
  });
  const project = await t3.project("Favicon privacy");
  const thread = await t3.thread({ project, title: "Localdomain favicon proof", model: "showcase", messages: ["Start"] });
  await t3.open(thread);
  await screen.getByRole("textbox").fill("Links: [local](http://localhost.localdomain:8080/) [devbox](http://devbox.localdomain:3000/) [public](https://example.com/)");
  await screen.getByRole("textbox").press("Enter");
  await screen.getByRole("button", "Stop generation").waitFor({ state: "visible" });
  await screen.getByRole("link", "local", { exact: true }).waitFor();
  await screen.getByRole("link", "devbox", { exact: true }).waitFor();
  await screen.getByRole("link", "public", { exact: true }).waitFor();
  await screen.getByRole("button", "Stop generation").waitFor({ state: "hidden" });
  await browser.evaluate(async () => {
    const links = Array.from(document.querySelectorAll("a")).filter(a => ["http://localhost.localdomain:8080/", "http://devbox.localdomain:3000/", "https://example.com/"].includes(a.href));
    await Promise.all(links.flatMap(a => Array.from(a.querySelectorAll("img"))).map(img => img.complete ? Promise.resolve() : new Promise<void>(resolve => { img.addEventListener("load", () => resolve(), { once: true }); img.addEventListener("error", () => resolve(), { once: true }); })));
    links[0]?.scrollIntoView({ block: "center" });
  });
  const observations = await browser.evaluate(() => ["http://localhost.localdomain:8080/", "http://devbox.localdomain:3000/", "https://example.com/"].map(href => {
    const a = Array.from(document.querySelectorAll("a")).find(a => a.href === href)!;
    const img = a.querySelector("img");
    return { host: new URL(href).hostname, src: img?.getAttribute("src") ?? null, complete: img?.complete ?? null, naturalWidth: img?.naturalWidth ?? null, globe: a.querySelector("svg.lucide-globe") !== null };
  }));
  await browser.evaluate(({ observations, requests }) => {
    const panel = document.createElement("aside");
    panel.id = "favicon-proof-inspection";
    panel.style.cssText = "position:fixed;bottom:12px;left:270px;right:12px;z-index:99999;padding:16px;background:#101820;color:#eef5ff;border:1px solid #7b98ad;border-radius:8px;font:13px/1.6 monospace;white-space:pre-wrap;pointer-events:none";
    panel.textContent = "BROWSER INSPECTION — rendered links and real network requests\n" + observations.map(o => `${o.host}: ${o.globe ? "GlobeIcon fallback" : "img"}\n  src=${o.src ?? "none"}; complete=${o.complete}; naturalWidth=${o.naturalWidth}`).join("\n") + "\nGoogle favicon requests forwarded to network: " + requests.length + "\n" + requests.join("\n");
    document.body.append(panel);
  }, { observations, requests });
  await app.screenshot("favicon-privacy");
  expect({ observations, requests }).toEqual({
    observations: [
      { host: "localhost.localdomain", src: null, complete: null, naturalWidth: null, globe: true },
      { host: "devbox.localdomain", src: null, complete: null, naturalWidth: null, globe: true },
      { host: "example.com", src: "https://www.google.com/s2/favicons?domain=example.com&sz=32", complete: true, naturalWidth: 16, globe: false },
    ],
    requests: ["https://www.google.com/s2/favicons?domain=example.com&sz=32"],
  });
});
