|
| 1 | +// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +import { describe, it, expect, vi } from 'vitest'; |
| 4 | +import { |
| 5 | + dispatchHookIsolating, |
| 6 | + dispatchHookPropagating, |
| 7 | + type HookDispatchLogger, |
| 8 | + type HookHandler, |
| 9 | +} from './hook-dispatch.js'; |
| 10 | + |
| 11 | +/** |
| 12 | + * Direct pins for the two shared dispatch flavours (#5282). |
| 13 | + * |
| 14 | + * These exist as well as — never instead of — the paired kernel pins in |
| 15 | + * `kernel.test.ts` / `lite-kernel.test.ts`. The kernel pins say what each hook |
| 16 | + * MEANS on each kernel; these say what each flavour DOES, once, at the single |
| 17 | + * place both kernels now reach. The division matters for the reverse |
| 18 | + * verification: mutating a function here turns both kernels' files red in one |
| 19 | + * edit, which is the property option B was ruled to buy. |
| 20 | + */ |
| 21 | + |
| 22 | +const makeLogger = () => { |
| 23 | + const debug = vi.fn(); |
| 24 | + const error = vi.fn(); |
| 25 | + const logger: HookDispatchLogger = { debug, error }; |
| 26 | + return { logger, debug, error }; |
| 27 | +}; |
| 28 | + |
| 29 | +describe('hook dispatch (shared by ObjectKernel and ObjectKernelBase, #5282)', () => { |
| 30 | + describe('dispatchHookIsolating', () => { |
| 31 | + it('runs every remaining handler when one throws, in registration order', async () => { |
| 32 | + const reached: string[] = []; |
| 33 | + const { logger } = makeLogger(); |
| 34 | + const handlers: HookHandler[] = [ |
| 35 | + async () => { reached.push('first'); }, |
| 36 | + async () => { throw new Error('boom'); }, |
| 37 | + async () => { reached.push('third'); }, |
| 38 | + ]; |
| 39 | + |
| 40 | + await expect(dispatchHookIsolating('kernel:shutdown', handlers, logger)).resolves.toBeUndefined(); |
| 41 | + |
| 42 | + expect(reached).toEqual(['first', 'third']); |
| 43 | + }); |
| 44 | + |
| 45 | + it('logs each failure as `Hook handler failed: <name>` with the original error', async () => { |
| 46 | + const { logger, error } = makeLogger(); |
| 47 | + const boom = new Error('boom'); |
| 48 | + |
| 49 | + await dispatchHookIsolating('kernel:shutdown', [async () => { throw boom; }], logger); |
| 50 | + |
| 51 | + // The wording is contract: both kernels' pins assert this exact |
| 52 | + // line, and before #5282 it was typed out twice to keep them equal. |
| 53 | + expect(error).toHaveBeenCalledWith('Hook handler failed: kernel:shutdown', boom); |
| 54 | + }); |
| 55 | + |
| 56 | + it('traces `Triggering hook: <name>` with the handler count before running any handler', async () => { |
| 57 | + const { logger, debug } = makeLogger(); |
| 58 | + const order: string[] = []; |
| 59 | + debug.mockImplementation(() => { order.push('trace'); }); |
| 60 | + |
| 61 | + await dispatchHookIsolating( |
| 62 | + 'kernel:shutdown', |
| 63 | + [async () => { order.push('handler'); }, async () => { order.push('handler'); }], |
| 64 | + logger, |
| 65 | + ); |
| 66 | + |
| 67 | + expect(debug).toHaveBeenCalledWith('Triggering hook: kernel:shutdown', { |
| 68 | + hook: 'kernel:shutdown', |
| 69 | + handlerCount: 2, |
| 70 | + }); |
| 71 | + expect(order).toEqual(['trace', 'handler', 'handler']); |
| 72 | + }); |
| 73 | + |
| 74 | + it('passes every argument through to each handler', async () => { |
| 75 | + const { logger } = makeLogger(); |
| 76 | + const seen: unknown[][] = []; |
| 77 | + |
| 78 | + await dispatchHookIsolating( |
| 79 | + 'data:beforeInsert', |
| 80 | + [(...args: unknown[]) => { seen.push(args); }, (...args: unknown[]) => { seen.push(args); }], |
| 81 | + logger, |
| 82 | + ['sys_user', { id: 'u1' }], |
| 83 | + ); |
| 84 | + |
| 85 | + expect(seen).toEqual([ |
| 86 | + ['sys_user', { id: 'u1' }], |
| 87 | + ['sys_user', { id: 'u1' }], |
| 88 | + ]); |
| 89 | + }); |
| 90 | + |
| 91 | + it('takes the handler array by reference — a handler subscribing mid-dispatch still runs', async () => { |
| 92 | + // Both hand-written originals read the live array off the hooks map |
| 93 | + // and iterated it directly. Pinned so a defensive `[...handlers]` |
| 94 | + // copy cannot change dispatch semantics as a tidy-up. |
| 95 | + const { logger } = makeLogger(); |
| 96 | + const reached: string[] = []; |
| 97 | + const handlers: HookHandler[] = []; |
| 98 | + handlers.push(async () => { |
| 99 | + reached.push('first'); |
| 100 | + handlers.push(async () => { reached.push('late-subscriber'); }); |
| 101 | + }); |
| 102 | + |
| 103 | + await dispatchHookIsolating('kernel:shutdown', handlers, logger); |
| 104 | + |
| 105 | + expect(reached).toEqual(['first', 'late-subscriber']); |
| 106 | + }); |
| 107 | + }); |
| 108 | + |
| 109 | + describe('dispatchHookPropagating', () => { |
| 110 | + it('lets the first failure escape unwrapped and skips the handlers behind it', async () => { |
| 111 | + const reached: string[] = []; |
| 112 | + const { logger, error } = makeLogger(); |
| 113 | + const boom = new Error('listen EACCES: permission denied 0.0.0.0:80'); |
| 114 | + const handlers: HookHandler[] = [ |
| 115 | + async () => { reached.push('first'); }, |
| 116 | + async () => { throw boom; }, |
| 117 | + async () => { reached.push('never'); }, |
| 118 | + ]; |
| 119 | + |
| 120 | + // Identity, not just message: the boot path re-throws the original |
| 121 | + // error to its caller, it does not wrap or re-create it. |
| 122 | + await expect(dispatchHookPropagating('kernel:listening', handlers, logger)).rejects.toBe(boom); |
| 123 | + |
| 124 | + expect(reached).toEqual(['first']); |
| 125 | + // A propagated failure is the caller's to report — the dispatcher |
| 126 | + // does not also log it. |
| 127 | + expect(error).not.toHaveBeenCalled(); |
| 128 | + }); |
| 129 | + |
| 130 | + it('traces `Triggering hook: <name>` with the handler count when a logger is supplied', async () => { |
| 131 | + const { logger, debug } = makeLogger(); |
| 132 | + |
| 133 | + await dispatchHookPropagating('kernel:ready', [async () => {}], logger); |
| 134 | + |
| 135 | + expect(debug).toHaveBeenCalledWith('Triggering hook: kernel:ready', { |
| 136 | + hook: 'kernel:ready', |
| 137 | + handlerCount: 1, |
| 138 | + }); |
| 139 | + }); |
| 140 | + |
| 141 | + it('emits NO trace when no logger is supplied — the `context.trigger` shape', async () => { |
| 142 | + // `PluginContext.trigger` has never logged a dispatch trace on |
| 143 | + // either kernel. Handing it a logger would add a debug line to a |
| 144 | + // path that has never had one; #5282 preserves every call path's |
| 145 | + // semantics exactly, so the absence is pinned rather than assumed. |
| 146 | + const { logger, debug } = makeLogger(); |
| 147 | + const reached: string[] = []; |
| 148 | + |
| 149 | + await dispatchHookPropagating('kernel:ready', [async () => { reached.push('ran'); }], undefined); |
| 150 | + |
| 151 | + expect(reached).toEqual(['ran']); |
| 152 | + expect(debug).not.toHaveBeenCalled(); |
| 153 | + expect(logger.debug).not.toHaveBeenCalled(); |
| 154 | + }); |
| 155 | + |
| 156 | + it('passes every argument through to each handler', async () => { |
| 157 | + const seen: unknown[][] = []; |
| 158 | + |
| 159 | + await dispatchHookPropagating( |
| 160 | + 'metadata:reloaded', |
| 161 | + [(...args: unknown[]) => { seen.push(args); }], |
| 162 | + undefined, |
| 163 | + ['sys_user'], |
| 164 | + ); |
| 165 | + |
| 166 | + expect(seen).toEqual([['sys_user']]); |
| 167 | + }); |
| 168 | + }); |
| 169 | + |
| 170 | + it('dispatches nothing, and never throws, for a hook with no subscribers', async () => { |
| 171 | + const { logger, debug, error } = makeLogger(); |
| 172 | + |
| 173 | + await expect(dispatchHookIsolating('kernel:shutdown', [], logger)).resolves.toBeUndefined(); |
| 174 | + await expect(dispatchHookPropagating('kernel:ready', [], logger)).resolves.toBeUndefined(); |
| 175 | + |
| 176 | + expect(error).not.toHaveBeenCalled(); |
| 177 | + expect(debug).toHaveBeenCalledTimes(2); |
| 178 | + expect(debug.mock.calls.map((c) => c[1])).toEqual([ |
| 179 | + { hook: 'kernel:shutdown', handlerCount: 0 }, |
| 180 | + { hook: 'kernel:ready', handlerCount: 0 }, |
| 181 | + ]); |
| 182 | + }); |
| 183 | +}); |
0 commit comments