From b40e278172261e1cb818859163e310a858ffdf3f Mon Sep 17 00:00:00 2001 From: Matt Bittarelli Date: Tue, 18 Aug 2020 12:47:33 -0400 Subject: [PATCH 1/4] feat: updating the curent node path every time we handleEvent --- lib/models/BlueshellState.ts | 1 + lib/nodes/Base.ts | 1 + 2 files changed, 2 insertions(+) diff --git a/lib/models/BlueshellState.ts b/lib/models/BlueshellState.ts index 8688f11..de99ba7 100644 --- a/lib/models/BlueshellState.ts +++ b/lib/models/BlueshellState.ts @@ -4,4 +4,5 @@ export interface BlueshellState { errorReason?: Error; __blueshell: any; + nodePath: string; } diff --git a/lib/nodes/Base.ts b/lib/nodes/Base.ts index 7d3373f..8cb744e 100644 --- a/lib/nodes/Base.ts +++ b/lib/nodes/Base.ts @@ -56,6 +56,7 @@ export class Base implements BaseNode { * @protected */ public handleEvent(state: S, event: E): ResultCode { + state.nodePath = this.path; this._beforeEvent(state, event); const passed = this.precondition(state, event); From d75f4b7f55d49e887bd87012993496563cd3c909 Mon Sep 17 00:00:00 2001 From: Matt Bittarelli Date: Tue, 18 Aug 2020 13:05:29 -0400 Subject: [PATCH 2/4] feat: updating tests --- test/nodes/Base.test.ts | 15 +++++++++++++++ test/nodes/Constant.test.ts | 4 ++-- test/nodes/IfElse.test.ts | 1 + test/nodes/IfElseWithNodeCondition.test.ts | 1 + test/nodes/Predicate.test.ts | 4 ++-- test/nodes/RunningAction.test.ts | 1 + test/nodes/SideEffect.test.ts | 4 ++-- test/nodes/Success.test.ts | 2 +- test/nodes/decorators/Retry.test.ts | 2 ++ test/nodes/test/DroneActions.ts | 1 + test/nodes/test/RobotActions.ts | 1 + test/utils/renderTree.test.ts | 3 ++- 12 files changed, 31 insertions(+), 8 deletions(-) diff --git a/test/nodes/Base.test.ts b/test/nodes/Base.test.ts index 5525240..50e4b49 100644 --- a/test/nodes/Base.test.ts +++ b/test/nodes/Base.test.ts @@ -11,6 +11,7 @@ const Decorator = Behavior.Decorator; class TestState implements Behavior.BlueshellState { public errorReason?: Error; public __blueshell: any; + public nodePath: string = ''; } class TestAction extends Base { @@ -47,6 +48,20 @@ describe('Base', function() { assert.equal(leaf.path, 'parent2_foo_parent1_leaf'); assert.equal(parent1.path, 'parent2_foo_parent1'); }); + + it('updates the active node path', function() { + const leaf = new TestAction('leaf'); + const parent1 = new Decorator('parent1', leaf); + const parent2 = new Decorator('parent2_foo', parent1); + + const testState = new TestState(); + + assert.equal('', testState.nodePath); + + parent2.handleEvent(testState, 'test event'); + + assert.equal(testState.nodePath, leaf.path); + }); }); describe('#getNodeStorage', function() { diff --git a/test/nodes/Constant.test.ts b/test/nodes/Constant.test.ts index 941df3c..21ec366 100644 --- a/test/nodes/Constant.test.ts +++ b/test/nodes/Constant.test.ts @@ -6,14 +6,14 @@ import * as Behavior from '../../lib'; describe('Success', function() { it('Returns success, default name', function() { const success = new Behavior.Constant(rc.SUCCESS); - assert.strictEqual(success.handleEvent({__blueshell: {}}, {}), rc.SUCCESS); + assert.strictEqual(success.handleEvent({__blueshell: {}, nodePath: ''}, {}), rc.SUCCESS); assert.strictEqual(success.name, rc.SUCCESS); }); it('Returns failure, custom name', function() { const name = 'myname'; const failure = new Behavior.Constant(rc.FAILURE, name); - assert.strictEqual(failure.handleEvent({__blueshell: {}}, {}), rc.FAILURE); + assert.strictEqual(failure.handleEvent({__blueshell: {}, nodePath: ''}, {}), rc.FAILURE); assert.strictEqual(failure.name, name); }); }); diff --git a/test/nodes/IfElse.test.ts b/test/nodes/IfElse.test.ts index 3e5b89d..348af4c 100644 --- a/test/nodes/IfElse.test.ts +++ b/test/nodes/IfElse.test.ts @@ -11,6 +11,7 @@ class TestState implements Behavior.BlueshellState { public failure: boolean = false; public errorReason?: Error; public __blueshell: any; + public nodePath: string = ''; } describe('IfElse', function() { diff --git a/test/nodes/IfElseWithNodeCondition.test.ts b/test/nodes/IfElseWithNodeCondition.test.ts index 47dcdad..59e174b 100644 --- a/test/nodes/IfElseWithNodeCondition.test.ts +++ b/test/nodes/IfElseWithNodeCondition.test.ts @@ -11,6 +11,7 @@ class TestState implements Behavior.BlueshellState { public failureCount: number = 0; public errorReason?: Error; public __blueshell: any; + public nodePath: string = ''; } class TestRunningAction extends Behavior.Action { diff --git a/test/nodes/Predicate.test.ts b/test/nodes/Predicate.test.ts index 6382198..591d68d 100644 --- a/test/nodes/Predicate.test.ts +++ b/test/nodes/Predicate.test.ts @@ -6,10 +6,10 @@ import * as Behavior from '../../lib'; describe('Predicate', function() { it('Turns truth into success', function() { const p = new Behavior.Predicate('test', () => true); - assert.strictEqual(p.handleEvent({__blueshell: {}}, {}), rc.SUCCESS); + assert.strictEqual(p.handleEvent({__blueshell: {}, nodePath: ''}, {}), rc.SUCCESS); }); it('Turns false into failure', function() { const p = new Behavior.Predicate('test', () => false); - assert.strictEqual(p.handleEvent({__blueshell: {}}, {}), rc.FAILURE); + assert.strictEqual(p.handleEvent({__blueshell: {}, nodePath: ''}, {}), rc.FAILURE); }); }); diff --git a/test/nodes/RunningAction.test.ts b/test/nodes/RunningAction.test.ts index 2d1aefd..075790b 100644 --- a/test/nodes/RunningAction.test.ts +++ b/test/nodes/RunningAction.test.ts @@ -12,6 +12,7 @@ const RunningAction = Behavior.RunningAction; class TestState implements Behavior.BlueshellState { public errorReason?: Error; public __blueshell: any; + public nodePath: string = ''; } class TestAction extends RunningAction { diff --git a/test/nodes/SideEffect.test.ts b/test/nodes/SideEffect.test.ts index 2feeb81..e7d28d7 100644 --- a/test/nodes/SideEffect.test.ts +++ b/test/nodes/SideEffect.test.ts @@ -9,11 +9,11 @@ describe('SideEffect', function() { const s = new Behavior.SideEffect('set X', () => { x = 3; }); - s.handleEvent({__blueshell: {}}, {}); + s.handleEvent({__blueshell: {}, nodePath: ''}, {}); assert.strictEqual(x, 3); }); it('Always succeeds if it completes', function() { const s = new Behavior.SideEffect('set X', () => ({})); - assert.strictEqual(s.handleEvent({__blueshell: {}}, {}), rc.SUCCESS); + assert.strictEqual(s.handleEvent({__blueshell: {}, nodePath: ''}, {}), rc.SUCCESS); }); }); diff --git a/test/nodes/Success.test.ts b/test/nodes/Success.test.ts index 2c385b8..09113bc 100644 --- a/test/nodes/Success.test.ts +++ b/test/nodes/Success.test.ts @@ -6,6 +6,6 @@ import * as Behavior from '../../lib'; describe('Success', function() { it('Returns success', function() { const success = new Behavior.Success(); - assert.strictEqual(success.handleEvent({__blueshell: {}}, {}), rc.SUCCESS); + assert.strictEqual(success.handleEvent({__blueshell: {}, nodePath: ''}, {}), rc.SUCCESS); }); }); diff --git a/test/nodes/decorators/Retry.test.ts b/test/nodes/decorators/Retry.test.ts index 98830a3..ba1bfa7 100644 --- a/test/nodes/decorators/Retry.test.ts +++ b/test/nodes/decorators/Retry.test.ts @@ -31,6 +31,7 @@ describe('Retry ', function() { const counter: RetryTestState = { number: 0, __blueshell: {}, + nodePath: '', }; const countUntil = new ResultReturner(rc.FAILURE); const retry = new Behavior.decorators.Retry('RetryTest', countUntil, 2); @@ -44,6 +45,7 @@ describe('Retry ', function() { const counter: RetryTestState = { number: 0, __blueshell: {}, + nodePath: '', }; const countUntil = new ResultReturner(rc.FAILURE, 3); const retry = new Behavior.decorators.Retry('RetryTest', countUntil, -1); diff --git a/test/nodes/test/DroneActions.ts b/test/nodes/test/DroneActions.ts index d91df03..5078e50 100644 --- a/test/nodes/test/DroneActions.ts +++ b/test/nodes/test/DroneActions.ts @@ -6,6 +6,7 @@ export class DroneState implements BlueshellState { public errorReason?: Error; public __blueshell: any; + public nodePath: string = ''; constructor(debug: boolean = false) { this.flares = 0; diff --git a/test/nodes/test/RobotActions.ts b/test/nodes/test/RobotActions.ts index 312f62b..5ef4eee 100644 --- a/test/nodes/test/RobotActions.ts +++ b/test/nodes/test/RobotActions.ts @@ -13,6 +13,7 @@ class RobotState implements Behavior.BlueshellState { public errorReason?: Error; public __blueshell: any; + public nodePath: string = ''; constructor(debug: boolean = false) { this.overheated = false; diff --git a/test/utils/renderTree.test.ts b/test/utils/renderTree.test.ts index 93c4eb3..8ee6aab 100644 --- a/test/utils/renderTree.test.ts +++ b/test/utils/renderTree.test.ts @@ -69,12 +69,13 @@ describe('renderTree', function() { let state: BlueshellState = { errorReason: undefined, __blueshell: {}, + nodePath: '', }; beforeEach(function() { state = { errorReason: undefined, __blueshell: {}, - }; + nodePath: '', }; }); function runContextDepthTest( expectedNodes: number, From 14de3ef61549ea51b708b53d148a2d2751f4d0bd Mon Sep 17 00:00:00 2001 From: Matt Bittarelli Date: Tue, 18 Aug 2020 13:07:32 -0400 Subject: [PATCH 3/4] feat: lint --- test/utils/renderTree.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/utils/renderTree.test.ts b/test/utils/renderTree.test.ts index 8ee6aab..3943a1c 100644 --- a/test/utils/renderTree.test.ts +++ b/test/utils/renderTree.test.ts @@ -75,7 +75,7 @@ describe('renderTree', function() { state = { errorReason: undefined, __blueshell: {}, - nodePath: '', }; + nodePath: ''}; }); function runContextDepthTest( expectedNodes: number, From fa6b338a305fe73149aedf43f9a166e77f57ae54 Mon Sep 17 00:00:00 2001 From: Matt Bittarelli Date: Tue, 18 Aug 2020 15:55:33 -0400 Subject: [PATCH 4/4] feat: logging when the latched composite gets reset --- lib/nodes/Composite.ts | 6 +++++- package.json | 2 +- test/nodes/Composite.test.ts | 20 ++++++++++++++++++++ 3 files changed, 26 insertions(+), 2 deletions(-) diff --git a/lib/nodes/Composite.ts b/lib/nodes/Composite.ts index ff2ac18..ba3e60a 100644 --- a/lib/nodes/Composite.ts +++ b/lib/nodes/Composite.ts @@ -63,7 +63,11 @@ export abstract class Composite extends Parent coverage.lcov && codecov", "prepublish": "npm-run-all clean compile" }, diff --git a/test/nodes/Composite.test.ts b/test/nodes/Composite.test.ts index 910ae52..39a5528 100644 --- a/test/nodes/Composite.test.ts +++ b/test/nodes/Composite.test.ts @@ -3,9 +3,11 @@ */ import {assert} from 'chai'; +import * as sinon from 'sinon'; import {rc} from '../../lib'; import {RobotState, waitAi} from './test/RobotActions'; +import {LatchedSelector, Constant} from '../../dist'; describe('Composite', function() { context('#resetNodeStorage', function() { @@ -34,5 +36,23 @@ describe('Composite', function() { // Normally would be 0 assert.equal(state.cooldownLevel, 1); }); + + it('should call logging method', function() { + const state = new RobotState(false); + state.__blueshell.loggingCallback = sinon.stub(); + + const constantNode = new Constant('RUNNING', 'test success'); + const selector = new LatchedSelector('testLatchedSelector', [ + constantNode, + ]); + let res = selector.handleEvent(state, 'test event'); + assert.equal(res, rc.RUNNING); + + (constantNode).result = 'SUCCESS'; + + res = selector.handleEvent(state, 'test event 2'); + assert.equal(res, rc.SUCCESS); + assert(state.__blueshell.loggingCallback.calledOnce, 'Logging callback not called'); + }); }); });