Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/chuck 5 logging #116

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/models/BlueshellState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@
export interface BlueshellState {
errorReason?: Error;
__blueshell: any;
nodePath: string;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we call this lastNodePath?

}
1 change: 1 addition & 0 deletions lib/nodes/Base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ export class Base<S extends BlueshellState, E> implements BaseNode<S, E> {
* @protected
*/
public handleEvent(state: S, event: E): ResultCode {
state.nodePath = this.path;
this._beforeEvent(state, event);

const passed = this.precondition(state, event);
Expand Down
6 changes: 5 additions & 1 deletion lib/nodes/Composite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,11 @@ export abstract class Composite<S extends BlueshellState, E> extends Parent<S, E
if (this.latched) {
const storage = this.getNodeStorage(state);
// clear latched status if the node is no longer running after processing the event
if (storage.running !== undefined && res !== resultCodes.RUNNING) {
if (res !== resultCodes.RUNNING) {
const blueshell = this._privateStorage(state);
if (blueshell.loggingCallback) {
blueshell.loggingCallback(`Resetting latched status for node: ${this.path}`);
}
storage.running = undefined;
}
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"pretest:eslint": "eslint --ext .ts,.js ${ESLINT_OPTS} .",
"test": "NODE_ENV=test nyc mocha ${MOCHA_OPTS}",
"test:no-cover": "NODE_ENV=test mocha",
"test:debug": "npm run test:no-cover -- --debug-brk test",
"test:debug": "npm run test:no-cover -- --inspect-brk test",
"coverage": "NODE_ENV=test nyc report --reporter=text-lcov > coverage.lcov && codecov",
"prepublish": "npm-run-all clean compile"
},
Expand Down
15 changes: 15 additions & 0 deletions test/nodes/Base.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<TestState, string> {
Expand Down Expand Up @@ -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() {
Expand Down
20 changes: 20 additions & 0 deletions test/nodes/Composite.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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);

(<any>constantNode).result = 'SUCCESS';

res = selector.handleEvent(state, 'test event 2');
assert.equal(res, rc.SUCCESS);
assert(state.__blueshell.loggingCallback.calledOnce, 'Logging callback not called');
});
});
});
4 changes: 2 additions & 2 deletions test/nodes/Constant.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
1 change: 1 addition & 0 deletions test/nodes/IfElse.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
1 change: 1 addition & 0 deletions test/nodes/IfElseWithNodeCondition.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<TestState, string> {
Expand Down
4 changes: 2 additions & 2 deletions test/nodes/Predicate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
1 change: 1 addition & 0 deletions test/nodes/RunningAction.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<TestState, string> {
Expand Down
4 changes: 2 additions & 2 deletions test/nodes/SideEffect.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
2 changes: 1 addition & 1 deletion test/nodes/Success.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
2 changes: 2 additions & 0 deletions test/nodes/decorators/Retry.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down
1 change: 1 addition & 0 deletions test/nodes/test/DroneActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions test/nodes/test/RobotActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion test/utils/renderTree.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down