diff --git a/lib/child-writer/index.js b/lib/child-writer/index.js index a2402903..d81b2c2f 100644 --- a/lib/child-writer/index.js +++ b/lib/child-writer/index.js @@ -126,8 +126,6 @@ exports.write = (image, drive, options) => { * })); */ const bridgeRobotMessage = (message) => { - const ROBOT_COMMAND_LOG = 'log'; - const parsedMessage = _.attempt(() => { if (robot.isMessage(message)) { return robot.parseMessage(message); @@ -136,7 +134,7 @@ exports.write = (image, drive, options) => { // Don't be so strict. If a message doesn't look like // a robot message, then make the child writer log it // for debugging purposes. - return robot.parseMessage(robot.buildMessage(ROBOT_COMMAND_LOG, { + return robot.parseMessage(robot.buildMessage(robot.COMMAND.LOG, { message })); @@ -157,9 +155,9 @@ exports.write = (image, drive, options) => { // The error object is decomposed by the CLI for serialisation // purposes. We compose it back to an `Error` here in order // to provide better encapsulation. - if (messageCommand === 'error') { + if (messageCommand === robot.COMMAND.ERROR) { emitError(robot.recomposeErrorMessage(parsedMessage)); - } else if (messageCommand === ROBOT_COMMAND_LOG) { + } else if (messageCommand === robot.COMMAND.LOG) { // If the message data is an object and it contains a // message string then log the message string only. diff --git a/lib/shared/robot/index.js b/lib/shared/robot/index.js index ee130383..03eea889 100644 --- a/lib/shared/robot/index.js +++ b/lib/shared/robot/index.js @@ -19,6 +19,33 @@ const _ = require('lodash'); const errors = require('../errors'); +/** + * @summary Robot commands + * @namespace COMMAND + * @public + */ +exports.COMMAND = { + + /** + * @property {String} ERROR + * @memberof COMMAND + * + * @description + * This command is used to represent an error message. + */ + ERROR: 'error', + + /** + * @property {String} LOG + * @memberof COMMAND + * + * @description + * This command is used to represent a log message. + */ + LOG: 'log' + +}; + /** * @summary Check whether we should emit parseable output * @function @@ -154,7 +181,7 @@ exports.parseMessage = (string) => { * > 'foo' */ exports.buildErrorMessage = (error) => { - return exports.buildMessage('error', errors.toJSON(error)); + return exports.buildMessage(exports.COMMAND.ERROR, errors.toJSON(error)); }; /** @@ -263,5 +290,5 @@ exports.printMessage = (message, data) => { * robot.log({ example: 'data' }); */ exports.log = (data) => { - exports.printMessage('log', data); + exports.printMessage(exports.COMMAND.LOG, data); }; diff --git a/tests/shared/robot.spec.js b/tests/shared/robot.spec.js index 8436a1a0..7dedf117 100644 --- a/tests/shared/robot.spec.js +++ b/tests/shared/robot.spec.js @@ -22,6 +22,23 @@ const robot = require('../../lib/shared/robot'); describe('Shared: Robot', function() { + describe('.COMMAND', function() { + + it('should be a plain object', function() { + m.chai.expect(_.isPlainObject(robot.COMMAND)).to.be.true; + }); + + it('should only contain string values', function() { + m.chai.expect(_.every(_.values(robot.COMMAND), _.isString)).to.be.true; + }); + + it('should contain only unique values', function() { + const numberOfKeys = _.size(_.keys(robot.COMMAND)); + m.chai.expect(_.size(_.uniq(_.values(robot.COMMAND)))).to.equal(numberOfKeys); + }); + + }); + describe('.isEnabled()', function() { it('should return false if ETCHER_CLI_ROBOT is not set', function() { @@ -193,7 +210,7 @@ describe('Shared: Robot', function() { const message = robot.buildErrorMessage(error); m.chai.expect(JSON.parse(message)).to.deep.equal({ - command: 'error', + command: robot.COMMAND.ERROR, data: { message: 'foo', stack: error.stack @@ -207,7 +224,7 @@ describe('Shared: Robot', function() { const message = robot.buildErrorMessage(error); m.chai.expect(JSON.parse(message)).to.deep.equal({ - command: 'error', + command: robot.COMMAND.ERROR, data: { message: 'foo', description: 'error description', @@ -222,7 +239,7 @@ describe('Shared: Robot', function() { const message = robot.buildErrorMessage(error); m.chai.expect(JSON.parse(message)).to.deep.equal({ - command: 'error', + command: robot.COMMAND.ERROR, data: { message: 'foo', code: 'MYERROR',