import type { HookStore, HookStoreBackedMap } from '@celilo/capabilities'; /** * In-memory hook-owned-state stores for tests that build a HookContext * without a broker. The four methods mutate the record directly; the * manifest validation a real store applies lives broker-side * (hook-store.ts) and has its own tests. * * Methods are defined non-enumerably, so the result spreads, serializes and * `toEqual`s as the plain record the test wrote — the same shape a real * broker-side context carries when executeHookScript frames it. */ export function configStore( values: Record = {}, ): Record & HookStore { return attach(values); } export function secretStore(values: Record = {}): HookStoreBackedMap { // Downcast: the caller passed Record; attach only adds // methods and never writes, so the value type is unchanged. return attach(values) as HookStoreBackedMap; } function attach(values: Record): Record & HookStore { const store: HookStore = { get: async (name) => values[name] as string | undefined, set: async (name, value) => { values[name] = value; }, delete: async (name) => { delete values[name]; }, transaction: async (fn) => { await fn(store); }, }; for (const [name, method] of Object.entries(store)) { Object.defineProperty(values, name, { value: method, enumerable: false, writable: true, configurable: true, }); } return Object.assign(values, store); }