UNPKG

4.84 kBJavaScriptView Raw
1var assert = require('assert');
2var mocks = require('./fixture/scout_test_mocks');
3var zetta = require('../zetta_runtime');
4var Runtime = require('../lib/runtime');
5var GoodScout = mocks.GoodScout;
6var GoodDevice = mocks.GoodDevice;
7var MockRegistry = require('./fixture/mem_registry');
8
9
10describe('Scout', function() {
11
12 it('runtime should export zetta.Scout', function() {
13 assert.ok(zetta.Scout);
14 });
15
16 describe('initialization of scout', function() {
17
18 var scout = null;
19
20 beforeEach(function(){
21 scout = new GoodScout();
22 });
23
24 it('it should implement discover prototype', function() {
25 assert.ok(scout.discover);
26 });
27
28 it('it should implement provision prototype', function() {
29 assert.ok(scout.provision);
30 });
31
32 });
33
34
35 describe('#discover()', function() {
36
37 var runtime = null;
38
39 beforeEach(function(){
40 var registry = new MockRegistry();
41 runtime = new Runtime({registry: registry});
42 });
43
44 it('it should pass arguments to device', function(done) {
45
46 var scout = new GoodScout();
47 scout.server = runtime;
48
49 runtime.on('deviceready', function(machine){
50 assert.equal(machine.foo, 'foo');
51 assert.equal(machine.bar, 'bar');
52 done();
53 });
54
55 scout.init(function(){});
56
57 });
58
59 it('it should add a new device to the registry', function(done) {
60 var scout = new GoodScout();
61 scout.server = runtime;
62
63 runtime.on('deviceready', function(machine){
64 assert.ok(machine);
65 assert.equal(machine.type, 'test');
66 assert.equal(machine.vendorId, '1234567');
67 done();
68 });
69
70 scout.init(function(){});
71 });
72 });
73
74
75 describe('#provision()', function() {
76
77 var runtime = null;
78
79 beforeEach(function(done){
80
81 GoodScout.prototype.init = function(cb){
82 var query = this.server.where({type:'test', vendorId:'1234567'});
83 var self = this;
84 this.server.find(query, function(err, results){
85 if(!err) {
86 if(results.length) {
87 self.provision(results[0], GoodDevice, 'foo1', 'foo2');
88 self.provision(results[0], GoodDevice, 'foo1', 'foo2');
89 cb();
90 }
91 } else {
92 console.log('error:');
93 console.log(err);
94 }
95 });
96 };
97
98 var registry = new MockRegistry();
99
100 registry.db.put('BC2832FD-9437-4473-A4A8-AC1D56B12C6F', {id:'BC2832FD-9437-4473-A4A8-AC1D56B12C6F',type:'test', vendorId:'1234567', foo:'foo', bar:'bar', name:'Test Device'}, {valueEncoding: 'json'}, function(err) {
101 if (err) {
102 done(err);
103 return;
104 }
105 runtime = new Runtime({registry: registry});
106 done();
107 });
108 });
109
110
111 it('it should pass arguments to device', function(done) {
112
113 var scout = new GoodScout();
114 scout.server = runtime;
115
116 runtime.on('deviceready', function(machine){
117 assert.equal(machine.foo, 'foo1');
118 assert.equal(machine.bar, 'foo2');
119 done();
120 });
121
122 scout.init(function(){});
123
124 });
125
126 it('it should initiate device with registry information', function(done) {
127 var scout = new GoodScout();
128 scout.server = runtime;
129
130 runtime.on('deviceready', function(machine){
131 assert.equal(machine.name, 'Good Device:foo1');
132 assert.equal(machine.type, 'test');
133 done();
134 });
135
136 scout.init(function(){});
137 });
138
139 it('should not return a device that has been already initialized', function(done) {
140 GoodScout.prototype.init = function(cb){
141 var query = this.server.where({type:'test', vendorId:'1234567'});
142 var self = this;
143 this.server.find(query, function(err, results){
144 if(!err) {
145 if(results.length) {
146 assert.ok(self.provision(results[0], GoodDevice, 'foo1', 'foo2'));
147 assert.ok(!self.provision(results[0], GoodDevice, 'foo1', 'foo2'));
148 done();
149 cb();
150 }
151 } else {
152 console.log('error:');
153 console.log(err);
154 }
155
156 });
157 };
158
159 var scout = new GoodScout();
160 scout.server = runtime;
161 scout.init(function(){
162 });
163 });
164
165
166 it('device init.name() should take presedence over registry value', function(done) {
167 GoodScout.prototype.init = function(cb){
168 var query = this.server.where({type:'test', vendorId:'1234567'});
169 var self = this;
170 this.server.find(query, function(err, results){
171 var device = self.provision(results[0], GoodDevice, 'foo1', 'foo2');
172 assert.equal(device.name, 'Good Device:foo1');
173 done();
174 });
175 };
176
177 var scout = new GoodScout();
178 scout.server = runtime;
179 scout.init(function(){});
180 });
181
182 });
183
184
185
186});