| 11 | import { KV } from "../src/state/schema.js"; |
| 12 | |
| 13 | function mockKV() { |
| 14 | const store = new Map<string, Map<string, unknown>>(); |
| 15 | const setCalls: Array<{ scope: string; key: string | undefined; value: any }> = []; |
| 16 | return { |
| 17 | get: async <T>(scope: string, key: string): Promise<T | null> => |
| 18 | (store.get(scope)?.get(key) as T) ?? null, |
| 19 | set: async <T>(scope: string, key: string, value: T): Promise<T> => { |
| 20 | setCalls.push({ scope, key, value }); |
| 21 | if (!store.has(scope)) store.set(scope, new Map()); |
| 22 | // Mirror the engine: a state::set with key=undefined fails. We |
| 23 | // surface this via setCalls so the test can assert key !== undefined. |
| 24 | if (key === undefined) { |
| 25 | throw new Error("missing field `key`"); |
| 26 | } |
| 27 | store.get(scope)!.set(key, value); |
| 28 | return value; |
| 29 | }, |
| 30 | delete: async (scope: string, key: string) => { |
| 31 | store.get(scope)?.delete(key); |
| 32 | }, |
| 33 | list: async <T>(scope: string): Promise<T[]> => |
| 34 | Array.from(store.get(scope)?.values() ?? []) as T[], |
| 35 | getSetCalls: () => setCalls, |
| 36 | }; |
| 37 | } |
| 38 | |
| 39 | function mockSdk(kv: ReturnType<typeof mockKV>) { |
| 40 | const fns = new Map<string, Function>(); |