From 6fd30cc19bdd4ce06dc44f2adf3c167f3bf96210 Mon Sep 17 00:00:00 2001 From: Nikolas Howard Date: Tue, 27 Feb 2024 08:15:13 +0000 Subject: [PATCH] Root specs --- test/nodes/decorator/Root.spec.ts | 65 +++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git a/test/nodes/decorator/Root.spec.ts b/test/nodes/decorator/Root.spec.ts index 5bfb8b2..4a50959 100644 --- a/test/nodes/decorator/Root.spec.ts +++ b/test/nodes/decorator/Root.spec.ts @@ -17,6 +17,17 @@ describe("A Root node", () => { "invalid definition: a root node must have a single child node defined" ); }); + + it("(JSON)", () => { + const definition = { + type: "root" + } as any; + assert.throws( + () => new BehaviourTree(definition, {}), + Error, + "invalid definition: expected property 'child' to be defined for root node" + ); + }); }); }); @@ -33,6 +44,24 @@ describe("A Root node", () => { assert.strictEqual(findNode(tree, "root").state, State.FAILED); }); + + it("(JSON)", () => { + const definition: RootNodeDefinition = { + type: "root", + child: { + type: "condition", + call: "someCondition" + } + }; + const agent = { someCondition: () => false }; + const tree = new BehaviourTree(definition, agent); + + assert.strictEqual(findNode(tree, "root").state, State.READY); + + tree.step(); + + assert.strictEqual(findNode(tree, "root").state, State.FAILED); + }); }); describe("move to the FAILED state if the child node moves to the FAILED state", () => { @@ -47,6 +76,24 @@ describe("A Root node", () => { assert.strictEqual(findNode(tree, "root").state, State.SUCCEEDED); }); + + it("(JSON)", () => { + const definition: RootNodeDefinition = { + type: "root", + child: { + type: "condition", + call: "someCondition" + } + }; + const agent = { someCondition: () => true }; + const tree = new BehaviourTree(definition, agent); + + assert.strictEqual(findNode(tree, "root").state, State.READY); + + tree.step(); + + assert.strictEqual(findNode(tree, "root").state, State.SUCCEEDED); + }); }); describe("move to the RUNNING state if the child node does not move to the SUCCESS or FAILED state", () => { @@ -61,6 +108,24 @@ describe("A Root node", () => { assert.strictEqual(findNode(tree, "root").state, State.RUNNING); }); + + it("(JSON)", () => { + const definition: RootNodeDefinition = { + type: "root", + child: { + type: "action", + call: "someAction" + } + }; + const agent = { someAction: () => {} }; + const tree = new BehaviourTree(definition, agent); + + assert.strictEqual(findNode(tree, "root").state, State.READY); + + tree.step(); + + assert.strictEqual(findNode(tree, "root").state, State.RUNNING); + }); }); }); });