|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * [#24910] A kernel with `webhooks` and without `realtime` delivers no declared |
| 5 | + * webhook — and now says so ONCE, at `error`, with the consequence and the fix. |
| 6 | + * |
| 7 | + * ## The defect |
| 8 | + * |
| 9 | + * The auto-enqueuer's only source of record events is the realtime service: the |
| 10 | + * engine publishes `data.record.*` through it and nothing else feeds the |
| 11 | + * enqueuer. Without it the plugin disabled the enqueuer with one boot `warn`, |
| 12 | + * while every record write still answered 200, the declaration still said the |
| 13 | + * webhook was active, and the activation door answered `enabled: true` for a |
| 14 | + * switch that switched nothing. By AGENTS.md "Degradation log levels" that is |
| 15 | + * the durability class: the system looks normal and a delivery it claims never |
| 16 | + * lands. |
| 17 | + * |
| 18 | + * ## What is pinned, on a real kernel boot |
| 19 | + * |
| 20 | + * A real `LiteKernel` runs the plugin's real `kernel:ready`; the engine is a |
| 21 | + * real `ObjectQL` over a real in-memory `SqlDriver`, the stack's webhook comes |
| 22 | + * from `defineWebhook` through the engine's real manifest decomposition |
| 23 | + * (`registerApp`), and the outbox is messaging's real `MessagingService`. Only |
| 24 | + * the realtime service is a stand-in, and only on the legs that mount one. |
| 25 | + * |
| 26 | + * 1. Declared webhook, no realtime: one `error` naming the webhook, `realtime` |
| 27 | + * and the fix; no `disabled` warn; and a record write enqueues nothing (the |
| 28 | + * loss itself). |
| 29 | + * 2. CONTROL, the same stack with realtime: no error, and the same write |
| 30 | + * enqueues its deliveries — so leg 1's zero is the missing service and not a |
| 31 | + * fixture that cannot deliver. |
| 32 | + * 3. No declared webhook, no realtime: the `warn` it always was, no `error`. |
| 33 | + * 4. `autoEnqueue: false`: the host's own choice — nothing reported. |
| 34 | + */ |
| 35 | + |
| 36 | +import { afterEach, describe, expect, it, vi } from 'vitest'; |
| 37 | +import { LiteKernel } from '@objectstack/core'; |
| 38 | +import type { Plugin, PluginContext } from '@objectstack/core'; |
| 39 | +import { ObjectQL } from '@objectstack/objectql'; |
| 40 | +import { SqlDriver } from '@objectstack/driver-sql'; |
| 41 | +import { defineWebhook } from '@objectstack/spec/automation'; |
| 42 | +import type { IRealtimeService, RealtimeEventHandler, RealtimeEventPayload } from '@objectstack/spec/contracts'; |
| 43 | +import { MemoryHttpOutbox, MessagingService, type EnqueueHttpInput } from '@objectstack/service-messaging'; |
| 44 | +import { DeclaredWebhookSource, declaredWebhookRefId } from './declared-webhooks.js'; |
| 45 | +import { SysWebhook } from './sys-webhook.object.js'; |
| 46 | +import { WebhookOutboxPlugin, type WebhookOutboxPluginOptions } from './webhook-outbox-plugin.js'; |
| 47 | + |
| 48 | +const OBJECT = 'my_app_note'; |
| 49 | +const HOOK = 'my_app_note_changed'; |
| 50 | +const silentLogger = { info() {}, warn() {}, error() {}, debug() {} } as any; |
| 51 | + |
| 52 | +/** The webhook the card's reproduction declares, as `defineWebhook` authors it. */ |
| 53 | +const DECLARED = defineWebhook({ |
| 54 | + name: HOOK, |
| 55 | + object: OBJECT, |
| 56 | + triggers: ['create', 'update', 'delete'], |
| 57 | + url: 'http://127.0.0.1:3999/hook', |
| 58 | + method: 'POST', |
| 59 | + isActive: true, |
| 60 | +}); |
| 61 | + |
| 62 | +class FakeRealtime implements IRealtimeService { |
| 63 | + private subs = new Map<string, { handler: RealtimeEventHandler; opts?: any }>(); |
| 64 | + private n = 0; |
| 65 | + async publish(event: RealtimeEventPayload): Promise<void> { |
| 66 | + for (const sub of this.subs.values()) { |
| 67 | + if (sub.opts?.object && event.object !== sub.opts.object) continue; |
| 68 | + await sub.handler(event); |
| 69 | + } |
| 70 | + } |
| 71 | + async subscribe(_channel: string, handler: any, opts?: any): Promise<string> { |
| 72 | + const id = `s-${++this.n}`; |
| 73 | + this.subs.set(id, { handler, opts }); |
| 74 | + return id; |
| 75 | + } |
| 76 | + async unsubscribe(id: string): Promise<void> { this.subs.delete(id); } |
| 77 | +} |
| 78 | + |
| 79 | +/** messaging's real outbox, with every enqueue it receives recorded. */ |
| 80 | +class RecordingOutbox extends MemoryHttpOutbox { |
| 81 | + readonly enqueued: EnqueueHttpInput[] = []; |
| 82 | + override async enqueue(input: EnqueueHttpInput): Promise<string> { |
| 83 | + this.enqueued.push(input); |
| 84 | + return super.enqueue(input); |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +interface BootOptions { |
| 89 | + realtime?: boolean; |
| 90 | + declare?: boolean; |
| 91 | + plugin?: WebhookOutboxPluginOptions; |
| 92 | +} |
| 93 | + |
| 94 | +/** |
| 95 | + * Stands in for the plugin's declared dependency |
| 96 | + * (`dependencies = ['com.objectstack.service.messaging']`) and registers what a |
| 97 | + * served boot holds for `requires: ['webhooks']`: the engine, messaging, and |
| 98 | + * realtime only where the case asks. |
| 99 | + */ |
| 100 | +class FixturePlugin implements Plugin { |
| 101 | + name = 'com.objectstack.service.messaging'; |
| 102 | + type = 'standard' as const; |
| 103 | + version = '1.0.0'; |
| 104 | + readonly outbox = new RecordingOutbox(); |
| 105 | + readonly messaging = new MessagingService({ logger: silentLogger }); |
| 106 | + |
| 107 | + constructor( |
| 108 | + private readonly engine: ObjectQL, |
| 109 | + private readonly realtime: FakeRealtime | undefined, |
| 110 | + ) { |
| 111 | + this.messaging.setHttpOutbox(this.outbox); |
| 112 | + } |
| 113 | + |
| 114 | + init(ctx: PluginContext): void { |
| 115 | + ctx.registerService('manifest', { register: () => {}, list: () => [] }); |
| 116 | + ctx.registerService('objectql', this.engine); |
| 117 | + ctx.registerService('messaging', this.messaging); |
| 118 | + if (this.realtime) ctx.registerService('realtime', this.realtime); |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +/** The real plugin, with what its boot logs observed. */ |
| 123 | +class ObservedWebhookPlugin extends WebhookOutboxPlugin { |
| 124 | + readonly logs: Array<{ level: string; message: string; meta: unknown }> = []; |
| 125 | + |
| 126 | + override async init(ctx: PluginContext): Promise<void> { |
| 127 | + for (const level of ['debug', 'info', 'warn', 'error'] as const) { |
| 128 | + vi.spyOn(ctx.logger, level).mockImplementation((message: unknown, ...rest: unknown[]) => { |
| 129 | + this.logs.push({ level, message: String(message), meta: level === 'error' ? rest[1] : rest[0] }); |
| 130 | + }); |
| 131 | + } |
| 132 | + await super.init(ctx); |
| 133 | + } |
| 134 | + |
| 135 | + lines(level: string, fragment: string) { |
| 136 | + return this.logs.filter((l) => l.level === level && l.message.includes(fragment)); |
| 137 | + } |
| 138 | +} |
| 139 | + |
| 140 | +const open: Array<() => Promise<void>> = []; |
| 141 | + |
| 142 | +afterEach(async () => { |
| 143 | + while (open.length) { |
| 144 | + try { await open.pop()?.(); } catch { /* already stopped */ } |
| 145 | + } |
| 146 | + vi.restoreAllMocks(); |
| 147 | +}); |
| 148 | + |
| 149 | +async function boot(options: BootOptions) { |
| 150 | + const driver = new SqlDriver({ client: 'better-sqlite3', connection: { filename: ':memory:' }, useNullAsDefault: true }); |
| 151 | + const engine = new ObjectQL(); |
| 152 | + engine.registerDriver(driver, true); |
| 153 | + await engine.init(); |
| 154 | + engine.registerApp({ |
| 155 | + id: 'my_app', |
| 156 | + namespace: 'my_app', |
| 157 | + version: '0.0.0', |
| 158 | + objects: [{ |
| 159 | + name: OBJECT, |
| 160 | + label: 'Note', |
| 161 | + // Owner sharing is not under test here — keep RLS out of the way. |
| 162 | + sharingModel: 'public_read_write', |
| 163 | + fields: { title: { type: 'text', label: 'Title' } }, |
| 164 | + }], |
| 165 | + ...(options.declare === false ? {} : { webhooks: [DECLARED] }), |
| 166 | + }); |
| 167 | + engine.registry.registerObject(SysWebhook as any, 'test'); |
| 168 | + await engine.syncSchemas(); |
| 169 | + const realtime = options.realtime ? new FakeRealtime() : undefined; |
| 170 | + if (realtime) engine.setRealtimeService(realtime); |
| 171 | + |
| 172 | + const fixture = new FixturePlugin(engine, realtime); |
| 173 | + const plugin = new ObservedWebhookPlugin(options.plugin); |
| 174 | + const kernel = new LiteKernel({ logger: { level: 'silent' } as any }); |
| 175 | + kernel.use(fixture); |
| 176 | + kernel.use(plugin); |
| 177 | + await kernel.bootstrap(); |
| 178 | + open.push(async () => { await kernel.shutdown(); await driver.disconnect(); }); |
| 179 | + |
| 180 | + const reader = kernel.getService<DeclaredWebhookSource>('declaredWebhooks'); |
| 181 | + const enqueuerStarted = (() => { |
| 182 | + try { return Boolean(kernel.getService('webhook.autoEnqueuer')); } catch { return false; } |
| 183 | + })(); |
| 184 | + /** Insert then update one record, and give fire-and-forget enqueues a moment to land. */ |
| 185 | + const writeRecord = async () => { |
| 186 | + const created: any = await engine.insert(OBJECT, { title: 'seed' }); |
| 187 | + await engine.update(OBJECT, { title: 'edited' }, { where: { id: created.id } }); |
| 188 | + for (let i = 0; i < 20 && fixture.outbox.enqueued.length < 2; i++) await new Promise((r) => setTimeout(r, 25)); |
| 189 | + }; |
| 190 | + return { plugin, reader, enqueuerStarted, writeRecord, outbox: fixture.outbox }; |
| 191 | +} |
| 192 | + |
| 193 | +describe('[#24910] the auto-enqueuer disabled on a stack that declares webhooks', () => { |
| 194 | + it('no realtime: ONE error naming the webhook, the missing service and the fix — and a record write enqueues nothing', async () => { |
| 195 | + const { plugin, reader, enqueuerStarted, writeRecord, outbox } = await boot({}); |
| 196 | + |
| 197 | + const errors = plugin.lines('error', '[webhook-auto-enqueuer]'); |
| 198 | + expect(errors).toHaveLength(1); |
| 199 | + expect(errors[0].message).toContain(HOOK); |
| 200 | + expect(errors[0].message).toContain("add 'realtime' to `requires`"); |
| 201 | + expect(errors[0].meta).toMatchObject({ hasEngine: true, hasRealtime: false, hasMessaging: true, declared: [HOOK] }); |
| 202 | + expect(plugin.lines('warn', 'auto-enqueuer] disabled')).toEqual([]); |
| 203 | + |
| 204 | + expect(enqueuerStarted).toBe(false); |
| 205 | + expect(reader).toBeInstanceOf(DeclaredWebhookSource); |
| 206 | + // The reader still answers the declaration — it is only dispatch that is missing. |
| 207 | + expect((await reader.declaredWebhookNamed(HOOK))?.active).toBe(true); |
| 208 | + |
| 209 | + await writeRecord(); |
| 210 | + expect(outbox.enqueued).toEqual([]); |
| 211 | + }); |
| 212 | + |
| 213 | + it('CONTROL — the same stack with realtime: no error, and the same write is delivered', async () => { |
| 214 | + const { plugin, enqueuerStarted, writeRecord, outbox } = await boot({ realtime: true }); |
| 215 | + |
| 216 | + expect(plugin.lines('error', '[webhook-auto-enqueuer]')).toEqual([]); |
| 217 | + expect(enqueuerStarted).toBe(true); |
| 218 | + |
| 219 | + await writeRecord(); |
| 220 | + expect(outbox.enqueued.map((d) => [d.refId, d.label])).toEqual([ |
| 221 | + [declaredWebhookRefId(HOOK), 'data.record.created'], |
| 222 | + [declaredWebhookRefId(HOOK), 'data.record.updated'], |
| 223 | + ]); |
| 224 | + }); |
| 225 | + |
| 226 | + it('no declared webhook, no realtime: the warn it always was, never the error', async () => { |
| 227 | + const { plugin } = await boot({ declare: false }); |
| 228 | + |
| 229 | + expect(plugin.lines('error', '[webhook-auto-enqueuer]')).toEqual([]); |
| 230 | + const warns = plugin.lines('warn', 'auto-enqueuer] disabled'); |
| 231 | + expect(warns).toHaveLength(1); |
| 232 | + expect(warns[0].meta).toMatchObject({ hasEngine: true, hasRealtime: false, hasMessaging: true }); |
| 233 | + }); |
| 234 | + |
| 235 | + it('`autoEnqueue: false` is the host\'s own composition: nothing reported', async () => { |
| 236 | + const { plugin, enqueuerStarted } = await boot({ plugin: { autoEnqueue: false } }); |
| 237 | + |
| 238 | + expect(plugin.lines('error', '[webhook-auto-enqueuer]')).toEqual([]); |
| 239 | + expect(plugin.lines('warn', '[webhook-auto-enqueuer]')).toEqual([]); |
| 240 | + expect(enqueuerStarted).toBe(false); |
| 241 | + }); |
| 242 | +}); |
0 commit comments