2014-12-15 07:21:28 +08:00
|
|
|
/* jshint expr:true */
|
2014-03-06 04:46:41 +08:00
|
|
|
"use strict";
|
2013-10-25 05:25:42 +08:00
|
|
|
|
2014-06-06 03:11:37 +08:00
|
|
|
var Cylon = source("cylon"),
|
2014-12-16 01:37:52 +08:00
|
|
|
Robot = source("robot");
|
2014-05-14 10:48:50 +08:00
|
|
|
|
2014-12-15 07:21:28 +08:00
|
|
|
var API = source("api"),
|
|
|
|
Logger = source("logger"),
|
|
|
|
Adaptor = source("adaptor"),
|
|
|
|
Driver = source("driver"),
|
|
|
|
Config = source("config");
|
2013-10-25 05:25:42 +08:00
|
|
|
|
2014-02-28 05:54:04 +08:00
|
|
|
describe("Cylon", function() {
|
2014-05-14 10:48:50 +08:00
|
|
|
describe("exports", function() {
|
|
|
|
it("sets Logger to the Logger module", function() {
|
|
|
|
expect(Cylon.Logger).to.be.eql(Logger);
|
2014-02-28 05:54:04 +08:00
|
|
|
});
|
2013-11-09 08:16:17 +08:00
|
|
|
|
2014-05-14 10:48:50 +08:00
|
|
|
it("sets Adaptor to the Adaptor module", function() {
|
|
|
|
expect(Cylon.Adaptor).to.be.eql(Adaptor);
|
2013-11-09 01:03:16 +08:00
|
|
|
});
|
2014-02-28 05:54:04 +08:00
|
|
|
|
2014-05-14 10:48:50 +08:00
|
|
|
it("sets Driver to the Driver module", function() {
|
|
|
|
expect(Cylon.Driver).to.be.eql(Driver);
|
2014-02-28 05:54:04 +08:00
|
|
|
});
|
|
|
|
|
2014-07-16 03:27:16 +08:00
|
|
|
it("sets @api_instance to null by default", function() {
|
|
|
|
expect(Cylon.api_instance).to.be.eql(null);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("sets @robots to an empty object by default", function() {
|
|
|
|
expect(Cylon.robots).to.be.eql({});
|
|
|
|
});
|
2014-05-14 10:48:50 +08:00
|
|
|
|
2014-07-16 03:27:16 +08:00
|
|
|
it("sets @robots to an empty object by default", function() {
|
|
|
|
expect(Cylon.commands).to.be.eql({});
|
|
|
|
});
|
2014-05-14 10:48:50 +08:00
|
|
|
});
|
|
|
|
|
2014-03-06 04:46:41 +08:00
|
|
|
describe("#robot", function() {
|
2014-07-05 00:49:29 +08:00
|
|
|
afterEach(function() {
|
2014-06-07 02:36:22 +08:00
|
|
|
Cylon.robots = {};
|
2013-11-09 01:28:48 +08:00
|
|
|
});
|
2014-02-28 09:39:02 +08:00
|
|
|
|
2014-03-06 04:46:41 +08:00
|
|
|
it("uses passed options to create a new Robot", function() {
|
|
|
|
var opts = { name: "Ultron" };
|
2014-05-14 10:48:50 +08:00
|
|
|
var robot = Cylon.robot(opts);
|
2014-02-28 09:39:02 +08:00
|
|
|
|
2014-12-16 01:37:52 +08:00
|
|
|
expect(robot.toString()).to.be.eql("[Robot name='Ultron']");
|
2014-12-15 07:21:28 +08:00
|
|
|
expect(Cylon.robots["Ultron"]).to.be.eql(robot);
|
2014-02-28 05:54:04 +08:00
|
|
|
});
|
2014-09-05 05:51:44 +08:00
|
|
|
|
|
|
|
it("avoids duplicating names", function() {
|
2014-12-16 01:37:52 +08:00
|
|
|
Cylon.robot({ name: "Ultron" });
|
|
|
|
Cylon.robot({ name: "Ultron" });
|
2014-09-05 05:51:44 +08:00
|
|
|
|
|
|
|
var bots = Object.keys(Cylon.robots);
|
2014-12-16 01:37:52 +08:00
|
|
|
expect(bots).to.be.eql(["Ultron", "Ultron-1"]);
|
2014-09-05 05:51:44 +08:00
|
|
|
});
|
2014-02-28 05:54:04 +08:00
|
|
|
});
|
|
|
|
|
2014-03-06 04:46:41 +08:00
|
|
|
describe("#api", function() {
|
2014-04-04 06:03:21 +08:00
|
|
|
beforeEach(function() {
|
2014-12-15 07:21:28 +08:00
|
|
|
stub(API.prototype, "listen");
|
2014-03-06 04:46:41 +08:00
|
|
|
});
|
2014-02-28 05:54:04 +08:00
|
|
|
|
2014-06-05 02:37:15 +08:00
|
|
|
afterEach(function() {
|
|
|
|
API.prototype.listen.restore();
|
2013-11-09 06:04:50 +08:00
|
|
|
});
|
2014-02-28 09:39:02 +08:00
|
|
|
|
2014-12-15 07:21:28 +08:00
|
|
|
it("creates a new API instance", function() {
|
2014-06-05 02:37:15 +08:00
|
|
|
Cylon.api();
|
|
|
|
expect(Cylon.api_instance).to.be.an.instanceOf(API);
|
2014-03-06 04:46:41 +08:00
|
|
|
});
|
2014-02-28 05:54:04 +08:00
|
|
|
|
2014-12-15 07:21:28 +08:00
|
|
|
it("passes configuration to the API constructor", function() {
|
|
|
|
Cylon.config({ api: { port: "1234" }});
|
2014-09-20 02:59:11 +08:00
|
|
|
Cylon.api();
|
2014-12-15 07:21:28 +08:00
|
|
|
expect(Cylon.api_instance.port).to.be.eql("1234");
|
2014-12-16 01:37:52 +08:00
|
|
|
});
|
2014-02-28 05:54:04 +08:00
|
|
|
});
|
2014-02-28 09:39:02 +08:00
|
|
|
|
2014-03-10 10:17:40 +08:00
|
|
|
describe("#start", function() {
|
|
|
|
it("calls #start() on all robots", function() {
|
|
|
|
var bot1 = { start: spy() },
|
|
|
|
bot2 = { start: spy() };
|
|
|
|
|
2014-06-07 02:36:22 +08:00
|
|
|
Cylon.robots = {
|
2014-12-15 07:21:28 +08:00
|
|
|
"bot1": bot1,
|
|
|
|
"bot2": bot2
|
2014-06-07 02:36:22 +08:00
|
|
|
};
|
2014-03-10 10:17:40 +08:00
|
|
|
|
2014-05-14 10:48:50 +08:00
|
|
|
Cylon.start();
|
2014-03-10 10:17:40 +08:00
|
|
|
|
|
|
|
expect(bot1.start).to.be.called;
|
|
|
|
expect(bot2.start).to.be.called;
|
|
|
|
});
|
|
|
|
});
|
2014-03-10 10:18:45 +08:00
|
|
|
|
2014-09-16 05:46:24 +08:00
|
|
|
describe("#config", function() {
|
2014-09-04 06:04:36 +08:00
|
|
|
beforeEach(function() {
|
|
|
|
for (var c in Config) {
|
|
|
|
delete Config[c];
|
|
|
|
}
|
2014-10-29 06:53:03 +08:00
|
|
|
|
2014-12-15 07:21:28 +08:00
|
|
|
stub(Logger, "setup");
|
2014-10-29 06:53:03 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(function() {
|
|
|
|
Logger.setup.restore();
|
2014-09-04 06:04:36 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it("sets config variables", function() {
|
2014-09-16 05:46:24 +08:00
|
|
|
Cylon.config({ a: 1, b: 2 });
|
2014-09-04 06:04:36 +08:00
|
|
|
expect(Config.a).to.be.eql(1);
|
|
|
|
expect(Config.b).to.be.eql(2);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("updates existing config", function() {
|
2014-09-16 05:46:24 +08:00
|
|
|
Cylon.config({ a: 1, b: 2 });
|
|
|
|
Cylon.config({ a: 3 });
|
2014-09-04 06:04:36 +08:00
|
|
|
expect(Config.a).to.be.eql(3);
|
|
|
|
expect(Config.b).to.be.eql(2);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("returns updated config", function() {
|
2014-09-16 05:46:24 +08:00
|
|
|
var config = Cylon.config({ a: 1, b: 2 });
|
2014-09-04 06:04:36 +08:00
|
|
|
expect(Config).to.be.eql(config);
|
|
|
|
});
|
|
|
|
|
2014-09-16 05:46:24 +08:00
|
|
|
it("doesn't ignores non-object arguments", function() {
|
|
|
|
var config = Cylon.config({ a: 1, b: 2 });
|
|
|
|
Cylon.config(["a", 1, "b", 2]);
|
|
|
|
Cylon.config("hello world");
|
|
|
|
expect(Config).to.be.eql(config);
|
|
|
|
});
|
2014-10-29 06:53:03 +08:00
|
|
|
|
|
|
|
it("updates the Logger setup if that changed", function() {
|
|
|
|
Cylon.config({ a: 1 });
|
|
|
|
expect(Logger.setup).to.not.be.called;
|
|
|
|
|
|
|
|
Cylon.config({ a: 1, logging: { logger: false } });
|
|
|
|
expect(Logger.setup).to.be.called;
|
2014-12-16 01:37:52 +08:00
|
|
|
});
|
2014-09-04 06:04:36 +08:00
|
|
|
});
|
|
|
|
|
2014-03-12 01:44:11 +08:00
|
|
|
describe("#halt", function() {
|
|
|
|
it("calls #halt() on all robots", function() {
|
|
|
|
var bot1 = { halt: spy() },
|
|
|
|
bot2 = { halt: spy() };
|
2014-03-10 10:18:45 +08:00
|
|
|
|
2014-06-07 02:36:22 +08:00
|
|
|
Cylon.robots = {
|
2014-12-15 07:21:28 +08:00
|
|
|
"bot1": bot1,
|
|
|
|
"bot2": bot2
|
2014-06-07 02:36:22 +08:00
|
|
|
};
|
2014-03-10 10:18:45 +08:00
|
|
|
|
2014-05-14 10:48:50 +08:00
|
|
|
Cylon.halt();
|
2014-03-10 10:18:45 +08:00
|
|
|
|
2014-03-12 01:44:11 +08:00
|
|
|
expect(bot1.halt).to.be.called;
|
|
|
|
expect(bot2.halt).to.be.called;
|
2014-03-10 10:18:45 +08:00
|
|
|
});
|
|
|
|
});
|
2014-07-16 03:27:16 +08:00
|
|
|
|
|
|
|
describe("#toJSON", function() {
|
|
|
|
var json, bot1, bot2, echo;
|
|
|
|
|
|
|
|
beforeEach(function() {
|
2014-11-12 03:41:18 +08:00
|
|
|
bot1 = new Robot();
|
|
|
|
bot2 = new Robot();
|
2014-07-16 03:27:16 +08:00
|
|
|
|
2014-12-15 07:21:28 +08:00
|
|
|
Cylon.robots = { "bot1": bot1, "bot2": bot2 };
|
2014-07-16 03:27:16 +08:00
|
|
|
Cylon.commands.echo = echo = function(arg) { return arg; };
|
|
|
|
|
|
|
|
json = Cylon.toJSON();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("contains all robots the MCP knows about", function() {
|
2014-11-12 03:41:18 +08:00
|
|
|
expect(json.robots).to.be.eql([bot1.toJSON(), bot2.toJSON()]);
|
2014-07-16 03:27:16 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it("contains an array of MCP commands", function() {
|
2014-12-15 07:21:28 +08:00
|
|
|
expect(json.commands).to.be.eql(["echo"]);
|
2014-12-16 01:37:52 +08:00
|
|
|
});
|
2015-01-06 04:20:53 +08:00
|
|
|
|
|
|
|
it("contains an array of MCP events", function() {
|
|
|
|
expect(json.events).to.be.eql(["robot_added", "robot_removed"]);
|
|
|
|
});
|
2014-07-16 03:27:16 +08:00
|
|
|
});
|
2014-02-28 05:54:04 +08:00
|
|
|
});
|