/** Shared Stripe webhook test harness: env, mocks, signed event posting and plan lookups. */
import { mock } from "bun:test";
import Stripe from "stripe";
import { fakeD1 } from "../metering/fake-d1.test-helper";
import { memoryScopes } from "../scoped-db/testing";
export const state = { d1: fakeD1(), scopes: memoryScopes(), sequence: 0 };
export const kv = new Map<string, string>();
export const SECRET = "whsec_test_secret";
export const fakeEnv: Record<string, unknown> = {
get DB() {
return state.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",
STRIPE_SECRET_KEY: "sk_test_unit",
STRIPE_WEBHOOK_SECRET: SECRET,
STRIPE_PRICE_PRO: "price_pro",
STRIPE_TOPUP_PRICES: JSON.stringify({
compute_10h: "price_c",
tokens_10m: { price: "price_t" },
tokens_50m: "nope",
unknown_pack: "price_x",
}),
};
mock.module("cloudflare:workers", () => ({ env: fakeEnv, waitUntil: () => undefined }));
mock.module("../scoped-db", () => ({
repoDb: (id: string) => state.scopes.repoDb(id),
accountDb: (id: string) => state.scopes.accountDb(id),
stmt: state.scopes.module.stmt,
}));
/** The account's own SQLite (AccountState), where billing and credit rows live. */
export function accountSql(accountId: string) {
return state.scopes.raw("account", accountId);