import { beforeEach, describe, expect, mock, test } from "bun:test";
import { fakeD1 } from "../metering/fake-d1.test-helper";
import { memoryScopes } from "../scoped-db/testing";
let d1 = fakeD1();
const stopped: string[] = [];
const fakeEnv: Record<string, unknown> = {
get DB() {
return d1.binding;
},
KV: { get: async () => null, put: async () => undefined, delete: async () => undefined },
EDITOR_CONTAINER: { getByName: (name: string) => ({ stop: async () => stopped.push(name) && 0 }) },
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 locators = new Map<string, string>();
const actualLocators = await import("../scoped-db/locators");
mock.module("../scoped-db/locators", () => ({
...actualLocators,
locate: async (kind: string, key: string) => locators.get(`${kind}:${key}`) ?? null,
registerLocator: async (kind: string, key: string, scopeId: string) => {
locators.set(`${kind}:${key}`, scopeId);
},
locatedScopes: async (kind: string) => [
...new Set([...locators].filter(([key]) => key.startsWith(`${kind}:`)).map(([, scope]) => scope)),
],
locatedByPrefix: async (kind: string, prefix: string) => [
...new Set([...locators].filter(([key]) => key.startsWith(`${kind}:${prefix}`)).map(([, scope]) => scope)),
],
forgetLocator: async (kind: string, key: string) => {