cylon/spec/lib/cylon.spec.js

202 lines
5.0 KiB
JavaScript
Raw Permalink Normal View History

2014-03-06 04:46:41 +08:00
"use strict";
2013-10-25 05:25:42 +08:00
var Cylon = source("cylon"),
2014-12-16 01:37:52 +08:00
Robot = source("robot");
2015-01-08 07:59:35 +08:00
var Logger = source("logger"),
2014-12-15 07:21:28 +08:00
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() {
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
it("sets Adaptor to the Adaptor module", function() {
expect(Cylon.Adaptor).to.be.eql(Adaptor);
});
2014-02-28 05:54:04 +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
});
2015-01-08 07:59:35 +08:00
it("sets @apiInstances to an empty array by default", function() {
expect(Cylon.apiInstances).to.be.eql([]);
2014-07-16 03:27:16 +08:00
});
it("sets @robots to an empty object by default", function() {
expect(Cylon.robots).to.be.eql({});
});
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-03-06 04:46:41 +08:00
describe("#robot", function() {
2014-07-05 00:49:29 +08:00
afterEach(function() {
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" };
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']");
2015-04-15 12:49:12 +08:00
expect(Cylon.robots.Ultron).to.be.eql(robot);
2014-02-28 05:54:04 +08:00
});
it("avoids duplicating names", function() {
2014-12-16 01:37:52 +08:00
Cylon.robot({ name: "Ultron" });
Cylon.robot({ name: "Ultron" });
var bots = Object.keys(Cylon.robots);
2014-12-16 01:37:52 +08:00
expect(bots).to.be.eql(["Ultron", "Ultron-1"]);
});
2014-02-28 05:54:04 +08:00
});
2014-03-06 04:46:41 +08:00
describe("#api", function() {
2014-06-05 02:37:15 +08:00
afterEach(function() {
2015-01-08 07:59:35 +08:00
Cylon.apiInstances = [];
});
2014-02-28 09:39:02 +08:00
2015-01-08 07:59:35 +08:00
context("with a provided API server and opts", function() {
var API, opts, instance;
beforeEach(function() {
2015-01-31 05:04:14 +08:00
instance = { start: spy() };
2015-01-08 07:59:35 +08:00
opts = { https: false };
API = stub().returns(instance);
Cylon.api(API, opts);
});
it("creates an API instance", function() {
expect(API).to.be.calledWithNew;
expect(API).to.be.calledWith(opts);
});
it("passes Cylon through to the instance as opts.mcp", function() {
expect(opts.mcp).to.be.eql(Cylon);
});
it("stores the API instance in @apiInstances", function() {
expect(Cylon.apiInstances).to.be.eql([instance]);
});
2014-02-28 05:54:04 +08:00
2015-01-31 05:04:14 +08:00
it("tells the API instance to start", function() {
expect(instance.start).to.be.called;
2015-01-08 07:59:35 +08:00
});
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() };
Cylon.robots = {
2015-04-15 12:49:12 +08:00
bot1: bot1,
bot2: bot2
};
2014-03-10 10:17:40 +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() {
beforeEach(function() {
for (var c in Config) {
delete Config[c];
}
2014-12-15 07:21:28 +08:00
stub(Logger, "setup");
});
afterEach(function() {
Logger.setup.restore();
});
it("sets config variables", function() {
2014-09-16 05:46:24 +08:00
Cylon.config({ a: 1, b: 2 });
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 });
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 });
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);
});
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
});
});
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
Cylon.robots = {
2015-04-15 12:49:12 +08:00
bot1: bot1,
bot2: bot2
};
2014-03-10 10:18:45 +08:00
Cylon.halt();
2014-03-10 10:18:45 +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() {
2015-04-15 12:49:12 +08:00
var json, bot1, bot2;
2014-07-16 03:27:16 +08:00
beforeEach(function() {
2014-11-12 03:41:18 +08:00
bot1 = new Robot();
bot2 = new Robot();
2014-07-16 03:27:16 +08:00
2015-04-15 12:49:12 +08:00
Cylon.robots = { bot1: bot1, bot2: bot2 };
Cylon.commands.echo = function(arg) { return arg; };
2014-07-16 03:27:16 +08:00
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
});
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
});