blueshell
Version:
A Behavior Tree implementation in modern Javascript
358 lines • 18.5 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const inspector_1 = require("inspector");
const chai_1 = require("chai");
const chai_as_promised_1 = __importDefault(require("chai-as-promised"));
const sinon = __importStar(require("sinon"));
const nodeManagerHelper_1 = require("../../lib/utils/nodeManagerHelper");
(0, chai_1.use)(chai_as_promised_1.default);
describe('nodeManagerHelper', function () {
describe('RuntimeWrappers', function () {
let session;
beforeEach(function () {
session = new inspector_1.Session();
});
afterEach(function () {
sinon.reset();
});
describe('getObjectIdFromRuntimeEvaluate', function () {
const key = 'foo';
it('should throw error if error present', async function () {
const testError = new Error('test error');
sinon
.stub(session, 'post')
.withArgs(
// HACK - types not well defined for post
'Runtime.evaluate', { expression: `global.breakpointMethods.get('${key}')` }, sinon.match.func)
.callsFake((_method, _params, callback) => {
callback(testError, {});
});
await chai_1.assert.isRejected(nodeManagerHelper_1.RuntimeWrappers.getObjectIdFromRuntimeEvaluate(session, key), testError.message);
});
it('should throw error if no objectId', async function () {
sinon
.stub(session, 'post')
.withArgs('Runtime.evaluate', { expression: `global.breakpointMethods.get('${key}')` }, sinon.match.func)
.callsFake((_method, _params, callback) => {
callback(null, { result: { type: '' } });
});
await chai_1.assert.isRejected(nodeManagerHelper_1.RuntimeWrappers.getObjectIdFromRuntimeEvaluate(session, key), new nodeManagerHelper_1.NoObjectIdError(key).message);
});
it('should succeed if objectId exists', async function () {
sinon
.stub(session, 'post')
.withArgs('Runtime.evaluate', { expression: `global.breakpointMethods.get('${key}')` }, sinon.match.func)
.callsFake((_method, _params, callback) => {
callback(null, { result: { type: '', objectId: 'foobar' } });
});
await chai_1.assert.isFulfilled(nodeManagerHelper_1.RuntimeWrappers.getObjectIdFromRuntimeEvaluate(session, key), 'foobar');
});
});
describe('getFunctionObjectIdFromRuntimeProperties', function () {
const objectId = 'mockObjectId';
const functionObjectId = 'mockFunctionObjectId';
it('should error if Runtime.getProperties has error', async function () {
const errMsg = 'test error';
sinon
.stub(session, 'post')
.withArgs('Runtime.getProperties', {
objectId,
}, sinon.match.func)
.callsFake((_method, _params, callback) => {
callback(new Error(errMsg), {});
});
await chai_1.assert.isRejected(nodeManagerHelper_1.RuntimeWrappers.getFunctionObjectIdFromRuntimeProperties(session, objectId), errMsg);
});
it('should error if Runtime.getProperties throw error when accessing function object id ', async function () {
sinon
.stub(session, 'post')
.withArgs('Runtime.getProperties', {
objectId,
}, sinon.match.func)
.callsFake((_method, _params, callback) => {
callback(null, { result: [] });
});
await chai_1.assert.isRejected(nodeManagerHelper_1.RuntimeWrappers.getFunctionObjectIdFromRuntimeProperties(session, objectId), new nodeManagerHelper_1.NoFunctionObjectIdError(objectId).message);
});
it('should error if Runtime.getProperties has no function object id ', async function () {
sinon
.stub(session, 'post')
.withArgs('Runtime.getProperties', {
objectId,
}, sinon.match.func)
.callsFake((_method, _params, callback) => {
callback(null, { result: [], internalProperties: [{ name: '', value: { type: '' } }] });
});
await chai_1.assert.isRejected(nodeManagerHelper_1.RuntimeWrappers.getFunctionObjectIdFromRuntimeProperties(session, objectId), new nodeManagerHelper_1.NoFunctionObjectIdError(objectId).message);
});
it('should return functionObjectId', async function () {
sinon
.stub(session, 'post')
.withArgs('Runtime.getProperties', {
objectId,
}, sinon.match.func)
.callsFake((_method, _params, callback) => {
callback(null, {
result: [],
internalProperties: [{ name: '', value: { type: '', objectId: functionObjectId } }],
});
});
await chai_1.assert.isFulfilled(nodeManagerHelper_1.RuntimeWrappers.getFunctionObjectIdFromRuntimeProperties(session, objectId), functionObjectId);
});
});
describe('setBreakpointOnFunctionCall', function () {
const functionObjectId = 'foo';
const mockBreakpointId = 'qwerty123';
let breakpointInfo;
beforeEach(function () {
breakpointInfo = {
methodInfo: {
className: 'myClass',
methodName: 'myMethod',
},
breakpointId: undefined,
breakpoints: new Map(),
};
});
it('should return reject if Debugger.setBreakpointOnFunctionCall has error', async function () {
sinon
.stub(session, 'post')
.withArgs('Debugger.setBreakpointOnFunctionCall', {
objectId: functionObjectId,
condition: '',
}, sinon.match.func)
.callsFake((_method, _params, callback) => {
callback(new Error('test error'), {});
});
await chai_1.assert.isRejected(nodeManagerHelper_1.RuntimeWrappers.setBreakpointOnFunctionCall(session, functionObjectId, '', breakpointInfo), 'test error');
});
it('should reject if Debugger.setBreakpointOnFunctionCall has empty result', async function () {
sinon
.stub(session, 'post')
.withArgs('Debugger.setBreakpointOnFunctionCall', {
objectId: functionObjectId,
condition: '',
}, sinon.match.func)
.callsFake((_method, _params, callback) => {
callback(null, undefined);
});
await chai_1.assert.isRejected(nodeManagerHelper_1.RuntimeWrappers.setBreakpointOnFunctionCall(session, functionObjectId, '', breakpointInfo), new nodeManagerHelper_1.NoBreakpointForBreakpointError(functionObjectId).message);
});
it('should reject if Debugger.setBreakpointOnFunctionCall has no breakpoint id', async function () {
sinon
.stub(session, 'post')
.withArgs('Debugger.setBreakpointOnFunctionCall', {
objectId: functionObjectId,
condition: '',
}, sinon.match.func)
.callsFake((_method, _params, callback) => {
callback(null, {});
});
await chai_1.assert.isRejected(nodeManagerHelper_1.RuntimeWrappers.setBreakpointOnFunctionCall(session, functionObjectId, '', breakpointInfo), new nodeManagerHelper_1.NoBreakpointIdForBreakpointError(functionObjectId).message);
});
it('should fulfill if breakpoint set successfully', async function () {
sinon
.stub(session, 'post')
.withArgs('Debugger.setBreakpointOnFunctionCall', {
objectId: functionObjectId,
condition: '',
}, sinon.match.func)
.callsFake((_method, _params, callback) => {
callback(null, { breakpointId: mockBreakpointId });
});
await chai_1.assert.isFulfilled(nodeManagerHelper_1.RuntimeWrappers.setBreakpointOnFunctionCall(session, functionObjectId, '', breakpointInfo));
chai_1.assert.equal(breakpointInfo.breakpointId, mockBreakpointId);
});
});
describe('removeBreakpointFromFunction', function () {
let breakpointInfo;
beforeEach(function () {
breakpointInfo = {
methodInfo: {
className: 'myClass',
methodName: 'myMethod',
},
breakpointId: 'mockBreakpointId',
breakpoints: new Map(),
};
});
it('should throw if remove breakpoint was not passed a breakpointId', async function () {
breakpointInfo.breakpointId = undefined;
await chai_1.assert.isRejected(nodeManagerHelper_1.RuntimeWrappers.removeBreakpointFromFunction(session, breakpointInfo), new nodeManagerHelper_1.BreakPointIdRequiredError().message);
});
it('should throw if remove breakpoint has error', async function () {
sinon
.stub(session, 'post')
.withArgs('Debugger.removeBreakpoint', {
breakpointId: breakpointInfo.breakpointId,
}, sinon.match.func)
.callsFake((_method, _params, callback) => {
callback(new Error('mock error'));
});
await chai_1.assert.isRejected(nodeManagerHelper_1.RuntimeWrappers.removeBreakpointFromFunction(session, breakpointInfo), 'mock error');
});
it('should resolve if remove breakpoint was successful', async function () {
sinon
.stub(session, 'post')
.withArgs('Debugger.removeBreakpoint', {
breakpointId: breakpointInfo.breakpointId,
}, sinon.match.func)
.callsFake((_method, _params, callback) => {
callback(null);
});
await chai_1.assert.isFulfilled(nodeManagerHelper_1.RuntimeWrappers.removeBreakpointFromFunction(session, breakpointInfo));
});
});
});
describe('Utils', function () {
describe('createConditionString', function () {
it('should create string with one breakpoint and no condition', function () {
const breakpointData = {
nodePath: 'foo_bar',
};
const conditionString = nodeManagerHelper_1.Utils.createConditionString([breakpointData]);
chai_1.assert.equal(conditionString, `(this.path === '${breakpointData.nodePath}')`);
});
it('should create string with one breakpoint and condition', function () {
const breakpointData = {
nodePath: 'foo_bar',
condition: 'myVar === 123',
};
const conditionString = nodeManagerHelper_1.Utils.createConditionString([breakpointData]);
chai_1.assert.equal(conditionString, `(this.path === '${breakpointData.nodePath}' && ${breakpointData.condition})`);
});
it('should create string with multiple breakpoints and no conditions', function () {
const breakpointData1 = {
nodePath: 'foo_bar',
};
const breakpointData2 = {
nodePath: 'fizz_buzz',
};
const conditionString = nodeManagerHelper_1.Utils.createConditionString([breakpointData1, breakpointData2]);
chai_1.assert.equal(conditionString, `(this.path === '${breakpointData1.nodePath}') || ` +
`(this.path === '${breakpointData2.nodePath}')`);
});
it('should create string with multiple breakpoints and one condition', function () {
const breakpointData1 = {
nodePath: 'foo_bar',
condition: 'myVar === 123',
};
const breakpointData2 = {
nodePath: 'fizz_buzz',
};
const conditionString = nodeManagerHelper_1.Utils.createConditionString([breakpointData1, breakpointData2]);
chai_1.assert.equal(conditionString, `(this.path === '${breakpointData1.nodePath}' && ${breakpointData1.condition}) || ` +
`(this.path === '${breakpointData2.nodePath}')`);
});
it('should create string with multiple breakpoints and multiple conditions', function () {
const breakpointData1 = {
nodePath: 'foo_bar',
condition: 'myVar === 123',
};
const breakpointData2 = {
nodePath: 'fizz_buzz',
condition: `otherVar === 'hello'`,
};
const conditionString = nodeManagerHelper_1.Utils.createConditionString([breakpointData1, breakpointData2]);
chai_1.assert.equal(conditionString, `(this.path === '${breakpointData1.nodePath}' && ${breakpointData1.condition}) || ` +
`(this.path === '${breakpointData2.nodePath}' && ${breakpointData2.condition})`);
});
});
describe('getMethodInfoForObject', function () {
it('should give back an alphabetized list of methods with class', function () {
class TestBaseClass {
// should work on properties
get getBaseProp() {
return 0;
}
set setBaseProp(i) {
i;
}
// should work on methods inherited from base class
inheritedMethod() {
return;
}
// should work on methods only in base class
baseMethod() {
return;
}
}
class TestChildClass extends TestBaseClass {
// should work on constructors
constructor() {
super();
}
// should work on properties
get childProp() {
return 0;
}
set childProp(i) {
i;
}
// should work on methods inherited from base class
inheritedMethod() {
return;
}
// should work on methods only in child class
childMethod() {
return;
}
// should work on private methods
privateChildMethod() {
return;
}
}
const obj = new TestChildClass();
const methodInfo = nodeManagerHelper_1.Utils.getMethodInfoForObject(obj);
chai_1.assert.deepEqual(methodInfo, [
{ methodName: 'baseMethod', className: 'TestBaseClass' },
{ methodName: 'childMethod', className: 'TestChildClass' },
{ methodName: 'constructor', className: 'TestChildClass' },
{ methodName: 'get childProp', className: 'TestChildClass' },
{ methodName: 'get getBaseProp', className: 'TestBaseClass' },
{ methodName: 'inheritedMethod', className: 'TestChildClass' },
{ methodName: 'privateChildMethod', className: 'TestChildClass' },
{ methodName: 'set childProp', className: 'TestChildClass' },
{ methodName: 'set setBaseProp', className: 'TestBaseClass' },
]);
});
});
});
});
//# sourceMappingURL=nodeManagerHelper.test.js.map