-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathuseExpression.test.ts
More file actions
220 lines (181 loc) · 8.46 KB
/
Copy pathuseExpression.test.ts
File metadata and controls
220 lines (181 loc) · 8.46 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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/**
* Tests for useExpression and useCondition hooks
*/
import { describe, it, expect, vi } from 'vitest';
import { renderHook } from '@testing-library/react';
import { createElement } from 'react';
import { useExpression, useCondition, useRowPredicate, toPredicateInput, PredicateScopeProvider } from '../useExpression';
describe('useExpression', () => {
it('returns string value directly for non-expression strings', () => {
const { result } = renderHook(() => useExpression('hello'));
expect(result.current).toBe('hello');
});
it('returns number value directly', () => {
const { result } = renderHook(() => useExpression(42));
expect(result.current).toBe(42);
});
it('returns boolean value directly', () => {
const { result } = renderHook(() => useExpression(true));
expect(result.current).toBe(true);
});
it('returns null for null expressions', () => {
const { result } = renderHook(() => useExpression(null));
expect(result.current).toBeNull();
});
it('returns undefined for undefined expressions', () => {
const { result } = renderHook(() => useExpression(undefined));
expect(result.current).toBeUndefined();
});
it('evaluates expressions with ${...} syntax', () => {
const context = { data: { name: 'John' } };
const { result } = renderHook(() =>
useExpression('${data.name}', context),
);
expect(result.current).toBe('John');
});
it('evaluates expressions with context data', () => {
const context = { data: { age: 25 } };
const { result } = renderHook(() =>
useExpression('${data.age > 18}', context),
);
expect(result.current).toBe(true);
});
});
describe('useCondition', () => {
it('returns true for boolean true', () => {
const { result } = renderHook(() => useCondition(true));
expect(result.current).toBe(true);
});
it('returns false for boolean false', () => {
const { result } = renderHook(() => useCondition(false));
expect(result.current).toBe(false);
});
it('returns true for undefined', () => {
const { result } = renderHook(() => useCondition(undefined));
expect(result.current).toBe(true);
});
it('evaluates string conditions with context', () => {
const context = { data: { status: 'active' } };
const { result } = renderHook(() =>
useCondition('${data.status === "active"}', context),
);
expect(result.current).toBe(true);
});
describe('{ throwOnError: true } — fail-closed opt-in (mirrors ActionEngine)', () => {
// A bare reference to an undeclared identifier — not merely a property
// access on an existing object — genuinely throws a ReferenceError from
// the compiled expression, the same shape of failure ActionEngine's own
// fail-closed regression test uses (a predicate referencing missing context).
const THROWING = '${nonexistentIdentifier.field}';
it('defaults to fail-OPEN (true) on a throwing predicate when not requested', () => {
const { result } = renderHook(() => useCondition(THROWING, { data: {} }));
expect(result.current).toBe(true);
});
it('fails CLOSED (false) on a throwing predicate when requested', () => {
const { result } = renderHook(() =>
useCondition(THROWING, { data: {} }, { throwOnError: true }),
);
expect(result.current).toBe(false);
});
it('still evaluates normally (no change) when the predicate does not throw', () => {
const context = { data: { status: 'active' } };
const { result } = renderHook(() =>
useCondition('${data.status === "active"}', context, { throwOnError: true }),
);
expect(result.current).toBe(true);
});
it('warns ONCE with the label when a fail-closed predicate throws (#2358)', () => {
const warn = vi.spyOn(console, 'warn').mockImplementation(() => {});
try {
const SRC = '${undeclaredWarnProbe2358.field}';
const { unmount } = renderHook(() =>
useCondition(SRC, { data: {} }, { throwOnError: true, label: 'action "probe_2358" (visible)' }),
);
const matching = () =>
warn.mock.calls.filter(c => String(c[0]).includes('probe_2358'));
expect(matching()).toHaveLength(1);
expect(String(matching()[0][0])).toMatch(/hidden\/disabled/);
expect(String(matching()[0][0])).toContain(SRC);
// Remount with the same predicate → deduped, no second warning.
unmount();
renderHook(() =>
useCondition(SRC, { data: {} }, { throwOnError: true, label: 'action "probe_2358" (visible)' }),
);
expect(matching()).toHaveLength(1);
} finally {
warn.mockRestore();
}
});
});
});
describe('useRowPredicate (canonical CEL row predicate — issue #1584)', () => {
it('returns a boolean predicate as-is (short-circuit)', () => {
expect(renderHook(() => useRowPredicate(true, { a: 1 })).result.current).toBe(true);
expect(renderHook(() => useRowPredicate(false, { a: 1 })).result.current).toBe(false);
});
it('returns the fallback for an absent predicate (default true)', () => {
expect(renderHook(() => useRowPredicate(undefined, { a: 1 })).result.current).toBe(true);
expect(renderHook(() => useRowPredicate('', { a: 1 })).result.current).toBe(true);
expect(renderHook(() => useRowPredicate(undefined, { a: 1 }, { fallback: false })).result.current).toBe(false);
});
it('evaluates a CEL predicate over the row (record.* and bare)', () => {
expect(renderHook(() => useRowPredicate("record.status == 'active'", { status: 'active' })).result.current).toBe(true);
expect(renderHook(() => useRowPredicate("status == 'active'", { status: 'closed' })).result.current).toBe(false);
});
it('supports the CEL `in` operator (legacy engine could not)', () => {
expect(renderHook(() => useRowPredicate("record.role in ['admin', 'owner']", { role: 'owner' })).result.current).toBe(true);
expect(renderHook(() => useRowPredicate("record.role in ['admin', 'owner']", { role: 'member' })).result.current).toBe(false);
});
it('merges the ambient predicate scope (features/user)', () => {
const wrapper = ({ children }: { children: React.ReactNode }) =>
createElement(PredicateScopeProvider, { scope: { features: { canEdit: true } } }, children);
expect(
renderHook(() => useRowPredicate('features.canEdit == true', { id: '1' }), { wrapper }).result.current,
).toBe(true);
});
it('fails CLOSED and warns on a broken predicate when warnOnError is set', () => {
const warn = vi.spyOn(console, 'warn').mockImplementation(() => {});
const { result } = renderHook(() =>
useRowPredicate('record.status ==', { status: 'x' }, { fallback: false, warnOnError: true, label: 'resume' }),
);
expect(result.current).toBe(false);
expect(warn).toHaveBeenCalled();
warn.mockRestore();
});
});
// #2661 — a CEL-dialect action/component predicate must reach the canonical
// engine through `toPredicateInput` → `useCondition`, not collapse to a legacy
// `${…}` string.
describe('toPredicateInput — CEL envelope preservation (#2661)', () => {
it('preserves a { dialect: "cel" } envelope (does not wrap as ${…})', () => {
expect(toPredicateInput({ dialect: 'cel', source: 'record.x == 1' }))
.toEqual({ dialect: 'cel', source: 'record.x == 1' });
});
it('still wraps bare strings and non-cel envelopes as legacy ${…}', () => {
expect(toPredicateInput('data.age >= 18')).toBe('${data.age >= 18}');
expect(toPredicateInput({ dialect: 'template', source: 'data.age >= 18' })).toBe('${data.age >= 18}');
});
it('passes booleans / empties through', () => {
expect(toPredicateInput(true)).toBe(true);
expect(toPredicateInput('')).toBeUndefined();
expect(toPredicateInput({ dialect: 'cel', source: '' })).toBeUndefined();
});
});
describe('useCondition — CEL envelope routes to the canonical engine (#2661)', () => {
it('evaluates a cel envelope from toPredicateInput on the CEL engine (CEL `in`)', () => {
const { result } = renderHook(() =>
useCondition(toPredicateInput({ dialect: 'cel', source: "'admin' in record.roles" }), {
record: { roles: ['admin'] },
}),
);
expect(result.current).toBe(true);
});
it('a cel envelope predicate that is false hides/disables (not defaulted true)', () => {
const { result } = renderHook(() =>
useCondition(toPredicateInput({ dialect: 'cel', source: 'record.status == "open"' }), {
record: { status: 'closed' },
}),
);
expect(result.current).toBe(false);
});
});