-
-
Notifications
You must be signed in to change notification settings - Fork 24.5k
Expand file tree
/
Copy pathfieldVisibility.test.ts
More file actions
356 lines (292 loc) · 14.3 KB
/
fieldVisibility.test.ts
File metadata and controls
356 lines (292 loc) · 14.3 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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
import type { InputParam } from '../types'
import { conditionMatches, evaluateFieldVisibility, evaluateParamVisibility, stripHiddenFieldValues } from './fieldVisibility'
const makeParam = (overrides: Partial<InputParam> = {}): InputParam => ({
id: 'p1',
name: 'field1',
label: 'Field 1',
type: 'string',
...overrides
})
describe('conditionMatches', () => {
it('returns true for array-array intersection', () => {
expect(conditionMatches(['a', 'b'], ['b', 'c'])).toBe(true)
})
it('returns false for array-array no intersection', () => {
expect(conditionMatches(['a', 'b'], ['c', 'd'])).toBe(false)
})
it('matches string in array ground value', () => {
expect(conditionMatches(['api', 'cloud'], 'api')).toBe(true)
expect(conditionMatches(['api', 'cloud'], 'local')).toBe(false)
})
it('matches regex against array ground value', () => {
expect(conditionMatches(['gpt-4', 'gpt-3.5'], 'gpt-.*')).toBe(true)
expect(conditionMatches(['claude-3'], 'gpt-.*')).toBe(false)
})
it('matches boolean/number in array ground value', () => {
expect(conditionMatches([true, false], true)).toBe(true)
expect(conditionMatches([1, 2, 3], 2)).toBe(true)
expect(conditionMatches([1, 2], 5)).toBe(false)
})
it('matches object in array via deep equality', () => {
expect(conditionMatches([{ a: 1 }, { b: 2 }], { b: 2 })).toBe(true)
expect(conditionMatches([{ a: 1 }], { a: 2 })).toBe(false)
})
it('scalar includes check against comparison array', () => {
expect(conditionMatches('api', ['api', 'cloud'])).toBe(true)
expect(conditionMatches('local', ['api', 'cloud'])).toBe(false)
})
it('scalar exact match with string', () => {
expect(conditionMatches('hello', 'hello')).toBe(true)
expect(conditionMatches('hello', 'world')).toBe(false)
})
it('scalar regex match', () => {
expect(conditionMatches('gpt-4-turbo', 'gpt-.*')).toBe(true)
expect(conditionMatches('claude-3', 'gpt-.*')).toBe(false)
})
it('dot in comparison value is literal, not regex wildcard', () => {
expect(conditionMatches('gpt-445', 'gpt-4.5')).toBe(false)
expect(conditionMatches('gpt-4.5', 'gpt-4.5')).toBe(true)
})
it('dot in comparison value is literal in array ground', () => {
expect(conditionMatches(['gpt-445'], 'gpt-4.5')).toBe(false)
expect(conditionMatches(['gpt-4.5'], 'gpt-4.5')).toBe(true)
})
it('regex alternation pattern still works', () => {
expect(conditionMatches('openAI', '(openAI|google)')).toBe(true)
expect(conditionMatches('anthropic', '(openAI|google)')).toBe(false)
})
it('regex wildcard pattern still works', () => {
expect(conditionMatches('gpt-4-turbo', 'gpt-.*')).toBe(true)
expect(conditionMatches('claude-3', 'gpt-.*')).toBe(false)
})
it('invalid regex does not throw, returns false', () => {
expect(conditionMatches('test', '[invalid')).toBe(false)
})
it('rejects oversized regex patterns to mitigate ReDoS', () => {
const longPattern = '(a+)'.repeat(60) // exceeds 200 char limit
expect(conditionMatches('aaa', longPattern)).toBe(false)
expect(conditionMatches(['aaa'], longPattern)).toBe(false)
})
it('rejects nested quantifier patterns to mitigate ReDoS', () => {
// Build patterns dynamically to avoid tripping CodeQL's static ReDoS scanner
const nestedPlus = ['(a', '+)', '+$'].join('')
const nestedStar = ['(a', '*)', '*'].join('')
const altQuantified = ['(a|a', 'a)', '+'].join('')
const spaceQuantified = ['(a', '+ )', '+'].join('')
expect(conditionMatches('aaa', nestedPlus)).toBe(false)
expect(conditionMatches(['aaa'], nestedStar)).toBe(false)
expect(conditionMatches('aaa', altQuantified)).toBe(false)
expect(conditionMatches('aaa', spaceQuantified)).toBe(false)
})
it('allows safe patterns with groups and quantifiers', () => {
// Non-nested: group without inner quantifier/alt, followed by quantifier
expect(conditionMatches('aaa', '(a)+')).toBe(true)
expect(conditionMatches('abc', '(abc)+')).toBe(true)
})
it('scalar boolean strict equality', () => {
expect(conditionMatches(true, true)).toBe(true)
expect(conditionMatches(true, false)).toBe(false)
})
it('scalar number strict equality', () => {
expect(conditionMatches(42, 42)).toBe(true)
expect(conditionMatches(42, 99)).toBe(false)
})
it('scalar deep object equality', () => {
expect(conditionMatches({ a: 1, b: [2] }, { a: 1, b: [2] })).toBe(true)
expect(conditionMatches({ a: 1 }, { a: 2 })).toBe(false)
})
})
describe('evaluateParamVisibility', () => {
it('returns true when no conditions', () => {
expect(evaluateParamVisibility(makeParam(), {})).toBe(true)
})
it('show match returns true', () => {
const param = makeParam({ show: { mode: 'api' } })
expect(evaluateParamVisibility(param, { mode: 'api' })).toBe(true)
})
it('show mismatch returns false', () => {
const param = makeParam({ show: { mode: 'api' } })
expect(evaluateParamVisibility(param, { mode: 'local' })).toBe(false)
})
it('hide match returns false', () => {
const param = makeParam({ hide: { mode: 'api' } })
expect(evaluateParamVisibility(param, { mode: 'api' })).toBe(false)
})
it('hide mismatch returns true', () => {
const param = makeParam({ hide: { mode: 'api' } })
expect(evaluateParamVisibility(param, { mode: 'local' })).toBe(true)
})
it('handles array comparison value with scalar ground', () => {
const param = makeParam({ show: { mode: ['api', 'cloud'] } })
expect(evaluateParamVisibility(param, { mode: 'api' })).toBe(true)
expect(evaluateParamVisibility(param, { mode: 'local' })).toBe(false)
})
it('resolves nested path via lodash get', () => {
const param = makeParam({ show: { 'config.nested.field': 'yes' } })
expect(evaluateParamVisibility(param, { config: { nested: { field: 'yes' } } })).toBe(true)
expect(evaluateParamVisibility(param, { config: { nested: { field: 'no' } } })).toBe(false)
})
it('resolves $index placeholder', () => {
const param = makeParam({ show: { 'items.$index.type': 'text' } })
const values = { items: [{ type: 'text' }, { type: 'image' }] }
expect(evaluateParamVisibility(param, values, 0)).toBe(true)
expect(evaluateParamVisibility(param, values, 1)).toBe(false)
})
it('parses JSON-encoded array string in ground value', () => {
const param = makeParam({ show: { tools: 'search' } })
expect(evaluateParamVisibility(param, { tools: '["search","browse"]' })).toBe(true)
})
it('multiple show conditions act as AND', () => {
const param = makeParam({ show: { mode: 'api', provider: 'openai' } })
expect(evaluateParamVisibility(param, { mode: 'api', provider: 'openai' })).toBe(true)
expect(evaluateParamVisibility(param, { mode: 'api', provider: 'azure' })).toBe(false)
})
it('combined show+hide: show passes but hide also matches returns false', () => {
const param = makeParam({ show: { mode: 'api' }, hide: { provider: 'azure' } })
expect(evaluateParamVisibility(param, { mode: 'api', provider: 'azure' })).toBe(false)
})
})
describe('evaluateFieldVisibility', () => {
it('returns new array with computed display, does not mutate originals', () => {
const params = [makeParam({ name: 'a', show: { mode: 'api' } }), makeParam({ name: 'b', hide: { mode: 'api' } })]
const values = { mode: 'api' }
const result = evaluateFieldVisibility(params, values)
expect(result).toHaveLength(2)
expect(result[0].display).toBe(true)
expect(result[1].display).toBe(false)
// Originals unchanged
expect(params[0].display).toBeUndefined()
expect(params[1].display).toBeUndefined()
})
describe('option-level show/hide filtering', () => {
it('removes options whose hide condition matches', () => {
const param = makeParam({
type: 'options',
options: [
{ label: 'String', name: 'string' },
{ label: 'Object', name: 'object', hide: { contentType: 'application/x-www-form-urlencoded' } }
] as any
})
const result = evaluateFieldVisibility([param], { contentType: 'application/x-www-form-urlencoded' })
expect(result[0].options).toHaveLength(1)
expect(result[0].options![0]).toMatchObject({ name: 'string' })
})
it('keeps options whose hide condition does not match', () => {
const param = makeParam({
type: 'options',
options: [
{ label: 'String', name: 'string' },
{ label: 'Object', name: 'object', hide: { contentType: 'application/x-www-form-urlencoded' } }
] as any
})
const result = evaluateFieldVisibility([param], { contentType: 'application/json' })
expect(result[0].options).toHaveLength(2)
})
it('removes options whose show condition does not match', () => {
const param = makeParam({
type: 'options',
options: [
{ label: 'Basic', name: 'basic' },
{ label: 'Advanced', name: 'advanced', show: { mode: 'expert' } }
] as any
})
const result = evaluateFieldVisibility([param], { mode: 'beginner' })
expect(result[0].options).toHaveLength(1)
expect(result[0].options![0]).toMatchObject({ name: 'basic' })
})
it('keeps options whose show condition matches', () => {
const param = makeParam({
type: 'options',
options: [
{ label: 'Basic', name: 'basic' },
{ label: 'Advanced', name: 'advanced', show: { mode: 'expert' } }
] as any
})
const result = evaluateFieldVisibility([param], { mode: 'expert' })
expect(result[0].options).toHaveLength(2)
})
it('passes through string options unchanged', () => {
const param = makeParam({
type: 'options',
options: ['one', 'two', 'three'] as any
})
const result = evaluateFieldVisibility([param], {})
expect(result[0].options).toHaveLength(3)
})
it('passes through options with no show/hide unchanged', () => {
const param = makeParam({
type: 'options',
options: [
{ label: 'A', name: 'a' },
{ label: 'B', name: 'b' }
] as any
})
const result = evaluateFieldVisibility([param], {})
expect(result[0].options).toHaveLength(2)
})
it('does not mutate the original options array', () => {
const options = [
{ label: 'String', name: 'string' },
{ label: 'Object', name: 'object', hide: { contentType: 'application/x-www-form-urlencoded' } }
] as any
const param = makeParam({ type: 'options', options })
evaluateFieldVisibility([param], { contentType: 'application/x-www-form-urlencoded' })
// Original options array is untouched
expect(options).toHaveLength(2)
})
it('does not affect non-options params', () => {
const param = makeParam({ type: 'string' })
const result = evaluateFieldVisibility([param], {})
expect(result[0].options).toBeUndefined()
})
})
})
describe('evaluateFieldVisibility – nested array $index pattern (Start node formInputTypes)', () => {
// Mirrors the Start node's formInputTypes definition:
// addOptions has show: { 'formInputTypes[$index].type': 'options' }
const addOptionsParam = makeParam({
name: 'addOptions',
label: 'Add Options',
type: 'array',
show: { 'formInputTypes[$index].type': 'options' }
})
const typeParam = makeParam({ name: 'type', label: 'Type', type: 'options' })
const labelParam = makeParam({ name: 'label', label: 'Label', type: 'string' })
const arraySubFields = [typeParam, labelParam, addOptionsParam]
it('shows addOptions when type is "options" at given index', () => {
const inputValues = {
formInputTypes: [
{ type: 'options', label: 'Pick one' },
{ type: 'string', label: 'Name' }
]
}
const row0 = evaluateFieldVisibility(arraySubFields, inputValues, 0)
expect(row0.find((p) => p.name === 'addOptions')!.display).toBe(true)
expect(row0.find((p) => p.name === 'type')!.display).toBe(true)
const row1 = evaluateFieldVisibility(arraySubFields, inputValues, 1)
expect(row1.find((p) => p.name === 'addOptions')!.display).toBe(false)
expect(row1.find((p) => p.name === 'type')!.display).toBe(true)
})
it('hides addOptions when type changes from "options" to "string"', () => {
const inputValues = {
formInputTypes: [{ type: 'string', label: 'Name' }]
}
const row0 = evaluateFieldVisibility(arraySubFields, inputValues, 0)
expect(row0.find((p) => p.name === 'addOptions')!.display).toBe(false)
})
it('handles missing array gracefully (defaults to empty)', () => {
const inputValues = {} // formInputTypes not yet set
const row0 = evaluateFieldVisibility(arraySubFields, inputValues, 0)
expect(row0.find((p) => p.name === 'addOptions')!.display).toBe(false)
})
})
describe('stripHiddenFieldValues', () => {
it('removes hidden keys and retains visible ones', () => {
const params = [makeParam({ name: 'visible', show: { mode: 'api' } }), makeParam({ name: 'hidden', hide: { mode: 'api' } })]
const values = { mode: 'api', visible: 'yes', hidden: 'secret' }
const result = stripHiddenFieldValues(params, values)
expect(result).toHaveProperty('visible', 'yes')
expect(result).not.toHaveProperty('hidden')
expect(result).toHaveProperty('mode', 'api')
})
})