import assert from "node:assert/strict";
import test from "node:test";

const developmentPreviewMeta =
  /<meta(?=[^>]*\bname=["']codex-preview["'])(?=[^>]*\bcontent=["']development["'])[^>]*>/i;

test("renders development preview metadata", async () => {
  const workerUrl = new URL("../dist/server/index.js", import.meta.url);
  workerUrl.searchParams.set("test", `${process.pid}-${Date.now()}`);
  const { default: worker } = await import(workerUrl.href);

  const response = await worker.fetch(
    new Request("http://localhost/", {
      headers: { accept: "text/html" },
    }),
    {
      ASSETS: {
        fetch: async () => new Response("Not found", { status: 404 }),
      },
    },
    {
      waitUntil() {},
      passThroughOnException() {},
    },
  );

  assert.equal(response.status, 200);
  assert.match(
    response.headers.get("content-type") ?? "",
    /^text\/html\b/i,
  );
  assert.match(await response.text(), developmentPreviewMeta);
});

test("renders the release controls and honest batch wording", async () => {
  const workerUrl = new URL("../dist/server/index.js", import.meta.url);
  workerUrl.searchParams.set("release-test", `${process.pid}-${Date.now()}`);
  const { default: worker } = await import(workerUrl.href);
  const response = await worker.fetch(new Request("http://localhost/"), {
    ASSETS: { fetch: async () => new Response("Not found", { status: 404 }) },
  }, { waitUntil() {}, passThroughOnException() {} });
  const html = await response.text();

  assert.match(html, /aria-label="Light mode"/);
  assert.match(html, /aria-label="Dark gray mode"/);
  assert.match(html, /aria-label="Pure black mode"/);
  assert.match(html, /data-theme="gray"/);
  assert.doesNotMatch(html, /src="\/batchforge-logo\.svg"/);
  assert.match(html, /class="site-header"/);
  assert.match(html, /Batch<span class="site-brand-accent">Forge<\/span>/);
  assert.match(html, /class="site-footer"/);
  assert.doesNotMatch(html, /site-footer-brand|site-footer-description|Local image conversion/);
  assert.match(html, /no batch cap/i);
  assert.match(html, /100% local · nothing uploaded/i);
  assert.match(html, /batchforge-icon-v3\.svg/i);
  assert.doesNotMatch(html, /Built for the work nobody has time for/i);
  assert.doesNotMatch(html, /Safe for student and school materials/i);
  assert.doesNotMatch(html, /no batch limit/i);
  assert.match(html, /href="\/guide"/);
  assert.doesNotMatch(html, /href="\/download-source"|href="\/LICENSE\.txt"/);
});

test("renders the indexed BatchForge Guide", async () => {
  const workerUrl = new URL("../dist/server/index.js", import.meta.url);
  workerUrl.searchParams.set("guide-test", `${process.pid}-${Date.now()}`);
  const { default: worker } = await import(workerUrl.href);
  const response = await worker.fetch(new Request("http://localhost/guide"), {
    ASSETS: { fetch: async () => new Response("Not found", { status: 404 }) },
  }, { waitUntil() {}, passThroughOnException() {} });
  const html = await response.text();

  assert.equal(response.status, 200);
  assert.match(html, /BatchForge Guide/);
  assert.match(html, /class="family-guide-index"/);
  assert.match(html, /href="#privacy"/);
  assert.match(html, /href="\/download-source"/);
  assert.match(html, /href="\/LICENSE\.txt"/);
});
