import { expect, test } from "bun:test";
import { fontCspSources, renderFontHead, renderProductionFontHead } from "../src/index.ts";

test("renders subsetted Google and escaped local font markup", () => {
  const html = renderFontHead({
    google: [{ family: "Inter", weights: [700, 400] }],
    local: [
      {
        family: 'Bad"</style><script>x</script>',
        src: '/font".woff2',
        preload: true,
      },
    ],
  });
  expect(html).toContain("family=Inter:wght@400;700");
  expect(html).not.toContain("</style><script>");
  expect(html).toContain("&quot;");
  expect(fontCspSources({ google: [{ family: "Inter" }] })).toEqual({
    style: ["https://fonts.googleapis.com"],
    font: ["https://fonts.gstatic.com"],
  });
});

test("production font head inlines Google CSS and removes its blocking stylesheet", async () => {
  const html = await renderProductionFontHead(
    { google: [{ family: "Plus Jakarta Sans", weights: [400, 700] }] },
    () => Promise.resolve(new Response('@font-face{font-family:"Plus Jakarta Sans"}')),
  );
  expect(html).toContain("data-wrnexus-google-fonts");
  expect(html).toContain("@font-face");
  expect(html).not.toContain('<link rel="stylesheet" href="https://fonts.googleapis.com');
});
