-
Notifications
You must be signed in to change notification settings - Fork 488
/
Copy pathengine.test.js
330 lines (283 loc) · 10.1 KB
/
engine.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
'use strict'
import sinon from 'sinon'
import engineFactory, { Fact, Rule, Operator } from '../src/index'
import defaultOperators from '../src/engine-default-operators'
describe('Engine', () => {
let engine
let sandbox
before(() => {
sandbox = sinon.createSandbox()
})
afterEach(() => {
sandbox.restore()
})
beforeEach(() => {
engine = engineFactory()
})
it('has methods for managing facts and rules, and running itself', () => {
expect(engine).to.have.property('addRule')
expect(engine).to.have.property('removeRule')
expect(engine).to.have.property('addOperator')
expect(engine).to.have.property('removeOperator')
expect(engine).to.have.property('addFact')
expect(engine).to.have.property('removeFact')
expect(engine).to.have.property('run')
expect(engine).to.have.property('stop')
})
describe('constructor', () => {
it('initializes with the default state', () => {
expect(engine.status).to.equal('READY')
expect(engine.rules.length).to.equal(0)
defaultOperators.forEach(op => {
expect(engine.operators.get(op.name)).to.be.an.instanceof(Operator)
})
})
it('can be initialized with rules', () => {
const rules = [
factories.rule(),
factories.rule(),
factories.rule()
]
engine = engineFactory(rules)
expect(engine.rules.length).to.equal(rules.length)
})
})
describe('stop()', () => {
it('changes the status to "FINISHED"', () => {
expect(engine.stop().status).to.equal('FINISHED')
})
})
describe('addRule()', () => {
describe('rule instance', () => {
it('adds the rule', () => {
const rule = new Rule(factories.rule())
expect(engine.rules.length).to.equal(0)
engine.addRule(rule)
expect(engine.rules.length).to.equal(1)
expect(engine.rules).to.include(rule)
})
})
describe('required fields', () => {
it('.conditions', () => {
const rule = factories.rule()
delete rule.conditions
expect(() => {
engine.addRule(rule)
}).to.throw(/Engine: addRule\(\) argument requires "conditions" property/)
})
it('.event', () => {
const rule = factories.rule()
delete rule.event
expect(() => {
engine.addRule(rule)
}).to.throw(/Engine: addRule\(\) argument requires "event" property/)
})
})
})
describe('updateRule()', () => {
it('updates rule', () => {
let rule1 = new Rule(factories.rule({ name: 'rule1' }))
let rule2 = new Rule(factories.rule({ name: 'rule2' }))
engine.addRule(rule1)
engine.addRule(rule2)
expect(engine.rules[0].conditions.all.length).to.equal(2)
expect(engine.rules[1].conditions.all.length).to.equal(2)
rule1.conditions = { all: [] }
engine.updateRule(rule1)
rule1 = engine.rules.find(rule => rule.name === 'rule1')
rule2 = engine.rules.find(rule => rule.name === 'rule2')
expect(rule1.conditions.all.length).to.equal(0)
expect(rule2.conditions.all.length).to.equal(2)
})
it('should throw error if rule not found', () => {
const rule1 = new Rule(factories.rule({ name: 'rule1' }))
engine.addRule(rule1)
const rule2 = new Rule(factories.rule({ name: 'rule2' }))
expect(() => {
engine.updateRule(rule2)
}).to.throw(/Engine: updateRule\(\) rule not found/)
})
})
describe('removeRule()', () => {
function setup () {
const rule1 = new Rule(factories.rule({ name: 'rule1' }))
const rule2 = new Rule(factories.rule({ name: 'rule2' }))
engine.addRule(rule1)
engine.addRule(rule2)
engine.prioritizeRules()
return [rule1, rule2]
}
context('remove by rule.name', () => {
it('removes a single rule', () => {
const [rule1] = setup()
expect(engine.rules.length).to.equal(2)
const isRemoved = engine.removeRule(rule1.name)
expect(isRemoved).to.be.true()
expect(engine.rules.length).to.equal(1)
expect(engine.prioritizedRules).to.equal(null)
})
it('removes multiple rules with the same name', () => {
const [rule1] = setup()
const rule3 = new Rule(factories.rule({ name: rule1.name }))
engine.addRule(rule3)
expect(engine.rules.length).to.equal(3)
const isRemoved = engine.removeRule(rule1.name)
expect(isRemoved).to.be.true()
expect(engine.rules.length).to.equal(1)
expect(engine.prioritizedRules).to.equal(null)
})
it('returns false when rule cannot be found', () => {
setup()
expect(engine.rules.length).to.equal(2)
const isRemoved = engine.removeRule('not-found-name')
expect(isRemoved).to.be.false()
expect(engine.rules.length).to.equal(2)
expect(engine.prioritizedRules).to.not.equal(null)
})
})
context('remove by rule object', () => {
it('removes a single rule', () => {
const [rule1] = setup()
expect(engine.rules.length).to.equal(2)
const isRemoved = engine.removeRule(rule1)
expect(isRemoved).to.be.true()
expect(engine.rules.length).to.equal(1)
expect(engine.prioritizedRules).to.equal(null)
})
it('removes a single rule, even if two have the same name', () => {
const [rule1] = setup()
const rule3 = new Rule(factories.rule({ name: rule1.name }))
engine.addRule(rule3)
expect(engine.rules.length).to.equal(3)
const isRemoved = engine.removeRule(rule1)
expect(isRemoved).to.be.true()
expect(engine.rules.length).to.equal(2)
expect(engine.prioritizedRules).to.equal(null)
})
it('returns false when rule cannot be found', () => {
setup()
expect(engine.rules.length).to.equal(2)
const rule3 = new Rule(factories.rule({ name: 'rule3' }))
const isRemoved = engine.removeRule(rule3)
expect(isRemoved).to.be.false()
expect(engine.rules.length).to.equal(2)
expect(engine.prioritizedRules).to.not.equal(null)
})
})
})
describe('addOperator()', () => {
it('adds the operator', () => {
engine.addOperator('startsWithLetter', (factValue, jsonValue) => {
return factValue[0] === jsonValue
})
expect(engine.operators.get('startsWithLetter')).to.exist()
expect(engine.operators.get('startsWithLetter')).to.be.an.instanceof(Operator)
})
it('accepts an operator instance', () => {
const op = new Operator('my-operator', _ => true)
engine.addOperator(op)
expect(engine.operators.get('my-operator')).to.equal(op)
})
})
describe('removeOperator()', () => {
it('removes the operator', () => {
engine.addOperator('startsWithLetter', (factValue, jsonValue) => {
return factValue[0] === jsonValue
})
expect(engine.operators.get('startsWithLetter')).to.be.an.instanceof(Operator)
engine.removeOperator('startsWithLetter')
expect(engine.operators.get('startsWithLetter')).to.be.null()
})
it('can only remove added operators', () => {
const isRemoved = engine.removeOperator('nonExisting')
expect(isRemoved).to.equal(false)
})
})
describe('addFact()', () => {
const FACT_NAME = 'FACT_NAME'
const FACT_VALUE = 'FACT_VALUE'
function assertFact (engine) {
expect(engine.facts.size).to.equal(1)
expect(engine.facts.has(FACT_NAME)).to.be.true()
}
it('allows a constant fact', () => {
engine.addFact(FACT_NAME, FACT_VALUE)
assertFact(engine)
expect(engine.facts.get(FACT_NAME).value).to.equal(FACT_VALUE)
})
it('allows options to be passed', () => {
const options = { cache: false }
engine.addFact(FACT_NAME, FACT_VALUE, options)
assertFact(engine)
expect(engine.facts.get(FACT_NAME).value).to.equal(FACT_VALUE)
expect(engine.facts.get(FACT_NAME).options).to.eql(options)
})
it('allows a lamba fact with no options', () => {
engine.addFact(FACT_NAME, async (params, engine) => {
return FACT_VALUE
})
assertFact(engine)
expect(engine.facts.get(FACT_NAME).value).to.be.undefined()
})
it('allows a lamba fact with options', () => {
const options = { cache: false }
engine.addFact(FACT_NAME, async (params, engine) => {
return FACT_VALUE
}, options)
assertFact(engine)
expect(engine.facts.get(FACT_NAME).options).to.eql(options)
expect(engine.facts.get(FACT_NAME).value).to.be.undefined()
})
it('allows a fact instance', () => {
const options = { cache: false }
const fact = new Fact(FACT_NAME, 50, options)
engine.addFact(fact)
assertFact(engine)
expect(engine.facts.get(FACT_NAME)).to.exist()
expect(engine.facts.get(FACT_NAME).options).to.eql(options)
})
})
describe('removeFact()', () => {
it('removes a Fact', () => {
expect(engine.facts.size).to.equal(0)
const fact = new Fact('newFact', 50, { cache: false })
engine.addFact(fact)
expect(engine.facts.size).to.equal(1)
engine.removeFact('newFact')
expect(engine.facts.size).to.equal(0)
})
it('can only remove added facts', () => {
expect(engine.facts.size).to.equal(0)
const isRemoved = engine.removeFact('newFact')
expect(isRemoved).to.equal(false)
})
})
describe('run()', () => {
beforeEach(() => {
const conditions = {
all: [{
fact: 'age',
operator: 'greaterThanInclusive',
value: 18
}]
}
const event = { type: 'generic' }
const rule = factories.rule({ conditions, event })
engine.addRule(rule)
engine.addFact('age', 20)
})
it('changes the status to "RUNNING"', () => {
const eventSpy = sandbox.spy()
engine.on('success', (event, almanac) => {
eventSpy()
expect(engine.status).to.equal('RUNNING')
})
return engine.run()
})
it('changes status to FINISHED once complete', async () => {
expect(engine.status).to.equal('READY')
await engine.run()
expect(engine.status).to.equal('FINISHED')
})
})
})