import { vi } from "vitest"; type Handler = (data: unknown) => Promise; export function mockKV() { const store = new Map>(); return { get: async (scope: string, key: string): Promise => { return (store.get(scope)?.get(key) as T) ?? null; }, set: async (scope: string, key: string, data: T): Promise => { if (!store.has(scope)) store.set(scope, new Map()); store.get(scope)!.set(key, data); return data; }, delete: async (scope: string, key: string): Promise => { store.get(scope)?.delete(key); }, list: async (scope: string): Promise => { const entries = store.get(scope); return entries ? (Array.from(entries.values()) as T[]) : []; }, }; } export function mockSdk() { const functions = new Map(); return { registerFunction: ( idOrOpts: string | { id: string }, handler: Handler, _options?: Record, ) => { const id = typeof idOrOpts === "string" ? idOrOpts : idOrOpts.id; functions.set(id, handler); }, registerTrigger: vi.fn(), trigger: async ( idOrInput: | string | { function_id: string; payload: unknown; action?: unknown }, data?: unknown, ) => { const id = typeof idOrInput === "string" ? idOrInput : idOrInput.function_id; const payload = typeof idOrInput === "string" ? data : (idOrInput.payload as unknown); const fn = functions.get(id); if (!fn) throw new Error(`No function: ${id}`); return fn(payload); }, }; }