dal-engine-core-js-lib-dev 0.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -0
- package/README.md +53 -0
- package/dist/index.cjs +486 -0
- package/dist/index.esm.js +484 -0
- package/eslint.config.js +83 -0
- package/package.json +35 -0
- package/rollup.config.js +16 -0
- package/src/Base.js +18 -0
- package/src/BehavioralControlGraph/BehavioralControlGraph.js +111 -0
- package/src/BehavioralControlGraph/GraphNode.js +62 -0
- package/src/DALEngine.js +73 -0
- package/src/Errors/DALEngineError.js +7 -0
- package/src/Errors/InvalidTransitionError.js +13 -0
- package/src/Errors/UnknownBehaviorError.js +12 -0
- package/src/Members/Behavior.js +70 -0
- package/src/Members/Invariant.js +69 -0
- package/src/Members/Participant.js +76 -0
- package/src/TYPES.js +11 -0
- package/tests/DALEngine.test.js +130 -0
- package/tests/Invariant.test.js +93 -0
- package/vitest.config.js +8 -0
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import {readFile, unlink, writeFile} from "fs/promises"
|
|
2
|
+
import {resolve} from "path"
|
|
3
|
+
import {describe, expect, it} from "vitest";
|
|
4
|
+
|
|
5
|
+
import {DALEngine} from "../src/DALEngine.js";
|
|
6
|
+
|
|
7
|
+
describe("invariantTests", () => {
|
|
8
|
+
it("test invariant directly through participants", async () => {
|
|
9
|
+
let d = new DALEngine({name: "Library Manager"});
|
|
10
|
+
|
|
11
|
+
// Create book participant and add invariant
|
|
12
|
+
const book = d.createParticipant({name: "book"});
|
|
13
|
+
const invariant = d.createInvariant(
|
|
14
|
+
{
|
|
15
|
+
"name": "MinLengthConstraint",
|
|
16
|
+
"rule": {
|
|
17
|
+
"type": "minLength",
|
|
18
|
+
"keys": ["value", "name"],
|
|
19
|
+
"value": 1,
|
|
20
|
+
},
|
|
21
|
+
}
|
|
22
|
+
);
|
|
23
|
+
book.addInvariant(invariant);
|
|
24
|
+
|
|
25
|
+
// Set the value and enforce invariants
|
|
26
|
+
book.setValue({
|
|
27
|
+
"uid": 1,
|
|
28
|
+
"value": {
|
|
29
|
+
"name": "",
|
|
30
|
+
},
|
|
31
|
+
});
|
|
32
|
+
book.enforceInvariants();
|
|
33
|
+
expect(book.invariantViolated).toBe(true);
|
|
34
|
+
expect(book.invariantViolationCount).toBe(1);
|
|
35
|
+
|
|
36
|
+
// Set another value and enforce invariant
|
|
37
|
+
book.setValue({
|
|
38
|
+
"uid": 1,
|
|
39
|
+
"value": {
|
|
40
|
+
"name": "Harry Potter",
|
|
41
|
+
},
|
|
42
|
+
});
|
|
43
|
+
book.enforceInvariants();
|
|
44
|
+
expect(book.invariantViolated).toBe(false);
|
|
45
|
+
expect(book.invariantViolationCount).toBe(0);
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
it("test invariant violaton of behavior", async () => {
|
|
50
|
+
let d = new DALEngine({name: "Library Manager"});
|
|
51
|
+
|
|
52
|
+
// Create book participant and add invariant to it
|
|
53
|
+
const book = d.createParticipant({name: "book"});
|
|
54
|
+
book.addInvariant(d.createInvariant(
|
|
55
|
+
{
|
|
56
|
+
"name": "MinLengthConstraint",
|
|
57
|
+
"rule": {
|
|
58
|
+
"type": "minLength",
|
|
59
|
+
"keys": ["value", "name"],
|
|
60
|
+
"value": 1,
|
|
61
|
+
},
|
|
62
|
+
}
|
|
63
|
+
));
|
|
64
|
+
|
|
65
|
+
// Create behavior and participant
|
|
66
|
+
const behavior1 = d.createBehavior({name: "AcceptBookFromUser"});
|
|
67
|
+
behavior1.addParticpant(book);
|
|
68
|
+
d.graph.addNode(behavior1, []);
|
|
69
|
+
|
|
70
|
+
// Add value that respects invariant and expect valid world state
|
|
71
|
+
behavior1.setParticipantValue("book", {
|
|
72
|
+
"uid": 1,
|
|
73
|
+
"value": {
|
|
74
|
+
"name": "Harry Potter and Chamber of Secrets",
|
|
75
|
+
},
|
|
76
|
+
})
|
|
77
|
+
expect(behavior1.invalidWorldState).toBe(false);
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
// Add value that violates invariant and expect invalid world state
|
|
81
|
+
behavior1.setParticipantValue("book", {
|
|
82
|
+
"uid": 1,
|
|
83
|
+
"value": {
|
|
84
|
+
"name": "",
|
|
85
|
+
},
|
|
86
|
+
})
|
|
87
|
+
expect(behavior1.invalidWorldState).toBe(true);
|
|
88
|
+
|
|
89
|
+
// Write to file for inspection
|
|
90
|
+
const filePath = resolve(__dirname, "./temp/invariantBehaviorTemp.json")
|
|
91
|
+
await writeFile(filePath, d.serialize())
|
|
92
|
+
});
|
|
93
|
+
})
|