import assert from "node:assert/strict";
import test from "node:test";
import {
  assertPublicResponse,
  checkPublicAccess,
} from "../../scripts/check-public-access.mjs";

test("accepts eight clean public responses", async () => {
  let requestCount = 0;
  const result = await checkPublicAccess("https://hearbooks.example", {
    attempts: 8,
    fetchImpl: async () => {
      requestCount += 1;
      return new Response("<main>HearBooks</main>", { status: 200 });
    },
  });

  assert.equal(requestCount, 8);
  assert.equal(result.attempts, 8);
});

test("rejects HTTP errors and Cloudflare block pages", () => {
  assert.throws(
    () => assertPublicResponse({ status: 403, body: "Forbidden" }),
    /HTTP 403/,
  );
  assert.throws(
    () => assertPublicResponse({ status: 200, body: "Sorry, you have been blocked" }),
    /blocked/i,
  );
});
