Skip to content

Commit 81c14ba

Browse files
committed
test(formula,lint): pin the fail-closed date sentence and the time-relative sweep sentence (red)
Claude-Session: https://claude.ai/code/session_01Utp6CbntgiC5D1vTyg2pBV Co-authored-by: Claude <noreply@anthropic.com>
1 parent c580081 commit 81c14ba

3 files changed

Lines changed: 201 additions & 10 deletions

File tree

‎packages/formula/src/expression-refusal-codes.test.ts‎

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,23 @@ const PINS: { readonly [C in ExpressionRefusalCode]: readonly [Pin<C>, ...Pin<C>
296296
'in whole days, and `daysFromNow(n)` / `addDays(d, n)` / `addMonths(d, n)` to shift a date.',
297297
},
298298
],
299+
// [#30250] A fail-closed site: the fault refuses the write, nothing reads null.
300+
'date-arithmetic-fail-closed': [
301+
{
302+
produce: () =>
303+
onlyError('predicate', 'record.close_date + 1 > 3', {
304+
scope: 'record',
305+
fieldTypes: { close_date: 'date' },
306+
faultOutcome: 'fail-closed',
307+
}),
308+
params: { operands: 'google.protobuf.Timestamp + int', reference: 'record.close_date' },
309+
message:
310+
'date arithmetic `google.protobuf.Timestamp + int` — `record.close_date` is a date, and CEL can\'t do arithmetic ' +
311+
'on dates: this faults at runtime, and here an expression that cannot be evaluated fails closed: the write (or the operation ' +
312+
'a hook guards) is refused, not skipped (it does not read null). ' +
313+
'Use `daysBetween(a, b)` for the span in whole days, and `daysFromNow(n)` / `addDays(d, n)` / `addMonths(d, n)` to shift a date.',
314+
},
315+
],
299316
// [#30016] A flattened (flow) condition: the fault fails the run, it never
300317
// reads null — `run-fails` where the run is proven to hold the field.
301318
'date-arithmetic-run-fails': [
@@ -343,6 +360,23 @@ const PINS: { readonly [C in ExpressionRefusalCode]: readonly [Pin<C>, ...Pin<C>
343360
'and `daysFromNow(n)` / `addDays(d, n)` / `addMonths(d, n)` to shift a date.',
344361
},
345362
],
363+
'date-text-arithmetic-fail-closed': [
364+
{
365+
produce: () =>
366+
onlyError('predicate', 'record.end_date - record.start_date > duration("24h")', {
367+
scope: 'record',
368+
fieldTypes: { start_date: 'date', end_date: 'date' },
369+
faultOutcome: 'fail-closed',
370+
}),
371+
params: { operands: 'DateText - DateText', reference: 'record.start_date' },
372+
message:
373+
'date arithmetic `DateText - DateText` — `record.start_date` is a date field, and the runtime holds a date field ' +
374+
'as its text (`YYYY-MM-DD`, or an ISO-8601 instant for a date-time), which CEL can\'t do arithmetic on: ' +
375+
'this faults at runtime, and here an expression that cannot be evaluated fails closed: the write (or the operation ' +
376+
'a hook guards) is refused, not skipped (it does not read null). ' +
377+
'Use `daysBetween(a, b)` for the span in whole days, and `daysFromNow(n)` / `addDays(d, n)` / `addMonths(d, n)` to shift a date.',
378+
},
379+
],
346380
'date-text-arithmetic-run-fails': [
347381
{
348382
produce: () =>
@@ -458,6 +492,24 @@ const PINS: { readonly [C in ExpressionRefusalCode]: readonly [Pin<C>, ...Pin<C>
458492
'length, `double(name)` for its value (which itself faults on text that is not a number).',
459493
},
460494
],
495+
// [#30250] The same outcome on a time-relative sweep, which no write fires.
496+
'type-mismatch-sweep-run-fails': [
497+
{
498+
produce: () =>
499+
onlyError('predicate', 'name > 5', {
500+
scope: 'flattened', fields: ['name'], fieldTypes: { name: 'text' }, faultOutcome: 'run-fails', timeRelativeSweep: true,
501+
}),
502+
params: { operands: 'string > int', operator: '>', held: 'text', reference: 'name' },
503+
message:
504+
'type mismatch `string > int` — `name` holds text but is used with `>` against a number. CEL has no such operation, so ' +
505+
'wherever the value is not numeric text (an empty value included) the expression faults, and here ' +
506+
'a flow condition that cannot be evaluated fails the run: the time-relative sweep starts one run per row it finds, ' +
507+
'the run for this row stops at this condition and the work the flow exists to do is not done for that row, and ' +
508+
'nothing else happens (the sweep logs the failure and goes on to the next row). ' +
509+
'Compare like with like: use a number field, compare text with text, or convert explicitly — `size(name)` for its ' +
510+
'length, `double(name)` for its value (which itself faults on text that is not a number).',
511+
},
512+
],
461513
'type-mismatch-fail-open': [
462514
{
463515
produce: () =>
@@ -690,6 +742,24 @@ const SWEEP_SCHEMAS: readonly (ExprSchemaHint | undefined)[] = [
690742
fieldTypes: { name: 'text', active: 'boolean' },
691743
faultOutcome,
692744
})),
745+
// [#30250] A fail-closed site with a date field reaches the date verdicts'
746+
// `-fail-closed` codes; a time-relative sweep's `run-fails` condition
747+
// reaches `type-mismatch-sweep-run-fails`.
748+
{
749+
scope: 'record',
750+
objectName: 'deal',
751+
fields: ['close_date'],
752+
fieldTypes: { close_date: 'date' },
753+
faultOutcome: 'fail-closed',
754+
},
755+
{
756+
scope: 'flattened',
757+
objectName: 'deal',
758+
fields: ['amount', 'name', 'active'],
759+
fieldTypes: { name: 'text', active: 'boolean' },
760+
faultOutcome: 'run-fails',
761+
timeRelativeSweep: true,
762+
},
693763
// [#29959] A flattened (flow) condition with no declared outcome reaches
694764
// `type-mismatch-run-may-fail`, bare and through `record.`.
695765
{ scope: 'flattened', objectName: 'deal', fields: ['amount', 'name', 'active'], fieldTypes: { name: 'text', active: 'boolean' } },

‎packages/formula/src/validate.test.ts‎

Lines changed: 64 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -806,6 +806,33 @@ describe('validateExpression (ADR-0032)', () => {
806806
}
807807
});
808808

809+
// [#30250] A time-relative sweep's run is `run-fails` too (it binds a row
810+
// of the typed object), but no write fires it: the sweep starts one run per
811+
// row it finds. Its sentence names no write, under its own code.
812+
it('on a time-relative sweep says the run for that row fails and names no write (#30250)', () => {
813+
const sweep = { ...schema, scope: 'flattened' as const, faultOutcome: 'run-fails' as const, timeRelativeSweep: true };
814+
for (const [source, reference] of [['name > 5', 'name'], ['record.name > 5', 'record.name']] as const) {
815+
const r = validateExpression('predicate', source, sweep);
816+
expect(r.warnings, source).toHaveLength(0);
817+
expect(r.errors.map((e) => [e.code, e.params]), source).toEqual([
818+
['type-mismatch-sweep-run-fails', { operands: 'string > int', operator: '>', held: 'text', reference }],
819+
]);
820+
expect(r.errors[0].message, source).toContain(
821+
'a flow condition that cannot be evaluated fails the run: the time-relative sweep starts one run per row it ' +
822+
'finds, the run for this row stops at this condition and the work the flow exists to do is not done for that ' +
823+
'row, and nothing else happens',
824+
);
825+
expect(r.errors[0].message, source).not.toMatch(/still lands|write that fired|evaluates to null|is refused/);
826+
}
827+
// The key reads only with the `run-fails` outcome: with none declared the
828+
// warning is the same as without it.
829+
const w = validateExpression('predicate', 'name > 5', { ...schema, scope: 'flattened', timeRelativeSweep: true });
830+
expect(w.warnings.map((e) => e.code)).toEqual(['type-mismatch-run-may-fail']);
831+
// The date verdict's run-fails sentence names no write already: unchanged.
832+
const d = validateExpression('predicate', 'due + 1 > 3', sweep);
833+
expect(d.errors.map((e) => e.code)).toEqual(['date-arithmetic-run-fails']);
834+
});
835+
809836
// [#29959] A flow condition reads the field bare AND as `record.<field>`
810837
// (`seedRunVariables` binds both to the trigger record), so the record
811838
// read gets the bare name's verdict: here the run-fails error.
@@ -1086,10 +1113,43 @@ describe('validateExpression (ADR-0032)', () => {
10861113
}
10871114
// A bare name a flow variable holds is not the field: no verdict.
10881115
expect(codeOf({ faultOutcome: 'run-fails', flowVariables: ['start_date'] })).toEqual([]);
1089-
// Control: a record-scoped validation rule keeps its code and sentence.
1090-
const rule = validateExpression('predicate', 'record.start_date + 1 > 3', { ...schema, faultOutcome: 'fail-closed' });
1091-
expect(rule.errors.map((e) => e.code)).toEqual(['date-arithmetic']);
1092-
expect(rule.errors[0].message).toMatch(/this faults at runtime, so the field silently evaluates to null/);
1116+
// Control: a record-scoped site with no declared outcome (a formula
1117+
// field) keeps its code and sentence; a fail-closed rule is #30250's.
1118+
const field = validateExpression('value', 'record.start_date + 1', schema);
1119+
expect(field.errors.map((e) => e.code)).toEqual(['date-arithmetic']);
1120+
expect(field.errors[0].message).toMatch(/this faults at runtime, so the field silently evaluates to null/);
1121+
});
1122+
1123+
// [#30250] On a fail-closed site (an `error` validation rule, a field
1124+
// `requiredWhen` / `readonlyWhen`, an option `visibleWhen`, a hook
1125+
// `condition`) a fault refuses the write: nothing reads null. The date
1126+
// verdicts there take the fail-closed sentence under their own codes.
1127+
it('on a fail-closed site says the write is refused, not that the field reads null (#30250)', () => {
1128+
const closed = { ...schema, faultOutcome: 'fail-closed' } as const;
1129+
for (const [source, code] of [
1130+
['record.start_date + 1 > 3', 'date-arithmetic-fail-closed'],
1131+
['record.end_date - record.start_date > duration("24h")', 'date-text-arithmetic-fail-closed'],
1132+
] as const) {
1133+
const r = validateExpression('predicate', source, closed);
1134+
expect(r.warnings, source).toHaveLength(0);
1135+
expect(r.errors.map((e) => e.code), source).toEqual([code]);
1136+
expect(r.errors[0].message, source).toContain(
1137+
'this faults at runtime, and here an expression that cannot be evaluated fails closed: the write (or the ' +
1138+
'operation a hook guards) is refused, not skipped (it does not read null).',
1139+
);
1140+
expect(r.errors[0].message, source).not.toMatch(/evaluates to null/);
1141+
}
1142+
// Controls: a formula field (no outcome) keeps the null sentence; so does
1143+
// an advisory rule (`fail-open`), which #30250 does not cover.
1144+
for (const [role, source, hint, code] of [
1145+
['value', 'record.start_date + 1', schema, 'date-arithmetic'],
1146+
['value', 'record.end_date - record.start_date', schema, 'date-text-arithmetic'],
1147+
['predicate', 'record.start_date + 1 > 3', { ...schema, faultOutcome: 'fail-open' }, 'date-arithmetic'],
1148+
] as const) {
1149+
const r = validateExpression(role, source, hint);
1150+
expect(r.errors.map((e) => e.code), source).toEqual([code]);
1151+
expect(r.errors[0].message, source).toContain('this faults at runtime, so the field silently evaluates to null');
1152+
}
10931153
});
10941154
});
10951155

‎packages/lint/src/validate-expressions.test.ts‎

Lines changed: 67 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1146,13 +1146,56 @@ describe('validateStackExpressions (ADR-0032 build-time)', () => {
11461146
[`flow '${name}' · edge 'e1' (start→route) condition`, severity],
11471147
]);
11481148
for (const i of found) {
1149+
// [#30250] No write fires a sweep's run: its sentence names none.
11491150
expect(i.message, `${name} ${i.where}`).toContain(
1150-
severity === 'error' ? 'the write that fired it still lands' : 'so this is a warning, not a refusal',
1151+
severity === 'error'
1152+
? 'the time-relative sweep starts one run per row it finds, the run for this row stops at this condition'
1153+
: 'so this is a warning, not a refusal',
11511154
);
1155+
expect(i.message, `${name} ${i.where}`).not.toMatch(/still lands|write that fired/);
11521156
}
11531157
}
11541158
});
11551159

1160+
// [#30250] A time-relative sweep's run is fired by no write. The type
1161+
// verdict's run-fails sentence says the run for that row fails and nothing
1162+
// else happens; the nullable-comparison twin is not served on a sweep at
1163+
// all (it runs on record-triggered flows only). Control: the same flow
1164+
// record-triggered keeps the sentence about the write that fired it.
1165+
it('names no firing write on a time-relative sweep, and keeps it on a record-triggered flow (#30250)', () => {
1166+
const objects = [{
1167+
name: 'crm_opportunity',
1168+
fields: { title: { type: 'text', required: true }, budget: { type: 'number' }, close_date: { type: 'date' } },
1169+
}];
1170+
const flowOf = (name: string, startConfig: Record<string, unknown>) => ({
1171+
name,
1172+
nodes: [
1173+
{ id: 'start', type: 'start', config: { objectName: 'crm_opportunity', condition: 'title > 5', ...startConfig } },
1174+
{ id: 'end', type: 'end' },
1175+
],
1176+
edges: [{ id: 'e1', source: 'start', target: 'end', condition: 'budget > 100' }],
1177+
});
1178+
const sweep = validateStackExpressions({
1179+
objects,
1180+
flows: [flowOf('tr_sweep', {
1181+
schedule: '0 * * * *', timeRelative: { object: 'crm_opportunity', dateField: 'close_date', offsetDays: 3 },
1182+
})],
1183+
});
1184+
const sweepType = typeMismatches(sweep);
1185+
expect(sweepType.map(i => i.severity)).toEqual(['error']);
1186+
expect(sweepType[0]!.message).toContain('and nothing else happens (the sweep logs the failure and goes on to the next row)');
1187+
for (const i of sweep) expect(i.message, i.where).not.toMatch(/still lands|write that fired/);
1188+
const recordTriggered = validateStackExpressions({
1189+
objects, flows: [flowOf('rt', { triggerType: 'record-after-update' })],
1190+
});
1191+
const rtType = typeMismatches(recordTriggered);
1192+
expect(rtType.map(i => i.severity)).toEqual(['error']);
1193+
expect(rtType[0]!.message).toContain('the write that fired it still lands');
1194+
// The null-guard twin on the record-triggered edge: its sentence too.
1195+
expect(recordTriggered.filter(i => /budget/.test(i.message) && /still lands/.test(i.message)).map(i => i.where))
1196+
.toEqual(["flow 'rt' · edge 'e1' (start→end) condition"]);
1197+
});
1198+
11561199
// [#30026] Where the sweep is of another object, the author is told why the
11571200
// verdict is only a warning: once, at the start node's `objectName`, and
11581201
// only on a flow that was served a warning it explains.
@@ -1308,17 +1351,35 @@ describe('validateStackExpressions (ADR-0032 build-time)', () => {
13081351
}
13091352
});
13101353

1311-
it('keeps the null sentence on a record-scoped validation rule\'s date arithmetic (#30016 control)', () => {
1354+
// [#30250] An `error` validation rule fails closed: its date arithmetic
1355+
// refuses the write, so it says so. Control: a formula field's date
1356+
// arithmetic reads null, and keeps the null sentence.
1357+
it('says a fail-closed validation rule\'s date arithmetic refuses the write; a formula keeps the null sentence (#30250)', () => {
13121358
const issues = validateStackExpressions({
13131359
objects: [{
13141360
name: 'crm_opportunity',
1315-
fields: { close_date: { type: 'date' } },
1316-
validations: [{ name: 'late', condition: 'record.close_date + 1 > 3' }],
1361+
fields: {
1362+
close_date: { type: 'date' },
1363+
end_date: { type: 'date' },
1364+
next_day: { type: 'formula', expression: 'record.close_date + 1' },
1365+
},
1366+
validations: [
1367+
{ name: 'late', condition: 'record.close_date + 1 > 3' },
1368+
{ name: 'long', condition: 'record.end_date - record.close_date > duration("24h")' },
1369+
],
13171370
}],
13181371
});
13191372
const found = issues.filter(i => /^date arithmetic/.test(i.message));
1320-
expect(found.map(i => i.severity)).toEqual(['error']);
1321-
expect(found[0]!.message).toContain('this faults at runtime, so the field silently evaluates to null');
1373+
expect(found.map(i => i.severity)).toEqual(['error', 'error', 'error']);
1374+
const rules = found.filter(i => /validation/.test(i.where));
1375+
expect(rules).toHaveLength(2);
1376+
for (const i of rules) {
1377+
expect(i.message, i.where).toContain('fails closed: the write (or the operation a hook guards) is refused, not skipped');
1378+
expect(i.message, i.where).not.toMatch(/evaluates to null/);
1379+
}
1380+
const formula = found.filter(i => !/validation/.test(i.where));
1381+
expect(formula).toHaveLength(1);
1382+
expect(formula[0]!.message).toContain('this faults at runtime, so the field silently evaluates to null');
13221383
});
13231384

13241385
// [#29959] `seedRunVariables` seeds the flow's declared variables before it

0 commit comments

Comments
 (0)