105 lines · 4.6 KB
import { describe, expect, mock, test } from "bun:test";

mock.module("cloudflare:workers", () => ({ env: {} }));

const { deploymentProblems, isForgeRequestHost, isLocalDev, isLoopbackUrl, requestProblems } =
  await import("./deployment");

const GOOD = {
  PUBLIC_URL: "https://gitfor.dev",
  USERCONTENT_ORIGIN: "https://gitforusercontent.dev",
  SECRETS_KEY: "k".repeat(32),
  ARTIFACTS_SHARDS: "1",
};
const onlyArtifacts = (name: string) => name === "ARTIFACTS";

describe("deploymentProblems (DEPLOY-P2)", () => {
  test("a correctly configured deployment serves", () => {
    expect(deploymentProblems(GOOD, onlyArtifacts)).toEqual([]);
  });

  test("loopback, plain-http or empty PUBLIC_URL is refused when deployed", () => {
    expect(deploymentProblems({ ...GOOD, PUBLIC_URL: "http://localhost:3000" }, onlyArtifacts)).toEqual(
      expect.arrayContaining(["PUBLIC_URL must use https", "PUBLIC_URL must not be a loopback host"]),
    );
    expect(deploymentProblems({ ...GOOD, PUBLIC_URL: "http://gitfor.dev" }, onlyArtifacts)).toContain(
      "PUBLIC_URL must use https",
    );
    expect(deploymentProblems({ ...GOOD, PUBLIC_URL: "" }, onlyArtifacts)).toContain(
      "PUBLIC_URL is not set to a valid URL",
    );
  });

  test("missing or same-origin USERCONTENT_ORIGIN is refused", () => {
    expect(deploymentProblems({ ...GOOD, USERCONTENT_ORIGIN: "" }, onlyArtifacts)).toContain(
      "USERCONTENT_ORIGIN must be set to a separate origin",
    );
    expect(deploymentProblems({ ...GOOD, USERCONTENT_ORIGIN: "https://gitfor.dev" }, onlyArtifacts)).toContain(
      "USERCONTENT_ORIGIN must not be the forge host",
    );
  });