-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathvalidate.test.ts
More file actions
383 lines (330 loc) · 19.1 KB
/
Copy pathvalidate.test.ts
File metadata and controls
383 lines (330 loc) · 19.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
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
import { describe, it, expect } from 'vitest';
import { validateExpression, introspectScope, expectedDialect, inferExpressionType } from './validate';
describe('validateExpression (ADR-0032)', () => {
describe('predicates (CEL)', () => {
it('accepts a valid bare-CEL predicate', () => {
const r = validateExpression('predicate', 'record.rating >= 4');
expect(r.ok).toBe(true);
expect(r.errors).toHaveLength(0);
});
it('rejects the #1491 brace-in-CEL form with a corrective message', () => {
const r = validateExpression('predicate', '{record.rating} >= 4');
expect(r.ok).toBe(false);
expect(r.errors[0].message).toMatch(/map literal|bare reference|template brace/i);
expect(r.errors[0].message).toContain('record.rating');
expect(r.errors[0].source).toBe('{record.rating} >= 4');
});
it('rejects a CEL envelope placed in a template-only role', () => {
const r = validateExpression('template', { dialect: 'cel', source: 'record.x' });
expect(r.ok).toBe(false);
});
it('accepts an empty/absent expression (no-op)', () => {
expect(validateExpression('predicate', '').ok).toBe(true);
expect(validateExpression('predicate', null).ok).toBe(true);
});
// #1877 — a predicate calling an UNKNOWN function (e.g. `PRIOR()`, a typo'd
// `isBlnk()`) must be rejected at build/registration, not silently accepted
// and then no-op the flow at runtime. cel-js's type checker reports these as
// `found no matching overload`; the engine surfaces them as an invalid CEL
// predicate.
it('rejects an unknown function call (#1877)', () => {
const r = validateExpression('predicate', 'PRIOR(status) != "promoted"');
expect(r.ok).toBe(false);
expect(r.errors[0].message).toMatch(/invalid CEL predicate/i);
expect(r.errors[0].message).toMatch(/overload|PRIOR/);
});
it('rejects an unknown function even when guarded by a short-circuit (#1877)', () => {
const r = validateExpression('predicate', 'status == "promoted" && PRIOR(status) != "promoted"');
expect(r.ok).toBe(false);
});
it('still accepts a registered stdlib function (isBlank)', () => {
expect(validateExpression('predicate', '!isBlank(record.target_channels)').ok).toBe(true);
});
});
describe('templates', () => {
it('accepts a valid {{ path }} template', () => {
const r = validateExpression('template', 'Hot lead: {{ record.full_name }}');
expect(r.ok).toBe(true);
});
it('flags single-brace {x} in a template and suggests {{ }}', () => {
const r = validateExpression('template', 'Hi {record.name}');
expect(r.ok).toBe(false);
expect(r.errors[0].message).toMatch(/\{\{ record\.name \}\}|double braces/);
});
});
describe('schema-aware field existence (v1)', () => {
it('flags an unknown record field with a did-you-mean', () => {
const r = validateExpression('predicate', 'record.raitng >= 4', { objectName: 'crm_lead', fields: ['rating', 'status'] });
expect(r.ok).toBe(false);
expect(r.errors[0].message).toMatch(/unknown field `raitng`/);
expect(r.errors[0].message).toMatch(/did you mean `rating`/);
});
it('passes when fields exist', () => {
const r = validateExpression('predicate', 'record.rating >= 4 && record.status == "new"', { fields: ['rating', 'status'] });
expect(r.ok).toBe(true);
});
it('skips field checks when no schema is provided', () => {
expect(validateExpression('predicate', 'record.anything > 1').ok).toBe(true);
});
});
// #1928 — a bare top-level identifier is a silent bug in a `record`-scoped
// site (formula field / validation predicate) but correct in a `flattened`
// flow/automation condition. The validator must distinguish by `scope`.
describe('bare-reference detection by scope (#1928)', () => {
it('flags a bare field reference in a record-scoped predicate', () => {
const r = validateExpression('predicate', 'lead_score != null && lead_score > 100', { scope: 'record' });
expect(r.ok).toBe(false);
expect(r.errors[0].message).toMatch(/bare reference `lead_score`/);
expect(r.errors[0].message).toMatch(/record\.lead_score/);
});
it('flags a bare reference in a record-scoped value (formula) expression', () => {
const r = validateExpression('value', '(budget == null ? 0 : budget) - (spent == null ? 0 : spent)', { scope: 'record' });
expect(r.ok).toBe(false);
expect(r.errors[0].message).toMatch(/bare reference `(budget|spent)`/);
});
it('accepts the record-qualified form in a record-scoped site', () => {
const r = validateExpression('value', '(record.budget == null ? 0 : record.budget) - (record.spent == null ? 0 : record.spent)', { scope: 'record' });
expect(r.ok).toBe(true);
});
it('does NOT flag bare references in a flattened (flow) condition', () => {
// The record's fields are flattened to top-level for flow conditions, and
// flow variables share that namespace, so bare refs are correct here.
expect(validateExpression('predicate', 'status == "done" && previous.status != "done"', { scope: 'flattened' }).ok).toBe(true);
expect(validateExpression('predicate', 'budget > 100000', { scope: 'flattened' }).ok).toBe(true);
expect(validateExpression('predicate', 'expiring_deals.length > 0', { scope: 'flattened' }).ok).toBe(true);
});
it('defaults to flattened scope (no bare-ref flag) when scope is unset', () => {
expect(validateExpression('predicate', 'status == "done"').ok).toBe(true);
});
it('does not flag a null-guard on a record-qualified field (no type false-positive)', () => {
expect(validateExpression('predicate', 'record.lead_score != null && record.lead_score > 100', { scope: 'record' }).ok).toBe(true);
});
});
// #1928 tier 3 — flattened flow conditions reference fields bare, so a bare
// ref is not an error. A bare NON-field that is a near-miss of a known field
// is a likely typo → non-blocking warning (ok stays true).
describe('flow-condition typo warnings (#1928 tier 3)', () => {
const fields = ['stage', 'amount', 'status'] as const;
it('warns (does not error) on a likely field typo in a flattened condition', () => {
const r = validateExpression('predicate', 'stagee == "closed_won"', { objectName: 'crm_opportunity', fields, scope: 'flattened' });
expect(r.ok).toBe(true);
expect(r.errors).toHaveLength(0);
expect(r.warnings).toHaveLength(1);
expect(r.warnings[0].message).toMatch(/`stagee` is not a field/);
expect(r.warnings[0].message).toMatch(/did you mean `stage`/);
});
it('does not warn on a correct bare field reference', () => {
const r = validateExpression('predicate', 'stage == "closed_won" && previous.stage != "closed_won"', { objectName: 'crm_opportunity', fields, scope: 'flattened' });
expect(r.ok).toBe(true);
expect(r.warnings).toHaveLength(0);
});
it('does not warn on a flow variable that is far from any field name', () => {
const r = validateExpression('predicate', 'expiring_deals.length > 0', { objectName: 'crm_opportunity', fields, scope: 'flattened' });
expect(r.ok).toBe(true);
expect(r.warnings).toHaveLength(0);
});
it('emits no warnings without a field list (nothing to compare against)', () => {
const r = validateExpression('predicate', 'stagee == "x"', { scope: 'flattened' });
expect(r.ok).toBe(true);
expect(r.warnings).toHaveLength(0);
});
});
// #1928 tier 4 — a text/boolean field used with an arithmetic or ordering
// operator against a number faults at runtime (silent null). With per-field
// types the validator surfaces this as a NON-blocking warning, and — the
// design law — never flags a case the runtime tolerates (number/date fields,
// equality, string concat, null-guards).
describe('type-soundness warnings (#1928 tier 4)', () => {
const schema = {
objectName: 'crm_opportunity',
fields: ['name', 'amount', 'is_active', 'due', 'priority', 'title'] as const,
fieldTypes: {
name: 'text', title: 'textarea', amount: 'currency',
is_active: 'boolean', due: 'date', priority: 'select',
},
scope: 'record',
} as const;
it('warns (does not error) on a text field used in arithmetic against a number', () => {
const r = validateExpression('value', 'record.name * 2', schema);
expect(r.ok).toBe(true);
expect(r.errors).toHaveLength(0);
expect(r.warnings).toHaveLength(1);
expect(r.warnings[0].message).toMatch(/type mismatch/i);
expect(r.warnings[0].message).toMatch(/record\.name/);
expect(r.warnings[0].message).toMatch(/evaluates to null/);
});
it('warns on a text field ordered against a number', () => {
const r = validateExpression('predicate', 'record.title >= 5', schema);
expect(r.ok).toBe(true);
expect(r.warnings).toHaveLength(1);
expect(r.warnings[0].message).toMatch(/record\.title/);
});
it('warns on a boolean field used in arithmetic (always faults at runtime)', () => {
const r = validateExpression('value', 'record.is_active + 1', schema);
expect(r.ok).toBe(true);
expect(r.warnings).toHaveLength(1);
expect(r.warnings[0].message).toMatch(/boolean/i);
expect(r.warnings[0].message).toMatch(/record\.is_active/);
});
it('does NOT warn on number/currency arithmetic with an int literal (#1930 runtime fix)', () => {
// currency → dyn, so `amount / 100`, `amount * 2 - 50` never fault.
expect(validateExpression('value', 'record.amount / 100', schema).warnings).toHaveLength(0);
expect(validateExpression('value', 'record.amount * 2 - 50', schema).warnings).toHaveLength(0);
});
it('does NOT warn on a date field with an ORDERING comparison (they hydrate at runtime)', () => {
// Ordering ops fault → the engine's string-hydration retry fires → they work.
// (Equality `==`/`!=` does NOT — that is the #3183 silent-miss, covered in its
// own block below; this tier-4 check leaves it to the #3183 guardrail.)
expect(validateExpression('predicate', 'record.due <= daysFromNow(30)', schema).warnings).toHaveLength(0);
expect(validateExpression('predicate', 'record.due >= today()', schema).warnings).toHaveLength(0);
});
it('does NOT warn on a select field ordered against a number (option values may be numeric codes)', () => {
// select → dyn, so `priority >= 3` (a numeric-coded picklist) is not flagged.
expect(validateExpression('predicate', 'record.priority >= 3', schema).warnings).toHaveLength(0);
});
it('does NOT warn on heterogeneous equality (runtime-safe, returns false)', () => {
expect(validateExpression('predicate', 'record.name == 5', schema).warnings).toHaveLength(0);
expect(validateExpression('predicate', 'record.name != 5', schema).warnings).toHaveLength(0);
});
it('does NOT warn on string concatenation or a null-guard', () => {
expect(validateExpression('value', 'record.name + record.title', schema).warnings).toHaveLength(0);
expect(validateExpression('predicate', 'record.amount != null && record.amount > 0', schema).warnings).toHaveLength(0);
});
it('does not run without field types', () => {
// No fieldTypes → nothing to check.
expect(validateExpression('value', 'record.name * 2', { objectName: 'crm_opportunity', fields: schema.fields, scope: 'record' }).warnings).toHaveLength(0);
});
});
// #1928 tier 4 (flattened) — the same soundness check for bare-field flow /
// automation conditions. Fields are bound bare (`status - 1`); flow variables
// stay `dyn` and are never flagged.
describe('type-soundness warnings — flattened flow conditions (#1928 tier 4)', () => {
const schema = {
objectName: 'crm_opportunity',
fields: ['stage', 'amount', 'is_active', 'title'] as const,
fieldTypes: { stage: 'select', amount: 'currency', is_active: 'boolean', title: 'text' },
scope: 'flattened',
} as const;
it('warns on a bare text field used in arithmetic against a number', () => {
const r = validateExpression('predicate', 'title - 1 > 0', schema);
expect(r.ok).toBe(true);
expect(r.warnings).toHaveLength(1);
expect(r.warnings[0].message).toMatch(/type mismatch/i);
// Bare form — not `record.title`.
expect(r.warnings[0].message).toMatch(/`title`/);
expect(r.warnings[0].message).not.toMatch(/record\.title/);
});
it('warns on a bare boolean field used in arithmetic', () => {
const r = validateExpression('predicate', 'is_active + 1 > 0', schema);
expect(r.ok).toBe(true);
expect(r.warnings).toHaveLength(1);
expect(r.warnings[0].message).toMatch(/boolean/i);
});
it('does NOT flag a flow variable (unlisted → dyn) or number/date fields', () => {
// `expiring_count` is not a schema field → dyn → no fault.
expect(validateExpression('predicate', 'expiring_count * 2 > 10', schema).warnings).toHaveLength(0);
expect(validateExpression('predicate', 'amount / 100 > 5', schema).warnings).toHaveLength(0);
});
it('does NOT flag a correct bare condition, equality, or a select comparison', () => {
expect(validateExpression('predicate', 'stage == "closed_won" && amount > 1000', schema).warnings).toHaveLength(0);
expect(validateExpression('predicate', 'title == "VIP"', schema).warnings).toHaveLength(0);
});
});
describe('introspection', () => {
it('reports the dialect + scope for a field role', () => {
expect(expectedDialect('predicate')).toBe('cel');
expect(expectedDialect('template')).toBe('template');
const scope = introspectScope('predicate', { fields: ['rating'] });
expect(scope.dialect).toBe('cel');
expect(scope.fields).toContain('rating');
expect(scope.roots).toContain('record');
expect(scope.functions).toContain('daysFromNow');
});
});
});
describe('inferExpressionType — coarse value-type of a formula', () => {
// The host object's fields, so a bare `<field>` reference resolves the same as
// `record.<field>` (a stored formula may be written either way).
const fields = ['start_date', 'end_date', 'amount', 'rate', 'first', 'last', 'name', 'items'];
it('infers number for a computed-number formula (the leave_days repro)', () => {
// daysBetween(...): int, int + 1 → int → number. The exact case a "total
// leave days" dashboard card needs a SUM measure derived for.
expect(inferExpressionType('daysBetween(start_date, end_date) + 1', { fields })).toBe('number');
expect(inferExpressionType('daysBetween(record.start_date, record.end_date) + 1')).toBe('number');
expect(inferExpressionType('amount * 0.1', { fields })).toBe('number'); // dyn * double → double
expect(inferExpressionType('round(amount)', { fields })).toBe('number');
expect(inferExpressionType('len(items)', { fields })).toBe('number');
});
it('accepts the canonical Expression envelope as input', () => {
expect(inferExpressionType({ dialect: 'cel', source: 'amount * 0.1' }, { fields })).toBe('number');
});
it('infers text / boolean / date for non-numeric formulas', () => {
expect(inferExpressionType('upper(name)', { fields })).toBe('text');
expect(inferExpressionType('rate >= 0.5', { fields })).toBe('boolean');
expect(inferExpressionType('today()')).toBe('date');
});
it('is conservative — an ambiguous (dyn) result is unknown, never number', () => {
// `first + last` could be string concatenation OR numeric addition; with two
// untyped operands cel-js yields `dyn`, so we must NOT call it a number (else
// a dataset would SUM a text formula). This is the safety property.
expect(inferExpressionType('first + last', { fields })).toBe('unknown');
expect(inferExpressionType('amount + rate', { fields })).toBe('unknown');
});
it('returns unknown for empty, absent, or un-type-checkable expressions', () => {
expect(inferExpressionType('')).toBe('unknown');
expect(inferExpressionType(null)).toBe('unknown');
expect(inferExpressionType(undefined)).toBe('unknown');
expect(inferExpressionType('no_such_fn(amount)', { fields })).toBe('unknown'); // no overload
expect(inferExpressionType('undeclared_field + 1')).toBe('unknown'); // bare ref, no fields given
});
});
// #3183 — a `Field.date` reads back as a "YYYY-MM-DD" string, and cel-js's
// equality hard-codes `string == <timestamp>` to false, so `record.due_date ==
// today()` silently never matches. Advisory warning that guides the author to a
// working idiom. Derived from the shared `fieldTypes` hint (type === 'date').
describe('temporal date-equality guardrail (#3183)', () => {
const schema = {
objectName: 'task',
fields: ['due_date', 'status'] as const,
fieldTypes: { due_date: 'date', status: 'text' },
scope: 'record',
} as const;
const dateWarns = (src: string, s: Record<string, unknown> = schema) =>
validateExpression('predicate', src, s as never).warnings
.filter(w => /calendar-day \(date\) field/.test(w.message));
it('warns on `dateField == today()` (field on the left)', () => {
const r = validateExpression('predicate', 'record.due_date == today()', schema);
expect(r.ok).toBe(true); // advisory — never fails the build
expect(r.errors).toHaveLength(0);
const w = r.warnings.filter(x => /calendar-day \(date\) field/.test(x.message));
expect(w).toHaveLength(1);
expect(w[0].message).toMatch(/date\(record\.due_date\) == today\(\)/);
});
it('warns on `today() == dateField` and on daysFromNow/daysAgo/now', () => {
expect(dateWarns('today() == record.due_date')).toHaveLength(1);
expect(dateWarns('record.due_date == daysFromNow(3)')).toHaveLength(1);
expect(dateWarns('record.due_date != daysAgo(7)')).toHaveLength(1);
expect(dateWarns('record.due_date == now()')).toHaveLength(1);
});
it('warns on a bare date-field ref (flattened flow-condition scope)', () => {
const flat = { objectName: 'task', fields: ['due_date'], fieldTypes: { due_date: 'date' } };
expect(dateWarns('due_date == today()', flat)).toHaveLength(1);
});
it('does NOT warn on the working idioms', () => {
expect(dateWarns('date(record.due_date) == today()')).toHaveLength(0);
expect(dateWarns('record.due_date >= today() && record.due_date <= today()')).toHaveLength(0);
expect(dateWarns('daysBetween(today(), record.due_date) == 0')).toHaveLength(0);
});
it('does NOT warn on ordering comparisons (they fault→hydrate and work)', () => {
expect(dateWarns('record.due_date >= today()')).toHaveLength(0);
expect(dateWarns('record.due_date < daysFromNow(30)')).toHaveLength(0);
});
it('does NOT warn on a non-date field, or when fieldTypes is absent', () => {
expect(dateWarns('record.status == today()')).toHaveLength(0); // status is text, not date
expect(dateWarns('record.due_date == today()', { objectName: 'task', fields: ['due_date'], scope: 'record' }))
.toHaveLength(0); // no fieldTypes → check skipped
});
it('de-duplicates repeated references to the same field', () => {
expect(dateWarns('record.due_date == today() || record.due_date == daysFromNow(1)')).toHaveLength(1);
});
});