1
0
mirror of https://github.com/node-red/node-red.git synced 2023-10-10 13:36:53 +02:00
node-red/test/red/nodes/Node_spec.js

432 lines
15 KiB
JavaScript
Raw Normal View History

2014-05-03 23:26:35 +02:00
/**
* 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.
**/
2013-10-20 22:42:01 +02:00
var should = require("should");
2014-07-30 11:39:48 +02:00
var sinon = require('sinon');
2014-07-17 09:06:30 +02:00
var RedNode = require("../../../red/nodes/Node");
2015-01-27 15:41:20 +01:00
var Log = require("../../../red/log");
2015-01-10 23:09:37 +01:00
var flows = require("../../../red/nodes/flows");
2014-07-30 11:39:48 +02:00
var comms = require('../../../red/comms');
2013-10-20 22:42:01 +02:00
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;
});
2013-10-20 22:42:01 +02:00
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;
});
2013-10-20 22:42:01 +02:00
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);
2013-10-20 22:42:01 +02:00
});
2013-10-20 22:42:01 +02:00
});
2013-10-20 22:42:01 +02:00
describe('#close', function() {
it('emits close event when closed',function(done) {
var n = new RedNode({id:'123',type:'abc'});
2014-05-14 22:46:07 +02:00
n.on('close',function() {
done();
});
var p = n.close();
should.not.exist(p);
});
2014-05-14 22:46:07 +02:00
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();
});
2013-10-20 22:42:01 +02:00
});
});
2013-10-20 22:42:01 +02:00
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);
});
2015-01-27 15:41:20 +01:00
it('writes metric info with undefined msg', function(done){
var n = new RedNode({id:'123',type:'abc'});
n.on('input',function(msg) {
(typeof msg).should.not.be.equal("undefined");
(typeof msg._msgid).should.not.be.equal("undefined");
2015-01-27 15:41:20 +01:00
done();
});
n.receive();
});
it('writes metric info with null msg', function(done){
var n = new RedNode({id:'123',type:'abc'});
n.on('input',function(msg) {
(typeof msg).should.not.be.equal("undefined");
(typeof msg._msgid).should.not.be.equal("undefined");
2015-01-27 15:41:20 +01:00
done();
});
n.receive(null);
});
2013-10-20 22:42:01 +02:00
});
2013-10-20 22:42:01 +02:00
describe('#send', function() {
2013-10-20 22:42:01 +02:00
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'});
2015-01-10 23:09:37 +01:00
var flowGet = sinon.stub(flows,"get",function(id) {
return {'n1':n1,'n2':n2}[id];
});
2013-10-20 22:42:01 +02:00
var message = {payload:"hello world"};
2013-10-20 22:42:01 +02:00
n2.on('input',function(msg) {
// msg equals message, and is not a new copy
2013-10-20 22:42:01 +02:00
should.deepEqual(msg,message);
should.strictEqual(msg,message);
2015-01-10 23:09:37 +01:00
flowGet.restore();
2013-10-20 22:42:01 +02:00
done();
});
2013-10-20 22:42:01 +02:00
n1.send(message);
});
2013-10-20 22:42:01 +02:00
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'});
2015-01-10 23:09:37 +01:00
var flowGet = sinon.stub(flows,"get",function(id) {
return {'n1':n1,'n2':n2}[id];
});
2013-10-20 22:42:01 +02:00
var messages = [
{payload:"hello world"},
{payload:"hello world again"}
];
2013-10-20 22:42:01 +02:00
var rcvdCount = 0;
2015-01-27 15:41:20 +01:00
n2.on('input',function(msg) {
if (rcvdCount === 0) {
// first msg sent, don't clone
2015-01-27 15:41:20 +01:00
should.deepEqual(msg,messages[rcvdCount]);
should.strictEqual(msg,messages[rcvdCount]);
2015-01-27 15:41:20 +01:00
rcvdCount += 1;
} else {
// second msg sent, clone
2015-01-27 15:41:20 +01:00
msg.payload.should.equal(messages[rcvdCount].payload);
should.notStrictEqual(msg,messages[rcvdCount]);
2015-01-10 23:09:37 +01:00
flowGet.restore();
2013-10-20 22:42:01 +02:00
done();
}
});
n1.send([messages]);
});
2013-10-20 22:42:01 +02:00
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'});
2015-01-10 23:09:37 +01:00
var flowGet = sinon.stub(flows,"get",function(id) {
return {'n1':n1,'n2':n2,'n3':n3,'n4':n4,'n5':n5}[id];
});
2013-10-20 22:42:01 +02:00
var messages = [
{payload:"hello world"},
null,
{payload:"hello world again"}
];
2013-10-20 22:42:01 +02:00
var rcvdCount = 0;
2015-01-27 15:41:20 +01:00
// first message sent, don't clone
// message uuids should match
2013-10-20 22:42:01 +02:00
n2.on('input',function(msg) {
should.deepEqual(msg,messages[0]);
should.strictEqual(msg,messages[0]);
2013-10-20 22:42:01 +02:00
rcvdCount += 1;
if (rcvdCount == 3) {
2015-01-10 23:09:37 +01:00
flowGet.restore();
2013-10-20 22:42:01 +02:00
done();
}
});
2013-10-20 22:42:01 +02:00
n3.on('input',function(msg) {
should.fail(null,null,"unexpected message");
});
// second message sent, clone
2015-01-27 15:41:20 +01:00
// message uuids wont match since we've cloned
2013-10-20 22:42:01 +02:00
n4.on('input',function(msg) {
2015-01-27 15:41:20 +01:00
msg.payload.should.equal(messages[2].payload);
2013-10-20 22:42:01 +02:00
should.notStrictEqual(msg,messages[2]);
rcvdCount += 1;
if (rcvdCount == 3) {
2015-01-10 23:09:37 +01:00
flowGet.restore();
2013-10-20 22:42:01 +02:00
done();
}
});
// third message sent, clone
2015-01-27 15:41:20 +01:00
// message uuids wont match since we've cloned
2013-10-20 22:42:01 +02:00
n5.on('input',function(msg) {
2015-01-27 15:41:20 +01:00
msg.payload.should.equal(messages[2].payload);
2013-10-20 22:42:01 +02:00
should.notStrictEqual(msg,messages[2]);
rcvdCount += 1;
if (rcvdCount == 3) {
2015-01-10 23:09:37 +01:00
flowGet.restore();
2013-10-20 22:42:01 +02:00
done();
}
});
2013-10-20 22:42:01 +02:00
n1.send(messages);
});
2014-07-09 08:42:09 +02:00
2014-07-09 09:00:35 +02:00
it('emits no messages', function(done) {
var n1 = new RedNode({id:'n1',type:'abc',wires:[['n2']]});
var n2 = new RedNode({id:'n2',type:'abc'});
2015-01-10 23:09:37 +01:00
var flowGet = sinon.stub(flows,"get",function(id) {
return {'n1':n1,'n2':n2}[id];
});
2014-07-09 09:00:35 +02:00
n2.on('input',function(msg) {
should.fail(null,null,"unexpected message");
});
2014-07-09 09:00:35 +02:00
setTimeout(function() {
2015-01-10 23:09:37 +01:00
flowGet.restore();
2014-07-09 09:00:35 +02:00
done();
}, 200);
2014-07-09 09:00:35 +02:00
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'});
2015-01-10 23:09:37 +01:00
var flowGet = sinon.stub(flows,"get",function(id) {
return {'n1':n1,'n2':n2}[id];
});
var messages = [
{payload:"hello world"},
{payload:"hello world again"}
];
// only one message sent, so no copy needed
n2.on('input',function(msg) {
should.deepEqual(msg,messages[1]);
should.strictEqual(msg,messages[1]);
2015-01-10 23:09:37 +01:00
flowGet.restore();
done();
});
n1.send(messages);
});
2014-07-30 11:28:25 +02:00
it('emits messages without cloning req or res', function(done) {
var n1 = new RedNode({id:'n1',type:'abc',wires:[[['n2'],['n3']]]});
2014-07-30 11:28:25 +02:00
var n2 = new RedNode({id:'n2',type:'abc'});
var n3 = new RedNode({id:'n3',type:'abc'});
2015-01-10 23:09:37 +01:00
var flowGet = sinon.stub(flows,"get",function(id) {
return {'n1':n1,'n2':n2,'n3':n3}[id];
});
2014-07-30 11:28:25 +02:00
var req = {};
var res = {};
var cloned = {};
var message = {payload: "foo", cloned: cloned, req: req, res: res};
var rcvdCount = 0;
// first message to be sent, so should not be cloned
2014-07-30 11:28:25 +02:00
n2.on('input',function(msg) {
should.deepEqual(msg, message);
msg.cloned.should.be.exactly(message.cloned);
msg.req.should.be.exactly(message.req);
msg.res.should.be.exactly(message.res);
rcvdCount += 1;
if (rcvdCount == 2) {
flowGet.restore();
done();
}
});
// second message to be sent, so should be cloned
// message uuids wont match since we've cloned
n3.on('input',function(msg) {
msg.payload.should.equal(message.payload);
2014-07-30 11:28:25 +02:00
msg.cloned.should.not.be.exactly(message.cloned);
msg.req.should.be.exactly(message.req);
msg.res.should.be.exactly(message.res);
rcvdCount += 1;
if (rcvdCount == 2) {
flowGet.restore();
done();
}
2014-07-30 11:28:25 +02:00
});
n1.send(message);
});
2015-02-03 23:02:26 +01:00
it("logs the uuid for all messages sent", function(done) {
var flowGet = sinon.stub(flows,"get",function(id) {
return {'n1':sender,'n2':receiver1,'n3':receiver2}[id];
});
var logHandler = {
messagesSent: 0,
emit: function(event, msg) {
if (msg.event == "node.abc.send" && msg.level == Log.METRIC) {
this.messagesSent++;
(typeof msg.msgid).should.not.be.equal("undefined");
flowGet.restore();
done();
}
}
};
Log.addHandler(logHandler);
var sender = new RedNode({id:'n1',type:'abc', wires:[['n2', 'n3']]});
var receiver1 = new RedNode({id:'n2',type:'abc'});
var receiver2 = new RedNode({id:'n3',type:'abc'});
sender.send({"some": "message"});
})
2013-10-20 22:42:01 +02:00
});
2014-07-09 08:42:09 +02:00
2015-01-27 15:41:20 +01:00
2014-07-09 08:42:09 +02:00
describe('#log', function() {
2015-01-27 15:41:20 +01:00
it('produces a log message', function(done) {
2014-07-09 08:42:09 +02:00
var n = new RedNode({id:'123',type:'abc'});
2015-01-27 15:41:20 +01:00
var loginfo = {};
sinon.stub(Log, 'log', function(msg) {
loginfo = msg;
2014-07-09 08:42:09 +02:00
});
n.log("a log message");
2015-02-03 23:02:26 +01:00
should.deepEqual({level:Log.INFO, id:n.id,
2015-01-27 15:41:20 +01:00
type:n.type, msg:"a log message", }, loginfo);
Log.log.restore();
done();
2014-07-09 08:42:09 +02:00
});
2015-01-27 15:41:20 +01:00
it('produces a log message with a name', function(done) {
2014-07-09 08:42:09 +02:00
var n = new RedNode({id:'123', type:'abc', name:"barney"});
2015-01-27 15:41:20 +01:00
var loginfo = {};
sinon.stub(Log, 'log', function(msg) {
loginfo = msg;
2014-07-09 08:42:09 +02:00
});
n.log("a log message");
2015-02-03 23:02:26 +01:00
should.deepEqual({level:Log.INFO, id:n.id, name: "barney",
2015-01-27 15:41:20 +01:00
type:n.type, msg:"a log message"}, loginfo);
Log.log.restore();
done();
2014-07-09 08:42:09 +02:00
});
});
describe('#warn', function() {
2015-01-27 15:41:20 +01:00
it('produces a warning message', function(done) {
2014-07-09 08:42:09 +02:00
var n = new RedNode({id:'123',type:'abc'});
2015-01-27 15:41:20 +01:00
var loginfo = {};
sinon.stub(Log, 'log', function(msg) {
loginfo = msg;
2014-07-09 08:42:09 +02:00
});
n.warn("a warning");
2015-02-03 23:02:26 +01:00
should.deepEqual({level:Log.WARN, id:n.id,
2015-01-27 15:41:20 +01:00
type:n.type, msg:"a warning"}, loginfo);
Log.log.restore();
done();
2014-07-09 08:42:09 +02:00
});
});
describe('#error', function() {
2015-01-27 15:41:20 +01:00
it('produces an error message', function(done) {
2014-07-09 08:42:09 +02:00
var n = new RedNode({id:'123',type:'abc'});
2015-01-27 15:41:20 +01:00
var loginfo = {};
sinon.stub(Log, 'log', function(msg) {
loginfo = msg;
2014-07-09 08:42:09 +02:00
});
n.error("an error message");
2015-02-03 23:02:26 +01:00
should.deepEqual({level:Log.ERROR, id:n.id,
2015-01-27 15:41:20 +01:00
type:n.type, msg:"an error message"}, loginfo);
Log.log.restore();
done();
2014-07-09 08:42:09 +02:00
});
});
2015-01-27 15:41:20 +01:00
describe('#metric', function() {
it('produces a metric message', function(done) {
var n = new RedNode({id:'123',type:'abc'});
var loginfo = {};
sinon.stub(Log, 'log', function(msg) {
loginfo = msg;
});
var msg = {payload:"foo", _msgid:"987654321"};
n.metric("test.metric",msg,"15mb");
2015-02-04 23:28:17 +01:00
should.deepEqual({value:"15mb", level:Log.METRIC, nodeid:n.id,
2015-02-03 23:02:26 +01:00
event:"node.abc.test.metric",msgid:"987654321"}, loginfo);
2015-01-27 15:41:20 +01:00
Log.log.restore();
done();
});
});
2014-07-30 11:39:48 +02:00
describe('#status', function() {
after(function() {
comms.publish.restore();
});
2014-07-30 11:39:48 +02:00
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);
});
});
2013-10-20 22:42:01 +02:00
});