161 lines · 6.8 KB
import { beforeEach, describe, expect, mock, test } from "bun:test";
import { fakeD1 } from "../metering/fake-d1.test-helper";
import { memoryScopes } from "../scoped-db/testing";
import { CUSTOM_DOMAIN_LIMIT } from "~/lib/pricing";

let d1 = fakeD1();
const fakeEnv: Record<string, unknown> = {
  get DB() {
    return d1.binding;
  },
  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,
}));

/** A site in its scope (the repository's object, or `site:<id>` when it has no repository), with its locator. */
function seedSite(id: string, domain: string, repoId: string | null = null) {
  const scope = repoId ?? `site:${id}`;
  scopes.raw("repo", scope).run(
    `INSERT INTO sites (id, owner_id, repo_id, label, source, custom_domain, created_at, updated_at)
       VALUES (?, 'u1', ?, ?, 'upload', ?, 0, 0)`,
    [id, repoId, id, domain],
  );
  d1.db.run("INSERT INTO scope_locators (kind, key, scope_id) VALUES ('site', ?, ?)", [id, scope]);
}
const cancelled: string[] = [];
mock.module("./config", () => ({
  stripeEnabled: () => Boolean(fakeEnv.STRIPE_SECRET_KEY),
  stripe: () => ({
    subscriptions: {
      retrieve: async (id: string) => ({ id, status: id === "sub_gone" ? "canceled" : "active" }),
      cancel: async (id: string) => {
        if (id === "sub_missing") throw new Error("resource_missing");
        cancelled.push(id);