aboutsummaryrefslogtreecommitdiffstats
path: root/dgbuilder/test/red/nodes/Node_spec.js
blob: 6ac54bd2d3bc15252665f448b947fdf6db11ed63 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/**
 * Copyright 2014 IBM Corp.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 **/
 
var should = require("should");
var sinon = require('sinon');
var RedNode = require("../../../red/nodes/Node");
var comms = require('../../../red/comms');

describe('Node', function() {
    describe('#constructor',function() {
        it('is called with an id and a type',function() {
            var n = new RedNode({id:'123',type:'abc'});
            n.should.have.property('id','123');
            n.should.have.property('type','abc');
            n.should.not.have.property('name');
            n.wires.should.be.empty;
        });
        
        it('is called with an id, a type and a name',function() {
            var n = new RedNode({id:'123',type:'abc',name:'barney'});
            n.should.have.property('id','123');
            n.should.have.property('type','abc');
            n.should.have.property('name','barney');
            n.wires.should.be.empty;
        });
        
        it('is called with an id, a type and some wires',function() {
            var n = new RedNode({id:'123',type:'abc',wires:['123','456']});
            n.should.have.property('id','123');
            n.should.have.property('type','abc');
            n.should.not.have.property('name');
            n.wires.should.have.length(2);
        });
        
    });
    
    describe('#close', function() {
        it('emits close event when closed',function(done) {
            var n = new RedNode({id:'123',type:'abc'});
            n.on('close',function() {
                done();
            });
            var p = n.close();
            should.not.exist(p);
        });
        
        it('returns a promise when provided a callback with a done parameter',function(testdone) {
            var n = new RedNode({id:'123',type:'abc'});
            n.on('close',function(done) {
                setTimeout(function() {
                    done();
                },200);
            });
            var p = n.close();
            should.exist(p);
            p.then(function() {
                testdone();
            });
        });
    });
    
    
    describe('#receive', function() {
        it('emits input event when called', function(done) {
            var n = new RedNode({id:'123',type:'abc'});
            var message = {payload:"hello world"};
            n.on('input',function(msg) {
                should.deepEqual(msg,message);
                done();
            });
            n.receive(message);
        });
    });
    
    describe('#send', function() {
            
        it('emits a single message', function(done) {
            var n1 = new RedNode({id:'n1',type:'abc',wires:[['n2']]});
            var n2 = new RedNode({id:'n2',type:'abc'});
            var message = {payload:"hello world"};
            
            n2.on('input',function(msg) {
                // msg equals message, but is a new copy
                should.deepEqual(msg,message);
                should.notStrictEqual(msg,message);
                done();
            });
            
            n1.send(message);
        });
        
        it('emits multiple messages on a single output', function(done) {
            var n1 = new RedNode({id:'n1',type:'abc',wires:[['n2']]});
            var n2 = new RedNode({id:'n2',type:'abc'});
            
            var messages = [
                {payload:"hello world"},
                {payload:"hello world again"}
            ];
            
            var rcvdCount = 0;
            
            n2.on('input',function(msg) {
                should.deepEqual(msg,messages[rcvdCount]);
                should.notStrictEqual(msg,messages[rcvdCount]);
                rcvdCount += 1;
                if (rcvdCount == 2) {
                    done();
                }
            });
            n1.send([messages]);
        });
        
        it('emits messages to multiple outputs', function(done) {
            var n1 = new RedNode({id:'n1',type:'abc',wires:[['n2'],['n3'],['n4','n5']]});
            var n2 = new RedNode({id:'n2',type:'abc'});
            var n3 = new RedNode({id:'n3',type:'abc'});
            var n4 = new RedNode({id:'n4',type:'abc'});
            var n5 = new RedNode({id:'n5',type:'abc'});
            
            var messages = [
                {payload:"hello world"},
                null,
                {payload:"hello world again"}
            ];
            
            var rcvdCount = 0;
            
            n2.on('input',function(msg) {
                should.deepEqual(msg,messages[0]);
                should.notStrictEqual(msg,messages[0]);
                rcvdCount += 1;
                if (rcvdCount == 3) {
                    done();
                }
            });
            
            n3.on('input',function(msg) {
                    should.fail(null,null,"unexpected message");
            });
            
            n4.on('input',function(msg) {
                should.deepEqual(msg,messages[2]);
                should.notStrictEqual(msg,messages[2]);
                rcvdCount += 1;
                if (rcvdCount == 3) {
                    done();
                }
            });
            
            n5.on('input',function(msg) {
                should.deepEqual(msg,messages[2]);
                should.notStrictEqual(msg,messages[2]);
                rcvdCount += 1;
                if (rcvdCount == 3) {
                    done();
                }
            });
            
            n1.send(messages);
        });

        it('emits no messages', function(done) {
            var n1 = new RedNode({id:'n1',type:'abc',wires:[['n2']]});
            var n2 = new RedNode({id:'n2',type:'abc'});

            n2.on('input',function(msg) {
                should.fail(null,null,"unexpected message");
            });
            
            setTimeout(function() {
                done();
            }, 200);
            
            n1.send();
        });

        it('emits messages ignoring non-existent nodes', function(done) {
            var n1 = new RedNode({id:'n1',type:'abc',wires:[['n9'],['n2']]});
            var n2 = new RedNode({id:'n2',type:'abc'});

            var messages = [
                {payload:"hello world"},
                {payload:"hello world again"}
            ];

            n2.on('input',function(msg) {
                should.deepEqual(msg,messages[1]);
                should.notStrictEqual(msg,messages[1]);
                done();
            });

            n1.send(messages);
        });

        it('emits messages without cloning req or res', function(done) {
            var n1 = new RedNode({id:'n1',type:'abc',wires:[['n2']]});
            var n2 = new RedNode({id:'n2',type:'abc'});

            var req = {};
            var res = {};
            var cloned = {};
            var message = {payload: "foo", cloned: cloned, req: req, res: res};

            n2.on('input',function(msg) {
                should.deepEqual(msg, message);
                msg.cloned.should.not.be.exactly(message.cloned);
                msg.req.should.be.exactly(message.req);
                msg.res.should.be.exactly(message.res);
                done();
            });

            n1.send(message);
        });

    });

    describe('#log', function() {
        it('emits a log message', function(done) {
            var n = new RedNode({id:'123',type:'abc'});
            n.on('log',function(obj) {
                should.deepEqual({level:"log", id:n.id,
                                  type:n.type, msg:"a log message"}, obj);
                done();
            });
            n.log("a log message");
        });
    });

    describe('#log', function() {
        it('emits a log message with a name', function(done) {
            var n = new RedNode({id:'123', type:'abc', name:"barney"});
            n.on('log',function(obj) {
                should.deepEqual({level:"log", id:n.id, name: "barney",
                                  type:n.type, msg:"a log message"}, obj);
                done();
            });
            n.log("a log message");
        });
    });

    describe('#warn', function() {
        it('emits a warning', function(done) {
            var n = new RedNode({id:'123',type:'abc'});
            n.on('log',function(obj) {
                should.deepEqual({level:"warn", id:n.id,
                                  type:n.type, msg:"a warning"}, obj);
                done();
            });
            n.warn("a warning");
        });
    });

    describe('#error', function() {
        it('emits an error message', function(done) {
            var n = new RedNode({id:'123',type:'abc'});
            n.on('log',function(obj) {
                should.deepEqual({level:"error", id:n.id,
                                  type:n.type, msg:"an error message"}, obj);
                done();
            });
            n.error("an error message");
        });
    });

    describe('#status', function() {
        after(function() {
            comms.publish.restore();
        });
        it('publishes status', function(done) {
            var n = new RedNode({id:'123',type:'abc'});
            var status = {fill:"green",shape:"dot",text:"connected"};
            sinon.stub(comms, 'publish', function(topic, message, retain) {
                topic.should.equal('status/123');
                message.should.equal(status);
                retain.should.be.true;
                done();
            });

            n.status(status);
        });
    });

});