2014-03-05 05:03:31 +08:00
|
|
|
"use strict";
|
2014-02-28 06:52:25 +08:00
|
|
|
|
2015-06-09 22:21:03 +08:00
|
|
|
var Driver = lib("driver"),
|
|
|
|
Adaptor = lib("adaptor"),
|
|
|
|
Robot = lib("robot"),
|
|
|
|
Logger = lib("logger");
|
2014-05-07 10:18:13 +08:00
|
|
|
|
2014-05-08 23:57:20 +08:00
|
|
|
describe("Robot", function() {
|
2014-10-01 03:22:00 +08:00
|
|
|
var work, extraFunction, robot;
|
2014-02-28 06:52:25 +08:00
|
|
|
|
2014-10-01 03:22:00 +08:00
|
|
|
beforeEach(function() {
|
|
|
|
work = spy();
|
|
|
|
extraFunction = spy();
|
2014-02-28 09:39:02 +08:00
|
|
|
|
2014-10-01 03:22:00 +08:00
|
|
|
robot = new Robot({
|
|
|
|
name: "Robby",
|
|
|
|
work: work,
|
2014-02-28 06:52:25 +08:00
|
|
|
|
2014-10-01 03:22:00 +08:00
|
|
|
extraFunction: extraFunction,
|
|
|
|
extraValue: "Hello World",
|
|
|
|
|
|
|
|
master: { master: true }
|
|
|
|
});
|
2013-10-25 05:25:42 +08:00
|
|
|
});
|
|
|
|
|
2014-03-05 05:03:31 +08:00
|
|
|
describe("constructor", function() {
|
|
|
|
describe("name", function() {
|
|
|
|
context("if provided", function() {
|
|
|
|
it("is set to the passed value", function() {
|
2014-12-16 01:37:52 +08:00
|
|
|
expect(robot.name).to.be.eql("Robby");
|
2014-03-05 05:03:31 +08:00
|
|
|
});
|
|
|
|
});
|
2014-02-28 06:52:25 +08:00
|
|
|
|
2014-03-05 05:03:31 +08:00
|
|
|
context("if not provided", function() {
|
2015-06-10 02:50:50 +08:00
|
|
|
it("is set to an incrementing name", function() {
|
|
|
|
expect(new Robot({}).name).to.match(/Robot \d/);
|
2014-03-05 05:03:31 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("sets @master to the passed Master object", function() {
|
|
|
|
expect(robot.master).to.be.eql({ master: true });
|
|
|
|
});
|
|
|
|
|
|
|
|
it("sets @connections to an empty object by default", function() {
|
|
|
|
expect(robot.connections).to.be.eql({});
|
|
|
|
});
|
2014-02-28 06:52:25 +08:00
|
|
|
|
2014-03-05 05:03:31 +08:00
|
|
|
it("sets @devices to an empty object by default", function() {
|
|
|
|
expect(robot.devices).to.be.eql({});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("sets @work to the passed work function", function() {
|
|
|
|
expect(robot.work).to.be.eql(work);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("sets other obj params as values on the robot", function() {
|
2015-03-18 05:22:14 +08:00
|
|
|
expect(robot.extraFunction).to.be.a("function");
|
2014-03-05 05:03:31 +08:00
|
|
|
expect(robot.extraValue).to.be.eql("Hello World");
|
2014-02-28 06:52:25 +08:00
|
|
|
});
|
2014-07-12 01:40:45 +08:00
|
|
|
|
|
|
|
context("if there are devices but no connections", function() {
|
2014-12-15 07:21:28 +08:00
|
|
|
it("throws an error", function() {
|
2014-07-12 01:40:45 +08:00
|
|
|
var fn = function() {
|
|
|
|
return new Robot({
|
2014-12-15 07:21:28 +08:00
|
|
|
name: "BrokenBot",
|
2014-11-27 01:42:09 +08:00
|
|
|
devices: {
|
2014-12-15 07:21:28 +08:00
|
|
|
ping: { driver: "ping" }
|
2014-11-27 01:42:09 +08:00
|
|
|
}
|
2014-07-12 01:40:45 +08:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2015-06-27 01:31:23 +08:00
|
|
|
expect(fn).to.throw(Error);
|
2014-07-12 01:40:45 +08:00
|
|
|
});
|
|
|
|
});
|
2014-08-06 09:41:57 +08:00
|
|
|
|
|
|
|
context("if no commands are provided", function() {
|
|
|
|
beforeEach(function() {
|
|
|
|
robot = new Robot({
|
2014-12-15 07:21:28 +08:00
|
|
|
name: "NewBot",
|
|
|
|
otherThings: { more: "details" },
|
2014-12-16 01:37:52 +08:00
|
|
|
sayHello: function() { return "Hello!"; }
|
2014-08-06 09:41:57 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("sets #commands to the additionally provided functions", function() {
|
2015-03-18 05:22:14 +08:00
|
|
|
expect(robot.commands.sayHello).to.be.a("function");
|
2014-08-06 09:41:57 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
context("if a commands function is provided", function() {
|
|
|
|
beforeEach(function() {
|
|
|
|
robot = new Robot({
|
2014-12-15 07:21:28 +08:00
|
|
|
name: "NewBot",
|
2014-08-06 09:41:57 +08:00
|
|
|
|
2014-12-16 01:37:52 +08:00
|
|
|
sayHello: function() { return this.name + " says hello"; },
|
2014-08-06 09:41:57 +08:00
|
|
|
|
|
|
|
commands: function() {
|
|
|
|
return {
|
|
|
|
say_hello: this.sayHello
|
2014-12-16 01:37:52 +08:00
|
|
|
};
|
2014-08-06 09:41:57 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("sets #commands to the returned object", function() {
|
|
|
|
expect(robot.commands.say_hello).to.be.eql(robot.sayHello);
|
|
|
|
});
|
|
|
|
|
|
|
|
context("if the function doesn't return an object", function() {
|
|
|
|
var fn;
|
|
|
|
beforeEach(function() {
|
|
|
|
fn = function() {
|
2014-12-16 01:37:52 +08:00
|
|
|
var bot = new Robot({
|
2014-12-15 07:21:28 +08:00
|
|
|
name: "NewBot",
|
2014-08-06 09:41:57 +08:00
|
|
|
|
|
|
|
commands: function() {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
});
|
2014-12-16 01:37:52 +08:00
|
|
|
|
|
|
|
return bot;
|
|
|
|
};
|
2014-08-06 09:41:57 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it("throws an error", function() {
|
2014-12-16 01:37:52 +08:00
|
|
|
expect(fn).to.throw(
|
|
|
|
Error,
|
2014-12-18 07:07:03 +08:00
|
|
|
"#commands must be an object or a function that returns an object"
|
2014-12-16 01:37:52 +08:00
|
|
|
);
|
2014-08-06 09:41:57 +08:00
|
|
|
});
|
2014-12-16 01:37:52 +08:00
|
|
|
});
|
2014-08-06 09:41:57 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
context("if a commands object is provided", function() {
|
|
|
|
beforeEach(function() {
|
|
|
|
robot = new Robot({
|
2014-12-15 07:21:28 +08:00
|
|
|
name: "NewBot",
|
2014-08-06 09:41:57 +08:00
|
|
|
|
2014-12-16 01:37:52 +08:00
|
|
|
sayHello: function() { return this.name + " says hello"; },
|
2014-08-06 09:41:57 +08:00
|
|
|
|
|
|
|
commands: {
|
|
|
|
say_hello: function() {}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("sets #commands to the provided object", function() {
|
2014-12-15 07:21:28 +08:00
|
|
|
expect(robot.commands.say_hello).to.be.a("function");
|
2014-08-06 09:41:57 +08:00
|
|
|
});
|
|
|
|
});
|
2014-10-28 04:40:38 +08:00
|
|
|
|
|
|
|
context("arbitrary arguments", function() {
|
|
|
|
beforeEach(function() {
|
|
|
|
robot = new Robot({
|
2014-12-15 07:21:28 +08:00
|
|
|
name: "NewBot",
|
2014-10-28 04:40:38 +08:00
|
|
|
|
2014-12-15 07:21:28 +08:00
|
|
|
hiThere: "hi there",
|
2014-10-28 04:40:38 +08:00
|
|
|
|
|
|
|
sayHi: function() {
|
2014-12-15 07:21:28 +08:00
|
|
|
return "hi";
|
2014-10-28 04:40:38 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
start: "start"
|
2014-12-16 01:37:52 +08:00
|
|
|
});
|
2014-10-28 04:40:38 +08:00
|
|
|
});
|
|
|
|
|
2014-12-16 01:37:52 +08:00
|
|
|
context("if they don't conflict with built-ins", function() {
|
|
|
|
it("passes them through", function() {
|
|
|
|
expect(robot.hiThere).to.be.eql("hi there");
|
|
|
|
expect(robot.sayHi()).to.be.eql("hi");
|
|
|
|
});
|
2014-10-28 04:40:38 +08:00
|
|
|
});
|
|
|
|
|
2014-12-16 01:37:52 +08:00
|
|
|
context("if they do conflict with built-ins", function() {
|
|
|
|
it("doesn't pass them through", function() {
|
|
|
|
expect(robot.start).to.be.a("function");
|
|
|
|
});
|
2014-10-28 04:40:38 +08:00
|
|
|
});
|
|
|
|
});
|
2014-02-28 06:52:25 +08:00
|
|
|
});
|
2014-03-01 12:27:05 +08:00
|
|
|
|
2014-06-05 07:33:20 +08:00
|
|
|
describe("all work and no play", function() {
|
|
|
|
var play = spy();
|
|
|
|
|
|
|
|
var playBot = new Robot({
|
|
|
|
play: play
|
|
|
|
});
|
|
|
|
|
2014-12-15 07:21:28 +08:00
|
|
|
it("makes Jack a dull boy", function() {
|
2014-06-05 07:33:20 +08:00
|
|
|
expect(playBot.work).to.be.eql(play);
|
2014-12-16 01:37:52 +08:00
|
|
|
});
|
2014-08-06 09:41:57 +08:00
|
|
|
});
|
2014-06-05 07:33:20 +08:00
|
|
|
|
2014-06-07 05:11:35 +08:00
|
|
|
describe("#toJSON", function() {
|
2014-05-08 23:57:20 +08:00
|
|
|
var bot = new Robot({
|
2014-11-27 01:42:09 +08:00
|
|
|
connections: {
|
2014-12-15 07:21:28 +08:00
|
|
|
loopback: { adaptor: "loopback" }
|
2014-11-27 01:42:09 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
devices: {
|
2014-12-15 07:21:28 +08:00
|
|
|
ping: { driver: "ping" }
|
2015-01-06 04:20:53 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
events: ["hello", "world"]
|
2014-03-01 12:27:05 +08:00
|
|
|
});
|
|
|
|
|
2014-06-07 05:11:35 +08:00
|
|
|
var json = bot.toJSON();
|
2014-03-01 12:27:05 +08:00
|
|
|
|
|
|
|
it("returns an object", function() {
|
2014-12-15 07:21:28 +08:00
|
|
|
expect(json).to.be.a("object");
|
2014-03-01 12:27:05 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it("contains the robot's name", function() {
|
2014-06-07 05:11:35 +08:00
|
|
|
expect(json.name).to.eql(bot.name);
|
2014-03-01 12:27:05 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it("contains the robot's commands", function() {
|
2014-08-06 09:41:57 +08:00
|
|
|
expect(json.commands).to.eql(Object.keys(bot.commands));
|
2014-03-01 12:27:05 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it("contains the robot's devices", function() {
|
2014-11-12 03:41:18 +08:00
|
|
|
expect(json.devices).to.eql([bot.devices.ping.toJSON()]);
|
2014-03-01 12:27:05 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it("contains the robot's connections", function() {
|
2014-11-12 03:41:18 +08:00
|
|
|
expect(json.connections).to.eql([bot.connections.loopback.toJSON()]);
|
2014-03-01 12:27:05 +08:00
|
|
|
});
|
2015-01-06 04:20:53 +08:00
|
|
|
|
|
|
|
it("contains the robot's events, or an empty array", function() {
|
|
|
|
expect(json.events).to.eql(["hello", "world"]);
|
|
|
|
|
2015-04-15 12:49:12 +08:00
|
|
|
bot = new Robot();
|
2015-01-06 04:20:53 +08:00
|
|
|
expect(bot.toJSON().events).to.be.eql([]);
|
|
|
|
});
|
2014-03-01 12:27:05 +08:00
|
|
|
});
|
2014-03-05 05:03:31 +08:00
|
|
|
|
2014-11-21 00:43:08 +08:00
|
|
|
describe("#connection", function() {
|
|
|
|
var opts, bot;
|
|
|
|
|
|
|
|
beforeEach(function() {
|
|
|
|
bot = new Robot();
|
2014-12-15 07:21:28 +08:00
|
|
|
opts = { adaptor: "loopback" };
|
2014-11-21 00:43:08 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it("creates and adds a new Connection", function() {
|
|
|
|
expect(bot.connections.loopback).to.be.eql(undefined);
|
2014-12-15 07:21:28 +08:00
|
|
|
bot.connection("loopback", opts);
|
2014-11-21 00:43:08 +08:00
|
|
|
expect(bot.connections.loopback).to.be.an.instanceOf(Adaptor);
|
2014-12-16 01:37:52 +08:00
|
|
|
});
|
2014-11-21 00:43:08 +08:00
|
|
|
|
2014-12-16 01:37:52 +08:00
|
|
|
it("sets connection.robot on to the Robot initializing it", function() {
|
2014-12-15 07:21:28 +08:00
|
|
|
bot.connection("loopback", opts);
|
2014-11-21 00:43:08 +08:00
|
|
|
expect(bot.connections.loopback.robot).to.be.eql(bot);
|
2014-12-16 01:37:52 +08:00
|
|
|
});
|
2014-11-21 00:43:08 +08:00
|
|
|
|
|
|
|
it("avoids name collisions", function() {
|
2014-12-15 07:21:28 +08:00
|
|
|
bot.connection("loopback", opts);
|
|
|
|
bot.connection("loopback", opts);
|
2014-12-16 01:37:52 +08:00
|
|
|
|
|
|
|
var conns = Object.keys(bot.connections);
|
|
|
|
|
|
|
|
expect(conns).to.be.eql(["loopback", "loopback-1"]);
|
2014-11-21 00:43:08 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-06-27 01:31:23 +08:00
|
|
|
describe("initRobot", function() {
|
2014-09-05 05:51:44 +08:00
|
|
|
var bot;
|
|
|
|
|
|
|
|
beforeEach(function() {
|
|
|
|
bot = new Robot();
|
|
|
|
});
|
|
|
|
|
2015-06-27 01:31:23 +08:00
|
|
|
context("when connection details are provided", function() {
|
2014-12-16 05:17:04 +08:00
|
|
|
it("creates new connections with each of the ones provided", function() {
|
|
|
|
var connections = {
|
|
|
|
loopback: { adaptor: "loopback" }
|
|
|
|
};
|
|
|
|
|
2015-06-27 01:31:23 +08:00
|
|
|
bot.initRobot({ connections: connections });
|
2014-12-16 05:17:04 +08:00
|
|
|
expect(bot.connections.loopback).to.be.instanceOf(Adaptor);
|
|
|
|
});
|
2015-01-08 02:16:32 +08:00
|
|
|
|
|
|
|
context("when the object contains device details", function() {
|
|
|
|
var opts;
|
|
|
|
|
|
|
|
beforeEach(function() {
|
|
|
|
opts = {
|
|
|
|
connections: {
|
|
|
|
loopback: {
|
|
|
|
adaptor: "loopback",
|
|
|
|
devices: {
|
|
|
|
ping: { driver: "ping", pin: 1 }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-06-27 01:31:23 +08:00
|
|
|
bot.initRobot(opts);
|
2015-01-08 02:16:32 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it("adds the devices to opts.devices", function() {
|
|
|
|
expect(opts.devices).to.be.eql({
|
|
|
|
ping: { driver: "ping", pin: 1, connection: "loopback" }
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("removes the device details from optsconnections", function() {
|
|
|
|
expect(opts.connections.devices).to.be.eql(undefined);
|
|
|
|
});
|
|
|
|
});
|
2014-12-16 05:17:04 +08:00
|
|
|
});
|
2015-06-27 01:31:23 +08:00
|
|
|
|
|
|
|
context("when device details are provided", function() {
|
|
|
|
it("creates new devices with each of the ones provided", function() {
|
|
|
|
var opts = {
|
|
|
|
connections: {
|
|
|
|
loopback: { adaptor: "loopback" }
|
|
|
|
},
|
|
|
|
|
|
|
|
devices: {
|
|
|
|
ping: { driver: "ping" }
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
bot.initRobot(opts);
|
|
|
|
expect(bot.devices.ping).to.be.instanceOf(Driver);
|
|
|
|
});
|
|
|
|
});
|
2014-03-05 05:03:31 +08:00
|
|
|
});
|
|
|
|
|
2014-11-21 00:43:08 +08:00
|
|
|
describe("#device", function() {
|
|
|
|
var opts, bot;
|
|
|
|
|
|
|
|
beforeEach(function() {
|
|
|
|
bot = new Robot();
|
2014-12-15 07:21:28 +08:00
|
|
|
opts = { driver: "ping" };
|
2014-11-21 00:43:08 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it("creates and adds a new Device", function() {
|
|
|
|
expect(bot.devices.ping).to.be.eql(undefined);
|
2014-12-15 07:21:28 +08:00
|
|
|
bot.device("ping", opts);
|
2014-11-21 00:43:08 +08:00
|
|
|
expect(bot.devices.ping).to.be.an.instanceOf(Driver);
|
2014-12-16 01:37:52 +08:00
|
|
|
});
|
2014-11-21 00:43:08 +08:00
|
|
|
|
|
|
|
it("sets @robot on the Device to be the Robot initializing it", function() {
|
2014-12-15 07:21:28 +08:00
|
|
|
bot.device("ping", opts);
|
2014-11-21 00:43:08 +08:00
|
|
|
expect(bot.devices.ping.robot).to.be.eql(bot);
|
2014-12-16 01:37:52 +08:00
|
|
|
});
|
2014-11-21 00:43:08 +08:00
|
|
|
|
|
|
|
it("avoids name collisions", function() {
|
2014-12-15 07:21:28 +08:00
|
|
|
bot.device("ping", opts);
|
|
|
|
bot.device("ping", opts);
|
|
|
|
expect(Object.keys(bot.devices)).to.be.eql(["ping", "ping-1"]);
|
2014-11-21 00:43:08 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-06-27 01:31:23 +08:00
|
|
|
describe("initRobot", function() {
|
2014-03-05 05:03:31 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
describe("#start", function() {
|
2014-07-05 00:49:29 +08:00
|
|
|
beforeEach(function() {
|
2014-12-15 07:21:28 +08:00
|
|
|
stub(robot, "startConnections").callsArg(0);
|
|
|
|
stub(robot, "startDevices").callsArg(0);
|
|
|
|
stub(robot, "emit").returns(null);
|
2014-03-05 05:03:31 +08:00
|
|
|
|
|
|
|
robot.start();
|
|
|
|
});
|
|
|
|
|
2014-07-05 00:49:29 +08:00
|
|
|
afterEach(function() {
|
2014-03-05 05:03:31 +08:00
|
|
|
robot.startConnections.restore();
|
|
|
|
robot.startDevices.restore();
|
|
|
|
robot.emit.restore();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("starts the robot's connections", function() {
|
|
|
|
expect(robot.startConnections).to.be.called;
|
|
|
|
});
|
|
|
|
|
|
|
|
it("starts the robot's devices", function() {
|
|
|
|
expect(robot.startDevices).to.be.called;
|
|
|
|
});
|
|
|
|
|
|
|
|
it("starts the robot's work", function() {
|
|
|
|
expect(robot.work).to.be.called;
|
|
|
|
});
|
|
|
|
|
2014-09-30 01:27:52 +08:00
|
|
|
it("emits the 'ready' event", function() {
|
2014-12-16 01:37:52 +08:00
|
|
|
expect(robot.emit).to.be.calledWith("ready", robot);
|
2014-03-05 05:03:31 +08:00
|
|
|
});
|
2014-09-09 05:14:11 +08:00
|
|
|
|
|
|
|
it("returns the robot", function() {
|
|
|
|
expect(robot.start()).to.be.eql(robot);
|
|
|
|
});
|
2014-03-05 05:03:31 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
describe("#startConnections", function() {
|
|
|
|
var bot;
|
|
|
|
|
|
|
|
beforeEach(function() {
|
2014-05-08 23:57:20 +08:00
|
|
|
bot = new Robot({
|
2014-11-11 02:52:55 +08:00
|
|
|
connections: {
|
2014-12-15 07:21:28 +08:00
|
|
|
alpha: { adaptor: "loopback" },
|
|
|
|
bravo: { adaptor: "loopback" }
|
2014-11-11 02:52:55 +08:00
|
|
|
}
|
2014-03-05 05:03:31 +08:00
|
|
|
});
|
|
|
|
|
2014-12-15 07:21:28 +08:00
|
|
|
stub(bot.connections.alpha, "connect").returns(true);
|
|
|
|
stub(bot.connections.bravo, "connect").returns(true);
|
2014-03-05 05:03:31 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it("runs #connect on each connection", function() {
|
|
|
|
bot.startConnections();
|
|
|
|
|
|
|
|
expect(bot.connections.alpha.connect).to.be.called;
|
|
|
|
expect(bot.connections.bravo.connect).to.be.called;
|
|
|
|
});
|
2016-03-28 09:28:14 +08:00
|
|
|
|
|
|
|
it("defines a named connection on robot for each connection", function() {
|
|
|
|
bot.startConnections();
|
|
|
|
|
2016-03-28 09:38:07 +08:00
|
|
|
expect(bot.alpha).to.be.an.instanceOf(Adaptor);
|
|
|
|
expect(bot.bravo).to.be.an.instanceOf(Adaptor);
|
2016-03-28 09:28:14 +08:00
|
|
|
});
|
2014-03-05 05:03:31 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
describe("#startDevices", function() {
|
|
|
|
var bot;
|
|
|
|
|
|
|
|
beforeEach(function() {
|
2014-05-08 23:57:20 +08:00
|
|
|
bot = new Robot({
|
2014-11-27 01:42:09 +08:00
|
|
|
connections: {
|
2014-12-15 07:21:28 +08:00
|
|
|
loopback: { adaptor: "loopback" }
|
2014-11-27 01:42:09 +08:00
|
|
|
},
|
|
|
|
|
2014-11-11 02:52:55 +08:00
|
|
|
devices: {
|
2014-12-15 07:21:28 +08:00
|
|
|
alpha: { driver: "ping" },
|
|
|
|
bravo: { driver: "ping" }
|
2014-11-11 02:52:55 +08:00
|
|
|
}
|
2014-03-05 05:03:31 +08:00
|
|
|
});
|
|
|
|
|
2014-12-15 07:21:28 +08:00
|
|
|
stub(bot.devices.alpha, "start").returns(true);
|
|
|
|
stub(bot.devices.bravo, "start").returns(true);
|
2014-03-05 05:03:31 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it("runs #start on each device", function() {
|
|
|
|
bot.startDevices();
|
|
|
|
|
|
|
|
expect(bot.devices.alpha.start).to.be.called;
|
|
|
|
expect(bot.devices.bravo.start).to.be.called;
|
|
|
|
});
|
2015-09-02 09:12:39 +08:00
|
|
|
|
|
|
|
it("runs #start on each device only once", function() {
|
|
|
|
bot.startDevices();
|
|
|
|
bot.startDevices();
|
|
|
|
|
|
|
|
expect(bot.devices.alpha.start).to.be.called.once;
|
|
|
|
expect(bot.devices.bravo.start).to.be.called.once;
|
|
|
|
});
|
|
|
|
|
|
|
|
it("runs #start on a newly added device", function() {
|
|
|
|
bot.startDevices();
|
|
|
|
bot.device("charlie", { driver: "ping" });
|
|
|
|
stub(bot.devices.charlie, "start").returns(true);
|
|
|
|
bot.startDevices();
|
|
|
|
|
|
|
|
expect(bot.devices.alpha.start).to.be.called.once;
|
|
|
|
expect(bot.devices.bravo.start).to.be.called.once;
|
|
|
|
expect(bot.devices.charlie.start).to.be.called.once;
|
|
|
|
});
|
2016-03-28 09:28:14 +08:00
|
|
|
|
|
|
|
it("defines a named device on robot for each device", function() {
|
|
|
|
bot.startDevices();
|
|
|
|
|
2016-03-28 09:38:07 +08:00
|
|
|
expect(bot.alpha).to.be.an.instanceOf(Driver);
|
|
|
|
expect(bot.bravo).to.be.an.instanceOf(Driver);
|
2016-03-28 09:28:14 +08:00
|
|
|
});
|
2014-03-05 05:03:31 +08:00
|
|
|
});
|
|
|
|
|
2014-03-12 01:44:11 +08:00
|
|
|
describe("#halt", function() {
|
2014-07-05 00:49:29 +08:00
|
|
|
var bot, device, connection;
|
|
|
|
|
|
|
|
beforeEach(function() {
|
|
|
|
bot = new Robot({
|
2014-11-27 01:42:09 +08:00
|
|
|
devices: {
|
2014-12-15 07:21:28 +08:00
|
|
|
ping: { driver: "ping" }
|
2014-11-27 01:42:09 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
connections: {
|
2014-12-15 07:21:28 +08:00
|
|
|
loopback: { adaptor: "loopback" }
|
2014-11-27 01:42:09 +08:00
|
|
|
}
|
2014-07-05 00:49:29 +08:00
|
|
|
});
|
2014-03-05 05:03:31 +08:00
|
|
|
|
2015-06-19 05:56:39 +08:00
|
|
|
bot.running = true;
|
2015-01-22 00:56:13 +08:00
|
|
|
|
2014-07-05 00:49:29 +08:00
|
|
|
device = bot.devices.ping;
|
|
|
|
connection = bot.connections.loopback;
|
2014-03-05 05:03:31 +08:00
|
|
|
|
2015-07-22 01:31:52 +08:00
|
|
|
stub(device, "halt").yields();
|
|
|
|
stub(connection, "disconnect").yields();
|
2014-03-05 05:03:31 +08:00
|
|
|
});
|
|
|
|
|
2014-03-12 01:44:11 +08:00
|
|
|
it("calls #halt on all devices and connections", function() {
|
|
|
|
bot.halt();
|
2014-03-05 05:03:31 +08:00
|
|
|
|
2014-03-12 01:44:11 +08:00
|
|
|
expect(device.halt).to.be.called;
|
2014-09-03 11:26:26 +08:00
|
|
|
expect(connection.disconnect).to.be.called;
|
2014-03-05 05:03:31 +08:00
|
|
|
});
|
2015-07-22 01:31:52 +08:00
|
|
|
|
|
|
|
context("if a subcall triggers it's callback with an error", function() {
|
|
|
|
beforeEach(function() {
|
|
|
|
bot = new Robot({
|
|
|
|
devices: {
|
|
|
|
ping: { driver: "ping" },
|
|
|
|
ping2: { driver: "ping" }
|
|
|
|
},
|
|
|
|
|
|
|
|
connections: {
|
|
|
|
loopback: { adaptor: "loopback" },
|
|
|
|
loopback2: { adaptor: "loopback" }
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
bot.running = true;
|
|
|
|
|
|
|
|
stub(bot.devices.ping, "halt").yields("error!");
|
|
|
|
stub(bot.devices.ping2, "halt").yields();
|
|
|
|
stub(bot.connections.loopback, "disconnect").yields("another err!");
|
|
|
|
stub(bot.connections.loopback2, "disconnect").yields();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("doesn't effect the rest of the shutdown", function() {
|
|
|
|
bot.halt();
|
|
|
|
|
|
|
|
expect(bot.devices.ping.halt).to.be.called;
|
|
|
|
expect(bot.devices.ping2.halt).to.be.called;
|
|
|
|
expect(bot.connections.loopback.disconnect).to.be.called;
|
|
|
|
expect(bot.connections.loopback2.disconnect).to.be.called;
|
|
|
|
});
|
|
|
|
});
|
2014-03-05 05:03:31 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
describe("#toString", function() {
|
|
|
|
it("returns basic information about the robot", function() {
|
|
|
|
expect(robot.toString()).to.be.eql("[Robot name='Robby']");
|
|
|
|
});
|
|
|
|
});
|
2015-01-21 11:38:46 +08:00
|
|
|
|
|
|
|
describe("#log", function() {
|
|
|
|
beforeEach(function() {
|
2015-07-14 04:55:42 +08:00
|
|
|
stub(Logger, "log");
|
|
|
|
robot.log("an informative message");
|
2015-01-21 11:38:46 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(function() {
|
2015-07-14 04:55:42 +08:00
|
|
|
Logger.log.restore();
|
2015-01-21 11:38:46 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it("it passes messages onto Logger, with the Robot's name", function() {
|
2015-07-14 04:55:42 +08:00
|
|
|
var nameStr = "[" + robot.name + "] - ";
|
|
|
|
expect(Logger.log).to.be.calledWith(nameStr + "an informative message");
|
2015-01-21 11:38:46 +08:00
|
|
|
});
|
|
|
|
});
|
2014-02-28 06:52:25 +08:00
|
|
|
});
|