-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathscreen-nodes.test.ts
More file actions
239 lines (214 loc) · 11.1 KB
/
Copy pathscreen-nodes.test.ts
File metadata and controls
239 lines (214 loc) · 11.1 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
// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license.
import { describe, it, expect, beforeEach } from 'vitest';
import { AutomationEngine, type FlowFunctionHandler } from '../engine.js';
import { registerScreenNodes } from './screen-nodes.js';
function createTestLogger() {
return {
info: () => {},
warn: () => {},
error: () => {},
debug: () => {},
child: () => createTestLogger(),
} as any;
}
function createCtx() {
return { logger: createTestLogger(), getService: () => undefined } as any;
}
/** A one-`script`-node flow whose script node carries `config`. */
function scriptFlow(config: Record<string, unknown>) {
return {
name: 'script_flow',
label: 'Script Flow',
type: 'autolaunched' as const,
nodes: [
{ id: 'start', type: 'start' as const, label: 'Start' },
{ id: 'run', type: 'script' as const, label: 'Run', config },
{ id: 'end', type: 'end' as const, label: 'End' },
],
edges: [
{ id: 'e1', source: 'start', target: 'run' },
{ id: 'e2', source: 'run', target: 'end' },
],
};
}
describe('script node (#1870 — callable resolution)', () => {
let engine: AutomationEngine;
beforeEach(() => {
engine = new AutomationEngine(createTestLogger());
registerScreenNodes(engine, createCtx());
});
it('runs the built-in email side-effect', async () => {
engine.registerFlow('script_flow', scriptFlow({ actionType: 'email', template: 't', recipients: ['a'] }));
const result = await engine.execute('script_flow', {} as any);
expect(result.success).toBe(true);
});
it('invokes a registered function and captures its return value as output', async () => {
const calls: Array<Record<string, unknown>> = [];
const fn: FlowFunctionHandler = (c) => {
calls.push(c.input);
return { triaged: true, priority: 'high' };
};
engine.setFunctionResolver((name) => (name === 'helpdesk.aiTriageStub' ? fn : undefined));
engine.registerFlow('script_flow', scriptFlow({
function: 'helpdesk.aiTriageStub',
inputs: { ticket: 't_1' },
}));
const result = await engine.execute('script_flow', {} as any);
expect(result.success).toBe(true);
expect(calls).toEqual([{ ticket: 't_1' }]);
});
it('resolves a bare actionType that matches no built-in as a function name', async () => {
let called = false;
engine.setFunctionResolver((name) => (name === 'pm.aiRiskAssessmentStub' ? (() => { called = true; return 1; }) : undefined));
engine.registerFlow('script_flow', scriptFlow({ actionType: 'pm.aiRiskAssessmentStub' }));
const result = await engine.execute('script_flow', {} as any);
expect(result.success).toBe(true);
expect(called).toBe(true);
});
it('FAILS LOUDLY for an unregistered function instead of silently no-op (#1870)', async () => {
// No resolver wired → nothing resolves.
engine.registerFlow('script_flow', scriptFlow({ function: 'helpdesk.aiTriageStub' }));
const result = await engine.execute('script_flow', {} as any);
expect(result.success).toBe(false);
expect(result.error).toMatch(/aiTriageStub/);
expect(result.error).toMatch(/no function named|not a built-in/i);
});
it('recognizes inline config.script as a no-op (not a loud failure) — built-in runtime has no JS sandbox', async () => {
engine.registerFlow('script_flow', scriptFlow({ script: 'variables.x = 1;', outputVariables: ['x'] }));
const result = await engine.execute('script_flow', {} as any);
// Recognized form: succeeds (doesn't fail loud), but is documented as not executed.
expect(result.success).toBe(true);
});
it('FAILS LOUDLY when the script node declares no target at all (actionType: undefined repro)', async () => {
engine.registerFlow('script_flow', scriptFlow({ actionType: undefined }));
const result = await engine.execute('script_flow', {} as any);
expect(result.success).toBe(false);
expect(result.error).toMatch(/neither .*actionType.* nor .*function|nothing to run/i);
});
it('surfaces a thrown function as a loud step failure', async () => {
engine.setFunctionResolver(() => () => { throw new Error('boom'); });
engine.registerFlow('script_flow', scriptFlow({ function: 'explode' }));
const result = await engine.execute('script_flow', {} as any);
expect(result.success).toBe(false);
expect(result.error).toMatch(/explode.*failed|failed.*boom|boom/i);
});
it('resolves config.functionName as an alias for function (#1870 DX)', async () => {
let calledWith: any;
engine.setFunctionResolver((name) =>
name === 'helpdesk.aiTriageStub' ? ((c: any) => { calledWith = c.input; return { triaged: true }; }) : undefined);
engine.registerFlow('script_flow', scriptFlow({ actionType: 'invoke_function', functionName: 'helpdesk.aiTriageStub', inputs: { ticketId: 't1' } }));
const r = await engine.execute('script_flow', {} as any);
expect(r.success).toBe(true);
expect(calledWith).toEqual({ ticketId: 't1' });
});
it('treats actionType invoke_function as a marker, not a function name', async () => {
// invoke_function alone (no function/functionName) must NOT try to resolve a
// function literally named 'invoke_function'; it fails with a clear message.
engine.registerFlow('script_flow', scriptFlow({ actionType: 'invoke_function' }));
const r = await engine.execute('script_flow', {} as any);
expect(r.success).toBe(false);
expect(r.error).toMatch(/invoke_function.*requires.*function/i);
});
it('exposes the function result via outputVariable for downstream nodes (pure-function pattern)', async () => {
const seen: Array<Record<string, unknown>> = [];
engine.setFunctionResolver((name) => {
if (name === 'compute') return () => ({ ai_category: 'billing', ai_confidence: 0.9 });
if (name === 'consume') return ((c: any) => { seen.push(c.input); return null; });
return undefined;
});
engine.registerFlow('chain', {
name: 'chain', label: 'Chain', type: 'autolaunched',
nodes: [
{ id: 'start', type: 'start', label: 'Start' },
{ id: 'mk', type: 'script', label: 'compute', config: { function: 'compute', outputVariable: 'aiResult' } },
{ id: 'use', type: 'script', label: 'consume', config: { function: 'consume', inputs: { cat: '{aiResult.ai_category}', conf: '{aiResult.ai_confidence}' } } },
{ id: 'end', type: 'end', label: 'End' },
],
edges: [
{ id: 'e1', source: 'start', target: 'mk' },
{ id: 'e2', source: 'mk', target: 'use' },
{ id: 'e3', source: 'use', target: 'end' },
],
} as any);
const r = await engine.execute('chain', {} as any);
expect(r.success).toBe(true);
expect(seen).toEqual([{ cat: 'billing', conf: 0.9 }]);
});
});
/** A one-`screen`-node flow whose screen node carries `config`. */
function screenFlow(config: Record<string, unknown>) {
return {
name: 'screen_flow',
label: 'Screen Flow',
type: 'screen' as const,
nodes: [
{ id: 'start', type: 'start' as const, label: 'Start' },
{ id: 'collect', type: 'screen' as const, label: 'Collect', config },
{ id: 'end', type: 'end' as const, label: 'End' },
],
edges: [
{ id: 'e1', source: 'start', target: 'collect' },
{ id: 'e2', source: 'collect', target: 'end' },
],
};
}
describe('screen node — the field wire payload (#3528)', () => {
let engine: AutomationEngine;
beforeEach(() => {
engine = new AutomationEngine(createTestLogger());
registerScreenNodes(engine, createCtx());
});
/**
* `visibleWhen` has been on the screen node's designer form since #3304 but
* was dropped when the executor built the paused payload, so it reached no
* client and nothing honoured it. HotCRM's lead-conversion screen is the
* shape that made it fatal: an optional-by-design field that is `required`
* *when shown*. Rendered unconditionally, it blocks Submit on input the
* user was never asked for, and the run never resumes.
*/
it('forwards visibleWhen to the paused screen so the client can honour it', async () => {
engine.registerFlow('screen_flow', screenFlow({
title: 'Conversion Details',
fields: [
{ name: 'createOpportunity', label: 'Create Opportunity?', type: 'boolean', required: true },
{ name: 'opportunityName', label: 'Opportunity Name', type: 'text', required: true, visibleWhen: 'createOpportunity == true' },
{ name: 'opportunityAmount', label: 'Opportunity Amount', type: 'currency', visibleWhen: 'createOpportunity == true' },
],
}) as any);
const paused = await engine.execute('screen_flow');
expect(paused.status).toBe('paused');
const fields = paused.screen!.fields;
expect(fields.map((f) => f.visibleWhen)).toEqual([
undefined,
'createOpportunity == true',
'createOpportunity == true',
]);
// The conditional field keeps `required` — it is required *when shown*.
// Honouring one without the other is what dead-ends the run.
expect(fields[1]).toMatchObject({ name: 'opportunityName', required: true });
});
it('leaves visibleWhen undefined when the author declared none', async () => {
engine.registerFlow('screen_flow', screenFlow({
fields: [{ name: 'subject', label: 'Subject', type: 'text', required: true }],
}) as any);
const paused = await engine.execute('screen_flow');
expect(paused.screen!.fields[0].visibleWhen).toBeUndefined();
});
/**
* The predicate is re-evaluated by the client on every keystroke against
* the values collected so far, which the server cannot see. Interpolating
* it here would bake in a verdict from flow variables and freeze the field.
*/
it('forwards the predicate RAW — it is not interpolated against flow variables', async () => {
engine.registerFlow('screen_flow', screenFlow({
fields: [
{ name: 'tier', label: 'Tier', type: 'text' },
// `{recordId}` is a live flow variable; were the predicate
// interpolated it would come back with the value substituted.
{ name: 'note', label: 'Note', type: 'text', visibleWhen: 'tier == "gold" && "{recordId}" != ""' },
],
}) as any);
const paused = await engine.execute('screen_flow', { params: { recordId: 'lead_1' } } as any);
expect(paused.screen!.fields[1].visibleWhen).toBe('tier == "gold" && "{recordId}" != ""');
});
});