dal-engine-core-js-lib-dev 0.0.4 → 0.0.5

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/src/TYPES.js CHANGED
@@ -2,9 +2,8 @@ let ENGINE_TYPES = {
2
2
  BEHAVIOR: 1,
3
3
  INVARIANT: 2,
4
4
  PARTICIPANT: 3,
5
- PRIMITIVE: 4,
6
- BEHAVIORAL_CONTROL_GRAPH: 5,
7
- GRAPH_NODE: 6,
5
+ BEHAVIORAL_CONTROL_GRAPH: 4,
6
+ GRAPH_NODE: 5,
8
7
  };
9
8
  ENGINE_TYPES = Object.freeze(ENGINE_TYPES);
10
9
 
@@ -0,0 +1,15 @@
1
+ /**
2
+ * Checks if the provided object was loaded from a file. This is determind by
3
+ * checking if the argument is an object and has a "dal_engine_uid" attribute,
4
+ * which is added to all objects when they are created and is written to file
5
+ * when the object is serialized.
6
+ *
7
+ * @param {*} obj The object to check.
8
+ * @returns {Boolean} True if the object was loaded from file, false otherwise.
9
+ */
10
+ const isLoadedFromFile = (obj) => {
11
+ return typeof obj === "object" && obj !== null
12
+ && !Array.isArray(obj) && Object.hasOwn(obj, "dal_engine_uid");
13
+ };
14
+
15
+ export default isLoadedFromFile;
@@ -1,8 +1,10 @@
1
+ // eslint-disable-next-line no-unused-vars
1
2
  import {readFile, unlink, writeFile} from "fs/promises"
2
3
  import {resolve} from "path"
3
4
  import {describe, expect, it} from "vitest";
4
5
 
5
6
  import {DALEngine} from "../src/DALEngine.js";
7
+ import BehaviorAlreadyExistsError from "../src/Errors/BehaviorAlreadyExistsError.js";
6
8
  import InvalidTransitionError from "../src/Errors/InvalidTransitionError.js";
7
9
  import MissingAttributes from "../src/Errors/MissingAttributes.js";
8
10
  import UnknownBehaviorError from "../src/Errors/UnknownBehaviorError.js";
@@ -36,6 +38,12 @@ describe("DALEngine", () => {
36
38
  expect(node.getGoToBehaviors()).toStrictEqual(goToBehaviorIds);
37
39
  });
38
40
 
41
+ it("throws when a behavior with same name is added to graph", () => {
42
+ const d = new DALEngine({name: "Library Manager"});
43
+ d.addNode("AcceptBookFromUser", []);
44
+ expect(() => {d.addNode("AcceptBookFromUser", [])}).toThrow(BehaviorAlreadyExistsError);
45
+ });
46
+
39
47
  it("find node that was added using behavior name", () => {
40
48
  const d = new DALEngine({name: "Library Manager"});
41
49
  const node = d.addNode("AcceptBookFromUser", []);
@@ -0,0 +1,80 @@
1
+ // eslint-disable-next-line no-unused-vars
2
+ import {readFile, unlink, writeFile} from "fs/promises"
3
+ import {resolve} from "path"
4
+ import {describe, expect, it} from "vitest";
5
+
6
+ import {DALEngine} from "../src/DALEngine.js";
7
+ import GraphWithNameExistsError from "../src/Errors/GraphWithNameExistsError";
8
+
9
+ describe("multiple graphs test", () => {
10
+
11
+ it("create multiple graphs and switch", async () => {
12
+ const d = new DALEngine({name: "Library Manager"});
13
+ d.createGraph("graph 1");
14
+ d.addNode("graph1behavior", []);
15
+
16
+ d.createGraph("graph 2");
17
+ d.addNode("graph2behavior", []);
18
+
19
+ d.selectGraph("graph 1");
20
+ expect(d.getNode("graph1behavior")).toBeTruthy();
21
+ expect(() => d.getNode("graph2behavior")).toThrow();
22
+ await writeFile(resolve(__dirname, "./temp/graph1.json"), d.serialize())
23
+
24
+ d.selectGraph("graph 2");
25
+ expect(d.getNode("graph2behavior")).toBeTruthy();
26
+ expect(() => d.getNode("graph1behavior")).toThrow();
27
+ await writeFile(resolve(__dirname, "./temp/graph2.json"), d.serialize())
28
+ });
29
+
30
+
31
+ it("get list of graphs", async () => {
32
+ const d = new DALEngine({name: "Library Manager"});
33
+ d.createGraph("graph 1");
34
+ d.addNode("graph1behavior", []);
35
+
36
+ d.createGraph("graph 2");
37
+ d.addNode("graph2behavior", []);
38
+
39
+ expect(d.getSelectableGraphs()).toEqual(["default graph", "graph 1", "graph 2"]);
40
+
41
+ d.selectGraph("graph 1");
42
+ expect(d.getNode("graph1behavior")).toBeTruthy();
43
+ expect(() => d.getNode("graph2behavior")).toThrow();
44
+ });
45
+
46
+ it("create graph with existing name", async () => {
47
+ const d = new DALEngine({name: "Library Manager"});
48
+ d.createGraph("graph 1");
49
+ expect(() => d.createGraph("graph 1")).toThrow(GraphWithNameExistsError);
50
+ });
51
+
52
+ it("test that graph is removed", async () => {
53
+ const d = new DALEngine({name: "Library Manager"});
54
+ d.createGraph("graph 1");
55
+ d.addNode("graph1behavior", []);
56
+ d.createGraph("graph 2");
57
+ d.addNode("graph2behavior", []);
58
+ expect(d.getSelectableGraphs()).toEqual(["default graph", "graph 1", "graph 2"]);
59
+ d.removeGraph("graph 1");
60
+
61
+ // After removing a graph, the first graph in the list is active.
62
+ // If there are no graphs, a default graph is created and set as active.
63
+ expect(d.getSelectableGraphs()).toEqual(["default graph", "graph 2"]);
64
+ expect(d.graph.name).toBe("default graph");
65
+ });
66
+
67
+ it("serialize and deserialize", async () => {
68
+ const d = new DALEngine({name: "Library Manager"});
69
+ d.createGraph("graph 1");
70
+ d.addNode("graph1behavior", []);
71
+
72
+ d.createGraph("graph 2");
73
+ d.addNode("graph2behavior", []);
74
+
75
+ await writeFile(resolve(__dirname, "./temp/graphs.json"), d.serialize());
76
+
77
+ d.deserialize(await readFile(resolve(__dirname, "./temp/graphs.json"), "utf-8"));
78
+ expect(d.getSelectableGraphs()).toEqual(["default graph", "graph 1", "graph 2"]);
79
+ });
80
+ });
@@ -1,3 +1,4 @@
1
+ // eslint-disable-next-line no-unused-vars
1
2
  import {readFile, unlink, writeFile} from "fs/promises"
2
3
  import {resolve} from "path"
3
4
  import {describe, expect, it} from "vitest";