-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathvalidate-expressions.test.ts
More file actions
236 lines (220 loc) · 8.93 KB
/
Copy pathvalidate-expressions.test.ts
File metadata and controls
236 lines (220 loc) · 8.93 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
import { describe, it, expect } from 'vitest';
import { validateStackExpressions } from './validate-expressions.js';
describe('validateStackExpressions (ADR-0032 build-time)', () => {
const objects = [
{ name: 'crm_lead', fields: { rating: { type: 'number' }, status: { type: 'text' } } },
];
it('passes a clean stack', () => {
const issues = validateStackExpressions({
objects,
flows: [{
name: 'lead_flow',
nodes: [
{ id: 'start', type: 'start', config: { objectName: 'crm_lead' } },
{ id: 'check', type: 'decision', config: { condition: 'record.rating >= 4' } },
],
edges: [{ id: 'e1', source: 'check', target: 'end', condition: 'record.rating < 4' }],
}],
});
expect(issues).toHaveLength(0);
});
it('flags a brace-in-CEL condition with location + corrective message', () => {
const issues = validateStackExpressions({
objects,
flows: [{
name: 'lead_flow',
nodes: [
{ id: 'start', type: 'start', config: { objectName: 'crm_lead' } },
{ id: 'check', type: 'decision', config: { condition: '{record.rating} >= 4' } },
],
edges: [],
}],
});
expect(issues).toHaveLength(1);
expect(issues[0].where).toContain("flow 'lead_flow'");
expect(issues[0].where).toContain("node 'check'");
expect(issues[0].message).toMatch(/map literal|bare reference/);
expect(issues[0].source).toBe('{record.rating} >= 4');
});
it('flags an unknown record field against the resolved schema (did-you-mean)', () => {
const issues = validateStackExpressions({
objects,
flows: [{
name: 'lead_flow',
nodes: [
{ id: 'start', type: 'start', config: { objectName: 'crm_lead' } },
{ id: 'check', type: 'decision', config: { condition: 'record.raitng >= 4' } },
],
edges: [],
}],
});
expect(issues).toHaveLength(1);
expect(issues[0].message).toMatch(/unknown field `raitng`/);
expect(issues[0].message).toMatch(/did you mean `rating`/);
});
it('validates object validation-rule predicates too', () => {
const issues = validateStackExpressions({
objects: [
{ name: 'crm_lead', fields: { rating: {} }, validations: [{ name: 'r1', expression: '{record.rating} > 0' }] },
],
});
expect(issues).toHaveLength(1);
expect(issues[0].where).toContain("validation 'r1'");
});
// #1870 — a `script` node that names no callable is a silent no-op.
it('flags a script node that declares neither actionType nor function (#1870)', () => {
const issues = validateStackExpressions({
flows: [{
name: 'helpdesk_flow',
nodes: [
{ id: 'start', type: 'start', config: {} },
{ id: 'triage', type: 'script', config: { actionType: undefined } },
],
edges: [],
}],
});
expect(issues).toHaveLength(1);
expect(issues[0].where).toContain("node 'triage' (script) callable");
expect(issues[0].message).toMatch(/neither .*actionType.* nor .*function/);
});
it('accepts a script node that names a built-in action or a function (#1870)', () => {
const issues = validateStackExpressions({
flows: [{
name: 'helpdesk_flow',
nodes: [
{ id: 'start', type: 'start', config: {} },
{ id: 'mail', type: 'script', config: { actionType: 'email' } },
{ id: 'triage', type: 'script', config: { function: 'helpdesk.aiTriageStub' } },
{ id: 'inline', type: 'script', config: { script: 'variables.x = 1;' } },
],
edges: [],
}],
});
expect(issues).toHaveLength(0);
});
// #1870 DX — `functionName` is an accepted alias for `function`.
it('accepts a script node that names a callable via the functionName alias', () => {
const issues = validateStackExpressions({
flows: [{
name: 'helpdesk_flow',
nodes: [
{ id: 'start', type: 'start', config: {} },
{ id: 'triage', type: 'script', config: { actionType: 'invoke_function', functionName: 'helpdesk.aiTriageStub' } },
],
edges: [],
}],
});
expect(issues).toHaveLength(0);
});
it('flags actionType invoke_function with no function/functionName', () => {
const issues = validateStackExpressions({
flows: [{
name: 'helpdesk_flow',
nodes: [
{ id: 'start', type: 'start', config: {} },
{ id: 'triage', type: 'script', config: { actionType: 'invoke_function', inputs: { x: 1 } } },
],
edges: [],
}],
});
expect(issues).toHaveLength(1);
expect(issues[0].message).toMatch(/invoke_function.*no .*function/i);
});
// #1928 — bare field references are silently null in `record`-scoped sites
// (field formulas + validation predicates) but correct in flattened flow
// conditions. The validator wires the scope per site.
describe('bare-reference detection by site scope (#1928)', () => {
it('flags a bare reference in a field formula', () => {
const issues = validateStackExpressions({
objects: [{
name: 'crm_opportunity',
fields: {
amount: { type: 'currency' },
probability: { type: 'percent' },
expected_revenue: { type: 'formula', name: 'expected_revenue', formula: 'amount * probability / 100' },
},
}],
});
expect(issues).toHaveLength(1);
expect(issues[0].where).toContain("field 'expected_revenue' formula");
expect(issues[0].message).toMatch(/bare reference `(amount|probability)`/);
});
it('flags a bare reference in a validation predicate', () => {
const issues = validateStackExpressions({
objects: [{
name: 'crm_lead',
fields: { lead_score: { type: 'number' } },
validations: [{ name: 'lead_score_range', expression: 'lead_score != null && lead_score > 100' }],
}],
});
expect(issues).toHaveLength(1);
expect(issues[0].where).toContain("validation 'lead_score_range'");
expect(issues[0].message).toMatch(/bare reference `lead_score`/);
});
it('accepts the record-qualified forms', () => {
const issues = validateStackExpressions({
objects: [{
name: 'crm_opportunity',
fields: {
amount: { type: 'currency' },
probability: { type: 'percent' },
expected_revenue: { type: 'formula', name: 'expected_revenue', formula: 'record.amount * record.probability / 100' },
},
validations: [{ name: 'amt', expression: 'record.amount != null && record.amount >= 0' }],
}],
});
expect(issues).toHaveLength(0);
});
it('does NOT flag bare references in a flow condition (flattened scope)', () => {
const issues = validateStackExpressions({
objects: [{ name: 'crm_opportunity', fields: { stage: { type: 'select' }, amount: { type: 'currency' } } }],
flows: [{
name: 'high_value_deal',
nodes: [
{ id: 'start', type: 'start', config: { objectName: 'crm_opportunity', condition: 'amount > 100000 && previous.amount <= 100000' } },
],
edges: [{ id: 'e1', source: 'start', target: 'end', condition: 'stage != "closed_won"' }],
}],
});
expect(issues).toHaveLength(0);
});
// #1928 tier 3 — a likely field typo in a flow condition is a non-blocking warning.
it('warns (severity=warning) on a likely field typo in a flow condition', () => {
const issues = validateStackExpressions({
objects: [{ name: 'crm_opportunity', fields: { stage: { type: 'select' }, amount: { type: 'currency' } } }],
flows: [{
name: 'opp_won',
nodes: [
{ id: 'start', type: 'start', config: { objectName: 'crm_opportunity', condition: 'stagee == "closed_won"' } },
],
edges: [],
}],
});
expect(issues).toHaveLength(1);
expect(issues[0].severity).toBe('warning');
expect(issues[0].message).toMatch(/did you mean `stage`/);
});
it('does not warn when the bare ref is far from any field (likely a flow variable)', () => {
const issues = validateStackExpressions({
objects: [{ name: 'crm_opportunity', fields: { stage: { type: 'select' } } }],
flows: [{
name: 'renewal',
nodes: [{ id: 'start', type: 'start', config: { objectName: 'crm_opportunity' } }],
edges: [{ id: 'e1', source: 'start', target: 'end', condition: 'expiring_deals.length > 0' }],
}],
});
expect(issues).toHaveLength(0);
});
it('tags record-scoped bare-ref issues as errors', () => {
const issues = validateStackExpressions({
objects: [{
name: 'crm_lead',
fields: { lead_score: { type: 'number' } },
validations: [{ name: 'r', expression: 'lead_score > 100' }],
}],
});
expect(issues).toHaveLength(1);
expect(issues[0].severity).toBe('error');
});
});
});