|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * #24289 — `bindHooksToEngine`'s `wrapHandler` option: one wrap for every |
| 5 | + * handler a hook runs in-process, applied after a string `handler` resolves. |
| 6 | + * |
| 7 | + * - an inline function, a name from the bind's `functions` and a name the |
| 8 | + * same package registered on the engine all take the wrap; |
| 9 | + * - a `body`'s handler (built by `bodyRunner`) never does; |
| 10 | + * - control: a bind with no `wrapHandler` calls the resolved function with |
| 11 | + * the engine's own context object, exactly as before. |
| 12 | + */ |
| 13 | + |
| 14 | +import { describe, it, expect } from 'vitest'; |
| 15 | +import { ObjectQL } from './engine.js'; |
| 16 | +import { bindHooksToEngine } from './hook-binder.js'; |
| 17 | +import type { Hook, HookContext } from '@objectstack/spec/data'; |
| 18 | + |
| 19 | +const ctx = (): HookContext => |
| 20 | + ({ object: 'account', event: 'beforeInsert', input: { data: {} }, ql: undefined }) as HookContext; |
| 21 | + |
| 22 | +/** Marks each context the wrapped handler is called with, and records which hook it wrapped. */ |
| 23 | +function marking(wrappedFor: string[]) { |
| 24 | + return (handler: (context: HookContext) => unknown, hook: Hook) => { |
| 25 | + wrappedFor.push(hook.name); |
| 26 | + return (context: HookContext) => handler({ ...context, wrapped: hook.name } as HookContext); |
| 27 | + }; |
| 28 | +} |
| 29 | + |
| 30 | +const hook = (name: string, handler: Hook['handler']): Hook => ({ |
| 31 | + name, |
| 32 | + object: 'account', |
| 33 | + events: ['beforeInsert'], |
| 34 | + handler, |
| 35 | +}); |
| 36 | + |
| 37 | +describe('#24289: bindHooksToEngine wrapHandler', () => { |
| 38 | + it('wraps an inline handler and both kinds of named handler, once each, at bind', async () => { |
| 39 | + const engine = new ObjectQL(); |
| 40 | + engine.registerFunction('registered_fn', async (c: any) => { seen.push(`registered:${c.wrapped}`); }, { |
| 41 | + packageId: 'app:wrap', |
| 42 | + }); |
| 43 | + const seen: string[] = []; |
| 44 | + const wrappedFor: string[] = []; |
| 45 | + |
| 46 | + const result = bindHooksToEngine( |
| 47 | + engine, |
| 48 | + [ |
| 49 | + hook('inline', async (c: any) => { seen.push(`inline:${c.wrapped}`); }), |
| 50 | + hook('from_bind', 'bound_fn'), |
| 51 | + hook('from_registry', 'registered_fn'), |
| 52 | + ], |
| 53 | + { |
| 54 | + packageId: 'app:wrap', |
| 55 | + functions: { bound_fn: async (c: any) => { seen.push(`bound:${c.wrapped}`); } }, |
| 56 | + wrapHandler: marking(wrappedFor), |
| 57 | + }, |
| 58 | + ); |
| 59 | + expect(result.errors).toEqual([]); |
| 60 | + expect(wrappedFor).toEqual(['inline', 'from_bind', 'from_registry']); |
| 61 | + |
| 62 | + await engine.triggerHooks('beforeInsert', ctx()); |
| 63 | + await engine.triggerHooks('beforeInsert', ctx()); |
| 64 | + expect(seen.sort()).toEqual([ |
| 65 | + 'bound:from_bind', 'bound:from_bind', |
| 66 | + 'inline:inline', 'inline:inline', |
| 67 | + 'registered:from_registry', 'registered:from_registry', |
| 68 | + ]); |
| 69 | + expect(wrappedFor, 'the wrap is applied at bind, never per event').toHaveLength(3); |
| 70 | + }); |
| 71 | + |
| 72 | + it("never wraps the handler a body's runner builds", async () => { |
| 73 | + const engine = new ObjectQL(); |
| 74 | + const seen: unknown[] = []; |
| 75 | + const wrappedFor: string[] = []; |
| 76 | + bindHooksToEngine( |
| 77 | + engine, |
| 78 | + [{ ...hook('with_body', undefined), body: { language: 'js', source: 'return;' } } as Hook], |
| 79 | + { |
| 80 | + bodyRunner: () => async (c: any) => { seen.push(c.wrapped); }, |
| 81 | + wrapHandler: marking(wrappedFor), |
| 82 | + }, |
| 83 | + ); |
| 84 | + |
| 85 | + await engine.triggerHooks('beforeInsert', ctx()); |
| 86 | + expect(wrappedFor).toEqual([]); |
| 87 | + expect(seen).toEqual([undefined]); |
| 88 | + }); |
| 89 | + |
| 90 | + it("control: without wrapHandler, a named handler is called with the engine's own context", async () => { |
| 91 | + const engine = new ObjectQL(); |
| 92 | + const received: unknown[] = []; |
| 93 | + const fn = async (c: unknown) => { received.push(c); }; |
| 94 | + bindHooksToEngine(engine, [hook('plain_named', 'plain_fn'), hook('plain_inline', fn)], { |
| 95 | + packageId: 'app:plain', |
| 96 | + functions: { plain_fn: fn }, |
| 97 | + }); |
| 98 | + |
| 99 | + const context = ctx(); |
| 100 | + await engine.triggerHooks('beforeInsert', context); |
| 101 | + expect(received).toEqual([context, context]); |
| 102 | + expect(received.every((c) => c === context)).toBe(true); |
| 103 | + }); |
| 104 | +}); |
0 commit comments