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.
@@ -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
+ })
@@ -0,0 +1,8 @@
1
+ // vitest.config.js
2
+ import {defineConfig} from "vitest/config";
3
+
4
+ export default defineConfig({
5
+ test: {
6
+ environment: "node",
7
+ },
8
+ });