|
| 1 | +'use strict' |
| 2 | + |
| 3 | +import sinon from 'sinon' |
| 4 | +import { expect } from 'chai' |
| 5 | +import { Engine } from '../../src/index' |
| 6 | + |
| 7 | +/** |
| 8 | + * acceptance tests are intended to use features that, when used in combination, |
| 9 | + * could cause integration bugs not caught by the rest of the test suite |
| 10 | + */ |
| 11 | +describe('Acceptance', () => { |
| 12 | + let sandbox |
| 13 | + before(() => { |
| 14 | + sandbox = sinon.createSandbox() |
| 15 | + }) |
| 16 | + afterEach(() => { |
| 17 | + sandbox.restore() |
| 18 | + }) |
| 19 | + const factParam = 1 |
| 20 | + const event1 = { |
| 21 | + type: 'event-1', |
| 22 | + params: { |
| 23 | + eventParam: 1 |
| 24 | + } |
| 25 | + } |
| 26 | + const event2 = { |
| 27 | + type: 'event-2' |
| 28 | + } |
| 29 | + const expectedFirstRuleResult = { |
| 30 | + all: [{ |
| 31 | + fact: 'high-priority', |
| 32 | + params: { |
| 33 | + factParam |
| 34 | + }, |
| 35 | + operator: 'contains', |
| 36 | + path: '$.values', |
| 37 | + value: 2, |
| 38 | + factResult: [2], |
| 39 | + result: true |
| 40 | + }, |
| 41 | + { |
| 42 | + fact: 'low-priority', |
| 43 | + operator: 'in', |
| 44 | + value: [2], |
| 45 | + factResult: 2, |
| 46 | + result: true |
| 47 | + } |
| 48 | + ], |
| 49 | + operator: 'all', |
| 50 | + priority: 1 |
| 51 | + } |
| 52 | + let successSpy |
| 53 | + let failureSpy |
| 54 | + let highPrioritySpy |
| 55 | + let lowPrioritySpy |
| 56 | + |
| 57 | + function delay (value) { |
| 58 | + return new Promise(resolve => setTimeout(() => resolve(value), 5)) |
| 59 | + } |
| 60 | + |
| 61 | + function setup (options = {}) { |
| 62 | + const engine = new Engine() |
| 63 | + highPrioritySpy = sandbox.spy() |
| 64 | + lowPrioritySpy = sandbox.spy() |
| 65 | + |
| 66 | + engine.addRule({ |
| 67 | + name: 'first', |
| 68 | + priority: 10, |
| 69 | + conditions: { |
| 70 | + all: [{ |
| 71 | + fact: 'high-priority', |
| 72 | + params: { |
| 73 | + factParam |
| 74 | + }, |
| 75 | + operator: 'contains', |
| 76 | + path: '$.values', |
| 77 | + value: options.highPriorityValue |
| 78 | + }, { |
| 79 | + fact: 'low-priority', |
| 80 | + operator: 'in', |
| 81 | + value: options.lowPriorityValue |
| 82 | + }] |
| 83 | + }, |
| 84 | + event: event1, |
| 85 | + onSuccess: async (event, almanac, ruleResults) => { |
| 86 | + expect(ruleResults.name).to.equal('first') |
| 87 | + expect(ruleResults.event).to.deep.equal(event1) |
| 88 | + expect(ruleResults.priority).to.equal(10) |
| 89 | + expect(ruleResults.conditions).to.deep.equal(expectedFirstRuleResult) |
| 90 | + |
| 91 | + return delay(almanac.addRuntimeFact('rule-created-fact', { array: options.highPriorityValue })) |
| 92 | + } |
| 93 | + }) |
| 94 | + |
| 95 | + engine.addRule({ |
| 96 | + name: 'second', |
| 97 | + priority: 1, |
| 98 | + conditions: { |
| 99 | + all: [{ |
| 100 | + fact: 'high-priority', |
| 101 | + params: { |
| 102 | + factParam |
| 103 | + }, |
| 104 | + operator: 'containsDivisibleValuesOf', |
| 105 | + path: '$.values', |
| 106 | + value: { |
| 107 | + fact: 'rule-created-fact', |
| 108 | + path: '$.array' // set by 'success' of first rule |
| 109 | + } |
| 110 | + }] |
| 111 | + }, |
| 112 | + event: event2 |
| 113 | + }) |
| 114 | + |
| 115 | + engine.addOperator('containsDivisibleValuesOf', (factValue, jsonValue) => { |
| 116 | + return factValue.some(v => v % jsonValue === 0) |
| 117 | + }) |
| 118 | + |
| 119 | + engine.addFact('high-priority', async function (params, almanac) { |
| 120 | + highPrioritySpy(params) |
| 121 | + const idx = await almanac.factValue('sub-fact') |
| 122 | + return delay({ values: [idx + params.factParam] }) // { values: [baseIndex + factParam] } |
| 123 | + }, { priority: 2 }) |
| 124 | + |
| 125 | + engine.addFact('low-priority', async function (params, almanac) { |
| 126 | + lowPrioritySpy(params) |
| 127 | + const idx = await almanac.factValue('sub-fact') |
| 128 | + return delay(idx + 1) // baseIndex + 1 |
| 129 | + }, { priority: 1 }) |
| 130 | + |
| 131 | + engine.addFact('sub-fact', async function (params, almanac) { |
| 132 | + const baseIndex = await almanac.factValue('baseIndex') |
| 133 | + return delay(baseIndex) |
| 134 | + }) |
| 135 | + successSpy = sandbox.spy() |
| 136 | + failureSpy = sandbox.spy() |
| 137 | + engine.on('success', successSpy) |
| 138 | + engine.on('failure', failureSpy) |
| 139 | + |
| 140 | + return engine |
| 141 | + } |
| 142 | + |
| 143 | + it('succeeds', async () => { |
| 144 | + const engine = setup({ |
| 145 | + highPriorityValue: 2, |
| 146 | + lowPriorityValue: [2] |
| 147 | + }) |
| 148 | + |
| 149 | + const engineResult = await engine.run({ baseIndex: 1 }) |
| 150 | + |
| 151 | + expect(engineResult.events.length).to.equal(2) |
| 152 | + expect(engineResult.events[0]).to.deep.equal(event1) |
| 153 | + expect(engineResult.events[1]).to.deep.equal(event2) |
| 154 | + expect(successSpy).to.have.been.calledTwice() |
| 155 | + expect(successSpy).to.have.been.calledWith(event1) |
| 156 | + expect(successSpy).to.have.been.calledWith(event2) |
| 157 | + expect(highPrioritySpy).to.have.been.calledBefore(lowPrioritySpy) |
| 158 | + expect(failureSpy).to.not.have.been.called() |
| 159 | + }) |
| 160 | + |
| 161 | + it('fails', async () => { |
| 162 | + const engine = setup({ |
| 163 | + highPriorityValue: 2, |
| 164 | + lowPriorityValue: [3] // falsey |
| 165 | + }) |
| 166 | + |
| 167 | + const engineResult = await engine.run({ baseIndex: 1, 'rule-created-fact': '' }) |
| 168 | + |
| 169 | + expect(engineResult.events.length).to.equal(0) |
| 170 | + expect(failureSpy).to.have.been.calledTwice() |
| 171 | + expect(failureSpy).to.have.been.calledWith(event1) |
| 172 | + expect(failureSpy).to.have.been.calledWith(event2) |
| 173 | + expect(highPrioritySpy).to.have.been.calledBefore(lowPrioritySpy) |
| 174 | + expect(successSpy).to.not.have.been.called() |
| 175 | + }) |
| 176 | +}) |
0 commit comments