Skip to content

Commit

Permalink
specs
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikolas Howard committed Feb 20, 2024
1 parent cfc26d2 commit 903e634
Show file tree
Hide file tree
Showing 7 changed files with 1,189 additions and 159 deletions.
26 changes: 12 additions & 14 deletions test/BehaviourTree.spec.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import { assert } from "chai";
import sinon from "sinon";

import { BehaviourTree, State } from "../src/index";
import { RootNodeDefinition } from "../src/BehaviourTreeDefinition";
import { Agent } from "../src/Agent";

import { findNode } from "./TestUtilities";

Expand Down Expand Up @@ -147,20 +145,20 @@ describe("A BehaviourTree instance", () => {
const agent = { getActionResult: () => State.SUCCEEDED };
const tree = new BehaviourTree(definition, agent);

assert.strictEqual(findNode(tree, "root", "ROOT").state, State.READY);
assert.strictEqual(findNode(tree, "sequence", "SEQUENCE").state, State.READY);
assert.strictEqual(findNode(tree, "root").state, State.READY);
assert.strictEqual(findNode(tree, "sequence").state, State.READY);
assert.strictEqual(findNode(tree, "action", "getActionResult").state, State.READY);

tree.step();

assert.strictEqual(findNode(tree, "root", "ROOT").state, State.SUCCEEDED);
assert.strictEqual(findNode(tree, "sequence", "SEQUENCE").state, State.SUCCEEDED);
assert.strictEqual(findNode(tree, "root").state, State.SUCCEEDED);
assert.strictEqual(findNode(tree, "sequence").state, State.SUCCEEDED);
assert.strictEqual(findNode(tree, "action", "getActionResult").state, State.SUCCEEDED);

tree.reset();

assert.strictEqual(findNode(tree, "root", "ROOT").state, State.READY);
assert.strictEqual(findNode(tree, "sequence", "SEQUENCE").state, State.READY);
assert.strictEqual(findNode(tree, "root").state, State.READY);
assert.strictEqual(findNode(tree, "sequence").state, State.READY);
assert.strictEqual(findNode(tree, "action", "getActionResult").state, State.READY);
});

Expand All @@ -180,20 +178,20 @@ describe("A BehaviourTree instance", () => {
const agent = { getActionResult: () => State.SUCCEEDED };
const tree = new BehaviourTree(definition, agent);

assert.strictEqual(findNode(tree, "root", "ROOT").state, State.READY);
assert.strictEqual(findNode(tree, "sequence", "SEQUENCE").state, State.READY);
assert.strictEqual(findNode(tree, "root").state, State.READY);
assert.strictEqual(findNode(tree, "sequence").state, State.READY);
assert.strictEqual(findNode(tree, "action", "getActionResult").state, State.READY);

tree.step();

assert.strictEqual(findNode(tree, "root", "ROOT").state, State.SUCCEEDED);
assert.strictEqual(findNode(tree, "sequence", "SEQUENCE").state, State.SUCCEEDED);
assert.strictEqual(findNode(tree, "root").state, State.SUCCEEDED);
assert.strictEqual(findNode(tree, "sequence").state, State.SUCCEEDED);
assert.strictEqual(findNode(tree, "action", "getActionResult").state, State.SUCCEEDED);

tree.reset();

assert.strictEqual(findNode(tree, "root", "ROOT").state, State.READY);
assert.strictEqual(findNode(tree, "sequence", "SEQUENCE").state, State.READY);
assert.strictEqual(findNode(tree, "root").state, State.READY);
assert.strictEqual(findNode(tree, "sequence").state, State.READY);
assert.strictEqual(findNode(tree, "action", "getActionResult").state, State.READY);
});
});
Expand Down
233 changes: 232 additions & 1 deletion test/nodes/composite/Parallel.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,236 @@ describe("A Parallel node", () => {
});
});

describe("when updated as part of a tree step will", () => {});
describe("when updated as part of a tree step will", () => {
describe("update each child node concurrently", () => {
it("(MDSL)", () => {
const definition = "root { parallel { action [actionRunning1] action [actionRunning2] } }";
const agent = {
actionRunning1: () => {},
actionRunning2: () => {}
};
const tree = new BehaviourTree(definition, agent);

assert.strictEqual(findNode(tree, "root").state, State.READY);
assert.strictEqual(findNode(tree, "parallel").state, State.READY);
assert.strictEqual(findNode(tree, "action", "actionRunning1").state, State.READY);
assert.strictEqual(findNode(tree, "action", "actionRunning2").state, State.READY);

tree.step();

assert.strictEqual(findNode(tree, "root").state, State.RUNNING);
assert.strictEqual(findNode(tree, "parallel").state, State.RUNNING);
assert.strictEqual(findNode(tree, "action", "actionRunning1").state, State.RUNNING);
assert.strictEqual(findNode(tree, "action", "actionRunning2").state, State.RUNNING);
});

it("(JSON)", () => {
const definition: RootNodeDefinition = {
type: "root",
child: {
type: "parallel",
children: [
{
type: "action",
call: "actionRunning1"
},
{
type: "action",
call: "actionRunning2"
}
]
}
};
const agent = {
actionRunning1: () => {},
actionRunning2: () => {}
};
const tree = new BehaviourTree(definition, agent);

assert.strictEqual(findNode(tree, "root").state, State.READY);
assert.strictEqual(findNode(tree, "parallel").state, State.READY);
assert.strictEqual(findNode(tree, "action", "actionRunning1").state, State.READY);
assert.strictEqual(findNode(tree, "action", "actionRunning2").state, State.READY);

tree.step();

assert.strictEqual(findNode(tree, "root").state, State.RUNNING);
assert.strictEqual(findNode(tree, "parallel").state, State.RUNNING);
assert.strictEqual(findNode(tree, "action", "actionRunning1").state, State.RUNNING);
assert.strictEqual(findNode(tree, "action", "actionRunning2").state, State.RUNNING);
});
});

describe("move to the FAILED state if any child node moves to the FAILED state", () => {
it("(MDSL)", () => {
const definition = "root { parallel { action [action1] action [action2] } }";
const agent = {
action1: () => {},
action2: () => {}
};
const tree = new BehaviourTree(definition, agent);

assert.strictEqual(findNode(tree, "root").state, State.READY);
assert.strictEqual(findNode(tree, "parallel").state, State.READY);
assert.strictEqual(findNode(tree, "action", "action1").state, State.READY);
assert.strictEqual(findNode(tree, "action", "action2").state, State.READY);

tree.step();

assert.strictEqual(findNode(tree, "root").state, State.RUNNING);
assert.strictEqual(findNode(tree, "parallel").state, State.RUNNING);
assert.strictEqual(findNode(tree, "action", "action1").state, State.RUNNING);
assert.strictEqual(findNode(tree, "action", "action2").state, State.RUNNING);

agent.action2 = () => State.FAILED;

tree.step();

assert.strictEqual(findNode(tree, "root").state, State.FAILED);
assert.strictEqual(findNode(tree, "parallel").state, State.FAILED);
assert.strictEqual(findNode(tree, "action", "action1").state, State.READY);
assert.strictEqual(findNode(tree, "action", "action2").state, State.FAILED);
});

it("(JSON)", () => {
const definition: RootNodeDefinition = {
type: "root",
child: {
type: "parallel",
children: [
{
type: "action",
call: "action1"
},
{
type: "action",
call: "action2"
}
]
}
};
const agent = {
action1: () => {},
action2: () => {}
};
const tree = new BehaviourTree(definition, agent);

assert.strictEqual(findNode(tree, "root").state, State.READY);
assert.strictEqual(findNode(tree, "parallel").state, State.READY);
assert.strictEqual(findNode(tree, "action", "action1").state, State.READY);
assert.strictEqual(findNode(tree, "action", "action2").state, State.READY);

tree.step();

assert.strictEqual(findNode(tree, "root").state, State.RUNNING);
assert.strictEqual(findNode(tree, "parallel").state, State.RUNNING);
assert.strictEqual(findNode(tree, "action", "action1").state, State.RUNNING);
assert.strictEqual(findNode(tree, "action", "action2").state, State.RUNNING);

agent.action2 = () => State.FAILED;

tree.step();

assert.strictEqual(findNode(tree, "root").state, State.FAILED);
assert.strictEqual(findNode(tree, "parallel").state, State.FAILED);
assert.strictEqual(findNode(tree, "action", "action1").state, State.READY);
assert.strictEqual(findNode(tree, "action", "action2").state, State.FAILED);
});
});

describe("move to the SUCCEEDED state if any child node moves to the SUCCEEDED state", () => {
it("(MDSL)", () => {
const definition = "root { parallel { action [action1] action [action2] } }";
const agent = {
action1: () => {},
action2: () => {}
};
const tree = new BehaviourTree(definition, agent);

assert.strictEqual(findNode(tree, "root").state, State.READY);
assert.strictEqual(findNode(tree, "parallel").state, State.READY);
assert.strictEqual(findNode(tree, "action", "action1").state, State.READY);
assert.strictEqual(findNode(tree, "action", "action2").state, State.READY);

tree.step();

assert.strictEqual(findNode(tree, "root").state, State.RUNNING);
assert.strictEqual(findNode(tree, "parallel").state, State.RUNNING);
assert.strictEqual(findNode(tree, "action", "action1").state, State.RUNNING);
assert.strictEqual(findNode(tree, "action", "action2").state, State.RUNNING);

agent.action1 = () => State.SUCCEEDED;

tree.step();

assert.strictEqual(findNode(tree, "root").state, State.RUNNING);
assert.strictEqual(findNode(tree, "parallel").state, State.RUNNING);
assert.strictEqual(findNode(tree, "action", "action1").state, State.SUCCEEDED);
assert.strictEqual(findNode(tree, "action", "action2").state, State.RUNNING);

agent.action2 = () => State.SUCCEEDED;

tree.step();

assert.strictEqual(findNode(tree, "root").state, State.SUCCEEDED);
assert.strictEqual(findNode(tree, "parallel").state, State.SUCCEEDED);
assert.strictEqual(findNode(tree, "action", "action1").state, State.SUCCEEDED);
assert.strictEqual(findNode(tree, "action", "action2").state, State.SUCCEEDED);
});

it("(JSON)", () => {
const definition: RootNodeDefinition = {
type: "root",
child: {
type: "parallel",
children: [
{
type: "action",
call: "action1"
},
{
type: "action",
call: "action2"
}
]
}
};
const agent = {
action1: () => {},
action2: () => {}
};
const tree = new BehaviourTree(definition, agent);

assert.strictEqual(findNode(tree, "root").state, State.READY);
assert.strictEqual(findNode(tree, "parallel").state, State.READY);
assert.strictEqual(findNode(tree, "action", "action1").state, State.READY);
assert.strictEqual(findNode(tree, "action", "action2").state, State.READY);

tree.step();

assert.strictEqual(findNode(tree, "root").state, State.RUNNING);
assert.strictEqual(findNode(tree, "parallel").state, State.RUNNING);
assert.strictEqual(findNode(tree, "action", "action1").state, State.RUNNING);
assert.strictEqual(findNode(tree, "action", "action2").state, State.RUNNING);

agent.action1 = () => State.SUCCEEDED;

tree.step();

assert.strictEqual(findNode(tree, "root").state, State.RUNNING);
assert.strictEqual(findNode(tree, "parallel").state, State.RUNNING);
assert.strictEqual(findNode(tree, "action", "action1").state, State.SUCCEEDED);
assert.strictEqual(findNode(tree, "action", "action2").state, State.RUNNING);

agent.action2 = () => State.SUCCEEDED;

tree.step();

assert.strictEqual(findNode(tree, "root").state, State.SUCCEEDED);
assert.strictEqual(findNode(tree, "parallel").state, State.SUCCEEDED);
assert.strictEqual(findNode(tree, "action", "action1").state, State.SUCCEEDED);
assert.strictEqual(findNode(tree, "action", "action2").state, State.SUCCEEDED);
});
});
});
});
Loading

0 comments on commit 903e634

Please sign in to comment.