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

const fakeEnv: Record<string, string | undefined> = {};
mock.module("cloudflare:workers", () => ({ env: fakeEnv }));

const { checkoutDisabled, liveMode, stripeEnabled, stripeModeError } = await import("./config");

describe("stripe key modes", () => {
  test("matching test or live keys pass", () => {
    expect(stripeModeError({ secret: "sk_test_a", publishable: "pk_test_b", webhookMode: "test" })).toBeNull();
    expect(stripeModeError({ secret: "rk_live_a", publishable: "pk_live_b" })).toBeNull();
    expect(stripeModeError({})).toBeNull();
  });

  test("mixed modes and unknown prefixes are refused", () => {
    expect(stripeModeError({ secret: "sk_live_a", publishable: "pk_test_b" })).toMatch(/pk_live_/);
    expect(stripeModeError({ secret: "sk_test_a", publishable: "pk_live_b" })).toMatch(/pk_test_/);
    expect(stripeModeError({ secret: "sk_test_a", webhookMode: "live" })).toMatch(/STRIPE_WEBHOOK_MODE/);
    expect(stripeModeError({ secret: "whsec_oops" })).toMatch(/STRIPE_SECRET_KEY/);
  });

  test("a mixed configuration disables billing", () => {
    Object.assign(fakeEnv, { STRIPE_SECRET_KEY: "sk_live_a", STRIPE_PUBLISHABLE_KEY: "pk_test_b" });
    expect(stripeEnabled()).toBe(false);
    fakeEnv.STRIPE_PUBLISHABLE_KEY = "pk_live_b";
    expect(stripeEnabled()).toBe(true);
    expect(liveMode()).toBe(true);
  });

  test("BILLING_CHECKOUT_DISABLED", () => {
    expect(checkoutDisabled()).toBe(false);
    fakeEnv.BILLING_CHECKOUT_DISABLED = "true";
    expect(checkoutDisabled()).toBe(true);
    fakeEnv.BILLING_CHECKOUT_DISABLED = "0";
    expect(checkoutDisabled()).toBe(false);
  });
});