-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathrecord-change-integration.test.ts
More file actions
326 lines (293 loc) · 14 KB
/
Copy pathrecord-change-integration.test.ts
File metadata and controls
326 lines (293 loc) · 14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license.
/**
* End-to-end integration test for the record-change trigger (#1491).
*
* #1491 reported that record-change flows never fired on data writes (observed
* 7.4.1–7.7.0). The existing unit tests only exercised a *fake* data engine, so
* they never covered the real path: a flow pulled into the automation engine,
* the trigger binding to an ObjectQL lifecycle hook on `kernel:ready`, an actual
* insert firing that hook, and the flow's `update_record` writing back through
* the live data engine. This test boots a real kernel (ObjectQL + automation +
* record-change trigger + in-memory driver) and asserts the full chain — in BOTH
* registration orderings, since the engine relies on re-activating already-pulled
* flows when the trigger registers later.
*/
import { describe, it, expect } from 'vitest';
import { ObjectKernel } from '@objectstack/core';
import { ObjectQLPlugin } from '@objectstack/objectql';
import { AutomationServicePlugin, type AutomationEngine } from '@objectstack/service-automation';
import { RecordChangeTriggerPlugin } from './plugin.js';
const sleep = (ms: number) => new Promise((r) => setTimeout(r, ms));
/**
* A tiny equality-WHERE in-memory driver — enough to exercise the real engine's
* insert/update/find path without pulling a driver package as a dependency
* (mirrors objectql's own real-engine test helper). One record store per object.
*/
function makeMemoryDriver(): any {
const stores = new Map<string, Map<string, Record<string, unknown>>>();
const storeFor = (obj: string) => {
let s = stores.get(obj);
if (!s) { s = new Map(); stores.set(obj, s); }
return s;
};
let nextId = 0;
const matches = (row: Record<string, unknown>, where: any): boolean => {
if (!where || typeof where !== 'object') return true;
if (Array.isArray(where.$and)) return where.$and.every((w: any) => matches(row, w));
if (Array.isArray(where.$or)) return where.$or.some((w: any) => matches(row, w));
for (const [k, v] of Object.entries(where)) {
if (k.startsWith('$')) continue;
const expected = v && typeof v === 'object' && '$eq' in (v as any) ? (v as any).$eq : v;
const a = row[k] === undefined ? null : row[k];
const b = expected === undefined ? null : expected;
if (a !== b) return false;
}
return true;
};
return {
name: 'memory', version: '0.0.0', supports: {},
async connect() {}, async disconnect() {}, async checkHealth() { return true; },
async execute() { return null; }, async syncSchema() {},
async find(object: string, ast: any) {
return Array.from(storeFor(object).values()).filter((r) => matches(r, ast?.where));
},
findStream() { throw new Error('not implemented'); },
async findOne(object: string, ast: any) {
for (const r of storeFor(object).values()) if (matches(r, ast?.where)) return r;
return null;
},
async create(object: string, data: Record<string, unknown>) {
nextId += 1;
const id = (data.id as string) ?? `r_${nextId}`;
const row = { ...data, id };
storeFor(object).set(id, row);
return row;
},
async update(object: string, id: string, data: Record<string, unknown>) {
const s = storeFor(object);
const cur = s.get(id);
if (!cur) throw new Error(`not found: ${object}/${id}`);
const updated = { ...cur, ...data, id };
s.set(id, updated);
return updated;
},
async upsert(object: string, data: Record<string, unknown>) {
const id = data.id as string | undefined;
if (id && storeFor(object).has(id)) return this.update(object, id, data);
return this.create(object, data);
},
async delete(object: string, id: string) { return storeFor(object).delete(id); },
async count(object: string, ast: any) { return (await this.find(object, ast)).length; },
async bulkCreate(object: string, rows: Record<string, unknown>[]) {
return Promise.all(rows.map((r) => this.create(object, r)));
},
async bulkUpdate() { return []; }, async bulkDelete() {},
async beginTransaction() { return { commit: async () => {}, rollback: async () => {} }; },
async commit() {}, async rollback() {},
};
}
/** A flow that stamps `stamp: 'done'` on the just-created record of `object`. */
function stampFlow(name: string, object: string) {
return {
name,
label: name,
type: 'autolaunched',
nodes: [
{ id: 'start', type: 'start', label: 'Start', config: { objectName: object, triggerType: 'record-after-create' } },
{ id: 'stamp', type: 'update_record', label: 'Stamp', config: { objectName: object, filter: { id: '{record.id}' }, fields: { stamp: 'done' } } },
{ id: 'end', type: 'end', label: 'End' },
],
edges: [
{ id: 'e1', source: 'start', target: 'stamp' },
{ id: 'e2', source: 'stamp', target: 'end' },
],
};
}
/**
* A `record-after-write` flow (create OR update, #3427) that mirrors the
* record's live `status` into `mirror` on every write. Its own update_record
* write-back also fires afterUpdate, so this doubles as coverage that the
* engine's re-entrancy guard suppresses the self-trigger loop a write flow now
* exposes (afterUpdate IS bound, unlike a create-only flow).
*/
function mirrorWriteFlow(name: string, object: string) {
return {
name,
label: name,
type: 'record_change',
nodes: [
{ id: 'start', type: 'start', label: 'Start', config: { objectName: object, triggerType: 'record-after-write' } },
{ id: 'mirror', type: 'update_record', label: 'Mirror', config: { objectName: object, filter: { id: '{record.id}' }, fields: { mirror: '{record.status}' } } },
{ id: 'end', type: 'end', label: 'End' },
],
edges: [
{ id: 'e1', source: 'start', target: 'mirror' },
{ id: 'e2', source: 'mirror', target: 'end' },
],
};
}
/**
* A `record-after-write` flow whose START CONDITION uses the create/update
* discrimination the write trigger enables (mirrors the showcase
* `UrgentTaskAlertFlow`): fire when a record is created urgent (`previous == null`)
* OR escalated to urgent (`previous.priority != 'urgent'`) — but NOT on a later
* save while already urgent. Validates that `previous == null` is truthy on the
* afterInsert leg (previous is absent on create) and that the engine's start-node
* condition gate short-circuits before touching `previous.priority` there.
*/
function urgentAlertFlow(name: string, object: string) {
return {
name,
label: name,
type: 'record_change',
nodes: [
{
id: 'start',
type: 'start',
label: 'Start',
config: {
objectName: object,
triggerType: 'record-after-write',
condition: "priority == 'urgent' && (previous == null || previous.priority != 'urgent')",
},
},
{ id: 'alert', type: 'update_record', label: 'Alert', config: { objectName: object, filter: { id: '{record.id}' }, fields: { alerted: 'yes' } } },
{ id: 'end', type: 'end', label: 'End' },
],
edges: [
{ id: 'e1', source: 'start', target: 'alert' },
{ id: 'e2', source: 'alert', target: 'end' },
],
};
}
const objectDef = (name: string) => ({
name,
label: name,
fields: {
status: { name: 'status', label: 'S', type: 'text' },
stamp: { name: 'stamp', label: 'St', type: 'text' },
mirror: { name: 'mirror', label: 'M', type: 'text' },
priority: { name: 'priority', label: 'P', type: 'text' },
alerted: { name: 'alerted', label: 'A', type: 'text' },
},
});
describe('record-change trigger — end-to-end (#1491)', () => {
it('fires a record-after-create flow registered AFTER the trigger (engine.registerFlow path)', async () => {
const kernel = new ObjectKernel({ logLevel: 'silent' });
await kernel.use(new ObjectQLPlugin());
await kernel.use(new AutomationServicePlugin());
await kernel.use(new RecordChangeTriggerPlugin());
await kernel.bootstrap();
const objectql = kernel.getService('objectql') as any;
const data = kernel.getService('data') as any;
const automation = kernel.getService<AutomationEngine>('automation');
objectql.registerDriver(makeMemoryDriver(), true);
objectql.registry.registerObject(objectDef('wid'), 'test', 'test');
automation.registerFlow('stamp_flow', stampFlow('stamp_flow', 'wid') as any);
// The flow bound to the trigger…
expect((automation as any).getActiveTriggerBindings()).toContainEqual({
flowName: 'stamp_flow',
triggerType: 'record_change',
});
const created = await data.insert('wid', { status: 'new' });
const id = Array.isArray(created) ? created[0]?.id : created?.id ?? created;
await sleep(200);
const row = await data.findOne('wid', { where: { id } });
expect(row?.stamp).toBe('done');
}, 15000);
it('fires a flow PULLED FROM THE REGISTRY at automation.start(), bound when the trigger registers on kernel:ready (production ordering)', async () => {
const flowDef = stampFlow('stamp_flow2', 'wid2');
// Seeds the driver + object + flow into the registry in start(), which runs
// before AutomationServicePlugin.start() pulls flows — the production
// sequence (metadata seeds → automation pulls → trigger binds on
// kernel:ready via re-activation of the already-registered flow).
const seeder = {
name: 'test.seeder',
type: 'standard',
version: '1.0.0',
dependencies: ['com.objectstack.engine.objectql'],
async init() {},
async start(ctx: any) {
const ql = ctx.getService('objectql');
ql.registerDriver(makeMemoryDriver(), true);
ql.registry.registerObject(objectDef('wid2'), 'test', 'test');
ql.registry.registerItem('flow', flowDef, 'name', 'test');
},
};
const kernel = new ObjectKernel({ logLevel: 'silent' });
await kernel.use(new ObjectQLPlugin());
await kernel.use(seeder as any);
await kernel.use(new AutomationServicePlugin());
await kernel.use(new RecordChangeTriggerPlugin());
await kernel.bootstrap();
const data = kernel.getService('data') as any;
const automation = kernel.getService<AutomationEngine>('automation');
// The registry-pulled flow bound to the trigger after kernel:ready.
expect((automation as any).getActiveTriggerBindings()).toContainEqual({
flowName: 'stamp_flow2',
triggerType: 'record_change',
});
const created = await data.insert('wid2', { status: 'new' });
const id = Array.isArray(created) ? created[0]?.id : created?.id ?? created;
await sleep(200);
const row = await data.findOne('wid2', { where: { id } });
expect(row?.stamp).toBe('done');
}, 15000);
it('a single record-after-write flow fires on BOTH create and update (#3427)', async () => {
const kernel = new ObjectKernel({ logLevel: 'silent' });
await kernel.use(new ObjectQLPlugin());
await kernel.use(new AutomationServicePlugin());
await kernel.use(new RecordChangeTriggerPlugin());
await kernel.bootstrap();
const objectql = kernel.getService('objectql') as any;
const data = kernel.getService('data') as any;
const automation = kernel.getService<AutomationEngine>('automation');
objectql.registerDriver(makeMemoryDriver(), true);
objectql.registry.registerObject(objectDef('wid3'), 'test', 'test');
automation.registerFlow('mirror_write', mirrorWriteFlow('mirror_write', 'wid3') as any);
expect((automation as any).getActiveTriggerBindings()).toContainEqual({
flowName: 'mirror_write',
triggerType: 'record_change',
});
// Create — the afterInsert leg fires; the flow mirrors status → mirror.
const created = await data.insert('wid3', { status: 'a' });
const id = Array.isArray(created) ? created[0]?.id : created?.id ?? created;
await sleep(200);
expect((await data.findOne('wid3', { where: { id } }))?.mirror).toBe('a');
// Update — the afterUpdate leg of the SAME flow fires; mirror re-syncs. (The
// flow's own write-back does not loop: the re-entrancy guard suppresses it.)
await data.update('wid3', { id, status: 'b' });
await sleep(200);
expect((await data.findOne('wid3', { where: { id } }))?.mirror).toBe('b');
}, 15000);
it('record-after-write start condition uses `previous == null` to discriminate create vs update (#3427)', async () => {
const kernel = new ObjectKernel({ logLevel: 'silent' });
await kernel.use(new ObjectQLPlugin());
await kernel.use(new AutomationServicePlugin());
await kernel.use(new RecordChangeTriggerPlugin());
await kernel.bootstrap();
const objectql = kernel.getService('objectql') as any;
const data = kernel.getService('data') as any;
const automation = kernel.getService<AutomationEngine>('automation');
objectql.registerDriver(makeMemoryDriver(), true);
objectql.registry.registerObject(objectDef('wid5'), 'test', 'test');
automation.registerFlow('urgent_alert', urgentAlertFlow('urgent_alert', 'wid5') as any);
// Create leg — a brand-new URGENT record: `previous == null` makes the
// condition true, so the flow fires on afterInsert (the create-discrimination
// pattern the docs/showcase advertise).
const urgent = await data.insert('wid5', { priority: 'urgent' });
const urgentId = Array.isArray(urgent) ? urgent[0]?.id : urgent?.id ?? urgent;
await sleep(200);
expect((await data.findOne('wid5', { where: { id: urgentId } }))?.alerted).toBe('yes');
// Create leg — a NON-urgent record: the condition is false, no fire.
const low = await data.insert('wid5', { priority: 'low' });
const lowId = Array.isArray(low) ? low[0]?.id : low?.id ?? low;
await sleep(200);
expect((await data.findOne('wid5', { where: { id: lowId } }))?.alerted).toBeFalsy();
// Update leg — escalate that low record to urgent: `previous.priority` was
// 'low', so the transition guard fires the flow on afterUpdate.
await data.update('wid5', { id: lowId, priority: 'urgent' });
await sleep(200);
expect((await data.findOne('wid5', { where: { id: lowId } }))?.alerted).toBe('yes');
}, 15000);
});