/
flow.compiled.test.js
470 lines (398 loc) · 15.7 KB
/
flow.compiled.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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
"use strict";
var it = require("it"),
assert = require("assert");
it.describe("Flow compiled", function (it) {
it.describe("not rule", function (it) {
var called, flow;
it.beforeAll(function () {
flow = require("./rules/notRule-compiled")();
called = new (flow.getDefined("count"))();
});
it.should("call when a string that does not equal 'hello'", function () {
return flow.getSession("world", called).match().then(function () {
assert.equal(called.called, 1);
});
});
it.should(" not call when a string that does equal 'hello'", function () {
called.called = 0;
return flow.getSession("hello", called).match().then(function () {
assert.equal(called.called, 0);
});
});
it.should(" not call when a string that does equal 'hello' and one that does not", function () {
called.called = 0;
return flow.getSession("hello", "world", called).match().then(function () {
assert.equal(called.called, 0);
});
});
});
it.describe("or rule", function (it) {
var called, flow;
it.beforeAll(function () {
flow = require("./rules/orRule-compiled")();
called = new (flow.getDefined("count"))();
});
it.should("call when a string equals 'hello'", function () {
return flow.getSession("world", called).match().then(function () {
assert.equal(called.called, 1);
assert.equal(called.s, "world");
});
});
it.describe("or rule with not conditions", function (it) {
var flow, count;
it.beforeAll(function () {
flow = require("./rules/orRule-notConditions-compiled")();
var Count = flow.getDefined("count");
count = new Count();
});
it.should("activate for each fact that does not exist", function () {
return flow.getSession(count).match()
.then(function () {
assert.equal(count.called, 3);
count.called = 0;
return flow.getSession(count, 1).match();
})
.then(function () {
assert.equal(count.called, 2);
count.called = 0;
return flow.getSession(count, 'hello').match();
})
.then(function () {
assert.equal(count.called, 2);
count.called = 0;
return flow.getSession(count, new Date()).match();
})
.then(function () {
assert.equal(count.called, 2);
count.called = 0;
return flow.getSession(count, 1, 'hello', new Date()).match();
})
.then(function () {
assert.equal(count.called, 0);
});
});
});
});
it.describe("exists rule", function (it) {
var called, flow;
it.beforeAll(function () {
flow = require("./rules/existsRule-compiled")();
called = new (flow.getDefined("count"))();
});
it.should("call when a string equals 'hello'", function () {
return flow.getSession("hello", called).match().then(function () {
assert.equal(called.called, 1);
});
});
it.should("not call when a string that does not equal 'hello'", function () {
called.called = 0;
return flow.getSession("world", called).match().then(function () {
assert.equal(called.called, 0);
});
});
it.should(" call when a string that does not equal 'hello' and one that does exist", function () {
called.called = 0;
return flow.getSession("hello", "world", called).match().then(function () {
assert.equal(called.called, 1);
});
});
});
it.describe("scoped functions", function (it) {
var session, Message;
it.beforeAll(function () {
var flow = require("./rules/scope-compiled")();
Message = flow.getDefined("message");
session = flow.getSession();
});
it.should("call the scoped function", function (next) {
var m = new Message("hello");
session.once("assert", function (fact) {
assert.deepEqual(fact, m);
next();
});
session.assert(m);
});
});
it.describe("events", function (it) {
it.timeout(1000);
var flow, Message, session;
it.beforeAll(function () {
flow = require("./rules/simple-compiled")();
Message = flow.getDefined("Message");
});
it.beforeEach(function () {
session = flow.getSession();
});
it.should("emit when facts are asserted", function (next) {
var m = new Message("hello world");
session.once("assert", function (fact) {
assert.deepEqual(fact, m);
next();
});
session.assert(m);
});
it.should("emit when facts are retracted", function (next) {
var m = new Message("hello world");
session.once("retract", function (fact) {
assert.deepEqual(fact, m);
next();
});
session.assert(m);
session.retract(m);
});
it.should("emit when facts are modified", function (next) {
var m = new Message("hello world");
session.once("modify", function (fact) {
assert.deepEqual(fact, m);
next();
});
session.assert(m);
session.modify(m);
});
it.should("emit when rules are fired", function (next) {
var m = new Message("hello world");
var fire = [
["Hello", "hello world"],
["Goodbye", "hello world goodbye"]
], i = 0;
session.on("fire", function (name, facts) {
assert.equal(name, fire[i][0]);
assert.equal(facts.m.message, fire[i++][1]);
});
session.assert(m);
session.match(function () {
assert.equal(i, 2);
next();
});
});
});
it.describe("agenda-groups", function (it) {
var flow = require("./rules/agenda-group-compiled")(),
Message = flow.getDefined("message"),
session;
it.beforeEach(function () {
session = flow.getSession();
});
it.should("only fire events in focused group", function () {
var events = [];
session.assert(new Message("hello"));
session.focus("ag1");
session.on("fire", function (name) {
events.push(name);
});
return session.match()
.then(function () {
assert.deepEqual(events, ["Hello World", "GoodBye"]);
events = [];
session = flow.getSession();
session.assert(new Message("hello"));
session.focus("ag2");
session.on("fire", function (name) {
events.push(name);
});
return session.match().then(function () {
assert.deepEqual(events, ["Hello World 2", "GoodBye 2"]);
});
});
});
it.should("should treat focus like a stack", function () {
var events = [];
session.assert(new Message("hello"));
session.focus("ag2");
session.focus("ag1");
session.on("fire", function (name) {
events.push(name);
});
return session.match()
.then(function () {
assert.deepEqual(events, ["Hello World", "GoodBye", "GoodBye 2"]);
events = [];
session = flow.getSession();
session.assert(new Message("hello"));
session.focus("ag1");
session.focus("ag2");
session.on("fire", function (name) {
events.push(name);
});
return session.match().then(function () {
assert.deepEqual(events, ["Hello World 2", "GoodBye 2", "GoodBye"]);
});
});
});
});
it.describe("auto-focus", function (it) {
var flow = require("./rules/auto-focus-compiled")(),
State = flow.getDefined("state"),
session;
it.beforeEach(function () {
session = flow.getSession();
});
it.should("activate agenda groups in proper order", function () {
session.assert(new State("A", "NOT_RUN"));
session.assert(new State("B", "NOT_RUN"));
session.assert(new State("C", "NOT_RUN"));
session.assert(new State("D", "NOT_RUN"));
var fired = [];
session.on("fire", function (name) {
fired.push(name);
});
return session.match().then(function () {
assert.deepEqual(fired, ["Bootstrap", "A to B", "B to C", "B to D"]);
});
});
});
it.describe("defined objects", function (it) {
var flow, Point, Line, session;
it.beforeAll(function () {
flow = require("./rules/defined-compiled")();
Point = flow.getDefined("point");
Line = flow.getDefined("line");
});
it.beforeEach(function () {
session = flow.getSession();
});
it.should("allow creating a new defined object", function () {
var point = new Point(1, 2);
assert.equal(point.x, 1);
assert.equal(point.y, 2);
});
it("defined classes should have other classes in scope", function () {
var line = new Line();
line.addPointFromDefined(1, 2);
assert.lengthOf(line.points, 1);
var point = line.points[0];
assert.instanceOf(point, Point);
assert.equal(point.x, 1);
assert.equal(point.y, 2);
});
it("defined classes should have access to functions in scope", function () {
var line = new Line();
line.addPointWithScope(1, 2);
assert.lengthOf(line.points, 1);
var point = line.points[0];
assert.instanceOf(point, Point);
assert.equal(point.x, 1);
assert.equal(point.y, 2);
});
});
it.describe("fibonacci nools dsl", function (it) {
var flow, Fibonacci, Result;
it.beforeAll(function () {
flow = require("./rules/fibonacci-compiled")();
Fibonacci = flow.getDefined("fibonacci");
Result = flow.getDefined("result");
});
it.should("calculate fibonacci 3", function () {
var result = new Result();
return flow.getSession(new Fibonacci(3), result).match()
.then(function () {
assert.equal(result.value, 2);
});
});
it.should("calculate fibonacci 4", function () {
var result = new Result();
return flow.getSession(new Fibonacci(4), result).match().then(function () {
assert.equal(result.value, 3);
});
});
it.should("calculate fibonacci 5", function () {
var result = new Result();
return flow.getSession(new Fibonacci(5), result).match()
.then(function () {
assert.equal(result.value, 5);
});
});
it.should("calculate fibonacci 6", function () {
var result = new Result();
return flow.getSession(new Fibonacci(6), result).match()
.then(function () {
assert.equal(result.value, 8);
});
});
});
it.describe("diagnosis using dsl", function (it) {
var flow, Patient;
it.beforeAll(function () {
flow = require("./rules/diagnosis-compiled")();
Patient = flow.getDefined("patient");
});
it.should("treat properly", function () {
var session = flow.getSession();
var results = [];
session.assert(new Patient({name: "Fred", fever: "none", spots: true, rash: false, soreThroat: false, innoculated: false}));
session.assert(new Patient({name: "Joe", fever: "high", spots: false, rash: false, soreThroat: true, innoculated: false}));
session.assert(new Patient({name: "Bob", fever: "high", spots: true, rash: false, soreThroat: false, innoculated: true}));
session.assert(new Patient({name: "Tom", fever: "none", spots: false, rash: true, soreThroat: false, innoculated: false}));
session.assert(results);
//flow.print();
return session.match().then(function () {
//session.dispose();
assert.deepEqual(results, [
{"name": "Tom", "treatment": "allergyShot"},
{"name": "Bob", "treatment": "penicillin"},
{"name": "Joe", "treatment": "bedRest"},
{"name": "Fred", "treatment": "allergyShot"}
]);
});
});
});
it.describe("getFacts from action", function (it) {
var flow;
it.beforeAll(function () {
flow = require("./rules/getFacts-compiled")();
});
it.should("get all facts", function () {
var session = flow.getSession().focus("get-facts");
var facts = [
{},
1,
"hello",
true,
new Date()
];
for (var i = 0; i < facts.length; i++) {
session.assert(facts[i]);
}
var called = 0;
return session.on("get-facts", function (gottenFacts) {
assert.deepEqual(gottenFacts, facts);
called++;
}).match().then(function () {
assert.equal(called, 1);
});
});
it.should("get facts by type", function () {
var session = flow.getSession().focus("get-facts-by-type");
var facts = [
1,
"hello",
true,
new Date()
];
for (var i = 0; i < facts.length; i++) {
session.assert(facts[i]);
}
var called = 0;
return session
.on("get-facts-number", function (fact) {
assert.deepEqual(fact, [facts[0]]);
called++;
})
.on("get-facts-string", function (fact) {
assert.deepEqual(fact, [facts[1]]);
called++;
})
.on("get-facts-boolean", function (fact) {
assert.deepEqual(fact, [facts[2]]);
called++;
})
.on("get-facts-date", function (fact) {
assert.deepEqual(fact, [facts[3]]);
called++;
})
.match().then(function () {
assert.equal(called, 4);
});
});
});
});