|
| 1 | +// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * Deprecation window for non-canonical `config` keys on the CRUD nodes |
| 5 | + * (`object` → `objectName`, `filters` → `filter`). The alias keeps working so |
| 6 | + * already-stored flows keep running, but emits a one-time `logger.warn` steering |
| 7 | + * the author to the canonical key. See config-aliases.ts. |
| 8 | + */ |
| 9 | +import { describe, it, expect, beforeEach } from 'vitest'; |
| 10 | +import { AutomationEngine } from '../engine.js'; |
| 11 | +import { registerCrudNodes } from './crud-nodes.js'; |
| 12 | +import { __resetAliasDeprecationWarnings } from './config-aliases.js'; |
| 13 | + |
| 14 | +function silentLogger(): any { |
| 15 | + const l: any = { info() {}, warn() {}, error() {}, debug() {} }; |
| 16 | + l.child = () => l; |
| 17 | + return l; |
| 18 | +} |
| 19 | + |
| 20 | +function collectingLogger(warns: string[]): any { |
| 21 | + const l: any = { info() {}, warn(m: string) { warns.push(m); }, error() {}, debug() {} }; |
| 22 | + l.child = () => l; |
| 23 | + return l; |
| 24 | +} |
| 25 | + |
| 26 | +function fakeData() { |
| 27 | + const calls: Array<{ op: string; obj: string; opts?: any }> = []; |
| 28 | + const data: any = { |
| 29 | + async find(obj: string, opts: any) { calls.push({ op: 'find', obj, opts }); return [{ id: 'r1' }]; }, |
| 30 | + async findOne(obj: string, opts: any) { calls.push({ op: 'findOne', obj, opts }); return { id: 'r1' }; }, |
| 31 | + async insert(obj: string, fields: any) { calls.push({ op: 'insert', obj, opts: { fields } }); return { id: `${obj}_1`, ...fields }; }, |
| 32 | + async update(obj: string, fields: any, opts: any) { calls.push({ op: 'update', obj, opts: { ...opts, fields } }); return { ok: true }; }, |
| 33 | + async delete(obj: string, opts: any) { calls.push({ op: 'delete', obj, opts }); return { ok: true }; }, |
| 34 | + }; |
| 35 | + return { data, calls }; |
| 36 | +} |
| 37 | + |
| 38 | +const ctxWith = (data: any, logger: any): any => ({ |
| 39 | + logger, |
| 40 | + getService: (n: string) => (n === 'data' ? data : undefined), |
| 41 | +}); |
| 42 | + |
| 43 | +function getRecordFlow(config: Record<string, unknown>) { |
| 44 | + return { |
| 45 | + name: 'gr', label: 'G', type: 'autolaunched', |
| 46 | + nodes: [ |
| 47 | + { id: 'start', type: 'start', label: 'Start' }, |
| 48 | + { id: 'g', type: 'get_record', label: 'Get', config }, |
| 49 | + { id: 'end', type: 'end', label: 'End' }, |
| 50 | + ], |
| 51 | + edges: [ |
| 52 | + { id: 'e1', source: 'start', target: 'g' }, |
| 53 | + { id: 'e2', source: 'g', target: 'end' }, |
| 54 | + ], |
| 55 | + } as any; |
| 56 | +} |
| 57 | + |
| 58 | +describe('CRUD config-key alias deprecation (object→objectName, filters→filter)', () => { |
| 59 | + beforeEach(() => __resetAliasDeprecationWarnings()); |
| 60 | + |
| 61 | + it('still resolves the deprecated `object` + `filters` aliases at runtime', async () => { |
| 62 | + const engine = new AutomationEngine(silentLogger()); |
| 63 | + const { data, calls } = fakeData(); |
| 64 | + const warns: string[] = []; |
| 65 | + registerCrudNodes(engine, ctxWith(data, collectingLogger(warns))); |
| 66 | + |
| 67 | + engine.registerFlow('gr', getRecordFlow({ object: 'crm_lead', filters: { id: 'L1' }, outputVariable: 'lead' })); |
| 68 | + const res = await engine.execute('gr'); |
| 69 | + |
| 70 | + expect(res.success).toBe(true); |
| 71 | + // The alias values reached the data engine unchanged. |
| 72 | + expect(calls).toHaveLength(1); |
| 73 | + expect(calls[0].obj).toBe('crm_lead'); |
| 74 | + expect(calls[0].opts.where).toEqual({ id: 'L1' }); |
| 75 | + }); |
| 76 | + |
| 77 | + it('warns once per alias, naming the canonical key', async () => { |
| 78 | + const engine = new AutomationEngine(silentLogger()); |
| 79 | + const { data } = fakeData(); |
| 80 | + const warns: string[] = []; |
| 81 | + registerCrudNodes(engine, ctxWith(data, collectingLogger(warns))); |
| 82 | + |
| 83 | + engine.registerFlow('gr', getRecordFlow({ object: 'crm_lead', filters: { id: 'L1' } })); |
| 84 | + await engine.execute('gr'); |
| 85 | + |
| 86 | + const objectWarn = warns.find((w) => w.includes("'object'") && w.includes("'objectName'")); |
| 87 | + const filterWarn = warns.find((w) => w.includes("'filters'") && w.includes("'filter'")); |
| 88 | + expect(objectWarn).toBeTruthy(); |
| 89 | + expect(filterWarn).toBeTruthy(); |
| 90 | + |
| 91 | + // Second run in the same process must NOT warn again (one-time per alias). |
| 92 | + const before = warns.length; |
| 93 | + await engine.execute('gr'); |
| 94 | + expect(warns.length).toBe(before); |
| 95 | + }); |
| 96 | + |
| 97 | + it('does NOT warn when the canonical keys are used', async () => { |
| 98 | + const engine = new AutomationEngine(silentLogger()); |
| 99 | + const { data } = fakeData(); |
| 100 | + const warns: string[] = []; |
| 101 | + registerCrudNodes(engine, ctxWith(data, collectingLogger(warns))); |
| 102 | + |
| 103 | + engine.registerFlow('gr', getRecordFlow({ objectName: 'crm_lead', filter: { id: 'L1' }, outputVariable: 'lead' })); |
| 104 | + const res = await engine.execute('gr'); |
| 105 | + |
| 106 | + expect(res.success).toBe(true); |
| 107 | + expect(warns).toHaveLength(0); |
| 108 | + }); |
| 109 | +}); |
0 commit comments