Repository navigation
Expand file tree
/
Copy pathvalidate-date-text-arithmetic.test.ts
More file actions
154 lines (142 loc) · 8.18 KB
/
Copy pathvalidate-date-text-arithmetic.test.ts
File metadata and controls
154 lines (142 loc) · 8.18 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
// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license.
/**
* Date subtraction the runtime cannot answer is refused at author time (#28944).
*
* The type-soundness gate declared a `date` / `datetime` field a Timestamp, so
* `record.end_date - record.start_date` checked as a legal `Duration` and
* built. The runtime never holds a Timestamp there: every read door hands a
* date field out as its text (`YYYY-MM-DD`, or the ISO-8601 instant for a
* date-time), so the evaluator faults on the subtraction (the formula reads
* null), and `today() - record.start_date` (the text coerced against a
* Timestamp) computes a `Duration`, which a field serialises as `{}`.
* `firstDateTextVerdict` re-checks what the Timestamp typing passes with the
* field declared as that text, so the gate's verdict falls out of the same
* typing the evaluator applies.
*
* Every row is judged twice — by the gate and by the evaluator over the row a
* read door serves — so the table pins that the gate refuses exactly the
* shapes that cannot produce a value, and nothing the evaluator answers.
* Message pins live with the other code pins in
* `expression-refusal-codes.test.ts`.
*/
import { describe, expect, it } from 'vitest';
import { celEngine } from './cel-engine';
import { validateExpression, type ExprSchemaHint } from './validate';
const RECORD_SCOPE: ExprSchemaHint = {
scope: 'record',
objectName: 'qa_doctor_time_off',
fields: ['title', 'start_date', 'end_date', 'start_at', 'end_at', 'amount', 'cost'],
fieldTypes: {
title: 'text',
start_date: 'date',
end_date: 'date',
start_at: 'datetime',
end_at: 'datetime',
amount: 'number',
cost: 'currency',
},
};
/** The row a read door serves for those fields: dates as their text. */
const SERVED_ROW = {
title: 't1',
start_date: '2026-11-01',
end_date: '2026-11-05',
start_at: '2026-11-01T10:00:00.000Z',
end_at: '2026-11-05T12:00:00.000Z',
amount: 10,
cost: 4,
};
/** What the evaluator gives a field computed from `source` over the served row. */
function runtimeReading(source: string): 'null' | '{}' | 'value' {
const r = celEngine.evaluate({ dialect: 'cel', source }, { record: SERVED_ROW });
if (!r.ok) return 'null';
return JSON.stringify(r.value) === '{}' ? '{}' : 'value';
}
function codesOf(role: 'value' | 'predicate', source: string): string[] {
const r = validateExpression(role, source, RECORD_SCOPE);
return r.errors.map((e) => e.code);
}
describe('date subtraction the runtime cannot answer (#28944)', () => {
// [role, source, the code the gate answers (null = clean), what the evaluator reads]
const ROWS: ReadonlyArray<readonly ['value' | 'predicate', string, string | null, 'null' | '{}' | 'value']> = [
// The card's shapes, refused.
['value', 'record.end_date - record.start_date', 'date-text-arithmetic', 'null'],
['value', 'today() - record.start_date', 'date-duration-value', '{}'],
// Datetime, measured: its text faults the same way, and a Timestamp minus it is a Duration.
['value', 'record.end_at - record.start_at', 'date-text-arithmetic', 'null'],
['value', 'now() - record.start_at', 'date-duration-value', '{}'],
['value', 'record.start_date - today()', 'date-duration-value', '{}'],
// The subtraction feeding a comparison still cannot produce a value.
['predicate', 'record.end_date - record.start_date > duration("72h")', 'date-text-arithmetic', 'null'],
['value', 'record.start_date - duration("24h")', 'date-text-arithmetic', 'null'],
// The `+ 1` shape keeps its own refusal (asserted word for word below).
['value', '(record.end_date - record.start_date) + 1', 'date-arithmetic', 'null'],
['value', '(today() - record.start_date) + 1', 'date-arithmetic', 'null'],
// Controls: every shape the evaluator answers stays clean.
['value', 'daysBetween(record.start_date, record.end_date)', null, 'value'],
['value', 'daysBetween(record.start_date, today())', null, 'value'],
['predicate', 'today() - record.start_date > duration("72h")', null, 'value'],
['value', '(today() - record.start_date).getHours()', null, 'value'],
['predicate', 'record.end_date < record.start_date', null, 'value'],
['predicate', 'record.start_date < today()', null, 'value'],
['predicate', 'record.start_at > now()', null, 'value'],
['predicate', 'record.start_date >= "2026-01-01"', null, 'value'],
['predicate', 'record.start_date == "2026-11-01"', null, 'value'],
['predicate', 'record.start_date != null && record.end_date != null', null, 'value'],
['value', '"Due: " + record.end_date', null, 'value'],
['value', 'addDays(record.start_date, 3)', null, 'value'],
['value', 'record.amount - record.cost', null, 'value'],
];
for (const [role, source, code, reads] of ROWS) {
it(`${role} \`${source}\` → ${code ?? 'clean'} (the evaluator reads ${reads})`, () => {
expect(runtimeReading(source)).toBe(reads);
expect(codesOf(role, source)).toEqual(code === null ? [] : [code]);
});
}
it('the `+ 1` shape keeps its words: the Timestamp typing still answers first', () => {
const r = validateExpression('value', '(record.end_date - record.start_date) + 1', RECORD_SCOPE);
expect(r.errors).toHaveLength(1);
expect(r.errors[0].code).toBe('date-arithmetic');
expect(r.errors[0].params).toEqual({ operands: 'google.protobuf.Duration + int', reference: 'record.start_date' });
});
it('names the field and prescribes daysBetween', () => {
const [span] = validateExpression('value', 'record.end_date - record.start_date', RECORD_SCOPE).errors;
expect(span.params).toEqual({ operands: 'DateText - DateText', reference: 'record.start_date' });
expect(span.message).toMatch(/evaluates to null/);
expect(span.message).toMatch(/daysBetween\(a, b\)/);
const [since] = validateExpression('value', 'today() - record.start_date', RECORD_SCOPE).errors;
expect(since.params).toEqual({ reference: 'record.start_date' });
expect(since.message).toMatch(/reads `\{\}`/);
expect(since.message).toMatch(/daysBetween\(a, b\)/);
});
it('judges a flattened (flow) condition the same way, under the flow codes (#30016)', () => {
const flattened: ExprSchemaHint = {
fields: ['start_date', 'end_date'],
fieldTypes: { start_date: 'date', end_date: 'date' },
};
// No declared outcome: the run fails wherever the name holds the date's text.
const refused = validateExpression('predicate', 'end_date - start_date > duration("72h")', flattened);
expect(refused.errors.map((e) => e.code)).toEqual(['date-text-arithmetic-run-may-fail']);
expect(refused.errors[0].params).toEqual({ operands: 'DateText - DateText', reference: 'start_date' });
expect(refused.errors[0].message).not.toMatch(/evaluates to null/);
// The run is proven to hold the field: it fails at the condition.
const proven = validateExpression('predicate', 'end_date - start_date > duration("72h")', { ...flattened, faultOutcome: 'run-fails' });
expect(proven.errors.map((e) => e.code)).toEqual(['date-text-arithmetic-run-fails']);
expect(proven.errors[0].message).toMatch(/this faults at runtime, and a flow condition that cannot be evaluated fails the run/);
// A node can rebind the bare names: not proven, whatever the outcome.
const unproven = validateExpression('predicate', 'end_date - start_date > duration("72h")', {
...flattened, faultOutcome: 'run-fails', bareNamesUnproven: true,
});
expect(unproven.errors.map((e) => e.code)).toEqual(['date-text-arithmetic-run-may-fail']);
expect(validateExpression('predicate', 'today() - start_date > duration("72h")', flattened).errors).toEqual([]);
});
it('a predicate that computes a duration is not a formula value, so only `value` refuses it', () => {
// A predicate's computed type is another check's question; the `{}` reading is a field's.
expect(codesOf('predicate', 'today() - record.start_date')).toEqual([]);
expect(codesOf('value', 'today() - record.start_date')).toEqual(['date-duration-value']);
});
it('without field types the check does not run, as before', () => {
const r = validateExpression('value', 'record.end_date - record.start_date', { scope: 'record' });
expect(r.errors).toEqual([]);
});
});