227 lines · 10.1 KB
import { beforeEach, describe, expect, mock, test } from "bun:test";
import { fakeD1 } from "./metering/fake-d1.test-helper";

let d1 = fakeD1();
const kv = new Map<string, string>();
const fakeEnv: Record<string, unknown> = {
  get DB() {
    return d1.binding;
  },
  KV: {
    get: async (key: string) => kv.get(key) ?? null,
    put: async (key: string, value: string) => void kv.set(key, value),
    delete: async (key: string) => void kv.delete(key),
  },
  PUBLIC_URL: "https://forge.test",
  METERING_PLANS: "",
  SUPPORT_EMAIL: "",
  LEGAL_CONTACT_EMAIL: "legal@forge.test",
};
mock.module("cloudflare:workers", () => ({ env: fakeEnv, waitUntil: () => undefined }));

const cancelled: string[] = [];
let cancelError: Error | null = null;
const realConfig = await import("./billing/config");
const realEmail = await import("./email");
mock.module("./billing/config", () => ({
  ...realConfig,
  stripeEnabled: () => true,
  stripe: () => ({
    subscriptions: {
      retrieve: async (id: string) => ({ id, status: cancelled.includes(id) ? "canceled" : "active" }),
      cancel: async (id: string, options: { prorate: boolean }) => {
        if (cancelError) throw cancelError;
        expect(options.prorate).toBe(false);
        cancelled.push(id);
      },
    },
  }),
}));
const stoppedContainers: string[] = [];