/** Shared metering test harness: env, analytics mock, module handles and the fixture accounts. */
import { mock } from "bun:test";
import { fakeD1 } from "./fake-d1.test-helper";
import { memoryScopes } from "../scoped-db/testing";
export const state = { d1: fakeD1(), scopes: memoryScopes() };
export const points: unknown[] = [];
export const kv = new Map<string, string>();
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),
},
USAGE: { writeDataPoint: (point: unknown) => points.push(point) },
METERING_PLANS: "",
PUBLIC_URL: "https://forge.test",
};
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), for seeding and assertions. */
export function accountSql(accountId: string) {
return state.scopes.raw("account", accountId);
}
/** The repository's own SQLite (RepoState). */
export function repoSql(repoId: string) {
return state.scopes.raw("repo", repoId);
}
export function locator(kind: string, key: string, scopeId: string): void {
state.d1.db