|
1 | 1 | import { describe, expect, it } from 'vitest'; |
2 | 2 |
|
3 | | -import { celEngine, rewriteTemporalEquality, collectCelRootIdentifiers } from './cel-engine'; |
| 3 | +import { buildEnv, celEngine, rewriteTemporalEquality, collectCelRootIdentifiers, timestampReturningFunctions } from './cel-engine'; |
4 | 4 | import { CEL_STDLIB_FUNCTIONS } from './validate'; |
5 | 5 | import { buildWorkCalendar } from './work-calendar'; |
6 | 6 | import type { Expression } from '@objectstack/spec'; |
@@ -594,8 +594,17 @@ describe('celEngine', () => { |
594 | 594 | }); |
595 | 595 |
|
596 | 596 | it('is idempotent — an already-coerced operand is not re-wrapped', () => { |
| 597 | + // [#38135] `date(…)` returns a Timestamp, so the call arm now wraps the |
| 598 | + // field opposite it, whatever that field holds; the coerced side is never |
| 599 | + // wrapped twice. A Date-valued `previous.due` passes through `date()`. |
597 | 600 | expect(rewriteTemporalEquality('date(record.due) == previous.due', mixed)) |
598 | | - .toBe('date(record.due) == previous.due'); |
| 601 | + .toBe('date(record.due) == date(previous.due)'); |
| 602 | + expect(rewriteTemporalEquality('date(record.due) == date(previous.due)', mixed)) |
| 603 | + .toBe('date(record.due) == date(previous.due)'); |
| 604 | + expect(celEngine.evaluate(cel('date(record.due) == previous.due'), mixed)).toEqual({ ok: true, value: true }); |
| 605 | + // Both ISO texts: the coerced side used to meet the other's text and answer a silent false. |
| 606 | + expect(celEngine.evaluate(cel('date(record.due) == previous.due'), |
| 607 | + { record: { due: '2026-06-20' }, previous: { due: '2026-06-20' } })).toEqual({ ok: true, value: true }); |
599 | 608 | }); |
600 | 609 |
|
601 | 610 | it('composes with the temporal-call arm on one expression', () => { |
@@ -850,3 +859,89 @@ describe('collectCelRootIdentifiers', () => { |
850 | 859 | expect(empty.ok).toBe(false); |
851 | 860 | }); |
852 | 861 | }); |
| 862 | + |
| 863 | +// [#38135] A date field compared with a call that RETURNS a Timestamp other than |
| 864 | +// the four clock functions. The business case: a flow that runs on the day a loan |
| 865 | +// falls due tomorrow (`record.due_date == addDays(today(), 1)`). The field reaches |
| 866 | +// the run as its `YYYY-MM-DD` text; the #3183 equality rewrite paired it only with |
| 867 | +// `today` / `now` / `daysFromNow` / `daysAgo`, so the comparison answered a silent |
| 868 | +// `false` (and `!=` a silent `true`) on the very day it should match, and the flow |
| 869 | +// was skipped as "condition not met". `daysFromNow(1)` matched. Both rewrites now |
| 870 | +// ask one set read off the environment's declarations. |
| 871 | +describe('a date field compared with any Timestamp-returning call (#38135)', () => { |
| 872 | + const now = new Date('2026-06-20T08:00:00Z'); |
| 873 | + const rec = (due: unknown) => ({ now, record: { due } }); |
| 874 | + |
| 875 | + it('the rewrite wraps the field opposite addDays / addMonths / date / datetime / timestamp', () => { |
| 876 | + expect(rewriteTemporalEquality('record.due == addDays(today(), 1)')).toBe('date(record.due) == addDays(today(), 1)'); |
| 877 | + expect(rewriteTemporalEquality('addMonths(today(), 1) != previous.due')).toBe('addMonths(today(), 1) != date(previous.due)'); |
| 878 | + expect(rewriteTemporalEquality('due == date("2026-06-21")')).toBe('date(due) == date("2026-06-21")'); |
| 879 | + expect(rewriteTemporalEquality('record.due == datetime("2026-06-21T00:00:00Z")')).toBe('date(record.due) == datetime("2026-06-21T00:00:00Z")'); |
| 880 | + expect(rewriteTemporalEquality('record.due == timestamp("2026-06-21T00:00:00Z")')).toBe('date(record.due) == timestamp("2026-06-21T00:00:00Z")'); |
| 881 | + // A call that returns no Timestamp, and an already-coerced operand, are left alone. |
| 882 | + for (const src of ['daysBetween(today(), record.due) == 0', 'record.n == round(record.m)', 'date(record.due) == addDays(today(), 1)']) { |
| 883 | + expect(rewriteTemporalEquality(src), src).toBe(src); |
| 884 | + } |
| 885 | + }); |
| 886 | + |
| 887 | + it('matches on the day it names, as daysFromNow() does, and misses the day before', () => { |
| 888 | + for (const [src, control] of [ |
| 889 | + ['record.due == addDays(today(), 1)', 'record.due == daysFromNow(1)'], |
| 890 | + ['record.due != addDays(today(), 1)', 'record.due != daysFromNow(1)'], |
| 891 | + ['record.due == date("2026-06-21")', 'record.due == daysFromNow(1)'], |
| 892 | + ['record.due == timestamp("2026-06-21T00:00:00Z")', 'record.due == daysFromNow(1)'], |
| 893 | + ]) { |
| 894 | + for (const due of ['2026-06-21', '2026-06-20']) { |
| 895 | + expect(celEngine.evaluate(cel(src), rec(due)), `${src} over ${due}`).toEqual(celEngine.evaluate(cel(control), rec(due))); |
| 896 | + } |
| 897 | + } |
| 898 | + expect(celEngine.evaluate(cel('record.due == addDays(today(), 1)'), rec('2026-06-21'))).toEqual({ ok: true, value: true }); |
| 899 | + expect(celEngine.evaluate(cel('record.due != addDays(today(), 1)'), rec('2026-06-21'))).toEqual({ ok: true, value: false }); |
| 900 | + }); |
| 901 | + |
| 902 | + it('a string literal comparison beside it keeps its own answer', () => { |
| 903 | + expect(celEngine.evaluate(cel('record.due == "2026-06-19" || record.due == addDays(today(), 1)'), rec('2026-06-21'))) |
| 904 | + .toEqual({ ok: true, value: true }); |
| 905 | + expect(celEngine.evaluate(cel('record.due == "2026-06-19" || record.due == addDays(today(), 1)'), rec('2026-06-18'))) |
| 906 | + .toEqual({ ok: true, value: false }); |
| 907 | + }); |
| 908 | + |
| 909 | + // Drift pin: the set is read off `getDefinitions()`, so this enumerates every |
| 910 | + // global Timestamp signature the environment declares and needs a probe row for |
| 911 | + // each NAME. A function added with a Timestamp return fails here until it has |
| 912 | + // one, and every row must meet a date field's text under both rewrites. |
| 913 | + it('every Timestamp-returning function the environment declares meets a date field\'s text under ==, != and ordering', () => { |
| 914 | + const declared = [...new Set(buildEnv(() => now).getDefinitions().functions |
| 915 | + .filter((f) => f.receiverType === null && f.returnType === 'google.protobuf.Timestamp') |
| 916 | + .map((f) => f.name))].sort(); |
| 917 | + const probe: Record<string, string> = { |
| 918 | + now: 'now()', today: 'today()', daysFromNow: 'daysFromNow(3)', daysAgo: 'daysAgo(3)', |
| 919 | + addDays: 'addDays(today(), 3)', addMonths: 'addMonths(today(), 1)', |
| 920 | + date: 'date("2026-06-23")', datetime: 'datetime("2026-06-23T10:00:00Z")', |
| 921 | + timestamp: 'timestamp("2026-06-23T10:00:00Z")', |
| 922 | + addWorkingDays: 'addWorkingDays(today(), 2)', addWorkingHours: 'addWorkingHours(now(), 4)', |
| 923 | + }; |
| 924 | + expect(Object.keys(probe).sort(), 'a probe row per declared Timestamp-returning function').toEqual(declared); |
| 925 | + expect([...timestampReturningFunctions()].sort(), 'the set both rewrites ask').toEqual(declared); |
| 926 | + const weekday = '09:00-18:00'; |
| 927 | + const workCalendars = { default: buildWorkCalendar({ |
| 928 | + code: 'probe', timezone: 'UTC', maintained_from: '2025-01-01', maintained_through: '2027-12-31', |
| 929 | + monday_periods: weekday, tuesday_periods: weekday, wednesday_periods: weekday, |
| 930 | + thursday_periods: weekday, friday_periods: weekday, |
| 931 | + }, []) }; |
| 932 | + const missed: string[] = []; |
| 933 | + for (const name of declared) { |
| 934 | + const call = probe[name]; |
| 935 | + // The field holds the call's own instant as text, the way a datetime field reaches the run. |
| 936 | + const text = celEngine.evaluate(cel(`isoDatetime(${call})`), { now, workCalendars }); |
| 937 | + expect(text.ok, `isoDatetime(${call})`).toBe(true); |
| 938 | + if (!text.ok) continue; |
| 939 | + const ctx = { now, workCalendars, record: { at: text.value } }; |
| 940 | + for (const [op, expected] of [['==', true], ['!=', false], ['<', false], ['<=', true]] as const) { |
| 941 | + const r = celEngine.evaluate(cel(`record.at ${op} ${call}`), ctx); |
| 942 | + if (!(r.ok && r.value === expected)) missed.push(`record.at ${op} ${call}: ${JSON.stringify(r)}`); |
| 943 | + } |
| 944 | + } |
| 945 | + expect(missed).toEqual([]); |
| 946 | + }); |
| 947 | +}); |
0 commit comments