Skip to content

Commit

Permalink
Step specs
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikolas Howard committed Feb 28, 2024
1 parent dc312de commit 758adb4
Show file tree
Hide file tree
Showing 4 changed files with 154 additions and 34 deletions.
123 changes: 122 additions & 1 deletion test/attributes/callbacks/Step.spec.ts
Original file line number Diff line number Diff line change
@@ -1 +1,122 @@
describe("A Step callback node attribute", () => {});
import { assert } from "chai";
import sinon from "sinon";

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

describe("A Step callback node attribute", () => {
describe("on tree initialisation", () => {
describe("will error if no function name is defined", () => {
it("(MDSL)", () => {
const definition = "root { action [noop] step() }";
assert.throws(
() => new BehaviourTree(definition, {}),
Error,
"invalid definition: expected agent function or registered function name identifier argument for attribute"
);
});

it("(JSON)", () => {
const definition: RootNodeDefinition = {
type: "root",
child: {
type: "action",
call: "noop",
step: {} as any
}
};
assert.throws(
() => new BehaviourTree(definition, {}),
Error,
"invalid definition: expected 'call' property for attribute 'step' to be a non-empty string for 'action' node at depth '1'"
);
});
});
});

describe("when the node is updated as part of a tree step will call the step function", () => {
it("(MDSL)", () => {
const definition = `root step(onRootStep, "root") { action [someAction] step(onActionStep, "action") }`;
const agent = {
someAction: () => State.RUNNING,
onRootStep: sinon.stub(),
onActionStep: sinon.stub()
};
const tree = new BehaviourTree(definition, agent);

assert.isNotTrue(agent.onRootStep.called);
assert.isNotTrue(agent.onActionStep.called);

tree.step();

assert.isTrue(tree.isRunning());
assert.isTrue(agent.onRootStep.calledOnce);
assert.isTrue(agent.onActionStep.calledOnce);
assert.isTrue(agent.onRootStep.calledWith("root"));
assert.isTrue(agent.onActionStep.calledWith("action"));

tree.step();

assert.isTrue(tree.isRunning());
assert.isTrue(agent.onRootStep.calledTwice);
assert.isTrue(agent.onActionStep.calledTwice);

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

tree.step();

assert.isFalse(tree.isRunning());
assert.isTrue(agent.onRootStep.calledThrice);
assert.isTrue(agent.onActionStep.calledThrice);
});

it("(JSON)", () => {
const definition: RootNodeDefinition = {
type: "root",
step: {
call: "onRootStep",
args: ["root"]
},
child: {
type: "action",
step: {
call: "onActionStep",
args: ["action"]
},
call: "someAction"
}
};
const agent = {
someAction: () => State.RUNNING,
onRootStep: sinon.stub(),
onActionStep: sinon.stub()
};
const tree = new BehaviourTree(definition, agent);

assert.isNotTrue(agent.onRootStep.called);
assert.isNotTrue(agent.onActionStep.called);

tree.step();

assert.isTrue(tree.isRunning());
assert.isTrue(agent.onRootStep.calledOnce);
assert.isTrue(agent.onActionStep.calledOnce);
assert.isTrue(agent.onRootStep.calledWith("root"));
assert.isTrue(agent.onActionStep.calledWith("action"));

tree.step();

assert.isTrue(tree.isRunning());
assert.isTrue(agent.onRootStep.calledTwice);
assert.isTrue(agent.onActionStep.calledTwice);

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

tree.step();

assert.isFalse(tree.isRunning());
assert.isTrue(agent.onRootStep.calledThrice);
assert.isTrue(agent.onActionStep.calledThrice);
});
});
});
16 changes: 8 additions & 8 deletions test/nodes/decorator/Fail.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,12 @@ describe("A Fail node", () => {
const agent = { someCondition: () => true };
const tree = new BehaviourTree(definition, agent);

let node = findNode(tree, "fail", "FAIL");
let node = findNode(tree, "fail");
assert.strictEqual(node.state, State.READY);

tree.step();

node = findNode(tree, "fail", "FAIL");
node = findNode(tree, "fail");
assert.strictEqual(node.state, State.FAILED);
});

Expand All @@ -104,12 +104,12 @@ describe("A Fail node", () => {
const agent = { someCondition: () => true };
const tree = new BehaviourTree(definition, agent);

let node = findNode(tree, "fail", "FAIL");
let node = findNode(tree, "fail");
assert.strictEqual(node.state, State.READY);

tree.step();

node = findNode(tree, "fail", "FAIL");
node = findNode(tree, "fail");
assert.strictEqual(node.state, State.FAILED);
});
});
Expand All @@ -121,12 +121,12 @@ describe("A Fail node", () => {
const agent = { someAction: () => State.RUNNING };
const tree = new BehaviourTree(definition, agent);

let node = findNode(tree, "fail", "FAIL");
let node = findNode(tree, "fail");
assert.strictEqual(node.state, State.READY);

tree.step();

node = findNode(tree, "fail", "FAIL");
node = findNode(tree, "fail");
assert.strictEqual(node.state, State.RUNNING);
});

Expand All @@ -144,12 +144,12 @@ describe("A Fail node", () => {
const agent = { someAction: () => State.RUNNING };
const tree = new BehaviourTree(definition, agent);

let node = findNode(tree, "fail", "FAIL");
let node = findNode(tree, "fail");
assert.strictEqual(node.state, State.READY);

tree.step();

node = findNode(tree, "fail", "FAIL");
node = findNode(tree, "fail");
assert.strictEqual(node.state, State.RUNNING);
});
});
Expand Down
25 changes: 12 additions & 13 deletions test/nodes/decorator/Flip.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { assert } from "chai";

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

import { findNode } from "../../TestUtilities";

Expand Down Expand Up @@ -41,12 +40,12 @@ describe("A Flip node", () => {
const agent = { someCondition: () => false };
const tree = new BehaviourTree(definition, agent);

let node = findNode(tree, "flip", "FLIP");
let node = findNode(tree, "flip");
assert.strictEqual(node.state, State.READY);

tree.step();

node = findNode(tree, "flip", "FLIP");
node = findNode(tree, "flip");
assert.strictEqual(node.state, State.SUCCEEDED);
});

Expand All @@ -64,12 +63,12 @@ describe("A Flip node", () => {
const agent = { someCondition: () => false };
const tree = new BehaviourTree(definition, agent);

let node = findNode(tree, "flip", "FLIP");
let node = findNode(tree, "flip");
assert.strictEqual(node.state, State.READY);

tree.step();

node = findNode(tree, "flip", "FLIP");
node = findNode(tree, "flip");
assert.strictEqual(node.state, State.SUCCEEDED);
});
});
Expand All @@ -80,12 +79,12 @@ describe("A Flip node", () => {
const agent = { someCondition: () => true };
const tree = new BehaviourTree(definition, agent);

let node = findNode(tree, "flip", "FLIP");
let node = findNode(tree, "flip");
assert.strictEqual(node.state, State.READY);

tree.step();

node = findNode(tree, "flip", "FLIP");
node = findNode(tree, "flip");
assert.strictEqual(node.state, State.FAILED);
});

Expand All @@ -103,12 +102,12 @@ describe("A Flip node", () => {
const agent = { someCondition: () => true };
const tree = new BehaviourTree(definition, agent);

let node = findNode(tree, "flip", "FLIP");
let node = findNode(tree, "flip");
assert.strictEqual(node.state, State.READY);

tree.step();

node = findNode(tree, "flip", "FLIP");
node = findNode(tree, "flip");
assert.strictEqual(node.state, State.FAILED);
});
});
Expand All @@ -119,12 +118,12 @@ describe("A Flip node", () => {
const agent = { someAction: () => State.RUNNING };
const tree = new BehaviourTree(definition, agent);

let node = findNode(tree, "flip", "FLIP");
let node = findNode(tree, "flip");
assert.strictEqual(node.state, State.READY);

tree.step();

node = findNode(tree, "flip", "FLIP");
node = findNode(tree, "flip");
assert.strictEqual(node.state, State.RUNNING);
});

Expand All @@ -142,12 +141,12 @@ describe("A Flip node", () => {
const agent = { someAction: () => State.RUNNING };
const tree = new BehaviourTree(definition, agent);

let node = findNode(tree, "flip", "FLIP");
let node = findNode(tree, "flip");
assert.strictEqual(node.state, State.READY);

tree.step();

node = findNode(tree, "flip", "FLIP");
node = findNode(tree, "flip");
assert.strictEqual(node.state, State.RUNNING);
});
});
Expand Down
24 changes: 12 additions & 12 deletions test/nodes/decorator/Succeed.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ describe("A Succeed node", () => {
const agent = { someCondition: () => false };
const tree = new BehaviourTree(definition, agent);

let node = findNode(tree, "succeed", "SUCCEED");
let node = findNode(tree, "succeed");
assert.strictEqual(node.state, State.READY);

tree.step();

node = findNode(tree, "succeed", "SUCCEED");
node = findNode(tree, "succeed");
assert.strictEqual(node.state, State.SUCCEEDED);
});

Expand All @@ -65,12 +65,12 @@ describe("A Succeed node", () => {
const agent = { someCondition: () => false };
const tree = new BehaviourTree(definition, agent);

let node = findNode(tree, "succeed", "SUCCEED");
let node = findNode(tree, "succeed");
assert.strictEqual(node.state, State.READY);

tree.step();

node = findNode(tree, "succeed", "SUCCEED");
node = findNode(tree, "succeed");
assert.strictEqual(node.state, State.SUCCEEDED);
});
});
Expand All @@ -81,12 +81,12 @@ describe("A Succeed node", () => {
const agent = { someCondition: () => true };
const tree = new BehaviourTree(definition, agent);

let node = findNode(tree, "succeed", "SUCCEED");
let node = findNode(tree, "succeed");
assert.strictEqual(node.state, State.READY);

tree.step();

node = findNode(tree, "succeed", "SUCCEED");
node = findNode(tree, "succeed");
assert.strictEqual(node.state, State.SUCCEEDED);
});

Expand All @@ -104,12 +104,12 @@ describe("A Succeed node", () => {
const agent = { someCondition: () => true };
const tree = new BehaviourTree(definition, agent);

let node = findNode(tree, "succeed", "SUCCEED");
let node = findNode(tree, "succeed");
assert.strictEqual(node.state, State.READY);

tree.step();

node = findNode(tree, "succeed", "SUCCEED");
node = findNode(tree, "succeed");
assert.strictEqual(node.state, State.SUCCEEDED);
});
});
Expand All @@ -121,12 +121,12 @@ describe("A Succeed node", () => {
const agent = { someAction: () => State.RUNNING };
const tree = new BehaviourTree(definition, agent);

let node = findNode(tree, "succeed", "SUCCEED");
let node = findNode(tree, "succeed");
assert.strictEqual(node.state, State.READY);

tree.step();

node = findNode(tree, "succeed", "SUCCEED");
node = findNode(tree, "succeed");
assert.strictEqual(node.state, State.RUNNING);
});

Expand All @@ -144,12 +144,12 @@ describe("A Succeed node", () => {
const agent = { someAction: () => State.RUNNING };
const tree = new BehaviourTree(definition, agent);

let node = findNode(tree, "succeed", "SUCCEED");
let node = findNode(tree, "succeed");
assert.strictEqual(node.state, State.READY);

tree.step();

node = findNode(tree, "succeed", "SUCCEED");
node = findNode(tree, "succeed");
assert.strictEqual(node.state, State.RUNNING);
});
});
Expand Down

0 comments on commit 758adb4

Please sign in to comment.