forked from CacheControl/json-rules-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathengine-fact-comparison.test.mjs
140 lines (125 loc) · 3.84 KB
/
engine-fact-comparison.test.mjs
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
import engineFactory from "../src/index.mjs";
import { describe, it, expect, vi } from "vitest";
import ruleFactory from "./support/rule-factory.mjs";
describe("Engine: fact to fact comparison", () => {
let engine;
let eventSpy;
function setup(conditions) {
const event = { type: "success-event" };
eventSpy = vi.fn();
engine = engineFactory();
const rule = ruleFactory({ conditions, event });
engine.addRule(rule);
engine.on("success", eventSpy);
}
describe("constant facts", () => {
const constantCondition = {
all: [
{
fact: "height",
operator: "lessThanInclusive",
value: {
fact: "width",
},
},
],
};
it("allows a fact to retrieve other fact values", async () => {
setup(constantCondition);
await engine.run({ height: 1, width: 2 });
expect(eventSpy).toHaveBeenCalledOnce();
eventSpy.mockReset();
await engine.run({ height: 2, width: 1 }); // negative case
expect(eventSpy).not.toHaveBeenCalled();
});
});
describe("rules with parameterized conditions", () => {
const paramsCondition = {
all: [
{
fact: "widthMultiplier",
params: {
multiplier: 2,
},
operator: "equal",
value: {
fact: "heightMultiplier",
params: {
multiplier: 4,
},
},
},
],
};
it("honors the params", async () => {
setup(paramsCondition);
engine.addFact("heightMultiplier", async (params, almanac) => {
const height = await almanac.factValue("height");
return params.multiplier * height;
});
engine.addFact("widthMultiplier", async (params, almanac) => {
const width = await almanac.factValue("width");
return params.multiplier * width;
});
await engine.run({ height: 5, width: 10 });
expect(eventSpy).toHaveBeenCalledOnce();
eventSpy.mockReset();
await engine.run({ height: 5, width: 9 }); // negative case
expect(eventSpy).not.toHaveBeenCalled();
});
});
describe("rules with parameterized conditions and path values", () => {
const pathCondition = {
all: [
{
fact: "widthMultiplier",
params: {
multiplier: 2,
},
path: "$.feet",
operator: "equal",
value: {
fact: "heightMultiplier",
params: {
multiplier: 4,
},
path: "$.meters",
},
},
],
};
it("honors the path", async () => {
setup(pathCondition);
engine.addFact("heightMultiplier", async (params, almanac) => {
const height = await almanac.factValue("height");
return { meters: params.multiplier * height };
});
engine.addFact("widthMultiplier", async (params, almanac) => {
const width = await almanac.factValue("width");
return { feet: params.multiplier * width };
});
await engine.run({ height: 5, width: 10 });
expect(eventSpy).toHaveBeenCalledOnce();
eventSpy.mockReset();
await engine.run({ height: 5, width: 9 }); // negative case
expect(eventSpy).not.toHaveBeenCalled();
});
});
describe('constant facts: checking valueResult and factResult', () => {
const constantCondition = {
all: [{
fact: 'height',
operator: 'lessThanInclusive',
value: {
fact: 'width'
}
}]
}
it('result has the correct valueResult and factResult properties', async () => {
setup(constantCondition)
const result = await engine.run({ height: 1, width: 2 })
expect(result.results[0].conditions.all[0].factResult).toEqual(1)
expect(result.results[0].conditions.all[0].valueResult).toEqual(2)
})
})
});