252 lines · 11.1 KB
import { beforeEach, describe, expect, mock, test } from "bun:test";
import { fakeD1 } from "./fake-d1.test-helper";
import { memoryScopes } from "../scoped-db/testing";

let d1 = fakeD1();
const kv = new Map<string, string>();
const HOUR = 3600;
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),
  },
  METERING_PLANS: "",
  PUBLIC_URL: "https://forge.test",
};
mock.module("cloudflare:workers", () => ({ env: fakeEnv, waitUntil: () => undefined }));
let scopes = memoryScopes();
mock.module("../scoped-db", () => ({
  repoDb: (id: string) => scopes.repoDb(id),
  accountDb: (id: string) => scopes.accountDb(id),
  stmt: scopes.module.stmt,
}));
const accountSql = (id: string) => scopes.raw("account", id);

const { recordUsage, periodOf } = await import("./record");
const {
  reserveUsage,
  commitUsage,
  releaseUsage,
  remainingQuota,
  assertWithinQuota,
  setPlan,
  setSpendingLimit,
  periodSpendCents,
  billingSettings,
  QuotaExceededError,