-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathnotify-node.test.ts
More file actions
284 lines (249 loc) · 11.4 KB
/
Copy pathnotify-node.test.ts
File metadata and controls
284 lines (249 loc) · 11.4 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
// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license.
import { describe, it, expect, beforeEach } from 'vitest';
import { AutomationEngine } from '../engine.js';
import { registerNotifyNode } from './notify-node.js';
import type { MessagingServiceSurface } from './notify-node.js';
function createTestLogger() {
return {
info: () => {},
warn: () => {},
error: () => {},
debug: () => {},
child: () => createTestLogger(),
} as any;
}
/**
* A PluginContext stub whose `messaging` service can be toggled on/off, so we
* exercise both the wired path and the degrade-when-absent path.
*/
function createCtx(messaging?: MessagingServiceSurface) {
return {
logger: createTestLogger(),
getService(name: string) {
if (name === 'messaging') return messaging;
return undefined;
},
} as any;
}
/** A fake messaging service capturing emitted notifications. */
function fakeMessaging() {
const emitted: any[] = [];
const service: MessagingServiceSurface = {
async emit(n) {
emitted.push(n);
return { notificationId: 'evt_1', delivered: n.audience.length, failed: 0 };
},
};
return { service, emitted };
}
function notifyFlow(config: Record<string, unknown>) {
return {
name: 'notify_flow',
label: 'Notify Flow',
type: 'autolaunched' as const,
variables: [
{ name: 'dealName', type: 'text' as const, isInput: true },
{ name: 'dealId', type: 'text' as const, isInput: true },
{ name: 'notify.delivered', type: 'number' as const, isOutput: true },
],
nodes: [
{ id: 'start', type: 'start' as const, label: 'Start' },
{ id: 'notify', type: 'notify' as const, label: 'Notify', config },
{ id: 'end', type: 'end' as const, label: 'End' },
],
edges: [
{ id: 'e1', source: 'start', target: 'notify' },
{ id: 'e2', source: 'notify', target: 'end' },
],
};
}
describe('notify (baseline node)', () => {
it('publishes a builtin io descriptor in the action registry', () => {
const engine = new AutomationEngine(createTestLogger());
registerNotifyNode(engine, createCtx());
expect(engine.getRegisteredNodeTypes()).toContain('notify');
const descriptor = engine.getActionDescriptor('notify');
expect(descriptor?.source).toBe('builtin');
expect(descriptor?.category).toBe('io');
expect(descriptor?.paradigms).toEqual(
expect.arrayContaining(['flow', 'approval']),
);
});
describe('with a messaging service registered', () => {
let engine: AutomationEngine;
let messaging: ReturnType<typeof fakeMessaging>;
beforeEach(() => {
messaging = fakeMessaging();
engine = new AutomationEngine(createTestLogger());
registerNotifyNode(engine, createCtx(messaging.service));
});
it('emits a notification, interpolating recipients/title/body, and reports delivered count', async () => {
engine.registerFlow('notify_flow', notifyFlow({
topic: 'deal.won',
recipients: ['user_1', 'user_2'],
title: 'Deal {dealName} closed',
message: 'Congrats on {dealName}',
channels: ['inbox', 'email'],
severity: 'info',
actionUrl: '/opps/{dealId}',
}));
const result = await engine.execute('notify_flow', {
params: { dealName: 'Acme', dealId: '42' },
} as any);
expect(result.success).toBe(true);
expect(messaging.emitted).toHaveLength(1);
expect(messaging.emitted[0]).toMatchObject({
topic: 'deal.won',
audience: ['user_1', 'user_2'],
channels: ['inbox', 'email'],
severity: 'info',
payload: {
title: 'Deal Acme closed',
body: 'Congrats on Acme',
url: '/opps/42',
},
});
expect(result.output).toMatchObject({ 'notify.delivered': 2 });
});
it('forwards a click-through target via sourceObject/sourceId, interpolating the id (#2675)', async () => {
engine.registerFlow('notify_flow', notifyFlow({
recipients: ['user_1'],
title: 'Quote {dealName} approved',
message: 'Fill in the line items',
channels: ['inbox'],
sourceObject: 'mtc_quotation',
sourceId: '{dealId}',
}));
const result = await engine.execute('notify_flow', {
params: { dealName: 'Acme', dealId: 'q_42' },
} as any);
expect(result.success).toBe(true);
expect(messaging.emitted[0]).toMatchObject({
source: { object: 'mtc_quotation', id: 'q_42' },
});
});
// #4045 — this now proves the CONVERSION, not executor tolerance. The
// executor reads only the canonical `sourceObject`/`sourceId`; the nested
// form reaches them because `registerFlow` applies
// `flow-node-notify-config-aliases`, which lifts it. Verified by disabling
// the lift: `source` comes back `undefined` and this test fails.
it('accepts the nested source:{object,id} form and forwards actorId', async () => {
engine.registerFlow('notify_flow', notifyFlow({
recipients: ['user_1'],
title: 'Assigned to you',
source: { object: 'opportunity', id: '{dealId}' },
actorId: '{dealName}',
}));
const result = await engine.execute('notify_flow', {
params: { dealName: 'user_boss', dealId: '99' },
} as any);
expect(result.success).toBe(true);
expect(messaging.emitted[0]).toMatchObject({
source: { object: 'opportunity', id: '99' },
actorId: 'user_boss',
});
});
it('drops a half-specified target (object without id) rather than emitting a dead link', async () => {
engine.registerFlow('notify_flow', notifyFlow({
recipients: ['user_1'],
title: 'Heads up',
sourceObject: 'opportunity',
// no sourceId
}));
const result = await engine.execute('notify_flow');
expect(result.success).toBe(true);
expect(messaging.emitted[0].source).toBeUndefined();
});
// The deprecated aliases (`to`/`subject`/`body`/`url`) are rewritten to
// the canonical keys on rehydration — `registerFlow` runs the ADR-0087
// D2 conversion 'flow-node-notify-config-aliases' (#3796) — so a stored
// pre-protocol-17 flow keeps working while the executor reads canonical
// keys only.
it('canonicalizes a stored `url` key to `actionUrl` at load', async () => {
engine.registerFlow('notify_flow', notifyFlow({
recipients: ['user_1'],
title: 'Heads up',
url: '/opps/{dealId}',
}));
const result = await engine.execute('notify_flow', {
params: { dealId: '7' },
} as any);
expect(result.success).toBe(true);
expect(messaging.emitted[0].payload).toMatchObject({ url: '/opps/7' });
});
it('canonicalizes stored `to`/`subject` keys at load (single recipient string form)', async () => {
engine.registerFlow('notify_flow', notifyFlow({
to: 'user_9',
subject: 'Heads up',
}));
const result = await engine.execute('notify_flow');
expect(result.success).toBe(true);
expect(messaging.emitted[0]).toMatchObject({ audience: ['user_9'], payload: { title: 'Heads up' } });
});
it('fails the step when title is missing', async () => {
engine.registerFlow('notify_flow', notifyFlow({ recipients: ['user_1'] }));
const result = await engine.execute('notify_flow');
expect(result.success).toBe(false);
expect(result.error).toContain('title');
});
it('fails the step when no recipient is given', async () => {
engine.registerFlow('notify_flow', notifyFlow({ title: 'Hi' }));
const result = await engine.execute('notify_flow');
expect(result.success).toBe(false);
expect(result.error).toContain('recipient');
});
// ── framework#3582 — unresolved recipient templates ──────────────────
// `{record.owner.manager}` walks `.manager` on a scalar foreign-key id
// and resolves to nothing. `String(undefined)` used to make that the
// six-character audience member "undefined": the emit succeeded, the
// notification addressed nobody, and nothing anywhere said so.
it('never sends the literal "undefined" as an audience member', async () => {
engine.registerFlow('notify_flow', notifyFlow({
title: 'Escalated',
recipients: ['user_1', '{record.owner.manager}'],
}));
const result = await engine.execute('notify_flow', {
record: { id: 'case_1', owner: 'usr_7' },
} as any);
expect(result.success).toBe(true);
expect(messaging.emitted[0].audience).toEqual(['user_1']);
});
it('fails the step, naming the template, when every recipient resolves to nothing', async () => {
engine.registerFlow('notify_flow', notifyFlow({
title: 'Escalated',
recipients: ['{record.owner.manager}'],
}));
const result = await engine.execute('notify_flow', {
record: { id: 'case_1', owner: 'usr_7' },
} as any);
expect(result.success).toBe(false);
expect(result.error).toContain('{record.owner.manager}');
expect(result.error).toContain('expand');
expect(messaging.emitted).toHaveLength(0);
});
it('resolves a recipient hop when the start node expanded the relation (#3475)', async () => {
engine.registerFlow('notify_flow', notifyFlow({
title: 'Escalated',
recipients: ['{record.owner.manager}'],
}));
// An expanded relation arrives on the record as a nested object.
const result = await engine.execute('notify_flow', {
record: { id: 'case_1', owner: { id: 'usr_7', manager: 'usr_9' } },
} as any);
expect(result.success).toBe(true);
expect(messaging.emitted[0].audience).toEqual(['usr_9']);
});
});
describe('without a messaging service', () => {
it('degrades to a no-op success (skipped) rather than failing the flow', async () => {
const engine = new AutomationEngine(createTestLogger());
registerNotifyNode(engine, createCtx(undefined));
engine.registerFlow('notify_flow', notifyFlow({
recipients: ['user_1'],
title: 'Hi',
}));
const result = await engine.execute('notify_flow');
expect(result.success).toBe(true);
});
});
});