Skip to content

Commit 89eebee

Browse files
committed
test(formula,lint): pin an advisory rule's date verdicts to the fail-open sentence (red)
Claude-Session: https://claude.ai/code/session_01Utp6CbJFWadws6f9r4B8Ke Co-authored-by: Claude <noreply@anthropic.com>
1 parent ebda9ac commit 89eebee

3 files changed

Lines changed: 118 additions & 7 deletions

File tree

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

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,26 @@ const PINS: { readonly [C in ExpressionRefusalCode]: readonly [Pin<C>, ...Pin<C>
313313
'Use `daysBetween(a, b)` for the span in whole days, and `daysFromNow(n)` / `addDays(d, n)` / `addMonths(d, n)` to shift a date.',
314314
},
315315
],
316+
// [#30494] An advisory (fail-open) rule: the fault skips the rule and the
317+
// write proceeds, nothing reads null.
318+
'date-arithmetic-fail-open': [
319+
{
320+
produce: () =>
321+
onlyError('predicate', 'record.close_date + 1 > 3', {
322+
scope: 'record',
323+
fieldTypes: { close_date: 'date' },
324+
faultOutcome: 'fail-open',
325+
}),
326+
params: { operands: 'google.protobuf.Timestamp + int', reference: 'record.close_date' },
327+
message:
328+
'date arithmetic `google.protobuf.Timestamp + int` — `record.close_date` is a date, and CEL can\'t do arithmetic ' +
329+
'on dates: ' +
330+
'this faults at runtime, and here the rule is advisory, so a rule that cannot be evaluated is skipped, not refused: ' +
331+
'the write proceeds, the rule is never applied (its advisory never fires), and the only trace is a log line saying ' +
332+
'it could not be evaluated. ' +
333+
'Use `daysBetween(a, b)` for the span in whole days, and `daysFromNow(n)` / `addDays(d, n)` / `addMonths(d, n)` to shift a date.',
334+
},
335+
],
316336
// [#30016] A flattened (flow) condition: the fault fails the run, it never
317337
// reads null — `run-fails` where the run is proven to hold the field.
318338
'date-arithmetic-run-fails': [
@@ -377,6 +397,24 @@ const PINS: { readonly [C in ExpressionRefusalCode]: readonly [Pin<C>, ...Pin<C>
377397
'Use `daysBetween(a, b)` for the span in whole days, and `daysFromNow(n)` / `addDays(d, n)` / `addMonths(d, n)` to shift a date.',
378398
},
379399
],
400+
'date-text-arithmetic-fail-open': [
401+
{
402+
produce: () =>
403+
onlyError('predicate', 'record.end_date - record.start_date > duration("24h")', {
404+
scope: 'record',
405+
fieldTypes: { start_date: 'date', end_date: 'date' },
406+
faultOutcome: 'fail-open',
407+
}),
408+
params: { operands: 'DateText - DateText', reference: 'record.start_date' },
409+
message:
410+
'date arithmetic `DateText - DateText` — `record.start_date` is a date field, and the runtime holds a date field ' +
411+
'as its text (`YYYY-MM-DD`, or an ISO-8601 instant for a date-time), which CEL can\'t do arithmetic on: ' +
412+
'this faults at runtime, and here the rule is advisory, so a rule that cannot be evaluated is skipped, not refused: ' +
413+
'the write proceeds, the rule is never applied (its advisory never fires), and the only trace is a log line saying ' +
414+
'it could not be evaluated. ' +
415+
'Use `daysBetween(a, b)` for the span in whole days, and `daysFromNow(n)` / `addDays(d, n)` / `addMonths(d, n)` to shift a date.',
416+
},
417+
],
380418
'date-text-arithmetic-run-fails': [
381419
{
382420
produce: () =>
@@ -752,6 +790,15 @@ const SWEEP_SCHEMAS: readonly (ExprSchemaHint | undefined)[] = [
752790
fieldTypes: { close_date: 'date' },
753791
faultOutcome: 'fail-closed',
754792
},
793+
// [#30494] The same date schema on an advisory rule reaches the date
794+
// verdicts' `-fail-open` codes.
795+
{
796+
scope: 'record',
797+
objectName: 'deal',
798+
fields: ['close_date'],
799+
fieldTypes: { close_date: 'date' },
800+
faultOutcome: 'fail-open',
801+
},
755802
{
756803
scope: 'flattened',
757804
objectName: 'deal',

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

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1139,14 +1139,45 @@ describe('validateExpression (ADR-0032)', () => {
11391139
);
11401140
expect(r.errors[0].message, source).not.toMatch(/evaluates to null/);
11411141
}
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'],
1142+
// Controls: a formula field (no outcome) keeps the null sentence. An
1143+
// advisory rule (`fail-open`) is #30494's, below.
1144+
for (const [role, source, code] of [
1145+
['value', 'record.start_date + 1', 'date-arithmetic'],
1146+
['value', 'record.end_date - record.start_date', 'date-text-arithmetic'],
11481147
] as const) {
1149-
const r = validateExpression(role, source, hint);
1148+
const r = validateExpression(role, source, schema);
1149+
expect(r.errors.map((e) => e.code), source).toEqual([code]);
1150+
expect(r.errors[0].message, source).toContain('this faults at runtime, so the field silently evaluates to null');
1151+
}
1152+
});
1153+
1154+
// [#30494] On an advisory (`warning` / `info`) validation rule a fault
1155+
// skips the rule and the write proceeds: nothing reads null. The date
1156+
// verdicts there close on the `type-mismatch-fail-open` clause, under their
1157+
// own codes. The grade stays an error.
1158+
it('on an advisory rule says the rule is skipped and the write proceeds, not that the field reads null (#30494)', () => {
1159+
const open = { ...schema, faultOutcome: 'fail-open' } as const;
1160+
for (const [source, code] of [
1161+
['record.start_date + 1 > 3', 'date-arithmetic-fail-open'],
1162+
['record.end_date - record.start_date > duration("24h")', 'date-text-arithmetic-fail-open'],
1163+
] as const) {
1164+
const r = validateExpression('predicate', source, open);
1165+
expect(r.warnings, source).toHaveLength(0);
1166+
expect(r.errors.map((e) => e.code), source).toEqual([code]);
1167+
expect(r.errors[0].message, source).toContain(
1168+
'this faults at runtime, and here the rule is advisory, so a rule that cannot be evaluated is skipped, not ' +
1169+
'refused: the write proceeds, the rule is never applied (its advisory never fires), and the only trace is a ' +
1170+
'log line saying it could not be evaluated.',
1171+
);
1172+
expect(r.errors[0].message, source).not.toMatch(/evaluates to null/);
1173+
}
1174+
// Control: the same predicates on a formula-field site (no outcome) keep
1175+
// their codes and the null sentence.
1176+
for (const [source, code] of [
1177+
['record.start_date + 1 > 3', 'date-arithmetic'],
1178+
['record.end_date - record.start_date > duration("24h")', 'date-text-arithmetic'],
1179+
] as const) {
1180+
const r = validateExpression('predicate', source, schema);
11501181
expect(r.errors.map((e) => e.code), source).toEqual([code]);
11511182
expect(r.errors[0].message, source).toContain('this faults at runtime, so the field silently evaluates to null');
11521183
}

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1382,6 +1382,39 @@ describe('validateStackExpressions (ADR-0032 build-time)', () => {
13821382
expect(formula[0]!.message).toContain('this faults at runtime, so the field silently evaluates to null');
13831383
});
13841384

1385+
// [#30494] An advisory (`warning` / `info`) validation rule fails open: a
1386+
// fault skips the rule and the write proceeds, so its date arithmetic says
1387+
// so, still an error. Control: a formula field keeps the null sentence.
1388+
it('says an advisory validation rule\'s date arithmetic skips the rule; a formula keeps the null sentence (#30494)', () => {
1389+
const issues = validateStackExpressions({
1390+
objects: [{
1391+
name: 'crm_opportunity',
1392+
fields: {
1393+
close_date: { type: 'date' },
1394+
end_date: { type: 'date' },
1395+
next_day: { type: 'formula', expression: 'record.close_date + 1' },
1396+
},
1397+
validations: [
1398+
{ name: 'late', condition: 'record.close_date + 1 > 3', severity: 'warning' },
1399+
{ name: 'long', condition: 'record.end_date - record.close_date > duration("24h")', severity: 'info' },
1400+
],
1401+
}],
1402+
});
1403+
const found = issues.filter(i => /^date arithmetic/.test(i.message));
1404+
expect(found.map(i => i.severity)).toEqual(['error', 'error', 'error']);
1405+
const rules = found.filter(i => /validation/.test(i.where));
1406+
expect(rules).toHaveLength(2);
1407+
for (const i of rules) {
1408+
expect(i.message, i.where).toContain(
1409+
'the rule is advisory, so a rule that cannot be evaluated is skipped, not refused: the write proceeds',
1410+
);
1411+
expect(i.message, i.where).not.toMatch(/evaluates to null|is refused, not skipped/);
1412+
}
1413+
const formula = found.filter(i => !/validation/.test(i.where));
1414+
expect(formula).toHaveLength(1);
1415+
expect(formula[0]!.message).toContain('this faults at runtime, so the field silently evaluates to null');
1416+
});
1417+
13851418
// [#29959] `seedRunVariables` seeds the flow's declared variables before it
13861419
// flattens the record, and the flattening skips a name already bound: a
13871420
// bare `title` here reads the NUMBER variable and `title > 5` runs. Only

0 commit comments

Comments
 (0)