import { beforeEach, describe, expect, mock, test } from "bun:test";
import { fakeD1 } from "./fake-d1.test-helper";
import { memoryScopes } from "../scoped-db/testing";
import { DEFAULT_PRO_SPENDING_LIMIT_CENTS } from "~/lib/plans";
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),
},
USAGE: { writeDataPoint: () => undefined },
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 counterRows = new Map<string, [number, number, number]>();
mock.module("../counters", () => ({
addCounter: async (kind: string, scope: string, key: string, delta: [number, number, number]) => {
const id = `${kind}|${scope}|${key}`;
const current = counterRows.get(id) ?? [0, 0, 0];
const next: [number, number, number] = [current[0] + delta[0], current[1] + delta[1], current[2] + delta[2]];
counterRows.set(id, next);
return next;
},
counterTotals: async (kind: string, scope: string, key: string) =>
counterRows.get(`${kind}|${scope}|${key}`) ?? [0, 0, 0],
}));