|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | +// |
| 3 | +// [#28591] A provider-bound connector entry a plugin registers during |
| 4 | +// `kernel:ready` — after the boot reconcile in `start()` — is materialized |
| 5 | +// before the boot completes. |
| 6 | +// |
| 7 | +// The install-local rehydrate is the producer this covers: it re-registers every |
| 8 | +// installed package in its own `kernel:ready` handler and announces no reload. |
| 9 | +// Before the fix, the boot reconcile in `start()` was the only one a boot ran, |
| 10 | +// so a rehydrated package's connector stayed absent from `GET /connectors` |
| 11 | +// until some unrelated `metadata:reloaded`. |
| 12 | +// |
| 13 | +// Pinned here: |
| 14 | +// 1. an entry registered during `kernel:ready` is listed and dispatchable once |
| 15 | +// `bootstrap()` resolves, and the host entry is not materialized again; |
| 16 | +// 2. a boot that registers nothing after `start()` runs ONE reconcile (control); |
| 17 | +// 3. a configuration fault in an entry registered during `kernel:ready` is |
| 18 | +// logged and that entry skipped — the boot completes and the rest of the |
| 19 | +// batch is materialized (reload semantics, as on install). |
| 20 | + |
| 21 | +import { describe, it, expect, vi, afterEach } from 'vitest'; |
| 22 | +import { LiteKernel, ObjectLogger } from '@objectstack/core'; |
| 23 | +import type { Connector, ConnectorProviderContext, ConnectorProviderFactory } from '@objectstack/spec/integration'; |
| 24 | +import { AutomationServicePlugin } from './plugin.js'; |
| 25 | +import type { AutomationEngine } from './engine.js'; |
| 26 | + |
| 27 | +afterEach(() => { |
| 28 | + vi.restoreAllMocks(); |
| 29 | +}); |
| 30 | + |
| 31 | +/** A provider-bound declarative entry, as `registerApp` stores it. */ |
| 32 | +function providerConnector(name: string, provider = 'fake') { |
| 33 | + return { name, label: name, type: 'api', provider, providerConfig: {} }; |
| 34 | +} |
| 35 | + |
| 36 | +/** A factory that records each materialization and returns a one-action connector. */ |
| 37 | +function makeProvider() { |
| 38 | + const calls: ConnectorProviderContext[] = []; |
| 39 | + const factory: ConnectorProviderFactory = (ctx) => { |
| 40 | + calls.push(ctx); |
| 41 | + return { |
| 42 | + def: { |
| 43 | + name: ctx.name, |
| 44 | + label: ctx.label, |
| 45 | + type: 'api', |
| 46 | + authentication: { type: 'none' }, |
| 47 | + actions: [{ key: 'ping', label: 'Ping' }], |
| 48 | + } as unknown as Connector, |
| 49 | + handlers: { ping: async () => ({ ok: true }) }, |
| 50 | + }; |
| 51 | + }; |
| 52 | + return { factory, calls }; |
| 53 | +} |
| 54 | + |
| 55 | +/** |
| 56 | + * Boot the automation plugin over a registry holding `atStart`, with a harness |
| 57 | + * that appends `atReady` to the registry in its `kernel:ready` handler — the |
| 58 | + * shape of the install-local rehydrate. |
| 59 | + */ |
| 60 | +async function boot(atStart: unknown[], atReady: unknown[], factory: ConnectorProviderFactory) { |
| 61 | + const declared = [...atStart]; |
| 62 | + const kernel = new LiteKernel({ logger: { level: 'silent' } } as never); |
| 63 | + kernel.use(new AutomationServicePlugin()); |
| 64 | + kernel.use({ |
| 65 | + name: 'test.rehydrate-harness', |
| 66 | + type: 'standard' as const, |
| 67 | + version: '1.0.0', |
| 68 | + dependencies: ['com.objectstack.service-automation'], |
| 69 | + async init(ctx: any) { |
| 70 | + ctx.registerService('objectql', { |
| 71 | + registry: { listItems: (type: string) => (type === 'connector' ? declared : []) }, |
| 72 | + }); |
| 73 | + ctx.getService('automation').registerConnectorProvider('fake', factory); |
| 74 | + }, |
| 75 | + async start(ctx: any) { |
| 76 | + ctx.hook('kernel:ready', async () => { |
| 77 | + declared.push(...atReady); |
| 78 | + }); |
| 79 | + }, |
| 80 | + } as never); |
| 81 | + await kernel.bootstrap(); |
| 82 | + return { kernel, engine: kernel.getService('automation') as AutomationEngine }; |
| 83 | +} |
| 84 | + |
| 85 | +const reconcileSpy = () => |
| 86 | + vi.spyOn(AutomationServicePlugin.prototype as any, 'reconcileDeclaredConnectors'); |
| 87 | + |
| 88 | +describe('[#28591] connectors registered during kernel:ready are materialized before the boot completes', () => { |
| 89 | + it('lists and dispatches an entry registered during kernel:ready, without re-materializing the host entry', async () => { |
| 90 | + const { factory, calls } = makeProvider(); |
| 91 | + const { kernel, engine } = await boot([providerConnector('host_conn')], [providerConnector('installed_conn')], factory); |
| 92 | + |
| 93 | + expect(engine.getRegisteredConnectors()).toEqual(expect.arrayContaining(['host_conn', 'installed_conn'])); |
| 94 | + expect(engine.getConnectorOrigin('installed_conn')).toBe('declarative'); |
| 95 | + expect(calls.map((c) => c.name)).toEqual(['host_conn', 'installed_conn']); |
| 96 | + |
| 97 | + engine.registerFlow('call_installed', { |
| 98 | + name: 'call_installed', |
| 99 | + label: 'Call installed', |
| 100 | + type: 'autolaunched', |
| 101 | + variables: [{ name: 'call.ok', type: 'boolean', isOutput: true }], |
| 102 | + nodes: [ |
| 103 | + { id: 'start', type: 'start', label: 'Start' }, |
| 104 | + { |
| 105 | + id: 'call', |
| 106 | + type: 'connector_action', |
| 107 | + label: 'Ping', |
| 108 | + connectorConfig: { connectorId: 'installed_conn', actionId: 'ping', input: {} }, |
| 109 | + }, |
| 110 | + { id: 'end', type: 'end', label: 'End' }, |
| 111 | + ], |
| 112 | + edges: [ |
| 113 | + { id: 'e1', source: 'start', target: 'call' }, |
| 114 | + { id: 'e2', source: 'call', target: 'end' }, |
| 115 | + ], |
| 116 | + } as never); |
| 117 | + const result = await engine.execute('call_installed'); |
| 118 | + expect(result.success).toBe(true); |
| 119 | + expect(result.output).toMatchObject({ 'call.ok': true }); |
| 120 | + |
| 121 | + await kernel.shutdown(); |
| 122 | + }); |
| 123 | + |
| 124 | + it('runs one reconcile on a boot that registers nothing after start() (control)', async () => { |
| 125 | + const spy = reconcileSpy(); |
| 126 | + const { factory, calls } = makeProvider(); |
| 127 | + const { kernel, engine } = await boot([providerConnector('host_conn')], [], factory); |
| 128 | + |
| 129 | + expect(engine.getRegisteredConnectors()).toContain('host_conn'); |
| 130 | + expect(calls).toHaveLength(1); |
| 131 | + expect(spy).toHaveBeenCalledTimes(1); |
| 132 | + expect(spy.mock.calls[0]?.[1]).toEqual({ fatal: true }); |
| 133 | + |
| 134 | + await kernel.shutdown(); |
| 135 | + }); |
| 136 | + |
| 137 | + it('runs a second, non-fatal reconcile when kernel:ready registered an entry', async () => { |
| 138 | + const spy = reconcileSpy(); |
| 139 | + const { factory } = makeProvider(); |
| 140 | + const { kernel } = await boot([providerConnector('host_conn')], [providerConnector('installed_conn')], factory); |
| 141 | + |
| 142 | + expect(spy.mock.calls.map((c) => c[1])).toEqual([{ fatal: true }, { fatal: false }]); |
| 143 | + |
| 144 | + await kernel.shutdown(); |
| 145 | + }); |
| 146 | + |
| 147 | + it('logs and skips a configuration fault in an entry registered during kernel:ready; the boot completes', async () => { |
| 148 | + const errors = vi.spyOn(ObjectLogger.prototype, 'error'); |
| 149 | + const { factory } = makeProvider(); |
| 150 | + const { kernel, engine } = await boot( |
| 151 | + [providerConnector('host_conn')], |
| 152 | + [providerConnector('broken_conn', 'no_such_provider'), providerConnector('installed_conn')], |
| 153 | + factory, |
| 154 | + ); |
| 155 | + |
| 156 | + const registered = engine.getRegisteredConnectors(); |
| 157 | + expect(registered).toEqual(expect.arrayContaining(['host_conn', 'installed_conn'])); |
| 158 | + expect(registered).not.toContain('broken_conn'); |
| 159 | + const messages = errors.mock.calls.map((c) => String(c[0])); |
| 160 | + expect(messages.some((m) => m.includes('broken_conn') && m.includes('no_such_provider')), messages.join('\n')).toBe(true); |
| 161 | + |
| 162 | + await kernel.shutdown(); |
| 163 | + }); |
| 164 | +}); |
0 commit comments