|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * [ADR-0130 D4] A flow a composed stack declares only on its TOP LEVEL, beside |
| 5 | + * package bodies that do not declare it — the stack's residual — is registered |
| 6 | + * with the automation engine at boot and runs, the way one a body declares does. |
| 7 | + * |
| 8 | + * ## The defect this pins |
| 9 | + * |
| 10 | + * `composeStacks([mixed, other], { manifest: 'preserve' })`, where `mixed` is an |
| 11 | + * input that carries `packages[]` AND a top-level flow no body repeats, booted: |
| 12 | + * the residual rule registered that flow into the metadata service only, and the |
| 13 | + * engine's `manifest` service registered the residual's objects and connectors |
| 14 | + * only. The automation plugin's boot pull reads `registry.listItems('flow')`, so |
| 15 | + * the flow never reached the engine: `/meta` listed a flow `getFlow` answered |
| 16 | + * `null` for, and its trigger never armed. |
| 17 | + * |
| 18 | + * ## What each pin is for |
| 19 | + * |
| 20 | + * - the residual flow is registered with the engine at boot, owned by the |
| 21 | + * stack's own id (the owner the residual rule names), and runs; |
| 22 | + * - a flow a package body declares is registered once, owned by that body, |
| 23 | + * whether or not the top level repeats it; |
| 24 | + * - CONTROL: a stack with no residual flow registers exactly the bodies' flows. |
| 25 | + * |
| 26 | + * The stack is booted through the `manifest` service, the way `AppPlugin` hands |
| 27 | + * a config boot's stack to it: `{ ...bundle.manifest, ...bundle }`. |
| 28 | + */ |
| 29 | +import { afterEach, describe, expect, it } from 'vitest'; |
| 30 | +import { ObjectKernel, type Plugin, type PluginContext } from '@objectstack/core'; |
| 31 | +import { ObjectQLPlugin, type ObjectQL } from '@objectstack/objectql'; |
| 32 | +import { SqlDriver } from '@objectstack/driver-sql'; |
| 33 | +import { composeStacks, defineStack } from '@objectstack/spec'; |
| 34 | + |
| 35 | +import { AutomationServicePlugin } from './plugin.js'; |
| 36 | +import type { AutomationEngine } from './engine.js'; |
| 37 | + |
| 38 | +const MIXED_ID = 'com.acme.bundle'; |
| 39 | +const BODY_ID = 'com.acme.orders'; |
| 40 | + |
| 41 | +const pluginManifest = (id: string) => ({ |
| 42 | + id, |
| 43 | + namespace: id.split('.').pop(), |
| 44 | + version: '1.0.0', |
| 45 | + type: 'plugin', |
| 46 | + name: id.split('.').pop(), |
| 47 | +}); |
| 48 | + |
| 49 | +const flow = (name: string) => ({ |
| 50 | + name, |
| 51 | + label: name, |
| 52 | + type: 'autolaunched', |
| 53 | + nodes: [ |
| 54 | + { id: 'start', type: 'start', label: 'Start' }, |
| 55 | + { id: 'end', type: 'end', label: 'End' }, |
| 56 | + ], |
| 57 | + edges: [{ id: 'e1', source: 'start', target: 'end' }], |
| 58 | +}); |
| 59 | + |
| 60 | +/** |
| 61 | + * An input that carries `packages[]` and, on its top level only, `topLevel` |
| 62 | + * flows — the mixed shape. `bodyFlows` lands on the package body. |
| 63 | + */ |
| 64 | +const mixed = (topLevel: unknown[] | undefined, bodyFlows: unknown[]) => |
| 65 | + defineStack( |
| 66 | + { |
| 67 | + manifest: pluginManifest(MIXED_ID), |
| 68 | + ...(topLevel ? { flows: topLevel } : {}), |
| 69 | + packages: [{ manifest: { ...pluginManifest(BODY_ID), flows: bodyFlows } }], |
| 70 | + } as never, |
| 71 | + { strict: false }, |
| 72 | + ); |
| 73 | + |
| 74 | +const other = () => defineStack({ manifest: pluginManifest('com.acme.other') } as never, { strict: false }); |
| 75 | + |
| 76 | +const composed = (topLevel: unknown[] | undefined, bodyFlows: unknown[]) => |
| 77 | + composeStacks([mixed(topLevel, bodyFlows), other()] as never, { manifest: 'preserve' } as never) as Record<string, unknown>; |
| 78 | + |
| 79 | +/** What `AppPlugin.init` hands the `manifest` service. */ |
| 80 | +const payload = (bundle: Record<string, unknown>) => ({ ...(bundle.manifest as object), ...bundle }); |
| 81 | + |
| 82 | +const kernels: ObjectKernel[] = []; |
| 83 | +afterEach(async () => { |
| 84 | + for (const k of kernels.splice(0)) await k.shutdown().catch(() => {}); |
| 85 | +}); |
| 86 | + |
| 87 | +/** Boot the engine and the automation plugin; register `stack` through the `manifest` service at init. */ |
| 88 | +async function boot(stack: Record<string, unknown>) { |
| 89 | + const kernel = new ObjectKernel({ logger: { level: 'fatal' }, gracefulShutdown: false }); |
| 90 | + kernels.push(kernel); |
| 91 | + const driver = new SqlDriver({ client: 'better-sqlite3', connection: { filename: ':memory:' }, useNullAsDefault: true }); |
| 92 | + await kernel.use({ |
| 93 | + name: 'test.driver.sqlite', |
| 94 | + type: 'standard', |
| 95 | + version: '1.0.0', |
| 96 | + async init(ctx: PluginContext) { |
| 97 | + ctx.registerService('driver.default', driver); |
| 98 | + }, |
| 99 | + } as Plugin); |
| 100 | + await kernel.use(new ObjectQLPlugin()); |
| 101 | + await kernel.use(new AutomationServicePlugin({ suspendedRunStore: 'memory' })); |
| 102 | + await kernel.use({ |
| 103 | + name: 'test.stack', |
| 104 | + type: 'standard', |
| 105 | + version: '1.0.0', |
| 106 | + dependencies: ['com.objectstack.engine.objectql', 'com.objectstack.service-automation'], |
| 107 | + async init(ctx: PluginContext) { |
| 108 | + await ctx.getService<{ register(m: unknown): Promise<void> }>('manifest').register(payload(stack)); |
| 109 | + }, |
| 110 | + } as Plugin); |
| 111 | + await kernel.bootstrap(); |
| 112 | + return { ql: kernel.getService<ObjectQL>('objectql'), automation: kernel.getService<AutomationEngine>('automation') }; |
| 113 | +} |
| 114 | + |
| 115 | +const flowOwners = (ql: ObjectQL): Array<[unknown, unknown]> => |
| 116 | + (ql.registry.listItems('flow') as Array<{ name?: unknown; _packageId?: unknown }>) |
| 117 | + .map((f): [unknown, unknown] => [f.name, f._packageId]) |
| 118 | + .sort((a, b) => String(a[0]).localeCompare(String(b[0]))); |
| 119 | + |
| 120 | +describe('[ADR-0130 D4] a flow a mixed composed input declares only on its top level', () => { |
| 121 | + it('is registered with the automation engine at boot, owned by the stack\'s own id, and runs', async () => { |
| 122 | + const stack = composed([flow('acme_residual_flow')], [flow('acme_body_flow')]); |
| 123 | + const { ql, automation } = await boot(stack); |
| 124 | + const ownerId = (stack.manifest as { id?: string }).id; |
| 125 | + |
| 126 | + expect(flowOwners(ql)).toEqual([ |
| 127 | + ['acme_body_flow', BODY_ID], |
| 128 | + ['acme_residual_flow', ownerId], |
| 129 | + ]); |
| 130 | + expect((await automation.listFlows()).sort()).toEqual(['acme_body_flow', 'acme_residual_flow']); |
| 131 | + expect(await automation.getFlow('acme_residual_flow')).not.toBeNull(); |
| 132 | + const run = await automation.execute('acme_residual_flow'); |
| 133 | + expect(run.error).toBeUndefined(); |
| 134 | + expect(run.success).toBe(true); |
| 135 | + }); |
| 136 | + |
| 137 | + it('a flow a package body declares is registered once, owned by that body, when the top level repeats it', async () => { |
| 138 | + const { ql, automation } = await boot(composed([flow('acme_body_flow')], [flow('acme_body_flow')])); |
| 139 | + expect(flowOwners(ql)).toEqual([['acme_body_flow', BODY_ID]]); |
| 140 | + expect(await automation.listFlows()).toEqual(['acme_body_flow']); |
| 141 | + }); |
| 142 | + |
| 143 | + it('CONTROL: a stack with no top-level flow registers exactly the bodies\' flows', async () => { |
| 144 | + const { ql, automation } = await boot(composed(undefined, [flow('acme_body_flow')])); |
| 145 | + expect(flowOwners(ql)).toEqual([['acme_body_flow', BODY_ID]]); |
| 146 | + expect(await automation.listFlows()).toEqual(['acme_body_flow']); |
| 147 | + }); |
| 148 | +}); |
0 commit comments