forked from CacheControl/json-rules-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathengine-fact.test.js
339 lines (310 loc) · 10.1 KB
/
engine-fact.test.js
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
'use strict'
import sinon from 'sinon'
import { get } from 'lodash'
import engineFactory from '../src/index'
const CHILD = 14
const ADULT = 75
async function eligibilityField (params, engine) {
if (params.field === 'age') {
if (params.eligibilityId === 1) {
return CHILD
}
return ADULT
}
}
async function eligibilityData (params, engine) {
const address = {
street: '123 Fake Street',
state: {
abbreviation: 'CO',
name: 'Colorado'
},
zip: '80403',
'dot.property': 'dot-property-value',
occupantHistory: [
{ name: 'Joe', year: 2011 },
{ name: 'Jane', year: 2013 }
],
currentOccupants: [
{ name: 'Larry', year: 2020 }
]
}
if (params.eligibilityId === 1) {
return { age: CHILD, address }
}
return { age: ADULT, address }
}
describe('Engine: fact evaluation', () => {
let engine
let sandbox
before(() => {
sandbox = sinon.createSandbox()
})
afterEach(() => {
sandbox.restore()
})
const event = {
type: 'ageTrigger',
params: {
demographic: 'under50'
}
}
function baseConditions () {
return {
any: [{
fact: 'eligibilityField',
operator: 'lessThan',
params: {
eligibilityId: 1,
field: '{{whichField}}'
},
value: 50
}]
}
}
const runtimeFacts = {
whichField:'age'
}
let successSpy
let failureSpy
beforeEach(() => {
successSpy = sandbox.spy()
failureSpy = sandbox.spy()
})
function setup (conditions = baseConditions(), engineOptions = {}) {
engine = engineFactory([], engineOptions)
const rule = factories.rule({ conditions, event })
engine.addRule(rule)
engine.addFact('eligibilityField', eligibilityField)
engine.addFact('eligibilityData', eligibilityData)
engine.on('success', successSpy)
engine.on('failure', failureSpy)
}
describe('options', () => {
describe('options.allowUndefinedFacts', () => {
it('throws when fact is undefined by default', async () => {
const conditions = Object.assign({}, baseConditions())
conditions.any.push({
fact: 'undefined-fact',
operator: 'equal',
value: true
})
setup(conditions)
return expect(engine.run(runtimeFacts)).to.be.rejectedWith(/Undefined fact: undefined-fact/)
})
context('treats undefined facts as falsey when allowUndefinedFacts is set', () => {
it('emits "success" when the condition succeeds', async () => {
const conditions = Object.assign({}, baseConditions())
conditions.any.push({
fact: 'undefined-fact',
operator: 'equal',
value: true
})
setup(conditions, { allowUndefinedFacts: true })
await engine.run(runtimeFacts)
expect(successSpy).to.have.been.called()
expect(failureSpy).to.not.have.been.called()
})
it('emits "failure" when the condition fails', async () => {
const conditions = Object.assign({}, baseConditions())
conditions.any.push({
fact: 'undefined-fact',
operator: 'equal',
value: true
})
conditions.any[0].params.eligibilityId = 2
setup(conditions, { allowUndefinedFacts: true })
await engine.run()
expect(successSpy).to.not.have.been.called()
expect(failureSpy).to.have.been.called()
})
})
})
})
describe('params', () => {
it('emits when the condition is met', async () => {
setup()
await engine.run(runtimeFacts)
expect(successSpy).to.have.been.calledWith(event)
})
it('does not emit when the condition fails', async () => {
const conditions = Object.assign({}, baseConditions())
conditions.any[0].params.eligibilityId = 2
setup(conditions)
await engine.run()
expect(successSpy).to.not.have.been.called()
})
})
describe('path', () => {
function conditions () {
return {
any: [{
fact: 'eligibilityData',
operator: 'lessThan',
path: '$.age',
params: {
eligibilityId: 1
},
value: 50
}]
}
}
it('emits when the condition is met', async () => {
setup(conditions())
await engine.run()
expect(successSpy).to.have.been.calledWith(event)
})
it('does not emit when the condition fails', async () => {
const failureCondition = conditions()
failureCondition.any[0].params.eligibilityId = 2
setup(failureCondition)
await engine.run()
expect(successSpy).to.not.have.been.called()
})
describe('arrays', () => {
it('can extract an array, allowing it to be used in concert with array operators', async () => {
const complexCondition = conditions()
complexCondition.any[0].path = '$.address.occupantHistory[*].year'
complexCondition.any[0].value = 2011
complexCondition.any[0].operator = 'contains'
setup(complexCondition)
await engine.run()
expect(successSpy).to.have.been.calledWith(event)
})
it('can extract an array with a single element', async () => {
const complexCondition = conditions()
complexCondition.any[0].path = '$.address.currentOccupants[*].year'
complexCondition.any[0].value = 2020
complexCondition.any[0].operator = 'contains'
setup(complexCondition)
await engine.run()
expect(successSpy).to.have.been.calledWith(event)
})
})
context('complex paths', () => {
it('correctly interprets "path" when dynamic facts return objects', async () => {
const complexCondition = conditions()
complexCondition.any[0].path = '$.address.occupantHistory[0].year'
complexCondition.any[0].value = 2011
complexCondition.any[0].operator = 'equal'
setup(complexCondition)
await engine.run()
expect(successSpy).to.have.been.calledWith(event)
})
it('correctly interprets "path" when target object properties have dots', async () => {
const complexCondition = conditions()
complexCondition.any[0].path = '$.address.[\'dot.property\']'
complexCondition.any[0].value = 'dot-property-value'
complexCondition.any[0].operator = 'equal'
setup(complexCondition)
await engine.run()
expect(successSpy).to.have.been.calledWith(event)
})
it('correctly interprets "path" with runtime fact objects', async () => {
const fact = { x: { y: 1 }, a: 2 }
const conditions = {
all: [{
fact: 'x',
path: '$.y',
operator: 'equal',
value: 1
}]
}
engine = engineFactory([])
const rule = factories.rule({ conditions, event })
engine.addRule(rule)
engine.on('success', successSpy)
engine.on('failure', failureSpy)
await engine.run(fact)
expect(successSpy).to.have.been.calledWith(event)
expect(failureSpy).to.not.have.been.calledWith(event)
})
})
it('does not emit when complex object paths fail the condition', async () => {
const complexCondition = conditions()
complexCondition.any[0].path = '$.address.occupantHistory[0].year'
complexCondition.any[0].value = 2010
complexCondition.any[0].operator = 'equal'
setup(complexCondition)
await engine.run()
expect(successSpy).to.not.have.been.calledWith(event)
})
it('treats invalid object paths as undefined', async () => {
const complexCondition = conditions()
complexCondition.any[0].path = '$.invalid.object[99].path'
complexCondition.any[0].value = undefined
complexCondition.any[0].operator = 'equal'
setup(complexCondition)
await engine.run()
expect(successSpy).to.have.been.calledWith(event)
})
it('ignores "path" when facts return non-objects', async () => {
setup(conditions())
const eligibilityData = async (params, engine) => {
return CHILD
}
engine.addFact('eligibilityData', eligibilityData)
await engine.run()
expect(successSpy).to.have.been.calledWith(event)
})
describe('pathResolver', () => {
it('allows a custom path resolver to be registered which interprets the path property', async () => {
const fact = { x: { y: [99] }, a: 2 }
const conditions = {
all: [{
fact: 'x',
path: 'y[0]',
operator: 'equal',
value: 99
}]
}
const pathResolver = (value, path) => {
return get(value, path)
}
engine = engineFactory([], { pathResolver })
const rule = factories.rule({ conditions, event })
engine.addRule(rule)
engine.on('success', successSpy)
engine.on('failure', failureSpy)
await engine.run(fact)
expect(successSpy).to.have.been.calledWith(event)
expect(failureSpy).to.not.have.been.called()
})
})
})
describe('promises', () => {
it('works with asynchronous evaluations', async () => {
setup()
const eligibilityField = function (params, engine) {
return new Promise((resolve, reject) => {
setImmediate(() => {
resolve(30)
})
})
}
engine.addFact('eligibilityField', eligibilityField)
await engine.run()
expect(successSpy).to.have.been.called()
})
})
describe('synchronous functions', () => {
it('works with synchronous, non-promise evaluations that are truthy', async () => {
setup()
const eligibilityField = function (params, engine) {
return 20
}
engine.addFact('eligibilityField', eligibilityField)
await engine.run()
expect(successSpy).to.have.been.called()
})
it('works with synchronous, non-promise evaluations that are falsey', async () => {
setup()
const eligibilityField = function (params, engine) {
return 100
}
engine.addFact('eligibilityField', eligibilityField)
await engine.run()
expect(successSpy).to.not.have.been.called()
})
})
})