-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathvalidate.test.ts
More file actions
167 lines (143 loc) · 7.93 KB
/
Copy pathvalidate.test.ts
File metadata and controls
167 lines (143 loc) · 7.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
import { describe, it, expect } from 'vitest';
import { validateExpression, introspectScope, expectedDialect } 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);
});
});
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');
});
});
});