import assert from "node:assert/strict";
import { readFile } from "node:fs/promises";
import test from "node:test";
import {
  PAGES_PROJECT,
  buildPagesConfig,
} from "../../scripts/cloudflare-pages-config.mjs";

const databaseId = "123e4567-e89b-42d3-a456-426614174000";

test("builds an isolated Pages Advanced Mode config", () => {
  const config = buildPagesConfig({ databaseId });
  assert.equal(PAGES_PROJECT, "hearbooks-reader-pages");
  assert.deepEqual(config, {
    $schema: "../node_modules/wrangler/config-schema.json",
    name: "hearbooks-reader-pages",
    pages_build_output_dir: "../dist/pages",
    compatibility_date: "2026-08-28",
    compatibility_flags: ["nodejs_compat"],
    d1_databases: [{
      binding: "DB",
      database_name: "hearbooks-reader-db",
      database_id: databaseId,
      migrations_dir: "../drizzle",
    }],
    r2_buckets: [{
      binding: "STORAGE",
      bucket_name: "hearbooks-reader-storage",
    }],
  });
  assert.doesNotMatch(JSON.stringify(config), /reader-1|site-creator|chatgpt\.site|IMAGES/);
});

test("the checked-in Pages config equals the deterministic config", async () => {
  const production = JSON.parse(await readFile(
    new URL("../../wrangler.production.jsonc", import.meta.url),
    "utf8",
  ));
  const actual = JSON.parse(await readFile(
    new URL("../../pages/wrangler.jsonc", import.meta.url),
    "utf8",
  ));
  assert.deepEqual(actual, buildPagesConfig({
    databaseId: production.d1_databases[0].database_id,
  }));
});
