|
| 1 | +// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest'; |
| 4 | +import { LiteKernel } from '@objectstack/core'; |
| 5 | +import type { Plugin, PluginContext } from '@objectstack/core'; |
| 6 | + |
| 7 | +import { AutomationServicePlugin } from './plugin.js'; |
| 8 | +import type { AutomationEngine } from './engine.js'; |
| 9 | + |
| 10 | +/** |
| 11 | + * End-to-end coverage for the #1928 production wiring: `AutomationServicePlugin` |
| 12 | + * must, at `start()`, bridge the engine's object-schema resolver to the live |
| 13 | + * `objectql.registry.getObject`. The engine-level tests set the resolver by |
| 14 | + * hand; this proves the PLUGIN actually wires it from a real service and that a |
| 15 | + * flow registered through the running kernel gets the schema-aware advisory. |
| 16 | + * |
| 17 | + * A minimal fake `objectql` plugin supplies `registry.getObject` — the exact |
| 18 | + * seam the plugin consumes. Bootstrap runs every `init()` before any `start()`, |
| 19 | + * so the service is present when automation's `start()` looks it up. |
| 20 | + */ |
| 21 | +function fakeObjectqlPlugin(): Plugin { |
| 22 | + return { |
| 23 | + name: 'fake-objectql', |
| 24 | + version: '1.0.0', |
| 25 | + async init(ctx: PluginContext) { |
| 26 | + (ctx as unknown as { registerService(name: string, svc: unknown): void }).registerService('objectql', { |
| 27 | + registry: { |
| 28 | + getObject: (name: string) => |
| 29 | + name === 'crm_opportunity' |
| 30 | + ? { |
| 31 | + name, |
| 32 | + fields: { |
| 33 | + amount: { type: 'currency' }, |
| 34 | + title: { type: 'text' }, |
| 35 | + is_active: { type: 'boolean' }, |
| 36 | + stage: { type: 'select' }, |
| 37 | + }, |
| 38 | + } |
| 39 | + : undefined, |
| 40 | + }, |
| 41 | + }); |
| 42 | + }, |
| 43 | + }; |
| 44 | +} |
| 45 | + |
| 46 | +const oppFlow = (condition: string) => ({ |
| 47 | + name: 'opp_flow', |
| 48 | + label: 'Opportunity Flow', |
| 49 | + type: 'record_change', |
| 50 | + nodes: [ |
| 51 | + { id: 'start', type: 'start', label: 'Start', config: { objectName: 'crm_opportunity' } }, |
| 52 | + { id: 'check', type: 'decision', label: 'Check', config: { condition } }, |
| 53 | + { id: 'end', type: 'end', label: 'End' }, |
| 54 | + ], |
| 55 | + edges: [ |
| 56 | + { id: 'e1', source: 'start', target: 'check' }, |
| 57 | + { id: 'e2', source: 'check', target: 'end' }, |
| 58 | + ], |
| 59 | +}); |
| 60 | + |
| 61 | +describe('AutomationServicePlugin — object-schema resolver wired end-to-end (#1928)', () => { |
| 62 | + let kernel: LiteKernel; |
| 63 | + let engine: AutomationEngine; |
| 64 | + let stdout: string[]; |
| 65 | + let stdoutSpy: ReturnType<typeof vi.spyOn>; |
| 66 | + |
| 67 | + beforeEach(async () => { |
| 68 | + stdout = []; |
| 69 | + // `warn` logs go to process.stdout (non-error level); capture without |
| 70 | + // forwarding so we can assert on the registration advisory. |
| 71 | + stdoutSpy = vi.spyOn(process.stdout, 'write').mockImplementation(((chunk: unknown) => { |
| 72 | + stdout.push(String(chunk)); |
| 73 | + return true; |
| 74 | + }) as never); |
| 75 | + kernel = new LiteKernel(); |
| 76 | + kernel.use(fakeObjectqlPlugin()); |
| 77 | + kernel.use(new AutomationServicePlugin()); |
| 78 | + await kernel.bootstrap(); |
| 79 | + engine = kernel.getService('automation') as AutomationEngine; |
| 80 | + }); |
| 81 | + |
| 82 | + afterEach(async () => { |
| 83 | + stdoutSpy.mockRestore(); |
| 84 | + await kernel.shutdown(); |
| 85 | + }); |
| 86 | + |
| 87 | + it('bridges the resolver to objectql.registry.getObject (fields + types)', () => { |
| 88 | + const resolver = (engine as unknown as { objectSchemaResolver: ((n: string) => unknown) | null }) |
| 89 | + .objectSchemaResolver; |
| 90 | + expect(typeof resolver).toBe('function'); |
| 91 | + const schema = resolver!('crm_opportunity') as { fields: string[]; fieldTypes: Record<string, string> }; |
| 92 | + expect(schema.fields.slice().sort()).toEqual(['amount', 'is_active', 'stage', 'title']); |
| 93 | + expect(schema.fieldTypes).toMatchObject({ amount: 'currency', title: 'text', is_active: 'boolean' }); |
| 94 | + expect(resolver!('does_not_exist')).toBeUndefined(); |
| 95 | + }); |
| 96 | + |
| 97 | + it('emits a tier-4 advisory when a registered flow does arithmetic on a text field', () => { |
| 98 | + stdout.length = 0; |
| 99 | + expect(() => engine.registerFlow('opp_flow', oppFlow('title * 2 > 10'))).not.toThrow(); |
| 100 | + const logged = stdout.join(''); |
| 101 | + expect(logged).toMatch(/type mismatch/i); |
| 102 | + expect(logged).toMatch(/title/); |
| 103 | + }); |
| 104 | + |
| 105 | + it('emits no schema advisory for a sound flow condition', () => { |
| 106 | + stdout.length = 0; |
| 107 | + engine.registerFlow('opp_flow_ok', oppFlow('stage == "won" && amount > 1000')); |
| 108 | + const logged = stdout.join(''); |
| 109 | + expect(logged).not.toMatch(/type mismatch|did you mean|unknown field/i); |
| 110 | + }); |
| 111 | +}); |
0 commit comments