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

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

test("renders a complete SmartCut64 product home page", async () => {
  const response = await render();
  assert.equal(response.status, 200);
  assert.match(response.headers.get("content-type") ?? "", /^text\/html\b/i);
  assert.equal(response.headers.get("strict-transport-security"), "max-age=63072000; includeSubDomains");
  assert.equal(response.headers.get("x-content-type-options"), "nosniff");
  assert.equal(response.headers.get("x-frame-options"), "DENY");
  assert.equal(response.headers.get("referrer-policy"), "strict-origin-when-cross-origin");
  assert.equal(response.headers.get("permissions-policy"), "camera=(), microphone=(), geolocation=()");
  const html = await response.text();
  assert.match(html, /<title>SmartCut64 — fast, frame-accurate Windows video editing<\/title>/i);
  assert.match(html, /Cut precisely/);
  assert.match(html, /Keep the quality/);
  assert.match(html, /Intelligent smart render/);
  assert.match(html, /Checkout is not yet enabled/);
  assert.match(html, /support@linekerpandi\.com/);
  assert.match(html, /href="\/help"/);
  assert.match(html, /href="\/formats"/);
  assert.match(html, /https:\/\/linekerpandi\.com\/smartcut64\/og\.png/);
  assert.match(html, /summary_large_image/);
  assert.doesNotMatch(html, /codex-preview|loading skeleton|Your site is taking shape/i);
  assert.doesNotMatch(html, /checkout\.lemonsqueezy\.com|lemonsqueezy\.com\/buy/i);
});

test("server-renders every public information route", async () => {
  const routes = ["/features", "/formats", "/demo", "/help", "/licensing", "/support", "/about", "/terms", "/privacy", "/refunds", "/release-readiness"];
  for (const route of routes) {
    const response = await render(route);
    assert.equal(response.status, 200, route);
    const html = await response.text();
    assert.match(html, /SmartCut64/);
    assert.match(html, /LINEKERPANDI LIMITED/);
  }
});

test("every internal page link resolves to a real route", async () => {
  const pages = ["/", "/features", "/formats", "/demo", "/help", "/licensing", "/support", "/about", "/terms", "/privacy", "/refunds", "/release-readiness"];
  const destinations = new Set(["/"]);
  for (const page of pages) {
    const response = await render(page);
    const html = await response.text();
    for (const match of html.matchAll(/href="(\/[^"#?]*)/g)) {
      const destination = match[1] || "/";
      if (!destination.startsWith("/_next/") && !/\.[a-z0-9]+$/i.test(destination)) destinations.add(destination);
    }
  }
  for (const destination of destinations) {
    const response = await render(destination);
    assert.equal(response.status, 200, `dead internal link: ${destination}`);
  }
});

test("format page distinguishes codec operations and retained evidence", async () => {
  const response = await render("/formats");
  const html = await response.text();
  assert.match(html, /Video codec support/);
  assert.match(html, /Audio codec support/);
  assert.match(html, /Container and elementary-stream support/);
  assert.match(html, /MPEG-2 Video/);
  assert.match(html, /H\.264 \/ AVC/);
  assert.match(html, /HEVC \/ H\.265/);
  assert.match(html, /AV1/);
  assert.match(html, /VP9/);
  assert.match(html, /FFV1/);
  assert.match(html, /Apple ProRes/);
  assert.match(html, /DTS \/ DTS-HD family/);
  assert.match(html, /Dolby TrueHD \/ Atmos bitstream/);
  assert.match(html, /Blu-ray LPCM/);
  assert.match(html, /19\/19/);
  assert.match(html, /20\/24-bit DVD-LPCM-to-Matroska bridge claim/);
});

test("help explains the operational workflow and safety behavior", async () => {
  const response = await render("/help");
  const html = await response.text();
  assert.match(html, /Preview Your Edits/);
  assert.match(html, /Intelligent smart render/);
  assert.match(html, /QuickStream Fix/);
  assert.match(html, /Failed validation deliberately removes/);
  assert.match(html, /Hardware acceleration/);
});

test("release page keeps checkout gated by external review", async () => {
  const response = await render("/release-readiness");
  const html = await response.text();
  assert.match(html, /No live checkout yet/);
  assert.match(html, /store approval completed/);
  assert.match(html, /controlled live purchase/);
  assert.match(html, /Independent GPL\/LGPL/);
  assert.match(html, /codec-patent exposure/);
  assert.match(html, /Artifact Signing/);
});

test("merchant-review pages state the product, policies and pre-launch status", async () => {
  const about = await (await render("/about")).text();
  assert.match(about, /Premade Windows software/);
  assert.match(about, /£4\.99/);
  assert.match(about, /does not currently operate dedicated product social-media accounts/i);

  const terms = await (await render("/terms")).text();
  assert.match(terms, /merchant of record/i);
  assert.match(terms, /two concurrently activated computers/i);
  assert.match(terms, /12 months/);

  const privacy = await (await render("/privacy")).text();
  assert.match(privacy, /raw Windows MachineGuid is not transmitted/i);
  assert.match(privacy, /No advertising, behavioural tracking or general app telemetry/i);
  assert.match(privacy, /Information Commissioner/);

  const refunds = await (await render("/refunds")).text();
  assert.match(refunds, /30-day voluntary refund/i);
  assert.match(refunds, /mandatory consumer rights/i);

  const demo = await (await render("/demo")).text();
  assert.match(demo, /smartcut64-overview\.mp4/);
  assert.match(demo, /synthetic, original test media/i);
});
